forked from rfivet/uemacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.c
1061 lines (899 loc) · 26.6 KB
/
exec.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
/* exec.c -- implements exec.h */
#include "exec.h"
/* This file is for bindable functions dealing with execution of commands,
command lines, buffers, files and startup files.
written 1986 by Daniel Lawrence
modified by Petri Kutvonen
*/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "buffer.h"
#include "bind.h"
#include "defines.h" /* malloc/allocate, free/release */
#include "eval.h"
#include "file.h"
#include "flook.h"
#include "input.h"
#include "line.h"
#include "list.h"
#include "mlout.h"
#include "random.h"
#include "util.h"
#include "window.h"
static char *execstr = NULL ; /* pointer to string to execute */
boolean clexec = FALSE ; /* command line execution flag */
/* Directive definitions */
#define DBREAK 0
#define DELSE 1
#define DENDIF 2
#define DENDM 3
#define DENDWHILE 4
#define DFORCE 5
#define DGOTO 6
#define DGOSUB 7
#define DIF 8
#define DRETURN 9
#define DWHILE 10
/* directive name table: holds the names of all the directives.... */
static const char *dname[] = {
"break", "else", "endif", "endm", "endwhile",
"force", "goto", "gosub", "if", "return",
"while"
} ;
#define NUMDIRS ARRAY_SIZE( dname)
typedef struct caller {
struct caller *next ;
line_p caller ;
} *caller_p ;
/* The !WHILE directive in the execution language needs to
* stack references to pending whiles. These are stored linked
* to each currently open procedure via a linked list of
* the following structure.
*/
typedef struct while_block {
struct while_block *w_next ; /* next while */
line_p w_begin ; /* ptr to !while statement */
line_p w_end ; /* ptr to the !endwhile statement */
int w_type ; /* block type */
} *while_p ;
#define BTWHILE 1
#define BTBREAK 2
static char golabel[ NSTRING] = "" ; /* current line to go to */
static buffer_p bstore = NULL ; /* buffer to store macro text to */
static int storing_f = FALSE ; /* storing text to macro flag */
static int dobuf( buffer_p bp) ;
static int macarg( char *tok, int toksz) ;
/* Execute a named command even if it is not bound. */
BINDABLE( namedcmd) {
/* prompt the user to type a named command */
mloutstr( "execute-named-cmd: ");
/* and now get the function name to execute */
nbind_p nbp = getname() ;
if( nbp == NULL) /* abort */
return ABORT ;
fnp_t kfunc = nbp->n_func ;
if( kfunc == NULL)
return mloutfail( "(No such function)") ;
if( (bind_tag( nbp) & 1) && (curbp->b_mode & MDVIEW))
return rdonly() ;
/* and then execute the command */
return kfunc( f, n) ;
}
/* docmd:
take a passed string as a command line and translate it to be executed
as a command. This function will be used by execute-command-line and by
all source and startup files. Lastflag/thisflag is also updated.
* format of the command line is:
*
* {# arg} <command-name> {<argument string(s)>}
*
* char *cline; command line to execute
*/
static int docmd( char *cline) {
char tkn[ NSTRING] ; /* next token off of command line */
int status ;
char *oldestr = execstr ; /* save last ptr to string to execute */
execstr = cline ; /* and set this one as current */
/* first set up the default command values */
int f = FALSE ;
int n = 1 ;
lastflag = thisflag ;
thisflag = 0 ;
for( ;;) {
/* evaluate next token */
status = macarg( tkn, sizeof tkn) ;
if( status != TRUE || !*tkn) {
execstr = oldestr ;
return status ;
}
/* proceed if it is a command */
if( is_it_cmd( tkn))
break ;
/* otherwise set as argument of coming command */
f = TRUE ;
n = atoi( tkn) ;
}
/* and match the token to see if it exists */
nbind_p nbp = fncmatch( tkn) ;
fnp_t fnc = nbp->n_func ;
if( fnc == NULL) {
execstr = oldestr ;
return mloutfail( "(No such Function)") ;
}
if( (bind_tag( nbp) & 1) && (curbp->b_mode & MDVIEW))
status = rdonly() ;
else {
/* save the arguments and go execute the command */
boolean oldcle = clexec ; /* save old clexec flag */
clexec = TRUE ; /* in cline execution */
status = fnc( f, n) ; /* call the function */
clexec = oldcle ; /* restore clexec flag */
execstr = oldestr ;
}
cmdstatus = status ; /* save the status */
return status ;
}
/* execcmd:
* Execute a command line command to be typed in by the user
*
* int f, n; default Flag and Numeric argument
*/
BINDABLE( execcmd) {
char *cmdstr ; /* string holding command to execute */
/* get the line wanted */
int status = newmlarg( &cmdstr, "execute-command-line: ", 0) ;
if( status != TRUE)
return status ;
while( status == TRUE && n-- > 0)
status = docmd( cmdstr) ;
free( cmdstr) ;
return status ;
}
/* new token:
* chop a token off a string
* return a pointer past the token
*
* char *src in, source string
* char **tokref out, destination of newly allocated token string
*/
static char *newtoken( char *src, char **tokref) {
char *tok = malloc( NSTRING) ;
int size = (tok == NULL) ? 0 : NSTRING ;
int idx = 0 ; /* insertion point into token string */
/* first scan past any whitespace in the source string */
while( *src == ' ' || *src == '\t')
++src ;
/* scan through the source string */
boolean quote_f = FALSE ; /* is the current string quoted? */
char c = *src ;
for( ; c ; c = *++src) {
/* process special characters */
if( c == '~') {
c = *++src ;
if( c == 0)
break ;
switch( c) {
case 'r':
c = 13 ;
break ;
case 'n':
c = 10 ;
break ;
case 't':
c = 9 ;
break ;
case 'b':
c = 8 ;
break ;
case 'f':
c = 12 ;
break ;
}
} else {
/* check for the end of the token: EOS, space or comment */
if( quote_f) {
if( c == '"')
break ;
} else if( c == ' ' || c == '\t')
break ;
else if( c == '#' || c == ';') {
/* comments act like EOS */
c = 0 ;
break ;
}
/* set quote mode if quote found */
if( c == '"')
quote_f = TRUE ;
}
/* record the character */
if( idx < size - 1)
tok[ idx++] = c ;
else if( size > 1) {
char *tmptok ;
tmptok = malloc( size + 32) ;
if( tmptok == NULL)
size = 0 ; /* can't store more */
else {
memcpy( tmptok, tok, idx) ;
free( tok) ;
tok = tmptok ;
size += 32 ;
tok[ idx++] = c ;
}
}
}
if( tok != NULL)
tok[ idx] = 0 ;
*tokref = tok ;
return src + (c != 0) ;
}
static char *token( char *srcstr, char *tok, int maxtoksize) {
char *newtok ;
srcstr = newtoken( srcstr, &newtok) ;
if( newtok == NULL)
tok[ 0] = 0 ;
else {
mystrscpy( tok, newtok, maxtoksize) ;
free( newtok) ;
}
return srcstr ;
}
void gettoken( char *tok, int maxtoksize) {
execstr = token( execstr, tok, maxtoksize) ;
}
static char *getnewtoken( void) {
char *tok ;
execstr = newtoken( execstr, &tok) ;
return tok ;
}
boolean gettokval( char *tok, int size) {
char *tmpbuf ;
/* grab token and advance past */
tmpbuf = getnewtoken() ;
if( tmpbuf == NULL)
return FALSE ;
/* evaluate it */
mystrscpy( tok, getval( tmpbuf), size) ;
free( tmpbuf) ;
return TRUE ;
}
char *getnewtokval( void) {
char *tmpbuf ;
char *valbuf ;
/* grab token and advance past */
tmpbuf = getnewtoken() ;
if( tmpbuf == NULL)
return NULL ;
/* evaluate it */
const char *tmpval = getval( tmpbuf) ;
valbuf = malloc( strlen( tmpval) + 1 ) ;
if( valbuf != NULL)
strcpy( valbuf, tmpval) ;
free( tmpbuf) ;
return valbuf ;
}
/*
* get a macro line argument
*
* char *tok; buffer to place argument
*/
static int macarg( char *tok, int toksz) {
int status ;
boolean savcle ; /* buffer to store original clexec */
savcle = clexec ; /* save execution mode */
clexec = TRUE ; /* get the argument */
status = gettokval( tok, toksz) ;
clexec = savcle ; /* restore execution mode */
return status ;
}
/* storemac:
* Set up a macro buffer and start recording all command lines until !endm
*
* int f; default flag
* int n; macro number to use
*/
static char macbufname[] = "*Macro xx*" ;
#define MACDIGITPOS 7
static boolean setstore( char *bufname) {
/* set up the new macro buffer */
bstore = bfind( bufname, TRUE, BFINVS) ;
if( bstore == NULL) {
storing_f = FALSE ; /* should be already the case as we are executing */
return mloutfail( "Can not create macro") ;
}
/* and make sure it is empty */
bclear( bstore) ;
/* start recording */
storing_f = TRUE ;
return TRUE ;
}
BBINDABLE( storemac) {
/* must have a numeric argument to this function */
if( f == FALSE)
return mloutfail( "No macro number specified");
/* range check the macro number */
if( n < 1 || n > 40)
return mloutfail( "Macro number out of range") ;
/* construct the macro buffer name */
macbufname[ MACDIGITPOS] = '0' + (n / 10) ;
macbufname[ MACDIGITPOS + 1] = '0' + (n % 10) ;
return setstore( macbufname) ;
}
/* exec -- execute a buffer
** common to execute buffer, procedure and macro
*/
static int exec( int n, char *bufname, char *errstr) {
/* find the pointer to that buffer */
buffer_p bp = bfind( bufname, FALSE, 0) ;
if( bp == NULL) {
mloutfmt( "No such %s", errstr) ;
return FALSE ;
}
/* and now execute it as asked */
int status = TRUE ;
while( status == TRUE && n-- > 0)
status = dobuf( bp) ;
return status ;
}
/* storeproc:
* Set up a procedure buffer and start recording all command lines until !endm
*
* int f; default flag
* int n; macro number to use
*/
BINDABLE( storeproc) {
bname_t bname ; /* name of buffer to use */
char *name ;
/* a numeric argument means it is a numbered macro */
if( f == TRUE)
return storemac( f, n) ;
/* get the name of the procedure */
int status = newmlarg( &name, "Procedure name: ", sizeof bname - 2) ;
if( status != TRUE)
return status ;
/* construct the macro buffer name */
bname[ 0] = '*';
mystrscpy( &bname[ 1], name, sizeof bname - 2) ;
strcat( bname, "*") ;
free( name) ;
return setstore( bname) ;
}
/* execproc:
* Execute a procedure
*
* int f, n; default flag and numeric arg
*/
BINDABLE( execproc) {
bname_t bufn ; /* name of buffer to execute */
char *name ;
/* find out what buffer the user wants to execute */
int status = newmlarg( &name, "execute-procedure: ", sizeof bufn - 2) ;
if( status != TRUE)
return status ;
/* construct the buffer name */
bufn[ 0] = '*' ;
mystrscpy( &bufn[ 1], name, sizeof bufn - 2) ;
strcat( bufn, "*") ;
free( name) ;
return exec( n, bufn, "procedure") ;
}
/* execbuf:
* Execute the contents of a buffer of commands
*
* int f, n; default flag and numeric arg
*/
BINDABLE( execbuf) {
char *bufn ; /* name of buffer to execute */
/* find out what buffer the user wants to execute */
int status = newmlarg( &bufn, "execute-buffer: ", sizeof( bname_t)) ;
if( status != TRUE)
return status ;
status = exec( n, bufn, "buffer") ;
free( bufn) ;
return status ;
}
static boolean storeline( char *eline, int linlen) {
/* allocate the space for the line */
line_p mp = lalloc( linlen) ;
if( mp == NULL)
return mloutfail( "Out of memory while storing macro") ;
/* copy the text into the new line */
memcpy( mp->l_text, eline, linlen) ; /* lalloc has set lp->l_used */
/* attach the line to the end of the buffer */
bstore->b_linep->l_bp->l_fp = mp ;
mp->l_bp = bstore->b_linep->l_bp ;
bstore->b_linep->l_bp = mp ;
mp->l_fp = bstore->b_linep ;
return TRUE ;
}
/* dobuf:
* execute the contents of the buffer pointed to by the passed BP
*
* Directives start with a "!" and include:
*
* !endm End a macro
* !if (cond) conditional execution
* !else
* !endif
* !return Return (terminating current macro)
* !goto <label> Jump to a label in the current macro
* !force Force macro to continue...even if command fails
* !while (cond) Execute a loop if the condition is true
* !endwhile
*
* Line Labels begin with a "*" or ':' as the first nonblank char, like:
*
* *LBL01 or :LBL01
*
* buffer_p bp; buffer to execute
*/
static int dobuf( buffer_p bp) {
while_p whtemp ; /* temporary ptr to a struct while_block */
char *eline ; /* text of line to execute */
char tkn[ NSTRING] ; /* buffer to evaluate an expression in */
int w_type ; /* either while or break */
/* clear IF level flags/while ptr */
while_p whlist = NULL ; /* ptr to !WHILE list */
while_p scanner = NULL ; /* ptr during scan */
/* range of lines with labels, initially no lines in range */
line_p firstlbl = NULL ;
line_p eolbl = firstlbl ;
/* parse the buffer to execute, building WHILE header blocks */
storing_f = FALSE ;
const line_p hlp = bp->b_linep ; /* pointer to line header */
for( line_p lp = hlp->l_fp ; lp != hlp ; lp = lp->l_fp) {
/* scan the current line */
eline = lp->l_text ;
char const *eol = &eline[ lp->l_used] ;
for( ; eline < eol ; eline++)
if( *eline != ' ' && *eline != '\t')
break ;
/* delete blank and comment lines */
if( eline == eol || *eline == '#' || *eline == ';') {
lp = lp->l_bp ;
lfree( lp->l_fp) ;
continue ;
}
/* remove leading spaces */
if( eline != lp->l_text) {
int size = lp->l_used = eol - eline ;
memmove( lp->l_text, eline, size) ;
eline = lp->l_text ;
eol = &lp->l_text[ size] ;
}
/* handle storing */
if( storing_f) {
if( !strncmp( eline, "!endm", 5)) {
bstore = NULL ;
storing_f = FALSE ;
} else if( bstore && !storeline( lp->l_text, lp->l_used))
goto failexit ;
/* remove line */
lp = lp->l_bp ;
lfree( lp->l_fp) ;
continue ;
}
/* process labels, update the range of lines containing labels */
if( *eline == ':' || *eline == '*') {
if( firstlbl == NULL)
firstlbl = lp ;
eolbl = lp->l_fp ;
continue ;
}
/* process directives, skip others */
if( *eline != '!')
continue ;
if( !strncmp( eline, "!store", 6)) {
/* turn line into a string */
int size = lp->l_used - 6 ;
if( size)
memmove( lp->l_text, &eline[ 6], size) ;
eline = lp->l_text ;
eline[ size] = 0 ;
lp->l_used = size ;
/* get procedure or macro name */
token( eline, &tkn[ 1], sizeof tkn - 1) ;
char c = tkn[ 1] ;
if( !c) {
/* no name */
storing_f = TRUE ; /* just throw away the lines (no bstore) */
} else if( c >= '1' && c <= '9') { /* number >= 1 */
/* macro number */
if( !storemac( TRUE, atoi( &tkn[ 1])))
goto failexit ;
} else {
/* whatever */
*tkn = '*' ;
strcat( tkn, "*") ;
if( !setstore( tkn))
goto failexit ;
}
/* remove line */
lp = lp->l_bp ;
lfree( lp->l_fp) ;
continue ;
} else if( !strncmp( eline, "!while", 6)) {
/* if it is a while directive, make a block... */
w_type = BTWHILE ;
scan:
whtemp = malloc( sizeof *whtemp) ;
if( whtemp == NULL) {
mloutstr( "%%Out of memory during while scan") ;
goto failexit ;
}
whtemp->w_begin = lp ;
whtemp->w_type = w_type ;
whtemp->w_next = scanner ;
scanner = whtemp ;
} else if( !strncmp( eline, "!break", 6)) {
/* if is a BREAK directive, make a block... */
if( scanner == NULL) {
mloutstr( "%%!BREAK outside of any !WHILE loop") ;
goto failexit ;
}
w_type = BTBREAK ;
goto scan ;
} else if( !strncmp( eline, "!endwhile", 9)) {
/* if it is an endwhile directive, record the spot... */
if( scanner == NULL) {
mloutfmt( "%%!ENDWHILE with no preceding !WHILE in '%s'",
bp->b_bname) ;
goto failexit ;
}
/* move top records from the scanner list to the whlist until we
have moved all BREAK records and one WHILE record */
do {
scanner->w_end = lp ;
whtemp = whlist ;
whlist = scanner ;
scanner = scanner->w_next ;
whlist->w_next = whtemp ;
} while( whlist->w_type == BTBREAK) ;
}
}
/* check consistency after parsing */
if( storing_f) {
/* missing !endm */
mloutstr( "!store without !endm") ;
goto failexit ;
}
if( scanner != NULL) {
/* while and endwhile should match! */
mloutfmt( "%%!WHILE with no matching !ENDWHILE in '%s'", bp->b_bname) ;
failexit:
freelist( (list_p) scanner) ;
freelist( (list_p) whlist) ;
return FALSE ;
}
/* execute the parsed buffer */
/* let the first command inherit the flags from the last one.. */
thisflag = lastflag;
/* starting at the beginning of the buffer */
char *einit = NULL ; /* initial value of eline */
int esize = 0 ; /* size available for storage in einit */
int status = TRUE ; /* status of command execution */
boolean done = FALSE ;
int execlevel = 0 ; /* execution IF level */
caller_p returnto = NULL ;
line_p lp = hlp->l_fp ;
for( ; lp != hlp ; lp = lp->l_fp) {
/* turns macro line into a string pointed by eline */
int length = lp->l_used ;
if( length < lp->l_size) {
/* there is space for EOS, let's use the line */
eline = lp->l_text ;
} else {
/* make a copy using (pre-)allocated storage */
if( esize <= length) {
esize = length + 1 ;
char *newbuf = realloc( einit, esize) ;
if( newbuf == NULL) {
status = mloutfail( "%%Out of Memory during macro execution") ;
break ;
} else
einit = newbuf ;
}
eline = einit ;
memcpy( eline, lp->l_text, length) ;
}
eline[ length] = 0 ;
#if DEBUGM
/* if $debug == TRUE, every line to execute
gets echoed and a key needs to be pressed to continue
^G will abort the command */
if( macbug) {
/* debug macro name, if levels and lastly the line */
int c = mdbugout( "<<<%s:%d:%s>>>", bp->b_bname, execlevel, eline) ;
if( c == abortc) {
status = FALSE ;
break ;
} else if( c == metac) {
macbug = FALSE ;
}
}
#endif
/* if macro store is on, just salt this away */
if( storing_f) {
if( !strncmp( eline, "!endm", 5)) {
storing_f = FALSE ;
bstore = NULL ;
lp = lp->l_bp ;
lfree( lp->l_fp) ;
} else {
status = storeline( eline, strlen( eline)) ;
if( status != TRUE)
break ;
}
lp = lp->l_bp ;
lfree( lp->l_fp) ;
continue ;
}
/* skip labels */
if( *eline == '*' || *eline == ':')
continue ;
/* Parse directives here.... */
if( *eline == '!') {
/* Find out which directive this is */
++eline ;
int dirnum = NUMDIRS ;
while( --dirnum >= 0) {
length = strlen( dname[ dirnum]) ;
if( !strncmp( eline, dname[ dirnum], length )) {
eline += length ;
/* either EOS, comment, space separator or FAIL */
if( !*eline || *eline == ';' || *eline == '#')
;
else if( *eline == ' ' || *eline == '\t') {
eline += 1 ;
} else
dirnum = -1 ;
execstr = eline ; /* set source of token() and macarg() */
break ;
}
}
/* now, execute directives */
switch( dirnum) {
case DENDM:
if( execlevel == 0)
status = mloutfail( "!endm out of context") ;
break ;
case DIF: /* IF directive */
if( execlevel == 0) {
/* grab the value of the logical exp */
status = macarg( tkn, sizeof tkn) ;
if( status == TRUE && stol( tkn) == FALSE)
execlevel = 1 ; /* skip to else or endif */
} else
++execlevel ;
break ;
case DELSE: /* ELSE directive */
if( execlevel == 1)
execlevel = 0 ; /* execute the else part */
else if( execlevel == 0)
execlevel = 1 ; /* skip to endif */
/* else keep skipping */
break ;
case DENDIF: /* ENDIF directive */
if( execlevel)
--execlevel ;
break ;
case DWHILE: /* WHILE directive */
if( execlevel == 0) {
/* grab the value of the logical exp */
status = macarg( tkn, sizeof tkn) ;
if( status != TRUE || stol( tkn) == TRUE)
break ;
}
/* fallthrough */
case DBREAK: /* BREAK directive */
/* jump down to the endwhile */
/* find the right while loop */
for( whtemp = whlist ; whtemp ; whtemp = whtemp->w_next)
if( whtemp->w_begin == lp) {
/* reset the line pointer back.. */
lp = whtemp->w_end ;
break ;
}
if( whtemp == NULL)
status = mloutfail( "%%Internal While loop error") ;
break ;
case DENDWHILE: /* ENDWHILE directive */
assert( execlevel == 0) ;
/* find the right while loop */
for( whtemp = whlist ; whtemp ; whtemp = whtemp->w_next)
if( whtemp->w_type == BTWHILE && whtemp->w_end == lp) {
/* reset the line pointer back.. */
lp = whtemp->w_begin->l_bp ;
break ;
}
if( whtemp == NULL)
status = mloutfail( "%%Internal While loop error") ;
break ;
case DGOSUB: /* GOSUB directive */
case DGOTO: /* GOTO directive */
/* .....only if we are currently executing */
if( execlevel != 0)
break ;
if( firstlbl != NULL) {
line_p glp ; /* line to goto */
/* grab label to jump to */
eline = token( eline, golabel, sizeof golabel) ;
length = strlen( golabel) ;
for( glp = firstlbl ; glp != eolbl ; glp = glp->l_fp) {
char c = *glp->l_text ;
if( (c == '*' || c == ':')
&& !strncmp( &glp->l_text[ 1], golabel, length)) {
if( dirnum == DGOSUB) {
caller_p cp = malloc( sizeof *cp) ;
if( cp == NULL) {
status = mloutfail( "OoM on !gosub") ;
break ;
} else {
cp->caller = lp ;
cp->next = returnto ;
returnto = cp ;
}
}
lp = glp ;
break ;
}
}
if( glp == eolbl)
goto nolabel ;
} else {
nolabel:
status = mloutfail( "%%No such label") ;
}
break ;
case DRETURN: /* RETURN directive */
if( execlevel == 0) {
if( returnto != NULL) {
caller_p cp = returnto ;
lp = returnto->caller ;
returnto = returnto->next ;
free( cp) ;
} else {
lp = lp->l_fp ;
done = TRUE ;
}
}
break ;
case DFORCE: /* FORCE directive */
if( execlevel == 0)
docmd( eline) ; /* execute ignoring returned status */
break ;
default:
status = mloutfail( "%%Unknown Directive") ;
}
} else if( execlevel == 0)
status = docmd( eline) ; /* execute the statement */
if( done || status != TRUE)
break ;
}
/* refresh window if buffer is displayed */
if( bp->b_nwnd) {
for( window_p wp = wheadp ; wp != NULL ; wp = wp->w_wndp) {
if( wp->w_bufp == bp) {
/* and point it */
wp->w_dotp = lp ;
wp->w_doto = 0 ;
wp->w_flag |= WFHARD ;
}
}
}
/* in any case set the buffer . */
bp->b_dotp = lp ;
bp->b_doto = 0 ;
freelist( (list_p) returnto) ;
freelist( (list_p) whlist) ;
if( einit)
free( einit) ;
return status ;
}
/* execute a series of commands in a file
*
* int f, n; default flag and numeric arg to pass on to file
*/
BINDABLE( execfile) {
char *fname ; /* name of file to execute */
int status = newmlarg( &fname, "execute-file: ", 0) ;
if( status != TRUE)
return status ;
/* look up the path for the file */
char *fspec = flook( fname, FALSE) ; /* used to be TRUE, P.K. */
free( fname) ;
/* if it isn't around */
if( fspec == NULL)
return FALSE ;
/* otherwise, execute it */
while( status == TRUE && n-- > 0)
status = dofile( fspec) ;
return status ;
}
/* dofile:
* yank a file into a buffer and execute it
* if there are no errors, delete the buffer on exit
*
* char *fname; file name to execute
*/
int dofile( const char *fname) {
bname_t bname ; /* name of buffer */
makename( bname, fname) ; /* derive the name of the buffer */
unqname( bname) ; /* make sure we don't stomp things */
buffer_p bp = bfind( bname, TRUE, 0) ; /* get the needed buffer */
if( bp == NULL)
return FALSE ;
bp->b_mode = MDVIEW ; /* mark the buffer as read only */
buffer_p cb = curbp ; /* save the old buffer */
curbp = bp ; /* make this one current */
/* and try to read in the file to execute */
int status = readin( fname, FALSE) ;
curbp = cb ; /* restore the current buffer */
if( status == TRUE) {
/* go execute it! */
status = dobuf( bp) ;
if( status == TRUE && bp->b_nwnd == 0)
/* if not displayed, remove the now unneeded buffer and exit */
zotbuf( bp) ;
}
return status ;
}
/* cbuf: