-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
377 lines (332 loc) · 7.83 KB
/
main.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
/*
* $Header: f:/src/gulam\RCS\main.c,v 1.1 1991/09/10 01:02:04 apratt Exp $ $Locker: $
* ======================================================================
* $Log: main.c,v $
* Revision 1.1 1991/09/10 01:02:04 apratt
* First CI of AKP
*
* Revision: 1.8 90.10.22.11.01.20 apratt
* Changed the date to 10/22/90 because 10/11 was a pretty wide release
* and this is different: in fop.c, moved userfeedback so it happens
* for mv as well as cp.
*
* Revision: 1.7 90.10.11.14.22.46 apratt
* Changed date after kbad/akp to "10/11/90"
*
* Revision: 1.6 89.12.04.16.13.16 apratt
* Updated version number (added date after akp/kbad line).
*
* Revision: 1.5 89.06.16.17.24.00 apratt
* Header style change.
*
* Revision: 1.4 89.02.14.14.57.54 Author: apratt
* Added masterdate, which is exported to gmcatari.c and used in tooold().
*
* Revision: 1.3 89.02.07.12.23.30 Author: apratt
* Changed usage of cmdfirst to cmdprobe, zero for "don't" and incremented
* when tested so it's only zero once. Maybe this will work...
*
* Revision: 1.2 89.02.01.16.28.18 Author: apratt
* Added cmdfirst variable: when zero, no change. When nonzero, inhibits
* call to useraborted() in processcmd(). Zeroed by processcmd().
* Set in read-and-execute procedure (I forget its name). This should
* mean that you can type a command name, hit return, and immediately
* start typing ahead for that command.
*
* Revision: 1.1 89.01.11.15.02.34 Author: apratt
* Initial Revision
*
*/
/*
main.c -- main file of a new shell, called gulam 10/11/86
copyright (c) 1987 [email protected]
*/
/* 890111 kbad Modified header slightly, made error messages inverse video
*/
#ifdef __GNUC__
#include "minimal.h" /* use customized one, not the one in include */
#endif
#include "ue.h"
/*
* cmdprobe: (AKP) when zero, causes useraborted not to be called.
* It's incremented when it's checked, so it only inhibits the call once.
*/
int cmdprobe;
uchar cnmcomplete = ESC;
uchar cnmshow = CTRLD;
static uchar ID[] = /* id of this program */
"\r\n\
beta-test version 1.03.04.05 890111 of\r\n\
\tyet another shell for %s\r\n\r\n\
%s Copyright %s 1987 [email protected]\r\n\
(non-profit redistribution is permitted)\r\n\r\n\
some changes by [email protected],\r\n\
\t{akp,kbad}@atari.uucp (10/22/90)\r\n\
more changes by tho (10/30/21)\r\n\
\r\n";
unsigned long masterdate; /* AKP: extern exported to gmcatari.c */
/** last alpha 0.09.09.15 090687 of\r\n\ **/
static void showid(void)
{
outstr(sprintp(ID, OS, GulamLogo, Copyright));
}
void outstrg(void)
{
outstr(strg);
gfree(strg);
strg = NULL;
}
static void outemsg(void)
{
onreversevideo();
outstr(emsg);
offreversevideo();
emsg = NULL;
}
/* Make a gmalloc'd copy of the prompt; when freeing, must be gfreed
*/
uchar *getprompt(void)
{
uchar *p;
uchar *q;
WS *ws;
p = varstr("prompt");
if (*p == '\0')
p = gstrdup(defaultprompt);
else
{
ws = expand(gstrdup(p), 0);
p = catall(ws, 0); /* p now == ws->ps */
gfree(ws);
}
q = str3cat(p, varstr("prompt_tail"), ES);
gfree(p);
return q;
}
void gulamhelp(uchar *arg)
{
UNUSED(arg);
showid();
showbuiltins();
outstr("\r\nprograms accessible through hash table:");
which(0);
if (strg == NULL || *strg == '\0')
outstr("none! (your hash table is empty)");
outstrg(); /* at least gfree(strg) */
keysetup(); /* may have been clobbered by the Pexec'd progs */
state = TRUE;
}
unsigned int userfnminput(uchar **p, int sz, void (*fn)(uchar *r), int pexp)
{
uchar *q;
uchar *r;
uchar c;
int n;
for (;;)
{
c = getuserinput(*p, sz);
if (c != cnmcomplete && c != cnmshow)
break;
n = fnmexpand(p, &q, pexp, 1); /* 1 ==> TENEX */
if (c == cnmshow && n > 1 && fn != NULL)
{
r = pscolumnize(q, -1, -1);
if (r)
{
(*fn) (r);
gfree(r);
}
mlmesg(NULL);
}
gfree(q);
}
return c;
}
static void showcompletes(uchar *r)
{
gputs(CRLF);
gputs(r);
}
/* Get one (cmd) line from user. Include esc-name
completion. */
uchar *getoneline(void)
{
char *p;
unsigned int u;
tominibuf();
if ((p = gmalloc(SZcmd)) != NULL)
{
*p = '\0';
u = userfnminput(&p, SZcmd, showcompletes, 0);
if (u == '\007')
*p = '\0';
}
return p;
}
/* Get one cmd, and do it. Called from (1) within this file, (2) from
misc.c via spawn(), and (3) via _shell_p */
void getcmdanddoit(void)
{
char *p;
p = getoneline();
gputs(CRLF);
cmdprobe = 0; /* AKP: inhibit first call to useraborted() */
processcmd(p, 1); /* processcmd() frees p */
}
void gtime(uchar *arg) /* called from docmd(); see do.c */
{
WS *ws;
UNUSED(arg);
starttick = getticks();
ws = useuplexws();
shiftws(ws, 1);
gfree(execcmd(ws));
}
uchar *execcmd(WS *ws)
{
char *qq;
qq = NULL;
if (stackoverflown(256))
goto ret;
ws = aliasexp(ws);
if (ws && ws->ps)
{
lexaccept(ws);
if (ws->ps[0])
{
qq = gstrdup(lexsemicol());
docmd();
}
} else
ret:freews(ws);
/* do not freews(ws) ow because lex() will later on */
return qq;
}
/* Process the given line as a shell cmd: store it in history (if
savehist != 0), expand it, and finally run it. The globals strg,
emsg, valu are initialized in docmd(), and contain the results at the
end. The semicolon counting is there to break possible recursion in
aliassed cmds containing semicolons. */
void processcmd(char *qq, int savehist)
{
char *p;
WS *ws;
int nsemi;
int maxsemi;
if (qq == NULL || *qq == '\0')
{
gfree(qq);
return;
}
emsg = NULL;
if (strchr(qq, '!') != NULL)
{
p = substhist(qq); /* p != qq => qq got freed there */
if (p != qq)
userfeedback(p, 0);
outemsg();
} else
p = qq;
if (savehist)
remember(p, 1);
ws = expand(p, 0); /* expand frees p */
if (emsg)
{
outemsg();
freews(ws);
return;
}
maxsemi = varnum("semicolon_max");
if (maxsemi <= 0)
maxsemi = 20;
for (nsemi = 0;;)
{
if (varnum("time"))
starttick = getticks();
if ((++nsemi > maxsemi) || (cmdprobe++ && useraborted()))
{
freews(ws);
valu = -nsemi;
qq = NULL;
} else
{
qq = execcmd(ws);
outstrg();
undoredirections();
outemsg();
}
if (valu)
outstr(sprintp("cmd exit code %d", (int) valu));
valu = 0L;
computetime();
if (qq == NULL)
break; /* <== */
lex(qq, DELIMS, TKN2);
gfree(qq);
ws = useuplexws();
}
}
/* main() of Gulam. If argc > 1, the shell is being invoked for
non-interactive work. */
int main(int argc, char **argv, char **envp)
{
uchar *p;
WS *ws;
int m;
int nco;
ueinit();
if (exitue == 3)
exit(-1);
setgulam();
readinenv(*envp); /* read in the supplied environment */
if ((p = gfgetcwd()) != NULL)
{
insertvar(Home, p);
insertvar(Cwd, p);
gfree(p);
}
insertvar(Ncmd, itoal(1L));
insertvar(Verbosity, "1");
stamptime(&masterdate); /* AKP */
if (argc == 1)
goto interactive;
processcmd(gstrdup("gulam.g"), 0);
processcmd(gstrdup("rehash"), 0);
for (nco = 1; argc-- > 1;)
{
p = *++argv;
if (nco && strcmp(p, "-c"))
processcmd(gstrdup(p), 0);
else if (nco)
{
nco = 0;
ws = initws();
} else
strwcat(ws, p, 1);
}
if ((nco == 0) && ws)
{
p = catall(ws, 0);
processcmd(p, 0); /* ws->ps is freed here */
gfree(ws);
}
return (int) valu;
interactive:
tioinit();
gputenv("LINES", itoal(getnrow()));
gputenv("COLUMNS", itoal(getncol()));
exitue = -1;
update();
mlerase();
showid();
processcmd(gstrdup("date"), 0);
processcmd(gstrdup("gulam.g"), 0);
readhistory();
m = varnum("show_mem");
for (;;)
{
getcmdanddoit(); /* see bexit() in do.c */
if (m)
processcmd(gstrdup("mem"), 0);
}
}