script-interpreter.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Part of Scheme 48 1.9. See file COPYING for notices and license.
  3. *
  4. * Authors: Mike Sperber
  5. */
  6. /* Implementation of SRFI-22-style script interpreters. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. #include <unistd.h>
  11. #include <string.h>
  12. #include "sysdep.h"
  13. int
  14. main(int argc, char *argv[])
  15. {
  16. char **new_argv;
  17. char *p, *language_tag;
  18. int i;
  19. if (argc < 2)
  20. {
  21. fprintf(stderr, "usage: %s script arg ...\n", argv[0]);
  22. return EX_USAGE;
  23. }
  24. p = strrchr(argv[0], '/');
  25. p = p ? (p+1) : argv[0];
  26. if (strncmp(p, "scheme-", 7))
  27. {
  28. fprintf(stderr,
  29. "%s: script interpreter name must start with `scheme-'\n",
  30. p);
  31. return EX_USAGE;
  32. }
  33. language_tag = p + 7;
  34. new_argv = (char **)malloc((argc + 4) * sizeof(char *));
  35. if (!new_argv)
  36. {
  37. fprintf(stderr, "%s: out of memory\n", argv[0]);
  38. return EX_SOFTWARE;
  39. }
  40. new_argv[0] = RUNNABLE;
  41. new_argv[1] = "-a";
  42. new_argv[2] = "run-script";
  43. new_argv[3] = language_tag;
  44. for (i = 4; i < argc + 4; ++i)
  45. new_argv[i] = argv[i-3];
  46. new_argv[argc + 3] = NULL;
  47. execv(RUNNABLE, new_argv);
  48. fprintf(stderr,
  49. "%s: exec'ing %s failed (%s)\n",
  50. argv[0], RUNNABLE, strerror(errno));
  51. return EX_SOFTWARE;
  52. }