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/>.
28 #include "version-etc.h"
30 #define AUTHORS "Rene' Seindal"
32 static void usage (int);
34 /* Enable sync output for /lib/cpp (-s). */
37 /* Debug (-d[flags]). */
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
= "";
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. */
76 struct macro_definition
78 struct macro_definition
*next
;
79 int code
; /* D, U, s, t, or '\1' */
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 `------------------------------------------------------------------*/
96 m4_verror_at_line (bool warn
, int status
, int errnum
, const char *file
,
97 int line
, const char *macro
, const char *format
,
101 /* Prepend warning and the macro name, as needed. But if that fails
102 for non-memory reasons (unlikely), then still use the original
105 full
= xasprintf (_("Warning: %s: %s"), macro
, format
);
107 full
= xasprintf (_("Warning: %s"), format
);
109 full
= xasprintf (_("%s: %s"), macro
, format
);
110 verror_at_line (status
, errnum
, line
? file
: NULL
, line
,
111 full
? full
: format
, args
);
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 `----------------------------------------------------------------*/
125 m4_error (int status
, int errnum
, const char *macro
, const char *format
, ...)
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
);
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 `----------------------------------------------------------------*/
145 m4_error_at_line (int status
, int errnum
, const char *file
, int line
,
146 const char *macro
, const char *format
, ...)
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
);
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 |
161 `------------------------------------------------------------------*/
164 m4_warn (int errnum
, const char *macro
, const char *format
, ...)
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
);
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 `----------------------------------------------------------------*/
184 m4_warn_at_line (int errnum
, const char *file
, int line
, const char *macro
,
185 const char *format
, ...)
188 if (!suppress_warnings
)
190 va_start (args
, format
);
191 m4_verror_at_line (true, warning_status
, errnum
, file
, line
, macro
,
199 /*---------------------------------------.
200 | Tell user stack overflowed and abort. |
201 `---------------------------------------*/
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 `---------------------------------------------*/
220 if (status
!= EXIT_SUCCESS
)
221 xfprintf (stderr
, "Try `%s --help' for more information.\n", program_name
);
224 xprintf ("Usage: %s [OPTION]... [FILE]...\n", program_name
);
226 Process macros in FILEs. If no FILE or if FILE is `-', standard input\n\
231 Mandatory or optional arguments to long options are mandatory or optional\n\
232 for short options too.\n\
235 --help display this help and exit\n\
236 --version output version information and exit\n\
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\
247 ", DEFAULT_MACRO_SEQUENCE
);
248 #ifdef ENABLE_CHANGEWORD
250 -W, --word-regexp=REGEXP use REGEXP for macro name syntax\n\
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\
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\
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\
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\
285 a show actual arguments\n\
286 c show before collect, after collect and after call\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\
299 If defined, the environment variable `M4PATH' is a colon-separated list\n\
300 of directories included after any specified by `-I'.\n\
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\
307 xprintf ("\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT
);
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. */
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
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);
376 FILE *fp
= m4_path_search (name
, &full_name
);
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
;
385 push_file (fp
, full_name
, true);
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:"
399 #define OPTSTRING "-B:D:EF:GH:I:L:N:PQR:S:T:U:d::eil:o:st:"
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
);
428 const char *name
= getenv ("M4_TRACE_FILE");
430 trace_file
= fopen (name
, "a");
432 fputs ("m4:\n", trace_file
);
434 #endif /* DEBUG_REGEX */
439 setup_stackovf_trap (argv
, envp
, stackovf_handler
);
442 /* First, we decode the arguments, to size up tables and stuff. */
446 while ((optchar
= getopt_long (argc
, (char **) argv
, OPTSTRING
,
447 long_options
, NULL
)) != -1)
451 usage (EXIT_FAILURE
);
456 /* Compatibility junk: options that other implementations
457 support, but which we ignore as no-ops and don't list in
459 error (0, 0, "Warning: `m4 -%c' may be removed in a future release",
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");
474 /* Arguments that cannot be handled until later are accumulated. */
476 defn
= (macro_definition
*) xmalloc (sizeof (macro_definition
));
477 defn
->code
= optchar
;
491 fatal_warnings
= true;
493 warning_status
= EXIT_FAILURE
;
497 frozen_file_to_write
= optarg
;
501 no_gnu_extensions
= 1;
505 hash_table_size
= atol (optarg
);
506 if (hash_table_size
== 0)
507 hash_table_size
= HASHMAX
;
511 add_include_directory (optarg
);
515 nesting_limit
= atoi (optarg
);
519 prefix_all_builtins
= 1;
523 suppress_warnings
= 1;
527 frozen_file_to_read
= optarg
;
530 #ifdef ENABLE_CHANGEWORD
532 user_word_regexp
= optarg
;
537 debug_level
= debug_decode (optarg
);
540 error (0, 0, "bad debug flags: `%s'", optarg
);
546 error (0, 0, "Warning: `m4 -e' is deprecated, use `-i' instead");
553 max_debug_argument_length
= atoi (optarg
);
554 if (max_debug_argument_length
<= 0)
555 max_debug_argument_length
= INT_MAX
;
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. */
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
;
577 version_etc (stdout
, PACKAGE
, PACKAGE_NAME
, VERSION
, AUTHORS
, NULL
);
582 usage (EXIT_SUCCESS
);
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
);
595 set_macro_sequence (macro_sequence
);
598 if (frozen_file_to_read
)
599 reload_frozen_state (frozen_file_to_read
);
603 /* Interactive mode means unbuffered output, and interrupts ignored. */
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
;
619 switch (defines
->code
)
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
, '=');
627 *macro_value
++ = '\0';
628 define_user_macro (macro_name
, strlen (macro_name
),
629 macro_value
, SYMBOL_INSERT
);
635 lookup_symbol (defines
->arg
, SYMBOL_DELETE
);
639 sym
= lookup_symbol (defines
->arg
, SYMBOL_INSERT
);
640 SYMBOL_TRACED (sym
) = true;
649 process_file (defines
->arg
);
657 next
= 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
)
668 for (; optind
< argc
; optind
++)
669 process_file (argv
[optind
]);
671 /* Now handle wrapup text. */
673 while (pop_wrapup ())
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
);
693 #endif /* DEBUG_REGEX */