* src/m4.c (main): Check for errors when closing stdin.
[m4.git] / src / m4.h
blob809da2ec15cfa32799bd27856647e71f656dc911
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 /* We use <config.h> instead of "config.h" so that a compilation
23 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
24 (which it would do because it found this file in $srcdir). */
26 #include <config.h>
28 #include <ctype.h>
29 #include <errno.h>
30 #include <stdbool.h>
31 #include <string.h>
32 #include <sys/types.h>
34 #include "binary-io.h"
35 #include "clean-temp.h"
36 #include "cloexec.h"
37 #include "close-stream.h"
38 #include "closeout.h"
39 #include "error.h"
40 #include "exit.h"
41 #include "exitfail.h"
42 #include "obstack.h"
43 #include "stdio--.h"
44 #include "stdlib--.h"
45 #include "unistd--.h"
46 #include "verror.h"
47 #include "xalloc.h"
48 #include "xvasprintf.h"
50 /* Canonicalize UNIX recognition macros. */
51 #if defined unix || defined __unix || defined __unix__ \
52 || defined _POSIX_VERSION || defined _POSIX2_VERSION \
53 || defined __NetBSD__ || defined __OpenBSD__ \
54 || defined __APPLE__ || defined __APPLE_CC__
55 # define UNIX 1
56 #endif
58 /* Canonicalize Windows recognition macros. */
59 #if (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__
60 # define W32_NATIVE 1
61 #endif
63 /* Canonicalize OS/2 recognition macro. */
64 #ifdef __EMX__
65 # define OS2 1
66 #endif
68 /* Used for version mismatch, when -R detects a frozen file it can't parse. */
69 #define EXIT_MISMATCH 63
71 /* Various declarations. */
73 struct string
75 char *string; /* characters of the string */
76 size_t length; /* length of the string */
78 typedef struct string STRING;
80 /* Memory allocation. */
81 #define obstack_chunk_alloc xmalloc
82 #define obstack_chunk_free free
84 /* Those must come first. */
85 typedef struct token_data token_data;
86 typedef void builtin_func (struct obstack *, int, token_data **);
88 /* Take advantage of GNU C compiler source level optimization hints,
89 using portable macros. */
90 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 6)
91 # define M4_GNUC_ATTRIBUTE(args) __attribute__(args)
92 #else
93 # define M4_GNUC_ATTRIBUTE(args)
94 #endif /* __GNUC__ */
96 #define M4_GNUC_UNUSED M4_GNUC_ATTRIBUTE((__unused__))
97 #define M4_GNUC_PRINTF(fmt, arg) \
98 M4_GNUC_ATTRIBUTE((__format__ (__printf__, fmt, arg)))
100 /* File: m4.c --- global definitions. */
102 /* Option flags. */
103 extern int sync_output; /* -s */
104 extern int debug_level; /* -d */
105 extern size_t hash_table_size; /* -H */
106 extern int no_gnu_extensions; /* -G */
107 extern int prefix_all_builtins; /* -P */
108 extern int max_debug_argument_length; /* -l */
109 extern int suppress_warnings; /* -Q */
110 extern int warning_status; /* -E */
111 extern int nesting_limit; /* -L */
112 #ifdef ENABLE_CHANGEWORD
113 extern const char *user_word_regexp; /* -W */
114 #endif
116 /* Error handling. */
117 extern int retcode;
118 extern const char *program_name;
120 void m4_error (int, int, const char *, ...) M4_GNUC_PRINTF(3, 4);
121 void m4_error_at_line (int, int, const char *, int,
122 const char *, ...) M4_GNUC_PRINTF(5, 6);
124 #define M4ERROR(Arglist) (m4_error Arglist)
125 #define M4ERROR_AT_LINE(Arglist) (m4_error_at_line Arglist)
127 #ifdef USE_STACKOVF
128 void setup_stackovf_trap (char *const *, char *const *,
129 void (*handler) (void));
130 #endif
132 /* File: debug.c --- debugging and tracing function. */
134 extern FILE *debug;
136 /* The value of debug_level is a bitmask of the following. */
138 /* a: show arglist in trace output */
139 #define DEBUG_TRACE_ARGS 1
140 /* e: show expansion in trace output */
141 #define DEBUG_TRACE_EXPANSION 2
142 /* q: quote args and expansion in trace output */
143 #define DEBUG_TRACE_QUOTE 4
144 /* t: trace all macros -- overrides trace{on,off} */
145 #define DEBUG_TRACE_ALL 8
146 /* l: add line numbers to trace output */
147 #define DEBUG_TRACE_LINE 16
148 /* f: add file name to trace output */
149 #define DEBUG_TRACE_FILE 32
150 /* p: trace path search of include files */
151 #define DEBUG_TRACE_PATH 64
152 /* c: show macro call before args collection */
153 #define DEBUG_TRACE_CALL 128
154 /* i: trace changes of input files */
155 #define DEBUG_TRACE_INPUT 256
156 /* x: add call id to trace output */
157 #define DEBUG_TRACE_CALLID 512
159 /* V: very verbose -- print everything */
160 #define DEBUG_TRACE_VERBOSE 1023
161 /* default flags -- equiv: aeq */
162 #define DEBUG_TRACE_DEFAULT 7
164 #define DEBUG_PRINT1(Fmt, Arg1) \
165 do \
167 if (debug != NULL) \
168 fprintf (debug, Fmt, Arg1); \
170 while (0)
172 #define DEBUG_PRINT3(Fmt, Arg1, Arg2, Arg3) \
173 do \
175 if (debug != NULL) \
176 fprintf (debug, Fmt, Arg1, Arg2, Arg3); \
178 while (0)
180 #define DEBUG_MESSAGE(Fmt) \
181 do \
183 if (debug != NULL) \
185 debug_message_prefix (); \
186 fprintf (debug, Fmt); \
187 putc ('\n', debug); \
190 while (0)
192 #define DEBUG_MESSAGE1(Fmt, Arg1) \
193 do \
195 if (debug != NULL) \
197 debug_message_prefix (); \
198 fprintf (debug, Fmt, Arg1); \
199 putc ('\n', debug); \
202 while (0)
204 #define DEBUG_MESSAGE2(Fmt, Arg1, Arg2) \
205 do \
207 if (debug != NULL) \
209 debug_message_prefix (); \
210 fprintf (debug, Fmt, Arg1, Arg2); \
211 putc ('\n', debug); \
214 while (0)
216 void debug_init (void);
217 int debug_decode (const char *);
218 void debug_flush_files (void);
219 bool debug_set_output (const char *);
220 void debug_message_prefix (void);
222 void trace_prepre (const char *, int);
223 void trace_pre (const char *, int, int, token_data **);
224 void trace_post (const char *, int, int, token_data **, const char *);
226 /* File: input.c --- lexical definitions. */
228 /* Various different token types. */
229 enum token_type
231 TOKEN_EOF, /* end of file */
232 TOKEN_STRING, /* a quoted string or comment */
233 TOKEN_WORD, /* an identifier */
234 TOKEN_OPEN, /* ( */
235 TOKEN_COMMA, /* , */
236 TOKEN_CLOSE, /* ) */
237 TOKEN_SIMPLE, /* any other single character */
238 TOKEN_MACDEF /* a macro's definition (see "defn") */
241 /* The data for a token, a macro argument, and a macro definition. */
242 enum token_data_type
244 TOKEN_VOID,
245 TOKEN_TEXT,
246 TOKEN_FUNC
249 struct token_data
251 enum token_data_type type;
252 union
254 struct
256 char *text;
257 #ifdef ENABLE_CHANGEWORD
258 char *original_text;
259 #endif
261 u_t;
262 builtin_func *func;
267 #define TOKEN_DATA_TYPE(Td) ((Td)->type)
268 #define TOKEN_DATA_TEXT(Td) ((Td)->u.u_t.text)
269 #ifdef ENABLE_CHANGEWORD
270 # define TOKEN_DATA_ORIG_TEXT(Td) ((Td)->u.u_t.original_text)
271 #endif
272 #define TOKEN_DATA_FUNC(Td) ((Td)->u.func)
274 typedef enum token_type token_type;
275 typedef enum token_data_type token_data_type;
277 void input_init (void);
278 token_type peek_token (void);
279 token_type next_token (token_data *);
280 void skip_line (void);
282 /* push back input */
283 void push_file (FILE *, const char *, bool);
284 void push_macro (builtin_func *);
285 struct obstack *push_string_init (void);
286 const char *push_string_finish (void);
287 void push_wrapup (const char *);
288 bool pop_wrapup (void);
290 /* current input file, and line */
291 extern const char *current_file;
292 extern int current_line;
294 /* left and right quote, begin and end comment */
295 extern STRING bcomm, ecomm;
296 extern STRING lquote, rquote;
298 #define DEF_LQUOTE "`"
299 #define DEF_RQUOTE "\'"
300 #define DEF_BCOMM "#"
301 #define DEF_ECOMM "\n"
303 void set_quotes (const char *, const char *);
304 void set_comment (const char *, const char *);
305 #ifdef ENABLE_CHANGEWORD
306 void set_word_regexp (const char *);
307 #endif
309 /* File: output.c --- output functions. */
310 extern int current_diversion;
311 extern int output_current_line;
313 void output_init (void);
314 void output_exit (void);
315 void shipout_text (struct obstack *, const char *, int);
316 void make_diversion (int);
317 void insert_diversion (int);
318 void insert_file (FILE *);
319 void freeze_diversions (FILE *);
321 /* File symtab.c --- symbol table definitions. */
323 /* Operation modes for lookup_symbol (). */
324 enum symbol_lookup
326 SYMBOL_LOOKUP,
327 SYMBOL_INSERT,
328 SYMBOL_DELETE,
329 SYMBOL_PUSHDEF,
330 SYMBOL_POPDEF
333 /* Symbol table entry. */
334 struct symbol
336 struct symbol *next;
337 bool traced : 1;
338 bool shadowed : 1;
339 bool macro_args : 1;
340 bool blind_no_args : 1;
341 bool deleted : 1;
342 int pending_expansions;
344 char *name;
345 token_data data;
348 #define SYMBOL_NEXT(S) ((S)->next)
349 #define SYMBOL_TRACED(S) ((S)->traced)
350 #define SYMBOL_SHADOWED(S) ((S)->shadowed)
351 #define SYMBOL_MACRO_ARGS(S) ((S)->macro_args)
352 #define SYMBOL_BLIND_NO_ARGS(S) ((S)->blind_no_args)
353 #define SYMBOL_DELETED(S) ((S)->deleted)
354 #define SYMBOL_PENDING_EXPANSIONS(S) ((S)->pending_expansions)
355 #define SYMBOL_NAME(S) ((S)->name)
356 #define SYMBOL_TYPE(S) (TOKEN_DATA_TYPE (&(S)->data))
357 #define SYMBOL_TEXT(S) (TOKEN_DATA_TEXT (&(S)->data))
358 #define SYMBOL_FUNC(S) (TOKEN_DATA_FUNC (&(S)->data))
360 typedef enum symbol_lookup symbol_lookup;
361 typedef struct symbol symbol;
362 typedef void hack_symbol (symbol *, void *);
364 #define HASHMAX 509 /* default, overridden by -Hsize */
366 extern symbol **symtab;
368 void free_symbol (symbol *sym);
369 void symtab_init (void);
370 symbol *lookup_symbol (const char *, symbol_lookup);
371 void hack_all_symbols (hack_symbol *, void *);
373 /* File: macro.c --- macro expansion. */
375 void expand_input (void);
376 void call_macro (symbol *, int, token_data **, struct obstack *);
378 /* File: builtin.c --- builtins. */
380 struct builtin
382 const char *name;
383 bool gnu_extension : 1;
384 bool groks_macro_args : 1;
385 bool blind_if_no_args : 1;
386 builtin_func *func;
389 struct predefined
391 const char *unix_name;
392 const char *gnu_name;
393 const char *func;
396 typedef struct builtin builtin;
397 typedef struct predefined predefined;
399 void builtin_init (void);
400 void define_builtin (const char *, const builtin *, symbol_lookup);
401 void define_user_macro (const char *, const char *, symbol_lookup);
402 void undivert_all (void);
403 void expand_user_macro (struct obstack *, symbol *, int, token_data **);
404 void m4_placeholder (struct obstack *, int, token_data **);
406 const builtin *find_builtin_by_addr (builtin_func *);
407 const builtin *find_builtin_by_name (const char *);
409 /* File: path.c --- path search for include files. */
411 void include_init (void);
412 void include_env_init (void);
413 void add_include_directory (const char *);
414 FILE *m4_path_search (const char *, char **);
416 /* File: eval.c --- expression evaluation. */
418 /* eval_t and unsigned_eval_t should be at least 32 bits. */
419 typedef int eval_t;
420 typedef unsigned int unsigned_eval_t;
422 bool evaluate (const char *, eval_t *);
424 /* File: format.c --- printf like formatting. */
426 void format (struct obstack *, int, token_data **);
428 /* File: freeze.c --- frozen state files. */
430 void produce_frozen_state (const char *);
431 void reload_frozen_state (const char *);
433 /* Debugging the memory allocator. */
435 #ifdef WITH_DMALLOC
436 # define DMALLOC_FUNC_CHECK
437 # include <dmalloc.h>
438 #endif
440 /* Other debug stuff. */
442 #ifdef DEBUG
443 # define DEBUG_INCL 1
444 # define DEBUG_INPUT 1
445 # define DEBUG_MACRO 1
446 # define DEBUG_OUTPUT 1
447 # define DEBUG_STKOVF 1
448 # define DEBUG_SYM 1
449 #endif
451 /* Convert a possibly-signed character to an unsigned character. This is
452 a bit safer than casting to unsigned char, since it catches some type
453 errors that the cast doesn't. */
454 #if HAVE_INLINE
455 static inline unsigned char to_uchar (char ch) { return ch; }
456 #else
457 # define to_uchar(C) ((unsigned char) (C))
458 #endif