1 /* GNU m4 -- A simple macro processor
3 Copyright (C) 1989-1994, 2000, 2004, 2006-2013 Free Software
6 This file is part of GNU M4.
8 GNU M4 is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU M4 is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /* Code for all builtin macros, initialization of symbol table, and
23 expansion of user defined macros. */
31 #include "spawn-pipe.h"
32 #include "wait-process.h"
34 #define ARG(i) (argc > (i) ? TOKEN_DATA_TEXT (argv[i]) : "")
36 /* Initialization of builtin and predefined macros. The table
37 "builtin_tab" is both used for initialization, and by the "builtin"
40 #define DECLARE(name) \
41 static void name (struct obstack *, int, token_data **)
43 DECLARE (m4___file__);
44 DECLARE (m4___line__);
45 DECLARE (m4___program__);
47 DECLARE (m4_changecom);
48 DECLARE (m4_changequote);
49 #ifdef ENABLE_CHANGEWORD
50 DECLARE (m4_changeword);
52 DECLARE (m4_debugmode);
53 DECLARE (m4_debugfile);
61 DECLARE (m4_errprint);
74 DECLARE (m4_maketemp);
76 DECLARE (m4_patsubst);
81 DECLARE (m4_sinclude);
85 DECLARE (m4_traceoff);
87 DECLARE (m4_translit);
88 DECLARE (m4_undefine);
89 DECLARE (m4_undivert);
93 static builtin const builtin_tab[] =
96 /* name GNUext macros blind function */
98 { "__file__", true, false, false, m4___file__ },
99 { "__line__", true, false, false, m4___line__ },
100 { "__program__", true, false, false, m4___program__ },
101 { "builtin", true, true, true, m4_builtin },
102 { "changecom", false, false, false, m4_changecom },
103 { "changequote", false, false, false, m4_changequote },
104 #ifdef ENABLE_CHANGEWORD
105 { "changeword", true, false, true, m4_changeword },
107 { "debugmode", true, false, false, m4_debugmode },
108 { "debugfile", true, false, false, m4_debugfile },
109 { "decr", false, false, true, m4_decr },
110 { "define", false, true, true, m4_define },
111 { "defn", false, false, true, m4_defn },
112 { "divert", false, false, false, m4_divert },
113 { "divnum", false, false, false, m4_divnum },
114 { "dnl", false, false, false, m4_dnl },
115 { "dumpdef", false, false, false, m4_dumpdef },
116 { "errprint", false, false, true, m4_errprint },
117 { "esyscmd", true, false, true, m4_esyscmd },
118 { "eval", false, false, true, m4_eval },
119 { "format", true, false, true, m4_format },
120 { "ifdef", false, false, true, m4_ifdef },
121 { "ifelse", false, false, true, m4_ifelse },
122 { "include", false, false, true, m4_include },
123 { "incr", false, false, true, m4_incr },
124 { "index", false, false, true, m4_index },
125 { "indir", true, true, true, m4_indir },
126 { "len", false, false, true, m4_len },
127 { "m4exit", false, false, false, m4_m4exit },
128 { "m4wrap", false, false, true, m4_m4wrap },
129 { "maketemp", false, false, true, m4_maketemp },
130 { "mkstemp", false, false, true, m4_mkstemp },
131 { "patsubst", true, false, true, m4_patsubst },
132 { "popdef", false, false, true, m4_popdef },
133 { "pushdef", false, true, true, m4_pushdef },
134 { "regexp", true, false, true, m4_regexp },
135 { "shift", false, false, true, m4_shift },
136 { "sinclude", false, false, true, m4_sinclude },
137 { "substr", false, false, true, m4_substr },
138 { "syscmd", false, false, true, m4_syscmd },
139 { "sysval", false, false, false, m4_sysval },
140 { "traceoff", false, false, false, m4_traceoff },
141 { "traceon", false, false, false, m4_traceon },
142 { "translit", false, false, true, m4_translit },
143 { "undefine", false, false, true, m4_undefine },
144 { "undivert", false, false, false, m4_undivert },
146 { 0, false, false, false, 0 },
148 /* placeholder is intentionally stuck after the table end delimiter,
149 so that we can easily find it, while not treating it as a real
151 { "placeholder", true, false, false, m4_placeholder },
154 static predefined const predefined_tab[] =
157 { "unix", "__unix__", "" },
160 { "windows", "__windows__", "" },
163 { "os2", "__os2__", "" },
165 #if !UNIX && !W32_NATIVE && !OS2
166 # warning Platform macro not provided
168 { NULL, "__gnu__", "" },
170 { NULL, NULL, NULL },
173 /*----------------------------------------.
174 | Find the builtin, which lives on ADDR. |
175 `----------------------------------------*/
177 const builtin * M4_GNUC_PURE
178 find_builtin_by_addr (builtin_func *func)
182 for (bp = &builtin_tab[0]; bp->name != NULL; bp++)
183 if (bp->func == func)
185 if (func == m4_placeholder)
190 /*----------------------------------------------------------.
191 | Find the builtin, which has NAME. On failure, return the |
192 | placeholder builtin. |
193 `----------------------------------------------------------*/
195 const builtin * M4_GNUC_PURE
196 find_builtin_by_name (const char *name)
200 for (bp = &builtin_tab[0]; bp->name != NULL; bp++)
201 if (STREQ (bp->name, name))
206 /*----------------------------------------------------------------.
207 | Install a builtin macro with name NAME, bound to the C function |
208 | given in BP. MODE is SYMBOL_INSERT or SYMBOL_PUSHDEF. |
209 `----------------------------------------------------------------*/
212 define_builtin (const char *name, const builtin *bp, symbol_lookup mode)
216 sym = lookup_symbol (name, mode);
217 SYMBOL_TYPE (sym) = TOKEN_FUNC;
218 SYMBOL_MACRO_ARGS (sym) = bp->groks_macro_args;
219 SYMBOL_BLIND_NO_ARGS (sym) = bp->blind_if_no_args;
220 SYMBOL_FUNC (sym) = bp->func;
223 /* Storage for the compiled regular expression of
224 --warn-macro-sequence. */
225 static struct re_pattern_buffer macro_sequence_buf;
227 /* Storage for the matches of --warn-macro-sequence. */
228 static struct re_registers macro_sequence_regs;
230 /* True if --warn-macro-sequence is in effect. */
231 static bool macro_sequence_inuse;
233 /*----------------------------------------.
234 | Clean up regular expression variables. |
235 `----------------------------------------*/
238 free_pattern_buffer (struct re_pattern_buffer *buf, struct re_registers *regs)
245 /*-----------------------------------------------------------------.
246 | Set the regular expression of --warn-macro-sequence that will be |
247 | checked during define and pushdef. Exit on failure. |
248 `-----------------------------------------------------------------*/
250 set_macro_sequence (const char *regexp)
255 regexp = DEFAULT_MACRO_SEQUENCE;
256 else if (regexp[0] == '\0')
258 macro_sequence_inuse = false;
262 msg = re_compile_pattern (regexp, strlen (regexp), ¯o_sequence_buf);
265 M4ERROR ((EXIT_FAILURE, 0,
266 "--warn-macro-sequence: bad regular expression `%s': %s",
269 re_set_registers (¯o_sequence_buf, ¯o_sequence_regs,
270 macro_sequence_regs.num_regs,
271 macro_sequence_regs.start, macro_sequence_regs.end);
272 macro_sequence_inuse = true;
275 /*-----------------------------------------------------------.
276 | Free dynamic memory utilized by the macro sequence regular |
277 | expression during the define builtin. |
278 `-----------------------------------------------------------*/
280 free_macro_sequence (void)
282 free_pattern_buffer (¯o_sequence_buf, ¯o_sequence_regs);
285 /*-----------------------------------------------------------------.
286 | Define a predefined or user-defined macro, with name NAME, and |
287 | expansion TEXT. MODE destinguishes between the "define" and the |
288 | "pushdef" case. It is also used from main. |
289 `-----------------------------------------------------------------*/
292 define_user_macro (const char *name, const char *text, symbol_lookup mode)
295 char *defn = xstrdup (text ? text : "");
297 s = lookup_symbol (name, mode);
298 if (SYMBOL_TYPE (s) == TOKEN_TEXT)
299 free (SYMBOL_TEXT (s));
301 SYMBOL_TYPE (s) = TOKEN_TEXT;
302 SYMBOL_TEXT (s) = defn;
304 /* Implement --warn-macro-sequence. */
305 if (macro_sequence_inuse && text)
308 size_t len = strlen (defn);
310 while ((offset = re_search (¯o_sequence_buf, defn, len, offset,
311 len - offset, ¯o_sequence_regs)) >= 0)
313 /* Skip empty matches. */
314 if (macro_sequence_regs.start[0] == macro_sequence_regs.end[0])
319 offset = macro_sequence_regs.end[0];
322 M4ERROR ((warning_status, 0,
323 "Warning: definition of `%s' contains sequence `%s'",
324 name, defn + macro_sequence_regs.start[0]));
329 M4ERROR ((warning_status, 0,
330 "error checking --warn-macro-sequence for macro `%s'",
335 /*-----------------------------------------------.
336 | Initialize all builtin and predefined macros. |
337 `-----------------------------------------------*/
343 const predefined *pp;
346 for (bp = &builtin_tab[0]; bp->name != NULL; bp++)
347 if (!no_gnu_extensions || !bp->gnu_extension)
349 if (prefix_all_builtins)
351 string = (char *) xmalloc (strlen (bp->name) + 4);
352 strcpy (string, "m4_");
353 strcat (string, bp->name);
354 define_builtin (string, bp, SYMBOL_INSERT);
358 define_builtin (bp->name, bp, SYMBOL_INSERT);
361 for (pp = &predefined_tab[0]; pp->func != NULL; pp++)
362 if (no_gnu_extensions)
364 if (pp->unix_name != NULL)
365 define_user_macro (pp->unix_name, pp->func, SYMBOL_INSERT);
369 if (pp->gnu_name != NULL)
370 define_user_macro (pp->gnu_name, pp->func, SYMBOL_INSERT);
374 /*-------------------------------------------------------------------.
375 | Give friendly warnings if a builtin macro is passed an |
376 | inappropriate number of arguments. NAME is the macro name for |
377 | messages, ARGC is actual number of arguments, MIN is the minimum |
378 | number of acceptable arguments, negative if not applicable, MAX is |
379 | the maximum number, negative if not applicable. |
380 `-------------------------------------------------------------------*/
383 bad_argc (token_data *name, int argc, int min, int max)
387 if (min > 0 && argc < min)
389 if (!suppress_warnings)
390 M4ERROR ((warning_status, 0,
391 "Warning: too few arguments to builtin `%s'",
392 TOKEN_DATA_TEXT (name)));
395 else if (max > 0 && argc > max && !suppress_warnings)
396 M4ERROR ((warning_status, 0,
397 "Warning: excess arguments to builtin `%s' ignored",
398 TOKEN_DATA_TEXT (name)));
403 /*-----------------------------------------------------------------.
404 | The function numeric_arg () converts ARG to an int pointed to by |
405 | VALUEP. If the conversion fails, print error message for macro |
406 | MACRO. Return true iff conversion succeeds. |
407 `-----------------------------------------------------------------*/
410 numeric_arg (token_data *macro, const char *arg, int *valuep)
417 M4ERROR ((warning_status, 0,
418 "empty string treated as 0 in builtin `%s'",
419 TOKEN_DATA_TEXT (macro)));
424 *valuep = strtol (arg, &endp, 10);
427 M4ERROR ((warning_status, 0,
428 "non-numeric argument to builtin `%s'",
429 TOKEN_DATA_TEXT (macro)));
432 if (isspace (to_uchar (*arg)))
433 M4ERROR ((warning_status, 0,
434 "leading whitespace ignored in builtin `%s'",
435 TOKEN_DATA_TEXT (macro)));
436 else if (errno == ERANGE)
437 M4ERROR ((warning_status, 0,
438 "numeric overflow detected in builtin `%s'",
439 TOKEN_DATA_TEXT (macro)));
444 /*------------------------------------------------------.
445 | The function ntoa () converts VALUE to a signed ASCII |
446 | representation in radix RADIX. |
447 `------------------------------------------------------*/
449 /* Digits for number to ASCII conversions. */
450 static char const digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
453 ntoa (int32_t value, int radix)
457 static char str[256];
458 char *s = &str[sizeof str];
465 uvalue = -(uint32_t) value;
470 uvalue = (uint32_t) value;
475 *--s = digits[uvalue % radix];
485 /*---------------------------------------------------------------.
486 | Format an int VAL, and stuff it into an obstack OBS. Used for |
487 | macros expanding to numbers. |
488 `---------------------------------------------------------------*/
491 shipout_int (struct obstack *obs, int val)
495 s = ntoa ((int32_t) val, 10);
496 obstack_grow (obs, s, strlen (s));
499 /*-------------------------------------------------------------------.
500 | Print ARGC arguments from the table ARGV to obstack OBS, separated |
501 | by SEP, and quoted by the current quotes if QUOTED is true. |
502 `-------------------------------------------------------------------*/
505 dump_args (struct obstack *obs, int argc, token_data **argv,
506 const char *sep, bool quoted)
509 size_t len = strlen (sep);
511 for (i = 1; i < argc; i++)
514 obstack_grow (obs, sep, len);
516 obstack_grow (obs, lquote.string, lquote.length);
517 obstack_grow (obs, TOKEN_DATA_TEXT (argv[i]),
518 strlen (TOKEN_DATA_TEXT (argv[i])));
520 obstack_grow (obs, rquote.string, rquote.length);
524 /* The rest of this file is code for builtins and expansion of user
525 defined macros. All the functions for builtins have a prototype as:
527 void m4_MACRONAME (struct obstack *obs, int argc, char *argv[]);
529 The function are expected to leave their expansion on the obstack OBS,
530 as an unfinished object. ARGV is a table of ARGC pointers to the
531 individual arguments to the macro. Please note that in general
532 argv[argc] != NULL. */
534 /* The first section are macros for definining, undefining, examining,
535 changing, ... other macros. */
537 /*-------------------------------------------------------------------.
538 | The function define_macro is common for the builtins "define", |
539 | "undefine", "pushdef" and "popdef". ARGC and ARGV is as for the |
540 | caller, and MODE argument determines how the macro name is entered |
541 | into the symbol table. |
542 `-------------------------------------------------------------------*/
545 define_macro (int argc, token_data **argv, symbol_lookup mode)
549 if (bad_argc (argv[0], argc, 2, 3))
552 if (TOKEN_DATA_TYPE (argv[1]) != TOKEN_TEXT)
554 M4ERROR ((warning_status, 0,
555 "Warning: %s: invalid macro name ignored", ARG (0)));
561 define_user_macro (ARG (1), "", mode);
565 switch (TOKEN_DATA_TYPE (argv[2]))
568 define_user_macro (ARG (1), ARG (2), mode);
572 bp = find_builtin_by_addr (TOKEN_DATA_FUNC (argv[2]));
576 define_builtin (ARG (1), bp, mode);
581 M4ERROR ((warning_status, 0,
582 "INTERNAL ERROR: bad token data type in define_macro ()"));
588 m4_define (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
590 define_macro (argc, argv, SYMBOL_INSERT);
594 m4_undefine (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
597 if (bad_argc (argv[0], argc, 2, -1))
599 for (i = 1; i < argc; i++)
600 lookup_symbol (ARG (i), SYMBOL_DELETE);
604 m4_pushdef (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
606 define_macro (argc, argv, SYMBOL_PUSHDEF);
610 m4_popdef (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
613 if (bad_argc (argv[0], argc, 2, -1))
615 for (i = 1; i < argc; i++)
616 lookup_symbol (ARG (i), SYMBOL_POPDEF);
619 /*---------------------.
620 | Conditionals of m4. |
621 `---------------------*/
624 m4_ifdef (struct obstack *obs, int argc, token_data **argv)
629 if (bad_argc (argv[0], argc, 3, 4))
631 s = lookup_symbol (ARG (1), SYMBOL_LOOKUP);
633 if (s != NULL && SYMBOL_TYPE (s) != TOKEN_VOID)
641 obstack_grow (obs, result, strlen (result));
645 m4_ifelse (struct obstack *obs, int argc, token_data **argv)
648 token_data *me = argv[0];
653 if (bad_argc (me, argc, 4, -1))
656 /* Diagnose excess arguments if 5, 8, 11, etc., actual arguments. */
657 bad_argc (me, (argc + 2) % 3, -1, 1);
663 while (result == NULL)
665 if (STREQ (ARG (0), ARG (1)))
684 obstack_grow (obs, result, strlen (result));
687 /*-------------------------------------------------------------------.
688 | The function dump_symbol () is for use by "dumpdef". It builds up |
689 | a table of all defined, un-shadowed, symbols. |
690 `-------------------------------------------------------------------*/
692 /* The structure dump_symbol_data is used to pass the information needed
693 from call to call to dump_symbol. */
695 struct dump_symbol_data
697 struct obstack *obs; /* obstack for table */
698 symbol **base; /* base of table */
699 int size; /* size of table */
703 dump_symbol (symbol *sym, void *arg)
705 struct dump_symbol_data *data = (struct dump_symbol_data *) arg;
706 if (!SYMBOL_SHADOWED (sym) && SYMBOL_TYPE (sym) != TOKEN_VOID)
708 obstack_blank (data->obs, sizeof (symbol *));
709 data->base = (symbol **) obstack_base (data->obs);
710 data->base[data->size++] = sym;
714 /*------------------------------------------------------------------------.
715 | qsort comparison routine, for sorting the table made in m4_dumpdef (). |
716 `------------------------------------------------------------------------*/
719 dumpdef_cmp (const void *s1, const void *s2)
721 return strcmp (SYMBOL_NAME (* (symbol *const *) s1),
722 SYMBOL_NAME (* (symbol *const *) s2));
725 /*-------------------------------------------------------------.
726 | Implementation of "dumpdef" itself. It builds up a table of |
727 | pointers to symbols, sorts it and prints the sorted table. |
728 `-------------------------------------------------------------*/
731 m4_dumpdef (struct obstack *obs, int argc, token_data **argv)
735 struct dump_symbol_data data;
739 data.base = (symbol **) obstack_base (obs);
744 hack_all_symbols (dump_symbol, &data);
748 for (i = 1; i < argc; i++)
750 s = lookup_symbol (TOKEN_DATA_TEXT (argv[i]), SYMBOL_LOOKUP);
751 if (s != NULL && SYMBOL_TYPE (s) != TOKEN_VOID)
752 dump_symbol (s, &data);
754 M4ERROR ((warning_status, 0,
755 "undefined macro `%s'", TOKEN_DATA_TEXT (argv[i])));
759 /* Make table of symbols invisible to expand_macro (). */
761 obstack_finish (obs);
763 qsort (data.base, data.size, sizeof (symbol *), dumpdef_cmp);
765 for (; data.size > 0; --data.size, data.base++)
767 DEBUG_PRINT1 ("%s:\t", SYMBOL_NAME (data.base[0]));
769 switch (SYMBOL_TYPE (data.base[0]))
772 if (debug_level & DEBUG_TRACE_QUOTE)
773 DEBUG_PRINT3 ("%s%s%s\n",
774 lquote.string, SYMBOL_TEXT (data.base[0]), rquote.string);
776 DEBUG_PRINT1 ("%s\n", SYMBOL_TEXT (data.base[0]));
780 bp = find_builtin_by_addr (SYMBOL_FUNC (data.base[0]));
783 M4ERROR ((warning_status, 0, "\
784 INTERNAL ERROR: builtin not found in builtin table"));
787 DEBUG_PRINT1 ("<%s>\n", bp->name);
792 M4ERROR ((warning_status, 0,
793 "INTERNAL ERROR: bad token data type in m4_dumpdef ()"));
800 /*-----------------------------------------------------------------.
801 | The builtin "builtin" allows calls to builtin macros, even if |
802 | their definition has been overridden or shadowed. It is thus |
803 | possible to redefine builtins, and still access their original |
804 | definition. This macro is not available in compatibility mode. |
805 `-----------------------------------------------------------------*/
808 m4_builtin (struct obstack *obs, int argc, token_data **argv)
813 if (bad_argc (argv[0], argc, 2, -1))
815 if (TOKEN_DATA_TYPE (argv[1]) != TOKEN_TEXT)
817 M4ERROR ((warning_status, 0,
818 "Warning: %s: invalid macro name ignored", ARG (0)));
823 bp = find_builtin_by_name (name);
824 if (bp->func == m4_placeholder)
825 M4ERROR ((warning_status, 0,
826 "undefined builtin `%s'", name));
830 if (! bp->groks_macro_args)
831 for (i = 2; i < argc; i++)
832 if (TOKEN_DATA_TYPE (argv[i]) != TOKEN_TEXT)
834 TOKEN_DATA_TYPE (argv[i]) = TOKEN_TEXT;
835 TOKEN_DATA_TEXT (argv[i]) = (char *) "";
837 bp->func (obs, argc - 1, argv + 1);
841 /*-------------------------------------------------------------------.
842 | The builtin "indir" allows indirect calls to macros, even if their |
843 | name is not a proper macro name. It is thus possible to define |
844 | macros with ill-formed names for internal use in larger macro |
845 | packages. This macro is not available in compatibility mode. |
846 `-------------------------------------------------------------------*/
849 m4_indir (struct obstack *obs, int argc, token_data **argv)
854 if (bad_argc (argv[0], argc, 2, -1))
856 if (TOKEN_DATA_TYPE (argv[1]) != TOKEN_TEXT)
858 M4ERROR ((warning_status, 0,
859 "Warning: %s: invalid macro name ignored", ARG (0)));
864 s = lookup_symbol (name, SYMBOL_LOOKUP);
865 if (s == NULL || SYMBOL_TYPE (s) == TOKEN_VOID)
866 M4ERROR ((warning_status, 0,
867 "undefined macro `%s'", name));
871 if (! SYMBOL_MACRO_ARGS (s))
872 for (i = 2; i < argc; i++)
873 if (TOKEN_DATA_TYPE (argv[i]) != TOKEN_TEXT)
875 TOKEN_DATA_TYPE (argv[i]) = TOKEN_TEXT;
876 TOKEN_DATA_TEXT (argv[i]) = (char *) "";
878 call_macro (s, argc - 1, argv + 1, obs);
882 /*------------------------------------------------------------------.
883 | The macro "defn" returns the quoted definition of the macro named |
884 | by the first argument. If the macro is builtin, it will push a |
885 | special macro-definition token on the input stack. |
886 `------------------------------------------------------------------*/
889 m4_defn (struct obstack *obs, int argc, token_data **argv)
895 if (bad_argc (argv[0], argc, 2, -1))
899 for (i = 1; i < (unsigned) argc; i++)
901 const char *arg = ARG((int) i);
902 s = lookup_symbol (arg, SYMBOL_LOOKUP);
906 switch (SYMBOL_TYPE (s))
909 obstack_grow (obs, lquote.string, lquote.length);
910 obstack_grow (obs, SYMBOL_TEXT (s), strlen (SYMBOL_TEXT (s)));
911 obstack_grow (obs, rquote.string, rquote.length);
916 if (b == m4_placeholder)
917 M4ERROR ((warning_status, 0, "\
918 builtin `%s' requested by frozen file is not supported", arg));
920 M4ERROR ((warning_status, 0,
921 "Warning: cannot concatenate builtin `%s'",
928 /* Nothing to do for traced but undefined macro. */
932 M4ERROR ((warning_status, 0,
933 "INTERNAL ERROR: bad symbol type in m4_defn ()"));
939 /*--------------------------------------------------------------.
940 | This section contains macros to handle the builtins "syscmd", |
941 | "esyscmd" and "sysval". "esyscmd" is GNU specific. |
942 `--------------------------------------------------------------*/
944 /* Exit code from last "syscmd" command. */
948 m4_syscmd (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
950 const char *cmd = ARG (1);
953 const char *prog_args[4] = { "sh", "-c" };
954 if (bad_argc (argv[0], argc, 2, 2) || !*cmd)
956 /* The empty command is successful. */
961 debug_flush_files ();
963 if (strstr (SYSCMD_SHELL, "cmd"))
965 prog_args[0] = "cmd";
971 status = execute (ARG (0), SYSCMD_SHELL, (char **) prog_args, false,
972 false, false, false, true, false, &sig_status);
975 assert (status == 127);
976 sysval = sig_status << 8;
980 if (status == 127 && errno)
981 M4ERROR ((warning_status, errno, "cannot run command `%s'", cmd));
987 m4_esyscmd (struct obstack *obs, int argc, token_data **argv)
989 const char *cmd = ARG (1);
990 const char *prog_args[4] = { "sh", "-c" };
997 if (bad_argc (argv[0], argc, 2, 2) || !*cmd)
999 /* The empty command is successful. */
1004 debug_flush_files ();
1006 if (strstr (SYSCMD_SHELL, "cmd"))
1008 prog_args[0] = "cmd";
1009 prog_args[1] = "/c";
1014 child = create_pipe_in (ARG (0), SYSCMD_SHELL, (char **) prog_args,
1015 NULL, false, true, false, &fd);
1018 M4ERROR ((warning_status, errno, "cannot run command `%s'", cmd));
1022 pin = fdopen (fd, "r");
1025 M4ERROR ((warning_status, errno, "cannot run command `%s'", cmd));
1032 size_t avail = obstack_room (obs);
1036 int ch = getc (pin);
1039 obstack_1grow (obs, ch);
1042 len = fread (obstack_next_free (obs), 1, avail, pin);
1045 obstack_blank_fast (obs, len);
1047 if (ferror (pin) || fclose (pin))
1048 M4ERROR ((EXIT_FAILURE, errno, "cannot read pipe"));
1050 status = wait_subprocess (child, ARG (0), false, true, true, false,
1054 assert (status == 127);
1055 sysval = sig_status << 8;
1059 if (status == 127 && errno)
1060 M4ERROR ((warning_status, errno, "cannot run command `%s'", cmd));
1066 m4_sysval (struct obstack *obs, int argc M4_GNUC_UNUSED,
1067 token_data **argv M4_GNUC_UNUSED)
1069 shipout_int (obs, sysval);
1072 /*------------------------------------------------------------------.
1073 | This section contains the top level code for the "eval" builtin. |
1074 | The actual work is done in the function evaluate (), which lives |
1076 `------------------------------------------------------------------*/
1079 m4_eval (struct obstack *obs, int argc, token_data **argv)
1086 if (bad_argc (argv[0], argc, 2, 4))
1089 if (*ARG (2) && !numeric_arg (argv[0], ARG (2), &radix))
1092 if (radix < 1 || radix > (int) strlen (digits))
1094 M4ERROR ((warning_status, 0,
1095 "radix %d in builtin `%s' out of range",
1100 if (argc >= 4 && !numeric_arg (argv[0], ARG (3), &min))
1104 M4ERROR ((warning_status, 0,
1105 "negative width to builtin `%s'", ARG (0)));
1110 M4ERROR ((warning_status, 0,
1111 "empty string treated as 0 in builtin `%s'", ARG (0)));
1112 else if (evaluate (ARG (1), &value))
1119 obstack_1grow (obs, '-');
1122 /* This assumes 2's-complement for correctly handling INT_MIN. */
1123 while (min-- - value > 0)
1124 obstack_1grow (obs, '0');
1125 while (value-- != 0)
1126 obstack_1grow (obs, '1');
1127 obstack_1grow (obs, '\0');
1131 s = ntoa (value, radix);
1135 obstack_1grow (obs, '-');
1138 for (min -= strlen (s); --min >= 0;)
1139 obstack_1grow (obs, '0');
1141 obstack_grow (obs, s, strlen (s));
1145 m4_incr (struct obstack *obs, int argc, token_data **argv)
1149 if (bad_argc (argv[0], argc, 2, 2))
1152 if (!numeric_arg (argv[0], ARG (1), &value))
1155 shipout_int (obs, value + 1);
1159 m4_decr (struct obstack *obs, int argc, token_data **argv)
1163 if (bad_argc (argv[0], argc, 2, 2))
1166 if (!numeric_arg (argv[0], ARG (1), &value))
1169 shipout_int (obs, value - 1);
1172 /* This section contains the macros "divert", "undivert" and "divnum" for
1173 handling diversion. The utility functions used lives in output.c. */
1175 /*-----------------------------------------------------------------.
1176 | Divert further output to the diversion given by ARGV[1]. Out of |
1177 | range means discard further output. |
1178 `-----------------------------------------------------------------*/
1181 m4_divert (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
1185 if (bad_argc (argv[0], argc, 1, 2))
1188 if (argc >= 2 && !numeric_arg (argv[0], ARG (1), &i))
1194 /*-----------------------------------------------------.
1195 | Expand to the current diversion number, -1 if none. |
1196 `-----------------------------------------------------*/
1199 m4_divnum (struct obstack *obs, int argc, token_data **argv)
1201 if (bad_argc (argv[0], argc, 1, 1))
1203 shipout_int (obs, current_diversion);
1206 /*------------------------------------------------------------------.
1207 | Bring back the diversion given by the argument list. If none is |
1208 | specified, bring back all diversions. GNU specific is the option |
1209 | of undiverting named files, by passing a non-numeric argument to |
1211 `------------------------------------------------------------------*/
1214 m4_undivert (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
1223 for (i = 1; i < argc; i++)
1225 file = strtol (ARG (i), &endp, 10);
1226 if (*endp == '\0' && !isspace (to_uchar (*ARG (i))))
1227 insert_diversion (file);
1228 else if (no_gnu_extensions)
1229 M4ERROR ((warning_status, 0,
1230 "non-numeric argument to builtin `%s'", ARG (0)));
1233 fp = m4_path_search (ARG (i), NULL);
1237 if (fclose (fp) == EOF)
1238 M4ERROR ((warning_status, errno,
1239 "error undiverting `%s'", ARG (i)));
1242 M4ERROR ((warning_status, errno,
1243 "cannot undivert `%s'", ARG (i)));
1248 /* This section contains various macros, which does not fall into any
1249 specific group. These are "dnl", "shift", "changequote", "changecom"
1250 and "changeword". */
1252 /*-----------------------------------------------------------.
1253 | Delete all subsequent whitespace from input. The function |
1254 | skip_line () lives in input.c. |
1255 `-----------------------------------------------------------*/
1258 m4_dnl (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
1260 if (bad_argc (argv[0], argc, 1, 1))
1266 /*--------------------------------------------------------------------.
1267 | Shift all arguments one to the left, discarding the first |
1268 | argument. Each output argument is quoted with the current quotes. |
1269 `--------------------------------------------------------------------*/
1272 m4_shift (struct obstack *obs, int argc, token_data **argv)
1274 if (bad_argc (argv[0], argc, 2, -1))
1276 dump_args (obs, argc - 1, argv + 1, ",", true);
1279 /*--------------------------------------------------------------------------.
1280 | Change the current quotes. The function set_quotes () lives in input.c. |
1281 `--------------------------------------------------------------------------*/
1284 m4_changequote (struct obstack *obs M4_GNUC_UNUSED, int argc,
1287 if (bad_argc (argv[0], argc, 1, 3))
1290 /* Explicit NULL distinguishes between empty and missing argument. */
1291 set_quotes ((argc >= 2) ? TOKEN_DATA_TEXT (argv[1]) : NULL,
1292 (argc >= 3) ? TOKEN_DATA_TEXT (argv[2]) : NULL);
1295 /*-----------------------------------------------------------------.
1296 | Change the current comment delimiters. The function set_comment |
1297 | () lives in input.c. |
1298 `-----------------------------------------------------------------*/
1301 m4_changecom (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
1303 if (bad_argc (argv[0], argc, 1, 3))
1306 /* Explicit NULL distinguishes between empty and missing argument. */
1307 set_comment ((argc >= 2) ? TOKEN_DATA_TEXT (argv[1]) : NULL,
1308 (argc >= 3) ? TOKEN_DATA_TEXT (argv[2]) : NULL);
1311 #ifdef ENABLE_CHANGEWORD
1313 /*---------------------------------------------------------------.
1314 | Change the regular expression used for breaking the input into |
1315 | words. The function set_word_regexp () lives in input.c. |
1316 `---------------------------------------------------------------*/
1319 m4_changeword (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
1321 if (bad_argc (argv[0], argc, 2, 2))
1324 set_word_regexp (TOKEN_DATA_TEXT (argv[1]));
1327 #endif /* ENABLE_CHANGEWORD */
1329 /* This section contains macros for inclusion of other files -- "include"
1330 and "sinclude". This differs from bringing back diversions, in that
1331 the input is scanned before being copied to the output. */
1333 /*---------------------------------------------------------------.
1334 | Generic include function. Include the file given by the first |
1335 | argument, if it exists. Complain about inaccessible files iff |
1336 | SILENT is false. |
1337 `---------------------------------------------------------------*/
1340 include (int argc, token_data **argv, bool silent)
1345 if (bad_argc (argv[0], argc, 2, 2))
1348 fp = m4_path_search (ARG (1), &name);
1353 M4ERROR ((warning_status, errno, "cannot open `%s'", ARG (1)));
1354 retcode = EXIT_FAILURE;
1359 push_file (fp, name, true);
1363 /*------------------------------------------------.
1364 | Include a file, complaining in case of errors. |
1365 `------------------------------------------------*/
1368 m4_include (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
1370 include (argc, argv, false);
1373 /*----------------------------------.
1374 | Include a file, ignoring errors. |
1375 `----------------------------------*/
1378 m4_sinclude (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
1380 include (argc, argv, true);
1383 /* More miscellaneous builtins -- "maketemp", "errprint", "__file__",
1384 "__line__", and "__program__". The last three are GNU specific. */
1386 /*------------------------------------------------------------------.
1387 | Use the first argument as at template for a temporary file name. |
1388 `------------------------------------------------------------------*/
1390 /* Add trailing 'X' to PATTERN of length LEN as necessary, then
1391 securely create the file, and place the quoted new file name on
1392 OBS. Report errors on behalf of ME. */
1394 mkstemp_helper (struct obstack *obs, const char *me, const char *pattern,
1401 /* Guarantee that there are six trailing 'X' characters, even if the
1402 user forgot to supply them. Output must be quoted if
1404 obstack_grow (obs, lquote.string, lquote.length);
1405 obstack_grow (obs, pattern, len);
1406 for (i = 0; len > 0 && i < 6; i++)
1407 if (pattern[len - i - 1] != 'X')
1409 obstack_grow0 (obs, "XXXXXX", 6 - i);
1410 name = (char *) obstack_base (obs) + lquote.length;
1413 fd = mkstemp (name);
1416 M4ERROR ((0, errno, "%s: cannot create tempfile `%s'", me, pattern));
1417 obstack_free (obs, obstack_finish (obs));
1422 /* Remove NUL, then finish quote. */
1423 obstack_blank (obs, -1);
1424 obstack_grow (obs, rquote.string, rquote.length);
1429 m4_maketemp (struct obstack *obs, int argc, token_data **argv)
1431 if (bad_argc (argv[0], argc, 2, 2))
1433 if (no_gnu_extensions)
1435 /* POSIX states "any trailing 'X' characters [are] replaced with
1436 the current process ID as a string", without referencing the
1437 file system. Horribly insecure, but we have to do it when we
1438 are in traditional mode.
1440 For reference, Solaris m4 does:
1443 maketemp(XX) -> `Xn', where n is last digit of pid
1444 maketemp(XXXXXXXX) -> `X00nnnnn', where nnnnn is 16-bit pid
1446 const char *str = ARG (1);
1447 int len = strlen (str);
1451 M4ERROR ((warning_status, 0, "recommend using mkstemp instead"));
1452 for (i = len; i > 1; i--)
1453 if (str[i - 1] != 'X')
1455 obstack_grow (obs, str, i);
1456 str = ntoa ((int32_t) getpid (), 10);
1457 len2 = strlen (str);
1459 obstack_grow0 (obs, str + len2 - (len - i), len - i);
1462 while (i++ < len - len2)
1463 obstack_1grow (obs, '0');
1464 obstack_grow0 (obs, str, len2);
1468 mkstemp_helper (obs, ARG (0), ARG (1), strlen (ARG (1)));
1472 m4_mkstemp (struct obstack *obs, int argc, token_data **argv)
1474 if (bad_argc (argv[0], argc, 2, 2))
1476 mkstemp_helper (obs, ARG (0), ARG (1), strlen (ARG (1)));
1479 /*----------------------------------------.
1480 | Print all arguments on standard error. |
1481 `----------------------------------------*/
1484 m4_errprint (struct obstack *obs, int argc, token_data **argv)
1486 if (bad_argc (argv[0], argc, 2, -1))
1488 dump_args (obs, argc, argv, " ", false);
1489 obstack_1grow (obs, '\0');
1490 debug_flush_files ();
1491 xfprintf (stderr, "%s", (char *) obstack_finish (obs));
1496 m4___file__ (struct obstack *obs, int argc, token_data **argv)
1498 if (bad_argc (argv[0], argc, 1, 1))
1500 obstack_grow (obs, lquote.string, lquote.length);
1501 obstack_grow (obs, current_file, strlen (current_file));
1502 obstack_grow (obs, rquote.string, rquote.length);
1506 m4___line__ (struct obstack *obs, int argc, token_data **argv)
1508 if (bad_argc (argv[0], argc, 1, 1))
1510 shipout_int (obs, current_line);
1514 m4___program__ (struct obstack *obs, int argc, token_data **argv)
1516 if (bad_argc (argv[0], argc, 1, 1))
1518 obstack_grow (obs, lquote.string, lquote.length);
1519 obstack_grow (obs, program_name, strlen (program_name));
1520 obstack_grow (obs, rquote.string, rquote.length);
1523 /* This section contains various macros for exiting, saving input until
1524 EOF is seen, and tracing macro calls. That is: "m4exit", "m4wrap",
1525 "traceon" and "traceoff". */
1527 /*----------------------------------------------------------.
1528 | Exit immediately, with exit status specified by the first |
1529 | argument, or 0 if no arguments are present. |
1530 `----------------------------------------------------------*/
1532 static void M4_GNUC_NORETURN
1533 m4_m4exit (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
1535 int exit_code = EXIT_SUCCESS;
1537 /* Warn on bad arguments, but still exit. */
1538 bad_argc (argv[0], argc, 1, 2);
1539 if (argc >= 2 && !numeric_arg (argv[0], ARG (1), &exit_code))
1540 exit_code = EXIT_FAILURE;
1541 if (exit_code < 0 || exit_code > 255)
1543 M4ERROR ((warning_status, 0,
1544 "exit status out of range: `%d'", exit_code));
1545 exit_code = EXIT_FAILURE;
1547 /* Change debug stream back to stderr, to force flushing debug stream and
1548 detect any errors it might have encountered. */
1549 debug_set_output (NULL);
1550 debug_flush_files ();
1551 if (exit_code == EXIT_SUCCESS && retcode != EXIT_SUCCESS)
1552 exit_code = retcode;
1553 /* Propagate non-zero status to atexit handlers. */
1554 if (exit_code != EXIT_SUCCESS)
1555 exit_failure = exit_code;
1559 /*------------------------------------------------------------------.
1560 | Save the argument text until EOF has been seen, allowing for user |
1561 | specified cleanup action. GNU version saves all arguments, the |
1562 | standard version only the first. |
1563 `------------------------------------------------------------------*/
1566 m4_m4wrap (struct obstack *obs, int argc, token_data **argv)
1568 if (bad_argc (argv[0], argc, 2, -1))
1570 if (no_gnu_extensions)
1571 obstack_grow (obs, ARG (1), strlen (ARG (1)));
1573 dump_args (obs, argc, argv, " ", false);
1574 obstack_1grow (obs, '\0');
1575 push_wrapup ((char *) obstack_finish (obs));
1578 /* Enable tracing of all specified macros, or all, if none is specified.
1579 Tracing is disabled by default, when a macro is defined. This can be
1580 overridden by the "t" debug flag. */
1582 /*------------------------------------------------------------------.
1583 | Set_trace () is used by "traceon" and "traceoff" to enable and |
1584 | disable tracing of a macro. It disables tracing if DATA is NULL, |
1585 | otherwise it enables tracing. |
1586 `------------------------------------------------------------------*/
1589 set_trace (symbol *sym, void *data)
1591 SYMBOL_TRACED (sym) = data != NULL;
1592 /* Remove placeholder from table if macro is undefined and untraced. */
1593 if (SYMBOL_TYPE (sym) == TOKEN_VOID && data == NULL)
1594 lookup_symbol (SYMBOL_NAME (sym), SYMBOL_POPDEF);
1598 m4_traceon (struct obstack *obs, int argc, token_data **argv)
1604 hack_all_symbols (set_trace, obs);
1606 for (i = 1; i < argc; i++)
1608 s = lookup_symbol (ARG (i), SYMBOL_LOOKUP);
1610 s = lookup_symbol (ARG (i), SYMBOL_INSERT);
1615 /*------------------------------------------------------------------------.
1616 | Disable tracing of all specified macros, or all, if none is specified. |
1617 `------------------------------------------------------------------------*/
1620 m4_traceoff (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
1626 hack_all_symbols (set_trace, NULL);
1628 for (i = 1; i < argc; i++)
1630 s = lookup_symbol (TOKEN_DATA_TEXT (argv[i]), SYMBOL_LOOKUP);
1632 set_trace (s, NULL);
1636 /*------------------------------------------------------------------.
1637 | On-the-fly control of the format of the tracing output. It takes |
1638 | one argument, which is a character string like given to the -d |
1639 | option, or none in which case the debug_level is zeroed. |
1640 `------------------------------------------------------------------*/
1643 m4_debugmode (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
1645 int new_debug_level;
1648 if (bad_argc (argv[0], argc, 1, 2))
1655 if (ARG (1)[0] == '+' || ARG (1)[0] == '-')
1657 change_flag = ARG (1)[0];
1658 new_debug_level = debug_decode (ARG (1) + 1);
1663 new_debug_level = debug_decode (ARG (1));
1666 if (new_debug_level < 0)
1667 M4ERROR ((warning_status, 0,
1668 "Debugmode: bad debug flags: `%s'", ARG (1)));
1671 switch (change_flag)
1674 debug_level = new_debug_level;
1678 debug_level |= new_debug_level;
1682 debug_level &= ~new_debug_level;
1686 M4ERROR ((warning_status, 0,
1687 "INTERNAL ERROR: bad flag in m4_debugmode ()"));
1694 /*-------------------------------------------------------------------------.
1695 | Specify the destination of the debugging output. With one argument, the |
1696 | argument is taken as a file name, with no arguments, revert to stderr. |
1697 `-------------------------------------------------------------------------*/
1700 m4_debugfile (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
1702 if (bad_argc (argv[0], argc, 1, 2))
1706 debug_set_output (NULL);
1707 else if (!debug_set_output (ARG (1)))
1708 M4ERROR ((warning_status, errno,
1709 "cannot set debug file `%s'", ARG (1)));
1712 /* This section contains text processing macros: "len", "index",
1713 "substr", "translit", "format", "regexp" and "patsubst". The last
1714 three are GNU specific. */
1716 /*---------------------------------------------.
1717 | Expand to the length of the first argument. |
1718 `---------------------------------------------*/
1721 m4_len (struct obstack *obs, int argc, token_data **argv)
1723 if (bad_argc (argv[0], argc, 2, 2))
1725 shipout_int (obs, strlen (ARG (1)));
1728 /*-------------------------------------------------------------------.
1729 | The macro expands to the first index of the second argument in the |
1731 `-------------------------------------------------------------------*/
1734 m4_index (struct obstack *obs, int argc, token_data **argv)
1736 const char *haystack;
1740 if (bad_argc (argv[0], argc, 3, 3))
1742 /* builtin(`index') is blank, but index(`abc') is 0. */
1744 shipout_int (obs, 0);
1749 result = strstr (haystack, ARG (2));
1750 retval = result ? result - haystack : -1;
1752 shipout_int (obs, retval);
1755 /*-----------------------------------------------------------------.
1756 | The macro "substr" extracts substrings from the first argument, |
1757 | starting from the index given by the second argument, extending |
1758 | for a length given by the third argument. If the third argument |
1759 | is missing, the substring extends to the end of the first |
1761 `-----------------------------------------------------------------*/
1764 m4_substr (struct obstack *obs, int argc, token_data **argv)
1769 if (bad_argc (argv[0], argc, 3, 4))
1771 /* builtin(`substr') is blank, but substr(`abc') is abc. */
1773 obstack_grow (obs, ARG (1), strlen (ARG (1)));
1777 length = avail = strlen (ARG (1));
1778 if (!numeric_arg (argv[0], ARG (2), &start))
1781 if (argc >= 4 && !numeric_arg (argv[0], ARG (3), &length))
1784 if (start < 0 || length <= 0 || start >= avail)
1787 if (start + length > avail)
1788 length = avail - start;
1789 obstack_grow (obs, ARG (1) + start, length);
1792 /*------------------------------------------------------------------.
1793 | For "translit", ranges are allowed in the second and third |
1794 | argument. They are expanded in the following function, and the |
1795 | expanded strings, without any ranges left, are used to translate |
1796 | the characters of the first argument. A single - (dash) can be |
1797 | included in the strings by being the first or the last character |
1798 | in the string. If the first character in a range is after the |
1799 | first in the character set, the range is made backwards, thus 9-0 |
1800 | is the string 9876543210. |
1801 `------------------------------------------------------------------*/
1804 expand_ranges (const char *s, struct obstack *obs)
1809 for (from = '\0'; *s != '\0'; from = to_uchar (*s++))
1811 if (*s == '-' && from != '\0')
1813 to = to_uchar (*++s);
1817 obstack_1grow (obs, '-');
1820 else if (from <= to)
1823 obstack_1grow (obs, from);
1827 while (--from >= to)
1828 obstack_1grow (obs, from);
1832 obstack_1grow (obs, *s);
1834 obstack_1grow (obs, '\0');
1835 return (char *) obstack_finish (obs);
1838 /*-----------------------------------------------------------------.
1839 | The macro "translit" translates all characters in the first |
1840 | argument, which are present in the second argument, into the |
1841 | corresponding character from the third argument. If the third |
1842 | argument is shorter than the second, the extra characters in the |
1843 | second argument are deleted from the first. |
1844 `-----------------------------------------------------------------*/
1847 m4_translit (struct obstack *obs, int argc, token_data **argv)
1849 const char *data = ARG (1);
1850 const char *from = ARG (2);
1852 char map[UCHAR_MAX + 1];
1853 char found[UCHAR_MAX + 1];
1856 if (bad_argc (argv[0], argc, 3, 4) || !*data || !*from)
1858 /* builtin(`translit') is blank, but translit(`abc') is abc. */
1860 obstack_grow (obs, data, strlen (data));
1865 if (strchr (to, '-') != NULL)
1867 to = expand_ranges (to, obs);
1871 /* If there are only one or two bytes to replace, it is faster to
1872 use memchr2. Using expand_ranges does nothing unless there are
1873 at least three bytes. */
1874 if (!from[1] || !from[2])
1877 size_t len = strlen (data);
1878 while ((p = (char *) memchr2 (data, from[0], from[1], len)))
1880 obstack_grow (obs, data, p - data);
1886 if (*p == from[0] && to[0])
1887 obstack_1grow (obs, to[0]);
1888 else if (*p == from[1] && to[0] && to[1])
1889 obstack_1grow (obs, to[1]);
1891 obstack_grow (obs, data, len);
1895 if (strchr (from, '-') != NULL)
1897 from = expand_ranges (from, obs);
1898 assert (from && *from);
1901 /* Calling strchr(from) for each character in data is quadratic,
1902 since both strings can be arbitrarily long. Instead, create a
1903 from-to mapping in one pass of from, then use that map in one
1904 pass of data, for linear behavior. Traditional behavior is that
1905 only the first instance of a character in from is consulted,
1906 hence the found map. */
1907 memset (map, 0, sizeof map);
1908 memset (found, 0, sizeof found);
1909 for ( ; (ch = *from) != '\0'; from++)
1920 for (data = ARG (1); (ch = *data) != '\0'; data++)
1923 obstack_1grow (obs, ch);
1925 obstack_1grow (obs, map[ch]);
1929 /*-------------------------------------------------------------------.
1930 | Frontend for printf like formatting. The function format () lives |
1931 | in the file format.c. |
1932 `-------------------------------------------------------------------*/
1935 m4_format (struct obstack *obs, int argc, token_data **argv)
1937 if (bad_argc (argv[0], argc, 2, -1))
1939 expand_format (obs, argc - 1, argv + 1);
1942 /*------------------------------------------------------------------.
1943 | Function to perform substitution by regular expressions. Used by |
1944 | the builtins regexp and patsubst. The changed text is placed on |
1945 | the obstack. The substitution is REPL, with \& substituted by |
1946 | this part of VICTIM matched by the last whole regular expression, |
1947 | taken from REGS[0], and \N substituted by the text matched by the |
1948 | Nth parenthesized sub-expression, taken from REGS[N]. |
1949 `------------------------------------------------------------------*/
1951 static int substitute_warned = 0;
1954 substitute (struct obstack *obs, const char *victim, const char *repl,
1955 struct re_registers *regs)
1961 const char *backslash = strchr (repl, '\\');
1964 obstack_grow (obs, repl, strlen (repl));
1967 obstack_grow (obs, repl, backslash - repl);
1973 if (!substitute_warned)
1975 M4ERROR ((warning_status, 0, "\
1976 Warning: \\0 will disappear, use \\& instead in replacements"));
1977 substitute_warned = 1;
1982 obstack_grow (obs, victim + regs->start[0],
1983 regs->end[0] - regs->start[0]);
1987 case '1': case '2': case '3': case '4': case '5': case '6':
1988 case '7': case '8': case '9':
1990 if (regs->num_regs - 1 <= ind)
1991 M4ERROR ((warning_status, 0,
1992 "Warning: sub-expression %d not present", ch));
1993 else if (regs->end[ch] > 0)
1994 obstack_grow (obs, victim + regs->start[ch],
1995 regs->end[ch] - regs->start[ch]);
2000 M4ERROR ((warning_status, 0,
2001 "Warning: trailing \\ ignored in replacement"));
2005 obstack_1grow (obs, ch);
2012 /*------------------------------------------.
2013 | Initialize regular expression variables. |
2014 `------------------------------------------*/
2017 init_pattern_buffer (struct re_pattern_buffer *buf, struct re_registers *regs)
2019 buf->translate = NULL;
2020 buf->fastmap = NULL;
2030 /*------------------------------------------------------------------.
2031 | Regular expression version of index. Given two arguments, expand |
2032 | to the index of the first match of the second argument (a regexp) |
2033 | in the first. Expand to -1 if here is no match. Given a third |
2034 | argument, it changes the expansion to this argument. |
2035 `------------------------------------------------------------------*/
2038 m4_regexp (struct obstack *obs, int argc, token_data **argv)
2040 const char *victim; /* first argument */
2041 const char *regexp; /* regular expression */
2042 const char *repl; /* replacement string */
2044 struct re_pattern_buffer buf; /* compiled regular expression */
2045 struct re_registers regs; /* for subexpression matches */
2046 const char *msg; /* error message from re_compile_pattern */
2047 int startpos; /* start position of match */
2048 int length; /* length of first argument */
2050 if (bad_argc (argv[0], argc, 3, 4))
2052 /* builtin(`regexp') is blank, but regexp(`abc') is 0. */
2054 shipout_int (obs, 0);
2058 victim = TOKEN_DATA_TEXT (argv[1]);
2059 regexp = TOKEN_DATA_TEXT (argv[2]);
2061 init_pattern_buffer (&buf, ®s);
2062 msg = re_compile_pattern (regexp, strlen (regexp), &buf);
2066 M4ERROR ((warning_status, 0,
2067 "bad regular expression: `%s': %s", regexp, msg));
2068 free_pattern_buffer (&buf, ®s);
2072 length = strlen (victim);
2073 /* Avoid overhead of allocating regs if we won't use it. */
2074 startpos = re_search (&buf, victim, length, 0, length,
2075 argc == 3 ? NULL : ®s);
2078 M4ERROR ((warning_status, 0,
2079 "error matching regular expression `%s'", regexp));
2081 shipout_int (obs, startpos);
2082 else if (startpos >= 0)
2084 repl = TOKEN_DATA_TEXT (argv[3]);
2085 substitute (obs, victim, repl, ®s);
2088 free_pattern_buffer (&buf, ®s);
2091 /*--------------------------------------------------------------------------.
2092 | Substitute all matches of a regexp occuring in a string. Each match of |
2093 | the second argument (a regexp) in the first argument is changed to the |
2094 | third argument, with \& substituted by the matched text, and \N |
2095 | substituted by the text matched by the Nth parenthesized sub-expression. |
2096 `--------------------------------------------------------------------------*/
2099 m4_patsubst (struct obstack *obs, int argc, token_data **argv)
2101 const char *victim; /* first argument */
2102 const char *regexp; /* regular expression */
2104 struct re_pattern_buffer buf; /* compiled regular expression */
2105 struct re_registers regs; /* for subexpression matches */
2106 const char *msg; /* error message from re_compile_pattern */
2107 int matchpos; /* start position of match */
2108 int offset; /* current match offset */
2109 int length; /* length of first argument */
2111 if (bad_argc (argv[0], argc, 3, 4))
2113 /* builtin(`patsubst') is blank, but patsubst(`abc') is abc. */
2115 obstack_grow (obs, ARG (1), strlen (ARG (1)));
2119 regexp = TOKEN_DATA_TEXT (argv[2]);
2121 init_pattern_buffer (&buf, ®s);
2122 msg = re_compile_pattern (regexp, strlen (regexp), &buf);
2126 M4ERROR ((warning_status, 0,
2127 "bad regular expression `%s': %s", regexp, msg));
2132 victim = TOKEN_DATA_TEXT (argv[1]);
2133 length = strlen (victim);
2136 while (offset <= length)
2138 matchpos = re_search (&buf, victim, length,
2139 offset, length - offset, ®s);
2143 /* Match failed -- either error or there is no match in the
2144 rest of the string, in which case the rest of the string is
2148 M4ERROR ((warning_status, 0,
2149 "error matching regular expression `%s'", regexp));
2150 else if (offset < length)
2151 obstack_grow (obs, victim + offset, length - offset);
2155 /* Copy the part of the string that was skipped by re_search (). */
2157 if (matchpos > offset)
2158 obstack_grow (obs, victim + offset, matchpos - offset);
2160 /* Handle the part of the string that was covered by the match. */
2162 substitute (obs, victim, ARG (3), ®s);
2164 /* Update the offset to the end of the match. If the regexp
2165 matched a null string, advance offset one more, to avoid
2168 offset = regs.end[0];
2169 if (regs.start[0] == regs.end[0])
2170 obstack_1grow (obs, victim[offset++]);
2172 obstack_1grow (obs, '\0');
2174 free_pattern_buffer (&buf, ®s);
2177 /* Finally, a placeholder builtin. This builtin is not installed by
2178 default, but when reading back frozen files, this is associated
2179 with any builtin we don't recognize (for example, if the frozen
2180 file was created with a changeword capable m4, but is then loaded
2181 by a different m4 that does not support changeword). This way, we
2182 can keep 'm4 -R' quiet in the common case that the user did not
2183 know or care about the builtin when the frozen file was created,
2184 while still flagging it as a potential error if an attempt is made
2185 to actually use the builtin. */
2187 /*--------------------------------------------------------------------.
2188 | Issue a warning that this macro is a placeholder for an unsupported |
2189 | builtin that was requested while reloading a frozen file. |
2190 `--------------------------------------------------------------------*/
2193 m4_placeholder (struct obstack *obs M4_GNUC_UNUSED, int argc,
2196 M4ERROR ((warning_status, 0, "\
2197 builtin `%s' requested by frozen file is not supported", ARG (0)));
2200 /*-------------------------------------------------------------------.
2201 | This function handles all expansion of user defined and predefined |
2202 | macros. It is called with an obstack OBS, where the macros |
2203 | expansion will be placed, as an unfinished object. SYM points to |
2204 | the macro definition, giving the expansion text. ARGC and ARGV |
2205 | are the arguments, as usual. |
2206 `-------------------------------------------------------------------*/
2209 expand_user_macro (struct obstack *obs, symbol *sym,
2210 int argc, token_data **argv)
2212 const char *text = SYMBOL_TEXT (sym);
2216 const char *dollar = strchr (text, '$');
2219 obstack_grow (obs, text, strlen (text));
2222 obstack_grow (obs, text, dollar - text);
2226 case '0': case '1': case '2': case '3': case '4':
2227 case '5': case '6': case '7': case '8': case '9':
2228 if (no_gnu_extensions)
2234 for (i = 0; isdigit (to_uchar (*text)); text++)
2235 i = i*10 + (*text - '0');
2238 obstack_grow (obs, TOKEN_DATA_TEXT (argv[i]),
2239 strlen (TOKEN_DATA_TEXT (argv[i])));
2242 case '#': /* number of arguments */
2243 shipout_int (obs, argc - 1);
2247 case '*': /* all arguments */
2248 case '@': /* ... same, but quoted */
2249 dump_args (obs, argc, argv, ",", *text == '@');
2254 obstack_1grow (obs, '$');