home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / app / batch.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  6.1 KB  |  257 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 2 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17.  */
  18.  
  19. #include "config.h"
  20.  
  21. #include <ctype.h>
  22. #include <errno.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #ifdef HAVE_UNISTD_H
  27. #include <unistd.h>
  28. #endif
  29.  
  30. #include <gtk/gtk.h>
  31.  
  32. #include "apptypes.h"
  33.  
  34. #include "appenv.h"
  35. #include "app_procs.h"
  36. #include "batch.h"
  37. #include "procedural_db.h"
  38.  
  39. static void batch_run_cmd  (char              *cmd);
  40. static void batch_read     (gpointer           data,
  41.                 gint               source,
  42.                 GdkInputCondition  condition);
  43. static void batch_pserver  (int                run_mode,
  44.                             int                flags,
  45.                             int                extra);
  46.  
  47.  
  48. static ProcRecord *eval_proc;
  49.  
  50. void
  51. batch_init ()
  52. {
  53.   extern char **batch_cmds;
  54.  
  55.   int read_from_stdin;
  56.   int i;
  57.   int perl_server_already_running = 0;
  58.  
  59.   eval_proc = procedural_db_lookup ("extension_script_fu_eval");
  60.  
  61.   read_from_stdin = FALSE;
  62.   for (i = 0; batch_cmds[i]; i++)
  63.     {
  64.  
  65.       /* until --batch-interp=xxx or something similar is implemented 
  66.        * and gimp-1.0 is not extinct use a shortcut to speed up starting the
  67.        * perl-server tremendously. This is also fully compatible with 1.0.
  68.        */
  69.       {
  70.         int run_mode, flags, extra;
  71.  
  72.         if (sscanf (batch_cmds[i], "(extension%*[-_]perl%*[-_]server %i %i %i)", &run_mode, &flags, &extra) == 3)
  73.           {
  74.             if (!perl_server_already_running)
  75.              {
  76.                batch_pserver (run_mode, flags, extra);
  77.                perl_server_already_running = 1;
  78.              }
  79.             continue;
  80.           }
  81.       }
  82.       
  83.       if (!eval_proc)
  84.         {
  85.           g_message ("script-fu not available: batch mode disabled\n");
  86.           return;
  87.         }
  88.  
  89.       if (strcmp (batch_cmds[i], "-") == 0)
  90.     {
  91.       if (!read_from_stdin)
  92.         {
  93. #ifndef G_OS_WIN32 /* for now */
  94.           g_print ("reading batch commands from stdin\n");
  95.           gdk_input_add (STDIN_FILENO, GDK_INPUT_READ, batch_read, NULL);
  96.           read_from_stdin = TRUE;
  97. #else
  98.           g_error ("Batch mode from standard input not implemented on Win32");
  99. #endif
  100.         }
  101.     }
  102.       else
  103.     {
  104.       batch_run_cmd (batch_cmds[i]);
  105.     }
  106.     }
  107. }
  108.  
  109.  
  110. static void
  111. batch_run_cmd (char *cmd)
  112. {
  113.   Argument *args;
  114.   Argument *vals;
  115.   int i;
  116.  
  117.   if (g_strcasecmp (cmd, "(gimp-quit 0)") == 0)
  118.     {
  119.       app_exit (FALSE);
  120.       exit (0);
  121.     }
  122.  
  123.   args = g_new0 (Argument, eval_proc->num_args);
  124.   for (i = 0; i < eval_proc->num_args; i++)
  125.     args[i].arg_type = eval_proc->args[i].arg_type;
  126.  
  127.   args[0].value.pdb_int = 1;
  128.   args[1].value.pdb_pointer = cmd;
  129.  
  130.   vals = procedural_db_execute ("extension_script_fu_eval", args);
  131.   switch (vals[0].value.pdb_int)
  132.     {
  133.     case PDB_EXECUTION_ERROR:
  134.       g_print ("batch command: experienced an execution error.\n");
  135.       break;
  136.     case PDB_CALLING_ERROR:
  137.       g_print ("batch command: experienced a calling error.\n");
  138.       break;
  139.     case PDB_SUCCESS:
  140.       g_print ("batch command: executed successfully.\n");
  141.       break;
  142.     default:
  143.       break;
  144.     }
  145.   
  146.   procedural_db_destroy_args (vals, eval_proc->num_values);
  147.   g_free(args);
  148.  
  149.   return;
  150. }
  151.  
  152. #ifndef G_OS_WIN32
  153.  
  154. static void
  155. batch_read (gpointer          data,
  156.         gint              source,
  157.         GdkInputCondition condition)
  158. {
  159.   static GString *string;
  160.   char buf[32], *t;
  161.   int nread, done;
  162.  
  163.   if (condition & GDK_INPUT_READ)
  164.     {
  165.       do {
  166.     nread = read (source, &buf, sizeof (char) * 31);
  167.       } while ((nread == -1) && ((errno == EAGAIN) || (errno == EINTR)));
  168.  
  169.       if ((nread == 0) && (!string || (string->len == 0)))
  170.     app_exit (FALSE);
  171.  
  172.       buf[nread] = '\0';
  173.  
  174.       if (!string)
  175.     string = g_string_new ("");
  176.  
  177.       t = buf;
  178.       if (string->len == 0)
  179.     {
  180.       while (*t)
  181.         {
  182.           if (isspace (*t))
  183.         t++;
  184.           else
  185.         break;
  186.         }
  187.     }
  188.  
  189.       g_string_append (string, t);
  190.  
  191.       done = FALSE;
  192.  
  193.       while (*t)
  194.     {
  195.       if ((*t == '\n') || (*t == '\r'))
  196.         done = TRUE;
  197.       t++;
  198.     }
  199.  
  200.       if (done)
  201.     {
  202.       batch_run_cmd (string->str);
  203.       g_string_truncate (string, 0);
  204.     }
  205.     }
  206. }
  207.  
  208. #endif /* !G_OS_WIN32 */
  209.  
  210. static void
  211. batch_pserver  (int                run_mode,
  212.                 int                flags,
  213.                 int                extra)
  214. {
  215.   ProcRecord *pserver_proc;
  216.   Argument *args;
  217.   Argument *vals;
  218.   int i;
  219.  
  220.   pserver_proc = procedural_db_lookup ("extension_perl_server");
  221.  
  222.   if (!pserver_proc)
  223.     {
  224.       g_message ("extension_perl_server not available: unable to start the perl server\n");
  225.       return;
  226.     }
  227.  
  228.   args = g_new0 (Argument, pserver_proc->num_args);
  229.   for (i = 0; i < pserver_proc->num_args; i++)
  230.     args[i].arg_type = pserver_proc->args[i].arg_type;
  231.  
  232.   args[0].value.pdb_int = run_mode;
  233.   args[1].value.pdb_int = flags;
  234.   args[2].value.pdb_int = extra;
  235.  
  236.   vals = procedural_db_execute ("extension_perl_server", args);
  237.   switch (vals[0].value.pdb_int)
  238.     {
  239.     case PDB_EXECUTION_ERROR:
  240.       g_print ("perl server: experienced an execution error.\n");
  241.       break;
  242.     case PDB_CALLING_ERROR:
  243.       g_print ("perl server: experienced a calling error.\n");
  244.       break;
  245.     case PDB_SUCCESS:
  246.       g_print ("perl server: executed successfully.\n");
  247.       break;
  248.     default:
  249.       break;
  250.     }
  251.   
  252.   procedural_db_destroy_args (vals, pserver_proc->num_values);
  253.   g_free(args);
  254.  
  255.   return;
  256. }
  257.