2014-07-27 22:54:13 +00:00
|
|
|
/* $OpenBSD: eval.c,v 1.73 2014/07/11 21:04:17 espie Exp $ */
|
2002-04-19 17:26:21 +00:00
|
|
|
/* $NetBSD: eval.c,v 1.7 1996/11/10 21:21:29 pk Exp $ */
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Ozan Yigit at York University.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2011-11-28 13:32:39 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-27 12:33:43 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2002-04-19 17:26:21 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2011-11-28 13:32:39 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* eval.c
|
|
|
|
* Facility: m4 macro processor
|
|
|
|
* by: oz
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2011-11-28 13:32:39 +00:00
|
|
|
#include <err.h>
|
2002-04-19 17:26:21 +00:00
|
|
|
#include <errno.h>
|
2011-11-28 13:32:39 +00:00
|
|
|
#include <limits.h>
|
2002-04-19 17:26:21 +00:00
|
|
|
#include <unistd.h>
|
2011-11-28 13:32:39 +00:00
|
|
|
#include <stdint.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2002-04-19 17:26:21 +00:00
|
|
|
#include <stddef.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <string.h>
|
2002-04-19 17:26:21 +00:00
|
|
|
#include <fcntl.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include "mdef.h"
|
|
|
|
#include "stdd.h"
|
|
|
|
#include "extern.h"
|
|
|
|
#include "pathnames.h"
|
|
|
|
|
2002-04-19 17:26:21 +00:00
|
|
|
static void dodefn(const char *);
|
|
|
|
static void dopushdef(const char *, const char *);
|
|
|
|
static void dodump(const char *[], int);
|
|
|
|
static void dotrace(const char *[], int, int);
|
|
|
|
static void doifelse(const char *[], int);
|
|
|
|
static int doincl(const char *);
|
|
|
|
static int dopaste(const char *);
|
|
|
|
static void dochq(const char *[], int);
|
|
|
|
static void dochc(const char *[], int);
|
2011-11-28 13:32:39 +00:00
|
|
|
static void dom4wrap(const char *);
|
2002-04-19 17:26:21 +00:00
|
|
|
static void dodiv(int);
|
|
|
|
static void doundiv(const char *[], int);
|
|
|
|
static void dosub(const char *[], int);
|
|
|
|
static void map(char *, const char *, const char *, const char *);
|
|
|
|
static const char *handledash(char *, char *, const char *);
|
|
|
|
static void expand_builtin(const char *[], int, int);
|
|
|
|
static void expand_macro(const char *[], int);
|
2011-11-28 13:32:39 +00:00
|
|
|
static void dump_one_def(const char *, struct macro_definition *);
|
2002-04-19 17:26:21 +00:00
|
|
|
|
|
|
|
unsigned long expansion_id;
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
2002-04-19 17:26:21 +00:00
|
|
|
* eval - eval all macros and builtins calls
|
1994-05-27 12:33:43 +00:00
|
|
|
* argc - number of elements in argv.
|
|
|
|
* argv - element vector :
|
|
|
|
* argv[0] = definition of a user
|
2011-11-28 13:32:39 +00:00
|
|
|
* macro or NULL if built-in.
|
1994-05-27 12:33:43 +00:00
|
|
|
* argv[1] = name of the macro or
|
|
|
|
* built-in.
|
|
|
|
* argv[2] = parameters to user-defined
|
|
|
|
* . macro or built-in.
|
|
|
|
* .
|
|
|
|
*
|
2002-04-19 17:26:21 +00:00
|
|
|
* A call in the form of macro-or-builtin() will result in:
|
1994-05-27 12:33:43 +00:00
|
|
|
* argv[0] = nullstr
|
|
|
|
* argv[1] = macro-or-builtin
|
|
|
|
* argv[2] = nullstr
|
2002-04-19 17:26:21 +00:00
|
|
|
*
|
|
|
|
* argc is 3 for macro-or-builtin() and 2 for macro-or-builtin
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
|
|
|
void
|
2011-11-28 13:32:39 +00:00
|
|
|
eval(const char *argv[], int argc, int td, int is_traced)
|
2002-04-19 17:26:21 +00:00
|
|
|
{
|
2011-11-28 13:32:39 +00:00
|
|
|
size_t mark = SIZE_MAX;
|
2002-04-19 17:26:21 +00:00
|
|
|
|
|
|
|
expansion_id++;
|
2002-07-15 02:15:12 +00:00
|
|
|
if (td & RECDEF)
|
2011-11-28 13:32:39 +00:00
|
|
|
m4errx(1, "expanding recursive definition for %s.", argv[1]);
|
|
|
|
if (is_traced)
|
2002-04-19 17:26:21 +00:00
|
|
|
mark = trace(argv, argc, infile+ilevel);
|
|
|
|
if (td == MACRTYPE)
|
|
|
|
expand_macro(argv, argc);
|
|
|
|
else
|
|
|
|
expand_builtin(argv, argc, td);
|
2011-11-28 13:32:39 +00:00
|
|
|
if (mark != SIZE_MAX)
|
2002-04-19 17:26:21 +00:00
|
|
|
finish_trace(mark);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* expand_builtin - evaluate built-in macros.
|
|
|
|
*/
|
|
|
|
void
|
2002-05-01 21:37:29 +00:00
|
|
|
expand_builtin(const char *argv[], int argc, int td)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-04-19 17:26:21 +00:00
|
|
|
int c, n;
|
|
|
|
int ac;
|
1994-05-27 12:33:43 +00:00
|
|
|
static int sysval = 0;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("argc = %d\n", argc);
|
|
|
|
for (n = 0; n < argc; n++)
|
|
|
|
printf("argv[%d] = %s\n", n, argv[n]);
|
2002-04-19 17:26:21 +00:00
|
|
|
fflush(stdout);
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif
|
2002-04-19 17:26:21 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* if argc == 3 and argv[2] is null, then we
|
|
|
|
* have macro-or-builtin() type call. We adjust
|
|
|
|
* argc to avoid further checking..
|
|
|
|
*/
|
2011-11-28 13:32:39 +00:00
|
|
|
/* we keep the initial value for those built-ins that differentiate
|
|
|
|
* between builtin() and builtin.
|
|
|
|
*/
|
|
|
|
ac = argc;
|
2002-04-19 17:26:21 +00:00
|
|
|
|
2011-11-28 13:32:39 +00:00
|
|
|
if (argc == 3 && !*(argv[2]) && !mimic_gnu)
|
1994-05-27 12:33:43 +00:00
|
|
|
argc--;
|
|
|
|
|
2002-04-19 17:26:21 +00:00
|
|
|
switch (td & TYPEMASK) {
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
case DEFITYPE:
|
|
|
|
if (argc > 2)
|
|
|
|
dodefine(argv[2], (argc > 3) ? argv[3] : null);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PUSDTYPE:
|
|
|
|
if (argc > 2)
|
|
|
|
dopushdef(argv[2], (argc > 3) ? argv[3] : null);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DUMPTYPE:
|
|
|
|
dodump(argv, argc);
|
|
|
|
break;
|
|
|
|
|
2002-04-19 17:26:21 +00:00
|
|
|
case TRACEONTYPE:
|
|
|
|
dotrace(argv, argc, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TRACEOFFTYPE:
|
|
|
|
dotrace(argv, argc, 0);
|
|
|
|
break;
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
case EXPRTYPE:
|
|
|
|
/*
|
|
|
|
* doexpr - evaluate arithmetic
|
|
|
|
* expression
|
|
|
|
*/
|
2011-11-28 13:32:39 +00:00
|
|
|
{
|
|
|
|
int base = 10;
|
|
|
|
int maxdigits = 0;
|
|
|
|
const char *errstr;
|
|
|
|
|
|
|
|
if (argc > 3) {
|
|
|
|
base = strtonum(argv[3], 2, 36, &errstr);
|
|
|
|
if (errstr) {
|
|
|
|
m4errx(1, "expr: base %s invalid.", argv[3]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (argc > 4) {
|
|
|
|
maxdigits = strtonum(argv[4], 0, INT_MAX, &errstr);
|
|
|
|
if (errstr) {
|
|
|
|
m4errx(1, "expr: maxdigits %s invalid.", argv[4]);
|
|
|
|
}
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
if (argc > 2)
|
2011-11-28 13:32:39 +00:00
|
|
|
pbnumbase(expr(argv[2]), base, maxdigits);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
2011-11-28 13:32:39 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
case IFELTYPE:
|
|
|
|
if (argc > 4)
|
|
|
|
doifelse(argv, argc);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IFDFTYPE:
|
|
|
|
/*
|
|
|
|
* doifdef - select one of two
|
|
|
|
* alternatives based on the existence of
|
|
|
|
* another definition
|
|
|
|
*/
|
|
|
|
if (argc > 3) {
|
2011-11-28 13:32:39 +00:00
|
|
|
if (lookup_macro_definition(argv[2]) != NULL)
|
1994-05-27 12:33:43 +00:00
|
|
|
pbstr(argv[3]);
|
|
|
|
else if (argc > 4)
|
|
|
|
pbstr(argv[4]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LENGTYPE:
|
|
|
|
/*
|
|
|
|
* dolen - find the length of the
|
|
|
|
* argument
|
|
|
|
*/
|
2001-05-29 03:32:38 +00:00
|
|
|
pbnum((argc > 2) ? strlen(argv[2]) : 0);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case INCRTYPE:
|
|
|
|
/*
|
|
|
|
* doincr - increment the value of the
|
|
|
|
* argument
|
|
|
|
*/
|
|
|
|
if (argc > 2)
|
|
|
|
pbnum(atoi(argv[2]) + 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DECRTYPE:
|
|
|
|
/*
|
|
|
|
* dodecr - decrement the value of the
|
|
|
|
* argument
|
|
|
|
*/
|
|
|
|
if (argc > 2)
|
|
|
|
pbnum(atoi(argv[2]) - 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SYSCTYPE:
|
|
|
|
/*
|
|
|
|
* dosys - execute system command
|
1995-05-30 06:41:30 +00:00
|
|
|
*/
|
2003-05-01 02:36:27 +00:00
|
|
|
if (argc > 2) {
|
2011-11-28 13:32:39 +00:00
|
|
|
fflush(stdout);
|
1994-05-27 12:33:43 +00:00
|
|
|
sysval = system(argv[2]);
|
2003-05-01 02:36:27 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SYSVTYPE:
|
|
|
|
/*
|
|
|
|
* dosysval - return value of the last
|
|
|
|
* system call.
|
2002-07-15 02:15:12 +00:00
|
|
|
*
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
|
|
|
pbnum(sysval);
|
|
|
|
break;
|
|
|
|
|
2002-04-19 17:26:21 +00:00
|
|
|
case ESYSCMDTYPE:
|
|
|
|
if (argc > 2)
|
|
|
|
doesyscmd(argv[2]);
|
2011-11-28 13:32:39 +00:00
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case INCLTYPE:
|
2014-07-27 22:54:13 +00:00
|
|
|
if (argc > 2) {
|
2011-12-19 08:50:17 +00:00
|
|
|
if (!doincl(argv[2])) {
|
2012-04-15 13:25:51 +00:00
|
|
|
if (mimic_gnu) {
|
2011-12-18 22:04:55 +00:00
|
|
|
warn("%s at line %lu: include(%s)",
|
|
|
|
CURRENT_NAME, CURRENT_LINE, argv[2]);
|
2012-04-15 13:25:51 +00:00
|
|
|
exit_code = 1;
|
|
|
|
} else
|
2011-12-18 22:04:55 +00:00
|
|
|
err(1, "%s at line %lu: include(%s)",
|
|
|
|
CURRENT_NAME, CURRENT_LINE, argv[2]);
|
2011-12-19 08:50:17 +00:00
|
|
|
}
|
2014-07-27 22:54:13 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SINCTYPE:
|
|
|
|
if (argc > 2)
|
|
|
|
(void) doincl(argv[2]);
|
|
|
|
break;
|
|
|
|
#ifdef EXTENDED
|
|
|
|
case PASTTYPE:
|
|
|
|
if (argc > 2)
|
|
|
|
if (!dopaste(argv[2]))
|
2011-11-28 13:32:39 +00:00
|
|
|
err(1, "%s at line %lu: paste(%s)",
|
2002-04-19 17:26:21 +00:00
|
|
|
CURRENT_NAME, CURRENT_LINE, argv[2]);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SPASTYPE:
|
|
|
|
if (argc > 2)
|
|
|
|
(void) dopaste(argv[2]);
|
|
|
|
break;
|
2011-11-28 13:32:39 +00:00
|
|
|
case FORMATTYPE:
|
|
|
|
doformat(argv, argc);
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif
|
|
|
|
case CHNQTYPE:
|
2011-11-28 13:32:39 +00:00
|
|
|
dochq(argv, ac);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CHNCTYPE:
|
2011-11-28 13:32:39 +00:00
|
|
|
dochc(argv, argc);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SUBSTYPE:
|
|
|
|
/*
|
|
|
|
* dosub - select substring
|
2002-07-15 02:15:12 +00:00
|
|
|
*
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
|
|
|
if (argc > 3)
|
|
|
|
dosub(argv, argc);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SHIFTYPE:
|
|
|
|
/*
|
|
|
|
* doshift - push back all arguments
|
|
|
|
* except the first one (i.e. skip
|
|
|
|
* argv[2])
|
|
|
|
*/
|
|
|
|
if (argc > 3) {
|
|
|
|
for (n = argc - 1; n > 3; n--) {
|
2002-04-19 17:26:21 +00:00
|
|
|
pbstr(rquote);
|
1994-05-27 12:33:43 +00:00
|
|
|
pbstr(argv[n]);
|
2002-04-19 17:26:21 +00:00
|
|
|
pbstr(lquote);
|
2011-11-28 13:32:39 +00:00
|
|
|
pushback(COMMA);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2002-04-19 17:26:21 +00:00
|
|
|
pbstr(rquote);
|
1994-05-27 12:33:43 +00:00
|
|
|
pbstr(argv[3]);
|
2002-04-19 17:26:21 +00:00
|
|
|
pbstr(lquote);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DIVRTYPE:
|
|
|
|
if (argc > 2 && (n = atoi(argv[2])) != 0)
|
|
|
|
dodiv(n);
|
|
|
|
else {
|
|
|
|
active = stdout;
|
|
|
|
oindex = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNDVTYPE:
|
|
|
|
doundiv(argv, argc);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DIVNTYPE:
|
|
|
|
/*
|
|
|
|
* dodivnum - return the number of
|
|
|
|
* current output diversion
|
|
|
|
*/
|
|
|
|
pbnum(oindex);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNDFTYPE:
|
|
|
|
/*
|
|
|
|
* doundefine - undefine a previously
|
|
|
|
* defined macro(s) or m4 keyword(s).
|
|
|
|
*/
|
|
|
|
if (argc > 2)
|
|
|
|
for (n = 2; n < argc; n++)
|
2011-11-28 13:32:39 +00:00
|
|
|
macro_undefine(argv[n]);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case POPDTYPE:
|
|
|
|
/*
|
|
|
|
* dopopdef - remove the topmost
|
|
|
|
* definitions of macro(s) or m4
|
|
|
|
* keyword(s).
|
|
|
|
*/
|
|
|
|
if (argc > 2)
|
|
|
|
for (n = 2; n < argc; n++)
|
2011-11-28 13:32:39 +00:00
|
|
|
macro_popdef(argv[n]);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MKTMTYPE:
|
|
|
|
/*
|
|
|
|
* dotemp - create a temporary file
|
|
|
|
*/
|
2002-04-19 17:26:21 +00:00
|
|
|
if (argc > 2) {
|
|
|
|
int fd;
|
|
|
|
char *temp;
|
|
|
|
|
|
|
|
temp = xstrdup(argv[2]);
|
2002-07-15 02:15:12 +00:00
|
|
|
|
2002-04-19 17:26:21 +00:00
|
|
|
fd = mkstemp(temp);
|
|
|
|
if (fd == -1)
|
2002-07-15 02:15:12 +00:00
|
|
|
err(1,
|
|
|
|
"%s at line %lu: couldn't make temp file %s",
|
2002-04-19 17:26:21 +00:00
|
|
|
CURRENT_NAME, CURRENT_LINE, argv[2]);
|
|
|
|
close(fd);
|
|
|
|
pbstr(temp);
|
|
|
|
free(temp);
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TRNLTYPE:
|
|
|
|
/*
|
|
|
|
* dotranslit - replace all characters in
|
|
|
|
* the source string that appears in the
|
|
|
|
* "from" string with the corresponding
|
|
|
|
* characters in the "to" string.
|
|
|
|
*/
|
|
|
|
if (argc > 3) {
|
2002-04-19 17:26:21 +00:00
|
|
|
char *temp;
|
|
|
|
|
2011-11-28 13:32:39 +00:00
|
|
|
temp = xalloc(strlen(argv[2])+1, NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (argc > 4)
|
|
|
|
map(temp, argv[2], argv[3], argv[4]);
|
|
|
|
else
|
|
|
|
map(temp, argv[2], argv[3], null);
|
|
|
|
pbstr(temp);
|
2002-04-19 17:26:21 +00:00
|
|
|
free(temp);
|
|
|
|
} else if (argc > 2)
|
1994-05-27 12:33:43 +00:00
|
|
|
pbstr(argv[2]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INDXTYPE:
|
|
|
|
/*
|
|
|
|
* doindex - find the index of the second
|
|
|
|
* argument string in the first argument
|
|
|
|
* string. -1 if not present.
|
|
|
|
*/
|
|
|
|
pbnum((argc > 3) ? indx(argv[2], argv[3]) : -1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ERRPTYPE:
|
|
|
|
/*
|
|
|
|
* doerrp - print the arguments to stderr
|
|
|
|
* file
|
|
|
|
*/
|
|
|
|
if (argc > 2) {
|
|
|
|
for (n = 2; n < argc; n++)
|
|
|
|
fprintf(stderr, "%s ", argv[n]);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DNLNTYPE:
|
|
|
|
/*
|
|
|
|
* dodnl - eat-up-to and including
|
|
|
|
* newline
|
|
|
|
*/
|
|
|
|
while ((c = gpbc()) != '\n' && c != EOF)
|
|
|
|
;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case M4WRTYPE:
|
|
|
|
/*
|
|
|
|
* dom4wrap - set up for
|
|
|
|
* wrap-up/wind-down activity
|
|
|
|
*/
|
2011-11-28 13:32:39 +00:00
|
|
|
if (argc > 2)
|
|
|
|
dom4wrap(argv[2]);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EXITTYPE:
|
|
|
|
/*
|
|
|
|
* doexit - immediate exit from m4.
|
|
|
|
*/
|
1995-04-16 23:31:35 +00:00
|
|
|
killdiv();
|
1994-05-27 12:33:43 +00:00
|
|
|
exit((argc > 2) ? atoi(argv[2]) : 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DEFNTYPE:
|
|
|
|
if (argc > 2)
|
|
|
|
for (n = 2; n < argc; n++)
|
|
|
|
dodefn(argv[n]);
|
|
|
|
break;
|
|
|
|
|
2002-04-19 17:26:21 +00:00
|
|
|
case INDIRTYPE: /* Indirect call */
|
|
|
|
if (argc > 2)
|
|
|
|
doindir(argv, argc);
|
|
|
|
break;
|
2002-07-15 02:15:12 +00:00
|
|
|
|
2002-04-19 17:26:21 +00:00
|
|
|
case BUILTINTYPE: /* Builtins only */
|
|
|
|
if (argc > 2)
|
|
|
|
dobuiltin(argv, argc);
|
1998-09-07 22:09:13 +00:00
|
|
|
break;
|
|
|
|
|
2002-04-19 17:26:21 +00:00
|
|
|
case PATSTYPE:
|
|
|
|
if (argc > 2)
|
|
|
|
dopatsubst(argv, argc);
|
|
|
|
break;
|
|
|
|
case REGEXPTYPE:
|
|
|
|
if (argc > 2)
|
|
|
|
doregexp(argv, argc);
|
|
|
|
break;
|
|
|
|
case LINETYPE:
|
|
|
|
doprintlineno(infile+ilevel);
|
|
|
|
break;
|
|
|
|
case FILENAMETYPE:
|
|
|
|
doprintfilename(infile+ilevel);
|
|
|
|
break;
|
|
|
|
case SELFTYPE:
|
|
|
|
pbstr(rquote);
|
|
|
|
pbstr(argv[1]);
|
|
|
|
pbstr(lquote);
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
default:
|
2011-11-28 13:32:39 +00:00
|
|
|
m4errx(1, "eval: major botch.");
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-04-19 17:26:21 +00:00
|
|
|
* expand_macro - user-defined macro expansion
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
|
|
|
void
|
2002-05-01 21:37:29 +00:00
|
|
|
expand_macro(const char *argv[], int argc)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-04-19 17:26:21 +00:00
|
|
|
const char *t;
|
|
|
|
const char *p;
|
|
|
|
int n;
|
|
|
|
int argno;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
t = argv[0]; /* defn string as a whole */
|
|
|
|
p = t;
|
|
|
|
while (*p)
|
|
|
|
p++;
|
|
|
|
p--; /* last character of defn */
|
|
|
|
while (p > t) {
|
|
|
|
if (*(p - 1) != ARGFLAG)
|
2011-11-28 13:32:39 +00:00
|
|
|
PUSHBACK(*p);
|
1994-05-27 12:33:43 +00:00
|
|
|
else {
|
|
|
|
switch (*p) {
|
|
|
|
|
|
|
|
case '#':
|
|
|
|
pbnum(argc - 2);
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
if ((argno = *p - '0') < argc - 1)
|
|
|
|
pbstr(argv[argno + 1]);
|
|
|
|
break;
|
|
|
|
case '*':
|
2002-04-19 17:26:21 +00:00
|
|
|
if (argc > 2) {
|
|
|
|
for (n = argc - 1; n > 2; n--) {
|
|
|
|
pbstr(argv[n]);
|
2011-11-28 13:32:39 +00:00
|
|
|
pushback(COMMA);
|
2002-04-19 17:26:21 +00:00
|
|
|
}
|
|
|
|
pbstr(argv[2]);
|
2011-11-28 13:32:39 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
2002-04-19 17:26:21 +00:00
|
|
|
case '@':
|
|
|
|
if (argc > 2) {
|
|
|
|
for (n = argc - 1; n > 2; n--) {
|
|
|
|
pbstr(rquote);
|
|
|
|
pbstr(argv[n]);
|
|
|
|
pbstr(lquote);
|
2011-11-28 13:32:39 +00:00
|
|
|
pushback(COMMA);
|
2002-04-19 17:26:21 +00:00
|
|
|
}
|
|
|
|
pbstr(rquote);
|
|
|
|
pbstr(argv[2]);
|
|
|
|
pbstr(lquote);
|
1997-04-13 17:51:32 +00:00
|
|
|
}
|
2002-04-19 17:26:21 +00:00
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
default:
|
2011-11-28 13:32:39 +00:00
|
|
|
PUSHBACK(*p);
|
|
|
|
PUSHBACK('$');
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
p--;
|
|
|
|
}
|
|
|
|
p--;
|
|
|
|
}
|
|
|
|
if (p == t) /* do last character */
|
2011-11-28 13:32:39 +00:00
|
|
|
PUSHBACK(*p);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2011-11-28 13:32:39 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* dodefine - install definition in the table
|
|
|
|
*/
|
|
|
|
void
|
2002-05-01 21:37:29 +00:00
|
|
|
dodefine(const char *name, const char *defn)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2011-11-28 13:32:39 +00:00
|
|
|
if (!*name && !mimic_gnu)
|
|
|
|
m4errx(1, "null definition.");
|
1994-05-27 12:33:43 +00:00
|
|
|
else
|
2011-11-28 13:32:39 +00:00
|
|
|
macro_define(name, defn);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dodefn - push back a quoted definition of
|
|
|
|
* the given name.
|
|
|
|
*/
|
2002-04-19 17:26:21 +00:00
|
|
|
static void
|
2002-05-01 21:37:29 +00:00
|
|
|
dodefn(const char *name)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2011-11-28 13:32:39 +00:00
|
|
|
struct macro_definition *p;
|
2002-04-19 17:26:21 +00:00
|
|
|
|
2011-11-28 13:32:39 +00:00
|
|
|
if ((p = lookup_macro_definition(name)) != NULL) {
|
|
|
|
if ((p->type & TYPEMASK) == MACRTYPE) {
|
2002-04-19 17:26:21 +00:00
|
|
|
pbstr(rquote);
|
|
|
|
pbstr(p->defn);
|
|
|
|
pbstr(lquote);
|
2011-11-28 13:32:39 +00:00
|
|
|
} else {
|
|
|
|
pbstr(p->defn);
|
2002-04-19 17:26:21 +00:00
|
|
|
pbstr(BUILTIN_MARKER);
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dopushdef - install a definition in the hash table
|
|
|
|
* without removing a previous definition. Since
|
|
|
|
* each new entry is entered in *front* of the
|
|
|
|
* hash bucket, it hides a previous definition from
|
|
|
|
* lookup.
|
|
|
|
*/
|
2002-04-19 17:26:21 +00:00
|
|
|
static void
|
2002-05-01 21:37:29 +00:00
|
|
|
dopushdef(const char *name, const char *defn)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2011-11-28 13:32:39 +00:00
|
|
|
if (!*name && !mimic_gnu)
|
|
|
|
m4errx(1, "null definition.");
|
1994-05-27 12:33:43 +00:00
|
|
|
else
|
2011-11-28 13:32:39 +00:00
|
|
|
macro_pushdef(name, defn);
|
2002-04-19 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dump_one_def - dump the specified definition.
|
|
|
|
*/
|
|
|
|
static void
|
2011-11-28 13:32:39 +00:00
|
|
|
dump_one_def(const char *name, struct macro_definition *p)
|
2002-04-19 17:26:21 +00:00
|
|
|
{
|
2011-11-28 13:32:39 +00:00
|
|
|
if (!traceout)
|
|
|
|
traceout = stderr;
|
2002-04-19 17:26:21 +00:00
|
|
|
if (mimic_gnu) {
|
|
|
|
if ((p->type & TYPEMASK) == MACRTYPE)
|
2011-11-28 13:32:39 +00:00
|
|
|
fprintf(traceout, "%s:\t%s\n", name, p->defn);
|
2002-04-19 17:26:21 +00:00
|
|
|
else {
|
2011-11-28 13:32:39 +00:00
|
|
|
fprintf(traceout, "%s:\t<%s>\n", name, p->defn);
|
|
|
|
}
|
2002-04-19 17:26:21 +00:00
|
|
|
} else
|
2011-11-28 13:32:39 +00:00
|
|
|
fprintf(traceout, "`%s'\t`%s'\n", name, p->defn);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dodumpdef - dump the specified definitions in the hash
|
|
|
|
* table to stderr. If nothing is specified, the entire
|
|
|
|
* hash table is dumped.
|
|
|
|
*/
|
2002-04-19 17:26:21 +00:00
|
|
|
static void
|
2002-05-01 21:37:29 +00:00
|
|
|
dodump(const char *argv[], int argc)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-04-19 17:26:21 +00:00
|
|
|
int n;
|
2011-11-28 13:32:39 +00:00
|
|
|
struct macro_definition *p;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (argc > 2) {
|
|
|
|
for (n = 2; n < argc; n++)
|
2011-11-28 13:32:39 +00:00
|
|
|
if ((p = lookup_macro_definition(argv[n])) != NULL)
|
|
|
|
dump_one_def(argv[n], p);
|
|
|
|
} else
|
|
|
|
macro_for_all(dump_one_def);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2002-04-19 17:26:21 +00:00
|
|
|
/*
|
|
|
|
* dotrace - mark some macros as traced/untraced depending upon on.
|
|
|
|
*/
|
|
|
|
static void
|
2002-05-01 21:37:29 +00:00
|
|
|
dotrace(const char *argv[], int argc, int on)
|
2002-04-19 17:26:21 +00:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (argc > 2) {
|
|
|
|
for (n = 2; n < argc; n++)
|
|
|
|
mark_traced(argv[n], on);
|
|
|
|
} else
|
|
|
|
mark_traced(NULL, on);
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* doifelse - select one of two alternatives - loop.
|
|
|
|
*/
|
2002-04-19 17:26:21 +00:00
|
|
|
static void
|
2002-05-01 21:37:29 +00:00
|
|
|
doifelse(const char *argv[], int argc)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
cycle {
|
|
|
|
if (STREQ(argv[2], argv[3]))
|
|
|
|
pbstr(argv[4]);
|
|
|
|
else if (argc == 6)
|
|
|
|
pbstr(argv[5]);
|
|
|
|
else if (argc > 6) {
|
|
|
|
argv += 3;
|
|
|
|
argc -= 3;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* doinclude - include a given file.
|
|
|
|
*/
|
2002-04-19 17:26:21 +00:00
|
|
|
static int
|
2002-05-01 21:37:29 +00:00
|
|
|
doincl(const char *ifile)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
if (ilevel + 1 == MAXINP)
|
2011-11-28 13:32:39 +00:00
|
|
|
m4errx(1, "too many include files.");
|
2002-04-19 17:26:21 +00:00
|
|
|
if (fopen_trypath(infile+ilevel+1, ifile) != NULL) {
|
1994-05-27 12:33:43 +00:00
|
|
|
ilevel++;
|
|
|
|
bbase[ilevel] = bufbase = bp;
|
|
|
|
return (1);
|
2002-04-19 17:26:21 +00:00
|
|
|
} else
|
1994-05-27 12:33:43 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef EXTENDED
|
|
|
|
/*
|
|
|
|
* dopaste - include a given file without any
|
|
|
|
* macro processing.
|
|
|
|
*/
|
2002-04-19 17:26:21 +00:00
|
|
|
static int
|
2002-05-01 21:37:29 +00:00
|
|
|
dopaste(const char *pfile)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
FILE *pf;
|
2002-04-19 17:26:21 +00:00
|
|
|
int c;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if ((pf = fopen(pfile, "r")) != NULL) {
|
2011-11-28 13:32:39 +00:00
|
|
|
if (synch_lines)
|
|
|
|
fprintf(active, "#line 1 \"%s\"\n", pfile);
|
1994-05-27 12:33:43 +00:00
|
|
|
while ((c = getc(pf)) != EOF)
|
|
|
|
putc(c, active);
|
|
|
|
(void) fclose(pf);
|
2011-11-28 13:32:39 +00:00
|
|
|
emit_synchline();
|
1994-05-27 12:33:43 +00:00
|
|
|
return (1);
|
2002-04-19 17:26:21 +00:00
|
|
|
} else
|
1994-05-27 12:33:43 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-11-28 13:32:39 +00:00
|
|
|
/*
|
|
|
|
* dochq - change quote characters
|
|
|
|
*/
|
2002-04-19 17:26:21 +00:00
|
|
|
static void
|
2011-11-28 13:32:39 +00:00
|
|
|
dochq(const char *argv[], int ac)
|
2002-04-19 17:26:21 +00:00
|
|
|
{
|
|
|
|
if (ac == 2) {
|
2011-11-28 13:32:39 +00:00
|
|
|
lquote[0] = LQUOTE; lquote[1] = EOS;
|
|
|
|
rquote[0] = RQUOTE; rquote[1] = EOS;
|
2002-04-19 17:26:21 +00:00
|
|
|
} else {
|
|
|
|
strlcpy(lquote, argv[2], sizeof(lquote));
|
2011-11-28 13:32:39 +00:00
|
|
|
if (ac > 3) {
|
2002-04-19 17:26:21 +00:00
|
|
|
strlcpy(rquote, argv[3], sizeof(rquote));
|
2011-11-28 13:32:39 +00:00
|
|
|
} else {
|
|
|
|
rquote[0] = ECOMMT; rquote[1] = EOS;
|
|
|
|
}
|
2002-04-19 17:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
2011-11-28 13:32:39 +00:00
|
|
|
* dochc - change comment characters
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
2002-04-19 17:26:21 +00:00
|
|
|
static void
|
2011-11-28 13:32:39 +00:00
|
|
|
dochc(const char *argv[], int argc)
|
2002-04-19 17:26:21 +00:00
|
|
|
{
|
2011-11-28 13:32:39 +00:00
|
|
|
/* XXX Note that there is no difference between no argument and a single
|
|
|
|
* empty argument.
|
|
|
|
*/
|
|
|
|
if (argc == 2) {
|
2002-04-19 17:26:21 +00:00
|
|
|
scommt[0] = EOS;
|
|
|
|
ecommt[0] = EOS;
|
|
|
|
} else {
|
2011-11-28 13:32:39 +00:00
|
|
|
strlcpy(scommt, argv[2], sizeof(scommt));
|
|
|
|
if (argc == 3) {
|
|
|
|
ecommt[0] = ECOMMT; ecommt[1] = EOS;
|
|
|
|
} else {
|
2002-04-19 17:26:21 +00:00
|
|
|
strlcpy(ecommt, argv[3], sizeof(ecommt));
|
2011-11-28 13:32:39 +00:00
|
|
|
}
|
2002-04-19 17:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-28 13:32:39 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
2011-11-28 13:32:39 +00:00
|
|
|
* dom4wrap - expand text at EOF
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
2002-04-19 17:26:21 +00:00
|
|
|
static void
|
2011-11-28 13:32:39 +00:00
|
|
|
dom4wrap(const char *text)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2011-11-28 13:32:39 +00:00
|
|
|
if (wrapindex >= maxwraps) {
|
|
|
|
if (maxwraps == 0)
|
|
|
|
maxwraps = 16;
|
1994-05-27 12:33:43 +00:00
|
|
|
else
|
2011-11-28 13:32:39 +00:00
|
|
|
maxwraps *= 2;
|
2014-07-27 22:54:13 +00:00
|
|
|
m4wraps = xreallocarray(m4wraps, maxwraps, sizeof(*m4wraps),
|
2011-11-28 13:32:39 +00:00
|
|
|
"too many m4wraps");
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2011-11-28 13:32:39 +00:00
|
|
|
m4wraps[wrapindex++] = xstrdup(text);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dodivert - divert the output to a temporary file
|
|
|
|
*/
|
2002-04-19 17:26:21 +00:00
|
|
|
static void
|
2002-05-01 21:37:29 +00:00
|
|
|
dodiv(int n)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-04-19 17:26:21 +00:00
|
|
|
int fd;
|
|
|
|
|
1997-08-18 21:01:45 +00:00
|
|
|
oindex = n;
|
2002-04-19 17:26:21 +00:00
|
|
|
if (n >= maxout) {
|
|
|
|
if (mimic_gnu)
|
|
|
|
resizedivs(n + 10);
|
|
|
|
else
|
|
|
|
n = 0; /* bitbucket */
|
2011-11-28 13:32:39 +00:00
|
|
|
}
|
2002-04-19 17:26:21 +00:00
|
|
|
|
|
|
|
if (n < 0)
|
1994-05-27 12:33:43 +00:00
|
|
|
n = 0; /* bitbucket */
|
|
|
|
if (outfile[n] == NULL) {
|
2002-04-19 17:26:21 +00:00
|
|
|
char fname[] = _PATH_DIVNAME;
|
|
|
|
|
2014-07-27 22:54:13 +00:00
|
|
|
if ((fd = mkstemp(fname)) < 0 ||
|
|
|
|
unlink(fname) == -1 ||
|
|
|
|
(outfile[n] = fdopen(fd, "w+")) == NULL)
|
|
|
|
err(1, "%s: cannot divert", fname);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
active = outfile[n];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* doundivert - undivert a specified output, or all
|
|
|
|
* other outputs, in numerical order.
|
|
|
|
*/
|
2002-04-19 17:26:21 +00:00
|
|
|
static void
|
2002-05-01 21:37:29 +00:00
|
|
|
doundiv(const char *argv[], int argc)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-04-19 17:26:21 +00:00
|
|
|
int ind;
|
|
|
|
int n;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (argc > 2) {
|
|
|
|
for (ind = 2; ind < argc; ind++) {
|
2011-11-28 13:32:39 +00:00
|
|
|
const char *errstr;
|
|
|
|
n = strtonum(argv[ind], 1, INT_MAX, &errstr);
|
|
|
|
if (errstr) {
|
|
|
|
if (errno == EINVAL && mimic_gnu)
|
|
|
|
getdivfile(argv[ind]);
|
|
|
|
} else {
|
|
|
|
if (n < maxout && outfile[n] != NULL)
|
|
|
|
getdiv(n);
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2002-04-19 17:26:21 +00:00
|
|
|
for (n = 1; n < maxout; n++)
|
1994-05-27 12:33:43 +00:00
|
|
|
if (outfile[n] != NULL)
|
|
|
|
getdiv(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dosub - select substring
|
|
|
|
*/
|
2002-04-19 17:26:21 +00:00
|
|
|
static void
|
2002-05-01 21:37:29 +00:00
|
|
|
dosub(const char *argv[], int argc)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-04-19 17:26:21 +00:00
|
|
|
const char *ap, *fc, *k;
|
|
|
|
int nc;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2001-05-18 18:35:34 +00:00
|
|
|
ap = argv[2]; /* target string */
|
|
|
|
#ifdef EXPR
|
|
|
|
fc = ap + expr(argv[3]); /* first char */
|
|
|
|
#else
|
|
|
|
fc = ap + atoi(argv[3]); /* first char */
|
|
|
|
#endif
|
2002-04-19 17:26:21 +00:00
|
|
|
nc = strlen(fc);
|
|
|
|
if (argc >= 5)
|
1994-05-27 12:33:43 +00:00
|
|
|
#ifdef EXPR
|
2002-04-19 17:26:21 +00:00
|
|
|
nc = min(nc, expr(argv[4]));
|
1994-05-27 12:33:43 +00:00
|
|
|
#else
|
2002-04-19 17:26:21 +00:00
|
|
|
nc = min(nc, atoi(argv[4]));
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif
|
|
|
|
if (fc >= ap && fc < ap + strlen(ap))
|
2001-05-18 18:35:34 +00:00
|
|
|
for (k = fc + nc - 1; k >= fc; k--)
|
2011-11-28 13:32:39 +00:00
|
|
|
pushback(*k);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* map:
|
|
|
|
* map every character of s1 that is specified in from
|
|
|
|
* into s3 and replace in s. (source s1 remains untouched)
|
|
|
|
*
|
2011-11-28 13:32:39 +00:00
|
|
|
* This is derived from the a standard implementation of map(s,from,to)
|
|
|
|
* function of ICON language. Within mapvec, we replace every character
|
|
|
|
* of "from" with the corresponding character in "to".
|
|
|
|
* If "to" is shorter than "from", than the corresponding entries are null,
|
|
|
|
* which means that those characters dissapear altogether.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
2002-04-19 17:26:21 +00:00
|
|
|
static void
|
2002-05-01 21:37:29 +00:00
|
|
|
map(char *dest, const char *src, const char *from, const char *to)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-04-19 17:26:21 +00:00
|
|
|
const char *tmp;
|
|
|
|
unsigned char sch, dch;
|
|
|
|
static char frombis[257];
|
|
|
|
static char tobis[257];
|
2011-11-28 13:32:39 +00:00
|
|
|
int i;
|
|
|
|
char seen[256];
|
2002-04-19 17:26:21 +00:00
|
|
|
static unsigned char mapvec[256] = {
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
|
|
|
|
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
|
|
|
|
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
|
|
|
|
53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
|
|
|
|
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
|
|
|
|
87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102,
|
|
|
|
103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
|
|
|
|
116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128,
|
|
|
|
129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141,
|
|
|
|
142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154,
|
|
|
|
155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167,
|
|
|
|
168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180,
|
|
|
|
181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193,
|
|
|
|
194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206,
|
|
|
|
207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
|
|
|
|
220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232,
|
|
|
|
233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245,
|
|
|
|
246, 247, 248, 249, 250, 251, 252, 253, 254, 255
|
1994-05-27 12:33:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (*src) {
|
2002-04-19 17:26:21 +00:00
|
|
|
if (mimic_gnu) {
|
|
|
|
/*
|
|
|
|
* expand character ranges on the fly
|
|
|
|
*/
|
|
|
|
from = handledash(frombis, frombis + 256, from);
|
|
|
|
to = handledash(tobis, tobis + 256, to);
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
tmp = from;
|
|
|
|
/*
|
|
|
|
* create a mapping between "from" and
|
|
|
|
* "to"
|
|
|
|
*/
|
2011-11-28 13:32:39 +00:00
|
|
|
for (i = 0; i < 256; i++)
|
|
|
|
seen[i] = 0;
|
|
|
|
while (*from) {
|
|
|
|
if (!seen[(unsigned char)(*from)]) {
|
|
|
|
mapvec[(unsigned char)(*from)] = (unsigned char)(*to);
|
|
|
|
seen[(unsigned char)(*from)] = 1;
|
|
|
|
}
|
|
|
|
from++;
|
|
|
|
if (*to)
|
|
|
|
to++;
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
while (*src) {
|
2002-04-19 17:26:21 +00:00
|
|
|
sch = (unsigned char)(*src++);
|
1994-05-27 12:33:43 +00:00
|
|
|
dch = mapvec[sch];
|
2002-04-19 17:26:21 +00:00
|
|
|
if ((*dest = (char)dch))
|
1994-05-27 12:33:43 +00:00
|
|
|
dest++;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* restore all the changed characters
|
|
|
|
*/
|
|
|
|
while (*tmp) {
|
2002-04-19 17:26:21 +00:00
|
|
|
mapvec[(unsigned char)(*tmp)] = (unsigned char)(*tmp);
|
1994-05-27 12:33:43 +00:00
|
|
|
tmp++;
|
|
|
|
}
|
|
|
|
}
|
2002-04-19 17:26:21 +00:00
|
|
|
*dest = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* handledash:
|
|
|
|
* use buffer to copy the src string, expanding character ranges
|
|
|
|
* on the way.
|
|
|
|
*/
|
|
|
|
static const char *
|
2002-05-01 21:37:29 +00:00
|
|
|
handledash(char *buffer, char *end, const char *src)
|
2002-04-19 17:26:21 +00:00
|
|
|
{
|
|
|
|
char *p;
|
2002-07-15 02:15:12 +00:00
|
|
|
|
2002-04-19 17:26:21 +00:00
|
|
|
p = buffer;
|
|
|
|
while(*src) {
|
|
|
|
if (src[1] == '-' && src[2]) {
|
|
|
|
unsigned char i;
|
2011-11-28 13:32:39 +00:00
|
|
|
if ((unsigned char)src[0] <= (unsigned char)src[2]) {
|
|
|
|
for (i = (unsigned char)src[0];
|
|
|
|
i <= (unsigned char)src[2]; i++) {
|
|
|
|
*p++ = i;
|
|
|
|
if (p == end) {
|
|
|
|
*p = '\0';
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = (unsigned char)src[0];
|
|
|
|
i >= (unsigned char)src[2]; i--) {
|
|
|
|
*p++ = i;
|
|
|
|
if (p == end) {
|
|
|
|
*p = '\0';
|
|
|
|
return buffer;
|
|
|
|
}
|
2002-04-19 17:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
src += 3;
|
|
|
|
} else
|
|
|
|
*p++ = *src++;
|
|
|
|
if (p == end)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
return buffer;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|