1 /* Copyright (C) 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public License
4 * as published by the Free Software Foundation; either version 3 of
5 * the License, or (at your option) any later version.
7 * This library is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 /* "script.c" argv tricks for `#!' scripts.
19 Authors: Aubrey Jaffer and Jim Blandy */
30 #include <version-etc.h>
32 #include "libguile/_scm.h"
33 #include "libguile/eval.h"
34 #include "libguile/feature.h"
35 #include "libguile/load.h"
36 #include "libguile/private-gc.h" /* scm_getenv_int */
37 #include "libguile/read.h"
38 #include "libguile/script.h"
39 #include "libguile/strings.h"
40 #include "libguile/strports.h"
41 #include "libguile/validate.h"
42 #include "libguile/version.h"
49 #include <unistd.h> /* for X_OK define */
56 /* Concatentate str2 onto str1 at position n and return concatenated
57 string if file exists; 0 otherwise. */
60 scm_cat_path (char *str1
, const char *str2
, long n
)
66 size_t len
= strlen (str1
);
67 str1
= (char *) realloc (str1
, (size_t) (len
+ n
+ 1));
70 strncat (str1
+ len
, str2
, n
);
73 str1
= (char *) scm_malloc ((size_t) (n
+ 1));
77 strncat (str1
, str2
, n
);
83 scm_try_path (char *path
)
86 /* fprintf(stderr, "Trying %s\n", path);fflush(stderr); */
89 SCM_SYSCALL (f
= fopen (path
, "r");
101 scm_sep_init_try (char *path
, const char *sep
, const char *initname
)
104 path
= scm_cat_path (path
, sep
, 0L);
106 path
= scm_cat_path (path
, initname
, 0L);
107 return scm_try_path (path
);
111 #ifndef LINE_INCREMENTORS
112 #define LINE_INCREMENTORS '\n'
114 #define WHITE_SPACES ' ':case '\t':case '\r':case '\f':case 26
116 #define WHITE_SPACES ' ':case '\t':case '\r':case '\f'
117 #endif /* def MSDOS */
118 #endif /* ndef LINE_INCREMENTORS */
121 #define MAXPATHLEN 80
122 #endif /* ndef MAXPATHLEN */
125 #endif /* ndef X_OK */
128 scm_find_executable (const char *name
)
130 char tbuf
[MAXPATHLEN
];
134 /* fprintf(stderr, "s_f_e checking access %s ->%d\n", name, access(name, X_OK)); fflush(stderr); */
135 if (access (name
, X_OK
))
137 f
= fopen (name
, "r");
140 if ((fgetc (f
) == '#') && (fgetc (f
) == '!'))
143 switch (c
= fgetc (f
))
145 case /*WHITE_SPACES */ ' ':
152 return scm_cat_path (0L, tbuf
, 0L);
159 return scm_cat_path (0L, name
, 0L);
163 /* Read a \nnn-style escape. We've just read the backslash. */
165 script_get_octal (FILE *f
)
166 #define FUNC_NAME "script_get_octal"
171 for (i
= 0; i
< 3; i
++)
174 if ('0' <= c
&& c
<= '7')
175 value
= (value
* 8) + (c
- '0');
177 SCM_MISC_ERROR ("malformed script: bad octal backslash escape",
186 script_get_backslash (FILE *f
)
187 #define FUNC_NAME "script_get_backslash"
193 case 'a': return '\a';
194 case 'b': return '\b';
195 case 'f': return '\f';
196 case 'n': return '\n';
197 case 'r': return '\r';
198 case 't': return '\t';
199 case 'v': return '\v';
207 case '0': case '1': case '2': case '3':
208 case '4': case '5': case '6': case '7':
210 return script_get_octal (f
);
213 SCM_MISC_ERROR ("malformed script: backslash followed by EOF", SCM_EOL
);
214 return 0; /* not reached? */
217 SCM_MISC_ERROR ("malformed script: bad backslash sequence", SCM_EOL
);
218 return 0; /* not reached? */
225 script_read_arg (FILE *f
)
226 #define FUNC_NAME "script_read_arg"
229 char *buf
= scm_malloc (size
+ 1);
241 c
= script_get_backslash (f
);
242 /* The above produces a new character to add to the argument.
247 size
= (size
+ 1) * 2;
248 buf
= realloc (buf
, size
);
256 /* This may terminate an arg now, but it will terminate the
257 entire list next time through. */
265 /* Otherwise, those characters terminate the argument; fall
273 SCM_MISC_ERROR ("malformed script: TAB in meta-arguments", SCM_EOL
);
274 return 0; /* not reached? */
282 script_meta_arg_P (char *arg
)
302 scm_get_meta_args (int argc
, char **argv
)
304 int nargc
= argc
, argi
= 1, nargi
= 1;
306 if (!(argc
> 2 && script_meta_arg_P (argv
[1])))
308 if (!(nargv
= (char **) scm_malloc ((1 + nargc
) * sizeof (char *))))
311 while (((argi
+ 1) < argc
) && (script_meta_arg_P (argv
[argi
])))
313 FILE *f
= fopen (argv
[++argi
], "r");
316 nargc
--; /* to compensate for replacement of '\\' */
328 while ((narg
= script_read_arg (f
)))
329 if (!(nargv
= (char **) realloc (nargv
,
330 (1 + ++nargc
) * sizeof (char *))))
333 nargv
[nargi
++] = narg
;
335 nargv
[nargi
++] = argv
[argi
++];
339 nargv
[nargi
++] = argv
[argi
++];
344 scm_count_argv (char **argv
)
353 /* For use in error messages. */
354 char *scm_usage_name
= 0;
357 scm_shell_usage (int fatal
, char *message
)
359 FILE *fp
= (fatal
? stderr
: stdout
);
362 fprintf (fp
, "%s\n", message
);
365 "Usage: %s [OPTION]... [FILE]...\n"
366 "Evaluate Scheme code, interactively or from a script.\n"
368 " [-s] FILE load Scheme source code from FILE, and exit\n"
369 " -c EXPR evalute Scheme expression EXPR, and exit\n"
370 " -- stop scanning arguments; run interactively\n\n"
371 "The above switches stop argument processing, and pass all\n"
372 "remaining arguments as the value of (command-line).\n"
373 "If FILE begins with `-' the -s switch is mandatory.\n"
375 " -L DIRECTORY add DIRECTORY to the front of the module load path\n"
376 " -l FILE load Scheme source code from FILE\n"
377 " -e FUNCTION after reading script, apply FUNCTION to\n"
378 " command line arguments\n"
379 " -ds do -s script at this point\n"
380 " --debug start with debugging evaluator and backtraces\n"
381 " --no-debug start with normal evaluator\n"
382 " Default is to enable debugging for interactive\n"
383 " use, but not for `-s' and `-c'.\n"
384 " --autocompile compile source files automatically\n"
385 " --no-autocompile disable automatic source file compilation\n"
386 " Default is to enable autocompilation of source\n"
388 " -q inhibit loading of user init file\n"
389 " --use-srfi=LS load SRFI modules for the SRFIs in LS,\n"
390 " which is a list of numbers like \"2,13,14\"\n"
391 " -h, --help display this help and exit\n"
392 " -v, --version display version information and exit\n"
393 " \\ read arguments from following script lines\n",
396 emit_bug_reporting_address ();
403 /* Some symbols used by the command-line compiler. */
404 SCM_SYMBOL (sym_load
, "load");
405 SCM_SYMBOL (sym_eval_string
, "eval-string");
406 SCM_SYMBOL (sym_command_line
, "command-line");
407 SCM_SYMBOL (sym_begin
, "begin");
408 SCM_SYMBOL (sym_turn_on_debugging
, "turn-on-debugging");
409 SCM_SYMBOL (sym_load_user_init
, "load-user-init");
410 SCM_SYMBOL (sym_ice_9
, "ice-9");
411 SCM_SYMBOL (sym_top_repl
, "top-repl");
412 SCM_SYMBOL (sym_quit
, "quit");
413 SCM_SYMBOL (sym_use_srfis
, "use-srfis");
414 SCM_SYMBOL (sym_load_path
, "%load-path");
415 SCM_SYMBOL (sym_set_x
, "set!");
416 SCM_SYMBOL (sym_sys_load_should_autocompile
, "%load-should-autocompile");
417 SCM_SYMBOL (sym_cons
, "cons");
418 SCM_SYMBOL (sym_at
, "@");
419 SCM_SYMBOL (sym_atat
, "@@");
420 SCM_SYMBOL (sym_main
, "main");
422 /* Given an array of command-line switches, return a Scheme expression
423 to carry out the actions specified by the switches.
425 If you told me this should have been written in Scheme, I'd
426 probably agree. I'd say I didn't feel comfortable doing that in
427 the present system. You'd say, well, fix the system so you are
428 comfortable doing that. I'd agree again. *shrug*
431 static char guile
[] = "guile";
434 all_symbols (SCM list
)
436 while (scm_is_pair (list
))
438 if (!scm_is_symbol (SCM_CAR (list
)))
440 list
= SCM_CDR (list
);
446 scm_compile_shell_switches (int argc
, char **argv
)
448 SCM tail
= SCM_EOL
; /* We accumulate the list backwards,
449 and then reverse! it before we
451 SCM do_script
= SCM_EOL
; /* The element of the list containing
452 the "load" command, in case we get
454 SCM entry_point
= SCM_EOL
; /* for -e switch */
455 SCM user_load_path
= SCM_EOL
; /* for -L switch */
456 int interactive
= 1; /* Should we go interactive when done? */
457 int inhibit_user_init
= 0; /* Don't load user init file */
458 int turn_on_debugging
= 0;
459 int dont_turn_on_debugging
= 0;
467 scm_usage_name
= strrchr (argv
[0], '/');
468 if (! scm_usage_name
)
469 scm_usage_name
= argv
[0];
473 if (! scm_usage_name
)
474 scm_usage_name
= guile
;
476 for (i
= 1; i
< argc
; i
++)
478 if ((! strcmp (argv
[i
], "-s")) || (argv
[i
][0] != '-')) /* load script */
480 if ((argv
[i
][0] == '-') && (++i
>= argc
))
481 scm_shell_usage (1, "missing argument to `-s' switch");
483 /* If we specified the -ds option, do_script points to the
484 cdr of an expression like (load #f); we replace the car
485 (i.e., the #f) with the script name. */
486 if (!scm_is_null (do_script
))
488 SCM_SETCAR (do_script
, scm_from_locale_string (argv
[i
]));
492 /* Construct an application of LOAD to the script name. */
493 tail
= scm_cons (scm_cons2 (sym_load
,
494 scm_from_locale_string (argv
[i
]),
503 else if (! strcmp (argv
[i
], "-c")) /* evaluate expr */
506 scm_shell_usage (1, "missing argument to `-c' switch");
507 tail
= scm_cons (scm_cons2 (sym_eval_string
,
508 scm_from_locale_string (argv
[i
]),
516 else if (! strcmp (argv
[i
], "--")) /* end args; go interactive */
522 else if (! strcmp (argv
[i
], "-l")) /* load a file */
525 tail
= scm_cons (scm_cons2 (sym_load
,
526 scm_from_locale_string (argv
[i
]),
530 scm_shell_usage (1, "missing argument to `-l' switch");
533 else if (! strcmp (argv
[i
], "-L")) /* add to %load-path */
537 scm_cons (scm_list_3 (sym_set_x
,
539 scm_list_3 (sym_cons
,
540 scm_from_locale_string (argv
[i
]),
544 scm_shell_usage (1, "missing argument to `-L' switch");
547 else if (! strcmp (argv
[i
], "-e")) /* entry point */
552 = scm_open_input_string (scm_from_locale_string (argv
[i
]));
553 SCM arg1
= scm_read (port
);
554 SCM arg2
= scm_read (port
);
556 /* Recognize syntax of certain versions of Guile 1.4 and
557 transform to (@ MODULE-NAME FUNC).
559 if (scm_is_false (scm_eof_object_p (arg2
)))
560 entry_point
= scm_list_3 (sym_at
, arg1
, arg2
);
561 else if (scm_is_pair (arg1
)
562 && !(scm_is_eq (SCM_CAR (arg1
), sym_at
)
563 || scm_is_eq (SCM_CAR (arg1
), sym_atat
))
564 && all_symbols (arg1
))
565 entry_point
= scm_list_3 (sym_at
, arg1
, sym_main
);
570 scm_shell_usage (1, "missing argument to `-e' switch");
573 else if (! strcmp (argv
[i
], "-ds")) /* do script here */
575 /* We put a dummy "load" expression, and let the -s put the
577 if (!scm_is_null (do_script
))
578 scm_shell_usage (1, "the -ds switch may only be specified once");
579 do_script
= scm_cons (SCM_BOOL_F
, SCM_EOL
);
580 tail
= scm_cons (scm_cons (sym_load
, do_script
),
584 else if (! strcmp (argv
[i
], "--debug"))
586 turn_on_debugging
= 1;
587 dont_turn_on_debugging
= 0;
590 else if (! strcmp (argv
[i
], "--no-debug"))
592 dont_turn_on_debugging
= 1;
593 turn_on_debugging
= 0;
596 /* Do autocompile on/off now, because the form itself might need this
598 else if (! strcmp (argv
[i
], "--autocompile"))
599 scm_variable_set_x (scm_c_lookup ("%load-should-autocompile"),
602 else if (! strcmp (argv
[i
], "--no-autocompile"))
603 scm_variable_set_x (scm_c_lookup ("%load-should-autocompile"),
606 else if (! strcmp (argv
[i
], "-q")) /* don't load user init */
607 inhibit_user_init
= 1;
609 else if (! strncmp (argv
[i
], "--use-srfi=", 11)) /* load SRFIs */
611 SCM srfis
= SCM_EOL
; /* List of requested SRFIs. */
612 char * p
= argv
[i
] + 11;
618 num
= strtol (p
, &end
, 10);
621 srfis
= scm_cons (scm_from_long (num
), srfis
);
627 scm_shell_usage (1, "invalid SRFI specification");
633 scm_shell_usage (1, "invalid SRFI specification");
635 if (scm_ilength (srfis
) <= 0)
636 scm_shell_usage (1, "invalid SRFI specification");
637 srfis
= scm_reverse_x (srfis
, SCM_UNDEFINED
);
638 tail
= scm_cons (scm_list_2 (sym_use_srfis
,
639 scm_list_2 (scm_sym_quote
, srfis
)),
643 else if (! strcmp (argv
[i
], "-h")
644 || ! strcmp (argv
[i
], "--help"))
646 scm_shell_usage (0, 0);
650 else if (! strcmp (argv
[i
], "-v")
651 || ! strcmp (argv
[i
], "--version"))
653 /* Print version number. */
654 version_etc (stdout
, scm_usage_name
, PACKAGE_NAME
, PACKAGE_VERSION
,
655 /* XXX: Use gettext for the string below. */
656 "the Guile developers", NULL
);
662 fprintf (stderr
, "%s: Unrecognized switch `%s'\n",
663 scm_usage_name
, argv
[i
]);
664 scm_shell_usage (1, 0);
668 /* Check to make sure the -ds got a -s. */
669 if (!scm_is_null (do_script
))
670 scm_shell_usage (1, "the `-ds' switch requires the use of `-s' as well");
672 /* Make any remaining arguments available to the
673 script/command/whatever. */
674 scm_set_program_arguments (argc
? argc
- i
: 0, argv
+ i
, argv0
);
676 /* Handle the `-e' switch, if it was specified. */
677 if (!scm_is_null (entry_point
))
678 tail
= scm_cons (scm_cons2 (entry_point
,
679 scm_cons (sym_command_line
, SCM_EOL
),
683 /* If we didn't end with a -c or a -s, start the repl. */
686 tail
= scm_cons (scm_list_1 (scm_list_3
688 scm_list_2 (sym_ice_9
, sym_top_repl
),
694 /* After doing all the other actions prescribed by the command line,
696 tail
= scm_cons (scm_cons (sym_quit
, SCM_EOL
),
700 /* After the following line, actions will be added to the front. */
701 tail
= scm_reverse_x (tail
, SCM_UNDEFINED
);
703 /* add the user-specified load path here, so it won't be in effect
704 during the loading of the user's customization file. */
705 if(!scm_is_null(user_load_path
))
707 tail
= scm_append_x( scm_cons2(user_load_path
, tail
, SCM_EOL
) );
710 /* If we didn't end with a -c or a -s and didn't supply a -q, load
711 the user's customization file. */
712 if (interactive
&& !inhibit_user_init
)
714 tail
= scm_cons (scm_cons (sym_load_user_init
, SCM_EOL
), tail
);
717 /* If debugging was requested, or we are interactive and debugging
718 was not explicitly turned off, turn on debugging. */
719 if (turn_on_debugging
|| (interactive
&& !dont_turn_on_debugging
))
721 tail
= scm_cons (scm_cons (sym_turn_on_debugging
, SCM_EOL
), tail
);
725 SCM val
= scm_cons (sym_begin
, tail
);
727 /* Wrap the expression in a prompt. */
728 val
= scm_list_2 (scm_list_3 (scm_sym_at
,
729 scm_list_2 (scm_from_locale_symbol ("ice-9"),
730 scm_from_locale_symbol ("control")),
731 scm_from_locale_symbol ("%")),
735 scm_write (val
, SCM_UNDEFINED
);
736 scm_newline (SCM_UNDEFINED
);
745 scm_shell (int argc
, char **argv
)
747 /* If present, add SCSH-style meta-arguments from the top of the
748 script file to the argument vector. See the SCSH manual: "The
749 meta argument" for more details. */
751 char **new_argv
= scm_get_meta_args (argc
, argv
);
756 argc
= scm_count_argv (new_argv
);
760 exit (scm_exit_status (scm_eval_x (scm_compile_shell_switches (argc
, argv
),
761 scm_current_module ())));
768 #include "libguile/script.x"