|
From: Martin R. <ru...@us...> - 2005-02-03 15:27:01
|
Update of /cvsroot/foo/foo/elkfoo/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20937 Modified Files: main.c Log Message: added hack for scripting support: tokenize cmdline args which get messed together when coming from a script Index: main.c =================================================================== RCS file: /cvsroot/foo/foo/elkfoo/src/main.c,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** main.c 7 Aug 2004 21:20:42 -0000 1.3 --- main.c 3 Feb 2005 15:26:35 -0000 1.4 *************** *** 9,14 **** * foo sound synthesis system * ! * (C)1993-2004 Gerhard Eckel, Ramon Gonzalez-Arroyo, IRCAM, ZKM ! * (C)2003-2004 Martin Rumori */ --- 9,15 ---- * foo sound synthesis system * ! * (C)1993-2005 Gerhard Eckel, Ramon Gonzalez-Arroyo ! * (C)1993-1996 IRCAM, ZKM ! * (C)2003-2005 Martin Rumori */ *************** *** 54,98 **** extern int Case_Insensitive; - #define ELKFOO_NO_READLINE_OPTION "--no-rl" - int main (int argc, ! char **argv) { ! int i, rl = 1; char toplevel[PATH_MAX] = ELKFOO_SCM_PATH; ! char *elk_rl; /* toplevel filename */ strcat(toplevel, "/toplevel.foo"); ! /* readline extension? */ ! for (i = 0; i < argc; ++i) { ! if (! strcmp(argv[i], ELKFOO_NO_READLINE_OPTION)) { ! rl = 0; ! /* tweak argv so that elk sees nothing */ ! for (; i < argc; ++i) ! { ! argv[i] = argv[i + 1]; ! } ! --argc; ! break; } } ! if (rl ! && ((elk_rl = getenv("ELK_READLINE")) ! && (strlen(elk_rl) && strcasecmp(elk_rl, "NO")) ! || (! elk_rl))) { ! setenv("ELK_READLINE", "YES", 1); ! } ! else ! { /* --no-rl */ ! unsetenv("ELK_READLINE"); } --- 55,144 ---- extern int Case_Insensitive; int main (int argc, ! char **argv, ! char **envp) { ! int i, help = 0; char toplevel[PATH_MAX] = ELKFOO_SCM_PATH; ! ! /* scripting-mode-dirty-cmdline-tokenize-hack ! * ! * for scripting mode we have to tokenize the option string, since ! * execve() sends the whole [arg] string in the #!/interpreter [arg] ! * line as one single argument (string) to the process, which will ! * render the options unparseable. in that case, we construct a ! * new, tokenized argv and do an execve() of ourselves because elk ! * depends on a clean argv[] vector at the bottom of the stack. ! * first hint for detecting the need for tokenizing is argc>2 (prog ! * + arg string + script [+ additional cmdline given args]), second ! * hint is argv[1] containing whitespace, third hint is a '-' at the ! * beginning of this fat string (otherwise it could mean a filename ! * containing spaces formerly quoted with '\' and removed by the ! * shell). not sure whether this heuristics is sufficient though. ! */ ! if (argc > 2 && strchr(argv[1], ' ') && argv[1][0] == '-') ! { ! char *new_argv[512]; /* max cmdline args for scripting */ ! char **iter = new_argv; ! ! *iter++ = argv[0]; /* save program name */ ! *iter++ = (char *)strtok(argv[1], " "); /* start */ ! while ((*iter++ = (char *)strtok(NULL, " "))); /* iterate */ ! --iter; ! ! for (i = 0; i < argc; ++i) ! { ! *iter++ = argv[i + 2]; /* save cmdline arguments */ ! } ! *iter = NULL; /* terminate */ ! ! /* "recursively" execute ourselves with new argv */ ! execve(new_argv[0], new_argv, envp); ! } ! ! ! /* ! * real start of main function (clean code only from now on, promised!) ! */ /* toplevel filename */ strcat(toplevel, "/toplevel.foo"); ! /* show help? */ ! ! /* the problem here is: elk will parse the cmdline again and ! probably complain, but giving us no response about its decision. ! thus we have to guess somehow whether to show our help as well. ! we have to know that in advance, since elk does an exit() ! directly after having printed its help. options -help and --help ! looks like a good constraint */ ! ! /* PROMISED TOO MUCH: STILL HACKY CODE HERE */ ! for (i = 0; i < argc && strcmp(argv[i], "--"); ++i) { ! /* we don't have --longopts at the moment in elk and no opts ! * currently in foo. thus we could suppose that the user might ! * want to pass a given longopt to the scheme interpreter rather ! * than to the startup code. if so, we show our help, but "--" ! * will have stopped the scan anyway */ ! if (! strncmp(argv[i], "--", 2) || ! ! strcmp(argv[i], "--help") || /* this is redundant */ ! ! strcmp(argv[i], "-help")) { ! help = 1; break; } } ! /* show help if requested */ ! if (help) { ! fprintf(stderr, "Usage: %s [elk-options] \ ! -- [foo-options] [script-file [script-options]]\n\ ! foo-loader options:\n currently none.\n\ ! call %s -- --help for foo scheme evaluated options\n\n\ ! elk options following...\n", argv[0], argv[0]); } |