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, 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
);
290 expand(trp
, np
, &validators
);
296 mvl_destruct(&validators
);
300 * Expand the macro whose name is np, at token trp->tp, in the tokenrow.
301 * Return trp->tp at the first token next to be expanded
302 * (ordinarily the beginning of the expansion)
303 * I.e.: the same position as before!
304 * Only one expansion is performed, then we return to the expandrow()
305 * loop and start at same position.
308 expand(Tokenrow
* trp
, Nlist
* np
, MacroValidatorList
* pValidators
)
312 Tokenrow
*atr
[NARG
+ 1];
317 error(INFO
, "Macro expansion of %t with %s(%r)", trp
->tp
, np
->name
, np
->ap
);
319 error(INFO
, "Macro expansion of %t with %s", trp
->tp
, np
->name
);
322 copytokenrow(&ntr
, np
->vp
); /* copy macro value */
323 if (np
->ap
== NULL
) /* parameterless */
329 ntokc
= gatherargs(trp
, atr
, &narg
);
331 { /* not actually a call (no '(') */
335 if (narg
!= rowlen(np
->ap
))
337 error(ERROR
, "Disagreement in number of macro arguments");
342 /** If gatherargs passed a macro validating token, this token
343 must become valid here.
344 trp->tp+0 was checked in expandrow(), so we don't need to do it
347 for (i
= 1; i
< ntokc
; i
++)
349 mvl_check(pValidators
,trp
->tp
+i
);
352 substargs(np
, &ntr
, atr
); /* put args into replacement */
353 for (i
= 0; i
< narg
; i
++)
360 doconcat(&ntr
); /* execute ## operators */
362 makespace(&ntr
, trp
->tp
);
364 tokenrow_zeroTokenIdentifiers(&ntr
);
365 insertrow(trp
, ntokc
, &ntr
);
367 /* add validator for just invalidated macro:
369 np
->flag
|= ISACTIVE
;
370 if (trp
->tp
!= trp
->lp
)
371 { /* tp is a valid pointer: */
372 mvl_add(pValidators
,np
,trp
->tp
);
375 { /* tp is == lp, therefore does not point to valid memory: */
376 mvl_add(pValidators
,np
,NULL
);
378 /* reset trp->tp to original position:
380 trp
->tp
-= ntr
.lp
- ntr
.bp
; /* so the result will be tested for macros from the same position again */
388 * Gather an arglist, starting in trp with tp pointing at the macro name.
389 * Return total number of tokens passed, stash number of args found.
390 * trp->tp is not changed relative to the tokenrow.
393 gatherargs(Tokenrow
* trp
, Tokenrow
** atr
, int *narg
)
402 *narg
= -1; /* means that there is no macro
409 if (trp
->tp
>= trp
->lp
)
412 if ((trp
->lp
- 1)->type
== END
)
419 if (trp
->tp
->type
== LP
)
421 if (trp
->tp
->type
!= NL
)
428 /* search for the terminating ), possibly extending the row */
432 if (trp
->tp
>= trp
->lp
)
437 /* makespace(trp); [rh] */
439 if (trp
->tp
->type
== END
)
443 error(ERROR
, "EOF in macro arglist");
446 if (trp
->tp
->type
== NL
)
451 /* makespace(trp); [rh] */
455 if (trp
->tp
->type
== LP
)
458 if (trp
->tp
->type
== RP
)
464 /* Now trp->tp won't move underneath us */
465 lp
= bp
= trp
->tp
+ ntokp
;
466 for (; parens
>= 0; lp
++)
475 if (lp
->type
== DSHARP
)
476 lp
->type
= DSHARP1
; /* ## not special in arg */
477 if ((lp
->type
== COMMA
&& parens
== 0) ||
478 ( parens
< 0 && ((lp
- 1)->type
!= LP
)))
480 if (*narg
>= NARG
- 1)
481 error(FATAL
, "Sorry, too many macro arguments");
482 ttr
.bp
= ttr
.tp
= bp
;
484 atr
[(*narg
)++] = normtokenrow(&ttr
);
492 * substitute the argument list into the replacement string
493 * This would be simple except for ## and #
496 substargs(Nlist
* np
, Tokenrow
* rtr
, Tokenrow
** atr
)
502 for (rtr
->tp
= rtr
->bp
; rtr
->tp
< rtr
->lp
;)
504 if (rtr
->tp
->type
== SHARP
)
505 { /* string operator */
508 if ((argno
= lookuparg(np
, rtr
->tp
)) < 0)
510 error(ERROR
, "# not followed by macro parameter");
513 ntok
= 1 + (int)(rtr
->tp
- tp
);
515 insertrow(rtr
, ntok
, stringify(atr
[argno
]));
518 if (rtr
->tp
->type
== NAME
519 && (argno
= lookuparg(np
, rtr
->tp
)) >= 0)
521 if (((rtr
->tp
+ 1) < rtr
->lp
&& (rtr
->tp
+ 1)->type
== DSHARP
)
522 || (rtr
->tp
!= rtr
->bp
&& (rtr
->tp
- 1)->type
== DSHARP
))
524 copytokenrow(&tatr
, atr
[argno
]);
525 makespace(&tatr
, rtr
->tp
);
526 insertrow(rtr
, 1, &tatr
);
531 copytokenrow(&tatr
, atr
[argno
]);
532 makespace(&tatr
, rtr
->tp
);
533 expandrow(&tatr
, "<macro>");
534 insertrow(rtr
, 1, &tatr
);
544 * Evaluate the ## operators in a tokenrow
547 doconcat(Tokenrow
* trp
)
553 for (trp
->tp
= trp
->bp
; trp
->tp
< trp
->lp
; trp
->tp
++)
555 if (trp
->tp
->type
== DSHARP1
)
556 trp
->tp
->type
= DSHARP
;
558 if (trp
->tp
->type
== DSHARP
)
566 if (ltp
< trp
->bp
|| ntp
>= trp
->lp
)
568 error(ERROR
, "## occurs at border of replacement");
578 if (len
+ ntp
->len
+ ntp
->wslen
> sizeof(tt
))
580 error(ERROR
, "## string concatination buffer overrun");
584 if (ntp
!= trp
->tp
+ 1)
586 strncpy((char *) tt
+ len
, (char *) ntp
->t
- ntp
->wslen
,
587 ntp
->len
+ ntp
->wslen
);
588 len
+= ntp
->len
+ ntp
->wslen
;
590 else // Leerzeichen um ## herum entfernen:
592 strncpy((char *) tt
+ len
, (char *) ntp
->t
, ntp
->len
);
599 while (ntp
< trp
->lp
);
602 setsource("<##>", -1, -1, tt
, 0);
603 maketokenrow(3, &ntr
);
606 if (ntr
.bp
->type
== UNCLASS
)
607 error(WARNING
, "Bad token %r produced by ##", &ntr
);
608 while ((ntr
.lp
-1)->len
== 0 && ntr
.lp
!= ntr
.bp
)
613 makespace(&ntr
, ltp
);
614 insertrow(trp
, (int)(ntp
- ltp
), &ntr
);
622 * tp is a potential parameter name of macro mac;
623 * look it up in mac's arglist, and if found, return the
624 * corresponding index in the argname array. Return -1 if not found.
627 lookuparg(Nlist
* mac
, Token
* tp
)
631 if (tp
->type
!= NAME
|| mac
->ap
== NULL
)
633 for (ap
= mac
->ap
->bp
; ap
< mac
->ap
->lp
; ap
++)
635 if (ap
->len
== tp
->len
&& strncmp((char *) ap
->t
, (char *) tp
->t
, ap
->len
) == 0)
636 return (int)(ap
- mac
->ap
->bp
);
642 * Return a quoted version of the tokenrow (from # arg)
646 stringify(Tokenrow
* vp
)
648 static Token t
= {STRING
, 0, 0, 0, NULL
, 0};
649 static Tokenrow tr
= {&t
, &t
, &t
+ 1, 1};
656 for (tp
= vp
->bp
; tp
< vp
->lp
; tp
++)
658 instring
= tp
->type
== STRING
|| tp
->type
== CCON
;
659 if (sp
+ 2 * tp
->len
+ tp
->wslen
>= &s
[STRLEN
- 10])
661 error(ERROR
, "Stringified macro arg is too long");
665 // Change by np 31.10.2001, #93725 - begin
670 for (i
= 0, cp
= tp
->t
; (unsigned int)i
< tp
->len
; i
++)
672 if (instring
&& (*cp
== '"' || *cp
== '\\'))
680 t
.len
= strlen((char *) sp
);
681 t
.t
= newstring(sp
, t
.len
, 0);
686 * expand a builtin name
689 builtin(Tokenrow
* trp
, int biname
)
697 /* need to find the real source */
699 while (s
&& s
->fd
== -1)
703 /* most are strings */
717 op
= outnum(op
- 1, s
->line
);
722 char *src
= s
->filename
;
724 while ((*op
++ = *src
++) != 0)
732 strncpy(op
, curtime
+ 4, 7);
733 strncpy(op
+ 7, curtime
+ 20, 4);
738 strncpy(op
, curtime
+ 11, 8);
743 error(ERROR
, "cpp botch: unknown internal macro");
746 if (tp
->type
== STRING
)
748 tp
->t
= (uchar
*) outptr
;
749 tp
->len
= op
- outptr
;
753 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */