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
27 static void usage (int);
29 /* Operate interactively (-e). */
30 static int interactive
= 0;
32 /* Enable sync output for /lib/cpp (-s). */
35 /* Debug (-d[flags]). */
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
= "";
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 */
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 `-------------------------------------------------------------------------*/
95 reference_error (void)
99 fprintf (stderr
, "%s:%d: ", current_file
, current_line
);
105 /*---------------------------------------.
106 | Tell user stack overflowed and abort. |
107 `---------------------------------------*/
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 `------------------------*/
136 /*---------------------------------------------.
137 | Print a usage message and exit with STATUS. |
138 `---------------------------------------------*/
143 if (status
!= EXIT_SUCCESS
)
144 fprintf (stderr
, "Try `%s --help' for more information.\n", program_name
);
147 printf ("Usage: %s [OPTION]... [FILE]...\n", program_name
);
149 Process macros in FILEs. If no FILE or if FILE is `-', standard input\n\
154 Mandatory or optional arguments to long options are mandatory or optional\n\
155 for short options too.\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\
165 #ifdef ENABLE_CHANGEWORD
167 -W, --word-regexp=REGEXP use REGEXP for macro name syntax\n\
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\
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\
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\
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\
202 a show actual arguments\n\
203 c show before collect, after collect and after call\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\
216 If defined, the environment variable `M4PATH' is a colon-separated list\n\
217 of directories included after any specified by `-I'.\n\
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\
224 printf ("\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT
);
227 if (close_stream (stdout
) != 0)
228 M4ERROR ((EXIT_FAILURE
, errno
, "write error"));
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' },
267 /* Global catchall for any errors that should affect final error status, but
268 where we try to continue execution in the meantime. */
271 #ifdef ENABLE_CHANGEWORD
272 #define OPTSTRING "B:D:EF:GH:I:L:N:PQR:S:T:U:W:d::el:o:st:"
274 #define OPTSTRING "B:D:EF:GH:I:L:N:PQR:S:T:U:d::el:o:st:"
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
;
288 program_name
= argv
[0];
289 retcode
= EXIT_SUCCESS
;
294 setup_stackovf_trap (argv
, envp
, stackovf_handler
);
297 /* First, we decode the arguments, to size up tables and stuff. */
301 while (optchar
= getopt_long (argc
, (char **) argv
, OPTSTRING
,
307 usage (EXIT_FAILURE
);
312 case 'B': /* compatibility junk */
322 /* Arguments that cannot be handled until later are accumulated. */
324 new = (macro_definition
*) xmalloc (sizeof (macro_definition
));
338 warning_status
= EXIT_FAILURE
;
342 frozen_file_to_write
= optarg
;
346 no_gnu_extensions
= 1;
350 hash_table_size
= atol (optarg
);
351 if (hash_table_size
== 0)
352 hash_table_size
= HASHMAX
;
356 add_include_directory (optarg
);
360 nesting_limit
= atoi (optarg
);
364 prefix_all_builtins
= 1;
368 suppress_warnings
= 1;
372 frozen_file_to_read
= optarg
;
375 #ifdef ENABLE_CHANGEWORD
377 user_word_regexp
= optarg
;
382 debug_level
= debug_decode (optarg
);
385 error (0, 0, "bad debug flags: `%s'", optarg
);
395 max_debug_argument_length
= atoi (optarg
);
396 if (max_debug_argument_length
<= 0)
397 max_debug_argument_length
= 0;
401 if (!debug_set_output (optarg
))
402 error (0, errno
, "%s", optarg
);
412 printf ("%s\n", PACKAGE_STRING
);
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\
421 if (close_stream (stdout
) != 0)
422 M4ERROR ((EXIT_FAILURE
, errno
, "write error"));
427 usage (EXIT_SUCCESS
);
431 /* Do the basic initialisations. */
438 if (frozen_file_to_read
)
439 reload_frozen_state (frozen_file_to_read
);
443 /* Handle deferred command line macro definitions. Must come after
444 initialisation of the symbol table. */
446 while (defines
!= NULL
)
448 macro_definition
*next
;
452 switch (defines
->code
)
455 macro_value
= strchr (defines
->macro
, '=');
456 if (macro_value
== NULL
)
459 *macro_value
++ = '\0';
460 define_user_macro (defines
->macro
, macro_value
, SYMBOL_INSERT
);
464 lookup_symbol (defines
->macro
, SYMBOL_DELETE
);
468 sym
= lookup_symbol (defines
->macro
, SYMBOL_INSERT
);
469 SYMBOL_TRACED (sym
) = TRUE
;
473 M4ERROR ((warning_status
, 0,
474 "INTERNAL ERROR: bad code in deferred arguments"));
478 next
= defines
->next
;
483 /* Interactive mode means unbuffered output, and interrupts ignored. */
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. */
496 push_file (stdin
, "stdin");
500 for (; optind
< argc
; optind
++)
502 if (strcmp (argv
[optind
], "-") == 0)
503 push_file (stdin
, "stdin");
506 fp
= path_search (argv
[optind
]);
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
;
516 push_file (fp
, argv
[optind
]);
522 /* Now handle wrapup text. */
524 while (pop_wrapup ())
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
);
539 if (close_stream (stdout
) != 0)
540 M4ERROR ((EXIT_FAILURE
, errno
, "write error"));