-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtins.c
558 lines (510 loc) · 11.8 KB
/
builtins.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/* builtins.c: the collection of rc's builtin commands */
/*
NOTE: rc's builtins do not call "rc_error" because they are
commands, and rc errors usually arise from syntax errors. e.g.,
you probably don't want interpretation of a shell script to stop
because of a bad umask.
*/
#include "rc.h"
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <setjmp.h>
#include <errno.h>
#include "addon.h"
#include "input.h"
#include "jbwrap.h"
#include "rlimit.h"
#include "sigmsgs.h"
static void b_break(char **), b_cd(char **), b_eval(char **), b_exit(char **),
b_newpgrp(char **), b_return(char **), b_shift(char **), b_umask(char **),
b_wait(char **), b_whatis(char **);
#if HAVE_SETRLIMIT
static void b_limit(char **);
#endif
#if RC_ECHO
static void b_echo(char **);
#endif
static struct {
builtin_t *p;
char *name;
} builtins[] = {
{ b_break, "break" },
{ b_builtin, "builtin" },
{ b_cd, "cd" },
#if RC_ECHO
{ b_echo, "echo" },
#endif
{ b_eval, "eval" },
{ b_exec, "exec" },
{ b_exit, "exit" },
#if HAVE_SETRLIMIT
{ b_limit, "limit" },
#endif
{ b_newpgrp, "newpgrp" },
{ b_return, "return" },
{ b_shift, "shift" },
{ b_umask, "umask" },
{ b_wait, "wait" },
{ b_whatis, "whatis" },
{ b_dot, "." },
#ifdef ADDONS
ADDONS
#endif
};
extern builtin_t *isbuiltin(char *s) {
int i;
for (i = 0; i < arraysize(builtins); i++)
if (streq(builtins[i].name, s))
return builtins[i].p;
return NULL;
}
/* funcall() is the wrapper used to invoke shell functions. pushes $*, and "return" returns here. */
extern void funcall(char **av) {
Jbwrap j;
Estack e1, e2;
Edata jreturn, star;
if (sigsetjmp(j.j, 1))
return;
starassign(*av, av+1, TRUE);
jreturn.jb = &j;
star.name = "*";
except(eReturn, jreturn, &e1);
except(eVarstack, star, &e2);
walk(treecpy(fnlookup(*av), nalloc), TRUE);
varrm("*", TRUE);
unexcept(); /* eVarstack */
unexcept(); /* eReturn */
}
static void arg_count(char *name) {
fprint(2, RC "too many arguments to %s\n", name);
set(FALSE);
}
static void badnum(char *num) {
fprint(2, RC "`%s' is a bad number\n", num);
set(FALSE);
}
/* a dummy command. (exec() performs "exec" simply by not forking) */
extern void b_exec(char **ignore) {
}
#if RC_ECHO
/* echo -n omits a newline. echo -- -n echos '-n' */
static void b_echo(char **av) {
char *format = "%A\n";
if (*++av != NULL) {
if (streq(*av, "-n"))
format = "%A", av++;
else if (streq(*av, "--"))
av++;
}
fprint(1, format, av);
set(TRUE);
}
#endif
/* cd. traverse $cdpath if the directory given is not an absolute pathname */
static void b_cd(char **av) {
List *s, nil;
char *path = NULL;
size_t t, pathlen = 0;
if (*++av == NULL) {
s = varlookup("home");
*av = (s == NULL) ? "/" : s->w;
} else if (av[1] != NULL) {
arg_count("cd");
return;
}
if (isabsolute(*av) || streq(*av, ".") || streq(*av, "..")) { /* absolute pathname? */
if (chdir(*av) < 0) {
set(FALSE);
uerror(*av);
} else
set(TRUE);
} else {
s = varlookup("cdpath");
if (s == NULL) {
s = &nil;
nil.w = "";
nil.n = NULL;
}
do {
if (s != &nil && *s->w != '\0') {
t = strlen(*av) + strlen(s->w) + 2;
if (t > pathlen)
path = nalloc(pathlen = t);
strcpy(path, s->w);
if (!streq(s->w, "/")) /* "//" is special to POSIX */
strcat(path, "/");
strcat(path, *av);
} else {
pathlen = 0;
path = *av;
}
if (chdir(path) >= 0) {
set(TRUE);
if (interactive && *s->w != '\0' && !streq(s->w, "."))
fprint(1, "%s\n", path);
return;
}
s = s->n;
} while (s != NULL);
fprint(2, "couldn't cd to %s\n", *av);
set(FALSE);
}
}
static void b_umask(char **av) {
int i;
if (*++av == NULL) {
set(TRUE);
i = umask(0);
umask(i);
fprint(1, "0%o\n", i);
} else if (av[1] == NULL) {
i = o2u(*av);
if ((unsigned int) i > 0777) {
fprint(2, "bad umask\n");
set(FALSE);
} else {
umask(i);
set(TRUE);
}
} else {
arg_count("umask");
return;
}
}
static void b_exit(char **av) {
if (*++av != NULL)
ssetstatus(av);
rc_exit(getstatus());
}
/* raise a "return" exception, i.e., return from a function. if an integer argument is present, set $status to it */
static void b_return(char **av) {
if (*++av != NULL)
ssetstatus(av);
rc_raise(eReturn);
}
/* raise a "break" exception for breaking out of for and while loops */
static void b_break(char **av) {
if (av[1] != NULL) {
arg_count("break");
return;
}
rc_raise(eBreak);
}
/* shift $* n places (default 1) */
static void b_shift(char **av) {
int shift = (av[1] == NULL ? 1 : a2u(av[1]));
List *s, *dollarzero;
if (av[1] != NULL && av[2] != NULL) {
arg_count("shift");
return;
}
if (shift < 0) {
badnum(av[1]);
return;
}
s = varlookup("*")->n;
dollarzero = varlookup("0");
while (s != NULL && shift != 0) {
s = s->n;
--shift;
}
if (s == NULL && shift != 0) {
fprint(2, "cannot shift\n");
set(FALSE);
} else {
varassign("*", append(dollarzero, s), FALSE);
set(TRUE);
}
}
/* dud function */
extern void b_builtin(char **ignore) {
}
/* wait for a given process, or all outstanding processes */
static void b_wait(char **av) {
int status;
pid_t pid;
if (av[1] == NULL) {
waitforall();
return;
}
if (av[2] != NULL) {
arg_count("wait");
return;
}
if ((pid = a2u(av[1])) < 0) {
badnum(av[1]);
return;
}
if (rc_wait4(pid, &status, FALSE) > 0)
setstatus(pid, status);
else
set(FALSE);
sigchk();
}
/*
whatis without arguments prints all variables and functions. Otherwise, check to see if a name
is defined as a variable, function or pathname.
*/
#define not(b) ((b)^TRUE)
#define show(b) (not(eff|vee|pee|bee|ess)|(b))
static bool issig(char *s) {
int i;
for (i = 0; i < NUMOFSIGNALS; i++)
if (streq(s, signals[i].name))
return TRUE;
return FALSE;
}
static void b_whatis(char **av) {
bool ess, eff, vee, pee, bee;
bool f, found;
int i, ac, c;
List *s;
Node *n;
char *e;
for (rc_optind = ac = 0; av[ac] != NULL; ac++)
; /* count the arguments for getopt */
ess = eff = vee = pee = bee = FALSE;
while ((c = rc_getopt(ac, av, "sfvpb")) != -1)
switch (c) {
default: set(FALSE); return;
case 's': ess = TRUE; break;
case 'f': eff = TRUE; break;
case 'v': vee = TRUE; break;
case 'p': pee = TRUE; break;
case 'b': bee = TRUE; break;
}
av += rc_optind;
if (*av == NULL) {
if (vee|eff)
whatare_all_vars(eff, vee);
if (ess)
whatare_all_signals();
if (bee)
for (i = 0; i < arraysize(builtins); i++)
fprint(1, "builtin %s\n", builtins[i].name);
if (pee)
fprint(2, "whatis -p: must specify argument\n");
if (show(FALSE)) /* no options? */
whatare_all_vars(TRUE, TRUE);
set(TRUE);
return;
}
found = TRUE;
for (i = 0; av[i] != NULL; i++) {
f = FALSE;
errno = ENOENT;
if (show(vee) && (s = varlookup(av[i])) != NULL) {
f = TRUE;
prettyprint_var(1, av[i], s);
}
if (((show(ess)&&issig(av[i])) || show(eff)) && (n = fnlookup(av[i])) != NULL) {
f = TRUE;
prettyprint_fn(1, av[i], n);
} else if (show(bee) && isbuiltin(av[i]) != NULL) {
f = TRUE;
fprint(1, "builtin %s\n", av[i]);
} else if (show(pee) && (e = which(av[i], FALSE)) != NULL) {
f = TRUE;
fprint(1, "%S\n", e);
}
if (!f) {
found = FALSE;
if (errno != ENOENT)
uerror(av[i]);
else
fprint(2, "%s not found\n", av[i]);
}
}
set(found);
}
/* push a string to be eval'ed onto the input stack. evaluate it */
static void b_eval(char **av) {
bool i = interactive;
if (av[1] == NULL)
return;
interactive = FALSE;
pushstring(av + 1, i); /* don't reset line numbers on noninteractive eval */
doit(TRUE);
interactive = i;
}
/*
push a file to be interpreted onto the input stack. with "-i" treat this as an interactive
input source.
*/
extern void b_dot(char **av) {
int fd;
bool old_i = interactive, i = FALSE;
Estack e;
Edata star;
av++;
if (*av == NULL)
return;
if (streq(*av, "-i")) {
av++;
i = TRUE;
}
if (dasheye) { /* rc -i file has to do the right thing. reset the dasheye state to FALSE, though. */
dasheye = FALSE;
i = TRUE;
}
if (*av == NULL)
return;
fd = rc_open(*av, rFrom);
if (fd < 0) {
uerror(*av);
set(FALSE);
return;
}
starassign(*av, av+1, TRUE);
interactive = i;
pushfd(fd);
star.name = "*";
except(eVarstack, star, &e);
doit(TRUE);
varrm("*", TRUE);
unexcept(); /* eVarstack */
interactive = old_i;
}
/* put rc into a new pgrp. Used on the NeXT where the Terminal program is broken (sigh) */
static void b_newpgrp(char **av) {
if (av[1] != NULL) {
arg_count("newpgrp");
return;
}
setpgid(rc_pid, rc_pid); /* XXX check return value */
tcsetpgrp(2, rc_pid); /* XXX check return value */
}
/* Berkeley limit support was cleaned up by Paul Haahr. */
#if HAVE_SETRLIMIT
static const struct Suffix
kbsuf = { NULL, 1024, "k" },
mbsuf = { &kbsuf, 1024*1024, "m" },
gbsuf = { &mbsuf, 1024*1024*1024, "g" },
stsuf = { NULL, 1, "s" },
mtsuf = { &stsuf, 60, "m" },
htsuf = { &mtsuf, 60*60, "h" };
#define SIZESUF &gbsuf
#define TIMESUF &htsuf
#define NOSUF ((struct Suffix *) NULL) /* for RLIMIT_NOFILE on SunOS 4.1 */
static const struct Limit limits[] = {
{ "cputime", RLIMIT_CPU, TIMESUF },
{ "filesize", RLIMIT_FSIZE, SIZESUF },
{ "datasize", RLIMIT_DATA, SIZESUF },
{ "stacksize", RLIMIT_STACK, SIZESUF },
{ "coredumpsize", RLIMIT_CORE, SIZESUF },
#ifdef RLIMIT_NOFILE /* SUSv2, but not universal */
{ "descriptors", RLIMIT_NOFILE, NOSUF },
#endif
#ifdef RLIMIT_AS /* SUSv2, but not universal */
{ "memoryuse", RLIMIT_AS, SIZESUF },
#endif
#if defined(RLIMIT_VMEM) && !defined(RLIMIT_AS) /* old name for AS */
{ "memoryuse", RLIMIT_VMEM, SIZESUF },
#endif
#ifdef RLIMIT_RSS
{ "memoryrss", RLIMIT_RSS, SIZESUF },
#endif
#ifdef RLIMIT_NPROC
{ "maxproc", RLIMIT_NPROC, NOSUF },
#endif
#ifdef RLIMIT_MEMLOCK
{ "memorylocked", RLIMIT_MEMLOCK, SIZESUF },
#endif
#ifdef RLIMIT_LOCKS
{ "filelocks", RLIMIT_LOCKS, NOSUF },
#endif
{ NULL, 0, NULL }
};
static void printlimit(const struct Limit *limit, bool hard) {
struct rlimit rlim;
rlim_t lim;
getrlimit(limit->flag, &rlim);
if (hard)
lim = rlim.rlim_max;
else
lim = rlim.rlim_cur;
if (lim == RLIM_INFINITY)
fprint(1, "%s \tunlimited\n", limit->name);
else {
const struct Suffix *suf;
for (suf = limit->suffix; suf != NULL; suf = suf->next)
if (lim % suf->amount == 0 && (lim != 0 || suf->amount > 1)) {
lim /= suf->amount;
break;
}
fprint(1, RLIM_FMT, limit->name, (RLIM_CONV)lim, (suf == NULL || lim == 0) ? "" : suf->name);
}
}
static bool parselimit(const struct Limit *resource, rlim_t *limit, char *s) {
char *t;
int len = strlen(s);
const struct Suffix *suf = resource->suffix;
*limit = 1;
if (streq(s, "unlimited")) {
*limit = RLIM_INFINITY;
return TRUE;
}
if (suf == TIMESUF && (t = strchr(s, ':')) != NULL) {
int min, sec;
*t++ = '\0';
min = a2u(s); sec = a2u(t);
if (min == -1 || sec == -1) return FALSE;
*limit = 60 * min + sec;
} else {
int n;
for (; suf != NULL; suf = suf->next)
if (streq(suf->name, s + len - strlen(suf->name))) {
s[len - strlen(suf->name)] = '\0';
*limit *= suf->amount;
break;
}
n = a2u(s);
if (n == -1) return FALSE;
*limit *= n;
}
return TRUE;
}
static void b_limit(char **av) {
const struct Limit *lp = limits;
bool hard = FALSE;
if (*++av != NULL && streq(*av, "-h")) {
av++;
hard = TRUE;
}
if (*av == NULL) {
for (; lp->name != NULL; lp++)
printlimit(lp, hard);
return;
}
for (;; lp++) {
if (lp->name == NULL) {
fprint(2, "no such limit\n");
set(FALSE);
return;
}
if (streq(*av, lp->name))
break;
}
if (*++av == NULL)
printlimit(lp, hard);
else {
struct rlimit rlim;
rlim_t pl;
getrlimit(lp->flag, &rlim);
if (!parselimit(lp, &pl, *av)) {
fprint(2, "bad limit\n");
set(FALSE);
return;
}
if (hard)
rlim.rlim_max = pl;
else
rlim.rlim_cur = pl;
if (setrlimit(lp->flag, &rlim) == -1) {
uerror("setrlimit");
set(FALSE);
} else
set(TRUE);
}
}
#endif