1 /* GNU m4 -- A simple macro processor
2 Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 2001, 2006, 2007
3 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. */
28 #include "m4private.h"
32 static void collect_arguments (m4
*, const char *, m4_symbol
*,
33 m4_obstack
*, m4_obstack
*);
34 static void expand_macro (m4
*, const char *, m4_symbol
*);
35 static void expand_token (m4
*, m4_obstack
*, m4__token_type
,
36 m4_symbol_value
*, int);
37 static bool expand_argument (m4
*, m4_obstack
*, m4_symbol_value
*,
39 static void process_macro (m4
*, m4_symbol_value
*, m4_obstack
*, int,
42 static void trace_prepre (m4
*, const char *, size_t,
44 static void trace_pre (m4
*, const char *, size_t, int,
46 static void trace_post (m4
*, const char *, size_t, int,
47 m4_symbol_value
**, m4_input_block
*, bool);
49 static void trace_format (m4
*, const char *, ...)
50 M4_GNUC_PRINTF (2, 3);
51 static void trace_header (m4
*, size_t);
52 static void trace_flush (m4
*);
55 /* Current recursion level in expand_macro (). */
56 static size_t expansion_level
= 0;
58 /* The number of the current call of expand_macro (). */
59 static size_t macro_call_id
= 0;
61 /* The shared stack of collected arguments for macro calls; as each
62 argument is collected, it is finished and its location stored in
63 argv_stack. This stack can be used simultaneously by multiple
64 macro calls, using obstack_regrow to handle partial objects
65 embedded in the stack. */
66 static struct obstack argc_stack
;
68 /* The shared stack of pointers to collected arguments for macro
69 calls. This object is never finished; we exploit the fact that
70 obstack_blank is documented to take a negative size to reduce the
72 static struct obstack argv_stack
;
74 /* This function reads all input, and expands each token, one at a time. */
76 m4_macro_expand_input (m4
*context
)
79 m4_symbol_value token
;
82 obstack_init (&argc_stack
);
83 obstack_init (&argv_stack
);
85 while ((type
= m4__next_token (context
, &token
, &line
, NULL
))
87 expand_token (context
, (m4_obstack
*) NULL
, type
, &token
, line
);
89 obstack_free (&argc_stack
, NULL
);
90 obstack_free (&argv_stack
, NULL
);
94 /* Expand one token, according to its type. Potential macro names
95 (M4_TOKEN_WORD) are looked up in the symbol table, to see if they have a
96 macro definition. If they have, they are expanded as macros, otherwise
97 the text are just copied to the output. */
99 expand_token (m4
*context
, m4_obstack
*obs
,
100 m4__token_type type
, m4_symbol_value
*token
, int line
)
103 const char *text
= (m4_is_symbol_value_text (token
)
104 ? m4_get_symbol_value_text (token
) : NULL
);
109 case M4_TOKEN_MACDEF
:
115 case M4_TOKEN_SIMPLE
:
116 case M4_TOKEN_STRING
:
118 m4_shipout_text (context
, obs
, text
, strlen (text
), line
);
123 const char *textp
= text
;
125 if (m4_has_syntax (M4SYNTAX
, to_uchar (*textp
), M4_SYNTAX_ESCAPE
))
128 symbol
= m4_symbol_lookup (M4SYMTAB
, textp
);
129 assert (! symbol
|| ! m4_is_symbol_void (symbol
));
131 || (symbol
->value
->type
== M4_SYMBOL_FUNC
132 && BIT_TEST (SYMBOL_FLAGS (symbol
), VALUE_BLIND_ARGS_BIT
)
133 && ! m4__next_token_is_open (context
)))
135 m4_shipout_text (context
, obs
, text
, strlen (text
), line
);
138 expand_macro (context
, textp
, symbol
);
143 assert (!"INTERNAL ERROR: bad token type in expand_token ()");
149 /* This function parses one argument to a macro call. It expects the
150 first left parenthesis or the separating comma to have been read by
151 the caller. It skips leading whitespace, then reads and expands
152 tokens, until it finds a comma or a right parenthesis at the same
153 level of parentheses. It returns a flag indicating whether the
154 argument read is the last for the active macro call. The arguments
155 are built on the obstack OBS, indirectly through expand_token ().
156 Report errors on behalf of CALLER. */
158 expand_argument (m4
*context
, m4_obstack
*obs
, m4_symbol_value
*argp
,
162 m4_symbol_value token
;
164 const char *file
= m4_get_current_file (context
);
165 int line
= m4_get_current_line (context
);
167 argp
->type
= M4_SYMBOL_VOID
;
169 /* Skip leading white space. */
172 type
= m4__next_token (context
, &token
, NULL
, caller
);
174 while (type
== M4_TOKEN_SPACE
);
182 if (paren_level
== 0)
184 /* FIXME - For now, we match the behavior of the branch,
185 except we don't issue warnings. But in the future,
186 we want to allow concatenation of builtins and
188 if (argp
->type
== M4_SYMBOL_FUNC
189 && obstack_object_size (obs
) == 0)
190 return type
== M4_TOKEN_COMMA
;
191 obstack_1grow (obs
, '\0');
192 VALUE_MODULE (argp
) = NULL
;
193 m4_set_symbol_value_text (argp
, obstack_finish (obs
));
194 return type
== M4_TOKEN_COMMA
;
198 case M4_TOKEN_SIMPLE
:
199 if (type
== M4_TOKEN_OPEN
)
201 else if (type
== M4_TOKEN_CLOSE
)
203 expand_token (context
, obs
, type
, &token
, line
);
207 m4_error_at_line (context
, EXIT_FAILURE
, 0, file
, line
, caller
,
208 _("end of file in argument list"));
213 case M4_TOKEN_STRING
:
214 expand_token (context
, obs
, type
, &token
, line
);
217 case M4_TOKEN_MACDEF
:
218 if (argp
->type
== M4_SYMBOL_VOID
&& obstack_object_size (obs
) == 0)
219 m4_symbol_value_copy (argp
, &token
);
221 argp
->type
= M4_SYMBOL_TEXT
;
225 assert (!"INTERNAL ERROR: bad token type in expand_argument ()");
229 type
= m4__next_token (context
, &token
, NULL
, caller
);
234 /* The macro expansion is handled by expand_macro (). It parses the
235 arguments, using collect_arguments (), and builds a table of pointers to
236 the arguments. The arguments themselves are stored on a local obstack.
237 Expand_macro () uses call_macro () to do the call of the macro.
239 Expand_macro () is potentially recursive, since it calls expand_argument
240 (), which might call expand_token (), which might call expand_macro ().
242 NAME points to storage on the token stack, so it is only valid
243 until a call to collect_arguments parses more tokens. SYMBOL is
244 the result of the symbol table lookup on NAME. */
246 expand_macro (m4
*context
, const char *name
, m4_symbol
*symbol
)
248 char *argc_base
= NULL
; /* Base of argc_stack on entry. */
249 unsigned int argc_size
; /* Size of argc_stack on entry. */
250 unsigned int argv_size
; /* Size of argv_stack on entry. */
251 m4_symbol_value
**argv
;
253 m4_obstack
*expansion
;
254 m4_input_block
*expanded
;
256 bool trace_expansion
= false;
258 m4_symbol_value
*value
;
260 /* Report errors at the location where the open parenthesis (if any)
261 was found, but after expansion, restore global state back to the
262 location of the close parenthesis. This is safe since we
263 guarantee that macro expansion does not alter the state of
264 current_file/current_line (dnl, include, and sinclude are special
265 cased in the input engine to ensure this fact). */
266 const char *loc_open_file
= m4_get_current_file (context
);
267 int loc_open_line
= m4_get_current_line (context
);
268 const char *loc_close_file
;
271 /* Grab the current value of this macro, because it may change while
272 collecting arguments. Likewise, grab any state needed during
274 value
= m4_get_symbol_value (symbol
);
275 traced
= (m4_is_debug_bit (context
, M4_DEBUG_TRACE_ALL
)
276 || m4_get_symbol_traced (symbol
));
278 trace_expansion
= m4_is_debug_bit (context
, M4_DEBUG_TRACE_EXPANSION
);
280 /* Prepare for macro expansion. */
281 VALUE_PENDING (value
)++;
283 if (m4_get_nesting_limit_opt (context
) > 0
284 && expansion_level
> m4_get_nesting_limit_opt (context
))
285 m4_error (context
, EXIT_FAILURE
, 0, NULL
, _("\
286 recursion limit of %zu exceeded, use -L<N> to change it"),
287 m4_get_nesting_limit_opt (context
));
290 my_call_id
= macro_call_id
;
292 argc_size
= obstack_object_size (&argc_stack
);
293 argv_size
= obstack_object_size (&argv_stack
);
295 argc_base
= obstack_finish (&argc_stack
);
297 if (traced
&& m4_is_debug_bit (context
, M4_DEBUG_TRACE_CALL
))
298 trace_prepre (context
, name
, my_call_id
, value
);
300 collect_arguments (context
, name
, symbol
, &argv_stack
, &argc_stack
);
302 argc
= ((obstack_object_size (&argv_stack
) - argv_size
)
303 / sizeof (m4_symbol_value
*));
304 argv
= (m4_symbol_value
**) ((char *) obstack_base (&argv_stack
)
306 /* Calling collect_arguments invalidated name, but we copied it as
308 name
= m4_get_symbol_value_text (argv
[0]);
310 loc_close_file
= m4_get_current_file (context
);
311 loc_close_line
= m4_get_current_line (context
);
312 m4_set_current_file (context
, loc_open_file
);
313 m4_set_current_line (context
, loc_open_line
);
316 trace_pre (context
, name
, my_call_id
, argc
, argv
);
318 expansion
= m4_push_string_init (context
);
319 m4_macro_call (context
, value
, expansion
, argc
, argv
);
320 expanded
= m4_push_string_finish ();
323 trace_post (context
, name
, my_call_id
, argc
, argv
, expanded
,
326 m4_set_current_file (context
, loc_close_file
);
327 m4_set_current_line (context
, loc_close_line
);
330 --VALUE_PENDING (value
);
331 if (BIT_TEST (VALUE_FLAGS (value
), VALUE_DELETED_BIT
))
332 m4_symbol_value_delete (value
);
335 obstack_regrow (&argc_stack
, argc_base
, argc_size
);
337 obstack_free (&argc_stack
, argv
[0]);
338 obstack_blank (&argv_stack
, -argc
* sizeof (m4_symbol_value
*));
341 /* Collect all the arguments to a call of the macro SYMBOL (called NAME).
342 The arguments are stored on the obstack ARGUMENTS and a table of pointers
343 to the arguments on the obstack ARGPTR. */
345 collect_arguments (m4
*context
, const char *name
, m4_symbol
*symbol
,
346 m4_obstack
*argptr
, m4_obstack
*arguments
)
348 m4_symbol_value token
;
349 m4_symbol_value
*tokenp
;
351 bool groks_macro_args
;
353 groks_macro_args
= BIT_TEST (SYMBOL_FLAGS (symbol
), VALUE_MACRO_ARGS_BIT
);
355 tokenp
= (m4_symbol_value
*) obstack_alloc (arguments
, sizeof *tokenp
);
356 m4_set_symbol_value_text (tokenp
, (char *) obstack_copy0 (arguments
, name
,
358 name
= m4_get_symbol_value_text (tokenp
);
359 obstack_ptr_grow (argptr
, tokenp
);
361 if (m4__next_token_is_open (context
))
363 m4__next_token (context
, &token
, NULL
, name
); /* gobble parenthesis */
366 more_args
= expand_argument (context
, arguments
, &token
, name
);
368 if (!groks_macro_args
&& m4_is_symbol_value_func (&token
))
370 VALUE_MODULE (&token
) = NULL
;
371 m4_set_symbol_value_text (&token
, "");
373 tokenp
= (m4_symbol_value
*) obstack_copy (arguments
, &token
,
375 obstack_ptr_grow (argptr
, tokenp
);
382 /* The actual call of a macro is handled by m4_macro_call ().
383 m4_macro_call () is passed a SYMBOL, whose type is used to
384 call either a builtin function, or the user macro expansion
385 function process_macro (). There are ARGC arguments to
386 the call, stored in the ARGV table. The expansion is left on
387 the obstack EXPANSION. Macro tracing is also handled here. */
389 m4_macro_call (m4
*context
, m4_symbol_value
*value
, m4_obstack
*expansion
,
390 int argc
, m4_symbol_value
**argv
)
392 if (m4_bad_argc (context
, argc
, argv
,
393 VALUE_MIN_ARGS (value
), VALUE_MAX_ARGS (value
),
394 BIT_TEST (VALUE_FLAGS (value
),
395 VALUE_SIDE_EFFECT_ARGS_BIT
)))
397 if (m4_is_symbol_value_text (value
))
399 process_macro (context
, value
, expansion
, argc
, argv
);
401 else if (m4_is_symbol_value_func (value
))
403 (*m4_get_symbol_value_func (value
)) (context
, expansion
, argc
, argv
);
405 else if (m4_is_symbol_value_placeholder (value
))
407 m4_warn (context
, 0, M4ARG (0),
408 _("builtin `%s' requested by frozen file not found"),
409 m4_get_symbol_value_placeholder (value
));
413 assert (!"INTERNAL ERROR: bad symbol type in call_macro ()");
418 /* This function handles all expansion of user defined and predefined
419 macros. It is called with an obstack OBS, where the macros expansion
420 will be placed, as an unfinished object. SYMBOL points to the macro
421 definition, giving the expansion text. ARGC and ARGV are the arguments,
424 process_macro (m4
*context
, m4_symbol_value
*value
, m4_obstack
*obs
,
425 int argc
, m4_symbol_value
**argv
)
430 for (text
= m4_get_symbol_value_text (value
); *text
!= '\0';)
434 if (!m4_has_syntax (M4SYNTAX
, to_uchar (*text
), M4_SYNTAX_DOLLAR
))
436 obstack_1grow (obs
, *text
);
443 case '0': case '1': case '2': case '3': case '4':
444 case '5': case '6': case '7': case '8': case '9':
445 /* FIXME - multidigit arguments should convert over to ${10}
446 syntax instead of $10; see
447 http://lists.gnu.org/archive/html/m4-discuss/2006-08/msg00028.html
448 for more discussion. */
449 if (m4_get_posixly_correct_opt (context
) || !isdigit(text
[1]))
456 i
= (int) strtol (text
, &endp
, 10);
460 m4_shipout_string (context
, obs
, M4ARG (i
), 0, false);
463 case '#': /* number of arguments */
464 m4_shipout_int (obs
, argc
- 1);
468 case '*': /* all arguments */
469 case '@': /* ... same, but quoted */
470 m4_dump_args (context
, obs
, argc
, argv
, ",", *text
== '@');
475 if (m4_get_posixly_correct_opt (context
)
476 || !VALUE_ARG_SIGNATURE (value
))
478 obstack_1grow (obs
, ch
);
487 *endp
&& m4_has_syntax (M4SYNTAX
, to_uchar (*endp
),
488 (M4_SYNTAX_OTHER
| M4_SYNTAX_ALPHA
494 key
= xstrndup (text
, len
);
498 struct m4_symbol_arg
**arg
499 = (struct m4_symbol_arg
**)
500 m4_hash_lookup (VALUE_ARG_SIGNATURE (value
), key
);
504 i
= SYMBOL_ARG_INDEX (*arg
);
507 m4_shipout_string (context
, obs
, M4ARG (i
), 0, false);
510 assert (!"INTERNAL ERROR: out of range reference");
517 m4_error (context
, 0, 0, M4ARG (0),
518 _("unterminated parameter reference: %s"),
522 text
= *endp
? 1 + endp
: endp
;
534 /* The rest of this file contains the functions for macro tracing output.
535 All tracing output for a macro call is collected on an obstack TRACE,
536 and printed whenever the line is complete. This prevents tracing
537 output from interfering with other debug messages generated by the
540 /* Tracing output is formatted here, by a simplified printf-to-obstack
541 function trace_format (). Understands only %s, %d, %zu (size_t
544 trace_format (m4
*context
, const char *fmt
, ...)
550 va_start (args
, fmt
);
554 while ((ch
= *fmt
++) != '\0' && ch
!= '%')
555 obstack_1grow (&context
->trace_messages
, ch
);
563 s
= va_arg (args
, const char *);
568 int d
= va_arg (args
, int);
569 char nbuf
[INT_BUFSIZE_BOUND (int)];
571 sprintf (nbuf
, "%d", d
);
580 size_t z
= va_arg (args
, size_t);
581 char nbuf
[INT_BUFSIZE_BOUND (size_t)];
583 sprintf (nbuf
, "%zu", z
);
593 obstack_grow (&context
->trace_messages
, s
, strlen (s
));
599 /* Format the standard header attached to all tracing output lines. */
601 trace_header (m4
*context
, size_t id
)
603 trace_format (context
, "m4trace:");
604 if (m4_get_current_line (context
))
606 if (m4_is_debug_bit (context
, M4_DEBUG_TRACE_FILE
))
607 trace_format (context
, "%s:", m4_get_current_file (context
));
608 if (m4_is_debug_bit (context
, M4_DEBUG_TRACE_LINE
))
609 trace_format (context
, "%d:", m4_get_current_line (context
));
611 trace_format (context
, " -%zu- ", expansion_level
);
612 if (m4_is_debug_bit (context
, M4_DEBUG_TRACE_CALLID
))
613 trace_format (context
, "id %zu: ", id
);
616 /* Print current tracing line, and clear the obstack. */
618 trace_flush (m4
*context
)
622 obstack_1grow (&context
->trace_messages
, '\n');
623 obstack_1grow (&context
->trace_messages
, '\0');
624 line
= obstack_finish (&context
->trace_messages
);
625 if (m4_get_debug_file (context
))
626 fputs (line
, m4_get_debug_file (context
));
627 obstack_free (&context
->trace_messages
, line
);
630 /* Do pre-argument-collction tracing for macro NAME. Used from
633 trace_prepre (m4
*context
, const char *name
, size_t id
, m4_symbol_value
*value
)
635 bool quote
= m4_is_debug_bit (context
, M4_DEBUG_TRACE_QUOTE
);
636 const char *lquote
= m4_get_syntax_lquote (M4SYNTAX
);
637 const char *rquote
= m4_get_syntax_rquote (M4SYNTAX
);
638 size_t arg_length
= m4_get_max_debug_arg_length_opt (context
);
639 bool module
= m4_is_debug_bit (context
, M4_DEBUG_TRACE_MODULE
);
641 trace_header (context
, id
);
642 trace_format (context
, "%s ... = ", name
);
643 m4_symbol_value_print (value
, &context
->trace_messages
,
644 quote
, lquote
, rquote
, arg_length
, module
);
645 trace_flush (context
);
648 /* Format the parts of a trace line, that can be made before the macro is
649 actually expanded. Used from expand_macro (). */
651 trace_pre (m4
*context
, const char *name
, size_t id
,
652 int argc
, m4_symbol_value
**argv
)
656 trace_header (context
, id
);
657 trace_format (context
, "%s", name
);
659 if ((argc
> 1) && m4_is_debug_bit (context
, M4_DEBUG_TRACE_ARGS
))
661 bool quote
= m4_is_debug_bit (context
, M4_DEBUG_TRACE_QUOTE
);
662 const char *lquote
= m4_get_syntax_lquote (M4SYNTAX
);
663 const char *rquote
= m4_get_syntax_rquote (M4SYNTAX
);
664 size_t arg_length
= m4_get_max_debug_arg_length_opt (context
);
665 bool module
= m4_is_debug_bit (context
, M4_DEBUG_TRACE_MODULE
);
667 trace_format (context
, "(");
668 for (i
= 1; i
< argc
; i
++)
671 trace_format (context
, ", ");
673 m4_symbol_value_print (argv
[i
], &context
->trace_messages
,
674 quote
, lquote
, rquote
, arg_length
, module
);
676 trace_format (context
, ")");
680 /* Format the final part of a trace line and print it all. Used from
683 trace_post (m4
*context
, const char *name
, size_t id
,
684 int argc
, m4_symbol_value
**argv
, m4_input_block
*expanded
,
685 bool trace_expansion
)
689 trace_format (context
, " -> ");
690 m4_input_print (context
, &context
->trace_messages
, expanded
);
693 trace_flush (context
);