-
Notifications
You must be signed in to change notification settings - Fork 0
/
lila_unix.c
115 lines (103 loc) · 1.98 KB
/
lila_unix.c
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "lila.h"
static void
usage(void)
{
fprintf(stderr, "usage\n");
exit(64);
}
void
die(const char *msg)
{
fprintf(stderr, "%s: error: %s\n", progname, msg);
exit(50);
}
static void
diesys(const char *msg)
{
fprintf(stderr, "%s: error: %s: %s\n", progname, msg, strerror(errno));
exit(50);
}
static void
slurp_fd(int fd)
{
ssize_t nr;
for (;;) {
nr = read(fd, bytes, sizeof(bytes));
if (nr != (ssize_t)-1) {
break;
}
if (errno != EINTR) {
diesys("cannot read from file");
}
}
nbyte = (size_t)nr;
}
static void
slurp_file(void)
{
int fd;
fd = open(script, O_RDONLY);
if (fd == -1) {
diesys("cannot open file");
}
slurp_fd(fd);
if (close(fd) == -1) {
diesys("cannot close file");
}
}
static void
run(void)
{
const char *argv[5];
const char **ins;
if (!implementation) {
die("no suitable implementation found");
}
ins = argv;
*ins++ = implementation->command;
if (implementation->scriptflag) {
*ins++ = implementation->scriptflag;
*ins++ = script;
}
if (implementation->flags & FLAG_DOUBLE_DASH) {
*ins++ = "--";
}
if (!implementation->scriptflag) {
*ins++ = script;
}
*ins++ = 0;
if (vflags >= 1) {
for (ins = argv; *ins; ins++) {
fprintf(stderr, "%s: arg: %s\n", progname, *ins);
}
}
execvp(argv[0], (char **)argv);
diesys("cannot exec interpreter");
exit(126);
}
int
main(int argc, char **argv)
{
if (argc < 2) {
usage();
}
script = argv[1];
if (vflags >= 1) {
fprintf(stderr, "%s: script: %s\n", progname, script);
}
slurp_file();
read_all();
if (vflags >= 1) {
print_all();
}
run();
return 0;
}