-
Notifications
You must be signed in to change notification settings - Fork 4
/
do.c
637 lines (563 loc) · 13.6 KB
/
do.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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*
* $Header: f:/src/gulam\RCS\do.c,v 1.1 1991/09/10 01:02:04 apratt Exp $ $Locker: $
* ======================================================================
* $Log: do.c,v $
* Revision 1.1 1991/09/10 01:02:04 apratt
* First CI of AKP
*
* Revision: 1.9 89.06.16.17.23.18 apratt
* Header style change.
*
* Revision: 1.8 89.04.14.23.11.50 Author: apratt
* Added use of strrchr to find .g in file name to see if it is
* a batch file... Previously, used index(), so ..\foo.g wouldn't be
* executed right.
*
* Added command-line length byte = 0x7f if env_style == mw.
*
* Revision: 1.7 89.03.10.17.13.16 Author: apratt
* Fixed a REALLY STUPID bug: args were processed according to b->type
* for the built-in command lexically after the command name,
* if the command wasn't a built-in. Now it's processed as a '1'
* meaning it might change dir stuff, but no other strange handling.
*
* Revision: 1.6 89.03.10.14.23.52 Author: apratt
* Fixed range checking for command tail. Maxes out at 7e as it should.
*
* Revision: 1.5 89.03.10.13.56.06 Author: apratt
* Stomped cmdline tail length to max 0x7e so it's not a lie.
*
* Revision: 1.4 89.02.07.13.31.34 Author: apratt
* Reinstated mem, but made it more innocuous
*
* Revision: 1.3 89.02.01.15.06.26 Author: apratt
* oops
*
* Revision: 1.2 89.02.01.14.45.34 Author: apratt
* Removed "mem" command -- doesn't work for TOS 1.4.
*
* Revision: 1.1 88.06.03.15.39.00 Author: apratt
* Initial Revision
*
*/
/*
docmd.c of gulam -- builtin commands 7/20/86
copyright (c) 1986 pm@Case
*/
#include "ue.h"
int state = TRUE; /* == T => do the cmd; == F => skip it */
long valu; /* result of calling the function */
uchar *strg; /* result string, if any */
uchar *emsg; /* error msg string, if any errors */
uchar negopts[256]; /* -ve options */
uchar posopts[256]; /* +ve options */
int fda[4] = { MINFH - 1, MINFH - 1, MINFH - 1, MINFH - 1 };
uchar *rednm[2] = { NULL, NULL }; /* names of redirections */
int outappend = 0;
static uchar Sgulamend[] = "gulamend.g";
/* Collect options from the users command. This is invoked only
if the cmd is a builtin. All non-option words are given back to the
lex module. */
void addoptions(uchar *p)
{
uchar *q;
int i;
q = (*p == '+' ? posopts : negopts);
while ((i = (int) *++p) != 0)
q[i] = (uchar) 1;
}
static void setdate(uchar *arg)
{
UNUSED(arg);
gsdatetime(lexgetword());
}
/* "run" the file given by the pathname g */
static void run(uchar *g, uchar *cmdln, uchar *envp)
{
uchar *p;
emsg = NULL;
p = strrchr(g, '.');
if (p != NULL)
{
if (isgulamfile(p))
{
valu = batch(g, cmdln, envp);
return;
}
}
keyreset(FALSE, 1);
valu = execfile(g, cmdln, envp);
keysetup();
setgulam(); /* set _shell_p ptrs again */
}
/* Prepare command tail, and environment for Pexec() */
static void mkcmdenv(int flag, char *pgm, char **cmdp, char **envp)
{
#if TOS
static uchar ARGVVAL[] = "ARGV=";
#endif
uchar *p;
WS *ws;
int mwcflag = 0;
ws = dupenvws(0);
#if TOS
p = varstr("env_style");
if (p == NULL || *p == '\0')
p = "gu";
if (flag == 0 && p[0] == 'm' && p[1] == 'w') /* Mark Williams style */
{
strwcat(ws, ARGVVAL, 0);
strwcat(ws, pgm, 1);
appendlextail(ws); /* all the args appended */
mwcflag = 1;
}
#endif
*envp = ws->ps;
gfree(ws);
ws = initws();
strwcat(ws, ES, 1); /* 1 byte for length */
strwcat(ws, lextail(), 0);
if (ws == NULL)
*cmdp = NULL;
else
{
p = *cmdp = ws->ps;
*p = (uchar) (ws->nc - 1); /* 'cmdln' of Pexec needs length */
#if TOS
/* A little range checking might lighten things up... (AKP) */
if ((ws->nc - 1) > 0x7e)
*p = 0x7e;
if (mwcflag)
*p = 0x7f; /* validate ARGV in env (AKP) */
#endif
}
gfree(ws);
}
/* Execute file p: We search the hash table first; then try as is;
then try appending .tos|.ttp|.ttp|.g. If flag != 0, just see if p is
batch-able as is. */
static void lexec(uchar *p, int flag)
{
uchar *q;
uchar *lst;
int i, n;
uchar *cmdln;
uchar *envp;
uchar *g;
mkcmdenv(flag, p, &cmdln, &envp);
if (flag)
{
valu = batch(p, cmdln, envp);
goto freecmdln;
}
if ((q = hashlookup(0, p)) != NULL)
{
run(q, cmdln, envp);
goto freecmdln;
}
q = fnmsinparent(p);
lst = strg;
strg = NULL;
n = matchednms(lst, q, 0); /* first try as-is in current dir */
if (n == 1)
{
run(p, cmdln, envp);
if (valu != EFILNF)
goto freelst;
}
valu = EFILNF;
g = NULL;
n = 0;
#if TOS
if ((q = str3cat(q, ".(g|tos|ttp|prg)", ES)) != NULL)
{
n = matchednms(lst, q, 1);
gfree(q);
}
if ((g = str3cat(p, ".xxx", ES)) != NULL)
{
if (n > 0)
{
for (q = g + strlen(p) + 1, i = 0; i < SZext - 1; i += 4)
{
strcpy(q, &ext[i]);
run(g, cmdln, envp);
if (valu != EFILNF)
break;
}
}
} else
valu = ENSMEM;
#endif
if (valu == EFILNF && (q = hashlookup(1, p)) != NULL)
{
run(q, cmdln, envp);
goto freelst;
}
if (valu == ENSMEM)
emsg = "not enough memory";
if (valu == EFILNF)
emsg = (char *) sprintp("%s: not found", p);
gfree(g);
freelst:gfree(lst);
freecmdln:gfree(cmdln);
gfree(envp);
insertvar(Status, itoal(valu));
}
void source(uchar *arg)
{
UNUSED(arg);
lexec(lexgetword(), 1);
}
void echo(uchar *arg)
{
UNUSED(arg);
strg = gstrdup(lextail());
}
static void bexit(uchar *arg)
{
char *p;
int n;
UNUSED(arg);
n = atoi(lexgetword());
if (inbatchfile())
{
csexit(n);
return;
}
if (quickexit(0, 1) != 1)
return;
invisiblecursor();
mouseon(NULL);
cd(varstr("home"));
p = fnmpred('e', Sgulamend);
if (*p == '1')
processcmd(gstrdup(Sgulamend), 0);
savehistory();
keyreset(FALSE, 1);
fontreset();
exit(n);
}
/* Ask, if the -i option is present, if "op p?". Return 1 if okayed,
0 if nayed, and ABORT (==2), if aborted. */
int asktodoop(uchar *op, uchar *p)
{
return negopts['i'] ? mlyesno(sprintp("%s %s?", op, p)) : TRUE;
}
/* do f() for each word in current cmd */
void doforeach(uchar *op, void (*f)(uchar *arg))
{
uchar *p;
int flag;
for (;;)
{
p = lexgetword();
if (*p == '\0')
break;
flag = asktodoop(op, p);
if (flag == ABORT)
break;
if (flag)
(*f) (p);
if (useraborted())
break;
}
mlmesg(ES);
}
void setuekey(uchar *arg)
{
uchar *p;
int n;
UNUSED(arg);
n = negopts['m'] ? MINIKB : negopts['g'] ? GUKB : REGKB;
p = lexgetword();
bindkey(n, p, lexgetword()); /* see ue's kb.c */
}
/* note redirected filename */
static int noteredfnm(int r, char *fnm)
{
char c;
c = *fnm;
if (c == '>')
{
r = 1;
outappend = 0;
if (fnm[1] == '>')
{
fnm++;
outappend = 1;
}
} else
{
if (c == '<')
r = 0;
else if (r <= 1)
fnm--;
else
return 3;
}
if (fnm[1])
{
gfree(rednm[r]);
unquote(fnm + 1, fnm + 1);
rednm[r] = gstrdup(fnm + 1);
r = 2;
}
return r;
}
/*
Unquote the arguments to cmd (ie., the ws->ps stringlets) in place.
Also, note redirections and options (if builtin cmd).
As we handle the redirs, p will lag behind q;
if there are no redirs, p will == q all the time.
builtin commands have the form
cmd options args
using /posopts arrays imply that options of all builtin commands
are +/- single letter only.
So we have, for example,
chmod +w file
but we cannot have
chmod -r reference +w files
we need to know which part of the command line a '+' or '-' appears so we can
interpret it as an option or part of an expression
input: /cmd/ /-f/ /arg1/ ...
output: /cmd/ /arg1/ ...
& nopts[], popts[] set accordingly
*/
static void processargs(int builtin, int unqflag)
{
WS *ws;
uchar *p;
uchar *q;
int i;
int k;
int n;
int redi;
int brl;
int finopt = -1; /* -1 -> cmd part, 0 -> options, >0 -> args */
ws = useuplexws();
redi = 3;
if (ws && (q = p = ws->ps) != NULL)
{
for (brl = 0, i = ws->ns; i-- > 0; q += n + 1)
{
n = (int)strlen(q);
k = *q;
if (k == '{')
brl++;
else if (k == '}')
brl--;
redi = (brl == 0 ? noteredfnm(redi, q) : 3);
if (redi < 3)
{
ws->ns--;
} else if (builtin && finopt == 0 && (k == '-' || k == '+'))
{
addoptions(q);
ws->ns--;
} else
{
finopt++;
if (unqflag)
p = unquote(p, q) + 1;
else
{
if (p < q)
cpymem(p, q, n);
p += n + 1;
}
}
}
*p++ = '\0';
ws->nc = (int) (p - ws->ps);
}
lexaccept(ws);
doredirections();
}
#if XMDM
static void utemul(uchar *arg)
{
UNUSED(arg);
temul(FALSE, 1);
}
#endif
/* b->type values */
#define OWNWAY 2 /* cmd handles args own way */
#define ARGREQ 4 /* 1=>0/1 arg reqd */
#define PREFIX 6 /* cmd is a prefix to others */
#define NOUNQU 8 /* don't do unquote for this */
#define DOEACH 10 /* do the cmd for each arg */
#define BATCHS 12 /* ok in batch files only */
/* +1 added if cmd may change dir contents */
/* built-in commands */
/* must list in alpha order */
static struct bi
{
const char *name; /* name of builtin cmd */
uchar len; /* strlen of the cmd name */
uchar type; /* cmd type; see below */
void (*rtnp)(uchar *);
} builtins[] = {
{ "alias", 5, OWNWAY, alias },
{ "cat", 3, DOEACH, cat },
{ "cd", 2, ARGREQ, cdcmd },
{ "chmod", 5, DOEACH + 1, gchmod },
{ "copy", 4, OWNWAY + 1, cp },
{ "cp", 2, OWNWAY + 1, cp },
{ "date", 4, ARGREQ, setdate },
{ "df", 2, DOEACH, df },
#if TOS
{ "dm", 2, OWNWAY, dm },
#endif
{ "dirc", 4, OWNWAY, fnmtbl },
{ "dirs", 4, OWNWAY, dirs },
{ "echo", 4, OWNWAY, echo },
{ "egrep", 5, OWNWAY, egrep },
{ "ef", 2, BATCHS, cselse }, /* invoke even if state==F */
{ "endfor", 6, BATCHS, csendfor },
{ "endif", 5, BATCHS, csendif }, /* invoke even if state==F */
{ "endwhile", 8, BATCHS, csendwhile },
{ "exit", 4, OWNWAY, bexit },
{ "fg", 2, OWNWAY, fg },
{ "fgrep", 5, OWNWAY, fgrep },
{ "foreach", 7, BATCHS, csforeach },
#if TOS
{ "format", 6, DOEACH + 1, format },
{ "gem", 3, PREFIX, Gem },
#endif
{ "grep", 4, OWNWAY, egrep },
{ "help", 4, OWNWAY, gulamhelp },
{ "history", 7, OWNWAY, history },
{ "if", 2, BATCHS, csif }, /* invoke even if state==F */
{ "kb", 2, OWNWAY, setuekey },
#if LPRINT
{ "lpr", 3, OWNWAY, lpr },
#endif
{ "ls", 2, OWNWAY, ls },
#if TOS
{ "mem", 3, OWNWAY, mem },
#endif
{ "mkdir", 5, DOEACH + 1, gmkdir },
{ "more", 4, OWNWAY, moreue },
#if TOS
{ "mson", 4, OWNWAY, mouseon },
{ "msoff", 5, OWNWAY, mouseoff },
#endif
{ "mv", 2, OWNWAY + 1, mv },
#if TOS
{ "peekw", 5, OWNWAY, lpeekw },
{ "pokew", 5, OWNWAY, lpokew },
#endif
{ "popd", 4, OWNWAY, popd },
#if LPRINT
{ "print", 5, OWNWAY, print },
#endif
{ "printenv", 8, OWNWAY, printenv },
{ "pushd", 5, ARGREQ, pushd },
{ "pwd", 3, OWNWAY, pwd },
{ "rehash", 6, OWNWAY, rehash },
{ "ren", 3, OWNWAY + 1, grename },
{ "rm", 2, DOEACH + 1, rm },
{ "rmdir", 5, DOEACH + 1, grmdir },
#if XMDM
{ "rx", 2, DOEACH + 1, rxmdm },
#endif
{ "set", 3, NOUNQU, setvar },
{ "setenv", 6, OWNWAY, gsetenv },
{ "source", 6, OWNWAY, source },
#if XMDM
{ "sx", 2, DOEACH, sxmdm },
{ "te", 2, OWNWAY, utemul },
{ "teexit", 6, OWNWAY, teexit },
#endif
{ "time", 4, PREFIX, gtime },
{ "touch", 5, DOEACH, touch },
{ "ue", 2, OWNWAY + 1, ue },
{ "unalias", 7, OWNWAY, unalias },
{ "unset", 5, OWNWAY, unsetvar },
{ "unsetenv", 8, OWNWAY, gunsetenv },
{ "which", 5, OWNWAY, cmdwhich },
{ "while", 5, BATCHS, cswhile },
{ NULL, 0, OWNWAY, NULL }
};
#define NBC ((int)(sizeof(builtins) / sizeof(struct bi)))
#define MXL 8 /* length of longest built-in cmd */
#define MXC 8 /* #columns in the showbuiltin list */
void showbuiltins(void)
{
struct bi *b;
uchar *s;
WS *ws;
outstr(sprintp("%d built-in commands:", NBC - 1));
ws = initws();
if (ws == NULL)
return;
for (b = builtins; b->name != NULL; b++)
strwcat(ws, b->name, 1);
s = pscolumnize(ws->ps, -1, 0);
outstr(s);
gfree(s);
freews(ws);
}
void docmd(void)
{
struct bi *b;
uchar *p;
int lw;
int i;
int btype;
valu = 0;
strg = emsg = NULL;
charset(negopts, ES, 1);
charset(posopts, ES, 1);
p = lexgetword();
lw = (int)strlen(p);
i = 1;
if (lw <= MXL)
for (b = builtins; b->name; b++)
{
if ((int) b->len != lw)
continue;
i = strcmp(p, b->name);
if (i <= 0)
break;
}
/*
* STOOPID! btype used to be set to b's type whether or not
* b was actually the command you typed in! Now btype for
* non-builtins is one, meaning the command may change dir stuff
* Also, btype is compared against values, not bits, so you'd
* better not try to set more than one bit (except the +1 bit).
*/
if (i != 0)
btype = 1;
else
btype = (int) b->type;
lw = btype & 0x0001;
btype &= 0x00FE; /* i==0 => built-in command */
if (i == 0 && btype == BATCHS)
{
if (!inbatchfile())
goto skipit;
} else if (state != TRUE)
goto skipit;
if (btype != PREFIX)
{
processargs(i == 0, btype != NOUNQU);
p = lexgetword(); /* see above rtn ... */
}
if (i == 0)
{
if (btype == DOEACH)
doforeach(p, b->rtnp);
else
(*(b->rtnp)) (p);
} else
{
lexec(p, 0);
lw = 1;
}
skipit:
if (lw || varnum("dir_cache") == 0)
freewtbl();
}