Cache regex compilation for another autoconf speedup.
[m4/ericb.git] / src / m4.c
blobdaa15a8ad2bc44ad77fc2e29d7e47fdc0e74f1af
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 #include "m4.h"
24 #include <getopt.h>
25 #include <limits.h>
26 #include <signal.h>
28 #include "version-etc.h"
30 #define AUTHORS "Rene' Seindal"
32 static void usage (int);
34 /* Enable sync output for /lib/cpp (-s). */
35 int sync_output = 0;
37 /* Debug (-d[flags]). */
38 int debug_level = 0;
40 /* Hash table size (should be a prime) (-Hsize). */
41 size_t hash_table_size = HASHMAX;
43 /* Disable GNU extensions (-G). */
44 int no_gnu_extensions = 0;
46 /* Prefix all builtin functions by `m4_'. */
47 int prefix_all_builtins = 0;
49 /* Max length of arguments in trace output (-lsize). */
50 int max_debug_argument_length = 0;
52 /* Suppress warnings about missing arguments. */
53 int suppress_warnings = 0;
55 /* If true, then warnings affect exit status. */
56 static bool fatal_warnings = false;
58 /* If not zero, then value of exit status for warning diagnostics. */
59 int warning_status = 0;
61 /* Artificial limit for expansion_level in macro.c. */
62 int nesting_limit = 1024;
64 #ifdef ENABLE_CHANGEWORD
65 /* User provided regexp for describing m4 words. */
66 const char *user_word_regexp = "";
67 #endif
69 /* The name this program was run with. */
70 const char *program_name;
72 /* Global catchall for any errors that should affect final error status, but
73 where we try to continue execution in the meantime. */
74 int retcode;
76 struct macro_definition
78 struct macro_definition *next;
79 int code; /* D, U, s, t, or '\1' */
80 const char *arg;
82 typedef struct macro_definition macro_definition;
84 /* Error handling functions. */
86 /*-----------------------.
87 | Wrapper around error. |
88 `-----------------------*/
90 void
91 m4_error (int status, int errnum, const char *format, ...)
93 va_list args;
94 va_start (args, format);
95 verror_at_line (status, errnum, current_line ? current_file : NULL,
96 current_line, format, args);
97 if (fatal_warnings && ! retcode)
98 retcode = EXIT_FAILURE;
101 /*-------------------------------.
102 | Wrapper around error_at_line. |
103 `-------------------------------*/
105 void
106 m4_error_at_line (int status, int errnum, const char *file, int line,
107 const char *format, ...)
109 va_list args;
110 va_start (args, format);
111 verror_at_line (status, errnum, line ? file : NULL, line, format, args);
112 if (fatal_warnings && ! retcode)
113 retcode = EXIT_FAILURE;
116 #ifdef USE_STACKOVF
118 /*---------------------------------------.
119 | Tell user stack overflowed and abort. |
120 `---------------------------------------*/
122 static void
123 stackovf_handler (void)
125 M4ERROR ((EXIT_FAILURE, 0,
126 "ERROR: stack overflow. (Infinite define recursion?)"));
129 #endif /* USE_STACKOV */
132 /*---------------------------------------------.
133 | Print a usage message and exit with STATUS. |
134 `---------------------------------------------*/
136 static void
137 usage (int status)
139 if (status != EXIT_SUCCESS)
140 fprintf (stderr, "Try `%s --help' for more information.\n", program_name);
141 else
143 printf ("Usage: %s [OPTION]... [FILE]...\n", program_name);
144 fputs ("\
145 Process macros in FILEs. If no FILE or if FILE is `-', standard input\n\
146 is read.\n\
147 ", stdout);
148 fputs ("\
150 Mandatory or optional arguments to long options are mandatory or optional\n\
151 for short options too.\n\
153 Operation modes:\n\
154 --help display this help and exit\n\
155 --version output version information and exit\n\
156 ", stdout);
157 printf ("\
158 -E, --fatal-warnings once: warnings become errors, twice: stop\n\
159 execution at first error\n\
160 -i, --interactive unbuffer output, ignore interrupts\n\
161 -P, --prefix-builtins force a `m4_' prefix to all builtins\n\
162 -Q, --quiet, --silent suppress some warnings for builtins\n\
163 --warn-macro-sequence[=REGEXP]\n\
164 warn if macro definition matches REGEXP,\n\
165 default %s\n\
166 ", DEFAULT_MACRO_SEQUENCE);
167 #ifdef ENABLE_CHANGEWORD
168 fputs ("\
169 -W, --word-regexp=REGEXP use REGEXP for macro name syntax\n\
170 ", stdout);
171 #endif
172 fputs ("\
174 Preprocessor features:\n\
175 -D, --define=NAME[=VALUE] define NAME as having VALUE, or empty\n\
176 -I, --include=DIRECTORY append DIRECTORY to include path\n\
177 -s, --synclines generate `#line NUM \"FILE\"' lines\n\
178 -U, --undefine=NAME undefine NAME\n\
179 ", stdout);
180 fputs ("\
182 Limits control:\n\
183 -G, --traditional suppress all GNU extensions\n\
184 -H, --hashsize=PRIME set symbol lookup hash table size [509]\n\
185 -L, --nesting-limit=NUMBER change artificial nesting limit [1024]\n\
186 ", stdout);
187 fputs ("\
189 Frozen state files:\n\
190 -F, --freeze-state=FILE produce a frozen state on FILE at end\n\
191 -R, --reload-state=FILE reload a frozen state from FILE at start\n\
192 ", stdout);
193 fputs ("\
195 Debugging:\n\
196 -d, --debug[=FLAGS] set debug level (no FLAGS implies `aeq')\n\
197 --debugfile=FILE redirect debug and trace output\n\
198 -l, --arglength=NUM restrict macro tracing size\n\
199 -t, --trace=NAME trace NAME when it is defined\n\
200 ", stdout);
201 fputs ("\
203 FLAGS is any of:\n\
204 a show actual arguments\n\
205 c show before collect, after collect and after call\n\
206 e show expansion\n\
207 f say current input file name\n\
208 i show changes in input files\n\
209 l say current input line number\n\
210 p show results of path searches\n\
211 q quote values as necessary, with a or e flag\n\
212 t trace for all macro calls, not only traceon'ed\n\
213 x add a unique macro call id, useful with c flag\n\
214 V shorthand for all of the above flags\n\
215 ", stdout);
216 fputs ("\
218 If defined, the environment variable `M4PATH' is a colon-separated list\n\
219 of directories included after any specified by `-I'.\n\
220 ", stdout);
221 fputs ("\
223 Exit status is 0 for success, 1 for failure, 63 for frozen file version\n\
224 mismatch, or whatever value was passed to the m4exit macro.\n\
225 ", stdout);
226 printf ("\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
228 exit (status);
231 /*--------------------------------------.
232 | Decode options and launch execution. |
233 `--------------------------------------*/
235 /* For long options that have no equivalent short option, use a
236 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
237 enum
239 DEBUGFILE_OPTION = CHAR_MAX + 1, /* no short opt */
240 DIVERSIONS_OPTION, /* not quite -N, because of message */
241 WARN_MACRO_SEQUENCE_OPTION, /* no short opt */
243 HELP_OPTION, /* no short opt */
244 VERSION_OPTION /* no short opt */
247 static const struct option long_options[] =
249 {"arglength", required_argument, NULL, 'l'},
250 {"debug", optional_argument, NULL, 'd'},
251 {"define", required_argument, NULL, 'D'},
252 {"error-output", required_argument, NULL, 'o'}, /* FIXME: deprecate in 2.0 */
253 {"fatal-warnings", no_argument, NULL, 'E'},
254 {"freeze-state", required_argument, NULL, 'F'},
255 {"hashsize", required_argument, NULL, 'H'},
256 {"include", required_argument, NULL, 'I'},
257 {"interactive", no_argument, NULL, 'i'},
258 {"nesting-limit", required_argument, NULL, 'L'},
259 {"prefix-builtins", no_argument, NULL, 'P'},
260 {"quiet", no_argument, NULL, 'Q'},
261 {"reload-state", required_argument, NULL, 'R'},
262 {"silent", no_argument, NULL, 'Q'},
263 {"synclines", no_argument, NULL, 's'},
264 {"trace", required_argument, NULL, 't'},
265 {"traditional", no_argument, NULL, 'G'},
266 {"undefine", required_argument, NULL, 'U'},
267 {"word-regexp", required_argument, NULL, 'W'},
269 {"debugfile", required_argument, NULL, DEBUGFILE_OPTION},
270 {"diversions", required_argument, NULL, DIVERSIONS_OPTION},
271 {"warn-macro-sequence", optional_argument, NULL, WARN_MACRO_SEQUENCE_OPTION},
273 {"help", no_argument, NULL, HELP_OPTION},
274 {"version", no_argument, NULL, VERSION_OPTION},
276 { NULL, 0, NULL, 0 },
279 /* Process a command line file NAME, and return true only if it was
280 stdin. */
281 static void
282 process_file (const char *name)
284 if (strcmp (name, "-") == 0)
286 /* If stdin is a terminal, we want to allow 'm4 - file -'
287 to read input from stdin twice, like GNU cat. Besides,
288 there is no point closing stdin before wrapped text, to
289 minimize bugs in syscmd called from wrapped text. */
290 push_file (stdin, "stdin", false);
292 else
294 char *full_name;
295 FILE *fp = m4_path_search (name, &full_name);
296 if (fp == NULL)
298 error (0, errno, "%s", name);
299 /* Set the status to EXIT_FAILURE, even though we
300 continue to process files after a missing file. */
301 retcode = EXIT_FAILURE;
302 return;
304 push_file (fp, full_name, true);
305 free (full_name);
307 expand_input ();
310 /* POSIX requires only -D, -U, and -s; and says that the first two
311 must be recognized when interspersed with file names. Traditional
312 behavior also handles -s between files. Starting OPTSTRING with
313 '-' forces getopt_long to hand back file names as arguments to opt
314 '\1', rather than reordering the command line. */
315 #ifdef ENABLE_CHANGEWORD
316 #define OPTSTRING "-B:D:EF:GH:I:L:N:PQR:S:T:U:W:d::eil:o:st:"
317 #else
318 #define OPTSTRING "-B:D:EF:GH:I:L:N:PQR:S:T:U:d::eil:o:st:"
319 #endif
322 main (int argc, char *const *argv, char *const *envp)
324 macro_definition *head; /* head of deferred argument list */
325 macro_definition *tail;
326 macro_definition *defn;
327 int optchar; /* option character */
329 macro_definition *defines;
330 bool interactive = false;
331 bool seen_file = false;
332 const char *debugfile = NULL;
333 const char *frozen_file_to_read = NULL;
334 const char *frozen_file_to_write = NULL;
335 const char *macro_sequence = "";
337 program_name = argv[0];
338 retcode = EXIT_SUCCESS;
339 atexit (close_stdin);
341 include_init ();
342 debug_init ();
343 #ifdef USE_STACKOVF
344 setup_stackovf_trap (argv, envp, stackovf_handler);
345 #endif
347 /* First, we decode the arguments, to size up tables and stuff. */
349 head = tail = NULL;
351 while ((optchar = getopt_long (argc, (char **) argv, OPTSTRING,
352 long_options, NULL)) != -1)
353 switch (optchar)
355 default:
356 usage (EXIT_FAILURE);
358 case 'B':
359 case 'S':
360 case 'T':
361 /* Compatibility junk: options that other implementations
362 support, but which we ignore as no-ops and don't list in
363 --help. */
364 error (0, 0, "Warning: `m4 -%c' may be removed in a future release",
365 optchar);
366 break;
368 case 'N':
369 case DIVERSIONS_OPTION:
370 /* -N became an obsolete no-op in 1.4.x. */
371 error (0, 0, "Warning: `m4 %s' is deprecated",
372 optchar == 'N' ? "-N" : "--diversions");
374 case 'D':
375 case 'U':
376 case 's':
377 case 't':
378 case '\1':
379 /* Arguments that cannot be handled until later are accumulated. */
381 defn = (macro_definition *) xmalloc (sizeof (macro_definition));
382 defn->code = optchar;
383 defn->arg = optarg;
384 defn->next = NULL;
386 if (head == NULL)
387 head = defn;
388 else
389 tail->next = defn;
390 tail = defn;
392 break;
394 case 'E':
395 if (! fatal_warnings)
396 fatal_warnings = true;
397 else
398 warning_status = EXIT_FAILURE;
399 break;
401 case 'F':
402 frozen_file_to_write = optarg;
403 break;
405 case 'G':
406 no_gnu_extensions = 1;
407 break;
409 case 'H':
410 hash_table_size = atol (optarg);
411 if (hash_table_size == 0)
412 hash_table_size = HASHMAX;
413 break;
415 case 'I':
416 add_include_directory (optarg);
417 break;
419 case 'L':
420 nesting_limit = atoi (optarg);
421 break;
423 case 'P':
424 prefix_all_builtins = 1;
425 break;
427 case 'Q':
428 suppress_warnings = 1;
429 break;
431 case 'R':
432 frozen_file_to_read = optarg;
433 break;
435 #ifdef ENABLE_CHANGEWORD
436 case 'W':
437 user_word_regexp = optarg;
438 break;
439 #endif
441 case 'd':
442 debug_level = debug_decode (optarg);
443 if (debug_level < 0)
445 error (0, 0, "bad debug flags: `%s'", optarg);
446 debug_level = 0;
448 break;
450 case 'e':
451 error (0, 0, "Warning: `m4 -e' is deprecated, use `-i' instead");
452 /* fall through */
453 case 'i':
454 interactive = true;
455 break;
457 case 'l':
458 max_debug_argument_length = atoi (optarg);
459 if (max_debug_argument_length <= 0)
460 max_debug_argument_length = 0;
461 break;
463 case 'o':
464 /* -o/--error-output are deprecated synonyms of --debugfile,
465 but don't issue a deprecation warning until autoconf 2.61
466 or later is more widely established, as such a warning
467 would interfere with all earlier versions of autoconf. */
468 case DEBUGFILE_OPTION:
469 /* Don't call debug_set_output here, as it has side effects. */
470 debugfile = optarg;
471 break;
473 case WARN_MACRO_SEQUENCE_OPTION:
474 /* Don't call set_macro_sequence here, as it can exit.
475 --warn-macro-sequence sets optarg to NULL (which uses the
476 default regexp); --warn-macro-sequence= sets optarg to ""
477 (which disables these warnings). */
478 macro_sequence = optarg;
479 break;
481 case VERSION_OPTION:
482 version_etc (stdout, PACKAGE, PACKAGE_NAME, VERSION, AUTHORS, NULL);
483 exit (EXIT_SUCCESS);
484 break;
486 case HELP_OPTION:
487 usage (EXIT_SUCCESS);
488 break;
491 defines = head;
493 /* Do the basic initializations. */
494 if (debugfile && !debug_set_output (debugfile))
495 M4ERROR ((0, errno, "cannot set debug file `%s'", debugfile));
497 input_init ();
498 output_init ();
499 symtab_init ();
500 set_macro_sequence (macro_sequence);
501 include_env_init ();
503 if (frozen_file_to_read)
504 reload_frozen_state (frozen_file_to_read);
505 else
506 builtin_init ();
508 /* Interactive mode means unbuffered output, and interrupts ignored. */
510 if (interactive)
512 signal (SIGINT, SIG_IGN);
513 setbuf (stdout, (char *) NULL);
516 /* Handle deferred command line macro definitions. Must come after
517 initialization of the symbol table. */
519 while (defines != NULL)
521 macro_definition *next;
522 symbol *sym;
524 switch (defines->code)
526 case 'D':
528 /* defines->arg is read-only, so we need a copy. */
529 char *macro_name = xstrdup (defines->arg);
530 char *macro_value = strchr (macro_name, '=');
531 if (macro_value)
532 *macro_value++ = '\0';
533 define_user_macro (macro_name, macro_value, SYMBOL_INSERT);
534 free (macro_name);
536 break;
538 case 'U':
539 lookup_symbol (defines->arg, SYMBOL_DELETE);
540 break;
542 case 't':
543 sym = lookup_symbol (defines->arg, SYMBOL_INSERT);
544 SYMBOL_TRACED (sym) = true;
545 break;
547 case 's':
548 sync_output = 1;
549 break;
551 case '\1':
552 seen_file = true;
553 process_file (defines->arg);
554 break;
556 default:
557 M4ERROR ((0, 0, "INTERNAL ERROR: bad code in deferred arguments"));
558 abort ();
561 next = defines->next;
562 free (defines);
563 defines = next;
566 /* Handle remaining input files. Each file is pushed on the input,
567 and the input read. Wrapup text is handled separately later. */
569 if (optind == argc && !seen_file)
570 process_file ("-");
571 else
572 for (; optind < argc; optind++)
573 process_file (argv[optind]);
575 /* Now handle wrapup text. */
577 while (pop_wrapup ())
578 expand_input ();
580 /* Change debug stream back to stderr, to force flushing the debug
581 stream and detect any errors it might have encountered. The
582 three standard streams are closed by close_stdin. */
583 debug_set_output (NULL);
585 if (frozen_file_to_write)
586 produce_frozen_state (frozen_file_to_write);
587 else
589 make_diversion (0);
590 undivert_all ();
592 output_exit ();
593 free_regex ();
594 exit (retcode);