1 /* Copyright (C) 1994, 1995, 1996, 1997, 1998, 2000, 2001 Free Software Foundation, Inc.
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2, or (at your option)
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this software; see the file COPYING. If not, write to
14 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15 * Boston, MA 02111-1307 USA
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice. */
41 /* "script.c" argv tricks for `#!' scripts.
42 Authors: Aubrey Jaffer and Jim Blandy */
49 #include "libguile/_scm.h"
50 #include "libguile/gh.h"
51 #include "libguile/load.h"
52 #include "libguile/version.h"
54 #include "libguile/validate.h"
55 #include "libguile/script.h"
62 #include <unistd.h> /* for X_OK define */
69 /* Concatentate str2 onto str1 at position n and return concatenated
70 string if file exists; 0 otherwise. */
73 scm_cat_path (char *str1
, const char *str2
, long n
)
79 size_t len
= strlen (str1
);
80 str1
= (char *) realloc (str1
, (size_t) (len
+ n
+ 1));
83 strncat (str1
+ len
, str2
, n
);
86 str1
= (char *) scm_malloc ((size_t) (n
+ 1));
90 strncat (str1
, str2
, n
);
96 scm_try_path (char *path
)
99 /* fprintf(stderr, "Trying %s\n", path);fflush(stderr); */
102 SCM_SYSCALL (f
= fopen (path
, "r");
114 scm_sep_init_try (char *path
, const char *sep
, const char *initname
)
117 path
= scm_cat_path (path
, sep
, 0L);
119 path
= scm_cat_path (path
, initname
, 0L);
120 return scm_try_path (path
);
124 #ifndef LINE_INCREMENTORS
125 #define LINE_INCREMENTORS '\n'
127 #define WHITE_SPACES ' ':case '\t':case '\r':case '\f':case 26
129 #define WHITE_SPACES ' ':case '\t':case '\r':case '\f'
130 #endif /* def MSDOS */
131 #endif /* ndef LINE_INCREMENTORS */
134 #define MAXPATHLEN 80
135 #endif /* ndef MAXPATHLEN */
138 #endif /* ndef X_OK */
141 scm_find_executable (const char *name
)
143 char tbuf
[MAXPATHLEN
];
147 /* fprintf(stderr, "s_f_e checking access %s ->%d\n", name, access(name, X_OK)); fflush(stderr); */
148 if (access (name
, X_OK
))
150 f
= fopen (name
, "r");
153 if ((fgetc (f
) == '#') && (fgetc (f
) == '!'))
156 switch (tbuf
[i
++] = fgetc (f
))
158 case /*WHITE_SPACES */ ' ':
165 return scm_cat_path (0L, tbuf
, 0L);
169 return scm_cat_path (0L, name
, 0L);
173 /* Read a \nnn-style escape. We've just read the backslash. */
175 script_get_octal (FILE *f
)
176 #define FUNC_NAME "script_get_octal"
181 for (i
= 0; i
< 3; i
++)
184 if ('0' <= c
&& c
<= '7')
185 value
= (value
* 8) + (c
- '0');
187 SCM_MISC_ERROR ("malformed script: bad octal backslash escape",
196 script_get_backslash (FILE *f
)
197 #define FUNC_NAME "script_get_backslash"
203 case 'a': return '\a';
204 case 'b': return '\b';
205 case 'f': return '\f';
206 case 'n': return '\n';
207 case 'r': return '\r';
208 case 't': return '\t';
209 case 'v': return '\v';
217 case '0': case '1': case '2': case '3':
218 case '4': case '5': case '6': case '7':
220 return script_get_octal (f
);
223 SCM_MISC_ERROR ("malformed script: backslash followed by EOF", SCM_EOL
);
224 return 0; /* not reached? */
227 SCM_MISC_ERROR ("malformed script: bad backslash sequence", SCM_EOL
);
228 return 0; /* not reached? */
235 script_read_arg (FILE *f
)
236 #define FUNC_NAME "script_read_arg"
239 char *buf
= scm_malloc (size
+ 1);
251 c
= script_get_backslash (f
);
252 /* The above produces a new character to add to the argument.
257 size
= (size
+ 1) * 2;
258 buf
= realloc (buf
, size
);
266 /* This may terminate an arg now, but it will terminate the
267 entire list next time through. */
275 /* Otherwise, those characters terminate the argument; fall
283 SCM_MISC_ERROR ("malformed script: TAB in meta-arguments", SCM_EOL
);
284 return 0; /* not reached? */
292 script_meta_arg_P (char *arg
)
312 scm_get_meta_args (int argc
, char **argv
)
314 int nargc
= argc
, argi
= 1, nargi
= 1;
316 if (!(argc
> 2 && script_meta_arg_P (argv
[1])))
318 if (!(nargv
= (char **) scm_malloc ((1 + nargc
) * sizeof (char *))))
321 while (((argi
+ 1) < argc
) && (script_meta_arg_P (argv
[argi
])))
323 FILE *f
= fopen (argv
[++argi
], "r");
326 nargc
--; /* to compensate for replacement of '\\' */
338 while ((narg
= script_read_arg (f
)))
339 if (!(nargv
= (char **) realloc (nargv
,
340 (1 + ++nargc
) * sizeof (char *))))
343 nargv
[nargi
++] = narg
;
345 nargv
[nargi
++] = argv
[argi
++];
349 nargv
[nargi
++] = argv
[argi
++];
354 scm_count_argv (char **argv
)
363 /* For use in error messages. */
364 char *scm_usage_name
= 0;
367 scm_shell_usage (int fatal
, char *message
)
370 fprintf (stderr
, "%s\n", message
);
373 "Usage: %s OPTION ...\n"
374 "Evaluate Scheme code, interactively or from a script.\n"
376 " -s SCRIPT load Scheme source code from FILE, and exit\n"
377 " -c EXPR evalute Scheme expression EXPR, and exit\n"
378 " -- stop scanning arguments; run interactively\n"
379 "The above switches stop argument processing, and pass all\n"
380 "remaining arguments as the value of (command-line).\n"
382 " -l FILE load Scheme source code from FILE\n"
383 " -e FUNCTION after reading script, apply FUNCTION to\n"
384 " command line arguments\n"
385 " -ds do -s script at this point\n"
386 " --debug start with debugging evaluator and backtraces\n"
387 " --no-debug start with normal evaluator\n"
388 " Default is to enable debugging for interactive\n"
389 " use, but not for `-s' and `-c'.\n"
390 " -q inhibit loading of user init file\n"
391 " --emacs enable Emacs protocol (experimental)\n"
392 " --use-srfi=LS load SRFI modules for the SRFIs in LS,\n"
393 " which is a list of numbers like \"2,13,14\"\n"
394 " -h, --help display this help and exit\n"
395 " -v, --version display version information and exit\n"
396 " \\ read arguments from following script lines\n",
404 /* Some symbols used by the command-line compiler. */
405 SCM_SYMBOL (sym_load
, "load");
406 SCM_SYMBOL (sym_eval_string
, "eval-string");
407 SCM_SYMBOL (sym_command_line
, "command-line");
408 SCM_SYMBOL (sym_begin
, "begin");
409 SCM_SYMBOL (sym_turn_on_debugging
, "turn-on-debugging");
410 SCM_SYMBOL (sym_load_user_init
, "load-user-init");
411 SCM_SYMBOL (sym_top_repl
, "top-repl");
412 SCM_SYMBOL (sym_quit
, "quit");
413 SCM_SYMBOL (sym_use_srfis
, "use-srfis");
416 /* Given an array of command-line switches, return a Scheme expression
417 to carry out the actions specified by the switches.
419 If you told me this should have been written in Scheme, I'd
420 probably agree. I'd say I didn't feel comfortable doing that in
421 the present system. You'd say, well, fix the system so you are
422 comfortable doing that. I'd agree again. *shrug*
425 static char guile
[] = "guile";
428 scm_compile_shell_switches (int argc
, char **argv
)
430 SCM tail
= SCM_EOL
; /* We accumulate the list backwards,
431 and then reverse! it before we
433 SCM do_script
= SCM_EOL
; /* The element of the list containing
434 the "load" command, in case we get
436 SCM entry_point
= SCM_EOL
; /* for -e switch */
437 int interactive
= 1; /* Should we go interactive when done? */
438 int inhibit_user_init
= 0; /* Don't load user init file */
439 int use_emacs_interface
= 0;
440 int turn_on_debugging
= 0;
441 int dont_turn_on_debugging
= 0;
449 scm_usage_name
= strrchr (argv
[0], '/');
450 if (! scm_usage_name
)
451 scm_usage_name
= argv
[0];
455 if (! scm_usage_name
)
456 scm_usage_name
= guile
;
458 for (i
= 1; i
< argc
; i
++)
460 if (! strcmp (argv
[i
], "-s")) /* load script */
463 scm_shell_usage (1, "missing argument to `-s' switch");
465 /* If we specified the -ds option, do_script points to the
466 cdr of an expression like (load #f); we replace the car
467 (i.e., the #f) with the script name. */
468 if (!SCM_NULLP (do_script
))
470 SCM_SETCAR (do_script
, scm_makfrom0str (argv
[i
]));
474 /* Construct an application of LOAD to the script name. */
475 tail
= scm_cons (scm_cons2 (sym_load
,
476 scm_makfrom0str (argv
[i
]),
485 else if (! strcmp (argv
[i
], "-c")) /* evaluate expr */
488 scm_shell_usage (1, "missing argument to `-c' switch");
489 tail
= scm_cons (scm_cons2 (sym_eval_string
,
490 scm_makfrom0str (argv
[i
]),
498 else if (! strcmp (argv
[i
], "--")) /* end args; go interactive */
504 else if (! strcmp (argv
[i
], "-l")) /* load a file */
507 tail
= scm_cons (scm_cons2 (sym_load
,
508 scm_makfrom0str (argv
[i
]),
512 scm_shell_usage (1, "missing argument to `-l' switch");
515 else if (! strcmp (argv
[i
], "-e")) /* entry point */
518 entry_point
= scm_str2symbol (argv
[i
]);
520 scm_shell_usage (1, "missing argument to `-e' switch");
523 else if (! strcmp (argv
[i
], "-ds")) /* do script here */
525 /* We put a dummy "load" expression, and let the -s put the
527 if (!SCM_NULLP (do_script
))
528 scm_shell_usage (1, "the -ds switch may only be specified once");
529 do_script
= scm_cons (SCM_BOOL_F
, SCM_EOL
);
530 tail
= scm_cons (scm_cons (sym_load
, do_script
),
534 else if (! strcmp (argv
[i
], "--debug"))
536 turn_on_debugging
= 1;
537 dont_turn_on_debugging
= 0;
540 else if (! strcmp (argv
[i
], "--no-debug"))
542 dont_turn_on_debugging
= 1;
543 turn_on_debugging
= 0;
546 else if (! strcmp (argv
[i
], "--emacs")) /* use emacs protocol */
547 use_emacs_interface
= 1;
549 else if (! strcmp (argv
[i
], "-q")) /* don't load user init */
550 inhibit_user_init
= 1;
552 else if (! strncmp (argv
[i
], "--use-srfi=", 11)) /* load SRFIs */
554 SCM srfis
= SCM_EOL
; /* List of requested SRFIs. */
555 char * p
= argv
[i
] + 11;
561 num
= strtol (p
, &end
, 10);
564 srfis
= scm_cons (scm_long2num (num
), srfis
);
570 scm_shell_usage (1, "invalid SRFI specification");
576 scm_shell_usage (1, "invalid SRFI specification");
578 if (scm_ilength (srfis
) <= 0)
579 scm_shell_usage (1, "invalid SRFI specification");
580 srfis
= scm_reverse_x (srfis
, SCM_UNDEFINED
);
581 tail
= scm_cons (scm_list_2 (sym_use_srfis
,
582 scm_list_2 (scm_sym_quote
, srfis
)),
586 else if (! strcmp (argv
[i
], "-h")
587 || ! strcmp (argv
[i
], "--help"))
589 scm_shell_usage (0, 0);
593 else if (! strcmp (argv
[i
], "-v")
594 || ! strcmp (argv
[i
], "--version"))
596 /* Print version number. */
598 "Copyright (c) 1995, 1996, 1997, 2000, 2001 Free Software Foundation\n"
599 "Guile may be distributed under the terms of the GNU General Public Licence;\n"
600 "certain other uses are permitted as well. For details, see the file\n"
601 "`COPYING', which is included in the Guile distribution.\n"
602 "There is no warranty, to the extent permitted by law.\n",
603 SCM_STRING_CHARS (scm_version ()));
609 fprintf (stderr
, "%s: Unrecognized switch `%s'\n",
610 scm_usage_name
, argv
[i
]);
611 scm_shell_usage (1, 0);
615 /* Check to make sure the -ds got a -s. */
616 if (!SCM_NULLP (do_script
))
617 scm_shell_usage (1, "the `-ds' switch requires the use of `-s' as well");
619 /* Make any remaining arguments available to the
620 script/command/whatever. */
621 scm_set_program_arguments (argc
? argc
- i
: 0, argv
+ i
, argv0
);
623 /* If the --emacs switch was set, now is when we process it. */
624 scm_c_define ("use-emacs-interface", SCM_BOOL (use_emacs_interface
));
626 /* Handle the `-e' switch, if it was specified. */
627 if (!SCM_NULLP (entry_point
))
628 tail
= scm_cons (scm_cons2 (entry_point
,
629 scm_cons (sym_command_line
, SCM_EOL
),
633 /* If we didn't end with a -c or a -s, start the repl. */
636 tail
= scm_cons (scm_cons (sym_top_repl
, SCM_EOL
), tail
);
640 /* After doing all the other actions prescribed by the command line,
642 tail
= scm_cons (scm_cons (sym_quit
, SCM_EOL
),
644 /* Allow asyncs (signal handlers etc.) to be run. */
648 /* After the following line, actions will be added to the front. */
649 tail
= scm_reverse_x (tail
, SCM_UNDEFINED
);
651 /* If we didn't end with a -c or a -s and didn't supply a -q, load
652 the user's customization file. */
653 if (interactive
&& !inhibit_user_init
)
655 tail
= scm_cons (scm_cons (sym_load_user_init
, SCM_EOL
), tail
);
658 /* If debugging was requested, or we are interactive and debugging
659 was not explicitely turned off, turn on debugging. */
660 if (turn_on_debugging
|| (interactive
&& !dont_turn_on_debugging
))
662 tail
= scm_cons (scm_cons (sym_turn_on_debugging
, SCM_EOL
), tail
);
666 SCM val
= scm_cons (sym_begin
, tail
);
669 scm_write (val
, SCM_UNDEFINED
);
670 scm_newline (SCM_UNDEFINED
);
679 scm_shell (int argc
, char **argv
)
681 /* If present, add SCSH-style meta-arguments from the top of the
682 script file to the argument vector. See the SCSH manual: "The
683 meta argument" for more details. */
685 char **new_argv
= scm_get_meta_args (argc
, argv
);
690 argc
= scm_count_argv (new_argv
);
694 exit (scm_exit_status (scm_eval_x (scm_compile_shell_switches (argc
, argv
),
695 scm_current_module ())));
702 #include "libguile/script.x"