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
|
/*
* ipp.c -- the Icon preprocessor.
*
* All Icon source passes through here before translation or compilation.
* Directives recognized are:
* #line n [filename]
* $line n [filename]
* $include filename
* $define identifier text
* $undef identifier
* $ifdef identifier
* $ifndef identifier
* $else
* $endif
* $error [text]
*
* Entry points are
* ppinit(fname,inclpath,m4flag) -- open input file
* ppdef(s,v) -- "$define s v", or "$undef s" if v is a null pointer
* ppch() -- return next preprocessed character
* ppecho() -- preprocess to stdout (for icont -E)
*
* See ../h/features.h for the set of predefined symbols.
*/
#include "../h/gsupport.h"
#define HTBINS 256 /* number of hash bins */
typedef struct fstruct { /* input file structure */
struct fstruct *prev; /* previous file */
char *fname; /* file name */
long lno; /* line number */
FILE *fp; /* stdio file pointer */
int m4flag; /* nz if preprocessed by m4 */
int ifdepth; /* $if nesting depth when opened */
} infile;
typedef struct bstruct { /* buffer pointer structure */
struct bstruct *prev; /* previous pointer structure */
struct cd *defn; /* definition being processed */
char *ptr; /* saved pointer value */
char *stop; /* saved stop value */
char *lim; /* saved limit value */
} buffer;
typedef struct { /* preprocessor token structure */
char *addr; /* beginning of token */
short len; /* length */
} ptok;
typedef struct cd { /* structure holding a definition */
struct cd *next; /* link to next defn */
struct cd *prev; /* link to previous defn */
short nlen, vlen; /* length of name & val */
char inuse; /* nonzero if curr being expanded */
char s[1]; /* name then value, as needed, no \0 */
} cdefn;
static int ppopen (char *fname, int m4);
static FILE * m4pipe (char *fname);
static char * rline (FILE *fp);
static void pushdef (cdefn *d);
static void pushline (char *fname, long lno);
static void ppdir (char *line);
static void pfatal (char *s1, char *s2);
static void skipcode (int doelse, int report);
static char * define (char *s);
static char * undef (char *s);
static char * ifdef (char *s);
static char * ifndef (char *s);
static char * ifxdef (char *s, int f);
static char * elsedir (char *s);
static char * endif (char *s);
static char * errdir (char *s);
static char * include (char *s);
static char * setline (char *s);
static char * wskip (char *s);
static char * nskip (char *s);
static char * matchq (char *s);
static char * getidt (char *dst, char *src);
static char * getfnm (char *dst, char *src);
static cdefn * dlookup (char *name, int len, char *val);
struct ppcmd {
char *name;
char *(*func)();
}
pplist[] = {
{ "define", define },
{ "undef", undef },
{ "ifdef", ifdef },
{ "ifndef", ifndef },
{ "else", elsedir },
{ "endif", endif },
{ "include", include },
{ "line", setline },
{ "error", errdir },
{ 0, 0 }};
static infile nofile; /* ancestor of all files; all zero */
static infile *curfile; /* pointer to current entry */
static buffer *bstack; /* stack of pending buffers */
static buffer *bfree; /* pool of free bstructs */
static char *buf; /* input line buffer */
static char *bnxt; /* next character */
static char *bstop; /* limit of preprocessed chars */
static char *blim; /* limit of all chars */
static cdefn *cbin[HTBINS]; /* hash bins for defn table */
static char *lpath; /* LPATH for finding source files */
static int ifdepth; /* depth of $if nesting */
extern int tfatals, nocode; /* provided by icont */
/*
* ppinit(fname, inclpath, m4) -- initialize preprocessor to read from fname.
*
* Returns 1 if successful, 0 if open failed.
*/
int ppinit(fname, inclpath, m4)
char *fname;
char *inclpath;
int m4;
{
int i;
cdefn *d, *n;
/*
* clear out any existing definitions from previous files
*/
for (i = 0; i < HTBINS; i++) {
for (d = cbin[i]; d != NULL; d = n) {
n = d->next;
free((char *)d);
}
cbin[i] = NULL;
}
/*
* install predefined symbols
*/
#define Feature(guard,symname,kwval) dlookup(symname, -1, "1");
#include "../h/features.h"
/*
* initialize variables and open source file
*/
lpath = inclpath;
curfile = &nofile; /* init file struct pointer */
return ppopen(fname, m4); /* open main source file */
}
/*
* ppopen(fname, m4) -- open a new file for reading by the preprocessor.
*
* Returns 1 if successful, 0 if open failed.
*
* Open calls may be nested. Files are closed when EOF is read.
*/
static int ppopen(fname, m4)
char *fname;
int m4;
{
FILE *f;
infile *fs;
for (fs = curfile; fs->fname != NULL; fs = fs->prev)
if (strcmp(fname, fs->fname) == 0) {
pfatal("circular include", fname); /* issue error message */
return 1; /* treat as success */
}
if (m4)
f = m4pipe(fname);
else if (curfile == &nofile && strcmp(fname, "-") == 0) { /* 1st file only */
f = stdin;
fname = "stdin";
}
else
f = fopen(fname, "r");
if (f == NULL) {
return 0;
}
fs = alloc(sizeof(infile));
fs->prev = curfile;
fs->fp = f;
fs->fname = salloc(fname);
fs->lno = 0;
fs->m4flag = m4;
fs->ifdepth = ifdepth;
pushline(fs->fname, 0L);
curfile = fs;
return 1;
}
/*
* m4pipe -- open a pipe from m4.
*/
static FILE *m4pipe(filename)
char *filename;
{
FILE *f;
char *s = alloc(4 + strlen(filename));
sprintf(s, "m4 %s", filename);
f = popen(s, "r");
free(s);
return f;
}
/*
* ppdef(s,v) -- define/undefine a symbol
*
* If v is a null pointer, undefines symbol s.
* Otherwise, defines s to have the value v.
* No error is given for a redefinition.
*/
void ppdef(s, v)
char *s, *v;
{
dlookup(s, -1, (char *)NULL);
if (v != NULL)
dlookup(s, -1, v);
}
/*
* ppecho() -- run input through preprocessor and echo directly to stdout.
*/
void ppecho()
{
int c;
while ((c = ppch()) != EOF)
putchar(c);
}
/*
* ppch() -- get preprocessed character.
*/
int ppch()
{
int c, f;
char *p;
buffer *b;
cdefn *d;
infile *fs;
for (;;) {
if (bnxt < bstop) /* if characters ready to go */
return ((int)*bnxt++) & 0xFF; /* return first one */
if (bnxt < blim) {
/*
* There are characters in the buffer, but they haven't been
* checked for substitutions yet. Process either one id, if
* that's what's next, or as much else as we can.
*/
f = *bnxt;
if (isalpha(f) || f == '_') {
/*
* This is the first character of an identifier. It could
* be the name of a definition. If so, the name will be
* contiguous in this buffer. Check it.
*/
p = bnxt + 1;
while (p < blim && (isalnum(c = *p) || c == '_')) /* find end */
p++;
bstop = p; /* safe to consume through end */
if (((d = dlookup(bnxt, p-bnxt, bnxt)) == 0) || (d->inuse == 1)) {
bnxt++;
return f; /* not defined; just use it */
}
/*
* We got a match. Remove the token from the input stream and
* push the replacement value.
*/
bnxt = p;
pushdef(d); /* make defn the curr buffer */
continue; /* loop to preprocess */
}
else {
/*
* Not an id. Find the end of non-id stuff and mark it as
* having been preprocessed. This is where we skip over
* string and cset literals to avoid processing them.
*/
p = bnxt++;
while (p < blim) {
c = *p;
if (isalpha(c) || c == '_') { /* there's an id ahead */
bstop = p;
return f;
}
else if (isdigit(c)) { /* numeric constant */
p = nskip(p);
}
else if (c == '#') { /* comment: skip to EOL */
bstop = blim;
return f;
}
else if (c == '"' || c == '\''){ /* quoted literal */
p = matchq(p); /* skip to end */
if (*p != '\0')
p++;
}
else
p++; /* else advance one char */
}
bstop = blim; /* mark end of processed chrs */
return f; /* return first char */
}
}
/*
* The buffer is empty. Revert to a previous buffer.
*/
if (bstack != NULL) {
b = bstack;
b->defn->inuse = 0;
bnxt = b->ptr;
bstop = b->stop;
blim = b->lim;
bstack = b->prev;
b->prev = bfree;
bfree = b;
continue; /* loop to preprocess */
}
/*
* There's nothing at all in memory. Read a new line.
*/
if ((buf = rline(curfile->fp)) != NULL) {
/*
* The read was successful.
*/
p = bnxt = bstop = blim = buf; /* reset buffer pointers */
curfile->lno++; /* bump line number */
while (isspace(c = *p))
p++; /* find first nonwhite */
if (c == '$' && (!ispunct(p[1]) || p[1]==' '))
ppdir(p + 1); /* handle preprocessor cmd */
else if (buf[1]=='l' && buf[2]=='i' && buf[3]=='n' && buf[4]=='e' &&
buf[0]=='#' && buf[5]==' ')
ppdir(p + 1); /* handle #line form */
else {
/*
* Not a preprocessor line; will need to scan for symbols.
*/
bnxt = buf;
blim = buf + strlen(buf);
bstop = bnxt; /* no chars scanned yet */
}
}
else {
/*
* The read hit EOF.
*/
if (curfile->ifdepth != ifdepth) {
pfatal("unterminated $if", (char *)0);
ifdepth = curfile->ifdepth;
}
/*
* switch to previous file and close current file.
*/
fs = curfile;
curfile = fs->prev;
if (fs->m4flag) { /* if m4 preprocessing */
void quit();
if (pclose(fs->fp) != 0) /* close pipe */
quit("m4 terminated abnormally");
}
else
fclose(fs->fp); /* close current file */
free((char *)fs->fname);
free((char *)fs);
if (curfile == &nofile) /* if at outer level, return EOF */
return EOF;
else /* else generate #line comment */
pushline(curfile->fname, curfile->lno);
}
}
}
/*
* rline(fp) -- read arbitrarily long line and return pointer.
*
* Allocates memory as needed. Returns NULL for EOF. Lines end with "\n\0".
*/
static char *rline(fp)
FILE *fp;
{
#define LINE_SIZE_INIT 100
#define LINE_SIZE_INCR 100
static char *lbuf = NULL; /* line buffer */
static int llen = 0; /* current buffer length */
register char *p;
register int c, n;
/* if first time, allocate buffer */
if (!lbuf) {
lbuf = alloc(LINE_SIZE_INIT);
llen = LINE_SIZE_INIT;
}
/* first character is special; return NULL if hit EOF here */
c = getc(fp);
if (c == EOF)
return NULL;
if (c == '\n')
return "\n";
p = lbuf;
n = llen - 3;
*p++ = c;
for (;;) {
/* read until buffer full; return after newline or EOF */
while (--n >= 0 && (c = getc(fp)) != '\n' && c != EOF)
*p++ = c;
if (n >= 0) {
*p++ = '\n'; /* always terminate with \n\0 */
*p++ = '\0';
return lbuf;
}
/* need to read more, so we need a bigger buffer */
llen += LINE_SIZE_INCR;
lbuf = realloc(lbuf, (unsigned int)llen);
if (!lbuf) {
fprintf(stderr, "rline(%d): out of memory\n", llen);
exit(EXIT_FAILURE);
}
p = lbuf + llen - LINE_SIZE_INCR - 2;
n = LINE_SIZE_INCR;
}
}
/*
* pushdef(d) -- insert definition into the input stream.
*/
static void pushdef(d)
cdefn *d;
{
buffer *b;
d->inuse = 1;
b = bfree;
if (b == NULL)
b = (buffer *)alloc(sizeof(buffer));
else
bfree = b->prev;
b->prev = bstack;
b->defn = d;
b->ptr = bnxt;
b->stop = bstop;
b->lim = blim;
bstack = b;
bnxt = bstop = d->s + d->nlen;
blim = bnxt + d->vlen;
}
/*
* pushline(fname,lno) -- push #line directive into input stream.
*/
static void pushline(fname, lno)
char *fname;
long lno;
{
static char tbuf[200];
sprintf(tbuf, "#line %ld \"%s\"\n", lno, fname);
bnxt = tbuf;
bstop = blim = tbuf + strlen(tbuf);
}
/*
* ppdir(s) -- handle preprocessing directive.
*
* s is the portion of the line following the $.
*/
static void ppdir(s)
char *s;
{
char b0, *cmd, *errmsg;
struct ppcmd *p;
b0 = buf[0]; /* remember first char of line */
bnxt = "\n"; /* set buffer pointers to empty line */
bstop = blim = bnxt + 1;
s = wskip(s); /* skip whitespace */
s = getidt(cmd = s - 1, s); /* get command name */
s = wskip(s); /* skip whitespace */
for (p = pplist; p->name != NULL; p++) /* find name in table */
if (strcmp(cmd, p->name) == 0) {
errmsg = (*p->func)(s); /* process directive */
if (errmsg != NULL && (p->func != setline || b0 != '#'))
pfatal(errmsg, (char *)0); /* issue err if not from #line form */
return;
}
pfatal("invalid preprocessing directive", cmd);
}
/*
* pfatal(s1,s2) -- output a preprocessing error message.
*
* s1 is the error message; s2 is the offending value, if any.
* If s2 ends in a newline, the newline is truncated in place.
*
* We can't use tfatal() because we have our own line counter which may be
* out of sync with the lexical analyzer's.
*/
static void pfatal(s1, s2)
char *s1, *s2;
{
int n;
fprintf(stderr, "File %s; Line %ld # ", curfile->fname, curfile->lno);
if (s2 != NULL && *s2 != '\0') {
n = strlen(s2);
if (n > 0 && s2[n-1] == '\n')
s2[n-1] = '\0'; /* remove newline */
fprintf(stderr, "\"%s\": ", s2); /* print offending value */
}
fprintf(stderr, "%s\n", s1); /* print diagnostic */
tfatals++;
nocode++;
}
/*
* errdir(s) -- handle deliberate $error.
*/
static char *errdir(s)
char *s;
{
pfatal("explicit $error", s); /* issue msg with text */
return NULL;
}
/*
* define(s) -- handle $define directive.
*/
static char *define(s)
char *s;
{
char c, *name, *val;
if (isalpha(c = *s) || c == '_')
s = getidt(name = s - 1, s); /* get name */
else
return "$define: missing name";
if (*s == '(')
return "$define: \"(\" after name requires preceding space";
val = s = wskip(s);
if (*s != '\0') {
while ((c = *s) != '\0' && c != '#') { /* scan value */
if (c == '"' || c == '\'') {
s = matchq(s);
if (*s == '\0')
return "$define: unterminated literal";
}
s++;
}
while (isspace(s[-1])) /* trim trailing whitespace */
s--;
}
*s = '\0';
dlookup(name, -1, val); /* install in table */
return NULL;
}
/*
* undef(s) -- handle $undef directive.
*/
static char *undef(s)
char *s;
{
char c, *name;
if (isalpha(c = *s) || c == '_')
s = getidt(name = s - 1, s); /* get name */
else
return "$undef: missing name";
if (*wskip(s) != '\0')
return "$undef: too many arguments";
dlookup(name, -1, (char *)NULL);
return NULL;
}
/*
* include(s) -- handle $include directive.
*/
static char *include(s)
char *s;
{
char *fname;
char fullpath[MaxPath];
s = getfnm(fname = s - 1, s);
if (*fname == '\0')
return "$include: invalid file name";
if (*wskip(s) != '\0')
return "$include: too many arguments";
if (!pathfind(fullpath, lpath, fname, (char *)NULL) || !ppopen(fullpath, 0))
pfatal("cannot open", fname);
return NULL;
}
/*
* setline(s) -- handle $line (or #line) directive.
*/
static char *setline(s)
char *s;
{
long n;
char c;
char *fname;
if (!isdigit(c = *s))
return "$line: no line number";
n = c - '0';
while (isdigit(c = *++s)) /* extract line number */
n = 10 * n + c - '0';
s = wskip(s); /* skip whitespace */
if (isalpha (c = *s) || c == '_' || c == '"') { /* if filename */
s = getfnm(fname = s - 1, s); /* extract it */
if (*fname == '\0')
return "$line: invalid file name";
}
else
fname = NULL;
if (*wskip(s) != '\0')
return "$line: too many arguments";
curfile->lno = n; /* set line number */
if (fname != NULL) { /* also set filename if given */
free(curfile->fname);
curfile->fname = salloc(fname);
}
pushline(curfile->fname, curfile->lno);
return NULL;
}
/*
* ifdef(s), ifndef(s) -- conditional processing if s is/isn't defined.
*/
static char *ifdef(s)
char *s;
{
return ifxdef(s, 1);
}
static char *ifndef(s)
char *s;
{
return ifxdef(s, 0);
}
/*
* ifxdef(s) -- handle $ifdef (if n is 1) or $ifndef (if n is 0).
*/
static char *ifxdef(s, f)
char *s;
int f;
{
char c, *name;
ifdepth++;
if (isalpha(c = *s) || c == '_')
s = getidt(name = s - 1, s); /* get name */
else
return "$ifdef/$ifndef: missing name";
if (*wskip(s) != '\0')
return "$ifdef/$ifndef: too many arguments";
if ((dlookup(name, -1, name) != NULL) ^ f)
skipcode(1, 1); /* skip to $else or $endif */
return NULL;
}
/*
* elsedir(s) -- handle $else by skipping to $endif.
*/
static char *elsedir(s)
char *s;
{
if (ifdepth <= curfile->ifdepth)
return "unexpected $else";
if (*s != '\0')
pfatal ("extraneous arguments on $else/$endif", s);
skipcode(0, 1); /* skip the $else section */
return NULL;
}
/*
* endif(s) -- handle $endif.
*/
static char *endif(s)
char *s;
{
if (ifdepth <= curfile->ifdepth)
return "unexpected $endif";
if (*s != '\0')
pfatal ("extraneous arguments on $else/$endif", s);
ifdepth--;
return NULL;
}
/*
* skipcode(doelse,report) -- skip code to $else (doelse=1) or $endif (=0).
*
* If report is nonzero, generate #line directive at end of skip.
*/
static void skipcode(doelse, report)
int doelse, report;
{
char c, *p, *cmd;
while ((p = buf = rline(curfile->fp)) != NULL) {
curfile->lno++; /* bump line number */
/*
* Handle #line form encountered while skipping.
*/
if (buf[1]=='l' && buf[2]=='i' && buf[3]=='n' && buf[4]=='e' &&
buf[0]=='#' && buf[5]==' ') {
ppdir(buf + 1); /* interpret #line */
continue;
}
/*
* Check for any other kind of preprocessing directive.
*/
while (isspace(c = *p))
p++; /* find first nonwhite */
if (c != '$' || (ispunct(p[1]) && p[1]!=' '))
continue; /* not a preprocessing directive */
p = wskip(p+1); /* skip whitespace */
p = getidt(cmd = p-1, p); /* get command name */
p = wskip(p); /* skip whitespace */
/*
* Check for a directive that needs special attention.
* Deliberately accept any form of $if... as valid
* in anticipation of possible future extensions;
* this allows them to appear here if commented out.
*/
if (cmd[0] == 'i' && cmd[1] == 'f') {
ifdepth++;
skipcode(0, 0); /* skip to $endif */
}
else if (strcmp(cmd, "line") == 0)
setline(p); /* process $line, ignore errors */
else if (strcmp(cmd, "endif") == 0 ||
(doelse == 1 && strcmp(cmd, "else") == 0)) {
/*
* Time to stop skipping.
*/
if (*p != '\0')
pfatal ("extraneous arguments on $else/$endif", p);
if (cmd[1] == 'n') /* if $endif */
ifdepth--;
if (report)
pushline(curfile->fname, curfile->lno);
return;
}
}
/*
* At EOF, just return; main loop will report unterminated $if.
*/
}
/*
* Token scanning functions.
*/
/*
* wskip(s) -- skip whitespace and return updated pointer
*
* If '#' is encountered, skips to end of string.
*/
static char *wskip(s)
char *s;
{
char c;
while (isspace(c = *s))
s++;
if (c == '#')
while ((c = *++s) != 0)
;
return s;
}
/*
* nskip(s) -- skip over numeric constant and return updated pointer.
*/
static char *nskip(s)
char *s;
{
char c;
while (isdigit(c = *++s))
;
if (c == 'r' || c == 'R') {
while (isalnum(c = *++s))
;
return s;
}
if (c == '.')
while (isdigit (c = *++s))
;
if (c == 'e' || c == 'E') {
c = s[1];
if (c == '+' || c == '-')
s++;
while (isdigit (c = *++s))
;
}
return s;
}
/*
* matchq(s) -- scan for matching quote character and return pointer.
*
* Taking *s as the quote character, s is incremented until it points
* to either another occurrence of the character or the '\0' terminating
* the string. Escaped quote characters do not stop the scan. The
* updated pointer is returned.
*/
static char *matchq(s)
char *s;
{
char c, q;
q = *s;
if (q == '\0')
return s;
while ((c = *++s) != q && c != '\0') {
if (c == '\\')
if (*++s == '\0')
return s;
}
return s;
}
/*
* getidt(dst,src) -- extract identifier, return updated pointer
*
* The identifier (in Icon terms, "many(&letters++&digits++'_')")
* at src is copied to dst and '\0' is appended. A pointer to the
* character following the identifier is returned.
*
* dst may partially overlap src if dst has a lower address. This
* is typically done to avoid the need for another arbitrarily-long
* buffer. An offset of -1 allows room for insertion of the '\0'.
*/
static char *getidt(dst, src)
char *dst, *src;
{
char c;
while (isalnum(c = *src) || (c == '_')) {
*dst++ = c;
src++;
}
*dst = '\0';
return src;
}
/*
* getfnm(dst,src) -- extract filename, return updated pointer
*
* Similarly to getidt, getfnm extracts a quoted or unquoted file name.
* An empty string at dst indicates a missing or unterminated file name.
*/
static char *getfnm(dst, src)
char *dst, *src;
{
char *lim;
if (*src != '"')
return getidt(dst, src);
lim = matchq(src);
if (*lim != '"') {
*dst = '\0';
return lim;
}
while (++src < lim)
if ((*dst++ = *src) == '\\')
dst[-1] = *++src;
*dst = '\0';
return lim + 1;
}
/*
* dlookup(name, len, val) look up entry in definition table.
*
* If val == name, return the existing value, or NULL if undefined.
* If val == NULL, delete any existing value and undefine the name.
* If val != NULL, install a new value, and print error if different.
*
* If name is null, the call is ignored.
* If len < 0, strlen(name) is taken.
*/
static cdefn *dlookup(name, len, val)
char *name;
int len;
char *val;
{
int h, i, nlen, vlen;
unsigned int t;
cdefn *d, **p;
if (len < 0)
len = strlen(name);
if (len == 0)
return NULL;
for (t = i = 0; i < len; i++)
t = 37 * t + (name[i] & 0xFF); /* calc hash value */
h = t % HTBINS; /* calc bin number */
p = &cbin[h]; /* get head of list */
while ((d = *p) != NULL) {
if (d->nlen == len && strncmp(name, d->s, len) == 0) {
/*
* We found a match in the table.
*/
if (val == NULL) { /* if $undef */
*p = d->next; /* delete from table */
free((char *)d);
return NULL;
}
if (val != name && strcmp(val, d->s + d->nlen) != 0)
pfatal("value redefined", name);
return d; /* return pointer to entry */
}
p = &d->next;
}
/*
* No match. Install a definition if that is what is wanted.
*/
if (val == name || val == NULL) /* if was reference or $undef */
return NULL;
nlen = strlen(name);
vlen = strlen(val);
d = (cdefn *)alloc(sizeof(*d) - sizeof(d->s) + nlen + vlen + 1);
d->nlen = nlen;
d->vlen = vlen;
d->inuse = 0;
strcpy(d->s, name);
strcpy(d->s + nlen, val);
d->prev = NULL;
d->next = cbin[h];
if (d->next != NULL)
d->next->prev = d;
cbin[h] = d;
return d;
}
|