1 /* GNU m4 -- A simple macro processor
3 Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 2000, 2004, 2006
4 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 /* Code for all builtin macros, initialization of symbol table, and
23 expansion of user defined macros. */
27 extern FILE *popen ();
33 # include <sys/wait.h>
36 #define ARG(i) (argc > (i) ? TOKEN_DATA_TEXT (argv[i]) : "")
38 /* Initialization of builtin and predefined macros. The table
39 "builtin_tab" is both used for initialization, and by the "builtin"
42 #define DECLARE(name) \
43 static void name (struct obstack *, int, token_data **)
45 DECLARE (m4___file__
);
46 DECLARE (m4___line__
);
47 DECLARE (m4___program__
);
49 DECLARE (m4_changecom
);
50 DECLARE (m4_changequote
);
51 #ifdef ENABLE_CHANGEWORD
52 DECLARE (m4_changeword
);
54 DECLARE (m4_debugmode
);
55 DECLARE (m4_debugfile
);
63 DECLARE (m4_errprint
);
76 DECLARE (m4_maketemp
);
78 DECLARE (m4_patsubst
);
83 DECLARE (m4_sinclude
);
87 DECLARE (m4_traceoff
);
89 DECLARE (m4_translit
);
90 DECLARE (m4_undefine
);
91 DECLARE (m4_undivert
);
99 /* name GNUext macros blind function */
101 { "__file__", true, false, false, m4___file__
},
102 { "__line__", true, false, false, m4___line__
},
103 { "__program__", true, false, false, m4___program__
},
104 { "builtin", true, true, true, m4_builtin
},
105 { "changecom", false, false, false, m4_changecom
},
106 { "changequote", false, false, false, m4_changequote
},
107 #ifdef ENABLE_CHANGEWORD
108 { "changeword", true, false, true, m4_changeword
},
110 { "debugmode", true, false, false, m4_debugmode
},
111 { "debugfile", true, false, false, m4_debugfile
},
112 { "decr", false, false, true, m4_decr
},
113 { "define", false, true, true, m4_define
},
114 { "defn", false, false, true, m4_defn
},
115 { "divert", false, false, false, m4_divert
},
116 { "divnum", false, false, false, m4_divnum
},
117 { "dnl", false, false, false, m4_dnl
},
118 { "dumpdef", false, false, false, m4_dumpdef
},
119 { "errprint", false, false, true, m4_errprint
},
120 { "esyscmd", true, false, true, m4_esyscmd
},
121 { "eval", false, false, true, m4_eval
},
122 { "format", true, false, true, m4_format
},
123 { "ifdef", false, false, true, m4_ifdef
},
124 { "ifelse", false, false, true, m4_ifelse
},
125 { "include", false, false, true, m4_include
},
126 { "incr", false, false, true, m4_incr
},
127 { "index", false, false, true, m4_index
},
128 { "indir", true, true, true, m4_indir
},
129 { "len", false, false, true, m4_len
},
130 { "m4exit", false, false, false, m4_m4exit
},
131 { "m4wrap", false, false, true, m4_m4wrap
},
132 { "maketemp", false, false, true, m4_maketemp
},
133 { "mkstemp", false, false, true, m4_mkstemp
},
134 { "patsubst", true, false, true, m4_patsubst
},
135 { "popdef", false, false, true, m4_popdef
},
136 { "pushdef", false, true, true, m4_pushdef
},
137 { "regexp", true, false, true, m4_regexp
},
138 { "shift", false, false, true, m4_shift
},
139 { "sinclude", false, false, true, m4_sinclude
},
140 { "substr", false, false, true, m4_substr
},
141 { "syscmd", false, false, true, m4_syscmd
},
142 { "sysval", false, false, false, m4_sysval
},
143 { "traceoff", false, false, false, m4_traceoff
},
144 { "traceon", false, false, false, m4_traceon
},
145 { "translit", false, false, true, m4_translit
},
146 { "undefine", false, false, true, m4_undefine
},
147 { "undivert", false, false, false, m4_undivert
},
149 { 0, false, false, false, 0 },
151 /* placeholder is intentionally stuck after the table end delimiter,
152 so that we can easily find it, while not treating it as a real
154 { "placeholder", true, false, false, m4_placeholder
},
157 static predefined
const
161 { "unix", "__unix__", "" },
163 { "windows", "__windows__", "" },
165 { "os2", "__os2__", "" },
167 # warning Platform macro not provided
169 { NULL
, "__gnu__", "" },
171 { NULL
, NULL
, NULL
},
174 /*----------------------------------------.
175 | Find the builtin, which lives on ADDR. |
176 `----------------------------------------*/
179 find_builtin_by_addr (builtin_func
*func
)
183 for (bp
= &builtin_tab
[0]; bp
->name
!= NULL
; bp
++)
184 if (bp
->func
== func
)
186 if (func
== m4_placeholder
)
191 /*----------------------------------------------------------.
192 | Find the builtin, which has NAME. On failure, return the |
193 | placeholder builtin. |
194 `----------------------------------------------------------*/
197 find_builtin_by_name (const char *name
)
201 for (bp
= &builtin_tab
[0]; bp
->name
!= NULL
; bp
++)
202 if (strcmp (bp
->name
, name
) == 0)
207 /*-------------------------------------------------------------------------.
208 | Install a builtin macro with name NAME, bound to the C function given in |
209 | BP. MODE is SYMBOL_INSERT or SYMBOL_PUSHDEF. TRACED defines whether |
210 | NAME is to be traced. |
211 `-------------------------------------------------------------------------*/
214 define_builtin (const char *name
, const builtin
*bp
, symbol_lookup mode
)
218 sym
= lookup_symbol (name
, mode
);
219 SYMBOL_TYPE (sym
) = TOKEN_FUNC
;
220 SYMBOL_MACRO_ARGS (sym
) = bp
->groks_macro_args
;
221 SYMBOL_BLIND_NO_ARGS (sym
) = bp
->blind_if_no_args
;
222 SYMBOL_FUNC (sym
) = bp
->func
;
225 /*-------------------------------------------------------------------------.
226 | Define a predefined or user-defined macro, with name NAME, and expansion |
227 | TEXT. MODE destinguishes between the "define" and the "pushdef" case. |
228 | It is also used from main (). |
229 `-------------------------------------------------------------------------*/
232 define_user_macro (const char *name
, const char *text
, symbol_lookup mode
)
236 s
= lookup_symbol (name
, mode
);
237 if (SYMBOL_TYPE (s
) == TOKEN_TEXT
)
238 free (SYMBOL_TEXT (s
));
240 SYMBOL_TYPE (s
) = TOKEN_TEXT
;
241 SYMBOL_TEXT (s
) = xstrdup (text
? text
: "");
244 /*-----------------------------------------------.
245 | Initialize all builtin and predefined macros. |
246 `-----------------------------------------------*/
252 const predefined
*pp
;
255 for (bp
= &builtin_tab
[0]; bp
->name
!= NULL
; bp
++)
256 if (!no_gnu_extensions
|| !bp
->gnu_extension
)
258 if (prefix_all_builtins
)
260 string
= (char *) xmalloc (strlen (bp
->name
) + 4);
261 strcpy (string
, "m4_");
262 strcat (string
, bp
->name
);
263 define_builtin (string
, bp
, SYMBOL_INSERT
);
267 define_builtin (bp
->name
, bp
, SYMBOL_INSERT
);
270 for (pp
= &predefined_tab
[0]; pp
->func
!= NULL
; pp
++)
271 if (no_gnu_extensions
)
273 if (pp
->unix_name
!= NULL
)
274 define_user_macro (pp
->unix_name
, pp
->func
, SYMBOL_INSERT
);
278 if (pp
->gnu_name
!= NULL
)
279 define_user_macro (pp
->gnu_name
, pp
->func
, SYMBOL_INSERT
);
283 /*------------------------------------------------------------------------.
284 | Give friendly warnings if a builtin macro is passed an inappropriate |
285 | number of arguments. NAME is macro name for messages, ARGC is actual |
286 | number of arguments, MIN is the minimum number of acceptable arguments, |
287 | negative if not applicable, MAX is the maximum number, negative if not |
289 `------------------------------------------------------------------------*/
292 bad_argc (token_data
*name
, int argc
, int min
, int max
)
296 if (min
> 0 && argc
< min
)
298 if (!suppress_warnings
)
299 M4ERROR ((warning_status
, 0,
300 "Warning: too few arguments to builtin `%s'",
301 TOKEN_DATA_TEXT (name
)));
304 else if (max
> 0 && argc
> max
&& !suppress_warnings
)
305 M4ERROR ((warning_status
, 0,
306 "Warning: excess arguments to builtin `%s' ignored",
307 TOKEN_DATA_TEXT (name
)));
312 /*--------------------------------------------------------------------------.
313 | The function numeric_arg () converts ARG to an int pointed to by VALUEP. |
314 | If the conversion fails, print error message for macro MACRO. Return |
315 | true iff conversion succeeds. |
316 `--------------------------------------------------------------------------*/
319 numeric_arg (token_data
*macro
, const char *arg
, int *valuep
)
326 M4ERROR ((warning_status
, 0,
327 "empty string treated as 0 in builtin `%s'",
328 TOKEN_DATA_TEXT (macro
)));
333 *valuep
= strtol (arg
, &endp
, 10);
336 M4ERROR ((warning_status
, 0,
337 "non-numeric argument to builtin `%s'",
338 TOKEN_DATA_TEXT (macro
)));
341 if (isspace (to_uchar (*arg
)))
342 M4ERROR ((warning_status
, 0,
343 "leading whitespace ignored in builtin `%s'",
344 TOKEN_DATA_TEXT (macro
)));
345 else if (errno
== ERANGE
)
346 M4ERROR ((warning_status
, 0,
347 "numeric overflow detected in builtin `%s'",
348 TOKEN_DATA_TEXT (macro
)));
353 /*------------------------------------------------------------------------.
354 | The function ntoa () converts VALUE to a signed ascii representation in |
356 `------------------------------------------------------------------------*/
358 /* Digits for number to ascii conversions. */
359 static char const digits
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
362 ntoa (eval_t value
, int radix
)
365 unsigned_eval_t uvalue
;
366 static char str
[256];
367 char *s
= &str
[sizeof str
];
374 uvalue
= (unsigned_eval_t
) -value
;
379 uvalue
= (unsigned_eval_t
) value
;
384 *--s
= digits
[uvalue
% radix
];
394 /*----------------------------------------------------------------------.
395 | Format an int VAL, and stuff it into an obstack OBS. Used for macros |
396 | expanding to numbers. |
397 `----------------------------------------------------------------------*/
400 shipout_int (struct obstack
*obs
, int val
)
404 s
= ntoa ((eval_t
) val
, 10);
405 obstack_grow (obs
, s
, strlen (s
));
408 /*----------------------------------------------------------------------.
409 | Print ARGC arguments from the table ARGV to obstack OBS, separated by |
410 | SEP, and quoted by the current quotes, if QUOTED is true. |
411 `----------------------------------------------------------------------*/
414 dump_args (struct obstack
*obs
, int argc
, token_data
**argv
,
415 const char *sep
, bool quoted
)
418 size_t len
= strlen (sep
);
420 for (i
= 1; i
< argc
; i
++)
423 obstack_grow (obs
, sep
, len
);
425 obstack_grow (obs
, lquote
.string
, lquote
.length
);
426 obstack_grow (obs
, TOKEN_DATA_TEXT (argv
[i
]),
427 strlen (TOKEN_DATA_TEXT (argv
[i
])));
429 obstack_grow (obs
, rquote
.string
, rquote
.length
);
433 /* The rest of this file is code for builtins and expansion of user
434 defined macros. All the functions for builtins have a prototype as:
436 void m4_MACRONAME (struct obstack *obs, int argc, char *argv[]);
438 The function are expected to leave their expansion on the obstack OBS,
439 as an unfinished object. ARGV is a table of ARGC pointers to the
440 individual arguments to the macro. Please note that in general
441 argv[argc] != NULL. */
443 /* The first section are macros for definining, undefining, examining,
444 changing, ... other macros. */
446 /*-------------------------------------------------------------------------.
447 | The function define_macro is common for the builtins "define", |
448 | "undefine", "pushdef" and "popdef". ARGC and ARGV is as for the caller, |
449 | and MODE argument determines how the macro name is entered into the |
451 `-------------------------------------------------------------------------*/
454 define_macro (int argc
, token_data
**argv
, symbol_lookup mode
)
458 if (bad_argc (argv
[0], argc
, 2, 3))
461 if (TOKEN_DATA_TYPE (argv
[1]) != TOKEN_TEXT
)
463 M4ERROR ((warning_status
, 0,
464 "Warning: %s: invalid macro name ignored", ARG (0)));
470 define_user_macro (ARG (1), "", mode
);
474 switch (TOKEN_DATA_TYPE (argv
[2]))
477 define_user_macro (ARG (1), ARG (2), mode
);
481 bp
= find_builtin_by_addr (TOKEN_DATA_FUNC (argv
[2]));
485 define_builtin (ARG (1), bp
, mode
);
489 M4ERROR ((warning_status
, 0,
490 "INTERNAL ERROR: bad token data type in define_macro ()"));
496 m4_define (struct obstack
*obs
, int argc
, token_data
**argv
)
498 define_macro (argc
, argv
, SYMBOL_INSERT
);
502 m4_undefine (struct obstack
*obs
, int argc
, token_data
**argv
)
505 if (bad_argc (argv
[0], argc
, 2, -1))
507 for (i
= 1; i
< argc
; i
++)
508 lookup_symbol (ARG (i
), SYMBOL_DELETE
);
512 m4_pushdef (struct obstack
*obs
, int argc
, token_data
**argv
)
514 define_macro (argc
, argv
, SYMBOL_PUSHDEF
);
518 m4_popdef (struct obstack
*obs
, int argc
, token_data
**argv
)
521 if (bad_argc (argv
[0], argc
, 2, -1))
523 for (i
= 1; i
< argc
; i
++)
524 lookup_symbol (ARG (i
), SYMBOL_POPDEF
);
527 /*---------------------.
528 | Conditionals of m4. |
529 `---------------------*/
532 m4_ifdef (struct obstack
*obs
, int argc
, token_data
**argv
)
537 if (bad_argc (argv
[0], argc
, 3, 4))
539 s
= lookup_symbol (ARG (1), SYMBOL_LOOKUP
);
541 if (s
!= NULL
&& SYMBOL_TYPE (s
) != TOKEN_VOID
)
549 obstack_grow (obs
, result
, strlen (result
));
553 m4_ifelse (struct obstack
*obs
, int argc
, token_data
**argv
)
561 if (bad_argc (argv
[0], argc
, 4, -1))
564 /* Diagnose excess arguments if 5, 8, 11, etc., actual arguments. */
565 bad_argc (argv
[0], (argc
+ 2) % 3, -1, 1);
572 while (result
== NULL
)
574 if (strcmp (ARG (0), ARG (1)) == 0)
593 obstack_grow (obs
, result
, strlen (result
));
596 /*---------------------------------------------------------------------.
597 | The function dump_symbol () is for use by "dumpdef". It builds up a |
598 | table of all defined, un-shadowed, symbols. |
599 `---------------------------------------------------------------------*/
601 /* The structure dump_symbol_data is used to pass the information needed
602 from call to call to dump_symbol. */
604 struct dump_symbol_data
606 struct obstack
*obs
; /* obstack for table */
607 symbol
**base
; /* base of table */
608 int size
; /* size of table */
612 dump_symbol (symbol
*sym
, void *arg
)
614 struct dump_symbol_data
*data
= (struct dump_symbol_data
*) arg
;
615 if (!SYMBOL_SHADOWED (sym
) && SYMBOL_TYPE (sym
) != TOKEN_VOID
)
617 obstack_blank (data
->obs
, sizeof (symbol
*));
618 data
->base
= (symbol
**) obstack_base (data
->obs
);
619 data
->base
[data
->size
++] = sym
;
623 /*------------------------------------------------------------------------.
624 | qsort comparison routine, for sorting the table made in m4_dumpdef (). |
625 `------------------------------------------------------------------------*/
628 dumpdef_cmp (const void *s1
, const void *s2
)
630 return strcmp (SYMBOL_NAME (* (symbol
*const *) s1
),
631 SYMBOL_NAME (* (symbol
*const *) s2
));
634 /*-------------------------------------------------------------------------.
635 | Implementation of "dumpdef" itself. It builds up a table of pointers to |
636 | symbols, sorts it and prints the sorted table. |
637 `-------------------------------------------------------------------------*/
640 m4_dumpdef (struct obstack
*obs
, int argc
, token_data
**argv
)
644 struct dump_symbol_data data
;
648 data
.base
= (symbol
**) obstack_base (obs
);
653 hack_all_symbols (dump_symbol
, &data
);
657 for (i
= 1; i
< argc
; i
++)
659 s
= lookup_symbol (TOKEN_DATA_TEXT (argv
[i
]), SYMBOL_LOOKUP
);
660 if (s
!= NULL
&& SYMBOL_TYPE (s
) != TOKEN_VOID
)
661 dump_symbol (s
, &data
);
663 M4ERROR ((warning_status
, 0,
664 "undefined macro `%s'", TOKEN_DATA_TEXT (argv
[i
])));
668 /* Make table of symbols invisible to expand_macro (). */
670 obstack_finish (obs
);
672 qsort (data
.base
, data
.size
, sizeof (symbol
*), dumpdef_cmp
);
674 for (; data
.size
> 0; --data
.size
, data
.base
++)
676 DEBUG_PRINT1 ("%s:\t", SYMBOL_NAME (data
.base
[0]));
678 switch (SYMBOL_TYPE (data
.base
[0]))
681 if (debug_level
& DEBUG_TRACE_QUOTE
)
682 DEBUG_PRINT3 ("%s%s%s\n",
683 lquote
.string
, SYMBOL_TEXT (data
.base
[0]), rquote
.string
);
685 DEBUG_PRINT1 ("%s\n", SYMBOL_TEXT (data
.base
[0]));
689 bp
= find_builtin_by_addr (SYMBOL_FUNC (data
.base
[0]));
692 M4ERROR ((warning_status
, 0, "\
693 INTERNAL ERROR: builtin not found in builtin table"));
696 DEBUG_PRINT1 ("<%s>\n", bp
->name
);
700 M4ERROR ((warning_status
, 0,
701 "INTERNAL ERROR: bad token data type in m4_dumpdef ()"));
708 /*---------------------------------------------------------------------.
709 | The builtin "builtin" allows calls to builtin macros, even if their |
710 | definition has been overridden or shadowed. It is thus possible to |
711 | redefine builtins, and still access their original definition. This |
712 | macro is not available in compatibility mode. |
713 `---------------------------------------------------------------------*/
716 m4_builtin (struct obstack
*obs
, int argc
, token_data
**argv
)
721 if (bad_argc (argv
[0], argc
, 2, -1))
723 if (TOKEN_DATA_TYPE (argv
[1]) != TOKEN_TEXT
)
725 M4ERROR ((warning_status
, 0,
726 "Warning: %s: invalid macro name ignored", ARG (0)));
731 bp
= find_builtin_by_name (name
);
732 if (bp
->func
== m4_placeholder
)
733 M4ERROR ((warning_status
, 0,
734 "undefined builtin `%s'", name
));
738 if (! bp
->groks_macro_args
)
739 for (i
= 2; i
< argc
; i
++)
740 if (TOKEN_DATA_TYPE (argv
[i
]) != TOKEN_TEXT
)
742 TOKEN_DATA_TYPE (argv
[i
]) = TOKEN_TEXT
;
743 TOKEN_DATA_TEXT (argv
[i
]) = (char *) "";
745 bp
->func (obs
, argc
- 1, argv
+ 1);
749 /*------------------------------------------------------------------------.
750 | The builtin "indir" allows indirect calls to macros, even if their name |
751 | is not a proper macro name. It is thus possible to define macros with |
752 | ill-formed names for internal use in larger macro packages. This macro |
753 | is not available in compatibility mode. |
754 `------------------------------------------------------------------------*/
757 m4_indir (struct obstack
*obs
, int argc
, token_data
**argv
)
762 if (bad_argc (argv
[0], argc
, 2, -1))
764 if (TOKEN_DATA_TYPE (argv
[1]) != TOKEN_TEXT
)
766 M4ERROR ((warning_status
, 0,
767 "Warning: %s: invalid macro name ignored", ARG (0)));
772 s
= lookup_symbol (name
, SYMBOL_LOOKUP
);
773 if (s
== NULL
|| SYMBOL_TYPE (s
) == TOKEN_VOID
)
774 M4ERROR ((warning_status
, 0,
775 "undefined macro `%s'", name
));
779 if (! SYMBOL_MACRO_ARGS (s
))
780 for (i
= 2; i
< argc
; i
++)
781 if (TOKEN_DATA_TYPE (argv
[i
]) != TOKEN_TEXT
)
783 TOKEN_DATA_TYPE (argv
[i
]) = TOKEN_TEXT
;
784 TOKEN_DATA_TEXT (argv
[i
]) = (char *) "";
786 call_macro (s
, argc
- 1, argv
+ 1, obs
);
790 /*-------------------------------------------------------------------------.
791 | The macro "defn" returns the quoted definition of the macro named by the |
792 | first argument. If the macro is builtin, it will push a special |
793 | macro-definition token on the input stack. |
794 `-------------------------------------------------------------------------*/
797 m4_defn (struct obstack
*obs
, int argc
, token_data
**argv
)
802 if (bad_argc (argv
[0], argc
, 2, 2))
805 s
= lookup_symbol (ARG (1), SYMBOL_LOOKUP
);
809 switch (SYMBOL_TYPE (s
))
812 obstack_grow (obs
, lquote
.string
, lquote
.length
);
813 obstack_grow (obs
, SYMBOL_TEXT (s
), strlen (SYMBOL_TEXT (s
)));
814 obstack_grow (obs
, rquote
.string
, rquote
.length
);
819 if (b
== m4_placeholder
)
820 M4ERROR ((warning_status
, 0, "\
821 builtin `%s' requested by frozen file is not supported", ARG (1)));
830 M4ERROR ((warning_status
, 0,
831 "INTERNAL ERROR: bad symbol type in m4_defn ()"));
836 /*------------------------------------------------------------------------.
837 | This section contains macros to handle the builtins "syscmd", "esyscmd" |
838 | and "sysval". "esyscmd" is GNU specific. |
839 `------------------------------------------------------------------------*/
841 /* Helper macros for readability. */
842 #if UNIX || defined WEXITSTATUS
843 # define M4SYSVAL_EXITBITS(status) \
844 (WIFEXITED (status) ? WEXITSTATUS (status) : 0)
845 # define M4SYSVAL_TERMSIGBITS(status) \
846 (WIFSIGNALED (status) ? WTERMSIG (status) << 8 : 0)
848 #else /* ! UNIX && ! defined WEXITSTATUS */
849 /* Platforms such as mingw do not support the notion of reporting
850 which signal terminated a process. Furthermore if WEXITSTATUS was
851 not provided, then the exit value is in the low eight bits. */
852 # define M4SYSVAL_EXITBITS(status) status
853 # define M4SYSVAL_TERMSIGBITS(status) 0
854 #endif /* ! UNIX && ! defined WEXITSTATUS */
856 /* Fallback definitions if <stdlib.h> or <sys/wait.h> are inadequate. */
858 # define WEXITSTATUS(status) (((status) >> 8) & 0xff)
861 # define WTERMSIG(status) ((status) & 0x7f)
864 # define WIFSIGNALED(status) (WTERMSIG (status) != 0)
867 # define WIFEXITED(status) (WTERMSIG (status) == 0)
870 /* Exit code from last "syscmd" command. */
874 m4_syscmd (struct obstack
*obs
, int argc
, token_data
**argv
)
876 if (bad_argc (argv
[0], argc
, 2, 2))
878 /* The empty command is successful. */
883 debug_flush_files ();
884 sysval
= system (ARG (1));
885 #if FUNC_SYSTEM_BROKEN
886 /* OS/2 has a buggy system() that returns exit status in the lowest eight
887 bits, although pclose() and WEXITSTATUS are defined to return exit
888 status in the next eight bits. This approach can't detect signals, but
889 at least syscmd(`ls') still works when stdout is a terminal. An
890 alternate approach is popen/insert_file/pclose, but that makes stdout
891 a pipe, which can change how some child processes behave. */
894 #endif /* FUNC_SYSTEM_BROKEN */
898 m4_esyscmd (struct obstack
*obs
, int argc
, token_data
**argv
)
903 if (bad_argc (argv
[0], argc
, 2, 2))
905 /* The empty command is successful. */
910 debug_flush_files ();
912 pin
= popen (ARG (1), "r");
915 M4ERROR ((warning_status
, errno
,
916 "cannot open pipe to command `%s'", ARG (1)));
921 while ((ch
= getc (pin
)) != EOF
)
922 obstack_1grow (obs
, (char) ch
);
923 sysval
= pclose (pin
);
928 m4_sysval (struct obstack
*obs
, int argc
, token_data
**argv
)
930 shipout_int (obs
, (sysval
== -1 ? 127
931 : (M4SYSVAL_EXITBITS (sysval
)
932 | M4SYSVAL_TERMSIGBITS (sysval
))));
935 /*-------------------------------------------------------------------------.
936 | This section contains the top level code for the "eval" builtin. The |
937 | actual work is done in the function evaluate (), which lives in eval.c. |
938 `-------------------------------------------------------------------------*/
941 m4_eval (struct obstack
*obs
, int argc
, token_data
**argv
)
948 if (bad_argc (argv
[0], argc
, 2, 4))
951 if (*ARG (2) && !numeric_arg (argv
[0], ARG (2), &radix
))
954 if (radix
< 1 || radix
> (int) strlen (digits
))
956 M4ERROR ((warning_status
, 0,
957 "radix in builtin `%s' out of range (radix = %d)",
962 if (argc
>= 4 && !numeric_arg (argv
[0], ARG (3), &min
))
966 M4ERROR ((warning_status
, 0,
967 "negative width to builtin `%s'", ARG (0)));
972 M4ERROR ((warning_status
, 0,
973 "empty string treated as 0 in builtin `%s'", ARG (0)));
974 else if (evaluate (ARG (1), &value
))
981 obstack_1grow (obs
, '-');
984 /* This assumes 2's-complement for correctly handling INT_MIN. */
985 while (min
-- - value
> 0)
986 obstack_1grow (obs
, '0');
988 obstack_1grow (obs
, '1');
989 obstack_1grow (obs
, '\0');
993 s
= ntoa (value
, radix
);
997 obstack_1grow (obs
, '-');
1000 for (min
-= strlen (s
); --min
>= 0;)
1001 obstack_1grow (obs
, '0');
1003 obstack_grow (obs
, s
, strlen (s
));
1007 m4_incr (struct obstack
*obs
, int argc
, token_data
**argv
)
1011 if (bad_argc (argv
[0], argc
, 2, 2))
1014 if (!numeric_arg (argv
[0], ARG (1), &value
))
1017 shipout_int (obs
, value
+ 1);
1021 m4_decr (struct obstack
*obs
, int argc
, token_data
**argv
)
1025 if (bad_argc (argv
[0], argc
, 2, 2))
1028 if (!numeric_arg (argv
[0], ARG (1), &value
))
1031 shipout_int (obs
, value
- 1);
1034 /* This section contains the macros "divert", "undivert" and "divnum" for
1035 handling diversion. The utility functions used lives in output.c. */
1037 /*-----------------------------------------------------------------------.
1038 | Divert further output to the diversion given by ARGV[1]. Out of range |
1039 | means discard further output. |
1040 `-----------------------------------------------------------------------*/
1043 m4_divert (struct obstack
*obs
, int argc
, token_data
**argv
)
1047 if (bad_argc (argv
[0], argc
, 1, 2))
1050 if (argc
>= 2 && !numeric_arg (argv
[0], ARG (1), &i
))
1056 /*-----------------------------------------------------.
1057 | Expand to the current diversion number, -1 if none. |
1058 `-----------------------------------------------------*/
1061 m4_divnum (struct obstack
*obs
, int argc
, token_data
**argv
)
1063 if (bad_argc (argv
[0], argc
, 1, 1))
1065 shipout_int (obs
, current_diversion
);
1068 /*-----------------------------------------------------------------------.
1069 | Bring back the diversion given by the argument list. If none is |
1070 | specified, bring back all diversions. GNU specific is the option of |
1071 | undiverting named files, by passing a non-numeric argument to undivert |
1073 `-----------------------------------------------------------------------*/
1076 m4_undivert (struct obstack
*obs
, int argc
, token_data
**argv
)
1085 for (i
= 1; i
< argc
; i
++)
1087 file
= strtol (ARG (i
), &endp
, 10);
1088 if (*endp
== '\0' && !isspace (to_uchar (*ARG (i
))))
1089 insert_diversion (file
);
1090 else if (no_gnu_extensions
)
1091 M4ERROR ((warning_status
, 0,
1092 "non-numeric argument to builtin `%s'", ARG (0)));
1095 fp
= m4_path_search (ARG (i
), NULL
);
1099 if (fclose (fp
) == EOF
)
1100 M4ERROR ((warning_status
, errno
,
1101 "error undiverting `%s'", ARG (i
)));
1104 M4ERROR ((warning_status
, errno
,
1105 "cannot undivert `%s'", ARG (i
)));
1110 /* This section contains various macros, which does not fall into any
1111 specific group. These are "dnl", "shift", "changequote", "changecom"
1112 and "changeword". */
1114 /*------------------------------------------------------------------------.
1115 | Delete all subsequent whitespace from input. The function skip_line () |
1116 | lives in input.c. |
1117 `------------------------------------------------------------------------*/
1120 m4_dnl (struct obstack
*obs
, int argc
, token_data
**argv
)
1122 if (bad_argc (argv
[0], argc
, 1, 1))
1128 /*-------------------------------------------------------------------------.
1129 | Shift all argument one to the left, discarding the first argument. Each |
1130 | output argument is quoted with the current quotes. |
1131 `-------------------------------------------------------------------------*/
1134 m4_shift (struct obstack
*obs
, int argc
, token_data
**argv
)
1136 if (bad_argc (argv
[0], argc
, 2, -1))
1138 dump_args (obs
, argc
- 1, argv
+ 1, ",", true);
1141 /*--------------------------------------------------------------------------.
1142 | Change the current quotes. The function set_quotes () lives in input.c. |
1143 `--------------------------------------------------------------------------*/
1146 m4_changequote (struct obstack
*obs
, int argc
, token_data
**argv
)
1148 if (bad_argc (argv
[0], argc
, 1, 3))
1151 /* Explicit NULL distinguishes between empty and missing argument. */
1152 set_quotes ((argc
>= 2) ? TOKEN_DATA_TEXT (argv
[1]) : NULL
,
1153 (argc
>= 3) ? TOKEN_DATA_TEXT (argv
[2]) : NULL
);
1156 /*--------------------------------------------------------------------.
1157 | Change the current comment delimiters. The function set_comment () |
1158 | lives in input.c. |
1159 `--------------------------------------------------------------------*/
1162 m4_changecom (struct obstack
*obs
, int argc
, token_data
**argv
)
1164 if (bad_argc (argv
[0], argc
, 1, 3))
1167 /* Explicit NULL distinguishes between empty and missing argument. */
1168 set_comment ((argc
>= 2) ? TOKEN_DATA_TEXT (argv
[1]) : NULL
,
1169 (argc
>= 3) ? TOKEN_DATA_TEXT (argv
[2]) : NULL
);
1172 #ifdef ENABLE_CHANGEWORD
1174 /*-----------------------------------------------------------------------.
1175 | Change the regular expression used for breaking the input into words. |
1176 | The function set_word_regexp () lives in input.c. |
1177 `-----------------------------------------------------------------------*/
1180 m4_changeword (struct obstack
*obs
, int argc
, token_data
**argv
)
1182 if (bad_argc (argv
[0], argc
, 2, 2))
1185 set_word_regexp (TOKEN_DATA_TEXT (argv
[1]));
1188 #endif /* ENABLE_CHANGEWORD */
1190 /* This section contains macros for inclusion of other files -- "include"
1191 and "sinclude". This differs from bringing back diversions, in that
1192 the input is scanned before being copied to the output. */
1194 /*-------------------------------------------------------------------------.
1195 | Generic include function. Include the file given by the first argument, |
1196 | if it exists. Complain about inaccesible files iff SILENT is false. |
1197 `-------------------------------------------------------------------------*/
1200 include (int argc
, token_data
**argv
, bool silent
)
1205 if (bad_argc (argv
[0], argc
, 2, 2))
1208 fp
= m4_path_search (ARG (1), &name
);
1212 M4ERROR ((warning_status
, errno
,
1213 "cannot open `%s'", ARG (1)));
1217 push_file (fp
, name
, true);
1221 /*------------------------------------------------.
1222 | Include a file, complaining in case of errors. |
1223 `------------------------------------------------*/
1226 m4_include (struct obstack
*obs
, int argc
, token_data
**argv
)
1228 include (argc
, argv
, false);
1231 /*----------------------------------.
1232 | Include a file, ignoring errors. |
1233 `----------------------------------*/
1236 m4_sinclude (struct obstack
*obs
, int argc
, token_data
**argv
)
1238 include (argc
, argv
, true);
1241 /* More miscellaneous builtins -- "maketemp", "errprint", "__file__",
1242 "__line__", and "__program__". The last three are GNU specific. */
1244 /*------------------------------------------------------------------.
1245 | Use the first argument as at template for a temporary file name. |
1246 `------------------------------------------------------------------*/
1248 /* Add trailing 'X' to NAME if necessary, securely create the file,
1249 and place the new file name on OBS. */
1251 mkstemp_helper (struct obstack
*obs
, const char *name
)
1257 /* Guarantee that there are six trailing 'X' characters, even if the
1258 user forgot to supply them. */
1259 len
= strlen (name
);
1260 obstack_grow (obs
, name
, len
);
1261 for (i
= 0; len
> 0 && i
< 6; i
++)
1262 if (name
[--len
] != 'X')
1265 obstack_1grow (obs
, 'X');
1266 obstack_1grow (obs
, '\0');
1269 fd
= mkstemp ((char *) obstack_base (obs
));
1272 M4ERROR ((0, errno
, "cannot create tempfile `%s'", name
));
1273 obstack_free (obs
, obstack_finish (obs
));
1280 m4_maketemp (struct obstack
*obs
, int argc
, token_data
**argv
)
1282 if (bad_argc (argv
[0], argc
, 2, 2))
1284 if (no_gnu_extensions
)
1286 /* POSIX states "any trailing 'X' characters [are] replaced with
1287 the current process ID as a string", without referencing the
1288 file system. Horribly insecure, but we have to do it when we
1289 are in traditional mode.
1291 For reference, Solaris m4 does:
1294 maketemp(XX) -> `Xn', where n is last digit of pid
1295 maketemp(XXXXXXXX) -> `X00nnnnn', where nnnnn is 16-bit pid
1297 const char *str
= ARG (1);
1298 int len
= strlen (str
);
1302 M4ERROR ((warning_status
, 0, "recommend using mkstemp instead"));
1303 for (i
= len
; i
> 1; i
--)
1304 if (str
[i
- 1] != 'X')
1306 obstack_grow (obs
, str
, i
);
1307 str
= ntoa ((eval_t
) getpid (), 10);
1308 len2
= strlen (str
);
1310 obstack_grow0 (obs
, str
+ len2
- (len
- i
), len
- i
);
1313 while (i
++ < len
- len2
)
1314 obstack_1grow (obs
, '0');
1315 obstack_grow0 (obs
, str
, len2
);
1319 mkstemp_helper (obs
, ARG (1));
1323 m4_mkstemp (struct obstack
*obs
, int argc
, token_data
**argv
)
1325 if (bad_argc (argv
[0], argc
, 2, 2))
1327 mkstemp_helper (obs
, ARG (1));
1330 /*----------------------------------------.
1331 | Print all arguments on standard error. |
1332 `----------------------------------------*/
1335 m4_errprint (struct obstack
*obs
, int argc
, token_data
**argv
)
1337 if (bad_argc (argv
[0], argc
, 2, -1))
1339 dump_args (obs
, argc
, argv
, " ", false);
1340 obstack_1grow (obs
, '\0');
1341 debug_flush_files ();
1342 fprintf (stderr
, "%s", (char *) obstack_finish (obs
));
1347 m4___file__ (struct obstack
*obs
, int argc
, token_data
**argv
)
1349 if (bad_argc (argv
[0], argc
, 1, 1))
1351 obstack_grow (obs
, lquote
.string
, lquote
.length
);
1352 obstack_grow (obs
, current_file
, strlen (current_file
));
1353 obstack_grow (obs
, rquote
.string
, rquote
.length
);
1357 m4___line__ (struct obstack
*obs
, int argc
, token_data
**argv
)
1359 if (bad_argc (argv
[0], argc
, 1, 1))
1361 shipout_int (obs
, current_line
);
1365 m4___program__ (struct obstack
*obs
, int argc
, token_data
**argv
)
1367 if (bad_argc (argv
[0], argc
, 1, 1))
1369 obstack_grow (obs
, lquote
.string
, lquote
.length
);
1370 obstack_grow (obs
, program_name
, strlen (program_name
));
1371 obstack_grow (obs
, rquote
.string
, rquote
.length
);
1374 /* This section contains various macros for exiting, saving input until
1375 EOF is seen, and tracing macro calls. That is: "m4exit", "m4wrap",
1376 "traceon" and "traceoff". */
1378 /*-------------------------------------------------------------------------.
1379 | Exit immediately, with exitcode specified by the first argument, 0 if no |
1380 | arguments are present. |
1381 `-------------------------------------------------------------------------*/
1384 m4_m4exit (struct obstack
*obs
, int argc
, token_data
**argv
)
1386 int exit_code
= EXIT_SUCCESS
;
1388 /* Warn on bad arguments, but still exit. */
1389 bad_argc (argv
[0], argc
, 1, 2);
1390 if (argc
>= 2 && !numeric_arg (argv
[0], ARG (1), &exit_code
))
1391 exit_code
= EXIT_FAILURE
;
1392 if (exit_code
< 0 || exit_code
> 255)
1394 M4ERROR ((warning_status
, 0,
1395 "exit status out of range: `%d'", exit_code
));
1396 exit_code
= EXIT_FAILURE
;
1398 /* Change debug stream back to stderr, to force flushing debug stream and
1399 detect any errors it might have encountered. */
1400 debug_set_output (NULL
);
1401 debug_flush_files ();
1402 if (exit_code
== EXIT_SUCCESS
&& retcode
!= EXIT_SUCCESS
)
1403 exit_code
= retcode
;
1404 /* Propagate non-zero status to atexit handlers. */
1405 if (exit_code
!= EXIT_SUCCESS
)
1406 exit_failure
= exit_code
;
1410 /*-------------------------------------------------------------------------.
1411 | Save the argument text until EOF has been seen, allowing for user |
1412 | specified cleanup action. GNU version saves all arguments, the standard |
1413 | version only the first. |
1414 `-------------------------------------------------------------------------*/
1417 m4_m4wrap (struct obstack
*obs
, int argc
, token_data
**argv
)
1419 if (bad_argc (argv
[0], argc
, 2, -1))
1421 if (no_gnu_extensions
)
1422 obstack_grow (obs
, ARG (1), strlen (ARG (1)));
1424 dump_args (obs
, argc
, argv
, " ", false);
1425 obstack_1grow (obs
, '\0');
1426 push_wrapup ((char *) obstack_finish (obs
));
1429 /* Enable tracing of all specified macros, or all, if none is specified.
1430 Tracing is disabled by default, when a macro is defined. This can be
1431 overridden by the "t" debug flag. */
1433 /*-----------------------------------------------------------------------.
1434 | Set_trace () is used by "traceon" and "traceoff" to enable and disable |
1435 | tracing of a macro. It disables tracing if DATA is NULL, otherwise it |
1437 `-----------------------------------------------------------------------*/
1440 set_trace (symbol
*sym
, void *data
)
1442 SYMBOL_TRACED (sym
) = data
!= NULL
;
1443 /* Remove placeholder from table if macro is undefined and untraced. */
1444 if (SYMBOL_TYPE (sym
) == TOKEN_VOID
&& data
== NULL
)
1445 lookup_symbol (SYMBOL_NAME (sym
), SYMBOL_POPDEF
);
1449 m4_traceon (struct obstack
*obs
, int argc
, token_data
**argv
)
1455 hack_all_symbols (set_trace
, obs
);
1457 for (i
= 1; i
< argc
; i
++)
1459 s
= lookup_symbol (TOKEN_DATA_TEXT (argv
[i
]), SYMBOL_INSERT
);
1464 /*------------------------------------------------------------------------.
1465 | Disable tracing of all specified macros, or all, if none is specified. |
1466 `------------------------------------------------------------------------*/
1469 m4_traceoff (struct obstack
*obs
, int argc
, token_data
**argv
)
1475 hack_all_symbols (set_trace
, NULL
);
1477 for (i
= 1; i
< argc
; i
++)
1479 s
= lookup_symbol (TOKEN_DATA_TEXT (argv
[i
]), SYMBOL_LOOKUP
);
1481 set_trace (s
, NULL
);
1485 /*----------------------------------------------------------------------.
1486 | On-the-fly control of the format of the tracing output. It takes one |
1487 | argument, which is a character string like given to the -d option, or |
1488 | none in which case the debug_level is zeroed. |
1489 `----------------------------------------------------------------------*/
1492 m4_debugmode (struct obstack
*obs
, int argc
, token_data
**argv
)
1494 int new_debug_level
;
1497 if (bad_argc (argv
[0], argc
, 1, 2))
1504 if (ARG (1)[0] == '+' || ARG (1)[0] == '-')
1506 change_flag
= ARG (1)[0];
1507 new_debug_level
= debug_decode (ARG (1) + 1);
1512 new_debug_level
= debug_decode (ARG (1));
1515 if (new_debug_level
< 0)
1516 M4ERROR ((warning_status
, 0,
1517 "Debugmode: bad debug flags: `%s'", ARG (1)));
1520 switch (change_flag
)
1523 debug_level
= new_debug_level
;
1527 debug_level
|= new_debug_level
;
1531 debug_level
&= ~new_debug_level
;
1538 /*-------------------------------------------------------------------------.
1539 | Specify the destination of the debugging output. With one argument, the |
1540 | argument is taken as a file name, with no arguments, revert to stderr. |
1541 `-------------------------------------------------------------------------*/
1544 m4_debugfile (struct obstack
*obs
, int argc
, token_data
**argv
)
1546 if (bad_argc (argv
[0], argc
, 1, 2))
1550 debug_set_output (NULL
);
1551 else if (!debug_set_output (ARG (1)))
1552 M4ERROR ((warning_status
, errno
,
1553 "cannot set error file: `%s'", ARG (1)));
1556 /* This section contains text processing macros: "len", "index",
1557 "substr", "translit", "format", "regexp" and "patsubst". The last
1558 three are GNU specific. */
1560 /*---------------------------------------------.
1561 | Expand to the length of the first argument. |
1562 `---------------------------------------------*/
1565 m4_len (struct obstack
*obs
, int argc
, token_data
**argv
)
1567 if (bad_argc (argv
[0], argc
, 2, 2))
1569 shipout_int (obs
, strlen (ARG (1)));
1572 /*-------------------------------------------------------------------------.
1573 | The macro expands to the first index of the second argument in the first |
1575 `-------------------------------------------------------------------------*/
1578 m4_index (struct obstack
*obs
, int argc
, token_data
**argv
)
1580 const char *haystack
;
1584 if (bad_argc (argv
[0], argc
, 3, 3))
1586 /* builtin(`index') is blank, but index(`abc') is 0. */
1588 shipout_int (obs
, 0);
1593 result
= strstr (haystack
, ARG (2));
1594 retval
= result
? result
- haystack
: -1;
1596 shipout_int (obs
, retval
);
1599 /*-------------------------------------------------------------------------.
1600 | The macro "substr" extracts substrings from the first argument, starting |
1601 | from the index given by the second argument, extending for a length |
1602 | given by the third argument. If the third argument is missing, the |
1603 | substring extends to the end of the first argument. |
1604 `-------------------------------------------------------------------------*/
1607 m4_substr (struct obstack
*obs
, int argc
, token_data
**argv
)
1612 if (bad_argc (argv
[0], argc
, 3, 4))
1614 /* builtin(`substr') is blank, but substr(`abc') is abc. */
1616 obstack_grow (obs
, ARG (1), strlen (ARG (1)));
1620 length
= avail
= strlen (ARG (1));
1621 if (!numeric_arg (argv
[0], ARG (2), &start
))
1624 if (argc
>= 4 && !numeric_arg (argv
[0], ARG (3), &length
))
1627 if (start
< 0 || length
<= 0 || start
>= avail
)
1630 if (start
+ length
> avail
)
1631 length
= avail
- start
;
1632 obstack_grow (obs
, ARG (1) + start
, length
);
1635 /*------------------------------------------------------------------------.
1636 | For "translit", ranges are allowed in the second and third argument. |
1637 | They are expanded in the following function, and the expanded strings, |
1638 | without any ranges left, are used to translate the characters of the |
1639 | first argument. A single - (dash) can be included in the strings by |
1640 | being the first or the last character in the string. If the first |
1641 | character in a range is after the first in the character set, the range |
1642 | is made backwards, thus 9-0 is the string 9876543210. |
1643 `------------------------------------------------------------------------*/
1646 expand_ranges (const char *s
, struct obstack
*obs
)
1651 for (from
= '\0'; *s
!= '\0'; from
= to_uchar (*s
++))
1653 if (*s
== '-' && from
!= '\0')
1655 to
= to_uchar (*++s
);
1659 obstack_1grow (obs
, '-');
1662 else if (from
<= to
)
1665 obstack_1grow (obs
, from
);
1669 while (--from
>= to
)
1670 obstack_1grow (obs
, from
);
1674 obstack_1grow (obs
, *s
);
1676 obstack_1grow (obs
, '\0');
1677 return (char *) obstack_finish (obs
);
1680 /*----------------------------------------------------------------------.
1681 | The macro "translit" translates all characters in the first argument, |
1682 | which are present in the second argument, into the corresponding |
1683 | character from the third argument. If the third argument is shorter |
1684 | than the second, the extra characters in the second argument, are |
1685 | deleted from the first (pueh). |
1686 `----------------------------------------------------------------------*/
1689 m4_translit (struct obstack
*obs
, int argc
, token_data
**argv
)
1694 char map
[256] = {0};
1695 char found
[256] = {0};
1698 if (bad_argc (argv
[0], argc
, 3, 4))
1700 /* builtin(`translit') is blank, but translit(`abc') is abc. */
1702 obstack_grow (obs
, ARG (1), strlen (ARG (1)));
1707 if (strchr (from
, '-') != NULL
)
1709 from
= expand_ranges (from
, obs
);
1715 if (strchr (to
, '-') != NULL
)
1717 to
= expand_ranges (to
, obs
);
1722 /* Calling strchr(from) for each character in data is quadratic,
1723 since both strings can be arbitrarily long. Instead, create a
1724 from-to mapping in one pass of from, then use that map in one
1725 pass of data, for linear behavior. Traditional behavior is that
1726 only the first instance of a character in from is consulted,
1727 hence the found map. */
1728 for ( ; (ch
= *from
) != '\0'; from
++)
1739 for (data
= ARG (1); (ch
= *data
) != '\0'; data
++)
1742 obstack_1grow (obs
, ch
);
1744 obstack_1grow (obs
, map
[ch
]);
1748 /*----------------------------------------------------------------------.
1749 | Frontend for printf like formatting. The function format () lives in |
1750 | the file format.c. |
1751 `----------------------------------------------------------------------*/
1754 m4_format (struct obstack
*obs
, int argc
, token_data
**argv
)
1756 if (bad_argc (argv
[0], argc
, 2, -1))
1758 format (obs
, argc
- 1, argv
+ 1);
1761 /*-------------------------------------------------------------------------.
1762 | Function to perform substitution by regular expressions. Used by the |
1763 | builtins regexp and patsubst. The changed text is placed on the |
1764 | obstack. The substitution is REPL, with \& substituted by this part of |
1765 | VICTIM matched by the last whole regular expression, taken from REGS[0], |
1766 | and \N substituted by the text matched by the Nth parenthesized |
1767 | sub-expression, taken from REGS[N]. |
1768 `-------------------------------------------------------------------------*/
1770 static int substitute_warned
= 0;
1773 substitute (struct obstack
*obs
, const char *victim
, const char *repl
,
1774 struct re_registers
*regs
)
1780 while ((ch
= *repl
++) != '\\')
1784 obstack_1grow (obs
, ch
);
1787 switch ((ch
= *repl
++))
1790 if (!substitute_warned
)
1792 M4ERROR ((warning_status
, 0, "\
1793 Warning: \\0 will disappear, use \\& instead in replacements"));
1794 substitute_warned
= 1;
1799 obstack_grow (obs
, victim
+ regs
->start
[0],
1800 regs
->end
[0] - regs
->start
[0]);
1803 case '1': case '2': case '3': case '4': case '5': case '6':
1804 case '7': case '8': case '9':
1806 if (regs
->num_regs
- 1 <= ch
)
1807 M4ERROR ((warning_status
, 0, "\
1808 Warning: sub-expression %d not present", ch
));
1809 else if (regs
->end
[ch
] > 0)
1810 obstack_grow (obs
, victim
+ regs
->start
[ch
],
1811 regs
->end
[ch
] - regs
->start
[ch
]);
1815 M4ERROR ((warning_status
, 0, "\
1816 Warning: trailing \\ ignored in replacement"));
1820 obstack_1grow (obs
, ch
);
1826 /*------------------------------------------.
1827 | Initialize regular expression variables. |
1828 `------------------------------------------*/
1831 init_pattern_buffer (struct re_pattern_buffer
*buf
, struct re_registers
*regs
)
1833 buf
->translate
= NULL
;
1834 buf
->fastmap
= NULL
;
1841 /*----------------------------------------.
1842 | Clean up regular expression variables. |
1843 `----------------------------------------*/
1846 free_pattern_buffer (struct re_pattern_buffer
*buf
, struct re_registers
*regs
)
1853 /*--------------------------------------------------------------------------.
1854 | Regular expression version of index. Given two arguments, expand to the |
1855 | index of the first match of the second argument (a regexp) in the first. |
1856 | Expand to -1 if here is no match. Given a third argument, is changes |
1857 | the expansion to this argument. |
1858 `--------------------------------------------------------------------------*/
1861 m4_regexp (struct obstack
*obs
, int argc
, token_data
**argv
)
1863 const char *victim
; /* first argument */
1864 const char *regexp
; /* regular expression */
1865 const char *repl
; /* replacement string */
1867 struct re_pattern_buffer buf
; /* compiled regular expression */
1868 struct re_registers regs
; /* for subexpression matches */
1869 const char *msg
; /* error message from re_compile_pattern */
1870 int startpos
; /* start position of match */
1871 int length
; /* length of first argument */
1873 if (bad_argc (argv
[0], argc
, 3, 4))
1875 /* builtin(`regexp') is blank, but regexp(`abc') is 0. */
1877 shipout_int (obs
, 0);
1881 victim
= TOKEN_DATA_TEXT (argv
[1]);
1882 regexp
= TOKEN_DATA_TEXT (argv
[2]);
1884 init_pattern_buffer (&buf
, ®s
);
1885 msg
= re_compile_pattern (regexp
, strlen (regexp
), &buf
);
1889 M4ERROR ((warning_status
, 0,
1890 "bad regular expression: `%s': %s", regexp
, msg
));
1891 free_pattern_buffer (&buf
, ®s
);
1895 length
= strlen (victim
);
1896 /* Avoid overhead of allocating regs if we won't use it. */
1897 startpos
= re_search (&buf
, victim
, length
, 0, length
,
1898 argc
== 3 ? NULL
: ®s
);
1901 M4ERROR ((warning_status
, 0,
1902 "error matching regular expression `%s'", regexp
));
1904 shipout_int (obs
, startpos
);
1905 else if (startpos
>= 0)
1907 repl
= TOKEN_DATA_TEXT (argv
[3]);
1908 substitute (obs
, victim
, repl
, ®s
);
1911 free_pattern_buffer (&buf
, ®s
);
1914 /*--------------------------------------------------------------------------.
1915 | Substitute all matches of a regexp occuring in a string. Each match of |
1916 | the second argument (a regexp) in the first argument is changed to the |
1917 | third argument, with \& substituted by the matched text, and \N |
1918 | substituted by the text matched by the Nth parenthesized sub-expression. |
1919 `--------------------------------------------------------------------------*/
1922 m4_patsubst (struct obstack
*obs
, int argc
, token_data
**argv
)
1924 const char *victim
; /* first argument */
1925 const char *regexp
; /* regular expression */
1927 struct re_pattern_buffer buf
; /* compiled regular expression */
1928 struct re_registers regs
; /* for subexpression matches */
1929 const char *msg
; /* error message from re_compile_pattern */
1930 int matchpos
; /* start position of match */
1931 int offset
; /* current match offset */
1932 int length
; /* length of first argument */
1934 if (bad_argc (argv
[0], argc
, 3, 4))
1936 /* builtin(`patsubst') is blank, but patsubst(`abc') is abc. */
1938 obstack_grow (obs
, ARG (1), strlen (ARG (1)));
1942 regexp
= TOKEN_DATA_TEXT (argv
[2]);
1944 init_pattern_buffer (&buf
, ®s
);
1945 msg
= re_compile_pattern (regexp
, strlen (regexp
), &buf
);
1949 M4ERROR ((warning_status
, 0,
1950 "bad regular expression `%s': %s", regexp
, msg
));
1955 victim
= TOKEN_DATA_TEXT (argv
[1]);
1956 length
= strlen (victim
);
1960 while (offset
<= length
)
1962 matchpos
= re_search (&buf
, victim
, length
,
1963 offset
, length
- offset
, ®s
);
1967 /* Match failed -- either error or there is no match in the
1968 rest of the string, in which case the rest of the string is
1972 M4ERROR ((warning_status
, 0,
1973 "error matching regular expression `%s'", regexp
));
1974 else if (offset
< length
)
1975 obstack_grow (obs
, victim
+ offset
, length
- offset
);
1979 /* Copy the part of the string that was skipped by re_search (). */
1981 if (matchpos
> offset
)
1982 obstack_grow (obs
, victim
+ offset
, matchpos
- offset
);
1984 /* Handle the part of the string that was covered by the match. */
1986 substitute (obs
, victim
, ARG (3), ®s
);
1988 /* Update the offset to the end of the match. If the regexp
1989 matched a null string, advance offset one more, to avoid
1992 offset
= regs
.end
[0];
1993 if (regs
.start
[0] == regs
.end
[0])
1994 obstack_1grow (obs
, victim
[offset
++]);
1996 obstack_1grow (obs
, '\0');
1998 free_pattern_buffer (&buf
, ®s
);
2001 /* Finally, a placeholder builtin. This builtin is not installed by
2002 default, but when reading back frozen files, this is associated
2003 with any builtin we don't recognize (for example, if the frozen
2004 file was created with a changeword capable m4, but is then loaded
2005 by a different m4 that does not support changeword). This way, we
2006 can keep 'm4 -R' quiet in the common case that the user did not
2007 know or care about the builtin when the frozen file was created,
2008 while still flagging it as a potential error if an attempt is made
2009 to actually use the builtin. */
2011 /*--------------------------------------------------------------------.
2012 | Issue a warning that this macro is a placeholder for an unsupported |
2013 | builtin that was requested while reloading a frozen file. |
2014 `--------------------------------------------------------------------*/
2017 m4_placeholder (struct obstack
*obs
, int argc
, token_data
**argv
)
2019 M4ERROR ((warning_status
, 0, "\
2020 builtin `%s' requested by frozen file is not supported", ARG (0)));
2023 /*-------------------------------------------------------------------------.
2024 | This function handles all expansion of user defined and predefined |
2025 | macros. It is called with an obstack OBS, where the macros expansion |
2026 | will be placed, as an unfinished object. SYM points to the macro |
2027 | definition, giving the expansion text. ARGC and ARGV are the arguments, |
2029 `-------------------------------------------------------------------------*/
2032 expand_user_macro (struct obstack
*obs
, symbol
*sym
,
2033 int argc
, token_data
**argv
)
2038 for (text
= SYMBOL_TEXT (sym
); *text
!= '\0';)
2042 obstack_1grow (obs
, *text
);
2049 case '0': case '1': case '2': case '3': case '4':
2050 case '5': case '6': case '7': case '8': case '9':
2051 if (no_gnu_extensions
)
2057 for (i
= 0; isdigit (to_uchar (*text
)); text
++)
2058 i
= i
*10 + (*text
- '0');
2061 obstack_grow (obs
, TOKEN_DATA_TEXT (argv
[i
]),
2062 strlen (TOKEN_DATA_TEXT (argv
[i
])));
2065 case '#': /* number of arguments */
2066 shipout_int (obs
, argc
- 1);
2070 case '*': /* all arguments */
2071 case '@': /* ... same, but quoted */
2072 dump_args (obs
, argc
, argv
, ",", *text
== '@');
2077 obstack_1grow (obs
, '$');