1 /* GNU m4 -- A simple macro processor
2 Copyright (C) 2000, 2002-2004, 2006-2010, 2013 Free Software
5 This file is part of GNU M4.
7 GNU M4 is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU M4 is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /* Build using only the exported interfaces, unless NDEBUG is set, in
24 which case use private symbols to speed things up as much as possible. */
26 # include <m4/m4module.h>
28 # include "m4private.h"
39 #include <modules/m4.h>
41 /* Rename exported symbols for dlpreload()ing. */
42 #define m4_builtin_table m4_LTX_m4_builtin_table
44 #define m4_set_sysval m4_LTX_m4_set_sysval
45 #define m4_sysval_flush m4_LTX_m4_sysval_flush
46 #define m4_dump_symbols m4_LTX_m4_dump_symbols
47 #define m4_expand_ranges m4_LTX_m4_expand_ranges
48 #define m4_make_temp m4_LTX_m4_make_temp
50 extern void m4_set_sysval (int);
51 extern void m4_sysval_flush (m4
*, bool);
52 extern void m4_dump_symbols (m4
*, m4_dump_symbol_data
*, size_t,
53 m4_macro_args
*, bool);
54 extern const char *m4_expand_ranges (const char *, size_t *, m4_obstack
*);
55 extern void m4_make_temp (m4
*, m4_obstack
*, const m4_call_info
*,
56 const char *, size_t, bool);
58 /* Maintain each of the builtins implemented in this modules along
59 with their details in a single table for easy maintenance.
61 function macros blind side minargs maxargs */
62 #define builtin_functions \
63 BUILTIN (changecom, false, false, false, 0, 2 ) \
64 BUILTIN (changequote, false, false, false, 0, 2 ) \
65 BUILTIN (decr, false, true, true, 1, 1 ) \
66 BUILTIN (define, true, true, false, 1, 2 ) \
67 BUILTIN (defn, true, true, false, 1, -1 ) \
68 BUILTIN (divert, false, false, false, 0, 2 ) \
69 BUILTIN (divnum, false, false, false, 0, 0 ) \
70 BUILTIN (dnl, false, false, false, 0, 0 ) \
71 BUILTIN (dumpdef, true, false, false, 0, -1 ) \
72 BUILTIN (errprint, false, true, false, 1, -1 ) \
73 BUILTIN (eval, false, true, true, 1, 3 ) \
74 BUILTIN (ifdef, true, true, false, 2, 3 ) \
75 BUILTIN (ifelse, true, true, false, 1, -1 ) \
76 BUILTIN (include, false, true, false, 1, 1 ) \
77 BUILTIN (incr, false, true, true, 1, 1 ) \
78 BUILTIN (index, false, true, true, 2, 3 ) \
79 BUILTIN (len, false, true, true, 1, 1 ) \
80 BUILTIN (m4exit, false, false, false, 0, 1 ) \
81 BUILTIN (m4wrap, true, true, false, 1, -1 ) \
82 BUILTIN (maketemp, false, true, false, 1, 1 ) \
83 BUILTIN (mkstemp, false, true, false, 1, 1 ) \
84 BUILTIN (popdef, true, true, false, 1, -1 ) \
85 BUILTIN (pushdef, true, true, false, 1, 2 ) \
86 BUILTIN (shift, true, true, false, 1, -1 ) \
87 BUILTIN (sinclude, false, true, false, 1, 1 ) \
88 BUILTIN (substr, false, true, true, 2, 4 ) \
89 BUILTIN (syscmd, false, true, true, 1, 1 ) \
90 BUILTIN (sysval, false, false, false, 0, 0 ) \
91 BUILTIN (traceoff, true, false, false, 0, -1 ) \
92 BUILTIN (traceon, true, false, false, 0, -1 ) \
93 BUILTIN (translit, false, true, true, 2, 3 ) \
94 BUILTIN (undefine, true, true, false, 1, -1 ) \
95 BUILTIN (undivert, false, false, false, 0, -1 ) \
98 typedef intmax_t number;
99 typedef uintmax_t unumber
;
101 static void include (m4
*context
, m4_obstack
*obs
, size_t argc
,
102 m4_macro_args
*argv
, bool silent
);
103 static int dumpdef_cmp_CB (const void *s1
, const void *s2
);
104 static void * dump_symbol_CB (m4_symbol_table
*, const char *, size_t,
105 m4_symbol
*symbol
, void *userdata
);
106 static const char *ntoa (number value
, int radix
);
107 static void numb_obstack (m4_obstack
*obs
, number value
,
111 /* Generate prototypes for each builtin handler function. */
112 #define BUILTIN(handler, macros, blind, side, min, max) M4BUILTIN (handler)
117 /* Generate a table for mapping m4 symbol names to handler functions. */
118 const m4_builtin m4_builtin_table
[] =
120 #define BUILTIN(handler, macros, blind, side, min, max) \
121 M4BUILTIN_ENTRY (handler, #handler, macros, blind, side, min, max)
126 { NULL
, NULL
, 0, 0, 0 },
131 /* The rest of this file is code for builtins and expansion of user
132 defined macros. All the functions for builtins have a prototype as:
134 void builtin_MACRONAME (m4_obstack *obs, int argc, char *argv[]);
136 The function are expected to leave their expansion on the obstack OBS,
137 as an unfinished object. ARGV is a table of ARGC pointers to the
138 individual arguments to the macro. Please note that in general
139 argv[argc] != NULL. */
141 M4BUILTIN_HANDLER (define
)
143 const m4_call_info
*me
= m4_arg_info (argv
);
145 if (m4_is_arg_text (argv
, 1))
147 m4_symbol_value
*value
= m4_symbol_value_create ();
149 if (m4_symbol_value_copy (context
, value
, m4_arg_symbol (argv
, 2)))
150 m4_warn (context
, 0, me
, _("cannot concatenate builtins"));
151 m4_symbol_define (M4SYMTAB
, M4ARG (1), M4ARGLEN (1), value
);
154 m4_warn (context
, 0, me
, _("invalid macro name ignored"));
157 M4BUILTIN_HANDLER (undefine
)
160 for (i
= 1; i
< argc
; i
++)
161 if (m4_symbol_value_lookup (context
, argv
, i
, true))
162 m4_symbol_delete (M4SYMTAB
, M4ARG (i
), M4ARGLEN (i
));
165 M4BUILTIN_HANDLER (pushdef
)
167 const m4_call_info
*me
= m4_arg_info (argv
);
169 if (m4_is_arg_text (argv
, 1))
171 m4_symbol_value
*value
= m4_symbol_value_create ();
173 if (m4_symbol_value_copy (context
, value
, m4_arg_symbol (argv
, 2)))
174 m4_warn (context
, 0, me
, _("cannot concatenate builtins"));
175 m4_symbol_pushdef (M4SYMTAB
, M4ARG (1), M4ARGLEN (1), value
);
178 m4_warn (context
, 0, me
, _("invalid macro name ignored"));
181 M4BUILTIN_HANDLER (popdef
)
184 for (i
= 1; i
< argc
; i
++)
185 if (m4_symbol_value_lookup (context
, argv
, i
, true))
186 m4_symbol_popdef (M4SYMTAB
, M4ARG (i
), M4ARGLEN (i
));
191 /* --- CONDITIONALS OF M4 --- */
194 M4BUILTIN_HANDLER (ifdef
)
196 m4_push_arg (context
, obs
, argv
,
197 m4_symbol_value_lookup (context
, argv
, 1, false) ? 2 : 3);
200 M4BUILTIN_HANDLER (ifelse
)
202 const m4_call_info
*me
= m4_arg_info (argv
);
205 /* The valid ranges of argc for ifelse is discontinuous, we cannot
206 rely on the regular mechanisms. */
207 if (argc
== 2 || m4_bad_argc (context
, argc
, me
, 3, -1, false))
209 else if (argc
% 3 == 0)
210 /* Diagnose excess arguments if 5, 8, 11, etc., actual arguments. */
211 m4_bad_argc (context
, argc
, me
, 0, argc
- 2, false);
218 if (m4_arg_equal (context
, argv
, i
, i
+ 1))
220 m4_push_arg (context
, obs
, argv
, i
+ 2);
230 m4_push_arg (context
, obs
, argv
, i
+ 3);
241 /* qsort comparison routine, for sorting the table made in m4_dumpdef (). */
243 dumpdef_cmp_CB (const void *s1
, const void *s2
)
245 const m4_string
*a
= (const m4_string
*) s1
;
246 const m4_string
*b
= (const m4_string
*) s2
;
247 return memcmp2 (a
->str
, a
->len
, b
->str
, b
->len
);
250 /* The function m4_dump_symbols () is for use by "dumpdef". It builds up a
251 table of all defined symbol names. */
253 dump_symbol_CB (m4_symbol_table
*ignored M4_GNUC_UNUSED
, const char *name
,
254 size_t len
, m4_symbol
*symbol
, void *userdata
)
256 m4_dump_symbol_data
*symbol_data
= (m4_dump_symbol_data
*) userdata
;
261 assert (!m4_is_symbol_value_void (m4_get_symbol_value (symbol
)));
263 if (symbol_data
->size
== 0)
266 size_t offset
= obstack_object_size (symbol_data
->obs
);
267 obstack_blank (symbol_data
->obs
, sizeof *symbol_data
->base
);
268 symbol_data
->size
= (obstack_room (symbol_data
->obs
)
269 / sizeof *symbol_data
->base
);
270 base
= (char *) obstack_base (symbol_data
->obs
) + offset
;
271 symbol_data
->base
= (m4_string
*) base
;
275 obstack_blank_fast (symbol_data
->obs
, sizeof *symbol_data
->base
);
279 /* Safe to cast away const, since m4_dump_symbols adds it back. */
280 key
= (m4_string
*) symbol_data
->base
++;
281 key
->str
= (char *) name
;
286 /* If there are no arguments, build a sorted list of all defined
287 symbols, otherwise, only the specified symbols. */
289 m4_dump_symbols (m4
*context
, m4_dump_symbol_data
*data
, size_t argc
,
290 m4_macro_args
*argv
, bool complain
)
292 assert (obstack_object_size (data
->obs
) == 0);
293 data
->size
= obstack_room (data
->obs
) / sizeof *data
->base
;
294 data
->base
= (m4_string
*) obstack_base (data
->obs
);
297 m4_symtab_apply (M4SYMTAB
, false, dump_symbol_CB
, data
);
303 for (i
= 1; i
< argc
; i
++)
305 symbol
= m4_symbol_value_lookup (context
, argv
, i
, complain
);
307 dump_symbol_CB (NULL
, M4ARG (i
), M4ARGLEN (i
), symbol
, data
);
311 data
->size
= obstack_object_size (data
->obs
) / sizeof *data
->base
;
312 data
->base
= (m4_string
*) obstack_finish (data
->obs
);
313 /* Safe to cast away const, since we don't modify entries. */
314 qsort ((m4_string
*) data
->base
, data
->size
, sizeof *data
->base
,
319 /* Implementation of "dumpdef" itself. It builds up a table of pointers to
320 symbols, sorts it and prints the sorted table. */
321 M4BUILTIN_HANDLER (dumpdef
)
323 m4_dump_symbol_data data
;
324 const m4_string_pair
*quotes
= NULL
;
325 bool stack
= m4_is_debug_bit (context
, M4_DEBUG_TRACE_STACK
);
326 size_t arg_length
= m4_get_max_debug_arg_length_opt (context
);
327 bool module
= m4_is_debug_bit (context
, M4_DEBUG_TRACE_MODULE
);
328 FILE *output
= (m4_is_debug_bit (context
, M4_DEBUG_TRACE_OUTPUT_DUMPDEF
)
329 ? stderr
: m4_get_debug_file (context
));
333 if (m4_is_debug_bit (context
, M4_DEBUG_TRACE_QUOTE
))
334 quotes
= m4_get_syntax_quotes (M4SYNTAX
);
335 data
.obs
= m4_arg_scratch (context
);
336 m4_dump_symbols (context
, &data
, argc
, argv
, true);
337 m4_sysval_flush (context
, false);
339 for (; data
.size
> 0; --data
.size
, data
.base
++)
341 m4_symbol
*symbol
= m4_symbol_lookup (M4SYMTAB
, data
.base
->str
,
347 /* TODO - add debugmode(b) option to control quoting style. */
348 obstack_grow (obs
, data
.base
->str
, data
.base
->len
);
349 obstack_1grow (obs
, ':');
350 obstack_1grow (obs
, '\t');
351 m4_symbol_print (context
, symbol
, obs
, quotes
, stack
, arg_length
,
353 obstack_1grow (obs
, '\n');
354 len
= obstack_object_size (obs
);
355 value
= (char *) obstack_finish (obs
);
356 fwrite (value
, 1, len
, output
);
357 obstack_free (obs
, value
);
361 /* The macro "defn" returns the quoted definition of the macro named by
362 the first argument. If the macro is builtin, it will push a special
363 macro-definition token on the input stack. */
364 M4BUILTIN_HANDLER (defn
)
366 const m4_call_info
*me
= m4_arg_info (argv
);
369 for (i
= 1; i
< argc
; i
++)
371 m4_symbol
*symbol
= m4_symbol_value_lookup (context
, argv
, i
, true);
375 else if (m4_is_symbol_text (symbol
))
376 m4_shipout_string (context
, obs
, m4_get_symbol_text (symbol
),
377 m4_get_symbol_len (symbol
), true);
378 else if (m4_is_symbol_func (symbol
))
379 m4_push_builtin (context
, obs
, m4_get_symbol_value (symbol
));
380 else if (m4_is_symbol_placeholder (symbol
))
381 m4_warn (context
, 0, me
,
382 _("%s: builtin %s requested by frozen file not found"),
383 quotearg_n_mem (2, M4ARG (i
), M4ARGLEN (i
)),
384 quotearg_style (locale_quoting_style
,
385 m4_get_symbol_placeholder (symbol
)));
388 assert (!"Bad token data type in m4_defn");
395 /* This section contains macros to handle the builtins "syscmd"
398 /* Exit code from last "syscmd" command. */
399 /* FIXME - we should preserve this value across freezing. See
400 http://lists.gnu.org/archive/html/bug-m4/2006-06/msg00059.html
401 for ideas on how do to that. */
402 static int m4_sysval
= 0;
405 m4_set_sysval (int value
)
410 /* Flush a given output STREAM. If REPORT, also print an error
411 message and clear the stream error bit. */
413 sysval_flush_helper (m4
*context
, FILE *stream
, bool report
)
415 if (fflush (stream
) == EOF
&& report
)
417 m4_error (context
, 0, errno
, NULL
, _("write error"));
422 /* Flush all user output streams, prior to doing something that can
423 could lose unflushed data or interleave debug and normal output
424 incorrectly. If REPORT, then print an error message on failure and
425 clear the stream error bit; otherwise a subsequent ferror can track
426 that an error occurred. */
428 m4_sysval_flush (m4
*context
, bool report
)
430 FILE *debug_file
= m4_get_debug_file (context
);
432 if (debug_file
!= stdout
)
433 sysval_flush_helper (context
, stdout
, report
);
434 if (debug_file
!= stderr
)
435 /* If we have problems with stderr, we can't really report that
436 problem to stderr. The closeout module will ensure the exit
437 status reflects the problem, though. */
439 if (debug_file
!= NULL
)
440 sysval_flush_helper (context
, debug_file
, report
);
441 /* POSIX requires that if m4 doesn't consume all input, but stdin is
442 opened on a seekable file, that the file pointer be left at the
443 next character on exit (but places no restrictions on the file
444 pointer location on a non-seekable file). It also requires that
445 fflush() followed by fseeko() on an input file set the underlying
446 file pointer, and gnulib guarantees these semantics. However,
447 fflush() on a non-seekable file can lose buffered data, which we
448 might otherwise want to process after syscmd. Hence, we must
449 check whether stdin is seekable. We must also be tolerant of
450 operating with stdin closed, so we don't report any failures in
451 this attempt. The stdio-safer module and friends are essential,
452 so that if stdin was closed, this lseek is not on some other file
453 that we have since opened. */
454 if (lseek (STDIN_FILENO
, 0, SEEK_CUR
) >= 0
455 && fflush (stdin
) == 0)
457 fseeko (stdin
, 0, SEEK_CUR
);
461 M4BUILTIN_HANDLER (syscmd
)
463 const m4_call_info
*me
= m4_arg_info (argv
);
464 const char *cmd
= M4ARG (1);
465 size_t len
= M4ARGLEN (1);
468 const char *prog_args
[4] = { "sh", "-c" };
470 if (m4_get_safer_opt (context
))
472 m4_error (context
, 0, 0, m4_arg_info (argv
), _("disabled by --safer"));
475 if (strlen (cmd
) != len
)
476 m4_warn (context
, 0, me
, _("argument %s truncated"),
477 quotearg_style_mem (locale_quoting_style
, cmd
, len
));
479 /* Optimize the empty command. */
485 m4_sysval_flush (context
, false);
487 if (strstr (M4_SYSCMD_SHELL
, "cmd"))
489 prog_args
[0] = "cmd";
495 status
= execute (m4_info_name (me
), M4_SYSCMD_SHELL
, (char **) prog_args
,
496 false, false, false, false, true, false, &sig_status
);
499 assert (status
== 127);
500 m4_sysval
= sig_status
<< 8;
504 if (status
== 127 && errno
)
505 m4_warn (context
, errno
, me
, _("cannot run command %s"),
506 quotearg_style (locale_quoting_style
, cmd
));
512 M4BUILTIN_HANDLER (sysval
)
514 m4_shipout_int (obs
, m4_sysval
);
518 M4BUILTIN_HANDLER (incr
)
522 if (!m4_numeric_arg (context
, m4_arg_info (argv
), M4ARG (1), M4ARGLEN (1),
526 m4_shipout_int (obs
, value
+ 1);
529 M4BUILTIN_HANDLER (decr
)
533 if (!m4_numeric_arg (context
, m4_arg_info (argv
), M4ARG (1), M4ARGLEN (1),
537 m4_shipout_int (obs
, value
- 1);
541 /* This section contains the macros "divert", "undivert" and "divnum" for
542 handling diversion. The utility functions used lives in output.c. */
544 /* Divert further output to the diversion given by ARGV[1]. Out of range
545 means discard further output. */
546 M4BUILTIN_HANDLER (divert
)
550 if (argc
>= 2 && !m4_numeric_arg (context
, m4_arg_info (argv
), M4ARG (1),
553 m4_make_diversion (context
, i
);
554 m4_divert_text (context
, NULL
, M4ARG (2), M4ARGLEN (2),
555 m4_get_current_line (context
));
558 /* Expand to the current diversion number. */
559 M4BUILTIN_HANDLER (divnum
)
561 m4_shipout_int (obs
, m4_get_current_diversion (context
));
564 /* Bring back the diversion given by the argument list. If none is
565 specified, bring back all diversions. GNU specific is the option
566 of undiverting the named file, by passing a non-numeric argument to
569 M4BUILTIN_HANDLER (undivert
)
572 const m4_call_info
*me
= m4_arg_info (argv
);
575 m4_undivert_all (context
);
577 for (i
= 1; i
< argc
; i
++)
579 const char *str
= M4ARG (i
);
580 size_t len
= M4ARGLEN (i
);
582 int diversion
= strtol (str
, &endp
, 10);
583 if (endp
- str
== len
&& !isspace ((unsigned char) *str
))
584 m4_insert_diversion (context
, diversion
);
585 else if (m4_get_posixly_correct_opt (context
))
586 m4_warn (context
, 0, me
, _("non-numeric argument %s"),
587 quotearg_style_mem (locale_quoting_style
, str
, len
));
588 else if (strlen (str
) != len
)
589 m4_warn (context
, 0, me
, _("invalid file name %s"),
590 quotearg_style_mem (locale_quoting_style
, str
, len
));
593 char *filepath
= m4_path_search (context
, str
, NULL
);
594 FILE *fp
= m4_fopen (context
, filepath
, "r");
599 m4_insert_file (context
, fp
);
600 if (fclose (fp
) == EOF
)
601 m4_error (context
, 0, errno
, me
, _("error undiverting %s"),
602 quotearg_style (locale_quoting_style
, str
));
605 m4_error (context
, 0, errno
, me
, _("cannot undivert %s"),
606 quotearg_style (locale_quoting_style
, str
));
612 /* This section contains various macros, which does not fall into
613 any specific group. These are "dnl", "shift", "changequote",
614 "changecom" and "changesyntax" */
616 /* Delete all subsequent whitespace from input. The function skip_line ()
618 M4BUILTIN_HANDLER (dnl
)
620 m4_skip_line (context
, m4_arg_info (argv
));
623 /* Shift all arguments one to the left, discarding the first argument.
624 Each output argument is quoted with the current quotes. */
625 M4BUILTIN_HANDLER (shift
)
627 m4_push_args (context
, obs
, argv
, true, true);
630 /* Change the current quotes. The function set_quotes () lives in
632 M4BUILTIN_HANDLER (changequote
)
634 m4_set_quotes (M4SYNTAX
,
635 (argc
>= 2) ? M4ARG (1) : NULL
, M4ARGLEN (1),
636 (argc
>= 3) ? M4ARG (2) : NULL
, M4ARGLEN (2));
639 /* Change the current comment delimiters. The function set_comment ()
640 lives in syntax.c. */
641 M4BUILTIN_HANDLER (changecom
)
643 m4_set_comment (M4SYNTAX
,
644 (argc
>= 2) ? M4ARG (1) : NULL
, M4ARGLEN (1),
645 (argc
>= 3) ? M4ARG (2) : NULL
, M4ARGLEN (2));
649 /* This section contains macros for inclusion of other files -- "include"
650 and "sinclude". This differs from bringing back diversions, in that
651 the input is scanned before being copied to the output. */
654 include (m4
*context
, m4_obstack
*obs
, size_t argc
, m4_macro_args
*argv
, bool silent
)
656 const m4_call_info
*me
= m4_arg_info (argv
);
657 const char *arg
= M4ARG (1);
658 size_t len
= M4ARGLEN (1);
660 if (strlen (arg
) != len
)
661 m4_warn (context
, 0, me
, _("argument %s truncated"),
662 quotearg_style_mem (locale_quoting_style
, arg
, len
));
663 m4_load_filename (context
, me
, arg
, obs
, silent
);
666 /* Include a file, complaining in case of errors. */
667 M4BUILTIN_HANDLER (include
)
669 include (context
, obs
, argc
, argv
, false);
672 /* Include a file, ignoring errors. */
673 M4BUILTIN_HANDLER (sinclude
)
675 include (context
, obs
, argc
, argv
, true);
679 /* More miscellaneous builtins -- "maketemp", "errprint". */
681 /* Add trailing `X' to PATTERN of length LEN as necessary, then
682 securely create the temporary file system object. If DIR, create a
683 directory instead of a file. Report errors on behalf of CALLER. If
684 successful, output the quoted resulting name on OBS. */
686 m4_make_temp (m4
*context
, m4_obstack
*obs
, const m4_call_info
*caller
,
687 const char *pattern
, size_t len
, bool dir
)
692 const m4_string_pair
*quotes
= m4_get_syntax_quotes (M4SYNTAX
);
694 if (m4_get_safer_opt (context
))
696 m4_error (context
, 0, 0, caller
, _("disabled by --safer"));
700 /* Guarantee that there are six trailing 'X' characters, even if the
701 user forgot to supply them. Output must be quoted if
703 assert (obstack_object_size (obs
) == 0);
704 obstack_grow (obs
, quotes
->str1
, quotes
->len1
);
705 if (strlen (pattern
) < len
)
707 m4_warn (context
, 0, caller
, _("argument %s truncated"),
708 quotearg_style_mem (locale_quoting_style
, pattern
, len
));
709 len
= strlen (pattern
);
711 obstack_grow (obs
, pattern
, len
);
712 for (i
= 0; len
> 0 && i
< 6; i
++)
713 if (pattern
[--len
] != 'X')
715 obstack_grow0 (obs
, "XXXXXX", 6 - i
);
716 name
= (char *) obstack_base (obs
) + quotes
->len1
;
718 /* Make the temporary object. */
720 fd
= gen_tempname (name
, 0, 0, dir
? GT_DIR
: GT_FILE
);
723 /* This use of _() will need to change if xgettext ever changes
724 its undocumented behavior of parsing both string options. */
726 m4_warn (context
, errno
, caller
,
727 _(dir
? "cannot create directory from template %s"
728 : "cannot create file from template %s"),
729 quotearg_style (locale_quoting_style
, pattern
));
730 obstack_free (obs
, obstack_finish (obs
));
736 /* Remove NUL, then finish quote. */
737 obstack_blank (obs
, -1);
738 obstack_grow (obs
, quotes
->str2
, quotes
->len2
);
742 /* Use the first argument as at template for a temporary file name. */
743 M4BUILTIN_HANDLER (maketemp
)
745 const m4_call_info
*me
= m4_arg_info (argv
);
746 m4_warn (context
, 0, me
, _("recommend using mkstemp instead"));
747 if (m4_get_posixly_correct_opt (context
))
749 /* POSIX states "any trailing 'X' characters [are] replaced with
750 the current process ID as a string", without referencing the
751 file system. Horribly insecure, but we have to do it.
753 For reference, Solaris m4 does:
756 maketemp(XX) -> `Xn', where n is last digit of pid
757 maketemp(XXXXXXXX) -> `X00nnnnn', where nnnnn is 16-bit pid
759 const char *str
= M4ARG (1);
760 size_t len
= M4ARGLEN (1);
762 m4_obstack
*scratch
= m4_arg_scratch (context
);
763 size_t pid_len
= obstack_printf (scratch
, "%lu",
764 (unsigned long) getpid ());
765 char *pid
= (char *) obstack_copy0 (scratch
, "", 0);
767 for (i
= len
; i
> 1; i
--)
768 if (str
[i
- 1] != 'X')
770 obstack_grow (obs
, str
, i
);
771 if (len
- i
< pid_len
)
772 obstack_grow (obs
, pid
+ pid_len
- (len
- i
), len
- i
);
774 obstack_printf (obs
, "%.*d%s", len
- i
- pid_len
, 0, pid
);
777 m4_make_temp (context
, obs
, me
, M4ARG (1), M4ARGLEN (1), false);
780 /* Use the first argument as a template for a temporary file name. */
781 M4BUILTIN_HANDLER (mkstemp
)
783 m4_make_temp (context
, obs
, m4_arg_info (argv
), M4ARG (1), M4ARGLEN (1),
787 /* Print all arguments on standard error. */
788 M4BUILTIN_HANDLER (errprint
)
792 m4_sysval_flush (context
, false);
793 /* The close_stdin module makes it safe to skip checking the return
795 fwrite (M4ARG (1), 1, M4ARGLEN (1), stderr
);
796 for (i
= 2; i
< m4_arg_argc (argv
); i
++)
799 fwrite (M4ARG (i
), 1, M4ARGLEN (i
), stderr
);
805 /* This section contains various macros for exiting, saving input until
806 EOF is seen, and tracing macro calls. That is: "m4exit", "m4wrap",
807 "traceon" and "traceoff". */
809 /* Exit immediately, with exitcode specified by the first argument, 0 if no
810 arguments are present. */
811 M4BUILTIN_HANDLER (m4exit
)
813 const m4_call_info
*me
= m4_arg_info (argv
);
814 int exit_code
= EXIT_SUCCESS
;
816 /* Warn on bad arguments, but still exit. */
817 if (argc
>= 2 && !m4_numeric_arg (context
, me
, M4ARG (1), M4ARGLEN (1),
819 exit_code
= EXIT_FAILURE
;
820 if (exit_code
< 0 || exit_code
> 255)
822 m4_warn (context
, 0, me
, _("exit status out of range: `%d'"), exit_code
);
823 exit_code
= EXIT_FAILURE
;
826 /* Ensure that atexit handlers see correct nonzero status. */
827 if (exit_code
!= EXIT_SUCCESS
)
828 m4_set_exit_failure (exit_code
);
830 /* Change debug stream back to stderr, to force flushing debug
831 stream and detect any errors. */
832 m4_debug_set_output (context
, me
, NULL
);
833 m4_sysval_flush (context
, true);
835 /* Check for saved error. */
836 if (exit_code
== 0 && m4_get_exit_status (context
) != 0)
837 exit_code
= m4_get_exit_status (context
);
841 /* Save the argument text until EOF has been seen, allowing for user
842 specified cleanup action. GNU version saves all arguments, the standard
843 version only the first. */
844 M4BUILTIN_HANDLER (m4wrap
)
846 m4_wrap_args (context
, argv
);
849 /* Enable tracing of all specified macros, or all, if none is specified.
850 Tracing is disabled by default, when a macro is defined. This can be
851 overridden by the "t" debug flag. */
853 M4BUILTIN_HANDLER (traceon
)
855 const m4_call_info
*me
= m4_arg_info (argv
);
859 m4_set_debug_level_opt (context
, (m4_get_debug_level_opt (context
)
860 | M4_DEBUG_TRACE_ALL
));
862 for (i
= 1; i
< argc
; i
++)
863 if (m4_is_arg_text (argv
, i
))
864 m4_set_symbol_name_traced (M4SYMTAB
, M4ARG (i
), M4ARGLEN (i
), true);
866 m4_warn (context
, 0, me
, _("invalid macro name ignored"));
869 /* Disable tracing of all specified macros, or all, if none is specified. */
870 M4BUILTIN_HANDLER (traceoff
)
872 const m4_call_info
*me
= m4_arg_info (argv
);
876 m4_set_debug_level_opt (context
, (m4_get_debug_level_opt (context
)
877 & ~M4_DEBUG_TRACE_ALL
));
879 for (i
= 1; i
< argc
; i
++)
880 if (m4_is_arg_text (argv
, i
))
881 m4_set_symbol_name_traced (M4SYMTAB
, M4ARG (i
), M4ARGLEN (i
), false);
883 m4_warn (context
, 0, me
, _("invalid macro name ignored"));
887 /* This section contains text processing macros: "len", "index",
888 "substr", "translit", "format", "regexp" and "patsubst". The last
889 three are GNU specific. */
891 /* Expand to the length of the first argument. */
892 M4BUILTIN_HANDLER (len
)
894 m4_shipout_int (obs
, M4ARGLEN (1));
897 /* The macro expands to the first index of the second argument in the
898 first argument. As an extension, start the search at the index
899 indicated by the third argument. */
900 M4BUILTIN_HANDLER (index
)
902 const char *haystack
= M4ARG (1);
903 size_t haystack_len
= M4ARGLEN (1);
904 const char *needle
= M4ARG (2);
905 const char *result
= NULL
;
909 if (!m4_arg_empty (argv
, 3) && !m4_numeric_arg (context
, m4_arg_info (argv
),
910 M4ARG (3), M4ARGLEN (3),
915 offset
+= haystack_len
;
919 else if (haystack_len
< offset
)
921 m4_shipout_int (obs
, -1);
925 /* Rely on the optimizations guaranteed by gnulib's memmem
927 result
= (char *) memmem (haystack
+ offset
, haystack_len
- offset
, needle
,
930 retval
= result
- haystack
;
932 m4_shipout_int (obs
, retval
);
935 /* The macro "substr" extracts substrings from the first argument,
936 starting from the index given by the second argument, extending for
937 a length given by the third argument. If the third argument is
938 missing or empty, the substring extends to the end of the first
939 argument. As an extension, negative arguments are treated as
940 indices relative to the string length. Also, if a fourth argument
941 is supplied, the original string is output with the selected
942 substring replaced by the argument. */
943 M4BUILTIN_HANDLER (substr
)
945 const m4_call_info
*me
= m4_arg_info (argv
);
946 const char *str
= M4ARG (1);
953 m4_push_arg (context
, obs
, argv
, 1);
957 length
= M4ARGLEN (1);
958 if (!m4_arg_empty (argv
, 2)
959 && !m4_numeric_arg (context
, me
, M4ARG (2), M4ARGLEN (2), &start
))
964 if (m4_arg_empty (argv
, 3))
968 if (!m4_numeric_arg (context
, me
, M4ARG (3), M4ARGLEN (3), &end
))
978 /* Replacement text provided. */
981 if (end
< 0 || length
< start
)
983 m4_warn (context
, 0, me
, _("substring out of range"));
990 obstack_grow (obs
, str
, start
);
991 m4_push_arg (context
, obs
, argv
, 4);
992 obstack_grow (obs
, str
+ end
, length
- end
);
1003 obstack_grow (obs
, str
+ start
, end
- start
);
1007 /* Any ranges in string S of length *LEN are expanded, using OBS for
1008 scratch space, and the expansion returned. *LEN is set to the
1009 expanded length. A single - (dash) can be included in the strings
1010 by being the first or the last character in the string. If the
1011 first character in a range is after the first in the character set,
1012 the range is made backwards, thus 9-0 is the string 9876543210. */
1014 m4_expand_ranges (const char *s
, size_t *len
, m4_obstack
*obs
)
1018 const char *end
= s
+ *len
;
1020 assert (obstack_object_size (obs
) == 0);
1023 obstack_1grow (obs
, from
);
1025 for ( ; s
!= end
; from
= *s
++)
1032 obstack_1grow (obs
, '-');
1039 obstack_1grow (obs
, from
);
1043 while (--from
>= to
)
1044 obstack_1grow (obs
, from
);
1048 obstack_1grow (obs
, *s
);
1050 *len
= obstack_object_size (obs
);
1051 return (char *) obstack_finish (obs
);
1054 /* The macro "translit" translates all characters in the first
1055 argument, which are present in the second argument, into the
1056 corresponding character from the third argument. If the third
1057 argument is shorter than the second, the extra characters in the
1058 second argument are deleted from the first. */
1059 M4BUILTIN_HANDLER (translit
)
1066 char map
[UCHAR_MAX
+ 1];
1067 char found
[UCHAR_MAX
+ 1];
1070 enum { ASIS
, REPLACE
, DELETE
};
1072 if (m4_arg_empty (argv
, 1) || m4_arg_empty (argv
, 2))
1074 m4_push_arg (context
, obs
, argv
, 1);
1079 from_len
= M4ARGLEN (2);
1082 to_len
= M4ARGLEN (3);
1083 if (memchr (to
, '-', to_len
) != NULL
)
1084 to
= m4_expand_ranges (to
, &to_len
, m4_arg_scratch (context
));
1086 /* If there are only one or two bytes to replace, it is faster to
1087 use memchr2. Using expand_ranges does nothing unless there are
1088 at least three bytes. */
1092 size_t len
= M4ARGLEN (1);
1093 int second
= from
[from_len
/ 2];
1095 while ((p
= (char *) memchr2 (data
, from
[0], second
, len
)))
1097 obstack_grow (obs
, data
, p
- data
);
1098 len
-= p
- data
+ 1;
1100 if (*p
== from
[0] && to_len
)
1101 obstack_1grow (obs
, to
[0]);
1102 else if (*p
== second
&& 1 < to_len
)
1103 obstack_1grow (obs
, to
[1]);
1105 obstack_grow (obs
, data
, len
);
1109 if (memchr (from
, '-', from_len
) != NULL
)
1110 from
= m4_expand_ranges (from
, &from_len
, m4_arg_scratch (context
));
1112 /* Calling memchr(from) for each character in data is quadratic,
1113 since both strings can be arbitrarily long. Instead, create a
1114 from-to mapping in one pass of from, then use that map in one
1115 pass of data, for linear behavior. Traditional behavior is that
1116 only the first instance of a character in from is consulted,
1117 hence the found map. */
1118 memset (map
, 0, sizeof map
);
1119 memset (found
, 0, sizeof found
);
1123 if (found
[ch
] == ASIS
)
1127 found
[ch
] = REPLACE
;
1141 from_len
= M4ARGLEN (1);
1148 obstack_1grow (obs
, ch
);
1151 obstack_1grow (obs
, map
[ch
]);
1156 assert (!"translit");
1164 /* The rest of this file contains the functions to evaluate integer
1165 * expressions for the "eval" macro. `number' should be at least 32 bits.
1167 #define numb_set(ans, x) ((ans) = (x))
1168 #define numb_set_si(ans, si) (*(ans) = (number) (si))
1170 #define numb_ZERO ((number) 0)
1171 #define numb_ONE ((number) 1)
1173 #define numb_init(x) ((x) = numb_ZERO)
1174 #define numb_fini(x)
1176 #define numb_incr(n) ((n) += numb_ONE)
1177 #define numb_decr(n) ((n) -= numb_ONE)
1179 #define numb_zerop(x) ((x) == numb_ZERO)
1180 #define numb_positivep(x) ((x) > numb_ZERO)
1181 #define numb_negativep(x) ((x) < numb_ZERO)
1183 #define numb_eq(x, y) ((x) = ((x) == (y)))
1184 #define numb_ne(x, y) ((x) = ((x) != (y)))
1185 #define numb_lt(x, y) ((x) = ((x) < (y)))
1186 #define numb_le(x, y) ((x) = ((x) <= (y)))
1187 #define numb_gt(x, y) ((x) = ((x) > (y)))
1188 #define numb_ge(x, y) ((x) = ((x) >= (y)))
1190 #define numb_lnot(x) ((x) = (!(x)))
1191 #define numb_lior(x, y) ((x) = ((x) || (y)))
1192 #define numb_land(x, y) ((x) = ((x) && (y)))
1194 #define numb_not(c, x) (*(x) = ~ *(x))
1195 #define numb_eor(c, x, y) (*(x) = *(x) ^ *(y))
1196 #define numb_ior(c, x, y) (*(x) = *(x) | *(y))
1197 #define numb_and(c, x, y) (*(x) = *(x) & *(y))
1199 #define numb_plus(x, y) ((x) = ((x) + (y)))
1200 #define numb_minus(x, y) ((x) = ((x) - (y)))
1201 #define numb_negate(x) ((x) = (- (x)))
1203 #define numb_times(x, y) ((x) = ((x) * (y)))
1204 /* Be careful of x86 SIGFPE. */
1205 #define numb_ratio(x, y) \
1206 (((y) == -1) ? (numb_negate (x)) : ((x) /= (y)))
1207 #define numb_divide(x, y) \
1208 ((*(y) == -1) ? (numb_negate (*(y))) : (*(x) /= *(y)))
1209 #define numb_modulo(c, x, y) \
1210 ((*(y) == -1) ? (*(x) = numb_ZERO) : (*(x) %= *(y)))
1211 /* numb_invert is only used in the context of x**-y, which integral math
1212 does not support. */
1213 #define numb_invert(x) return NEGATIVE_EXPONENT
1215 /* Minimize undefined C behavior (shifting by a negative number,
1216 shifting by the width or greater, left shift overflow, or right
1217 shift of a negative number). Implement Java wrap-around semantics,
1218 with implicit masking of shift amount. This code assumes that the
1219 implementation-defined overflow when casting unsigned to signed is
1220 a silent twos-complement wrap-around. */
1221 #define shift_mask (sizeof (number) * CHAR_BIT - 1)
1222 #define numb_lshift(c, x, y) \
1223 (*(x) = (number) ((unumber) *(x) << (*(y) & shift_mask)))
1224 #define numb_rshift(c, x, y) \
1225 (*(x) = (number) (*(x) < 0 \
1226 ? ~(~(unumber) *(x) >> (*(y) & shift_mask)) \
1227 : (unumber) *(x) >> (*(y) & shift_mask)))
1228 #define numb_urshift(c, x, y) \
1229 (*(x) = (number) ((unumber) *(x) >> (*(y) & shift_mask)))
1232 /* The function ntoa () converts VALUE to a signed ASCII representation in
1233 radix RADIX. Radix must be between 2 and 36, inclusive. */
1235 ntoa (number value
, int radix
)
1237 /* Digits for number to ASCII conversions. */
1238 static char const ntoa_digits
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
1242 /* Sized for radix 2, plus sign and trailing NUL. */
1243 static char str
[sizeof value
* CHAR_BIT
+ 2];
1244 char *s
= &str
[sizeof str
];
1251 uvalue
= (unumber
) -value
;
1256 uvalue
= (unumber
) value
;
1261 *--s
= ntoa_digits
[uvalue
% radix
];
1272 numb_obstack (m4_obstack
*obs
, number value
, int radix
, int min
)
1282 obstack_1grow (obs
, '-');
1289 obstack_blank (obs
, min
- uvalue
);
1290 memset ((char *) obstack_next_free (obs
) - (min
- uvalue
), '0',
1293 obstack_blank (obs
, uvalue
);
1294 memset ((char *) obstack_next_free (obs
) - uvalue
, '1', uvalue
);
1298 s
= ntoa (value
, radix
);
1302 obstack_1grow (obs
, '-');
1309 obstack_blank (obs
, min
);
1310 memset ((char *) obstack_next_free (obs
) - min
, '0', min
);
1312 obstack_grow (obs
, s
, len
);
1317 numb_initialise (void)
1322 /* This macro defines the top level code for the "eval" builtin. The
1323 actual work is done in the function m4_evaluate (), which lives in
1325 #define m4_evaluate builtin_eval
1326 #include "evalparse.c"