maint: summarize highlights of 1.4.18 release
[m4/ericb.git] / m4 / macro.c
blobce2675b2b366a4e08fcdad46a1ad6d4c964d3317
1 /* GNU m4 -- A simple macro processor
2 Copyright (C) 1989-1994, 2001, 2006-2010, 2013-2014, 2017 Free
3 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 /* Define this to 1 see runtime debug info. Implied by DEBUG. */
29 /*#define DEBUG_INPUT 1 */
30 #ifndef DEBUG_MACRO
31 # define DEBUG_MACRO 0
32 #endif /* DEBUG_MACRO */
34 /* A note on argument memory lifetimes: We use an internal struct
35 (m4__macro_args_stacks) to maintain list of argument obstacks.
36 Within a recursion level, consecutive macros can share a stack, but
37 distinct recursion levels need different stacks since the nested
38 macro is interrupting the argument collection of the outer level.
39 Note that a reference can live as long as the expansion containing
40 the reference can participate as an argument in a future macro
41 call.
43 Therefore, we implement a reference counter for each expansion
44 level, tracking how many references exist into the obstack, as well
45 as associate a level with each reference. Of course, expand_macro
46 is actively using argv, so it increments the refcount on entry and
47 decrements it on exit. Additionally, any time the input engine is
48 handed a reference that it does not inline, it increases the
49 refcount in push_token, then decreases it in pop_input once the
50 reference has been rescanned. Finally, when the input engine hands
51 a reference back to expand_argument, the refcount increases, which
52 is then cleaned up at the end of expand_macro.
54 For a running example, consider this input:
56 define(a,A)define(b,`a(`$1')')define(c,$*)dnl
57 define(x,`a(1)`'c($@')define(y,`$@)')dnl
58 x(a(`b')``a'')y(`b')(`a')
59 => AAaA
61 Assuming all arguments are large enough to exceed the inlining
62 thresholds of the input engine, the interesting sequence of events
63 is as follows:
65 stacks[0] refs stacks[1] refs
66 after second dnl ends: `' 0 `' 0
67 expand_macro for x, level 0: `' 1 `' 0
68 expand_macro for a, level 1: `' 1 `' 1
69 after collect_arguments for a: `' 1 `b' 1
70 push `A' to input stack: `' 1 `b' 1
71 exit expand_macro for a: `' 1 `' 0
72 after collect_arguments for x: `A`a'' 1 `' 0
73 push `a(1)`'c(' to input stack: `A`a'' 1 `' 0
74 push_token saves $@(x) ref: `A`a'' 2 `' 0
75 exit expand_macro for x: `A`a'' 1 `' 0
76 expand_macro for a, level 0: `A`a'' 2 `' 0
77 after collect_arguments for a: `A`a''`1' 2 `' 0
78 push `A' to input stack: `A`a''`1' 2 `' 0
79 exit expand_macro for a: `A`a'' 1 `' 0
80 output `A': `A`a'' 1 `' 0
81 expand_macro for c, level 0: `A`a'' 2 `' 0
82 expand_argument gets $@(x) ref: `A`a''`$@(x)' 3 `' 0
83 pop_input ends $@(x) ref: `A`a''`$@(x)' 2 `' 0
84 expand_macro for y, level 1: `A`a''`$@(x)' 2 `' 1
85 after collect_arguments for y: `A`a''`$@(x)' 2 `b' 1
86 push_token saves $@(y) ref: `A`a''`$@(x)' 2 `b' 2
87 push `)' to input stack: `A`a''`$@(x)' 2 `b' 2
88 exit expand_macro for y: `A`a''`$@(x)' 2 `b' 1
89 expand_argument gets $@(y) ref: `A`a''`$@(x)$@(y)' 2 `b' 2
90 pop_input ends $@(y) ref: `A`a''`$@(x)$@(y)' 2 `b' 1
91 after collect_arguments for c: `A`a''`$@(x)$@(y)' 2 `b' 1
92 push_token saves $*(c) ref: `A`a''`$@(x)$@(y)' 3 `b' 2
93 expand_macro frees $@(x) ref: `A`a''`$@(x)$@(y)' 2 `b' 2
94 expand_macro frees $@(y) ref: `A`a''`$@(x)$@(y)' 2 `b' 1
95 exit expand_macro for c: `A`a''`$@(x)$@(y)' 1 `b' 1
96 output `Aa': `A`a''`$@(x)$@(y)' 0 `b' 1
97 pop_input ends $*(c)$@(x) ref: `' 0 `b' 1
98 expand_macro for b, level 0: `' 1 `b' 1
99 pop_input ends $*(c)$@(y) ref: `' 1 `' 0
100 after collect_arguments for b: `a' 1 `' 0
101 push `a(`' to input stack: `a' 1 `' 0
102 push_token saves $1(b) ref: `a' 2 `' 0
103 push `')' to input stack: `a' 2 `' 0
104 exit expand_macro for b: `a' 1 `' 0
105 expand_macro for a, level 0 : `a' 2 `' 0
106 expand_argument gets $1(b) ref: `a'`$1(b)' 3 `' 0
107 pop_input ends $1(b) ref: `a'`$1(b)' 2 `' 0
108 after collect_arguments for a: `a'`$1(b)' 2 `' 0
109 push `A' to input stack: `a'`$1(b)' 2 `' 0
110 expand_macro frees $1(b) ref: `a'`$1(b)' 1 `' 0
111 exit expand_macro for a: `' 0 `' 0
112 output `A': `' 0 `' 0
114 An obstack is only completely cleared when its refcount reaches
115 zero. However, as an optimization, expand_macro also frees
116 anything that it added to the obstack if no additional references
117 were added at the current expansion level, to reduce the amount of
118 memory left on the obstack while waiting for refcounts to drop.
121 static m4_macro_args *collect_arguments (m4 *, m4_call_info *, m4_symbol *,
122 m4_obstack *, m4_obstack *);
123 static void expand_macro (m4 *, const char *, size_t, m4_symbol *);
124 static bool expand_token (m4 *, m4_obstack *, m4__token_type,
125 m4_symbol_value *, int, bool);
126 static bool expand_argument (m4 *, m4_obstack *, m4_symbol_value *,
127 const m4_call_info *);
128 static void process_macro (m4 *, m4_symbol_value *, m4_obstack *, int,
129 m4_macro_args *);
131 static unsigned int trace_pre (m4 *, m4_macro_args *);
132 static void trace_post (m4 *, unsigned int, const m4_call_info *);
133 static unsigned int trace_header (m4 *, const m4_call_info *);
134 static void trace_flush (m4 *, unsigned int);
137 /* The number of the current call of expand_macro (). */
138 static size_t macro_call_id = 0;
140 /* A placeholder symbol value representing the empty string, used to
141 optimize checks for emptiness. */
142 static m4_symbol_value empty_symbol;
144 #if DEBUG_MACRO
145 /* True if significant changes to stacks should be printed to the
146 trace stream. Primarily useful for debugging $@ ref memory leaks,
147 and controlled by M4_DEBUG_MACRO environment variable. */
148 static int debug_macro_level;
149 #else
150 # define debug_macro_level 0
151 #endif /* !DEBUG_MACRO */
152 #define PRINT_ARGCOUNT_CHANGES 1 /* Any change to argcount > 1. */
153 #define PRINT_REFCOUNT_INCREASE 2 /* Any increase to refcount. */
154 #define PRINT_REFCOUNT_DECREASE 4 /* Any decrease to refcount. */
158 /* This function reads all input, and expands each token, one at a time. */
159 void
160 m4_macro_expand_input (m4 *context)
162 m4__token_type type;
163 m4_symbol_value token;
164 int line;
166 #if DEBUG_MACRO
167 const char *s = getenv ("M4_DEBUG_MACRO");
168 if (s)
169 debug_macro_level = strtol (s, NULL, 0);
170 #endif /* DEBUG_MACRO */
172 m4_set_symbol_value_text (&empty_symbol, "", 0, 0);
173 VALUE_MAX_ARGS (&empty_symbol) = -1;
175 while ((type = m4__next_token (context, &token, &line, NULL, false, NULL))
176 != M4_TOKEN_EOF)
177 expand_token (context, NULL, type, &token, line, true);
181 /* Expand one token onto OBS, according to its type. If OBS is NULL,
182 output the expansion to the current diversion. TYPE determines the
183 contents of TOKEN. Potential macro names (a TYPE of M4_TOKEN_WORD)
184 are looked up in the symbol table, to see if they have a macro
185 definition. If they have, they are expanded as macros, otherwise
186 the text are just copied to the output. LINE determines where
187 TOKEN began. FIRST is true if there is no prior content in the
188 current macro argument. Return true if the result is guranteed to
189 give the same parse on rescan in a quoted context with the same
190 quote age. Returning false is always safe, although it may lead to
191 slower performance. */
192 static bool
193 expand_token (m4 *context, m4_obstack *obs, m4__token_type type,
194 m4_symbol_value *token, int line, bool first)
196 m4_symbol *symbol;
197 bool result = false;
198 const char *text = (m4_is_symbol_value_text (token)
199 ? m4_get_symbol_value_text (token) : NULL);
201 switch (type)
202 { /* TOKSW */
203 case M4_TOKEN_EOF:
204 case M4_TOKEN_MACDEF:
205 /* Always safe, since there is no text to rescan. */
206 return true;
208 case M4_TOKEN_STRING:
209 /* Strings are safe in isolation (since quote_age detects any
210 change in delimiters), or when safe_quotes is true. This is
211 also returned for sequences of benign characters, such as
212 digits. When safe_quotes is false, we could technically
213 return true if we can prove that the concatenation of this
214 string to prior text does not form a multi-byte quote
215 delimiter, but that is a lot of overhead, so we give the
216 conservative answer of false. */
217 result = first || m4__safe_quotes (M4SYNTAX);
218 /* fallthru */
219 case M4_TOKEN_COMMENT:
220 /* Comments can contain unbalanced quote delimiters. Rather
221 than search for one, we return the conservative answer of
222 false. If obstack is provided, the string or comment was
223 already expanded into it during next_token. */
224 if (obs)
225 return result;
226 break;
228 case M4_TOKEN_OPEN:
229 case M4_TOKEN_COMMA:
230 case M4_TOKEN_CLOSE:
231 case M4_TOKEN_SPACE:
232 /* If safe_quotes is true, then these do not form a quote
233 delimiter. If it is false, we give the conservative answer
234 of false rather than taking time to prove that no multi-byte
235 quote delimiter is formed. */
236 result = m4__safe_quotes (M4SYNTAX);
237 break;
239 case M4_TOKEN_SIMPLE:
240 /* If safe_quotes is true, then all but the single-byte end
241 quote delimiter is safe in a quoted context; a single-byte
242 start delimiter will trigger M4_TOKEN_STRING instead. If
243 safe_quotes is false, we give the conservative answer of
244 false rather than taking time to prove that no multi-byte
245 quote delimiter is formed. */
246 result = (!m4_has_syntax (M4SYNTAX, *text, M4_SYNTAX_RQUOTE)
247 && m4__safe_quotes (M4SYNTAX));
248 if (result)
249 assert (!m4_has_syntax (M4SYNTAX, *text, M4_SYNTAX_LQUOTE));
250 break;
252 case M4_TOKEN_WORD:
254 const char *textp = text;
255 size_t len = m4_get_symbol_value_len (token);
256 size_t len2 = len;
258 if (m4_has_syntax (M4SYNTAX, *textp, M4_SYNTAX_ESCAPE))
260 textp++;
261 len2--;
264 symbol = m4_symbol_lookup (M4SYMTAB, textp, len2);
265 assert (!symbol || !m4_is_symbol_void (symbol));
266 if (symbol == NULL
267 || (symbol->value->type == M4_SYMBOL_FUNC
268 && BIT_TEST (SYMBOL_FLAGS (symbol), VALUE_BLIND_ARGS_BIT)
269 && !m4__next_token_is_open (context)))
271 m4_divert_text (context, obs, text, len, line);
272 /* If safe_quotes is true, then words do not overlap with
273 quote delimiters. If it is false, we give the
274 conservative answer of false rather than prove that no
275 multi-byte delimiters are formed. */
276 return m4__safe_quotes (M4SYNTAX);
278 expand_macro (context, textp, len2, symbol);
279 /* Expanding a macro may create new tokens to scan, and those
280 tokens may generate unsafe text, but we did not append any
281 text now. */
282 return true;
285 default:
286 assert (!"INTERNAL ERROR: bad token type in expand_token ()");
287 abort ();
289 m4_divert_text (context, obs, text, m4_get_symbol_value_len (token), line);
290 return result;
294 /* This function parses one argument to a macro call. It expects the
295 first left parenthesis or the separating comma to have been read by
296 the caller. It skips leading whitespace, then reads and expands
297 tokens, until it finds a comma or a right parenthesis at the same
298 level of parentheses. It returns a flag indicating whether the
299 argument read is the last for the active macro call. The arguments
300 are built on the obstack OBS, indirectly through expand_token ().
301 Report errors on behalf of CALLER. */
302 static bool
303 expand_argument (m4 *context, m4_obstack *obs, m4_symbol_value *argp,
304 const m4_call_info *caller)
306 m4__token_type type;
307 m4_symbol_value token;
308 int paren_level = 0;
309 int line = m4_get_current_line (context);
310 size_t len;
311 unsigned int age = m4__quote_age (M4SYNTAX);
312 bool first = true;
314 memset (argp, '\0', sizeof *argp);
315 VALUE_MAX_ARGS (argp) = -1;
317 /* Skip leading white space. */
320 type = m4__next_token (context, &token, NULL, obs, true, caller);
322 while (type == M4_TOKEN_SPACE);
324 while (1)
326 if (VALUE_MIN_ARGS (argp) < VALUE_MIN_ARGS (&token))
327 VALUE_MIN_ARGS (argp) = VALUE_MIN_ARGS (&token);
328 if (VALUE_MAX_ARGS (&token) < VALUE_MAX_ARGS (argp))
329 VALUE_MAX_ARGS (argp) = VALUE_MAX_ARGS (&token);
330 switch (type)
331 { /* TOKSW */
332 case M4_TOKEN_COMMA:
333 case M4_TOKEN_CLOSE:
334 if (paren_level == 0)
336 assert (argp->type != M4_SYMBOL_FUNC);
337 if (argp->type != M4_SYMBOL_COMP)
339 len = obstack_object_size (obs);
340 VALUE_MODULE (argp) = NULL;
341 if (len)
343 obstack_1grow (obs, '\0');
344 m4_set_symbol_value_text (argp, obstack_finish (obs),
345 len, age);
347 else
348 m4_set_symbol_value_text (argp, "", len, 0);
350 else
352 m4__make_text_link (obs, NULL, &argp->u.u_c.end);
353 if (argp->u.u_c.chain == argp->u.u_c.end
354 && argp->u.u_c.chain->type == M4__CHAIN_FUNC)
356 const m4__builtin *func = argp->u.u_c.chain->u.builtin;
357 argp->type = M4_SYMBOL_FUNC;
358 argp->u.builtin = func;
361 return type == M4_TOKEN_COMMA;
363 /* fallthru */
364 case M4_TOKEN_OPEN:
365 case M4_TOKEN_SIMPLE:
366 if (type == M4_TOKEN_OPEN)
367 paren_level++;
368 else if (type == M4_TOKEN_CLOSE)
369 paren_level--;
370 if (!expand_token (context, obs, type, &token, line, first))
371 age = 0;
372 break;
374 case M4_TOKEN_EOF:
375 m4_error (context, EXIT_FAILURE, 0, caller,
376 _("end of file in argument list"));
377 break;
379 case M4_TOKEN_WORD:
380 case M4_TOKEN_SPACE:
381 case M4_TOKEN_STRING:
382 case M4_TOKEN_COMMENT:
383 case M4_TOKEN_MACDEF:
384 if (!expand_token (context, obs, type, &token, line, first))
385 age = 0;
386 if (token.type == M4_SYMBOL_COMP)
388 if (argp->type != M4_SYMBOL_COMP)
390 argp->type = M4_SYMBOL_COMP;
391 argp->u.u_c.chain = token.u.u_c.chain;
392 argp->u.u_c.wrapper = argp->u.u_c.has_func = false;
394 else
396 assert (argp->u.u_c.end);
397 argp->u.u_c.end->next = token.u.u_c.chain;
399 argp->u.u_c.end = token.u.u_c.end;
400 if (token.u.u_c.has_func)
401 argp->u.u_c.has_func = true;
403 break;
405 case M4_TOKEN_ARGV:
406 assert (paren_level == 0 && argp->type == M4_SYMBOL_VOID
407 && obstack_object_size (obs) == 0
408 && token.u.u_c.chain == token.u.u_c.end
409 && token.u.u_c.chain->quote_age == age
410 && token.u.u_c.chain->type == M4__CHAIN_ARGV);
411 argp->type = M4_SYMBOL_COMP;
412 argp->u.u_c.chain = argp->u.u_c.end = token.u.u_c.chain;
413 argp->u.u_c.wrapper = true;
414 argp->u.u_c.has_func = token.u.u_c.has_func;
415 type = m4__next_token (context, &token, NULL, NULL, false, caller);
416 if (argp->u.u_c.chain->u.u_a.skip_last)
417 assert (type == M4_TOKEN_COMMA);
418 else
419 assert (type == M4_TOKEN_COMMA || type == M4_TOKEN_CLOSE);
420 return type == M4_TOKEN_COMMA;
422 default:
423 assert (!"expand_argument");
424 abort ();
427 if (argp->type != M4_SYMBOL_VOID || obstack_object_size (obs))
428 first = false;
429 type = m4__next_token (context, &token, NULL, obs, first, caller);
434 /* The macro expansion is handled by expand_macro (). It parses the
435 arguments, using collect_arguments (), and builds a table of pointers to
436 the arguments. The arguments themselves are stored on a local obstack.
437 Expand_macro () uses m4_macro_call () to do the call of the macro.
439 Expand_macro () is potentially recursive, since it calls expand_argument
440 (), which might call expand_token (), which might call expand_macro ().
442 NAME points to storage on the token stack, so it is only valid
443 until a call to collect_arguments parses more tokens. SYMBOL is
444 the result of the symbol table lookup on NAME. */
445 static void
446 expand_macro (m4 *context, const char *name, size_t len, m4_symbol *symbol)
448 void *args_base; /* Base of stack->args on entry. */
449 void *args_scratch; /* Base of scratch space for m4_macro_call. */
450 void *argv_base; /* Base of stack->argv on entry. */
451 m4_macro_args *argv; /* Arguments to the called macro. */
452 m4_obstack *expansion; /* Collects the macro's expansion. */
453 m4_symbol_value *value; /* Original value of this macro. */
454 size_t level; /* Expansion level of this macro. */
455 m4__macro_arg_stacks *stack; /* Storage for this macro. */
456 m4_call_info info; /* Context of this macro call. */
458 /* Obstack preparation. */
459 level = context->expansion_level;
460 if (context->stacks_count <= level)
462 size_t count = context->stacks_count;
463 context->arg_stacks
464 = (m4__macro_arg_stacks *) x2nrealloc (context->arg_stacks,
465 &context->stacks_count,
466 sizeof *context->arg_stacks);
467 memset (&context->arg_stacks[count], 0,
468 sizeof *context->arg_stacks * (context->stacks_count - count));
470 stack = &context->arg_stacks[level];
471 if (!stack->args)
473 assert (!stack->refcount);
474 stack->args = (m4_obstack *) xmalloc (sizeof *stack->args);
475 stack->argv = (m4_obstack *) xmalloc (sizeof *stack->argv);
476 obstack_init (stack->args);
477 obstack_init (stack->argv);
478 stack->args_base = obstack_finish (stack->args);
479 stack->argv_base = obstack_finish (stack->argv);
481 assert (obstack_object_size (stack->args) == 0
482 && obstack_object_size (stack->argv) == 0);
483 args_base = obstack_finish (stack->args);
484 argv_base = obstack_finish (stack->argv);
485 m4__adjust_refcount (context, level, true);
486 stack->argcount++;
488 /* Grab the current value of this macro, because it may change while
489 collecting arguments. Likewise, grab any state needed during
490 tracing. */
491 value = m4_get_symbol_value (symbol);
492 info.file = m4_get_current_file (context);
493 info.line = m4_get_current_line (context);
494 info.call_id = ++macro_call_id;
495 info.trace = (m4_is_debug_bit (context, M4_DEBUG_TRACE_ALL)
496 || m4_get_symbol_traced (symbol));
497 info.debug_level = m4_get_debug_level_opt (context);
498 info.name = name;
499 info.name_len = len;
501 /* Prepare for macro expansion. */
502 VALUE_PENDING (value)++;
503 if (m4_get_nesting_limit_opt (context) < ++context->expansion_level)
504 m4_error (context, EXIT_FAILURE, 0, NULL, _("\
505 recursion limit of %zu exceeded, use -L<N> to change it"),
506 m4_get_nesting_limit_opt (context));
508 m4_trace_prepare (context, &info, value);
509 argv = collect_arguments (context, &info, symbol, stack->args, stack->argv);
510 /* Since collect_arguments can invalidate stack by reallocating
511 context->arg_stacks during a recursive expand_macro call, we must
512 reset it here. */
513 stack = &context->arg_stacks[level];
514 args_scratch = obstack_finish (stack->args);
516 /* The actual macro call. */
517 expansion = m4_push_string_init (context, info.file, info.line);
518 m4_macro_call (context, value, expansion, argv);
519 m4_push_string_finish ();
521 /* Cleanup. */
522 argv->info = NULL;
524 --context->expansion_level;
525 --VALUE_PENDING (value);
526 if (BIT_TEST (VALUE_FLAGS (value), VALUE_DELETED_BIT))
527 m4_symbol_value_delete (value);
529 /* We no longer need argv, so reduce the refcount. Additionally, if
530 no other references to argv were created, we can free our portion
531 of the obstack, although we must leave earlier content alone. A
532 refcount of 0 implies that adjust_refcount already freed the
533 entire stack. */
534 m4__arg_adjust_refcount (context, argv, false);
535 if (stack->refcount)
537 if (argv->inuse)
539 obstack_free (stack->args, args_scratch);
540 if (debug_macro_level & PRINT_ARGCOUNT_CHANGES)
541 xfprintf (stderr, "m4debug: -%zu- `%s' in use, level=%zu, "
542 "refcount=%zu, argcount=%zu\n", info.call_id,
543 argv->info->name, level, stack->refcount,
544 stack->argcount);
546 else
548 obstack_free (stack->args, args_base);
549 obstack_free (stack->argv, argv_base);
550 stack->argcount--;
555 /* Collect all the arguments to a call of the macro SYMBOL, with call
556 context INFO. The arguments are stored on the obstack ARGUMENTS
557 and a table of pointers to the arguments on ARGV_STACK. Return the
558 object describing all of the macro arguments. */
559 static m4_macro_args *
560 collect_arguments (m4 *context, m4_call_info *info, m4_symbol *symbol,
561 m4_obstack *arguments, m4_obstack *argv_stack)
563 m4_symbol_value token;
564 m4_symbol_value *tokenp;
565 bool more_args;
566 m4_macro_args args;
567 m4_macro_args *argv;
569 args.argc = 1;
570 args.inuse = false;
571 args.wrapper = false;
572 args.has_ref = false;
573 args.flatten = m4_symbol_flatten_args (symbol);
574 args.has_func = false;
575 /* Must copy here, since we are consuming tokens, and since symbol
576 table can be changed during argument collection. */
577 info->name = (char *) obstack_copy0 (arguments, info->name, info->name_len);
578 args.quote_age = m4__quote_age (M4SYNTAX);
579 args.info = info;
580 args.level = context->expansion_level - 1;
581 args.arraylen = 0;
582 obstack_grow (argv_stack, &args, offsetof (m4_macro_args, array));
584 if (m4__next_token_is_open (context))
586 /* Gobble parenthesis, then collect arguments. */
587 m4__next_token (context, &token, NULL, NULL, false, info);
590 tokenp = (m4_symbol_value *) obstack_alloc (arguments,
591 sizeof *tokenp);
592 more_args = expand_argument (context, arguments, tokenp, info);
594 if ((m4_is_symbol_value_text (tokenp)
595 && !m4_get_symbol_value_len (tokenp))
596 || (args.flatten && m4_is_symbol_value_func (tokenp)))
598 obstack_free (arguments, tokenp);
599 tokenp = &empty_symbol;
601 obstack_ptr_grow (argv_stack, tokenp);
602 args.arraylen++;
603 args.argc++;
604 switch (tokenp->type)
606 case M4_SYMBOL_TEXT:
607 /* Be conservative - any change in quoting while
608 collecting arguments, or any unsafe argument, will
609 require a rescan if $@ is reused. */
610 if (m4_get_symbol_value_len (tokenp)
611 && m4_get_symbol_value_quote_age (tokenp) != args.quote_age)
612 args.quote_age = 0;
613 break;
614 case M4_SYMBOL_FUNC:
615 args.has_func = true;
616 break;
617 case M4_SYMBOL_COMP:
618 args.has_ref = true;
619 if (tokenp->u.u_c.wrapper)
621 assert (tokenp->u.u_c.chain->type == M4__CHAIN_ARGV
622 && !tokenp->u.u_c.chain->next);
623 args.argc += (tokenp->u.u_c.chain->u.u_a.argv->argc
624 - tokenp->u.u_c.chain->u.u_a.index
625 - tokenp->u.u_c.chain->u.u_a.skip_last - 1);
626 args.wrapper = true;
628 if (tokenp->u.u_c.has_func)
629 args.has_func = true;
630 break;
631 default:
632 assert (!"expand_argument");
633 abort ();
636 while (more_args);
638 argv = (m4_macro_args *) obstack_finish (argv_stack);
639 argv->argc = args.argc;
640 argv->wrapper = args.wrapper;
641 argv->has_ref = args.has_ref;
642 argv->has_func = args.has_func;
643 if (args.quote_age != m4__quote_age (M4SYNTAX))
644 argv->quote_age = 0;
645 argv->arraylen = args.arraylen;
646 return argv;
650 /* The actual call of a macro is handled by m4_macro_call ().
651 m4_macro_call () is passed a symbol VALUE, whose type is used to
652 call either a builtin function, or the user macro expansion
653 function process_macro (). The arguments are provided by the ARGV
654 table. The expansion is left on the obstack EXPANSION. Macro
655 tracing is also handled here. */
656 void
657 m4_macro_call (m4 *context, m4_symbol_value *value, m4_obstack *expansion,
658 m4_macro_args *argv)
660 unsigned int trace_start = 0;
662 if (argv->info->trace)
663 trace_start = trace_pre (context, argv);
664 if (!m4_bad_argc (context, argv->argc, argv->info,
665 VALUE_MIN_ARGS (value), VALUE_MAX_ARGS (value),
666 BIT_TEST (VALUE_FLAGS (value),
667 VALUE_SIDE_EFFECT_ARGS_BIT)))
669 if (m4_is_symbol_value_text (value))
670 process_macro (context, value, expansion, argv->argc, argv);
671 else if (m4_is_symbol_value_func (value))
672 m4_get_symbol_value_func (value) (context, expansion, argv->argc,
673 argv);
674 else if (m4_is_symbol_value_placeholder (value))
675 m4_warn (context, 0, argv->info,
676 _("builtin %s requested by frozen file not found"),
677 quotearg_style (locale_quoting_style,
678 m4_get_symbol_value_placeholder (value)));
679 else
681 assert (!"m4_macro_call");
682 abort ();
685 if (argv->info->trace)
686 trace_post (context, trace_start, argv->info);
689 /* This function handles all expansion of user defined and predefined
690 macros. It is called with an obstack OBS, where the macros expansion
691 will be placed, as an unfinished object. SYMBOL points to the macro
692 definition, giving the expansion text. ARGC and ARGV are the arguments,
693 as usual. */
694 static void
695 process_macro (m4 *context, m4_symbol_value *value, m4_obstack *obs,
696 int argc, m4_macro_args *argv)
698 const char *text = m4_get_symbol_value_text (value);
699 size_t len = m4_get_symbol_value_len (value);
700 const char *end = text + len;
701 int i;
702 while (1)
704 const char *dollar;
705 if (m4_is_syntax_single_dollar (M4SYNTAX))
706 dollar = (char *) memchr (text, M4SYNTAX->dollar, len);
707 else
709 dollar = text;
710 while (dollar != end)
712 if (m4_has_syntax (M4SYNTAX, *dollar, M4_SYNTAX_DOLLAR))
713 break;
714 dollar++;
716 if (dollar == end)
717 dollar = NULL;
719 if (!dollar)
721 obstack_grow (obs, text, len);
722 return;
724 obstack_grow (obs, text, dollar - text);
725 len -= dollar - text;
726 text = dollar;
727 if (len == 1)
729 obstack_1grow (obs, *dollar);
730 return;
732 len--;
733 switch (*++text)
735 case '0': case '1': case '2': case '3': case '4':
736 case '5': case '6': case '7': case '8': case '9':
737 /* FIXME - multidigit arguments should convert over to ${10}
738 syntax instead of $10; see
739 http://lists.gnu.org/archive/html/m4-discuss/2006-08/msg00028.html
740 for more discussion. */
741 if (m4_get_posixly_correct_opt (context)
742 || !isdigit (to_uchar (text[1])))
744 i = *text++ - '0';
745 len--;
747 else
749 char *endp;
750 i = (int) strtol (text, &endp, 10);
751 len -= endp - text;
752 text = endp;
754 if (i < argc)
755 m4_push_arg (context, obs, argv, i);
756 break;
758 case '#': /* number of arguments */
759 m4_shipout_int (obs, argc - 1);
760 text++;
761 len--;
762 break;
764 case '*': /* all arguments */
765 case '@': /* ... same, but quoted */
766 m4_push_args (context, obs, argv, false, *text == '@');
767 text++;
768 len--;
769 break;
771 default:
772 if (m4_get_posixly_correct_opt (context)
773 || !VALUE_ARG_SIGNATURE (value))
775 obstack_1grow (obs, *dollar);
777 else
779 size_t len1 = 0;
780 const char *endp;
781 char *key;
783 for (endp = ++text;
784 len1 < len && m4_has_syntax (M4SYNTAX, *endp,
785 (M4_SYNTAX_OTHER
786 | M4_SYNTAX_ALPHA
787 | M4_SYNTAX_NUM));
788 ++endp)
790 ++len1;
792 key = xstrndup (text, len1);
794 if (*endp)
796 struct m4_symbol_arg **arg
797 = (struct m4_symbol_arg **)
798 m4_hash_lookup (VALUE_ARG_SIGNATURE (value), key);
800 if (arg)
802 i = SYMBOL_ARG_INDEX (*arg);
803 assert (i < argc);
804 m4_shipout_string (context, obs, M4ARG (i), M4ARGLEN (i),
805 false);
808 else
810 m4_error (context, 0, 0, argv->info,
811 _("unterminated parameter reference: %s"), key);
814 len -= endp - text;
815 text = endp;
817 free (key);
819 break;
826 /* The next portion of this file contains the functions for macro
827 tracing output. All tracing output for a macro call is collected
828 on an obstack TRACE, and printed whenever the line is complete.
829 This prevents tracing output from interfering with other debug
830 messages generated by the various builtins. */
832 /* Format the standard header attached to all tracing output lines,
833 using the context in INFO as appropriate. Return the offset into
834 the trace obstack where this particular trace begins. */
835 static unsigned int
836 trace_header (m4 *context, const m4_call_info *info)
838 m4_obstack *trace = &context->trace_messages;
839 unsigned int result = obstack_object_size (trace);
840 obstack_grow (trace, "m4trace:", 8);
841 if (info->debug_level & M4_DEBUG_TRACE_FILE)
842 obstack_printf (trace, "%s:", info->file);
843 if (info->debug_level & M4_DEBUG_TRACE_LINE)
844 obstack_printf (trace, "%d:", info->line);
845 obstack_printf (trace, " -%zu- ", context->expansion_level);
846 if (info->debug_level & M4_DEBUG_TRACE_CALLID)
847 obstack_printf (trace, "id %zu: ", info->call_id);
848 return result;
851 /* Print current tracing line starting at offset START, as returned
852 from an earlier trace_header(), then clear the obstack. */
853 static void
854 trace_flush (m4 *context, unsigned int start)
856 char *str;
857 size_t len = obstack_object_size (&context->trace_messages);
858 FILE *file = m4_get_debug_file (context);
860 if (file)
862 /* TODO - quote nonprintable characters if debug is tty? */
863 str = (char *) obstack_base (&context->trace_messages);
864 fwrite (&str[start], 1, len - start, file);
865 fputc ('\n', file);
867 obstack_blank_fast (&context->trace_messages, start - len);
870 /* Do pre-argument-collection tracing for the macro described in INFO.
871 Should be called prior to m4_macro_call(). */
872 void
873 m4_trace_prepare (m4 *context, const m4_call_info *info,
874 m4_symbol_value *value)
876 const m4_string_pair *quotes = NULL;
877 size_t arg_length = m4_get_max_debug_arg_length_opt (context);
878 bool module = (info->debug_level & M4_DEBUG_TRACE_MODULE) != 0;
880 if (info->debug_level & M4_DEBUG_TRACE_QUOTE)
881 quotes = m4_get_syntax_quotes (M4SYNTAX);
882 if (info->trace && (info->debug_level & M4_DEBUG_TRACE_CALL))
884 unsigned int start = trace_header (context, info);
885 obstack_grow (&context->trace_messages, info->name, info->name_len);
886 obstack_grow (&context->trace_messages, " ... = ", 7);
887 m4__symbol_value_print (context, value, &context->trace_messages, quotes,
888 false, NULL, &arg_length, module);
889 trace_flush (context, start);
893 /* Format the parts of a trace line that are known via ARGV before the
894 macro is actually expanded. Used from m4_macro_call(). Return the
895 start of the current trace, in case other traces are printed before
896 this trace completes trace_post. */
897 static unsigned int
898 trace_pre (m4 *context, m4_macro_args *argv)
900 int trace_level = argv->info->debug_level;
901 unsigned int start = trace_header (context, argv->info);
902 m4_obstack *trace = &context->trace_messages;
904 assert (argv->info->trace);
905 obstack_grow (trace, argv->info->name, argv->info->name_len);
907 if (1 < m4_arg_argc (argv) && (trace_level & M4_DEBUG_TRACE_ARGS))
909 const m4_string_pair *quotes = NULL;
910 size_t arg_length = m4_get_max_debug_arg_length_opt (context);
911 bool module = (trace_level & M4_DEBUG_TRACE_MODULE) != 0;
913 if (trace_level & M4_DEBUG_TRACE_QUOTE)
914 quotes = m4_get_syntax_quotes (M4SYNTAX);
915 obstack_1grow (trace, '(');
916 m4__arg_print (context, trace, argv, 1, quotes, false, NULL, ", ",
917 &arg_length, true, module);
918 obstack_1grow (trace, ')');
920 return start;
923 /* If requested by the trace state in INFO, format the final part of a
924 trace line. Then print all collected information from START,
925 returned from a prior trace_pre(). Used from m4_macro_call (). */
926 static void
927 trace_post (m4 *context, unsigned int start, const m4_call_info *info)
929 assert (info->trace);
930 if (info->debug_level & M4_DEBUG_TRACE_EXPANSION)
932 obstack_grow (&context->trace_messages, " -> ", 4);
933 m4_input_print (context, &context->trace_messages, info->debug_level);
935 trace_flush (context, start);
939 /* Accessors into m4_macro_args. */
941 /* Adjust the refcount of argument stack LEVEL. If INCREASE, then
942 increase the count, otherwise decrease the count and clear the
943 entire stack if the new count is zero. Return the new
944 refcount. */
945 size_t
946 m4__adjust_refcount (m4 *context, size_t level, bool increase)
948 m4__macro_arg_stacks *stack = &context->arg_stacks[level];
949 assert (level < context->stacks_count && stack->args
950 && (increase || stack->refcount));
951 if (increase)
952 stack->refcount++;
953 else if (--stack->refcount == 0)
955 obstack_free (stack->args, stack->args_base);
956 obstack_free (stack->argv, stack->argv_base);
957 if ((debug_macro_level & PRINT_ARGCOUNT_CHANGES) && 1 < stack->argcount)
958 xfprintf (stderr, "m4debug: -%zu- freeing %zu args, level=%zu\n",
959 macro_call_id, stack->argcount, level);
960 stack->argcount = 0;
962 if (debug_macro_level
963 & (increase ? PRINT_REFCOUNT_INCREASE : PRINT_REFCOUNT_DECREASE))
964 xfprintf (stderr, "m4debug: level %zu refcount=%zu\n", level,
965 stack->refcount);
966 return stack->refcount;
969 /* Given ARGV, adjust the refcount of every reference it contains in
970 the direction decided by INCREASE. Return true if increasing
971 references to ARGV implies the first use of ARGV. */
972 bool
973 m4__arg_adjust_refcount (m4 *context, m4_macro_args *argv, bool increase)
975 size_t i;
976 m4__symbol_chain *chain;
977 bool result = !argv->inuse;
979 if (argv->has_ref)
980 for (i = 0; i < argv->arraylen; i++)
981 if (argv->array[i]->type == M4_SYMBOL_COMP)
983 chain = argv->array[i]->u.u_c.chain;
984 while (chain)
986 switch (chain->type)
988 case M4__CHAIN_STR:
989 if (chain->u.u_s.level < SIZE_MAX)
990 m4__adjust_refcount (context, chain->u.u_s.level,
991 increase);
992 break;
993 case M4__CHAIN_FUNC:
994 break;
995 case M4__CHAIN_ARGV:
996 assert (chain->u.u_a.argv->inuse);
997 m4__arg_adjust_refcount (context, chain->u.u_a.argv,
998 increase);
999 break;
1000 default:
1001 assert (!"m4__arg_adjust_refcount");
1002 abort ();
1004 chain = chain->next;
1007 m4__adjust_refcount (context, argv->level, increase);
1008 return result;
1011 /* Mark ARGV as being in use, along with any $@ references that it
1012 wraps. */
1013 static void
1014 arg_mark (m4_macro_args *argv)
1016 size_t i;
1017 m4__symbol_chain *chain;
1019 if (argv->inuse)
1020 return;
1021 argv->inuse = true;
1022 if (argv->wrapper)
1024 for (i = 0; i < argv->arraylen; i++)
1025 if (argv->array[i]->type == M4_SYMBOL_COMP
1026 && argv->array[i]->u.u_c.wrapper)
1028 chain = argv->array[i]->u.u_c.chain;
1029 assert (!chain->next && chain->type == M4__CHAIN_ARGV);
1030 if (!chain->u.u_a.argv->inuse)
1031 arg_mark (chain->u.u_a.argv);
1036 /* Populate the newly-allocated VALUE as a wrapper around ARGV,
1037 starting with argument ARG. Allocate any data on OBS, owned by a
1038 given expansion LEVEL. FLATTEN determines whether to allow
1039 builtins, and QUOTES determines whether all arguments are quoted.
1040 Return TOKEN when successful, NULL when wrapping ARGV is trivially
1041 empty. */
1042 static m4_symbol_value *
1043 make_argv_ref (m4 *context, m4_symbol_value *value, m4_obstack *obs,
1044 size_t level, m4_macro_args *argv, size_t arg, bool flatten,
1045 const m4_string_pair *quotes)
1047 m4__symbol_chain *chain;
1049 if (argv->argc <= arg)
1050 return NULL;
1051 value->type = M4_SYMBOL_COMP;
1052 value->u.u_c.chain = value->u.u_c.end = NULL;
1054 /* Cater to the common idiom of $0(`$1',shift(shift($@))), by
1055 inlining the first few arguments and reusing the original $@ ref,
1056 rather than creating another layer of wrappers. */
1057 while (argv->wrapper)
1059 size_t i;
1060 for (i = 0; i < argv->arraylen; i++)
1062 if ((argv->array[i]->type == M4_SYMBOL_COMP
1063 && argv->array[i]->u.u_c.wrapper)
1064 || level < SIZE_MAX)
1065 break;
1066 if (arg == 1)
1068 m4__push_arg_quote (context, obs, argv, i + 1, quotes);
1069 /* TODO support M4_SYNTAX_COMMA. */
1070 obstack_1grow (obs, ',');
1072 else
1073 arg--;
1075 assert (i < argv->arraylen);
1076 if (i + 1 == argv->arraylen)
1078 assert (argv->array[i]->type == M4_SYMBOL_COMP
1079 && argv->array[i]->u.u_c.wrapper);
1080 chain = argv->array[i]->u.u_c.chain;
1081 assert (!chain->next && chain->type == M4__CHAIN_ARGV
1082 && !chain->u.u_a.skip_last);
1083 argv = chain->u.u_a.argv;
1084 arg += chain->u.u_a.index - 1;
1086 else
1088 arg += i;
1089 break;
1093 m4__make_text_link (obs, &value->u.u_c.chain, &value->u.u_c.end);
1094 chain = (m4__symbol_chain *) obstack_alloc (obs, sizeof *chain);
1095 if (value->u.u_c.end)
1096 value->u.u_c.end->next = chain;
1097 else
1098 value->u.u_c.chain = chain;
1099 value->u.u_c.end = chain;
1100 value->u.u_c.wrapper = true;
1101 value->u.u_c.has_func = argv->has_func;
1102 chain->next = NULL;
1103 chain->type = M4__CHAIN_ARGV;
1104 chain->quote_age = argv->quote_age;
1105 chain->u.u_a.argv = argv;
1106 chain->u.u_a.index = arg;
1107 chain->u.u_a.flatten = flatten;
1108 chain->u.u_a.has_func = argv->has_func;
1109 chain->u.u_a.comma = false;
1110 chain->u.u_a.skip_last = false;
1111 chain->u.u_a.quotes = m4__quote_cache (M4SYNTAX, obs, chain->quote_age,
1112 quotes);
1113 return value;
1116 /* Given ARGV, return the symbol value at the specified ARG, which
1117 must be non-zero. *LEVEL is set to the obstack level that contains
1118 the symbol (which is not necessarily the level of ARGV). If
1119 FLATTEN, avoid returning a builtin token. */
1120 static m4_symbol_value *
1121 arg_symbol (m4_macro_args *argv, size_t arg, size_t *level, bool flatten)
1123 size_t i;
1124 m4_symbol_value *value;
1126 assert (arg);
1127 if (level)
1128 *level = argv->level;
1129 flatten |= argv->flatten;
1130 if (argv->argc <= arg)
1131 return &empty_symbol;
1132 if (!argv->wrapper)
1134 value = argv->array[arg - 1];
1135 if (flatten && m4_is_symbol_value_func (value))
1136 value = &empty_symbol;
1137 return value;
1140 /* Must cycle through all array slots until we find arg, since
1141 wrappers can contain multiple arguments. */
1142 for (i = 0; i < argv->arraylen; i++)
1144 value = argv->array[i];
1145 if (value->type == M4_SYMBOL_COMP && value->u.u_c.wrapper)
1147 m4__symbol_chain *chain = value->u.u_c.chain;
1148 assert (!chain->next && chain->type == M4__CHAIN_ARGV);
1149 if (arg <= (chain->u.u_a.argv->argc - chain->u.u_a.index
1150 - chain->u.u_a.skip_last))
1152 value = arg_symbol (chain->u.u_a.argv,
1153 chain->u.u_a.index - 1 + arg, level,
1154 flatten || chain->u.u_a.flatten);
1155 break;
1157 arg -= (chain->u.u_a.argv->argc - chain->u.u_a.index
1158 - chain->u.u_a.skip_last);
1160 else if (--arg == 0)
1161 break;
1163 return value;
1166 /* Given ARGV, return the symbol value at the specified ARG, which
1167 must be non-zero. */
1168 m4_symbol_value *
1169 m4_arg_symbol (m4_macro_args *argv, size_t arg)
1171 return arg_symbol (argv, arg, NULL, false);
1174 /* Given ARGV, return true if argument ARG is text. Arg 0 is always
1175 text, as are indices beyond argc. */
1176 bool
1177 m4_is_arg_text (m4_macro_args *argv, size_t arg)
1179 m4_symbol_value *value;
1180 if (arg == 0 || argv->argc <= arg || argv->flatten || !argv->has_func)
1181 return true;
1182 value = m4_arg_symbol (argv, arg);
1183 if (m4_is_symbol_value_text (value)
1184 || (value->type == M4_SYMBOL_COMP && !value->u.u_c.has_func))
1185 return true;
1186 return false;
1189 /* Given ARGV, return true if argument ARG is a single builtin
1190 function. Only non-zero indices less than argc can return
1191 true. */
1192 bool
1193 m4_is_arg_func (m4_macro_args *argv, size_t arg)
1195 if (arg == 0 || argv->argc <= arg || argv->flatten || !argv->has_func)
1196 return false;
1197 return m4_is_symbol_value_func (m4_arg_symbol (argv, arg));
1200 /* Given ARGV, return true if argument ARG contains a builtin token
1201 concatenated with anything else. Only non-zero indices less than
1202 argc can return true. */
1203 bool
1204 m4_is_arg_composite (m4_macro_args *argv, size_t arg)
1206 m4_symbol_value *value;
1207 if (arg == 0 || argv->argc <= arg || argv->flatten || !argv->has_func)
1208 return false;
1209 value = m4_arg_symbol (argv, arg);
1210 if (value->type == M4_SYMBOL_COMP && value->u.u_c.has_func)
1211 return true;
1212 return false;
1215 /* Given ARGV, return the text at argument ARG. Abort if the argument
1216 is not text. Arg 0 is always text, and indices beyond argc return
1217 the empty string. If FLATTEN, builtins are ignored. The result is
1218 always NUL-terminated, even if it includes embedded NUL
1219 characters. */
1220 const char *
1221 m4_arg_text (m4 *context, m4_macro_args *argv, size_t arg, bool flatten)
1223 m4_symbol_value *value;
1224 m4__symbol_chain *chain;
1225 m4_obstack *obs;
1227 if (arg == 0)
1229 assert (argv->info);
1230 return argv->info->name;
1232 if (argv->argc <= arg)
1233 return "";
1234 value = arg_symbol (argv, arg, NULL, flatten);
1235 if (m4_is_symbol_value_text (value))
1236 return m4_get_symbol_value_text (value);
1237 assert (value->type == M4_SYMBOL_COMP);
1238 chain = value->u.u_c.chain;
1239 obs = m4_arg_scratch (context);
1240 while (chain)
1242 switch (chain->type)
1244 case M4__CHAIN_STR:
1245 obstack_grow (obs, chain->u.u_s.str, chain->u.u_s.len);
1246 break;
1247 case M4__CHAIN_FUNC:
1248 if (flatten)
1249 break;
1250 assert (!"m4_arg_text");
1251 abort ();
1252 case M4__CHAIN_ARGV:
1253 assert (!chain->u.u_a.has_func || flatten || argv->flatten);
1254 m4__arg_print (context, obs, chain->u.u_a.argv, chain->u.u_a.index,
1255 m4__quote_cache (M4SYNTAX, NULL, chain->quote_age,
1256 chain->u.u_a.quotes),
1257 flatten || argv->flatten || chain->u.u_a.flatten,
1258 NULL, NULL, NULL, false, false);
1259 break;
1260 default:
1261 assert (!"m4_arg_text");
1262 abort ();
1264 chain = chain->next;
1266 obstack_1grow (obs, '\0');
1267 return (char *) obstack_finish (obs);
1270 /* Given ARGV, compare text arguments INDEXA and INDEXB for equality.
1271 Both indices must be non-zero. Return true if the arguments
1272 contain the same contents; often more efficient than
1273 STREQ (m4_arg_text (context, argv, indexa),
1274 m4_arg_text (context, argv, indexb)). */
1275 bool
1276 m4_arg_equal (m4 *context, m4_macro_args *argv, size_t indexa, size_t indexb)
1278 m4_symbol_value *sa = m4_arg_symbol (argv, indexa);
1279 m4_symbol_value *sb = m4_arg_symbol (argv, indexb);
1280 m4__symbol_chain tmpa;
1281 m4__symbol_chain tmpb;
1282 m4__symbol_chain *ca = &tmpa;
1283 m4__symbol_chain *cb = &tmpb;
1284 m4__symbol_chain *chain;
1285 m4_obstack *obs = m4_arg_scratch (context);
1287 /* Quick tests. */
1288 if (sa == &empty_symbol || sb == &empty_symbol)
1289 return sa == sb;
1290 if (m4_is_symbol_value_text (sa) && m4_is_symbol_value_text (sb))
1291 return (m4_get_symbol_value_len (sa) == m4_get_symbol_value_len (sb)
1292 && memcmp (m4_get_symbol_value_text (sa),
1293 m4_get_symbol_value_text (sb),
1294 m4_get_symbol_value_len (sa)) == 0);
1296 /* Convert both arguments to chains, if not one already. */
1297 switch (sa->type)
1299 case M4_SYMBOL_TEXT:
1300 tmpa.next = NULL;
1301 tmpa.type = M4__CHAIN_STR;
1302 tmpa.u.u_s.str = m4_get_symbol_value_text (sa);
1303 tmpa.u.u_s.len = m4_get_symbol_value_len (sa);
1304 break;
1305 case M4_SYMBOL_FUNC:
1306 tmpa.next = NULL;
1307 tmpa.type = M4__CHAIN_FUNC;
1308 tmpa.u.builtin = sa->u.builtin;
1309 break;
1310 case M4_SYMBOL_COMP:
1311 ca = sa->u.u_c.chain;
1312 break;
1313 default:
1314 assert (!"m4_arg_equal");
1315 abort ();
1317 switch (sb->type)
1319 case M4_SYMBOL_TEXT:
1320 tmpb.next = NULL;
1321 tmpb.type = M4__CHAIN_STR;
1322 tmpb.u.u_s.str = m4_get_symbol_value_text (sb);
1323 tmpb.u.u_s.len = m4_get_symbol_value_len (sb);
1324 break;
1325 case M4_SYMBOL_FUNC:
1326 tmpb.next = NULL;
1327 tmpb.type = M4__CHAIN_FUNC;
1328 tmpb.u.builtin = sb->u.builtin;
1329 break;
1330 case M4_SYMBOL_COMP:
1331 cb = sb->u.u_c.chain;
1332 break;
1333 default:
1334 assert (!"m4_arg_equal");
1335 abort ();
1338 /* Compare each link of the chain. */
1339 while (ca && cb)
1341 if (ca->type == M4__CHAIN_ARGV)
1343 tmpa.next = NULL;
1344 tmpa.type = M4__CHAIN_STR;
1345 tmpa.u.u_s.str = NULL;
1346 tmpa.u.u_s.len = 0;
1347 chain = &tmpa;
1348 m4__arg_print (context, obs, ca->u.u_a.argv, ca->u.u_a.index,
1349 m4__quote_cache (M4SYNTAX, NULL, ca->quote_age,
1350 ca->u.u_a.quotes),
1351 argv->flatten || ca->u.u_a.flatten, &chain, NULL,
1352 NULL, false, false);
1353 assert (obstack_object_size (obs) == 0 && chain != &tmpa);
1354 chain->next = ca->next;
1355 ca = tmpa.next;
1356 continue;
1358 if (cb->type == M4__CHAIN_ARGV)
1360 tmpb.next = NULL;
1361 tmpb.type = M4__CHAIN_STR;
1362 tmpb.u.u_s.str = NULL;
1363 tmpb.u.u_s.len = 0;
1364 chain = &tmpb;
1365 m4__arg_print (context, obs, cb->u.u_a.argv, cb->u.u_a.index,
1366 m4__quote_cache (M4SYNTAX, NULL, cb->quote_age,
1367 cb->u.u_a.quotes),
1368 argv->flatten || cb->u.u_a.flatten, &chain, NULL,
1369 NULL, false, false);
1370 assert (obstack_object_size (obs) == 0 && chain != &tmpb);
1371 chain->next = cb->next;
1372 cb = tmpb.next;
1373 continue;
1375 if (ca->type == M4__CHAIN_FUNC)
1377 if (cb->type != M4__CHAIN_FUNC || ca->u.builtin != cb->u.builtin)
1378 return false;
1379 ca = ca->next;
1380 cb = cb->next;
1381 continue;
1383 assert (ca->type == M4__CHAIN_STR && cb->type == M4__CHAIN_STR);
1384 if (ca->u.u_s.len == cb->u.u_s.len)
1386 if (memcmp (ca->u.u_s.str, cb->u.u_s.str, ca->u.u_s.len) != 0)
1387 return false;
1388 ca = ca->next;
1389 cb = cb->next;
1391 else if (ca->u.u_s.len < cb->u.u_s.len)
1393 if (memcmp (ca->u.u_s.str, cb->u.u_s.str, ca->u.u_s.len) != 0)
1394 return false;
1395 tmpb.next = cb->next;
1396 tmpb.u.u_s.str = cb->u.u_s.str + ca->u.u_s.len;
1397 tmpb.u.u_s.len = cb->u.u_s.len - ca->u.u_s.len;
1398 ca = ca->next;
1399 cb = &tmpb;
1401 else
1403 assert (cb->u.u_s.len < ca->u.u_s.len);
1404 if (memcmp (ca->u.u_s.str, cb->u.u_s.str, cb->u.u_s.len) != 0)
1405 return false;
1406 tmpa.next = ca->next;
1407 tmpa.u.u_s.str = ca->u.u_s.str + cb->u.u_s.len;
1408 tmpa.u.u_s.len = ca->u.u_s.len - cb->u.u_s.len;
1409 ca = &tmpa;
1410 cb = cb->next;
1414 /* If we get this far, the two arguments are equal only if both
1415 chains are exhausted. */
1416 assert (ca != cb || !ca);
1417 return ca == cb;
1420 /* Given ARGV, return true if argument ARG is the empty string. This
1421 gives the same result as comparing m4_arg_len against 0, but is
1422 often faster. */
1423 bool
1424 m4_arg_empty (m4_macro_args *argv, size_t arg)
1426 if (!arg)
1428 assert (argv->info);
1429 return !argv->info->name_len;
1431 return m4_arg_symbol (argv, arg) == &empty_symbol;
1434 /* Given ARGV, return the length of argument ARG. Abort if the
1435 argument is not text and FLATTEN is not true. Indices beyond argc
1436 return 0. */
1437 size_t
1438 m4_arg_len (m4 *context, m4_macro_args *argv, size_t arg, bool flatten)
1440 m4_symbol_value *value;
1441 m4__symbol_chain *chain;
1442 size_t len;
1444 if (arg == 0)
1446 assert (argv->info);
1447 return argv->info->name_len;
1449 if (argv->argc <= arg)
1450 return 0;
1451 value = arg_symbol (argv, arg, NULL, flatten);
1452 if (m4_is_symbol_value_text (value))
1453 return m4_get_symbol_value_len (value);
1454 assert (value->type == M4_SYMBOL_COMP);
1455 chain = value->u.u_c.chain;
1456 len = 0;
1457 while (chain)
1459 size_t i;
1460 size_t limit;
1461 const m4_string_pair *quotes;
1462 switch (chain->type)
1464 case M4__CHAIN_STR:
1465 len += chain->u.u_s.len;
1466 break;
1467 case M4__CHAIN_FUNC:
1468 assert (flatten);
1469 break;
1470 case M4__CHAIN_ARGV:
1471 i = chain->u.u_a.index;
1472 limit = chain->u.u_a.argv->argc - i - chain->u.u_a.skip_last;
1473 quotes = m4__quote_cache (M4SYNTAX, NULL, chain->quote_age,
1474 chain->u.u_a.quotes);
1475 assert (limit);
1476 if (quotes)
1477 len += (quotes->len1 + quotes->len2) * limit;
1478 len += limit - 1;
1479 while (limit--)
1480 len += m4_arg_len (context, chain->u.u_a.argv, i++,
1481 flatten || chain->u.u_a.flatten);
1482 break;
1483 default:
1484 assert (!"m4_arg_len");
1485 abort ();
1487 chain = chain->next;
1489 assert (len || flatten);
1490 return len;
1493 /* Given ARGV, return the builtin function referenced by argument ARG.
1494 Abort if it is not a single builtin. */
1495 m4_builtin_func *
1496 m4_arg_func (m4_macro_args *argv, size_t arg)
1498 return m4_get_symbol_value_func (m4_arg_symbol (argv, arg));
1501 /* Dump a representation of ARGV to the obstack OBS, starting with
1502 argument ARG. If QUOTES is non-NULL, each argument is displayed
1503 with those quotes. If FLATTEN, builtins are converted to empty
1504 quotes; if CHAINP, *CHAINP is updated with macro tokens; otherwise,
1505 builtins are represented by their name. Separate arguments with
1506 SEP, which defaults to a comma. If MAX_LEN is non-NULL, truncate
1507 the output after *MAX_LEN bytes are output and return true;
1508 otherwise, return false, and reduce *MAX_LEN by the number of bytes
1509 output. If QUOTE_EACH, the truncation length is reset for each
1510 argument, quotes do not count against length, and all arguments are
1511 printed; otherwise, quotes count against the length and trailing
1512 arguments may be discarded. If MODULE, print any details about
1513 originating modules; modules do not count against truncation
1514 length. MAX_LEN and CHAINP may not both be specified. */
1515 bool
1516 m4__arg_print (m4 *context, m4_obstack *obs, m4_macro_args *argv, size_t arg,
1517 const m4_string_pair *quotes, bool flatten,
1518 m4__symbol_chain **chainp, const char *sep, size_t *max_len,
1519 bool quote_each, bool module)
1521 size_t len = max_len ? *max_len : SIZE_MAX;
1522 size_t i;
1523 bool use_sep = false;
1524 size_t sep_len;
1525 size_t *plen = quote_each ? NULL : &len;
1527 flatten |= argv->flatten;
1528 if (chainp)
1529 assert (!max_len && *chainp);
1530 if (!sep)
1531 sep = ",";
1532 sep_len = strlen (sep);
1533 for (i = arg; i < argv->argc; i++)
1535 if (quote_each && max_len)
1536 len = *max_len;
1537 if (use_sep && m4_shipout_string_trunc (obs, sep, sep_len, NULL, plen))
1538 return true;
1539 use_sep = true;
1540 if (quotes && !quote_each
1541 && m4_shipout_string_trunc (obs, quotes->str1, quotes->len1, NULL,
1542 plen))
1543 return true;
1544 if (m4__symbol_value_print (context, arg_symbol (argv, i, NULL, flatten),
1545 obs, quote_each ? quotes : NULL, flatten,
1546 chainp, &len, module))
1547 return true;
1548 if (quotes && !quote_each
1549 && m4_shipout_string_trunc (obs, quotes->str2, quotes->len2, NULL,
1550 plen))
1551 return true;
1553 if (max_len)
1554 *max_len = len;
1555 else if (chainp)
1556 m4__make_text_link (obs, NULL, chainp);
1557 return false;
1560 /* Create a new argument object using the same obstack as ARGV; thus,
1561 the new object will automatically be freed when the original is
1562 freed. Explicitly set the macro name (argv[0]) from ARGV0 with
1563 length ARGV0_LEN, and discard argv[1] of the wrapped ARGV. If
1564 FLATTEN, any builtins in ARGV are flattened to an empty string when
1565 referenced through the new object. If TRACE, then trace the macro
1566 regardless of global trace state. */
1567 m4_macro_args *
1568 m4_make_argv_ref (m4 *context, m4_macro_args *argv, const char *argv0,
1569 size_t argv0_len, bool flatten, bool trace)
1571 m4_macro_args *new_argv;
1572 m4_symbol_value *value;
1573 m4_symbol_value *new_value;
1574 m4_obstack *obs = m4_arg_scratch (context);
1575 m4_call_info *info;
1577 info = (m4_call_info *) obstack_copy (obs, argv->info, sizeof *info);
1578 new_value = (m4_symbol_value *) obstack_alloc (obs, sizeof *value);
1579 value = make_argv_ref (context, new_value, obs, context->expansion_level - 1,
1580 argv, 2, flatten, NULL);
1581 if (!value)
1583 obstack_free (obs, new_value);
1584 new_argv = (m4_macro_args *) obstack_alloc (obs, offsetof (m4_macro_args,
1585 array));
1586 new_argv->arraylen = 0;
1587 new_argv->wrapper = false;
1588 new_argv->has_ref = false;
1589 new_argv->flatten = false;
1590 new_argv->has_func = false;
1592 else
1594 new_argv = (m4_macro_args *) obstack_alloc (obs, (offsetof (m4_macro_args,
1595 array)
1596 + sizeof value));
1597 new_argv->arraylen = 1;
1598 new_argv->array[0] = value;
1599 new_argv->wrapper = true;
1600 new_argv->has_ref = argv->has_ref;
1601 new_argv->flatten = flatten;
1602 new_argv->has_func = argv->has_func;
1604 new_argv->argc = argv->argc - 1;
1605 new_argv->inuse = false;
1606 new_argv->quote_age = argv->quote_age;
1607 new_argv->info = info;
1608 info->trace = (argv->info->debug_level & M4_DEBUG_TRACE_ALL) || trace;
1609 info->name = argv0;
1610 info->name_len = argv0_len;
1611 new_argv->level = argv->level;
1612 return new_argv;
1615 /* Push argument ARG from ARGV, which must be a text token, onto the
1616 expansion stack OBS for rescanning. */
1617 void
1618 m4_push_arg (m4 *context, m4_obstack *obs, m4_macro_args *argv, size_t arg)
1620 m4_symbol_value value;
1622 if (arg == 0)
1624 assert (argv->info);
1625 m4_set_symbol_value_text (&value, argv->info->name, argv->info->name_len,
1627 if (m4__push_symbol (context, &value, context->expansion_level - 1,
1628 argv->inuse))
1629 arg_mark (argv);
1631 else
1632 m4__push_arg_quote (context, obs, argv, arg, NULL);
1635 /* Push argument ARG from ARGV onto the expansion stack OBS for
1636 rescanning. ARG must be non-zero. QUOTES determines any quote
1637 delimiters that were in effect when the reference was created. */
1638 void
1639 m4__push_arg_quote (m4 *context, m4_obstack *obs, m4_macro_args *argv,
1640 size_t arg, const m4_string_pair *quotes)
1642 size_t level;
1643 m4_symbol_value *value = arg_symbol (argv, arg, &level, false);
1645 if (quotes)
1646 obstack_grow (obs, quotes->str1, quotes->len1);
1647 if (value != &empty_symbol
1648 && m4__push_symbol (context, value, level, argv->inuse))
1649 arg_mark (argv);
1650 if (quotes)
1651 obstack_grow (obs, quotes->str2, quotes->len2);
1654 /* Push series of comma-separated arguments from ARGV onto the
1655 expansion stack OBS for rescanning. If SKIP, then don't push the
1656 first argument. If QUOTE, also push quoting around each arg. */
1657 void
1658 m4_push_args (m4 *context, m4_obstack *obs, m4_macro_args *argv, bool skip,
1659 bool quote)
1661 m4_symbol_value tmp;
1662 m4_symbol_value *value;
1663 size_t i = skip ? 2 : 1;
1664 const m4_string_pair *quotes = m4_get_syntax_quotes (M4SYNTAX);
1666 if (argv->argc <= i)
1667 return;
1669 if (argv->argc == i + 1)
1671 m4__push_arg_quote (context, obs, argv, i, quote ? quotes : NULL);
1672 return;
1675 value = make_argv_ref (context, &tmp, obs, -1, argv, i, argv->flatten,
1676 quote ? quotes : NULL);
1677 assert (value == &tmp);
1678 if (m4__push_symbol (context, value, -1, argv->inuse))
1679 arg_mark (argv);
1682 /* Push arguments from ARGV onto the wrap stack for later rescanning.
1683 If GNU extensions are disabled, only the first argument is pushed;
1684 otherwise, all arguments are pushed and separated with a space. */
1685 void
1686 m4_wrap_args (m4 *context, m4_macro_args *argv)
1688 size_t i;
1689 m4_obstack *obs;
1690 m4_symbol_value *value;
1691 m4__symbol_chain *chain;
1692 m4__symbol_chain **end;
1693 size_t limit = m4_get_posixly_correct_opt (context) ? 2 : argv->argc;
1695 if (limit == 2 && m4_arg_empty (argv, 1))
1696 return;
1698 obs = m4__push_wrapup_init (context, argv->info, &end);
1699 for (i = 1; i < limit; i++)
1701 if (i != 1)
1702 obstack_1grow (obs, ' ');
1703 value = m4_arg_symbol (argv, i);
1704 switch (value->type)
1706 case M4_SYMBOL_TEXT:
1707 obstack_grow (obs, m4_get_symbol_value_text (value),
1708 m4_get_symbol_value_len (value));
1709 break;
1710 case M4_SYMBOL_FUNC:
1711 m4__append_builtin (obs, value->u.builtin, NULL, end);
1712 break;
1713 case M4_SYMBOL_COMP:
1714 chain = value->u.u_c.chain;
1715 while (chain)
1717 switch (chain->type)
1719 case M4__CHAIN_STR:
1720 obstack_grow (obs, chain->u.u_s.str, chain->u.u_s.len);
1721 break;
1722 case M4__CHAIN_FUNC:
1723 m4__append_builtin (obs, chain->u.builtin, NULL, end);
1724 break;
1725 case M4__CHAIN_ARGV:
1726 m4__arg_print (context, obs, chain->u.u_a.argv,
1727 chain->u.u_a.index,
1728 m4__quote_cache (M4SYNTAX, NULL,
1729 chain->quote_age,
1730 chain->u.u_a.quotes),
1731 chain->u.u_a.flatten, end, NULL, NULL, false,
1732 false);
1733 break;
1734 default:
1735 assert (!"m4_wrap_args");
1736 abort ();
1738 chain = chain->next;
1740 break;
1741 default:
1742 assert (!"m4_wrap_args");
1743 abort ();
1746 m4__push_wrapup_finish ();
1750 /* Define these last, so that earlier uses can benefit from the macros
1751 in m4private.h. */
1753 /* Given ARGV, return one greater than the number of arguments it
1754 describes. */
1755 #undef m4_arg_argc
1756 size_t
1757 m4_arg_argc (m4_macro_args *argv)
1759 return argv->argc;
1762 /* Given ARGV, return the call context in effect when argument
1763 collection began. Only safe to call while the macro is being
1764 expanded. */
1765 #undef m4_arg_info
1766 const m4_call_info *
1767 m4_arg_info (m4_macro_args *argv)
1769 assert (argv->info);
1770 return argv->info;
1773 /* Return an obstack useful for scratch calculations, and which will
1774 not interfere with macro expansion. The obstack will be reset when
1775 expand_macro completes. */
1776 #undef m4_arg_scratch
1777 m4_obstack *
1778 m4_arg_scratch (m4 *context)
1780 m4__macro_arg_stacks *stack
1781 = &context->arg_stacks[context->expansion_level - 1];
1782 assert (obstack_object_size (stack->args) == 0);
1783 return stack->args;