Home | History | Annotate | Line # | Download | only in m4
eval.c revision 1.21
      1  1.20  christos /*	$OpenBSD: eval.c,v 1.66 2008/08/21 21:01:47 espie Exp $	*/
      2  1.21  christos /*	$NetBSD: eval.c,v 1.21 2011/03/05 16:38:25 christos Exp $	*/
      3   1.4       tls 
      4   1.1       cgd /*
      5   1.2     glass  * Copyright (c) 1989, 1993
      6   1.2     glass  *	The Regents of the University of California.  All rights reserved.
      7   1.1       cgd  *
      8   1.1       cgd  * This code is derived from software contributed to Berkeley by
      9   1.2     glass  * Ozan Yigit at York University.
     10   1.1       cgd  *
     11   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     12   1.1       cgd  * modification, are permitted provided that the following conditions
     13   1.1       cgd  * are met:
     14   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     15   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     16   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     18   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     19  1.17       agc  * 3. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36   1.1       cgd /*
     37   1.1       cgd  * eval.c
     38   1.1       cgd  * Facility: m4 macro processor
     39   1.1       cgd  * by: oz
     40   1.1       cgd  */
     41  1.20  christos #if HAVE_NBTOOL_CONFIG_H
     42  1.20  christos #include "nbtool_config.h"
     43  1.20  christos #endif
     44  1.20  christos #include <sys/cdefs.h>
     45  1.21  christos __RCSID("$NetBSD: eval.c,v 1.21 2011/03/05 16:38:25 christos Exp $");
     46   1.1       cgd 
     47   1.2     glass #include <sys/types.h>
     48  1.20  christos #include <err.h>
     49   1.2     glass #include <errno.h>
     50  1.20  christos #include <limits.h>
     51  1.20  christos #include <unistd.h>
     52   1.1       cgd #include <stdio.h>
     53   1.1       cgd #include <stdlib.h>
     54  1.14        tv #include <stddef.h>
     55   1.1       cgd #include <string.h>
     56  1.20  christos #include <fcntl.h>
     57   1.1       cgd #include "mdef.h"
     58   1.2     glass #include "stdd.h"
     59   1.2     glass #include "extern.h"
     60   1.2     glass #include "pathnames.h"
     61   1.1       cgd 
     62  1.20  christos static void	dodefn(const char *);
     63  1.20  christos static void	dopushdef(const char *, const char *);
     64  1.20  christos static void	dodump(const char *[], int);
     65  1.20  christos static void	dotrace(const char *[], int, int);
     66  1.20  christos static void	doifelse(const char *[], int);
     67  1.20  christos static int	doincl(const char *);
     68  1.20  christos static int	dopaste(const char *);
     69  1.20  christos static void	dochq(const char *[], int);
     70  1.20  christos static void	dochc(const char *[], int);
     71  1.20  christos static void	dom4wrap(const char *);
     72  1.20  christos static void	dodiv(int);
     73  1.20  christos static void	doundiv(const char *[], int);
     74  1.20  christos static void	dosub(const char *[], int);
     75  1.20  christos static void	map(char *, const char *, const char *, const char *);
     76  1.20  christos static const char *handledash(char *, char *, const char *);
     77  1.20  christos static void	expand_builtin(const char *[], int, int);
     78  1.20  christos static void	expand_macro(const char *[], int);
     79  1.20  christos static void	dump_one_def(const char *, struct macro_definition *);
     80  1.14        tv 
     81  1.14        tv unsigned long	expansion_id;
     82  1.14        tv 
     83   1.1       cgd /*
     84  1.14        tv  * eval - eval all macros and builtins calls
     85   1.1       cgd  *	  argc - number of elements in argv.
     86   1.1       cgd  *	  argv - element vector :
     87   1.1       cgd  *			argv[0] = definition of a user
     88  1.20  christos  *				  macro or NULL if built-in.
     89   1.1       cgd  *			argv[1] = name of the macro or
     90   1.1       cgd  *				  built-in.
     91   1.1       cgd  *			argv[2] = parameters to user-defined
     92   1.1       cgd  *			   .	  macro or built-in.
     93   1.1       cgd  *			   .
     94   1.1       cgd  *
     95  1.14        tv  * A call in the form of macro-or-builtin() will result in:
     96   1.1       cgd  *			argv[0] = nullstr
     97   1.1       cgd  *			argv[1] = macro-or-builtin
     98   1.1       cgd  *			argv[2] = nullstr
     99  1.14        tv  *
    100  1.14        tv  * argc is 3 for macro-or-builtin() and 2 for macro-or-builtin
    101   1.1       cgd  */
    102  1.14        tv void
    103  1.20  christos eval(const char *argv[], int argc, int td, int is_traced)
    104  1.14        tv {
    105  1.20  christos 	size_t mark = SIZE_MAX;
    106   1.1       cgd 
    107  1.14        tv 	expansion_id++;
    108  1.14        tv 	if (td & RECDEF)
    109  1.20  christos 		m4errx(1, "expanding recursive definition for %s.", argv[1]);
    110  1.20  christos 	if (is_traced)
    111  1.14        tv 		mark = trace(argv, argc, infile+ilevel);
    112  1.14        tv 	if (td == MACRTYPE)
    113  1.14        tv 		expand_macro(argv, argc);
    114  1.14        tv 	else
    115  1.14        tv 		expand_builtin(argv, argc, td);
    116  1.20  christos     	if (mark != SIZE_MAX)
    117  1.14        tv 		finish_trace(mark);
    118  1.14        tv }
    119  1.14        tv 
    120  1.14        tv /*
    121  1.14        tv  * expand_builtin - evaluate built-in macros.
    122  1.14        tv  */
    123   1.2     glass void
    124  1.20  christos expand_builtin(const char *argv[], int argc, int td)
    125   1.1       cgd {
    126  1.10     lukem 	int c, n;
    127  1.14        tv 	int ac;
    128   1.2     glass 	static int sysval = 0;
    129   1.1       cgd 
    130   1.1       cgd #ifdef DEBUG
    131   1.1       cgd 	printf("argc = %d\n", argc);
    132   1.1       cgd 	for (n = 0; n < argc; n++)
    133   1.1       cgd 		printf("argv[%d] = %s\n", n, argv[n]);
    134  1.20  christos 	fflush(stdout);
    135   1.1       cgd #endif
    136  1.14        tv 
    137   1.2     glass  /*
    138   1.2     glass   * if argc == 3 and argv[2] is null, then we
    139   1.2     glass   * have macro-or-builtin() type call. We adjust
    140   1.2     glass   * argc to avoid further checking..
    141   1.2     glass   */
    142  1.20  christos  /* we keep the initial value for those built-ins that differentiate
    143  1.20  christos   * between builtin() and builtin.
    144  1.20  christos   */
    145  1.14        tv   	ac = argc;
    146  1.14        tv 
    147  1.20  christos 	if (argc == 3 && !*(argv[2]) && !mimic_gnu)
    148   1.1       cgd 		argc--;
    149   1.1       cgd 
    150  1.14        tv 	switch (td & TYPEMASK) {
    151   1.1       cgd 
    152   1.1       cgd 	case DEFITYPE:
    153   1.1       cgd 		if (argc > 2)
    154   1.1       cgd 			dodefine(argv[2], (argc > 3) ? argv[3] : null);
    155   1.1       cgd 		break;
    156   1.1       cgd 
    157   1.1       cgd 	case PUSDTYPE:
    158   1.1       cgd 		if (argc > 2)
    159   1.1       cgd 			dopushdef(argv[2], (argc > 3) ? argv[3] : null);
    160   1.1       cgd 		break;
    161   1.1       cgd 
    162   1.1       cgd 	case DUMPTYPE:
    163   1.1       cgd 		dodump(argv, argc);
    164   1.1       cgd 		break;
    165   1.1       cgd 
    166  1.14        tv 	case TRACEONTYPE:
    167  1.14        tv 		dotrace(argv, argc, 1);
    168  1.14        tv 		break;
    169  1.14        tv 
    170  1.14        tv 	case TRACEOFFTYPE:
    171  1.14        tv 		dotrace(argv, argc, 0);
    172  1.14        tv 		break;
    173  1.14        tv 
    174   1.1       cgd 	case EXPRTYPE:
    175   1.2     glass 	/*
    176   1.2     glass 	 * doexpr - evaluate arithmetic
    177   1.2     glass 	 * expression
    178   1.2     glass 	 */
    179  1.20  christos 	{
    180  1.20  christos 		int base = 10;
    181  1.20  christos 		int maxdigits = 0;
    182  1.20  christos 		const char *errstr;
    183  1.20  christos 
    184  1.20  christos 		if (argc > 3) {
    185  1.20  christos 			base = strtonum(argv[3], 2, 36, &errstr);
    186  1.20  christos 			if (errstr) {
    187  1.20  christos 				m4errx(1, "expr: base %s invalid.", argv[3]);
    188  1.20  christos 			}
    189  1.20  christos 		}
    190  1.20  christos 		if (argc > 4) {
    191  1.20  christos 			maxdigits = strtonum(argv[4], 0, INT_MAX, &errstr);
    192  1.20  christos 			if (errstr) {
    193  1.20  christos 				m4errx(1, "expr: maxdigits %s invalid.", argv[4]);
    194  1.20  christos 			}
    195  1.20  christos 		}
    196   1.1       cgd 		if (argc > 2)
    197  1.20  christos 			pbnumbase(expr(argv[2]), base, maxdigits);
    198   1.1       cgd 		break;
    199  1.20  christos 	}
    200   1.1       cgd 
    201   1.1       cgd 	case IFELTYPE:
    202   1.1       cgd 		if (argc > 4)
    203   1.1       cgd 			doifelse(argv, argc);
    204   1.1       cgd 		break;
    205   1.1       cgd 
    206   1.1       cgd 	case IFDFTYPE:
    207   1.2     glass 	/*
    208   1.2     glass 	 * doifdef - select one of two
    209   1.2     glass 	 * alternatives based on the existence of
    210   1.2     glass 	 * another definition
    211   1.2     glass 	 */
    212   1.1       cgd 		if (argc > 3) {
    213  1.20  christos 			if (lookup_macro_definition(argv[2]) != NULL)
    214   1.1       cgd 				pbstr(argv[3]);
    215   1.1       cgd 			else if (argc > 4)
    216   1.1       cgd 				pbstr(argv[4]);
    217   1.1       cgd 		}
    218   1.1       cgd 		break;
    219   1.1       cgd 
    220   1.1       cgd 	case LENGTYPE:
    221   1.2     glass 	/*
    222   1.2     glass 	 * dolen - find the length of the
    223   1.2     glass 	 * argument
    224   1.2     glass 	 */
    225  1.14        tv 		pbnum((argc > 2) ? strlen(argv[2]) : 0);
    226   1.1       cgd 		break;
    227   1.1       cgd 
    228   1.1       cgd 	case INCRTYPE:
    229   1.2     glass 	/*
    230   1.2     glass 	 * doincr - increment the value of the
    231   1.2     glass 	 * argument
    232   1.2     glass 	 */
    233   1.1       cgd 		if (argc > 2)
    234   1.1       cgd 			pbnum(atoi(argv[2]) + 1);
    235   1.1       cgd 		break;
    236   1.1       cgd 
    237   1.1       cgd 	case DECRTYPE:
    238   1.2     glass 	/*
    239   1.2     glass 	 * dodecr - decrement the value of the
    240   1.2     glass 	 * argument
    241   1.2     glass 	 */
    242   1.1       cgd 		if (argc > 2)
    243   1.1       cgd 			pbnum(atoi(argv[2]) - 1);
    244   1.1       cgd 		break;
    245   1.1       cgd 
    246   1.1       cgd 	case SYSCTYPE:
    247   1.2     glass 	/*
    248   1.2     glass 	 * dosys - execute system command
    249   1.2     glass 	 */
    250  1.20  christos 		if (argc > 2) {
    251  1.20  christos 			fflush(stdout);
    252   1.1       cgd 			sysval = system(argv[2]);
    253  1.20  christos 		}
    254   1.1       cgd 		break;
    255   1.1       cgd 
    256   1.1       cgd 	case SYSVTYPE:
    257   1.2     glass 	/*
    258   1.2     glass 	 * dosysval - return value of the last
    259   1.2     glass 	 * system call.
    260   1.2     glass 	 *
    261   1.2     glass 	 */
    262   1.1       cgd 		pbnum(sysval);
    263   1.1       cgd 		break;
    264   1.1       cgd 
    265  1.14        tv 	case ESYSCMDTYPE:
    266  1.14        tv 		if (argc > 2)
    267  1.14        tv 			doesyscmd(argv[2]);
    268  1.14        tv 	    	break;
    269   1.1       cgd 	case INCLTYPE:
    270   1.1       cgd 		if (argc > 2)
    271   1.2     glass 			if (!doincl(argv[2]))
    272  1.14        tv 				err(1, "%s at line %lu: include(%s)",
    273  1.14        tv 				    CURRENT_NAME, CURRENT_LINE, argv[2]);
    274   1.1       cgd 		break;
    275   1.1       cgd 
    276   1.1       cgd 	case SINCTYPE:
    277   1.1       cgd 		if (argc > 2)
    278   1.1       cgd 			(void) doincl(argv[2]);
    279   1.1       cgd 		break;
    280   1.1       cgd #ifdef EXTENDED
    281   1.1       cgd 	case PASTTYPE:
    282   1.1       cgd 		if (argc > 2)
    283   1.2     glass 			if (!dopaste(argv[2]))
    284  1.14        tv 				err(1, "%s at line %lu: paste(%s)",
    285  1.14        tv 				    CURRENT_NAME, CURRENT_LINE, argv[2]);
    286   1.1       cgd 		break;
    287   1.1       cgd 
    288   1.1       cgd 	case SPASTYPE:
    289   1.1       cgd 		if (argc > 2)
    290   1.1       cgd 			(void) dopaste(argv[2]);
    291   1.1       cgd 		break;
    292  1.20  christos 	case FORMATTYPE:
    293  1.20  christos 		doformat(argv, argc);
    294  1.20  christos 		break;
    295   1.1       cgd #endif
    296   1.1       cgd 	case CHNQTYPE:
    297  1.20  christos 		dochq(argv, ac);
    298   1.1       cgd 		break;
    299   1.1       cgd 
    300   1.1       cgd 	case CHNCTYPE:
    301  1.20  christos 		dochc(argv, argc);
    302   1.1       cgd 		break;
    303   1.1       cgd 
    304   1.1       cgd 	case SUBSTYPE:
    305   1.2     glass 	/*
    306   1.2     glass 	 * dosub - select substring
    307   1.2     glass 	 *
    308   1.2     glass 	 */
    309   1.1       cgd 		if (argc > 3)
    310   1.2     glass 			dosub(argv, argc);
    311   1.1       cgd 		break;
    312   1.1       cgd 
    313   1.1       cgd 	case SHIFTYPE:
    314   1.2     glass 	/*
    315   1.2     glass 	 * doshift - push back all arguments
    316   1.2     glass 	 * except the first one (i.e. skip
    317   1.2     glass 	 * argv[2])
    318   1.2     glass 	 */
    319   1.1       cgd 		if (argc > 3) {
    320   1.2     glass 			for (n = argc - 1; n > 3; n--) {
    321   1.9       cgd 				pbstr(rquote);
    322   1.1       cgd 				pbstr(argv[n]);
    323   1.9       cgd 				pbstr(lquote);
    324  1.20  christos 				pushback(COMMA);
    325   1.1       cgd 			}
    326   1.9       cgd 			pbstr(rquote);
    327   1.1       cgd 			pbstr(argv[3]);
    328   1.9       cgd 			pbstr(lquote);
    329   1.1       cgd 		}
    330   1.1       cgd 		break;
    331   1.1       cgd 
    332   1.1       cgd 	case DIVRTYPE:
    333   1.1       cgd 		if (argc > 2 && (n = atoi(argv[2])) != 0)
    334   1.1       cgd 			dodiv(n);
    335   1.1       cgd 		else {
    336   1.1       cgd 			active = stdout;
    337   1.1       cgd 			oindex = 0;
    338   1.1       cgd 		}
    339   1.1       cgd 		break;
    340   1.1       cgd 
    341   1.1       cgd 	case UNDVTYPE:
    342   1.1       cgd 		doundiv(argv, argc);
    343   1.1       cgd 		break;
    344   1.1       cgd 
    345   1.1       cgd 	case DIVNTYPE:
    346   1.2     glass 	/*
    347   1.2     glass 	 * dodivnum - return the number of
    348   1.2     glass 	 * current output diversion
    349   1.2     glass 	 */
    350   1.1       cgd 		pbnum(oindex);
    351   1.1       cgd 		break;
    352   1.1       cgd 
    353   1.1       cgd 	case UNDFTYPE:
    354   1.2     glass 	/*
    355   1.2     glass 	 * doundefine - undefine a previously
    356   1.2     glass 	 * defined macro(s) or m4 keyword(s).
    357   1.2     glass 	 */
    358   1.1       cgd 		if (argc > 2)
    359   1.1       cgd 			for (n = 2; n < argc; n++)
    360  1.20  christos 				macro_undefine(argv[n]);
    361   1.1       cgd 		break;
    362   1.1       cgd 
    363   1.1       cgd 	case POPDTYPE:
    364   1.2     glass 	/*
    365   1.2     glass 	 * dopopdef - remove the topmost
    366   1.2     glass 	 * definitions of macro(s) or m4
    367   1.2     glass 	 * keyword(s).
    368   1.2     glass 	 */
    369   1.1       cgd 		if (argc > 2)
    370   1.1       cgd 			for (n = 2; n < argc; n++)
    371  1.20  christos 				macro_popdef(argv[n]);
    372   1.1       cgd 		break;
    373   1.1       cgd 
    374   1.1       cgd 	case MKTMTYPE:
    375   1.2     glass 	/*
    376   1.2     glass 	 * dotemp - create a temporary file
    377   1.2     glass 	 */
    378  1.11       mrg 		if (argc > 2) {
    379  1.11       mrg 			int fd;
    380  1.14        tv 			char *temp;
    381  1.11       mrg 
    382  1.14        tv 			temp = xstrdup(argv[2]);
    383  1.14        tv 
    384  1.14        tv 			fd = mkstemp(temp);
    385  1.11       mrg 			if (fd == -1)
    386  1.14        tv 				err(1,
    387  1.14        tv 	    "%s at line %lu: couldn't make temp file %s",
    388  1.14        tv 	    CURRENT_NAME, CURRENT_LINE, argv[2]);
    389  1.11       mrg 			close(fd);
    390  1.14        tv 			pbstr(temp);
    391  1.14        tv 			free(temp);
    392  1.11       mrg 		}
    393   1.1       cgd 		break;
    394   1.1       cgd 
    395   1.1       cgd 	case TRNLTYPE:
    396   1.2     glass 	/*
    397   1.2     glass 	 * dotranslit - replace all characters in
    398   1.2     glass 	 * the source string that appears in the
    399   1.2     glass 	 * "from" string with the corresponding
    400   1.2     glass 	 * characters in the "to" string.
    401   1.2     glass 	 */
    402   1.1       cgd 		if (argc > 3) {
    403  1.20  christos 			char *temp;
    404  1.20  christos 
    405  1.20  christos 			temp = xalloc(strlen(argv[2])+1, NULL);
    406   1.1       cgd 			if (argc > 4)
    407   1.1       cgd 				map(temp, argv[2], argv[3], argv[4]);
    408   1.1       cgd 			else
    409   1.1       cgd 				map(temp, argv[2], argv[3], null);
    410   1.1       cgd 			pbstr(temp);
    411  1.20  christos 			free(temp);
    412  1.14        tv 		} else if (argc > 2)
    413   1.1       cgd 			pbstr(argv[2]);
    414   1.1       cgd 		break;
    415   1.1       cgd 
    416   1.1       cgd 	case INDXTYPE:
    417   1.2     glass 	/*
    418   1.2     glass 	 * doindex - find the index of the second
    419   1.2     glass 	 * argument string in the first argument
    420   1.2     glass 	 * string. -1 if not present.
    421   1.2     glass 	 */
    422   1.1       cgd 		pbnum((argc > 3) ? indx(argv[2], argv[3]) : -1);
    423   1.1       cgd 		break;
    424   1.1       cgd 
    425   1.1       cgd 	case ERRPTYPE:
    426   1.2     glass 	/*
    427   1.2     glass 	 * doerrp - print the arguments to stderr
    428   1.2     glass 	 * file
    429   1.2     glass 	 */
    430   1.1       cgd 		if (argc > 2) {
    431   1.1       cgd 			for (n = 2; n < argc; n++)
    432  1.21  christos 				fprintf(stderr, "%s%s",
    433  1.21  christos 				    mimic_gnu && n == 2 ? "" : " ",
    434  1.21  christos 				    argv[n]);
    435  1.21  christos 			if (!mimic_gnu)
    436  1.21  christos 				fprintf(stderr, "\n");
    437   1.1       cgd 		}
    438   1.1       cgd 		break;
    439   1.1       cgd 
    440   1.1       cgd 	case DNLNTYPE:
    441   1.2     glass 	/*
    442   1.2     glass 	 * dodnl - eat-up-to and including
    443   1.2     glass 	 * newline
    444   1.2     glass 	 */
    445   1.1       cgd 		while ((c = gpbc()) != '\n' && c != EOF)
    446   1.1       cgd 			;
    447   1.1       cgd 		break;
    448   1.1       cgd 
    449   1.1       cgd 	case M4WRTYPE:
    450   1.2     glass 	/*
    451   1.2     glass 	 * dom4wrap - set up for
    452   1.2     glass 	 * wrap-up/wind-down activity
    453   1.2     glass 	 */
    454  1.20  christos 		if (argc > 2)
    455  1.20  christos 			dom4wrap(argv[2]);
    456   1.1       cgd 		break;
    457   1.1       cgd 
    458   1.1       cgd 	case EXITTYPE:
    459   1.2     glass 	/*
    460   1.2     glass 	 * doexit - immediate exit from m4.
    461   1.2     glass 	 */
    462   1.3   mycroft 		killdiv();
    463   1.1       cgd 		exit((argc > 2) ? atoi(argv[2]) : 0);
    464   1.1       cgd 		break;
    465   1.1       cgd 
    466   1.1       cgd 	case DEFNTYPE:
    467   1.1       cgd 		if (argc > 2)
    468   1.1       cgd 			for (n = 2; n < argc; n++)
    469   1.1       cgd 				dodefn(argv[n]);
    470   1.1       cgd 		break;
    471   1.1       cgd 
    472  1.14        tv 	case INDIRTYPE:	/* Indirect call */
    473  1.14        tv 		if (argc > 2)
    474  1.14        tv 			doindir(argv, argc);
    475  1.14        tv 		break;
    476  1.14        tv 
    477  1.14        tv 	case BUILTINTYPE: /* Builtins only */
    478  1.14        tv 		if (argc > 2)
    479  1.14        tv 			dobuiltin(argv, argc);
    480  1.14        tv 		break;
    481  1.14        tv 
    482  1.14        tv 	case PATSTYPE:
    483  1.14        tv 		if (argc > 2)
    484  1.14        tv 			dopatsubst(argv, argc);
    485  1.14        tv 		break;
    486  1.14        tv 	case REGEXPTYPE:
    487  1.14        tv 		if (argc > 2)
    488  1.14        tv 			doregexp(argv, argc);
    489  1.14        tv 		break;
    490  1.14        tv 	case LINETYPE:
    491  1.14        tv 		doprintlineno(infile+ilevel);
    492  1.14        tv 		break;
    493  1.14        tv 	case FILENAMETYPE:
    494  1.14        tv 		doprintfilename(infile+ilevel);
    495  1.14        tv 		break;
    496  1.14        tv 	case SELFTYPE:
    497  1.14        tv 		pbstr(rquote);
    498  1.14        tv 		pbstr(argv[1]);
    499  1.14        tv 		pbstr(lquote);
    500  1.14        tv 		break;
    501   1.1       cgd 	default:
    502  1.20  christos 		m4errx(1, "eval: major botch.");
    503   1.1       cgd 		break;
    504   1.1       cgd 	}
    505   1.2     glass }
    506   1.2     glass 
    507   1.2     glass /*
    508  1.14        tv  * expand_macro - user-defined macro expansion
    509   1.2     glass  */
    510   1.2     glass void
    511  1.20  christos expand_macro(const char *argv[], int argc)
    512   1.2     glass {
    513  1.14        tv 	const char *t;
    514  1.14        tv 	const char *p;
    515  1.10     lukem 	int n;
    516  1.10     lukem 	int argno;
    517   1.2     glass 
    518   1.2     glass 	t = argv[0];		       /* defn string as a whole */
    519   1.2     glass 	p = t;
    520   1.2     glass 	while (*p)
    521   1.2     glass 		p++;
    522   1.2     glass 	p--;			       /* last character of defn */
    523   1.2     glass 	while (p > t) {
    524   1.2     glass 		if (*(p - 1) != ARGFLAG)
    525  1.20  christos 			PUSHBACK(*p);
    526   1.2     glass 		else {
    527   1.2     glass 			switch (*p) {
    528   1.2     glass 
    529   1.2     glass 			case '#':
    530   1.2     glass 				pbnum(argc - 2);
    531   1.2     glass 				break;
    532   1.2     glass 			case '0':
    533   1.2     glass 			case '1':
    534   1.2     glass 			case '2':
    535   1.2     glass 			case '3':
    536   1.2     glass 			case '4':
    537   1.2     glass 			case '5':
    538   1.2     glass 			case '6':
    539   1.2     glass 			case '7':
    540   1.2     glass 			case '8':
    541   1.2     glass 			case '9':
    542   1.2     glass 				if ((argno = *p - '0') < argc - 1)
    543   1.2     glass 					pbstr(argv[argno + 1]);
    544   1.2     glass 				break;
    545   1.2     glass 			case '*':
    546  1.14        tv 				if (argc > 2) {
    547  1.14        tv 					for (n = argc - 1; n > 2; n--) {
    548  1.14        tv 						pbstr(argv[n]);
    549  1.20  christos 						pushback(COMMA);
    550  1.14        tv 					}
    551  1.14        tv 					pbstr(argv[2]);
    552  1.14        tv 			    	}
    553   1.2     glass 				break;
    554  1.14        tv                         case '@':
    555  1.14        tv 				if (argc > 2) {
    556  1.14        tv 					for (n = argc - 1; n > 2; n--) {
    557  1.14        tv 						pbstr(rquote);
    558  1.14        tv 						pbstr(argv[n]);
    559  1.14        tv 						pbstr(lquote);
    560  1.20  christos 						pushback(COMMA);
    561  1.14        tv 					}
    562  1.13  jdolecek 					pbstr(rquote);
    563  1.14        tv 					pbstr(argv[2]);
    564  1.13  jdolecek 					pbstr(lquote);
    565  1.13  jdolecek 				}
    566  1.14        tv                                 break;
    567   1.2     glass 			default:
    568  1.20  christos 				PUSHBACK(*p);
    569  1.20  christos 				PUSHBACK('$');
    570   1.2     glass 				break;
    571   1.2     glass 			}
    572   1.2     glass 			p--;
    573   1.2     glass 		}
    574   1.2     glass 		p--;
    575   1.2     glass 	}
    576   1.2     glass 	if (p == t)		       /* do last character */
    577  1.20  christos 		PUSHBACK(*p);
    578   1.2     glass }
    579   1.2     glass 
    580  1.20  christos 
    581   1.2     glass /*
    582   1.2     glass  * dodefine - install definition in the table
    583   1.2     glass  */
    584   1.2     glass void
    585  1.20  christos dodefine(const char *name, const char *defn)
    586   1.2     glass {
    587  1.20  christos 	if (!*name && !mimic_gnu)
    588  1.20  christos 		m4errx(1, "null definition.");
    589  1.20  christos 	else
    590  1.20  christos 		macro_define(name, defn);
    591   1.2     glass }
    592   1.2     glass 
    593   1.2     glass /*
    594   1.2     glass  * dodefn - push back a quoted definition of
    595   1.2     glass  *      the given name.
    596   1.2     glass  */
    597  1.14        tv static void
    598  1.20  christos dodefn(const char *name)
    599   1.2     glass {
    600  1.20  christos 	struct macro_definition *p;
    601   1.2     glass 
    602  1.20  christos 	if ((p = lookup_macro_definition(name)) != NULL) {
    603  1.20  christos 		if ((p->type & TYPEMASK) == MACRTYPE) {
    604  1.14        tv 			pbstr(rquote);
    605  1.14        tv 			pbstr(p->defn);
    606  1.14        tv 			pbstr(lquote);
    607  1.20  christos 		} else {
    608  1.20  christos 			pbstr(p->defn);
    609  1.14        tv 			pbstr(BUILTIN_MARKER);
    610  1.14        tv 		}
    611   1.2     glass 	}
    612   1.2     glass }
    613   1.2     glass 
    614   1.2     glass /*
    615   1.2     glass  * dopushdef - install a definition in the hash table
    616   1.2     glass  *      without removing a previous definition. Since
    617   1.2     glass  *      each new entry is entered in *front* of the
    618   1.2     glass  *      hash bucket, it hides a previous definition from
    619   1.2     glass  *      lookup.
    620   1.2     glass  */
    621  1.14        tv static void
    622  1.20  christos dopushdef(const char *name, const char *defn)
    623   1.2     glass {
    624  1.20  christos 	if (!*name && !mimic_gnu)
    625  1.20  christos 		m4errx(1, "null definition.");
    626   1.2     glass 	else
    627  1.20  christos 		macro_pushdef(name, defn);
    628  1.14        tv }
    629  1.14        tv 
    630  1.14        tv /*
    631  1.14        tv  * dump_one_def - dump the specified definition.
    632  1.14        tv  */
    633  1.14        tv static void
    634  1.20  christos dump_one_def(const char *name, struct macro_definition *p)
    635  1.14        tv {
    636  1.20  christos 	if (!traceout)
    637  1.20  christos 		traceout = stderr;
    638  1.14        tv 	if (mimic_gnu) {
    639  1.14        tv 		if ((p->type & TYPEMASK) == MACRTYPE)
    640  1.20  christos 			fprintf(traceout, "%s:\t%s\n", name, p->defn);
    641  1.14        tv 		else {
    642  1.20  christos 			fprintf(traceout, "%s:\t<%s>\n", name, p->defn);
    643  1.14        tv 	    	}
    644  1.14        tv 	} else
    645  1.20  christos 		fprintf(traceout, "`%s'\t`%s'\n", name, p->defn);
    646   1.2     glass }
    647   1.2     glass 
    648   1.2     glass /*
    649   1.2     glass  * dodumpdef - dump the specified definitions in the hash
    650   1.2     glass  *      table to stderr. If nothing is specified, the entire
    651   1.2     glass  *      hash table is dumped.
    652   1.2     glass  */
    653  1.14        tv static void
    654  1.20  christos dodump(const char *argv[], int argc)
    655   1.2     glass {
    656  1.10     lukem 	int n;
    657  1.20  christos 	struct macro_definition *p;
    658   1.2     glass 
    659   1.2     glass 	if (argc > 2) {
    660   1.2     glass 		for (n = 2; n < argc; n++)
    661  1.20  christos 			if ((p = lookup_macro_definition(argv[n])) != NULL)
    662  1.20  christos 				dump_one_def(argv[n], p);
    663  1.20  christos 	} else
    664  1.20  christos 		macro_for_all(dump_one_def);
    665   1.2     glass }
    666   1.2     glass 
    667   1.2     glass /*
    668  1.14        tv  * dotrace - mark some macros as traced/untraced depending upon on.
    669  1.14        tv  */
    670  1.14        tv static void
    671  1.20  christos dotrace(const char *argv[], int argc, int on)
    672  1.14        tv {
    673  1.14        tv 	int n;
    674  1.14        tv 
    675  1.14        tv 	if (argc > 2) {
    676  1.14        tv 		for (n = 2; n < argc; n++)
    677  1.14        tv 			mark_traced(argv[n], on);
    678  1.14        tv 	} else
    679  1.14        tv 		mark_traced(NULL, on);
    680  1.14        tv }
    681  1.14        tv 
    682  1.14        tv /*
    683   1.2     glass  * doifelse - select one of two alternatives - loop.
    684   1.2     glass  */
    685  1.14        tv static void
    686  1.20  christos doifelse(const char *argv[], int argc)
    687   1.2     glass {
    688   1.2     glass 	cycle {
    689   1.2     glass 		if (STREQ(argv[2], argv[3]))
    690   1.2     glass 			pbstr(argv[4]);
    691   1.2     glass 		else if (argc == 6)
    692   1.2     glass 			pbstr(argv[5]);
    693   1.2     glass 		else if (argc > 6) {
    694   1.2     glass 			argv += 3;
    695   1.2     glass 			argc -= 3;
    696   1.2     glass 			continue;
    697   1.2     glass 		}
    698   1.2     glass 		break;
    699   1.2     glass 	}
    700   1.2     glass }
    701   1.2     glass 
    702   1.2     glass /*
    703   1.2     glass  * doinclude - include a given file.
    704   1.2     glass  */
    705  1.14        tv static int
    706  1.20  christos doincl(const char *ifile)
    707   1.2     glass {
    708   1.2     glass 	if (ilevel + 1 == MAXINP)
    709  1.20  christos 		m4errx(1, "too many include files.");
    710  1.14        tv 	if (fopen_trypath(infile+ilevel+1, ifile) != NULL) {
    711   1.2     glass 		ilevel++;
    712   1.2     glass 		bbase[ilevel] = bufbase = bp;
    713   1.2     glass 		return (1);
    714  1.14        tv 	} else
    715   1.2     glass 		return (0);
    716   1.2     glass }
    717   1.2     glass 
    718   1.2     glass #ifdef EXTENDED
    719   1.2     glass /*
    720   1.2     glass  * dopaste - include a given file without any
    721   1.2     glass  *           macro processing.
    722   1.2     glass  */
    723  1.14        tv static int
    724  1.20  christos dopaste(const char *pfile)
    725   1.2     glass {
    726   1.2     glass 	FILE *pf;
    727  1.10     lukem 	int c;
    728   1.2     glass 
    729   1.2     glass 	if ((pf = fopen(pfile, "r")) != NULL) {
    730  1.20  christos 		if (synch_lines)
    731  1.20  christos 		    fprintf(active, "#line 1 \"%s\"\n", pfile);
    732   1.2     glass 		while ((c = getc(pf)) != EOF)
    733   1.2     glass 			putc(c, active);
    734   1.2     glass 		(void) fclose(pf);
    735  1.20  christos 		emit_synchline();
    736   1.2     glass 		return (1);
    737  1.14        tv 	} else
    738   1.2     glass 		return (0);
    739   1.2     glass }
    740   1.2     glass #endif
    741   1.2     glass 
    742  1.20  christos /*
    743  1.20  christos  * dochq - change quote characters
    744  1.20  christos  */
    745  1.14        tv static void
    746  1.20  christos dochq(const char *argv[], int ac)
    747  1.14        tv {
    748  1.14        tv 	if (ac == 2) {
    749  1.20  christos 		lquote[0] = LQUOTE; lquote[1] = EOS;
    750  1.20  christos 		rquote[0] = RQUOTE; rquote[1] = EOS;
    751  1.14        tv 	} else {
    752  1.14        tv 		strlcpy(lquote, argv[2], sizeof(lquote));
    753  1.20  christos 		if (ac > 3) {
    754  1.14        tv 			strlcpy(rquote, argv[3], sizeof(rquote));
    755  1.20  christos 		} else {
    756  1.20  christos 			rquote[0] = ECOMMT; rquote[1] = EOS;
    757  1.20  christos 		}
    758  1.14        tv 	}
    759  1.14        tv }
    760  1.14        tv 
    761   1.2     glass /*
    762  1.20  christos  * dochc - change comment characters
    763   1.2     glass  */
    764  1.14        tv static void
    765  1.20  christos dochc(const char *argv[], int argc)
    766   1.2     glass {
    767  1.20  christos /* XXX Note that there is no difference between no argument and a single
    768  1.20  christos  * empty argument.
    769  1.20  christos  */
    770  1.20  christos 	if (argc == 2) {
    771  1.14        tv 		scommt[0] = EOS;
    772  1.14        tv 		ecommt[0] = EOS;
    773  1.14        tv 	} else {
    774  1.20  christos 		strlcpy(scommt, argv[2], sizeof(scommt));
    775  1.20  christos 		if (argc == 3) {
    776  1.20  christos 			ecommt[0] = ECOMMT; ecommt[1] = EOS;
    777  1.20  christos 		} else {
    778  1.14        tv 			strlcpy(ecommt, argv[3], sizeof(ecommt));
    779  1.20  christos 		}
    780   1.2     glass 	}
    781   1.2     glass }
    782  1.20  christos 
    783   1.2     glass /*
    784  1.20  christos  * dom4wrap - expand text at EOF
    785   1.2     glass  */
    786  1.14        tv static void
    787  1.20  christos dom4wrap(const char *text)
    788   1.2     glass {
    789  1.20  christos 	if (wrapindex >= maxwraps) {
    790  1.20  christos 		if (maxwraps == 0)
    791  1.20  christos 			maxwraps = 16;
    792   1.2     glass 		else
    793  1.20  christos 			maxwraps *= 2;
    794  1.20  christos 		m4wraps = xrealloc(m4wraps, maxwraps * sizeof(*m4wraps),
    795  1.20  christos 		   "too many m4wraps");
    796   1.2     glass 	}
    797  1.20  christos 	m4wraps[wrapindex++] = xstrdup(text);
    798   1.2     glass }
    799   1.2     glass 
    800   1.2     glass /*
    801   1.2     glass  * dodivert - divert the output to a temporary file
    802   1.2     glass  */
    803  1.14        tv static void
    804  1.20  christos dodiv(int n)
    805   1.2     glass {
    806  1.14        tv 	int fd;
    807   1.9       cgd 
    808  1.14        tv 	oindex = n;
    809  1.14        tv 	if (n >= maxout) {
    810  1.14        tv 		if (mimic_gnu)
    811  1.14        tv 			resizedivs(n + 10);
    812  1.14        tv 		else
    813  1.14        tv 			n = 0;		/* bitbucket */
    814  1.14        tv     	}
    815   1.9       cgd 
    816  1.14        tv 	if (n < 0)
    817  1.14        tv 		n = 0;		       /* bitbucket */
    818  1.14        tv 	if (outfile[n] == NULL) {
    819  1.14        tv 		char fname[] = _PATH_DIVNAME;
    820  1.14        tv 
    821  1.14        tv 		if ((fd = mkstemp(fname)) < 0 ||
    822  1.14        tv 			(outfile[n] = fdopen(fd, "w+")) == NULL)
    823  1.14        tv 				err(1, "%s: cannot divert", fname);
    824  1.14        tv 		if (unlink(fname) == -1)
    825  1.14        tv 			err(1, "%s: cannot unlink", fname);
    826   1.2     glass 	}
    827  1.14        tv 	active = outfile[n];
    828   1.2     glass }
    829   1.2     glass 
    830   1.2     glass /*
    831   1.2     glass  * doundivert - undivert a specified output, or all
    832   1.2     glass  *              other outputs, in numerical order.
    833   1.2     glass  */
    834  1.14        tv static void
    835  1.20  christos doundiv(const char *argv[], int argc)
    836   1.2     glass {
    837  1.10     lukem 	int ind;
    838  1.10     lukem 	int n;
    839   1.2     glass 
    840   1.2     glass 	if (argc > 2) {
    841   1.2     glass 		for (ind = 2; ind < argc; ind++) {
    842  1.20  christos 			const char *errstr;
    843  1.20  christos 			n = strtonum(argv[ind], 1, INT_MAX, &errstr);
    844  1.20  christos 			if (errstr) {
    845  1.20  christos 				if (errno == EINVAL && mimic_gnu)
    846  1.20  christos 					getdivfile(argv[ind]);
    847  1.20  christos 			} else {
    848  1.20  christos 				if (n < maxout && outfile[n] != NULL)
    849  1.20  christos 					getdiv(n);
    850  1.20  christos 			}
    851   1.2     glass 		}
    852   1.2     glass 	}
    853   1.2     glass 	else
    854  1.14        tv 		for (n = 1; n < maxout; n++)
    855   1.2     glass 			if (outfile[n] != NULL)
    856   1.2     glass 				getdiv(n);
    857   1.2     glass }
    858   1.2     glass 
    859   1.2     glass /*
    860   1.2     glass  * dosub - select substring
    861   1.2     glass  */
    862  1.14        tv static void
    863  1.20  christos dosub(const char *argv[], int argc)
    864   1.2     glass {
    865  1.14        tv 	const char *ap, *fc, *k;
    866  1.10     lukem 	int nc;
    867   1.2     glass 
    868  1.14        tv 	ap = argv[2];		       /* target string */
    869   1.2     glass #ifdef EXPR
    870  1.14        tv 	fc = ap + expr(argv[3]);       /* first char */
    871   1.2     glass #else
    872  1.14        tv 	fc = ap + atoi(argv[3]);       /* first char */
    873   1.2     glass #endif
    874  1.14        tv 	nc = strlen(fc);
    875  1.14        tv 	if (argc >= 5)
    876   1.2     glass #ifdef EXPR
    877  1.14        tv 		nc = min(nc, expr(argv[4]));
    878   1.2     glass #else
    879  1.14        tv 		nc = min(nc, atoi(argv[4]));
    880   1.2     glass #endif
    881   1.2     glass 	if (fc >= ap && fc < ap + strlen(ap))
    882  1.14        tv 		for (k = fc + nc - 1; k >= fc; k--)
    883  1.20  christos 			pushback(*k);
    884   1.2     glass }
    885   1.2     glass 
    886   1.2     glass /*
    887   1.2     glass  * map:
    888   1.2     glass  * map every character of s1 that is specified in from
    889   1.2     glass  * into s3 and replace in s. (source s1 remains untouched)
    890   1.2     glass  *
    891   1.2     glass  * This is a standard implementation of map(s,from,to) function of ICON
    892   1.2     glass  * language. Within mapvec, we replace every character of "from" with
    893   1.2     glass  * the corresponding character in "to". If "to" is shorter than "from",
    894   1.2     glass  * than the corresponding entries are null, which means that those
    895   1.2     glass  * characters dissapear altogether. Furthermore, imagine
    896   1.2     glass  * map(dest, "sourcestring", "srtin", "rn..*") type call. In this case,
    897   1.2     glass  * `s' maps to `r', `r' maps to `n' and `n' maps to `*'. Thus, `s'
    898   1.2     glass  * ultimately maps to `*'. In order to achieve this effect in an efficient
    899   1.2     glass  * manner (i.e. without multiple passes over the destination string), we
    900   1.2     glass  * loop over mapvec, starting with the initial source character. if the
    901   1.2     glass  * character value (dch) in this location is different than the source
    902   1.2     glass  * character (sch), sch becomes dch, once again to index into mapvec, until
    903   1.2     glass  * the character value stabilizes (i.e. sch = dch, in other words
    904   1.2     glass  * mapvec[n] == n). Even if the entry in the mapvec is null for an ordinary
    905   1.2     glass  * character, it will stabilize, since mapvec[0] == 0 at all times. At the
    906   1.2     glass  * end, we restore mapvec* back to normal where mapvec[n] == n for
    907   1.2     glass  * 0 <= n <= 127. This strategy, along with the restoration of mapvec, is
    908   1.2     glass  * about 5 times faster than any algorithm that makes multiple passes over
    909   1.2     glass  * destination string.
    910   1.2     glass  */
    911  1.14        tv static void
    912  1.20  christos map(char *dest, const char *src, const char *from, const char *to)
    913  1.14        tv {
    914  1.14        tv 	const char *tmp;
    915  1.14        tv 	unsigned char sch, dch;
    916  1.14        tv 	static char frombis[257];
    917  1.14        tv 	static char tobis[257];
    918  1.14        tv 	static unsigned char mapvec[256] = {
    919  1.14        tv 	    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
    920  1.14        tv 	    19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
    921  1.14        tv 	    36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
    922  1.14        tv 	    53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
    923  1.14        tv 	    70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
    924  1.14        tv 	    87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102,
    925  1.14        tv 	    103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
    926  1.14        tv 	    116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128,
    927  1.14        tv 	    129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141,
    928  1.14        tv 	    142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154,
    929  1.14        tv 	    155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167,
    930  1.14        tv 	    168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180,
    931  1.14        tv 	    181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193,
    932  1.14        tv 	    194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206,
    933  1.14        tv 	    207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
    934  1.14        tv 	    220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232,
    935  1.14        tv 	    233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245,
    936  1.14        tv 	    246, 247, 248, 249, 250, 251, 252, 253, 254, 255
    937   1.2     glass 	};
    938   1.2     glass 
    939   1.2     glass 	if (*src) {
    940  1.14        tv 		if (mimic_gnu) {
    941  1.14        tv 			/*
    942  1.14        tv 			 * expand character ranges on the fly
    943  1.14        tv 			 */
    944  1.14        tv 			from = handledash(frombis, frombis + 256, from);
    945  1.14        tv 			to = handledash(tobis, tobis + 256, to);
    946  1.14        tv 		}
    947   1.2     glass 		tmp = from;
    948   1.2     glass 	/*
    949   1.2     glass 	 * create a mapping between "from" and
    950   1.2     glass 	 * "to"
    951   1.2     glass 	 */
    952   1.2     glass 		while (*from)
    953  1.14        tv 			mapvec[(unsigned char)(*from++)] = (*to) ?
    954  1.14        tv 				(unsigned char)(*to++) : 0;
    955   1.2     glass 
    956   1.2     glass 		while (*src) {
    957  1.14        tv 			sch = (unsigned char)(*src++);
    958  1.14        tv 			dch = mapvec[sch];
    959   1.2     glass 			while (dch != sch) {
    960   1.2     glass 				sch = dch;
    961  1.14        tv 				dch = mapvec[sch];
    962   1.2     glass 			}
    963  1.14        tv 			if ((*dest = (char)dch))
    964   1.2     glass 				dest++;
    965   1.2     glass 		}
    966   1.2     glass 	/*
    967   1.2     glass 	 * restore all the changed characters
    968   1.2     glass 	 */
    969   1.2     glass 		while (*tmp) {
    970  1.14        tv 			mapvec[(unsigned char)(*tmp)] = (unsigned char)(*tmp);
    971   1.2     glass 			tmp++;
    972   1.2     glass 		}
    973   1.2     glass 	}
    974  1.14        tv 	*dest = '\0';
    975  1.14        tv }
    976  1.14        tv 
    977  1.14        tv 
    978  1.14        tv /*
    979  1.14        tv  * handledash:
    980  1.14        tv  *  use buffer to copy the src string, expanding character ranges
    981  1.14        tv  * on the way.
    982  1.14        tv  */
    983  1.14        tv static const char *
    984  1.20  christos handledash(char *buffer, char *end, const char *src)
    985  1.14        tv {
    986  1.14        tv 	char *p;
    987  1.14        tv 
    988  1.14        tv 	p = buffer;
    989  1.14        tv 	while(*src) {
    990  1.14        tv 		if (src[1] == '-' && src[2]) {
    991  1.14        tv 			unsigned char i;
    992  1.20  christos 			if ((unsigned char)src[0] <= (unsigned char)src[2]) {
    993  1.20  christos 				for (i = (unsigned char)src[0];
    994  1.20  christos 				    i <= (unsigned char)src[2]; i++) {
    995  1.20  christos 					*p++ = i;
    996  1.20  christos 					if (p == end) {
    997  1.20  christos 						*p = '\0';
    998  1.20  christos 						return buffer;
    999  1.20  christos 					}
   1000  1.20  christos 				}
   1001  1.20  christos 			} else {
   1002  1.20  christos 				for (i = (unsigned char)src[0];
   1003  1.20  christos 				    i >= (unsigned char)src[2]; i--) {
   1004  1.20  christos 					*p++ = i;
   1005  1.20  christos 					if (p == end) {
   1006  1.20  christos 						*p = '\0';
   1007  1.20  christos 						return buffer;
   1008  1.20  christos 					}
   1009  1.14        tv 				}
   1010  1.14        tv 			}
   1011  1.14        tv 			src += 3;
   1012  1.14        tv 		} else
   1013  1.14        tv 			*p++ = *src++;
   1014  1.14        tv 		if (p == end)
   1015  1.14        tv 			break;
   1016  1.14        tv 	}
   1017  1.14        tv 	*p = '\0';
   1018  1.14        tv 	return buffer;
   1019   1.1       cgd }
   1020