7f611624263457923d52bafaf2e6e9bc2b3f4928
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 */
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 scm_call_3 (scm_c_private_ref ("ice-9 command-line",
363 ? scm_from_locale_string (scm_usage_name
)
364 : scm_from_latin1_string ("guile")),
365 scm_from_bool (fatal
),
367 ? scm_from_locale_string (message
)
372 /* Given an array of command-line switches, return a Scheme expression
373 to carry out the actions specified by the switches.
377 scm_compile_shell_switches (int argc
, char **argv
)
379 return scm_call_2 (scm_c_public_ref ("ice-9 command-line",
380 "compile-shell-switches"),
381 scm_makfromstrs (argc
, argv
),
383 ? scm_from_locale_string (scm_usage_name
)
384 : scm_from_latin1_string ("guile")));
389 scm_shell (int argc
, char **argv
)
391 /* If present, add SCSH-style meta-arguments from the top of the
392 script file to the argument vector. See the SCSH manual: "The
393 meta argument" for more details. */
395 char **new_argv
= scm_get_meta_args (argc
, argv
);
400 argc
= scm_count_argv (new_argv
);
404 exit (scm_exit_status (scm_eval_x (scm_compile_shell_switches (argc
, argv
),
405 scm_current_module ())));
412 #include "libguile/script.x"