Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifdef _MSC_VER
21 : # define _POSIX_
22 : #endif
23 : #include <stdio.h>
24 : #include <stdlib.h>
25 : #include <string.h>
26 : #if defined(__IBMC__) || defined(__EMX__) || defined(_MSC_VER)
27 : # ifndef PATH_MAX
28 : # define PATH_MAX _MAX_PATH
29 : # endif
30 : #endif
31 : #include <limits.h>
32 :
33 : #include "cpp.h"
34 :
35 : #define NCONCAT 16384
36 :
37 : /*
38 : * do a macro definition. tp points to the name being defined in the line
39 : */
40 : void
41 6567 : dodefine(Tokenrow * trp)
42 : {
43 : Token *tp;
44 : Nlist *np;
45 : Source *s;
46 : Tokenrow *def, *args;
47 : static uchar location[(PATH_MAX + 8) * NINC], *cp;
48 :
49 6567 : tp = trp->tp + 1;
50 6567 : if (tp >= trp->lp || tp->type != NAME)
51 : {
52 0 : error(ERROR, "#defined token is not a name");
53 0 : return;
54 : }
55 6567 : np = lookup(tp, 1);
56 6567 : if (np->flag & ISUNCHANGE)
57 : {
58 0 : error(ERROR, "#defined token %t can't be redefined", tp);
59 0 : return;
60 : }
61 : /* collect arguments */
62 6567 : tp += 1;
63 6567 : args = NULL;
64 6567 : if (tp < trp->lp && tp->type == LP && tp->wslen == 0)
65 : {
66 5035 : tp += 1;
67 5035 : args = new(Tokenrow);
68 5035 : maketokenrow(2, args);
69 5035 : if (tp->type != RP)
70 : {
71 : /* macro with args */
72 5035 : size_t narg = 0;
73 5035 : int err = 0;
74 :
75 : for (;;)
76 : {
77 : Token *atp;
78 :
79 14204 : if (tp->type != NAME)
80 : {
81 0 : err++;
82 0 : break;
83 : }
84 14204 : if (narg >= args->max)
85 3127 : growtokenrow(args);
86 37418 : for (atp = args->bp; atp < args->lp; atp++)
87 23214 : if (atp->len == tp->len
88 14151 : && strncmp((char *) atp->t, (char *) tp->t, tp->len) == 0)
89 0 : error(ERROR, "Duplicate macro argument");
90 14204 : *args->lp++ = *tp;
91 14204 : narg++;
92 14204 : tp += 1;
93 14204 : if (tp->type == RP)
94 5035 : break;
95 9169 : if (tp->type != COMMA)
96 : {
97 0 : err++;
98 0 : break;
99 : }
100 9169 : tp += 1;
101 9169 : }
102 5035 : if (err)
103 : {
104 0 : error(ERROR, "Syntax error in macro parameters");
105 0 : return;
106 : }
107 : }
108 5035 : tp += 1;
109 : }
110 6567 : trp->tp = tp;
111 6567 : if (((trp->lp) - 1)->type == NL)
112 6567 : trp->lp -= 1;
113 6567 : def = normtokenrow(trp);
114 6567 : if (np->flag & ISDEFINED)
115 : {
116 0 : if (comparetokens(def, np->vp)
117 0 : || (np->ap == NULL) != (args == NULL)
118 0 : || (np->ap && comparetokens(args, np->ap)))
119 : {
120 0 : if ( np->loc )
121 0 : error(ERROR,
122 : "Macro redefinition of %t (already defined at %s)",
123 0 : trp->bp + 2, np->loc);
124 : else
125 0 : error(ERROR,
126 : "Macro redefinition of %t (already defined at %s)",
127 0 : trp->bp + 2, "commandline" );
128 : }
129 : }
130 6567 : if (args)
131 : {
132 : Tokenrow *tap;
133 :
134 5035 : tap = normtokenrow(args);
135 5035 : dofree(args->bp);
136 5035 : args = tap;
137 : }
138 6567 : np->ap = args;
139 6567 : np->vp = def;
140 6567 : np->flag |= ISDEFINED;
141 :
142 : /* build location string of macro definition */
143 20496 : for (cp = location, s = cursource; s; s = s->next)
144 13929 : if (*s->filename)
145 : {
146 13929 : if (cp != location)
147 7362 : *cp++ = ' ';
148 13929 : sprintf((char *)cp, "%s:%d", s->filename, s->line);
149 13929 : cp += strlen((char *)cp);
150 : }
151 :
152 6567 : np->loc = newstring(location, strlen((char *)location), 0);
153 :
154 6567 : if (Mflag)
155 : {
156 0 : if (np->ap)
157 0 : error(INFO, "Macro definition of %s(%r) [%r]", np->name, np->ap, np->vp);
158 : else
159 0 : error(INFO, "Macro definition of %s [%r]", np->name, np->vp);
160 : }
161 : }
162 :
163 : /*
164 : * Definition received via -D or -U
165 : */
166 : void
167 1602 : doadefine(Tokenrow * trp, int type)
168 : {
169 : Nlist *np;
170 : static uchar onestr[2] = "1";
171 : static Token onetoken[1] = {{NUMBER, 0, 0, 1, onestr, 0}};
172 : static Tokenrow onetr = {onetoken, onetoken, onetoken + 1, 1};
173 :
174 1602 : trp->tp = trp->bp;
175 1602 : if (type == 'U')
176 : {
177 0 : if (trp->lp - trp->tp != 2 || trp->tp->type != NAME)
178 : goto syntax;
179 0 : if ((np = lookup(trp->tp, 0)) == NULL)
180 0 : return;
181 0 : np->flag &= ~ISDEFINED;
182 0 : return;
183 : }
184 :
185 1602 : if (type == 'A')
186 : {
187 0 : if (trp->tp >= trp->lp || trp->tp->type != NAME)
188 : goto syntax;
189 0 : trp->tp->type = ARCHITECTURE;
190 0 : np = lookup(trp->tp, 1);
191 0 : np->flag |= ISARCHITECTURE;
192 0 : trp->tp += 1;
193 0 : if (trp->tp >= trp->lp || trp->tp->type == END)
194 : {
195 0 : np->vp = &onetr;
196 0 : return;
197 : }
198 : else
199 0 : error(FATAL, "Illegal -A argument %r", trp);
200 : }
201 :
202 1602 : if (trp->tp >= trp->lp || trp->tp->type != NAME)
203 : goto syntax;
204 1602 : np = lookup(trp->tp, 1);
205 1602 : np->flag |= ISDEFINED;
206 1602 : trp->tp += 1;
207 1602 : if (trp->tp >= trp->lp || trp->tp->type == END)
208 : {
209 1358 : np->vp = &onetr;
210 1358 : return;
211 : }
212 244 : if (trp->tp->type != ASGN)
213 0 : goto syntax;
214 244 : trp->tp += 1;
215 244 : if ((trp->lp - 1)->type == END)
216 244 : trp->lp -= 1;
217 244 : np->vp = normtokenrow(trp);
218 244 : return;
219 : syntax:
220 0 : error(FATAL, "Illegal -D or -U argument %r", trp);
221 : }
222 :
223 :
224 :
225 : /*
226 : * Do macro expansion in a row of tokens.
227 : * Flag is NULL if more input can be gathered.
228 : */
229 : void
230 202663 : expandrow(Tokenrow * trp, char *flag)
231 : {
232 : Token * tp;
233 : Nlist * np;
234 :
235 : MacroValidatorList validators;
236 202663 : mvl_init(&validators);
237 : /* Sets all token-identifiers to 0 because tokens may not be initialised (never use C!) */
238 202663 : tokenrow_zeroTokenIdentifiers(trp);
239 :
240 202663 : if (flag)
241 194187 : setsource(flag, -1, -1, "", 0);
242 1306490 : for (tp = trp->tp; tp < trp->lp;)
243 : {
244 901164 : mvl_check(&validators, tp);
245 :
246 901164 : if (tp->type != NAME
247 512698 : || quicklook(tp->t[0], tp->len > 1 ? tp->t[1] : 0) == 0
248 207486 : || (np = lookup(tp, 0)) == NULL
249 46626 : || (np->flag & (ISDEFINED | ISMAC)) == 0
250 46626 : || (np->flag & ISACTIVE) != 0)
251 : {
252 854538 : tp++;
253 854538 : continue;
254 : }
255 46626 : trp->tp = tp;
256 46626 : if (np->val == KDEFINED)
257 : {
258 590 : tp->type = DEFINED;
259 590 : if ((tp + 1) < trp->lp && (tp + 1)->type == NAME)
260 510 : (tp + 1)->type = NAME1;
261 : else
262 80 : if ((tp + 3) < trp->lp && (tp + 1)->type == LP
263 80 : && (tp + 2)->type == NAME && (tp + 3)->type == RP)
264 80 : (tp + 2)->type = NAME1;
265 : else
266 0 : error(ERROR, "Incorrect syntax for `defined'");
267 590 : tp++;
268 590 : continue;
269 : }
270 : else
271 46036 : if (np->val == KMACHINE)
272 : {
273 0 : if (((tp - 1) >= trp->bp) && ((tp - 1)->type == SHARP))
274 : {
275 0 : tp->type = ARCHITECTURE;
276 0 : if ((tp + 1) < trp->lp && (tp + 1)->type == NAME)
277 0 : (tp + 1)->type = NAME2;
278 : else
279 0 : if ((tp + 3) < trp->lp && (tp + 1)->type == LP
280 0 : && (tp + 2)->type == NAME && (tp + 3)->type == RP)
281 0 : (tp + 2)->type = NAME2;
282 : else
283 0 : error(ERROR, "Incorrect syntax for `#machine'");
284 : }
285 0 : tp++;
286 0 : continue;
287 : }
288 :
289 46036 : if (np->flag & ISMAC)
290 0 : builtin(trp, np->val);
291 : else
292 46036 : expand(trp, np, &validators);
293 46036 : tp = trp->tp;
294 : } // end for
295 202663 : if (flag)
296 194187 : unsetsource();
297 :
298 202663 : mvl_destruct(&validators);
299 202663 : }
300 :
301 : /*
302 : * Expand the macro whose name is np, at token trp->tp, in the tokenrow.
303 : * Return trp->tp at the first token next to be expanded
304 : * (ordinarily the beginning of the expansion)
305 : * I.e.: the same position as before!
306 : * Only one expansion is performed, then we return to the expandrow()
307 : * loop and start at same position.
308 : */
309 : void
310 46036 : expand(Tokenrow * trp, Nlist * np, MacroValidatorList * pValidators)
311 : {
312 : Tokenrow ntr;
313 : int ntokc, narg;
314 : Tokenrow *atr[NARG + 1];
315 :
316 46036 : if (Mflag == 2)
317 : {
318 0 : if (np->ap)
319 0 : error(INFO, "Macro expansion of %t with %s(%r)", trp->tp, np->name, np->ap);
320 : else
321 0 : error(INFO, "Macro expansion of %t with %s", trp->tp, np->name);
322 : }
323 :
324 46036 : copytokenrow(&ntr, np->vp); /* copy macro value */
325 46036 : if (np->ap == NULL) /* parameterless */
326 1655 : ntokc = 1;
327 : else
328 : {
329 : int i;
330 :
331 44381 : ntokc = gatherargs(trp, atr, &narg);
332 44381 : if (narg < 0)
333 : { /* not actually a call (no '(') */
334 0 : trp->tp++;
335 0 : return;
336 : }
337 44381 : if (narg != rowlen(np->ap))
338 : {
339 0 : error(ERROR, "Disagreement in number of macro arguments");
340 0 : trp->tp += ntokc;
341 0 : return;
342 : }
343 :
344 : /** If gatherargs passed a macro validating token, this token
345 : must become valid here.
346 : trp->tp+0 was checked in expandrow(), so we dont need to do it
347 : again here:
348 : */
349 422091 : for (i = 1; i < ntokc; i++)
350 : {
351 377710 : mvl_check(pValidators,trp->tp+i);
352 : }
353 :
354 44381 : substargs(np, &ntr, atr); /* put args into replacement */
355 129556 : for (i = 0; i < narg; i++)
356 : {
357 85175 : dofree(atr[i]->bp);
358 85175 : dofree(atr[i]);
359 : }
360 : }
361 :
362 46036 : doconcat(&ntr); /* execute ## operators */
363 46036 : ntr.tp = ntr.bp;
364 46036 : makespace(&ntr, trp->tp);
365 :
366 46036 : tokenrow_zeroTokenIdentifiers(&ntr);
367 46036 : insertrow(trp, ntokc, &ntr);
368 :
369 : /* add validator for just invalidated macro:
370 : */
371 46036 : np->flag |= ISACTIVE;
372 46036 : if (trp->tp != trp->lp)
373 : { /* tp is a valid pointer: */
374 28119 : mvl_add(pValidators,np,trp->tp);
375 : }
376 : else
377 : { /* tp is == lp, therefore does not point to valid memory: */
378 17917 : mvl_add(pValidators,np,0);
379 : }
380 : /* reset trp->tp to original position:
381 : */
382 46036 : trp->tp -= ntr.lp - ntr.bp; /* so the result will be tested for macros from the same position again */
383 :
384 46036 : dofree(ntr.bp);
385 :
386 46036 : return;
387 : }
388 :
389 : /*
390 : * Gather an arglist, starting in trp with tp pointing at the macro name.
391 : * Return total number of tokens passed, stash number of args found.
392 : * trp->tp is not changed relative to the tokenrow.
393 : */
394 : int
395 44381 : gatherargs(Tokenrow * trp, Tokenrow ** atr, int *narg)
396 : {
397 44381 : int parens = 1;
398 44381 : int ntok = 0;
399 : Token *bp, *lp;
400 : Tokenrow ttr;
401 : int ntokp;
402 : int needspace;
403 :
404 44381 : *narg = -1; /* means that there is no macro
405 : * call */
406 : /* look for the ( */
407 : for (;;)
408 : {
409 44381 : trp->tp++;
410 44381 : ntok++;
411 44381 : if (trp->tp >= trp->lp)
412 : {
413 0 : gettokens(trp, 0);
414 0 : if ((trp->lp - 1)->type == END)
415 : {
416 0 : trp->lp -= 1;
417 0 : trp->tp -= ntok;
418 0 : return ntok;
419 : }
420 : }
421 44381 : if (trp->tp->type == LP)
422 44381 : break;
423 0 : if (trp->tp->type != NL)
424 0 : return ntok;
425 0 : }
426 44381 : *narg = 0;
427 44381 : ntok++;
428 44381 : ntokp = ntok;
429 44381 : trp->tp++;
430 : /* search for the terminating ), possibly extending the row */
431 44381 : needspace = 0;
432 422091 : while (parens > 0)
433 : {
434 333329 : if (trp->tp >= trp->lp)
435 0 : gettokens(trp, 0);
436 333329 : if (needspace)
437 : {
438 0 : needspace = 0;
439 : /* makespace(trp); [rh] */
440 : }
441 333329 : if (trp->tp->type == END)
442 : {
443 0 : trp->lp -= 1;
444 0 : trp->tp -= ntok;
445 0 : error(ERROR, "EOF in macro arglist");
446 0 : return ntok;
447 : }
448 333329 : if (trp->tp->type == NL)
449 : {
450 0 : trp->tp += 1;
451 0 : adjustrow(trp, -1);
452 0 : trp->tp -= 1;
453 : /* makespace(trp); [rh] */
454 0 : needspace = 1;
455 0 : continue;
456 : }
457 333329 : if (trp->tp->type == LP)
458 8948 : parens++;
459 : else
460 324381 : if (trp->tp->type == RP)
461 53329 : parens--;
462 333329 : trp->tp++;
463 333329 : ntok++;
464 : }
465 44381 : trp->tp -= ntok;
466 : /* Now trp->tp won't move underneath us */
467 44381 : lp = bp = trp->tp + ntokp;
468 377710 : for (; parens >= 0; lp++)
469 : {
470 333329 : if (lp->type == LP)
471 : {
472 8948 : parens++;
473 8948 : continue;
474 : }
475 324381 : if (lp->type == RP)
476 53329 : parens--;
477 324381 : if (lp->type == DSHARP)
478 0 : lp->type = DSHARP1; /* ## not special in arg */
479 324381 : if ((lp->type == COMMA && parens == 0) ||
480 44381 : ( parens < 0 && ((lp - 1)->type != LP)))
481 : {
482 85175 : if (*narg >= NARG - 1)
483 0 : error(FATAL, "Sorry, too many macro arguments");
484 85175 : ttr.bp = ttr.tp = bp;
485 85175 : ttr.lp = lp;
486 85175 : atr[(*narg)++] = normtokenrow(&ttr);
487 85175 : bp = lp + 1;
488 : }
489 : }
490 44381 : return ntok;
491 : }
492 :
493 : /*
494 : * substitute the argument list into the replacement string
495 : * This would be simple except for ## and #
496 : */
497 : void
498 44381 : substargs(Nlist * np, Tokenrow * rtr, Tokenrow ** atr)
499 : {
500 : Tokenrow tatr;
501 : Token *tp;
502 : int ntok, argno;
503 :
504 955245 : for (rtr->tp = rtr->bp; rtr->tp < rtr->lp;)
505 : {
506 866483 : if (rtr->tp->type == SHARP)
507 : { /* string operator */
508 8956 : tp = rtr->tp;
509 8956 : rtr->tp += 1;
510 8956 : if ((argno = lookuparg(np, rtr->tp)) < 0)
511 : {
512 0 : error(ERROR, "# not followed by macro parameter");
513 0 : continue;
514 : }
515 8956 : ntok = 1 + (int)(rtr->tp - tp);
516 8956 : rtr->tp = tp;
517 8956 : insertrow(rtr, ntok, stringify(atr[argno]));
518 8956 : continue;
519 : }
520 857527 : if (rtr->tp->type == NAME
521 382342 : && (argno = lookuparg(np, rtr->tp)) >= 0)
522 : {
523 219426 : if (((rtr->tp + 1) < rtr->lp && (rtr->tp + 1)->type == DSHARP)
524 203269 : || (rtr->tp != rtr->bp && (rtr->tp - 1)->type == DSHARP))
525 : {
526 25782 : copytokenrow(&tatr, atr[argno]);
527 25782 : makespace(&tatr, rtr->tp);
528 25782 : insertrow(rtr, 1, &tatr);
529 25782 : dofree(tatr.bp);
530 : }
531 : else
532 : {
533 193644 : copytokenrow(&tatr, atr[argno]);
534 193644 : makespace(&tatr, rtr->tp);
535 193644 : expandrow(&tatr, "<macro>");
536 193644 : insertrow(rtr, 1, &tatr);
537 193644 : dofree(tatr.bp);
538 : }
539 219426 : continue;
540 : }
541 638101 : rtr->tp++;
542 : }
543 44381 : }
544 :
545 : /*
546 : * Evaluate the ## operators in a tokenrow
547 : */
548 : void
549 62821 : doconcat(Tokenrow * trp)
550 : {
551 : Token *ltp, *ntp;
552 : Tokenrow ntr;
553 : size_t len;
554 :
555 1030019 : for (trp->tp = trp->bp; trp->tp < trp->lp; trp->tp++)
556 : {
557 967198 : if (trp->tp->type == DSHARP1)
558 0 : trp->tp->type = DSHARP;
559 : else
560 967198 : if (trp->tp->type == DSHARP)
561 : {
562 : int i;
563 : char tt[NCONCAT];
564 :
565 16785 : ltp = trp->tp - 1;
566 16785 : ntp = trp->tp + 1;
567 :
568 16785 : if (ltp < trp->bp || ntp >= trp->lp)
569 : {
570 0 : error(ERROR, "## occurs at border of replacement");
571 0 : continue;
572 : }
573 :
574 16785 : ntp = ltp;
575 16785 : i = 1;
576 16785 : len = 0;
577 :
578 : do
579 : {
580 80901 : if (len + ntp->len + ntp->wslen > sizeof(tt))
581 : {
582 0 : error(ERROR, "## string concatination buffer overrun");
583 0 : break;
584 : }
585 :
586 80901 : if (ntp != trp->tp + 1)
587 : {
588 64116 : strncpy((char *) tt + len, (char *) ntp->t - ntp->wslen,
589 64116 : ntp->len + ntp->wslen);
590 64116 : len += ntp->len + ntp->wslen;
591 : }
592 : else // Leerzeichen um ## herum entfernen:
593 : {
594 16785 : strncpy((char *) tt + len, (char *) ntp->t, ntp->len);
595 16785 : len += ntp->len;
596 : }
597 :
598 80901 : ntp = trp->tp + i;
599 80901 : i++;
600 : }
601 80901 : while (ntp < trp->lp);
602 :
603 16785 : tt[len] = '\0';
604 16785 : setsource("<##>", -1, -1, tt, 0);
605 16785 : maketokenrow(3, &ntr);
606 16785 : gettokens(&ntr, 1);
607 16785 : unsetsource();
608 16785 : if (ntr.bp->type == UNCLASS)
609 0 : error(WARNING, "Bad token %r produced by ##", &ntr);
610 50355 : while ((ntr.lp-1)->len == 0 && ntr.lp != ntr.bp)
611 16785 : ntr.lp--;
612 :
613 16785 : doconcat(&ntr);
614 16785 : trp->tp = ltp;
615 16785 : makespace(&ntr, ltp);
616 16785 : insertrow(trp, (int)(ntp - ltp), &ntr);
617 16785 : dofree(ntr.bp);
618 16785 : trp->tp--;
619 : }
620 : }
621 62821 : }
622 :
623 : /*
624 : * tp is a potential parameter name of macro mac;
625 : * look it up in mac's arglist, and if found, return the
626 : * corresponding index in the argname array. Return -1 if not found.
627 : */
628 : int
629 391298 : lookuparg(Nlist * mac, Token * tp)
630 : {
631 : Token *ap;
632 :
633 391298 : if (tp->type != NAME || mac->ap == NULL)
634 0 : return -1;
635 796159 : for (ap = mac->ap->bp; ap < mac->ap->lp; ap++)
636 : {
637 633243 : if (ap->len == tp->len && strncmp((char *) ap->t, (char *) tp->t, ap->len) == 0)
638 228382 : return (int)(ap - mac->ap->bp);
639 : }
640 162916 : return -1;
641 : }
642 :
643 : /*
644 : * Return a quoted version of the tokenrow (from # arg)
645 : */
646 : #define STRLEN 512
647 : Tokenrow *
648 8956 : stringify(Tokenrow * vp)
649 : {
650 : static Token t = {STRING, 0, 0, 0, NULL, 0};
651 : static Tokenrow tr = {&t, &t, &t + 1, 1};
652 : Token *tp;
653 : uchar s[STRLEN];
654 8956 : uchar *sp = s, *cp;
655 : int i, instring;
656 :
657 8956 : *sp++ = '"';
658 50168 : for (tp = vp->bp; tp < vp->lp; tp++)
659 : {
660 41212 : instring = tp->type == STRING || tp->type == CCON;
661 41212 : if (sp + 2 * tp->len + tp->wslen >= &s[STRLEN - 10])
662 : {
663 0 : error(ERROR, "Stringified macro arg is too long");
664 0 : break;
665 : }
666 :
667 : // Change by np 31.10.2001, #93725 - begin
668 41212 : if ( tp->wslen > 0 )
669 0 : *sp++ = ' ';
670 : // change end.
671 :
672 166295 : for (i = 0, cp = tp->t; (unsigned int)i < tp->len; i++)
673 : {
674 125083 : if (instring && (*cp == '"' || *cp == '\\'))
675 0 : *sp++ = '\\';
676 125083 : *sp++ = *cp++;
677 : }
678 : }
679 8956 : *sp++ = '"';
680 8956 : *sp = '\0';
681 8956 : sp = s;
682 8956 : t.len = strlen((char *) sp);
683 8956 : t.t = newstring(sp, t.len, 0);
684 8956 : return &tr;
685 : }
686 :
687 : /*
688 : * expand a builtin name
689 : */
690 : void
691 0 : builtin(Tokenrow * trp, int biname)
692 : {
693 : char *op;
694 : Token *tp;
695 : Source *s;
696 :
697 0 : tp = trp->tp;
698 0 : trp->tp++;
699 : /* need to find the real source */
700 0 : s = cursource;
701 0 : while (s && s->fd == -1)
702 0 : s = s->next;
703 0 : if (s == NULL)
704 0 : s = cursource;
705 : /* most are strings */
706 0 : tp->type = STRING;
707 0 : if (tp->wslen)
708 : {
709 0 : *outptr++ = ' ';
710 0 : tp->wslen = 1;
711 : }
712 0 : op = outptr;
713 0 : *op++ = '"';
714 0 : switch (biname)
715 : {
716 :
717 : case KLINENO:
718 0 : tp->type = NUMBER;
719 0 : op = outnum(op - 1, s->line);
720 0 : break;
721 :
722 : case KFILE:
723 : {
724 0 : char *src = s->filename;
725 :
726 0 : while ((*op++ = *src++) != 0)
727 0 : if (src[-1] == '\\')
728 0 : *op++ = '\\';
729 0 : op--;
730 0 : break;
731 : }
732 :
733 : case KDATE:
734 0 : strncpy(op, curtime + 4, 7);
735 0 : strncpy(op + 7, curtime + 20, 4);
736 0 : op += 11;
737 0 : break;
738 :
739 : case KTIME:
740 0 : strncpy(op, curtime + 11, 8);
741 0 : op += 8;
742 0 : break;
743 :
744 : default:
745 0 : error(ERROR, "cpp botch: unknown internal macro");
746 0 : return;
747 : }
748 0 : if (tp->type == STRING)
749 0 : *op++ = '"';
750 0 : tp->t = (uchar *) outptr;
751 0 : tp->len = op - outptr;
752 0 : outptr = op;
753 : }
754 :
755 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|