Branch libreoffice-5-0-4
[LibreOffice.git] / soltools / cpp / _macro.c
blob6d48ef6cf05dfa01b9d1176ed9d336509c018e76
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
33 #include "cpp.h"
35 #define NCONCAT 16384
38 * do a macro definition. tp points to the name being defined in the line
40 void
41 dodefine(Tokenrow * trp)
43 Token *tp;
44 Nlist *np;
45 Source *s;
46 Tokenrow *def, *args;
47 static uchar location[(PATH_MAX + 8) * NINC], *cp;
49 tp = trp->tp + 1;
50 if (tp >= trp->lp || tp->type != NAME)
52 error(ERROR, "#defined token is not a name");
53 return;
55 np = lookup(tp, 1);
56 if (np->flag & ISUNCHANGE)
58 error(ERROR, "#defined token %t can't be redefined", tp);
59 return;
61 /* collect arguments */
62 tp += 1;
63 args = NULL;
64 if (tp < trp->lp && tp->type == LP && tp->wslen == 0)
66 tp += 1;
67 args = new(Tokenrow);
68 maketokenrow(2, args);
69 if (tp->type != RP)
71 /* macro with args */
72 size_t narg = 0;
73 int err = 0;
75 for (;;)
77 Token *atp;
79 if (tp->type != NAME)
81 err++;
82 break;
84 if (narg >= args->max)
85 growtokenrow(args);
86 for (atp = args->bp; atp < args->lp; atp++)
87 if (atp->len == tp->len
88 && strncmp((char *) atp->t, (char *) tp->t, tp->len) == 0)
89 error(ERROR, "Duplicate macro argument");
90 *args->lp++ = *tp;
91 narg++;
92 tp += 1;
93 if (tp->type == RP)
94 break;
95 if (tp->type != COMMA)
97 err++;
98 break;
100 tp += 1;
102 if (err)
104 error(ERROR, "Syntax error in macro parameters");
105 return;
108 tp += 1;
110 trp->tp = tp;
111 if (((trp->lp) - 1)->type == NL)
112 trp->lp -= 1;
113 def = normtokenrow(trp);
114 if (np->flag & ISDEFINED)
116 if (comparetokens(def, np->vp)
117 || (np->ap == NULL) != (args == NULL)
118 || (np->ap && comparetokens(args, np->ap)))
120 if ( np->loc )
121 error(ERROR,
122 "Macro redefinition of %t (already defined at %s)",
123 trp->bp + 2, np->loc);
124 else
125 error(ERROR,
126 "Macro redefinition of %t (already defined at %s)",
127 trp->bp + 2, "commandline" );
130 if (args)
132 Tokenrow *tap;
134 tap = normtokenrow(args);
135 dofree(args->bp);
136 dofree(args);
137 args = tap;
139 np->ap = args;
140 np->vp = def;
141 np->flag |= ISDEFINED;
143 /* build location string of macro definition */
144 for (cp = location, s = cursource; s; s = s->next)
145 if (*s->filename)
147 if (cp != location)
148 *cp++ = ' ';
149 sprintf((char *)cp, "%s:%d", s->filename, s->line);
150 cp += strlen((char *)cp);
153 np->loc = newstring(location, strlen((char *)location), 0);
155 if (Mflag)
157 if (np->ap)
158 error(INFO, "Macro definition of %s(%r) [%r]", np->name, np->ap, np->vp);
159 else
160 error(INFO, "Macro definition of %s [%r]", np->name, np->vp);
165 * Definition received via -D or -U
167 void
168 doadefine(Tokenrow * trp, int type)
170 Nlist *np;
171 static uchar onestr[2] = "1";
172 static Token onetoken[1] = {{NUMBER, 0, 0, 1, onestr, 0}};
173 static Tokenrow onetr = {onetoken, onetoken, onetoken + 1, 1};
175 trp->tp = trp->bp;
176 if (type == 'U')
178 if (trp->lp - trp->tp != 2 || trp->tp->type != NAME)
179 goto syntax;
180 if ((np = lookup(trp->tp, 0)) == NULL)
181 return;
182 np->flag &= ~ISDEFINED;
183 return;
186 if (type == 'A')
188 if (trp->tp >= trp->lp || trp->tp->type != NAME)
189 goto syntax;
190 trp->tp->type = ARCHITECTURE;
191 np = lookup(trp->tp, 1);
192 np->flag |= ISARCHITECTURE;
193 trp->tp += 1;
194 if (trp->tp >= trp->lp || trp->tp->type == END)
196 np->vp = &onetr;
197 return;
199 else
200 error(FATAL, "Illegal -A argument %r", trp);
203 if (trp->tp >= trp->lp || trp->tp->type != NAME)
204 goto syntax;
205 np = lookup(trp->tp, 1);
206 np->flag |= ISDEFINED;
207 trp->tp += 1;
208 if (trp->tp >= trp->lp || trp->tp->type == END)
210 np->vp = &onetr;
211 return;
213 if (trp->tp->type != ASGN)
214 goto syntax;
215 trp->tp += 1;
216 if ((trp->lp - 1)->type == END)
217 trp->lp -= 1;
218 np->vp = normtokenrow(trp);
219 return;
220 syntax:
221 error(FATAL, "Illegal -D or -U argument %r", trp);
227 * Do macro expansion in a row of tokens.
228 * Flag is NULL if more input can be gathered.
230 void
231 expandrow(Tokenrow * trp, char *flag)
233 Token * tp;
234 Nlist * np;
236 MacroValidatorList validators;
237 mvl_init(&validators);
238 /* Sets all token-identifiers to 0 because tokens may not be initialised (never use C!) */
239 tokenrow_zeroTokenIdentifiers(trp);
241 if (flag)
242 setsource(flag, -1, -1, "", 0);
243 for (tp = trp->tp; tp < trp->lp;)
245 mvl_check(&validators, tp);
247 if (tp->type != NAME
248 || quicklook(tp->t[0], tp->len > 1 ? tp->t[1] : 0) == 0
249 || (np = lookup(tp, 0)) == NULL
250 || (np->flag & (ISDEFINED | ISMAC)) == 0
251 || (np->flag & ISACTIVE) != 0)
253 tp++;
254 continue;
256 trp->tp = tp;
257 if (np->val == KDEFINED)
259 tp->type = DEFINED;
260 if ((tp + 1) < trp->lp && (tp + 1)->type == NAME)
261 (tp + 1)->type = NAME1;
262 else
263 if ((tp + 3) < trp->lp && (tp + 1)->type == LP
264 && (tp + 2)->type == NAME && (tp + 3)->type == RP)
265 (tp + 2)->type = NAME1;
266 else
267 error(ERROR, "Incorrect syntax for `defined'");
268 tp++;
269 continue;
271 else
272 if (np->val == KMACHINE)
274 if (((tp - 1) >= trp->bp) && ((tp - 1)->type == SHARP))
276 tp->type = ARCHITECTURE;
277 if ((tp + 1) < trp->lp && (tp + 1)->type == NAME)
278 (tp + 1)->type = NAME2;
279 else
280 if ((tp + 3) < trp->lp && (tp + 1)->type == LP
281 && (tp + 2)->type == NAME && (tp + 3)->type == RP)
282 (tp + 2)->type = NAME2;
283 else
284 error(ERROR, "Incorrect syntax for `#machine'");
286 tp++;
287 continue;
290 if (np->flag & ISMAC)
291 builtin(trp, np->val);
292 else
293 expand(trp, np, &validators);
294 tp = trp->tp;
295 } // end for
296 if (flag)
297 unsetsource();
299 mvl_destruct(&validators);
303 * Expand the macro whose name is np, at token trp->tp, in the tokenrow.
304 * Return trp->tp at the first token next to be expanded
305 * (ordinarily the beginning of the expansion)
306 * I.e.: the same position as before!
307 * Only one expansion is performed, then we return to the expandrow()
308 * loop and start at same position.
310 void
311 expand(Tokenrow * trp, Nlist * np, MacroValidatorList * pValidators)
313 Tokenrow ntr;
314 int ntokc, narg;
315 Tokenrow *atr[NARG + 1];
317 if (Mflag == 2)
319 if (np->ap)
320 error(INFO, "Macro expansion of %t with %s(%r)", trp->tp, np->name, np->ap);
321 else
322 error(INFO, "Macro expansion of %t with %s", trp->tp, np->name);
325 copytokenrow(&ntr, np->vp); /* copy macro value */
326 if (np->ap == NULL) /* parameterless */
327 ntokc = 1;
328 else
330 int i;
332 ntokc = gatherargs(trp, atr, &narg);
333 if (narg < 0)
334 { /* not actually a call (no '(') */
335 trp->tp++;
336 return;
338 if (narg != rowlen(np->ap))
340 error(ERROR, "Disagreement in number of macro arguments");
341 trp->tp += ntokc;
342 return;
345 /** If gatherargs passed a macro validating token, this token
346 must become valid here.
347 trp->tp+0 was checked in expandrow(), so we dont need to do it
348 again here:
350 for (i = 1; i < ntokc; i++)
352 mvl_check(pValidators,trp->tp+i);
355 substargs(np, &ntr, atr); /* put args into replacement */
356 for (i = 0; i < narg; i++)
358 dofree(atr[i]->bp);
359 dofree(atr[i]);
363 doconcat(&ntr); /* execute ## operators */
364 ntr.tp = ntr.bp;
365 makespace(&ntr, trp->tp);
367 tokenrow_zeroTokenIdentifiers(&ntr);
368 insertrow(trp, ntokc, &ntr);
370 /* add validator for just invalidated macro:
372 np->flag |= ISACTIVE;
373 if (trp->tp != trp->lp)
374 { /* tp is a valid pointer: */
375 mvl_add(pValidators,np,trp->tp);
377 else
378 { /* tp is == lp, therefore does not point to valid memory: */
379 mvl_add(pValidators,np,0);
381 /* reset trp->tp to original position:
383 trp->tp -= ntr.lp - ntr.bp; /* so the result will be tested for macros from the same position again */
385 dofree(ntr.bp);
387 return;
391 * Gather an arglist, starting in trp with tp pointing at the macro name.
392 * Return total number of tokens passed, stash number of args found.
393 * trp->tp is not changed relative to the tokenrow.
396 gatherargs(Tokenrow * trp, Tokenrow ** atr, int *narg)
398 int parens = 1;
399 int ntok = 0;
400 Token *bp, *lp;
401 Tokenrow ttr;
402 int ntokp;
403 int needspace;
405 *narg = -1; /* means that there is no macro
406 * call */
407 /* look for the ( */
408 for (;;)
410 trp->tp++;
411 ntok++;
412 if (trp->tp >= trp->lp)
414 gettokens(trp, 0);
415 if ((trp->lp - 1)->type == END)
417 trp->lp -= 1;
418 trp->tp -= ntok;
419 return ntok;
422 if (trp->tp->type == LP)
423 break;
424 if (trp->tp->type != NL)
425 return ntok;
427 *narg = 0;
428 ntok++;
429 ntokp = ntok;
430 trp->tp++;
431 /* search for the terminating ), possibly extending the row */
432 needspace = 0;
433 while (parens > 0)
435 if (trp->tp >= trp->lp)
436 gettokens(trp, 0);
437 if (needspace)
439 needspace = 0;
440 /* makespace(trp); [rh] */
442 if (trp->tp->type == END)
444 trp->lp -= 1;
445 trp->tp -= ntok;
446 error(ERROR, "EOF in macro arglist");
447 return ntok;
449 if (trp->tp->type == NL)
451 trp->tp += 1;
452 adjustrow(trp, -1);
453 trp->tp -= 1;
454 /* makespace(trp); [rh] */
455 needspace = 1;
456 continue;
458 if (trp->tp->type == LP)
459 parens++;
460 else
461 if (trp->tp->type == RP)
462 parens--;
463 trp->tp++;
464 ntok++;
466 trp->tp -= ntok;
467 /* Now trp->tp won't move underneath us */
468 lp = bp = trp->tp + ntokp;
469 for (; parens >= 0; lp++)
471 if (lp->type == LP)
473 parens++;
474 continue;
476 if (lp->type == RP)
477 parens--;
478 if (lp->type == DSHARP)
479 lp->type = DSHARP1; /* ## not special in arg */
480 if ((lp->type == COMMA && parens == 0) ||
481 ( parens < 0 && ((lp - 1)->type != LP)))
483 if (*narg >= NARG - 1)
484 error(FATAL, "Sorry, too many macro arguments");
485 ttr.bp = ttr.tp = bp;
486 ttr.lp = lp;
487 atr[(*narg)++] = normtokenrow(&ttr);
488 bp = lp + 1;
491 return ntok;
495 * substitute the argument list into the replacement string
496 * This would be simple except for ## and #
498 void
499 substargs(Nlist * np, Tokenrow * rtr, Tokenrow ** atr)
501 Tokenrow tatr;
502 Token *tp;
503 int ntok, argno;
505 for (rtr->tp = rtr->bp; rtr->tp < rtr->lp;)
507 if (rtr->tp->type == SHARP)
508 { /* string operator */
509 tp = rtr->tp;
510 rtr->tp += 1;
511 if ((argno = lookuparg(np, rtr->tp)) < 0)
513 error(ERROR, "# not followed by macro parameter");
514 continue;
516 ntok = 1 + (int)(rtr->tp - tp);
517 rtr->tp = tp;
518 insertrow(rtr, ntok, stringify(atr[argno]));
519 continue;
521 if (rtr->tp->type == NAME
522 && (argno = lookuparg(np, rtr->tp)) >= 0)
524 if (((rtr->tp + 1) < rtr->lp && (rtr->tp + 1)->type == DSHARP)
525 || (rtr->tp != rtr->bp && (rtr->tp - 1)->type == DSHARP))
527 copytokenrow(&tatr, atr[argno]);
528 makespace(&tatr, rtr->tp);
529 insertrow(rtr, 1, &tatr);
530 dofree(tatr.bp);
532 else
534 copytokenrow(&tatr, atr[argno]);
535 makespace(&tatr, rtr->tp);
536 expandrow(&tatr, "<macro>");
537 insertrow(rtr, 1, &tatr);
538 dofree(tatr.bp);
540 continue;
542 rtr->tp++;
547 * Evaluate the ## operators in a tokenrow
549 void
550 doconcat(Tokenrow * trp)
552 Token *ltp, *ntp;
553 Tokenrow ntr;
554 size_t len;
556 for (trp->tp = trp->bp; trp->tp < trp->lp; trp->tp++)
558 if (trp->tp->type == DSHARP1)
559 trp->tp->type = DSHARP;
560 else
561 if (trp->tp->type == DSHARP)
563 int i;
564 char tt[NCONCAT];
566 ltp = trp->tp - 1;
567 ntp = trp->tp + 1;
569 if (ltp < trp->bp || ntp >= trp->lp)
571 error(ERROR, "## occurs at border of replacement");
572 continue;
575 ntp = ltp;
576 i = 1;
577 len = 0;
581 if (len + ntp->len + ntp->wslen > sizeof(tt))
583 error(ERROR, "## string concatination buffer overrun");
584 break;
587 if (ntp != trp->tp + 1)
589 strncpy((char *) tt + len, (char *) ntp->t - ntp->wslen,
590 ntp->len + ntp->wslen);
591 len += ntp->len + ntp->wslen;
593 else // Leerzeichen um ## herum entfernen:
595 strncpy((char *) tt + len, (char *) ntp->t, ntp->len);
596 len += ntp->len;
599 ntp = trp->tp + i;
600 i++;
602 while (ntp < trp->lp);
604 tt[len] = '\0';
605 setsource("<##>", -1, -1, tt, 0);
606 maketokenrow(3, &ntr);
607 gettokens(&ntr, 1);
608 unsetsource();
609 if (ntr.bp->type == UNCLASS)
610 error(WARNING, "Bad token %r produced by ##", &ntr);
611 while ((ntr.lp-1)->len == 0 && ntr.lp != ntr.bp)
612 ntr.lp--;
614 doconcat(&ntr);
615 trp->tp = ltp;
616 makespace(&ntr, ltp);
617 insertrow(trp, (int)(ntp - ltp), &ntr);
618 dofree(ntr.bp);
619 trp->tp--;
625 * tp is a potential parameter name of macro mac;
626 * look it up in mac's arglist, and if found, return the
627 * corresponding index in the argname array. Return -1 if not found.
630 lookuparg(Nlist * mac, Token * tp)
632 Token *ap;
634 if (tp->type != NAME || mac->ap == NULL)
635 return -1;
636 for (ap = mac->ap->bp; ap < mac->ap->lp; ap++)
638 if (ap->len == tp->len && strncmp((char *) ap->t, (char *) tp->t, ap->len) == 0)
639 return (int)(ap - mac->ap->bp);
641 return -1;
645 * Return a quoted version of the tokenrow (from # arg)
647 #define STRLEN 512
648 Tokenrow *
649 stringify(Tokenrow * vp)
651 static Token t = {STRING, 0, 0, 0, NULL, 0};
652 static Tokenrow tr = {&t, &t, &t + 1, 1};
653 Token *tp;
654 uchar s[STRLEN];
655 uchar *sp = s, *cp;
656 int i, instring;
658 *sp++ = '"';
659 for (tp = vp->bp; tp < vp->lp; tp++)
661 instring = tp->type == STRING || tp->type == CCON;
662 if (sp + 2 * tp->len + tp->wslen >= &s[STRLEN - 10])
664 error(ERROR, "Stringified macro arg is too long");
665 break;
668 // Change by np 31.10.2001, #93725 - begin
669 if ( tp->wslen > 0 )
670 *sp++ = ' ';
671 // change end.
673 for (i = 0, cp = tp->t; (unsigned int)i < tp->len; i++)
675 if (instring && (*cp == '"' || *cp == '\\'))
676 *sp++ = '\\';
677 *sp++ = *cp++;
680 *sp++ = '"';
681 *sp = '\0';
682 sp = s;
683 t.len = strlen((char *) sp);
684 t.t = newstring(sp, t.len, 0);
685 return &tr;
689 * expand a builtin name
691 void
692 builtin(Tokenrow * trp, int biname)
694 char *op;
695 Token *tp;
696 Source *s;
698 tp = trp->tp;
699 trp->tp++;
700 /* need to find the real source */
701 s = cursource;
702 while (s && s->fd == -1)
703 s = s->next;
704 if (s == NULL)
705 s = cursource;
706 /* most are strings */
707 tp->type = STRING;
708 if (tp->wslen)
710 *outptr++ = ' ';
711 tp->wslen = 1;
713 op = outptr;
714 *op++ = '"';
715 switch (biname)
718 case KLINENO:
719 tp->type = NUMBER;
720 op = outnum(op - 1, s->line);
721 break;
723 case KFILE:
725 char *src = s->filename;
727 while ((*op++ = *src++) != 0)
728 if (src[-1] == '\\')
729 *op++ = '\\';
730 op--;
731 break;
734 case KDATE:
735 strncpy(op, curtime + 4, 7);
736 strncpy(op + 7, curtime + 20, 4);
737 op += 11;
738 break;
740 case KTIME:
741 strncpy(op, curtime + 11, 8);
742 op += 8;
743 break;
745 default:
746 error(ERROR, "cpp botch: unknown internal macro");
747 return;
749 if (tp->type == STRING)
750 *op++ = '"';
751 tp->t = (uchar *) outptr;
752 tp->len = op - outptr;
753 outptr = op;
756 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */