1 /* Copyright (C) 1994-1998, 2000-2011 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 */
25 #include <localcharset.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 '\\' */
330 /* FIXME: we leak the result of calling script_read_arg. */
331 while ((narg
= script_read_arg (f
)))
332 if (!(nargv
= (char **) realloc (nargv
,
333 (1 + ++nargc
) * sizeof (char *))))
336 nargv
[nargi
++] = narg
;
338 nargv
[nargi
++] = argv
[argi
++];
342 nargv
[nargi
++] = argv
[argi
++];
347 scm_count_argv (char **argv
)
356 /* For use in error messages. */
357 char *scm_usage_name
= 0;
360 scm_shell_usage (int fatal
, char *message
)
362 scm_call_3 (scm_c_private_ref ("ice-9 command-line",
365 ? scm_from_locale_string (scm_usage_name
)
366 : scm_from_latin1_string ("guile")),
367 scm_from_bool (fatal
),
369 ? scm_from_locale_string (message
)
373 /* Return a list of strings from ARGV, which contains ARGC strings
374 assumed to be encoded in the current locale. Use
375 `environ_locale_charset' instead of relying on
376 `scm_from_locale_string' because the user hasn't had a change to call
377 (setlocale LC_ALL "") yet.
379 XXX: This hack is for 2.0 and will be removed in the next stable
380 series where the `setlocale' call will be implicit. See
381 <http://lists.gnu.org/archive/html/guile-devel/2011-11/msg00040.html>
384 locale_arguments_to_string_list (int argc
, char **const argv
)
388 const char *encoding
;
390 encoding
= environ_locale_charset ();
391 for (i
= argc
- 1, lst
= SCM_EOL
;
394 lst
= scm_cons (scm_from_stringn (argv
[i
], (size_t) -1, encoding
,
395 SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE
),
401 /* Set the value returned by `program-arguments', given ARGC and ARGV. */
403 scm_i_set_boot_program_arguments (int argc
, char *argv
[])
405 scm_fluid_set_x (scm_program_arguments_fluid
,
406 locale_arguments_to_string_list (argc
, argv
));
409 /* Given an array of command-line switches, return a Scheme expression
410 to carry out the actions specified by the switches.
414 scm_compile_shell_switches (int argc
, char **argv
)
416 return scm_call_2 (scm_c_public_ref ("ice-9 command-line",
417 "compile-shell-switches"),
418 locale_arguments_to_string_list (argc
, argv
),
420 ? scm_from_locale_string (scm_usage_name
)
421 : scm_from_latin1_string ("guile")));
426 scm_shell (int argc
, char **argv
)
428 /* If present, add SCSH-style meta-arguments from the top of the
429 script file to the argument vector. See the SCSH manual: "The
430 meta argument" for more details. */
432 char **new_argv
= scm_get_meta_args (argc
, argv
);
437 argc
= scm_count_argv (new_argv
);
441 exit (scm_exit_status (scm_eval_x (scm_compile_shell_switches (argc
, argv
),
442 scm_current_module ())));
449 #include "libguile/script.x"