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 fprintf (stderr
, "Try `%s --help' for more information.\n", program_name
);
143 printf ("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 printf ("\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:"
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
);
344 setup_stackovf_trap (argv
, envp
, stackovf_handler
);
347 /* First, we decode the arguments, to size up tables and stuff. */
351 while ((optchar
= getopt_long (argc
, (char **) argv
, OPTSTRING
,
352 long_options
, NULL
)) != -1)
356 usage (EXIT_FAILURE
);
361 /* Compatibility junk: options that other implementations
362 support, but which we ignore as no-ops and don't list in
364 error (0, 0, "Warning: `m4 -%c' may be removed in a future release",
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");
379 /* Arguments that cannot be handled until later are accumulated. */
381 defn
= (macro_definition
*) xmalloc (sizeof (macro_definition
));
382 defn
->code
= optchar
;
395 if (! fatal_warnings
)
396 fatal_warnings
= true;
398 warning_status
= EXIT_FAILURE
;
402 frozen_file_to_write
= optarg
;
406 no_gnu_extensions
= 1;
410 hash_table_size
= atol (optarg
);
411 if (hash_table_size
== 0)
412 hash_table_size
= HASHMAX
;
416 add_include_directory (optarg
);
420 nesting_limit
= atoi (optarg
);
424 prefix_all_builtins
= 1;
428 suppress_warnings
= 1;
432 frozen_file_to_read
= optarg
;
435 #ifdef ENABLE_CHANGEWORD
437 user_word_regexp
= optarg
;
442 debug_level
= debug_decode (optarg
);
445 error (0, 0, "bad debug flags: `%s'", optarg
);
451 error (0, 0, "Warning: `m4 -e' is deprecated, use `-i' instead");
458 max_debug_argument_length
= atoi (optarg
);
459 if (max_debug_argument_length
<= 0)
460 max_debug_argument_length
= 0;
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. */
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
;
482 version_etc (stdout
, PACKAGE
, PACKAGE_NAME
, VERSION
, AUTHORS
, NULL
);
487 usage (EXIT_SUCCESS
);
493 /* Do the basic initializations. */
494 if (debugfile
&& !debug_set_output (debugfile
))
495 M4ERROR ((0, errno
, "cannot set debug file `%s'", debugfile
));
500 set_macro_sequence (macro_sequence
);
503 if (frozen_file_to_read
)
504 reload_frozen_state (frozen_file_to_read
);
508 /* Interactive mode means unbuffered output, and interrupts ignored. */
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
;
524 switch (defines
->code
)
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
, '=');
532 *macro_value
++ = '\0';
533 define_user_macro (macro_name
, macro_value
, SYMBOL_INSERT
);
539 lookup_symbol (defines
->arg
, SYMBOL_DELETE
);
543 sym
= lookup_symbol (defines
->arg
, SYMBOL_INSERT
);
544 SYMBOL_TRACED (sym
) = true;
553 process_file (defines
->arg
);
557 M4ERROR ((0, 0, "INTERNAL ERROR: bad code in deferred arguments"));
561 next
= 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
)
572 for (; optind
< argc
; optind
++)
573 process_file (argv
[optind
]);
575 /* Now handle wrapup text. */
577 while (pop_wrapup ())
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
);