1 /* GNU m4 -- A simple macro processor
3 Copyright (C) 1989-1994, 2000, 2004, 2006-2011 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. */
30 #include "spawn-pipe.h"
31 #include "wait-process.h"
33 #define ARG(i) (argc > (i) ? TOKEN_DATA_TEXT (argv[i]) : "")
35 /* Initialization of builtin and predefined macros. The table
36 "builtin_tab" is both used for initialization, and by the "builtin"
39 #define DECLARE(name) \
40 static void name (struct obstack *, int, token_data **)
42 DECLARE (m4___file__);
43 DECLARE (m4___line__);
44 DECLARE (m4___program__);
46 DECLARE (m4_changecom);
47 DECLARE (m4_changequote);
48 #ifdef ENABLE_CHANGEWORD
49 DECLARE (m4_changeword);
51 DECLARE (m4_debugmode);
52 DECLARE (m4_debugfile);
60 DECLARE (m4_errprint);
73 DECLARE (m4_maketemp);
75 DECLARE (m4_patsubst);
80 DECLARE (m4_sinclude);
84 DECLARE (m4_traceoff);
86 DECLARE (m4_translit);
87 DECLARE (m4_undefine);
88 DECLARE (m4_undivert);
92 static builtin const builtin_tab[] =
95 /* name GNUext macros blind function */
97 { "__file__", true, false, false, m4___file__ },
98 { "__line__", true, false, false, m4___line__ },
99 { "__program__", true, false, false, m4___program__ },
100 { "builtin", true, true, true, m4_builtin },
101 { "changecom", false, false, false, m4_changecom },
102 { "changequote", false, false, false, m4_changequote },
103 #ifdef ENABLE_CHANGEWORD
104 { "changeword", true, false, true, m4_changeword },
106 { "debugmode", true, false, false, m4_debugmode },
107 { "debugfile", true, false, false, m4_debugfile },
108 { "decr", false, false, true, m4_decr },
109 { "define", false, true, true, m4_define },
110 { "defn", false, false, true, m4_defn },
111 { "divert", false, false, false, m4_divert },
112 { "divnum", false, false, false, m4_divnum },
113 { "dnl", false, false, false, m4_dnl },
114 { "dumpdef", false, false, false, m4_dumpdef },
115 { "errprint", false, false, true, m4_errprint },
116 { "esyscmd", true, false, true, m4_esyscmd },
117 { "eval", false, false, true, m4_eval },
118 { "format", true, false, true, m4_format },
119 { "ifdef", false, false, true, m4_ifdef },
120 { "ifelse", false, false, true, m4_ifelse },
121 { "include", false, false, true, m4_include },
122 { "incr", false, false, true, m4_incr },
123 { "index", false, false, true, m4_index },
124 { "indir", true, true, true, m4_indir },
125 { "len", false, false, true, m4_len },
126 { "m4exit", false, false, false, m4_m4exit },
127 { "m4wrap", false, false, true, m4_m4wrap },
128 { "maketemp", false, false, true, m4_maketemp },
129 { "mkstemp", false, false, true, m4_mkstemp },
130 { "patsubst", true, false, true, m4_patsubst },
131 { "popdef", false, false, true, m4_popdef },
132 { "pushdef", false, true, true, m4_pushdef },
133 { "regexp", true, false, true, m4_regexp },
134 { "shift", false, false, true, m4_shift },
135 { "sinclude", false, false, true, m4_sinclude },
136 { "substr", false, false, true, m4_substr },
137 { "syscmd", false, false, true, m4_syscmd },
138 { "sysval", false, false, false, m4_sysval },
139 { "traceoff", false, false, false, m4_traceoff },
140 { "traceon", false, false, false, m4_traceon },
141 { "translit", false, false, true, m4_translit },
142 { "undefine", false, false, true, m4_undefine },
143 { "undivert", false, false, false, m4_undivert },
145 { 0, false, false, false, 0 },
147 /* placeholder is intentionally stuck after the table end delimiter,
148 so that we can easily find it, while not treating it as a real
150 { "placeholder", true, false, false, m4_placeholder },
153 static predefined const predefined_tab[] =
156 { "unix", "__unix__", "" },
159 { "windows", "__windows__", "" },
162 { "os2", "__os2__", "" },
164 #if !UNIX && !W32_NATIVE && !OS2
165 # warning Platform macro not provided
167 { NULL, "__gnu__", "" },
169 { NULL, NULL, NULL },
172 /*----------------------------------------.
173 | Find the builtin, which lives on ADDR. |
174 `----------------------------------------*/
177 find_builtin_by_addr (builtin_func *func)
181 for (bp = &builtin_tab[0]; bp->name != NULL; bp++)
182 if (bp->func == func)
184 if (func == m4_placeholder)
189 /*----------------------------------------------------------.
190 | Find the builtin, which has NAME. On failure, return the |
191 | placeholder builtin. |
192 `----------------------------------------------------------*/
195 find_builtin_by_name (const char *name)
199 for (bp = &builtin_tab[0]; bp->name != NULL; bp++)
200 if (STREQ (bp->name, name))
205 /*----------------------------------------------------------------.
206 | Install a builtin macro with name NAME, bound to the C function |
207 | given in BP. MODE is SYMBOL_INSERT or SYMBOL_PUSHDEF. |
208 `----------------------------------------------------------------*/
211 define_builtin (const char *name, const builtin *bp, symbol_lookup mode)
215 sym = lookup_symbol (name, mode);
216 SYMBOL_TYPE (sym) = TOKEN_FUNC;
217 SYMBOL_MACRO_ARGS (sym) = bp->groks_macro_args;
218 SYMBOL_BLIND_NO_ARGS (sym) = bp->blind_if_no_args;
219 SYMBOL_FUNC (sym) = bp->func;
222 /* Storage for the compiled regular expression of
223 --warn-macro-sequence. */
224 static struct re_pattern_buffer macro_sequence_buf;
226 /* Storage for the matches of --warn-macro-sequence. */
227 static struct re_registers macro_sequence_regs;
229 /* True if --warn-macro-sequence is in effect. */
230 static bool macro_sequence_inuse;
232 /*----------------------------------------.
233 | Clean up regular expression variables. |
234 `----------------------------------------*/
237 free_pattern_buffer (struct re_pattern_buffer *buf, struct re_registers *regs)
244 /*-----------------------------------------------------------------.
245 | Set the regular expression of --warn-macro-sequence that will be |
246 | checked during define and pushdef. Exit on failure. |
247 `-----------------------------------------------------------------*/
249 set_macro_sequence (const char *regexp)
254 regexp = DEFAULT_MACRO_SEQUENCE;
255 else if (regexp[0] == '\0')
257 macro_sequence_inuse = false;
261 msg = re_compile_pattern (regexp, strlen (regexp), ¯o_sequence_buf);
264 M4ERROR ((EXIT_FAILURE, 0,
265 "--warn-macro-sequence: bad regular expression `%s': %s",
268 re_set_registers (¯o_sequence_buf, ¯o_sequence_regs,
269 macro_sequence_regs.num_regs,
270 macro_sequence_regs.start, macro_sequence_regs.end);
271 macro_sequence_inuse = true;
274 /*-----------------------------------------------------------.
275 | Free dynamic memory utilized by the macro sequence regular |
276 | expression during the define builtin. |
277 `-----------------------------------------------------------*/
279 free_macro_sequence (void)
281 free_pattern_buffer (¯o_sequence_buf, ¯o_sequence_regs);
284 /*-----------------------------------------------------------------.
285 | Define a predefined or user-defined macro, with name NAME, and |
286 | expansion TEXT. MODE destinguishes between the "define" and the |
287 | "pushdef" case. It is also used from main. |
288 `-----------------------------------------------------------------*/
291 define_user_macro (const char *name, const char *text, symbol_lookup mode)
294 char *defn = xstrdup (text ? text : "");
296 s = lookup_symbol (name, mode);
297 if (SYMBOL_TYPE (s) == TOKEN_TEXT)
298 free (SYMBOL_TEXT (s));
300 SYMBOL_TYPE (s) = TOKEN_TEXT;
301 SYMBOL_TEXT (s) = defn;
303 /* Implement --warn-macro-sequence. */
304 if (macro_sequence_inuse && text)
307 size_t len = strlen (defn);
309 while ((offset = re_search (¯o_sequence_buf, defn, len, offset,
310 len - offset, ¯o_sequence_regs)) >= 0)
312 /* Skip empty matches. */
313 if (macro_sequence_regs.start[0] == macro_sequence_regs.end[0])
318 offset = macro_sequence_regs.end[0];
321 M4ERROR ((warning_status, 0,
322 "Warning: definition of `%s' contains sequence `%s'",
323 name, defn + macro_sequence_regs.start[0]));
328 M4ERROR ((warning_status, 0,
329 "error checking --warn-macro-sequence for macro `%s'",
334 /*-----------------------------------------------.
335 | Initialize all builtin and predefined macros. |
336 `-----------------------------------------------*/
342 const predefined *pp;
345 for (bp = &builtin_tab[0]; bp->name != NULL; bp++)
346 if (!no_gnu_extensions || !bp->gnu_extension)
348 if (prefix_all_builtins)
350 string = (char *) xmalloc (strlen (bp->name) + 4);
351 strcpy (string, "m4_");
352 strcat (string, bp->name);
353 define_builtin (string, bp, SYMBOL_INSERT);
357 define_builtin (bp->name, bp, SYMBOL_INSERT);
360 for (pp = &predefined_tab[0]; pp->func != NULL; pp++)
361 if (no_gnu_extensions)
363 if (pp->unix_name != NULL)
364 define_user_macro (pp->unix_name, pp->func, SYMBOL_INSERT);
368 if (pp->gnu_name != NULL)
369 define_user_macro (pp->gnu_name, pp->func, SYMBOL_INSERT);
373 /*-------------------------------------------------------------------.
374 | Give friendly warnings if a builtin macro is passed an |
375 | inappropriate number of arguments. NAME is the macro name for |
376 | messages, ARGC is actual number of arguments, MIN is the minimum |
377 | number of acceptable arguments, negative if not applicable, MAX is |
378 | the maximum number, negative if not applicable. |
379 `-------------------------------------------------------------------*/
382 bad_argc (token_data *name, int argc, int min, int max)
386 if (min > 0 && argc < min)
388 if (!suppress_warnings)
389 M4ERROR ((warning_status, 0,
390 "Warning: too few arguments to builtin `%s'",
391 TOKEN_DATA_TEXT (name)));
394 else if (max > 0 && argc > max && !suppress_warnings)
395 M4ERROR ((warning_status, 0,
396 "Warning: excess arguments to builtin `%s' ignored",
397 TOKEN_DATA_TEXT (name)));
402 /*-----------------------------------------------------------------.
403 | The function numeric_arg () converts ARG to an int pointed to by |
404 | VALUEP. If the conversion fails, print error message for macro |
405 | MACRO. Return true iff conversion succeeds. |
406 `-----------------------------------------------------------------*/
409 numeric_arg (token_data *macro, const char *arg, int *valuep)
416 M4ERROR ((warning_status, 0,
417 "empty string treated as 0 in builtin `%s'",
418 TOKEN_DATA_TEXT (macro)));
423 *valuep = strtol (arg, &endp, 10);
426 M4ERROR ((warning_status, 0,
427 "non-numeric argument to builtin `%s'",
428 TOKEN_DATA_TEXT (macro)));
431 if (isspace (to_uchar (*arg)))
432 M4ERROR ((warning_status, 0,
433 "leading whitespace ignored in builtin `%s'",
434 TOKEN_DATA_TEXT (macro)));
435 else if (errno == ERANGE)
436 M4ERROR ((warning_status, 0,
437 "numeric overflow detected in builtin `%s'",
438 TOKEN_DATA_TEXT (macro)));
443 /*------------------------------------------------------.
444 | The function ntoa () converts VALUE to a signed ASCII |
445 | representation in radix RADIX. |
446 `------------------------------------------------------*/
448 /* Digits for number to ASCII conversions. */
449 static char const digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
452 ntoa (int32_t value, int radix)
456 static char str[256];
457 char *s = &str[sizeof str];
464 uvalue = -(uint32_t) value;
469 uvalue = (uint32_t) value;
474 *--s = digits[uvalue % radix];
484 /*---------------------------------------------------------------.
485 | Format an int VAL, and stuff it into an obstack OBS. Used for |
486 | macros expanding to numbers. |
487 `---------------------------------------------------------------*/
490 shipout_int (struct obstack *obs, int val)
494 s = ntoa ((int32_t) val, 10);
495 obstack_grow (obs, s, strlen (s));
498 /*-------------------------------------------------------------------.
499 | Print ARGC arguments from the table ARGV to obstack OBS, separated |
500 | by SEP, and quoted by the current quotes if QUOTED is true. |
501 `-------------------------------------------------------------------*/
504 dump_args (struct obstack *obs, int argc, token_data **argv,
505 const char *sep, bool quoted)
508 size_t len = strlen (sep);
510 for (i = 1; i < argc; i++)
513 obstack_grow (obs, sep, len);
515 obstack_grow (obs, lquote.string, lquote.length);
516 obstack_grow (obs, TOKEN_DATA_TEXT (argv[i]),
517 strlen (TOKEN_DATA_TEXT (argv[i])));
519 obstack_grow (obs, rquote.string, rquote.length);
523 /* The rest of this file is code for builtins and expansion of user
524 defined macros. All the functions for builtins have a prototype as:
526 void m4_MACRONAME (struct obstack *obs, int argc, char *argv[]);
528 The function are expected to leave their expansion on the obstack OBS,
529 as an unfinished object. ARGV is a table of ARGC pointers to the
530 individual arguments to the macro. Please note that in general
531 argv[argc] != NULL. */
533 /* The first section are macros for definining, undefining, examining,
534 changing, ... other macros. */
536 /*-------------------------------------------------------------------.
537 | The function define_macro is common for the builtins "define", |
538 | "undefine", "pushdef" and "popdef". ARGC and ARGV is as for the |
539 | caller, and MODE argument determines how the macro name is entered |
540 | into the symbol table. |
541 `-------------------------------------------------------------------*/
544 define_macro (int argc, token_data **argv, symbol_lookup mode)
548 if (bad_argc (argv[0], argc, 2, 3))
551 if (TOKEN_DATA_TYPE (argv[1]) != TOKEN_TEXT)
553 M4ERROR ((warning_status, 0,
554 "Warning: %s: invalid macro name ignored", ARG (0)));
560 define_user_macro (ARG (1), "", mode);
564 switch (TOKEN_DATA_TYPE (argv[2]))
567 define_user_macro (ARG (1), ARG (2), mode);
571 bp = find_builtin_by_addr (TOKEN_DATA_FUNC (argv[2]));
575 define_builtin (ARG (1), bp, mode);
580 M4ERROR ((warning_status, 0,
581 "INTERNAL ERROR: bad token data type in define_macro ()"));
587 m4_define (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
589 define_macro (argc, argv, SYMBOL_INSERT);
593 m4_undefine (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
596 if (bad_argc (argv[0], argc, 2, -1))
598 for (i = 1; i < argc; i++)
599 lookup_symbol (ARG (i), SYMBOL_DELETE);
603 m4_pushdef (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
605 define_macro (argc, argv, SYMBOL_PUSHDEF);
609 m4_popdef (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
612 if (bad_argc (argv[0], argc, 2, -1))
614 for (i = 1; i < argc; i++)
615 lookup_symbol (ARG (i), SYMBOL_POPDEF);
618 /*---------------------.
619 | Conditionals of m4. |
620 `---------------------*/
623 m4_ifdef (struct obstack *obs, int argc, token_data **argv)
628 if (bad_argc (argv[0], argc, 3, 4))
630 s = lookup_symbol (ARG (1), SYMBOL_LOOKUP);
632 if (s != NULL && SYMBOL_TYPE (s) != TOKEN_VOID)
640 obstack_grow (obs, result, strlen (result));
644 m4_ifelse (struct obstack *obs, int argc, token_data **argv)
647 token_data *me = argv[0];
652 if (bad_argc (me, argc, 4, -1))
655 /* Diagnose excess arguments if 5, 8, 11, etc., actual arguments. */
656 bad_argc (me, (argc + 2) % 3, -1, 1);
662 while (result == NULL)
664 if (STREQ (ARG (0), ARG (1)))
683 obstack_grow (obs, result, strlen (result));
686 /*-------------------------------------------------------------------.
687 | The function dump_symbol () is for use by "dumpdef". It builds up |
688 | a table of all defined, un-shadowed, symbols. |
689 `-------------------------------------------------------------------*/
691 /* The structure dump_symbol_data is used to pass the information needed
692 from call to call to dump_symbol. */
694 struct dump_symbol_data
696 struct obstack *obs; /* obstack for table */
697 symbol **base; /* base of table */
698 int size; /* size of table */
702 dump_symbol (symbol *sym, void *arg)
704 struct dump_symbol_data *data = (struct dump_symbol_data *) arg;
705 if (!SYMBOL_SHADOWED (sym) && SYMBOL_TYPE (sym) != TOKEN_VOID)
707 obstack_blank (data->obs, sizeof (symbol *));
708 data->base = (symbol **) obstack_base (data->obs);
709 data->base[data->size++] = sym;
713 /*------------------------------------------------------------------------.
714 | qsort comparison routine, for sorting the table made in m4_dumpdef (). |
715 `------------------------------------------------------------------------*/
718 dumpdef_cmp (const void *s1, const void *s2)
720 return strcmp (SYMBOL_NAME (* (symbol *const *) s1),
721 SYMBOL_NAME (* (symbol *const *) s2));
724 /*-------------------------------------------------------------.
725 | Implementation of "dumpdef" itself. It builds up a table of |
726 | pointers to symbols, sorts it and prints the sorted table. |
727 `-------------------------------------------------------------*/
730 m4_dumpdef (struct obstack *obs, int argc, token_data **argv)
734 struct dump_symbol_data data;
738 data.base = (symbol **) obstack_base (obs);
743 hack_all_symbols (dump_symbol, &data);
747 for (i = 1; i < argc; i++)
749 s = lookup_symbol (TOKEN_DATA_TEXT (argv[i]), SYMBOL_LOOKUP);
750 if (s != NULL && SYMBOL_TYPE (s) != TOKEN_VOID)
751 dump_symbol (s, &data);
753 M4ERROR ((warning_status, 0,
754 "undefined macro `%s'", TOKEN_DATA_TEXT (argv[i])));
758 /* Make table of symbols invisible to expand_macro (). */
760 obstack_finish (obs);
762 qsort (data.base, data.size, sizeof (symbol *), dumpdef_cmp);
764 for (; data.size > 0; --data.size, data.base++)
766 DEBUG_PRINT1 ("%s:\t", SYMBOL_NAME (data.base[0]));
768 switch (SYMBOL_TYPE (data.base[0]))
771 if (debug_level & DEBUG_TRACE_QUOTE)
772 DEBUG_PRINT3 ("%s%s%s\n",
773 lquote.string, SYMBOL_TEXT (data.base[0]), rquote.string);
775 DEBUG_PRINT1 ("%s\n", SYMBOL_TEXT (data.base[0]));
779 bp = find_builtin_by_addr (SYMBOL_FUNC (data.base[0]));
782 M4ERROR ((warning_status, 0, "\
783 INTERNAL ERROR: builtin not found in builtin table"));
786 DEBUG_PRINT1 ("<%s>\n", bp->name);
791 M4ERROR ((warning_status, 0,
792 "INTERNAL ERROR: bad token data type in m4_dumpdef ()"));
799 /*-----------------------------------------------------------------.
800 | The builtin "builtin" allows calls to builtin macros, even if |
801 | their definition has been overridden or shadowed. It is thus |
802 | possible to redefine builtins, and still access their original |
803 | definition. This macro is not available in compatibility mode. |
804 `-----------------------------------------------------------------*/
807 m4_builtin (struct obstack *obs, int argc, token_data **argv)
812 if (bad_argc (argv[0], argc, 2, -1))
814 if (TOKEN_DATA_TYPE (argv[1]) != TOKEN_TEXT)
816 M4ERROR ((warning_status, 0,
817 "Warning: %s: invalid macro name ignored", ARG (0)));
822 bp = find_builtin_by_name (name);
823 if (bp->func == m4_placeholder)
824 M4ERROR ((warning_status, 0,
825 "undefined builtin `%s'", name));
829 if (! bp->groks_macro_args)
830 for (i = 2; i < argc; i++)
831 if (TOKEN_DATA_TYPE (argv[i]) != TOKEN_TEXT)
833 TOKEN_DATA_TYPE (argv[i]) = TOKEN_TEXT;
834 TOKEN_DATA_TEXT (argv[i]) = (char *) "";
836 bp->func (obs, argc - 1, argv + 1);
840 /*-------------------------------------------------------------------.
841 | The builtin "indir" allows indirect calls to macros, even if their |
842 | name is not a proper macro name. It is thus possible to define |
843 | macros with ill-formed names for internal use in larger macro |
844 | packages. This macro is not available in compatibility mode. |
845 `-------------------------------------------------------------------*/
848 m4_indir (struct obstack *obs, int argc, token_data **argv)
853 if (bad_argc (argv[0], argc, 2, -1))
855 if (TOKEN_DATA_TYPE (argv[1]) != TOKEN_TEXT)
857 M4ERROR ((warning_status, 0,
858 "Warning: %s: invalid macro name ignored", ARG (0)));
863 s = lookup_symbol (name, SYMBOL_LOOKUP);
864 if (s == NULL || SYMBOL_TYPE (s) == TOKEN_VOID)
865 M4ERROR ((warning_status, 0,
866 "undefined macro `%s'", name));
870 if (! SYMBOL_MACRO_ARGS (s))
871 for (i = 2; i < argc; i++)
872 if (TOKEN_DATA_TYPE (argv[i]) != TOKEN_TEXT)
874 TOKEN_DATA_TYPE (argv[i]) = TOKEN_TEXT;
875 TOKEN_DATA_TEXT (argv[i]) = (char *) "";
877 call_macro (s, argc - 1, argv + 1, obs);
881 /*------------------------------------------------------------------.
882 | The macro "defn" returns the quoted definition of the macro named |
883 | by the first argument. If the macro is builtin, it will push a |
884 | special macro-definition token on the input stack. |
885 `------------------------------------------------------------------*/
888 m4_defn (struct obstack *obs, int argc, token_data **argv)
894 if (bad_argc (argv[0], argc, 2, -1))
897 assert (0 < argc && argc <= INT_MAX);
898 for (i = 1; i < (unsigned) argc; i++)
900 const char *arg = ARG((int) i);
901 s = lookup_symbol (arg, SYMBOL_LOOKUP);
905 switch (SYMBOL_TYPE (s))
908 obstack_grow (obs, lquote.string, lquote.length);
909 obstack_grow (obs, SYMBOL_TEXT (s), strlen (SYMBOL_TEXT (s)));
910 obstack_grow (obs, rquote.string, rquote.length);
915 if (b == m4_placeholder)
916 M4ERROR ((warning_status, 0, "\
917 builtin `%s' requested by frozen file is not supported", arg));
919 M4ERROR ((warning_status, 0,
920 "Warning: cannot concatenate builtin `%s'",
927 /* Nothing to do for traced but undefined macro. */
931 M4ERROR ((warning_status, 0,
932 "INTERNAL ERROR: bad symbol type in m4_defn ()"));
938 /*--------------------------------------------------------------.
939 | This section contains macros to handle the builtins "syscmd", |
940 | "esyscmd" and "sysval". "esyscmd" is GNU specific. |
941 `--------------------------------------------------------------*/
943 /* Exit code from last "syscmd" command. */
947 m4_syscmd (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
949 const char *cmd = ARG (1);
952 const char *prog_args[4] = { "sh", "-c" };
953 if (bad_argc (argv[0], argc, 2, 2) || !*cmd)
955 /* The empty command is successful. */
960 debug_flush_files ();
962 if (strstr (SYSCMD_SHELL, "cmd"))
964 prog_args[0] = "cmd";
970 status = execute (ARG (0), SYSCMD_SHELL, (char **) prog_args, false,
971 false, false, false, true, false, &sig_status);
974 assert (status == 127);
975 sysval = sig_status << 8;
979 if (status == 127 && errno)
980 M4ERROR ((warning_status, errno, "cannot run command `%s'", cmd));
986 m4_esyscmd (struct obstack *obs, int argc, token_data **argv)
988 const char *cmd = ARG (1);
989 const char *prog_args[4] = { "sh", "-c" };
996 if (bad_argc (argv[0], argc, 2, 2) || !*cmd)
998 /* The empty command is successful. */
1003 debug_flush_files ();
1005 if (strstr (SYSCMD_SHELL, "cmd"))
1007 prog_args[0] = "cmd";
1008 prog_args[1] = "/c";
1013 child = create_pipe_in (ARG (0), SYSCMD_SHELL, (char **) prog_args,
1014 NULL, false, true, false, &fd);
1017 M4ERROR ((warning_status, errno, "cannot run command `%s'", cmd));
1021 pin = fdopen (fd, "r");
1024 M4ERROR ((warning_status, errno, "cannot run command `%s'", cmd));
1031 size_t avail = obstack_room (obs);
1035 int ch = getc (pin);
1038 obstack_1grow (obs, ch);
1041 len = fread (obstack_next_free (obs), 1, avail, pin);
1044 obstack_blank_fast (obs, len);
1046 if (ferror (pin) || fclose (pin))
1047 M4ERROR ((EXIT_FAILURE, errno, "cannot read pipe"));
1049 status = wait_subprocess (child, ARG (0), false, true, true, false,
1053 assert (status == 127);
1054 sysval = sig_status << 8;
1058 if (status == 127 && errno)
1059 M4ERROR ((warning_status, errno, "cannot run command `%s'", cmd));
1065 m4_sysval (struct obstack *obs, int argc M4_GNUC_UNUSED,
1066 token_data **argv M4_GNUC_UNUSED)
1068 shipout_int (obs, sysval);
1071 /*------------------------------------------------------------------.
1072 | This section contains the top level code for the "eval" builtin. |
1073 | The actual work is done in the function evaluate (), which lives |
1075 `------------------------------------------------------------------*/
1078 m4_eval (struct obstack *obs, int argc, token_data **argv)
1085 if (bad_argc (argv[0], argc, 2, 4))
1088 if (*ARG (2) && !numeric_arg (argv[0], ARG (2), &radix))
1091 if (radix < 1 || radix > (int) strlen (digits))
1093 M4ERROR ((warning_status, 0,
1094 "radix %d in builtin `%s' out of range",
1099 if (argc >= 4 && !numeric_arg (argv[0], ARG (3), &min))
1103 M4ERROR ((warning_status, 0,
1104 "negative width to builtin `%s'", ARG (0)));
1109 M4ERROR ((warning_status, 0,
1110 "empty string treated as 0 in builtin `%s'", ARG (0)));
1111 else if (evaluate (ARG (1), &value))
1118 obstack_1grow (obs, '-');
1121 /* This assumes 2's-complement for correctly handling INT_MIN. */
1122 while (min-- - value > 0)
1123 obstack_1grow (obs, '0');
1124 while (value-- != 0)
1125 obstack_1grow (obs, '1');
1126 obstack_1grow (obs, '\0');
1130 s = ntoa (value, radix);
1134 obstack_1grow (obs, '-');
1137 for (min -= strlen (s); --min >= 0;)
1138 obstack_1grow (obs, '0');
1140 obstack_grow (obs, s, strlen (s));
1144 m4_incr (struct obstack *obs, int argc, token_data **argv)
1148 if (bad_argc (argv[0], argc, 2, 2))
1151 if (!numeric_arg (argv[0], ARG (1), &value))
1154 shipout_int (obs, value + 1);
1158 m4_decr (struct obstack *obs, int argc, token_data **argv)
1162 if (bad_argc (argv[0], argc, 2, 2))
1165 if (!numeric_arg (argv[0], ARG (1), &value))
1168 shipout_int (obs, value - 1);
1171 /* This section contains the macros "divert", "undivert" and "divnum" for
1172 handling diversion. The utility functions used lives in output.c. */
1174 /*-----------------------------------------------------------------.
1175 | Divert further output to the diversion given by ARGV[1]. Out of |
1176 | range means discard further output. |
1177 `-----------------------------------------------------------------*/
1180 m4_divert (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
1184 if (bad_argc (argv[0], argc, 1, 2))
1187 if (argc >= 2 && !numeric_arg (argv[0], ARG (1), &i))
1193 /*-----------------------------------------------------.
1194 | Expand to the current diversion number, -1 if none. |
1195 `-----------------------------------------------------*/
1198 m4_divnum (struct obstack *obs, int argc, token_data **argv)
1200 if (bad_argc (argv[0], argc, 1, 1))
1202 shipout_int (obs, current_diversion);
1205 /*------------------------------------------------------------------.
1206 | Bring back the diversion given by the argument list. If none is |
1207 | specified, bring back all diversions. GNU specific is the option |
1208 | of undiverting named files, by passing a non-numeric argument to |
1210 `------------------------------------------------------------------*/
1213 m4_undivert (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
1222 for (i = 1; i < argc; i++)
1224 file = strtol (ARG (i), &endp, 10);
1225 if (*endp == '\0' && !isspace (to_uchar (*ARG (i))))
1226 insert_diversion (file);
1227 else if (no_gnu_extensions)
1228 M4ERROR ((warning_status, 0,
1229 "non-numeric argument to builtin `%s'", ARG (0)));
1232 fp = m4_path_search (ARG (i), NULL);
1236 if (fclose (fp) == EOF)
1237 M4ERROR ((warning_status, errno,
1238 "error undiverting `%s'", ARG (i)));
1241 M4ERROR ((warning_status, errno,
1242 "cannot undivert `%s'", ARG (i)));
1247 /* This section contains various macros, which does not fall into any
1248 specific group. These are "dnl", "shift", "changequote", "changecom"
1249 and "changeword". */
1251 /*-----------------------------------------------------------.
1252 | Delete all subsequent whitespace from input. The function |
1253 | skip_line () lives in input.c. |
1254 `-----------------------------------------------------------*/
1257 m4_dnl (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
1259 if (bad_argc (argv[0], argc, 1, 1))
1265 /*--------------------------------------------------------------------.
1266 | Shift all arguments one to the left, discarding the first |
1267 | argument. Each output argument is quoted with the current quotes. |
1268 `--------------------------------------------------------------------*/
1271 m4_shift (struct obstack *obs, int argc, token_data **argv)
1273 if (bad_argc (argv[0], argc, 2, -1))
1275 dump_args (obs, argc - 1, argv + 1, ",", true);
1278 /*--------------------------------------------------------------------------.
1279 | Change the current quotes. The function set_quotes () lives in input.c. |
1280 `--------------------------------------------------------------------------*/
1283 m4_changequote (struct obstack *obs M4_GNUC_UNUSED, int argc,
1286 if (bad_argc (argv[0], argc, 1, 3))
1289 /* Explicit NULL distinguishes between empty and missing argument. */
1290 set_quotes ((argc >= 2) ? TOKEN_DATA_TEXT (argv[1]) : NULL,
1291 (argc >= 3) ? TOKEN_DATA_TEXT (argv[2]) : NULL);
1294 /*-----------------------------------------------------------------.
1295 | Change the current comment delimiters. The function set_comment |
1296 | () lives in input.c. |
1297 `-----------------------------------------------------------------*/
1300 m4_changecom (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
1302 if (bad_argc (argv[0], argc, 1, 3))
1305 /* Explicit NULL distinguishes between empty and missing argument. */
1306 set_comment ((argc >= 2) ? TOKEN_DATA_TEXT (argv[1]) : NULL,
1307 (argc >= 3) ? TOKEN_DATA_TEXT (argv[2]) : NULL);
1310 #ifdef ENABLE_CHANGEWORD
1312 /*---------------------------------------------------------------.
1313 | Change the regular expression used for breaking the input into |
1314 | words. The function set_word_regexp () lives in input.c. |
1315 `---------------------------------------------------------------*/
1318 m4_changeword (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
1320 if (bad_argc (argv[0], argc, 2, 2))
1323 set_word_regexp (TOKEN_DATA_TEXT (argv[1]));
1326 #endif /* ENABLE_CHANGEWORD */
1328 /* This section contains macros for inclusion of other files -- "include"
1329 and "sinclude". This differs from bringing back diversions, in that
1330 the input is scanned before being copied to the output. */
1332 /*---------------------------------------------------------------.
1333 | Generic include function. Include the file given by the first |
1334 | argument, if it exists. Complain about inaccessible files iff |
1335 | SILENT is false. |
1336 `---------------------------------------------------------------*/
1339 include (int argc, token_data **argv, bool silent)
1344 if (bad_argc (argv[0], argc, 2, 2))
1347 fp = m4_path_search (ARG (1), &name);
1352 M4ERROR ((warning_status, errno, "cannot open `%s'", ARG (1)));
1353 retcode = EXIT_FAILURE;
1358 push_file (fp, name, true);
1362 /*------------------------------------------------.
1363 | Include a file, complaining in case of errors. |
1364 `------------------------------------------------*/
1367 m4_include (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
1369 include (argc, argv, false);
1372 /*----------------------------------.
1373 | Include a file, ignoring errors. |
1374 `----------------------------------*/
1377 m4_sinclude (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
1379 include (argc, argv, true);
1382 /* More miscellaneous builtins -- "maketemp", "errprint", "__file__",
1383 "__line__", and "__program__". The last three are GNU specific. */
1385 /*------------------------------------------------------------------.
1386 | Use the first argument as at template for a temporary file name. |
1387 `------------------------------------------------------------------*/
1389 /* Add trailing 'X' to PATTERN of length LEN as necessary, then
1390 securely create the file, and place the quoted new file name on
1391 OBS. Report errors on behalf of ME. */
1393 mkstemp_helper (struct obstack *obs, const char *me, const char *pattern,
1400 /* Guarantee that there are six trailing 'X' characters, even if the
1401 user forgot to supply them. Output must be quoted if
1403 obstack_grow (obs, lquote.string, lquote.length);
1404 obstack_grow (obs, pattern, len);
1405 for (i = 0; len > 0 && i < 6; i++)
1406 if (pattern[len - i - 1] != 'X')
1408 obstack_grow0 (obs, "XXXXXX", 6 - i);
1409 name = (char *) obstack_base (obs) + lquote.length;
1412 fd = mkstemp (name);
1415 M4ERROR ((0, errno, "%s: cannot create tempfile `%s'", me, pattern));
1416 obstack_free (obs, obstack_finish (obs));
1421 /* Remove NUL, then finish quote. */
1422 obstack_blank (obs, -1);
1423 obstack_grow (obs, rquote.string, rquote.length);
1428 m4_maketemp (struct obstack *obs, int argc, token_data **argv)
1430 if (bad_argc (argv[0], argc, 2, 2))
1432 if (no_gnu_extensions)
1434 /* POSIX states "any trailing 'X' characters [are] replaced with
1435 the current process ID as a string", without referencing the
1436 file system. Horribly insecure, but we have to do it when we
1437 are in traditional mode.
1439 For reference, Solaris m4 does:
1442 maketemp(XX) -> `Xn', where n is last digit of pid
1443 maketemp(XXXXXXXX) -> `X00nnnnn', where nnnnn is 16-bit pid
1445 const char *str = ARG (1);
1446 int len = strlen (str);
1450 M4ERROR ((warning_status, 0, "recommend using mkstemp instead"));
1451 for (i = len; i > 1; i--)
1452 if (str[i - 1] != 'X')
1454 obstack_grow (obs, str, i);
1455 str = ntoa ((int32_t) getpid (), 10);
1456 len2 = strlen (str);
1458 obstack_grow0 (obs, str + len2 - (len - i), len - i);
1461 while (i++ < len - len2)
1462 obstack_1grow (obs, '0');
1463 obstack_grow0 (obs, str, len2);
1467 mkstemp_helper (obs, ARG (0), ARG (1), strlen (ARG (1)));
1471 m4_mkstemp (struct obstack *obs, int argc, token_data **argv)
1473 if (bad_argc (argv[0], argc, 2, 2))
1475 mkstemp_helper (obs, ARG (0), ARG (1), strlen (ARG (1)));
1478 /*----------------------------------------.
1479 | Print all arguments on standard error. |
1480 `----------------------------------------*/
1483 m4_errprint (struct obstack *obs, int argc, token_data **argv)
1485 if (bad_argc (argv[0], argc, 2, -1))
1487 dump_args (obs, argc, argv, " ", false);
1488 obstack_1grow (obs, '\0');
1489 debug_flush_files ();
1490 xfprintf (stderr, "%s", (char *) obstack_finish (obs));
1495 m4___file__ (struct obstack *obs, int argc, token_data **argv)
1497 if (bad_argc (argv[0], argc, 1, 1))
1499 obstack_grow (obs, lquote.string, lquote.length);
1500 obstack_grow (obs, current_file, strlen (current_file));
1501 obstack_grow (obs, rquote.string, rquote.length);
1505 m4___line__ (struct obstack *obs, int argc, token_data **argv)
1507 if (bad_argc (argv[0], argc, 1, 1))
1509 shipout_int (obs, current_line);
1513 m4___program__ (struct obstack *obs, int argc, token_data **argv)
1515 if (bad_argc (argv[0], argc, 1, 1))
1517 obstack_grow (obs, lquote.string, lquote.length);
1518 obstack_grow (obs, program_name, strlen (program_name));
1519 obstack_grow (obs, rquote.string, rquote.length);
1522 /* This section contains various macros for exiting, saving input until
1523 EOF is seen, and tracing macro calls. That is: "m4exit", "m4wrap",
1524 "traceon" and "traceoff". */
1526 /*----------------------------------------------------------.
1527 | Exit immediately, with exit status specified by the first |
1528 | argument, or 0 if no arguments are present. |
1529 `----------------------------------------------------------*/
1531 static void M4_GNUC_NORETURN
1532 m4_m4exit (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
1534 int exit_code = EXIT_SUCCESS;
1536 /* Warn on bad arguments, but still exit. */
1537 bad_argc (argv[0], argc, 1, 2);
1538 if (argc >= 2 && !numeric_arg (argv[0], ARG (1), &exit_code))
1539 exit_code = EXIT_FAILURE;
1540 if (exit_code < 0 || exit_code > 255)
1542 M4ERROR ((warning_status, 0,
1543 "exit status out of range: `%d'", exit_code));
1544 exit_code = EXIT_FAILURE;
1546 /* Change debug stream back to stderr, to force flushing debug stream and
1547 detect any errors it might have encountered. */
1548 debug_set_output (NULL);
1549 debug_flush_files ();
1550 if (exit_code == EXIT_SUCCESS && retcode != EXIT_SUCCESS)
1551 exit_code = retcode;
1552 /* Propagate non-zero status to atexit handlers. */
1553 if (exit_code != EXIT_SUCCESS)
1554 exit_failure = exit_code;
1558 /*------------------------------------------------------------------.
1559 | Save the argument text until EOF has been seen, allowing for user |
1560 | specified cleanup action. GNU version saves all arguments, the |
1561 | standard version only the first. |
1562 `------------------------------------------------------------------*/
1565 m4_m4wrap (struct obstack *obs, int argc, token_data **argv)
1567 if (bad_argc (argv[0], argc, 2, -1))
1569 if (no_gnu_extensions)
1570 obstack_grow (obs, ARG (1), strlen (ARG (1)));
1572 dump_args (obs, argc, argv, " ", false);
1573 obstack_1grow (obs, '\0');
1574 push_wrapup ((char *) obstack_finish (obs));
1577 /* Enable tracing of all specified macros, or all, if none is specified.
1578 Tracing is disabled by default, when a macro is defined. This can be
1579 overridden by the "t" debug flag. */
1581 /*------------------------------------------------------------------.
1582 | Set_trace () is used by "traceon" and "traceoff" to enable and |
1583 | disable tracing of a macro. It disables tracing if DATA is NULL, |
1584 | otherwise it enables tracing. |
1585 `------------------------------------------------------------------*/
1588 set_trace (symbol *sym, void *data)
1590 SYMBOL_TRACED (sym) = data != NULL;
1591 /* Remove placeholder from table if macro is undefined and untraced. */
1592 if (SYMBOL_TYPE (sym) == TOKEN_VOID && data == NULL)
1593 lookup_symbol (SYMBOL_NAME (sym), SYMBOL_POPDEF);
1597 m4_traceon (struct obstack *obs, int argc, token_data **argv)
1603 hack_all_symbols (set_trace, obs);
1605 for (i = 1; i < argc; i++)
1607 s = lookup_symbol (ARG (i), SYMBOL_LOOKUP);
1609 s = lookup_symbol (ARG (i), SYMBOL_INSERT);
1614 /*------------------------------------------------------------------------.
1615 | Disable tracing of all specified macros, or all, if none is specified. |
1616 `------------------------------------------------------------------------*/
1619 m4_traceoff (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
1625 hack_all_symbols (set_trace, NULL);
1627 for (i = 1; i < argc; i++)
1629 s = lookup_symbol (TOKEN_DATA_TEXT (argv[i]), SYMBOL_LOOKUP);
1631 set_trace (s, NULL);
1635 /*------------------------------------------------------------------.
1636 | On-the-fly control of the format of the tracing output. It takes |
1637 | one argument, which is a character string like given to the -d |
1638 | option, or none in which case the debug_level is zeroed. |
1639 `------------------------------------------------------------------*/
1642 m4_debugmode (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
1644 int new_debug_level;
1647 if (bad_argc (argv[0], argc, 1, 2))
1654 if (ARG (1)[0] == '+' || ARG (1)[0] == '-')
1656 change_flag = ARG (1)[0];
1657 new_debug_level = debug_decode (ARG (1) + 1);
1662 new_debug_level = debug_decode (ARG (1));
1665 if (new_debug_level < 0)
1666 M4ERROR ((warning_status, 0,
1667 "Debugmode: bad debug flags: `%s'", ARG (1)));
1670 switch (change_flag)
1673 debug_level = new_debug_level;
1677 debug_level |= new_debug_level;
1681 debug_level &= ~new_debug_level;
1685 M4ERROR ((warning_status, 0,
1686 "INTERNAL ERROR: bad flag in m4_debugmode ()"));
1693 /*-------------------------------------------------------------------------.
1694 | Specify the destination of the debugging output. With one argument, the |
1695 | argument is taken as a file name, with no arguments, revert to stderr. |
1696 `-------------------------------------------------------------------------*/
1699 m4_debugfile (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
1701 if (bad_argc (argv[0], argc, 1, 2))
1705 debug_set_output (NULL);
1706 else if (!debug_set_output (ARG (1)))
1707 M4ERROR ((warning_status, errno,
1708 "cannot set debug file `%s'", ARG (1)));
1711 /* This section contains text processing macros: "len", "index",
1712 "substr", "translit", "format", "regexp" and "patsubst". The last
1713 three are GNU specific. */
1715 /*---------------------------------------------.
1716 | Expand to the length of the first argument. |
1717 `---------------------------------------------*/
1720 m4_len (struct obstack *obs, int argc, token_data **argv)
1722 if (bad_argc (argv[0], argc, 2, 2))
1724 shipout_int (obs, strlen (ARG (1)));
1727 /*-------------------------------------------------------------------.
1728 | The macro expands to the first index of the second argument in the |
1730 `-------------------------------------------------------------------*/
1733 m4_index (struct obstack *obs, int argc, token_data **argv)
1735 const char *haystack;
1739 if (bad_argc (argv[0], argc, 3, 3))
1741 /* builtin(`index') is blank, but index(`abc') is 0. */
1743 shipout_int (obs, 0);
1748 result = strstr (haystack, ARG (2));
1749 retval = result ? result - haystack : -1;
1751 shipout_int (obs, retval);
1754 /*-----------------------------------------------------------------.
1755 | The macro "substr" extracts substrings from the first argument, |
1756 | starting from the index given by the second argument, extending |
1757 | for a length given by the third argument. If the third argument |
1758 | is missing, the substring extends to the end of the first |
1760 `-----------------------------------------------------------------*/
1763 m4_substr (struct obstack *obs, int argc, token_data **argv)
1768 if (bad_argc (argv[0], argc, 3, 4))
1770 /* builtin(`substr') is blank, but substr(`abc') is abc. */
1772 obstack_grow (obs, ARG (1), strlen (ARG (1)));
1776 length = avail = strlen (ARG (1));
1777 if (!numeric_arg (argv[0], ARG (2), &start))
1780 if (argc >= 4 && !numeric_arg (argv[0], ARG (3), &length))
1783 if (start < 0 || length <= 0 || start >= avail)
1786 if (start + length > avail)
1787 length = avail - start;
1788 obstack_grow (obs, ARG (1) + start, length);
1791 /*------------------------------------------------------------------.
1792 | For "translit", ranges are allowed in the second and third |
1793 | argument. They are expanded in the following function, and the |
1794 | expanded strings, without any ranges left, are used to translate |
1795 | the characters of the first argument. A single - (dash) can be |
1796 | included in the strings by being the first or the last character |
1797 | in the string. If the first character in a range is after the |
1798 | first in the character set, the range is made backwards, thus 9-0 |
1799 | is the string 9876543210. |
1800 `------------------------------------------------------------------*/
1803 expand_ranges (const char *s, struct obstack *obs)
1808 for (from = '\0'; *s != '\0'; from = to_uchar (*s++))
1810 if (*s == '-' && from != '\0')
1812 to = to_uchar (*++s);
1816 obstack_1grow (obs, '-');
1819 else if (from <= to)
1822 obstack_1grow (obs, from);
1826 while (--from >= to)
1827 obstack_1grow (obs, from);
1831 obstack_1grow (obs, *s);
1833 obstack_1grow (obs, '\0');
1834 return (char *) obstack_finish (obs);
1837 /*-----------------------------------------------------------------.
1838 | The macro "translit" translates all characters in the first |
1839 | argument, which are present in the second argument, into the |
1840 | corresponding character from the third argument. If the third |
1841 | argument is shorter than the second, the extra characters in the |
1842 | second argument are deleted from the first. |
1843 `-----------------------------------------------------------------*/
1846 m4_translit (struct obstack *obs, int argc, token_data **argv)
1848 const char *data = ARG (1);
1849 const char *from = ARG (2);
1851 char map[UCHAR_MAX + 1];
1852 char found[UCHAR_MAX + 1];
1855 if (bad_argc (argv[0], argc, 3, 4) || !*data || !*from)
1857 /* builtin(`translit') is blank, but translit(`abc') is abc. */
1859 obstack_grow (obs, data, strlen (data));
1864 if (strchr (to, '-') != NULL)
1866 to = expand_ranges (to, obs);
1870 /* If there are only one or two bytes to replace, it is faster to
1871 use memchr2. Using expand_ranges does nothing unless there are
1872 at least three bytes. */
1873 if (!from[1] || !from[2])
1876 size_t len = strlen (data);
1877 while ((p = (char *) memchr2 (data, from[0], from[1], len)))
1879 obstack_grow (obs, data, p - data);
1885 if (*p == from[0] && to[0])
1886 obstack_1grow (obs, to[0]);
1887 else if (*p == from[1] && to[0] && to[1])
1888 obstack_1grow (obs, to[1]);
1890 obstack_grow (obs, data, len);
1894 if (strchr (from, '-') != NULL)
1896 from = expand_ranges (from, obs);
1897 assert (from && *from);
1900 /* Calling strchr(from) for each character in data is quadratic,
1901 since both strings can be arbitrarily long. Instead, create a
1902 from-to mapping in one pass of from, then use that map in one
1903 pass of data, for linear behavior. Traditional behavior is that
1904 only the first instance of a character in from is consulted,
1905 hence the found map. */
1906 memset (map, 0, sizeof map);
1907 memset (found, 0, sizeof found);
1908 for ( ; (ch = *from) != '\0'; from++)
1919 for (data = ARG (1); (ch = *data) != '\0'; data++)
1922 obstack_1grow (obs, ch);
1924 obstack_1grow (obs, map[ch]);
1928 /*-------------------------------------------------------------------.
1929 | Frontend for printf like formatting. The function format () lives |
1930 | in the file format.c. |
1931 `-------------------------------------------------------------------*/
1934 m4_format (struct obstack *obs, int argc, token_data **argv)
1936 if (bad_argc (argv[0], argc, 2, -1))
1938 expand_format (obs, argc - 1, argv + 1);
1941 /*------------------------------------------------------------------.
1942 | Function to perform substitution by regular expressions. Used by |
1943 | the builtins regexp and patsubst. The changed text is placed on |
1944 | the obstack. The substitution is REPL, with \& substituted by |
1945 | this part of VICTIM matched by the last whole regular expression, |
1946 | taken from REGS[0], and \N substituted by the text matched by the |
1947 | Nth parenthesized sub-expression, taken from REGS[N]. |
1948 `------------------------------------------------------------------*/
1950 static int substitute_warned = 0;
1953 substitute (struct obstack *obs, const char *victim, const char *repl,
1954 struct re_registers *regs)
1960 const char *backslash = strchr (repl, '\\');
1963 obstack_grow (obs, repl, strlen (repl));
1966 obstack_grow (obs, repl, backslash - repl);
1972 if (!substitute_warned)
1974 M4ERROR ((warning_status, 0, "\
1975 Warning: \\0 will disappear, use \\& instead in replacements"));
1976 substitute_warned = 1;
1981 obstack_grow (obs, victim + regs->start[0],
1982 regs->end[0] - regs->start[0]);
1986 case '1': case '2': case '3': case '4': case '5': case '6':
1987 case '7': case '8': case '9':
1989 if (regs->num_regs - 1 <= ind)
1990 M4ERROR ((warning_status, 0,
1991 "Warning: sub-expression %d not present", ch));
1992 else if (regs->end[ch] > 0)
1993 obstack_grow (obs, victim + regs->start[ch],
1994 regs->end[ch] - regs->start[ch]);
1999 M4ERROR ((warning_status, 0,
2000 "Warning: trailing \\ ignored in replacement"));
2004 obstack_1grow (obs, ch);
2011 /*------------------------------------------.
2012 | Initialize regular expression variables. |
2013 `------------------------------------------*/
2016 init_pattern_buffer (struct re_pattern_buffer *buf, struct re_registers *regs)
2018 buf->translate = NULL;
2019 buf->fastmap = NULL;
2029 /*------------------------------------------------------------------.
2030 | Regular expression version of index. Given two arguments, expand |
2031 | to the index of the first match of the second argument (a regexp) |
2032 | in the first. Expand to -1 if here is no match. Given a third |
2033 | argument, it changes the expansion to this argument. |
2034 `------------------------------------------------------------------*/
2037 m4_regexp (struct obstack *obs, int argc, token_data **argv)
2039 const char *victim; /* first argument */
2040 const char *regexp; /* regular expression */
2041 const char *repl; /* replacement string */
2043 struct re_pattern_buffer buf; /* compiled regular expression */
2044 struct re_registers regs; /* for subexpression matches */
2045 const char *msg; /* error message from re_compile_pattern */
2046 int startpos; /* start position of match */
2047 int length; /* length of first argument */
2049 if (bad_argc (argv[0], argc, 3, 4))
2051 /* builtin(`regexp') is blank, but regexp(`abc') is 0. */
2053 shipout_int (obs, 0);
2057 victim = TOKEN_DATA_TEXT (argv[1]);
2058 regexp = TOKEN_DATA_TEXT (argv[2]);
2060 init_pattern_buffer (&buf, ®s);
2061 msg = re_compile_pattern (regexp, strlen (regexp), &buf);
2065 M4ERROR ((warning_status, 0,
2066 "bad regular expression: `%s': %s", regexp, msg));
2067 free_pattern_buffer (&buf, ®s);
2071 length = strlen (victim);
2072 /* Avoid overhead of allocating regs if we won't use it. */
2073 startpos = re_search (&buf, victim, length, 0, length,
2074 argc == 3 ? NULL : ®s);
2077 M4ERROR ((warning_status, 0,
2078 "error matching regular expression `%s'", regexp));
2080 shipout_int (obs, startpos);
2081 else if (startpos >= 0)
2083 repl = TOKEN_DATA_TEXT (argv[3]);
2084 substitute (obs, victim, repl, ®s);
2087 free_pattern_buffer (&buf, ®s);
2090 /*--------------------------------------------------------------------------.
2091 | Substitute all matches of a regexp occuring in a string. Each match of |
2092 | the second argument (a regexp) in the first argument is changed to the |
2093 | third argument, with \& substituted by the matched text, and \N |
2094 | substituted by the text matched by the Nth parenthesized sub-expression. |
2095 `--------------------------------------------------------------------------*/
2098 m4_patsubst (struct obstack *obs, int argc, token_data **argv)
2100 const char *victim; /* first argument */
2101 const char *regexp; /* regular expression */
2103 struct re_pattern_buffer buf; /* compiled regular expression */
2104 struct re_registers regs; /* for subexpression matches */
2105 const char *msg; /* error message from re_compile_pattern */
2106 int matchpos; /* start position of match */
2107 int offset; /* current match offset */
2108 int length; /* length of first argument */
2110 if (bad_argc (argv[0], argc, 3, 4))
2112 /* builtin(`patsubst') is blank, but patsubst(`abc') is abc. */
2114 obstack_grow (obs, ARG (1), strlen (ARG (1)));
2118 regexp = TOKEN_DATA_TEXT (argv[2]);
2120 init_pattern_buffer (&buf, ®s);
2121 msg = re_compile_pattern (regexp, strlen (regexp), &buf);
2125 M4ERROR ((warning_status, 0,
2126 "bad regular expression `%s': %s", regexp, msg));
2131 victim = TOKEN_DATA_TEXT (argv[1]);
2132 length = strlen (victim);
2135 while (offset <= length)
2137 matchpos = re_search (&buf, victim, length,
2138 offset, length - offset, ®s);
2142 /* Match failed -- either error or there is no match in the
2143 rest of the string, in which case the rest of the string is
2147 M4ERROR ((warning_status, 0,
2148 "error matching regular expression `%s'", regexp));
2149 else if (offset < length)
2150 obstack_grow (obs, victim + offset, length - offset);
2154 /* Copy the part of the string that was skipped by re_search (). */
2156 if (matchpos > offset)
2157 obstack_grow (obs, victim + offset, matchpos - offset);
2159 /* Handle the part of the string that was covered by the match. */
2161 substitute (obs, victim, ARG (3), ®s);
2163 /* Update the offset to the end of the match. If the regexp
2164 matched a null string, advance offset one more, to avoid
2167 offset = regs.end[0];
2168 if (regs.start[0] == regs.end[0])
2169 obstack_1grow (obs, victim[offset++]);
2171 obstack_1grow (obs, '\0');
2173 free_pattern_buffer (&buf, ®s);
2176 /* Finally, a placeholder builtin. This builtin is not installed by
2177 default, but when reading back frozen files, this is associated
2178 with any builtin we don't recognize (for example, if the frozen
2179 file was created with a changeword capable m4, but is then loaded
2180 by a different m4 that does not support changeword). This way, we
2181 can keep 'm4 -R' quiet in the common case that the user did not
2182 know or care about the builtin when the frozen file was created,
2183 while still flagging it as a potential error if an attempt is made
2184 to actually use the builtin. */
2186 /*--------------------------------------------------------------------.
2187 | Issue a warning that this macro is a placeholder for an unsupported |
2188 | builtin that was requested while reloading a frozen file. |
2189 `--------------------------------------------------------------------*/
2192 m4_placeholder (struct obstack *obs M4_GNUC_UNUSED, int argc,
2195 M4ERROR ((warning_status, 0, "\
2196 builtin `%s' requested by frozen file is not supported", ARG (0)));
2199 /*-------------------------------------------------------------------.
2200 | This function handles all expansion of user defined and predefined |
2201 | macros. It is called with an obstack OBS, where the macros |
2202 | expansion will be placed, as an unfinished object. SYM points to |
2203 | the macro definition, giving the expansion text. ARGC and ARGV |
2204 | are the arguments, as usual. |
2205 `-------------------------------------------------------------------*/
2208 expand_user_macro (struct obstack *obs, symbol *sym,
2209 int argc, token_data **argv)
2211 const char *text = SYMBOL_TEXT (sym);
2215 const char *dollar = strchr (text, '$');
2218 obstack_grow (obs, text, strlen (text));
2221 obstack_grow (obs, text, dollar - text);
2225 case '0': case '1': case '2': case '3': case '4':
2226 case '5': case '6': case '7': case '8': case '9':
2227 if (no_gnu_extensions)
2233 for (i = 0; isdigit (to_uchar (*text)); text++)
2234 i = i*10 + (*text - '0');
2237 obstack_grow (obs, TOKEN_DATA_TEXT (argv[i]),
2238 strlen (TOKEN_DATA_TEXT (argv[i])));
2241 case '#': /* number of arguments */
2242 shipout_int (obs, argc - 1);
2246 case '*': /* all arguments */
2247 case '@': /* ... same, but quoted */
2248 dump_args (obs, argc, argv, ",", *text == '@');
2253 obstack_1grow (obs, '$');