-
Notifications
You must be signed in to change notification settings - Fork 4
/
misc.c
548 lines (469 loc) · 9.68 KB
/
misc.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
/*
misc.c -- miscellaneous things re uE interface with gulam
(c) 1986 pm@Case 11/13/86
*/
#include "ue.h"
#ifdef TOS
#include "keynames.h"
#endif
BUFFER *setgulambp(int f) /* gulambp := shell buffer */
{
return gulambp = bfind(GulamLogo, f, BFTEMP, GUKB, BMCGUL);
}
void setminibp(void) /* minibp := minibuffer */
{
minibp = bfind(Mini, TRUE, BFTEMP, MINIKB, BMCREG);
}
int togulambuf(int f, int n)
{
UNUSED(f);
UNUSED(n);
switchwindow(wheadp);
if (setgulambp(TRUE))
{
switchbuffer(gulambp);
if (exitue != 0)
refresh(FALSE, 1);
exitue = 0;
}
return TRUE;
}
/* Switch to mesgln window, and to mini buffer. Then, add the prompt
string to the buffer. */
void tominibuf(void) /* called only from main.c of gulam */
{
uchar *p;
switchwindow(mlwp);
setminibp();
switchbuffer(minibp); /* minibuf always exists */
igotoeob();
p = getprompt();
if (p)
{
mlmesg(p);
gfree(p);
} /* add prompt */
}
int semireset(int f, int n)
{
UNUSED(f);
UNUSED(n);
bufkill(minibp); /* flush mini buf */
tominibuf();
freewtbl();
return TRUE;
}
int help(int f, int n)
{
KEY c;
if (curbp == minibp)
{
gulamhelp(NULL);
return TRUE;
}
mlwrite("help: press B for wall-chart; C for describe-key: ");
c = getkey();
if (c == 'b' || c == 'B')
return wallchart(FALSE, 1);
if (c == 'c' || c == 'C')
return desckey(f, n);
return FALSE;
}
/* Give user feed back by displaying s, which does not contain \r or \n.
Do this only when verbosity is at least the given level.
*/
void userfeedback(uchar *s, int n)
{
uchar c;
if (varnum(Verbosity) < n)
return;
if (exitue == 0)
mlwrite(s);
else
{
gputs(s);
c = s[strlen(s) - 1];
if (c != ':' && c != '?')
gputs(CRLF);
}
}
/* Output a string so the user can see it. If we are in ue, append
"text" to gulam buffer. Text may contain several lines. Even if
there is no \r\n at the end treat it as a whole line. We update() the
buffer because outstr() is invoked often in the middle of a long
computation; it will be quite a while before the update() of uebody()
is done. */
void outstr(uchar *text)
{
int (*fn)(void *, uchar *);
/* this used to be if (text && *text) but then cat ate blank lines */
if (text) /* so we don't make useless updates */
{
fn = (outisredir() || exitue != 0 ? doout : addline);
if (fn == addline)
setgulambp(TRUE);
streachline(text, fn, gulambp);
if (fn == addline)
{
lbpchange(gulambp, WFHARD);
update();
}
}
}
/* Open a temporary buffer containg file p. Called by cs.c of Gulam.
*/
BUFFER *opentempbuf(uchar *p)
{
flvisit(p);
curbp->b_flag |= (BFTEMP | BFRDO);
return curbp;
}
/* Close a buffer opened by the above rtn. We can delete it only if
the nesting level of batch files is zero; o.w. it may still be in use
by prev levels. It is thus possible that there are several *.g
buffers hanging around after a batch/source cmd; but that's ok, since
you can enter ue and exit causing all buffers to get killed. */
void closebuf(BUFFER *bp)
{
if (!inbatchfile())
bufkill(bp);
}
void addcurbuf(uchar *p)
{
uchar c;
while ((c = *p++) != 0)
{
if (c == '\r')
{
c = '\n';
if (*p == '\n')
p++;
}
if (c == '\n')
newline(TRUE, 1);
else
linsert(1, c);
}
}
/* Make a string out of ELINE lp. There is an extra byte in our ELINE
structs; see lalloc() in line.c. Fails if user inserted a \0; but
what the heck! */
uchar *makelnstr(ELINE *lp)
{
uchar *q;
q = lp->l_text;
q[llength(lp)] = '\0';
return q;
}
/* Make a string from the text of the line lp for use as a gulam cmd
line. Ignore the prompt prefix. */
static uchar *makecmdstr(ELINE *lp)
{
int n;
int i;
uchar *p;
uchar *q;
q = gmalloc(1 + (n = lp->l_used));
if (q == NULL)
return q;
cpymem(q, lp->l_text, n);
q[n] = '\0';
p = getprompt();
if (p)
{
i = (int)strlen(p);
if (strncmp(q, p, ((size_t) i)) == 0)
cpymem(q, q + i, n - i + 1);
gfree(p);
}
return q;
}
/* char terminating getuserinput() */
static int cgetuserinput; /* set by miniterm() */
/* Get a line of input from kbd, and stuff it into buf[sx..nbuf]. Index
sx == strlen(given buf). The reply is terminated by one of esc, help,
undo, \r, \n. The buffer will contain chars upto but not incl this char
followed by \0 at the end. Handle erase, kill, and abort keys. Returns the
char that terminated the input.
*/
uchar getuserinput(uchar *buf, int nbuf)
{
WINDOW *wp;
BUFFER *bp;
int savedxue;
int n;
ELINE *lp;
setminibp();
wp = curwp;
switchwindow(mlwp);
bp = curbp;
if (curbp != minibp)
switchbuffer(minibp);
igotoeob();
addcurbuf(buf); /* add the given start string, if any */
savedxue = exitue;
exitue = -1;
uebody();
exitue = savedxue;
lp = curwp->w_dotp;
n = llength(lp);
if (n > nbuf - 1)
n = nbuf - 1;
cpymem(buf, lp->l_text, n);
buf[n] = '\0';
igotoeob();
if (bp != curbp)
switchbuffer(bp);
switchwindow(wp);
return cgetuserinput;
}
/* Insert the corresponding control-char and terminate the
getuserinput() call made using minibuffer. */
static int dominiterm(int f, int n, int c)
{
UNUSED(f);
UNUSED(n);
cgetuserinput = c & 077;
exitue = 1;
return TRUE;
}
int miniterm(int f, int n)
{
return dominiterm(f, n, lastkey);
}
int escesc(int f, int n)
{
return dominiterm(f, n, '\033');
}
int escctrld(int f, int n)
{
return dominiterm(f, n, '\004');
}
/* TENEX style xpand the current line. If flag != 0, show the
possible completions. */
static void gxp(int flag)
{
uchar *p;
uchar *q;
uchar *r;
ELINE *lp;
if (curbp != setgulambp(FALSE))
return;
lp = curwp->w_dotp;
p = makecmdstr(lp);
fnmexpand(&p, &q, 0, 1); /* 1 ==> TENEX */
if (flag)
{
r = pscolumnize(q, -1, -1);
if (r)
{
outstr(r);
gfree(r);
}
igotoeob();
lp = curwp->w_dotp;
}
gfree(q);
if (p)
{
curwp->w_doto = 0;
ldelete(llength(lp), 0);
q = getprompt();
if (q)
{
addcurbuf(q);
gfree(q);
}
addcurbuf(p);
gfree(p);
}
}
int gxpand(int f, int n)
{
UNUSED(f);
UNUSED(n);
gxp(0);
return TRUE;
}
int gxpshow(int f, int n)
{
UNUSED(f);
UNUSED(n);
gxp(1);
return TRUE;
}
/* Replace the current line with the previous command obtained from
history */
int gforwline(int f, int n)
{
if (curbp == setgulambp(FALSE) || curbp == minibp)
{
ELINE *lp1;
ELINE *lp2;
lp1 = curbp->b_linep;
lp2 = curwp->w_dotp;
if ((lp2 == lp1 && backchar(TRUE, 1)) || lp2 == lp1->l_bp)
{
uchar *p;
uchar *q;
uchar *r;
q = prevhist();
if (q)
{
curwp->w_doto = 0;
killtext(TRUE, 1);
if (curbp == gulambp)
{
p = str3cat(r = getprompt(), q, ES);
if (r)
gfree(r);
gfree(q);
} else
p = q;
addline(curbp, p);
gfree(p);
return backchar(TRUE, 1);
}
return TRUE;
}
}
return forwline(f, n);
}
/* Execute the lines of current buffer as Gulam cmds. If the buffer
is gulam-buffer, output of cmds also will also be considered as cmds.
*/
int execbuf(int f, int n)
{
BUFFER *bp;
int s;
char bufn[NBUFN];
UNUSED(f);
UNUSED(n);
bp = (curbp != setgulambp(TRUE) ? curbp : nextbuffer());
s = getbufname("Execute cmds in buffer [", bp->b_bname, bufn);
if (s == ABORT)
return ABORT;
if (s == TRUE && (bp = bfind(bufn, TRUE, 0, REGKB, BMCREG)) == NULL)
return FALSE;
if (bp != gulambp)
{
switchbuffer(bp);
mlwrite("(cmd output will be in \257gul\204m\256 buffer)");
csexecbuf(bp);
}
return TRUE;
}
/* Execute one Gulam cmd, and return to where we were. Output does no
get deposited in any buffer. */
int gspawn(int f, int n)
{
WINDOW *wp;
int savedxue;
UNUSED(f);
UNUSED(n);
savedxue = exitue;
exitue = -1;
wp = curwp;
getcmdanddoit(); /* see main.c of gulam */
exitue = savedxue;
sgarbf = TRUE;
getkey(); /* await a key press */
switchwindow(wp);
mlmesg(ES);
return TRUE;
}
#if MYTRAPS
/* Called via mytrap handlers. Similar to Bconout(), Cconout() */
void gconout(int c)
{
linsert(1, c);
update();
}
void gcconws(char *p)
{
addcurbuf(p);
update();
}
void gfwrite(long n, char *s)
{
char c;
while (n--)
{
c = *s++;
if (c == '\r')
continue;
if (c == '\n')
lnewline();
else
linsert(1, c);
}
update();
}
long oldp1;
long oldp13;
void mytrap1(void);
void mytrap13(void);
static planttraps113(void)
{
oldp1 = Setexc(33, mytrap1);
}
static unplanttraps113(void)
{
Setexc(33, oldp1);
}
#else /* ! MYTRAPS */
#define planttraps113()
#define unplanttraps113()
#endif
/* Execute a shell command given on the current line */
int gulam(int f, int n)
{
uchar *p;
uchar *q;
uchar *r;
ELINE *lp;
UNUSED(f);
UNUSED(n);
lp = curwp->w_dotp;
q = makecmdstr(lp);
if (lp != gulambp->b_linep->l_bp)
{ /*invoked cmd was not the last line */
p = str3cat(r = getprompt(), q, ES);
if (r)
gfree(r);
outstr(p);
gfree(p);
} else
{
gotoeol(FALSE, 1);
newline(FALSE, 1);
}
planttraps113();
processcmd(q, 1); /* q is gfreed by processcmd */
unplanttraps113();
igotoeob();
p = getprompt();
if (p)
{
addcurbuf(p);
gfree(p);
} /* add prompt */
wupdatemodeline(curbp);
return TRUE;
}
#if 0
int gnewline(int f, int n)
{
if (curbp == setgulambp(FALSE))
{
gulam();
return TRUE;
}
if (curbp == minibp)
{
exitue = 1;
return TRUE;
} else
return newline(f, n);
}
#endif