1 /* GNU m4 -- A simple macro processor
2 Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 2001, 2006, 2007,
3 2008, 2009 Free Software Foundation, Inc.
5 This file is part of GNU M4.
7 GNU M4 is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU M4 is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 /* This file contains the functions that perform the basic argument
22 parsing and macro expansion. */
26 #include "m4private.h"
30 /* Define this to 1 see runtime debug info. Implied by DEBUG. */
31 /*#define DEBUG_INPUT 1 */
33 # define DEBUG_MACRO 0
34 #endif /* DEBUG_MACRO */
36 /* A note on argument memory lifetimes: We use an internal struct
37 (m4__macro_args_stacks) to maintain list of argument obstacks.
38 Within a recursion level, consecutive macros can share a stack, but
39 distinct recursion levels need different stacks since the nested
40 macro is interrupting the argument collection of the outer level.
41 Note that a reference can live as long as the expansion containing
42 the reference can participate as an argument in a future macro
45 Therefore, we implement a reference counter for each expansion
46 level, tracking how many references exist into the obstack, as well
47 as associate a level with each reference. Of course, expand_macro
48 is actively using argv, so it increments the refcount on entry and
49 decrements it on exit. Additionally, any time the input engine is
50 handed a reference that it does not inline, it increases the
51 refcount in push_token, then decreases it in pop_input once the
52 reference has been rescanned. Finally, when the input engine hands
53 a reference back to expand_argument, the refcount increases, which
54 is then cleaned up at the end of expand_macro.
56 For a running example, consider this input:
58 define(a,A)define(b,`a(`$1')')define(c,$*)dnl
59 define(x,`a(1)`'c($@')define(y,`$@)')dnl
60 x(a(`b')``a'')y(`b')(`a')
63 Assuming all arguments are large enough to exceed the inlining
64 thresholds of the input engine, the interesting sequence of events
67 stacks[0] refs stacks[1] refs
68 after second dnl ends: `' 0 `' 0
69 expand_macro for x, level 0: `' 1 `' 0
70 expand_macro for a, level 1: `' 1 `' 1
71 after collect_arguments for a: `' 1 `b' 1
72 push `A' to input stack: `' 1 `b' 1
73 exit expand_macro for a: `' 1 `' 0
74 after collect_arguments for x: `A`a'' 1 `' 0
75 push `a(1)`'c(' to input stack: `A`a'' 1 `' 0
76 push_token saves $@(x) ref: `A`a'' 2 `' 0
77 exit expand_macro for x: `A`a'' 1 `' 0
78 expand_macro for a, level 0: `A`a'' 2 `' 0
79 after collect_arguments for a: `A`a''`1' 2 `' 0
80 push `A' to input stack: `A`a''`1' 2 `' 0
81 exit expand_macro for a: `A`a'' 1 `' 0
82 output `A': `A`a'' 1 `' 0
83 expand_macro for c, level 0: `A`a'' 2 `' 0
84 expand_argument gets $@(x) ref: `A`a''`$@(x)' 3 `' 0
85 pop_input ends $@(x) ref: `A`a''`$@(x)' 2 `' 0
86 expand_macro for y, level 1: `A`a''`$@(x)' 2 `' 1
87 after collect_arguments for y: `A`a''`$@(x)' 2 `b' 1
88 push_token saves $@(y) ref: `A`a''`$@(x)' 2 `b' 2
89 push `)' to input stack: `A`a''`$@(x)' 2 `b' 2
90 exit expand_macro for y: `A`a''`$@(x)' 2 `b' 1
91 expand_argument gets $@(y) ref: `A`a''`$@(x)$@(y)' 2 `b' 2
92 pop_input ends $@(y) ref: `A`a''`$@(x)$@(y)' 2 `b' 1
93 after collect_arguments for c: `A`a''`$@(x)$@(y)' 2 `b' 1
94 push_token saves $*(c) ref: `A`a''`$@(x)$@(y)' 3 `b' 2
95 expand_macro frees $@(x) ref: `A`a''`$@(x)$@(y)' 2 `b' 2
96 expand_macro frees $@(y) ref: `A`a''`$@(x)$@(y)' 2 `b' 1
97 exit expand_macro for c: `A`a''`$@(x)$@(y)' 1 `b' 1
98 output `Aa': `A`a''`$@(x)$@(y)' 0 `b' 1
99 pop_input ends $*(c)$@(x) ref: `' 0 `b' 1
100 expand_macro for b, level 0: `' 1 `b' 1
101 pop_input ends $*(c)$@(y) ref: `' 1 `' 0
102 after collect_arguments for b: `a' 1 `' 0
103 push `a(`' to input stack: `a' 1 `' 0
104 push_token saves $1(b) ref: `a' 2 `' 0
105 push `')' to input stack: `a' 2 `' 0
106 exit expand_macro for b: `a' 1 `' 0
107 expand_macro for a, level 0 : `a' 2 `' 0
108 expand_argument gets $1(b) ref: `a'`$1(b)' 3 `' 0
109 pop_input ends $1(b) ref: `a'`$1(b)' 2 `' 0
110 after collect_arguments for a: `a'`$1(b)' 2 `' 0
111 push `A' to input stack: `a'`$1(b)' 2 `' 0
112 expand_macro frees $1(b) ref: `a'`$1(b)' 1 `' 0
113 exit expand_macro for a: `' 0 `' 0
114 output `A': `' 0 `' 0
116 An obstack is only completely cleared when its refcount reaches
117 zero. However, as an optimization, expand_macro also frees
118 anything that it added to the obstack if no additional references
119 were added at the current expansion level, to reduce the amount of
120 memory left on the obstack while waiting for refcounts to drop.
123 static m4_macro_args
*collect_arguments (m4
*, m4_call_info
*, m4_symbol
*,
124 m4_obstack
*, m4_obstack
*);
125 static void expand_macro (m4
*, const char *, size_t, m4_symbol
*);
126 static bool expand_token (m4
*, m4_obstack
*, m4__token_type
,
127 m4_symbol_value
*, int, bool);
128 static bool expand_argument (m4
*, m4_obstack
*, m4_symbol_value
*,
129 const m4_call_info
*);
130 static void process_macro (m4
*, m4_symbol_value
*, m4_obstack
*, int,
133 static unsigned int trace_pre (m4
*, m4_macro_args
*);
134 static void trace_post (m4
*, unsigned int, const m4_call_info
*);
135 static unsigned int trace_header (m4
*, const m4_call_info
*);
136 static void trace_flush (m4
*, unsigned int);
139 /* The number of the current call of expand_macro (). */
140 static size_t macro_call_id
= 0;
142 /* A placeholder symbol value representing the empty string, used to
143 optimize checks for emptiness. */
144 static m4_symbol_value empty_symbol
;
147 /* True if significant changes to stacks should be printed to the
148 trace stream. Primarily useful for debugging $@ ref memory leaks,
149 and controlled by M4_DEBUG_MACRO environment variable. */
150 static int debug_macro_level
;
152 # define debug_macro_level 0
153 #endif /* !DEBUG_MACRO */
154 #define PRINT_ARGCOUNT_CHANGES 1 /* Any change to argcount > 1. */
155 #define PRINT_REFCOUNT_INCREASE 2 /* Any increase to refcount. */
156 #define PRINT_REFCOUNT_DECREASE 4 /* Any decrease to refcount. */
160 /* This function reads all input, and expands each token, one at a time. */
162 m4_macro_expand_input (m4
*context
)
165 m4_symbol_value token
;
169 const char *s
= getenv ("M4_DEBUG_MACRO");
171 debug_macro_level
= strtol (s
, NULL
, 0);
172 #endif /* DEBUG_MACRO */
174 m4_set_symbol_value_text (&empty_symbol
, "", 0, 0);
175 VALUE_MAX_ARGS (&empty_symbol
) = -1;
177 while ((type
= m4__next_token (context
, &token
, &line
, NULL
, false, NULL
))
179 expand_token (context
, NULL
, type
, &token
, line
, true);
183 /* Expand one token onto OBS, according to its type. If OBS is NULL,
184 output the expansion to the current diversion. TYPE determines the
185 contents of TOKEN. Potential macro names (a TYPE of M4_TOKEN_WORD)
186 are looked up in the symbol table, to see if they have a macro
187 definition. If they have, they are expanded as macros, otherwise
188 the text are just copied to the output. LINE determines where
189 TOKEN began. FIRST is true if there is no prior content in the
190 current macro argument. Return true if the result is guranteed to
191 give the same parse on rescan in a quoted context with the same
192 quote age. Returning false is always safe, although it may lead to
193 slower performance. */
195 expand_token (m4
*context
, m4_obstack
*obs
, m4__token_type type
,
196 m4_symbol_value
*token
, int line
, bool first
)
200 const char *text
= (m4_is_symbol_value_text (token
)
201 ? m4_get_symbol_value_text (token
) : NULL
);
206 case M4_TOKEN_MACDEF
:
207 /* Always safe, since there is no text to rescan. */
210 case M4_TOKEN_STRING
:
211 /* Strings are safe in isolation (since quote_age detects any
212 change in delimiters), or when safe_quotes is true. This is
213 also returned for sequences of benign characters, such as
214 digits. When safe_quotes is false, we could technically
215 return true if we can prove that the concatenation of this
216 string to prior text does not form a multi-byte quote
217 delimiter, but that is a lot of overhead, so we give the
218 conservative answer of false. */
219 result
= first
|| m4__safe_quotes (M4SYNTAX
);
221 case M4_TOKEN_COMMENT
:
222 /* Comments can contain unbalanced quote delimiters. Rather
223 than search for one, we return the conservative answer of
224 false. If obstack is provided, the string or comment was
225 already expanded into it during next_token. */
234 /* If safe_quotes is true, then these do not form a quote
235 delimiter. If it is false, we give the conservative answer
236 of false rather than taking time to prove that no multi-byte
237 quote delimiter is formed. */
238 result
= m4__safe_quotes (M4SYNTAX
);
241 case M4_TOKEN_SIMPLE
:
242 /* If safe_quotes is true, then all but the single-byte end
243 quote delimiter is safe in a quoted context; a single-byte
244 start delimiter will trigger M4_TOKEN_STRING instead. If
245 safe_quotes is false, we give the conservative answer of
246 false rather than taking time to prove that no multi-byte
247 quote delimiter is formed. */
248 result
= (!m4_has_syntax (M4SYNTAX
, *text
, M4_SYNTAX_RQUOTE
)
249 && m4__safe_quotes (M4SYNTAX
));
251 assert (!m4_has_syntax (M4SYNTAX
, *text
, M4_SYNTAX_LQUOTE
));
256 const char *textp
= text
;
257 size_t len
= m4_get_symbol_value_len (token
);
260 if (m4_has_syntax (M4SYNTAX
, *textp
, M4_SYNTAX_ESCAPE
))
266 symbol
= m4_symbol_lookup (M4SYMTAB
, textp
, len2
);
267 assert (!symbol
|| !m4_is_symbol_void (symbol
));
269 || (symbol
->value
->type
== M4_SYMBOL_FUNC
270 && BIT_TEST (SYMBOL_FLAGS (symbol
), VALUE_BLIND_ARGS_BIT
)
271 && !m4__next_token_is_open (context
)))
273 m4_divert_text (context
, obs
, text
, len
, line
);
274 /* If safe_quotes is true, then words do not overlap with
275 quote delimiters. If it is false, we give the
276 conservative answer of false rather than prove that no
277 multi-byte delimiters are formed. */
278 return m4__safe_quotes (M4SYNTAX
);
280 expand_macro (context
, textp
, len2
, symbol
);
281 /* Expanding a macro may create new tokens to scan, and those
282 tokens may generate unsafe text, but we did not append any
288 assert (!"INTERNAL ERROR: bad token type in expand_token ()");
291 m4_divert_text (context
, obs
, text
, m4_get_symbol_value_len (token
), line
);
296 /* This function parses one argument to a macro call. It expects the
297 first left parenthesis or the separating comma to have been read by
298 the caller. It skips leading whitespace, then reads and expands
299 tokens, until it finds a comma or a right parenthesis at the same
300 level of parentheses. It returns a flag indicating whether the
301 argument read is the last for the active macro call. The arguments
302 are built on the obstack OBS, indirectly through expand_token ().
303 Report errors on behalf of CALLER. */
305 expand_argument (m4
*context
, m4_obstack
*obs
, m4_symbol_value
*argp
,
306 const m4_call_info
*caller
)
309 m4_symbol_value token
;
311 int line
= m4_get_current_line (context
);
313 unsigned int age
= m4__quote_age (M4SYNTAX
);
316 memset (argp
, '\0', sizeof *argp
);
317 VALUE_MAX_ARGS (argp
) = -1;
319 /* Skip leading white space. */
322 type
= m4__next_token (context
, &token
, NULL
, obs
, true, caller
);
324 while (type
== M4_TOKEN_SPACE
);
328 if (VALUE_MIN_ARGS (argp
) < VALUE_MIN_ARGS (&token
))
329 VALUE_MIN_ARGS (argp
) = VALUE_MIN_ARGS (&token
);
330 if (VALUE_MAX_ARGS (&token
) < VALUE_MAX_ARGS (argp
))
331 VALUE_MAX_ARGS (argp
) = VALUE_MAX_ARGS (&token
);
336 if (paren_level
== 0)
338 assert (argp
->type
!= M4_SYMBOL_FUNC
);
339 if (argp
->type
!= M4_SYMBOL_COMP
)
341 len
= obstack_object_size (obs
);
342 VALUE_MODULE (argp
) = NULL
;
345 obstack_1grow (obs
, '\0');
346 m4_set_symbol_value_text (argp
, obstack_finish (obs
),
350 m4_set_symbol_value_text (argp
, "", len
, 0);
354 m4__make_text_link (obs
, NULL
, &argp
->u
.u_c
.end
);
355 if (argp
->u
.u_c
.chain
== argp
->u
.u_c
.end
356 && argp
->u
.u_c
.chain
->type
== M4__CHAIN_FUNC
)
358 const m4__builtin
*func
= argp
->u
.u_c
.chain
->u
.builtin
;
359 argp
->type
= M4_SYMBOL_FUNC
;
360 argp
->u
.builtin
= func
;
363 return type
== M4_TOKEN_COMMA
;
367 case M4_TOKEN_SIMPLE
:
368 if (type
== M4_TOKEN_OPEN
)
370 else if (type
== M4_TOKEN_CLOSE
)
372 if (!expand_token (context
, obs
, type
, &token
, line
, first
))
377 m4_error (context
, EXIT_FAILURE
, 0, caller
,
378 _("end of file in argument list"));
383 case M4_TOKEN_STRING
:
384 case M4_TOKEN_COMMENT
:
385 case M4_TOKEN_MACDEF
:
386 if (!expand_token (context
, obs
, type
, &token
, line
, first
))
388 if (token
.type
== M4_SYMBOL_COMP
)
390 if (argp
->type
!= M4_SYMBOL_COMP
)
392 argp
->type
= M4_SYMBOL_COMP
;
393 argp
->u
.u_c
.chain
= token
.u
.u_c
.chain
;
394 argp
->u
.u_c
.wrapper
= argp
->u
.u_c
.has_func
= false;
398 assert (argp
->u
.u_c
.end
);
399 argp
->u
.u_c
.end
->next
= token
.u
.u_c
.chain
;
401 argp
->u
.u_c
.end
= token
.u
.u_c
.end
;
402 if (token
.u
.u_c
.has_func
)
403 argp
->u
.u_c
.has_func
= true;
408 assert (paren_level
== 0 && argp
->type
== M4_SYMBOL_VOID
409 && obstack_object_size (obs
) == 0
410 && token
.u
.u_c
.chain
== token
.u
.u_c
.end
411 && token
.u
.u_c
.chain
->quote_age
== age
412 && token
.u
.u_c
.chain
->type
== M4__CHAIN_ARGV
);
413 argp
->type
= M4_SYMBOL_COMP
;
414 argp
->u
.u_c
.chain
= argp
->u
.u_c
.end
= token
.u
.u_c
.chain
;
415 argp
->u
.u_c
.wrapper
= true;
416 argp
->u
.u_c
.has_func
= token
.u
.u_c
.has_func
;
417 type
= m4__next_token (context
, &token
, NULL
, NULL
, false, caller
);
418 if (argp
->u
.u_c
.chain
->u
.u_a
.skip_last
)
419 assert (type
== M4_TOKEN_COMMA
);
421 assert (type
== M4_TOKEN_COMMA
|| type
== M4_TOKEN_CLOSE
);
422 return type
== M4_TOKEN_COMMA
;
425 assert (!"expand_argument");
429 if (argp
->type
!= M4_SYMBOL_VOID
|| obstack_object_size (obs
))
431 type
= m4__next_token (context
, &token
, NULL
, obs
, first
, caller
);
436 /* The macro expansion is handled by expand_macro (). It parses the
437 arguments, using collect_arguments (), and builds a table of pointers to
438 the arguments. The arguments themselves are stored on a local obstack.
439 Expand_macro () uses m4_macro_call () to do the call of the macro.
441 Expand_macro () is potentially recursive, since it calls expand_argument
442 (), which might call expand_token (), which might call expand_macro ().
444 NAME points to storage on the token stack, so it is only valid
445 until a call to collect_arguments parses more tokens. SYMBOL is
446 the result of the symbol table lookup on NAME. */
448 expand_macro (m4
*context
, const char *name
, size_t len
, m4_symbol
*symbol
)
450 void *args_base
; /* Base of stack->args on entry. */
451 void *args_scratch
; /* Base of scratch space for m4_macro_call. */
452 void *argv_base
; /* Base of stack->argv on entry. */
453 m4_macro_args
*argv
; /* Arguments to the called macro. */
454 m4_obstack
*expansion
; /* Collects the macro's expansion. */
455 m4_symbol_value
*value
; /* Original value of this macro. */
456 size_t level
; /* Expansion level of this macro. */
457 m4__macro_arg_stacks
*stack
; /* Storage for this macro. */
458 m4_call_info info
; /* Context of this macro call. */
460 /* Obstack preparation. */
461 level
= context
->expansion_level
;
462 if (context
->stacks_count
<= level
)
464 size_t count
= context
->stacks_count
;
466 = (m4__macro_arg_stacks
*) x2nrealloc (context
->arg_stacks
,
467 &context
->stacks_count
,
468 sizeof *context
->arg_stacks
);
469 memset (&context
->arg_stacks
[count
], 0,
470 sizeof *context
->arg_stacks
* (context
->stacks_count
- count
));
472 stack
= &context
->arg_stacks
[level
];
475 assert (!stack
->refcount
);
476 stack
->args
= (m4_obstack
*) xmalloc (sizeof *stack
->args
);
477 stack
->argv
= (m4_obstack
*) xmalloc (sizeof *stack
->argv
);
478 obstack_init (stack
->args
);
479 obstack_init (stack
->argv
);
480 stack
->args_base
= obstack_finish (stack
->args
);
481 stack
->argv_base
= obstack_finish (stack
->argv
);
483 assert (obstack_object_size (stack
->args
) == 0
484 && obstack_object_size (stack
->argv
) == 0);
485 args_base
= obstack_finish (stack
->args
);
486 argv_base
= obstack_finish (stack
->argv
);
487 m4__adjust_refcount (context
, level
, true);
490 /* Grab the current value of this macro, because it may change while
491 collecting arguments. Likewise, grab any state needed during
493 value
= m4_get_symbol_value (symbol
);
494 info
.file
= m4_get_current_file (context
);
495 info
.line
= m4_get_current_line (context
);
496 info
.call_id
= ++macro_call_id
;
497 info
.trace
= (m4_is_debug_bit (context
, M4_DEBUG_TRACE_ALL
)
498 || m4_get_symbol_traced (symbol
));
499 info
.debug_level
= m4_get_debug_level_opt (context
);
503 /* Prepare for macro expansion. */
504 VALUE_PENDING (value
)++;
505 if (m4_get_nesting_limit_opt (context
) < ++context
->expansion_level
)
506 m4_error (context
, EXIT_FAILURE
, 0, NULL
, _("\
507 recursion limit of %zu exceeded, use -L<N> to change it"),
508 m4_get_nesting_limit_opt (context
));
510 m4_trace_prepare (context
, &info
, value
);
511 argv
= collect_arguments (context
, &info
, symbol
, stack
->args
, stack
->argv
);
512 /* Since collect_arguments can invalidate stack by reallocating
513 context->arg_stacks during a recursive expand_macro call, we must
515 stack
= &context
->arg_stacks
[level
];
516 args_scratch
= obstack_finish (stack
->args
);
518 /* The actual macro call. */
519 expansion
= m4_push_string_init (context
, info
.file
, info
.line
);
520 m4_macro_call (context
, value
, expansion
, argv
);
521 m4_push_string_finish ();
526 --context
->expansion_level
;
527 --VALUE_PENDING (value
);
528 if (BIT_TEST (VALUE_FLAGS (value
), VALUE_DELETED_BIT
))
529 m4_symbol_value_delete (value
);
531 /* We no longer need argv, so reduce the refcount. Additionally, if
532 no other references to argv were created, we can free our portion
533 of the obstack, although we must leave earlier content alone. A
534 refcount of 0 implies that adjust_refcount already freed the
536 m4__arg_adjust_refcount (context
, argv
, false);
541 obstack_free (stack
->args
, args_scratch
);
542 if (debug_macro_level
& PRINT_ARGCOUNT_CHANGES
)
543 xfprintf (stderr
, "m4debug: -%zu- `%s' in use, level=%zu, "
544 "refcount=%zu, argcount=%zu\n", info
.call_id
,
545 argv
->info
->name
, level
, stack
->refcount
,
550 obstack_free (stack
->args
, args_base
);
551 obstack_free (stack
->argv
, argv_base
);
557 /* Collect all the arguments to a call of the macro SYMBOL, with call
558 context INFO. The arguments are stored on the obstack ARGUMENTS
559 and a table of pointers to the arguments on ARGV_STACK. Return the
560 object describing all of the macro arguments. */
561 static m4_macro_args
*
562 collect_arguments (m4
*context
, m4_call_info
*info
, m4_symbol
*symbol
,
563 m4_obstack
*arguments
, m4_obstack
*argv_stack
)
565 m4_symbol_value token
;
566 m4_symbol_value
*tokenp
;
573 args
.wrapper
= false;
574 args
.has_ref
= false;
575 args
.flatten
= m4_symbol_flatten_args (symbol
);
576 args
.has_func
= false;
577 /* Must copy here, since we are consuming tokens, and since symbol
578 table can be changed during argument collection. */
579 info
->name
= (char *) obstack_copy0 (arguments
, info
->name
, info
->name_len
);
580 args
.quote_age
= m4__quote_age (M4SYNTAX
);
582 args
.level
= context
->expansion_level
- 1;
584 obstack_grow (argv_stack
, &args
, offsetof (m4_macro_args
, array
));
586 if (m4__next_token_is_open (context
))
588 /* Gobble parenthesis, then collect arguments. */
589 m4__next_token (context
, &token
, NULL
, NULL
, false, info
);
592 tokenp
= (m4_symbol_value
*) obstack_alloc (arguments
,
594 more_args
= expand_argument (context
, arguments
, tokenp
, info
);
596 if ((m4_is_symbol_value_text (tokenp
)
597 && !m4_get_symbol_value_len (tokenp
))
598 || (args
.flatten
&& m4_is_symbol_value_func (tokenp
)))
600 obstack_free (arguments
, tokenp
);
601 tokenp
= &empty_symbol
;
603 obstack_ptr_grow (argv_stack
, tokenp
);
606 switch (tokenp
->type
)
609 /* Be conservative - any change in quoting while
610 collecting arguments, or any unsafe argument, will
611 require a rescan if $@ is reused. */
612 if (m4_get_symbol_value_len (tokenp
)
613 && m4_get_symbol_value_quote_age (tokenp
) != args
.quote_age
)
617 args
.has_func
= true;
621 if (tokenp
->u
.u_c
.wrapper
)
623 assert (tokenp
->u
.u_c
.chain
->type
== M4__CHAIN_ARGV
624 && !tokenp
->u
.u_c
.chain
->next
);
625 args
.argc
+= (tokenp
->u
.u_c
.chain
->u
.u_a
.argv
->argc
626 - tokenp
->u
.u_c
.chain
->u
.u_a
.index
627 - tokenp
->u
.u_c
.chain
->u
.u_a
.skip_last
- 1);
630 if (tokenp
->u
.u_c
.has_func
)
631 args
.has_func
= true;
634 assert (!"expand_argument");
640 argv
= (m4_macro_args
*) obstack_finish (argv_stack
);
641 argv
->argc
= args
.argc
;
642 argv
->wrapper
= args
.wrapper
;
643 argv
->has_ref
= args
.has_ref
;
644 argv
->has_func
= args
.has_func
;
645 if (args
.quote_age
!= m4__quote_age (M4SYNTAX
))
647 argv
->arraylen
= args
.arraylen
;
652 /* The actual call of a macro is handled by m4_macro_call ().
653 m4_macro_call () is passed a symbol VALUE, whose type is used to
654 call either a builtin function, or the user macro expansion
655 function process_macro (). The arguments are provided by the ARGV
656 table. The expansion is left on the obstack EXPANSION. Macro
657 tracing is also handled here. */
659 m4_macro_call (m4
*context
, m4_symbol_value
*value
, m4_obstack
*expansion
,
662 unsigned int trace_start
= 0;
664 if (argv
->info
->trace
)
665 trace_start
= trace_pre (context
, argv
);
666 if (!m4_bad_argc (context
, argv
->argc
, argv
->info
,
667 VALUE_MIN_ARGS (value
), VALUE_MAX_ARGS (value
),
668 BIT_TEST (VALUE_FLAGS (value
),
669 VALUE_SIDE_EFFECT_ARGS_BIT
)))
671 if (m4_is_symbol_value_text (value
))
672 process_macro (context
, value
, expansion
, argv
->argc
, argv
);
673 else if (m4_is_symbol_value_func (value
))
674 m4_get_symbol_value_func (value
) (context
, expansion
, argv
->argc
,
676 else if (m4_is_symbol_value_placeholder (value
))
677 m4_warn (context
, 0, argv
->info
,
678 _("builtin %s requested by frozen file not found"),
679 quotearg_style (locale_quoting_style
,
680 m4_get_symbol_value_placeholder (value
)));
683 assert (!"m4_macro_call");
687 if (argv
->info
->trace
)
688 trace_post (context
, trace_start
, argv
->info
);
691 /* Locate the next byte with dollar syntax in string STR of length
692 LEN, or return NULL. */
694 locate_dollar (m4
*context
, const char *str
, size_t len
)
696 if (m4_is_syntax_single_dollar (M4SYNTAX
))
697 return (char *) memchr (str
, M4SYNTAX
->dollar
, len
);
700 if (m4_has_syntax (M4SYNTAX
, *str
, M4_SYNTAX_DOLLAR
))
707 /* This function handles all expansion of user defined and predefined
708 macros. It is called with an obstack OBS, where the macros expansion
709 will be placed, as an unfinished object. SYMBOL points to the macro
710 definition, giving the expansion text. ARGC and ARGV are the arguments,
713 process_macro (m4
*context
, m4_symbol_value
*value
, m4_obstack
*obs
,
714 int argc
, m4_macro_args
*argv
)
716 const char *text
= m4_get_symbol_value_text (value
);
717 size_t len
= m4_get_symbol_value_len (value
);
719 const char *dollar
= locate_dollar (context
, text
, len
);
723 obstack_grow (obs
, text
, dollar
- text
);
724 len
-= dollar
- text
;
731 case '0': case '1': case '2': case '3': case '4':
732 case '5': case '6': case '7': case '8': case '9':
733 /* FIXME - multidigit arguments should convert over to ${10}
734 syntax instead of $10; see
735 http://lists.gnu.org/archive/html/m4-discuss/2006-08/msg00028.html
736 for more discussion. */
737 if (m4_get_posixly_correct_opt (context
) || !isdigit(text
[1]))
745 i
= (int) strtol (text
, &endp
, 10);
750 m4_push_arg (context
, obs
, argv
, i
);
753 case '#': /* number of arguments */
754 m4_shipout_int (obs
, argc
- 1);
759 case '*': /* all arguments */
760 case '@': /* ... same, but quoted */
761 m4_push_args (context
, obs
, argv
, false, *text
== '@');
767 if (m4_get_posixly_correct_opt (context
)
768 || !VALUE_ARG_SIGNATURE (value
))
770 obstack_1grow (obs
, *dollar
);
779 len1
< len
&& m4_has_syntax (M4SYNTAX
, *endp
,
787 key
= xstrndup (text
, len1
);
791 struct m4_symbol_arg
**arg
792 = (struct m4_symbol_arg
**)
793 m4_hash_lookup (VALUE_ARG_SIGNATURE (value
), key
);
797 i
= SYMBOL_ARG_INDEX (*arg
);
799 m4_shipout_string (context
, obs
, M4ARG (i
), M4ARGLEN (i
),
805 m4_error (context
, 0, 0, argv
->info
,
806 _("unterminated parameter reference: %s"), key
);
816 dollar
= locate_dollar (context
, text
, len
);
818 obstack_grow (obs
, text
, len
);
823 /* The next portion of this file contains the functions for macro
824 tracing output. All tracing output for a macro call is collected
825 on an obstack TRACE, and printed whenever the line is complete.
826 This prevents tracing output from interfering with other debug
827 messages generated by the various builtins. */
829 /* Format the standard header attached to all tracing output lines,
830 using the context in INFO as appropriate. Return the offset into
831 the trace obstack where this particular trace begins. */
833 trace_header (m4
*context
, const m4_call_info
*info
)
835 m4_obstack
*trace
= &context
->trace_messages
;
836 unsigned int result
= obstack_object_size (trace
);
837 obstack_grow (trace
, "m4trace:", 8);
838 if (info
->debug_level
& M4_DEBUG_TRACE_FILE
)
839 obstack_printf (trace
, "%s:", info
->file
);
840 if (info
->debug_level
& M4_DEBUG_TRACE_LINE
)
841 obstack_printf (trace
, "%d:", info
->line
);
842 obstack_printf (trace
, " -%zu- ", context
->expansion_level
);
843 if (info
->debug_level
& M4_DEBUG_TRACE_CALLID
)
844 obstack_printf (trace
, "id %zu: ", info
->call_id
);
848 /* Print current tracing line starting at offset START, as returned
849 from an earlier trace_header(), then clear the obstack. */
851 trace_flush (m4
*context
, unsigned int start
)
854 size_t len
= obstack_object_size (&context
->trace_messages
);
855 FILE *file
= m4_get_debug_file (context
);
859 /* TODO - quote nonprintable characters if debug is tty? */
860 str
= (char *) obstack_base (&context
->trace_messages
);
861 fwrite (&str
[start
], 1, len
- start
, file
);
864 obstack_blank (&context
->trace_messages
, start
- len
);
867 /* Do pre-argument-collection tracing for the macro described in INFO.
868 Should be called prior to m4_macro_call(). */
870 m4_trace_prepare (m4
*context
, const m4_call_info
*info
,
871 m4_symbol_value
*value
)
873 const m4_string_pair
*quotes
= NULL
;
874 size_t arg_length
= m4_get_max_debug_arg_length_opt (context
);
875 bool module
= (info
->debug_level
& M4_DEBUG_TRACE_MODULE
) != 0;
877 if (info
->debug_level
& M4_DEBUG_TRACE_QUOTE
)
878 quotes
= m4_get_syntax_quotes (M4SYNTAX
);
879 if (info
->trace
&& (info
->debug_level
& M4_DEBUG_TRACE_CALL
))
881 unsigned int start
= trace_header (context
, info
);
882 obstack_grow (&context
->trace_messages
, info
->name
, info
->name_len
);
883 obstack_grow (&context
->trace_messages
, " ... = ", 7);
884 m4__symbol_value_print (context
, value
, &context
->trace_messages
, quotes
,
885 false, NULL
, &arg_length
, module
);
886 trace_flush (context
, start
);
890 /* Format the parts of a trace line that are known via ARGV before the
891 macro is actually expanded. Used from m4_macro_call(). Return the
892 start of the current trace, in case other traces are printed before
893 this trace completes trace_post. */
895 trace_pre (m4
*context
, m4_macro_args
*argv
)
897 int trace_level
= argv
->info
->debug_level
;
898 unsigned int start
= trace_header (context
, argv
->info
);
899 m4_obstack
*trace
= &context
->trace_messages
;
901 assert (argv
->info
->trace
);
902 obstack_grow (trace
, argv
->info
->name
, argv
->info
->name_len
);
904 if (1 < m4_arg_argc (argv
) && (trace_level
& M4_DEBUG_TRACE_ARGS
))
906 const m4_string_pair
*quotes
= NULL
;
907 size_t arg_length
= m4_get_max_debug_arg_length_opt (context
);
908 bool module
= (trace_level
& M4_DEBUG_TRACE_MODULE
) != 0;
910 if (trace_level
& M4_DEBUG_TRACE_QUOTE
)
911 quotes
= m4_get_syntax_quotes (M4SYNTAX
);
912 obstack_1grow (trace
, '(');
913 m4__arg_print (context
, trace
, argv
, 1, quotes
, false, NULL
, ", ",
914 &arg_length
, true, module
);
915 obstack_1grow (trace
, ')');
920 /* If requested by the trace state in INFO, format the final part of a
921 trace line. Then print all collected information from START,
922 returned from a prior trace_pre(). Used from m4_macro_call (). */
924 trace_post (m4
*context
, unsigned int start
, const m4_call_info
*info
)
926 assert (info
->trace
);
927 if (info
->debug_level
& M4_DEBUG_TRACE_EXPANSION
)
929 obstack_grow (&context
->trace_messages
, " -> ", 4);
930 m4_input_print (context
, &context
->trace_messages
, info
->debug_level
);
932 trace_flush (context
, start
);
936 /* Accessors into m4_macro_args. */
938 /* Adjust the refcount of argument stack LEVEL. If INCREASE, then
939 increase the count, otherwise decrease the count and clear the
940 entire stack if the new count is zero. Return the new
943 m4__adjust_refcount (m4
*context
, size_t level
, bool increase
)
945 m4__macro_arg_stacks
*stack
= &context
->arg_stacks
[level
];
946 assert (level
< context
->stacks_count
&& stack
->args
947 && (increase
|| stack
->refcount
));
950 else if (--stack
->refcount
== 0)
952 obstack_free (stack
->args
, stack
->args_base
);
953 obstack_free (stack
->argv
, stack
->argv_base
);
954 if ((debug_macro_level
& PRINT_ARGCOUNT_CHANGES
) && 1 < stack
->argcount
)
955 xfprintf (stderr
, "m4debug: -%zu- freeing %zu args, level=%zu\n",
956 macro_call_id
, stack
->argcount
, level
);
959 if (debug_macro_level
960 & (increase
? PRINT_REFCOUNT_INCREASE
: PRINT_REFCOUNT_DECREASE
))
961 xfprintf (stderr
, "m4debug: level %zu refcount=%zu\n", level
,
963 return stack
->refcount
;
966 /* Given ARGV, adjust the refcount of every reference it contains in
967 the direction decided by INCREASE. Return true if increasing
968 references to ARGV implies the first use of ARGV. */
970 m4__arg_adjust_refcount (m4
*context
, m4_macro_args
*argv
, bool increase
)
973 m4__symbol_chain
*chain
;
974 bool result
= !argv
->inuse
;
977 for (i
= 0; i
< argv
->arraylen
; i
++)
978 if (argv
->array
[i
]->type
== M4_SYMBOL_COMP
)
980 chain
= argv
->array
[i
]->u
.u_c
.chain
;
986 if (chain
->u
.u_s
.level
< SIZE_MAX
)
987 m4__adjust_refcount (context
, chain
->u
.u_s
.level
,
993 assert (chain
->u
.u_a
.argv
->inuse
);
994 m4__arg_adjust_refcount (context
, chain
->u
.u_a
.argv
,
998 assert (!"m4__arg_adjust_refcount");
1001 chain
= chain
->next
;
1004 m4__adjust_refcount (context
, argv
->level
, increase
);
1008 /* Mark ARGV as being in use, along with any $@ references that it
1011 arg_mark (m4_macro_args
*argv
)
1014 m4__symbol_chain
*chain
;
1021 for (i
= 0; i
< argv
->arraylen
; i
++)
1022 if (argv
->array
[i
]->type
== M4_SYMBOL_COMP
1023 && argv
->array
[i
]->u
.u_c
.wrapper
)
1025 chain
= argv
->array
[i
]->u
.u_c
.chain
;
1026 assert (!chain
->next
&& chain
->type
== M4__CHAIN_ARGV
);
1027 if (!chain
->u
.u_a
.argv
->inuse
)
1028 arg_mark (chain
->u
.u_a
.argv
);
1033 /* Populate the newly-allocated VALUE as a wrapper around ARGV,
1034 starting with argument ARG. Allocate any data on OBS, owned by a
1035 given expansion LEVEL. FLATTEN determines whether to allow
1036 builtins, and QUOTES determines whether all arguments are quoted.
1037 Return TOKEN when successful, NULL when wrapping ARGV is trivially
1039 static m4_symbol_value
*
1040 make_argv_ref (m4
*context
, m4_symbol_value
*value
, m4_obstack
*obs
,
1041 size_t level
, m4_macro_args
*argv
, size_t arg
, bool flatten
,
1042 const m4_string_pair
*quotes
)
1044 m4__symbol_chain
*chain
;
1046 if (argv
->argc
<= arg
)
1048 value
->type
= M4_SYMBOL_COMP
;
1049 value
->u
.u_c
.chain
= value
->u
.u_c
.end
= NULL
;
1051 /* Cater to the common idiom of $0(`$1',shift(shift($@))), by
1052 inlining the first few arguments and reusing the original $@ ref,
1053 rather than creating another layer of wrappers. */
1054 while (argv
->wrapper
)
1057 for (i
= 0; i
< argv
->arraylen
; i
++)
1059 if ((argv
->array
[i
]->type
== M4_SYMBOL_COMP
1060 && argv
->array
[i
]->u
.u_c
.wrapper
)
1061 || level
< SIZE_MAX
)
1065 m4__push_arg_quote (context
, obs
, argv
, i
+ 1, quotes
);
1066 /* TODO support M4_SYNTAX_COMMA. */
1067 obstack_1grow (obs
, ',');
1072 assert (i
< argv
->arraylen
);
1073 if (i
+ 1 == argv
->arraylen
)
1075 assert (argv
->array
[i
]->type
== M4_SYMBOL_COMP
1076 && argv
->array
[i
]->u
.u_c
.wrapper
);
1077 chain
= argv
->array
[i
]->u
.u_c
.chain
;
1078 assert (!chain
->next
&& chain
->type
== M4__CHAIN_ARGV
1079 && !chain
->u
.u_a
.skip_last
);
1080 argv
= chain
->u
.u_a
.argv
;
1081 arg
+= chain
->u
.u_a
.index
- 1;
1090 m4__make_text_link (obs
, &value
->u
.u_c
.chain
, &value
->u
.u_c
.end
);
1091 chain
= (m4__symbol_chain
*) obstack_alloc (obs
, sizeof *chain
);
1092 if (value
->u
.u_c
.end
)
1093 value
->u
.u_c
.end
->next
= chain
;
1095 value
->u
.u_c
.chain
= chain
;
1096 value
->u
.u_c
.end
= chain
;
1097 value
->u
.u_c
.wrapper
= true;
1098 value
->u
.u_c
.has_func
= argv
->has_func
;
1100 chain
->type
= M4__CHAIN_ARGV
;
1101 chain
->quote_age
= argv
->quote_age
;
1102 chain
->u
.u_a
.argv
= argv
;
1103 chain
->u
.u_a
.index
= arg
;
1104 chain
->u
.u_a
.flatten
= flatten
;
1105 chain
->u
.u_a
.has_func
= argv
->has_func
;
1106 chain
->u
.u_a
.comma
= false;
1107 chain
->u
.u_a
.skip_last
= false;
1108 chain
->u
.u_a
.quotes
= m4__quote_cache (M4SYNTAX
, obs
, chain
->quote_age
,
1113 /* Given ARGV, return the symbol value at the specified ARG, which
1114 must be non-zero. *LEVEL is set to the obstack level that contains
1115 the symbol (which is not necessarily the level of ARGV). If
1116 FLATTEN, avoid returning a builtin token. */
1117 static m4_symbol_value
*
1118 arg_symbol (m4_macro_args
*argv
, size_t arg
, size_t *level
, bool flatten
)
1121 m4_symbol_value
*value
;
1125 *level
= argv
->level
;
1126 flatten
|= argv
->flatten
;
1127 if (argv
->argc
<= arg
)
1128 return &empty_symbol
;
1131 value
= argv
->array
[arg
- 1];
1132 if (flatten
&& m4_is_symbol_value_func (value
))
1133 value
= &empty_symbol
;
1137 /* Must cycle through all array slots until we find arg, since
1138 wrappers can contain multiple arguments. */
1139 for (i
= 0; i
< argv
->arraylen
; i
++)
1141 value
= argv
->array
[i
];
1142 if (value
->type
== M4_SYMBOL_COMP
&& value
->u
.u_c
.wrapper
)
1144 m4__symbol_chain
*chain
= value
->u
.u_c
.chain
;
1145 assert (!chain
->next
&& chain
->type
== M4__CHAIN_ARGV
);
1146 if (arg
<= (chain
->u
.u_a
.argv
->argc
- chain
->u
.u_a
.index
1147 - chain
->u
.u_a
.skip_last
))
1149 value
= arg_symbol (chain
->u
.u_a
.argv
,
1150 chain
->u
.u_a
.index
- 1 + arg
, level
,
1151 flatten
|| chain
->u
.u_a
.flatten
);
1154 arg
-= (chain
->u
.u_a
.argv
->argc
- chain
->u
.u_a
.index
1155 - chain
->u
.u_a
.skip_last
);
1157 else if (--arg
== 0)
1163 /* Given ARGV, return the symbol value at the specified ARG, which
1164 must be non-zero. */
1166 m4_arg_symbol (m4_macro_args
*argv
, size_t arg
)
1168 return arg_symbol (argv
, arg
, NULL
, false);
1171 /* Given ARGV, return true if argument ARG is text. Arg 0 is always
1172 text, as are indices beyond argc. */
1174 m4_is_arg_text (m4_macro_args
*argv
, size_t arg
)
1176 m4_symbol_value
*value
;
1177 if (arg
== 0 || argv
->argc
<= arg
|| argv
->flatten
|| !argv
->has_func
)
1179 value
= m4_arg_symbol (argv
, arg
);
1180 if (m4_is_symbol_value_text (value
)
1181 || (value
->type
== M4_SYMBOL_COMP
&& !value
->u
.u_c
.has_func
))
1186 /* Given ARGV, return true if argument ARG is a single builtin
1187 function. Only non-zero indices less than argc can return
1190 m4_is_arg_func (m4_macro_args
*argv
, size_t arg
)
1192 if (arg
== 0 || argv
->argc
<= arg
|| argv
->flatten
|| !argv
->has_func
)
1194 return m4_is_symbol_value_func (m4_arg_symbol (argv
, arg
));
1197 /* Given ARGV, return true if argument ARG contains a builtin token
1198 concatenated with anything else. Only non-zero indices less than
1199 argc can return true. */
1201 m4_is_arg_composite (m4_macro_args
*argv
, size_t arg
)
1203 m4_symbol_value
*value
;
1204 if (arg
== 0 || argv
->argc
<= arg
|| argv
->flatten
|| !argv
->has_func
)
1206 value
= m4_arg_symbol (argv
, arg
);
1207 if (value
->type
== M4_SYMBOL_COMP
&& value
->u
.u_c
.has_func
)
1212 /* Given ARGV, return the text at argument ARG. Abort if the argument
1213 is not text. Arg 0 is always text, and indices beyond argc return
1214 the empty string. If FLATTEN, builtins are ignored. The result is
1215 always NUL-terminated, even if it includes embedded NUL
1218 m4_arg_text (m4
*context
, m4_macro_args
*argv
, size_t arg
, bool flatten
)
1220 m4_symbol_value
*value
;
1221 m4__symbol_chain
*chain
;
1226 assert (argv
->info
);
1227 return argv
->info
->name
;
1229 if (argv
->argc
<= arg
)
1231 value
= arg_symbol (argv
, arg
, NULL
, flatten
);
1232 if (m4_is_symbol_value_text (value
))
1233 return m4_get_symbol_value_text (value
);
1234 assert (value
->type
== M4_SYMBOL_COMP
);
1235 chain
= value
->u
.u_c
.chain
;
1236 obs
= m4_arg_scratch (context
);
1239 switch (chain
->type
)
1242 obstack_grow (obs
, chain
->u
.u_s
.str
, chain
->u
.u_s
.len
);
1244 case M4__CHAIN_FUNC
:
1247 assert (!"m4_arg_text");
1249 case M4__CHAIN_ARGV
:
1250 assert (!chain
->u
.u_a
.has_func
|| flatten
|| argv
->flatten
);
1251 m4__arg_print (context
, obs
, chain
->u
.u_a
.argv
, chain
->u
.u_a
.index
,
1252 m4__quote_cache (M4SYNTAX
, NULL
, chain
->quote_age
,
1253 chain
->u
.u_a
.quotes
),
1254 flatten
|| argv
->flatten
|| chain
->u
.u_a
.flatten
,
1255 NULL
, NULL
, NULL
, false, false);
1258 assert (!"m4_arg_text");
1261 chain
= chain
->next
;
1263 obstack_1grow (obs
, '\0');
1264 return (char *) obstack_finish (obs
);
1267 /* Given ARGV, compare text arguments INDEXA and INDEXB for equality.
1268 Both indices must be non-zero. Return true if the arguments
1269 contain the same contents; often more efficient than
1270 !strcmp (m4_arg_text (context, argv, indexa),
1271 m4_arg_text (context, argv, indexb)). */
1273 m4_arg_equal (m4
*context
, m4_macro_args
*argv
, size_t indexa
, size_t indexb
)
1275 m4_symbol_value
*sa
= m4_arg_symbol (argv
, indexa
);
1276 m4_symbol_value
*sb
= m4_arg_symbol (argv
, indexb
);
1277 m4__symbol_chain tmpa
;
1278 m4__symbol_chain tmpb
;
1279 m4__symbol_chain
*ca
= &tmpa
;
1280 m4__symbol_chain
*cb
= &tmpb
;
1281 m4__symbol_chain
*chain
;
1282 m4_obstack
*obs
= m4_arg_scratch (context
);
1285 if (sa
== &empty_symbol
|| sb
== &empty_symbol
)
1287 if (m4_is_symbol_value_text (sa
) && m4_is_symbol_value_text (sb
))
1288 return (m4_get_symbol_value_len (sa
) == m4_get_symbol_value_len (sb
)
1289 && memcmp (m4_get_symbol_value_text (sa
),
1290 m4_get_symbol_value_text (sb
),
1291 m4_get_symbol_value_len (sa
)) == 0);
1293 /* Convert both arguments to chains, if not one already. */
1296 case M4_SYMBOL_TEXT
:
1298 tmpa
.type
= M4__CHAIN_STR
;
1299 tmpa
.u
.u_s
.str
= m4_get_symbol_value_text (sa
);
1300 tmpa
.u
.u_s
.len
= m4_get_symbol_value_len (sa
);
1302 case M4_SYMBOL_FUNC
:
1304 tmpa
.type
= M4__CHAIN_FUNC
;
1305 tmpa
.u
.builtin
= sa
->u
.builtin
;
1307 case M4_SYMBOL_COMP
:
1308 ca
= sa
->u
.u_c
.chain
;
1311 assert (!"m4_arg_equal");
1316 case M4_SYMBOL_TEXT
:
1318 tmpb
.type
= M4__CHAIN_STR
;
1319 tmpb
.u
.u_s
.str
= m4_get_symbol_value_text (sb
);
1320 tmpb
.u
.u_s
.len
= m4_get_symbol_value_len (sb
);
1322 case M4_SYMBOL_FUNC
:
1324 tmpb
.type
= M4__CHAIN_FUNC
;
1325 tmpb
.u
.builtin
= sb
->u
.builtin
;
1327 case M4_SYMBOL_COMP
:
1328 cb
= sb
->u
.u_c
.chain
;
1331 assert (!"m4_arg_equal");
1335 /* Compare each link of the chain. */
1338 if (ca
->type
== M4__CHAIN_ARGV
)
1341 tmpa
.type
= M4__CHAIN_STR
;
1342 tmpa
.u
.u_s
.str
= NULL
;
1345 m4__arg_print (context
, obs
, ca
->u
.u_a
.argv
, ca
->u
.u_a
.index
,
1346 m4__quote_cache (M4SYNTAX
, NULL
, ca
->quote_age
,
1348 argv
->flatten
|| ca
->u
.u_a
.flatten
, &chain
, NULL
,
1349 NULL
, false, false);
1350 assert (obstack_object_size (obs
) == 0 && chain
!= &tmpa
);
1351 chain
->next
= ca
->next
;
1355 if (cb
->type
== M4__CHAIN_ARGV
)
1358 tmpb
.type
= M4__CHAIN_STR
;
1359 tmpb
.u
.u_s
.str
= NULL
;
1362 m4__arg_print (context
, obs
, cb
->u
.u_a
.argv
, cb
->u
.u_a
.index
,
1363 m4__quote_cache (M4SYNTAX
, NULL
, cb
->quote_age
,
1365 argv
->flatten
|| cb
->u
.u_a
.flatten
, &chain
, NULL
,
1366 NULL
, false, false);
1367 assert (obstack_object_size (obs
) == 0 && chain
!= &tmpb
);
1368 chain
->next
= cb
->next
;
1372 if (ca
->type
== M4__CHAIN_FUNC
)
1374 if (cb
->type
!= M4__CHAIN_FUNC
|| ca
->u
.builtin
!= cb
->u
.builtin
)
1380 assert (ca
->type
== M4__CHAIN_STR
&& cb
->type
== M4__CHAIN_STR
);
1381 if (ca
->u
.u_s
.len
== cb
->u
.u_s
.len
)
1383 if (memcmp (ca
->u
.u_s
.str
, cb
->u
.u_s
.str
, ca
->u
.u_s
.len
) != 0)
1388 else if (ca
->u
.u_s
.len
< cb
->u
.u_s
.len
)
1390 if (memcmp (ca
->u
.u_s
.str
, cb
->u
.u_s
.str
, ca
->u
.u_s
.len
) != 0)
1392 tmpb
.next
= cb
->next
;
1393 tmpb
.u
.u_s
.str
= cb
->u
.u_s
.str
+ ca
->u
.u_s
.len
;
1394 tmpb
.u
.u_s
.len
= cb
->u
.u_s
.len
- ca
->u
.u_s
.len
;
1400 assert (cb
->u
.u_s
.len
< ca
->u
.u_s
.len
);
1401 if (memcmp (ca
->u
.u_s
.str
, cb
->u
.u_s
.str
, cb
->u
.u_s
.len
) != 0)
1403 tmpa
.next
= ca
->next
;
1404 tmpa
.u
.u_s
.str
= ca
->u
.u_s
.str
+ cb
->u
.u_s
.len
;
1405 tmpa
.u
.u_s
.len
= ca
->u
.u_s
.len
- cb
->u
.u_s
.len
;
1411 /* If we get this far, the two arguments are equal only if both
1412 chains are exhausted. */
1413 assert (ca
!= cb
|| !ca
);
1417 /* Given ARGV, return true if argument ARG is the empty string. This
1418 gives the same result as comparing m4_arg_len against 0, but is
1421 m4_arg_empty (m4_macro_args
*argv
, size_t arg
)
1425 assert (argv
->info
);
1426 return !argv
->info
->name_len
;
1428 return m4_arg_symbol (argv
, arg
) == &empty_symbol
;
1431 /* Given ARGV, return the length of argument ARG. Abort if the
1432 argument is not text and FLATTEN is not true. Indices beyond argc
1435 m4_arg_len (m4
*context
, m4_macro_args
*argv
, size_t arg
, bool flatten
)
1437 m4_symbol_value
*value
;
1438 m4__symbol_chain
*chain
;
1443 assert (argv
->info
);
1444 return argv
->info
->name_len
;
1446 if (argv
->argc
<= arg
)
1448 value
= arg_symbol (argv
, arg
, NULL
, flatten
);
1449 if (m4_is_symbol_value_text (value
))
1450 return m4_get_symbol_value_len (value
);
1451 assert (value
->type
== M4_SYMBOL_COMP
);
1452 chain
= value
->u
.u_c
.chain
;
1458 const m4_string_pair
*quotes
;
1459 switch (chain
->type
)
1462 len
+= chain
->u
.u_s
.len
;
1464 case M4__CHAIN_FUNC
:
1467 case M4__CHAIN_ARGV
:
1468 i
= chain
->u
.u_a
.index
;
1469 limit
= chain
->u
.u_a
.argv
->argc
- i
- chain
->u
.u_a
.skip_last
;
1470 quotes
= m4__quote_cache (M4SYNTAX
, NULL
, chain
->quote_age
,
1471 chain
->u
.u_a
.quotes
);
1474 len
+= (quotes
->len1
+ quotes
->len2
) * limit
;
1477 len
+= m4_arg_len (context
, chain
->u
.u_a
.argv
, i
++,
1478 flatten
|| chain
->u
.u_a
.flatten
);
1481 assert (!"m4_arg_len");
1484 chain
= chain
->next
;
1486 assert (len
|| flatten
);
1490 /* Given ARGV, return the builtin function referenced by argument ARG.
1491 Abort if it is not a single builtin. */
1493 m4_arg_func (m4_macro_args
*argv
, size_t arg
)
1495 return m4_get_symbol_value_func (m4_arg_symbol (argv
, arg
));
1498 /* Dump a representation of ARGV to the obstack OBS, starting with
1499 argument ARG. If QUOTES is non-NULL, each argument is displayed
1500 with those quotes. If FLATTEN, builtins are converted to empty
1501 quotes; if CHAINP, *CHAINP is updated with macro tokens; otherwise,
1502 builtins are represented by their name. Separate arguments with
1503 SEP, which defaults to a comma. If MAX_LEN is non-NULL, truncate
1504 the output after *MAX_LEN bytes are output and return true;
1505 otherwise, return false, and reduce *MAX_LEN by the number of bytes
1506 output. If QUOTE_EACH, the truncation length is reset for each
1507 argument, quotes do not count against length, and all arguments are
1508 printed; otherwise, quotes count against the length and trailing
1509 arguments may be discarded. If MODULE, print any details about
1510 originating modules; modules do not count against truncation
1511 length. MAX_LEN and CHAINP may not both be specified. */
1513 m4__arg_print (m4
*context
, m4_obstack
*obs
, m4_macro_args
*argv
, size_t arg
,
1514 const m4_string_pair
*quotes
, bool flatten
,
1515 m4__symbol_chain
**chainp
, const char *sep
, size_t *max_len
,
1516 bool quote_each
, bool module
)
1518 size_t len
= max_len
? *max_len
: SIZE_MAX
;
1520 bool use_sep
= false;
1522 size_t *plen
= quote_each
? NULL
: &len
;
1524 flatten
|= argv
->flatten
;
1526 assert (!max_len
&& *chainp
);
1529 sep_len
= strlen (sep
);
1530 for (i
= arg
; i
< argv
->argc
; i
++)
1532 if (quote_each
&& max_len
)
1534 if (use_sep
&& m4_shipout_string_trunc (obs
, sep
, sep_len
, NULL
, plen
))
1537 if (quotes
&& !quote_each
1538 && m4_shipout_string_trunc (obs
, quotes
->str1
, quotes
->len1
, NULL
,
1541 if (m4__symbol_value_print (context
, arg_symbol (argv
, i
, NULL
, flatten
),
1542 obs
, quote_each
? quotes
: NULL
, flatten
,
1543 chainp
, &len
, module
))
1545 if (quotes
&& !quote_each
1546 && m4_shipout_string_trunc (obs
, quotes
->str2
, quotes
->len2
, NULL
,
1553 m4__make_text_link (obs
, NULL
, chainp
);
1557 /* Create a new argument object using the same obstack as ARGV; thus,
1558 the new object will automatically be freed when the original is
1559 freed. Explicitly set the macro name (argv[0]) from ARGV0 with
1560 length ARGV0_LEN, and discard argv[1] of the wrapped ARGV. If
1561 FLATTEN, any builtins in ARGV are flattened to an empty string when
1562 referenced through the new object. If TRACE, then trace the macro
1563 regardless of global trace state. */
1565 m4_make_argv_ref (m4
*context
, m4_macro_args
*argv
, const char *argv0
,
1566 size_t argv0_len
, bool flatten
, bool trace
)
1568 m4_macro_args
*new_argv
;
1569 m4_symbol_value
*value
;
1570 m4_symbol_value
*new_value
;
1571 m4_obstack
*obs
= m4_arg_scratch (context
);
1574 info
= (m4_call_info
*) obstack_copy (obs
, argv
->info
, sizeof *info
);
1575 new_value
= (m4_symbol_value
*) obstack_alloc (obs
, sizeof *value
);
1576 value
= make_argv_ref (context
, new_value
, obs
, context
->expansion_level
- 1,
1577 argv
, 2, flatten
, NULL
);
1580 obstack_free (obs
, new_value
);
1581 new_argv
= (m4_macro_args
*) obstack_alloc (obs
, offsetof (m4_macro_args
,
1583 new_argv
->arraylen
= 0;
1584 new_argv
->wrapper
= false;
1585 new_argv
->has_ref
= false;
1586 new_argv
->flatten
= false;
1587 new_argv
->has_func
= false;
1591 new_argv
= (m4_macro_args
*) obstack_alloc (obs
, (offsetof (m4_macro_args
,
1594 new_argv
->arraylen
= 1;
1595 new_argv
->array
[0] = value
;
1596 new_argv
->wrapper
= true;
1597 new_argv
->has_ref
= argv
->has_ref
;
1598 new_argv
->flatten
= flatten
;
1599 new_argv
->has_func
= argv
->has_func
;
1601 new_argv
->argc
= argv
->argc
- 1;
1602 new_argv
->inuse
= false;
1603 new_argv
->quote_age
= argv
->quote_age
;
1604 new_argv
->info
= info
;
1605 info
->trace
= (argv
->info
->debug_level
& M4_DEBUG_TRACE_ALL
) || trace
;
1607 info
->name_len
= argv0_len
;
1608 new_argv
->level
= argv
->level
;
1612 /* Push argument ARG from ARGV, which must be a text token, onto the
1613 expansion stack OBS for rescanning. */
1615 m4_push_arg (m4
*context
, m4_obstack
*obs
, m4_macro_args
*argv
, size_t arg
)
1617 m4_symbol_value value
;
1621 assert (argv
->info
);
1622 m4_set_symbol_value_text (&value
, argv
->info
->name
, argv
->info
->name_len
,
1624 if (m4__push_symbol (context
, &value
, context
->expansion_level
- 1,
1629 m4__push_arg_quote (context
, obs
, argv
, arg
, NULL
);
1632 /* Push argument ARG from ARGV onto the expansion stack OBS for
1633 rescanning. ARG must be non-zero. QUOTES determines any quote
1634 delimiters that were in effect when the reference was created. */
1636 m4__push_arg_quote (m4
*context
, m4_obstack
*obs
, m4_macro_args
*argv
,
1637 size_t arg
, const m4_string_pair
*quotes
)
1640 m4_symbol_value
*value
= arg_symbol (argv
, arg
, &level
, false);
1643 obstack_grow (obs
, quotes
->str1
, quotes
->len1
);
1644 if (value
!= &empty_symbol
1645 && m4__push_symbol (context
, value
, level
, argv
->inuse
))
1648 obstack_grow (obs
, quotes
->str2
, quotes
->len2
);
1651 /* Push series of comma-separated arguments from ARGV onto the
1652 expansion stack OBS for rescanning. If SKIP, then don't push the
1653 first argument. If QUOTE, also push quoting around each arg. */
1655 m4_push_args (m4
*context
, m4_obstack
*obs
, m4_macro_args
*argv
, bool skip
,
1658 m4_symbol_value tmp
;
1659 m4_symbol_value
*value
;
1660 size_t i
= skip
? 2 : 1;
1661 const m4_string_pair
*quotes
= m4_get_syntax_quotes (M4SYNTAX
);
1663 if (argv
->argc
<= i
)
1666 if (argv
->argc
== i
+ 1)
1668 m4__push_arg_quote (context
, obs
, argv
, i
, quote
? quotes
: NULL
);
1672 value
= make_argv_ref (context
, &tmp
, obs
, -1, argv
, i
, argv
->flatten
,
1673 quote
? quotes
: NULL
);
1674 assert (value
== &tmp
);
1675 if (m4__push_symbol (context
, value
, -1, argv
->inuse
))
1679 /* Push arguments from ARGV onto the wrap stack for later rescanning.
1680 If GNU extensions are disabled, only the first argument is pushed;
1681 otherwise, all arguments are pushed and separated with a space. */
1683 m4_wrap_args (m4
*context
, m4_macro_args
*argv
)
1687 m4_symbol_value
*value
;
1688 m4__symbol_chain
*chain
;
1689 m4__symbol_chain
**end
;
1690 size_t limit
= m4_get_posixly_correct_opt (context
) ? 2 : argv
->argc
;
1692 if (limit
== 2 && m4_arg_empty (argv
, 1))
1695 obs
= m4__push_wrapup_init (context
, argv
->info
, &end
);
1696 for (i
= 1; i
< limit
; i
++)
1699 obstack_1grow (obs
, ' ');
1700 value
= m4_arg_symbol (argv
, i
);
1701 switch (value
->type
)
1703 case M4_SYMBOL_TEXT
:
1704 obstack_grow (obs
, m4_get_symbol_value_text (value
),
1705 m4_get_symbol_value_len (value
));
1707 case M4_SYMBOL_FUNC
:
1708 m4__append_builtin (obs
, value
->u
.builtin
, NULL
, end
);
1710 case M4_SYMBOL_COMP
:
1711 chain
= value
->u
.u_c
.chain
;
1714 switch (chain
->type
)
1717 obstack_grow (obs
, chain
->u
.u_s
.str
, chain
->u
.u_s
.len
);
1719 case M4__CHAIN_FUNC
:
1720 m4__append_builtin (obs
, chain
->u
.builtin
, NULL
, end
);
1722 case M4__CHAIN_ARGV
:
1723 m4__arg_print (context
, obs
, chain
->u
.u_a
.argv
,
1725 m4__quote_cache (M4SYNTAX
, NULL
,
1727 chain
->u
.u_a
.quotes
),
1728 chain
->u
.u_a
.flatten
, end
, NULL
, NULL
, false,
1732 assert (!"m4_wrap_args");
1735 chain
= chain
->next
;
1739 assert (!"m4_wrap_args");
1743 m4__push_wrapup_finish ();
1747 /* Define these last, so that earlier uses can benefit from the macros
1750 /* Given ARGV, return one greater than the number of arguments it
1754 m4_arg_argc (m4_macro_args
*argv
)
1759 /* Given ARGV, return the call context in effect when argument
1760 collection began. Only safe to call while the macro is being
1763 const m4_call_info
*
1764 m4_arg_info (m4_macro_args
*argv
)
1766 assert (argv
->info
);
1770 /* Return an obstack useful for scratch calculations, and which will
1771 not interfere with macro expansion. The obstack will be reset when
1772 expand_macro completes. */
1773 #undef m4_arg_scratch
1775 m4_arg_scratch (m4
*context
)
1777 m4__macro_arg_stacks
*stack
1778 = &context
->arg_stacks
[context
->expansion_level
- 1];
1779 assert (obstack_object_size (stack
->args
) == 0);