Ignore Windows executables.
[m4/ericb.git] / src / m4.c
blob2cfed194cfa7f7656cae1eaeacf3d4d50a75342f
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 <signal.h>
26 #include <stdarg.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 = INT_MAX;
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 | Helper for all the error reporting, as a wrapper around |
88 | error_at_line. Report error message based on FORMAT and ARGS, on |
89 | behalf of MACRO, at the location FILE and LINE (but with no |
90 | location if LINE is 0). If ERRNUM, decode the errno value that |
91 | caused the error. If STATUS, exit immediately with that status. |
92 | If WARN, prepend 'Warning: '. |
93 `------------------------------------------------------------------*/
95 static void
96 m4_verror_at_line (bool warn, int status, int errnum, const char *file,
97 int line, const char *macro, const char *format,
98 va_list args)
100 char *full = NULL;
101 /* Prepend warning and the macro name, as needed. But if that fails
102 for non-memory reasons (unlikely), then still use the original
103 format. */
104 if (warn && macro)
105 full = xasprintf (_("Warning: %s: %s"), macro, format);
106 else if (warn)
107 full = xasprintf (_("Warning: %s"), format);
108 else if (macro)
109 full = xasprintf (_("%s: %s"), macro, format);
110 verror_at_line (status, errnum, line ? file : NULL, line,
111 full ? full : format, args);
112 free (full);
113 if ((!warn || fatal_warnings) && !retcode)
114 retcode = EXIT_FAILURE;
117 /*----------------------------------------------------------------.
118 | Wrapper around error. Report error message based on FORMAT and |
119 | subsequent args, on behalf of MACRO, and the current input line |
120 | (if any). If ERRNUM, decode the errno value that caused the |
121 | error. If STATUS, exit immediately with that status. |
122 `----------------------------------------------------------------*/
124 void
125 m4_error (int status, int errnum, const char *macro, const char *format, ...)
127 va_list args;
128 va_start (args, format);
129 if (status == EXIT_SUCCESS && warning_status)
130 status = EXIT_FAILURE;
131 m4_verror_at_line (false, status, errnum, current_file, current_line,
132 macro, format, args);
133 va_end (args);
136 /*----------------------------------------------------------------.
137 | Wrapper around error_at_line. Report error message based on |
138 | FORMAT and subsequent args, on behalf of MACRO, at the location |
139 | FILE and LINE (but with no location if LINE is 0). If ERRNUM, |
140 | decode the errno value that caused the error. If STATUS, exit |
141 | immediately with that status. |
142 `----------------------------------------------------------------*/
144 void
145 m4_error_at_line (int status, int errnum, const char *file, int line,
146 const char *macro, const char *format, ...)
148 va_list args;
149 va_start (args, format);
150 if (status == EXIT_SUCCESS && warning_status)
151 status = EXIT_FAILURE;
152 m4_verror_at_line (false, status, errnum, file, line, macro, format, args);
153 va_end (args);
156 /*------------------------------------------------------------------.
157 | Wrapper around error. Report warning message based on FORMAT and |
158 | subsequent args, on behalf of MACRO, and the current input line |
159 | (if any). If ERRNUM, decode the errno value that caused the |
160 | warning. |
161 `------------------------------------------------------------------*/
163 void
164 m4_warn (int errnum, const char *macro, const char *format, ...)
166 va_list args;
167 if (!suppress_warnings)
169 va_start (args, format);
170 m4_verror_at_line (true, warning_status, errnum, current_file,
171 current_line, macro, format, args);
172 va_end (args);
176 /*----------------------------------------------------------------.
177 | Wrapper around error_at_line. Report warning message based on |
178 | FORMAT and subsequent args, on behalf of MACRO, at the location |
179 | FILE and LINE (but with no location if LINE is 0). If ERRNUM, |
180 | decode the errno value that caused the warning. |
181 `----------------------------------------------------------------*/
183 void
184 m4_warn_at_line (int errnum, const char *file, int line, const char *macro,
185 const char *format, ...)
187 va_list args;
188 if (!suppress_warnings)
190 va_start (args, format);
191 m4_verror_at_line (true, warning_status, errnum, file, line, macro,
192 format, args);
193 va_end (args);
197 #ifdef USE_STACKOVF
199 /*---------------------------------------.
200 | Tell user stack overflowed and abort. |
201 `---------------------------------------*/
203 static void
204 stackovf_handler (void)
206 m4_error (EXIT_FAILURE, 0, NULL,
207 _("ERROR: stack overflow. (Infinite define recursion?)"));
210 #endif /* USE_STACKOV */
213 /*---------------------------------------------.
214 | Print a usage message and exit with STATUS. |
215 `---------------------------------------------*/
217 static void
218 usage (int status)
220 if (status != EXIT_SUCCESS)
221 xfprintf (stderr, "Try `%s --help' for more information.\n", program_name);
222 else
224 xprintf ("Usage: %s [OPTION]... [FILE]...\n", program_name);
225 fputs ("\
226 Process macros in FILEs. If no FILE or if FILE is `-', standard input\n\
227 is read.\n\
228 ", stdout);
229 fputs ("\
231 Mandatory or optional arguments to long options are mandatory or optional\n\
232 for short options too.\n\
234 Operation modes:\n\
235 --help display this help and exit\n\
236 --version output version information and exit\n\
237 ", stdout);
238 xprintf ("\
239 -E, --fatal-warnings once: warnings become errors, twice: stop\n\
240 execution at first error\n\
241 -i, --interactive unbuffer output, ignore interrupts\n\
242 -P, --prefix-builtins force a `m4_' prefix to all builtins\n\
243 -Q, --quiet, --silent suppress some warnings for builtins\n\
244 --warn-macro-sequence[=REGEXP]\n\
245 warn if macro definition matches REGEXP,\n\
246 default %s\n\
247 ", DEFAULT_MACRO_SEQUENCE);
248 #ifdef ENABLE_CHANGEWORD
249 fputs ("\
250 -W, --word-regexp=REGEXP use REGEXP for macro name syntax\n\
251 ", stdout);
252 #endif
253 fputs ("\
255 Preprocessor features:\n\
256 -D, --define=NAME[=VALUE] define NAME as having VALUE, or empty\n\
257 -I, --include=DIRECTORY append DIRECTORY to include path\n\
258 -s, --synclines generate `#line NUM \"FILE\"' lines\n\
259 -U, --undefine=NAME undefine NAME\n\
260 ", stdout);
261 fputs ("\
263 Limits control:\n\
264 -G, --traditional suppress all GNU extensions\n\
265 -H, --hashsize=PRIME set symbol lookup hash table size [509]\n\
266 -L, --nesting-limit=NUMBER change artificial nesting limit [1024]\n\
267 ", stdout);
268 fputs ("\
270 Frozen state files:\n\
271 -F, --freeze-state=FILE produce a frozen state on FILE at end\n\
272 -R, --reload-state=FILE reload a frozen state from FILE at start\n\
273 ", stdout);
274 fputs ("\
276 Debugging:\n\
277 -d, --debug[=FLAGS] set debug level (no FLAGS implies `aeq')\n\
278 --debugfile=FILE redirect debug and trace output\n\
279 -l, --arglength=NUM restrict macro tracing size\n\
280 -t, --trace=NAME trace NAME when it is defined\n\
281 ", stdout);
282 fputs ("\
284 FLAGS is any of:\n\
285 a show actual arguments\n\
286 c show before collect, after collect and after call\n\
287 e show expansion\n\
288 f say current input file name\n\
289 i show changes in input files\n\
290 l say current input line number\n\
291 p show results of path searches\n\
292 q quote values as necessary, with a or e flag\n\
293 t trace for all macro calls, not only traceon'ed\n\
294 x add a unique macro call id, useful with c flag\n\
295 V shorthand for all of the above flags\n\
296 ", stdout);
297 fputs ("\
299 If defined, the environment variable `M4PATH' is a colon-separated list\n\
300 of directories included after any specified by `-I'.\n\
301 ", stdout);
302 fputs ("\
304 Exit status is 0 for success, 1 for failure, 63 for frozen file version\n\
305 mismatch, or whatever value was passed to the m4exit macro.\n\
306 ", stdout);
307 xprintf ("\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
309 exit (status);
312 /*--------------------------------------.
313 | Decode options and launch execution. |
314 `--------------------------------------*/
316 /* For long options that have no equivalent short option, use a
317 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
318 enum
320 DEBUGFILE_OPTION = CHAR_MAX + 1, /* no short opt */
321 DIVERSIONS_OPTION, /* not quite -N, because of message */
322 WARN_MACRO_SEQUENCE_OPTION, /* no short opt */
324 HELP_OPTION, /* no short opt */
325 VERSION_OPTION /* no short opt */
328 static const struct option long_options[] =
330 {"arglength", required_argument, NULL, 'l'},
331 {"debug", optional_argument, NULL, 'd'},
332 {"define", required_argument, NULL, 'D'},
333 {"error-output", required_argument, NULL, 'o'}, /* FIXME: deprecate in 2.0 */
334 {"fatal-warnings", no_argument, NULL, 'E'},
335 {"freeze-state", required_argument, NULL, 'F'},
336 {"hashsize", required_argument, NULL, 'H'},
337 {"include", required_argument, NULL, 'I'},
338 {"interactive", no_argument, NULL, 'i'},
339 {"nesting-limit", required_argument, NULL, 'L'},
340 {"prefix-builtins", no_argument, NULL, 'P'},
341 {"quiet", no_argument, NULL, 'Q'},
342 {"reload-state", required_argument, NULL, 'R'},
343 {"silent", no_argument, NULL, 'Q'},
344 {"synclines", no_argument, NULL, 's'},
345 {"trace", required_argument, NULL, 't'},
346 {"traditional", no_argument, NULL, 'G'},
347 {"undefine", required_argument, NULL, 'U'},
348 {"word-regexp", required_argument, NULL, 'W'},
350 {"debugfile", required_argument, NULL, DEBUGFILE_OPTION},
351 {"diversions", required_argument, NULL, DIVERSIONS_OPTION},
352 {"warn-macro-sequence", optional_argument, NULL, WARN_MACRO_SEQUENCE_OPTION},
354 {"help", no_argument, NULL, HELP_OPTION},
355 {"version", no_argument, NULL, VERSION_OPTION},
357 { NULL, 0, NULL, 0 },
360 /* Process a command line file NAME, and return true only if it was
361 stdin. */
362 static void
363 process_file (const char *name)
365 if (strcmp (name, "-") == 0)
367 /* If stdin is a terminal, we want to allow 'm4 - file -'
368 to read input from stdin twice, like GNU cat. Besides,
369 there is no point closing stdin before wrapped text, to
370 minimize bugs in syscmd called from wrapped text. */
371 push_file (stdin, "stdin", false);
373 else
375 char *full_name;
376 FILE *fp = m4_path_search (name, &full_name);
377 if (fp == NULL)
379 error (0, errno, "%s", name);
380 /* Set the status to EXIT_FAILURE, even though we
381 continue to process files after a missing file. */
382 retcode = EXIT_FAILURE;
383 return;
385 push_file (fp, full_name, true);
386 free (full_name);
388 expand_input ();
391 /* POSIX requires only -D, -U, and -s; and says that the first two
392 must be recognized when interspersed with file names. Traditional
393 behavior also handles -s between files. Starting OPTSTRING with
394 '-' forces getopt_long to hand back file names as arguments to opt
395 '\1', rather than reordering the command line. */
396 #ifdef ENABLE_CHANGEWORD
397 #define OPTSTRING "-B:D:EF:GH:I:L:N:PQR:S:T:U:W:d::eil:o:st:"
398 #else
399 #define OPTSTRING "-B:D:EF:GH:I:L:N:PQR:S:T:U:d::eil:o:st:"
400 #endif
402 #ifdef DEBUG_REGEX
403 FILE *trace_file;
404 #endif /* DEBUG_REGEX */
407 main (int argc, char *const *argv, char *const *envp)
409 macro_definition *head; /* head of deferred argument list */
410 macro_definition *tail;
411 macro_definition *defn;
412 int optchar; /* option character */
414 macro_definition *defines;
415 bool interactive = false;
416 bool seen_file = false;
417 const char *debugfile = NULL;
418 const char *frozen_file_to_read = NULL;
419 const char *frozen_file_to_write = NULL;
420 const char *macro_sequence = "";
422 program_name = argv[0];
423 retcode = EXIT_SUCCESS;
424 atexit (close_stdin);
426 #ifdef DEBUG_REGEX
428 const char *name = getenv ("M4_TRACE_FILE");
429 if (name)
430 trace_file = fopen (name, "a");
431 if (trace_file)
432 fputs ("m4:\n", trace_file);
434 #endif /* DEBUG_REGEX */
436 include_init ();
437 debug_init ();
438 #ifdef USE_STACKOVF
439 setup_stackovf_trap (argv, envp, stackovf_handler);
440 #endif
442 /* First, we decode the arguments, to size up tables and stuff. */
444 head = tail = NULL;
446 while ((optchar = getopt_long (argc, (char **) argv, OPTSTRING,
447 long_options, NULL)) != -1)
448 switch (optchar)
450 default:
451 usage (EXIT_FAILURE);
453 case 'B':
454 case 'S':
455 case 'T':
456 /* Compatibility junk: options that other implementations
457 support, but which we ignore as no-ops and don't list in
458 --help. */
459 error (0, 0, "Warning: `m4 -%c' may be removed in a future release",
460 optchar);
461 break;
463 case 'N':
464 case DIVERSIONS_OPTION:
465 /* -N became an obsolete no-op in 1.4.x. */
466 error (0, 0, "Warning: `m4 %s' is deprecated",
467 optchar == 'N' ? "-N" : "--diversions");
469 case 'D':
470 case 'U':
471 case 's':
472 case 't':
473 case '\1':
474 /* Arguments that cannot be handled until later are accumulated. */
476 defn = (macro_definition *) xmalloc (sizeof (macro_definition));
477 defn->code = optchar;
478 defn->arg = optarg;
479 defn->next = NULL;
481 if (head == NULL)
482 head = defn;
483 else
484 tail->next = defn;
485 tail = defn;
487 break;
489 case 'E':
490 if (!fatal_warnings)
491 fatal_warnings = true;
492 else
493 warning_status = EXIT_FAILURE;
494 break;
496 case 'F':
497 frozen_file_to_write = optarg;
498 break;
500 case 'G':
501 no_gnu_extensions = 1;
502 break;
504 case 'H':
505 hash_table_size = atol (optarg);
506 if (hash_table_size == 0)
507 hash_table_size = HASHMAX;
508 break;
510 case 'I':
511 add_include_directory (optarg);
512 break;
514 case 'L':
515 nesting_limit = atoi (optarg);
516 break;
518 case 'P':
519 prefix_all_builtins = 1;
520 break;
522 case 'Q':
523 suppress_warnings = 1;
524 break;
526 case 'R':
527 frozen_file_to_read = optarg;
528 break;
530 #ifdef ENABLE_CHANGEWORD
531 case 'W':
532 user_word_regexp = optarg;
533 break;
534 #endif
536 case 'd':
537 debug_level = debug_decode (optarg);
538 if (debug_level < 0)
540 error (0, 0, "bad debug flags: `%s'", optarg);
541 debug_level = 0;
543 break;
545 case 'e':
546 error (0, 0, "Warning: `m4 -e' is deprecated, use `-i' instead");
547 /* fall through */
548 case 'i':
549 interactive = true;
550 break;
552 case 'l':
553 max_debug_argument_length = atoi (optarg);
554 if (max_debug_argument_length <= 0)
555 max_debug_argument_length = INT_MAX;
556 break;
558 case 'o':
559 /* -o/--error-output are deprecated synonyms of --debugfile,
560 but don't issue a deprecation warning until autoconf 2.61
561 or later is more widely established, as such a warning
562 would interfere with all earlier versions of autoconf. */
563 case DEBUGFILE_OPTION:
564 /* Don't call debug_set_output here, as it has side effects. */
565 debugfile = optarg;
566 break;
568 case WARN_MACRO_SEQUENCE_OPTION:
569 /* Don't call set_macro_sequence here, as it can exit.
570 --warn-macro-sequence sets optarg to NULL (which uses the
571 default regexp); --warn-macro-sequence= sets optarg to ""
572 (which disables these warnings). */
573 macro_sequence = optarg;
574 break;
576 case VERSION_OPTION:
577 version_etc (stdout, PACKAGE, PACKAGE_NAME, VERSION, AUTHORS, NULL);
578 exit (EXIT_SUCCESS);
579 break;
581 case HELP_OPTION:
582 usage (EXIT_SUCCESS);
583 break;
586 defines = head;
588 /* Do the basic initializations. */
589 if (debugfile && !debug_set_output (NULL, debugfile))
590 m4_error (0, errno, NULL, _("cannot set debug file `%s'"), debugfile);
592 input_init ();
593 output_init ();
594 symtab_init ();
595 set_macro_sequence (macro_sequence);
596 include_env_init ();
598 if (frozen_file_to_read)
599 reload_frozen_state (frozen_file_to_read);
600 else
601 builtin_init ();
603 /* Interactive mode means unbuffered output, and interrupts ignored. */
605 if (interactive)
607 signal (SIGINT, SIG_IGN);
608 setbuf (stdout, (char *) NULL);
611 /* Handle deferred command line macro definitions. Must come after
612 initialization of the symbol table. */
614 while (defines != NULL)
616 macro_definition *next;
617 symbol *sym;
619 switch (defines->code)
621 case 'D':
623 /* defines->arg is read-only, so we need a copy. */
624 char *macro_name = xstrdup (defines->arg);
625 char *macro_value = strchr (macro_name, '=');
626 if (macro_value)
627 *macro_value++ = '\0';
628 define_user_macro (macro_name, strlen (macro_name),
629 macro_value, SYMBOL_INSERT);
630 free (macro_name);
632 break;
634 case 'U':
635 lookup_symbol (defines->arg, SYMBOL_DELETE);
636 break;
638 case 't':
639 sym = lookup_symbol (defines->arg, SYMBOL_INSERT);
640 SYMBOL_TRACED (sym) = true;
641 break;
643 case 's':
644 sync_output = 1;
645 break;
647 case '\1':
648 seen_file = true;
649 process_file (defines->arg);
650 break;
652 default:
653 assert (!"main");
654 abort ();
657 next = defines->next;
658 free (defines);
659 defines = next;
662 /* Handle remaining input files. Each file is pushed on the input,
663 and the input read. Wrapup text is handled separately later. */
665 if (optind == argc && !seen_file)
666 process_file ("-");
667 else
668 for (; optind < argc; optind++)
669 process_file (argv[optind]);
671 /* Now handle wrapup text. */
673 while (pop_wrapup ())
674 expand_input ();
676 /* Change debug stream back to stderr, to force flushing the debug
677 stream and detect any errors it might have encountered. The
678 three standard streams are closed by close_stdin. */
679 debug_set_output (NULL, NULL);
681 if (frozen_file_to_write)
682 produce_frozen_state (frozen_file_to_write);
683 else
685 make_diversion (0);
686 undivert_all ();
688 output_exit ();
689 free_regex ();
690 #ifdef DEBUG_REGEX
691 if (trace_file)
692 fclose (trace_file);
693 #endif /* DEBUG_REGEX */
694 exit (retcode);