maint: bump copyright year
[m4.git] / m4 / builtin.c
blob19db0872542d6c7d143c9371fe15a587281231c5
1 /* GNU m4 -- A simple macro processor
2 Copyright (C) 1989-1994, 1999-2000, 2005-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 /* Code for all builtin macros, initialisation of symbol table, and
22 expansion of user defined macros. */
24 #include <config.h>
26 #include "m4private.h"
28 /* Comparison function, for use in bsearch, which compares NAME
29 against the name of BUILTIN. */
30 static int
31 compare_builtin_name_CB (const void *name, const void *b)
33 const m4__builtin *builtin = (const m4__builtin *) b;
34 return strcmp ((const char *) name, builtin->builtin.name);
37 /* Find the builtin which has NAME. If MODULE is not NULL, then
38 search only in MODULE's builtin table. The result is a malloc'd
39 symbol value, suitable for use in the symbol table or for an
40 argument to m4_push_builtin. */
41 m4_symbol_value * M4_GNUC_PURE
42 m4_builtin_find_by_name (m4 *context, m4_module *module, const char *name)
44 m4_module *cur = module ? module : m4_module_next (context, NULL);
45 m4__builtin *bp;
49 bp = (m4__builtin *) bsearch (name, cur->builtins, cur->builtins_len,
50 sizeof *bp, compare_builtin_name_CB);
51 if (bp)
53 m4_symbol_value *token = (m4_symbol_value *) xzalloc (sizeof *token);
54 m4__set_symbol_value_builtin (token, bp);
55 return token;
58 while (!module && (cur = m4_module_next (context, cur)));
60 return NULL;
63 /* Find the builtin which has FUNC. If MODULE argument is supplied
64 then search only in MODULE's builtin table. The result is a
65 malloc'd symbol value, suitable for use in the symbol table or for
66 an argument to m4_push_builtin. */
67 m4_symbol_value * M4_GNUC_PURE
68 m4_builtin_find_by_func (m4 *context, m4_module *module, m4_builtin_func *func)
70 m4_module *cur = module ? module : m4_module_next (context, NULL);
71 size_t i;
75 for (i = 0; i < cur->builtins_len; i++)
76 if (cur->builtins[i].builtin.func == func)
78 m4_symbol_value *token =
79 (m4_symbol_value *) xzalloc (sizeof *token);
80 m4__set_symbol_value_builtin (token, &cur->builtins[i]);
81 return token;
84 while (!module && (cur = m4_module_next (context, cur)));
86 return 0;
89 /* Print a representation of FUNC to OBS, optionally including the
90 MODULE it came from. If FLATTEN, output QUOTES around an empty
91 string; if CHAIN, append the builtin to the chain; otherwise print
92 the name of FUNC. */
93 void
94 m4__builtin_print (m4_obstack *obs, const m4__builtin *func, bool flatten,
95 m4__symbol_chain **chain, const m4_string_pair *quotes,
96 bool module)
98 assert (func);
99 if (flatten)
101 if (quotes)
103 obstack_grow (obs, quotes->str1, quotes->len1);
104 obstack_grow (obs, quotes->str2, quotes->len2);
106 module = false;
108 else if (chain)
109 m4__append_builtin (obs, func, NULL, chain);
110 else
112 obstack_1grow (obs, '<');
113 obstack_grow (obs, func->builtin.name, strlen (func->builtin.name));
114 obstack_1grow (obs, '>');
116 if (module)
118 const char *text = m4_get_module_name (func->module);
119 obstack_1grow (obs, '{');
120 obstack_grow (obs, text, strlen (text));
121 obstack_1grow (obs, '}');