maint: summarize highlights of 1.4.18 release
[m4/ericb.git] / m4 / m4.c
blob0bf185ce1b5eb73c3e7fc1e655d84eb5549bd50a
1 /* GNU m4 -- A simple macro processor
2 Copyright (C) 1989-1994, 2001, 2004, 2006-2008, 2010, 2013-2014, 2017
3 Free Software Foundation, Inc.
5 This file is part of GNU M4.
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.
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.
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/>.
21 #include <config.h>
23 #include "bitrotate.h"
24 #include "m4private.h"
26 #define DEFAULT_NESTING_LIMIT 1024
27 #define DEFAULT_NAMEMAP_SIZE 61
29 static size_t
30 hashfn (const void *ptr)
32 const char *s = (const char *) ptr;
33 size_t val = DEFAULT_NAMEMAP_SIZE;
34 while (*s)
35 val = rotl_sz (val, 7) + to_uchar (*s++);
36 return val;
40 m4 *
41 m4_create (void)
43 m4 *context = (m4 *) xzalloc (sizeof *context);
45 context->symtab = m4_symtab_create (0);
46 context->syntax = m4_syntax_create ();
48 context->namemap =
49 m4_hash_new (DEFAULT_NAMEMAP_SIZE, hashfn, (m4_hash_cmp_func *) strcmp);
51 context->debug_file = stderr;
52 obstack_init (&context->trace_messages);
54 context->nesting_limit = DEFAULT_NESTING_LIMIT;
55 context->debug_level = M4_DEBUG_TRACE_INITIAL;
56 context->max_debug_arg_length = SIZE_MAX;
58 context->search_path =
59 (m4__search_path_info *) xzalloc (sizeof *context->search_path);
60 m4__include_init (context);
62 return context;
65 void
66 m4_delete (m4 *context)
68 size_t i;
69 assert (context);
71 if (context->symtab)
72 m4_symtab_delete (context->symtab);
74 if (context->syntax)
75 m4_syntax_delete (context->syntax);
77 /* debug_file should have been reset to stdout or stderr, both of
78 which are closed later. */
79 assert (context->debug_file == stderr || context->debug_file == stdout);
81 obstack_free (&context->trace_messages, NULL);
83 if (context->search_path)
85 m4__search_path *path = context->search_path->list;
87 while (path)
89 m4__search_path *stale = path;
90 path = path->next;
92 DELETE (stale->dir); /* Cast away const. */
93 free (stale);
95 free (context->search_path);
98 for (i = 0; i < context->stacks_count; i++)
100 assert (context->arg_stacks[i].refcount == 0
101 && context->arg_stacks[i].argcount == 0);
102 if (context->arg_stacks[i].args)
104 obstack_free (context->arg_stacks[i].args, NULL);
105 free (context->arg_stacks[i].args);
106 obstack_free (context->arg_stacks[i].argv, NULL);
107 free (context->arg_stacks[i].argv);
110 free (context->arg_stacks);
112 free (context);
117 /* Use the preprocessor to generate the repetitive bit twiddling functions
118 for us. Note the additional paretheses around the expanded function
119 name to protect against macro expansion from the fast macros used to
120 replace these functions when NDEBUG is defined. */
121 #define M4FIELD(type, base, field) \
122 type (CONC(m4_get_, base)) (m4 *context) \
124 assert (context); \
125 return context->field; \
127 m4_context_field_table
128 #undef M4FIELD
130 #define M4FIELD(type, base, field) \
131 type (CONC(m4_set_, base)) (m4 *context, type value) \
133 assert (context); \
134 return context->field = value; \
136 m4_context_field_table
137 #undef M4FIELD
139 #define M4OPT_BIT(bit, base) \
140 bool (CONC(m4_get_, base)) (m4 *context) \
142 assert (context); \
143 return BIT_TEST (context->opt_flags, (bit)); \
145 m4_context_opt_bit_table
146 #undef M4OPT_BIT
148 #define M4OPT_BIT(bit, base) \
149 bool (CONC(m4_set_, base)) (m4 *context, bool value) \
151 assert (context); \
152 if (value) \
153 BIT_SET (context->opt_flags, (bit)); \
154 else \
155 BIT_RESET (context->opt_flags, (bit)); \
156 return value; \
158 m4_context_opt_bit_table
159 #undef M4OPT_BIT