batch.c 5.85 KB
Newer Older
Sven Neumann's avatar
Sven Neumann committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
18
19
#include "config.h"

Elliot Lee's avatar
Elliot Lee committed
20
21
22
23
24
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
25
#ifdef HAVE_UNISTD_H
Elliot Lee's avatar
Elliot Lee committed
26
#include <unistd.h>
27
#endif
Elliot Lee's avatar
Elliot Lee committed
28
29
30
31
32
33
34
35
36
37

#include "appenv.h"
#include "app_procs.h"
#include "batch.h"
#include "procedural_db.h"

static void batch_run_cmd  (char              *cmd);
static void batch_read     (gpointer           data,
			    gint               source,
			    GdkInputCondition  condition);
Marc Lehmann's avatar
Marc Lehmann committed
38
39
40
static void batch_pserver  (int                run_mode,
                            int                flags,
                            int                extra);
Elliot Lee's avatar
Elliot Lee committed
41
42


43
44
static ProcRecord *eval_proc;

Elliot Lee's avatar
Elliot Lee committed
45
46
47
48
49
50
51
void
batch_init ()
{
  extern char **batch_cmds;

  int read_from_stdin;
  int i;
Marc Lehmann's avatar
Marc Lehmann committed
52
  int perl_server_already_running = 0;
Elliot Lee's avatar
Elliot Lee committed
53

54
55
  eval_proc = procedural_db_lookup ("extension_script_fu_eval");

Elliot Lee's avatar
Elliot Lee committed
56
57
58
  read_from_stdin = FALSE;
  for (i = 0; batch_cmds[i]; i++)
    {
Marc Lehmann's avatar
Marc Lehmann committed
59
60
61

      /* until --batch-interp=xxx or something similar is implemented 
       * and gimp-1.0 is not extinct use a shortcut to speed up starting the
Marc Lehmann's avatar
Marc Lehmann committed
62
       * perl-server tremendously. This is also fully compatible with 1.0.
Marc Lehmann's avatar
Marc Lehmann committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
       */
      {
        int run_mode, flags, extra;

        if (sscanf (batch_cmds[i], "(extension%*[-_]perl%*[-_]server %i %i %i)", &run_mode, &flags, &extra) == 3)
          {
            if (!perl_server_already_running)
             {
               batch_pserver (run_mode, flags, extra);
               perl_server_already_running = 1;
             }
            continue;
          }
      }
      
      if (!eval_proc)
        {
Sven Neumann's avatar
Sven Neumann committed
80
          g_message ("script-fu not available: batch mode disabled\n");
Marc Lehmann's avatar
Marc Lehmann committed
81
82
83
          return;
        }

Elliot Lee's avatar
Elliot Lee committed
84
85
86
87
      if (strcmp (batch_cmds[i], "-") == 0)
	{
	  if (!read_from_stdin)
	    {
88
#ifndef G_OS_WIN32 /* for now */
Sven Neumann's avatar
Sven Neumann committed
89
	      g_print ("reading batch commands from stdin\n");
Elliot Lee's avatar
Elliot Lee committed
90
91
	      gdk_input_add (STDIN_FILENO, GDK_INPUT_READ, batch_read, NULL);
	      read_from_stdin = TRUE;
92
93
94
#else
	      g_error ("Batch mode from standard input not implemented on Win32");
#endif
Tor Lillqvist's avatar
Tor Lillqvist committed
95
	    }
Elliot Lee's avatar
Elliot Lee committed
96
97
98
	}
      else
	{
99
	  batch_run_cmd (batch_cmds[i]);
Elliot Lee's avatar
Elliot Lee committed
100
101
102
103
104
105
106
107
108
109
110
111
	}
    }
}


static void
batch_run_cmd (char *cmd)
{
  Argument *args;
  Argument *vals;
  int i;

112
  if (g_strcasecmp (cmd, "(gimp-quit 0)") == 0)
Elliot Lee's avatar
Elliot Lee committed
113
    {
114
      app_exit (FALSE);
115
      exit (0);
Elliot Lee's avatar
Elliot Lee committed
116
117
    }

118
119
120
  args = g_new0 (Argument, eval_proc->num_args);
  for (i = 0; i < eval_proc->num_args; i++)
    args[i].arg_type = eval_proc->args[i].arg_type;
Elliot Lee's avatar
Elliot Lee committed
121

122
123
  args[0].value.pdb_int = 1;
  args[1].value.pdb_pointer = cmd;
Elliot Lee's avatar
Elliot Lee committed
124

125
  vals = procedural_db_execute ("extension_script_fu_eval", args);
Elliot Lee's avatar
Elliot Lee committed
126
127
128
  switch (vals[0].value.pdb_int)
    {
    case PDB_EXECUTION_ERROR:
Sven Neumann's avatar
Sven Neumann committed
129
      g_print ("batch command: experienced an execution error.\n");
Elliot Lee's avatar
Elliot Lee committed
130
131
      break;
    case PDB_CALLING_ERROR:
Sven Neumann's avatar
Sven Neumann committed
132
      g_print ("batch command: experienced a calling error.\n");
Elliot Lee's avatar
Elliot Lee committed
133
134
      break;
    case PDB_SUCCESS:
Sven Neumann's avatar
Sven Neumann committed
135
      g_print ("batch command: executed successfully.\n");
136
137
      break;
    default:
Elliot Lee's avatar
Elliot Lee committed
138
139
      break;
    }
140
141
142
  
  procedural_db_destroy_args (vals, eval_proc->num_values);
  g_free(args);
Elliot Lee's avatar
Elliot Lee committed
143
144
145
146

  return;
}

147
#ifndef G_OS_WIN32
Elliot Lee's avatar
Elliot Lee committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194

static void
batch_read (gpointer          data,
	    gint              source,
	    GdkInputCondition condition)
{
  static GString *string;
  char buf[32], *t;
  int nread, done;

  if (condition & GDK_INPUT_READ)
    {
      do {
	nread = read (source, &buf, sizeof (char) * 31);
      } while ((nread == -1) && ((errno == EAGAIN) || (errno == EINTR)));

      if ((nread == 0) && (!string || (string->len == 0)))
	app_exit (FALSE);

      buf[nread] = '\0';

      if (!string)
	string = g_string_new ("");

      t = buf;
      if (string->len == 0)
	{
	  while (*t)
	    {
	      if (isspace (*t))
		t++;
	      else
		break;
	    }
	}

      g_string_append (string, t);

      done = FALSE;

      while (*t)
	{
	  if ((*t == '\n') || (*t == '\r'))
	    done = TRUE;
	  t++;
	}

195
      if (done)
Elliot Lee's avatar
Elliot Lee committed
196
197
198
199
200
201
	{
	  batch_run_cmd (string->str);
	  g_string_truncate (string, 0);
	}
    }
}
Marc Lehmann's avatar
Marc Lehmann committed
202

203
#endif /* !G_OS_WIN32 */
Tor Lillqvist's avatar
Tor Lillqvist committed
204

Marc Lehmann's avatar
Marc Lehmann committed
205
206
207
208
209
210
211
212
213
214
215
216
217
218
static void
batch_pserver  (int                run_mode,
                int                flags,
                int                extra)
{
  ProcRecord *pserver_proc;
  Argument *args;
  Argument *vals;
  int i;

  pserver_proc = procedural_db_lookup ("extension_perl_server");

  if (!pserver_proc)
    {
Sven Neumann's avatar
Sven Neumann committed
219
      g_message ("extension_perl_server not available: unable to start the perl server\n");
Marc Lehmann's avatar
Marc Lehmann committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
      return;
    }

  args = g_new0 (Argument, pserver_proc->num_args);
  for (i = 0; i < pserver_proc->num_args; i++)
    args[i].arg_type = pserver_proc->args[i].arg_type;

  args[0].value.pdb_int = run_mode;
  args[1].value.pdb_int = flags;
  args[2].value.pdb_int = extra;

  vals = procedural_db_execute ("extension_perl_server", args);
  switch (vals[0].value.pdb_int)
    {
    case PDB_EXECUTION_ERROR:
Sven Neumann's avatar
Sven Neumann committed
235
      g_print ("perl server: experienced an execution error.\n");
Marc Lehmann's avatar
Marc Lehmann committed
236
237
      break;
    case PDB_CALLING_ERROR:
Sven Neumann's avatar
Sven Neumann committed
238
      g_print ("perl server: experienced a calling error.\n");
Marc Lehmann's avatar
Marc Lehmann committed
239
240
      break;
    case PDB_SUCCESS:
Sven Neumann's avatar
Sven Neumann committed
241
      g_print ("perl server: executed successfully.\n");
Marc Lehmann's avatar
Marc Lehmann committed
242
243
244
245
246
247
248
249
250
251
      break;
    default:
      break;
    }
  
  procedural_db_destroy_args (vals, pserver_proc->num_values);
  g_free(args);

  return;
}