Imported Upstream version 1.4.17
[platform/upstream/m4.git] / src / m4.h
1 /* GNU m4 -- A simple macro processor
2
3    Copyright (C) 1989-1994, 2004-2013 Free Software Foundation, Inc.
4
5    This file is part of GNU M4.
6
7    GNU M4 is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11
12    GNU M4 is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /* We use <config.h> instead of "config.h" so that a compilation
22    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
23    (which it would do because it found this file in $srcdir).  */
24
25 #include <config.h>
26
27 #include <assert.h>
28 #include <ctype.h>
29 #include <errno.h>
30 #include <limits.h>
31 #include <stdbool.h>
32 #include <stdint.h>
33 #include <string.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36
37 #include "binary-io.h"
38 #include "clean-temp.h"
39 #include "cloexec.h"
40 #include "close-stream.h"
41 #include "closein.h"
42 #include "dirname.h"
43 #include "error.h"
44 #include "exitfail.h"
45 #include "filenamecat.h"
46 #include "obstack.h"
47 #include "stdio--.h"
48 #include "stdlib--.h"
49 #include "unistd--.h"
50 #include "verror.h"
51 #include "xalloc.h"
52 #include "xprintf.h"
53 #include "xvasprintf.h"
54
55 /* Canonicalize UNIX recognition macros.  */
56 #if defined unix || defined __unix || defined __unix__ \
57   || defined _POSIX_VERSION || defined _POSIX2_VERSION \
58   || defined __NetBSD__ || defined __OpenBSD__ \
59   || defined __APPLE__ || defined __APPLE_CC__
60 # define UNIX 1
61 #endif
62
63 /* Canonicalize Windows recognition macros.  */
64 #if (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__
65 # define W32_NATIVE 1
66 #endif
67
68 /* Canonicalize OS/2 recognition macro.  */
69 #ifdef __EMX__
70 # define OS2 1
71 # undef UNIX
72 #endif
73
74 /* Used if any programmer error is detected (not possible, right?)  */
75 #define EXIT_INTERNAL_ERROR 2
76
77 /* Used for version mismatch, when -R detects a frozen file it can't parse.  */
78 #define EXIT_MISMATCH 63
79
80 /* No-op, for future gettext compatibility.  */
81 #define _(ARG) ARG
82 \f
83 /* Various declarations.  */
84
85 struct string
86   {
87     char *string;               /* characters of the string */
88     size_t length;              /* length of the string */
89   };
90 typedef struct string STRING;
91
92 /* Memory allocation.  */
93 #define obstack_chunk_alloc     xmalloc
94 #define obstack_chunk_free      free
95
96 /* Those must come first.  */
97 typedef struct token_data token_data;
98 typedef void builtin_func (struct obstack *, int, token_data **);
99
100 /* Gnulib's stdbool doesn't work with bool bitfields.  For nicer
101    debugging, use bool when we know it works, but use the more
102    portable unsigned int elsewhere.  */
103 #if __GNUC__ > 2
104 typedef bool bool_bitfield;
105 #else
106 typedef unsigned int bool_bitfield;
107 #endif /* ! __GNUC__ */
108
109 /* Take advantage of GNU C compiler source level optimization hints,
110    using portable macros.  */
111 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 6)
112 # define M4_GNUC_ATTRIBUTE(args)        __attribute__ (args)
113 #else
114 # define M4_GNUC_ATTRIBUTE(args)
115 #endif  /* __GNUC__ */
116
117 #define M4_GNUC_UNUSED          M4_GNUC_ATTRIBUTE ((__unused__))
118 #define M4_GNUC_PRINTF(fmt, arg)                        \
119   M4_GNUC_ATTRIBUTE ((__format__ (__printf__, fmt, arg)))
120 #define M4_GNUC_NORETURN        M4_GNUC_ATTRIBUTE ((__noreturn__))
121 #define M4_GNUC_PURE            M4_GNUC_ATTRIBUTE ((__pure__))
122 \f
123 /* File: m4.c  --- global definitions.  */
124
125 /* Option flags.  */
126 extern int sync_output;                 /* -s */
127 extern int debug_level;                 /* -d */
128 extern size_t hash_table_size;          /* -H */
129 extern int no_gnu_extensions;           /* -G */
130 extern int prefix_all_builtins;         /* -P */
131 extern int max_debug_argument_length;   /* -l */
132 extern int suppress_warnings;           /* -Q */
133 extern int warning_status;              /* -E */
134 extern int nesting_limit;               /* -L */
135 #ifdef ENABLE_CHANGEWORD
136 extern const char *user_word_regexp;    /* -W */
137 #endif
138
139 /* Error handling.  */
140 extern int retcode;
141
142 extern void m4_error (int, int, const char *, ...) M4_GNUC_PRINTF(3, 4);
143 extern void m4_error_at_line (int, int, const char *, int,
144                               const char *, ...) M4_GNUC_PRINTF(5, 6);
145
146 #define M4ERROR(Arglist) (m4_error Arglist)
147 #define M4ERROR_AT_LINE(Arglist) (m4_error_at_line Arglist)
148
149 \f
150 /* File: debug.c  --- debugging and tracing function.  */
151
152 extern FILE *debug;
153
154 /* The value of debug_level is a bitmask of the following.  */
155
156 /* a: show arglist in trace output */
157 #define DEBUG_TRACE_ARGS 1
158 /* e: show expansion in trace output */
159 #define DEBUG_TRACE_EXPANSION 2
160 /* q: quote args and expansion in trace output */
161 #define DEBUG_TRACE_QUOTE 4
162 /* t: trace all macros -- overrides trace{on,off} */
163 #define DEBUG_TRACE_ALL 8
164 /* l: add line numbers to trace output */
165 #define DEBUG_TRACE_LINE 16
166 /* f: add file name to trace output */
167 #define DEBUG_TRACE_FILE 32
168 /* p: trace path search of include files */
169 #define DEBUG_TRACE_PATH 64
170 /* c: show macro call before args collection */
171 #define DEBUG_TRACE_CALL 128
172 /* i: trace changes of input files */
173 #define DEBUG_TRACE_INPUT 256
174 /* x: add call id to trace output */
175 #define DEBUG_TRACE_CALLID 512
176
177 /* V: very verbose --  print everything */
178 #define DEBUG_TRACE_VERBOSE 1023
179 /* default flags -- equiv: aeq */
180 #define DEBUG_TRACE_DEFAULT 7
181
182 #define DEBUG_PRINT1(Fmt, Arg1) \
183   do                                                            \
184     {                                                           \
185       if (debug != NULL)                                        \
186         xfprintf (debug, Fmt, Arg1);                            \
187     }                                                           \
188   while (0)
189
190 #define DEBUG_PRINT3(Fmt, Arg1, Arg2, Arg3) \
191   do                                                            \
192     {                                                           \
193       if (debug != NULL)                                        \
194         xfprintf (debug, Fmt, Arg1, Arg2, Arg3);                \
195     }                                                           \
196   while (0)
197
198 #define DEBUG_MESSAGE(Fmt) \
199   do                                                            \
200     {                                                           \
201       if (debug != NULL)                                        \
202         {                                                       \
203           debug_message_prefix ();                              \
204           xfprintf (debug, Fmt);                                \
205           putc ('\n', debug);                                   \
206         }                                                       \
207     }                                                           \
208   while (0)
209
210 #define DEBUG_MESSAGE1(Fmt, Arg1) \
211   do                                                            \
212     {                                                           \
213       if (debug != NULL)                                        \
214         {                                                       \
215           debug_message_prefix ();                              \
216           xfprintf (debug, Fmt, Arg1);                          \
217           putc ('\n', debug);                                   \
218         }                                                       \
219     }                                                           \
220   while (0)
221
222 #define DEBUG_MESSAGE2(Fmt, Arg1, Arg2) \
223   do                                                            \
224     {                                                           \
225       if (debug != NULL)                                        \
226         {                                                       \
227           debug_message_prefix ();                              \
228           xfprintf (debug, Fmt, Arg1, Arg2);                    \
229           putc ('\n', debug);                                   \
230         }                                                       \
231     }                                                           \
232   while (0)
233
234 extern void debug_init (void);
235 extern int debug_decode (const char *);
236 extern void debug_flush_files (void);
237 extern bool debug_set_output (const char *);
238 extern void debug_message_prefix (void);
239
240 extern void trace_prepre (const char *, int);
241 extern void trace_pre (const char *, int, int, token_data **);
242 extern void trace_post (const char *, int, int, const char *);
243 \f
244 /* File: input.c  --- lexical definitions.  */
245
246 /* Various different token types.  */
247 enum token_type
248 {
249   TOKEN_EOF,                    /* end of file */
250   TOKEN_STRING,                 /* a quoted string or comment */
251   TOKEN_WORD,                   /* an identifier */
252   TOKEN_OPEN,                   /* ( */
253   TOKEN_COMMA,                  /* , */
254   TOKEN_CLOSE,                  /* ) */
255   TOKEN_SIMPLE,                 /* any other single character */
256   TOKEN_MACDEF                  /* a macro's definition (see "defn") */
257 };
258
259 /* The data for a token, a macro argument, and a macro definition.  */
260 enum token_data_type
261 {
262   TOKEN_VOID,
263   TOKEN_TEXT,
264   TOKEN_FUNC
265 };
266
267 struct token_data
268 {
269   enum token_data_type type;
270   union
271     {
272       struct
273         {
274           char *text;
275 #ifdef ENABLE_CHANGEWORD
276           char *original_text;
277 #endif
278         }
279       u_t;
280       builtin_func *func;
281     }
282   u;
283 };
284
285 #define TOKEN_DATA_TYPE(Td)             ((Td)->type)
286 #define TOKEN_DATA_TEXT(Td)             ((Td)->u.u_t.text)
287 #ifdef ENABLE_CHANGEWORD
288 # define TOKEN_DATA_ORIG_TEXT(Td)       ((Td)->u.u_t.original_text)
289 #endif
290 #define TOKEN_DATA_FUNC(Td)             ((Td)->u.func)
291
292 typedef enum token_type token_type;
293 typedef enum token_data_type token_data_type;
294
295 extern void input_init (void);
296 extern token_type peek_token (void);
297 extern token_type next_token (token_data *, int *);
298 extern void skip_line (void);
299
300 /* push back input */
301 extern void push_file (FILE *, const char *, bool);
302 extern void push_macro (builtin_func *);
303 extern struct obstack *push_string_init (void);
304 extern const char *push_string_finish (void);
305 extern void push_wrapup (const char *);
306 extern bool pop_wrapup (void);
307
308 /* current input file, and line */
309 extern const char *current_file;
310 extern int current_line;
311
312 /* left and right quote, begin and end comment */
313 extern STRING bcomm;
314 extern STRING ecomm;
315 extern STRING lquote;
316 extern STRING rquote;
317
318 #define DEF_LQUOTE "`"
319 #define DEF_RQUOTE "\'"
320 #define DEF_BCOMM "#"
321 #define DEF_ECOMM "\n"
322
323 extern void set_quotes (const char *, const char *);
324 extern void set_comment (const char *, const char *);
325 #ifdef ENABLE_CHANGEWORD
326 extern void set_word_regexp (const char *);
327 #endif
328 \f
329 /* File: output.c --- output functions.  */
330 extern int current_diversion;
331 extern int output_current_line;
332
333 extern void output_init (void);
334 extern void output_exit (void);
335 extern void output_text (const char *, int);
336 extern void shipout_text (struct obstack *, const char *, int, int);
337 extern void make_diversion (int);
338 extern void insert_diversion (int);
339 extern void insert_file (FILE *);
340 extern void freeze_diversions (FILE *);
341 \f
342 /* File symtab.c  --- symbol table definitions.  */
343
344 /* Operation modes for lookup_symbol ().  */
345 enum symbol_lookup
346 {
347   SYMBOL_LOOKUP,
348   SYMBOL_INSERT,
349   SYMBOL_DELETE,
350   SYMBOL_PUSHDEF,
351   SYMBOL_POPDEF
352 };
353
354 /* Symbol table entry.  */
355 struct symbol
356 {
357   struct symbol *next;
358   bool_bitfield traced : 1;
359   bool_bitfield shadowed : 1;
360   bool_bitfield macro_args : 1;
361   bool_bitfield blind_no_args : 1;
362   bool_bitfield deleted : 1;
363   int pending_expansions;
364
365   char *name;
366   token_data data;
367 };
368
369 #define SYMBOL_NEXT(S)          ((S)->next)
370 #define SYMBOL_TRACED(S)        ((S)->traced)
371 #define SYMBOL_SHADOWED(S)      ((S)->shadowed)
372 #define SYMBOL_MACRO_ARGS(S)    ((S)->macro_args)
373 #define SYMBOL_BLIND_NO_ARGS(S) ((S)->blind_no_args)
374 #define SYMBOL_DELETED(S)       ((S)->deleted)
375 #define SYMBOL_PENDING_EXPANSIONS(S) ((S)->pending_expansions)
376 #define SYMBOL_NAME(S)          ((S)->name)
377 #define SYMBOL_TYPE(S)          (TOKEN_DATA_TYPE (&(S)->data))
378 #define SYMBOL_TEXT(S)          (TOKEN_DATA_TEXT (&(S)->data))
379 #define SYMBOL_FUNC(S)          (TOKEN_DATA_FUNC (&(S)->data))
380
381 typedef enum symbol_lookup symbol_lookup;
382 typedef struct symbol symbol;
383 typedef void hack_symbol (symbol *, void *);
384
385 #define HASHMAX 509             /* default, overridden by -Hsize */
386
387 extern symbol **symtab;
388
389 extern void free_symbol (symbol *sym);
390 extern void symtab_init (void);
391 extern symbol *lookup_symbol (const char *, symbol_lookup);
392 extern void hack_all_symbols (hack_symbol *, void *);
393 \f
394 /* File: macro.c  --- macro expansion.  */
395
396 extern int expansion_level;
397
398 extern void expand_input (void);
399 extern void call_macro (symbol *, int, token_data **, struct obstack *);
400 \f
401 /* File: builtin.c  --- builtins.  */
402
403 struct builtin
404 {
405   const char *name;
406   bool_bitfield gnu_extension : 1;
407   bool_bitfield groks_macro_args : 1;
408   bool_bitfield blind_if_no_args : 1;
409   builtin_func *func;
410 };
411
412 struct predefined
413 {
414   const char *unix_name;
415   const char *gnu_name;
416   const char *func;
417 };
418
419 typedef struct builtin builtin;
420 typedef struct predefined predefined;
421 struct re_pattern_buffer;
422 struct re_registers;
423
424 /* The default sequence detects multi-digit parameters (obsolete after
425    1.4.x), and any use of extended arguments with the default ${}
426    syntax (new in 2.0).  */
427 #define DEFAULT_MACRO_SEQUENCE "\\$\\({[^}]*}\\|[0-9][0-9]+\\)"
428
429 extern void builtin_init (void);
430 extern void define_builtin (const char *, const builtin *, symbol_lookup);
431 extern void set_macro_sequence (const char *);
432 extern void free_macro_sequence (void);
433 extern void define_user_macro (const char *, const char *, symbol_lookup);
434 extern void undivert_all (void);
435 extern void expand_user_macro (struct obstack *, symbol *, int, token_data **);
436 extern void m4_placeholder (struct obstack *, int, token_data **);
437 extern void init_pattern_buffer (struct re_pattern_buffer *,
438                                  struct re_registers *);
439 extern const char *ntoa (int32_t, int);
440
441 extern const builtin *find_builtin_by_addr (builtin_func *);
442 extern const builtin *find_builtin_by_name (const char *);
443 \f
444 /* File: path.c  --- path search for include files.  */
445
446 extern void include_init (void);
447 extern void include_env_init (void);
448 extern void add_include_directory (const char *);
449 extern FILE *m4_path_search (const char *, char **);
450 \f
451 /* File: eval.c  --- expression evaluation.  */
452
453 extern bool evaluate (const char *, int32_t *);
454 \f
455 /* File: format.c  --- printf like formatting.  */
456
457 extern void expand_format (struct obstack *, int, token_data **);
458 \f
459 /* File: freeze.c --- frozen state files.  */
460
461 extern void produce_frozen_state (const char *);
462 extern void reload_frozen_state (const char *);
463 \f
464 /* Debugging the memory allocator.  */
465
466 #ifdef WITH_DMALLOC
467 # define DMALLOC_FUNC_CHECK
468 # include <dmalloc.h>
469 #endif
470
471 /* Other debug stuff.  */
472
473 #ifdef DEBUG
474 # define DEBUG_INCL   1
475 # define DEBUG_INPUT  1
476 # define DEBUG_MACRO  1
477 # define DEBUG_OUTPUT 1
478 # define DEBUG_STKOVF 1
479 # define DEBUG_SYM    1
480 #endif
481
482 /* Convert a possibly-signed character to an unsigned character.  This is
483    a bit safer than casting to unsigned char, since it catches some type
484    errors that the cast doesn't.  */
485 #if HAVE_INLINE
486 static inline unsigned char to_uchar (char ch) { return ch; }
487 #else
488 # define to_uchar(C) ((unsigned char) (C))
489 #endif
490
491 /* Avoid negative logic when comparing two strings.  */
492 #define STREQ(a, b) (strcmp (a, b) == 0)