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
= 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
= "";
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 | Wrapper around error. |
88 `-----------------------*/
91 m4_error (int status
, int errnum
, const char *format
, ...)
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 `-------------------------------*/
106 m4_error_at_line (int status
, int errnum
, const char *file
, int line
,
107 const char *format
, ...)
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
;
118 /*---------------------------------------.
119 | Tell user stack overflowed and abort. |
120 `---------------------------------------*/
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 `---------------------------------------------*/
139 if (status
!= EXIT_SUCCESS
)
140 xfprintf (stderr
, "Try `%s --help' for more information.\n", program_name
);
143 xprintf ("Usage: %s [OPTION]... [FILE]...\n", program_name
);
145 Process macros in FILEs. If no FILE or if FILE is `-', standard input\n\
150 Mandatory or optional arguments to long options are mandatory or optional\n\
151 for short options too.\n\
154 --help display this help and exit\n\
155 --version output version information and exit\n\
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\
166 ", DEFAULT_MACRO_SEQUENCE
);
167 #ifdef ENABLE_CHANGEWORD
169 -W, --word-regexp=REGEXP use REGEXP for macro name syntax\n\
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\
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\
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\
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\
204 a show actual arguments\n\
205 c show before collect, after collect and after call\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\
218 If defined, the environment variable `M4PATH' is a colon-separated list\n\
219 of directories included after any specified by `-I'.\n\
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\
226 xprintf ("\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT
);
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. */
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
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);
295 FILE *fp
= m4_path_search (name
, &full_name
);
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
;
304 push_file (fp
, full_name
, true);
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:"
318 #define OPTSTRING "-B:D:EF:GH:I:L:N:PQR:S:T:U:d::eil:o:st:"
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
);
347 const char *name
= getenv ("M4_TRACE_FILE");
349 trace_file
= fopen (name
, "a");
351 fputs ("m4:\n", trace_file
);
353 #endif /* DEBUG_REGEX */
358 setup_stackovf_trap (argv
, envp
, stackovf_handler
);
361 /* First, we decode the arguments, to size up tables and stuff. */
365 while ((optchar
= getopt_long (argc
, (char **) argv
, OPTSTRING
,
366 long_options
, NULL
)) != -1)
370 usage (EXIT_FAILURE
);
375 /* Compatibility junk: options that other implementations
376 support, but which we ignore as no-ops and don't list in
378 error (0, 0, "Warning: `m4 -%c' may be removed in a future release",
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");
393 /* Arguments that cannot be handled until later are accumulated. */
395 defn
= (macro_definition
*) xmalloc (sizeof (macro_definition
));
396 defn
->code
= optchar
;
409 if (! fatal_warnings
)
410 fatal_warnings
= true;
412 warning_status
= EXIT_FAILURE
;
416 frozen_file_to_write
= optarg
;
420 no_gnu_extensions
= 1;
424 hash_table_size
= atol (optarg
);
425 if (hash_table_size
== 0)
426 hash_table_size
= HASHMAX
;
430 add_include_directory (optarg
);
434 nesting_limit
= atoi (optarg
);
438 prefix_all_builtins
= 1;
442 suppress_warnings
= 1;
446 frozen_file_to_read
= optarg
;
449 #ifdef ENABLE_CHANGEWORD
451 user_word_regexp
= optarg
;
456 debug_level
= debug_decode (optarg
);
459 error (0, 0, "bad debug flags: `%s'", optarg
);
465 error (0, 0, "Warning: `m4 -e' is deprecated, use `-i' instead");
472 max_debug_argument_length
= atoi (optarg
);
473 if (max_debug_argument_length
<= 0)
474 max_debug_argument_length
= 0;
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. */
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
;
496 version_etc (stdout
, PACKAGE
, PACKAGE_NAME
, VERSION
, AUTHORS
, NULL
);
501 usage (EXIT_SUCCESS
);
507 /* Do the basic initializations. */
508 if (debugfile
&& !debug_set_output (debugfile
))
509 M4ERROR ((0, errno
, "cannot set debug file `%s'", debugfile
));
514 set_macro_sequence (macro_sequence
);
517 if (frozen_file_to_read
)
518 reload_frozen_state (frozen_file_to_read
);
522 /* Interactive mode means unbuffered output, and interrupts ignored. */
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
;
538 switch (defines
->code
)
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
, '=');
546 *macro_value
++ = '\0';
547 define_user_macro (macro_name
, macro_value
, SYMBOL_INSERT
);
553 lookup_symbol (defines
->arg
, SYMBOL_DELETE
);
557 sym
= lookup_symbol (defines
->arg
, SYMBOL_INSERT
);
558 SYMBOL_TRACED (sym
) = true;
567 process_file (defines
->arg
);
571 M4ERROR ((0, 0, "INTERNAL ERROR: bad code in deferred arguments"));
575 next
= 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
)
586 for (; optind
< argc
; optind
++)
587 process_file (argv
[optind
]);
589 /* Now handle wrapup text. */
591 while (pop_wrapup ())
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
);
611 #endif /* DEBUG_REGEX */