plug-ins/script-fu/script-fu.c added an eval PDB interface for running

* plug-ins/script-fu/script-fu.c
* plug-ins/script-fu/script-fu-console.[ch]: added an eval
PDB interface for running arbitrary scheme code

* app/batch.c: redid batch mode to use script-fu-eval

* app/main.c: fixed command line parsing for batch mode and
image loading

-Yosh
This commit is contained in:
Manish Singh 1998-09-10 19:03:20 +00:00
parent bdc82fb50a
commit e5ef178966
7 changed files with 135 additions and 270 deletions

View File

@ -1,3 +1,14 @@
Thu Sep 10 12:00:55 PDT 1998 Manish Singh <yosh@gimp.org>
* plug-ins/script-fu/script-fu.c
* plug-ins/script-fu/script-fu-console.[ch]: added an eval
PDB interface for running arbitrary scheme code
* app/batch.c: redid batch mode to use script-fu-eval
* app/main.c: fixed command line parsing for batch mode and
image loading
Wed Sep 9 03:02:17 PDT 1998 Manish Singh <yosh@gimp.org> Wed Sep 9 03:02:17 PDT 1998 Manish Singh <yosh@gimp.org>
* configure.in: added -std1 check for DU4 * configure.in: added -std1 check for DU4

View File

@ -11,23 +11,30 @@
#include "procedural_db.h" #include "procedural_db.h"
static int batch_is_cmd (char *cmd);
static void batch_run_cmd (char *cmd); static void batch_run_cmd (char *cmd);
static void batch_run_cmds (FILE *fp);
static void batch_read (gpointer data, static void batch_read (gpointer data,
gint source, gint source,
GdkInputCondition condition); GdkInputCondition condition);
static ProcRecord *eval_proc;
void void
batch_init () batch_init ()
{ {
extern char **batch_cmds; extern char **batch_cmds;
FILE *fp;
int read_from_stdin; int read_from_stdin;
int i; int i;
eval_proc = procedural_db_lookup ("extension_script_fu_eval");
if (!eval_proc)
{
g_message ("script-fu not available: batch mode disabled\n");
return;
}
read_from_stdin = FALSE; read_from_stdin = FALSE;
for (i = 0; batch_cmds[i]; i++) for (i = 0; batch_cmds[i]; i++)
{ {
@ -40,297 +47,56 @@ batch_init ()
read_from_stdin = TRUE; read_from_stdin = TRUE;
} }
} }
else if (batch_is_cmd (batch_cmds[i])) else
{ {
batch_run_cmd (batch_cmds[i]); batch_run_cmd (batch_cmds[i]);
} }
else
{
fp = fopen (batch_cmds[i], "rb");
if (!fp)
g_print ("unable to open batch file: \"%s\"\n", batch_cmds[i]);
batch_run_cmds (fp);
fclose (fp);
}
} }
} }
static int
batch_is_cmd (char *cmd)
{
int paren_level;
if (!cmd)
return FALSE;
cmd = strchr (cmd, '(');
if (!cmd)
return FALSE;
cmd += 1;
paren_level = 1;
while (*cmd)
{
if (*cmd == ')')
paren_level -= 1;
else if (*cmd == '(')
paren_level += 1;
cmd++;
}
return (paren_level == 0);
}
char *
get_tok(char **rest)
{
char *tok_start, *tok;
int i,j,len,escapes;
/* Skip delimiters */
while((**rest != 0) &&
(**rest == ' ' || **rest == '\t'))
(*rest)++;
/* token starts here */
tok_start = *rest;
if(**rest == '"'){
/* Handle string */
/* Skip quote */
(*rest)++;
tok_start++;
len = 0;
escapes = 0;
/* Scan to end while skipping escaped quotes */
while((**rest != 0) &&
(**rest != '"')){
if(**rest == '\\'){
(*rest)++;
escapes++;
}
(*rest)++;
len++;
}
if(**rest == '"'){
(*rest)++;
tok = g_malloc(len+1);
}
else{
g_print("String not properly terminated.");
return NULL;
}
/* Copy the string while converting the escaped characters. */
/* Only double quote and backspace is accepted other escapes are ignored. */
j = 0;
for (i=0;i < len + escapes;i++){
if(tok_start[i] != '\\')
tok[j++] = tok_start[i];
else{
i++;
if(tok_start[i] == '"' || tok_start[i] == '\\')
tok[j++] = tok_start[i];
}
}
tok[j] = 0;
}
else{
/* Handle number or identifier */
while((**rest != 0) &&
((**rest >= 'a' && **rest <= 'z') ||
(**rest >= 'A' && **rest <= 'Z') ||
(**rest >= '0' && **rest <= '9') ||
(**rest == '-') ||
(**rest == '_')))
(*rest)++;
if (*rest != tok_start){
len = *rest - tok_start;
tok = g_malloc(len+1);
strncpy(tok,tok_start,len);
tok[len]=0;
}
else{
if(**rest == 0){
g_print("Unexpected end of command argument.");
return NULL;
}
/* One character token - normally "(" or ")" */
tok = g_malloc(2);
tok[0] = *rest[0];
tok[1] = 0;
(*rest)++;
}
}
return tok;
}
static void static void
batch_run_cmd (char *cmd) batch_run_cmd (char *cmd)
{ {
ProcRecord *proc;
Argument *args; Argument *args;
Argument *vals; Argument *vals;
char *rest;
char *cmdname;
char *tmpname;
char *t;
int i; int i;
rest = cmd; if (g_strcasecmp (cmd, "(gimp-quit 0)") == 0)
t = get_tok(&rest);
if (!t || t[0] != '(')
return;
g_free(t);
cmdname = get_tok (&rest);
if (!cmdname)
return;
proc = procedural_db_lookup (cmdname);
if (!proc)
{ {
/* Lame hack for "-" to "_" conversion */ app_exit (0);
t = tmpname = g_strdup (cmdname); exit (0);
while (*t)
{
if (*t == '-')
*t = '_';
t++;
}
proc = procedural_db_lookup (tmpname);
if (!proc)
{
g_print ("could not find procedure: \"%s\"\n", cmdname);
return;
}
g_free (tmpname);
} }
/* (gimp-procedural-db-dump "/tmp/pdb_dump") */ args = g_new0 (Argument, eval_proc->num_args);
args = g_new (Argument, proc->num_args); for (i = 0; i < eval_proc->num_args; i++)
args[i].arg_type = eval_proc->args[i].arg_type;
for (i = 0; i < proc->num_args; i++) args[0].value.pdb_int = 1;
{ args[1].value.pdb_pointer = cmd;
args[i].arg_type = proc->args[i].arg_type;
switch (proc->args[i].arg_type) vals = procedural_db_execute ("extension_script_fu_eval", args);
{
case PDB_INT32:
case PDB_INT16:
case PDB_INT8:
t = get_tok (&rest);
if (!t)
goto error;
args[i].value.pdb_int = atoi (t);
g_free(t);
break;
case PDB_FLOAT:
t = get_tok (&rest);
if (!t)
goto error;
args[i].value.pdb_float = atof (t);
g_free(t);
break;
case PDB_STRING:
t = get_tok (&rest);
if (!t)
goto error;
args[i].value.pdb_pointer = g_strdup (t);
g_free(t);
break;
case PDB_INT32ARRAY:
case PDB_INT16ARRAY:
case PDB_INT8ARRAY:
case PDB_FLOATARRAY:
case PDB_STRINGARRAY:
g_print ("procedures taking array arguments are currently not supported as batch operations\n");
goto error;
case PDB_COLOR:
g_print ("procedures taking color arguments are currently not supported as batch operations\n");
goto error;
break;
case PDB_REGION:
g_print ("procedures taking region arguments are currently not supported as batch operations\n");
goto error;
break;
case PDB_DISPLAY:
g_print ("procedures taking display arguments are currently not supported as batch operations\n");
goto error;
break;
case PDB_IMAGE:
g_print ("procedures taking image arguments are currently not supported as batch operations\n");
goto error;
break;
case PDB_LAYER:
g_print ("procedures taking layer arguments are currently not supported as batch operations\n");
goto error;
break;
case PDB_CHANNEL:
g_print ("procedures taking channel arguments are currently not supported as batch operations\n");
goto error;
break;
case PDB_DRAWABLE:
g_print ("procedures taking drawable arguments are currently not supported as batch operations\n");
goto error;
break;
case PDB_SELECTION:
g_print ("procedures taking selection arguments are currently not supported as batch operations\n");
goto error;
break;
case PDB_BOUNDARY:
g_print ("procedures taking boundary arguments are currently not supported as batch operations\n");
goto error;
break;
case PDB_PATH:
g_print ("procedures taking path arguments are currently not supported as batch operations\n");
goto error;
break;
case PDB_STATUS:
g_print ("procedures taking status arguments are currently not supported as batch operations\n");
goto error;
break;
case PDB_END:
break;
}
}
vals = procedural_db_execute (proc->name, args);
switch (vals[0].value.pdb_int) switch (vals[0].value.pdb_int)
{ {
case PDB_EXECUTION_ERROR: case PDB_EXECUTION_ERROR:
g_print ("batch command: %s experienced an execution error.\n", cmdname); g_print ("batch command: experienced an execution error.\n");
break; break;
case PDB_CALLING_ERROR: case PDB_CALLING_ERROR:
g_print ("batch command: %s experienced a calling error.\n", cmdname); g_print ("batch command: experienced a calling error.\n");
break; break;
case PDB_SUCCESS: case PDB_SUCCESS:
g_print ("batch command: %s executed successfully.\n", cmdname); g_print ("batch command: executed successfully.\n");
break;
default:
break; break;
} }
g_free(cmdname); procedural_db_destroy_args (vals, eval_proc->num_values);
g_free(args);
return; return;
error:
g_print ("Unable to run batch command: %s because of bad arguments.\n", cmdname);
g_free(cmdname);
} }
static void
batch_run_cmds (FILE *fp)
{
}
static void static void
batch_read (gpointer data, batch_read (gpointer data,
@ -378,7 +144,7 @@ batch_read (gpointer data,
t++; t++;
} }
if (done && batch_is_cmd (string->str)) if (done)
{ {
batch_run_cmd (string->str); batch_run_cmd (string->str);
g_string_truncate (string, 0); g_string_truncate (string, 0);

View File

@ -70,7 +70,10 @@ static char **gimp_argv;
* Arguments are either switches, their associated * Arguments are either switches, their associated
* values, or image files. As switches and their * values, or image files. As switches and their
* associated values are processed, those slots in * associated values are processed, those slots in
* the argv[] array are NULLed. * the argv[] array are NULLed. We do this because
* unparsed args are treated as images to load on
* startup.
*
* *
* The GTK switches are processed first (X switches are * The GTK switches are processed first (X switches are
* processed here, not by any X routines). Then the * processed here, not by any X routines). Then the
@ -154,20 +157,25 @@ main (int argc, char **argv)
(strcmp (argv[i], "-n") == 0)) (strcmp (argv[i], "-n") == 0))
{ {
no_interface = TRUE; no_interface = TRUE;
argv[i] = NULL;
} }
else if ((strcmp (argv[i], "--batch") == 0) || else if ((strcmp (argv[i], "--batch") == 0) ||
(strcmp (argv[i], "-b") == 0)) (strcmp (argv[i], "-b") == 0))
{ {
for (j = 0, i++ ; i < argc && argv[i][0] != '-'; j++, i++) argv[i] = NULL;
for (j = 0, i++ ; i < argc; j++, i++)
{
batch_cmds[j] = argv[i]; batch_cmds[j] = argv[i];
argv[i] = NULL;
}
batch_cmds[j] = NULL; batch_cmds[j] = NULL;
if (batch_cmds[0] == NULL) /* We need at least one batch command */
show_help = TRUE; if (batch_cmds[0] == NULL) /* We need at least one batch command */
if (argv[i-1][0] != '-') /* Did loop end due to a new argument? */ show_help = TRUE;
--i; /* Ensure new argument gets processed */
} }
else if (strcmp (argv[i], "--system-gimprc") == 0) else if (strcmp (argv[i], "--system-gimprc") == 0)
{ {
argv[i] = NULL;
if (argc <= ++i) if (argc <= ++i)
{ {
show_help = TRUE; show_help = TRUE;
@ -175,6 +183,7 @@ main (int argc, char **argv)
else else
{ {
alternate_system_gimprc = argv[i]; alternate_system_gimprc = argv[i];
argv[i] = NULL;
} }
} }
else if ((strcmp (argv[i], "--gimprc") == 0) || else if ((strcmp (argv[i], "--gimprc") == 0) ||
@ -187,50 +196,61 @@ main (int argc, char **argv)
else else
{ {
alternate_gimprc = argv[i]; alternate_gimprc = argv[i];
argv[i] = NULL;
} }
} }
else if ((strcmp (argv[i], "--help") == 0) || else if ((strcmp (argv[i], "--help") == 0) ||
(strcmp (argv[i], "-h") == 0)) (strcmp (argv[i], "-h") == 0))
{ {
show_help = TRUE; show_help = TRUE;
argv[i] = NULL;
} }
else if (strcmp (argv[i], "--version") == 0 || else if (strcmp (argv[i], "--version") == 0 ||
strcmp (argv[i], "-v") == 0) strcmp (argv[i], "-v") == 0)
{ {
show_version = TRUE; show_version = TRUE;
argv[i] = NULL;
} }
else if (strcmp (argv[i], "--no-data") == 0) else if (strcmp (argv[i], "--no-data") == 0)
{ {
no_data = TRUE; no_data = TRUE;
argv[i] = NULL;
} }
else if (strcmp (argv[i], "--no-splash") == 0) else if (strcmp (argv[i], "--no-splash") == 0)
{ {
no_splash = TRUE; no_splash = TRUE;
argv[i] = NULL;
} }
else if (strcmp (argv[i], "--no-splash-image") == 0) else if (strcmp (argv[i], "--no-splash-image") == 0)
{ {
no_splash_image = TRUE; no_splash_image = TRUE;
argv[i] = NULL;
} }
else if (strcmp (argv[i], "--verbose") == 0) else if (strcmp (argv[i], "--verbose") == 0)
{ {
be_verbose = TRUE; be_verbose = TRUE;
argv[i] = NULL;
} }
else if (strcmp (argv[i], "--no-shm") == 0) else if (strcmp (argv[i], "--no-shm") == 0)
{ {
use_shm = FALSE; use_shm = FALSE;
argv[i] = NULL;
} }
else if (strcmp (argv[i], "--debug-handlers") == 0) else if (strcmp (argv[i], "--debug-handlers") == 0)
{ {
use_debug_handler = TRUE; use_debug_handler = TRUE;
argv[i] = NULL;
} }
else if (strcmp (argv[i], "--console-messages") == 0) else if (strcmp (argv[i], "--console-messages") == 0)
{ {
console_messages = TRUE; console_messages = TRUE;
argv[i] = NULL;
} }
else if ((strcmp (argv[i], "--restore-session") == 0) || else if ((strcmp (argv[i], "--restore-session") == 0) ||
(strcmp (argv[i], "-r") == 0)) (strcmp (argv[i], "-r") == 0))
{ {
restore_session = TRUE; restore_session = TRUE;
argv[i] = NULL;
} }
/* /*
* ANYTHING ELSE starting with a '-' is an error. * ANYTHING ELSE starting with a '-' is an error.

View File

@ -118,7 +118,7 @@ AC_TRY_COMPILE([#include <dirent.h>], [DIR *dir;],
AC_MSG_RESULT(-posix), AC_MSG_RESULT(-posix),
AC_MSG_RESULT() AC_MSG_RESULT()
CFLAGS=$gimp_save_CFLAGS CFLAGS=$gimp_save_CFLAGS
AC_MSG_ERROR([Could not determine POSIX flag. (-posix didn't work.)]))) AC_MSG_WARN([Could not determine POSIX flag. (-posix didn't work.)])))
gimp_save_CPPFLAGS="$CPPFLAGS" gimp_save_CPPFLAGS="$CPPFLAGS"
gimp_save_LDFLAGS="$LDFLAGS" gimp_save_LDFLAGS="$LDFLAGS"

View File

@ -554,3 +554,40 @@ script_fu_close_siod_console ()
close (siod_output_pipe[0]); close (siod_output_pipe[0]);
close (siod_output_pipe[1]); close (siod_output_pipe[1]);
} }
void
script_fu_eval_run (char *name,
int nparams,
GParam *params,
int *nreturn_vals,
GParam **return_vals)
{
static GParam values[1];
GStatusType status = STATUS_SUCCESS;
GRunModeType run_mode;
run_mode = params[0].data.d_int32;
switch (run_mode)
{
case RUN_NONINTERACTIVE:
if (repl_c_string (params[1].data.d_string, 0, 0, 1) != 0)
status = STATUS_EXECUTION_ERROR;
break;
case RUN_INTERACTIVE:
case RUN_WITH_LAST_VALS:
status = STATUS_CALLING_ERROR;
gimp_message ("Script-Fu evaluate mode allows only noninteractive invocation");
break;
default:
break;
}
*nreturn_vals = 1;
*return_vals = values;
values[0].type = PARAM_STATUS;
values[0].data.d_status = status;
}

View File

@ -24,5 +24,10 @@ void script_fu_console_run (char *name,
GParam *params, GParam *params,
int *nreturn_vals, int *nreturn_vals,
GParam **return_vals); GParam **return_vals);
void script_fu_eval_run (char *name,
int nparams,
GParam *params,
int *nreturn_vals,
GParam **return_vals);
#endif /* __SCRIPT_FU_CONSOLE__ */ #endif /* __SCRIPT_FU_CONSOLE__ */

View File

@ -114,6 +114,13 @@ query ()
}; };
static gint nconsole_args = sizeof (console_args) / sizeof (console_args[0]); static gint nconsole_args = sizeof (console_args) / sizeof (console_args[0]);
static GParamDef eval_args[] =
{
{ PARAM_INT32, "run_mode", "[Interactive], non-interactive" },
{ PARAM_STRING, "code", "The code to evaluate" }
};
static gint neval_args = sizeof (eval_args) / sizeof (eval_args[0]);
static GParamDef server_args[] = static GParamDef server_args[] =
{ {
{ PARAM_INT32, "run_mode", "[Interactive], non-interactive" }, { PARAM_INT32, "run_mode", "[Interactive], non-interactive" },
@ -156,6 +163,18 @@ query ()
PROC_EXTENSION, PROC_EXTENSION,
nserver_args, 0, nserver_args, 0,
server_args, NULL); server_args, NULL);
gimp_install_procedure ("extension_script_fu_eval",
"Evaluate scheme code",
"Evaluate the code under the scheme interpeter (primarily for batch mode)",
"Manish Singh",
"Manish Singh",
"1998",
NULL,
NULL,
PROC_EXTENSION,
neval_args, 0,
eval_args, NULL);
} }
static void static void
@ -221,6 +240,13 @@ run (char *name,
{ {
script_fu_server_run (name, nparams, param, nreturn_vals, return_vals); script_fu_server_run (name, nparams, param, nreturn_vals, return_vals);
} }
/*
* A non-interactive "console" (for batch mode)
*/
else if (strcmp (name, "extension_script_fu_eval") == 0)
{
script_fu_eval_run (name, nparams, param, nreturn_vals, return_vals);
}
} }
static gint static gint