|
|
1.1 root 1: /*
2: * C preprocessor.
3: * This version of cpp runs as a module of cc0.
4: */
5: #ifdef vax
6: #include "INC$LIB:cc0.h"
7: #else
8: #include "cc0.h"
9: #endif
10:
11: /*
12: * Low-level functions and macros.
13: *
14: * Stash characters into the 'dbuf'
15: * where most things are being accumulated.
16: */
17:
18: dputc(c) int c;
19: {
20: if (dputp < dpshp)
21: *dputp++ = c;
22: else
23: dfull("dput");
24: }
25: dpshc(c) int c;
26: {
27: if (--dpshp > dputp)
28: *dpshp = c;
29: else
30: dfull("dpsh");
31: }
32: dspush(t, p) int t; char *p;
33: {
34: if (--dstackp > dlistp) {
35: dstackp->ds_type = t;
36: dstackp->ds_char = 0;
37: dstackp->ds_ptr = p;
38: } else
39: dfull("dspush");
40: }
41: dspushc(t, c) int t, c;
42: {
43: if (--dstackp > dlistp) {
44: dstackp->ds_type = t;
45: dstackp->ds_char = c;
46: dstackp->ds_ptr = 0;
47: } else
48: dfull("dspushc");
49: }
50: dslist(t, p) int t; char *p;
51: {
52: if (dlistp < dstackp) {
53: dlistp->ds_type = t;
54: dlistp->ds_char = 0;
55: (dlistp++)->ds_ptr = p;
56: } else
57: dfull("dslist");
58: }
59: dslistc(t, c) int t, c;
60: {
61: if (dlistp < dstackp) {
62: dlistp->ds_type = t;
63: dlistp->ds_char = c;
64: (dlistp++)->ds_ptr = 0;
65: } else
66: dfull("dslistc");
67: }
68:
69: dfull(where) char *where;
70: {
71: cfatal("macro expansion buffer overflow in %s", where);
72: }
73:
74: dputs(p) register char *p;
75: {
76: register int c;
77:
78: while (c = *p++) dputc(c);
79: }
80:
81: dpshs(p) register char *p;
82: {
83: register int c;
84:
85: while (c = *p++) dpshc(c);
86: }
87:
88: dputstr(d) register int d;
89: {
90: register int c;
91: instring = c = d;
92: for (;;) {
93: dputc(c);
94: if ((c = get()) < 0 || c == '\n' || c == d)
95: break;
96: if (c == '\\') {
97: dputc(c);
98: if ((c = get()) < 0 || c == '\n') {
99: dputc(' ');
100: break;
101: }
102: }
103: }
104: dputc(d);
105: instring = 0;
106: return c;
107: }
108:
109: dpshstr(d) register int d;
110: {
111: register int c;
112: instring = c = d;
113: for (;;) {
114: dpshc(c);
115: if ((c = get()) < 0 || c == '\n' || c == d)
116: break;
117: if (c == '\\') {
118: dpshc(c);
119: if ((c = get()) < 0 || c == '\n') {
120: dpshc(' ');
121: break;
122: }
123: }
124: }
125: dpshc(d);
126: instring = 0;
127: return c;
128: }
129:
130: dputnum(c) register int c;
131: {
132: ++incpp;
133: if (c == '.') {
134: unget(c = get());
135: if (c < 0 || ct[c] != CON) {
136: dputc('.');
137: --incpp;
138: return;
139: }
140: getnum(get(), 1);
141: } else
142: getnum(c, 0);
143: dputs(id);
144: --incpp;
145: }
146:
147: dpshnum(c) register int c;
148: {
149: ++incpp;
150: if (c == '.') {
151: unget(c = get());
152: if (c < 0 || ct[c] != CON) {
153: dpshc('.');
154: --incpp;
155: return;
156: }
157: getnum(get(), 1);
158: } else
159: getnum(c, 0);
160: dpshs(id);
161: --incpp;
162: }
163:
164: /* Allocate a new hide set element */
165: HIDESET *newhide()
166: {
167: register HIDESET *hp;
168: register HIDEMEM *mp;
169: register int n;
170: if ((hp = hidefree) == NULL) {
171: n = 8;
172: hidefree = hp = new(n*sizeof(HIDESET));
173: while (--n > 0) {
174: hp->h_next_set = hp+1;
175: hp->h_this_set = NULL;
176: hp += 1;
177: }
178: hp->h_next_set = NULL;
179: hp->h_this_set = NULL;
180: hp = hidefree;
181: }
182: if ((mp = hp->h_this_set) == NULL) {
183: hidefree = hp->h_next_set;
184: hp->h_next_set = NULL;
185: return hp;
186: }
187: hp->h_this_set = mp->h_next_mem;
188: mp->h_this_mem = NULL;
189: mp->h_next_mem = NULL;
190: return mp;
191: }
192:
193: /* return the HIDESET corresponding to index x */
194: HIDEMEM *hideget(x) register int x;
195: {
196: register HIDESET *hp;
197: if ((x -= SET0) == 0)
198: return NULL;
199: for (hp = hidesets; --x > 0; hp = hp->h_next_set)
200: if (hp == NULL)
201: cbotch("impossible hide set");
202: return hp->h_this_set;
203: }
204:
205: /* Compare hidesets for equality */
206: hideeq(mp1, mp2) register HIDEMEM *mp1, *mp2;
207: {
208: for (;;) {
209: if (mp1 == NULL)
210: if (mp2 == NULL)
211: return 1;
212: else
213: return 0;
214: else if (mp2 == NULL)
215: return 0;
216: else if (mp1->h_this_mem != mp2->h_this_mem)
217: return 0;
218: mp1 = mp1->h_next_mem;
219: mp2 = mp2->h_next_mem;
220: }
221: }
222:
223: /* Enter a hideset into the universe and return its index */
224: hideent(mp) HIDEMEM *mp;
225: {
226: register HIDESET **hpp, *hp;
227: register int x;
228: HIDESET *tp;
229: tp = hp = newhide();
230: hp->h_this_set = mp;
231: x = SET0;
232: hpp = &hidesets;
233: while ((hp = *hpp) != NULL) {
234: ++x;
235: if (hideeq(hp->h_this_set, mp)) {
236: hp = tp;
237: hp->h_next_set = hidefree;
238: hidefree = hp;
239: return x;
240: }
241: hpp = &hp->h_next_set;
242: }
243: ++x;
244: *hpp = tp;
245: return x;
246: }
247:
248: HIDEMEM *hideapp(mp1, tp) HIDEMEM *mp1; TOK *tp;
249: {
250: register HIDEMEM **mp;
251: for (mp = &mp1; *mp != NULL; mp = &((*mp)->h_next_mem))
252: ;
253: *mp = newhide();
254: (*mp)->h_this_mem = tp;
255: return mp1;
256: }
257:
258: /* Return true if the current id[] is hidden */
259: hidden()
260: {
261: register HIDEMEM *mp;
262:
263: for (mp = hideget(idhide); mp != NULL; mp = mp->h_next_mem)
264: if (idp == mp->h_this_mem)
265: return 1;
266: return 0;
267: }
268:
269: /* Return the intersection of s1 and s2 */
270: hideint(s1, s2) int s1, s2;
271: {
272: register HIDEMEM *mp1, *mp2, *mp3;
273: if (s1 == SET0)
274: return s1;
275: if (s2 == SET0)
276: return s2;
277: if (s1 == -1) {
278: return s2;
279: }
280: if (s2 == -1)
281: return s1;
282: if (s1 == s2)
283: return s1;
284: mp1 = hideget(s1);
285: mp2 = hideget(s2);
286: mp3 = NULL;
287: while (mp1 != NULL && mp2 != NULL) {
288: if (mp1->h_this_mem == mp2->h_this_mem) {
289: mp3 = hideapp(mp3, mp1->h_this_mem);
290: mp1 = mp1->h_next_mem;
291: mp2 = mp2->h_next_mem;
292: continue;
293: }
294: if (mp1->h_this_mem < mp2->h_this_mem) {
295: mp1 = mp1->h_next_mem;
296: continue;
297: }
298: if (mp2->h_this_mem < mp1->h_this_mem) {
299: mp2 = mp2->h_next_mem;
300: continue;
301: }
302: }
303: return hideent(mp3);
304: }
305:
306: /* Return the union of s1 and s2 */
307: hideaug(s1, s2) int s1, s2;
308: {
309: register HIDEMEM *mp1, *mp2, *mp3;
310: if (s1 == SET0)
311: return s2;
312: if (s2 == SET0)
313: return s1;
314: mp1 = hideget(s1);
315: mp2 = hideget(s2);
316: mp3 = NULL;
317: while (mp1 != NULL || mp2 != NULL) {
318: if (mp1 == NULL) {
319: mp3 = hideapp(mp3, mp2->h_this_mem);
320: mp2 = mp2->h_next_mem;
321: continue;
322: }
323: if (mp2 == NULL) {
324: mp3 = hideapp(mp3, mp1->h_this_mem);
325: mp1 = mp1->h_next_mem;
326: continue;
327: }
328: if (mp1->h_this_mem == mp2->h_this_mem) {
329: mp3 = hideapp(mp3, mp1->h_this_mem);
330: mp1 = mp1->h_next_mem;
331: mp2 = mp2->h_next_mem;
332: continue;
333: }
334: if (mp1->h_this_mem < mp2->h_this_mem) {
335: mp3 = hideapp(mp3, mp1->h_this_mem);
336: mp1 = mp1->h_next_mem;
337: continue;
338: }
339: if (mp2->h_this_mem < mp1->h_this_mem) {
340: mp3 = hideapp(mp3, mp2->h_this_mem);
341: mp2 = mp2->h_next_mem;
342: continue;
343: }
344: }
345: return hideent(mp3);
346: }
347:
348: /* Return the hide set for tp */
349: hideset(tp) register TOK *tp;
350: {
351: /* Determine if we are at source file level */
352: {
353: register DSTACK *dsp;
354: for (dsp = dstackp; dsp < dstack+NDLEV; dsp += 1)
355: if (dsp->ds_type == DS_NAME)
356: goto part2;
357: }
358: /* Release the previous hide set universe */
359: if (hidesets != NULL) {
360: register HIDESET **pp, *hp;
361: for (pp = &hidesets; (hp = *pp) != NULL; pp = &hp->h_next_set)
362: ;
363: *pp = hidefree;
364: hidefree = hidesets;
365: hidesets = NULL;
366: }
367: part2: return hideent(hideapp(NULL, tp));
368: }
369:
370: /*
371: * The following four functions
372: * are called from cc0 main().
373: * Install a new symbol with given value.
374: * -Dname=value
375: */
376: cppd(name, value) char *name, *value;
377: {
378: register CPPSYM *sp;
379:
380: setid(name);
381: sp = newcpp(-1, value, strlen(value)+1);
382: sp->s_sp = idp->t_sym;
383: idp->t_sym = sp;
384: }
385:
386: /*
387: * Undefine a cpp symbol.
388: * -Uname
389: */
390: cppu(name) char *name;
391: {
392: setid(name);
393: undefine();
394: }
395:
396: /*
397: * Add a directory to the list to be searched for include files.
398: * -Ipath
399: */
400: cppi(path) char *path;
401: {
402: if (ndirs >= NDIRS)
403: cfatal("too many directories in include list");
404: incdirs[ndirs++] = path;
405: }
406:
407: /*
408: * cpp loop for generating
409: * preprocessed text files.
410: * isvariant(VCPP) != 0
411: */
412: cppwork()
413: {
414: register int c, d;
415: register char *p;
416: register int myline;
417: register char *myfile;
418: myfile = file;
419: myline = line;
420: while ((c = get()) >= 0) {
421: switch (ct[c]) {
422: case ID:
423: if (expand(c))
424: continue;
425: for (p = id; c = *p++; putc(c, ofp));
426: continue;
427: case QUOTE:
428: case STRING:
429: putc(c, ofp);
430: instring = c;
431: while ((d = get()) >= 0 && d != c) {
432: putc(d, ofp);
433: if (d == '\\')
434: putc(get(), ofp);
435: }
436: instring = 0;
437: if (d < 0)
438: cerror("EOF in string");
439: else
440: putc(d, ofp);
441: continue;
442: default:
443: putc(c, ofp);
444: if (c != '\n')
445: continue;
446: if (myfile != file) {
447: myfile = file;
448: myline = line;
449: continue;
450: }
451: if (++myline == line)
452: continue;
453: myline = line;
454: if (notvariant(VCPPE))
455: fprintf(ofp, "#line %d\n", line);
456: continue;
457: }
458: }
459: }
460:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.