1 /* Copyright (C) 1994-1998, 2000-2011, 2013 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/read.h"
37 #include "libguile/script.h"
38 #include "libguile/strings.h"
39 #include "libguile/strports.h"
40 #include "libguile/validate.h"
41 #include "libguile/version.h"
42 #include "libguile/vm.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 '\\' */
329 /* FIXME: we leak the result of calling script_read_arg. */
330 while ((narg
= script_read_arg (f
)))
331 if (!(nargv
= (char **) realloc (nargv
,
332 (1 + ++nargc
) * sizeof (char *))))
335 nargv
[nargi
++] = narg
;
337 nargv
[nargi
++] = argv
[argi
++];
341 nargv
[nargi
++] = argv
[argi
++];
346 scm_count_argv (char **argv
)
355 /* For use in error messages. */
356 char *scm_usage_name
= 0;
359 scm_shell_usage (int fatal
, char *message
)
361 scm_call_3 (scm_c_private_ref ("ice-9 command-line",
364 ? scm_from_locale_string (scm_usage_name
)
365 : scm_from_latin1_string ("guile")),
366 scm_from_bool (fatal
),
368 ? scm_from_locale_string (message
)
372 /* Return a list of strings from ARGV, which contains ARGC strings
373 assumed to be encoded in the current locale. Use
374 `environ_locale_charset' instead of relying on
375 `scm_from_locale_string' because the user hasn't had a change to call
376 (setlocale LC_ALL "") yet.
378 XXX: This hack is for 2.0 and will be removed in the next stable
379 series where the `setlocale' call will be implicit. See
380 <http://lists.gnu.org/archive/html/guile-devel/2011-11/msg00040.html>
383 locale_arguments_to_string_list (int argc
, char **const argv
)
387 const char *encoding
;
389 encoding
= environ_locale_charset ();
390 for (i
= argc
- 1, lst
= SCM_EOL
;
393 lst
= scm_cons (scm_from_stringn (argv
[i
], (size_t) -1, encoding
,
394 SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE
),
400 /* Set the value returned by `program-arguments', given ARGC and ARGV. */
402 scm_i_set_boot_program_arguments (int argc
, char *argv
[])
404 scm_fluid_set_x (scm_program_arguments_fluid
,
405 locale_arguments_to_string_list (argc
, argv
));
408 /* Given an array of command-line switches, return a Scheme expression
409 to carry out the actions specified by the switches.
413 scm_compile_shell_switches (int argc
, char **argv
)
415 return scm_call_2 (scm_c_public_ref ("ice-9 command-line",
416 "compile-shell-switches"),
417 locale_arguments_to_string_list (argc
, argv
),
419 ? scm_from_locale_string (scm_usage_name
)
420 : scm_from_latin1_string ("guile")));
425 scm_shell (int argc
, char **argv
)
427 /* If present, add SCSH-style meta-arguments from the top of the
428 script file to the argument vector. See the SCSH manual: "The
429 meta argument" for more details. */
431 char **new_argv
= scm_get_meta_args (argc
, argv
);
436 argc
= scm_count_argv (new_argv
);
440 exit (scm_exit_status (scm_eval_x (scm_compile_shell_switches (argc
, argv
),
441 scm_current_module ())));
448 #include "libguile/script.x"