1 /* GIMP - The GNU 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 3 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, see <https://www.gnu.org/licenses/>.
16  */
17 
18 #include "config.h"
19 
20 #include <string.h>
21 #include <stdlib.h>
22 
23 #include <gdk-pixbuf/gdk-pixbuf.h>
24 #include <gegl.h>
25 
26 #include "libgimpbase/gimpbase.h"
27 
28 #include "core-types.h"
29 
30 #include "gimp.h"
31 #include "gimp-batch.h"
32 #include "gimpparamspecs.h"
33 
34 #include "pdb/gimppdb.h"
35 #include "pdb/gimpprocedure.h"
36 
37 #include "gimp-intl.h"
38 
39 
40 #define BATCH_DEFAULT_EVAL_PROC   "plug-in-script-fu-eval"
41 
42 
43 static void  gimp_batch_exit_after_callback (Gimp          *gimp) G_GNUC_NORETURN;
44 
45 static void  gimp_batch_run_cmd             (Gimp          *gimp,
46                                              const gchar   *proc_name,
47                                              GimpProcedure *procedure,
48                                              GimpRunMode    run_mode,
49                                              const gchar   *cmd);
50 
51 
52 void
gimp_batch_run(Gimp * gimp,const gchar * batch_interpreter,const gchar ** batch_commands)53 gimp_batch_run (Gimp         *gimp,
54                 const gchar  *batch_interpreter,
55                 const gchar **batch_commands)
56 {
57   gulong  exit_id;
58 
59   if (! batch_commands || ! batch_commands[0])
60     return;
61 
62   exit_id = g_signal_connect_after (gimp, "exit",
63                                     G_CALLBACK (gimp_batch_exit_after_callback),
64                                     NULL);
65 
66   if (! batch_interpreter)
67     {
68       batch_interpreter = g_getenv ("GIMP_BATCH_INTERPRETER");
69 
70       if (! batch_interpreter)
71         {
72           batch_interpreter = BATCH_DEFAULT_EVAL_PROC;
73 
74           if (gimp->be_verbose)
75             g_printerr ("No batch interpreter specified, using the default "
76                         "'%s'.\n", batch_interpreter);
77         }
78     }
79 
80   /*  script-fu text console, hardcoded for backward compatibility  */
81 
82   if (strcmp (batch_interpreter, "plug-in-script-fu-eval") == 0 &&
83       strcmp (batch_commands[0], "-") == 0)
84     {
85       const gchar   *proc_name = "plug-in-script-fu-text-console";
86       GimpProcedure *procedure = gimp_pdb_lookup_procedure (gimp->pdb,
87                                                             proc_name);
88 
89       if (procedure)
90         gimp_batch_run_cmd (gimp, proc_name, procedure,
91                             GIMP_RUN_NONINTERACTIVE, NULL);
92       else
93         g_message (_("The batch interpreter '%s' is not available. "
94                      "Batch mode disabled."), proc_name);
95     }
96   else
97     {
98       GimpProcedure *eval_proc = gimp_pdb_lookup_procedure (gimp->pdb,
99                                                             batch_interpreter);
100 
101       if (eval_proc)
102         {
103           gint i;
104 
105           for (i = 0; batch_commands[i]; i++)
106             gimp_batch_run_cmd (gimp, batch_interpreter, eval_proc,
107                                 GIMP_RUN_NONINTERACTIVE, batch_commands[i]);
108         }
109       else
110         {
111           g_message (_("The batch interpreter '%s' is not available. "
112                        "Batch mode disabled."), batch_interpreter);
113         }
114     }
115 
116   g_signal_handler_disconnect (gimp, exit_id);
117 }
118 
119 
120 /*
121  * The purpose of this handler is to exit GIMP cleanly when the batch
122  * procedure calls the gimp-exit procedure. Without this callback, the
123  * message "batch command experienced an execution error" would appear
124  * and gimp would hang forever.
125  */
126 static void
gimp_batch_exit_after_callback(Gimp * gimp)127 gimp_batch_exit_after_callback (Gimp *gimp)
128 {
129   if (gimp->be_verbose)
130     g_print ("EXIT: %s\n", G_STRFUNC);
131 
132   gegl_exit ();
133 
134   exit (EXIT_SUCCESS);
135 }
136 
137 static void
gimp_batch_run_cmd(Gimp * gimp,const gchar * proc_name,GimpProcedure * procedure,GimpRunMode run_mode,const gchar * cmd)138 gimp_batch_run_cmd (Gimp          *gimp,
139                     const gchar   *proc_name,
140                     GimpProcedure *procedure,
141                     GimpRunMode    run_mode,
142                     const gchar   *cmd)
143 {
144   GimpValueArray *args;
145   GimpValueArray *return_vals;
146   GError         *error = NULL;
147   gint            i     = 0;
148 
149   args = gimp_procedure_get_arguments (procedure);
150 
151   if (procedure->num_args > i &&
152       GIMP_IS_PARAM_SPEC_INT32 (procedure->args[i]))
153     g_value_set_int (gimp_value_array_index (args, i++), run_mode);
154 
155   if (procedure->num_args > i &&
156       GIMP_IS_PARAM_SPEC_STRING (procedure->args[i]))
157     g_value_set_static_string (gimp_value_array_index (args, i++), cmd);
158 
159   return_vals =
160     gimp_pdb_execute_procedure_by_name_args (gimp->pdb,
161                                              gimp_get_user_context (gimp),
162                                              NULL, &error,
163                                              proc_name, args);
164 
165   switch (g_value_get_enum (gimp_value_array_index (return_vals, 0)))
166     {
167     case GIMP_PDB_EXECUTION_ERROR:
168       if (error)
169         {
170           g_printerr ("batch command experienced an execution error:\n"
171                       "%s\n", error->message);
172         }
173       else
174         {
175           g_printerr ("batch command experienced an execution error\n");
176         }
177       break;
178 
179     case GIMP_PDB_CALLING_ERROR:
180       if (error)
181         {
182           g_printerr ("batch command experienced a calling error:\n"
183                       "%s\n", error->message);
184         }
185       else
186         {
187           g_printerr ("batch command experienced a calling error\n");
188         }
189       break;
190 
191     case GIMP_PDB_SUCCESS:
192       g_printerr ("batch command executed successfully\n");
193       break;
194     }
195 
196   gimp_value_array_unref (return_vals);
197   gimp_value_array_unref (args);
198 
199   if (error)
200     g_error_free (error);
201 
202   return;
203 }
204