File: batch.c

package info (click to toggle)
gimp 1.0.0-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 16,832 kB
  • ctags: 16,019
  • sloc: ansic: 225,077; lisp: 8,497; makefile: 4,769; sh: 2,154
file content (387 lines) | stat: -rw-r--r-- 8,048 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

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


static int  batch_is_cmd   (char              *cmd);
static void batch_run_cmd  (char              *cmd);
static void batch_run_cmds (FILE              *fp);
static void batch_read     (gpointer           data,
			    gint               source,
			    GdkInputCondition  condition);


void
batch_init ()
{
  extern char **batch_cmds;

  FILE *fp;
  int read_from_stdin;
  int i;

  read_from_stdin = FALSE;
  for (i = 0; batch_cmds[i]; i++)
    {
      if (strcmp (batch_cmds[i], "-") == 0)
	{
	  if (!read_from_stdin)
	    {
	      g_print ("reading batch commands from stdin\n");
	      gdk_input_add (STDIN_FILENO, GDK_INPUT_READ, batch_read, NULL);
	      read_from_stdin = TRUE;
	    }
	}
      else if (batch_is_cmd (batch_cmds[i]))
	{
	  batch_run_cmd (batch_cmds[i]);
	}
      else
	{
	  fp = fopen (batch_cmds[i], "r");
	  if (!fp)
	    g_print ("unable to open batch file: \"%s\"\n", batch_cmds[i]);

	  batch_run_cmds (fp);
	  fclose (fp);
	}
    }
}


static int
batch_is_cmd (char *cmd)
{
  int paren_level;

  if (!cmd)
    return FALSE;

  cmd = strchr (cmd, '(');
  if (!cmd)
    return FALSE;
  cmd += 1;

  paren_level = 1;

  while (*cmd)
    {
      if (*cmd == ')')
	paren_level -= 1;
      else if (*cmd == '(')
	paren_level += 1;
      cmd++;
    }

  return (paren_level == 0);
}

char *
get_tok(char **rest)
{
  char *tok_start, *tok;
  int i,j,len,escapes;

  /* Skip delimiters */
  while((**rest != 0) && 
	(**rest == ' ' || **rest == '\t'))
    (*rest)++;

  /* token starts here */
  tok_start = *rest;

  if(**rest == '"'){
    /* Handle string */

    /* Skip quote */
    (*rest)++;
    tok_start++;
    len = 0;
    escapes = 0;

    /* Scan to end while skipping escaped quotes */
    while((**rest != 0) && 
	  (**rest != '"')){
      if(**rest == '\\'){
	(*rest)++;
	escapes++;
      }
      (*rest)++;
      len++;
    }
    if(**rest == '"'){
      (*rest)++;
      tok = g_malloc(len+1);
    }
    else{
      g_print("String not properly terminated.");
      return NULL;
    }

    /* Copy the string while converting the escaped characters. */
    /* Only double quote and backspace is accepted other escapes are ignored. */
    j = 0;
    for (i=0;i < len + escapes;i++){
      if(tok_start[i] != '\\')
	tok[j++] = tok_start[i];
      else{
	i++;
	if(tok_start[i] == '"' || tok_start[i] == '\\')
	  tok[j++] = tok_start[i];
      }
    }
    tok[j] = 0;
  }
  else{
    /* Handle number or identifier */
    while((**rest != 0) && 
	  ((**rest >= 'a' && **rest <= 'z') || 
	   (**rest >= 'A' && **rest <= 'Z') || 
	   (**rest >= '0' && **rest <= '9') ||
	   (**rest == '-') ||
	   (**rest == '_')))
      (*rest)++;
    if (*rest != tok_start){
      len = *rest - tok_start;
      tok = g_malloc(len+1);
      strncpy(tok,tok_start,len);
      tok[len]=0;
    }
    else{
      if(**rest == 0){
	g_print("Unexpected end of command argument.");
        return NULL;
      }
      /* One character token - normally "(" or ")" */
      tok = g_malloc(2);
      tok[0] = *rest[0];
      tok[1] = 0;
      (*rest)++;
    }
  }
  return tok;
}

static void
batch_run_cmd (char *cmd)
{
  ProcRecord *proc;
  Argument *args;
  Argument *vals;
  char *rest;
  char *cmdname;
  char *tmpname;
  char *t;
  int i;

  rest = cmd;
  t = get_tok(&rest);
  if (!t || t[0] != '(')
    return;
  g_free(t);

  cmdname = get_tok (&rest);
  if (!cmdname)
    return;

  proc = procedural_db_lookup (cmdname);
  if (!proc)
    {
      /* Lame hack for "-" to "_" conversion */
      t = tmpname = g_strdup (cmdname);
      while (*t)
	{
	  if (*t == '-')
	    *t = '_';
	  t++;
	}

      proc = procedural_db_lookup (tmpname);
      if (!proc)
	{
	  g_print ("could not find procedure: \"%s\"\n", cmdname);
	  return;
	}

      g_free (tmpname);
    }

  /* (gimp-procedural-db-dump "/tmp/pdb_dump") */
  args = g_new (Argument, proc->num_args);

  for (i = 0; i < proc->num_args; i++)
    {
      args[i].arg_type = proc->args[i].arg_type;

      switch (proc->args[i].arg_type)
	{
	case PDB_INT32:
	case PDB_INT16:
	case PDB_INT8:
	  t = get_tok (&rest);
	  if (!t)
	    goto error;

	  args[i].value.pdb_int = atoi (t);
	  g_free(t);
	  break;
	case PDB_FLOAT:
	  t = get_tok (&rest);
	  if (!t)
	    goto error;

	  args[i].value.pdb_float = atof (t);
	  g_free(t);
	  break;
	case PDB_STRING:
	  t = get_tok (&rest);
	  if (!t)
	    goto error;

	  args[i].value.pdb_pointer = g_strdup (t);
	  g_free(t);
	  break;
	case PDB_INT32ARRAY:
	case PDB_INT16ARRAY:
	case PDB_INT8ARRAY:
	case PDB_FLOATARRAY:
	case PDB_STRINGARRAY:
	  g_print ("procedures taking array arguments are currently not supported as batch operations\n");
	  goto error;
	case PDB_COLOR:
	  g_print ("procedures taking color arguments are currently not supported as batch operations\n");
	  goto error;
	  break;
	case PDB_REGION:
	  g_print ("procedures taking region arguments are currently not supported as batch operations\n");
	  goto error;
	  break;
	case PDB_DISPLAY:
	  g_print ("procedures taking display arguments are currently not supported as batch operations\n");
	  goto error;
	  break;
	case PDB_IMAGE:
	  g_print ("procedures taking image arguments are currently not supported as batch operations\n");
	  goto error;
	  break;
	case PDB_LAYER:
	  g_print ("procedures taking layer arguments are currently not supported as batch operations\n");
	  goto error;
	  break;
	case PDB_CHANNEL:
	  g_print ("procedures taking channel arguments are currently not supported as batch operations\n");
	  goto error;
	  break;
	case PDB_DRAWABLE:
	  g_print ("procedures taking drawable arguments are currently not supported as batch operations\n");
	  goto error;
	  break;
	case PDB_SELECTION:
	  g_print ("procedures taking selection arguments are currently not supported as batch operations\n");
	  goto error;
	  break;
	case PDB_BOUNDARY:
	  g_print ("procedures taking boundary arguments are currently not supported as batch operations\n");
	  goto error;
	  break;
	case PDB_PATH:
	  g_print ("procedures taking path arguments are currently not supported as batch operations\n");
	  goto error;
	  break;
	case PDB_STATUS:
	  g_print ("procedures taking status arguments are currently not supported as batch operations\n");
	  goto error;
	  break;
	case PDB_END:
	  break;
	}
    }

  vals = procedural_db_execute (proc->name, args);
  switch (vals[0].value.pdb_int)
    {
    case PDB_EXECUTION_ERROR:
      g_print ("batch command: %s experienced an execution error.\n", cmdname);
      break;
    case PDB_CALLING_ERROR:
      g_print ("batch command: %s experienced a calling error.\n", cmdname);
      break;
    case PDB_SUCCESS:
      g_print ("batch command: %s executed successfully.\n", cmdname);
      break;
    }

  g_free(cmdname);
  return;

error:
  g_print ("Unable to run batch command: %s because of bad arguments.\n", cmdname);
  g_free(cmdname);
}

static void
batch_run_cmds (FILE *fp)
{
}

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++;
	}

      if (done && batch_is_cmd (string->str))
	{
	  batch_run_cmd (string->str);
	  g_string_truncate (string, 0);
	}
    }
}