-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.c
1764 lines (1473 loc) · 42.3 KB
/
eval.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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* EVAL.C: Expresion evaluation functions for
MicroEMACS
written 1993 by Daniel Lawrence */
#include <stdio.h>
#include "estruct.h"
#include "eproto.h"
#include "edef.h"
#include "elang.h"
#include "evar.h"
/* initialize the entries in one user variable table */
VOID PASCAL NEAR uv_init(ut)
UTABLE *ut; /* user variable table to initialize */
{
register int i;
for (i=0; i < ut->size; i++) {
ut->uv[i].u_name[0] = 0;
ut->uv[i].u_value = (char *)NULL;
}
}
VOID PASCAL NEAR varinit() /* initialize the global user variable table */
{
/* allocate the global user variable table */
uv_global = uv_head =
(UTABLE *)room(sizeof(UTABLE) + MAXVARS * sizeof(UVAR));
/* and set up its fields */
uv_head->next = (UTABLE *)NULL;
uv_head->size = MAXVARS;
uv_head->bufp = (BUFFER *)NULL;
uv_init(uv_head);
}
VOID PASCAL NEAR uv_clean(ut) /* discard the contents of a user variable table */
UTABLE *ut; /* ptr to table to clear */
{
register int i;
/* now clear the entries in this one */
for (i=0; i < ut->size; i++)
if (ut->uv[i].u_name[0] != 0)
free(ut->uv[i].u_value);
}
VOID PASCAL NEAR varclean(ut) /* discard and clear all user variable tables */
UTABLE *ut; /* table to clear */
{
/* first clean all the ones under this one */
if (ut->next != (UTABLE *)NULL)
varclean(ut->next);
/* clear the contents of this table */
uv_clean(ut);
/* and then deallocate the this table itself */
free(ut);
}
char *PASCAL NEAR gtfun(fname) /* evaluate a function */
char *fname; /* name of function to evaluate */
{
register int fnum; /* index to function to eval */
register int arg; /* value of some arguments */
BUFFER *bp; /* scratch buffer pointer */
char arg1[NSTRING]; /* value of first argument */
char arg2[NSTRING]; /* value of second argument */
char arg3[NSTRING]; /* value of third argument */
static char result[2 * NSTRING]; /* string result */
/* look the function up in the function table */
mklower(fname); /* and let it be upper or lower case */
fnum = binary(fname, funval, NFUNCS, MINFLEN);
/* return errorm on a bad reference */
if (fnum == -1) {
mlwrite(TEXT244, fname);
/* "%%No such function as '%s'" */
return(errorm);
}
/* if needed, retrieve the first argument */
if (funcs[fnum].f_type >= MONAMIC) {
if (macarg(arg1) != TRUE)
return(errorm);
/* if needed, retrieve the second argument */
if (funcs[fnum].f_type >= DYNAMIC) {
if (macarg(arg2) != TRUE)
return(errorm);
/* if needed, retrieve the third argument */
if (funcs[fnum].f_type >= TRINAMIC)
if (macarg(arg3) != TRUE)
return(errorm);
}
}
/* and now evaluate it! */
switch (fnum) {
case UFABBREV: return(fixnull(ab_lookup(arg1)));
case UFABS: return(int_asc(absv(asc_int(arg1))));
case UFADD: return(int_asc(asc_int(arg1) + asc_int(arg2)));
case UFAND: return(ltos(stol(arg1) && stol(arg2)));
case UFASCII: return(int_asc((int)arg1[0]));
case UFBAND: return(int_asc(asc_int(arg1) & asc_int(arg2)));
case UFBIND: return(transbind(arg1));
case UFBNOT: return(int_asc(~asc_int(arg1)));
case UFBOR: return(int_asc(asc_int(arg1) | asc_int(arg2)));
case UFBXOR: return(int_asc(asc_int(arg1) ^ asc_int(arg2)));
case UFCALL: /* construct buffer name to execute */
result[0] = '[';
strcpy(&result[1], arg1);
strcat(result, "]");
/* find it, return ERROR if it does not exist */
bp = bfind(result, FALSE, 0);
if (bp == NULL)
return(errorm);
/* execute it and return whats in the $rval */
dobuf(bp);
return(fixnull(rval));
case UFCAT: strcpy(result, arg1);
strncat(result, arg2, NSTRING);
result[NSTRING - 1] = 0;
return(result);
case UFCHR: result[0] = asc_int(arg1);
result[1] = 0;
return(result);
case UFDIV: if ((arg = asc_int(arg2)) != 0)
return(int_asc(asc_int(arg1) / arg));
else {
mlwrite(TEXT245);
/* "%%Division by Zero is illegal" */
return(errorm);
}
case UFENV:
#if ENVFUNC
return(fixnull(getenv(arg1)));
#else
return("");
#endif
case UFEQUAL: return(ltos(asc_int(arg1) == asc_int(arg2)));
case UFEXIST: return(ltos(fexist(arg1)));
case UFFIND:
return(fixnull(flook(arg1, TRUE)));
case UFGREATER: return(ltos(asc_int(arg1) > asc_int(arg2)));
case UFGROUP:
arg = asc_int(arg1);
#if MAGIC
if (arg < 0 || arg >= MAXGROUPS)
return(bytecopy(result, errorm, NSTRING * 2));
return(bytecopy(result, fixnull(grpmatch[arg]),
NSTRING * 2));
#else
if (arg == 0)
bytecopy(result, patmatch, NSTRING * 2);
else
return(bytecopy(result, errorm, NSTRING * 2));
return(result);
#endif
case UFGTCMD: return(cmdstr(getcmd(), result));
case UFGTKEY: result[0] = tgetc();
result[1] = 0;
return(result);
case UFIND: return(strcpy(result, fixnull(getval(arg1))));
case UFISNUM: return(ltos(is_num(arg1)));
case UFLEFT: return(bytecopy(result, arg1, asc_int(arg2)));
case UFLENGTH: return(int_asc(strlen(arg1)));
case UFLESS: return(ltos(asc_int(arg1) < asc_int(arg2)));
case UFLOWER: return(mklower(arg1));
case UFMID: arg = asc_int(arg2);
if (arg > strlen(arg1))
arg = strlen(arg1);
return(bytecopy(result, &arg1[arg-1],
asc_int(arg3)));
case UFMKCOL: if ((arg = asc_int(arg1)) < 0 || arg >= NMARKS ||
curwp->w_markp[arg] == NULL)
{
mlwrite(TEXT11, arg);
return (int_asc(-1));
}
return(int_asc(findcol(curwp->w_markp[arg], curwp->w_marko[arg])));
case UFMKLINE: if ((arg = asc_int(arg1)) < 0 || arg >= NMARKS ||
curwp->w_markp[arg] == NULL)
{
mlwrite(TEXT11, arg);
return (int_asc(0));
}
return(long_asc(getlinenum(curbp, curwp->w_markp[arg])));
case UFMOD: if ((arg = asc_int(arg2)) != 0)
return(int_asc(asc_int(arg1) % arg));
else {
mlwrite(TEXT245);
/* "%%Division by Zero is illegal" */
return(errorm);
}
case UFNEG: return(int_asc(-asc_int(arg1)));
case UFNOT: return(ltos(stol(arg1) == FALSE));
case UFOR: return(ltos(stol(arg1) || stol(arg2)));
case UFREVERSE: return(strrev(bytecopy(result, arg1, NSTRING * 2)));
case UFRIGHT: arg = asc_int(arg2);
if (arg > strlen(arg1))
arg = strlen(arg1);
return(strcpy(result,
&arg1[strlen(arg1) - arg]));
case UFRND: return(int_asc((int)(ernd() % (long)absv(asc_int(arg1))) + 1L));
case UFSEQUAL: return(ltos(strcmp(arg1, arg2) == 0));
case UFSGREAT: return(ltos(strcmp(arg1, arg2) > 0));
case UFSINDEX: return(int_asc(sindex(arg1, arg2)));
case UFSLESS: return(ltos(strcmp(arg1, arg2) < 0));
case UFSLOWER: return(setlower(arg1, arg2), "");
case UFSUB: return(int_asc(asc_int(arg1) - asc_int(arg2)));
case UFSUPPER: return(setupper(arg1, arg2), "");
case UFTIMES: return(int_asc(asc_int(arg1) * asc_int(arg2)));
case UFTRIM: return(trimstr(arg1));
case UFTRUTH: return(ltos(asc_int(arg1) == 42));
case UFUPPER: return(mkupper(arg1));
case UFXLATE: return(xlat(arg1, arg2, arg3));
}
meexit(-11); /* never should get here */
}
char *PASCAL NEAR gtusr(vname) /* look up a user var's value */
char *vname; /* name of user variable to fetch */
{
register int vnum; /* ordinal number of user var */
register char *vptr; /* temp pointer to function value */
register UTABLE *ut; /* ptr to the current variable table */
/* limit comparisons to significant length */
if (strlen(vname) >= NVSIZE) /* "%" counts, but is not passed */
vname[NVSIZE] = '\0';
/* scan through each user variable table starting with the
most local and going to the global table */
ut = uv_head;
while (ut) {
/* scan this table looking for the user var name */
for (vnum = 0; vnum < ut->size; vnum++) {
/* out of entries? */
if (ut->uv[vnum].u_name[0] == 0)
goto next_ut;
/* is this the one? */
if (strcmp(vname, ut->uv[vnum].u_name) == 0) {
/* return its value..... */
vptr = ut->uv[vnum].u_value;
if (vptr)
return(vptr);
else
return(errorm);
}
}
next_ut: ut = ut->next;
}
/* return errorm if we run off the end */
return(errorm);
}
char *PASCAL NEAR funval(i)
int i;
{
return(funcs[i].f_name);
}
char *PASCAL NEAR envval(i)
int i;
{
return(envars[i]);
}
PASCAL NEAR binary(key, tval, tlength, klength)
char *key; /* key string to look for */
char *(PASCAL NEAR *tval)(); /* ptr to function to fetch table value with */
int tlength; /* length of table to search */
int klength; /* maximum length of string to compare */
{
int l, u; /* lower and upper limits of binary search */
int i; /* current search index */
int cresult; /* result of comparison */
/* set current search limit as entire list */
l = 0;
u = tlength - 1;
/* get the midpoint! */
while (u >= l) {
i = (l + u) >> 1;
/* do the comparison */
cresult = strncmp(key, (*tval)(i), klength);
if (cresult == 0)
return(i);
if (cresult < 0)
u = i - 1;
else
l = i + 1;
}
return(-1);
}
char *PASCAL NEAR gtenv(vname)
char *vname; /* name of environment variable to retrieve */
{
register int vnum; /* ordinal number of var refrenced */
static char result[2 * NSTRING]; /* string result */
/* scan the list, looking for the referenced name */
vnum = binary(vname, envval, NEVARS, NVSIZE);
/* return errorm on a bad reference */
if (vnum == -1)
return(errorm);
/* otherwise, fetch the appropriate value */
switch (vnum) {
case EVABBELL: return(ltos(ab_bell));
case EVABCAP: return(ltos(ab_cap));
case EVABQUICK: return(ltos(ab_quick));
case EVACOUNT: return(int_asc(gacount));
case EVASAVE: return(int_asc(gasave));
case EVBUFHOOK: return(fixnull(getfname(&bufhook)));
case EVCBFLAGS: return(int_asc(curbp->b_flag));
case EVCBUFNAME:return(curbp->b_bname);
case EVCFNAME: return(curbp->b_fname);
case EVCMDHK: return(fixnull(getfname(&cmdhook)));
case EVCMODE: return(int_asc(curbp->b_mode));
case EVCURCHAR:
return(lused(curwp->w_dotp) ==
curwp->w_doto ? int_asc('\r') :
int_asc(lgetc(curwp->w_dotp, curwp->w_doto)));
case EVCURCOL: return(int_asc(getccol(FALSE)));
case EVCURLINE: return(long_asc(getlinenum(curbp, curwp->w_dotp)));
case EVCURWIDTH:return(int_asc(term.t_ncol));
case EVCURWIND: return(int_asc(getcwnum()));
case EVCWLINE: return(int_asc(getwpos()));
case EVDEBUG: return(ltos(macbug));
case EVDESKCLR: return(cname[deskcolor]);
case EVDIAGFLAG:return(ltos(diagflag));
case EVDISCMD: return(ltos(discmd));
case EVDISINP: return(ltos(disinp));
case EVDISPHIGH:return(ltos(disphigh));
case EVDISPUNDO:return(ltos(dispundo));
case EVEXBHOOK: return(fixnull(getfname(&exbhook)));
case EVEXITHOOK:return(fixnull(getfname(&exithook)));
case EVFCOL: return(int_asc(curwp->w_fcol));
case EVFILLCOL: return(int_asc(fillcol));
case EVFLICKER: return(ltos(flickcode));
case EVFMTLEAD: return(fmtlead);
case EVGFLAGS: return(int_asc(gflags));
case EVGMODE: return(int_asc(gmode));
case EVHARDTAB: return(int_asc(tabsize));
case EVHILITE: return(int_asc(hilite));
case EVHJUMP: return(int_asc(hjump));
case EVHSCRLBAR: return(ltos(hscrollbar));
case EVHSCROLL: return(ltos(hscroll));
case EVISTERM: return(cmdstr(isterm, result));
case EVKILL: return(getkill());
case EVLANG: return(LANGUAGE);
case EVLASTKEY: return(int_asc(lastkey));
case EVLASTMESG:return(lastmesg);
case EVLINE: return(getctext(result));
case EVLTERM: return(lterm);
case EVLWIDTH: return(int_asc(lused(curwp->w_dotp)));
case EVMATCH: return(fixnull(patmatch));
case EVMMOVE: return(int_asc(mouse_move));
case EVMODEFLAG:return(ltos(modeflag));
case EVMSFLAG: return(ltos(mouseflag));
case EVNEWSCRN: return(ltos(newscreenflag));
case EVNUMWIND: return(int_asc(gettwnum()));
case EVORGCOL: return(int_asc(term.t_colorg));
case EVORGROW: return(int_asc(term.t_roworg));
case EVOS: return(os);
case EVOVERLAP: return(int_asc(overlap));
case EVPAGELEN: return(int_asc(term.t_nrow + 1));
case EVPALETTE: return(palstr);
case EVPARALEAD:return(paralead);
case EVPENDING:
#if TYPEAH || WINDOW_MSWIN
return(ltos(typahead()));
#else
return(falsem);
#endif
case EVPOPFLAG: return(ltos(popflag));
case EVPOPWAIT: return(ltos(popwait));
case EVPOSFLAG: return(ltos(posflag));
case EVPROGNAME:return(PROGNAME);
case EVRAM: return(int_asc((int)(envram / 1024l)));
case EVREADHK: return(fixnull(getfname(&readhook)));
case EVREGION: return(getreg(result));
case EVREPLACE: return((char *)rpat);
case EVRVAL: return(rval);
case EVSCRNAME: return(first_screen->s_screen_name);
case EVSEARCH: return((char *)pat);
case EVSEARCHPNT: return(int_asc(searchtype));
case EVSEED: return(int_asc((int)seed));
case EVSOFTTAB: return(int_asc(stabsize));
case EVSRES: return(sres);
case EVSSAVE: return(ltos(ssave));
case EVSSCROLL: return(ltos(sscroll));
case EVSTATUS: return(ltos(cmdstatus));
case EVSTERM: return(cmdstr(sterm, result));
case EVTARGET: saveflag = lastflag;
return(int_asc(curgoal));
case EVTIME: return(timeset());
case EVTIMEFLAG: return(ltos(timeflag));
case EVTPAUSE: return(int_asc(term.t_pause));
case EVUNDOFLAG: return(ltos(undoflag));
case EVVERSION: return(VERSION);
case EVVSCRLBAR: return(ltos(vscrollbar));
case EVWCHARS: return(getwlist(result));
case EVWLINE: return(int_asc(curwp->w_ntrows));
case EVWRAPHK: return(fixnull(getfname(&wraphook)));
case EVWRITEHK: return(fixnull(getfname(&writehook)));
case EVXPOS: return(int_asc(xpos));
case EVYANKFLAG: return(ltos(yankflag));
case EVYPOS: return(int_asc(ypos));
}
meexit(-12); /* again, we should never get here */
}
char *PASCAL NEAR fixnull(s) /* Don't return NULL pointers! */
char *s;
{
if (s == NULL)
return("");
else
return(s);
}
/* return some of the contents of the kill buffer */
char *PASCAL NEAR getkill()
{
register int size; /* max number of chars left to return */
register char *sp; /* ptr into KILL block data chunk */
register char *vp; /* ptr into return value */
KILL *kptr; /* ptr to the current KILL block */
int counter; /* index into data chunk */
static char value[NSTRING]; /* temp buffer for value */
/* no kill buffer....just a null string */
if (kbufh[kill_index] == (KILL *)NULL) {
value[0] = 0;
return(value);
}
/* set up the output buffer */
vp = value;
size = NSTRING - 1;
/* backed up characters? */
if (kskip[kill_index] > 0) {
kptr = kbufh[kill_index];
sp = &(kptr->d_chunk[kskip[kill_index]]);
counter = kskip[kill_index];
while (counter++ < KBLOCK) {
*vp++ = *sp++;
if (--size == 0) {
*vp = 0;
return(value);
}
}
kptr = kptr->d_next;
} else {
kptr = kbufh[kill_index];
}
if (kptr != (KILL *)NULL) {
while (kptr != kbufp[kill_index]) {
sp = kptr->d_chunk;
for (counter = 0; counter < KBLOCK; counter++) {
*vp++ = *sp++;
if (--size == 0) {
*vp = 0;
return(value);
}
}
kptr = kptr->d_next;
}
counter = kused[kill_index];
sp = kptr->d_chunk;
while (counter--) {
*vp++ = *sp++;
if (--size == 0) {
*vp = 0;
return(value);
}
}
}
/* and return the constructed value */
*vp = 0;
return(value);
}
char *PASCAL NEAR trimstr(s) /* trim whitespace off the end of a string */
char *s; /* string to trim */
{
char *sp; /* backward index */
sp = s + strlen(s) - 1;
while ((sp >= s) && (*sp == ' ' || *sp == '\t'))
--sp;
*(sp+1) = 0;
return(s);
}
int PASCAL NEAR setvar(f, n) /* set a variable */
int f; /* default flag */
int n; /* numeric arg (can overide prompted value) */
{
register int status; /* status return */
VDESC vd; /* variable num/type */
char var[NVSIZE+1]; /* name of variable to fetch */
char value[NSTRING]; /* value to set variable to */
/* first get the variable to set.. */
if (clexec == FALSE) {
status = mlreply(TEXT51, &var[0], NVSIZE+1);
/* "Variable to set: " */
if (status != TRUE)
return(status);
} else { /* macro line argument */
/* grab token and skip it */
execstr = token(execstr, var, NVSIZE + 1);
}
/* check the legality and find the var */
findvar(var, &vd, NVSIZE + 1, VT_GLOBAL);
/* if its not legal....bitch */
if (vd.v_type == -1) {
mlwrite(TEXT52, var);
/* "%%No such variable as '%s'" */
return(FALSE);
}
/* get the value for that variable */
if (f == TRUE)
strcpy(value, int_asc(n));
else {
status = mlreply(TEXT53, &value[0], NSTRING);
/* "Value: " */
if (status == ABORT)
return(status);
}
/* and set the appropriate value */
status = svar(&vd, value);
/* if $debug == TRUE, every assignment will echo a statment to
that effect here. */
if (macbug && (strcmp(var, "%track") != 0)) {
strcpy(outline, "(((");
strcat(outline, var);
strcat(outline, " <- ");
/* and lastly the value we tried to assign */
strcat(outline, value);
strcat(outline, ")))");
/* write out the debug line */
mlforce(outline);
update(TRUE);
/* and get the keystroke to hold the output */
if (get_key() == abortc) {
mlforce(TEXT54);
/* "[Macro aborted]" */
status = FALSE;
}
}
/* and return it */
return(status);
}
int PASCAL NEAR global_var(f, n) /* declare a global variable */
int f; /* default flag */
int n; /* numeric arg (ignored here) */
{
register int status; /* status return */
VDESC vd; /* variable num/type */
char var[NVSIZE+1]; /* name of variable to fetch */
/* first get the variable to set.. */
if (clexec == FALSE) {
status = mlreply(TEXT249, &var[0], NVSIZE+1);
/* "Global variable to declare: " */
if (status != TRUE)
return(status);
} else { /* macro line argument */
/* grab token and skip it */
execstr = token(execstr, var, NVSIZE + 1);
}
/* check the legality and find the var */
findvar(var, &vd, NVSIZE + 1, VT_GLOBAL);
/* if its not legal....bitch */
if (vd.v_type == -1) {
mlwrite(TEXT52, var);
/* "%%No such variable as '%s'" */
return(FALSE);
}
/* and set the appropriate value */
status = svar(&vd, "");
/* if $debug == TRUE, every assignment will echo a statment to
that effect here. */
if (macbug && (strcmp(var, "%track") != 0)) {
strcpy(outline, "(((Globally declare ");
strcat(outline, var);
strcat(outline, ")))");
/* write out the debug line */
mlforce(outline);
update(TRUE);
/* and get the keystroke to hold the output */
if (get_key() == abortc) {
mlforce(TEXT54);
/* "[Macro aborted]" */
status = FALSE;
}
}
/* and return it */
return(status);
}
int PASCAL NEAR local_var(f, n) /* declare a local variable */
int f; /* default flag */
int n; /* numeric arg (ignored here) */
{
register int status; /* status return */
VDESC vd; /* variable num/type */
char var[NVSIZE+1]; /* name of variable to fetch */
/* first get the variable to set.. */
if (clexec == FALSE) {
status = mlreply(TEXT250, &var[0], NVSIZE+1);
/* "Local variable to declare: " */
if (status != TRUE)
return(status);
} else { /* macro line argument */
/* grab token and skip it */
execstr = token(execstr, var, NVSIZE + 1);
}
/* check the legality and find the var */
findvar(var, &vd, NVSIZE + 1, VT_LOCAL);
/* if its not legal....bitch */
if (vd.v_type == -1) {
mlwrite(TEXT52, var);
/* "%%No such variable as '%s'" */
return(FALSE);
}
/* and set the appropriate value */
status = svar(&vd, "");
/* if $debug == TRUE, every assignment will echo a statment to
that effect here. */
if (macbug && (strcmp(var, "%track") != 0)) {
strcpy(outline, "(((Locally declare ");
strcat(outline, var);
strcat(outline, ")))");
/* write out the debug line */
mlforce(outline);
update(TRUE);
/* and get the keystroke to hold the output */
if (get_key() == abortc) {
mlforce(TEXT54);
/* "[Macro aborted]" */
status = FALSE;
}
}
/* and return it */
return(status);
}
/* find a variables type and name */
VOID PASCAL NEAR findvar(var, vd, size, scope)
char *var; /* name of var to get */
VDESC *vd; /* structure to hold type and ptr */
int size; /* size of var array */
int scope; /* intended scope of any created user variables */
{
register int vnum; /* subscript in varable arrays */
register int vtype; /* type to return */
register UTABLE *vut; /* user var table to search */
fvar: vtype = -1;
vut = uv_head;
switch (var[0]) {
case '$': /* check for legal enviromnent var */
if ((vnum = binary(&var[1], envval, NEVARS, NVSIZE)) != -1)
vtype = TKENV;
break;
case '%': /* check for existing legal user variable */
while (vut) {
for (vnum = 0; vnum < vut->size; vnum++)
if (strcmp(&var[1],
vut->uv[vnum].u_name) == 0) {
vtype = TKVAR;
goto retvar;
}
vut = vut->next;
if (scope == VT_LOCAL)
break;
}
/* if we should not define one.... */
if (scope == VT_NONE)
break;
/* scope it as requested */
if (scope == VT_LOCAL)
vut = uv_head;
else
vut = uv_global;
/* no room left in requested user var table? */
if (vnum < vut->size)
break;
/* create a new variable */
for (vnum = 0; vnum < vut->size; vnum++)
if (vut->uv[vnum].u_name[0] == 0) {
vtype = TKVAR;
memset((char *)&vut->uv[vnum].u_name[0], '\0', NVSIZE);
strncpy(vut->uv[vnum].u_name, &var[1], NVSIZE);
vut->uv[vnum].u_value = NULL;
break;
}
break;
case '&': /* indirect operator? */
var[4] = 0;
if (strcmp(&var[1], "ind") == 0) {
/* grab token, and eval it */
execstr = token(execstr, var, size);
strcpy(var, fixnull(getval(var)));
goto fvar;
}
}
/* return the results */
retvar: vd->v_num = vnum;
vd->v_type = vtype;
vd->v_ut = vut;
return;
}
int PASCAL NEAR svar(var, value) /* set a variable */
VDESC *var; /* variable to set */
char *value; /* value to set to */
{
register int vnum; /* ordinal number of var refrenced */
register int vtype; /* type of variable to set */
register UTABLE *vut; /* user table pointer */
register int status; /* status return */
register int c; /* translated character */
register char *sp; /* scratch string pointer */
/* simplify the vd structure (we are gonna look at it a lot) */
vnum = var->v_num;
vtype = var->v_type;
vut = var->v_ut;
/* and set the appropriate value */
status = TRUE;
switch (vtype) {
case TKVAR: /* set a user variable */
if (vut->uv[vnum].u_value != NULL)
free(vut->uv[vnum].u_value);
sp = room(strlen(value) + 1);
if (sp == NULL)
return(FALSE);
strcpy(sp, value);
vut->uv[vnum].u_value = sp;
/* setting a variable to error stops macro execution */
if (strcmp(value, errorm) == 0)
status = FALSE;
break;
case TKENV: /* set an environment variable */
status = TRUE; /* by default */
switch (vnum) {
case EVABBELL: ab_bell = stol(value);
break;
case EVABCAP: ab_cap = stol(value);
break;
case EVABQUICK: ab_quick = stol(value);
break;
case EVACOUNT: gacount = asc_int(value);
break;
case EVASAVE: gasave = asc_int(value);
break;
case EVBUFHOOK: set_key(&bufhook, value);
break;
case EVCBFLAGS: c = asc_int(value);
curbp->b_flag = (curbp->b_flag & ~(BFCHG|BFINVS))
| (c & (BFCHG|BFINVS));
if ((c & BFCHG) == BFCHG)
lchange(WFMODE);
break;
case EVCBUFNAME:strcpy(curbp->b_bname, value);
curwp->w_flag |= WFMODE;
break;
case EVCFNAME: strcpy(curbp->b_fname, value);
#if WINDOW_MSWIN
fullpathname(curbp->b_fname, NFILEN);
#endif
curwp->w_flag |= WFMODE;
break;
case EVCMDHK: set_key(&cmdhook, value);
break;
case EVCMODE: curbp->b_mode = asc_int(value);
curwp->w_flag |= WFMODE;
break;
case EVCURCHAR: ldelete(1L, FALSE); /* delete 1 char */
c = asc_int(value);
if (c == '\r')
lnewline();
else
linsert(1, (char)c);
backchar(FALSE, 1);
break;
case EVCURCOL: status = setccol(asc_int(value));
break;
case EVCURLINE: status = gotoline(TRUE, asc_int(value));
break;
case EVCURWIDTH:status = newwidth(TRUE, asc_int(value));
break;
case EVCURWIND: nextwind(TRUE, asc_int(value));
break;
case EVCWLINE: status = forwline(TRUE,
asc_int(value) - getwpos());
break;
case EVDEBUG: macbug = stol(value);
break;
case EVDESKCLR: c = lookup_color(mkupper(value));
if (c != -1) {
deskcolor = c;
#if WINDOW_TEXT
refresh_screen(first_screen);
#endif
}
break;
case EVDIAGFLAG:diagflag = stol(value);
break;
case EVDISCMD: discmd = stol(value);
break;
case EVDISINP: disinp = stol(value);
break;
case EVDISPHIGH:
c = disphigh;
disphigh = stol(value);
if (c != disphigh)
upwind();
break;
case EVDISPUNDO:
dispundo = stol(value);
break;
case EVEXBHOOK: set_key(&exbhook, value);
break;
case EVEXITHOOK:set_key(&exithook, value);
break;
case EVFCOL: curwp->w_fcol = asc_int(value);
if (curwp->w_fcol < 0)
curwp->w_fcol = 0;
curwp->w_flag |= WFHARD | WFMODE;
break;
case EVFILLCOL: fillcol = asc_int(value);
break;
case EVFLICKER: flickcode = stol(value);
break;
case EVFMTLEAD: bytecopy(fmtlead, value, NSTRING);
break;
case EVGFLAGS: gflags = asc_int(value);
break;
case EVGMODE: gmode = asc_int(value);
break;
case EVHARDTAB: if ((c = asc_int(value)) >= 0)
{
tabsize = c;
upwind();
}
break;
case EVHILITE: hilite = asc_int(value);
if (hilite > NMARKS)
hilite = 255;
break;
case EVHJUMP: hjump = asc_int(value);
if (hjump < 1)
hjump = 1;
if (hjump > term.t_ncol - 1)
hjump = term.t_ncol - 1;
break;
case EVHSCRLBAR: hscrollbar = stol(value);
break;
case EVHSCROLL: hscroll = stol(value);
lbound = 0;
break;
case EVISTERM: isterm = stock(value);
break;
case EVKILL: break;
case EVLANG: break;
case EVLASTKEY: lastkey = asc_int(value);
break;
case EVLASTMESG:strcpy(lastmesg, value);
break;
case EVLINE: putctext(value);
break;
case EVLTERM: bytecopy(lterm, value, NSTRING);
break;
case EVLWIDTH: break;
case EVMATCH: break;
case EVMMOVE: mouse_move = asc_int(value);
if (mouse_move < 0) mouse_move = 0;
if (mouse_move > 2) mouse_move = 2;
break;
case EVMODEFLAG:modeflag = stol(value);
upwind();
break;
case EVMSFLAG: mouseflag = stol(value);
break;
case EVNEWSCRN: newscreenflag = stol(value);
break;
case EVNUMWIND: break;