-
Notifications
You must be signed in to change notification settings - Fork 0
/
syscall.c
250 lines (208 loc) · 5.47 KB
/
syscall.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
syscall - Launch system calls from your shell
Copyright (c) 2017, Olivier Duclos
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifndef VERSION
#define VERSION "unknown"
#endif
#define LEN(x) (sizeof(x) / sizeof(*x))
#define streq(a,b) (strcmp(a,b) == 0)
#define CMD_MAX 20 /* maximum number of commands per invocation */
static int ret_values[CMD_MAX];
typedef struct syscall {
const char *name;
long code;
} Syscall;
Syscall systab[] = {
# include "systab.h"
{"", 0}
};
void usage()
{
puts("usage: syscall [-<n>] name [args...] [, name [args...]]...");
}
int scomp(const void *m1, const void *m2)
{
Syscall *sys1 = (Syscall *)m1;
Syscall *sys2 = (Syscall *)m2;
return strcmp(sys1->name, sys2->name);
}
long lookup(const char *name)
{
Syscall key, *res;
key.name = name;
res = bsearch(&key, systab, LEN(systab), sizeof key, scomp);
if (res == NULL) {
errx(1, "unknown system call: %s", name);
}
return res->code;
}
/* Quick and dirty way to unescape \n at the end of strings */
void unescape_nl(char *str) {
size_t end = strlen(str) - 1;
while (str[end] == 'n' && str[end-1] == '\\') {
str[end-1] = '\n';
str[end] = '\0';
end -= 2;
}
}
unsigned long parse_arg(const char *syscall_name, char *arg)
{
unsigned long num;
char *endp = NULL;
/* #hello - length of a string */
if (arg[0] == '#') {
return (unsigned long)strlen(arg + 1);
}
/* $0 - return value of a previous syscall */
if (arg[0] == '$') {
num = strtoul(arg + 1, &endp, 10);
if (num < CMD_MAX - 1) {
return (unsigned long)ret_values[num];
}
}
/* Try to parse it as a number */
endp = NULL;
num = strtoul(arg, &endp, 0);
if (errno == ERANGE) {
errx(1, "%s: argument '%s' is out of range",
syscall_name, arg);
}
if (endp && *endp == '\0') {
/* strtoul succeeded */
return num;
}
/* assume it is a string */
/* unescape any \n at the end of the string */
unescape_nl(arg);
return (unsigned long)arg;
}
void echo(int argc, char **argv) {
int i;
for (i = 1; i < argc; i++) {
if (argv[i][0] == '$' || argv[i][0] == '#') {
printf("%ld\n", parse_arg(__func__, argv[i]));
} else {
printf("%s\n", argv[i]);
}
}
}
#define ARG(n) parse_arg(syscall_name, cmd[n])
void parse_syscall(int cmd_no, char **cmd, int cmd_len)
{
long syscall_num, ret = -1;
char *syscall_name = cmd[0];
/* Special case */
if (streq(syscall_name, "echo")) {
echo(cmd_len, cmd);
ret_values[cmd_no] = 0; /* echo never fails */
return;
}
syscall_num = lookup(syscall_name);
if (syscall_num == -1) {
errx(1, "unknown system call '%s'", syscall_name);
}
switch (cmd_len) {
case 0:
ret = syscall(syscall_num);
break;
case 1:
ret = syscall(syscall_num, ARG(1));
break;
case 2:
ret = syscall(syscall_num, ARG(1), ARG(2));
break;
case 3:
ret = syscall(syscall_num, ARG(1), ARG(2), ARG(3));
break;
case 4:
ret = syscall(syscall_num, ARG(1), ARG(2), ARG(3), ARG(4));
break;
case 5:
ret = syscall(syscall_num, ARG(1), ARG(2), ARG(3), ARG(4), ARG(5));
break;
default:
errx(1, "too many arguments for %s", syscall_name);
}
if (ret == -1) {
errx(1, "%s failed: %s", syscall_name, strerror(errno));
}
ret_values[cmd_no] = (int)ret;
}
void split_cmdline(int argc, char **argv)
{
int cmd_no = 0, cmd_len = 0, i;
for (i = 0; i < argc; i++) {
//printf("dbg: %s\n", argv[i]);
if (cmd_no == CMD_MAX) {
errx(1, "too many command (%d > %d)", cmd_no, CMD_MAX);
}
if (streq(argv[i], ",") || i == argc-1) {
//printf("parse: len=%d no=%d start=%s\n", cmd_len, cmd_no, (argv + (i - cmd_len))[0]);
parse_syscall(cmd_no, argv + (i - cmd_len), cmd_len);
cmd_no++;
cmd_len = 0;
} else {
cmd_len++;
}
}
}
/* for debugging */
void dump_ret_values(void) {
for (int i = 0; i < CMD_MAX; i++) {
printf("%0d %d\n", i, ret_values[i]);
}
}
int main(int argc, char **argv)
{
int repeat = 1, skip = 1;
if (argc <= 1) {
usage();
return 0;
}
/* arguments */
if (argv[1][0] == '-') {
/* help */
if (streq(argv[1], "-h") || streq(argv[1], "--help")) {
usage();
return 0;
}
/* version */
if (streq(argv[1], "-v") || streq(argv[1], "--version")) {
puts("syscall version "VERSION);
return 0;
}
/* Handle -n option */
repeat = atoi(argv[1] + 1);
if (repeat < 1) {
errx(1, "option -<n> must be between 1 and %d", INT_MAX);
}
if (argc <= 3) {
usage();
return 1;
}
skip++;
}
memset(ret_values, -1, sizeof ret_values);
while (repeat--) {
split_cmdline(argc - skip, argv + skip);
}
return 0;
}