1 | /* GNU m4 -- A simple macro processor
|
---|
2 |
|
---|
3 | Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 2004, 2005, 2006 Free
|
---|
4 | Software Foundation, Inc.
|
---|
5 |
|
---|
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.
|
---|
10 |
|
---|
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.
|
---|
15 |
|
---|
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
|
---|
19 | 02110-1301 USA
|
---|
20 | */
|
---|
21 |
|
---|
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). */
|
---|
25 |
|
---|
26 | #include <config.h>
|
---|
27 |
|
---|
28 | #include <ctype.h>
|
---|
29 | #include <errno.h>
|
---|
30 | #include <stdbool.h>
|
---|
31 | #include <string.h>
|
---|
32 | #include <sys/types.h>
|
---|
33 |
|
---|
34 | #include "binary-io.h"
|
---|
35 | #include "clean-temp.h"
|
---|
36 | #include "cloexec.h"
|
---|
37 | #include "close-stream.h"
|
---|
38 | #include "closeout.h"
|
---|
39 | #include "error.h"
|
---|
40 | #include "exit.h"
|
---|
41 | #include "exitfail.h"
|
---|
42 | #include "obstack.h"
|
---|
43 | #include "stdio--.h"
|
---|
44 | #include "stdlib--.h"
|
---|
45 | #include "unistd--.h"
|
---|
46 | #include "verror.h"
|
---|
47 | #include "xalloc.h"
|
---|
48 | #include "xvasprintf.h"
|
---|
49 |
|
---|
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__
|
---|
55 | # define UNIX 1
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | /* Canonicalize Windows recognition macros. */
|
---|
59 | #if (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__
|
---|
60 | # define W32_NATIVE 1
|
---|
61 | #endif
|
---|
62 |
|
---|
63 | /* Canonicalize OS/2 recognition macro. */
|
---|
64 | #ifdef __EMX__
|
---|
65 | # define OS2 1
|
---|
66 | #endif
|
---|
67 |
|
---|
68 | /* Used for version mismatch, when -R detects a frozen file it can't parse. */
|
---|
69 | #define EXIT_MISMATCH 63
|
---|
70 | |
---|
71 |
|
---|
72 | /* Various declarations. */
|
---|
73 |
|
---|
74 | struct string
|
---|
75 | {
|
---|
76 | char *string; /* characters of the string */
|
---|
77 | size_t length; /* length of the string */
|
---|
78 | };
|
---|
79 | typedef struct string STRING;
|
---|
80 |
|
---|
81 | /* Memory allocation. */
|
---|
82 | #define obstack_chunk_alloc xmalloc
|
---|
83 | #define obstack_chunk_free free
|
---|
84 |
|
---|
85 | /* Those must come first. */
|
---|
86 | typedef struct token_data token_data;
|
---|
87 | typedef void builtin_func (struct obstack *, int, token_data **);
|
---|
88 |
|
---|
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)
|
---|
93 | #else
|
---|
94 | # define M4_GNUC_ATTRIBUTE(args)
|
---|
95 | #endif /* __GNUC__ */
|
---|
96 |
|
---|
97 | #define M4_GNUC_UNUSED M4_GNUC_ATTRIBUTE((__unused__))
|
---|
98 | #define M4_GNUC_PRINTF(fmt, arg) \
|
---|
99 | M4_GNUC_ATTRIBUTE((__format__ (__printf__, fmt, arg)))
|
---|
100 | |
---|
101 |
|
---|
102 | /* File: m4.c --- global definitions. */
|
---|
103 |
|
---|
104 | /* Option flags. */
|
---|
105 | extern int sync_output; /* -s */
|
---|
106 | extern int debug_level; /* -d */
|
---|
107 | extern size_t hash_table_size; /* -H */
|
---|
108 | extern int no_gnu_extensions; /* -G */
|
---|
109 | extern int prefix_all_builtins; /* -P */
|
---|
110 | extern int max_debug_argument_length; /* -l */
|
---|
111 | extern int suppress_warnings; /* -Q */
|
---|
112 | extern int warning_status; /* -E */
|
---|
113 | extern int nesting_limit; /* -L */
|
---|
114 | #ifdef ENABLE_CHANGEWORD
|
---|
115 | extern const char *user_word_regexp; /* -W */
|
---|
116 | #endif
|
---|
117 |
|
---|
118 | /* Error handling. */
|
---|
119 | extern int retcode;
|
---|
120 | extern const char *program_name;
|
---|
121 |
|
---|
122 | void m4_error (int, int, const char *, ...) M4_GNUC_PRINTF(3, 4);
|
---|
123 | void m4_error_at_line (int, int, const char *, int,
|
---|
124 | const char *, ...) M4_GNUC_PRINTF(5, 6);
|
---|
125 |
|
---|
126 | #define M4ERROR(Arglist) (m4_error Arglist)
|
---|
127 | #define M4ERROR_AT_LINE(Arglist) (m4_error_at_line Arglist)
|
---|
128 |
|
---|
129 | #ifdef USE_STACKOVF
|
---|
130 | void setup_stackovf_trap (char *const *, char *const *,
|
---|
131 | void (*handler) (void));
|
---|
132 | #endif
|
---|
133 | |
---|
134 |
|
---|
135 | /* File: debug.c --- debugging and tracing function. */
|
---|
136 |
|
---|
137 | extern FILE *debug;
|
---|
138 |
|
---|
139 | /* The value of debug_level is a bitmask of the following. */
|
---|
140 |
|
---|
141 | /* a: show arglist in trace output */
|
---|
142 | #define DEBUG_TRACE_ARGS 1
|
---|
143 | /* e: show expansion in trace output */
|
---|
144 | #define DEBUG_TRACE_EXPANSION 2
|
---|
145 | /* q: quote args and expansion in trace output */
|
---|
146 | #define DEBUG_TRACE_QUOTE 4
|
---|
147 | /* t: trace all macros -- overrides trace{on,off} */
|
---|
148 | #define DEBUG_TRACE_ALL 8
|
---|
149 | /* l: add line numbers to trace output */
|
---|
150 | #define DEBUG_TRACE_LINE 16
|
---|
151 | /* f: add file name to trace output */
|
---|
152 | #define DEBUG_TRACE_FILE 32
|
---|
153 | /* p: trace path search of include files */
|
---|
154 | #define DEBUG_TRACE_PATH 64
|
---|
155 | /* c: show macro call before args collection */
|
---|
156 | #define DEBUG_TRACE_CALL 128
|
---|
157 | /* i: trace changes of input files */
|
---|
158 | #define DEBUG_TRACE_INPUT 256
|
---|
159 | /* x: add call id to trace output */
|
---|
160 | #define DEBUG_TRACE_CALLID 512
|
---|
161 |
|
---|
162 | /* V: very verbose -- print everything */
|
---|
163 | #define DEBUG_TRACE_VERBOSE 1023
|
---|
164 | /* default flags -- equiv: aeq */
|
---|
165 | #define DEBUG_TRACE_DEFAULT 7
|
---|
166 |
|
---|
167 | #define DEBUG_PRINT1(Fmt, Arg1) \
|
---|
168 | do \
|
---|
169 | { \
|
---|
170 | if (debug != NULL) \
|
---|
171 | fprintf (debug, Fmt, Arg1); \
|
---|
172 | } \
|
---|
173 | while (0)
|
---|
174 |
|
---|
175 | #define DEBUG_PRINT3(Fmt, Arg1, Arg2, Arg3) \
|
---|
176 | do \
|
---|
177 | { \
|
---|
178 | if (debug != NULL) \
|
---|
179 | fprintf (debug, Fmt, Arg1, Arg2, Arg3); \
|
---|
180 | } \
|
---|
181 | while (0)
|
---|
182 |
|
---|
183 | #define DEBUG_MESSAGE(Fmt) \
|
---|
184 | do \
|
---|
185 | { \
|
---|
186 | if (debug != NULL) \
|
---|
187 | { \
|
---|
188 | debug_message_prefix (); \
|
---|
189 | fprintf (debug, Fmt); \
|
---|
190 | putc ('\n', debug); \
|
---|
191 | } \
|
---|
192 | } \
|
---|
193 | while (0)
|
---|
194 |
|
---|
195 | #define DEBUG_MESSAGE1(Fmt, Arg1) \
|
---|
196 | do \
|
---|
197 | { \
|
---|
198 | if (debug != NULL) \
|
---|
199 | { \
|
---|
200 | debug_message_prefix (); \
|
---|
201 | fprintf (debug, Fmt, Arg1); \
|
---|
202 | putc ('\n', debug); \
|
---|
203 | } \
|
---|
204 | } \
|
---|
205 | while (0)
|
---|
206 |
|
---|
207 | #define DEBUG_MESSAGE2(Fmt, Arg1, Arg2) \
|
---|
208 | do \
|
---|
209 | { \
|
---|
210 | if (debug != NULL) \
|
---|
211 | { \
|
---|
212 | debug_message_prefix (); \
|
---|
213 | fprintf (debug, Fmt, Arg1, Arg2); \
|
---|
214 | putc ('\n', debug); \
|
---|
215 | } \
|
---|
216 | } \
|
---|
217 | while (0)
|
---|
218 |
|
---|
219 | void debug_init (void);
|
---|
220 | int debug_decode (const char *);
|
---|
221 | void debug_flush_files (void);
|
---|
222 | bool debug_set_output (const char *);
|
---|
223 | void debug_message_prefix (void);
|
---|
224 |
|
---|
225 | void trace_prepre (const char *, int);
|
---|
226 | void trace_pre (const char *, int, int, token_data **);
|
---|
227 | void trace_post (const char *, int, int, token_data **, const char *);
|
---|
228 | |
---|
229 |
|
---|
230 | /* File: input.c --- lexical definitions. */
|
---|
231 |
|
---|
232 | /* Various different token types. */
|
---|
233 | enum token_type
|
---|
234 | {
|
---|
235 | TOKEN_EOF, /* end of file */
|
---|
236 | TOKEN_STRING, /* a quoted string or comment */
|
---|
237 | TOKEN_WORD, /* an identifier */
|
---|
238 | TOKEN_OPEN, /* ( */
|
---|
239 | TOKEN_COMMA, /* , */
|
---|
240 | TOKEN_CLOSE, /* ) */
|
---|
241 | TOKEN_SIMPLE, /* any other single character */
|
---|
242 | TOKEN_MACDEF /* a macro's definition (see "defn") */
|
---|
243 | };
|
---|
244 |
|
---|
245 | /* The data for a token, a macro argument, and a macro definition. */
|
---|
246 | enum token_data_type
|
---|
247 | {
|
---|
248 | TOKEN_VOID,
|
---|
249 | TOKEN_TEXT,
|
---|
250 | TOKEN_FUNC
|
---|
251 | };
|
---|
252 |
|
---|
253 | struct token_data
|
---|
254 | {
|
---|
255 | enum token_data_type type;
|
---|
256 | union
|
---|
257 | {
|
---|
258 | struct
|
---|
259 | {
|
---|
260 | char *text;
|
---|
261 | #ifdef ENABLE_CHANGEWORD
|
---|
262 | char *original_text;
|
---|
263 | #endif
|
---|
264 | }
|
---|
265 | u_t;
|
---|
266 | builtin_func *func;
|
---|
267 | }
|
---|
268 | u;
|
---|
269 | };
|
---|
270 |
|
---|
271 | #define TOKEN_DATA_TYPE(Td) ((Td)->type)
|
---|
272 | #define TOKEN_DATA_TEXT(Td) ((Td)->u.u_t.text)
|
---|
273 | #ifdef ENABLE_CHANGEWORD
|
---|
274 | # define TOKEN_DATA_ORIG_TEXT(Td) ((Td)->u.u_t.original_text)
|
---|
275 | #endif
|
---|
276 | #define TOKEN_DATA_FUNC(Td) ((Td)->u.func)
|
---|
277 |
|
---|
278 | typedef enum token_type token_type;
|
---|
279 | typedef enum token_data_type token_data_type;
|
---|
280 |
|
---|
281 | void input_init (void);
|
---|
282 | token_type peek_token (void);
|
---|
283 | token_type next_token (token_data *);
|
---|
284 | void skip_line (void);
|
---|
285 |
|
---|
286 | /* push back input */
|
---|
287 | void push_file (FILE *, const char *, bool);
|
---|
288 | void push_macro (builtin_func *);
|
---|
289 | struct obstack *push_string_init (void);
|
---|
290 | const char *push_string_finish (void);
|
---|
291 | void push_wrapup (const char *);
|
---|
292 | bool pop_wrapup (void);
|
---|
293 |
|
---|
294 | /* current input file, and line */
|
---|
295 | extern const char *current_file;
|
---|
296 | extern int current_line;
|
---|
297 |
|
---|
298 | /* left and right quote, begin and end comment */
|
---|
299 | extern STRING bcomm, ecomm;
|
---|
300 | extern STRING lquote, rquote;
|
---|
301 |
|
---|
302 | #define DEF_LQUOTE "`"
|
---|
303 | #define DEF_RQUOTE "\'"
|
---|
304 | #define DEF_BCOMM "#"
|
---|
305 | #define DEF_ECOMM "\n"
|
---|
306 |
|
---|
307 | void set_quotes (const char *, const char *);
|
---|
308 | void set_comment (const char *, const char *);
|
---|
309 | #ifdef ENABLE_CHANGEWORD
|
---|
310 | void set_word_regexp (const char *);
|
---|
311 | #endif
|
---|
312 | |
---|
313 |
|
---|
314 | /* File: output.c --- output functions. */
|
---|
315 | extern int current_diversion;
|
---|
316 | extern int output_current_line;
|
---|
317 |
|
---|
318 | void output_init (void);
|
---|
319 | void output_exit (void);
|
---|
320 | void shipout_text (struct obstack *, const char *, int);
|
---|
321 | void make_diversion (int);
|
---|
322 | void insert_diversion (int);
|
---|
323 | void insert_file (FILE *);
|
---|
324 | void freeze_diversions (FILE *);
|
---|
325 | |
---|
326 |
|
---|
327 | /* File symtab.c --- symbol table definitions. */
|
---|
328 |
|
---|
329 | /* Operation modes for lookup_symbol (). */
|
---|
330 | enum symbol_lookup
|
---|
331 | {
|
---|
332 | SYMBOL_LOOKUP,
|
---|
333 | SYMBOL_INSERT,
|
---|
334 | SYMBOL_DELETE,
|
---|
335 | SYMBOL_PUSHDEF,
|
---|
336 | SYMBOL_POPDEF
|
---|
337 | };
|
---|
338 |
|
---|
339 | /* Symbol table entry. */
|
---|
340 | struct symbol
|
---|
341 | {
|
---|
342 | struct symbol *next;
|
---|
343 | bool traced : 1;
|
---|
344 | bool shadowed : 1;
|
---|
345 | bool macro_args : 1;
|
---|
346 | bool blind_no_args : 1;
|
---|
347 | bool deleted : 1;
|
---|
348 | int pending_expansions;
|
---|
349 |
|
---|
350 | char *name;
|
---|
351 | token_data data;
|
---|
352 | };
|
---|
353 |
|
---|
354 | #define SYMBOL_NEXT(S) ((S)->next)
|
---|
355 | #define SYMBOL_TRACED(S) ((S)->traced)
|
---|
356 | #define SYMBOL_SHADOWED(S) ((S)->shadowed)
|
---|
357 | #define SYMBOL_MACRO_ARGS(S) ((S)->macro_args)
|
---|
358 | #define SYMBOL_BLIND_NO_ARGS(S) ((S)->blind_no_args)
|
---|
359 | #define SYMBOL_DELETED(S) ((S)->deleted)
|
---|
360 | #define SYMBOL_PENDING_EXPANSIONS(S) ((S)->pending_expansions)
|
---|
361 | #define SYMBOL_NAME(S) ((S)->name)
|
---|
362 | #define SYMBOL_TYPE(S) (TOKEN_DATA_TYPE (&(S)->data))
|
---|
363 | #define SYMBOL_TEXT(S) (TOKEN_DATA_TEXT (&(S)->data))
|
---|
364 | #define SYMBOL_FUNC(S) (TOKEN_DATA_FUNC (&(S)->data))
|
---|
365 |
|
---|
366 | typedef enum symbol_lookup symbol_lookup;
|
---|
367 | typedef struct symbol symbol;
|
---|
368 | typedef void hack_symbol (symbol *, void *);
|
---|
369 |
|
---|
370 | #define HASHMAX 509 /* default, overridden by -Hsize */
|
---|
371 |
|
---|
372 | extern symbol **symtab;
|
---|
373 |
|
---|
374 | void free_symbol (symbol *sym);
|
---|
375 | void symtab_init (void);
|
---|
376 | symbol *lookup_symbol (const char *, symbol_lookup);
|
---|
377 | void hack_all_symbols (hack_symbol *, void *);
|
---|
378 | |
---|
379 |
|
---|
380 | /* File: macro.c --- macro expansion. */
|
---|
381 |
|
---|
382 | void expand_input (void);
|
---|
383 | void call_macro (symbol *, int, token_data **, struct obstack *);
|
---|
384 | |
---|
385 |
|
---|
386 | /* File: builtin.c --- builtins. */
|
---|
387 |
|
---|
388 | struct builtin
|
---|
389 | {
|
---|
390 | const char *name;
|
---|
391 | bool gnu_extension : 1;
|
---|
392 | bool groks_macro_args : 1;
|
---|
393 | bool blind_if_no_args : 1;
|
---|
394 | builtin_func *func;
|
---|
395 | };
|
---|
396 |
|
---|
397 | struct predefined
|
---|
398 | {
|
---|
399 | const char *unix_name;
|
---|
400 | const char *gnu_name;
|
---|
401 | const char *func;
|
---|
402 | };
|
---|
403 |
|
---|
404 | typedef struct builtin builtin;
|
---|
405 | typedef struct predefined predefined;
|
---|
406 |
|
---|
407 | void builtin_init (void);
|
---|
408 | void define_builtin (const char *, const builtin *, symbol_lookup);
|
---|
409 | void define_user_macro (const char *, const char *, symbol_lookup);
|
---|
410 | void undivert_all (void);
|
---|
411 | void expand_user_macro (struct obstack *, symbol *, int, token_data **);
|
---|
412 | void m4_placeholder (struct obstack *, int, token_data **);
|
---|
413 |
|
---|
414 | const builtin *find_builtin_by_addr (builtin_func *);
|
---|
415 | const builtin *find_builtin_by_name (const char *);
|
---|
416 | |
---|
417 |
|
---|
418 | /* File: path.c --- path search for include files. */
|
---|
419 |
|
---|
420 | void include_init (void);
|
---|
421 | void include_env_init (void);
|
---|
422 | void add_include_directory (const char *);
|
---|
423 | FILE *m4_path_search (const char *, char **);
|
---|
424 | |
---|
425 |
|
---|
426 | /* File: eval.c --- expression evaluation. */
|
---|
427 |
|
---|
428 | /* eval_t and unsigned_eval_t should be at least 32 bits. */
|
---|
429 | typedef int eval_t;
|
---|
430 | typedef unsigned int unsigned_eval_t;
|
---|
431 |
|
---|
432 | bool evaluate (const char *, eval_t *);
|
---|
433 | |
---|
434 |
|
---|
435 | /* File: format.c --- printf like formatting. */
|
---|
436 |
|
---|
437 | void format (struct obstack *, int, token_data **);
|
---|
438 | |
---|
439 |
|
---|
440 | /* File: freeze.c --- frozen state files. */
|
---|
441 |
|
---|
442 | void produce_frozen_state (const char *);
|
---|
443 | void reload_frozen_state (const char *);
|
---|
444 | |
---|
445 |
|
---|
446 | /* Debugging the memory allocator. */
|
---|
447 |
|
---|
448 | #ifdef WITH_DMALLOC
|
---|
449 | # define DMALLOC_FUNC_CHECK
|
---|
450 | # include <dmalloc.h>
|
---|
451 | #endif
|
---|
452 |
|
---|
453 | /* Other debug stuff. */
|
---|
454 |
|
---|
455 | #ifdef DEBUG
|
---|
456 | # define DEBUG_INCL 1
|
---|
457 | # define DEBUG_INPUT 1
|
---|
458 | # define DEBUG_MACRO 1
|
---|
459 | # define DEBUG_OUTPUT 1
|
---|
460 | # define DEBUG_STKOVF 1
|
---|
461 | # define DEBUG_SYM 1
|
---|
462 | #endif
|
---|
463 |
|
---|
464 | /* Convert a possibly-signed character to an unsigned character. This is
|
---|
465 | a bit safer than casting to unsigned char, since it catches some type
|
---|
466 | errors that the cast doesn't. */
|
---|
467 | #if HAVE_INLINE
|
---|
468 | static inline unsigned char to_uchar (char ch) { return ch; }
|
---|
469 | #else
|
---|
470 | # define to_uchar(C) ((unsigned char) (C))
|
---|
471 | #endif
|
---|