File: script-fu-command.c

package info (click to toggle)
gimp 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 222,880 kB
  • sloc: ansic: 870,914; python: 10,965; lisp: 10,857; cpp: 7,355; perl: 4,536; sh: 1,753; xml: 972; yacc: 609; lex: 348; javascript: 150; makefile: 42
file content (175 lines) | stat: -rw-r--r-- 6,280 bytes parent folder | download | duplicates (3)
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
/* 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 <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <libgimp/gimp.h>

#include "script-fu-types.h"    /* SFScript */
#include "script-fu-lib.h"
#include "script-fu-script.h"

#include "script-fu-command.h"


/* Methods for interpreting commands.
 *
 * Usually there is a stack of calls similar to:
 *       script_fu_run_image_procedure (outer run func)
 * calls script_fu_interpret_image_proc
 * calls script_fu_run_command
 * calls ts_interpret_string
 * calls the inner run func in Scheme
 *
 * but script_fu_run_command is also called directly for loading scripts.
 *
 * FUTURE: see also similar code in script-fu-interface.c
 * which could be migrated here.
 */


/* Interpret a command.
 * A command is a string for a Scheme call to a script plugin's run function.
 *
 * When errors during interpretation:
 * 1) set the error message from tinyscheme into GError at given handle.
 * 2) return FALSE
 * Otherwise, return TRUE and discard any result of interpretation.
 * ScriptFu return values only have a GimpPDBStatus,
 * since ScriptFu plugin scripts can only be declared returning void.
 *
 * In v2, we captured output from a script (calls to Scheme:display)
 * and they were a prefix of any error message.
 * In v3, output from a script is shown in any stdout/terminal in which Gimp was started.
 * And any error msg is retrieved from the inner interpreter.
 *
 * While interpreting, any errors from further calls to the PDB
 * can show error dialogs in any GIMP gui,
 * unless the caller has taken responsibility with a prior call to
 * gimp_plug_in_set_pdb_error_handler
 *
 * FIXME: see script_fu_run_procedure.
 * It does not call gimp_plug_in_set_pdb_error_handler for NON-INTERACTIVE mode.
 */
gboolean
script_fu_run_command (const gchar  *command,
                       GError      **error)
{
  g_debug ("script_fu_run_command: %s", command);

  if (script_fu_interpret_string (command))
    {
      *error = script_fu_get_gerror ();
      return FALSE;
    }
  else
    {
      return TRUE;
    }
}

/* Wrap a run_command in error handler.
 * Returns the result of running the command.
 * The result is a PDB call result whose only value is the success.
 * Since no SF command returns more values i.e. scripts return type void.
 */
static GimpValueArray *
sf_wrap_run_command (GimpProcedure *procedure,
                     SFScript      *script,
                     gchar         *command)
{
  GimpValueArray *result = NULL;
  gboolean        interpretation_result;
  GError         *error = NULL;

  /* Take responsibility for handling errors from the scripts further calls to PDB.
   * ScriptFu does not show an error dialog, but forwards errors back to GIMP.
   * This only tells GIMP that ScriptFu itself will forward GimpPDBStatus errors from
   * this scripts calls to the PDB.
   * The onus is on this script's called PDB procedures to return errors in the GimpPDBStatus.
   * Any that do not, but for example only call gimp-message, are breaching contract.
   */
  gimp_plug_in_set_pdb_error_handler (gimp_get_plug_in (),
                                      GIMP_PDB_ERROR_HANDLER_PLUGIN);

  interpretation_result = script_fu_run_command (command, &error);

  if (! interpretation_result)
    {
      /* This is to the console.
       * script->name not localized.
       * error->message expected to be localized.
       * GIMP will later display "PDB procedure failed: <message>" localized.
       */
      g_warning ("While executing %s: %s",
                 script->name,
                 error->message);
      /* A GError was allocated and this will take ownership. */
      result = gimp_procedure_new_return_values (procedure,
                                                 GIMP_PDB_EXECUTION_ERROR,
                                                 error);
    }
  else
    {
      result = gimp_procedure_new_return_values (procedure,
                                                 GIMP_PDB_SUCCESS,
                                                 NULL);
    }

  gimp_plug_in_set_pdb_error_handler (gimp_get_plug_in (),
                                      GIMP_PDB_ERROR_HANDLER_INTERNAL);

  return result;
}

/* Interpret a script that defines a GimpImageProcedure.
 *
 * Similar to v2 code in script-fu-interface.c, except:
 * 1) builds a command from a GValueArray from a GimpConfig,
 *    instead of from local array of SFArg.
 * 2) adds actual args image, drawable, etc. for GimpImageProcedure
 */
GimpValueArray *
script_fu_interpret_image_proc (GimpProcedure        *procedure,
                                SFScript             *script,
                                GimpImage            *image,
                                GimpDrawable        **drawables,
                                GimpProcedureConfig  *config)
{
  gchar          *command;
  GimpValueArray *result = NULL;

  command = script_fu_script_get_command_for_image_proc (script, image, drawables, config);
  result = sf_wrap_run_command (procedure, script, command);
  g_free (command);
  return result;
}

/* Interpret a script that defines a GimpProcedure. */
GimpValueArray *
script_fu_interpret_regular_proc (GimpProcedure        *procedure,
                                  SFScript             *script,
                                  GimpProcedureConfig  *config)
{
  gchar          *command;
  GimpValueArray *result = NULL;

  command = script_fu_script_get_command_for_regular_proc (script, config);
  result = sf_wrap_run_command (procedure, script, command);
  g_free (command);
  return result;
}