-
Notifications
You must be signed in to change notification settings - Fork 4
/
tv.c
510 lines (432 loc) · 9.12 KB
/
tv.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
/*
* $Header: f:/src/gulam\RCS\tv.c,v 1.1 1991/09/10 01:02:04 apratt Exp $ $Locker: $
* ======================================================================
* $Log: tv.c,v $
* Revision 1.1 1991/09/10 01:02:04 apratt
* First CI of AKP
*
* Revision: 1.5 90.02.13.14.33.14 apratt
* Added Mdmport[] and setmdmport() in variable table - see gioatari.c.
*
* Revision: 1.4 90.02.05.14.24.10 apratt
* Added variable Font[] = "font" with handler font().
*
* Revision: 1.3 89.06.16.17.24.06 apratt
* Header style change.
*
* Revision: 1.2 89.06.02.13.42.00 Author: apratt
* Compiler generated incorrect code for tbldelete when it
* had register variables. Don't ask my why; I just ripped them out.
*
* Revision: 1.1 88.06.03.15.38.46 Author: apratt
* Initial Revision
*
*/
/*sep 9, 86
tbl.c of gulam -- a simple table mechanism
alias.c of gulam -- alias module
env.c of gulam -- environment maintainer
var.c of gulam -- shell var maintainer
hlp.c of gulam -- help, what little there is
copyright (c) 1986 pm@Case
These modules are together here for no good reason;
they were just too short!
*/
#include "ue.h"
/* Notes: This table module can create and maintain simple tables of two
columns; the first column has to be a string, the second column is whatever.
The first node in the TBLE list is used as a header. The key-, and elm-fields
(except those of hdr) point to malloc'd areas; key pts to a string, elm can
be arbitrary.
*/
TBLE *tblfind(TBLE *t, uchar *k)
{
TBLE *p;
p = NULL;
if (k && (p = t) != NULL)
do
{
p = p->next;
} while (p && (strcmp(p->key, k)) != 0);
return p;
}
void tbldelete(TBLE *t, uchar *k)
{
TBLE *p;
TBLE *q;
if ((k == NULL) || ((p = t) == NULL))
return;
do
{
q = p;
p = p->next;
} while (p && (strcmp(p->key, k)) != 0);
if (p)
{
q->next = p->next;
gfree(p->key);
gfree(p->elm);
gfree(p);
}
}
void tblinsert(TBLE *t, uchar *k, uchar *e)
{
TBLE *p;
if ((t == NULL) || (k == NULL) || (e == NULL))
return;
tbldelete(t, k);
if ((p = (TBLE *) gmalloc((uint) sizeof(TBLE))) != NULL)
{
p->key = k;
p->elm = e;
p->next = t->next;
t->next = p;
}
}
/* Create a new table. */
TBLE *tblcreate(void)
{
TBLE *p;
if ((p = (TBLE *) gmalloc(((uint) sizeof(TBLE)))) != NULL)
{
p->key = NULL;
p->elm = NULL;
p->next = NULL;
}
return p;
}
void tblfree(TBLE *t)
{
TBLE *p;
TBLE *q;
for (p = t; p; p = q)
{
q = p->next;
gfree(p->key);
gfree(p->elm);
gfree(p);
}
}
/* Make a WS out of table's contents, in the requested style.
0 => columnar format (a->key's only) 1 => key \t elm \r\n
2 => key = elm \r\n 3 => key = elm \0
4 => key =\0 elm \0
*/
WS *tblws(TBLE *t, int style)
{
TBLE *a;
int i;
WS *ws;
uchar seps[2];
if (t == NULL)
return NULL;
ws = initws();
if (ws == NULL)
return NULL;
seps[0] = (style < 2 ? '\t' : '='); /* separator uchar */
seps[1] = '\0';
i = (style == 0 ? 1 : 0);
for (a = t->next; a; a = a->next)
{
strwcat(ws, a->key, i);
if (style)
{
strwcat(ws, seps, 0);
if (style == 4)
strwcat(ws, ES, 1);
strwcat(ws, a->elm, 0);
if (style < 3)
strwcat(ws, CRLF, 0);
else
strwcat(ws, ES, 1);
}
}
return ws;
}
uchar *tblstr(TBLE *t, int style)
{
WS *ws;
uchar *p;
ws = tblws(t, style);
if (ws == NULL)
return NULL;
p = ws->ps;
if (style == 0)
{
p = pscolumnize(p, -1, 0);
gfree(ws->ps);
}
gfree(ws);
return p;
}
static TBLE *alip = NULL;
/* show/set alias(es) */
void alias(uchar *arg)
{
uchar *p;
uchar *q;
UNUSED(arg);
p = lexgetword();
if (*p)
{
q = lextail();
if (*q)
{
if (alip == NULL)
alip = tblcreate();
tblinsert(alip, gstrdup(p), gstrdup(q));
} else
strg = gstrdup(getalias(p));
} else
strg = tblstr(alip, 1);
}
void unalias(uchar *arg)
{
UNUSED(arg);
tbldelete(alip, lexgetword());
}
/* return the alias'd string, if any, or itself */
uchar *getalias(uchar *p)
{
TBLE *a;
a = tblfind(alip, p);
return a ? a->elm : p;
}
/* eof alias.c */
/* env.c of shell -- environment maintainer */
static TBLE *envp = NULL;
/* get string value of env var whose */
/* name appears in *p... */
uchar *ggetenv(uchar *p)
{
TBLE *a;
a = tblfind(envp, p);
return a ? a->elm : NULL;
}
/* insert name p with string value q */
void gputenv(const char *p, const char *q)
{
if (envp == NULL)
envp = tblcreate();
tblinsert(envp, gstrdup(p), gstrdup(q));
}
/* duplicate the env string; if flag, put in \r\n */
WS *dupenvws(int flag)
{
flag = (flag ? 2 : 3);
if (flag == 3 && strcmp(varstr("env_style"), "bm") == 0)
flag = 4;
return tblws(envp, flag);
}
void printenv(uchar *arg)
{
WS *ws;
UNUSED(arg);
ws = dupenvws(1);
strg = ws->ps;
gfree(ws);
}
void gsetenv(uchar *arg)
{
uchar *p;
uchar *q;
UNUSED(arg);
p = lexgetword();
q = lexgetword();
if (*p)
gputenv(p, q);
else
printenv(NULL);
}
void gunsetenv(uchar *arg)
{
UNUSED(arg);
tbldelete(envp, lexgetword());
}
void readinenv(uchar *p)
{
uchar *q;
uchar *r;
if (p == NULL)
return;
for (; *p; p = r + strlen(r) + 1)
{
q = p;
p = strchr(p, '=');
if (p == NULL)
break;
*p = '\0';
r = p + 1;
if (*r == '\0')
r++;
gputenv(q, r);
*p = '=';
}
}
/* var.c == shell variables and their operations */
uchar Gulam[] = "Gulam";
uchar Shell[] = "shell";
uchar GulamHelp[] = "gulam_help_file";
uchar Nrows[] = "nrows";
uchar OwnFonts[] = "own_fonts";
uchar History[] = "history";
uchar Cwd[] = "cwd";
uchar Home[] = "home";
uchar Rgb[] = "rgb";
uchar Path[] = "path";
uchar Status[] = "status";
uchar Verbosity[] = "verbosity";
uchar Mscursor[] = "mscursor";
uchar Ncmd[] = "ncmd";
#if AtariST
uchar Font[] = "font"; /* extension by AKP */
uchar Mdmport[] = "mdmport"; /* extension by AKP */
#endif
struct SETVAR
{
void (*fun)(void); /* execute after updating the setvar */
const char *coupled; /* iff coupled to env var */
uchar *varname; /* name of the set variable */
};
static void rehashv(void)
{
rehash(NULL);
}
static struct SETVAR stv[] = {
{ NULL, "PWD", Cwd },
{ rehashv, "PATH", Path },
{ histinit, NULL, History },
{ NULL, "HOME", Home },
{ NULL, "SHELL", Shell },
{ rdhelpfile, NULL, GulamHelp },
#if AtariST
{ pallete, "RGB", Rgb },
{ nrow2550, "LINES", Nrows },
{ font, NULL, Font }, /* extension by AKP */
{ setmdmport, NULL, Mdmport }, /* extension by AKP */
{ mousecursor, NULL, Mscursor }
#endif
};
#define Nstv ((uint)(sizeof(stv)/sizeof(struct SETVAR)))
static TBLE *varp = NULL;
/* return the string value of a var */
uchar *varstr(uchar *p)
{
TBLE *a;
a = tblfind(varp, p);
return a ? a->elm : ES;
}
int varnum(uchar *p)
{
uchar *q;
q = varstr(p);
return atoi(q);
}
void showvars(void)
{
TBLE *p;
if (varp == NULL)
return;
for (p = varp->next; p; p = p->next)
{
gputs(sprintp("var %D %s=%s %D\r\n", p->key, p->key, p->elm, p->elm));
}
gputs("--\r\n");
}
void insertvar(uchar *p, uchar *q)
{
int i;
void (*f)(void);
if (varp == NULL)
varp = tblcreate();
q = gstrdup(q);
unquote(q, q);
tblinsert(varp, p = gstrdup(p), q);
for (i = 0; i < Nstv; i++)
{
if (strcmp(p, stv[i].varname) == 0)
{
if (stv[i].coupled)
{
gputenv(stv[i].coupled, q);
}
if ((f = stv[i].fun) != 0)
(*f) ();
break;
}
}
}
void setvarnum(uchar *p, int n)
{
insertvar(p, itoal((long) n));
}
void setvar(uchar *arg)
{
uchar *p;
uchar *dummy;
UNUSED(arg);
p = lexgetword();
if (*p)
insertvar(p, csexp(lexgetword(), &dummy));
else
strg = tblstr(varp, 1);
}
void unsetvar(uchar *arg)
{
UNUSED(arg);
tbldelete(varp, lexgetword());
}
/* -eof var.c- */
/* hlp.c */
static int nstate = -1;
static TBLE *atbl[3] = { NULL, NULL, NULL };
#define XKCDNM 0 /* key code to key name */
#define XFCDNM 1 /* fn code to fn name */
#define XGCMDD 2 /* Gulam cmd descriptive name */
#define XUNSHI 3 /* key codes for unshifted keys */
static void rdhelpln(uchar *q, int nb)
{
uchar *p;
lnn++;
ntotal += nb;
if (q[0] == '#')
{
nstate = q[1] - '1';
return;
}
if (XKCDNM > nstate || nstate > XGCMDD)
return;
p = strchr(q, ' ');
if (p == NULL || p == q)
return;
*p = '\0';
tblinsert(atbl[nstate], gstrdup(q), gstrdup(p + 1));
*p = ' ';
}
/* Read the Gulam help file. Called from setvar(). */
void rdhelpfile(void)
{
uchar *p;
int i;
nstate = -1;
p = varstr(GulamHelp);
if (*p == '\0')
return;
for (i = XKCDNM; i <= XGCMDD; i++)
{
tblfree(atbl[i]);
atbl[i] = tblcreate();
}
frdapply(p, rdhelpln);
gputs(CRLF);
}
/* Transform a key code into a name, using the above table.
*/
uchar *findname(int x, int k)
{
uchar *kp;
TBLE *t;
kp = itoar(0x1000L + (long) k, 16) + 1; /* skip the leading '1' */
t = tblfind(atbl[x], kp);
return t ? t->elm : ES;
}