1 /* GNU m4 -- A simple macro processor
3 Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 2004, 2005, 2006, 2007
4 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). */
33 #include <sys/types.h>
35 #include "binary-io.h"
36 #include "clean-temp.h"
38 #include "close-stream.h"
48 #include "xvasprintf.h"
50 /* Canonicalize UNIX recognition macros. */
51 #if defined unix || defined __unix || defined __unix__ \
52 || defined _POSIX_VERSION || defined _POSIX2_VERSION \
53 || defined __NetBSD__ || defined __OpenBSD__ \
54 || defined __APPLE__ || defined __APPLE_CC__
58 /* Canonicalize Windows recognition macros. */
59 #if (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__
63 /* Canonicalize OS/2 recognition macro. */
68 /* Used for version mismatch, when -R detects a frozen file it can't parse. */
69 #define EXIT_MISMATCH 63
71 /* Various declarations. */
75 char *string
; /* characters of the string */
76 size_t length
; /* length of the string */
78 typedef struct string STRING
;
80 /* Memory allocation. */
81 #define obstack_chunk_alloc xmalloc
82 #define obstack_chunk_free free
84 /* Those must come first. */
85 typedef struct token_data token_data
;
86 typedef void builtin_func (struct obstack
*, int, token_data
**);
88 /* Gnulib's stdbool doesn't work with bool bitfields. For nicer
89 debugging, use bool when we know it works, but use the more
90 portable unsigned int elsewhere. */
92 typedef bool bool_bitfield
;
94 typedef unsigned int bool_bitfield
;
95 #endif /* ! __GNUC__ */
97 /* Take advantage of GNU C compiler source level optimization hints,
98 using portable macros. */
99 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 6)
100 # define M4_GNUC_ATTRIBUTE(args) __attribute__(args)
102 # define M4_GNUC_ATTRIBUTE(args)
103 #endif /* __GNUC__ */
105 #define M4_GNUC_UNUSED M4_GNUC_ATTRIBUTE((__unused__))
106 #define M4_GNUC_PRINTF(fmt, arg) \
107 M4_GNUC_ATTRIBUTE((__format__ (__printf__, fmt, arg)))
109 /* File: m4.c --- global definitions. */
112 extern int sync_output
; /* -s */
113 extern int debug_level
; /* -d */
114 extern size_t hash_table_size
; /* -H */
115 extern int no_gnu_extensions
; /* -G */
116 extern int prefix_all_builtins
; /* -P */
117 extern int max_debug_argument_length
; /* -l */
118 extern int suppress_warnings
; /* -Q */
119 extern int warning_status
; /* -E */
120 extern int nesting_limit
; /* -L */
121 #ifdef ENABLE_CHANGEWORD
122 extern const char *user_word_regexp
; /* -W */
125 /* Error handling. */
127 extern const char *program_name
;
129 void m4_error (int, int, const char *, ...) M4_GNUC_PRINTF(3, 4);
130 void m4_error_at_line (int, int, const char *, int,
131 const char *, ...) M4_GNUC_PRINTF(5, 6);
133 #define M4ERROR(Arglist) (m4_error Arglist)
134 #define M4ERROR_AT_LINE(Arglist) (m4_error_at_line Arglist)
137 void setup_stackovf_trap (char *const *, char *const *,
138 void (*handler
) (void));
141 /* File: debug.c --- debugging and tracing function. */
145 /* The value of debug_level is a bitmask of the following. */
147 /* a: show arglist in trace output */
148 #define DEBUG_TRACE_ARGS 1
149 /* e: show expansion in trace output */
150 #define DEBUG_TRACE_EXPANSION 2
151 /* q: quote args and expansion in trace output */
152 #define DEBUG_TRACE_QUOTE 4
153 /* t: trace all macros -- overrides trace{on,off} */
154 #define DEBUG_TRACE_ALL 8
155 /* l: add line numbers to trace output */
156 #define DEBUG_TRACE_LINE 16
157 /* f: add file name to trace output */
158 #define DEBUG_TRACE_FILE 32
159 /* p: trace path search of include files */
160 #define DEBUG_TRACE_PATH 64
161 /* c: show macro call before args collection */
162 #define DEBUG_TRACE_CALL 128
163 /* i: trace changes of input files */
164 #define DEBUG_TRACE_INPUT 256
165 /* x: add call id to trace output */
166 #define DEBUG_TRACE_CALLID 512
168 /* V: very verbose -- print everything */
169 #define DEBUG_TRACE_VERBOSE 1023
170 /* default flags -- equiv: aeq */
171 #define DEBUG_TRACE_DEFAULT 7
173 #define DEBUG_PRINT1(Fmt, Arg1) \
177 fprintf (debug, Fmt, Arg1); \
181 #define DEBUG_PRINT3(Fmt, Arg1, Arg2, Arg3) \
185 fprintf (debug, Fmt, Arg1, Arg2, Arg3); \
189 #define DEBUG_MESSAGE(Fmt) \
194 debug_message_prefix (); \
195 fprintf (debug, Fmt); \
196 putc ('\n', debug); \
201 #define DEBUG_MESSAGE1(Fmt, Arg1) \
206 debug_message_prefix (); \
207 fprintf (debug, Fmt, Arg1); \
208 putc ('\n', debug); \
213 #define DEBUG_MESSAGE2(Fmt, Arg1, Arg2) \
218 debug_message_prefix (); \
219 fprintf (debug, Fmt, Arg1, Arg2); \
220 putc ('\n', debug); \
225 void debug_init (void);
226 int debug_decode (const char *);
227 void debug_flush_files (void);
228 bool debug_set_output (const char *);
229 void debug_message_prefix (void);
231 void trace_prepre (const char *, int);
232 void trace_pre (const char *, int, int, token_data
**);
233 void trace_post (const char *, int, int, token_data
**, const char *);
235 /* File: input.c --- lexical definitions. */
237 /* Various different token types. */
240 TOKEN_EOF
, /* end of file */
241 TOKEN_STRING
, /* a quoted string or comment */
242 TOKEN_WORD
, /* an identifier */
246 TOKEN_SIMPLE
, /* any other single character */
247 TOKEN_MACDEF
/* a macro's definition (see "defn") */
250 /* The data for a token, a macro argument, and a macro definition. */
260 enum token_data_type type
;
266 #ifdef ENABLE_CHANGEWORD
276 #define TOKEN_DATA_TYPE(Td) ((Td)->type)
277 #define TOKEN_DATA_TEXT(Td) ((Td)->u.u_t.text)
278 #ifdef ENABLE_CHANGEWORD
279 # define TOKEN_DATA_ORIG_TEXT(Td) ((Td)->u.u_t.original_text)
281 #define TOKEN_DATA_FUNC(Td) ((Td)->u.func)
283 typedef enum token_type token_type
;
284 typedef enum token_data_type token_data_type
;
286 void input_init (void);
287 token_type
peek_token (void);
288 token_type
next_token (token_data
*, int *);
289 void skip_line (void);
291 /* push back input */
292 void push_file (FILE *, const char *, bool);
293 void push_macro (builtin_func
*);
294 struct obstack
*push_string_init (void);
295 const char *push_string_finish (void);
296 void push_wrapup (const char *);
297 bool pop_wrapup (void);
299 /* current input file, and line */
300 extern const char *current_file
;
301 extern int current_line
;
303 /* left and right quote, begin and end comment */
304 extern STRING bcomm
, ecomm
;
305 extern STRING lquote
, rquote
;
307 #define DEF_LQUOTE "`"
308 #define DEF_RQUOTE "\'"
309 #define DEF_BCOMM "#"
310 #define DEF_ECOMM "\n"
312 void set_quotes (const char *, const char *);
313 void set_comment (const char *, const char *);
314 #ifdef ENABLE_CHANGEWORD
315 void set_word_regexp (const char *);
318 /* File: output.c --- output functions. */
319 extern int current_diversion
;
320 extern int output_current_line
;
322 void output_init (void);
323 void output_exit (void);
324 void output_text (const char *, int);
325 void shipout_text (struct obstack
*, const char *, int, int);
326 void make_diversion (int);
327 void insert_diversion (int);
328 void insert_file (FILE *);
329 void freeze_diversions (FILE *);
331 /* File symtab.c --- symbol table definitions. */
333 /* Operation modes for lookup_symbol (). */
343 /* Symbol table entry. */
347 bool_bitfield traced
: 1;
348 bool_bitfield shadowed
: 1;
349 bool_bitfield macro_args
: 1;
350 bool_bitfield blind_no_args
: 1;
351 bool_bitfield deleted
: 1;
352 int pending_expansions
;
358 #define SYMBOL_NEXT(S) ((S)->next)
359 #define SYMBOL_TRACED(S) ((S)->traced)
360 #define SYMBOL_SHADOWED(S) ((S)->shadowed)
361 #define SYMBOL_MACRO_ARGS(S) ((S)->macro_args)
362 #define SYMBOL_BLIND_NO_ARGS(S) ((S)->blind_no_args)
363 #define SYMBOL_DELETED(S) ((S)->deleted)
364 #define SYMBOL_PENDING_EXPANSIONS(S) ((S)->pending_expansions)
365 #define SYMBOL_NAME(S) ((S)->name)
366 #define SYMBOL_TYPE(S) (TOKEN_DATA_TYPE (&(S)->data))
367 #define SYMBOL_TEXT(S) (TOKEN_DATA_TEXT (&(S)->data))
368 #define SYMBOL_FUNC(S) (TOKEN_DATA_FUNC (&(S)->data))
370 typedef enum symbol_lookup symbol_lookup
;
371 typedef struct symbol symbol
;
372 typedef void hack_symbol (symbol
*, void *);
374 #define HASHMAX 509 /* default, overridden by -Hsize */
376 extern symbol
**symtab
;
378 void free_symbol (symbol
*sym
);
379 void symtab_init (void);
380 symbol
*lookup_symbol (const char *, symbol_lookup
);
381 void hack_all_symbols (hack_symbol
*, void *);
383 /* File: macro.c --- macro expansion. */
385 void expand_input (void);
386 void call_macro (symbol
*, int, token_data
**, struct obstack
*);
388 /* File: builtin.c --- builtins. */
393 bool_bitfield gnu_extension
: 1;
394 bool_bitfield groks_macro_args
: 1;
395 bool_bitfield blind_if_no_args
: 1;
401 const char *unix_name
;
402 const char *gnu_name
;
406 typedef struct builtin builtin
;
407 typedef struct predefined predefined
;
408 struct re_pattern_buffer
;
411 /* The default sequence detects multi-digit parameters (obsolete after
412 1.4.x), and any use of extended arguments with the default ${}
413 syntax (new in 2.0). */
414 #define DEFAULT_MACRO_SEQUENCE "\\$\\({[^}]*}\\|[0-9][0-9]+\\)"
416 void builtin_init (void);
417 void define_builtin (const char *, const builtin
*, symbol_lookup
);
418 void set_macro_sequence (const char *);
419 void free_macro_sequence (void);
420 void define_user_macro (const char *, const char *, symbol_lookup
);
421 void undivert_all (void);
422 void expand_user_macro (struct obstack
*, symbol
*, int, token_data
**);
423 void m4_placeholder (struct obstack
*, int, token_data
**);
424 void init_pattern_buffer (struct re_pattern_buffer
*, struct re_registers
*);
426 const builtin
*find_builtin_by_addr (builtin_func
*);
427 const builtin
*find_builtin_by_name (const char *);
429 /* File: path.c --- path search for include files. */
431 void include_init (void);
432 void include_env_init (void);
433 void add_include_directory (const char *);
434 FILE *m4_path_search (const char *, char **);
436 /* File: eval.c --- expression evaluation. */
438 bool evaluate (const char *, int32_t *);
440 /* File: format.c --- printf like formatting. */
442 void format (struct obstack
*, int, token_data
**);
444 /* File: freeze.c --- frozen state files. */
446 void produce_frozen_state (const char *);
447 void reload_frozen_state (const char *);
449 /* Debugging the memory allocator. */
452 # define DMALLOC_FUNC_CHECK
453 # include <dmalloc.h>
456 /* Other debug stuff. */
459 # define DEBUG_INCL 1
460 # define DEBUG_INPUT 1
461 # define DEBUG_MACRO 1
462 # define DEBUG_OUTPUT 1
463 # define DEBUG_STKOVF 1
467 /* Convert a possibly-signed character to an unsigned character. This is
468 a bit safer than casting to unsigned char, since it catches some type
469 errors that the cast doesn't. */
471 static inline unsigned char to_uchar (char ch
) { return ch
; }
473 # define to_uchar(C) ((unsigned char) (C))