File: script-interpreter.c

package info (click to toggle)
scheme48 1.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 18,232 kB
  • sloc: lisp: 88,907; ansic: 87,519; sh: 3,224; makefile: 771
file content (69 lines) | stat: -rw-r--r-- 1,378 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * Part of Scheme 48 1.9.  See file COPYING for notices and license.
 *
 * Authors: Mike Sperber
 */

/* Implementation of SRFI-22-style script interpreters. */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include "sysdep.h"

int
main(int argc, char *argv[])
{
  char **new_argv;
  char *p, *language_tag;
  int i;

  if (argc < 2)
    {
      fprintf(stderr, "usage: %s script arg ...\n", argv[0]);
      return EX_USAGE;
    }

  p = strrchr(argv[0], '/');
  p = p ? (p+1) : argv[0];
  if (strncmp(p, "scheme-", 7))
    {
      fprintf(stderr,
	      "%s: script interpreter name must start with `scheme-'\n",
	      p);
      return EX_USAGE;
    }
  language_tag = p + 7;

  /* Debian specific: Live with scheme-r5rs.scheme48 */
  p = strchr(language_tag, '.');
  if (p) {
       *p = '\0';
  }

  new_argv = (char **)malloc((argc + 4) * sizeof(char *));
  if (!new_argv)
    {
      fprintf(stderr, "%s: out of memory\n", argv[0]);
      return EX_SOFTWARE;
    }

  new_argv[0] = RUNNABLE;
  new_argv[1] = "-a";
  new_argv[2] = "run-script";
  new_argv[3] = language_tag;

  for (i = 4; i < argc + 4; ++i)
    new_argv[i] = argv[i-3];

  new_argv[argc + 3] = NULL;

  execv(RUNNABLE, new_argv);

  fprintf(stderr,
	  "%s: exec'ing %s failed (%s)\n",
	  argv[0], RUNNABLE, strerror(errno));
  return EX_SOFTWARE;
}