Use build-aux directory.
[m4/ericb.git] / src / m4.c
blob401ce4b7ec90265cbf357379da53a1768f051df2
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 xfprintf (stderr, "Try `%s --help' for more information.\n", program_name);
141 else
143 xprintf ("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 xprintf ("\
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 xprintf ("\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
321 #ifdef DEBUG_REGEX
322 FILE *trace_file;
323 #endif /* DEBUG_REGEX */
326 main (int argc, char *const *argv, char *const *envp)
328 macro_definition *head; /* head of deferred argument list */
329 macro_definition *tail;
330 macro_definition *defn;
331 int optchar; /* option character */
333 macro_definition *defines;
334 bool interactive = false;
335 bool seen_file = false;
336 const char *debugfile = NULL;
337 const char *frozen_file_to_read = NULL;
338 const char *frozen_file_to_write = NULL;
339 const char *macro_sequence = "";
341 program_name = argv[0];
342 retcode = EXIT_SUCCESS;
343 atexit (close_stdin);
345 #ifdef DEBUG_REGEX
347 const char *name = getenv ("M4_TRACE_FILE");
348 if (name)
349 trace_file = fopen (name, "a");
350 if (trace_file)
351 fputs ("m4:\n", trace_file);
353 #endif /* DEBUG_REGEX */
355 include_init ();
356 debug_init ();
357 #ifdef USE_STACKOVF
358 setup_stackovf_trap (argv, envp, stackovf_handler);
359 #endif
361 /* First, we decode the arguments, to size up tables and stuff. */
363 head = tail = NULL;
365 while ((optchar = getopt_long (argc, (char **) argv, OPTSTRING,
366 long_options, NULL)) != -1)
367 switch (optchar)
369 default:
370 usage (EXIT_FAILURE);
372 case 'B':
373 case 'S':
374 case 'T':
375 /* Compatibility junk: options that other implementations
376 support, but which we ignore as no-ops and don't list in
377 --help. */
378 error (0, 0, "Warning: `m4 -%c' may be removed in a future release",
379 optchar);
380 break;
382 case 'N':
383 case DIVERSIONS_OPTION:
384 /* -N became an obsolete no-op in 1.4.x. */
385 error (0, 0, "Warning: `m4 %s' is deprecated",
386 optchar == 'N' ? "-N" : "--diversions");
388 case 'D':
389 case 'U':
390 case 's':
391 case 't':
392 case '\1':
393 /* Arguments that cannot be handled until later are accumulated. */
395 defn = (macro_definition *) xmalloc (sizeof (macro_definition));
396 defn->code = optchar;
397 defn->arg = optarg;
398 defn->next = NULL;
400 if (head == NULL)
401 head = defn;
402 else
403 tail->next = defn;
404 tail = defn;
406 break;
408 case 'E':
409 if (! fatal_warnings)
410 fatal_warnings = true;
411 else
412 warning_status = EXIT_FAILURE;
413 break;
415 case 'F':
416 frozen_file_to_write = optarg;
417 break;
419 case 'G':
420 no_gnu_extensions = 1;
421 break;
423 case 'H':
424 hash_table_size = atol (optarg);
425 if (hash_table_size == 0)
426 hash_table_size = HASHMAX;
427 break;
429 case 'I':
430 add_include_directory (optarg);
431 break;
433 case 'L':
434 nesting_limit = atoi (optarg);
435 break;
437 case 'P':
438 prefix_all_builtins = 1;
439 break;
441 case 'Q':
442 suppress_warnings = 1;
443 break;
445 case 'R':
446 frozen_file_to_read = optarg;
447 break;
449 #ifdef ENABLE_CHANGEWORD
450 case 'W':
451 user_word_regexp = optarg;
452 break;
453 #endif
455 case 'd':
456 debug_level = debug_decode (optarg);
457 if (debug_level < 0)
459 error (0, 0, "bad debug flags: `%s'", optarg);
460 debug_level = 0;
462 break;
464 case 'e':
465 error (0, 0, "Warning: `m4 -e' is deprecated, use `-i' instead");
466 /* fall through */
467 case 'i':
468 interactive = true;
469 break;
471 case 'l':
472 max_debug_argument_length = atoi (optarg);
473 if (max_debug_argument_length <= 0)
474 max_debug_argument_length = 0;
475 break;
477 case 'o':
478 /* -o/--error-output are deprecated synonyms of --debugfile,
479 but don't issue a deprecation warning until autoconf 2.61
480 or later is more widely established, as such a warning
481 would interfere with all earlier versions of autoconf. */
482 case DEBUGFILE_OPTION:
483 /* Don't call debug_set_output here, as it has side effects. */
484 debugfile = optarg;
485 break;
487 case WARN_MACRO_SEQUENCE_OPTION:
488 /* Don't call set_macro_sequence here, as it can exit.
489 --warn-macro-sequence sets optarg to NULL (which uses the
490 default regexp); --warn-macro-sequence= sets optarg to ""
491 (which disables these warnings). */
492 macro_sequence = optarg;
493 break;
495 case VERSION_OPTION:
496 version_etc (stdout, PACKAGE, PACKAGE_NAME, VERSION, AUTHORS, NULL);
497 exit (EXIT_SUCCESS);
498 break;
500 case HELP_OPTION:
501 usage (EXIT_SUCCESS);
502 break;
505 defines = head;
507 /* Do the basic initializations. */
508 if (debugfile && !debug_set_output (debugfile))
509 M4ERROR ((0, errno, "cannot set debug file `%s'", debugfile));
511 input_init ();
512 output_init ();
513 symtab_init ();
514 set_macro_sequence (macro_sequence);
515 include_env_init ();
517 if (frozen_file_to_read)
518 reload_frozen_state (frozen_file_to_read);
519 else
520 builtin_init ();
522 /* Interactive mode means unbuffered output, and interrupts ignored. */
524 if (interactive)
526 signal (SIGINT, SIG_IGN);
527 setbuf (stdout, (char *) NULL);
530 /* Handle deferred command line macro definitions. Must come after
531 initialization of the symbol table. */
533 while (defines != NULL)
535 macro_definition *next;
536 symbol *sym;
538 switch (defines->code)
540 case 'D':
542 /* defines->arg is read-only, so we need a copy. */
543 char *macro_name = xstrdup (defines->arg);
544 char *macro_value = strchr (macro_name, '=');
545 if (macro_value)
546 *macro_value++ = '\0';
547 define_user_macro (macro_name, macro_value, SYMBOL_INSERT);
548 free (macro_name);
550 break;
552 case 'U':
553 lookup_symbol (defines->arg, SYMBOL_DELETE);
554 break;
556 case 't':
557 sym = lookup_symbol (defines->arg, SYMBOL_INSERT);
558 SYMBOL_TRACED (sym) = true;
559 break;
561 case 's':
562 sync_output = 1;
563 break;
565 case '\1':
566 seen_file = true;
567 process_file (defines->arg);
568 break;
570 default:
571 M4ERROR ((0, 0, "INTERNAL ERROR: bad code in deferred arguments"));
572 abort ();
575 next = defines->next;
576 free (defines);
577 defines = next;
580 /* Handle remaining input files. Each file is pushed on the input,
581 and the input read. Wrapup text is handled separately later. */
583 if (optind == argc && !seen_file)
584 process_file ("-");
585 else
586 for (; optind < argc; optind++)
587 process_file (argv[optind]);
589 /* Now handle wrapup text. */
591 while (pop_wrapup ())
592 expand_input ();
594 /* Change debug stream back to stderr, to force flushing the debug
595 stream and detect any errors it might have encountered. The
596 three standard streams are closed by close_stdin. */
597 debug_set_output (NULL);
599 if (frozen_file_to_write)
600 produce_frozen_state (frozen_file_to_write);
601 else
603 make_diversion (0);
604 undivert_all ();
606 output_exit ();
607 free_regex ();
608 #ifdef DEBUG_REGEX
609 if (trace_file)
610 fclose (trace_file);
611 #endif /* DEBUG_REGEX */
612 exit (retcode);