* doc/Makefile.am (m4.1): Improve man page.
[m4/ericb.git] / src / m4.c
bloba2eb9dea777fa0287445a13c60aea1bbeb9d2bd2
1 /* GNU m4 -- A simple macro processor
3 Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 2004, 2005, 2006 Free
4 Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA
22 #include "m4.h"
24 #include <getopt.h>
25 #include <signal.h>
27 static void usage (int);
29 /* Operate interactively (-e). */
30 static int interactive = 0;
32 /* Enable sync output for /lib/cpp (-s). */
33 int sync_output = 0;
35 /* Debug (-d[flags]). */
36 int debug_level = 0;
38 /* Hash table size (should be a prime) (-Hsize). */
39 size_t hash_table_size = HASHMAX;
41 /* Disable GNU extensions (-G). */
42 int no_gnu_extensions = 0;
44 /* Prefix all builtin functions by `m4_'. */
45 int prefix_all_builtins = 0;
47 /* Max length of arguments in trace output (-lsize). */
48 int max_debug_argument_length = 0;
50 /* Suppress warnings about missing arguments. */
51 int suppress_warnings = 0;
53 /* If not zero, then value of exit status for warning diagnostics. */
54 int warning_status = 0;
56 /* Artificial limit for expansion_level in macro.c. */
57 int nesting_limit = 1024;
59 #ifdef ENABLE_CHANGEWORD
60 /* User provided regexp for describing m4 words. */
61 const char *user_word_regexp = "";
62 #endif
64 /* Name of frozen file to digest after initialization. */
65 const char *frozen_file_to_read = NULL;
67 /* Name of frozen file to produce near completion. */
68 const char *frozen_file_to_write = NULL;
70 /* The name this program was run with. */
71 const char *program_name;
73 /* If non-zero, display usage information and exit. */
74 static int show_help = 0;
76 /* If non-zero, print the version on standard output and exit. */
77 static int show_version = 0;
79 struct macro_definition
81 struct macro_definition *next;
82 int code; /* D, U or t */
83 const char *macro;
85 typedef struct macro_definition macro_definition;
87 /* Error handling functions. */
89 /*-------------------------------------------------------------------------.
90 | Print source and line reference on standard error, as a prefix for error |
91 | messages. Flush standard output first. |
92 `-------------------------------------------------------------------------*/
94 void
95 reference_error (void)
97 int e = errno;
98 fflush (stdout);
99 fprintf (stderr, "%s:%d: ", current_file, current_line);
100 errno = e;
103 #ifdef USE_STACKOVF
105 /*---------------------------------------.
106 | Tell user stack overflowed and abort. |
107 `---------------------------------------*/
109 static void
110 stackovf_handler (void)
112 M4ERROR ((EXIT_FAILURE, 0,
113 "ERROR: stack overflow. (Infinite define recursion?)"));
116 #endif /* USE_STACKOV */
118 /* Memory allocation. */
120 /*------------------------.
121 | Failsafe free routine. |
122 `------------------------*/
124 #ifdef WITH_DMALLOC
125 # undef xfree
126 #endif
128 void
129 xfree (void *p)
131 if (p != NULL)
132 free (p);
136 /*---------------------------------------------.
137 | Print a usage message and exit with STATUS. |
138 `---------------------------------------------*/
140 static void
141 usage (int status)
143 if (status != EXIT_SUCCESS)
144 fprintf (stderr, "Try `%s --help' for more information.\n", program_name);
145 else
147 printf ("Usage: %s [OPTION]... [FILE]...\n", program_name);
148 fputs ("\
149 Process macros in FILEs. If no FILE or if FILE is `-', standard input\n\
150 is read.\n\
151 ", stdout);
152 fputs ("\
154 Mandatory or optional arguments to long options are mandatory or optional\n\
155 for short options too.\n\
157 Operation modes:\n\
158 --help display this help and exit\n\
159 --version output version information and exit\n\
160 -E, --fatal-warnings stop execution after first warning\n\
161 -e, --interactive unbuffer output, ignore interrupts\n\
162 -P, --prefix-builtins force a `m4_' prefix to all builtins\n\
163 -Q, --quiet, --silent suppress some warnings for builtins\n\
164 ", stdout);
165 #ifdef ENABLE_CHANGEWORD
166 fputs ("\
167 -W, --word-regexp=REGEXP use REGEXP for macro name syntax\n\
168 ", stdout);
169 #endif
170 fputs ("\
172 Preprocessor features:\n\
173 -D, --define=NAME[=VALUE] define NAME has having VALUE, or empty\n\
174 -I, --include=DIRECTORY append DIRECTORY to include path\n\
175 -s, --synclines generate `#line NUM \"FILE\"' lines\n\
176 -U, --undefine=NAME undefine NAME\n\
177 ", stdout);
178 fputs ("\
180 Limits control:\n\
181 -G, --traditional suppress all GNU extensions\n\
182 -H, --hashsize=PRIME set symbol lookup hash table size [509]\n\
183 -L, --nesting-limit=NUMBER change artificial nesting limit [1024]\n\
184 ", stdout);
185 fputs ("\
187 Frozen state files:\n\
188 -F, --freeze-state=FILE produce a frozen state on FILE at end\n\
189 -R, --reload-state=FILE reload a frozen state from FILE at start\n\
190 ", stdout);
191 fputs ("\
193 Debugging:\n\
194 -d, --debug[=FLAGS] set debug level (no FLAGS implies `aeq')\n\
195 -l, --arglength=NUM restrict macro tracing size\n\
196 -o, --error-output=FILE redirect debug and trace output\n\
197 -t, --trace=NAME trace NAME when it will be defined\n\
198 ", stdout);
199 fputs ("\
201 FLAGS is any of:\n\
202 a show actual arguments\n\
203 c show before collect, after collect and after call\n\
204 e show expansion\n\
205 f say current input file name\n\
206 i show changes in input files\n\
207 l say current input line number\n\
208 p show results of path searches\n\
209 q quote values as necessary, with a or e flag\n\
210 t trace for all macro calls, not only traceon'ed\n\
211 x add a unique macro call id, useful with c flag\n\
212 V shorthand for all of the above flags\n\
213 ", stdout);
214 fputs ("\
216 If defined, the environment variable `M4PATH' is a colon-separated list\n\
217 of directories included after any specified by `-I'.\n\
218 ", stdout);
219 fputs ("\
221 Exit status is 0 for success, 1 for failure, 63 for frozen file version\n\
222 mismatch, or whatever value was passed to the m4exit macro.\n\
223 ", stdout);
224 printf ("\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
227 if (close_stream (stdout) != 0)
228 M4ERROR ((EXIT_FAILURE, errno, "write error"));
229 exit (status);
232 /*--------------------------------------.
233 | Decode options and launch execution. |
234 `--------------------------------------*/
236 static const struct option long_options[] =
238 {"arglength", required_argument, NULL, 'l'},
239 {"debug", optional_argument, NULL, 'd'},
240 {"diversions", required_argument, NULL, 'N'},
241 {"error-output", required_argument, NULL, 'o'},
242 {"fatal-warnings", no_argument, NULL, 'E'},
243 {"freeze-state", required_argument, NULL, 'F'},
244 {"hashsize", required_argument, NULL, 'H'},
245 {"include", required_argument, NULL, 'I'},
246 {"interactive", no_argument, NULL, 'e'},
247 {"nesting-limit", required_argument, NULL, 'L'},
248 {"prefix-builtins", no_argument, NULL, 'P'},
249 {"quiet", no_argument, NULL, 'Q'},
250 {"reload-state", required_argument, NULL, 'R'},
251 {"silent", no_argument, NULL, 'Q'},
252 {"synclines", no_argument, NULL, 's'},
253 {"traditional", no_argument, NULL, 'G'},
254 {"word-regexp", required_argument, NULL, 'W'},
256 {"help", no_argument, &show_help, 1},
257 {"version", no_argument, &show_version, 1},
259 /* These are somewhat troublesome. */
260 { "define", required_argument, NULL, 'D' },
261 { "undefine", required_argument, NULL, 'U' },
262 { "trace", required_argument, NULL, 't' },
264 { 0, 0, 0, 0 },
267 /* Global catchall for any errors that should affect final error status, but
268 where we try to continue execution in the meantime. */
269 int retcode;
271 #ifdef ENABLE_CHANGEWORD
272 #define OPTSTRING "B:D:EF:GH:I:L:N:PQR:S:T:U:W:d::el:o:st:"
273 #else
274 #define OPTSTRING "B:D:EF:GH:I:L:N:PQR:S:T:U:d::el:o:st:"
275 #endif
278 main (int argc, char *const *argv, char *const *envp)
280 macro_definition *head; /* head of deferred argument list */
281 macro_definition *tail;
282 macro_definition *new;
283 int optchar; /* option character */
285 macro_definition *defines;
286 FILE *fp;
288 program_name = argv[0];
289 retcode = EXIT_SUCCESS;
291 include_init ();
292 debug_init ();
293 #ifdef USE_STACKOVF
294 setup_stackovf_trap (argv, envp, stackovf_handler);
295 #endif
297 /* First, we decode the arguments, to size up tables and stuff. */
299 head = tail = NULL;
301 while (optchar = getopt_long (argc, (char **) argv, OPTSTRING,
302 long_options, NULL),
303 optchar != EOF)
304 switch (optchar)
306 default:
307 usage (EXIT_FAILURE);
309 case 0:
310 break;
312 case 'B': /* compatibility junk */
313 case 'N':
314 case 'S':
315 case 'T':
316 break;
318 case 'D':
319 case 'U':
320 case 't':
322 /* Arguments that cannot be handled until later are accumulated. */
324 new = (macro_definition *) xmalloc (sizeof (macro_definition));
325 new->code = optchar;
326 new->macro = optarg;
327 new->next = NULL;
329 if (head == NULL)
330 head = new;
331 else
332 tail->next = new;
333 tail = new;
335 break;
337 case 'E':
338 warning_status = EXIT_FAILURE;
339 break;
341 case 'F':
342 frozen_file_to_write = optarg;
343 break;
345 case 'G':
346 no_gnu_extensions = 1;
347 break;
349 case 'H':
350 hash_table_size = atol (optarg);
351 if (hash_table_size == 0)
352 hash_table_size = HASHMAX;
353 break;
355 case 'I':
356 add_include_directory (optarg);
357 break;
359 case 'L':
360 nesting_limit = atoi (optarg);
361 break;
363 case 'P':
364 prefix_all_builtins = 1;
365 break;
367 case 'Q':
368 suppress_warnings = 1;
369 break;
371 case 'R':
372 frozen_file_to_read = optarg;
373 break;
375 #ifdef ENABLE_CHANGEWORD
376 case 'W':
377 user_word_regexp = optarg;
378 break;
379 #endif
381 case 'd':
382 debug_level = debug_decode (optarg);
383 if (debug_level < 0)
385 error (0, 0, "bad debug flags: `%s'", optarg);
386 debug_level = 0;
388 break;
390 case 'e':
391 interactive = 1;
392 break;
394 case 'l':
395 max_debug_argument_length = atoi (optarg);
396 if (max_debug_argument_length <= 0)
397 max_debug_argument_length = 0;
398 break;
400 case 'o':
401 if (!debug_set_output (optarg))
402 error (0, errno, "%s", optarg);
403 break;
405 case 's':
406 sync_output = 1;
407 break;
410 if (show_version)
412 printf ("%s\n", PACKAGE_STRING);
413 fputs ("\
414 Copyright (C) 2006 Free Software Foundation, Inc.\n\
415 This is free software; see the source for copying conditions. There is NO\n\
416 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
418 Written by Rene' Seindal.\n\
419 ", stdout);
421 if (close_stream (stdout) != 0)
422 M4ERROR ((EXIT_FAILURE, errno, "write error"));
423 exit (EXIT_SUCCESS);
426 if (show_help)
427 usage (EXIT_SUCCESS);
429 defines = head;
431 /* Do the basic initialisations. */
433 input_init ();
434 output_init ();
435 symtab_init ();
436 include_env_init ();
438 if (frozen_file_to_read)
439 reload_frozen_state (frozen_file_to_read);
440 else
441 builtin_init ();
443 /* Handle deferred command line macro definitions. Must come after
444 initialisation of the symbol table. */
446 while (defines != NULL)
448 macro_definition *next;
449 char *macro_value;
450 symbol *sym;
452 switch (defines->code)
454 case 'D':
455 macro_value = strchr (defines->macro, '=');
456 if (macro_value == NULL)
457 macro_value = "";
458 else
459 *macro_value++ = '\0';
460 define_user_macro (defines->macro, macro_value, SYMBOL_INSERT);
461 break;
463 case 'U':
464 lookup_symbol (defines->macro, SYMBOL_DELETE);
465 break;
467 case 't':
468 sym = lookup_symbol (defines->macro, SYMBOL_INSERT);
469 SYMBOL_TRACED (sym) = TRUE;
470 break;
472 default:
473 M4ERROR ((warning_status, 0,
474 "INTERNAL ERROR: bad code in deferred arguments"));
475 abort ();
478 next = defines->next;
479 xfree (defines);
480 defines = next;
483 /* Interactive mode means unbuffered output, and interrupts ignored. */
485 if (interactive)
487 signal (SIGINT, SIG_IGN);
488 setbuf (stdout, (char *) NULL);
491 /* Handle the various input files. Each file is pushed on the input,
492 and the input read. Wrapup text is handled separately later. */
494 if (optind == argc)
496 push_file (stdin, "stdin");
497 expand_input ();
499 else
500 for (; optind < argc; optind++)
502 if (strcmp (argv[optind], "-") == 0)
503 push_file (stdin, "stdin");
504 else
506 fp = path_search (argv[optind]);
507 if (fp == NULL)
509 error (0, errno, "%s", argv[optind]);
510 /* Set the status to EXIT_FAILURE, even though we
511 continue to process files after a missing file. */
512 retcode = EXIT_FAILURE;
513 continue;
515 else
516 push_file (fp, argv[optind]);
518 expand_input ();
520 #undef NEXTARG
522 /* Now handle wrapup text. */
524 while (pop_wrapup ())
525 expand_input ();
527 /* Change debug stream back to stderr, to force flushing debug stream and
528 detect any errors it might have encountered. */
529 debug_set_output (NULL);
531 if (frozen_file_to_write)
532 produce_frozen_state (frozen_file_to_write);
533 else
535 make_diversion (0);
536 undivert_all ();
539 if (close_stream (stdout) != 0)
540 M4ERROR ((EXIT_FAILURE, errno, "write error"));
541 exit (retcode);