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 /* This function handles all expansion of user defined and predefined
692 macros. It is called with an obstack OBS, where the macros expansion
693 will be placed, as an unfinished object. SYMBOL points to the macro
694 definition, giving the expansion text. ARGC and ARGV are the arguments,
697 process_macro (m4
*context
, m4_symbol_value
*value
, m4_obstack
*obs
,
698 int argc
, m4_macro_args
*argv
)
700 const char *text
= m4_get_symbol_value_text (value
);
701 size_t len
= m4_get_symbol_value_len (value
);
702 const char *end
= text
+ len
;
707 if (m4_is_syntax_single_dollar (M4SYNTAX
))
708 dollar
= (char *) memchr (text
, M4SYNTAX
->dollar
, len
);
712 while (dollar
!= end
)
714 if (m4_has_syntax (M4SYNTAX
, *dollar
, M4_SYNTAX_DOLLAR
))
723 obstack_grow (obs
, text
, len
);
726 obstack_grow (obs
, text
, dollar
- text
);
727 len
-= dollar
- text
;
731 obstack_1grow (obs
, *dollar
);
737 case '0': case '1': case '2': case '3': case '4':
738 case '5': case '6': case '7': case '8': case '9':
739 /* FIXME - multidigit arguments should convert over to ${10}
740 syntax instead of $10; see
741 http://lists.gnu.org/archive/html/m4-discuss/2006-08/msg00028.html
742 for more discussion. */
743 if (m4_get_posixly_correct_opt (context
) || !isdigit(text
[1]))
751 i
= (int) strtol (text
, &endp
, 10);
756 m4_push_arg (context
, obs
, argv
, i
);
759 case '#': /* number of arguments */
760 m4_shipout_int (obs
, argc
- 1);
765 case '*': /* all arguments */
766 case '@': /* ... same, but quoted */
767 m4_push_args (context
, obs
, argv
, false, *text
== '@');
773 if (m4_get_posixly_correct_opt (context
)
774 || !VALUE_ARG_SIGNATURE (value
))
776 obstack_1grow (obs
, *dollar
);
785 len1
< len
&& m4_has_syntax (M4SYNTAX
, *endp
,
793 key
= xstrndup (text
, len1
);
797 struct m4_symbol_arg
**arg
798 = (struct m4_symbol_arg
**)
799 m4_hash_lookup (VALUE_ARG_SIGNATURE (value
), key
);
803 i
= SYMBOL_ARG_INDEX (*arg
);
805 m4_shipout_string (context
, obs
, M4ARG (i
), M4ARGLEN (i
),
811 m4_error (context
, 0, 0, argv
->info
,
812 _("unterminated parameter reference: %s"), key
);
827 /* The next portion of this file contains the functions for macro
828 tracing output. All tracing output for a macro call is collected
829 on an obstack TRACE, and printed whenever the line is complete.
830 This prevents tracing output from interfering with other debug
831 messages generated by the various builtins. */
833 /* Format the standard header attached to all tracing output lines,
834 using the context in INFO as appropriate. Return the offset into
835 the trace obstack where this particular trace begins. */
837 trace_header (m4
*context
, const m4_call_info
*info
)
839 m4_obstack
*trace
= &context
->trace_messages
;
840 unsigned int result
= obstack_object_size (trace
);
841 obstack_grow (trace
, "m4trace:", 8);
842 if (info
->debug_level
& M4_DEBUG_TRACE_FILE
)
843 obstack_printf (trace
, "%s:", info
->file
);
844 if (info
->debug_level
& M4_DEBUG_TRACE_LINE
)
845 obstack_printf (trace
, "%d:", info
->line
);
846 obstack_printf (trace
, " -%zu- ", context
->expansion_level
);
847 if (info
->debug_level
& M4_DEBUG_TRACE_CALLID
)
848 obstack_printf (trace
, "id %zu: ", info
->call_id
);
852 /* Print current tracing line starting at offset START, as returned
853 from an earlier trace_header(), then clear the obstack. */
855 trace_flush (m4
*context
, unsigned int start
)
858 size_t len
= obstack_object_size (&context
->trace_messages
);
859 FILE *file
= m4_get_debug_file (context
);
863 /* TODO - quote nonprintable characters if debug is tty? */
864 str
= (char *) obstack_base (&context
->trace_messages
);
865 fwrite (&str
[start
], 1, len
- start
, file
);
868 obstack_blank (&context
->trace_messages
, start
- len
);
871 /* Do pre-argument-collection tracing for the macro described in INFO.
872 Should be called prior to m4_macro_call(). */
874 m4_trace_prepare (m4
*context
, const m4_call_info
*info
,
875 m4_symbol_value
*value
)
877 const m4_string_pair
*quotes
= NULL
;
878 size_t arg_length
= m4_get_max_debug_arg_length_opt (context
);
879 bool module
= (info
->debug_level
& M4_DEBUG_TRACE_MODULE
) != 0;
881 if (info
->debug_level
& M4_DEBUG_TRACE_QUOTE
)
882 quotes
= m4_get_syntax_quotes (M4SYNTAX
);
883 if (info
->trace
&& (info
->debug_level
& M4_DEBUG_TRACE_CALL
))
885 unsigned int start
= trace_header (context
, info
);
886 obstack_grow (&context
->trace_messages
, info
->name
, info
->name_len
);
887 obstack_grow (&context
->trace_messages
, " ... = ", 7);
888 m4__symbol_value_print (context
, value
, &context
->trace_messages
, quotes
,
889 false, NULL
, &arg_length
, module
);
890 trace_flush (context
, start
);
894 /* Format the parts of a trace line that are known via ARGV before the
895 macro is actually expanded. Used from m4_macro_call(). Return the
896 start of the current trace, in case other traces are printed before
897 this trace completes trace_post. */
899 trace_pre (m4
*context
, m4_macro_args
*argv
)
901 int trace_level
= argv
->info
->debug_level
;
902 unsigned int start
= trace_header (context
, argv
->info
);
903 m4_obstack
*trace
= &context
->trace_messages
;
905 assert (argv
->info
->trace
);
906 obstack_grow (trace
, argv
->info
->name
, argv
->info
->name_len
);
908 if (1 < m4_arg_argc (argv
) && (trace_level
& M4_DEBUG_TRACE_ARGS
))
910 const m4_string_pair
*quotes
= NULL
;
911 size_t arg_length
= m4_get_max_debug_arg_length_opt (context
);
912 bool module
= (trace_level
& M4_DEBUG_TRACE_MODULE
) != 0;
914 if (trace_level
& M4_DEBUG_TRACE_QUOTE
)
915 quotes
= m4_get_syntax_quotes (M4SYNTAX
);
916 obstack_1grow (trace
, '(');
917 m4__arg_print (context
, trace
, argv
, 1, quotes
, false, NULL
, ", ",
918 &arg_length
, true, module
);
919 obstack_1grow (trace
, ')');
924 /* If requested by the trace state in INFO, format the final part of a
925 trace line. Then print all collected information from START,
926 returned from a prior trace_pre(). Used from m4_macro_call (). */
928 trace_post (m4
*context
, unsigned int start
, const m4_call_info
*info
)
930 assert (info
->trace
);
931 if (info
->debug_level
& M4_DEBUG_TRACE_EXPANSION
)
933 obstack_grow (&context
->trace_messages
, " -> ", 4);
934 m4_input_print (context
, &context
->trace_messages
, info
->debug_level
);
936 trace_flush (context
, start
);
940 /* Accessors into m4_macro_args. */
942 /* Adjust the refcount of argument stack LEVEL. If INCREASE, then
943 increase the count, otherwise decrease the count and clear the
944 entire stack if the new count is zero. Return the new
947 m4__adjust_refcount (m4
*context
, size_t level
, bool increase
)
949 m4__macro_arg_stacks
*stack
= &context
->arg_stacks
[level
];
950 assert (level
< context
->stacks_count
&& stack
->args
951 && (increase
|| stack
->refcount
));
954 else if (--stack
->refcount
== 0)
956 obstack_free (stack
->args
, stack
->args_base
);
957 obstack_free (stack
->argv
, stack
->argv_base
);
958 if ((debug_macro_level
& PRINT_ARGCOUNT_CHANGES
) && 1 < stack
->argcount
)
959 xfprintf (stderr
, "m4debug: -%zu- freeing %zu args, level=%zu\n",
960 macro_call_id
, stack
->argcount
, level
);
963 if (debug_macro_level
964 & (increase
? PRINT_REFCOUNT_INCREASE
: PRINT_REFCOUNT_DECREASE
))
965 xfprintf (stderr
, "m4debug: level %zu refcount=%zu\n", level
,
967 return stack
->refcount
;
970 /* Given ARGV, adjust the refcount of every reference it contains in
971 the direction decided by INCREASE. Return true if increasing
972 references to ARGV implies the first use of ARGV. */
974 m4__arg_adjust_refcount (m4
*context
, m4_macro_args
*argv
, bool increase
)
977 m4__symbol_chain
*chain
;
978 bool result
= !argv
->inuse
;
981 for (i
= 0; i
< argv
->arraylen
; i
++)
982 if (argv
->array
[i
]->type
== M4_SYMBOL_COMP
)
984 chain
= argv
->array
[i
]->u
.u_c
.chain
;
990 if (chain
->u
.u_s
.level
< SIZE_MAX
)
991 m4__adjust_refcount (context
, chain
->u
.u_s
.level
,
997 assert (chain
->u
.u_a
.argv
->inuse
);
998 m4__arg_adjust_refcount (context
, chain
->u
.u_a
.argv
,
1002 assert (!"m4__arg_adjust_refcount");
1005 chain
= chain
->next
;
1008 m4__adjust_refcount (context
, argv
->level
, increase
);
1012 /* Mark ARGV as being in use, along with any $@ references that it
1015 arg_mark (m4_macro_args
*argv
)
1018 m4__symbol_chain
*chain
;
1025 for (i
= 0; i
< argv
->arraylen
; i
++)
1026 if (argv
->array
[i
]->type
== M4_SYMBOL_COMP
1027 && argv
->array
[i
]->u
.u_c
.wrapper
)
1029 chain
= argv
->array
[i
]->u
.u_c
.chain
;
1030 assert (!chain
->next
&& chain
->type
== M4__CHAIN_ARGV
);
1031 if (!chain
->u
.u_a
.argv
->inuse
)
1032 arg_mark (chain
->u
.u_a
.argv
);
1037 /* Populate the newly-allocated VALUE as a wrapper around ARGV,
1038 starting with argument ARG. Allocate any data on OBS, owned by a
1039 given expansion LEVEL. FLATTEN determines whether to allow
1040 builtins, and QUOTES determines whether all arguments are quoted.
1041 Return TOKEN when successful, NULL when wrapping ARGV is trivially
1043 static m4_symbol_value
*
1044 make_argv_ref (m4
*context
, m4_symbol_value
*value
, m4_obstack
*obs
,
1045 size_t level
, m4_macro_args
*argv
, size_t arg
, bool flatten
,
1046 const m4_string_pair
*quotes
)
1048 m4__symbol_chain
*chain
;
1050 if (argv
->argc
<= arg
)
1052 value
->type
= M4_SYMBOL_COMP
;
1053 value
->u
.u_c
.chain
= value
->u
.u_c
.end
= NULL
;
1055 /* Cater to the common idiom of $0(`$1',shift(shift($@))), by
1056 inlining the first few arguments and reusing the original $@ ref,
1057 rather than creating another layer of wrappers. */
1058 while (argv
->wrapper
)
1061 for (i
= 0; i
< argv
->arraylen
; i
++)
1063 if ((argv
->array
[i
]->type
== M4_SYMBOL_COMP
1064 && argv
->array
[i
]->u
.u_c
.wrapper
)
1065 || level
< SIZE_MAX
)
1069 m4__push_arg_quote (context
, obs
, argv
, i
+ 1, quotes
);
1070 /* TODO support M4_SYNTAX_COMMA. */
1071 obstack_1grow (obs
, ',');
1076 assert (i
< argv
->arraylen
);
1077 if (i
+ 1 == argv
->arraylen
)
1079 assert (argv
->array
[i
]->type
== M4_SYMBOL_COMP
1080 && argv
->array
[i
]->u
.u_c
.wrapper
);
1081 chain
= argv
->array
[i
]->u
.u_c
.chain
;
1082 assert (!chain
->next
&& chain
->type
== M4__CHAIN_ARGV
1083 && !chain
->u
.u_a
.skip_last
);
1084 argv
= chain
->u
.u_a
.argv
;
1085 arg
+= chain
->u
.u_a
.index
- 1;
1094 m4__make_text_link (obs
, &value
->u
.u_c
.chain
, &value
->u
.u_c
.end
);
1095 chain
= (m4__symbol_chain
*) obstack_alloc (obs
, sizeof *chain
);
1096 if (value
->u
.u_c
.end
)
1097 value
->u
.u_c
.end
->next
= chain
;
1099 value
->u
.u_c
.chain
= chain
;
1100 value
->u
.u_c
.end
= chain
;
1101 value
->u
.u_c
.wrapper
= true;
1102 value
->u
.u_c
.has_func
= argv
->has_func
;
1104 chain
->type
= M4__CHAIN_ARGV
;
1105 chain
->quote_age
= argv
->quote_age
;
1106 chain
->u
.u_a
.argv
= argv
;
1107 chain
->u
.u_a
.index
= arg
;
1108 chain
->u
.u_a
.flatten
= flatten
;
1109 chain
->u
.u_a
.has_func
= argv
->has_func
;
1110 chain
->u
.u_a
.comma
= false;
1111 chain
->u
.u_a
.skip_last
= false;
1112 chain
->u
.u_a
.quotes
= m4__quote_cache (M4SYNTAX
, obs
, chain
->quote_age
,
1117 /* Given ARGV, return the symbol value at the specified ARG, which
1118 must be non-zero. *LEVEL is set to the obstack level that contains
1119 the symbol (which is not necessarily the level of ARGV). If
1120 FLATTEN, avoid returning a builtin token. */
1121 static m4_symbol_value
*
1122 arg_symbol (m4_macro_args
*argv
, size_t arg
, size_t *level
, bool flatten
)
1125 m4_symbol_value
*value
;
1129 *level
= argv
->level
;
1130 flatten
|= argv
->flatten
;
1131 if (argv
->argc
<= arg
)
1132 return &empty_symbol
;
1135 value
= argv
->array
[arg
- 1];
1136 if (flatten
&& m4_is_symbol_value_func (value
))
1137 value
= &empty_symbol
;
1141 /* Must cycle through all array slots until we find arg, since
1142 wrappers can contain multiple arguments. */
1143 for (i
= 0; i
< argv
->arraylen
; i
++)
1145 value
= argv
->array
[i
];
1146 if (value
->type
== M4_SYMBOL_COMP
&& value
->u
.u_c
.wrapper
)
1148 m4__symbol_chain
*chain
= value
->u
.u_c
.chain
;
1149 assert (!chain
->next
&& chain
->type
== M4__CHAIN_ARGV
);
1150 if (arg
<= (chain
->u
.u_a
.argv
->argc
- chain
->u
.u_a
.index
1151 - chain
->u
.u_a
.skip_last
))
1153 value
= arg_symbol (chain
->u
.u_a
.argv
,
1154 chain
->u
.u_a
.index
- 1 + arg
, level
,
1155 flatten
|| chain
->u
.u_a
.flatten
);
1158 arg
-= (chain
->u
.u_a
.argv
->argc
- chain
->u
.u_a
.index
1159 - chain
->u
.u_a
.skip_last
);
1161 else if (--arg
== 0)
1167 /* Given ARGV, return the symbol value at the specified ARG, which
1168 must be non-zero. */
1170 m4_arg_symbol (m4_macro_args
*argv
, size_t arg
)
1172 return arg_symbol (argv
, arg
, NULL
, false);
1175 /* Given ARGV, return true if argument ARG is text. Arg 0 is always
1176 text, as are indices beyond argc. */
1178 m4_is_arg_text (m4_macro_args
*argv
, size_t arg
)
1180 m4_symbol_value
*value
;
1181 if (arg
== 0 || argv
->argc
<= arg
|| argv
->flatten
|| !argv
->has_func
)
1183 value
= m4_arg_symbol (argv
, arg
);
1184 if (m4_is_symbol_value_text (value
)
1185 || (value
->type
== M4_SYMBOL_COMP
&& !value
->u
.u_c
.has_func
))
1190 /* Given ARGV, return true if argument ARG is a single builtin
1191 function. Only non-zero indices less than argc can return
1194 m4_is_arg_func (m4_macro_args
*argv
, size_t arg
)
1196 if (arg
== 0 || argv
->argc
<= arg
|| argv
->flatten
|| !argv
->has_func
)
1198 return m4_is_symbol_value_func (m4_arg_symbol (argv
, arg
));
1201 /* Given ARGV, return true if argument ARG contains a builtin token
1202 concatenated with anything else. Only non-zero indices less than
1203 argc can return true. */
1205 m4_is_arg_composite (m4_macro_args
*argv
, size_t arg
)
1207 m4_symbol_value
*value
;
1208 if (arg
== 0 || argv
->argc
<= arg
|| argv
->flatten
|| !argv
->has_func
)
1210 value
= m4_arg_symbol (argv
, arg
);
1211 if (value
->type
== M4_SYMBOL_COMP
&& value
->u
.u_c
.has_func
)
1216 /* Given ARGV, return the text at argument ARG. Abort if the argument
1217 is not text. Arg 0 is always text, and indices beyond argc return
1218 the empty string. If FLATTEN, builtins are ignored. The result is
1219 always NUL-terminated, even if it includes embedded NUL
1222 m4_arg_text (m4
*context
, m4_macro_args
*argv
, size_t arg
, bool flatten
)
1224 m4_symbol_value
*value
;
1225 m4__symbol_chain
*chain
;
1230 assert (argv
->info
);
1231 return argv
->info
->name
;
1233 if (argv
->argc
<= arg
)
1235 value
= arg_symbol (argv
, arg
, NULL
, flatten
);
1236 if (m4_is_symbol_value_text (value
))
1237 return m4_get_symbol_value_text (value
);
1238 assert (value
->type
== M4_SYMBOL_COMP
);
1239 chain
= value
->u
.u_c
.chain
;
1240 obs
= m4_arg_scratch (context
);
1243 switch (chain
->type
)
1246 obstack_grow (obs
, chain
->u
.u_s
.str
, chain
->u
.u_s
.len
);
1248 case M4__CHAIN_FUNC
:
1251 assert (!"m4_arg_text");
1253 case M4__CHAIN_ARGV
:
1254 assert (!chain
->u
.u_a
.has_func
|| flatten
|| argv
->flatten
);
1255 m4__arg_print (context
, obs
, chain
->u
.u_a
.argv
, chain
->u
.u_a
.index
,
1256 m4__quote_cache (M4SYNTAX
, NULL
, chain
->quote_age
,
1257 chain
->u
.u_a
.quotes
),
1258 flatten
|| argv
->flatten
|| chain
->u
.u_a
.flatten
,
1259 NULL
, NULL
, NULL
, false, false);
1262 assert (!"m4_arg_text");
1265 chain
= chain
->next
;
1267 obstack_1grow (obs
, '\0');
1268 return (char *) obstack_finish (obs
);
1271 /* Given ARGV, compare text arguments INDEXA and INDEXB for equality.
1272 Both indices must be non-zero. Return true if the arguments
1273 contain the same contents; often more efficient than
1274 !strcmp (m4_arg_text (context, argv, indexa),
1275 m4_arg_text (context, argv, indexb)). */
1277 m4_arg_equal (m4
*context
, m4_macro_args
*argv
, size_t indexa
, size_t indexb
)
1279 m4_symbol_value
*sa
= m4_arg_symbol (argv
, indexa
);
1280 m4_symbol_value
*sb
= m4_arg_symbol (argv
, indexb
);
1281 m4__symbol_chain tmpa
;
1282 m4__symbol_chain tmpb
;
1283 m4__symbol_chain
*ca
= &tmpa
;
1284 m4__symbol_chain
*cb
= &tmpb
;
1285 m4__symbol_chain
*chain
;
1286 m4_obstack
*obs
= m4_arg_scratch (context
);
1289 if (sa
== &empty_symbol
|| sb
== &empty_symbol
)
1291 if (m4_is_symbol_value_text (sa
) && m4_is_symbol_value_text (sb
))
1292 return (m4_get_symbol_value_len (sa
) == m4_get_symbol_value_len (sb
)
1293 && memcmp (m4_get_symbol_value_text (sa
),
1294 m4_get_symbol_value_text (sb
),
1295 m4_get_symbol_value_len (sa
)) == 0);
1297 /* Convert both arguments to chains, if not one already. */
1300 case M4_SYMBOL_TEXT
:
1302 tmpa
.type
= M4__CHAIN_STR
;
1303 tmpa
.u
.u_s
.str
= m4_get_symbol_value_text (sa
);
1304 tmpa
.u
.u_s
.len
= m4_get_symbol_value_len (sa
);
1306 case M4_SYMBOL_FUNC
:
1308 tmpa
.type
= M4__CHAIN_FUNC
;
1309 tmpa
.u
.builtin
= sa
->u
.builtin
;
1311 case M4_SYMBOL_COMP
:
1312 ca
= sa
->u
.u_c
.chain
;
1315 assert (!"m4_arg_equal");
1320 case M4_SYMBOL_TEXT
:
1322 tmpb
.type
= M4__CHAIN_STR
;
1323 tmpb
.u
.u_s
.str
= m4_get_symbol_value_text (sb
);
1324 tmpb
.u
.u_s
.len
= m4_get_symbol_value_len (sb
);
1326 case M4_SYMBOL_FUNC
:
1328 tmpb
.type
= M4__CHAIN_FUNC
;
1329 tmpb
.u
.builtin
= sb
->u
.builtin
;
1331 case M4_SYMBOL_COMP
:
1332 cb
= sb
->u
.u_c
.chain
;
1335 assert (!"m4_arg_equal");
1339 /* Compare each link of the chain. */
1342 if (ca
->type
== M4__CHAIN_ARGV
)
1345 tmpa
.type
= M4__CHAIN_STR
;
1346 tmpa
.u
.u_s
.str
= NULL
;
1349 m4__arg_print (context
, obs
, ca
->u
.u_a
.argv
, ca
->u
.u_a
.index
,
1350 m4__quote_cache (M4SYNTAX
, NULL
, ca
->quote_age
,
1352 argv
->flatten
|| ca
->u
.u_a
.flatten
, &chain
, NULL
,
1353 NULL
, false, false);
1354 assert (obstack_object_size (obs
) == 0 && chain
!= &tmpa
);
1355 chain
->next
= ca
->next
;
1359 if (cb
->type
== M4__CHAIN_ARGV
)
1362 tmpb
.type
= M4__CHAIN_STR
;
1363 tmpb
.u
.u_s
.str
= NULL
;
1366 m4__arg_print (context
, obs
, cb
->u
.u_a
.argv
, cb
->u
.u_a
.index
,
1367 m4__quote_cache (M4SYNTAX
, NULL
, cb
->quote_age
,
1369 argv
->flatten
|| cb
->u
.u_a
.flatten
, &chain
, NULL
,
1370 NULL
, false, false);
1371 assert (obstack_object_size (obs
) == 0 && chain
!= &tmpb
);
1372 chain
->next
= cb
->next
;
1376 if (ca
->type
== M4__CHAIN_FUNC
)
1378 if (cb
->type
!= M4__CHAIN_FUNC
|| ca
->u
.builtin
!= cb
->u
.builtin
)
1384 assert (ca
->type
== M4__CHAIN_STR
&& cb
->type
== M4__CHAIN_STR
);
1385 if (ca
->u
.u_s
.len
== cb
->u
.u_s
.len
)
1387 if (memcmp (ca
->u
.u_s
.str
, cb
->u
.u_s
.str
, ca
->u
.u_s
.len
) != 0)
1392 else if (ca
->u
.u_s
.len
< cb
->u
.u_s
.len
)
1394 if (memcmp (ca
->u
.u_s
.str
, cb
->u
.u_s
.str
, ca
->u
.u_s
.len
) != 0)
1396 tmpb
.next
= cb
->next
;
1397 tmpb
.u
.u_s
.str
= cb
->u
.u_s
.str
+ ca
->u
.u_s
.len
;
1398 tmpb
.u
.u_s
.len
= cb
->u
.u_s
.len
- ca
->u
.u_s
.len
;
1404 assert (cb
->u
.u_s
.len
< ca
->u
.u_s
.len
);
1405 if (memcmp (ca
->u
.u_s
.str
, cb
->u
.u_s
.str
, cb
->u
.u_s
.len
) != 0)
1407 tmpa
.next
= ca
->next
;
1408 tmpa
.u
.u_s
.str
= ca
->u
.u_s
.str
+ cb
->u
.u_s
.len
;
1409 tmpa
.u
.u_s
.len
= ca
->u
.u_s
.len
- cb
->u
.u_s
.len
;
1415 /* If we get this far, the two arguments are equal only if both
1416 chains are exhausted. */
1417 assert (ca
!= cb
|| !ca
);
1421 /* Given ARGV, return true if argument ARG is the empty string. This
1422 gives the same result as comparing m4_arg_len against 0, but is
1425 m4_arg_empty (m4_macro_args
*argv
, size_t arg
)
1429 assert (argv
->info
);
1430 return !argv
->info
->name_len
;
1432 return m4_arg_symbol (argv
, arg
) == &empty_symbol
;
1435 /* Given ARGV, return the length of argument ARG. Abort if the
1436 argument is not text and FLATTEN is not true. Indices beyond argc
1439 m4_arg_len (m4
*context
, m4_macro_args
*argv
, size_t arg
, bool flatten
)
1441 m4_symbol_value
*value
;
1442 m4__symbol_chain
*chain
;
1447 assert (argv
->info
);
1448 return argv
->info
->name_len
;
1450 if (argv
->argc
<= arg
)
1452 value
= arg_symbol (argv
, arg
, NULL
, flatten
);
1453 if (m4_is_symbol_value_text (value
))
1454 return m4_get_symbol_value_len (value
);
1455 assert (value
->type
== M4_SYMBOL_COMP
);
1456 chain
= value
->u
.u_c
.chain
;
1462 const m4_string_pair
*quotes
;
1463 switch (chain
->type
)
1466 len
+= chain
->u
.u_s
.len
;
1468 case M4__CHAIN_FUNC
:
1471 case M4__CHAIN_ARGV
:
1472 i
= chain
->u
.u_a
.index
;
1473 limit
= chain
->u
.u_a
.argv
->argc
- i
- chain
->u
.u_a
.skip_last
;
1474 quotes
= m4__quote_cache (M4SYNTAX
, NULL
, chain
->quote_age
,
1475 chain
->u
.u_a
.quotes
);
1478 len
+= (quotes
->len1
+ quotes
->len2
) * limit
;
1481 len
+= m4_arg_len (context
, chain
->u
.u_a
.argv
, i
++,
1482 flatten
|| chain
->u
.u_a
.flatten
);
1485 assert (!"m4_arg_len");
1488 chain
= chain
->next
;
1490 assert (len
|| flatten
);
1494 /* Given ARGV, return the builtin function referenced by argument ARG.
1495 Abort if it is not a single builtin. */
1497 m4_arg_func (m4_macro_args
*argv
, size_t arg
)
1499 return m4_get_symbol_value_func (m4_arg_symbol (argv
, arg
));
1502 /* Dump a representation of ARGV to the obstack OBS, starting with
1503 argument ARG. If QUOTES is non-NULL, each argument is displayed
1504 with those quotes. If FLATTEN, builtins are converted to empty
1505 quotes; if CHAINP, *CHAINP is updated with macro tokens; otherwise,
1506 builtins are represented by their name. Separate arguments with
1507 SEP, which defaults to a comma. If MAX_LEN is non-NULL, truncate
1508 the output after *MAX_LEN bytes are output and return true;
1509 otherwise, return false, and reduce *MAX_LEN by the number of bytes
1510 output. If QUOTE_EACH, the truncation length is reset for each
1511 argument, quotes do not count against length, and all arguments are
1512 printed; otherwise, quotes count against the length and trailing
1513 arguments may be discarded. If MODULE, print any details about
1514 originating modules; modules do not count against truncation
1515 length. MAX_LEN and CHAINP may not both be specified. */
1517 m4__arg_print (m4
*context
, m4_obstack
*obs
, m4_macro_args
*argv
, size_t arg
,
1518 const m4_string_pair
*quotes
, bool flatten
,
1519 m4__symbol_chain
**chainp
, const char *sep
, size_t *max_len
,
1520 bool quote_each
, bool module
)
1522 size_t len
= max_len
? *max_len
: SIZE_MAX
;
1524 bool use_sep
= false;
1526 size_t *plen
= quote_each
? NULL
: &len
;
1528 flatten
|= argv
->flatten
;
1530 assert (!max_len
&& *chainp
);
1533 sep_len
= strlen (sep
);
1534 for (i
= arg
; i
< argv
->argc
; i
++)
1536 if (quote_each
&& max_len
)
1538 if (use_sep
&& m4_shipout_string_trunc (obs
, sep
, sep_len
, NULL
, plen
))
1541 if (quotes
&& !quote_each
1542 && m4_shipout_string_trunc (obs
, quotes
->str1
, quotes
->len1
, NULL
,
1545 if (m4__symbol_value_print (context
, arg_symbol (argv
, i
, NULL
, flatten
),
1546 obs
, quote_each
? quotes
: NULL
, flatten
,
1547 chainp
, &len
, module
))
1549 if (quotes
&& !quote_each
1550 && m4_shipout_string_trunc (obs
, quotes
->str2
, quotes
->len2
, NULL
,
1557 m4__make_text_link (obs
, NULL
, chainp
);
1561 /* Create a new argument object using the same obstack as ARGV; thus,
1562 the new object will automatically be freed when the original is
1563 freed. Explicitly set the macro name (argv[0]) from ARGV0 with
1564 length ARGV0_LEN, and discard argv[1] of the wrapped ARGV. If
1565 FLATTEN, any builtins in ARGV are flattened to an empty string when
1566 referenced through the new object. If TRACE, then trace the macro
1567 regardless of global trace state. */
1569 m4_make_argv_ref (m4
*context
, m4_macro_args
*argv
, const char *argv0
,
1570 size_t argv0_len
, bool flatten
, bool trace
)
1572 m4_macro_args
*new_argv
;
1573 m4_symbol_value
*value
;
1574 m4_symbol_value
*new_value
;
1575 m4_obstack
*obs
= m4_arg_scratch (context
);
1578 info
= (m4_call_info
*) obstack_copy (obs
, argv
->info
, sizeof *info
);
1579 new_value
= (m4_symbol_value
*) obstack_alloc (obs
, sizeof *value
);
1580 value
= make_argv_ref (context
, new_value
, obs
, context
->expansion_level
- 1,
1581 argv
, 2, flatten
, NULL
);
1584 obstack_free (obs
, new_value
);
1585 new_argv
= (m4_macro_args
*) obstack_alloc (obs
, offsetof (m4_macro_args
,
1587 new_argv
->arraylen
= 0;
1588 new_argv
->wrapper
= false;
1589 new_argv
->has_ref
= false;
1590 new_argv
->flatten
= false;
1591 new_argv
->has_func
= false;
1595 new_argv
= (m4_macro_args
*) obstack_alloc (obs
, (offsetof (m4_macro_args
,
1598 new_argv
->arraylen
= 1;
1599 new_argv
->array
[0] = value
;
1600 new_argv
->wrapper
= true;
1601 new_argv
->has_ref
= argv
->has_ref
;
1602 new_argv
->flatten
= flatten
;
1603 new_argv
->has_func
= argv
->has_func
;
1605 new_argv
->argc
= argv
->argc
- 1;
1606 new_argv
->inuse
= false;
1607 new_argv
->quote_age
= argv
->quote_age
;
1608 new_argv
->info
= info
;
1609 info
->trace
= (argv
->info
->debug_level
& M4_DEBUG_TRACE_ALL
) || trace
;
1611 info
->name_len
= argv0_len
;
1612 new_argv
->level
= argv
->level
;
1616 /* Push argument ARG from ARGV, which must be a text token, onto the
1617 expansion stack OBS for rescanning. */
1619 m4_push_arg (m4
*context
, m4_obstack
*obs
, m4_macro_args
*argv
, size_t arg
)
1621 m4_symbol_value value
;
1625 assert (argv
->info
);
1626 m4_set_symbol_value_text (&value
, argv
->info
->name
, argv
->info
->name_len
,
1628 if (m4__push_symbol (context
, &value
, context
->expansion_level
- 1,
1633 m4__push_arg_quote (context
, obs
, argv
, arg
, NULL
);
1636 /* Push argument ARG from ARGV onto the expansion stack OBS for
1637 rescanning. ARG must be non-zero. QUOTES determines any quote
1638 delimiters that were in effect when the reference was created. */
1640 m4__push_arg_quote (m4
*context
, m4_obstack
*obs
, m4_macro_args
*argv
,
1641 size_t arg
, const m4_string_pair
*quotes
)
1644 m4_symbol_value
*value
= arg_symbol (argv
, arg
, &level
, false);
1647 obstack_grow (obs
, quotes
->str1
, quotes
->len1
);
1648 if (value
!= &empty_symbol
1649 && m4__push_symbol (context
, value
, level
, argv
->inuse
))
1652 obstack_grow (obs
, quotes
->str2
, quotes
->len2
);
1655 /* Push series of comma-separated arguments from ARGV onto the
1656 expansion stack OBS for rescanning. If SKIP, then don't push the
1657 first argument. If QUOTE, also push quoting around each arg. */
1659 m4_push_args (m4
*context
, m4_obstack
*obs
, m4_macro_args
*argv
, bool skip
,
1662 m4_symbol_value tmp
;
1663 m4_symbol_value
*value
;
1664 size_t i
= skip
? 2 : 1;
1665 const m4_string_pair
*quotes
= m4_get_syntax_quotes (M4SYNTAX
);
1667 if (argv
->argc
<= i
)
1670 if (argv
->argc
== i
+ 1)
1672 m4__push_arg_quote (context
, obs
, argv
, i
, quote
? quotes
: NULL
);
1676 value
= make_argv_ref (context
, &tmp
, obs
, -1, argv
, i
, argv
->flatten
,
1677 quote
? quotes
: NULL
);
1678 assert (value
== &tmp
);
1679 if (m4__push_symbol (context
, value
, -1, argv
->inuse
))
1683 /* Push arguments from ARGV onto the wrap stack for later rescanning.
1684 If GNU extensions are disabled, only the first argument is pushed;
1685 otherwise, all arguments are pushed and separated with a space. */
1687 m4_wrap_args (m4
*context
, m4_macro_args
*argv
)
1691 m4_symbol_value
*value
;
1692 m4__symbol_chain
*chain
;
1693 m4__symbol_chain
**end
;
1694 size_t limit
= m4_get_posixly_correct_opt (context
) ? 2 : argv
->argc
;
1696 if (limit
== 2 && m4_arg_empty (argv
, 1))
1699 obs
= m4__push_wrapup_init (context
, argv
->info
, &end
);
1700 for (i
= 1; i
< limit
; i
++)
1703 obstack_1grow (obs
, ' ');
1704 value
= m4_arg_symbol (argv
, i
);
1705 switch (value
->type
)
1707 case M4_SYMBOL_TEXT
:
1708 obstack_grow (obs
, m4_get_symbol_value_text (value
),
1709 m4_get_symbol_value_len (value
));
1711 case M4_SYMBOL_FUNC
:
1712 m4__append_builtin (obs
, value
->u
.builtin
, NULL
, end
);
1714 case M4_SYMBOL_COMP
:
1715 chain
= value
->u
.u_c
.chain
;
1718 switch (chain
->type
)
1721 obstack_grow (obs
, chain
->u
.u_s
.str
, chain
->u
.u_s
.len
);
1723 case M4__CHAIN_FUNC
:
1724 m4__append_builtin (obs
, chain
->u
.builtin
, NULL
, end
);
1726 case M4__CHAIN_ARGV
:
1727 m4__arg_print (context
, obs
, chain
->u
.u_a
.argv
,
1729 m4__quote_cache (M4SYNTAX
, NULL
,
1731 chain
->u
.u_a
.quotes
),
1732 chain
->u
.u_a
.flatten
, end
, NULL
, NULL
, false,
1736 assert (!"m4_wrap_args");
1739 chain
= chain
->next
;
1743 assert (!"m4_wrap_args");
1747 m4__push_wrapup_finish ();
1751 /* Define these last, so that earlier uses can benefit from the macros
1754 /* Given ARGV, return one greater than the number of arguments it
1758 m4_arg_argc (m4_macro_args
*argv
)
1763 /* Given ARGV, return the call context in effect when argument
1764 collection began. Only safe to call while the macro is being
1767 const m4_call_info
*
1768 m4_arg_info (m4_macro_args
*argv
)
1770 assert (argv
->info
);
1774 /* Return an obstack useful for scratch calculations, and which will
1775 not interfere with macro expansion. The obstack will be reset when
1776 expand_macro completes. */
1777 #undef m4_arg_scratch
1779 m4_arg_scratch (m4
*context
)
1781 m4__macro_arg_stacks
*stack
1782 = &context
->arg_stacks
[context
->expansion_level
- 1];
1783 assert (obstack_object_size (stack
->args
) == 0);