From e5ef178966a839272a9ea9ab04d43257b90437f3 Mon Sep 17 00:00:00 2001 From: Manish Singh Date: Thu, 10 Sep 1998 19:03:20 +0000 Subject: [PATCH] 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 --- ChangeLog | 11 + app/batch.c | 292 +++---------------------- app/main.c | 32 ++- configure.in | 2 +- plug-ins/script-fu/script-fu-console.c | 37 ++++ plug-ins/script-fu/script-fu-console.h | 5 + plug-ins/script-fu/script-fu.c | 26 +++ 7 files changed, 135 insertions(+), 270 deletions(-) diff --git a/ChangeLog b/ChangeLog index c6e8cede7d..491e3d4948 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +Thu Sep 10 12:00:55 PDT 1998 Manish Singh + + * 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 * configure.in: added -std1 check for DU4 diff --git a/app/batch.c b/app/batch.c index 8cc9085db5..e9fcef11c6 100644 --- a/app/batch.c +++ b/app/batch.c @@ -11,23 +11,30 @@ #include "procedural_db.h" -static int batch_is_cmd (char *cmd); static void batch_run_cmd (char *cmd); -static void batch_run_cmds (FILE *fp); static void batch_read (gpointer data, gint source, GdkInputCondition condition); +static ProcRecord *eval_proc; + + void batch_init () { extern char **batch_cmds; - FILE *fp; int read_from_stdin; 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; for (i = 0; batch_cmds[i]; i++) { @@ -40,297 +47,56 @@ batch_init () read_from_stdin = TRUE; } } - else if (batch_is_cmd (batch_cmds[i])) + else { 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 batch_run_cmd (char *cmd) { - ProcRecord *proc; Argument *args; Argument *vals; - char *rest; - char *cmdname; - char *tmpname; - char *t; int i; - rest = cmd; - 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) + if (g_strcasecmp (cmd, "(gimp-quit 0)") == 0) { - /* Lame hack for "-" to "_" conversion */ - t = tmpname = g_strdup (cmdname); - 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); + app_exit (0); + exit (0); } - /* (gimp-procedural-db-dump "/tmp/pdb_dump") */ - args = g_new (Argument, proc->num_args); + args = g_new0 (Argument, eval_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[i].arg_type = proc->args[i].arg_type; + args[0].value.pdb_int = 1; + args[1].value.pdb_pointer = cmd; - switch (proc->args[i].arg_type) - { - 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); + vals = procedural_db_execute ("extension_script_fu_eval", args); switch (vals[0].value.pdb_int) { 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; 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; case PDB_SUCCESS: - g_print ("batch command: %s executed successfully.\n", cmdname); + g_print ("batch command: executed successfully.\n"); + break; + default: break; } + + procedural_db_destroy_args (vals, eval_proc->num_values); + g_free(args); - g_free(cmdname); 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 batch_read (gpointer data, @@ -378,7 +144,7 @@ batch_read (gpointer data, t++; } - if (done && batch_is_cmd (string->str)) + if (done) { batch_run_cmd (string->str); g_string_truncate (string, 0); diff --git a/app/main.c b/app/main.c index 0c3d31d2d9..7c204c0c10 100644 --- a/app/main.c +++ b/app/main.c @@ -70,7 +70,10 @@ static char **gimp_argv; * Arguments are either switches, their associated * values, or image files. As switches and their * 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 * processed here, not by any X routines). Then the @@ -154,20 +157,25 @@ main (int argc, char **argv) (strcmp (argv[i], "-n") == 0)) { no_interface = TRUE; + argv[i] = NULL; } else if ((strcmp (argv[i], "--batch") == 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]; + argv[i] = NULL; + } batch_cmds[j] = NULL; - if (batch_cmds[0] == NULL) /* We need at least one batch command */ - show_help = TRUE; - if (argv[i-1][0] != '-') /* Did loop end due to a new argument? */ - --i; /* Ensure new argument gets processed */ + + if (batch_cmds[0] == NULL) /* We need at least one batch command */ + show_help = TRUE; } else if (strcmp (argv[i], "--system-gimprc") == 0) { + argv[i] = NULL; if (argc <= ++i) { show_help = TRUE; @@ -175,6 +183,7 @@ main (int argc, char **argv) else { alternate_system_gimprc = argv[i]; + argv[i] = NULL; } } else if ((strcmp (argv[i], "--gimprc") == 0) || @@ -187,50 +196,61 @@ main (int argc, char **argv) else { alternate_gimprc = argv[i]; + argv[i] = NULL; } } else if ((strcmp (argv[i], "--help") == 0) || (strcmp (argv[i], "-h") == 0)) { show_help = TRUE; + argv[i] = NULL; } else if (strcmp (argv[i], "--version") == 0 || strcmp (argv[i], "-v") == 0) { show_version = TRUE; + argv[i] = NULL; } else if (strcmp (argv[i], "--no-data") == 0) { no_data = TRUE; + argv[i] = NULL; } else if (strcmp (argv[i], "--no-splash") == 0) { no_splash = TRUE; + argv[i] = NULL; } else if (strcmp (argv[i], "--no-splash-image") == 0) { no_splash_image = TRUE; + argv[i] = NULL; } else if (strcmp (argv[i], "--verbose") == 0) { be_verbose = TRUE; + argv[i] = NULL; } else if (strcmp (argv[i], "--no-shm") == 0) { use_shm = FALSE; + argv[i] = NULL; } else if (strcmp (argv[i], "--debug-handlers") == 0) { use_debug_handler = TRUE; + argv[i] = NULL; } else if (strcmp (argv[i], "--console-messages") == 0) { console_messages = TRUE; + argv[i] = NULL; } else if ((strcmp (argv[i], "--restore-session") == 0) || (strcmp (argv[i], "-r") == 0)) { restore_session = TRUE; + argv[i] = NULL; } /* * ANYTHING ELSE starting with a '-' is an error. diff --git a/configure.in b/configure.in index 02ab23dee7..53b87c926a 100644 --- a/configure.in +++ b/configure.in @@ -118,7 +118,7 @@ AC_TRY_COMPILE([#include ], [DIR *dir;], AC_MSG_RESULT(-posix), AC_MSG_RESULT() 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_LDFLAGS="$LDFLAGS" diff --git a/plug-ins/script-fu/script-fu-console.c b/plug-ins/script-fu/script-fu-console.c index 6204c79d7a..aa373d8278 100644 --- a/plug-ins/script-fu/script-fu-console.c +++ b/plug-ins/script-fu/script-fu-console.c @@ -554,3 +554,40 @@ script_fu_close_siod_console () close (siod_output_pipe[0]); 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; +} diff --git a/plug-ins/script-fu/script-fu-console.h b/plug-ins/script-fu/script-fu-console.h index 231420320d..e0d79cfaa6 100644 --- a/plug-ins/script-fu/script-fu-console.h +++ b/plug-ins/script-fu/script-fu-console.h @@ -24,5 +24,10 @@ void script_fu_console_run (char *name, GParam *params, int *nreturn_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__ */ diff --git a/plug-ins/script-fu/script-fu.c b/plug-ins/script-fu/script-fu.c index 3be6477337..34633d4043 100644 --- a/plug-ins/script-fu/script-fu.c +++ b/plug-ins/script-fu/script-fu.c @@ -114,6 +114,13 @@ query () }; 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[] = { { PARAM_INT32, "run_mode", "[Interactive], non-interactive" }, @@ -156,6 +163,18 @@ query () PROC_EXTENSION, nserver_args, 0, 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 @@ -221,6 +240,13 @@ run (char *name, { 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