Merge branch 'bdw-gc-static-alloc'
[bpt/guile.git] / libguile / script.c
CommitLineData
25a640ca 1/* Copyright (C) 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
73be1d9e 2 * This library is free software; you can redistribute it and/or
53befeb7
NJ
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.
224c49f9 6 *
53befeb7
NJ
7 * This library is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
224c49f9 11 *
73be1d9e
MV
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
53befeb7
NJ
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301 USA
73be1d9e 16 */
224c49f9
JB
17
18/* "script.c" argv tricks for `#!' scripts.
d3be4a7a 19 Authors: Aubrey Jaffer and Jim Blandy */
224c49f9 20
dbb605f5 21#ifdef HAVE_CONFIG_H
773ca93e
RB
22# include <config.h>
23#endif
6e8d25a6 24
224c49f9 25#include <stdio.h>
e6e2e95a 26#include <errno.h>
224c49f9 27#include <ctype.h>
e6e2e95a 28
a0599745 29#include "libguile/_scm.h"
89bc270d
HWN
30#include "libguile/eval.h"
31#include "libguile/feature.h"
a0599745 32#include "libguile/load.h"
ee001750 33#include "libguile/private-gc.h" /* scm_getenv_int */
89bc270d 34#include "libguile/read.h"
a0599745 35#include "libguile/script.h"
89bc270d
HWN
36#include "libguile/strings.h"
37#include "libguile/strports.h"
38#include "libguile/validate.h"
39#include "libguile/version.h"
224c49f9 40
bd9e24b3
GH
41#ifdef HAVE_STRING_H
42#include <string.h>
43#endif
44
d3be4a7a 45#ifdef HAVE_UNISTD_H
224c49f9 46#include <unistd.h> /* for X_OK define */
224c49f9
JB
47#endif
48
7beabedb
MG
49#ifdef HAVE_IO_H
50#include <io.h>
51#endif
52
224c49f9
JB
53/* Concatentate str2 onto str1 at position n and return concatenated
54 string if file exists; 0 otherwise. */
55
56static char *
6e8d25a6 57scm_cat_path (char *str1, const char *str2, long n)
224c49f9
JB
58{
59 if (!n)
60 n = strlen (str2);
61 if (str1)
62 {
1be6b49c
ML
63 size_t len = strlen (str1);
64 str1 = (char *) realloc (str1, (size_t) (len + n + 1));
224c49f9
JB
65 if (!str1)
66 return 0L;
67 strncat (str1 + len, str2, n);
68 return str1;
69 }
67329a9e 70 str1 = (char *) scm_malloc ((size_t) (n + 1));
224c49f9
JB
71 if (!str1)
72 return 0L;
73 str1[0] = 0;
74 strncat (str1, str2, n);
75 return str1;
76}
77
78#if 0
79static char *
6e8d25a6 80scm_try_path (char *path)
224c49f9
JB
81{
82 FILE *f;
83 /* fprintf(stderr, "Trying %s\n", path);fflush(stderr); */
84 if (!path)
85 return 0L;
86 SCM_SYSCALL (f = fopen (path, "r");
87 );
88 if (f)
89 {
90 fclose (f);
91 return path;
92 }
93 free (path);
94 return 0L;
95}
96
97static char *
6e8d25a6 98scm_sep_init_try (char *path, const char *sep, const char *initname)
224c49f9
JB
99{
100 if (path)
101 path = scm_cat_path (path, sep, 0L);
102 if (path)
103 path = scm_cat_path (path, initname, 0L);
104 return scm_try_path (path);
105}
106#endif
107
108#ifndef LINE_INCREMENTORS
109#define LINE_INCREMENTORS '\n'
110#ifdef MSDOS
111#define WHITE_SPACES ' ':case '\t':case '\r':case '\f':case 26
112#else
113#define WHITE_SPACES ' ':case '\t':case '\r':case '\f'
114#endif /* def MSDOS */
115#endif /* ndef LINE_INCREMENTORS */
116
117#ifndef MAXPATHLEN
118#define MAXPATHLEN 80
119#endif /* ndef MAXPATHLEN */
120#ifndef X_OK
121#define X_OK 1
122#endif /* ndef X_OK */
123
224c49f9 124char *
d3be4a7a 125scm_find_executable (const char *name)
224c49f9
JB
126{
127 char tbuf[MAXPATHLEN];
d9c36d2a 128 int i = 0, c;
224c49f9
JB
129 FILE *f;
130
131 /* fprintf(stderr, "s_f_e checking access %s ->%d\n", name, access(name, X_OK)); fflush(stderr); */
132 if (access (name, X_OK))
133 return 0L;
134 f = fopen (name, "r");
135 if (!f)
136 return 0L;
137 if ((fgetc (f) == '#') && (fgetc (f) == '!'))
138 {
139 while (1)
d9c36d2a 140 switch (c = fgetc (f))
224c49f9
JB
141 {
142 case /*WHITE_SPACES */ ' ':
143 case '\t':
144 case '\r':
145 case '\f':
146 case EOF:
d9c36d2a 147 tbuf[i] = 0;
224c49f9
JB
148 fclose (f);
149 return scm_cat_path (0L, tbuf, 0L);
d9c36d2a
MV
150 default:
151 tbuf[i++] = c;
152 break;
224c49f9
JB
153 }
154 }
155 fclose (f);
156 return scm_cat_path (0L, name, 0L);
157}
224c49f9 158
224c49f9
JB
159
160/* Read a \nnn-style escape. We've just read the backslash. */
161static int
6e8d25a6 162script_get_octal (FILE *f)
db4b4ca6 163#define FUNC_NAME "script_get_octal"
224c49f9
JB
164{
165 int i;
166 int value = 0;
167
168 for (i = 0; i < 3; i++)
169 {
170 int c = getc (f);
171 if ('0' <= c && c <= '7')
172 value = (value * 8) + (c - '0');
173 else
db4b4ca6
DH
174 SCM_MISC_ERROR ("malformed script: bad octal backslash escape",
175 SCM_EOL);
224c49f9
JB
176 }
177 return value;
178}
db4b4ca6 179#undef FUNC_NAME
224c49f9
JB
180
181
182static int
6e8d25a6 183script_get_backslash (FILE *f)
db4b4ca6 184#define FUNC_NAME "script_get_backslash"
224c49f9
JB
185{
186 int c = getc (f);
187
188 switch (c)
189 {
190 case 'a': return '\a';
191 case 'b': return '\b';
192 case 'f': return '\f';
193 case 'n': return '\n';
194 case 'r': return '\r';
195 case 't': return '\t';
196 case 'v': return '\v';
197
198 case '\\':
199 case ' ':
200 case '\t':
201 case '\n':
202 return c;
203
204 case '0': case '1': case '2': case '3':
205 case '4': case '5': case '6': case '7':
206 ungetc (c, f);
207 return script_get_octal (f);
db4b4ca6 208
224c49f9 209 case EOF:
db4b4ca6 210 SCM_MISC_ERROR ("malformed script: backslash followed by EOF", SCM_EOL);
224c49f9
JB
211 return 0; /* not reached? */
212
213 default:
db4b4ca6 214 SCM_MISC_ERROR ("malformed script: bad backslash sequence", SCM_EOL);
224c49f9
JB
215 return 0; /* not reached? */
216 }
217}
db4b4ca6 218#undef FUNC_NAME
224c49f9
JB
219
220
221static char *
6e8d25a6 222script_read_arg (FILE *f)
db4b4ca6 223#define FUNC_NAME "script_read_arg"
224c49f9 224{
1be6b49c 225 size_t size = 7;
67329a9e 226 char *buf = scm_malloc (size + 1);
1be6b49c 227 size_t len = 0;
224c49f9
JB
228
229 if (! buf)
230 return 0;
231
232 for (;;)
233 {
234 int c = getc (f);
235 switch (c)
236 {
237 case '\\':
238 c = script_get_backslash (f);
239 /* The above produces a new character to add to the argument.
240 Fall through. */
241 default:
242 if (len >= size)
243 {
244 size = (size + 1) * 2;
245 buf = realloc (buf, size);
246 if (! buf)
247 return 0;
248 }
249 buf[len++] = c;
250 break;
251
252 case '\n':
253 /* This may terminate an arg now, but it will terminate the
254 entire list next time through. */
255 ungetc ('\n', f);
256 case EOF:
257 if (len == 0)
258 {
259 free (buf);
260 return 0;
261 }
262 /* Otherwise, those characters terminate the argument; fall
263 through. */
264 case ' ':
265 buf[len] = '\0';
266 return buf;
267
268 case '\t':
269 free (buf);
db4b4ca6 270 SCM_MISC_ERROR ("malformed script: TAB in meta-arguments", SCM_EOL);
224c49f9
JB
271 return 0; /* not reached? */
272 }
273 }
274}
db4b4ca6 275#undef FUNC_NAME
224c49f9
JB
276
277
278static int
6e8d25a6 279script_meta_arg_P (char *arg)
224c49f9
JB
280{
281 if ('\\' != arg[0])
282 return 0L;
283#ifdef MSDOS
284 return !arg[1];
285#else
286 switch (arg[1])
287 {
288 case 0:
289 case '%':
290 case WHITE_SPACES:
291 return !0;
292 default:
293 return 0L;
294 }
295#endif
296}
297
298char **
6e8d25a6 299scm_get_meta_args (int argc, char **argv)
224c49f9
JB
300{
301 int nargc = argc, argi = 1, nargi = 1;
302 char *narg, **nargv;
303 if (!(argc > 2 && script_meta_arg_P (argv[1])))
304 return 0L;
67329a9e 305 if (!(nargv = (char **) scm_malloc ((1 + nargc) * sizeof (char *))))
224c49f9
JB
306 return 0L;
307 nargv[0] = argv[0];
308 while (((argi + 1) < argc) && (script_meta_arg_P (argv[argi])))
309 {
310 FILE *f = fopen (argv[++argi], "r");
311 if (f)
312 {
313 nargc--; /* to compensate for replacement of '\\' */
314 while (1)
315 switch (getc (f))
316 {
317 case EOF:
318 return 0L;
319 default:
320 continue;
321 case '\n':
322 goto found_args;
323 }
324 found_args:
325 while ((narg = script_read_arg (f)))
326 if (!(nargv = (char **) realloc (nargv,
327 (1 + ++nargc) * sizeof (char *))))
328 return 0L;
329 else
330 nargv[nargi++] = narg;
331 fclose (f);
332 nargv[nargi++] = argv[argi++];
333 }
334 }
335 while (argi <= argc)
336 nargv[nargi++] = argv[argi++];
337 return nargv;
338}
339
340int
6e8d25a6 341scm_count_argv (char **argv)
224c49f9
JB
342{
343 int argc = 0;
344 while (argv[argc])
345 argc++;
346 return argc;
347}
348
349
350/* For use in error messages. */
351char *scm_usage_name = 0;
352
353void
354scm_shell_usage (int fatal, char *message)
355{
48dc9f34
KR
356 FILE *fp = (fatal ? stderr : stdout);
357
224c49f9 358 if (message)
48dc9f34 359 fprintf (fp, "%s\n", message);
224c49f9 360
48dc9f34 361 fprintf (fp,
224c49f9
JB
362 "Usage: %s OPTION ...\n"
363 "Evaluate Scheme code, interactively or from a script.\n"
364 "\n"
defdc4b4 365 " [-s] FILE load Scheme source code from FILE, and exit\n"
224c49f9
JB
366 " -c EXPR evalute Scheme expression EXPR, and exit\n"
367 " -- stop scanning arguments; run interactively\n"
368 "The above switches stop argument processing, and pass all\n"
369 "remaining arguments as the value of (command-line).\n"
defdc4b4 370 "If FILE begins with `-' the -s switch is mandatory.\n"
224c49f9 371 "\n"
caa29067 372 " -L DIRECTORY add DIRECTORY to the front of the module load path\n"
224c49f9
JB
373 " -l FILE load Scheme source code from FILE\n"
374 " -e FUNCTION after reading script, apply FUNCTION to\n"
375 " command line arguments\n"
376 " -ds do -s script at this point\n"
d150e491 377 " --debug start with debugging evaluator and backtraces\n"
3682a51a
MV
378 " --no-debug start with normal evaluator\n"
379 " Default is to enable debugging for interactive\n"
380 " use, but not for `-s' and `-c'.\n"
ee001750
AW
381 " --autocompile compile source files automatically\n"
382 " --no-autocompile disable automatic source file compilation\n"
383 " Default is to enable autocompilation of source\n"
384 " files.\n"
97c524bd 385 " -q inhibit loading of user init file\n"
224c49f9 386 " --emacs enable Emacs protocol (experimental)\n"
39cde5c5
MG
387 " --use-srfi=LS load SRFI modules for the SRFIs in LS,\n"
388 " which is a list of numbers like \"2,13,14\"\n"
224c49f9
JB
389 " -h, --help display this help and exit\n"
390 " -v, --version display version information and exit\n"
e69681ae
KR
391 " \\ read arguments from following script lines\n"
392 "\n"
55aae983 393 "Please report bugs to bug-guile@gnu.org\n",
224c49f9
JB
394 scm_usage_name);
395
396 if (fatal)
8e568309 397 exit (fatal);
224c49f9
JB
398}
399
400
401/* Some symbols used by the command-line compiler. */
402SCM_SYMBOL (sym_load, "load");
403SCM_SYMBOL (sym_eval_string, "eval-string");
404SCM_SYMBOL (sym_command_line, "command-line");
405SCM_SYMBOL (sym_begin, "begin");
3682a51a 406SCM_SYMBOL (sym_turn_on_debugging, "turn-on-debugging");
224c49f9
JB
407SCM_SYMBOL (sym_load_user_init, "load-user-init");
408SCM_SYMBOL (sym_top_repl, "top-repl");
409SCM_SYMBOL (sym_quit, "quit");
39cde5c5 410SCM_SYMBOL (sym_use_srfis, "use-srfis");
caa29067
MV
411SCM_SYMBOL (sym_load_path, "%load-path");
412SCM_SYMBOL (sym_set_x, "set!");
ee001750 413SCM_SYMBOL (sym_sys_load_should_autocompile, "%load-should-autocompile");
caa29067 414SCM_SYMBOL (sym_cons, "cons");
39e707a1
MV
415SCM_SYMBOL (sym_at, "@");
416SCM_SYMBOL (sym_atat, "@@");
417SCM_SYMBOL (sym_main, "main");
224c49f9
JB
418
419/* Given an array of command-line switches, return a Scheme expression
420 to carry out the actions specified by the switches.
421
422 If you told me this should have been written in Scheme, I'd
423 probably agree. I'd say I didn't feel comfortable doing that in
424 the present system. You'd say, well, fix the system so you are
425 comfortable doing that. I'd agree again. *shrug*
ac16426b 426 */
224c49f9 427
1abb11b6
MD
428static char guile[] = "guile";
429
39e707a1
MV
430static int
431all_symbols (SCM list)
432{
433 while (scm_is_pair (list))
434 {
435 if (!scm_is_symbol (SCM_CAR (list)))
436 return 0;
437 list = SCM_CDR (list);
438 }
439 return 1;
440}
441
224c49f9
JB
442SCM
443scm_compile_shell_switches (int argc, char **argv)
444{
445 SCM tail = SCM_EOL; /* We accumulate the list backwards,
446 and then reverse! it before we
447 return it. */
448 SCM do_script = SCM_EOL; /* The element of the list containing
449 the "load" command, in case we get
450 the "-ds" switch. */
451 SCM entry_point = SCM_EOL; /* for -e switch */
caa29067 452 SCM user_load_path = SCM_EOL; /* for -L switch */
224c49f9 453 int interactive = 1; /* Should we go interactive when done? */
97c524bd 454 int inhibit_user_init = 0; /* Don't load user init file */
224c49f9 455 int use_emacs_interface = 0;
3682a51a
MV
456 int turn_on_debugging = 0;
457 int dont_turn_on_debugging = 0;
ee001750
AW
458 int turn_on_autocompile = 0;
459 int dont_turn_on_autocompile = 0;
3682a51a 460
224c49f9 461 int i;
1abb11b6 462 char *argv0 = guile;
224c49f9
JB
463
464 if (argc > 0)
465 {
d0e32dd5 466 argv0 = argv[0];
224c49f9
JB
467 scm_usage_name = strrchr (argv[0], '/');
468 if (! scm_usage_name)
469 scm_usage_name = argv[0];
470 else
471 scm_usage_name++;
472 }
473 if (! scm_usage_name)
1abb11b6 474 scm_usage_name = guile;
224c49f9
JB
475
476 for (i = 1; i < argc; i++)
477 {
defdc4b4 478 if ((! strcmp (argv[i], "-s")) || (argv[i][0] != '-')) /* load script */
224c49f9 479 {
defdc4b4 480 if ((argv[i][0] == '-') && (++i >= argc))
224c49f9
JB
481 scm_shell_usage (1, "missing argument to `-s' switch");
482
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. */
d2e53ed6 486 if (!scm_is_null (do_script))
224c49f9 487 {
cc95e00a 488 SCM_SETCAR (do_script, scm_from_locale_string (argv[i]));
224c49f9
JB
489 do_script = SCM_EOL;
490 }
491 else
492 /* Construct an application of LOAD to the script name. */
493 tail = scm_cons (scm_cons2 (sym_load,
cc95e00a 494 scm_from_locale_string (argv[i]),
224c49f9
JB
495 SCM_EOL),
496 tail);
497 argv0 = argv[i];
498 i++;
499 interactive = 0;
500 break;
501 }
502
503 else if (! strcmp (argv[i], "-c")) /* evaluate expr */
504 {
505 if (++i >= argc)
506 scm_shell_usage (1, "missing argument to `-c' switch");
507 tail = scm_cons (scm_cons2 (sym_eval_string,
cc95e00a 508 scm_from_locale_string (argv[i]),
224c49f9
JB
509 SCM_EOL),
510 tail);
511 i++;
512 interactive = 0;
513 break;
514 }
515
516 else if (! strcmp (argv[i], "--")) /* end args; go interactive */
517 {
518 i++;
519 break;
520 }
521
522 else if (! strcmp (argv[i], "-l")) /* load a file */
523 {
524 if (++i < argc)
525 tail = scm_cons (scm_cons2 (sym_load,
cc95e00a 526 scm_from_locale_string (argv[i]),
224c49f9
JB
527 SCM_EOL),
528 tail);
529 else
530 scm_shell_usage (1, "missing argument to `-l' switch");
531 }
532
caa29067
MV
533 else if (! strcmp (argv[i], "-L")) /* add to %load-path */
534 {
535 if (++i < argc)
7311b3e8
MV
536 user_load_path =
537 scm_cons (scm_list_3 (sym_set_x,
538 sym_load_path,
539 scm_list_3 (sym_cons,
540 scm_from_locale_string (argv[i]),
541 sym_load_path)),
542 user_load_path);
caa29067
MV
543 else
544 scm_shell_usage (1, "missing argument to `-L' switch");
545 }
546
224c49f9
JB
547 else if (! strcmp (argv[i], "-e")) /* entry point */
548 {
549 if (++i < argc)
39e707a1
MV
550 {
551 SCM port
552 = scm_open_input_string (scm_from_locale_string (argv[i]));
553 SCM arg1 = scm_read (port);
554 SCM arg2 = scm_read (port);
555
556 /* Recognize syntax of certain versions of Guile 1.4 and
557 transform to (@ MODULE-NAME FUNC).
558 */
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);
566 else
567 entry_point = arg1;
568 }
224c49f9
JB
569 else
570 scm_shell_usage (1, "missing argument to `-e' switch");
571 }
572
573 else if (! strcmp (argv[i], "-ds")) /* do script here */
574 {
575 /* We put a dummy "load" expression, and let the -s put the
576 filename in. */
d2e53ed6 577 if (!scm_is_null (do_script))
224c49f9
JB
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),
581 tail);
582 }
583
3682a51a
MV
584 else if (! strcmp (argv[i], "--debug"))
585 {
586 turn_on_debugging = 1;
587 dont_turn_on_debugging = 0;
588 }
589
590 else if (! strcmp (argv[i], "--no-debug"))
d150e491 591 {
3682a51a
MV
592 dont_turn_on_debugging = 1;
593 turn_on_debugging = 0;
d150e491
MD
594 }
595
ee001750
AW
596 else if (! strcmp (argv[i], "--autocompile"))
597 {
598 turn_on_autocompile = 1;
599 dont_turn_on_autocompile = 0;
600 }
601
602 else if (! strcmp (argv[i], "--no-autocompile"))
603 {
604 dont_turn_on_autocompile = 1;
605 turn_on_autocompile = 0;
606 }
607
224c49f9
JB
608 else if (! strcmp (argv[i], "--emacs")) /* use emacs protocol */
609 use_emacs_interface = 1;
610
97c524bd
MD
611 else if (! strcmp (argv[i], "-q")) /* don't load user init */
612 inhibit_user_init = 1;
613
39cde5c5
MG
614 else if (! strncmp (argv[i], "--use-srfi=", 11)) /* load SRFIs */
615 {
616 SCM srfis = SCM_EOL; /* List of requested SRFIs. */
617 char * p = argv[i] + 11;
618 while (*p)
619 {
620 long num;
621 char * end;
622
623 num = strtol (p, &end, 10);
624 if (end - p > 0)
625 {
b9bd8526 626 srfis = scm_cons (scm_from_long (num), srfis);
39cde5c5
MG
627 if (*end)
628 {
629 if (*end == ',')
630 p = end + 1;
631 else
632 scm_shell_usage (1, "invalid SRFI specification");
633 }
634 else
635 break;
636 }
637 else
638 scm_shell_usage (1, "invalid SRFI specification");
639 }
640 if (scm_ilength (srfis) <= 0)
641 scm_shell_usage (1, "invalid SRFI specification");
642 srfis = scm_reverse_x (srfis, SCM_UNDEFINED);
1afff620
KN
643 tail = scm_cons (scm_list_2 (sym_use_srfis,
644 scm_list_2 (scm_sym_quote, srfis)),
39cde5c5
MG
645 tail);
646 }
647
224c49f9
JB
648 else if (! strcmp (argv[i], "-h")
649 || ! strcmp (argv[i], "--help"))
650 {
651 scm_shell_usage (0, 0);
652 exit (0);
653 }
654
655 else if (! strcmp (argv[i], "-v")
656 || ! strcmp (argv[i], "--version"))
657 {
658 /* Print version number. */
659 printf ("Guile %s\n"
25a640ca 660 "Copyright (c) 1995, 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation\n"
224c49f9
JB
661 "Guile may be distributed under the terms of the GNU General Public Licence;\n"
662 "certain other uses are permitted as well. For details, see the file\n"
663 "`COPYING', which is included in the Guile distribution.\n"
664 "There is no warranty, to the extent permitted by law.\n",
0d189573 665 scm_to_locale_string (scm_version ()));
224c49f9
JB
666 exit (0);
667 }
668
669 else
670 {
671 fprintf (stderr, "%s: Unrecognized switch `%s'\n",
672 scm_usage_name, argv[i]);
673 scm_shell_usage (1, 0);
674 }
675 }
676
677 /* Check to make sure the -ds got a -s. */
d2e53ed6 678 if (!scm_is_null (do_script))
224c49f9
JB
679 scm_shell_usage (1, "the `-ds' switch requires the use of `-s' as well");
680
681 /* Make any remaining arguments available to the
682 script/command/whatever. */
28795b1f 683 scm_set_program_arguments (argc ? argc - i : 0, argv + i, argv0);
224c49f9
JB
684
685 /* If the --emacs switch was set, now is when we process it. */
7888309b 686 scm_c_define ("use-emacs-interface", scm_from_bool (use_emacs_interface));
224c49f9
JB
687
688 /* Handle the `-e' switch, if it was specified. */
d2e53ed6 689 if (!scm_is_null (entry_point))
224c49f9
JB
690 tail = scm_cons (scm_cons2 (entry_point,
691 scm_cons (sym_command_line, SCM_EOL),
692 SCM_EOL),
693 tail);
694
97c524bd 695 /* If we didn't end with a -c or a -s, start the repl. */
224c49f9
JB
696 if (interactive)
697 {
224c49f9
JB
698 tail = scm_cons (scm_cons (sym_top_repl, SCM_EOL), tail);
699 }
08fea088
GH
700 else
701 {
702 /* After doing all the other actions prescribed by the command line,
703 quit. */
704 tail = scm_cons (scm_cons (sym_quit, SCM_EOL),
6b8d19d3 705 tail);
08fea088 706 }
224c49f9 707
97c524bd
MD
708 /* After the following line, actions will be added to the front. */
709 tail = scm_reverse_x (tail, SCM_UNDEFINED);
caa29067
MV
710
711 /* add the user-specified load path here, so it won't be in effect
712 during the loading of the user's customization file. */
d2e53ed6 713 if(!scm_is_null(user_load_path))
caa29067
MV
714 {
715 tail = scm_append_x( scm_cons2(user_load_path, tail, SCM_EOL) );
716 }
97c524bd
MD
717
718 /* If we didn't end with a -c or a -s and didn't supply a -q, load
719 the user's customization file. */
720 if (interactive && !inhibit_user_init)
721 {
722 tail = scm_cons (scm_cons (sym_load_user_init, SCM_EOL), tail);
723 }
724
ee001750
AW
725 /* If GUILE_AUTO_COMPILE is not set and no args are given, default to
726 autocompilation. */
727 if (turn_on_autocompile || (scm_getenv_int ("GUILE_AUTO_COMPILE", 1)
728 && !dont_turn_on_autocompile))
729 {
730 tail = scm_cons (scm_list_3 (sym_set_x, sym_sys_load_should_autocompile,
731 SCM_BOOL_T),
732 tail);
733 }
734
3682a51a 735 /* If debugging was requested, or we are interactive and debugging
480fa28d
NJ
736 was not explicitly turned off, turn on debugging. */
737 if (turn_on_debugging || (interactive && !dont_turn_on_debugging))
3682a51a
MV
738 {
739 tail = scm_cons (scm_cons (sym_turn_on_debugging, SCM_EOL), tail);
740 }
741
224c49f9 742 {
97c524bd 743 SCM val = scm_cons (sym_begin, tail);
224c49f9 744
53e82297 745#if 0
224c49f9
JB
746 scm_write (val, SCM_UNDEFINED);
747 scm_newline (SCM_UNDEFINED);
ebe2a6c1 748#endif
224c49f9
JB
749
750 return val;
751 }
752}
753
754
755void
6e8d25a6 756scm_shell (int argc, char **argv)
224c49f9
JB
757{
758 /* If present, add SCSH-style meta-arguments from the top of the
759 script file to the argument vector. See the SCSH manual: "The
760 meta argument" for more details. */
761 {
762 char **new_argv = scm_get_meta_args (argc, argv);
763
764 if (new_argv)
765 {
766 argv = new_argv;
767 argc = scm_count_argv (new_argv);
768 }
769 }
770
b3138544 771 exit (scm_exit_status (scm_eval_x (scm_compile_shell_switches (argc, argv),
deec8fc2 772 scm_current_module ())));
224c49f9
JB
773}
774
775
776void
777scm_init_script ()
778{
a0599745 779#include "libguile/script.x"
224c49f9 780}
89e00824
ML
781
782/*
783 Local Variables:
784 c-file-style: "gnu"
785 End:
786*/