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"
43 #include "libguile/vm.h"
50 #include <unistd.h> /* for X_OK define */
57 /* Concatentate str2 onto str1 at position n and return concatenated
58 string if file exists; 0 otherwise. */
61 scm_cat_path (char *str1
, const char *str2
, long n
)
67 size_t len
= strlen (str1
);
68 str1
= (char *) realloc (str1
, (size_t) (len
+ n
+ 1));
71 strncat (str1
+ len
, str2
, n
);
74 str1
= (char *) scm_malloc ((size_t) (n
+ 1));
78 strncat (str1
, str2
, n
);
84 scm_try_path (char *path
)
87 /* fprintf(stderr, "Trying %s\n", path);fflush(stderr); */
90 SCM_SYSCALL (f
= fopen (path
, "r");
102 scm_sep_init_try (char *path
, const char *sep
, const char *initname
)
105 path
= scm_cat_path (path
, sep
, 0L);
107 path
= scm_cat_path (path
, initname
, 0L);
108 return scm_try_path (path
);
112 #ifndef LINE_INCREMENTORS
113 #define LINE_INCREMENTORS '\n'
115 #define WHITE_SPACES ' ':case '\t':case '\r':case '\f':case 26
117 #define WHITE_SPACES ' ':case '\t':case '\r':case '\f'
118 #endif /* def MSDOS */
119 #endif /* ndef LINE_INCREMENTORS */
122 #define MAXPATHLEN 80
123 #endif /* ndef MAXPATHLEN */
126 #endif /* ndef X_OK */
129 scm_find_executable (const char *name
)
131 char tbuf
[MAXPATHLEN
];
135 /* fprintf(stderr, "s_f_e checking access %s ->%d\n", name, access(name, X_OK)); fflush(stderr); */
136 if (access (name
, X_OK
))
138 f
= fopen (name
, "r");
141 if ((fgetc (f
) == '#') && (fgetc (f
) == '!'))
144 switch (c
= fgetc (f
))
146 case /*WHITE_SPACES */ ' ':
153 return scm_cat_path (0L, tbuf
, 0L);
160 return scm_cat_path (0L, name
, 0L);
164 /* Read a \nnn-style escape. We've just read the backslash. */
166 script_get_octal (FILE *f
)
167 #define FUNC_NAME "script_get_octal"
172 for (i
= 0; i
< 3; i
++)
175 if ('0' <= c
&& c
<= '7')
176 value
= (value
* 8) + (c
- '0');
178 SCM_MISC_ERROR ("malformed script: bad octal backslash escape",
187 script_get_backslash (FILE *f
)
188 #define FUNC_NAME "script_get_backslash"
194 case 'a': return '\a';
195 case 'b': return '\b';
196 case 'f': return '\f';
197 case 'n': return '\n';
198 case 'r': return '\r';
199 case 't': return '\t';
200 case 'v': return '\v';
208 case '0': case '1': case '2': case '3':
209 case '4': case '5': case '6': case '7':
211 return script_get_octal (f
);
214 SCM_MISC_ERROR ("malformed script: backslash followed by EOF", SCM_EOL
);
215 return 0; /* not reached? */
218 SCM_MISC_ERROR ("malformed script: bad backslash sequence", SCM_EOL
);
219 return 0; /* not reached? */
226 script_read_arg (FILE *f
)
227 #define FUNC_NAME "script_read_arg"
230 char *buf
= scm_malloc (size
+ 1);
242 c
= script_get_backslash (f
);
243 /* The above produces a new character to add to the argument.
248 size
= (size
+ 1) * 2;
249 buf
= realloc (buf
, size
);
257 /* This may terminate an arg now, but it will terminate the
258 entire list next time through. */
266 /* Otherwise, those characters terminate the argument; fall
274 SCM_MISC_ERROR ("malformed script: TAB in meta-arguments", SCM_EOL
);
275 return 0; /* not reached? */
283 script_meta_arg_P (char *arg
)
303 scm_get_meta_args (int argc
, char **argv
)
305 int nargc
= argc
, argi
= 1, nargi
= 1;
307 if (!(argc
> 2 && script_meta_arg_P (argv
[1])))
309 if (!(nargv
= (char **) scm_malloc ((1 + nargc
) * sizeof (char *))))
312 while (((argi
+ 1) < argc
) && (script_meta_arg_P (argv
[argi
])))
314 FILE *f
= fopen (argv
[++argi
], "r");
317 nargc
--; /* to compensate for replacement of '\\' */
329 while ((narg
= script_read_arg (f
)))
330 if (!(nargv
= (char **) realloc (nargv
,
331 (1 + ++nargc
) * sizeof (char *))))
334 nargv
[nargi
++] = narg
;
336 nargv
[nargi
++] = argv
[argi
++];
340 nargv
[nargi
++] = argv
[argi
++];
345 scm_count_argv (char **argv
)
354 /* For use in error messages. */
355 char *scm_usage_name
= 0;
358 scm_shell_usage (int fatal
, char *message
)
360 FILE *fp
= (fatal
? stderr
: stdout
);
363 fprintf (fp
, "%s\n", message
);
366 "Usage: %s [OPTION]... [FILE]...\n"
367 "Evaluate Scheme code, interactively or from a script.\n"
369 " [-s] FILE load Scheme source code from FILE, and exit\n"
370 " -c EXPR evalute Scheme expression EXPR, and exit\n"
371 " -- stop scanning arguments; run interactively\n\n"
372 "The above switches stop argument processing, and pass all\n"
373 "remaining arguments as the value of (command-line).\n"
374 "If FILE begins with `-' the -s switch is mandatory.\n"
376 " -L DIRECTORY add DIRECTORY to the front of the module load path\n"
377 " -l FILE load Scheme source code from FILE\n"
378 " -e FUNCTION after reading script, apply FUNCTION to\n"
379 " command line arguments\n"
380 " -ds do -s script at this point\n"
381 " --debug start with debugging evaluator and backtraces\n"
382 " --no-debug start with normal evaluator\n"
383 " Default is to enable debugging for interactive\n"
384 " use, but not for `-s' and `-c'.\n"
385 " --autocompile compile source files automatically\n"
386 " --no-autocompile disable automatic source file compilation\n"
387 " Default is to enable autocompilation of source\n"
389 " -q inhibit loading of user init file\n"
390 " --use-srfi=LS load SRFI modules for the SRFIs in LS,\n"
391 " which is a list of numbers like \"2,13,14\"\n"
392 " -h, --help display this help and exit\n"
393 " -v, --version display version information and exit\n"
394 " \\ read arguments from following script lines\n",
397 emit_bug_reporting_address ();
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_ice_9
, "ice-9");
412 SCM_SYMBOL (sym_top_repl
, "top-repl");
413 SCM_SYMBOL (sym_quit
, "quit");
414 SCM_SYMBOL (sym_use_srfis
, "use-srfis");
415 SCM_SYMBOL (sym_load_path
, "%load-path");
416 SCM_SYMBOL (sym_set_x
, "set!");
417 SCM_SYMBOL (sym_sys_load_should_autocompile
, "%load-should-autocompile");
418 SCM_SYMBOL (sym_cons
, "cons");
419 SCM_SYMBOL (sym_at
, "@");
420 SCM_SYMBOL (sym_atat
, "@@");
421 SCM_SYMBOL (sym_main
, "main");
423 /* Given an array of command-line switches, return a Scheme expression
424 to carry out the actions specified by the switches.
426 If you told me this should have been written in Scheme, I'd
427 probably agree. I'd say I didn't feel comfortable doing that in
428 the present system. You'd say, well, fix the system so you are
429 comfortable doing that. I'd agree again. *shrug*
432 static char guile
[] = "guile";
435 all_symbols (SCM list
)
437 while (scm_is_pair (list
))
439 if (!scm_is_symbol (SCM_CAR (list
)))
441 list
= SCM_CDR (list
);
447 scm_compile_shell_switches (int argc
, char **argv
)
449 SCM tail
= SCM_EOL
; /* We accumulate the list backwards,
450 and then reverse! it before we
452 SCM do_script
= SCM_EOL
; /* The element of the list containing
453 the "load" command, in case we get
455 SCM entry_point
= SCM_EOL
; /* for -e switch */
456 SCM user_load_path
= SCM_EOL
; /* for -L switch */
457 int interactive
= 1; /* Should we go interactive when done? */
458 int inhibit_user_init
= 0; /* Don't load user init file */
459 int turn_on_debugging
= 0;
460 int dont_turn_on_debugging
= 0;
468 scm_usage_name
= strrchr (argv
[0], '/');
469 if (! scm_usage_name
)
470 scm_usage_name
= argv
[0];
474 if (! scm_usage_name
)
475 scm_usage_name
= guile
;
477 for (i
= 1; i
< argc
; i
++)
479 if ((! strcmp (argv
[i
], "-s")) || (argv
[i
][0] != '-')) /* load script */
481 if ((argv
[i
][0] == '-') && (++i
>= argc
))
482 scm_shell_usage (1, "missing argument to `-s' switch");
484 /* If we specified the -ds option, do_script points to the
485 cdr of an expression like (load #f); we replace the car
486 (i.e., the #f) with the script name. */
487 if (!scm_is_null (do_script
))
489 SCM_SETCAR (do_script
, scm_from_locale_string (argv
[i
]));
493 /* Construct an application of LOAD to the script name. */
494 tail
= scm_cons (scm_cons2 (sym_load
,
495 scm_from_locale_string (argv
[i
]),
504 else if (! strcmp (argv
[i
], "-c")) /* evaluate expr */
507 scm_shell_usage (1, "missing argument to `-c' switch");
508 tail
= scm_cons (scm_cons2 (sym_eval_string
,
509 scm_from_locale_string (argv
[i
]),
517 else if (! strcmp (argv
[i
], "--")) /* end args; go interactive */
523 else if (! strcmp (argv
[i
], "-l")) /* load a file */
526 tail
= scm_cons (scm_cons2 (sym_load
,
527 scm_from_locale_string (argv
[i
]),
531 scm_shell_usage (1, "missing argument to `-l' switch");
534 else if (! strcmp (argv
[i
], "-L")) /* add to %load-path */
538 scm_cons (scm_list_3 (sym_set_x
,
540 scm_list_3 (sym_cons
,
541 scm_from_locale_string (argv
[i
]),
545 scm_shell_usage (1, "missing argument to `-L' switch");
548 else if (! strcmp (argv
[i
], "-e")) /* entry point */
553 = scm_open_input_string (scm_from_locale_string (argv
[i
]));
554 SCM arg1
= scm_read (port
);
555 SCM arg2
= scm_read (port
);
557 /* Recognize syntax of certain versions of Guile 1.4 and
558 transform to (@ MODULE-NAME FUNC).
560 if (scm_is_false (scm_eof_object_p (arg2
)))
561 entry_point
= scm_list_3 (sym_at
, arg1
, arg2
);
562 else if (scm_is_pair (arg1
)
563 && !(scm_is_eq (SCM_CAR (arg1
), sym_at
)
564 || scm_is_eq (SCM_CAR (arg1
), sym_atat
))
565 && all_symbols (arg1
))
566 entry_point
= scm_list_3 (sym_at
, arg1
, sym_main
);
571 scm_shell_usage (1, "missing argument to `-e' switch");
574 else if (! strcmp (argv
[i
], "-ds")) /* do script here */
576 /* We put a dummy "load" expression, and let the -s put the
578 if (!scm_is_null (do_script
))
579 scm_shell_usage (1, "the -ds switch may only be specified once");
580 do_script
= scm_cons (SCM_BOOL_F
, SCM_EOL
);
581 tail
= scm_cons (scm_cons (sym_load
, do_script
),
585 else if (! strcmp (argv
[i
], "--debug"))
587 turn_on_debugging
= 1;
588 dont_turn_on_debugging
= 0;
591 else if (! strcmp (argv
[i
], "--no-debug"))
593 dont_turn_on_debugging
= 1;
594 turn_on_debugging
= 0;
597 /* Do autocompile on/off now, because the form itself might need this
599 else if (! strcmp (argv
[i
], "--autocompile"))
600 scm_variable_set_x (scm_c_lookup ("%load-should-autocompile"),
603 else if (! strcmp (argv
[i
], "--no-autocompile"))
604 scm_variable_set_x (scm_c_lookup ("%load-should-autocompile"),
607 else if (! strcmp (argv
[i
], "-q")) /* don't load user init */
608 inhibit_user_init
= 1;
610 else if (! strncmp (argv
[i
], "--use-srfi=", 11)) /* load SRFIs */
612 SCM srfis
= SCM_EOL
; /* List of requested SRFIs. */
613 char * p
= argv
[i
] + 11;
619 num
= strtol (p
, &end
, 10);
622 srfis
= scm_cons (scm_from_long (num
), srfis
);
628 scm_shell_usage (1, "invalid SRFI specification");
634 scm_shell_usage (1, "invalid SRFI specification");
636 if (scm_ilength (srfis
) <= 0)
637 scm_shell_usage (1, "invalid SRFI specification");
638 srfis
= scm_reverse_x (srfis
, SCM_UNDEFINED
);
639 tail
= scm_cons (scm_list_2 (sym_use_srfis
,
640 scm_list_2 (scm_sym_quote
, srfis
)),
644 else if (! strcmp (argv
[i
], "-h")
645 || ! strcmp (argv
[i
], "--help"))
647 scm_shell_usage (0, 0);
651 else if (! strcmp (argv
[i
], "-v")
652 || ! strcmp (argv
[i
], "--version"))
654 /* Print version number. */
655 version_etc (stdout
, scm_usage_name
, PACKAGE_NAME
, PACKAGE_VERSION
,
656 /* XXX: Use gettext for the string below. */
657 "the Guile developers", NULL
);
663 fprintf (stderr
, "%s: Unrecognized switch `%s'\n",
664 scm_usage_name
, argv
[i
]);
665 scm_shell_usage (1, 0);
669 /* Check to make sure the -ds got a -s. */
670 if (!scm_is_null (do_script
))
671 scm_shell_usage (1, "the `-ds' switch requires the use of `-s' as well");
673 /* Make any remaining arguments available to the
674 script/command/whatever. */
675 scm_set_program_arguments (argc
? argc
- i
: 0, argv
+ i
, argv0
);
677 /* Handle the `-e' switch, if it was specified. */
678 if (!scm_is_null (entry_point
))
679 tail
= scm_cons (scm_cons2 (entry_point
,
680 scm_cons (sym_command_line
, SCM_EOL
),
684 /* If we didn't end with a -c or a -s, start the repl. */
687 tail
= scm_cons (scm_list_1 (scm_list_3
689 scm_list_2 (sym_ice_9
, sym_top_repl
),
695 /* After doing all the other actions prescribed by the command line,
697 tail
= scm_cons (scm_cons (sym_quit
, SCM_EOL
),
701 /* After the following line, actions will be added to the front. */
702 tail
= scm_reverse_x (tail
, SCM_UNDEFINED
);
704 /* add the user-specified load path here, so it won't be in effect
705 during the loading of the user's customization file. */
706 if(!scm_is_null(user_load_path
))
708 tail
= scm_append_x( scm_cons2(user_load_path
, tail
, SCM_EOL
) );
711 /* If we didn't end with a -c or a -s and didn't supply a -q, load
712 the user's customization file. */
713 if (interactive
&& !inhibit_user_init
)
715 tail
= scm_cons (scm_cons (sym_load_user_init
, SCM_EOL
), tail
);
718 /* If debugging was requested, or we are interactive and debugging
719 was not explicitly turned off, turn on debugging. */
720 if (turn_on_debugging
|| (interactive
&& !dont_turn_on_debugging
))
722 /* FIXME: backtraces and positions should always be on (?) */
723 tail
= scm_cons (scm_cons (sym_turn_on_debugging
, SCM_EOL
), tail
);
724 scm_c_set_default_vm_engine_x (SCM_VM_DEBUG_ENGINE
);
725 scm_c_set_vm_engine_x (scm_the_vm (), SCM_VM_DEBUG_ENGINE
);
729 SCM val
= scm_cons (sym_begin
, tail
);
731 /* Wrap the expression in a prompt. */
732 val
= scm_list_2 (scm_list_3 (scm_sym_at
,
733 scm_list_2 (scm_from_locale_symbol ("ice-9"),
734 scm_from_locale_symbol ("control")),
735 scm_from_locale_symbol ("%")),
739 scm_write (val
, SCM_UNDEFINED
);
740 scm_newline (SCM_UNDEFINED
);
749 scm_shell (int argc
, char **argv
)
751 /* If present, add SCSH-style meta-arguments from the top of the
752 script file to the argument vector. See the SCSH manual: "The
753 meta argument" for more details. */
755 char **new_argv
= scm_get_meta_args (argc
, argv
);
760 argc
= scm_count_argv (new_argv
);
764 exit (scm_exit_status (scm_eval_x (scm_compile_shell_switches (argc
, argv
),
765 scm_current_module ())));
772 #include "libguile/script.x"