-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathedit_source.c
1744 lines (1546 loc) · 53.3 KB
/
edit_source.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
#define CONFIGURE_VERSION 5
#define EDIT_SOURCE
#define NO_MALLOC
#define NO_SOCKETS
#define NO_OPCODES
#include "std.h"
#include "lex.h"
#include "preprocess.h"
#include "make_func.h"
#include "cc.h"
#include "hash.h"
#include <stdlib.h>
#include <unistd.h>
#ifdef WIN32
#include <process.h>
#include <malloc.h>
#endif
#if defined(DEBUG) || defined(WIN32)
#define TO_DEV_NULL ""
#else
#define TO_DEV_NULL ">/dev/null 2>&1"
#endif
/* Using an include file at this point would be bad */
#ifdef PEDANTIC
char *malloc(int);
char *realloc(char *, int);
void free(char *);
#endif
char *outp;
static int buffered = 0;
static int nexpands = 0;
FILE *yyin = 0, *yyout = 0;
#define SYNTAX "edit_source [-process file] [-options] [-malloc] [-build_func_spec 'command'] [-build_efuns] [-configure]\n"
/* The files we fool with. (Actually, there are more. See -process).
*
* TODO: teach this file how to fix bugs in the source code :)
*/
#define OPTIONS_INCL "options_incl.h"
#define PACKAGES "packages/packages"
#define OPTIONS_H "options.h"
#define LOCAL_OPTIONS "local_options"
#define OPTION_DEFINES "option_defs.c"
#define FUNC_SPEC "func_spec.c"
#define FUNC_SPEC_CPP "func_spec.cpp"
#define EFUN_TABLE "efunctions.h"
#define OPC_PROF "opc.h"
#define OPCODES "opcodes.h"
#define EFUN_PROTO "efun_protos.h"
#define EFUN_DEFS "efun_defs.c"
#define PRAGMA_NOTE_CASE_START 1
int num_packages = 0;
char *packages[100];
char ppchar;
char *current_file = 0;
int current_line;
int grammar_mode = 0; /* which section we're in for .y files */
int in_c_case, cquote, pragmas, block_nest;
char yytext[MAXLINE];
static char defbuf[DEFMAX];
typedef struct incstate_t {
struct incstate_t *next;
FILE *yyin;
int line;
char *file;
} incstate;
static incstate *inctop = 0;
#define CHAR_QUOTE 1
#define STRING_QUOTE 2
static void add_define (const char *, int, const char *);
#ifdef WIN32
#include <io.h>
int compile(char *command) {
FILE *tf = fopen("trash_me.bat","wt+");
fprintf(tf,"%s%s\n%s",
"@echo off\n",
command,
"if errorlevel == 1 goto error\n"
"del trash_me.err >nul\n"
"goto ok\n"
":error\n"
"echo ERROR > trash_me.err\n"
":ok\n");
fclose(tf);
if (!system("trash_me.bat > nul")) return 1;
if (_access("trash_me.err",0) ) return 1;
return 0;
}
#else
int compile (char * str) {
return system(str);
}
#endif
#if defined(WIN32)
int dos_style_link (char * x, char * y) {
char link_cmd[100];
sprintf(link_cmd, "copy %s %s", x, y);
return system(link_cmd);
}
#endif
void yyerror (const char * str)
{
fprintf(stderr, "%s:%d: %s\n", current_file, current_line, str);
exit(1);
}
void mf_fatal (const char * str)
{
yyerror(str);
}
void yywarn (char * str)
{
/* ignore errors :) local_options generates redefinition warnings,
which we don't want to see */
}
void yyerrorp (const char * str)
{
char buff[200];
sprintf(buff, str, ppchar);
fprintf(stderr, "%s:%d: %s\n", current_file, current_line, buff);
exit(1);
}
static void add_input (const char * p)
{
int l = strlen(p);
if (outp - l < defbuf) yyerror("Macro expansion buffer overflow.\n");
strncpy(outp - l, p, l);
outp -= l;
}
#define SKIPW(foo) while (isspace(*foo)) foo++;
static char *skip_comment(char *tmp, int flag)
{
int c;
for (;;) {
while ((c = *++tmp) != '*') {
if (c == EOF) yyerror("End of file in a comment");
if (c == '\n') {
nexpands = 0;
current_line++;
if (!fgets(yytext, MAXLINE - 1, yyin)) yyerror("End of file in a comment");
if (flag && yyout) fputs(yytext, yyout);
tmp = yytext - 1;
}
}
do {
if ((c = *++tmp) == '/')
return tmp + 1;
if (c == '\n') {
nexpands = 0;
current_line++;
if (!fgets(yytext, MAXLINE - 1, yyin)) yyerror("End of file in a comment");
if (flag && yyout) fputs(yytext, yyout);
tmp = yytext - 1;
}
} while (c == '*');
}
}
static void refill()
{
register char *p, *yyp;
int c;
if (fgets(p = yyp = defbuf + (DEFMAX >> 1), MAXLINE - 1, yyin)) {
while (((c = *yyp++) != '\n') && (c != EOF)) {
if (c == '/') {
if ((c = *yyp) == '*') {
yyp = skip_comment(yyp, 0);
continue;
}
else if (c == '/') break;
}
*p++ = c;
}
}
else yyerror("End of macro definition in \\");
nexpands = 0;
current_line++;
*p = 0;
return;
}
static void handle_define()
{
char namebuf[NSIZE];
char args[NARGS][NSIZE];
char mtext[MLEN];
char *end;
register char *tmp = outp, *q;
q = namebuf;
end = q + NSIZE - 1;
while (isalunum(*tmp)) {
if (q < end) *q++ = *tmp++;
else yyerror("Name too long.\n");
}
if (q == namebuf) yyerror("Macro name missing.\n");
if (*namebuf != '_' && !isalpha(*namebuf)) yyerror("Invalid macro name.\n");
*q = 0;
if (*tmp == '(') { /* if "function macro" */
int arg;
int inid;
char *ids = (char *) NULL;
tmp++; /* skip '(' */
SKIPW(tmp);
if (*tmp == ')') {
arg = 0;
} else {
for (arg = 0; arg < NARGS;) {
end = (q = args[arg]) + NSIZE - 1;
while (isalunum(*tmp) || (*tmp == '#')) {
if (q < end) *q++ = *tmp++;
else yyerror("Name too long.\n");
}
if (q == args[arg]) {
char buff[200];
sprintf(buff, "Missing argument %d in #define parameter list", arg + 1);
yyerror(buff);
}
arg++;
SKIPW(tmp);
if (*tmp == ')')
break;
if (*tmp++ != ',') {
yyerror("Missing ',' in #define parameter list");
}
SKIPW(tmp);
}
if (arg == NARGS) yyerror("Too many macro arguments");
}
tmp++; /* skip ')' */
end = mtext + MLEN - 2;
for (inid = 0, q = mtext; *tmp;) {
if (isalunum(*tmp)) {
if (!inid) {
inid++;
ids = tmp;
}
} else {
if (inid) {
int idlen = tmp - ids;
int n, l;
for (n = 0; n < arg; n++) {
l = strlen(args[n]);
if (l == idlen && strncmp(args[n], ids, l) == 0) {
q -= idlen;
*q++ = MARKS;
*q++ = n + MARKS + 1;
break;
}
}
inid = 0;
}
}
if ((*q = *tmp++) == MARKS) *++q = MARKS;
if (q < end) q++;
else yyerror("Macro text too long");
if (!*tmp && tmp[-2] == '\\') {
q -= 2;
refill();
tmp = defbuf + (DEFMAX >> 1);
}
}
*--q = 0;
add_define(namebuf, arg, mtext);
} else if (isspace(*tmp) || (!*tmp && (*(tmp+1) = '\0', *tmp = ' '))) {
end = mtext + MLEN - 2;
for (q = mtext; *tmp;) {
*q = *tmp++;
if (q < end) q++;
else yyerror("Macro text too long");
if (!*tmp && tmp[-2] == '\\') {
q -= 2;
refill();
tmp = defbuf + (DEFMAX >> 1);
}
}
*q = 0;
add_define(namebuf, -1, mtext);
} else {
yyerror("Illegal macro symbol");
}
return;
}
#define SKPW while (isspace(*outp)) outp++
static int cmygetc() {
int c;
for (;;) {
if ((c = *outp++) == '/') {
if ((c = *outp) == '*') outp = skip_comment(outp, 0);
else if (c == '/') return -1;
else return c;
} else return c;
}
}
/* Check if yytext is a macro and expand if it is. */
static int expand_define()
{
defn_t *p;
char expbuf[DEFMAX];
char *args[NARGS];
char buf[DEFMAX];
char *q, *e, *b;
if (nexpands++ > EXPANDMAX) yyerror("Too many macro expansions");
if (!(p = lookup_define(yytext))) return 0;
if (p->nargs == -1) {
add_input(p->exps);
} else {
int c, parcnt = 0, dquote = 0, squote = 0;
int n;
SKPW;
if (*outp++ != '(') yyerror("Missing '(' in macro call");
SKPW;
if ((c = *outp++) == ')')
n = 0;
else {
q = expbuf;
args[0] = q;
for (n = 0; n < NARGS;) {
switch (c) {
case '"':
if (!squote)
dquote ^= 1;
break;
case '\'':
if (!dquote)
squote ^= 1;
break;
case '(':
if (!squote && !dquote)
parcnt++;
break;
case ')':
if (!squote && !dquote)
parcnt--;
break;
case '#':
if (!squote && !dquote) {
*q++ = c;
if (*outp++ != '#') yyerror("'#' expected");
}
break;
case '\\':
if (squote || dquote) {
*q++ = c;
c = *outp++;
} break;
case '\n':
if (squote || dquote) yyerror("Newline in string");
break;
}
if (c == ',' && !parcnt && !dquote && !squote) {
*q++ = 0;
args[++n] = q;
} else if (parcnt < 0) {
*q++ = 0;
n++;
break;
} else {
if (c == EOF) yyerror("Unexpected end of file");
if (q >= expbuf + DEFMAX - 5) {
yyerror("Macro argument overflow");
} else {
*q++ = c;
}
}
if (!squote && !dquote) {
if ((c = cmygetc()) < 0) yyerror("End of macro in // comment");
}
else c = *outp++;
}
if (n == NARGS) {
yyerror("Maximum macro argument count exceeded");
return 0;
}
}
if (n != p->nargs) {
yyerror("Wrong number of macro arguments");
return 0;
}
/* Do expansion */
b = buf;
e = p->exps;
while (*e) {
if (*e == '#' && *(e + 1) == '#')
e += 2;
if (*e == MARKS) {
if (*++e == MARKS)
*b++ = *e++;
else {
for (q = args[*e++ - MARKS - 1]; *q;) {
*b++ = *q++;
if (b >= buf + DEFMAX) yyerror("Macro expansion overflow");
}
}
} else {
*b++ = *e++;
if (b >= buf + DEFMAX) yyerror("Macro expansion overflow");
}
}
*b++ = 0;
add_input(buf);
}
return 1;
}
static int exgetc()
{
register char c, *yyp;
SKPW;
while (isalpha(c = *outp) || c == '_') {
yyp = yytext;
do {
*yyp++ = c;
} while (isalnum(c = *++outp) || (c == '_'));
*yyp = '\0';
if (!strcmp(yytext, "defined")) {
/* handle the defined "function" in #/%if */
SKPW;
if (*outp++ != '(') yyerror("Missing ( after 'defined'");
SKPW;
yyp = yytext;
if (isalpha(c = *outp) || c == '_') {
do {
*yyp++ = c;
} while (isalnum(c = *++outp) || (c == '_'));
*yyp = '\0';
}
else yyerror("Incorrect definition macro after defined(\n");
SKPW;
if (*outp != ')') yyerror("Missing ) in defined");
if (lookup_define(yytext))
add_input("1 ");
else
add_input("0 ");
} else {
if (!expand_define())
add_input("0 ");
else SKPW;
}
}
return c;
}
static int skip_to(const char *token, const char *atoken)
{
char b[20], *p, *end;
int c;
int nest;
for (nest = 0;;) {
if (!fgets(outp = defbuf + (DEFMAX >> 1), MAXLINE-1,yyin)) {
yyerror("Unexpected end of file while skipping");
}
current_line++;
if ((c = *outp++) == ppchar) {
while (isspace(*outp)) outp++;
end = b + sizeof b - 1;
for (p = b; (c = *outp++) != '\n' && !isspace(c) && c != EOF;) {
if (p < end) *p++ = c;
}
*p = 0;
if (!strcmp(b, "if") || !strcmp(b, "ifdef") || !strcmp(b, "ifndef")) {
nest++;
} else if (nest > 0) {
if (!strcmp(b, "endif"))
nest--;
} else {
if (!strcmp(b, token)) {
*--outp = c;
add_input(b);
*--outp = ppchar;
buffered = 1;
return 1;
} else if (atoken && !strcmp(b, atoken)) {
*--outp = c;
add_input(b);
*--outp = ppchar;
buffered = 1;
return 0;
} else if (!strcmp(b, "elif")) {
*--outp = c;
add_input(b);
*--outp = ppchar;
buffered = 1;
return !atoken;
}
}
}
}
}
#include "preprocess.c"
static int maybe_open_input_file (const char * fn) {
if ((yyin = fopen(fn, "r")) == NULL) {
return 0;
}
if (current_file) free((char *)current_file);
current_file = (char *)malloc(strlen(fn) + 1);
current_line = 0;
strcpy(current_file, fn);
return 1;
}
static void open_input_file (const char * fn) {
if (!maybe_open_input_file(fn)) {
perror(fn);
exit(-1);
}
}
static void open_output_file (const char * fn) {
if ((yyout = fopen(fn, "w")) == NULL) {
perror(fn);
exit(-1);
}
}
static void close_output_file() {
fclose(yyout);
yyout = 0;
}
static char *protect (const char * p) {
static char buf[1024];
char *bufp = buf;
while (*p) {
if (*p=='"' || *p == '\\') *bufp++ = '\\';
*bufp++ = *p++;
}
*bufp = 0;
return buf;
}
static void
create_option_defines() {
defn_t *p;
int count = 0;
int i;
fprintf(stderr, "Writing build options to %s ...\n", OPTION_DEFINES);
open_output_file(OPTION_DEFINES);
fprintf(yyout, "{\n");
for (i = 0; i < DEFHASH; i++) {
for (p = defns[i]; p; p = p->next)
if (!(p->flags & DEF_IS_UNDEFINED)) {
count++;
fprintf(yyout, " \"__%s__\", \"%s\",\n",
p->name, protect(p->exps));
if (strncmp(p->name, "PACKAGE_", 8)==0) {
int len;
char *tmp, *t;
len = strlen(p->name + 8);
t = tmp = (char *)malloc(len + 1);
strcpy(tmp, p->name + 8);
while (*t) {
if (isupper(*t))
*t = tolower(*t);
t++;
}
if (num_packages == 100) {
fprintf(stderr, "Too many packages.\n");
exit(-1);
}
packages[num_packages++] = tmp;
}
}
}
fprintf(yyout,"};\n\n#define NUM_OPTION_DEFS %d\n\n", count);
close_output_file();
}
static void deltrail() {
register char *p;
p = outp;
while (*p && !isspace(*p) && *p != '\n') {
p++;
}
*p = 0;
}
static void
handle_include (char * name)
{
char *p;
static char buf[1024];
FILE *f;
incstate *is;
if (*name != '"') {
defn_t *d;
if ((d = lookup_define(name)) && d->nargs == -1) {
char *q;
q = d->exps;
while (isspace(*q))
q++;
handle_include(q);
} else {
yyerrorp("Missing leading \" in %cinclude");
}
return;
}
for (p = ++name; *p && *p != '"'; p++);
if (!*p) yyerrorp("Missing trailing \" in %cinclude");
*p = 0;
if ((f = fopen(name, "r")) != NULL) {
is = (incstate *)
malloc(sizeof(incstate) /*, 61, "handle_include: 1" */);
is->yyin = yyin;
is->line = current_line;
is->file = current_file;
is->next = inctop;
inctop = is;
current_line = 0;
current_file = (char *)malloc(strlen(name) + 1 /*, 62, "handle_include: 2" */);
strcpy(current_file, name);
yyin = f;
} else {
sprintf(buf, "Cannot %cinclude %s", ppchar, name);
yyerror(buf);
}
}
static void
handle_pragma (char * name)
{
if (!strcmp(name, "auto_note_compiler_case_start"))
pragmas |= PRAGMA_NOTE_CASE_START;
else if (!strcmp(name, "no_auto_note_compiler_case_start"))
pragmas &= ~PRAGMA_NOTE_CASE_START;
else if (!strncmp(name, "ppchar:", 7) && *(name + 8))
ppchar = *(name + 8);
else yyerrorp("Unidentified %cpragma");
}
static void
preprocess() {
register char *yyp, *yyp2;
int c;
int cond;
while (buffered ? (yyp = yyp2 = outp) : fgets(yyp = yyp2 = defbuf + (DEFMAX >> 1), MAXLINE-1, yyin)) {
if (!buffered) current_line++;
else buffered = 0;
while (isspace(*yyp2)) yyp2++;
if ((c = *yyp2) == ppchar) {
int quote = 0;
char sp_buf = 0, *oldoutp;
if (c == '%' && yyp2[1] == '%')
grammar_mode++;
outp = 0;
if (yyp != yyp2) yyerrorp("Misplaced '%c'.\n");
while (isspace(*++yyp2));
yyp++;
for (;;) {
if ((c = *yyp2++) == '"') quote ^= 1;
else{
if (!quote && c == '/') {
if (*yyp2 == '*') {
yyp2 = skip_comment(yyp2, 0);
continue;
}
else if (*yyp2 == '/') break;
}
if (!outp && isspace(c)) outp = yyp;
if (c == '\n' || c == EOF) break;
}
*yyp++ = c;
}
if (outp) {
if (yyout) sp_buf = *(oldoutp = outp);
*outp++ = 0;
while (isspace(*outp)) outp++;
}
else outp = yyp;
*yyp = 0;
yyp = defbuf + (DEFMAX >> 1) + 1;
if (!strcmp("define", yyp)) {
handle_define();
} else if (!strcmp("if", yyp)) {
cond = cond_get_exp(0);
if (*outp != '\n') yyerrorp("Condition too complex in %cif");
else handle_cond(cond);
} else if (!strcmp("ifdef", yyp)) {
deltrail();
handle_cond(lookup_define(outp) != 0);
} else if (!strcmp("ifndef", yyp)) {
deltrail();
handle_cond(!lookup_define(outp));
} else if (!strcmp("elif", yyp)) {
handle_elif();
} else if (!strcmp("else", yyp)) {
handle_else();
} else if (!strcmp("endif", yyp)) {
handle_endif();
} else if (!strcmp("undef", yyp)) {
defn_t *d;
deltrail();
if ((d = lookup_definition(outp))) {
d->flags |= DEF_IS_UNDEFINED;
d->flags &= ~DEF_IS_NOT_LOCAL;
} else {
add_define(outp, -1, " ");
d = lookup_definition(outp);
d->flags |= DEF_IS_UNDEFINED;
d->flags &= ~DEF_IS_NOT_LOCAL;
}
} else if (!strcmp("echo", yyp)) {
fprintf(stderr, "echo at line %d of %s: %s\n", current_line, current_file, outp);
} else if (!strcmp("include", yyp)) {
handle_include(outp);
} else if (!strcmp("pragma", yyp)) {
handle_pragma(outp);
} else if (yyout) {
if (!strcmp("line", yyp)) {
fprintf(yyout, "#line %d \"%s\"\n", current_line,
current_file);
} else {
if (sp_buf) *oldoutp = sp_buf;
if (pragmas & PRAGMA_NOTE_CASE_START) {
if (*yyp == '%') pragmas &= ~PRAGMA_NOTE_CASE_START;
}
fprintf(yyout, "%s\n", yyp-1);
}
} else {
char buff[200];
sprintf(buff, "Unrecognised %c directive : %s\n", ppchar, yyp);
yyerror(buff);
}
}
else if (c == '/') {
if ((c = *++yyp2) == '*') {
if (yyout) fputs(yyp, yyout);
yyp2 = skip_comment(yyp2, 1);
} else if (c == '/' && !yyout) continue;
else if (yyout) {
fprintf(yyout, "%s", yyp);
}
}
else if (yyout) {
fprintf(yyout, "%s", yyp);
if (pragmas & PRAGMA_NOTE_CASE_START) {
static int line_to_print;
line_to_print = 0;
if (!in_c_case) {
while (isalunum(*yyp2)) yyp2++;
while (isspace(*yyp2)) yyp2++;
if (*yyp2 == ':') {
in_c_case = 1;
yyp2++;
}
}
if (in_c_case) {
while ((c = *yyp2++)) {
switch(c) {
case '{':
{
if (!cquote && (++block_nest == 1))
line_to_print = 1;
break;
}
case '}':
{
if (!cquote) {
if (--block_nest < 0) yyerror("Too many }'s");
}
break;
}
case '"':
if (!(cquote & CHAR_QUOTE)) cquote ^= STRING_QUOTE;
break;
case '\'':
if (!(cquote & STRING_QUOTE)) cquote ^= CHAR_QUOTE;
break;
case '\\':
if (cquote && *yyp2) yyp2++;
break;
case '/':
if (!cquote) {
if ((c = *yyp2) == '*') {
yyp2 = skip_comment(yyp2, 1);
} else if (c == '/') {
*(yyp2-1) = '\n';
*yyp2 = '\0';
}
}
break;
case ':':
if (!cquote && !block_nest)
yyerror("Case started before ending previous case with ;");
break;
case ';':
if (!cquote && !block_nest) in_c_case = 0;
}
}
}
if (line_to_print)
fprintf(yyout, "#line %d \"%s\"\n", current_line + 1,current_file);
}
}
}
if (iftop) {
ifstate_t *p = iftop;
while (iftop) {
p = iftop;
iftop = p->next;
free((char *)p);
}
yyerrorp("Missing %cendif");
}
fclose(yyin);
free(current_file);
current_file = 0;
nexpands = 0;
if (inctop) {
incstate *p = inctop;
current_file = p->file;
current_line = p->line;
yyin = p->yyin;
inctop = p->next;
free((char *) p);
preprocess();
} else yyin = 0;
}
void make_efun_tables()
{
#define NUM_FILES 5
static const char* outfiles[NUM_FILES] = {
EFUN_TABLE, OPC_PROF, OPCODES, EFUN_PROTO, EFUN_DEFS
};
FILE *files[NUM_FILES];
int i;
fprintf(stderr, "Building efun tables ...\n");
for (i = 0; i < NUM_FILES; i++) {
files[i] = fopen(outfiles[i], "w");
if (!files[i]) {
fprintf(stderr, "make_func: unable to open %s\n", outfiles[i]);
exit(-1);
}
fprintf(files[i],
"/*\n\tThis file is automatically generated by make_func.\n");
fprintf(files[i],
"\tdo not make any manual changes to this file.\n*/\n\n");
}
fprintf(files[0],"\n#include \"efun_protos.h\"\n\n");
fprintf(files[0],"\ntypedef void (*func_t) (void);\n\n");
fprintf(files[0],"func_t efun_table[] = {\n");
fprintf(files[1],"\ntypedef struct opc_s { char *name; int count; } opc_t;\n\n");
fprintf(files[1],"opc_t opc_efun[] = {\n");
fprintf(files[2], "\n/* operators */\n\n");
for (i = 0; i < op_code; i++) {
fprintf(files[2],"#define %-30s %d\n", oper_codes[i], i+1);
}
fprintf(files[2],"\n/* 1 arg efuns */\n#define BASE %d\n\n", op_code+1);
for (i = 0; i < efun1_code; i++) {
fprintf(files[0],"\tf_%s,\n", efun1_names[i]);
fprintf(files[1],"{\"%s\", 0},\n", efun1_names[i]);
fprintf(files[2],"#define %-30s %d\n", efun1_codes[i], i+op_code+1);
fprintf(files[3],"void f_%s (void);\n", efun1_names[i]);
}
fprintf(files[2],"\n/* efuns */\n#define ONEARG_MAX %d\n\n", efun1_code + op_code+1);
for (i = 0; i < efun_code; i++) {
fprintf(files[0],"\tf_%s,\n", efun_names[i]);
fprintf(files[1],"{\"%s\", 0},\n", efun_names[i]);
fprintf(files[2],"#define %-30s %d\n", efun_codes[i], i+op_code+efun1_code+1);
fprintf(files[3],"void f_%s (void);\n", efun_names[i]);
}
fprintf(files[0], "};\n");
fprintf(files[1], "};\n");
if (efun1_code + op_code >= 256) {
fprintf(stderr, "You have way too many efuns. Contact the MudOS developers if you really need this many.\n");
}
if (efun_code >= 256) {
fprintf(stderr, "You have way too many efuns. Contact the MudOS developers if you really need this many.\n");
}
fprintf(files[2],"\n/* efuns */\n#define NUM_OPCODES %d\n\n", efun_code + efun1_code + op_code);
/* Now sort the main_list */
for (i = 0; i < num_buff; i++) {
int j;
for (j = 0; j < i; j++)
if (strcmp(key[i], key[j]) < 0) {
const char *tmp;
tmp = key[i]; key[i] = key[j]; key[j] = tmp;
tmp = buf[i]; buf[i] = buf[j]; buf[j] = tmp;
}
}
/* Now display it... */
fprintf(files[4], "{\n");
for (i = 0; i < num_buff; i++)
fprintf(files[4], "%s", buf[i]);
fprintf(files[4], "\n};\nint efun_arg_types[] = {\n");
for (i=0; i < last_current_type; i++) {
if (arg_types[i] == 0)
fprintf(files[4], "0,\n");
else
fprintf(files[4], "%s,", ctype(arg_types[i]));
}
fprintf(files[4],"};\n");
for (i=0; i < NUM_FILES; i++)
fclose(files[i]);
}
static void handle_local_defines(int check) {
defn_t *p;
int i;
int problem = 0;
for (i = 0; i < DEFHASH; i++)
for (p = defns[i]; p; p = p->next)
p->flags |= DEF_IS_NOT_LOCAL;
/* undefine _OPTIONS_H_ so it doesn't get propagated to the mudlib
or interfere with copies of options.h */
if ((p = lookup_define("_OPTIONS_H_"))) {
p->flags |= DEF_IS_UNDEFINED;
p->flags &= ~DEF_IS_NOT_LOCAL;
}
if ((p = lookup_define("DEBUG")))
p->flags &= ~DEF_IS_NOT_LOCAL;
ppchar = '#';
preprocess();
if ((p = lookup_define("_OPTIONS_H_")))
p->flags |= DEF_IS_UNDEFINED;