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
|
/* GIMP - The GNU 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h>
#include <stdlib.h>
#include <gegl.h>
#include "core/core-types.h"
#include "base/tile-swap.h"
#include "core/gimp.h"
#include "core/gimpparamspecs.h"
#include "batch.h"
#include "pdb/gimppdb.h"
#include "pdb/gimpprocedure.h"
#include "gimp-intl.h"
#define BATCH_DEFAULT_EVAL_PROC "plug-in-script-fu-eval"
static void batch_exit_after_callback (Gimp *gimp) G_GNUC_NORETURN;
static void batch_run_cmd (Gimp *gimp,
const gchar *proc_name,
GimpProcedure *procedure,
GimpRunMode run_mode,
const gchar *cmd);
void
batch_run (Gimp *gimp,
const gchar *batch_interpreter,
const gchar **batch_commands)
{
gulong exit_id;
if (! batch_commands || ! batch_commands[0])
return;
exit_id = g_signal_connect_after (gimp, "exit",
G_CALLBACK (batch_exit_after_callback),
NULL);
if (! batch_interpreter)
{
batch_interpreter = g_getenv ("GIMP_BATCH_INTERPRETER");
if (! batch_interpreter)
{
batch_interpreter = BATCH_DEFAULT_EVAL_PROC;
if (gimp->be_verbose)
g_printerr (_("No batch interpreter specified, using the default "
"'%s'.\n"), batch_interpreter);
}
}
/* script-fu text console, hardcoded for backward compatibility */
if (strcmp (batch_interpreter, "plug-in-script-fu-eval") == 0 &&
strcmp (batch_commands[0], "-") == 0)
{
const gchar *proc_name = "plug-in-script-fu-text-console";
GimpProcedure *procedure = gimp_pdb_lookup_procedure (gimp->pdb,
proc_name);
if (procedure)
batch_run_cmd (gimp, proc_name, procedure,
GIMP_RUN_NONINTERACTIVE, NULL);
else
g_message (_("The batch interpreter '%s' is not available. "
"Batch mode disabled."), proc_name);
}
else
{
GimpProcedure *eval_proc = gimp_pdb_lookup_procedure (gimp->pdb,
batch_interpreter);
if (eval_proc)
{
gint i;
for (i = 0; batch_commands[i]; i++)
batch_run_cmd (gimp, batch_interpreter, eval_proc,
GIMP_RUN_NONINTERACTIVE, batch_commands[i]);
}
else
{
g_message (_("The batch interpreter '%s' is not available. "
"Batch mode disabled."), batch_interpreter);
}
}
g_signal_handler_disconnect (gimp, exit_id);
}
/*
* The purpose of this handler is to exit GIMP cleanly when the batch
* procedure calls the gimp-exit procedure. Without this callback, the
* message "batch command experienced an execution error" would appear
* and gimp would hang forever.
*/
static void
batch_exit_after_callback (Gimp *gimp)
{
if (gimp->be_verbose)
g_print ("EXIT: %s\n", G_STRFUNC);
gegl_exit ();
/* make sure that the swap files are removed before we quit */
tile_swap_exit ();
exit (EXIT_SUCCESS);
}
static void
batch_run_cmd (Gimp *gimp,
const gchar *proc_name,
GimpProcedure *procedure,
GimpRunMode run_mode,
const gchar *cmd)
{
GValueArray *args;
GValueArray *return_vals;
GError *error = NULL;
gint i = 0;
args = gimp_procedure_get_arguments (procedure);
if (procedure->num_args > i && GIMP_IS_PARAM_SPEC_INT32 (procedure->args[i]))
g_value_set_int (&args->values[i++], run_mode);
if (procedure->num_args > i && GIMP_IS_PARAM_SPEC_STRING (procedure->args[i]))
g_value_set_static_string (&args->values[i++], cmd);
return_vals =
gimp_pdb_execute_procedure_by_name_args (gimp->pdb,
gimp_get_user_context (gimp),
NULL, &error,
proc_name, args);
switch (g_value_get_enum (&return_vals->values[0]))
{
case GIMP_PDB_EXECUTION_ERROR:
if (error)
{
g_printerr ("batch command experienced an execution error:\n"
"%s\n", error->message);
}
else
{
g_printerr ("batch command experienced an execution error\n");
}
break;
case GIMP_PDB_CALLING_ERROR:
if (error)
{
g_printerr ("batch command experienced a calling error:\n"
"%s\n", error->message);
}
else
{
g_printerr ("batch command experienced a calling error\n");
}
break;
case GIMP_PDB_SUCCESS:
g_printerr ("batch command executed successfully\n");
break;
}
g_value_array_free (return_vals);
g_value_array_free (args);
if (error)
g_error_free (error);
return;
}
|