update dev300-m58
[ooovba.git] / soltools / cpp / _cpp.c
blobfc60eb3d27f2227a43f440d717ffcb332ac7fdae
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <time.h>
5 #include <stdarg.h>
6 #include "cpp.h"
8 #define OUTS 16384
9 char outbuf[OUTS];
10 char *outptr = outbuf;
11 Source *cursource;
12 int nerrs;
13 struct token nltoken = {NL, 0, 0, 1, (uchar *) "\n", 0};
14 char *curtime;
15 int incdepth;
16 int ifdepth;
17 int ifsatisfied[NIF];
18 int skipping;
20 char rcsid[] = "$Version 1.2 $ $Revision: 1.5 $ $Date: 2006-06-20 05:05:46 $";
22 int
23 #ifdef _WIN32
24 __cdecl
25 #endif // _WIN32
26 main(int argc, char **argv)
29 Tokenrow tr;
30 time_t t;
31 char ebuf[BUFSIZ];
33 fprintf(stderr,"%s","cpp version 17.8.1999\n");
35 setbuf(stderr, ebuf);
36 t = time(NULL);
37 curtime = ctime(&t);
38 maketokenrow(3, &tr);
39 expandlex();
40 setup(argc, argv);
41 fixlex();
42 if (!Pflag)
43 genline();
44 process(&tr);
45 flushout();
46 fflush(stderr);
47 exit(nerrs > 0);
50 void
51 process(Tokenrow * trp)
53 int anymacros = 0;
55 for (;;)
57 if (trp->tp >= trp->lp)
59 trp->tp = trp->lp = trp->bp;
60 outptr = outbuf;
61 anymacros |= gettokens(trp, 1);
62 trp->tp = trp->bp;
64 if (trp->tp->type == END)
66 if (--incdepth >= 0)
68 if (cursource->ifdepth)
69 error(ERROR,
70 "Unterminated conditional in #include");
71 unsetsource();
72 cursource->line += cursource->lineinc;
73 trp->tp = trp->lp;
74 if (!Pflag)
75 genline();
76 continue;
78 if (ifdepth)
79 error(ERROR, "Unterminated #if/#ifdef/#ifndef");
80 break;
82 if (trp->tp->type == SHARP)
84 trp->tp += 1;
85 control(trp);
87 else
88 if (!skipping && anymacros)
89 expandrow(trp, NULL);
90 if (skipping)
91 setempty(trp);
92 puttokens(trp);
93 anymacros = 0;
94 cursource->line += cursource->lineinc;
95 if (cursource->lineinc > 1)
97 if (!Pflag)
98 genline();
103 void
104 control(Tokenrow * trp)
106 Nlist *np;
107 Token *tp;
109 tp = trp->tp;
110 if (tp->type != NAME)
112 if (tp->type == NUMBER)
113 goto kline;
114 if (tp->type != NL)
115 error(ERROR, "Unidentifiable control line");
116 return; /* else empty line */
118 if ((np = lookup(tp, 0)) == NULL || ((np->flag & ISKW) == 0 && !skipping))
120 error(WARNING, "Unknown preprocessor control %t", tp);
121 return;
123 if (skipping)
125 switch (np->val)
127 case KENDIF:
128 if (--ifdepth < skipping)
129 skipping = 0;
130 --cursource->ifdepth;
131 setempty(trp);
132 return;
134 case KIFDEF:
135 case KIFNDEF:
136 case KIF:
137 if (++ifdepth >= NIF)
138 error(FATAL, "#if too deeply nested");
139 ++cursource->ifdepth;
140 return;
142 case KELIF:
143 case KELSE:
144 if (ifdepth <= skipping)
145 break;
146 return;
148 default:
149 return;
152 switch (np->val)
154 case KDEFINE:
155 dodefine(trp);
156 break;
158 case KUNDEF:
159 tp += 1;
160 if (tp->type != NAME || trp->lp - trp->bp != 4)
162 error(ERROR, "Syntax error in #undef");
163 break;
165 if ((np = lookup(tp, 0)) != NULL)
167 np->flag &= ~ISDEFINED;
169 if (Mflag)
171 if (np->ap)
172 error(INFO, "Macro deletion of %s(%r)", np->name, np->ap);
173 else
174 error(INFO, "Macro deletion of %s", np->name);
177 break;
179 case KPRAGMA:
180 case KIDENT:
181 for (tp = trp->tp - 1; ((tp->type != NL) && (tp < trp->lp)); tp++)
182 tp->type = UNCLASS;
183 return;
185 case KIFDEF:
186 case KIFNDEF:
187 case KIF:
188 if (++ifdepth >= NIF)
189 error(FATAL, "#if too deeply nested");
190 ++cursource->ifdepth;
191 ifsatisfied[ifdepth] = 0;
192 if (eval(trp, np->val))
193 ifsatisfied[ifdepth] = 1;
194 else
195 skipping = ifdepth;
196 break;
198 case KELIF:
199 if (ifdepth == 0)
201 error(ERROR, "#elif with no #if");
202 return;
204 if (ifsatisfied[ifdepth] == 2)
205 error(ERROR, "#elif after #else");
206 if (eval(trp, np->val))
208 if (ifsatisfied[ifdepth])
209 skipping = ifdepth;
210 else
212 skipping = 0;
213 ifsatisfied[ifdepth] = 1;
216 else
217 skipping = ifdepth;
218 break;
220 case KELSE:
221 if (ifdepth == 0 || cursource->ifdepth == 0)
223 error(ERROR, "#else with no #if");
224 return;
226 if (ifsatisfied[ifdepth] == 2)
227 error(ERROR, "#else after #else");
228 if (trp->lp - trp->bp != 3)
229 error(ERROR, "Syntax error in #else");
230 skipping = ifsatisfied[ifdepth] ? ifdepth : 0;
231 ifsatisfied[ifdepth] = 2;
232 break;
234 case KENDIF:
235 if (ifdepth == 0 || cursource->ifdepth == 0)
237 error(ERROR, "#endif with no #if");
238 return;
240 --ifdepth;
241 --cursource->ifdepth;
242 if (trp->lp - trp->bp != 3)
243 error(WARNING, "Syntax error in #endif");
244 break;
246 case KERROR:
247 trp->tp = tp + 1;
248 error(WARNING, "#error directive: %r", trp);
249 break;
251 case KLINE:
252 trp->tp = tp + 1;
253 expandrow(trp, "<line>");
254 tp = trp->bp + 2;
255 kline:
256 if (tp + 1 >= trp->lp || tp->type != NUMBER || tp + 3 < trp->lp
257 || (tp + 3 == trp->lp
258 && ((tp + 1)->type != STRING || *(tp + 1)->t == 'L')))
260 error(ERROR, "Syntax error in #line");
261 return;
263 cursource->line = atol((char *) tp->t) - 1;
264 if (cursource->line < 0 || cursource->line >= 32768)
265 error(WARNING, "#line specifies number out of range");
266 tp = tp + 1;
267 if (tp + 1 < trp->lp)
268 cursource->filename = (char *) newstring(tp->t + 1, tp->len - 2, 0);
269 return;
271 case KDEFINED:
272 error(ERROR, "Bad syntax for control line");
273 break;
275 case KIMPORT:
276 doinclude(trp, -1, 1);
277 trp->lp = trp->bp;
278 return;
280 case KINCLUDE:
281 doinclude(trp, -1, 0);
282 trp->lp = trp->bp;
283 return;
285 case KINCLUDENEXT:
286 doinclude(trp, cursource->pathdepth, 0);
287 trp->lp = trp->bp;
288 return;
290 case KEVAL:
291 eval(trp, np->val);
292 break;
294 default:
295 error(ERROR, "Preprocessor control `%t' not yet implemented", tp);
296 break;
298 setempty(trp);
299 return;
302 void *
303 domalloc(int size)
305 void *p = malloc(size);
307 if (p == NULL)
308 error(FATAL, "Out of memory from malloc");
309 return p;
312 void
313 dofree(void *p)
315 free(p);
318 void
319 error(enum errtype type, char *string,...)
321 va_list ap;
322 char c, *cp, *ep;
323 Token *tp;
324 Tokenrow *trp;
325 Source *s;
326 int i;
328 fprintf(stderr, "cpp: ");
329 for (s = cursource; s; s = s->next)
330 if (*s->filename)
331 fprintf(stderr, "%s:%d ", s->filename, s->line);
332 va_start(ap, string);
333 for (ep = string; *ep; ep++)
335 if (*ep == '%')
337 switch (*++ep)
340 case 'c':
341 c = (char) va_arg(ap, int);
342 fprintf(stderr, "%c", c);
343 break;
345 case 's':
346 cp = va_arg(ap, char *);
347 fprintf(stderr, "%s", cp);
348 break;
350 case 'd':
351 i = va_arg(ap, int);
352 fprintf(stderr, "%d", i);
353 break;
355 case 't':
356 tp = va_arg(ap, Token *);
357 fprintf(stderr, "%.*s", (int)tp->len, tp->t);
358 break;
360 case 'r':
361 trp = va_arg(ap, Tokenrow *);
362 for (tp = trp->tp; tp < trp->lp && tp->type != NL; tp++)
364 if (tp > trp->tp && tp->wslen)
365 fputc(' ', stderr);
366 fprintf(stderr, "%.*s", (int)tp->len, tp->t);
368 break;
370 default:
371 fputc(*ep, stderr);
372 break;
375 else
376 fputc(*ep, stderr);
378 va_end(ap);
379 fputc('\n', stderr);
380 if (type == FATAL)
381 exit(1);
382 if (type != WARNING)
383 nerrs = 1;
384 fflush(stderr);