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 program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
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"
49 #include "xvasprintf.h"
51 /* Canonicalize UNIX recognition macros. */
52 #if defined unix || defined __unix || defined __unix__ \
53 || defined _POSIX_VERSION || defined _POSIX2_VERSION \
54 || defined __NetBSD__ || defined __OpenBSD__ \
55 || defined __APPLE__ || defined __APPLE_CC__
59 /* Canonicalize Windows recognition macros. */
60 #if (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__
64 /* Canonicalize OS/2 recognition macro. */
69 /* Used for version mismatch, when -R detects a frozen file it can't parse. */
70 #define EXIT_MISMATCH 63
72 /* Various declarations. */
76 char *string
; /* characters of the string */
77 size_t length
; /* length of the string */
79 typedef struct string STRING
;
81 /* Memory allocation. */
82 #define obstack_chunk_alloc xmalloc
83 #define obstack_chunk_free free
85 /* Those must come first. */
86 typedef struct token_data token_data
;
87 typedef void builtin_func (struct obstack
*, int, token_data
**);
89 /* Take advantage of GNU C compiler source level optimization hints,
90 using portable macros. */
91 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 6)
92 # define M4_GNUC_ATTRIBUTE(args) __attribute__(args)
94 # define M4_GNUC_ATTRIBUTE(args)
97 #define M4_GNUC_UNUSED M4_GNUC_ATTRIBUTE((__unused__))
98 #define M4_GNUC_PRINTF(fmt, arg) \
99 M4_GNUC_ATTRIBUTE((__format__ (__printf__, fmt, arg)))
101 /* File: m4.c --- global definitions. */
104 extern int sync_output
; /* -s */
105 extern int debug_level
; /* -d */
106 extern size_t hash_table_size
; /* -H */
107 extern int no_gnu_extensions
; /* -G */
108 extern int prefix_all_builtins
; /* -P */
109 extern int max_debug_argument_length
; /* -l */
110 extern int suppress_warnings
; /* -Q */
111 extern int warning_status
; /* -E */
112 extern int nesting_limit
; /* -L */
113 #ifdef ENABLE_CHANGEWORD
114 extern const char *user_word_regexp
; /* -W */
117 /* Error handling. */
119 extern const char *program_name
;
121 void m4_error (int, int, const char *, ...) M4_GNUC_PRINTF(3, 4);
122 void m4_error_at_line (int, int, const char *, int,
123 const char *, ...) M4_GNUC_PRINTF(5, 6);
125 #define M4ERROR(Arglist) (m4_error Arglist)
126 #define M4ERROR_AT_LINE(Arglist) (m4_error_at_line Arglist)
129 void setup_stackovf_trap (char *const *, char *const *,
130 void (*handler
) (void));
133 /* File: debug.c --- debugging and tracing function. */
137 /* The value of debug_level is a bitmask of the following. */
139 /* a: show arglist in trace output */
140 #define DEBUG_TRACE_ARGS 1
141 /* e: show expansion in trace output */
142 #define DEBUG_TRACE_EXPANSION 2
143 /* q: quote args and expansion in trace output */
144 #define DEBUG_TRACE_QUOTE 4
145 /* t: trace all macros -- overrides trace{on,off} */
146 #define DEBUG_TRACE_ALL 8
147 /* l: add line numbers to trace output */
148 #define DEBUG_TRACE_LINE 16
149 /* f: add file name to trace output */
150 #define DEBUG_TRACE_FILE 32
151 /* p: trace path search of include files */
152 #define DEBUG_TRACE_PATH 64
153 /* c: show macro call before args collection */
154 #define DEBUG_TRACE_CALL 128
155 /* i: trace changes of input files */
156 #define DEBUG_TRACE_INPUT 256
157 /* x: add call id to trace output */
158 #define DEBUG_TRACE_CALLID 512
160 /* V: very verbose -- print everything */
161 #define DEBUG_TRACE_VERBOSE 1023
162 /* default flags -- equiv: aeq */
163 #define DEBUG_TRACE_DEFAULT 7
165 #define DEBUG_PRINT1(Fmt, Arg1) \
169 fprintf (debug, Fmt, Arg1); \
173 #define DEBUG_PRINT3(Fmt, Arg1, Arg2, Arg3) \
177 fprintf (debug, Fmt, Arg1, Arg2, Arg3); \
181 #define DEBUG_MESSAGE(Fmt) \
186 debug_message_prefix (); \
187 fprintf (debug, Fmt); \
188 putc ('\n', debug); \
193 #define DEBUG_MESSAGE1(Fmt, Arg1) \
198 debug_message_prefix (); \
199 fprintf (debug, Fmt, Arg1); \
200 putc ('\n', debug); \
205 #define DEBUG_MESSAGE2(Fmt, Arg1, Arg2) \
210 debug_message_prefix (); \
211 fprintf (debug, Fmt, Arg1, Arg2); \
212 putc ('\n', debug); \
217 void debug_init (void);
218 int debug_decode (const char *);
219 void debug_flush_files (void);
220 bool debug_set_output (const char *);
221 void debug_message_prefix (void);
223 void trace_prepre (const char *, int);
224 void trace_pre (const char *, int, int, token_data
**);
225 void trace_post (const char *, int, int, token_data
**, const char *);
227 /* File: input.c --- lexical definitions. */
229 /* Various different token types. */
232 TOKEN_EOF
, /* end of file */
233 TOKEN_STRING
, /* a quoted string or comment */
234 TOKEN_WORD
, /* an identifier */
238 TOKEN_SIMPLE
, /* any other single character */
239 TOKEN_MACDEF
/* a macro's definition (see "defn") */
242 /* The data for a token, a macro argument, and a macro definition. */
252 enum token_data_type type
;
258 #ifdef ENABLE_CHANGEWORD
268 #define TOKEN_DATA_TYPE(Td) ((Td)->type)
269 #define TOKEN_DATA_TEXT(Td) ((Td)->u.u_t.text)
270 #ifdef ENABLE_CHANGEWORD
271 # define TOKEN_DATA_ORIG_TEXT(Td) ((Td)->u.u_t.original_text)
273 #define TOKEN_DATA_FUNC(Td) ((Td)->u.func)
275 typedef enum token_type token_type
;
276 typedef enum token_data_type token_data_type
;
278 void input_init (void);
279 token_type
peek_token (void);
280 token_type
next_token (token_data
*);
281 void skip_line (void);
283 /* push back input */
284 void push_file (FILE *, const char *, bool);
285 void push_macro (builtin_func
*);
286 struct obstack
*push_string_init (void);
287 const char *push_string_finish (void);
288 void push_wrapup (const char *);
289 bool pop_wrapup (void);
291 /* current input file, and line */
292 extern const char *current_file
;
293 extern int current_line
;
295 /* left and right quote, begin and end comment */
296 extern STRING bcomm
, ecomm
;
297 extern STRING lquote
, rquote
;
299 #define DEF_LQUOTE "`"
300 #define DEF_RQUOTE "\'"
301 #define DEF_BCOMM "#"
302 #define DEF_ECOMM "\n"
304 void set_quotes (const char *, const char *);
305 void set_comment (const char *, const char *);
306 #ifdef ENABLE_CHANGEWORD
307 void set_word_regexp (const char *);
310 /* File: output.c --- output functions. */
311 extern int current_diversion
;
312 extern int output_current_line
;
314 void output_init (void);
315 void output_exit (void);
316 void shipout_text (struct obstack
*, const char *, int);
317 void make_diversion (int);
318 void insert_diversion (int);
319 void insert_file (FILE *);
320 void freeze_diversions (FILE *);
322 /* File symtab.c --- symbol table definitions. */
324 /* Operation modes for lookup_symbol (). */
334 /* Symbol table entry. */
341 bool blind_no_args
: 1;
343 int pending_expansions
;
349 #define SYMBOL_NEXT(S) ((S)->next)
350 #define SYMBOL_TRACED(S) ((S)->traced)
351 #define SYMBOL_SHADOWED(S) ((S)->shadowed)
352 #define SYMBOL_MACRO_ARGS(S) ((S)->macro_args)
353 #define SYMBOL_BLIND_NO_ARGS(S) ((S)->blind_no_args)
354 #define SYMBOL_DELETED(S) ((S)->deleted)
355 #define SYMBOL_PENDING_EXPANSIONS(S) ((S)->pending_expansions)
356 #define SYMBOL_NAME(S) ((S)->name)
357 #define SYMBOL_TYPE(S) (TOKEN_DATA_TYPE (&(S)->data))
358 #define SYMBOL_TEXT(S) (TOKEN_DATA_TEXT (&(S)->data))
359 #define SYMBOL_FUNC(S) (TOKEN_DATA_FUNC (&(S)->data))
361 typedef enum symbol_lookup symbol_lookup
;
362 typedef struct symbol symbol
;
363 typedef void hack_symbol (symbol
*, void *);
365 #define HASHMAX 509 /* default, overridden by -Hsize */
367 extern symbol
**symtab
;
369 void free_symbol (symbol
*sym
);
370 void symtab_init (void);
371 symbol
*lookup_symbol (const char *, symbol_lookup
);
372 void hack_all_symbols (hack_symbol
*, void *);
374 /* File: macro.c --- macro expansion. */
376 void expand_input (void);
377 void call_macro (symbol
*, int, token_data
**, struct obstack
*);
379 /* File: builtin.c --- builtins. */
384 bool gnu_extension
: 1;
385 bool groks_macro_args
: 1;
386 bool blind_if_no_args
: 1;
392 const char *unix_name
;
393 const char *gnu_name
;
397 typedef struct builtin builtin
;
398 typedef struct predefined predefined
;
400 void builtin_init (void);
401 void define_builtin (const char *, const builtin
*, symbol_lookup
);
402 void define_user_macro (const char *, const char *, symbol_lookup
);
403 void undivert_all (void);
404 void expand_user_macro (struct obstack
*, symbol
*, int, token_data
**);
405 void m4_placeholder (struct obstack
*, int, token_data
**);
407 const builtin
*find_builtin_by_addr (builtin_func
*);
408 const builtin
*find_builtin_by_name (const char *);
410 /* File: path.c --- path search for include files. */
412 void include_init (void);
413 void include_env_init (void);
414 void add_include_directory (const char *);
415 FILE *m4_path_search (const char *, char **);
417 /* File: eval.c --- expression evaluation. */
419 bool evaluate (const char *, int32_t *);
421 /* File: format.c --- printf like formatting. */
423 void format (struct obstack
*, int, token_data
**);
425 /* File: freeze.c --- frozen state files. */
427 void produce_frozen_state (const char *);
428 void reload_frozen_state (const char *);
430 /* Debugging the memory allocator. */
433 # define DMALLOC_FUNC_CHECK
434 # include <dmalloc.h>
437 /* Other debug stuff. */
440 # define DEBUG_INCL 1
441 # define DEBUG_INPUT 1
442 # define DEBUG_MACRO 1
443 # define DEBUG_OUTPUT 1
444 # define DEBUG_STKOVF 1
448 /* Convert a possibly-signed character to an unsigned character. This is
449 a bit safer than casting to unsigned char, since it catches some type
450 errors that the cast doesn't. */
452 static inline unsigned char to_uchar (char ch
) { return ch
; }
454 # define to_uchar(C) ((unsigned char) (C))