script.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /* Copyright (C) 1994-1998, 2000-2011, 2013, 2014 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. /* "script.c" argv tricks for `#!' scripts.
  19. Authors: Aubrey Jaffer and Jim Blandy */
  20. #ifdef HAVE_CONFIG_H
  21. # include <config.h>
  22. #endif
  23. #include <localcharset.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <errno.h>
  27. #include <ctype.h>
  28. #include <uniconv.h>
  29. #include "libguile/_scm.h"
  30. #include "libguile/eval.h"
  31. #include "libguile/feature.h"
  32. #include "libguile/load.h"
  33. #include "libguile/read.h"
  34. #include "libguile/script.h"
  35. #include "libguile/strings.h"
  36. #include "libguile/strports.h"
  37. #include "libguile/validate.h"
  38. #include "libguile/version.h"
  39. #include "libguile/vm.h"
  40. #ifdef HAVE_STRING_H
  41. #include <string.h>
  42. #endif
  43. #include <unistd.h> /* for X_OK define */
  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. /*
  197. * Like `realloc', but free memory on failure;
  198. * unlike `scm_realloc', return NULL, not aborts.
  199. */
  200. static void*
  201. realloc0 (void *ptr, size_t size)
  202. {
  203. void *new_ptr = realloc (ptr, size);
  204. if (!new_ptr)
  205. {
  206. free (ptr);
  207. }
  208. return new_ptr;
  209. }
  210. static char *
  211. script_read_arg (FILE *f)
  212. #define FUNC_NAME "script_read_arg"
  213. {
  214. size_t size = 7;
  215. char *buf = scm_malloc (size + 1);
  216. size_t len = 0;
  217. if (! buf)
  218. return 0;
  219. for (;;)
  220. {
  221. int c = getc (f);
  222. switch (c)
  223. {
  224. case '\\':
  225. c = script_get_backslash (f);
  226. /* The above produces a new character to add to the argument.
  227. Fall through. */
  228. default:
  229. if (len >= size)
  230. {
  231. size = (size + 1) * 2;
  232. buf = realloc0 (buf, size);
  233. if (! buf)
  234. return 0;
  235. }
  236. buf[len++] = c;
  237. break;
  238. case '\n':
  239. /* This may terminate an arg now, but it will terminate the
  240. entire list next time through. */
  241. ungetc ('\n', f);
  242. case EOF:
  243. if (len == 0)
  244. {
  245. free (buf);
  246. return 0;
  247. }
  248. /* Otherwise, those characters terminate the argument; fall
  249. through. */
  250. case ' ':
  251. buf[len] = '\0';
  252. return buf;
  253. case '\t':
  254. free (buf);
  255. SCM_MISC_ERROR ("malformed script: TAB in meta-arguments", SCM_EOL);
  256. return 0; /* not reached? */
  257. }
  258. }
  259. }
  260. #undef FUNC_NAME
  261. static int
  262. script_meta_arg_P (char *arg)
  263. {
  264. if ('\\' != arg[0])
  265. return 0L;
  266. #ifdef MSDOS
  267. return !arg[1];
  268. #else
  269. switch (arg[1])
  270. {
  271. case 0:
  272. case '%':
  273. case WHITE_SPACES:
  274. return !0;
  275. default:
  276. return 0L;
  277. }
  278. #endif
  279. }
  280. char **
  281. scm_get_meta_args (int argc, char **argv)
  282. {
  283. int nargc = argc, argi = 1, nargi = 1;
  284. char *narg, **nargv;
  285. if (!(argc > 2 && script_meta_arg_P (argv[1])))
  286. return 0L;
  287. if (!(nargv = (char **) scm_malloc ((1 + nargc) * sizeof (char *))))
  288. return 0L;
  289. nargv[0] = argv[0];
  290. while (((argi + 1) < argc) && (script_meta_arg_P (argv[argi])))
  291. {
  292. FILE *f = fopen (argv[++argi], "r");
  293. if (f)
  294. {
  295. nargc--; /* to compensate for replacement of '\\' */
  296. while (1)
  297. switch (getc (f))
  298. {
  299. case EOF:
  300. free (nargv);
  301. return 0L;
  302. default:
  303. continue;
  304. case '\n':
  305. goto found_args;
  306. }
  307. found_args:
  308. /* FIXME: we leak the result of calling script_read_arg. */
  309. while ((narg = script_read_arg (f)))
  310. if (!(nargv = (char **) realloc0 (nargv,
  311. (1 + ++nargc) * sizeof (char *))))
  312. return 0L;
  313. else
  314. nargv[nargi++] = narg;
  315. fclose (f);
  316. nargv[nargi++] = argv[argi++];
  317. }
  318. }
  319. while (argi <= argc)
  320. nargv[nargi++] = argv[argi++];
  321. return nargv;
  322. }
  323. int
  324. scm_count_argv (char **argv)
  325. {
  326. int argc = 0;
  327. while (argv[argc])
  328. argc++;
  329. return argc;
  330. }
  331. /* For use in error messages. */
  332. char *scm_usage_name = 0;
  333. void
  334. scm_shell_usage (int fatal, char *message)
  335. {
  336. scm_call_3 (scm_c_private_ref ("ice-9 command-line",
  337. "shell-usage"),
  338. (scm_usage_name
  339. ? scm_from_locale_string (scm_usage_name)
  340. : scm_from_latin1_string ("guile")),
  341. scm_from_bool (fatal),
  342. (message
  343. ? scm_from_locale_string (message)
  344. : SCM_BOOL_F));
  345. }
  346. /* Return a list of strings from ARGV, which contains ARGC strings
  347. assumed to be encoded in the current locale. Use
  348. `environ_locale_charset' instead of relying on
  349. `scm_from_locale_string' because the user hasn't had a change to call
  350. (setlocale LC_ALL "") yet.
  351. XXX: This hack is for 2.0 and will be removed in the next stable
  352. series where the `setlocale' call will be implicit. See
  353. <http://lists.gnu.org/archive/html/guile-devel/2011-11/msg00040.html>
  354. for details. */
  355. static SCM
  356. locale_arguments_to_string_list (int argc, char **const argv)
  357. {
  358. int i;
  359. SCM lst;
  360. const char *encoding;
  361. encoding = environ_locale_charset ();
  362. for (i = argc - 1, lst = SCM_EOL;
  363. i >= 0;
  364. i--)
  365. lst = scm_cons (scm_from_stringn (argv[i], (size_t) -1, encoding,
  366. SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE),
  367. lst);
  368. return lst;
  369. }
  370. /* Set the value returned by `program-arguments', given ARGC and ARGV. */
  371. void
  372. scm_i_set_boot_program_arguments (int argc, char *argv[])
  373. {
  374. scm_fluid_set_x (scm_program_arguments_fluid,
  375. locale_arguments_to_string_list (argc, argv));
  376. }
  377. /* Given an array of command-line switches, return a Scheme expression
  378. to carry out the actions specified by the switches.
  379. */
  380. SCM
  381. scm_compile_shell_switches (int argc, char **argv)
  382. {
  383. return scm_call_2 (scm_c_public_ref ("ice-9 command-line",
  384. "compile-shell-switches"),
  385. locale_arguments_to_string_list (argc, argv),
  386. (scm_usage_name
  387. ? scm_from_locale_string (scm_usage_name)
  388. : scm_from_latin1_string ("guile")));
  389. }
  390. void
  391. scm_shell (int argc, char **argv)
  392. {
  393. /* If present, add SCSH-style meta-arguments from the top of the
  394. script file to the argument vector. See the SCSH manual: "The
  395. meta argument" for more details. */
  396. {
  397. char **new_argv = scm_get_meta_args (argc, argv);
  398. if (new_argv)
  399. {
  400. argv = new_argv;
  401. argc = scm_count_argv (new_argv);
  402. }
  403. }
  404. exit (scm_exit_status (scm_eval_x (scm_compile_shell_switches (argc, argv),
  405. scm_current_module ())));
  406. }
  407. void
  408. scm_init_script ()
  409. {
  410. #include "libguile/script.x"
  411. }
  412. /*
  413. Local Variables:
  414. c-file-style: "gnu"
  415. End:
  416. */