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. */
26 #include "m4private.h"
28 /* Define this to 1 see runtime debug info. Implied by DEBUG. */
29 /*#define DEBUG_INPUT 1 */
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
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')
61 Assuming all arguments are large enough to exceed the inlining
62 thresholds of the input engine, the interesting sequence of events
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,
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
;
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
;
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. */
160 m4_macro_expand_input (m4
*context
)
163 m4_symbol_value token
;
167 const char *s
= getenv ("M4_DEBUG_MACRO");
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
))
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. */
193 expand_token (m4
*context
, m4_obstack
*obs
, m4__token_type type
,
194 m4_symbol_value
*token
, int line
, bool first
)
198 const char *text
= (m4_is_symbol_value_text (token
)
199 ? m4_get_symbol_value_text (token
) : NULL
);
204 case M4_TOKEN_MACDEF
:
205 /* Always safe, since there is no text to rescan. */
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
);
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. */
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
);
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
));
249 assert (!m4_has_syntax (M4SYNTAX
, *text
, M4_SYNTAX_LQUOTE
));
254 const char *textp
= text
;
255 size_t len
= m4_get_symbol_value_len (token
);
258 if (m4_has_syntax (M4SYNTAX
, *textp
, M4_SYNTAX_ESCAPE
))
264 symbol
= m4_symbol_lookup (M4SYMTAB
, textp
, len2
);
265 assert (!symbol
|| !m4_is_symbol_void (symbol
));
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
286 assert (!"INTERNAL ERROR: bad token type in expand_token ()");
289 m4_divert_text (context
, obs
, text
, m4_get_symbol_value_len (token
), line
);
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. */
303 expand_argument (m4
*context
, m4_obstack
*obs
, m4_symbol_value
*argp
,
304 const m4_call_info
*caller
)
307 m4_symbol_value token
;
309 int line
= m4_get_current_line (context
);
311 unsigned int age
= m4__quote_age (M4SYNTAX
);
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
);
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
);
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
;
343 obstack_1grow (obs
, '\0');
344 m4_set_symbol_value_text (argp
, obstack_finish (obs
),
348 m4_set_symbol_value_text (argp
, "", len
, 0);
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
;
365 case M4_TOKEN_SIMPLE
:
366 if (type
== M4_TOKEN_OPEN
)
368 else if (type
== M4_TOKEN_CLOSE
)
370 if (!expand_token (context
, obs
, type
, &token
, line
, first
))
375 m4_error (context
, EXIT_FAILURE
, 0, caller
,
376 _("end of file in argument list"));
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
))
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;
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;
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
);
419 assert (type
== M4_TOKEN_COMMA
|| type
== M4_TOKEN_CLOSE
);
420 return type
== M4_TOKEN_COMMA
;
423 assert (!"expand_argument");
427 if (argp
->type
!= M4_SYMBOL_VOID
|| obstack_object_size (obs
))
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. */
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
;
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
];
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);
488 /* Grab the current value of this macro, because it may change while
489 collecting arguments. Likewise, grab any state needed during
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
);
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
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 ();
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
534 m4__arg_adjust_refcount (context
, argv
, false);
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
,
548 obstack_free (stack
->args
, args_base
);
549 obstack_free (stack
->argv
, argv_base
);
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
;
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
);
580 args
.level
= context
->expansion_level
- 1;
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
,
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
);
604 switch (tokenp
->type
)
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
)
615 args
.has_func
= 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);
628 if (tokenp
->u
.u_c
.has_func
)
629 args
.has_func
= true;
632 assert (!"expand_argument");
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
))
645 argv
->arraylen
= args
.arraylen
;
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. */
657 m4_macro_call (m4
*context
, m4_symbol_value
*value
, m4_obstack
*expansion
,
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
,
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
)));
681 assert (!"m4_macro_call");
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,
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
;
705 if (m4_is_syntax_single_dollar (M4SYNTAX
))
706 dollar
= (char *) memchr (text
, M4SYNTAX
->dollar
, len
);
710 while (dollar
!= end
)
712 if (m4_has_syntax (M4SYNTAX
, *dollar
, M4_SYNTAX_DOLLAR
))
721 obstack_grow (obs
, text
, len
);
724 obstack_grow (obs
, text
, dollar
- text
);
725 len
-= dollar
- text
;
729 obstack_1grow (obs
, *dollar
);
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])))
750 i
= (int) strtol (text
, &endp
, 10);
755 m4_push_arg (context
, obs
, argv
, i
);
758 case '#': /* number of arguments */
759 m4_shipout_int (obs
, argc
- 1);
764 case '*': /* all arguments */
765 case '@': /* ... same, but quoted */
766 m4_push_args (context
, obs
, argv
, false, *text
== '@');
772 if (m4_get_posixly_correct_opt (context
)
773 || !VALUE_ARG_SIGNATURE (value
))
775 obstack_1grow (obs
, *dollar
);
784 len1
< len
&& m4_has_syntax (M4SYNTAX
, *endp
,
792 key
= xstrndup (text
, len1
);
796 struct m4_symbol_arg
**arg
797 = (struct m4_symbol_arg
**)
798 m4_hash_lookup (VALUE_ARG_SIGNATURE (value
), key
);
802 i
= SYMBOL_ARG_INDEX (*arg
);
804 m4_shipout_string (context
, obs
, M4ARG (i
), M4ARGLEN (i
),
810 m4_error (context
, 0, 0, argv
->info
,
811 _("unterminated parameter reference: %s"), key
);
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. */
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
);
851 /* Print current tracing line starting at offset START, as returned
852 from an earlier trace_header(), then clear the obstack. */
854 trace_flush (m4
*context
, unsigned int start
)
857 size_t len
= obstack_object_size (&context
->trace_messages
);
858 FILE *file
= m4_get_debug_file (context
);
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
);
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(). */
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. */
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
, ')');
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 (). */
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
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
));
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
);
962 if (debug_macro_level
963 & (increase
? PRINT_REFCOUNT_INCREASE
: PRINT_REFCOUNT_DECREASE
))
964 xfprintf (stderr
, "m4debug: level %zu refcount=%zu\n", level
,
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. */
973 m4__arg_adjust_refcount (m4
*context
, m4_macro_args
*argv
, bool increase
)
976 m4__symbol_chain
*chain
;
977 bool result
= !argv
->inuse
;
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
;
989 if (chain
->u
.u_s
.level
< SIZE_MAX
)
990 m4__adjust_refcount (context
, chain
->u
.u_s
.level
,
996 assert (chain
->u
.u_a
.argv
->inuse
);
997 m4__arg_adjust_refcount (context
, chain
->u
.u_a
.argv
,
1001 assert (!"m4__arg_adjust_refcount");
1004 chain
= chain
->next
;
1007 m4__adjust_refcount (context
, argv
->level
, increase
);
1011 /* Mark ARGV as being in use, along with any $@ references that it
1014 arg_mark (m4_macro_args
*argv
)
1017 m4__symbol_chain
*chain
;
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
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
)
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
)
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
)
1068 m4__push_arg_quote (context
, obs
, argv
, i
+ 1, quotes
);
1069 /* TODO support M4_SYNTAX_COMMA. */
1070 obstack_1grow (obs
, ',');
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;
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
;
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
;
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
,
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
)
1124 m4_symbol_value
*value
;
1128 *level
= argv
->level
;
1129 flatten
|= argv
->flatten
;
1130 if (argv
->argc
<= arg
)
1131 return &empty_symbol
;
1134 value
= argv
->array
[arg
- 1];
1135 if (flatten
&& m4_is_symbol_value_func (value
))
1136 value
= &empty_symbol
;
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
);
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)
1166 /* Given ARGV, return the symbol value at the specified ARG, which
1167 must be non-zero. */
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. */
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
)
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
))
1189 /* Given ARGV, return true if argument ARG is a single builtin
1190 function. Only non-zero indices less than argc can return
1193 m4_is_arg_func (m4_macro_args
*argv
, size_t arg
)
1195 if (arg
== 0 || argv
->argc
<= arg
|| argv
->flatten
|| !argv
->has_func
)
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. */
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
)
1209 value
= m4_arg_symbol (argv
, arg
);
1210 if (value
->type
== M4_SYMBOL_COMP
&& value
->u
.u_c
.has_func
)
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
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
;
1229 assert (argv
->info
);
1230 return argv
->info
->name
;
1232 if (argv
->argc
<= arg
)
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
);
1242 switch (chain
->type
)
1245 obstack_grow (obs
, chain
->u
.u_s
.str
, chain
->u
.u_s
.len
);
1247 case M4__CHAIN_FUNC
:
1250 assert (!"m4_arg_text");
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);
1261 assert (!"m4_arg_text");
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)). */
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
);
1288 if (sa
== &empty_symbol
|| sb
== &empty_symbol
)
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. */
1299 case M4_SYMBOL_TEXT
:
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
);
1305 case M4_SYMBOL_FUNC
:
1307 tmpa
.type
= M4__CHAIN_FUNC
;
1308 tmpa
.u
.builtin
= sa
->u
.builtin
;
1310 case M4_SYMBOL_COMP
:
1311 ca
= sa
->u
.u_c
.chain
;
1314 assert (!"m4_arg_equal");
1319 case M4_SYMBOL_TEXT
:
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
);
1325 case M4_SYMBOL_FUNC
:
1327 tmpb
.type
= M4__CHAIN_FUNC
;
1328 tmpb
.u
.builtin
= sb
->u
.builtin
;
1330 case M4_SYMBOL_COMP
:
1331 cb
= sb
->u
.u_c
.chain
;
1334 assert (!"m4_arg_equal");
1338 /* Compare each link of the chain. */
1341 if (ca
->type
== M4__CHAIN_ARGV
)
1344 tmpa
.type
= M4__CHAIN_STR
;
1345 tmpa
.u
.u_s
.str
= NULL
;
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
,
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
;
1358 if (cb
->type
== M4__CHAIN_ARGV
)
1361 tmpb
.type
= M4__CHAIN_STR
;
1362 tmpb
.u
.u_s
.str
= NULL
;
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
,
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
;
1375 if (ca
->type
== M4__CHAIN_FUNC
)
1377 if (cb
->type
!= M4__CHAIN_FUNC
|| ca
->u
.builtin
!= cb
->u
.builtin
)
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)
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)
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
;
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)
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
;
1414 /* If we get this far, the two arguments are equal only if both
1415 chains are exhausted. */
1416 assert (ca
!= cb
|| !ca
);
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
1424 m4_arg_empty (m4_macro_args
*argv
, size_t 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
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
;
1446 assert (argv
->info
);
1447 return argv
->info
->name_len
;
1449 if (argv
->argc
<= arg
)
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
;
1461 const m4_string_pair
*quotes
;
1462 switch (chain
->type
)
1465 len
+= chain
->u
.u_s
.len
;
1467 case M4__CHAIN_FUNC
:
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
);
1477 len
+= (quotes
->len1
+ quotes
->len2
) * limit
;
1480 len
+= m4_arg_len (context
, chain
->u
.u_a
.argv
, i
++,
1481 flatten
|| chain
->u
.u_a
.flatten
);
1484 assert (!"m4_arg_len");
1487 chain
= chain
->next
;
1489 assert (len
|| flatten
);
1493 /* Given ARGV, return the builtin function referenced by argument ARG.
1494 Abort if it is not a single builtin. */
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. */
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
;
1523 bool use_sep
= false;
1525 size_t *plen
= quote_each
? NULL
: &len
;
1527 flatten
|= argv
->flatten
;
1529 assert (!max_len
&& *chainp
);
1532 sep_len
= strlen (sep
);
1533 for (i
= arg
; i
< argv
->argc
; i
++)
1535 if (quote_each
&& max_len
)
1537 if (use_sep
&& m4_shipout_string_trunc (obs
, sep
, sep_len
, NULL
, plen
))
1540 if (quotes
&& !quote_each
1541 && m4_shipout_string_trunc (obs
, quotes
->str1
, quotes
->len1
, NULL
,
1544 if (m4__symbol_value_print (context
, arg_symbol (argv
, i
, NULL
, flatten
),
1545 obs
, quote_each
? quotes
: NULL
, flatten
,
1546 chainp
, &len
, module
))
1548 if (quotes
&& !quote_each
1549 && m4_shipout_string_trunc (obs
, quotes
->str2
, quotes
->len2
, NULL
,
1556 m4__make_text_link (obs
, NULL
, chainp
);
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. */
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
);
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
);
1583 obstack_free (obs
, new_value
);
1584 new_argv
= (m4_macro_args
*) obstack_alloc (obs
, offsetof (m4_macro_args
,
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;
1594 new_argv
= (m4_macro_args
*) obstack_alloc (obs
, (offsetof (m4_macro_args
,
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
;
1610 info
->name_len
= argv0_len
;
1611 new_argv
->level
= argv
->level
;
1615 /* Push argument ARG from ARGV, which must be a text token, onto the
1616 expansion stack OBS for rescanning. */
1618 m4_push_arg (m4
*context
, m4_obstack
*obs
, m4_macro_args
*argv
, size_t arg
)
1620 m4_symbol_value value
;
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,
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. */
1639 m4__push_arg_quote (m4
*context
, m4_obstack
*obs
, m4_macro_args
*argv
,
1640 size_t arg
, const m4_string_pair
*quotes
)
1643 m4_symbol_value
*value
= arg_symbol (argv
, arg
, &level
, false);
1646 obstack_grow (obs
, quotes
->str1
, quotes
->len1
);
1647 if (value
!= &empty_symbol
1648 && m4__push_symbol (context
, value
, level
, argv
->inuse
))
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. */
1658 m4_push_args (m4
*context
, m4_obstack
*obs
, m4_macro_args
*argv
, bool skip
,
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
)
1669 if (argv
->argc
== i
+ 1)
1671 m4__push_arg_quote (context
, obs
, argv
, i
, quote
? quotes
: NULL
);
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
))
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. */
1686 m4_wrap_args (m4
*context
, m4_macro_args
*argv
)
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))
1698 obs
= m4__push_wrapup_init (context
, argv
->info
, &end
);
1699 for (i
= 1; i
< limit
; i
++)
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
));
1710 case M4_SYMBOL_FUNC
:
1711 m4__append_builtin (obs
, value
->u
.builtin
, NULL
, end
);
1713 case M4_SYMBOL_COMP
:
1714 chain
= value
->u
.u_c
.chain
;
1717 switch (chain
->type
)
1720 obstack_grow (obs
, chain
->u
.u_s
.str
, chain
->u
.u_s
.len
);
1722 case M4__CHAIN_FUNC
:
1723 m4__append_builtin (obs
, chain
->u
.builtin
, NULL
, end
);
1725 case M4__CHAIN_ARGV
:
1726 m4__arg_print (context
, obs
, chain
->u
.u_a
.argv
,
1728 m4__quote_cache (M4SYNTAX
, NULL
,
1730 chain
->u
.u_a
.quotes
),
1731 chain
->u
.u_a
.flatten
, end
, NULL
, NULL
, false,
1735 assert (!"m4_wrap_args");
1738 chain
= chain
->next
;
1742 assert (!"m4_wrap_args");
1746 m4__push_wrapup_finish ();
1750 /* Define these last, so that earlier uses can benefit from the macros
1753 /* Given ARGV, return one greater than the number of arguments it
1757 m4_arg_argc (m4_macro_args
*argv
)
1762 /* Given ARGV, return the call context in effect when argument
1763 collection began. Only safe to call while the macro is being
1766 const m4_call_info
*
1767 m4_arg_info (m4_macro_args
*argv
)
1769 assert (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
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);