-
Notifications
You must be signed in to change notification settings - Fork 7
/
eval.c
1453 lines (1281 loc) · 32.7 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 -- implements eval.h */
#include "eval.h"
/* Expression evaluation functions
*
* written 1986 by Daniel Lawrence
* modified by Petri Kutvonen
*/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "basic.h"
#include "bind.h"
#include "buffer.h"
#include "defines.h"
#include "display.h"
#include "exec.h"
#include "execute.h"
#include "flook.h"
#include "input.h"
#include "line.h"
#include "random.h"
#include "search.h"
#include "terminal.h"
#include "termio.h"
#include "util.h"
#include "version.h"
#include "window.h"
/* Macro argument token types */
#define TKNUL 0 /* end-of-string */
#define TKARG 1 /* interactive argument */
#define TKBUF 2 /* buffer argument */
#define TKVAR 3 /* user variables */
#define TKENV 4 /* environment variables */
#define TKFUN 5 /* function.... */
#define TKDIR 6 /* directive */
#define TKLBL 7 /* line label */
#define TKLIT 8 /* numeric literal */
#define TKSTR 9 /* quoted string literal */
#define TKCMD 10 /* command name */
/* Emacs global flag bit definitions (for gflags). */
/* if GFREAD is set, current buffer will be set on first file (read in) */
#define GFREAD 1
static int gflags = GFREAD ; /* global control flag */
int readfirst_f( void) {
return GFREAD == (gflags & GFREAD) ;
}
int macbug = FALSE ; /* macro debuging flag */
int cmdstatus = TRUE ; /* last command status */
static int flickcode = FALSE ; /* do flicker supression? */
int rval = 0 ; /* return value of a subprocess */
static int saveflag = 0 ; /* Flags, saved with the $target var */
unsigned envram = 0 ; /* # of bytes current in use by malloc */
/* User variables ******************************************/
#define MAXVARS 255
#define NVSIZE 10 /* Max #chars in a var name. */
/* Structure to hold user variables and their definitions. */
static struct {
char u_name[ NVSIZE + 1] ; /* name of user variable */
char *u_value ; /* value (string) */
} uv[ MAXVARS + 1] ;
static char errorm[] = "ERROR" ; /* error literal */
static int seed = 0 ; /* random number seed */
static char *ltos( int val) ;
static char *mkupper( char *dst, char *src) ;
/* List of recognized environment variables. */
static const char *envars[] = {
"fillcol", /* current fill column */
"pagelen", /* number of lines used by editor */
"curcol", /* current column pos of cursor */
"curline", /* current line in file */
"ram", /* ram in use by malloc */
"flicker", /* flicker supression */
"curwidth", /* current screen width */
"cbufname", /* current buffer name */
"cfname", /* current file name */
"sres", /* current screen resolution */
"debug", /* macro debugging */
"status", /* returns the status of the last command */
"palette", /* current palette string */
"asave", /* # of chars between auto-saves */
"acount", /* # of chars until next auto-save */
"lastkey", /* last keyboard char struck */
"curchar", /* current character under the cursor */
"discmd", /* display commands on command line */
"version", /* current version number */
"progname", /* returns current prog name - "MicroEMACS" */
"seed", /* current random number seed */
"disinp", /* display command line input characters */
"wline", /* # of lines in current window */
"cwline", /* current screen line in window */
"target", /* target for line moves */
"search", /* search pattern */
"replace", /* replacement pattern */
"match", /* last matched magic pattern */
"kill", /* kill buffer (read only) */
"cmode", /* mode of current buffer */
"gmode", /* global modes */
"tpause", /* length to pause for paren matching */
"pending", /* type ahead pending flag */
"lwidth", /* width of current line */
"line", /* text of current line */
"gflags", /* global internal emacs flags */
"rval", /* child process return value */
"tab", /* tab width, 1... */
"hardtab", /* TRUE for hard coded tab, FALSE for soft ones */
"viewtab", /* TRUE to visualize hard coded tabs */
"overlap",
"jump",
#if SCROLLCODE
"scroll", /* scroll enabled */
#endif
};
/* And its preprocessor definitions. */
#define EVFILLCOL 0
#define EVPAGELEN 1
#define EVCURCOL 2
#define EVCURLINE 3
#define EVRAM 4
#define EVFLICKER 5
#define EVCURWIDTH 6
#define EVCBUFNAME 7
#define EVCFNAME 8
#define EVSRES 9
#define EVDEBUG 10
#define EVSTATUS 11
#define EVPALETTE 12
#define EVASAVE 13
#define EVACOUNT 14
#define EVLASTKEY 15
#define EVCURCHAR 16
#define EVDISCMD 17
#define EVVERSION 18
#define EVPROGNAME 19
#define EVSEED 20
#define EVDISINP 21
#define EVWLINE 22
#define EVCWLINE 23
#define EVTARGET 24
#define EVSEARCH 25
#define EVREPLACE 26
#define EVMATCH 27
#define EVKILL 28
#define EVCMODE 29
#define EVGMODE 30
#define EVTPAUSE 31
#define EVPENDING 32
#define EVLWIDTH 33
#define EVLINE 34
#define EVGFLAGS 35
#define EVRVAL 36
#define EVTAB 37
#define EVHARDTAB 38
#define EVVIEWTAB 39
#define EVOVERLAP 40
#define EVSCROLLCOUNT 41
#define EVSCROLL 42
enum function_type {
NILNAMIC = 0,
MONAMIC = (1 << 6),
DYNAMIC = (2 << 6),
TRINAMIC = (3 << 6)
} ;
#define ARGCOUNT( ft) (ft >> 6)
#define FUNID( ft) (ft & ((1 << 6) - 1))
enum function_code {
UFADD = 0, UFSUB, UFTIMES, UFDIV, UFMOD, UFNEG,
UFCAT, UFLEFT, UFRIGHT, UFMID, UFNOT, UFEQUAL,
UFLESS, UFGREATER, UFSEQUAL, UFSLESS, UFSGREAT, UFIND,
UFAND, UFOR, UFLENGTH, UFUPPER, UFLOWER, UFTRUTH,
UFASCII, UFCHR, UFGTKEY, UFRND, UFABS, UFSINDEX,
UFENV, UFBIND, UFEXIST, UFFIND, UFBAND, UFBOR,
UFBXOR, UFBNOT, UFXLATE
} ;
/* List of recognized user functions. */
static struct {
const char f_name[ 4] ;
const int f_type ;
} const funcs[] = {
{ "abs", UFABS | MONAMIC }, /* absolute value of a number */
{ "add", UFADD | DYNAMIC }, /* add two numbers together */
{ "and", UFAND | DYNAMIC }, /* logical and */
{ "asc", UFASCII | MONAMIC }, /* char to integer conversion */
{ "ban", UFBAND | DYNAMIC }, /* bitwise and 9-10-87 jwm */
{ "bin", UFBIND | MONAMIC }, /* look up function name bound to key */
{ "bno", UFBNOT | MONAMIC }, /* bitwise not */
{ "bor", UFBOR | DYNAMIC }, /* bitwise or 9-10-87 jwm */
{ "bxo", UFBXOR | DYNAMIC }, /* bitwise xor 9-10-87 jwm */
{ "cat", UFCAT | DYNAMIC }, /* concatenate string */
{ "chr", UFCHR | MONAMIC }, /* integer to char conversion */
{ "div", UFDIV | DYNAMIC }, /* division */
{ "env", UFENV | MONAMIC }, /* retrieve a system environment var */
{ "equ", UFEQUAL | DYNAMIC }, /* logical equality check */
{ "exi", UFEXIST | MONAMIC }, /* check if a file exists */
{ "fin", UFFIND | MONAMIC }, /* look for a file on the path... */
{ "gre", UFGREATER | DYNAMIC }, /* logical greater than */
{ "gtk", UFGTKEY | NILNAMIC }, /* get 1 charater */
{ "ind", UFIND | MONAMIC }, /* evaluate indirect value */
{ "lef", UFLEFT | DYNAMIC }, /* left string(string, len) */
{ "len", UFLENGTH | MONAMIC }, /* string length */
{ "les", UFLESS | DYNAMIC }, /* logical less than */
{ "low", UFLOWER | MONAMIC }, /* lower case string */
{ "mid", UFMID | TRINAMIC }, /* mid string(string, pos, len) */
{ "mod", UFMOD | DYNAMIC }, /* mod */
{ "neg", UFNEG | MONAMIC }, /* negate */
{ "not", UFNOT | MONAMIC }, /* logical not */
{ "or", UFOR | DYNAMIC }, /* logical or */
{ "rig", UFRIGHT | DYNAMIC }, /* right string(string, pos) */
{ "rnd", UFRND | MONAMIC }, /* get a random number */
{ "seq", UFSEQUAL | DYNAMIC }, /* string logical equality check */
{ "sgr", UFSGREAT | DYNAMIC }, /* string logical greater than */
{ "sin", UFSINDEX | DYNAMIC }, /* find the index of one string in another */
{ "sle", UFSLESS | DYNAMIC }, /* string logical less than */
{ "sub", UFSUB | DYNAMIC }, /* substraction */
{ "tim", UFTIMES | DYNAMIC }, /* multiplication */
{ "tru", UFTRUTH | MONAMIC }, /* Truth of the universe logical test */
{ "upp", UFUPPER | MONAMIC }, /* uppercase string */
{ "xla", UFXLATE | TRINAMIC }, /* XLATE character string translation */
} ;
/* When emacs' command interpetor needs to get a variable's name,
* rather than it's value, it is passed back as a variable description
* structure. The v_num field is a index into the appropriate variable table.
*/
typedef struct {
int v_type ; /* Type of variable. */
int v_num ; /* Ordinal pointer to variable in list. */
} variable_description ;
static void findvar( char *var, variable_description *vd, int size) ;
static int svar( variable_description *var, char *value) ;
static const char *i_to_a( int i) ;
/*
* putctext:
* replace the current line with the passed in text
*
* char *iline; contents of new line
*/
static int putctext( char *iline)
{
int status;
/* delete the current line */
curwp->w_doto = 0; /* starting at the beginning of the line */
if ((status = killtext(TRUE, 1)) != TRUE)
return status;
/* insert the new line */
if ((status = linstr(iline)) != TRUE)
return status;
status = lnewline();
backline(TRUE, 1);
return status;
}
static char *result ; /* string result */
static int ressize = 0 ; /* mark result as uninitialized */
static int ernd( int i) ;
static int sindex( char *source, char *pattern) ;
static char *xlat( char *source, char *lookup, char *trans) ;
/* Initialize the user variable list. */
void varinit(void)
{
int i;
for (i = 0; i < MAXVARS; i++)
uv[i].u_name[0] = 0;
if( ressize == 0) {
result = malloc( NSTRING) ;
ressize = NSTRING ;
}
seed = time( NULL) ;
}
/* Evaluate a function.
*
* @fname: name of function to evaluate.
*/
static const char *gtfun( char *fname) {
char *argv[ 3] ;
const char *retstr ; /* return value */
int i ;
/* look the function up in the function table */
fname[ 3] = 0 ; /* only first 3 chars significant */
mklower( fname) ; /* and let it be upper or lower case */
unsigned fnum = ARRAY_SIZE( funcs) ; /* index to function to eval */
int low = 0 ; /* binary search low bound */
int high = fnum - 1 ; /* binary search high bound */
do {
int s, cur ;
cur = (high + low) / 2 ;
s = strcmp( fname, funcs[ cur].f_name) ;
if( s == 0) {
fnum = cur ;
break ;
} else if( s < 0)
high = cur - 1 ;
else
low = cur + 1 ;
} while( low <= high) ;
/* return errorm on a bad reference */
if( fnum == ARRAY_SIZE( funcs))
return errorm ;
/* fetch arguments */
assert( clexec == TRUE) ; /* means macarg can be replaced by gettokval */
int argc = ARGCOUNT( funcs[ fnum].f_type) ;
for( int i = 0 ; i < argc ; i++) {
argv[ i] = getnewtokval() ;
if( argv[ i] == NULL) {
while( i > 0)
free( argv[ --i]) ;
return errorm ;
}
}
/* and now evaluate it! */
switch( FUNID( funcs[ fnum].f_type)) {
int sz, sz1 ;
unicode_t c ;
case UFADD:
retstr = i_to_a( atoi( argv[ 0]) + atoi( argv[ 1])) ;
break ;
case UFSUB:
retstr = i_to_a( atoi( argv[ 0]) - atoi( argv[ 1])) ;
break ;
case UFTIMES:
retstr = i_to_a( atoi( argv[ 0]) * atoi( argv[ 1])) ;
break ;
case UFDIV:
sz = atoi( argv[ 1]) ;
retstr = (sz == 0) ? errorm : i_to_a( atoi( argv[ 0]) / sz) ;
break ;
case UFMOD:
sz = atoi( argv[ 1]) ;
retstr = (sz == 0) ? errorm : i_to_a( atoi( argv[ 0]) % sz) ;
break ;
case UFNEG:
retstr = i_to_a( -atoi( argv[ 0])) ;
break ;
case UFCAT:
sz1 = strlen( argv[ 0]) ;
sz = sz1 + strlen( argv[ 1]) + 1 ;
if( sz > ressize) {
free( result) ;
result = malloc( sz) ;
ressize = sz ;
}
strcpy( result, argv[ 0]) ;
strcpy( &result[ sz1], argv[ 1]) ;
retstr = result ;
break ;
case UFLEFT:
sz1 = strlen( argv[ 0]) ;
sz = 0 ;
for( int i = atoi( argv[ 1]) ; sz < sz1 && i > 0 ; i -= 1)
sz += utf8_to_unicode( argv[ 0], sz, sz1, &c) ;
if( sz >= ressize) {
free( result) ;
result = malloc( sz + 1) ;
ressize = sz + 1 ;
}
mystrscpy( result, argv[ 0], sz + 1) ;
retstr = result ;
break ;
case UFRIGHT:
sz = strlen( argv[ 0]) ;
for( sz1 = atoi( argv[ 1]) ; sz1 > 0 && sz > 0 ; sz1--)
if( --sz > 0)
sz -= utf8_revdelta( (unsigned char *) &( argv[ 0])[ sz], sz) ;
retstr = &( argv[ 0])[ sz] ;
sz = strlen( retstr) ;
if( sz >= ressize) {
free( result) ;
ressize = sz + 1 ;
result = malloc( ressize) ;
}
retstr = strcpy( result, retstr) ;
break ;
case UFMID:
sz1 = strlen( argv[ 0]) ;
int start = 0 ;
for( i = atoi( argv[ 1]) - 1 ; start < sz1 && i > 0 ; i -= 1)
start += utf8_to_unicode( argv[ 0], start, sz1, &c) ;
sz = start ;
for( i = atoi( argv[ 2]) ; sz < sz1 && i > 0 ; i -= 1)
sz += utf8_to_unicode( argv[ 0], sz, sz1, &c) ;
sz -= start ;
if( sz >= ressize) {
free( result) ;
result = malloc( sz + 1) ;
ressize = sz + 1 ;
}
mystrscpy( result, &(argv[ 0][ start]), sz + 1) ;
retstr = result ;
break ;
case UFNOT:
retstr = ltos( stol( argv[ 0]) == FALSE) ;
break ;
case UFEQUAL:
retstr = ltos( atoi( argv[ 0]) == atoi( argv[ 1])) ;
break ;
case UFLESS:
retstr = ltos( atoi( argv[ 0]) < atoi( argv[ 1])) ;
break ;
case UFGREATER:
retstr = ltos( atoi( argv[ 0]) > atoi( argv[ 1])) ;
break ;
case UFSEQUAL:
retstr = ltos( strcmp( argv[ 0], argv[ 1]) == 0) ;
break ;
case UFSLESS:
retstr = ltos( strcmp( argv[ 0], argv[ 1]) < 0) ;
break ;
case UFSGREAT:
retstr = ltos( strcmp( argv[ 0], argv[ 1]) > 0) ;
break ;
case UFIND:
retstr = getval( argv[ 0]) ;
sz = strlen( retstr) + 1 ;
if( sz > ressize) {
free( result) ;
result = malloc( sz) ;
ressize = sz ;
}
retstr = strcpy( result, retstr) ;
break ;
case UFAND:
retstr = ltos( stol( argv[ 0]) && stol( argv[ 1])) ;
break ;
case UFOR:
retstr = ltos( stol( argv[ 0]) || stol( argv[ 1])) ;
break ;
case UFLENGTH:
retstr = i_to_a( strlen( argv[ 0])) ;
break ;
case UFUPPER:
sz = strlen( argv[ 0]) ;
if( sz >= ressize) {
free( result) ;
result = malloc( sz + 1) ;
ressize = sz + 1 ;
}
retstr = mkupper( result, argv[ 0]) ;
break ;
case UFLOWER:
sz = strlen( argv[ 0]) ;
if( sz >= ressize) {
free( result) ;
result = malloc( sz + 1) ;
ressize = sz + 1 ;
}
strcpy( result, argv[ 0]) ; /* result is at least as long as argv[ 0] */
retstr = mklower( result) ;
break ;
case UFTRUTH:
retstr = ltos( atoi( argv[ 0]) == 42) ;
break ;
case UFASCII:
utf8_to_unicode( argv[ 0], 0, 4, &c) ;
retstr = i_to_a( c) ;
break ;
case UFCHR:
c = atoi( argv[ 0]) ;
if( c > 0x10FFFF)
retstr = errorm ;
else {
sz = unicode_to_utf8( c, result) ;
result[ sz] = 0 ;
retstr = result ;
}
break ;
case UFGTKEY:
result[0] = tgetc();
result[1] = 0;
retstr = result ;
break ;
case UFRND:
retstr = i_to_a( ernd( atoi( argv[ 0]))) ;
break ;
case UFABS:
retstr = i_to_a( abs( atoi( argv[ 0]))) ;
break ;
case UFSINDEX:
retstr = i_to_a( sindex( argv[ 0], argv[ 1])) ;
break ;
case UFENV:
#if ENVFUNC
retstr = getenv( argv[ 0]) ;
if( retstr == NULL)
#endif
retstr = "" ;
break ;
case UFBIND:
retstr = transbind( argv[ 0]) ;
break ;
case UFEXIST:
retstr = ltos( fexist( argv[ 0])) ;
break ;
case UFFIND:
retstr = flook( argv[ 0], TRUE) ;
if( retstr == NULL)
retstr = "" ;
break ;
case UFBAND:
retstr = i_to_a( atoi( argv[ 0]) & atoi( argv[ 1])) ;
break ;
case UFBOR:
retstr = i_to_a( atoi( argv[ 0]) | atoi( argv[ 1])) ;
break ;
case UFBXOR:
retstr = i_to_a( atoi( argv[ 0]) ^ atoi( argv[ 1])) ;
break ;
case UFBNOT:
retstr = i_to_a( ~atoi( argv[ 0])) ;
break ;
case UFXLATE:
retstr = xlat( argv[ 0], argv[ 1], argv[ 2]) ;
break ;
default:
assert( FALSE) ; /* never should get here */
retstr = errorm ;
}
while( argc > 0)
free( argv[ --argc]) ;
return retstr ;
}
/*
* look up a user var's value
*
* char *vname; name of user variable to fetch
*/
static char *gtusr( char *vname) {
int vnum; /* ordinal number of user var */
/* scan the list looking for the user var name */
for (vnum = 0; vnum < MAXVARS; vnum++) {
if (uv[vnum].u_name[0] == 0)
break ;
if( strncmp( vname, uv[ vnum].u_name, NVSIZE) == 0)
return uv[vnum].u_value;
}
/* return errorm if we run off the end */
return errorm;
}
/* getctext: grab and return a string with the text of the current line */
static const char *getctext( void) {
static int rsize = 0 ;
static char *rline = NULL ; /* line to return */
/* find the contents of the current line and its length */
line_p lp = curwp->w_dotp ; /* line to copy */
int size = lp->l_used ; /* length of line to return */
if( size >= rsize) {
/* extend storage */
int newsize = size + 1 ;
char *newstr = realloc( rline, newsize) ;
if( newstr == NULL)
return "" ;
rline = newstr ;
rsize = newsize ;
}
/* copy it across */
memcpy( rline, lp->l_text, size) ;
rline[ size] = 0 ;
return rline ;
}
/* gtenv()
*
* char *vname; name of environment variable to retrieve
*/
static const char *gtenv( char *vname) {
unsigned vnum ; /* ordinal number of var referenced */
/* scan the list, looking for the referenced name */
for( vnum = 0 ; vnum < ARRAY_SIZE( envars) ; vnum++)
if( strcmp( vname, envars[ vnum]) == 0)
break ;
/* return errorm on a bad reference */
if( vnum == ARRAY_SIZE( envars)) {
#if ENVFUNC
char *ename = getenv(vname);
if( ename != NULL)
return ename ;
#endif
return errorm ;
}
/* otherwise, fetch the appropriate value */
switch (vnum) {
case EVFILLCOL:
return i_to_a(fillcol);
case EVPAGELEN:
return i_to_a(term.t_nrow + 1);
case EVCURCOL:
return i_to_a(getccol(FALSE));
case EVCURLINE:
return i_to_a(getcline());
case EVRAM:
return i_to_a((int) (envram / 1024l));
case EVFLICKER:
return ltos(flickcode);
case EVCURWIDTH:
return i_to_a(term.t_ncol);
case EVCBUFNAME:
return curbp->b_bname;
case EVCFNAME:
return curbp->b_fname;
case EVSRES:
return sres;
case EVDEBUG:
return ltos(macbug);
case EVSTATUS:
return ltos(cmdstatus);
case EVPALETTE: {
static char palstr[ 49] = "" ; /* palette string */
return palstr;
}
case EVASAVE:
return i_to_a(gasave);
case EVACOUNT:
return i_to_a(gacount);
case EVLASTKEY:
return i_to_a(lastkey);
case EVCURCHAR: {
unicode_t c ;
lgetchar( &c) ;
return i_to_a( c) ;
}
case EVDISCMD:
return ltos(discmd);
case EVVERSION:
return VERSION;
case EVPROGNAME:
return PROGRAM_NAME_UTF8 ;
case EVSEED:
return i_to_a(seed);
case EVDISINP:
return ltos(disinp);
case EVWLINE:
return i_to_a(curwp->w_ntrows);
case EVCWLINE:
return i_to_a(getwpos());
case EVTARGET:
saveflag = lastflag;
return i_to_a(curgoal);
case EVSEARCH:
return pat;
case EVREPLACE:
return rpat;
case EVMATCH:
return (patmatch == NULL) ? "" : patmatch;
case EVKILL:
return getkill();
case EVCMODE:
return i_to_a(curbp->b_mode);
case EVGMODE:
return i_to_a(gmode);
case EVTPAUSE:
return i_to_a(term.t_pause);
case EVPENDING:
#if TYPEAH
return ltos(typahead());
#else
return falsem;
#endif
case EVLWIDTH:
return i_to_a(llength(curwp->w_dotp));
case EVLINE:
return getctext();
case EVGFLAGS:
return i_to_a(gflags);
case EVRVAL:
return i_to_a(rval);
case EVTAB:
return i_to_a( tabwidth) ;
case EVHARDTAB:
return ltos( hardtab) ;
case EVVIEWTAB:
return ltos( viewtab) ;
case EVOVERLAP:
return i_to_a(overlap);
case EVSCROLLCOUNT:
return i_to_a(scrollcount);
#if SCROLLCODE
case EVSCROLL:
return ltos(term.t_scroll != NULL);
#else
case EVSCROLL:
return ltos(0);
#endif
}
assert( FALSE) ; /* again, we should never get here */
return errorm ;
}
/* set a variable
*
* int f; default flag
* int n; numeric arg (can overide prompted value)
*/
BINDABLE( setvar) {
int status; /* status return */
variable_description vd ; /* variable num/type */
char var[NVSIZE + 2]; /* name of variable to fetch %1234567890\0 */
char *value ; /* value to set variable to */
/* first get the variable to set.. */
if (clexec == FALSE) {
status = getstring( "Variable to set: ", var, sizeof var, nlc) ;
if (status != TRUE)
return status;
} else { /* macro line argument */
/* grab token and skip it */
gettoken( var, sizeof var) ;
}
/* check the legality and find the var */
findvar( var, &vd, sizeof var) ;
/* if its not legal....bitch */
if (vd.v_type == -1) {
mlwrite("%%No such variable as '%s'", var);
return FALSE;
}
/* get the value for that variable */
if( f == TRUE) {
value = malloc( NSTRING) ;
if( value == NULL)
return FALSE ;
/* a bit overcautious here */
mystrscpy( value, i_to_a( n), NSTRING) ;
} else {
status = newmlarg( &value, "Value: ", 0) ;
if (status != TRUE)
return status;
}
/* and set the appropriate value */
status = svar(&vd, value);
#if DEBUGM
/* if $debug == TRUE, every assignment will echo a statment to
that effect here. */
if( macbug)
if( abortc == mdbugout( "(((%s:%s:%s)))", ltos( status), var, value))
status = FALSE ;
#endif
/* and return it */
free( value) ;
return status;
}
static void mlforce( char *s) ;
#if DEBUGM
int mdbugout( char *fmt, ...) {
int c ; /* input from kbd, output to terminal */
int savediscmd ;
va_list ap ;
/* assignment status ; variable name ; value we tried to assign */
/* write out the debug line */
savediscmd = discmd ;
discmd = TRUE ;
va_start( ap, fmt) ;
vmlwrite( fmt, ap) ;
va_end( ap) ;
discmd = savediscmd ;
update( TRUE) ;
/* and get the keystroke to hold the output */
c = get1key() ;
if( c == abortc)
mlforce( "(Macro aborted)") ;
return c ;
}
#endif
/*
* Find a variables type and name.
*
* @var: name of variable to get.
* @vd: structure to hold type and pointer.
* @size: size of variable array.
*/
static void findvar(char *var, variable_description *vd, int size)
{
unsigned vnum = 0 ; /* subscript in variable arrays */
int vtype; /* type to return */
fvar:
vtype = -1;
switch (var[0]) {
case '$': /* check for legal enviromnent var */
for (vnum = 0; vnum < ARRAY_SIZE(envars); vnum++)
if (strcmp(&var[1], envars[vnum]) == 0) {
vtype = TKENV;
break;
}
break;
case '%': /* check for existing legal user variable */
for (vnum = 0; vnum < MAXVARS; vnum++)
if (strcmp(&var[1], uv[vnum].u_name) == 0) {
vtype = TKVAR;
break;
}
if (vnum < MAXVARS)
break;
/* create a new one??? */
for (vnum = 0; vnum < MAXVARS; vnum++)
if (uv[vnum].u_name[0] == 0) {
vtype = TKVAR;
mystrscpy( uv[ vnum].u_name, &var[ 1], NVSIZE + 1) ;
break;
}
break;
case '&': /* indirect operator? */
var[4] = 0;
if (strcmp(&var[1], "ind") == 0) {
/* grab token, and eval it */
if( TRUE == gettokval( var, size))
goto fvar ;
}
}
/* return the results */
vd->v_num = vnum;
vd->v_type = vtype;
return;
}
/*
* Set a variable.
*
* @var: variable to set.
* @value: value to set to.
*/
static int svar( variable_description *var, char *value)
{
int vnum; /* ordinal number of var refrenced */
int vtype; /* type of variable to set */
int status; /* status return */
int c; /* translated character */
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;
/* and set the appropriate value */
status = TRUE;
switch (vtype) {
case TKVAR: /* set a user variable */
if (uv[vnum].u_value != NULL)
free(uv[vnum].u_value);
sp = malloc(strlen(value) + 1);
if (sp == NULL)
return FALSE;
strcpy(sp, value);
uv[vnum].u_value = sp;
break;
case TKENV: /* set an environment variable */
status = TRUE; /* by default */
switch (vnum) {
case EVFILLCOL:
fillcol = atoi(value);
break;
case EVPAGELEN:
status = newsize(TRUE, atoi(value));
break;
case EVCURCOL:
status = setccol(atoi(value));
break;
case EVCURLINE:
status = gotoline(TRUE, atoi(value));
break;
case EVRAM:
break;
case EVFLICKER:
flickcode = stol(value);
break;
case EVCURWIDTH:
status = newwidth(TRUE, atoi(value));
break;
case EVCBUFNAME:
strcpy(curbp->b_bname, value);
curwp->w_flag |= WFMODE;
break;
case EVCFNAME:
strcpy(curbp->b_fname, value);
curwp->w_flag |= WFMODE;
break;
case EVSRES:
status = TTrez(value);
break;
case EVDEBUG:
macbug = stol(value);
break;
case EVSTATUS:
cmdstatus = stol(value);
break;
case EVASAVE:
gasave = atoi(value);
break;
case EVACOUNT:
gacount = atoi(value);
break;
case EVLASTKEY:
lastkey = atoi(value);
break;
case EVCURCHAR:
ldelchar(1, FALSE); /* delete 1 char */
c = atoi(value);
if (c == '\n')
lnewline();
else