1 /* GNU m4 -- A simple macro processor
3 Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 2004, 2005, 2006, 2007,
4 2008 Free Software Foundation, Inc.
6 This file is part of GNU M4.
8 GNU M4 is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU M4 is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /* We use <config.h> instead of "config.h" so that a compilation
23 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
24 (which it would do because it found this file in $srcdir). */
35 #include <sys/types.h>
37 #include "binary-io.h"
38 #include "clean-temp.h"
40 #include "close-stream.h"
50 #include "vasnprintf.h"
54 #include "xvasprintf.h"
56 /* Canonicalize UNIX recognition macros. */
57 #if defined unix || defined __unix || defined __unix__ \
58 || defined _POSIX_VERSION || defined _POSIX2_VERSION \
59 || defined __NetBSD__ || defined __OpenBSD__ \
60 || defined __APPLE__ || defined __APPLE_CC__
64 /* Canonicalize Windows recognition macros. */
65 #if (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__
69 /* Canonicalize OS/2 recognition macro. */
74 /* Used for version mismatch, when -R detects a frozen file it can't parse. */
75 #define EXIT_MISMATCH 63
77 /* M4 1.4.x is not yet internationalized. But when it is, this can be
78 redefined as gettext(). */
79 #define _(STRING) STRING
81 /* Various declarations. */
83 /* Describes a pair of strings, such as begin and end quotes. */
91 typedef struct string_pair string_pair
;
93 /* Memory allocation. */
94 #define obstack_chunk_alloc xmalloc
95 #define obstack_chunk_free free
97 /* These must come first. */
98 typedef struct input_block input_block
;
99 typedef struct token_data token_data
;
100 typedef struct macro_arguments macro_arguments
;
101 typedef void builtin_func (struct obstack
*, int, macro_arguments
*);
103 /* Gnulib's stdbool doesn't work with bool bitfields. For nicer
104 debugging, use bool when we know it works, but use the more
105 portable unsigned int elsewhere. */
107 typedef bool bool_bitfield
;
109 typedef unsigned int bool_bitfield
;
110 #endif /* !__GNUC__ */
112 /* Take advantage of GNU C compiler source level optimization hints,
113 using portable macros. */
114 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 6)
115 # define M4_GNUC_ATTRIBUTE(args) __attribute__ (args)
117 # define M4_GNUC_ATTRIBUTE(args)
118 #endif /* __GNUC__ */
120 #define M4_GNUC_UNUSED M4_GNUC_ATTRIBUTE ((__unused__))
121 #define M4_GNUC_PRINTF(fmt, arg) \
122 M4_GNUC_ATTRIBUTE ((__format__ (__printf__, fmt, arg)))
124 /* File: m4.c --- global definitions. */
127 extern int sync_output
; /* -s */
128 extern int debug_level
; /* -d */
129 extern size_t hash_table_size
; /* -H */
130 extern int no_gnu_extensions
; /* -G */
131 extern int prefix_all_builtins
; /* -P */
132 extern int max_debug_argument_length
; /* -l */
133 extern int suppress_warnings
; /* -Q */
134 extern int warning_status
; /* -E */
135 extern int nesting_limit
; /* -L */
136 #ifdef ENABLE_CHANGEWORD
137 extern const char *user_word_regexp
; /* -W */
140 /* Error handling. */
142 extern const char *program_name
;
144 void m4_error (int, int, const char *, const char *, ...)
145 M4_GNUC_PRINTF (4, 5);
146 void m4_error_at_line (int, int, const char *, int, const char *,
147 const char *, ...) M4_GNUC_PRINTF (6, 7);
148 void m4_warn (int, const char *, const char *, ...) M4_GNUC_PRINTF (3, 4);
149 void m4_warn_at_line (int, const char *, int, const char *,
150 const char *, ...) M4_GNUC_PRINTF (5, 6);
153 void setup_stackovf_trap (char *const *, char *const *,
154 void (*handler
) (void));
157 /* File: debug.c --- debugging and tracing function. */
161 /* The value of debug_level is a bitmask of the following. */
163 /* a: show arglist in trace output */
164 #define DEBUG_TRACE_ARGS 0x001
165 /* e: show expansion in trace output */
166 #define DEBUG_TRACE_EXPANSION 0x002
167 /* q: quote args and expansion in trace output */
168 #define DEBUG_TRACE_QUOTE 0x004
169 /* t: trace all macros -- overrides trace{on,off} */
170 #define DEBUG_TRACE_ALL 0x008
171 /* l: add line numbers to trace output */
172 #define DEBUG_TRACE_LINE 0x010
173 /* f: add file name to trace output */
174 #define DEBUG_TRACE_FILE 0x020
175 /* p: trace path search of include files */
176 #define DEBUG_TRACE_PATH 0x040
177 /* c: show macro call before args collection */
178 #define DEBUG_TRACE_CALL 0x080
179 /* i: trace changes of input files */
180 #define DEBUG_TRACE_INPUT 0x100
181 /* x: add call id to trace output */
182 #define DEBUG_TRACE_CALLID 0x200
184 /* V: very verbose -- print everything */
185 #define DEBUG_TRACE_VERBOSE 0x377
186 /* default flags -- equiv: aeq */
187 #define DEBUG_TRACE_DEFAULT 0x007
189 #define DEBUG_PRINT1(Fmt, Arg1) \
193 xfprintf (debug, Fmt, Arg1); \
197 #define DEBUG_PRINT3(Fmt, Arg1, Arg2, Arg3) \
201 xfprintf (debug, Fmt, Arg1, Arg2, Arg3); \
205 #define DEBUG_MESSAGE(Fmt) \
210 debug_message_prefix (); \
211 xfprintf (debug, Fmt); \
212 putc ('\n', debug); \
217 #define DEBUG_MESSAGE1(Fmt, Arg1) \
222 debug_message_prefix (); \
223 xfprintf (debug, Fmt, Arg1); \
224 putc ('\n', debug); \
229 #define DEBUG_MESSAGE2(Fmt, Arg1, Arg2) \
234 debug_message_prefix (); \
235 xfprintf (debug, Fmt, Arg1, Arg2); \
236 putc ('\n', debug); \
241 void debug_init (void);
242 int debug_decode (const char *);
243 void debug_flush_files (void);
244 bool debug_set_output (const char *, const char *);
245 void debug_message_prefix (void);
247 void trace_prepre (const char *, int);
248 void trace_pre (const char *, int, macro_arguments
*);
249 void trace_post (const char *, int, macro_arguments
*,
250 const input_block
*);
253 /* File: input.c --- lexical definitions. */
255 typedef struct token_chain token_chain
;
257 /* Various different token types. Avoid overlap with token_data_type,
258 since the shared prefix of the enumerators is a bit confusing. */
261 TOKEN_EOF
= 4,/* End of file, TOKEN_VOID. */
262 TOKEN_STRING
, /* Quoted string or comment, TOKEN_TEXT or TOKEN_COMP. */
263 TOKEN_WORD
, /* An identifier, TOKEN_TEXT. */
264 TOKEN_OPEN
, /* Active character `(', TOKEN_TEXT. */
265 TOKEN_COMMA
, /* Active character `,', TOKEN_TEXT. */
266 TOKEN_CLOSE
, /* Active character `)', TOKEN_TEXT. */
267 TOKEN_SIMPLE
, /* Any other single character, TOKEN_TEXT. */
268 TOKEN_MACDEF
, /* A macro's definition (see "defn"), TOKEN_FUNC. */
269 TOKEN_ARGV
/* A series of parameters, TOKEN_COMP. */
272 /* The data for a token, a macro argument, and a macro definition. */
275 TOKEN_VOID
, /* Token still being constructed, u is invalid. */
276 TOKEN_TEXT
, /* Straight text, u.u_t is valid. */
277 TOKEN_FUNC
, /* Builtin function definition, u.func is valid. */
278 TOKEN_COMP
/* Composite argument, u.u_c is valid. */
281 /* A link in a chain of token data. */
282 enum token_chain_type
284 CHAIN_STR
, /* Link contains a string, u.u_s is valid. */
285 /* TODO add CHAIN_FUNC. */
286 CHAIN_ARGV
/* Link contains a $@ reference, u.u_a is valid. */
289 /* Composite tokens are built of a linked list of chains. Each link
290 of the chain is either a single text reference (ie. $1), or an argv
291 reference (ie. $@). */
294 token_chain
*next
; /* Pointer to next link of chain. */
295 enum token_chain_type type
; /* Type of this link. */
296 unsigned int quote_age
; /* Quote_age of this link of chain, or 0. */
301 const char *str
; /* Pointer to text. */
302 size_t len
; /* Remaining length of str. */
303 int level
; /* Expansion level of link content, or -1. */
308 macro_arguments
*argv
; /* Reference to earlier $@. */
309 unsigned int index
; /* Argument index within argv. */
310 bool_bitfield flatten
: 1; /* True to treat builtins as text. */
311 bool_bitfield comma
: 1; /* True when `,' is next input. */
312 bool_bitfield skip_last
: 1; /* True if last argument omitted. */
313 bool_bitfield has_func
: 1; /* True if argv includes func. */
314 const string_pair
*quotes
; /* NULL for $*, quotes for $@. */
321 /* The content of a token or macro argument. */
324 enum token_data_type type
;
329 /* We don't support NUL in text, yet. So len is just a
330 cache for now. But it will be essential if we ever DO
333 char *text
; /* The contents of the token. */
334 /* The value of quote_age when this token was scanned. If
335 this token is later encountered in the context of
336 scanning a quoted string, and quote_age has not changed,
337 then rescanning this string is provably unnecessary. If
338 zero, then this string potentially contains content that
339 might change the parse on rescan. Ignored for 0 len. */
340 unsigned int quote_age
;
341 #ifdef ENABLE_CHANGEWORD
342 /* If changeword is in effect, and contains a () group, then
343 this contains the entire token, while text contains the
344 portion that matched the () group to form a macro name.
345 Otherwise, this field is unused. */
346 const char *original_text
;
352 /* Composite text: a linked list of straight text and $@
356 token_chain
*chain
; /* First link of the chain. */
357 token_chain
*end
; /* Last link of the chain. */
358 bool_bitfield wrapper
: 1; /* True if this is a $@ ref. */
359 bool_bitfield has_func
: 1; /* True if chain includes func. */
366 #define TOKEN_DATA_TYPE(Td) ((Td)->type)
367 #define TOKEN_DATA_LEN(Td) ((Td)->u.u_t.len)
368 #define TOKEN_DATA_TEXT(Td) ((Td)->u.u_t.text)
369 #define TOKEN_DATA_QUOTE_AGE(Td) ((Td)->u.u_t.quote_age)
370 #ifdef ENABLE_CHANGEWORD
371 # define TOKEN_DATA_ORIG_TEXT(Td) ((Td)->u.u_t.original_text)
373 #define TOKEN_DATA_FUNC(Td) ((Td)->u.func)
375 typedef enum token_type token_type
;
376 typedef enum token_data_type token_data_type
;
378 void input_init (void);
379 token_type
peek_token (void);
380 token_type
next_token (token_data
*, int *, struct obstack
*, bool,
382 void skip_line (const char *);
384 /* push back input */
385 void make_text_link (struct obstack
*, token_chain
**, token_chain
**);
386 void push_file (FILE *, const char *, bool);
387 void push_macro (builtin_func
*);
388 struct obstack
*push_string_init (void);
389 bool push_token (token_data
*, int, bool);
390 const input_block
*push_string_finish (void);
391 struct obstack
*push_wrapup_init (void);
392 void push_wrapup_finish (void);
393 bool pop_wrapup (void);
394 void input_print (struct obstack
*, const input_block
*);
396 /* current input file, and line */
397 extern const char *current_file
;
398 extern int current_line
;
400 /* left and right quote, begin and end comment */
401 extern string_pair curr_comm
;
402 extern string_pair curr_quote
;
404 #define DEF_LQUOTE "`"
405 #define DEF_RQUOTE "\'"
406 #define DEF_BCOMM "#"
407 #define DEF_ECOMM "\n"
409 void set_quotes (const char *, const char *);
410 void set_comment (const char *, const char *);
411 #ifdef ENABLE_CHANGEWORD
412 void set_word_regexp (const char *, const char *);
414 unsigned int quote_age (void);
415 bool safe_quotes (void);
416 const string_pair
*quote_cache (struct obstack
*, unsigned int,
417 const string_pair
*);
419 /* File: output.c --- output functions. */
420 extern int current_diversion
;
421 extern int output_current_line
;
423 void output_init (void);
424 void output_exit (void);
425 void output_text (const char *, int);
426 void divert_text (struct obstack
*, const char *, int, int);
427 bool shipout_string_trunc (struct obstack
*, const char *, size_t, int *);
428 void make_diversion (int);
429 void insert_diversion (int);
430 void insert_file (FILE *);
431 void freeze_diversions (FILE *);
433 /* File symtab.c --- symbol table definitions. */
435 /* Operation modes for lookup_symbol (). */
445 /* Symbol table entry. */
449 bool_bitfield traced
: 1;
450 bool_bitfield shadowed
: 1;
451 bool_bitfield macro_args
: 1;
452 bool_bitfield blind_no_args
: 1;
453 bool_bitfield deleted
: 1;
454 int pending_expansions
;
457 token_data data
; /* Type should be only TOKEN_TEXT or TOKEN_FUNC. */
460 #define SYMBOL_NEXT(S) ((S)->next)
461 #define SYMBOL_TRACED(S) ((S)->traced)
462 #define SYMBOL_SHADOWED(S) ((S)->shadowed)
463 #define SYMBOL_MACRO_ARGS(S) ((S)->macro_args)
464 #define SYMBOL_BLIND_NO_ARGS(S) ((S)->blind_no_args)
465 #define SYMBOL_DELETED(S) ((S)->deleted)
466 #define SYMBOL_PENDING_EXPANSIONS(S) ((S)->pending_expansions)
467 #define SYMBOL_NAME(S) ((S)->name)
468 #define SYMBOL_TYPE(S) (TOKEN_DATA_TYPE (&(S)->data))
469 #define SYMBOL_TEXT(S) (TOKEN_DATA_TEXT (&(S)->data))
470 #define SYMBOL_FUNC(S) (TOKEN_DATA_FUNC (&(S)->data))
472 typedef enum symbol_lookup symbol_lookup
;
473 typedef struct symbol symbol
;
474 typedef void hack_symbol (symbol
*, void *);
476 #define HASHMAX 509 /* default, overridden by -Hsize */
478 extern symbol
**symtab
;
480 void free_symbol (symbol
*sym
);
481 void symtab_init (void);
482 symbol
*lookup_symbol (const char *, symbol_lookup
);
483 void hack_all_symbols (hack_symbol
*, void *);
485 /* File: macro.c --- macro expansion. */
487 extern int expansion_level
;
489 void expand_input (void);
490 void call_macro (symbol
*, int, macro_arguments
*, struct obstack
*);
491 size_t adjust_refcount (int, bool);
493 bool arg_adjust_refcount (macro_arguments
*, bool);
494 unsigned int arg_argc (macro_arguments
*);
495 token_data_type
arg_type (macro_arguments
*, unsigned int);
496 const char *arg_text (macro_arguments
*, unsigned int);
497 bool arg_equal (macro_arguments
*, unsigned int, unsigned int);
498 bool arg_empty (macro_arguments
*, unsigned int);
499 size_t arg_len (macro_arguments
*, unsigned int);
500 builtin_func
*arg_func (macro_arguments
*, unsigned int);
501 struct obstack
*arg_scratch (void);
502 bool arg_print (struct obstack
*, macro_arguments
*, unsigned int,
503 const string_pair
*, bool, const char *, int *, bool);
504 macro_arguments
*make_argv_ref (macro_arguments
*, const char *, size_t,
506 void push_arg (struct obstack
*, macro_arguments
*, unsigned int);
507 void push_arg_quote (struct obstack
*, macro_arguments
*, unsigned int,
508 const string_pair
*);
509 void push_args (struct obstack
*, macro_arguments
*, bool, bool);
511 /* Grab the text at argv index I. Assumes macro_argument *argv is in
512 scope, and aborts if the argument is not text. */
513 #define ARG(i) arg_text (argv, i)
515 /* Grab the text length at argv index I. Assumes macro_argument *argv
516 is in scope, and aborts if the argument is not text. */
517 #define ARG_LEN(i) arg_len (argv, i)
520 /* File: builtin.c --- builtins. */
525 bool_bitfield gnu_extension
: 1;
526 bool_bitfield groks_macro_args
: 1;
527 bool_bitfield blind_if_no_args
: 1;
533 const char *unix_name
;
534 const char *gnu_name
;
538 typedef struct builtin builtin
;
539 typedef struct predefined predefined
;
540 struct re_pattern_buffer
;
543 /* The default sequence detects multi-digit parameters (obsolete after
544 1.4.x), and any use of extended arguments with the default ${}
545 syntax (new in 2.0). */
546 #define DEFAULT_MACRO_SEQUENCE "\\$\\({[^}]*}\\|[0-9][0-9]+\\)"
548 void builtin_init (void);
549 bool bad_argc (const char *, int, unsigned int, unsigned int);
550 void define_builtin (const char *, const builtin
*, symbol_lookup
);
551 void set_macro_sequence (const char *);
552 void free_regex (void);
553 void define_user_macro (const char *, size_t, const char *, symbol_lookup
);
554 void undivert_all (void);
555 void expand_user_macro (struct obstack
*, symbol
*, int, macro_arguments
*);
556 void m4_placeholder (struct obstack
*, int, macro_arguments
*);
557 void init_pattern_buffer (struct re_pattern_buffer
*, struct re_registers
*);
558 const char *ntoa (int32_t, int);
560 const builtin
*find_builtin_by_addr (builtin_func
*);
561 const builtin
*find_builtin_by_name (const char *);
562 void func_print (struct obstack
*, const builtin
*, bool, const string_pair
*);
564 /* File: path.c --- path search for include files. */
566 void include_init (void);
567 void include_env_init (void);
568 void add_include_directory (const char *);
569 FILE *m4_path_search (const char *, char **);
571 /* File: eval.c --- expression evaluation. */
573 bool evaluate (const char *, const char *, int32_t *);
575 /* File: format.c --- printf like formatting. */
577 void format (struct obstack
*, int, macro_arguments
*);
579 /* File: freeze.c --- frozen state files. */
581 void produce_frozen_state (const char *);
582 void reload_frozen_state (const char *);
584 /* Debugging the memory allocator. */
587 # define DMALLOC_FUNC_CHECK
588 # include <dmalloc.h>
591 /* Other debug stuff. */
594 # define DEBUG_INCL 1
595 # define DEBUG_INPUT 1
596 # define DEBUG_MACRO 1
597 # define DEBUG_OUTPUT 1
598 # define DEBUG_REGEX 1
599 # define DEBUG_STKOVF 1
603 /* Convert a possibly-signed character to an unsigned character. This is
604 a bit safer than casting to unsigned char, since it catches some type
605 errors that the cast doesn't. */
607 static inline unsigned char to_uchar (char ch
) { return ch
; }
609 # define to_uchar(C) ((unsigned char) (C))