batch.c 5.74 KB
Newer Older
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
/* 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.
 */

#include "config.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

29
#include <glib-object.h>
30 31 32

#include "core/core-types.h"

33 34
#include "core/gimp.h"

35 36 37 38 39
#include "batch.h"

#include "pdb/procedural_db.h"


40 41 42
static gboolean  batch_exit_after_callback (Gimp        *gimp,
                                            gboolean     kill_it);
static void      batch_run_cmd             (Gimp        *gimp,
43
                                            ProcRecord  *proc,
44 45 46 47 48
                                            const gchar *cmd);
static void      batch_perl_server         (Gimp        *gimp,
                                            GimpRunMode  run_mode,
                                            gint         flags,
                                            gint         extra);
49 50 51


void
52 53
batch_run (Gimp         *gimp,
           const gchar **batch_cmds)
54
{
Sven Neumann's avatar
Sven Neumann committed
55 56 57 58
  ProcRecord *eval_proc = NULL;
  gboolean    perl_server_running = FALSE;
  gulong      exit_id;
  gint        i;
59

60 61 62 63
  exit_id = g_signal_connect_after (gimp, "exit",
                                    G_CALLBACK (batch_exit_after_callback),
                                    NULL);

64
  if (batch_cmds[0] && strcmp (batch_cmds[0], "-") == 0)
65
    {
66
      batch_cmds[0] = "(plug-in-script-fu-text-console RUN-INTERACTIVE)";
67
      batch_cmds[1] = NULL;
68 69 70 71
    }

  for (i = 0; batch_cmds[i]; i++)
    {
72
      /* until --batch-interp=xxx or something similar is implemented
73 74 75 76
       * and gimp-1.0 is not extinct use a shortcut to speed up starting the
       * perl-server tremendously. This is also fully compatible with 1.0.
       */
      {
77
        gint  run_mode, flags, extra;
78

79 80 81
        if (sscanf (batch_cmds[i],
                    "(extension%*[-_]perl%*[-_]server %i %i %i)",
                    &run_mode, &flags, &extra) == 3)
82
          {
Sven Neumann's avatar
Sven Neumann committed
83
            if (! perl_server_running)
84 85
              {
                batch_perl_server (gimp, run_mode, flags, extra);
Sven Neumann's avatar
Sven Neumann committed
86
                perl_server_running = TRUE;
87
              }
88 89 90
            continue;
          }
      }
91

92
      if (! eval_proc)
93
        eval_proc = procedural_db_lookup (gimp, "plug_in_script_fu_eval");
94

95
      if (! eval_proc)
96
        {
97
          g_message ("script-fu not available: batch mode disabled");
98 99 100
          return;
        }

101
      batch_run_cmd (gimp, eval_proc, batch_cmds[i]);
102
    }
103 104

  g_signal_handler_disconnect (gimp, exit_id);
105 106
}

107 108 109 110 111 112 113 114 115 116 117
static gboolean
batch_exit_after_callback (Gimp      *gimp,
                           gboolean   kill_it)
{
  if (gimp->be_verbose)
    g_print ("EXIT: batch_exit_after_callback\n");

  exit (EXIT_SUCCESS);

  return TRUE;
}
118 119

static void
120
batch_run_cmd (Gimp        *gimp,
121
               ProcRecord  *proc,
122
	       const gchar *cmd)
123 124 125 126 127
{
  Argument *args;
  Argument *vals;
  gint      i;

128 129 130
  args = g_new0 (Argument, proc->num_args);
  for (i = 0; i < proc->num_args; i++)
    args[i].arg_type = proc->args[i].arg_type;
131

132 133
  args[0].value.pdb_int     = GIMP_RUN_NONINTERACTIVE;
  args[1].value.pdb_pointer = (gpointer) cmd;
134

135 136
  vals = procedural_db_execute (gimp, gimp_get_user_context (gimp),
                                "plug_in_script_fu_eval", args);
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

  switch (vals[0].value.pdb_int)
    {
    case GIMP_PDB_EXECUTION_ERROR:
      g_print ("batch command: experienced an execution error.\n");
      break;
    case GIMP_PDB_CALLING_ERROR:
      g_print ("batch command: experienced a calling error.\n");
      break;
    case GIMP_PDB_SUCCESS:
      g_print ("batch command: executed successfully.\n");
      break;
    default:
      break;
    }
152

153
  procedural_db_destroy_args (vals, proc->num_values);
154
  g_free (args);
155 156 157 158 159

  return;
}

static void
160 161 162 163
batch_perl_server (Gimp        *gimp,
                   GimpRunMode  run_mode,
                   gint         flags,
                   gint         extra)
164 165 166 167 168 169 170 171 172 173
{
  ProcRecord *pserver_proc;
  Argument   *args;
  Argument   *vals;
  gint        i;

  pserver_proc = procedural_db_lookup (gimp, "extension_perl_server");

  if (!pserver_proc)
    {
174 175
      g_message ("extension_perl_server not available: "
                 "unable to start the perl server");
176 177 178 179 180 181 182 183 184 185 186
      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;

187 188
  vals = procedural_db_execute (gimp, gimp_get_user_context (gimp),
                                "extension_perl_server", args);
189 190 191 192

  switch (vals[0].value.pdb_int)
    {
    case GIMP_PDB_EXECUTION_ERROR:
193
      g_printerr ("perl server: experienced an execution error.\n");
194 195
      break;
    case GIMP_PDB_CALLING_ERROR:
196
      g_printerr ("perl server: experienced a calling error.\n");
197 198
      break;
    case GIMP_PDB_SUCCESS:
199
      g_printerr ("perl server: executed successfully.\n");
200 201 202 203
      break;
    default:
      break;
    }
204

205 206 207 208 209
  procedural_db_destroy_args (vals, pserver_proc->num_values);
  g_free(args);

  return;
}