Silence verbose testsuite runs.
[m4.git] / m4 / macro.c
blobfc28cd359f84dbf9cb8f64976ef52fc1e6aedc6a
1 /* GNU m4 -- A simple macro processor
2 Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 2001, 2006, 2007,
3 2008, 2009 Free Software Foundation, Inc.
5 This file is part of GNU M4.
7 GNU M4 is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU M4 is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 /* This file contains the functions that perform the basic argument
22 parsing and macro expansion. */
24 #include <config.h>
26 #include "m4private.h"
28 #include "intprops.h"
30 /* Define this to 1 see runtime debug info. Implied by DEBUG. */
31 /*#define DEBUG_INPUT 1 */
32 #ifndef DEBUG_MACRO
33 # define DEBUG_MACRO 0
34 #endif /* DEBUG_MACRO */
36 /* A note on argument memory lifetimes: We use an internal struct
37 (m4__macro_args_stacks) to maintain list of argument obstacks.
38 Within a recursion level, consecutive macros can share a stack, but
39 distinct recursion levels need different stacks since the nested
40 macro is interrupting the argument collection of the outer level.
41 Note that a reference can live as long as the expansion containing
42 the reference can participate as an argument in a future macro
43 call.
45 Therefore, we implement a reference counter for each expansion
46 level, tracking how many references exist into the obstack, as well
47 as associate a level with each reference. Of course, expand_macro
48 is actively using argv, so it increments the refcount on entry and
49 decrements it on exit. Additionally, any time the input engine is
50 handed a reference that it does not inline, it increases the
51 refcount in push_token, then decreases it in pop_input once the
52 reference has been rescanned. Finally, when the input engine hands
53 a reference back to expand_argument, the refcount increases, which
54 is then cleaned up at the end of expand_macro.
56 For a running example, consider this input:
58 define(a,A)define(b,`a(`$1')')define(c,$*)dnl
59 define(x,`a(1)`'c($@')define(y,`$@)')dnl
60 x(a(`b')``a'')y(`b')(`a')
61 => AAaA
63 Assuming all arguments are large enough to exceed the inlining
64 thresholds of the input engine, the interesting sequence of events
65 is as follows:
67 stacks[0] refs stacks[1] refs
68 after second dnl ends: `' 0 `' 0
69 expand_macro for x, level 0: `' 1 `' 0
70 expand_macro for a, level 1: `' 1 `' 1
71 after collect_arguments for a: `' 1 `b' 1
72 push `A' to input stack: `' 1 `b' 1
73 exit expand_macro for a: `' 1 `' 0
74 after collect_arguments for x: `A`a'' 1 `' 0
75 push `a(1)`'c(' to input stack: `A`a'' 1 `' 0
76 push_token saves $@(x) ref: `A`a'' 2 `' 0
77 exit expand_macro for x: `A`a'' 1 `' 0
78 expand_macro for a, level 0: `A`a'' 2 `' 0
79 after collect_arguments for a: `A`a''`1' 2 `' 0
80 push `A' to input stack: `A`a''`1' 2 `' 0
81 exit expand_macro for a: `A`a'' 1 `' 0
82 output `A': `A`a'' 1 `' 0
83 expand_macro for c, level 0: `A`a'' 2 `' 0
84 expand_argument gets $@(x) ref: `A`a''`$@(x)' 3 `' 0
85 pop_input ends $@(x) ref: `A`a''`$@(x)' 2 `' 0
86 expand_macro for y, level 1: `A`a''`$@(x)' 2 `' 1
87 after collect_arguments for y: `A`a''`$@(x)' 2 `b' 1
88 push_token saves $@(y) ref: `A`a''`$@(x)' 2 `b' 2
89 push `)' to input stack: `A`a''`$@(x)' 2 `b' 2
90 exit expand_macro for y: `A`a''`$@(x)' 2 `b' 1
91 expand_argument gets $@(y) ref: `A`a''`$@(x)$@(y)' 2 `b' 2
92 pop_input ends $@(y) ref: `A`a''`$@(x)$@(y)' 2 `b' 1
93 after collect_arguments for c: `A`a''`$@(x)$@(y)' 2 `b' 1
94 push_token saves $*(c) ref: `A`a''`$@(x)$@(y)' 3 `b' 2
95 expand_macro frees $@(x) ref: `A`a''`$@(x)$@(y)' 2 `b' 2
96 expand_macro frees $@(y) ref: `A`a''`$@(x)$@(y)' 2 `b' 1
97 exit expand_macro for c: `A`a''`$@(x)$@(y)' 1 `b' 1
98 output `Aa': `A`a''`$@(x)$@(y)' 0 `b' 1
99 pop_input ends $*(c)$@(x) ref: `' 0 `b' 1
100 expand_macro for b, level 0: `' 1 `b' 1
101 pop_input ends $*(c)$@(y) ref: `' 1 `' 0
102 after collect_arguments for b: `a' 1 `' 0
103 push `a(`' to input stack: `a' 1 `' 0
104 push_token saves $1(b) ref: `a' 2 `' 0
105 push `')' to input stack: `a' 2 `' 0
106 exit expand_macro for b: `a' 1 `' 0
107 expand_macro for a, level 0 : `a' 2 `' 0
108 expand_argument gets $1(b) ref: `a'`$1(b)' 3 `' 0
109 pop_input ends $1(b) ref: `a'`$1(b)' 2 `' 0
110 after collect_arguments for a: `a'`$1(b)' 2 `' 0
111 push `A' to input stack: `a'`$1(b)' 2 `' 0
112 expand_macro frees $1(b) ref: `a'`$1(b)' 1 `' 0
113 exit expand_macro for a: `' 0 `' 0
114 output `A': `' 0 `' 0
116 An obstack is only completely cleared when its refcount reaches
117 zero. However, as an optimization, expand_macro also frees
118 anything that it added to the obstack if no additional references
119 were added at the current expansion level, to reduce the amount of
120 memory left on the obstack while waiting for refcounts to drop.
123 static m4_macro_args *collect_arguments (m4 *, m4_call_info *, m4_symbol *,
124 m4_obstack *, m4_obstack *);
125 static void expand_macro (m4 *, const char *, size_t, m4_symbol *);
126 static bool expand_token (m4 *, m4_obstack *, m4__token_type,
127 m4_symbol_value *, int, bool);
128 static bool expand_argument (m4 *, m4_obstack *, m4_symbol_value *,
129 const m4_call_info *);
130 static void process_macro (m4 *, m4_symbol_value *, m4_obstack *, int,
131 m4_macro_args *);
133 static unsigned int trace_pre (m4 *, m4_macro_args *);
134 static void trace_post (m4 *, unsigned int, const m4_call_info *);
135 static unsigned int trace_header (m4 *, const m4_call_info *);
136 static void trace_flush (m4 *, unsigned int);
139 /* The number of the current call of expand_macro (). */
140 static size_t macro_call_id = 0;
142 /* A placeholder symbol value representing the empty string, used to
143 optimize checks for emptiness. */
144 static m4_symbol_value empty_symbol;
146 #if DEBUG_MACRO
147 /* True if significant changes to stacks should be printed to the
148 trace stream. Primarily useful for debugging $@ ref memory leaks,
149 and controlled by M4_DEBUG_MACRO environment variable. */
150 static int debug_macro_level;
151 #else
152 # define debug_macro_level 0
153 #endif /* !DEBUG_MACRO */
154 #define PRINT_ARGCOUNT_CHANGES 1 /* Any change to argcount > 1. */
155 #define PRINT_REFCOUNT_INCREASE 2 /* Any increase to refcount. */
156 #define PRINT_REFCOUNT_DECREASE 4 /* Any decrease to refcount. */
160 /* This function reads all input, and expands each token, one at a time. */
161 void
162 m4_macro_expand_input (m4 *context)
164 m4__token_type type;
165 m4_symbol_value token;
166 int line;
168 #if DEBUG_MACRO
169 const char *s = getenv ("M4_DEBUG_MACRO");
170 if (s)
171 debug_macro_level = strtol (s, NULL, 0);
172 #endif /* DEBUG_MACRO */
174 m4_set_symbol_value_text (&empty_symbol, "", 0, 0);
175 VALUE_MAX_ARGS (&empty_symbol) = -1;
177 while ((type = m4__next_token (context, &token, &line, NULL, false, NULL))
178 != M4_TOKEN_EOF)
179 expand_token (context, NULL, type, &token, line, true);
183 /* Expand one token onto OBS, according to its type. If OBS is NULL,
184 output the expansion to the current diversion. TYPE determines the
185 contents of TOKEN. Potential macro names (a TYPE of M4_TOKEN_WORD)
186 are looked up in the symbol table, to see if they have a macro
187 definition. If they have, they are expanded as macros, otherwise
188 the text are just copied to the output. LINE determines where
189 TOKEN began. FIRST is true if there is no prior content in the
190 current macro argument. Return true if the result is guranteed to
191 give the same parse on rescan in a quoted context with the same
192 quote age. Returning false is always safe, although it may lead to
193 slower performance. */
194 static bool
195 expand_token (m4 *context, m4_obstack *obs, m4__token_type type,
196 m4_symbol_value *token, int line, bool first)
198 m4_symbol *symbol;
199 bool result = false;
200 const char *text = (m4_is_symbol_value_text (token)
201 ? m4_get_symbol_value_text (token) : NULL);
203 switch (type)
204 { /* TOKSW */
205 case M4_TOKEN_EOF:
206 case M4_TOKEN_MACDEF:
207 /* Always safe, since there is no text to rescan. */
208 return true;
210 case M4_TOKEN_STRING:
211 /* Strings are safe in isolation (since quote_age detects any
212 change in delimiters), or when safe_quotes is true. This is
213 also returned for sequences of benign characters, such as
214 digits. When safe_quotes is false, we could technically
215 return true if we can prove that the concatenation of this
216 string to prior text does not form a multi-byte quote
217 delimiter, but that is a lot of overhead, so we give the
218 conservative answer of false. */
219 result = first || m4__safe_quotes (M4SYNTAX);
220 /* fallthru */
221 case M4_TOKEN_COMMENT:
222 /* Comments can contain unbalanced quote delimiters. Rather
223 than search for one, we return the conservative answer of
224 false. If obstack is provided, the string or comment was
225 already expanded into it during next_token. */
226 if (obs)
227 return result;
228 break;
230 case M4_TOKEN_OPEN:
231 case M4_TOKEN_COMMA:
232 case M4_TOKEN_CLOSE:
233 case M4_TOKEN_SPACE:
234 /* If safe_quotes is true, then these do not form a quote
235 delimiter. If it is false, we give the conservative answer
236 of false rather than taking time to prove that no multi-byte
237 quote delimiter is formed. */
238 result = m4__safe_quotes (M4SYNTAX);
239 break;
241 case M4_TOKEN_SIMPLE:
242 /* If safe_quotes is true, then all but the single-byte end
243 quote delimiter is safe in a quoted context; a single-byte
244 start delimiter will trigger M4_TOKEN_STRING instead. If
245 safe_quotes is false, we give the conservative answer of
246 false rather than taking time to prove that no multi-byte
247 quote delimiter is formed. */
248 result = (!m4_has_syntax (M4SYNTAX, *text, M4_SYNTAX_RQUOTE)
249 && m4__safe_quotes (M4SYNTAX));
250 if (result)
251 assert (!m4_has_syntax (M4SYNTAX, *text, M4_SYNTAX_LQUOTE));
252 break;
254 case M4_TOKEN_WORD:
256 const char *textp = text;
257 size_t len = m4_get_symbol_value_len (token);
258 size_t len2 = len;
260 if (m4_has_syntax (M4SYNTAX, *textp, M4_SYNTAX_ESCAPE))
262 textp++;
263 len2--;
266 symbol = m4_symbol_lookup (M4SYMTAB, textp, len2);
267 assert (!symbol || !m4_is_symbol_void (symbol));
268 if (symbol == NULL
269 || (symbol->value->type == M4_SYMBOL_FUNC
270 && BIT_TEST (SYMBOL_FLAGS (symbol), VALUE_BLIND_ARGS_BIT)
271 && !m4__next_token_is_open (context)))
273 m4_divert_text (context, obs, text, len, line);
274 /* If safe_quotes is true, then words do not overlap with
275 quote delimiters. If it is false, we give the
276 conservative answer of false rather than prove that no
277 multi-byte delimiters are formed. */
278 return m4__safe_quotes (M4SYNTAX);
280 expand_macro (context, textp, len2, symbol);
281 /* Expanding a macro may create new tokens to scan, and those
282 tokens may generate unsafe text, but we did not append any
283 text now. */
284 return true;
287 default:
288 assert (!"INTERNAL ERROR: bad token type in expand_token ()");
289 abort ();
291 m4_divert_text (context, obs, text, m4_get_symbol_value_len (token), line);
292 return result;
296 /* This function parses one argument to a macro call. It expects the
297 first left parenthesis or the separating comma to have been read by
298 the caller. It skips leading whitespace, then reads and expands
299 tokens, until it finds a comma or a right parenthesis at the same
300 level of parentheses. It returns a flag indicating whether the
301 argument read is the last for the active macro call. The arguments
302 are built on the obstack OBS, indirectly through expand_token ().
303 Report errors on behalf of CALLER. */
304 static bool
305 expand_argument (m4 *context, m4_obstack *obs, m4_symbol_value *argp,
306 const m4_call_info *caller)
308 m4__token_type type;
309 m4_symbol_value token;
310 int paren_level = 0;
311 int line = m4_get_current_line (context);
312 size_t len;
313 unsigned int age = m4__quote_age (M4SYNTAX);
314 bool first = true;
316 memset (argp, '\0', sizeof *argp);
317 VALUE_MAX_ARGS (argp) = -1;
319 /* Skip leading white space. */
322 type = m4__next_token (context, &token, NULL, obs, true, caller);
324 while (type == M4_TOKEN_SPACE);
326 while (1)
328 if (VALUE_MIN_ARGS (argp) < VALUE_MIN_ARGS (&token))
329 VALUE_MIN_ARGS (argp) = VALUE_MIN_ARGS (&token);
330 if (VALUE_MAX_ARGS (&token) < VALUE_MAX_ARGS (argp))
331 VALUE_MAX_ARGS (argp) = VALUE_MAX_ARGS (&token);
332 switch (type)
333 { /* TOKSW */
334 case M4_TOKEN_COMMA:
335 case M4_TOKEN_CLOSE:
336 if (paren_level == 0)
338 assert (argp->type != M4_SYMBOL_FUNC);
339 if (argp->type != M4_SYMBOL_COMP)
341 len = obstack_object_size (obs);
342 VALUE_MODULE (argp) = NULL;
343 if (len)
345 obstack_1grow (obs, '\0');
346 m4_set_symbol_value_text (argp, obstack_finish (obs),
347 len, age);
349 else
350 m4_set_symbol_value_text (argp, "", len, 0);
352 else
354 m4__make_text_link (obs, NULL, &argp->u.u_c.end);
355 if (argp->u.u_c.chain == argp->u.u_c.end
356 && argp->u.u_c.chain->type == M4__CHAIN_FUNC)
358 const m4__builtin *func = argp->u.u_c.chain->u.builtin;
359 argp->type = M4_SYMBOL_FUNC;
360 argp->u.builtin = func;
363 return type == M4_TOKEN_COMMA;
365 /* fallthru */
366 case M4_TOKEN_OPEN:
367 case M4_TOKEN_SIMPLE:
368 if (type == M4_TOKEN_OPEN)
369 paren_level++;
370 else if (type == M4_TOKEN_CLOSE)
371 paren_level--;
372 if (!expand_token (context, obs, type, &token, line, first))
373 age = 0;
374 break;
376 case M4_TOKEN_EOF:
377 m4_error (context, EXIT_FAILURE, 0, caller,
378 _("end of file in argument list"));
379 break;
381 case M4_TOKEN_WORD:
382 case M4_TOKEN_SPACE:
383 case M4_TOKEN_STRING:
384 case M4_TOKEN_COMMENT:
385 case M4_TOKEN_MACDEF:
386 if (!expand_token (context, obs, type, &token, line, first))
387 age = 0;
388 if (token.type == M4_SYMBOL_COMP)
390 if (argp->type != M4_SYMBOL_COMP)
392 argp->type = M4_SYMBOL_COMP;
393 argp->u.u_c.chain = token.u.u_c.chain;
394 argp->u.u_c.wrapper = argp->u.u_c.has_func = false;
396 else
398 assert (argp->u.u_c.end);
399 argp->u.u_c.end->next = token.u.u_c.chain;
401 argp->u.u_c.end = token.u.u_c.end;
402 if (token.u.u_c.has_func)
403 argp->u.u_c.has_func = true;
405 break;
407 case M4_TOKEN_ARGV:
408 assert (paren_level == 0 && argp->type == M4_SYMBOL_VOID
409 && obstack_object_size (obs) == 0
410 && token.u.u_c.chain == token.u.u_c.end
411 && token.u.u_c.chain->quote_age == age
412 && token.u.u_c.chain->type == M4__CHAIN_ARGV);
413 argp->type = M4_SYMBOL_COMP;
414 argp->u.u_c.chain = argp->u.u_c.end = token.u.u_c.chain;
415 argp->u.u_c.wrapper = true;
416 argp->u.u_c.has_func = token.u.u_c.has_func;
417 type = m4__next_token (context, &token, NULL, NULL, false, caller);
418 if (argp->u.u_c.chain->u.u_a.skip_last)
419 assert (type == M4_TOKEN_COMMA);
420 else
421 assert (type == M4_TOKEN_COMMA || type == M4_TOKEN_CLOSE);
422 return type == M4_TOKEN_COMMA;
424 default:
425 assert (!"expand_argument");
426 abort ();
429 if (argp->type != M4_SYMBOL_VOID || obstack_object_size (obs))
430 first = false;
431 type = m4__next_token (context, &token, NULL, obs, first, caller);
436 /* The macro expansion is handled by expand_macro (). It parses the
437 arguments, using collect_arguments (), and builds a table of pointers to
438 the arguments. The arguments themselves are stored on a local obstack.
439 Expand_macro () uses m4_macro_call () to do the call of the macro.
441 Expand_macro () is potentially recursive, since it calls expand_argument
442 (), which might call expand_token (), which might call expand_macro ().
444 NAME points to storage on the token stack, so it is only valid
445 until a call to collect_arguments parses more tokens. SYMBOL is
446 the result of the symbol table lookup on NAME. */
447 static void
448 expand_macro (m4 *context, const char *name, size_t len, m4_symbol *symbol)
450 void *args_base; /* Base of stack->args on entry. */
451 void *args_scratch; /* Base of scratch space for m4_macro_call. */
452 void *argv_base; /* Base of stack->argv on entry. */
453 m4_macro_args *argv; /* Arguments to the called macro. */
454 m4_obstack *expansion; /* Collects the macro's expansion. */
455 m4_symbol_value *value; /* Original value of this macro. */
456 size_t level; /* Expansion level of this macro. */
457 m4__macro_arg_stacks *stack; /* Storage for this macro. */
458 m4_call_info info; /* Context of this macro call. */
460 /* Obstack preparation. */
461 level = context->expansion_level;
462 if (context->stacks_count <= level)
464 size_t count = context->stacks_count;
465 context->arg_stacks
466 = (m4__macro_arg_stacks *) x2nrealloc (context->arg_stacks,
467 &context->stacks_count,
468 sizeof *context->arg_stacks);
469 memset (&context->arg_stacks[count], 0,
470 sizeof *context->arg_stacks * (context->stacks_count - count));
472 stack = &context->arg_stacks[level];
473 if (!stack->args)
475 assert (!stack->refcount);
476 stack->args = (m4_obstack *) xmalloc (sizeof *stack->args);
477 stack->argv = (m4_obstack *) xmalloc (sizeof *stack->argv);
478 obstack_init (stack->args);
479 obstack_init (stack->argv);
480 stack->args_base = obstack_finish (stack->args);
481 stack->argv_base = obstack_finish (stack->argv);
483 assert (obstack_object_size (stack->args) == 0
484 && obstack_object_size (stack->argv) == 0);
485 args_base = obstack_finish (stack->args);
486 argv_base = obstack_finish (stack->argv);
487 m4__adjust_refcount (context, level, true);
488 stack->argcount++;
490 /* Grab the current value of this macro, because it may change while
491 collecting arguments. Likewise, grab any state needed during
492 tracing. */
493 value = m4_get_symbol_value (symbol);
494 info.file = m4_get_current_file (context);
495 info.line = m4_get_current_line (context);
496 info.call_id = ++macro_call_id;
497 info.trace = (m4_is_debug_bit (context, M4_DEBUG_TRACE_ALL)
498 || m4_get_symbol_traced (symbol));
499 info.debug_level = m4_get_debug_level_opt (context);
500 info.name = name;
501 info.name_len = len;
503 /* Prepare for macro expansion. */
504 VALUE_PENDING (value)++;
505 if (m4_get_nesting_limit_opt (context) < ++context->expansion_level)
506 m4_error (context, EXIT_FAILURE, 0, NULL, _("\
507 recursion limit of %zu exceeded, use -L<N> to change it"),
508 m4_get_nesting_limit_opt (context));
510 m4_trace_prepare (context, &info, value);
511 argv = collect_arguments (context, &info, symbol, stack->args, stack->argv);
512 /* Since collect_arguments can invalidate stack by reallocating
513 context->arg_stacks during a recursive expand_macro call, we must
514 reset it here. */
515 stack = &context->arg_stacks[level];
516 args_scratch = obstack_finish (stack->args);
518 /* The actual macro call. */
519 expansion = m4_push_string_init (context, info.file, info.line);
520 m4_macro_call (context, value, expansion, argv);
521 m4_push_string_finish ();
523 /* Cleanup. */
524 argv->info = NULL;
526 --context->expansion_level;
527 --VALUE_PENDING (value);
528 if (BIT_TEST (VALUE_FLAGS (value), VALUE_DELETED_BIT))
529 m4_symbol_value_delete (value);
531 /* We no longer need argv, so reduce the refcount. Additionally, if
532 no other references to argv were created, we can free our portion
533 of the obstack, although we must leave earlier content alone. A
534 refcount of 0 implies that adjust_refcount already freed the
535 entire stack. */
536 m4__arg_adjust_refcount (context, argv, false);
537 if (stack->refcount)
539 if (argv->inuse)
541 obstack_free (stack->args, args_scratch);
542 if (debug_macro_level & PRINT_ARGCOUNT_CHANGES)
543 xfprintf (stderr, "m4debug: -%zu- `%s' in use, level=%zu, "
544 "refcount=%zu, argcount=%zu\n", info.call_id,
545 argv->info->name, level, stack->refcount,
546 stack->argcount);
548 else
550 obstack_free (stack->args, args_base);
551 obstack_free (stack->argv, argv_base);
552 stack->argcount--;
557 /* Collect all the arguments to a call of the macro SYMBOL, with call
558 context INFO. The arguments are stored on the obstack ARGUMENTS
559 and a table of pointers to the arguments on ARGV_STACK. Return the
560 object describing all of the macro arguments. */
561 static m4_macro_args *
562 collect_arguments (m4 *context, m4_call_info *info, m4_symbol *symbol,
563 m4_obstack *arguments, m4_obstack *argv_stack)
565 m4_symbol_value token;
566 m4_symbol_value *tokenp;
567 bool more_args;
568 m4_macro_args args;
569 m4_macro_args *argv;
571 args.argc = 1;
572 args.inuse = false;
573 args.wrapper = false;
574 args.has_ref = false;
575 args.flatten = m4_symbol_flatten_args (symbol);
576 args.has_func = false;
577 /* Must copy here, since we are consuming tokens, and since symbol
578 table can be changed during argument collection. */
579 info->name = (char *) obstack_copy0 (arguments, info->name, info->name_len);
580 args.quote_age = m4__quote_age (M4SYNTAX);
581 args.info = info;
582 args.level = context->expansion_level - 1;
583 args.arraylen = 0;
584 obstack_grow (argv_stack, &args, offsetof (m4_macro_args, array));
586 if (m4__next_token_is_open (context))
588 /* Gobble parenthesis, then collect arguments. */
589 m4__next_token (context, &token, NULL, NULL, false, info);
592 tokenp = (m4_symbol_value *) obstack_alloc (arguments,
593 sizeof *tokenp);
594 more_args = expand_argument (context, arguments, tokenp, info);
596 if ((m4_is_symbol_value_text (tokenp)
597 && !m4_get_symbol_value_len (tokenp))
598 || (args.flatten && m4_is_symbol_value_func (tokenp)))
600 obstack_free (arguments, tokenp);
601 tokenp = &empty_symbol;
603 obstack_ptr_grow (argv_stack, tokenp);
604 args.arraylen++;
605 args.argc++;
606 switch (tokenp->type)
608 case M4_SYMBOL_TEXT:
609 /* Be conservative - any change in quoting while
610 collecting arguments, or any unsafe argument, will
611 require a rescan if $@ is reused. */
612 if (m4_get_symbol_value_len (tokenp)
613 && m4_get_symbol_value_quote_age (tokenp) != args.quote_age)
614 args.quote_age = 0;
615 break;
616 case M4_SYMBOL_FUNC:
617 args.has_func = true;
618 break;
619 case M4_SYMBOL_COMP:
620 args.has_ref = true;
621 if (tokenp->u.u_c.wrapper)
623 assert (tokenp->u.u_c.chain->type == M4__CHAIN_ARGV
624 && !tokenp->u.u_c.chain->next);
625 args.argc += (tokenp->u.u_c.chain->u.u_a.argv->argc
626 - tokenp->u.u_c.chain->u.u_a.index
627 - tokenp->u.u_c.chain->u.u_a.skip_last - 1);
628 args.wrapper = true;
630 if (tokenp->u.u_c.has_func)
631 args.has_func = true;
632 break;
633 default:
634 assert (!"expand_argument");
635 abort ();
638 while (more_args);
640 argv = (m4_macro_args *) obstack_finish (argv_stack);
641 argv->argc = args.argc;
642 argv->wrapper = args.wrapper;
643 argv->has_ref = args.has_ref;
644 argv->has_func = args.has_func;
645 if (args.quote_age != m4__quote_age (M4SYNTAX))
646 argv->quote_age = 0;
647 argv->arraylen = args.arraylen;
648 return argv;
652 /* The actual call of a macro is handled by m4_macro_call ().
653 m4_macro_call () is passed a symbol VALUE, whose type is used to
654 call either a builtin function, or the user macro expansion
655 function process_macro (). The arguments are provided by the ARGV
656 table. The expansion is left on the obstack EXPANSION. Macro
657 tracing is also handled here. */
658 void
659 m4_macro_call (m4 *context, m4_symbol_value *value, m4_obstack *expansion,
660 m4_macro_args *argv)
662 unsigned int trace_start = 0;
664 if (argv->info->trace)
665 trace_start = trace_pre (context, argv);
666 if (!m4_bad_argc (context, argv->argc, argv->info,
667 VALUE_MIN_ARGS (value), VALUE_MAX_ARGS (value),
668 BIT_TEST (VALUE_FLAGS (value),
669 VALUE_SIDE_EFFECT_ARGS_BIT)))
671 if (m4_is_symbol_value_text (value))
672 process_macro (context, value, expansion, argv->argc, argv);
673 else if (m4_is_symbol_value_func (value))
674 m4_get_symbol_value_func (value) (context, expansion, argv->argc,
675 argv);
676 else if (m4_is_symbol_value_placeholder (value))
677 m4_warn (context, 0, argv->info,
678 _("builtin %s requested by frozen file not found"),
679 quotearg_style (locale_quoting_style,
680 m4_get_symbol_value_placeholder (value)));
681 else
683 assert (!"m4_macro_call");
684 abort ();
687 if (argv->info->trace)
688 trace_post (context, trace_start, argv->info);
691 /* This function handles all expansion of user defined and predefined
692 macros. It is called with an obstack OBS, where the macros expansion
693 will be placed, as an unfinished object. SYMBOL points to the macro
694 definition, giving the expansion text. ARGC and ARGV are the arguments,
695 as usual. */
696 static void
697 process_macro (m4 *context, m4_symbol_value *value, m4_obstack *obs,
698 int argc, m4_macro_args *argv)
700 const char *text = m4_get_symbol_value_text (value);
701 size_t len = m4_get_symbol_value_len (value);
702 const char *end = text + len;
703 int i;
704 while (1)
706 const char *dollar;
707 if (m4_is_syntax_single_dollar (M4SYNTAX))
708 dollar = (char *) memchr (text, M4SYNTAX->dollar, len);
709 else
711 dollar = text;
712 while (dollar != end)
714 if (m4_has_syntax (M4SYNTAX, *dollar, M4_SYNTAX_DOLLAR))
715 break;
716 dollar++;
718 if (dollar == end)
719 dollar = NULL;
721 if (!dollar)
723 obstack_grow (obs, text, len);
724 return;
726 obstack_grow (obs, text, dollar - text);
727 len -= dollar - text;
728 text = dollar;
729 if (len == 1)
731 obstack_1grow (obs, *dollar);
732 return;
734 len--;
735 switch (*++text)
737 case '0': case '1': case '2': case '3': case '4':
738 case '5': case '6': case '7': case '8': case '9':
739 /* FIXME - multidigit arguments should convert over to ${10}
740 syntax instead of $10; see
741 http://lists.gnu.org/archive/html/m4-discuss/2006-08/msg00028.html
742 for more discussion. */
743 if (m4_get_posixly_correct_opt (context) || !isdigit(text[1]))
745 i = *text++ - '0';
746 len--;
748 else
750 char *endp;
751 i = (int) strtol (text, &endp, 10);
752 len -= endp - text;
753 text = endp;
755 if (i < argc)
756 m4_push_arg (context, obs, argv, i);
757 break;
759 case '#': /* number of arguments */
760 m4_shipout_int (obs, argc - 1);
761 text++;
762 len--;
763 break;
765 case '*': /* all arguments */
766 case '@': /* ... same, but quoted */
767 m4_push_args (context, obs, argv, false, *text == '@');
768 text++;
769 len--;
770 break;
772 default:
773 if (m4_get_posixly_correct_opt (context)
774 || !VALUE_ARG_SIGNATURE (value))
776 obstack_1grow (obs, *dollar);
778 else
780 size_t len1 = 0;
781 const char *endp;
782 char *key;
784 for (endp = ++text;
785 len1 < len && m4_has_syntax (M4SYNTAX, *endp,
786 (M4_SYNTAX_OTHER
787 | M4_SYNTAX_ALPHA
788 | M4_SYNTAX_NUM));
789 ++endp)
791 ++len1;
793 key = xstrndup (text, len1);
795 if (*endp)
797 struct m4_symbol_arg **arg
798 = (struct m4_symbol_arg **)
799 m4_hash_lookup (VALUE_ARG_SIGNATURE (value), key);
801 if (arg)
803 i = SYMBOL_ARG_INDEX (*arg);
804 assert (i < argc);
805 m4_shipout_string (context, obs, M4ARG (i), M4ARGLEN (i),
806 false);
809 else
811 m4_error (context, 0, 0, argv->info,
812 _("unterminated parameter reference: %s"), key);
815 len -= endp - text;
816 text = endp;
818 free (key);
820 break;
827 /* The next portion of this file contains the functions for macro
828 tracing output. All tracing output for a macro call is collected
829 on an obstack TRACE, and printed whenever the line is complete.
830 This prevents tracing output from interfering with other debug
831 messages generated by the various builtins. */
833 /* Format the standard header attached to all tracing output lines,
834 using the context in INFO as appropriate. Return the offset into
835 the trace obstack where this particular trace begins. */
836 static unsigned int
837 trace_header (m4 *context, const m4_call_info *info)
839 m4_obstack *trace = &context->trace_messages;
840 unsigned int result = obstack_object_size (trace);
841 obstack_grow (trace, "m4trace:", 8);
842 if (info->debug_level & M4_DEBUG_TRACE_FILE)
843 obstack_printf (trace, "%s:", info->file);
844 if (info->debug_level & M4_DEBUG_TRACE_LINE)
845 obstack_printf (trace, "%d:", info->line);
846 obstack_printf (trace, " -%zu- ", context->expansion_level);
847 if (info->debug_level & M4_DEBUG_TRACE_CALLID)
848 obstack_printf (trace, "id %zu: ", info->call_id);
849 return result;
852 /* Print current tracing line starting at offset START, as returned
853 from an earlier trace_header(), then clear the obstack. */
854 static void
855 trace_flush (m4 *context, unsigned int start)
857 char *str;
858 size_t len = obstack_object_size (&context->trace_messages);
859 FILE *file = m4_get_debug_file (context);
861 if (file)
863 /* TODO - quote nonprintable characters if debug is tty? */
864 str = (char *) obstack_base (&context->trace_messages);
865 fwrite (&str[start], 1, len - start, file);
866 fputc ('\n', file);
868 obstack_blank (&context->trace_messages, start - len);
871 /* Do pre-argument-collection tracing for the macro described in INFO.
872 Should be called prior to m4_macro_call(). */
873 void
874 m4_trace_prepare (m4 *context, const m4_call_info *info,
875 m4_symbol_value *value)
877 const m4_string_pair *quotes = NULL;
878 size_t arg_length = m4_get_max_debug_arg_length_opt (context);
879 bool module = (info->debug_level & M4_DEBUG_TRACE_MODULE) != 0;
881 if (info->debug_level & M4_DEBUG_TRACE_QUOTE)
882 quotes = m4_get_syntax_quotes (M4SYNTAX);
883 if (info->trace && (info->debug_level & M4_DEBUG_TRACE_CALL))
885 unsigned int start = trace_header (context, info);
886 obstack_grow (&context->trace_messages, info->name, info->name_len);
887 obstack_grow (&context->trace_messages, " ... = ", 7);
888 m4__symbol_value_print (context, value, &context->trace_messages, quotes,
889 false, NULL, &arg_length, module);
890 trace_flush (context, start);
894 /* Format the parts of a trace line that are known via ARGV before the
895 macro is actually expanded. Used from m4_macro_call(). Return the
896 start of the current trace, in case other traces are printed before
897 this trace completes trace_post. */
898 static unsigned int
899 trace_pre (m4 *context, m4_macro_args *argv)
901 int trace_level = argv->info->debug_level;
902 unsigned int start = trace_header (context, argv->info);
903 m4_obstack *trace = &context->trace_messages;
905 assert (argv->info->trace);
906 obstack_grow (trace, argv->info->name, argv->info->name_len);
908 if (1 < m4_arg_argc (argv) && (trace_level & M4_DEBUG_TRACE_ARGS))
910 const m4_string_pair *quotes = NULL;
911 size_t arg_length = m4_get_max_debug_arg_length_opt (context);
912 bool module = (trace_level & M4_DEBUG_TRACE_MODULE) != 0;
914 if (trace_level & M4_DEBUG_TRACE_QUOTE)
915 quotes = m4_get_syntax_quotes (M4SYNTAX);
916 obstack_1grow (trace, '(');
917 m4__arg_print (context, trace, argv, 1, quotes, false, NULL, ", ",
918 &arg_length, true, module);
919 obstack_1grow (trace, ')');
921 return start;
924 /* If requested by the trace state in INFO, format the final part of a
925 trace line. Then print all collected information from START,
926 returned from a prior trace_pre(). Used from m4_macro_call (). */
927 static void
928 trace_post (m4 *context, unsigned int start, const m4_call_info *info)
930 assert (info->trace);
931 if (info->debug_level & M4_DEBUG_TRACE_EXPANSION)
933 obstack_grow (&context->trace_messages, " -> ", 4);
934 m4_input_print (context, &context->trace_messages, info->debug_level);
936 trace_flush (context, start);
940 /* Accessors into m4_macro_args. */
942 /* Adjust the refcount of argument stack LEVEL. If INCREASE, then
943 increase the count, otherwise decrease the count and clear the
944 entire stack if the new count is zero. Return the new
945 refcount. */
946 size_t
947 m4__adjust_refcount (m4 *context, size_t level, bool increase)
949 m4__macro_arg_stacks *stack = &context->arg_stacks[level];
950 assert (level < context->stacks_count && stack->args
951 && (increase || stack->refcount));
952 if (increase)
953 stack->refcount++;
954 else if (--stack->refcount == 0)
956 obstack_free (stack->args, stack->args_base);
957 obstack_free (stack->argv, stack->argv_base);
958 if ((debug_macro_level & PRINT_ARGCOUNT_CHANGES) && 1 < stack->argcount)
959 xfprintf (stderr, "m4debug: -%zu- freeing %zu args, level=%zu\n",
960 macro_call_id, stack->argcount, level);
961 stack->argcount = 0;
963 if (debug_macro_level
964 & (increase ? PRINT_REFCOUNT_INCREASE : PRINT_REFCOUNT_DECREASE))
965 xfprintf (stderr, "m4debug: level %zu refcount=%zu\n", level,
966 stack->refcount);
967 return stack->refcount;
970 /* Given ARGV, adjust the refcount of every reference it contains in
971 the direction decided by INCREASE. Return true if increasing
972 references to ARGV implies the first use of ARGV. */
973 bool
974 m4__arg_adjust_refcount (m4 *context, m4_macro_args *argv, bool increase)
976 size_t i;
977 m4__symbol_chain *chain;
978 bool result = !argv->inuse;
980 if (argv->has_ref)
981 for (i = 0; i < argv->arraylen; i++)
982 if (argv->array[i]->type == M4_SYMBOL_COMP)
984 chain = argv->array[i]->u.u_c.chain;
985 while (chain)
987 switch (chain->type)
989 case M4__CHAIN_STR:
990 if (chain->u.u_s.level < SIZE_MAX)
991 m4__adjust_refcount (context, chain->u.u_s.level,
992 increase);
993 break;
994 case M4__CHAIN_FUNC:
995 break;
996 case M4__CHAIN_ARGV:
997 assert (chain->u.u_a.argv->inuse);
998 m4__arg_adjust_refcount (context, chain->u.u_a.argv,
999 increase);
1000 break;
1001 default:
1002 assert (!"m4__arg_adjust_refcount");
1003 abort ();
1005 chain = chain->next;
1008 m4__adjust_refcount (context, argv->level, increase);
1009 return result;
1012 /* Mark ARGV as being in use, along with any $@ references that it
1013 wraps. */
1014 static void
1015 arg_mark (m4_macro_args *argv)
1017 size_t i;
1018 m4__symbol_chain *chain;
1020 if (argv->inuse)
1021 return;
1022 argv->inuse = true;
1023 if (argv->wrapper)
1025 for (i = 0; i < argv->arraylen; i++)
1026 if (argv->array[i]->type == M4_SYMBOL_COMP
1027 && argv->array[i]->u.u_c.wrapper)
1029 chain = argv->array[i]->u.u_c.chain;
1030 assert (!chain->next && chain->type == M4__CHAIN_ARGV);
1031 if (!chain->u.u_a.argv->inuse)
1032 arg_mark (chain->u.u_a.argv);
1037 /* Populate the newly-allocated VALUE as a wrapper around ARGV,
1038 starting with argument ARG. Allocate any data on OBS, owned by a
1039 given expansion LEVEL. FLATTEN determines whether to allow
1040 builtins, and QUOTES determines whether all arguments are quoted.
1041 Return TOKEN when successful, NULL when wrapping ARGV is trivially
1042 empty. */
1043 static m4_symbol_value *
1044 make_argv_ref (m4 *context, m4_symbol_value *value, m4_obstack *obs,
1045 size_t level, m4_macro_args *argv, size_t arg, bool flatten,
1046 const m4_string_pair *quotes)
1048 m4__symbol_chain *chain;
1050 if (argv->argc <= arg)
1051 return NULL;
1052 value->type = M4_SYMBOL_COMP;
1053 value->u.u_c.chain = value->u.u_c.end = NULL;
1055 /* Cater to the common idiom of $0(`$1',shift(shift($@))), by
1056 inlining the first few arguments and reusing the original $@ ref,
1057 rather than creating another layer of wrappers. */
1058 while (argv->wrapper)
1060 size_t i;
1061 for (i = 0; i < argv->arraylen; i++)
1063 if ((argv->array[i]->type == M4_SYMBOL_COMP
1064 && argv->array[i]->u.u_c.wrapper)
1065 || level < SIZE_MAX)
1066 break;
1067 if (arg == 1)
1069 m4__push_arg_quote (context, obs, argv, i + 1, quotes);
1070 /* TODO support M4_SYNTAX_COMMA. */
1071 obstack_1grow (obs, ',');
1073 else
1074 arg--;
1076 assert (i < argv->arraylen);
1077 if (i + 1 == argv->arraylen)
1079 assert (argv->array[i]->type == M4_SYMBOL_COMP
1080 && argv->array[i]->u.u_c.wrapper);
1081 chain = argv->array[i]->u.u_c.chain;
1082 assert (!chain->next && chain->type == M4__CHAIN_ARGV
1083 && !chain->u.u_a.skip_last);
1084 argv = chain->u.u_a.argv;
1085 arg += chain->u.u_a.index - 1;
1087 else
1089 arg += i;
1090 break;
1094 m4__make_text_link (obs, &value->u.u_c.chain, &value->u.u_c.end);
1095 chain = (m4__symbol_chain *) obstack_alloc (obs, sizeof *chain);
1096 if (value->u.u_c.end)
1097 value->u.u_c.end->next = chain;
1098 else
1099 value->u.u_c.chain = chain;
1100 value->u.u_c.end = chain;
1101 value->u.u_c.wrapper = true;
1102 value->u.u_c.has_func = argv->has_func;
1103 chain->next = NULL;
1104 chain->type = M4__CHAIN_ARGV;
1105 chain->quote_age = argv->quote_age;
1106 chain->u.u_a.argv = argv;
1107 chain->u.u_a.index = arg;
1108 chain->u.u_a.flatten = flatten;
1109 chain->u.u_a.has_func = argv->has_func;
1110 chain->u.u_a.comma = false;
1111 chain->u.u_a.skip_last = false;
1112 chain->u.u_a.quotes = m4__quote_cache (M4SYNTAX, obs, chain->quote_age,
1113 quotes);
1114 return value;
1117 /* Given ARGV, return the symbol value at the specified ARG, which
1118 must be non-zero. *LEVEL is set to the obstack level that contains
1119 the symbol (which is not necessarily the level of ARGV). If
1120 FLATTEN, avoid returning a builtin token. */
1121 static m4_symbol_value *
1122 arg_symbol (m4_macro_args *argv, size_t arg, size_t *level, bool flatten)
1124 size_t i;
1125 m4_symbol_value *value;
1127 assert (arg);
1128 if (level)
1129 *level = argv->level;
1130 flatten |= argv->flatten;
1131 if (argv->argc <= arg)
1132 return &empty_symbol;
1133 if (!argv->wrapper)
1135 value = argv->array[arg - 1];
1136 if (flatten && m4_is_symbol_value_func (value))
1137 value = &empty_symbol;
1138 return value;
1141 /* Must cycle through all array slots until we find arg, since
1142 wrappers can contain multiple arguments. */
1143 for (i = 0; i < argv->arraylen; i++)
1145 value = argv->array[i];
1146 if (value->type == M4_SYMBOL_COMP && value->u.u_c.wrapper)
1148 m4__symbol_chain *chain = value->u.u_c.chain;
1149 assert (!chain->next && chain->type == M4__CHAIN_ARGV);
1150 if (arg <= (chain->u.u_a.argv->argc - chain->u.u_a.index
1151 - chain->u.u_a.skip_last))
1153 value = arg_symbol (chain->u.u_a.argv,
1154 chain->u.u_a.index - 1 + arg, level,
1155 flatten || chain->u.u_a.flatten);
1156 break;
1158 arg -= (chain->u.u_a.argv->argc - chain->u.u_a.index
1159 - chain->u.u_a.skip_last);
1161 else if (--arg == 0)
1162 break;
1164 return value;
1167 /* Given ARGV, return the symbol value at the specified ARG, which
1168 must be non-zero. */
1169 m4_symbol_value *
1170 m4_arg_symbol (m4_macro_args *argv, size_t arg)
1172 return arg_symbol (argv, arg, NULL, false);
1175 /* Given ARGV, return true if argument ARG is text. Arg 0 is always
1176 text, as are indices beyond argc. */
1177 bool
1178 m4_is_arg_text (m4_macro_args *argv, size_t arg)
1180 m4_symbol_value *value;
1181 if (arg == 0 || argv->argc <= arg || argv->flatten || !argv->has_func)
1182 return true;
1183 value = m4_arg_symbol (argv, arg);
1184 if (m4_is_symbol_value_text (value)
1185 || (value->type == M4_SYMBOL_COMP && !value->u.u_c.has_func))
1186 return true;
1187 return false;
1190 /* Given ARGV, return true if argument ARG is a single builtin
1191 function. Only non-zero indices less than argc can return
1192 true. */
1193 bool
1194 m4_is_arg_func (m4_macro_args *argv, size_t arg)
1196 if (arg == 0 || argv->argc <= arg || argv->flatten || !argv->has_func)
1197 return false;
1198 return m4_is_symbol_value_func (m4_arg_symbol (argv, arg));
1201 /* Given ARGV, return true if argument ARG contains a builtin token
1202 concatenated with anything else. Only non-zero indices less than
1203 argc can return true. */
1204 bool
1205 m4_is_arg_composite (m4_macro_args *argv, size_t arg)
1207 m4_symbol_value *value;
1208 if (arg == 0 || argv->argc <= arg || argv->flatten || !argv->has_func)
1209 return false;
1210 value = m4_arg_symbol (argv, arg);
1211 if (value->type == M4_SYMBOL_COMP && value->u.u_c.has_func)
1212 return true;
1213 return false;
1216 /* Given ARGV, return the text at argument ARG. Abort if the argument
1217 is not text. Arg 0 is always text, and indices beyond argc return
1218 the empty string. If FLATTEN, builtins are ignored. The result is
1219 always NUL-terminated, even if it includes embedded NUL
1220 characters. */
1221 const char *
1222 m4_arg_text (m4 *context, m4_macro_args *argv, size_t arg, bool flatten)
1224 m4_symbol_value *value;
1225 m4__symbol_chain *chain;
1226 m4_obstack *obs;
1228 if (arg == 0)
1230 assert (argv->info);
1231 return argv->info->name;
1233 if (argv->argc <= arg)
1234 return "";
1235 value = arg_symbol (argv, arg, NULL, flatten);
1236 if (m4_is_symbol_value_text (value))
1237 return m4_get_symbol_value_text (value);
1238 assert (value->type == M4_SYMBOL_COMP);
1239 chain = value->u.u_c.chain;
1240 obs = m4_arg_scratch (context);
1241 while (chain)
1243 switch (chain->type)
1245 case M4__CHAIN_STR:
1246 obstack_grow (obs, chain->u.u_s.str, chain->u.u_s.len);
1247 break;
1248 case M4__CHAIN_FUNC:
1249 if (flatten)
1250 break;
1251 assert (!"m4_arg_text");
1252 abort ();
1253 case M4__CHAIN_ARGV:
1254 assert (!chain->u.u_a.has_func || flatten || argv->flatten);
1255 m4__arg_print (context, obs, chain->u.u_a.argv, chain->u.u_a.index,
1256 m4__quote_cache (M4SYNTAX, NULL, chain->quote_age,
1257 chain->u.u_a.quotes),
1258 flatten || argv->flatten || chain->u.u_a.flatten,
1259 NULL, NULL, NULL, false, false);
1260 break;
1261 default:
1262 assert (!"m4_arg_text");
1263 abort ();
1265 chain = chain->next;
1267 obstack_1grow (obs, '\0');
1268 return (char *) obstack_finish (obs);
1271 /* Given ARGV, compare text arguments INDEXA and INDEXB for equality.
1272 Both indices must be non-zero. Return true if the arguments
1273 contain the same contents; often more efficient than
1274 !strcmp (m4_arg_text (context, argv, indexa),
1275 m4_arg_text (context, argv, indexb)). */
1276 bool
1277 m4_arg_equal (m4 *context, m4_macro_args *argv, size_t indexa, size_t indexb)
1279 m4_symbol_value *sa = m4_arg_symbol (argv, indexa);
1280 m4_symbol_value *sb = m4_arg_symbol (argv, indexb);
1281 m4__symbol_chain tmpa;
1282 m4__symbol_chain tmpb;
1283 m4__symbol_chain *ca = &tmpa;
1284 m4__symbol_chain *cb = &tmpb;
1285 m4__symbol_chain *chain;
1286 m4_obstack *obs = m4_arg_scratch (context);
1288 /* Quick tests. */
1289 if (sa == &empty_symbol || sb == &empty_symbol)
1290 return sa == sb;
1291 if (m4_is_symbol_value_text (sa) && m4_is_symbol_value_text (sb))
1292 return (m4_get_symbol_value_len (sa) == m4_get_symbol_value_len (sb)
1293 && memcmp (m4_get_symbol_value_text (sa),
1294 m4_get_symbol_value_text (sb),
1295 m4_get_symbol_value_len (sa)) == 0);
1297 /* Convert both arguments to chains, if not one already. */
1298 switch (sa->type)
1300 case M4_SYMBOL_TEXT:
1301 tmpa.next = NULL;
1302 tmpa.type = M4__CHAIN_STR;
1303 tmpa.u.u_s.str = m4_get_symbol_value_text (sa);
1304 tmpa.u.u_s.len = m4_get_symbol_value_len (sa);
1305 break;
1306 case M4_SYMBOL_FUNC:
1307 tmpa.next = NULL;
1308 tmpa.type = M4__CHAIN_FUNC;
1309 tmpa.u.builtin = sa->u.builtin;
1310 break;
1311 case M4_SYMBOL_COMP:
1312 ca = sa->u.u_c.chain;
1313 break;
1314 default:
1315 assert (!"m4_arg_equal");
1316 abort ();
1318 switch (sb->type)
1320 case M4_SYMBOL_TEXT:
1321 tmpb.next = NULL;
1322 tmpb.type = M4__CHAIN_STR;
1323 tmpb.u.u_s.str = m4_get_symbol_value_text (sb);
1324 tmpb.u.u_s.len = m4_get_symbol_value_len (sb);
1325 break;
1326 case M4_SYMBOL_FUNC:
1327 tmpb.next = NULL;
1328 tmpb.type = M4__CHAIN_FUNC;
1329 tmpb.u.builtin = sb->u.builtin;
1330 break;
1331 case M4_SYMBOL_COMP:
1332 cb = sb->u.u_c.chain;
1333 break;
1334 default:
1335 assert (!"m4_arg_equal");
1336 abort ();
1339 /* Compare each link of the chain. */
1340 while (ca && cb)
1342 if (ca->type == M4__CHAIN_ARGV)
1344 tmpa.next = NULL;
1345 tmpa.type = M4__CHAIN_STR;
1346 tmpa.u.u_s.str = NULL;
1347 tmpa.u.u_s.len = 0;
1348 chain = &tmpa;
1349 m4__arg_print (context, obs, ca->u.u_a.argv, ca->u.u_a.index,
1350 m4__quote_cache (M4SYNTAX, NULL, ca->quote_age,
1351 ca->u.u_a.quotes),
1352 argv->flatten || ca->u.u_a.flatten, &chain, NULL,
1353 NULL, false, false);
1354 assert (obstack_object_size (obs) == 0 && chain != &tmpa);
1355 chain->next = ca->next;
1356 ca = tmpa.next;
1357 continue;
1359 if (cb->type == M4__CHAIN_ARGV)
1361 tmpb.next = NULL;
1362 tmpb.type = M4__CHAIN_STR;
1363 tmpb.u.u_s.str = NULL;
1364 tmpb.u.u_s.len = 0;
1365 chain = &tmpb;
1366 m4__arg_print (context, obs, cb->u.u_a.argv, cb->u.u_a.index,
1367 m4__quote_cache (M4SYNTAX, NULL, cb->quote_age,
1368 cb->u.u_a.quotes),
1369 argv->flatten || cb->u.u_a.flatten, &chain, NULL,
1370 NULL, false, false);
1371 assert (obstack_object_size (obs) == 0 && chain != &tmpb);
1372 chain->next = cb->next;
1373 cb = tmpb.next;
1374 continue;
1376 if (ca->type == M4__CHAIN_FUNC)
1378 if (cb->type != M4__CHAIN_FUNC || ca->u.builtin != cb->u.builtin)
1379 return false;
1380 ca = ca->next;
1381 cb = cb->next;
1382 continue;
1384 assert (ca->type == M4__CHAIN_STR && cb->type == M4__CHAIN_STR);
1385 if (ca->u.u_s.len == cb->u.u_s.len)
1387 if (memcmp (ca->u.u_s.str, cb->u.u_s.str, ca->u.u_s.len) != 0)
1388 return false;
1389 ca = ca->next;
1390 cb = cb->next;
1392 else if (ca->u.u_s.len < cb->u.u_s.len)
1394 if (memcmp (ca->u.u_s.str, cb->u.u_s.str, ca->u.u_s.len) != 0)
1395 return false;
1396 tmpb.next = cb->next;
1397 tmpb.u.u_s.str = cb->u.u_s.str + ca->u.u_s.len;
1398 tmpb.u.u_s.len = cb->u.u_s.len - ca->u.u_s.len;
1399 ca = ca->next;
1400 cb = &tmpb;
1402 else
1404 assert (cb->u.u_s.len < ca->u.u_s.len);
1405 if (memcmp (ca->u.u_s.str, cb->u.u_s.str, cb->u.u_s.len) != 0)
1406 return false;
1407 tmpa.next = ca->next;
1408 tmpa.u.u_s.str = ca->u.u_s.str + cb->u.u_s.len;
1409 tmpa.u.u_s.len = ca->u.u_s.len - cb->u.u_s.len;
1410 ca = &tmpa;
1411 cb = cb->next;
1415 /* If we get this far, the two arguments are equal only if both
1416 chains are exhausted. */
1417 assert (ca != cb || !ca);
1418 return ca == cb;
1421 /* Given ARGV, return true if argument ARG is the empty string. This
1422 gives the same result as comparing m4_arg_len against 0, but is
1423 often faster. */
1424 bool
1425 m4_arg_empty (m4_macro_args *argv, size_t arg)
1427 if (!arg)
1429 assert (argv->info);
1430 return !argv->info->name_len;
1432 return m4_arg_symbol (argv, arg) == &empty_symbol;
1435 /* Given ARGV, return the length of argument ARG. Abort if the
1436 argument is not text and FLATTEN is not true. Indices beyond argc
1437 return 0. */
1438 size_t
1439 m4_arg_len (m4 *context, m4_macro_args *argv, size_t arg, bool flatten)
1441 m4_symbol_value *value;
1442 m4__symbol_chain *chain;
1443 size_t len;
1445 if (arg == 0)
1447 assert (argv->info);
1448 return argv->info->name_len;
1450 if (argv->argc <= arg)
1451 return 0;
1452 value = arg_symbol (argv, arg, NULL, flatten);
1453 if (m4_is_symbol_value_text (value))
1454 return m4_get_symbol_value_len (value);
1455 assert (value->type == M4_SYMBOL_COMP);
1456 chain = value->u.u_c.chain;
1457 len = 0;
1458 while (chain)
1460 size_t i;
1461 size_t limit;
1462 const m4_string_pair *quotes;
1463 switch (chain->type)
1465 case M4__CHAIN_STR:
1466 len += chain->u.u_s.len;
1467 break;
1468 case M4__CHAIN_FUNC:
1469 assert (flatten);
1470 break;
1471 case M4__CHAIN_ARGV:
1472 i = chain->u.u_a.index;
1473 limit = chain->u.u_a.argv->argc - i - chain->u.u_a.skip_last;
1474 quotes = m4__quote_cache (M4SYNTAX, NULL, chain->quote_age,
1475 chain->u.u_a.quotes);
1476 assert (limit);
1477 if (quotes)
1478 len += (quotes->len1 + quotes->len2) * limit;
1479 len += limit - 1;
1480 while (limit--)
1481 len += m4_arg_len (context, chain->u.u_a.argv, i++,
1482 flatten || chain->u.u_a.flatten);
1483 break;
1484 default:
1485 assert (!"m4_arg_len");
1486 abort ();
1488 chain = chain->next;
1490 assert (len || flatten);
1491 return len;
1494 /* Given ARGV, return the builtin function referenced by argument ARG.
1495 Abort if it is not a single builtin. */
1496 m4_builtin_func *
1497 m4_arg_func (m4_macro_args *argv, size_t arg)
1499 return m4_get_symbol_value_func (m4_arg_symbol (argv, arg));
1502 /* Dump a representation of ARGV to the obstack OBS, starting with
1503 argument ARG. If QUOTES is non-NULL, each argument is displayed
1504 with those quotes. If FLATTEN, builtins are converted to empty
1505 quotes; if CHAINP, *CHAINP is updated with macro tokens; otherwise,
1506 builtins are represented by their name. Separate arguments with
1507 SEP, which defaults to a comma. If MAX_LEN is non-NULL, truncate
1508 the output after *MAX_LEN bytes are output and return true;
1509 otherwise, return false, and reduce *MAX_LEN by the number of bytes
1510 output. If QUOTE_EACH, the truncation length is reset for each
1511 argument, quotes do not count against length, and all arguments are
1512 printed; otherwise, quotes count against the length and trailing
1513 arguments may be discarded. If MODULE, print any details about
1514 originating modules; modules do not count against truncation
1515 length. MAX_LEN and CHAINP may not both be specified. */
1516 bool
1517 m4__arg_print (m4 *context, m4_obstack *obs, m4_macro_args *argv, size_t arg,
1518 const m4_string_pair *quotes, bool flatten,
1519 m4__symbol_chain **chainp, const char *sep, size_t *max_len,
1520 bool quote_each, bool module)
1522 size_t len = max_len ? *max_len : SIZE_MAX;
1523 size_t i;
1524 bool use_sep = false;
1525 size_t sep_len;
1526 size_t *plen = quote_each ? NULL : &len;
1528 flatten |= argv->flatten;
1529 if (chainp)
1530 assert (!max_len && *chainp);
1531 if (!sep)
1532 sep = ",";
1533 sep_len = strlen (sep);
1534 for (i = arg; i < argv->argc; i++)
1536 if (quote_each && max_len)
1537 len = *max_len;
1538 if (use_sep && m4_shipout_string_trunc (obs, sep, sep_len, NULL, plen))
1539 return true;
1540 use_sep = true;
1541 if (quotes && !quote_each
1542 && m4_shipout_string_trunc (obs, quotes->str1, quotes->len1, NULL,
1543 plen))
1544 return true;
1545 if (m4__symbol_value_print (context, arg_symbol (argv, i, NULL, flatten),
1546 obs, quote_each ? quotes : NULL, flatten,
1547 chainp, &len, module))
1548 return true;
1549 if (quotes && !quote_each
1550 && m4_shipout_string_trunc (obs, quotes->str2, quotes->len2, NULL,
1551 plen))
1552 return true;
1554 if (max_len)
1555 *max_len = len;
1556 else if (chainp)
1557 m4__make_text_link (obs, NULL, chainp);
1558 return false;
1561 /* Create a new argument object using the same obstack as ARGV; thus,
1562 the new object will automatically be freed when the original is
1563 freed. Explicitly set the macro name (argv[0]) from ARGV0 with
1564 length ARGV0_LEN, and discard argv[1] of the wrapped ARGV. If
1565 FLATTEN, any builtins in ARGV are flattened to an empty string when
1566 referenced through the new object. If TRACE, then trace the macro
1567 regardless of global trace state. */
1568 m4_macro_args *
1569 m4_make_argv_ref (m4 *context, m4_macro_args *argv, const char *argv0,
1570 size_t argv0_len, bool flatten, bool trace)
1572 m4_macro_args *new_argv;
1573 m4_symbol_value *value;
1574 m4_symbol_value *new_value;
1575 m4_obstack *obs = m4_arg_scratch (context);
1576 m4_call_info *info;
1578 info = (m4_call_info *) obstack_copy (obs, argv->info, sizeof *info);
1579 new_value = (m4_symbol_value *) obstack_alloc (obs, sizeof *value);
1580 value = make_argv_ref (context, new_value, obs, context->expansion_level - 1,
1581 argv, 2, flatten, NULL);
1582 if (!value)
1584 obstack_free (obs, new_value);
1585 new_argv = (m4_macro_args *) obstack_alloc (obs, offsetof (m4_macro_args,
1586 array));
1587 new_argv->arraylen = 0;
1588 new_argv->wrapper = false;
1589 new_argv->has_ref = false;
1590 new_argv->flatten = false;
1591 new_argv->has_func = false;
1593 else
1595 new_argv = (m4_macro_args *) obstack_alloc (obs, (offsetof (m4_macro_args,
1596 array)
1597 + sizeof value));
1598 new_argv->arraylen = 1;
1599 new_argv->array[0] = value;
1600 new_argv->wrapper = true;
1601 new_argv->has_ref = argv->has_ref;
1602 new_argv->flatten = flatten;
1603 new_argv->has_func = argv->has_func;
1605 new_argv->argc = argv->argc - 1;
1606 new_argv->inuse = false;
1607 new_argv->quote_age = argv->quote_age;
1608 new_argv->info = info;
1609 info->trace = (argv->info->debug_level & M4_DEBUG_TRACE_ALL) || trace;
1610 info->name = argv0;
1611 info->name_len = argv0_len;
1612 new_argv->level = argv->level;
1613 return new_argv;
1616 /* Push argument ARG from ARGV, which must be a text token, onto the
1617 expansion stack OBS for rescanning. */
1618 void
1619 m4_push_arg (m4 *context, m4_obstack *obs, m4_macro_args *argv, size_t arg)
1621 m4_symbol_value value;
1623 if (arg == 0)
1625 assert (argv->info);
1626 m4_set_symbol_value_text (&value, argv->info->name, argv->info->name_len,
1628 if (m4__push_symbol (context, &value, context->expansion_level - 1,
1629 argv->inuse))
1630 arg_mark (argv);
1632 else
1633 m4__push_arg_quote (context, obs, argv, arg, NULL);
1636 /* Push argument ARG from ARGV onto the expansion stack OBS for
1637 rescanning. ARG must be non-zero. QUOTES determines any quote
1638 delimiters that were in effect when the reference was created. */
1639 void
1640 m4__push_arg_quote (m4 *context, m4_obstack *obs, m4_macro_args *argv,
1641 size_t arg, const m4_string_pair *quotes)
1643 size_t level;
1644 m4_symbol_value *value = arg_symbol (argv, arg, &level, false);
1646 if (quotes)
1647 obstack_grow (obs, quotes->str1, quotes->len1);
1648 if (value != &empty_symbol
1649 && m4__push_symbol (context, value, level, argv->inuse))
1650 arg_mark (argv);
1651 if (quotes)
1652 obstack_grow (obs, quotes->str2, quotes->len2);
1655 /* Push series of comma-separated arguments from ARGV onto the
1656 expansion stack OBS for rescanning. If SKIP, then don't push the
1657 first argument. If QUOTE, also push quoting around each arg. */
1658 void
1659 m4_push_args (m4 *context, m4_obstack *obs, m4_macro_args *argv, bool skip,
1660 bool quote)
1662 m4_symbol_value tmp;
1663 m4_symbol_value *value;
1664 size_t i = skip ? 2 : 1;
1665 const m4_string_pair *quotes = m4_get_syntax_quotes (M4SYNTAX);
1667 if (argv->argc <= i)
1668 return;
1670 if (argv->argc == i + 1)
1672 m4__push_arg_quote (context, obs, argv, i, quote ? quotes : NULL);
1673 return;
1676 value = make_argv_ref (context, &tmp, obs, -1, argv, i, argv->flatten,
1677 quote ? quotes : NULL);
1678 assert (value == &tmp);
1679 if (m4__push_symbol (context, value, -1, argv->inuse))
1680 arg_mark (argv);
1683 /* Push arguments from ARGV onto the wrap stack for later rescanning.
1684 If GNU extensions are disabled, only the first argument is pushed;
1685 otherwise, all arguments are pushed and separated with a space. */
1686 void
1687 m4_wrap_args (m4 *context, m4_macro_args *argv)
1689 size_t i;
1690 m4_obstack *obs;
1691 m4_symbol_value *value;
1692 m4__symbol_chain *chain;
1693 m4__symbol_chain **end;
1694 size_t limit = m4_get_posixly_correct_opt (context) ? 2 : argv->argc;
1696 if (limit == 2 && m4_arg_empty (argv, 1))
1697 return;
1699 obs = m4__push_wrapup_init (context, argv->info, &end);
1700 for (i = 1; i < limit; i++)
1702 if (i != 1)
1703 obstack_1grow (obs, ' ');
1704 value = m4_arg_symbol (argv, i);
1705 switch (value->type)
1707 case M4_SYMBOL_TEXT:
1708 obstack_grow (obs, m4_get_symbol_value_text (value),
1709 m4_get_symbol_value_len (value));
1710 break;
1711 case M4_SYMBOL_FUNC:
1712 m4__append_builtin (obs, value->u.builtin, NULL, end);
1713 break;
1714 case M4_SYMBOL_COMP:
1715 chain = value->u.u_c.chain;
1716 while (chain)
1718 switch (chain->type)
1720 case M4__CHAIN_STR:
1721 obstack_grow (obs, chain->u.u_s.str, chain->u.u_s.len);
1722 break;
1723 case M4__CHAIN_FUNC:
1724 m4__append_builtin (obs, chain->u.builtin, NULL, end);
1725 break;
1726 case M4__CHAIN_ARGV:
1727 m4__arg_print (context, obs, chain->u.u_a.argv,
1728 chain->u.u_a.index,
1729 m4__quote_cache (M4SYNTAX, NULL,
1730 chain->quote_age,
1731 chain->u.u_a.quotes),
1732 chain->u.u_a.flatten, end, NULL, NULL, false,
1733 false);
1734 break;
1735 default:
1736 assert (!"m4_wrap_args");
1737 abort ();
1739 chain = chain->next;
1741 break;
1742 default:
1743 assert (!"m4_wrap_args");
1744 abort ();
1747 m4__push_wrapup_finish ();
1751 /* Define these last, so that earlier uses can benefit from the macros
1752 in m4private.h. */
1754 /* Given ARGV, return one greater than the number of arguments it
1755 describes. */
1756 #undef m4_arg_argc
1757 size_t
1758 m4_arg_argc (m4_macro_args *argv)
1760 return argv->argc;
1763 /* Given ARGV, return the call context in effect when argument
1764 collection began. Only safe to call while the macro is being
1765 expanded. */
1766 #undef m4_arg_info
1767 const m4_call_info *
1768 m4_arg_info (m4_macro_args *argv)
1770 assert (argv->info);
1771 return argv->info;
1774 /* Return an obstack useful for scratch calculations, and which will
1775 not interfere with macro expansion. The obstack will be reset when
1776 expand_macro completes. */
1777 #undef m4_arg_scratch
1778 m4_obstack *
1779 m4_arg_scratch (m4 *context)
1781 m4__macro_arg_stacks *stack
1782 = &context->arg_stacks[context->expansion_level - 1];
1783 assert (obstack_object_size (stack->args) == 0);
1784 return stack->args;