Avoid risk of stack overflow.
[m4.git] / modules / load.c
blobc9437026acbf3f7793e00e012b9dbae07efe05f4
1 /* GNU m4 -- A simple macro processor
2 Copyright (C) 2000, 2005, 2006, 2007, 2008 Free Software
3 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 /* This module needs private symbols, and may not compile correctly if
24 m4private.h isn't included. */
25 #include "m4private.h"
28 /* Rename exported symbols for dlpreload()ing. */
29 #define m4_builtin_table load_LTX_m4_builtin_table
30 #define m4_macro_table load_LTX_m4_macro_table
33 /* Maintain each of the builtins implemented in this modules along
34 with their details in a single table for easy maintenance.
36 function macros blind side minargs maxargs */
37 #define builtin_functions \
38 BUILTIN (load, false, true, false, 1, 1 ) \
39 BUILTIN (m4modules, false, false, false, 0, 0 ) \
40 BUILTIN (refcount, false, true, false, 1, 1 ) \
41 BUILTIN (unload, false, true, false, 1, 1 ) \
44 /* Generate prototypes for each builtin handler function. */
45 #define BUILTIN(handler, macros, blind, side, min, max) M4BUILTIN (handler)
46 builtin_functions
47 #undef BUILTIN
50 /* Generate a table for mapping m4 symbol names to handler functions. */
51 const m4_builtin m4_builtin_table[] =
53 #define BUILTIN(handler, macros, blind, side, min, max) \
54 M4BUILTIN_ENTRY (handler, #handler, macros, blind, side, min, max)
56 builtin_functions
57 #undef BUILTIN
59 { NULL, NULL, 0, 0, 0 },
63 /* A table for mapping m4 symbol names to simple expansion text. */
64 const m4_macro m4_macro_table[] =
66 /* name text min max */
67 { "__load__", "", 0, 0 },
68 { NULL, NULL, 0, 0 },
72 /* This module cannot be safely unloaded from memory, incase the unload
73 is triggered by the unload builtin, and the module is removed while
74 unload is in progress. */
75 M4INIT_HANDLER (load)
77 const char *err = m4_module_makeresident (module);
78 if (err)
79 m4_error (context, 0, 0, NULL, _("cannot make module `%s' resident: %s"),
80 m4_get_module_name (module), err);
85 /* Loading an external module at runtime.
86 The following functions provide the implementation for each
87 of the m4 builtins declared in `m4_builtin_table[]' above. */
89 /**
90 * m4modules()
91 **/
92 M4BUILTIN_HANDLER (m4modules)
94 /* The expansion of this builtin is a comma separated list of
95 loaded modules. */
96 m4_module *module = m4__module_next (NULL);
98 if (module)
101 m4_shipout_string (context, obs, m4_get_module_name (module), SIZE_MAX,
102 true);
104 if ((module = m4__module_next (module)))
105 obstack_1grow (obs, ',');
107 while (module);
111 * refcount(module)
113 M4BUILTIN_HANDLER (refcount)
115 m4_module *module = m4__module_find (M4ARG (1));
116 m4_shipout_int (obs, module ? m4_module_refcount (module) : 0);
120 * load(MODULE-NAME)
122 M4BUILTIN_HANDLER (load)
124 /* Load the named module and install the builtins and macros
125 exported by that module. */
126 m4_module_load (context, M4ARG(1), obs);
130 * unload(MODULE-NAME)
132 M4BUILTIN_HANDLER (unload)
134 /* Remove all builtins and macros installed by the named module,
135 and then unload the module from memory entirely. */
136 m4_module_unload (context, M4ARG(1), obs);