1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
23 #if defined(__IBMC__) || defined(__EMX__) || defined(_MSC_VER)
25 # define PATH_MAX _MAX_PATH
35 * do a macro definition. tp points to the name being defined in the line
38 dodefine(Tokenrow
* trp
)
44 static uchar location
[(PATH_MAX
+ 8) * NINC
], *cp
;
47 if (tp
>= trp
->lp
|| tp
->type
!= NAME
)
49 error(ERROR
, "#defined token is not a name");
53 if (np
->flag
& ISUNCHANGE
)
55 error(ERROR
, "#defined token %t can't be redefined", tp
);
58 /* collect arguments */
61 if (tp
< trp
->lp
&& tp
->type
== LP
&& tp
->wslen
== 0)
65 maketokenrow(2, args
);
81 if (narg
>= args
->max
)
83 for (atp
= args
->bp
; atp
< args
->lp
; atp
++)
84 if (atp
->len
== tp
->len
85 && strncmp((char *) atp
->t
, (char *) tp
->t
, tp
->len
) == 0)
86 error(ERROR
, "Duplicate macro argument");
92 if (tp
->type
!= COMMA
)
101 error(ERROR
, "Syntax error in macro parameters");
108 if (((trp
->lp
) - 1)->type
== NL
)
110 def
= normtokenrow(trp
);
111 if (np
->flag
& ISDEFINED
)
113 if (comparetokens(def
, np
->vp
)
114 || (np
->ap
== NULL
) != (args
== NULL
)
115 || (np
->ap
&& comparetokens(args
, np
->ap
)))
119 "Macro redefinition of %t (already defined at %s)",
120 trp
->bp
+ 2, np
->loc
);
123 "Macro redefinition of %t (already defined at %s)",
124 trp
->bp
+ 2, "commandline" );
131 tap
= normtokenrow(args
);
138 np
->flag
|= ISDEFINED
;
140 /* build location string of macro definition */
141 for (cp
= location
, s
= cursource
; s
; s
= s
->next
)
146 sprintf((char *)cp
, "%s:%d", s
->filename
, s
->line
);
147 cp
+= strlen((char *)cp
);
150 np
->loc
= newstring(location
, strlen((char *)location
), 0);
155 error(INFO
, "Macro definition of %s(%r) [%r]", np
->name
, np
->ap
, np
->vp
);
157 error(INFO
, "Macro definition of %s [%r]", np
->name
, np
->vp
);
162 * Definition received via -D or -U
165 doadefine(Tokenrow
* trp
, int type
)
168 static uchar onestr
[2] = "1";
169 static Token onetoken
[1] = {{NUMBER
, 0, 1, onestr
, 0}};
170 static Tokenrow onetr
= {onetoken
, onetoken
, onetoken
+ 1, 1};
175 if (trp
->lp
- trp
->tp
!= 2 || trp
->tp
->type
!= NAME
)
177 if ((np
= lookup(trp
->tp
, 0)) == NULL
)
179 np
->flag
&= ~ISDEFINED
;
185 if (trp
->tp
>= trp
->lp
|| trp
->tp
->type
!= NAME
)
187 trp
->tp
->type
= ARCHITECTURE
;
188 np
= lookup(trp
->tp
, 1);
189 np
->flag
|= ISARCHITECTURE
;
191 if (trp
->tp
>= trp
->lp
|| trp
->tp
->type
== END
)
197 error(FATAL
, "Illegal -A argument %r", trp
);
200 if (trp
->tp
>= trp
->lp
|| trp
->tp
->type
!= NAME
)
202 np
= lookup(trp
->tp
, 1);
203 np
->flag
|= ISDEFINED
;
205 if (trp
->tp
>= trp
->lp
|| trp
->tp
->type
== END
)
210 if (trp
->tp
->type
!= ASGN
)
213 if ((trp
->lp
- 1)->type
== END
)
215 np
->vp
= normtokenrow(trp
);
218 error(FATAL
, "Illegal -D or -U argument %r", trp
);
224 * Do macro expansion in a row of tokens.
225 * Flag is NULL if more input can be gathered.
228 expandrow(Tokenrow
* trp
, char *flag
)
233 MacroValidatorList validators
;
234 mvl_init(&validators
);
235 /* Sets all token-identifiers to 0 because tokens may not be initialised (never use C!) */
236 tokenrow_zeroTokenIdentifiers(trp
);
239 setsource(flag
, -1, -1, "", 0);
240 for (tp
= trp
->tp
; tp
< trp
->lp
;)
242 mvl_check(&validators
, tp
);
245 || quicklook(tp
->t
[0], tp
->len
> 1 ? tp
->t
[1] : 0) == 0
246 || (np
= lookup(tp
, 0)) == NULL
247 || (np
->flag
& (ISDEFINED
| ISMAC
)) == 0
248 || (np
->flag
& ISACTIVE
) != 0)
254 if (np
->val
== KDEFINED
)
257 if ((tp
+ 1) < trp
->lp
&& (tp
+ 1)->type
== NAME
)
258 (tp
+ 1)->type
= NAME1
;
260 if ((tp
+ 3) < trp
->lp
&& (tp
+ 1)->type
== LP
261 && (tp
+ 2)->type
== NAME
&& (tp
+ 3)->type
== RP
)
262 (tp
+ 2)->type
= NAME1
;
264 error(ERROR
, "Incorrect syntax for `defined'");
269 if (np
->val
== KMACHINE
)
271 if (((tp
- 1) >= trp
->bp
) && ((tp
- 1)->type
== SHARP
))
273 tp
->type
= ARCHITECTURE
;
274 if ((tp
+ 1) < trp
->lp
&& (tp
+ 1)->type
== NAME
)
275 (tp
+ 1)->type
= NAME2
;
277 if ((tp
+ 3) < trp
->lp
&& (tp
+ 1)->type
== LP
278 && (tp
+ 2)->type
== NAME
&& (tp
+ 3)->type
== RP
)
279 (tp
+ 2)->type
= NAME2
;
281 error(ERROR
, "Incorrect syntax for `#machine'");
287 if (np
->flag
& ISMAC
)
288 builtin(trp
, np
->val
);
291 // coverity[overrun-buffer-arg: FALSE] - a multiple of trp->max is allocated, not trp->max itself
292 expand(trp
, np
, &validators
);
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.
311 expand(Tokenrow
* trp
, Nlist
* np
, MacroValidatorList
* pValidators
)
315 Tokenrow
*atr
[NARG
+ 1];
320 error(INFO
, "Macro expansion of %t with %s(%r)", trp
->tp
, np
->name
, np
->ap
);
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 */
332 ntokc
= gatherargs(trp
, atr
, &narg
);
334 { /* not actually a call (no '(') */
338 if (narg
!= rowlen(np
->ap
))
340 error(ERROR
, "Disagreement in number of macro arguments");
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 don't need to do it
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
++)
363 doconcat(&ntr
); /* execute ## operators */
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
);
378 { /* tp is == lp, therefore does not point to valid memory: */
379 mvl_add(pValidators
,np
,NULL
);
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 */
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
)
405 *narg
= -1; /* means that there is no macro
412 if (trp
->tp
>= trp
->lp
)
415 if ((trp
->lp
- 1)->type
== END
)
422 if (trp
->tp
->type
== LP
)
424 if (trp
->tp
->type
!= NL
)
431 /* search for the terminating ), possibly extending the row */
435 if (trp
->tp
>= trp
->lp
)
437 // coverity[overrun-buffer-arg: FALSE] - a multiple of trp->max is allocated, not trp->max itself
443 /* makespace(trp); [rh] */
445 if (trp
->tp
->type
== END
)
449 error(ERROR
, "EOF in macro arglist");
452 if (trp
->tp
->type
== NL
)
457 /* makespace(trp); [rh] */
461 if (trp
->tp
->type
== LP
)
464 if (trp
->tp
->type
== RP
)
470 /* Now trp->tp won't move underneath us */
471 lp
= bp
= trp
->tp
+ ntokp
;
472 for (; parens
>= 0; lp
++)
481 if (lp
->type
== DSHARP
)
482 lp
->type
= DSHARP1
; /* ## not special in arg */
483 if ((lp
->type
== COMMA
&& parens
== 0) ||
484 ( parens
< 0 && ((lp
- 1)->type
!= LP
)))
486 if (*narg
>= NARG
- 1)
487 error(FATAL
, "Sorry, too many macro arguments");
488 ttr
.bp
= ttr
.tp
= bp
;
490 atr
[(*narg
)++] = normtokenrow(&ttr
);
498 * substitute the argument list into the replacement string
499 * This would be simple except for ## and #
502 substargs(Nlist
* np
, Tokenrow
* rtr
, Tokenrow
** atr
)
508 for (rtr
->tp
= rtr
->bp
; rtr
->tp
< rtr
->lp
;)
510 if (rtr
->tp
->type
== SHARP
)
511 { /* string operator */
514 if ((argno
= lookuparg(np
, rtr
->tp
)) < 0)
516 error(ERROR
, "# not followed by macro parameter");
519 ntok
= 1 + (int)(rtr
->tp
- tp
);
521 insertrow(rtr
, ntok
, stringify(atr
[argno
]));
524 if (rtr
->tp
->type
== NAME
525 && (argno
= lookuparg(np
, rtr
->tp
)) >= 0)
527 if (((rtr
->tp
+ 1) < rtr
->lp
&& (rtr
->tp
+ 1)->type
== DSHARP
)
528 || (rtr
->tp
!= rtr
->bp
&& (rtr
->tp
- 1)->type
== DSHARP
))
530 copytokenrow(&tatr
, atr
[argno
]);
531 makespace(&tatr
, rtr
->tp
);
532 insertrow(rtr
, 1, &tatr
);
537 copytokenrow(&tatr
, atr
[argno
]);
538 makespace(&tatr
, rtr
->tp
);
539 expandrow(&tatr
, "<macro>");
540 insertrow(rtr
, 1, &tatr
);
550 * Evaluate the ## operators in a tokenrow
553 doconcat(Tokenrow
* trp
)
559 for (trp
->tp
= trp
->bp
; trp
->tp
< trp
->lp
; trp
->tp
++)
561 if (trp
->tp
->type
== DSHARP1
)
562 trp
->tp
->type
= DSHARP
;
564 if (trp
->tp
->type
== DSHARP
)
572 if (ltp
< trp
->bp
|| ntp
>= trp
->lp
)
574 error(ERROR
, "## occurs at border of replacement");
584 if (len
+ ntp
->len
+ ntp
->wslen
> sizeof(tt
))
586 error(ERROR
, "## string concatenation buffer overrun");
590 if (ntp
!= trp
->tp
+ 1)
592 strncpy((char *) tt
+ len
, (char *) ntp
->t
- ntp
->wslen
,
593 ntp
->len
+ ntp
->wslen
);
594 len
+= ntp
->len
+ ntp
->wslen
;
598 // remove spaces around ##
599 strncpy((char *) tt
+ len
, (char *) ntp
->t
, ntp
->len
);
606 while (ntp
< trp
->lp
);
609 setsource("<##>", -1, -1, tt
, 0);
610 maketokenrow(3, &ntr
);
611 // coverity[overrun-buffer-arg: FALSE] - a multiple of trp->max is allocated, not trp->max itself
614 if (ntr
.bp
->type
== UNCLASS
)
615 error(WARNING
, "Bad token %r produced by ##", &ntr
);
616 while ((ntr
.lp
-1)->len
== 0 && ntr
.lp
!= ntr
.bp
)
621 makespace(&ntr
, ltp
);
622 insertrow(trp
, (int)(ntp
- ltp
), &ntr
);
630 * tp is a potential parameter name of macro mac;
631 * look it up in mac's arglist, and if found, return the
632 * corresponding index in the argname array. Return -1 if not found.
635 lookuparg(Nlist
* mac
, Token
const * tp
)
639 if (tp
->type
!= NAME
|| mac
->ap
== NULL
)
641 for (ap
= mac
->ap
->bp
; ap
< mac
->ap
->lp
; ap
++)
643 if (ap
->len
== tp
->len
&& strncmp((char *) ap
->t
, (char *) tp
->t
, ap
->len
) == 0)
644 return (int)(ap
- mac
->ap
->bp
);
650 * Return a quoted version of the tokenrow (from # arg)
654 stringify(Tokenrow
* vp
)
656 static Token t
= {STRING
, 0, 0, NULL
, 0};
657 static Tokenrow tr
= {&t
, &t
, &t
+ 1, 1};
664 for (tp
= vp
->bp
; tp
< vp
->lp
; tp
++)
666 instring
= tp
->type
== STRING
|| tp
->type
== CCON
;
667 if (sp
+ 2 * tp
->len
+ tp
->wslen
>= &s
[STRLEN
- 10])
669 error(ERROR
, "Stringified macro arg is too long");
673 // Change by np 31.10.2001, #93725 - begin
678 for (i
= 0, cp
= tp
->t
; (unsigned int)i
< tp
->len
; i
++)
680 if (instring
&& (*cp
== '"' || *cp
== '\\'))
688 t
.len
= strlen((char *) sp
);
689 t
.t
= newstring(sp
, t
.len
, 0);
694 * expand a builtin name
697 builtin(Tokenrow
* trp
, int biname
)
705 /* need to find the real source */
707 while (s
&& s
->fd
== -1)
711 /* most are strings */
725 op
= outnum(op
- 1, s
->line
);
730 char *src
= s
->filename
;
732 while ((*op
++ = *src
++) != 0)
740 strncpy(op
, curtime
+ 4, 7);
741 strncpy(op
+ 7, curtime
+ 20, 4);
746 strncpy(op
, curtime
+ 11, 8);
751 error(ERROR
, "cpp botch: unknown internal macro");
754 if (tp
->type
== STRING
)
756 tp
->t
= (uchar
*) outptr
;
757 tp
->len
= op
- outptr
;
761 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */