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 .
26 #if defined(__IBMC__) || defined(__EMX__) || defined(_MSC_VER)
28 # define PATH_MAX _MAX_PATH
38 * do a macro definition. tp points to the name being defined in the line
41 dodefine(Tokenrow
* trp
)
47 static uchar location
[(PATH_MAX
+ 8) * NINC
], *cp
;
50 if (tp
>= trp
->lp
|| tp
->type
!= NAME
)
52 error(ERROR
, "#defined token is not a name");
56 if (np
->flag
& ISUNCHANGE
)
58 error(ERROR
, "#defined token %t can't be redefined", tp
);
61 /* collect arguments */
64 if (tp
< trp
->lp
&& tp
->type
== LP
&& tp
->wslen
== 0)
68 maketokenrow(2, args
);
84 if (narg
>= args
->max
)
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");
95 if (tp
->type
!= COMMA
)
104 error(ERROR
, "Syntax error in macro parameters");
111 if (((trp
->lp
) - 1)->type
== NL
)
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
)))
122 "Macro redefinition of %t (already defined at %s)",
123 trp
->bp
+ 2, np
->loc
);
126 "Macro redefinition of %t (already defined at %s)",
127 trp
->bp
+ 2, "commandline" );
134 tap
= normtokenrow(args
);
141 np
->flag
|= ISDEFINED
;
143 /* build location string of macro definition */
144 for (cp
= location
, s
= cursource
; s
; s
= s
->next
)
149 sprintf((char *)cp
, "%s:%d", s
->filename
, s
->line
);
150 cp
+= strlen((char *)cp
);
153 np
->loc
= newstring(location
, strlen((char *)location
), 0);
158 error(INFO
, "Macro definition of %s(%r) [%r]", np
->name
, np
->ap
, np
->vp
);
160 error(INFO
, "Macro definition of %s [%r]", np
->name
, np
->vp
);
165 * Definition received via -D or -U
168 doadefine(Tokenrow
* trp
, int type
)
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};
178 if (trp
->lp
- trp
->tp
!= 2 || trp
->tp
->type
!= NAME
)
180 if ((np
= lookup(trp
->tp
, 0)) == NULL
)
182 np
->flag
&= ~ISDEFINED
;
188 if (trp
->tp
>= trp
->lp
|| trp
->tp
->type
!= NAME
)
190 trp
->tp
->type
= ARCHITECTURE
;
191 np
= lookup(trp
->tp
, 1);
192 np
->flag
|= ISARCHITECTURE
;
194 if (trp
->tp
>= trp
->lp
|| trp
->tp
->type
== END
)
200 error(FATAL
, "Illegal -A argument %r", trp
);
203 if (trp
->tp
>= trp
->lp
|| trp
->tp
->type
!= NAME
)
205 np
= lookup(trp
->tp
, 1);
206 np
->flag
|= ISDEFINED
;
208 if (trp
->tp
>= trp
->lp
|| trp
->tp
->type
== END
)
213 if (trp
->tp
->type
!= ASGN
)
216 if ((trp
->lp
- 1)->type
== END
)
218 np
->vp
= normtokenrow(trp
);
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.
231 expandrow(Tokenrow
* trp
, char *flag
)
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
);
242 setsource(flag
, -1, -1, "", 0);
243 for (tp
= trp
->tp
; tp
< trp
->lp
;)
245 mvl_check(&validators
, tp
);
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)
257 if (np
->val
== KDEFINED
)
260 if ((tp
+ 1) < trp
->lp
&& (tp
+ 1)->type
== NAME
)
261 (tp
+ 1)->type
= NAME1
;
263 if ((tp
+ 3) < trp
->lp
&& (tp
+ 1)->type
== LP
264 && (tp
+ 2)->type
== NAME
&& (tp
+ 3)->type
== RP
)
265 (tp
+ 2)->type
= NAME1
;
267 error(ERROR
, "Incorrect syntax for `defined'");
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
;
280 if ((tp
+ 3) < trp
->lp
&& (tp
+ 1)->type
== LP
281 && (tp
+ 2)->type
== NAME
&& (tp
+ 3)->type
== RP
)
282 (tp
+ 2)->type
= NAME2
;
284 error(ERROR
, "Incorrect syntax for `#machine'");
290 if (np
->flag
& ISMAC
)
291 builtin(trp
, np
->val
);
293 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 dont 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
,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 */
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
)
440 /* makespace(trp); [rh] */
442 if (trp
->tp
->type
== END
)
446 error(ERROR
, "EOF in macro arglist");
449 if (trp
->tp
->type
== NL
)
454 /* makespace(trp); [rh] */
458 if (trp
->tp
->type
== LP
)
461 if (trp
->tp
->type
== RP
)
467 /* Now trp->tp won't move underneath us */
468 lp
= bp
= trp
->tp
+ ntokp
;
469 for (; parens
>= 0; lp
++)
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
;
487 atr
[(*narg
)++] = normtokenrow(&ttr
);
495 * substitute the argument list into the replacement string
496 * This would be simple except for ## and #
499 substargs(Nlist
* np
, Tokenrow
* rtr
, Tokenrow
** atr
)
505 for (rtr
->tp
= rtr
->bp
; rtr
->tp
< rtr
->lp
;)
507 if (rtr
->tp
->type
== SHARP
)
508 { /* string operator */
511 if ((argno
= lookuparg(np
, rtr
->tp
)) < 0)
513 error(ERROR
, "# not followed by macro parameter");
516 ntok
= 1 + (int)(rtr
->tp
- tp
);
518 insertrow(rtr
, ntok
, stringify(atr
[argno
]));
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
);
534 copytokenrow(&tatr
, atr
[argno
]);
535 makespace(&tatr
, rtr
->tp
);
536 expandrow(&tatr
, "<macro>");
537 insertrow(rtr
, 1, &tatr
);
547 * Evaluate the ## operators in a tokenrow
550 doconcat(Tokenrow
* trp
)
556 for (trp
->tp
= trp
->bp
; trp
->tp
< trp
->lp
; trp
->tp
++)
558 if (trp
->tp
->type
== DSHARP1
)
559 trp
->tp
->type
= DSHARP
;
561 if (trp
->tp
->type
== DSHARP
)
569 if (ltp
< trp
->bp
|| ntp
>= trp
->lp
)
571 error(ERROR
, "## occurs at border of replacement");
581 if (len
+ ntp
->len
+ ntp
->wslen
> sizeof(tt
))
583 error(ERROR
, "## string concatination buffer overrun");
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
);
602 while (ntp
< trp
->lp
);
605 setsource("<##>", -1, -1, tt
, 0);
606 maketokenrow(3, &ntr
);
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
)
616 makespace(&ntr
, ltp
);
617 insertrow(trp
, (int)(ntp
- ltp
), &ntr
);
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
)
634 if (tp
->type
!= NAME
|| mac
->ap
== NULL
)
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
);
645 * Return a quoted version of the tokenrow (from # arg)
649 stringify(Tokenrow
* vp
)
651 static Token t
= {STRING
, 0, 0, 0, NULL
, 0};
652 static Tokenrow tr
= {&t
, &t
, &t
+ 1, 1};
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");
668 // Change by np 31.10.2001, #93725 - begin
673 for (i
= 0, cp
= tp
->t
; (unsigned int)i
< tp
->len
; i
++)
675 if (instring
&& (*cp
== '"' || *cp
== '\\'))
683 t
.len
= strlen((char *) sp
);
684 t
.t
= newstring(sp
, t
.len
, 0);
689 * expand a builtin name
692 builtin(Tokenrow
* trp
, int biname
)
700 /* need to find the real source */
702 while (s
&& s
->fd
== -1)
706 /* most are strings */
720 op
= outnum(op
- 1, s
->line
);
725 char *src
= s
->filename
;
727 while ((*op
++ = *src
++) != 0)
735 strncpy(op
, curtime
+ 4, 7);
736 strncpy(op
+ 7, curtime
+ 20, 4);
741 strncpy(op
, curtime
+ 11, 8);
746 error(ERROR
, "cpp botch: unknown internal macro");
749 if (tp
->type
== STRING
)
751 tp
->t
= (uchar
*) outptr
;
752 tp
->len
= op
- outptr
;
756 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */