script.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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.
  6. *
  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.
  11. *
  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
  15. * 02110-1301 USA
  16. */
  17. /* "script.c" argv tricks for `#!' scripts.
  18. Authors: Aubrey Jaffer and Jim Blandy */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <errno.h>
  25. #include <ctype.h>
  26. #include "libguile/_scm.h"
  27. #include "libguile/eval.h"
  28. #include "libguile/feature.h"
  29. #include "libguile/load.h"
  30. #include "libguile/private-gc.h" /* scm_getenv_int */
  31. #include "libguile/read.h"
  32. #include "libguile/script.h"
  33. #include "libguile/strings.h"
  34. #include "libguile/strports.h"
  35. #include "libguile/validate.h"
  36. #include "libguile/version.h"
  37. #include "libguile/vm.h"
  38. #ifdef HAVE_STRING_H
  39. #include <string.h>
  40. #endif
  41. #ifdef HAVE_UNISTD_H
  42. #include <unistd.h> /* for X_OK define */
  43. #endif
  44. #ifdef HAVE_IO_H
  45. #include <io.h>
  46. #endif
  47. /* Concatentate str2 onto str1 at position n and return concatenated
  48. string if file exists; 0 otherwise. */
  49. static char *
  50. scm_cat_path (char *str1, const char *str2, long n)
  51. {
  52. if (!n)
  53. n = strlen (str2);
  54. if (str1)
  55. {
  56. size_t len = strlen (str1);
  57. str1 = (char *) realloc (str1, (size_t) (len + n + 1));
  58. if (!str1)
  59. return 0L;
  60. strncat (str1 + len, str2, n);
  61. return str1;
  62. }
  63. str1 = (char *) scm_malloc ((size_t) (n + 1));
  64. if (!str1)
  65. return 0L;
  66. str1[0] = 0;
  67. strncat (str1, str2, n);
  68. return str1;
  69. }
  70. #if 0
  71. static char *
  72. scm_try_path (char *path)
  73. {
  74. FILE *f;
  75. /* fprintf(stderr, "Trying %s\n", path);fflush(stderr); */
  76. if (!path)
  77. return 0L;
  78. SCM_SYSCALL (f = fopen (path, "r");
  79. );
  80. if (f)
  81. {
  82. fclose (f);
  83. return path;
  84. }
  85. free (path);
  86. return 0L;
  87. }
  88. static char *
  89. scm_sep_init_try (char *path, const char *sep, const char *initname)
  90. {
  91. if (path)
  92. path = scm_cat_path (path, sep, 0L);
  93. if (path)
  94. path = scm_cat_path (path, initname, 0L);
  95. return scm_try_path (path);
  96. }
  97. #endif
  98. #ifndef LINE_INCREMENTORS
  99. #define LINE_INCREMENTORS '\n'
  100. #ifdef MSDOS
  101. #define WHITE_SPACES ' ':case '\t':case '\r':case '\f':case 26
  102. #else
  103. #define WHITE_SPACES ' ':case '\t':case '\r':case '\f'
  104. #endif /* def MSDOS */
  105. #endif /* ndef LINE_INCREMENTORS */
  106. #ifndef MAXPATHLEN
  107. #define MAXPATHLEN 80
  108. #endif /* ndef MAXPATHLEN */
  109. #ifndef X_OK
  110. #define X_OK 1
  111. #endif /* ndef X_OK */
  112. char *
  113. scm_find_executable (const char *name)
  114. {
  115. char tbuf[MAXPATHLEN];
  116. int i = 0, c;
  117. FILE *f;
  118. /* fprintf(stderr, "s_f_e checking access %s ->%d\n", name, access(name, X_OK)); fflush(stderr); */
  119. if (access (name, X_OK))
  120. return 0L;
  121. f = fopen (name, "r");
  122. if (!f)
  123. return 0L;
  124. if ((fgetc (f) == '#') && (fgetc (f) == '!'))
  125. {
  126. while (1)
  127. switch (c = fgetc (f))
  128. {
  129. case /*WHITE_SPACES */ ' ':
  130. case '\t':
  131. case '\r':
  132. case '\f':
  133. case EOF:
  134. tbuf[i] = 0;
  135. fclose (f);
  136. return scm_cat_path (0L, tbuf, 0L);
  137. default:
  138. tbuf[i++] = c;
  139. break;
  140. }
  141. }
  142. fclose (f);
  143. return scm_cat_path (0L, name, 0L);
  144. }
  145. /* Read a \nnn-style escape. We've just read the backslash. */
  146. static int
  147. script_get_octal (FILE *f)
  148. #define FUNC_NAME "script_get_octal"
  149. {
  150. int i;
  151. int value = 0;
  152. for (i = 0; i < 3; i++)
  153. {
  154. int c = getc (f);
  155. if ('0' <= c && c <= '7')
  156. value = (value * 8) + (c - '0');
  157. else
  158. SCM_MISC_ERROR ("malformed script: bad octal backslash escape",
  159. SCM_EOL);
  160. }
  161. return value;
  162. }
  163. #undef FUNC_NAME
  164. static int
  165. script_get_backslash (FILE *f)
  166. #define FUNC_NAME "script_get_backslash"
  167. {
  168. int c = getc (f);
  169. switch (c)
  170. {
  171. case 'a': return '\a';
  172. case 'b': return '\b';
  173. case 'f': return '\f';
  174. case 'n': return '\n';
  175. case 'r': return '\r';
  176. case 't': return '\t';
  177. case 'v': return '\v';
  178. case '\\':
  179. case ' ':
  180. case '\t':
  181. case '\n':
  182. return c;
  183. case '0': case '1': case '2': case '3':
  184. case '4': case '5': case '6': case '7':
  185. ungetc (c, f);
  186. return script_get_octal (f);
  187. case EOF:
  188. SCM_MISC_ERROR ("malformed script: backslash followed by EOF", SCM_EOL);
  189. return 0; /* not reached? */
  190. default:
  191. SCM_MISC_ERROR ("malformed script: bad backslash sequence", SCM_EOL);
  192. return 0; /* not reached? */
  193. }
  194. }
  195. #undef FUNC_NAME
  196. static char *
  197. script_read_arg (FILE *f)
  198. #define FUNC_NAME "script_read_arg"
  199. {
  200. size_t size = 7;
  201. char *buf = scm_malloc (size + 1);
  202. size_t len = 0;
  203. if (! buf)
  204. return 0;
  205. for (;;)
  206. {
  207. int c = getc (f);
  208. switch (c)
  209. {
  210. case '\\':
  211. c = script_get_backslash (f);
  212. /* The above produces a new character to add to the argument.
  213. Fall through. */
  214. default:
  215. if (len >= size)
  216. {
  217. size = (size + 1) * 2;
  218. buf = realloc (buf, size);
  219. if (! buf)
  220. return 0;
  221. }
  222. buf[len++] = c;
  223. break;
  224. case '\n':
  225. /* This may terminate an arg now, but it will terminate the
  226. entire list next time through. */
  227. ungetc ('\n', f);
  228. case EOF:
  229. if (len == 0)
  230. {
  231. free (buf);
  232. return 0;
  233. }
  234. /* Otherwise, those characters terminate the argument; fall
  235. through. */
  236. case ' ':
  237. buf[len] = '\0';
  238. return buf;
  239. case '\t':
  240. free (buf);
  241. SCM_MISC_ERROR ("malformed script: TAB in meta-arguments", SCM_EOL);
  242. return 0; /* not reached? */
  243. }
  244. }
  245. }
  246. #undef FUNC_NAME
  247. static int
  248. script_meta_arg_P (char *arg)
  249. {
  250. if ('\\' != arg[0])
  251. return 0L;
  252. #ifdef MSDOS
  253. return !arg[1];
  254. #else
  255. switch (arg[1])
  256. {
  257. case 0:
  258. case '%':
  259. case WHITE_SPACES:
  260. return !0;
  261. default:
  262. return 0L;
  263. }
  264. #endif
  265. }
  266. char **
  267. scm_get_meta_args (int argc, char **argv)
  268. {
  269. int nargc = argc, argi = 1, nargi = 1;
  270. char *narg, **nargv;
  271. if (!(argc > 2 && script_meta_arg_P (argv[1])))
  272. return 0L;
  273. if (!(nargv = (char **) scm_malloc ((1 + nargc) * sizeof (char *))))
  274. return 0L;
  275. nargv[0] = argv[0];
  276. while (((argi + 1) < argc) && (script_meta_arg_P (argv[argi])))
  277. {
  278. FILE *f = fopen (argv[++argi], "r");
  279. if (f)
  280. {
  281. nargc--; /* to compensate for replacement of '\\' */
  282. while (1)
  283. switch (getc (f))
  284. {
  285. case EOF:
  286. free (nargv);
  287. return 0L;
  288. default:
  289. continue;
  290. case '\n':
  291. goto found_args;
  292. }
  293. found_args:
  294. /* FIXME: we leak the result of calling script_read_arg. */
  295. while ((narg = script_read_arg (f)))
  296. if (!(nargv = (char **) realloc (nargv,
  297. (1 + ++nargc) * sizeof (char *))))
  298. return 0L;
  299. else
  300. nargv[nargi++] = narg;
  301. fclose (f);
  302. nargv[nargi++] = argv[argi++];
  303. }
  304. }
  305. while (argi <= argc)
  306. nargv[nargi++] = argv[argi++];
  307. return nargv;
  308. }
  309. int
  310. scm_count_argv (char **argv)
  311. {
  312. int argc = 0;
  313. while (argv[argc])
  314. argc++;
  315. return argc;
  316. }
  317. /* For use in error messages. */
  318. char *scm_usage_name = 0;
  319. void
  320. scm_shell_usage (int fatal, char *message)
  321. {
  322. scm_call_3 (scm_c_private_ref ("ice-9 command-line",
  323. "shell-usage"),
  324. (scm_usage_name
  325. ? scm_from_locale_string (scm_usage_name)
  326. : scm_from_latin1_string ("guile")),
  327. scm_from_bool (fatal),
  328. (message
  329. ? scm_from_locale_string (message)
  330. : SCM_BOOL_F));
  331. }
  332. /* Given an array of command-line switches, return a Scheme expression
  333. to carry out the actions specified by the switches.
  334. */
  335. SCM
  336. scm_compile_shell_switches (int argc, char **argv)
  337. {
  338. return scm_call_2 (scm_c_public_ref ("ice-9 command-line",
  339. "compile-shell-switches"),
  340. scm_makfromstrs (argc, argv),
  341. (scm_usage_name
  342. ? scm_from_locale_string (scm_usage_name)
  343. : scm_from_latin1_string ("guile")));
  344. }
  345. void
  346. scm_shell (int argc, char **argv)
  347. {
  348. /* If present, add SCSH-style meta-arguments from the top of the
  349. script file to the argument vector. See the SCSH manual: "The
  350. meta argument" for more details. */
  351. {
  352. char **new_argv = scm_get_meta_args (argc, argv);
  353. if (new_argv)
  354. {
  355. argv = new_argv;
  356. argc = scm_count_argv (new_argv);
  357. }
  358. }
  359. exit (scm_exit_status (scm_eval_x (scm_compile_shell_switches (argc, argv),
  360. scm_current_module ())));
  361. }
  362. void
  363. scm_init_script ()
  364. {
  365. #include "libguile/script.x"
  366. }
  367. /*
  368. Local Variables:
  369. c-file-style: "gnu"
  370. End:
  371. */