123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- #ifdef HAVE_CONFIG_H
- # include <config.h>
- #endif
- #include <ctype.h>
- #include <errno.h>
- #include <localcharset.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <uniconv.h>
- #include <unistd.h>
- #ifdef HAVE_IO_H
- #include <io.h>
- #endif
- #include "eval.h"
- #include "feature.h"
- #include "fluids.h"
- #include "load.h"
- #include "modules.h"
- #include "pairs.h"
- #include "read.h"
- #include "strings.h"
- #include "strports.h"
- #include "throw.h"
- #include "version.h"
- #include "vm.h"
- #include "script.h"
- #ifndef WHITE_SPACES
- #ifdef MSDOS
- #define WHITE_SPACES ' ':case '\t':case '\r':case '\f':case 26
- #else
- #define WHITE_SPACES ' ':case '\t':case '\r':case '\f'
- #endif
- #endif
- static int
- script_get_octal (FILE *f)
- #define FUNC_NAME "script_get_octal"
- {
- int i;
- int value = 0;
- for (i = 0; i < 3; i++)
- {
- int c = getc (f);
- if ('0' <= c && c <= '7')
- value = (value * 8) + (c - '0');
- else
- SCM_MISC_ERROR ("malformed script: bad octal backslash escape",
- SCM_EOL);
- }
- return value;
- }
- #undef FUNC_NAME
- static int
- script_get_backslash (FILE *f)
- #define FUNC_NAME "script_get_backslash"
- {
- int c = getc (f);
- switch (c)
- {
- case 'a': return '\a';
- case 'b': return '\b';
- case 'f': return '\f';
- case 'n': return '\n';
- case 'r': return '\r';
- case 't': return '\t';
- case 'v': return '\v';
- case '\\':
- case ' ':
- case '\t':
- case '\n':
- return c;
- case '0': case '1': case '2': case '3':
- case '4': case '5': case '6': case '7':
- ungetc (c, f);
- return script_get_octal (f);
- case EOF:
- SCM_MISC_ERROR ("malformed script: backslash followed by EOF", SCM_EOL);
- return 0;
- default:
- SCM_MISC_ERROR ("malformed script: bad backslash sequence", SCM_EOL);
- return 0;
- }
- }
- #undef FUNC_NAME
- static void*
- realloc0 (void *ptr, size_t size)
- {
- void *new_ptr = realloc (ptr, size);
- if (!new_ptr)
- {
- free (ptr);
- }
- return new_ptr;
- }
- static char *
- script_read_arg (FILE *f)
- #define FUNC_NAME "script_read_arg"
- {
- size_t size = 7;
- char *buf = scm_malloc (size + 1);
- size_t len = 0;
- if (! buf)
- return 0;
- for (;;)
- {
- int c = getc (f);
- switch (c)
- {
- case '\\':
- c = script_get_backslash (f);
-
- default:
- if (len >= size)
- {
- size = (size + 1) * 2;
- buf = realloc0 (buf, size);
- if (! buf)
- return 0;
- }
- buf[len++] = c;
- break;
- case '\n':
-
- ungetc ('\n', f);
- case EOF:
- if (len == 0)
- {
- free (buf);
- return 0;
- }
-
- case ' ':
- buf[len] = '\0';
- return buf;
- case '\t':
- free (buf);
- SCM_MISC_ERROR ("malformed script: TAB in meta-arguments", SCM_EOL);
- return 0;
- }
- }
- }
- #undef FUNC_NAME
- static int
- script_meta_arg_P (char *arg)
- {
- if ('\\' != arg[0])
- return 0L;
- #ifdef MSDOS
- return !arg[1];
- #else
- switch (arg[1])
- {
- case 0:
- case '%':
- case WHITE_SPACES:
- return !0;
- default:
- return 0L;
- }
- #endif
- }
- char **
- scm_get_meta_args (int argc, char **argv)
- {
- int nargc = argc, argi = 1, nargi = 1;
- char *narg, **nargv;
- if (!(argc > 2 && script_meta_arg_P (argv[1])))
- return 0L;
- if (!(nargv = (char **) scm_malloc ((1 + nargc) * sizeof (char *))))
- return 0L;
- nargv[0] = argv[0];
- while (((argi + 1) < argc) && (script_meta_arg_P (argv[argi])))
- {
- FILE *f = fopen (argv[++argi], "r");
- if (f)
- {
- nargc--;
- while (1)
- switch (getc (f))
- {
- case EOF:
- free (nargv);
- return 0L;
- default:
- continue;
- case '\n':
- goto found_args;
- }
- found_args:
-
- while ((narg = script_read_arg (f)))
- if (!(nargv = (char **) realloc0 (nargv,
- (1 + ++nargc) * sizeof (char *))))
- return 0L;
- else
- nargv[nargi++] = narg;
- fclose (f);
- nargv[nargi++] = argv[argi++];
- }
- }
- while (argi <= argc)
- nargv[nargi++] = argv[argi++];
- return nargv;
- }
- int
- scm_count_argv (char **argv)
- {
- int argc = 0;
- while (argv[argc])
- argc++;
- return argc;
- }
- char *scm_usage_name = 0;
- void
- scm_shell_usage (int fatal, char *message)
- {
- scm_call_3 (scm_c_private_ref ("ice-9 command-line",
- "shell-usage"),
- (scm_usage_name
- ? scm_from_locale_string (scm_usage_name)
- : scm_from_latin1_string ("guile")),
- scm_from_bool (fatal),
- (message
- ? scm_from_locale_string (message)
- : SCM_BOOL_F));
- }
- static SCM
- locale_arguments_to_string_list (int argc, char **const argv)
- {
- int i;
- SCM lst;
- const char *encoding;
- encoding = environ_locale_charset ();
- for (i = argc - 1, lst = SCM_EOL;
- i >= 0;
- i--)
- lst = scm_cons (scm_from_stringn (argv[i], (size_t) -1, encoding,
- SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE),
- lst);
- return lst;
- }
- void
- scm_i_set_boot_program_arguments (int argc, char *argv[])
- {
- scm_fluid_set_x (scm_program_arguments_fluid,
- locale_arguments_to_string_list (argc, argv));
- }
- SCM
- scm_compile_shell_switches (int argc, char **argv)
- {
- return scm_call_2 (scm_c_public_ref ("ice-9 command-line",
- "compile-shell-switches"),
- locale_arguments_to_string_list (argc, argv),
- (scm_usage_name
- ? scm_from_locale_string (scm_usage_name)
- : scm_from_latin1_string ("guile")));
- }
- void
- scm_shell (int argc, char **argv)
- {
-
- {
- char **new_argv = scm_get_meta_args (argc, argv);
- if (new_argv)
- {
- argv = new_argv;
- argc = scm_count_argv (new_argv);
- }
- }
- exit (scm_exit_status (scm_eval_x (scm_compile_shell_switches (argc, argv),
- scm_current_module ())));
- }
- void
- scm_init_script ()
- {
- #include "script.x"
- }
|