Home | History | Annotate | Line # | Download | only in m4
eval.c revision 1.20
      1  1.20  christos /*	$OpenBSD: eval.c,v 1.66 2008/08/21 21:01:47 espie Exp $	*/
      2  1.20  christos /*	$NetBSD: eval.c,v 1.20 2009/10/26 21:11:28 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.20  christos __RCSID("$NetBSD: eval.c,v 1.20 2009/10/26 21:11:28 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.2     glass 				fprintf(stderr, "%s ", argv[n]);
    433   1.1       cgd 			fprintf(stderr, "\n");
    434   1.1       cgd 		}
    435   1.1       cgd 		break;
    436   1.1       cgd 
    437   1.1       cgd 	case DNLNTYPE:
    438   1.2     glass 	/*
    439   1.2     glass 	 * dodnl - eat-up-to and including
    440   1.2     glass 	 * newline
    441   1.2     glass 	 */
    442   1.1       cgd 		while ((c = gpbc()) != '\n' && c != EOF)
    443   1.1       cgd 			;
    444   1.1       cgd 		break;
    445   1.1       cgd 
    446   1.1       cgd 	case M4WRTYPE:
    447   1.2     glass 	/*
    448   1.2     glass 	 * dom4wrap - set up for
    449   1.2     glass 	 * wrap-up/wind-down activity
    450   1.2     glass 	 */
    451  1.20  christos 		if (argc > 2)
    452  1.20  christos 			dom4wrap(argv[2]);
    453   1.1       cgd 		break;
    454   1.1       cgd 
    455   1.1       cgd 	case EXITTYPE:
    456   1.2     glass 	/*
    457   1.2     glass 	 * doexit - immediate exit from m4.
    458   1.2     glass 	 */
    459   1.3   mycroft 		killdiv();
    460   1.1       cgd 		exit((argc > 2) ? atoi(argv[2]) : 0);
    461   1.1       cgd 		break;
    462   1.1       cgd 
    463   1.1       cgd 	case DEFNTYPE:
    464   1.1       cgd 		if (argc > 2)
    465   1.1       cgd 			for (n = 2; n < argc; n++)
    466   1.1       cgd 				dodefn(argv[n]);
    467   1.1       cgd 		break;
    468   1.1       cgd 
    469  1.14        tv 	case INDIRTYPE:	/* Indirect call */
    470  1.14        tv 		if (argc > 2)
    471  1.14        tv 			doindir(argv, argc);
    472  1.14        tv 		break;
    473  1.14        tv 
    474  1.14        tv 	case BUILTINTYPE: /* Builtins only */
    475  1.14        tv 		if (argc > 2)
    476  1.14        tv 			dobuiltin(argv, argc);
    477  1.14        tv 		break;
    478  1.14        tv 
    479  1.14        tv 	case PATSTYPE:
    480  1.14        tv 		if (argc > 2)
    481  1.14        tv 			dopatsubst(argv, argc);
    482  1.14        tv 		break;
    483  1.14        tv 	case REGEXPTYPE:
    484  1.14        tv 		if (argc > 2)
    485  1.14        tv 			doregexp(argv, argc);
    486  1.14        tv 		break;
    487  1.14        tv 	case LINETYPE:
    488  1.14        tv 		doprintlineno(infile+ilevel);
    489  1.14        tv 		break;
    490  1.14        tv 	case FILENAMETYPE:
    491  1.14        tv 		doprintfilename(infile+ilevel);
    492  1.14        tv 		break;
    493  1.14        tv 	case SELFTYPE:
    494  1.14        tv 		pbstr(rquote);
    495  1.14        tv 		pbstr(argv[1]);
    496  1.14        tv 		pbstr(lquote);
    497  1.14        tv 		break;
    498   1.1       cgd 	default:
    499  1.20  christos 		m4errx(1, "eval: major botch.");
    500   1.1       cgd 		break;
    501   1.1       cgd 	}
    502   1.2     glass }
    503   1.2     glass 
    504   1.2     glass /*
    505  1.14        tv  * expand_macro - user-defined macro expansion
    506   1.2     glass  */
    507   1.2     glass void
    508  1.20  christos expand_macro(const char *argv[], int argc)
    509   1.2     glass {
    510  1.14        tv 	const char *t;
    511  1.14        tv 	const char *p;
    512  1.10     lukem 	int n;
    513  1.10     lukem 	int argno;
    514   1.2     glass 
    515   1.2     glass 	t = argv[0];		       /* defn string as a whole */
    516   1.2     glass 	p = t;
    517   1.2     glass 	while (*p)
    518   1.2     glass 		p++;
    519   1.2     glass 	p--;			       /* last character of defn */
    520   1.2     glass 	while (p > t) {
    521   1.2     glass 		if (*(p - 1) != ARGFLAG)
    522  1.20  christos 			PUSHBACK(*p);
    523   1.2     glass 		else {
    524   1.2     glass 			switch (*p) {
    525   1.2     glass 
    526   1.2     glass 			case '#':
    527   1.2     glass 				pbnum(argc - 2);
    528   1.2     glass 				break;
    529   1.2     glass 			case '0':
    530   1.2     glass 			case '1':
    531   1.2     glass 			case '2':
    532   1.2     glass 			case '3':
    533   1.2     glass 			case '4':
    534   1.2     glass 			case '5':
    535   1.2     glass 			case '6':
    536   1.2     glass 			case '7':
    537   1.2     glass 			case '8':
    538   1.2     glass 			case '9':
    539   1.2     glass 				if ((argno = *p - '0') < argc - 1)
    540   1.2     glass 					pbstr(argv[argno + 1]);
    541   1.2     glass 				break;
    542   1.2     glass 			case '*':
    543  1.14        tv 				if (argc > 2) {
    544  1.14        tv 					for (n = argc - 1; n > 2; n--) {
    545  1.14        tv 						pbstr(argv[n]);
    546  1.20  christos 						pushback(COMMA);
    547  1.14        tv 					}
    548  1.14        tv 					pbstr(argv[2]);
    549  1.14        tv 			    	}
    550   1.2     glass 				break;
    551  1.14        tv                         case '@':
    552  1.14        tv 				if (argc > 2) {
    553  1.14        tv 					for (n = argc - 1; n > 2; n--) {
    554  1.14        tv 						pbstr(rquote);
    555  1.14        tv 						pbstr(argv[n]);
    556  1.14        tv 						pbstr(lquote);
    557  1.20  christos 						pushback(COMMA);
    558  1.14        tv 					}
    559  1.13  jdolecek 					pbstr(rquote);
    560  1.14        tv 					pbstr(argv[2]);
    561  1.13  jdolecek 					pbstr(lquote);
    562  1.13  jdolecek 				}
    563  1.14        tv                                 break;
    564   1.2     glass 			default:
    565  1.20  christos 				PUSHBACK(*p);
    566  1.20  christos 				PUSHBACK('$');
    567   1.2     glass 				break;
    568   1.2     glass 			}
    569   1.2     glass 			p--;
    570   1.2     glass 		}
    571   1.2     glass 		p--;
    572   1.2     glass 	}
    573   1.2     glass 	if (p == t)		       /* do last character */
    574  1.20  christos 		PUSHBACK(*p);
    575   1.2     glass }
    576   1.2     glass 
    577  1.20  christos 
    578   1.2     glass /*
    579   1.2     glass  * dodefine - install definition in the table
    580   1.2     glass  */
    581   1.2     glass void
    582  1.20  christos dodefine(const char *name, const char *defn)
    583   1.2     glass {
    584  1.20  christos 	if (!*name && !mimic_gnu)
    585  1.20  christos 		m4errx(1, "null definition.");
    586  1.20  christos 	else
    587  1.20  christos 		macro_define(name, defn);
    588   1.2     glass }
    589   1.2     glass 
    590   1.2     glass /*
    591   1.2     glass  * dodefn - push back a quoted definition of
    592   1.2     glass  *      the given name.
    593   1.2     glass  */
    594  1.14        tv static void
    595  1.20  christos dodefn(const char *name)
    596   1.2     glass {
    597  1.20  christos 	struct macro_definition *p;
    598   1.2     glass 
    599  1.20  christos 	if ((p = lookup_macro_definition(name)) != NULL) {
    600  1.20  christos 		if ((p->type & TYPEMASK) == MACRTYPE) {
    601  1.14        tv 			pbstr(rquote);
    602  1.14        tv 			pbstr(p->defn);
    603  1.14        tv 			pbstr(lquote);
    604  1.20  christos 		} else {
    605  1.20  christos 			pbstr(p->defn);
    606  1.14        tv 			pbstr(BUILTIN_MARKER);
    607  1.14        tv 		}
    608   1.2     glass 	}
    609   1.2     glass }
    610   1.2     glass 
    611   1.2     glass /*
    612   1.2     glass  * dopushdef - install a definition in the hash table
    613   1.2     glass  *      without removing a previous definition. Since
    614   1.2     glass  *      each new entry is entered in *front* of the
    615   1.2     glass  *      hash bucket, it hides a previous definition from
    616   1.2     glass  *      lookup.
    617   1.2     glass  */
    618  1.14        tv static void
    619  1.20  christos dopushdef(const char *name, const char *defn)
    620   1.2     glass {
    621  1.20  christos 	if (!*name && !mimic_gnu)
    622  1.20  christos 		m4errx(1, "null definition.");
    623   1.2     glass 	else
    624  1.20  christos 		macro_pushdef(name, defn);
    625  1.14        tv }
    626  1.14        tv 
    627  1.14        tv /*
    628  1.14        tv  * dump_one_def - dump the specified definition.
    629  1.14        tv  */
    630  1.14        tv static void
    631  1.20  christos dump_one_def(const char *name, struct macro_definition *p)
    632  1.14        tv {
    633  1.20  christos 	if (!traceout)
    634  1.20  christos 		traceout = stderr;
    635  1.14        tv 	if (mimic_gnu) {
    636  1.14        tv 		if ((p->type & TYPEMASK) == MACRTYPE)
    637  1.20  christos 			fprintf(traceout, "%s:\t%s\n", name, p->defn);
    638  1.14        tv 		else {
    639  1.20  christos 			fprintf(traceout, "%s:\t<%s>\n", name, p->defn);
    640  1.14        tv 	    	}
    641  1.14        tv 	} else
    642  1.20  christos 		fprintf(traceout, "`%s'\t`%s'\n", name, p->defn);
    643   1.2     glass }
    644   1.2     glass 
    645   1.2     glass /*
    646   1.2     glass  * dodumpdef - dump the specified definitions in the hash
    647   1.2     glass  *      table to stderr. If nothing is specified, the entire
    648   1.2     glass  *      hash table is dumped.
    649   1.2     glass  */
    650  1.14        tv static void
    651  1.20  christos dodump(const char *argv[], int argc)
    652   1.2     glass {
    653  1.10     lukem 	int n;
    654  1.20  christos 	struct macro_definition *p;
    655   1.2     glass 
    656   1.2     glass 	if (argc > 2) {
    657   1.2     glass 		for (n = 2; n < argc; n++)
    658  1.20  christos 			if ((p = lookup_macro_definition(argv[n])) != NULL)
    659  1.20  christos 				dump_one_def(argv[n], p);
    660  1.20  christos 	} else
    661  1.20  christos 		macro_for_all(dump_one_def);
    662   1.2     glass }
    663   1.2     glass 
    664   1.2     glass /*
    665  1.14        tv  * dotrace - mark some macros as traced/untraced depending upon on.
    666  1.14        tv  */
    667  1.14        tv static void
    668  1.20  christos dotrace(const char *argv[], int argc, int on)
    669  1.14        tv {
    670  1.14        tv 	int n;
    671  1.14        tv 
    672  1.14        tv 	if (argc > 2) {
    673  1.14        tv 		for (n = 2; n < argc; n++)
    674  1.14        tv 			mark_traced(argv[n], on);
    675  1.14        tv 	} else
    676  1.14        tv 		mark_traced(NULL, on);
    677  1.14        tv }
    678  1.14        tv 
    679  1.14        tv /*
    680   1.2     glass  * doifelse - select one of two alternatives - loop.
    681   1.2     glass  */
    682  1.14        tv static void
    683  1.20  christos doifelse(const char *argv[], int argc)
    684   1.2     glass {
    685   1.2     glass 	cycle {
    686   1.2     glass 		if (STREQ(argv[2], argv[3]))
    687   1.2     glass 			pbstr(argv[4]);
    688   1.2     glass 		else if (argc == 6)
    689   1.2     glass 			pbstr(argv[5]);
    690   1.2     glass 		else if (argc > 6) {
    691   1.2     glass 			argv += 3;
    692   1.2     glass 			argc -= 3;
    693   1.2     glass 			continue;
    694   1.2     glass 		}
    695   1.2     glass 		break;
    696   1.2     glass 	}
    697   1.2     glass }
    698   1.2     glass 
    699   1.2     glass /*
    700   1.2     glass  * doinclude - include a given file.
    701   1.2     glass  */
    702  1.14        tv static int
    703  1.20  christos doincl(const char *ifile)
    704   1.2     glass {
    705   1.2     glass 	if (ilevel + 1 == MAXINP)
    706  1.20  christos 		m4errx(1, "too many include files.");
    707  1.14        tv 	if (fopen_trypath(infile+ilevel+1, ifile) != NULL) {
    708   1.2     glass 		ilevel++;
    709   1.2     glass 		bbase[ilevel] = bufbase = bp;
    710   1.2     glass 		return (1);
    711  1.14        tv 	} else
    712   1.2     glass 		return (0);
    713   1.2     glass }
    714   1.2     glass 
    715   1.2     glass #ifdef EXTENDED
    716   1.2     glass /*
    717   1.2     glass  * dopaste - include a given file without any
    718   1.2     glass  *           macro processing.
    719   1.2     glass  */
    720  1.14        tv static int
    721  1.20  christos dopaste(const char *pfile)
    722   1.2     glass {
    723   1.2     glass 	FILE *pf;
    724  1.10     lukem 	int c;
    725   1.2     glass 
    726   1.2     glass 	if ((pf = fopen(pfile, "r")) != NULL) {
    727  1.20  christos 		if (synch_lines)
    728  1.20  christos 		    fprintf(active, "#line 1 \"%s\"\n", pfile);
    729   1.2     glass 		while ((c = getc(pf)) != EOF)
    730   1.2     glass 			putc(c, active);
    731   1.2     glass 		(void) fclose(pf);
    732  1.20  christos 		emit_synchline();
    733   1.2     glass 		return (1);
    734  1.14        tv 	} else
    735   1.2     glass 		return (0);
    736   1.2     glass }
    737   1.2     glass #endif
    738   1.2     glass 
    739  1.20  christos /*
    740  1.20  christos  * dochq - change quote characters
    741  1.20  christos  */
    742  1.14        tv static void
    743  1.20  christos dochq(const char *argv[], int ac)
    744  1.14        tv {
    745  1.14        tv 	if (ac == 2) {
    746  1.20  christos 		lquote[0] = LQUOTE; lquote[1] = EOS;
    747  1.20  christos 		rquote[0] = RQUOTE; rquote[1] = EOS;
    748  1.14        tv 	} else {
    749  1.14        tv 		strlcpy(lquote, argv[2], sizeof(lquote));
    750  1.20  christos 		if (ac > 3) {
    751  1.14        tv 			strlcpy(rquote, argv[3], sizeof(rquote));
    752  1.20  christos 		} else {
    753  1.20  christos 			rquote[0] = ECOMMT; rquote[1] = EOS;
    754  1.20  christos 		}
    755  1.14        tv 	}
    756  1.14        tv }
    757  1.14        tv 
    758   1.2     glass /*
    759  1.20  christos  * dochc - change comment characters
    760   1.2     glass  */
    761  1.14        tv static void
    762  1.20  christos dochc(const char *argv[], int argc)
    763   1.2     glass {
    764  1.20  christos /* XXX Note that there is no difference between no argument and a single
    765  1.20  christos  * empty argument.
    766  1.20  christos  */
    767  1.20  christos 	if (argc == 2) {
    768  1.14        tv 		scommt[0] = EOS;
    769  1.14        tv 		ecommt[0] = EOS;
    770  1.14        tv 	} else {
    771  1.20  christos 		strlcpy(scommt, argv[2], sizeof(scommt));
    772  1.20  christos 		if (argc == 3) {
    773  1.20  christos 			ecommt[0] = ECOMMT; ecommt[1] = EOS;
    774  1.20  christos 		} else {
    775  1.14        tv 			strlcpy(ecommt, argv[3], sizeof(ecommt));
    776  1.20  christos 		}
    777   1.2     glass 	}
    778   1.2     glass }
    779  1.20  christos 
    780   1.2     glass /*
    781  1.20  christos  * dom4wrap - expand text at EOF
    782   1.2     glass  */
    783  1.14        tv static void
    784  1.20  christos dom4wrap(const char *text)
    785   1.2     glass {
    786  1.20  christos 	if (wrapindex >= maxwraps) {
    787  1.20  christos 		if (maxwraps == 0)
    788  1.20  christos 			maxwraps = 16;
    789   1.2     glass 		else
    790  1.20  christos 			maxwraps *= 2;
    791  1.20  christos 		m4wraps = xrealloc(m4wraps, maxwraps * sizeof(*m4wraps),
    792  1.20  christos 		   "too many m4wraps");
    793   1.2     glass 	}
    794  1.20  christos 	m4wraps[wrapindex++] = xstrdup(text);
    795   1.2     glass }
    796   1.2     glass 
    797   1.2     glass /*
    798   1.2     glass  * dodivert - divert the output to a temporary file
    799   1.2     glass  */
    800  1.14        tv static void
    801  1.20  christos dodiv(int n)
    802   1.2     glass {
    803  1.14        tv 	int fd;
    804   1.9       cgd 
    805  1.14        tv 	oindex = n;
    806  1.14        tv 	if (n >= maxout) {
    807  1.14        tv 		if (mimic_gnu)
    808  1.14        tv 			resizedivs(n + 10);
    809  1.14        tv 		else
    810  1.14        tv 			n = 0;		/* bitbucket */
    811  1.14        tv     	}
    812   1.9       cgd 
    813  1.14        tv 	if (n < 0)
    814  1.14        tv 		n = 0;		       /* bitbucket */
    815  1.14        tv 	if (outfile[n] == NULL) {
    816  1.14        tv 		char fname[] = _PATH_DIVNAME;
    817  1.14        tv 
    818  1.14        tv 		if ((fd = mkstemp(fname)) < 0 ||
    819  1.14        tv 			(outfile[n] = fdopen(fd, "w+")) == NULL)
    820  1.14        tv 				err(1, "%s: cannot divert", fname);
    821  1.14        tv 		if (unlink(fname) == -1)
    822  1.14        tv 			err(1, "%s: cannot unlink", fname);
    823   1.2     glass 	}
    824  1.14        tv 	active = outfile[n];
    825   1.2     glass }
    826   1.2     glass 
    827   1.2     glass /*
    828   1.2     glass  * doundivert - undivert a specified output, or all
    829   1.2     glass  *              other outputs, in numerical order.
    830   1.2     glass  */
    831  1.14        tv static void
    832  1.20  christos doundiv(const char *argv[], int argc)
    833   1.2     glass {
    834  1.10     lukem 	int ind;
    835  1.10     lukem 	int n;
    836   1.2     glass 
    837   1.2     glass 	if (argc > 2) {
    838   1.2     glass 		for (ind = 2; ind < argc; ind++) {
    839  1.20  christos 			const char *errstr;
    840  1.20  christos 			n = strtonum(argv[ind], 1, INT_MAX, &errstr);
    841  1.20  christos 			if (errstr) {
    842  1.20  christos 				if (errno == EINVAL && mimic_gnu)
    843  1.20  christos 					getdivfile(argv[ind]);
    844  1.20  christos 			} else {
    845  1.20  christos 				if (n < maxout && outfile[n] != NULL)
    846  1.20  christos 					getdiv(n);
    847  1.20  christos 			}
    848   1.2     glass 		}
    849   1.2     glass 	}
    850   1.2     glass 	else
    851  1.14        tv 		for (n = 1; n < maxout; n++)
    852   1.2     glass 			if (outfile[n] != NULL)
    853   1.2     glass 				getdiv(n);
    854   1.2     glass }
    855   1.2     glass 
    856   1.2     glass /*
    857   1.2     glass  * dosub - select substring
    858   1.2     glass  */
    859  1.14        tv static void
    860  1.20  christos dosub(const char *argv[], int argc)
    861   1.2     glass {
    862  1.14        tv 	const char *ap, *fc, *k;
    863  1.10     lukem 	int nc;
    864   1.2     glass 
    865  1.14        tv 	ap = argv[2];		       /* target string */
    866   1.2     glass #ifdef EXPR
    867  1.14        tv 	fc = ap + expr(argv[3]);       /* first char */
    868   1.2     glass #else
    869  1.14        tv 	fc = ap + atoi(argv[3]);       /* first char */
    870   1.2     glass #endif
    871  1.14        tv 	nc = strlen(fc);
    872  1.14        tv 	if (argc >= 5)
    873   1.2     glass #ifdef EXPR
    874  1.14        tv 		nc = min(nc, expr(argv[4]));
    875   1.2     glass #else
    876  1.14        tv 		nc = min(nc, atoi(argv[4]));
    877   1.2     glass #endif
    878   1.2     glass 	if (fc >= ap && fc < ap + strlen(ap))
    879  1.14        tv 		for (k = fc + nc - 1; k >= fc; k--)
    880  1.20  christos 			pushback(*k);
    881   1.2     glass }
    882   1.2     glass 
    883   1.2     glass /*
    884   1.2     glass  * map:
    885   1.2     glass  * map every character of s1 that is specified in from
    886   1.2     glass  * into s3 and replace in s. (source s1 remains untouched)
    887   1.2     glass  *
    888   1.2     glass  * This is a standard implementation of map(s,from,to) function of ICON
    889   1.2     glass  * language. Within mapvec, we replace every character of "from" with
    890   1.2     glass  * the corresponding character in "to". If "to" is shorter than "from",
    891   1.2     glass  * than the corresponding entries are null, which means that those
    892   1.2     glass  * characters dissapear altogether. Furthermore, imagine
    893   1.2     glass  * map(dest, "sourcestring", "srtin", "rn..*") type call. In this case,
    894   1.2     glass  * `s' maps to `r', `r' maps to `n' and `n' maps to `*'. Thus, `s'
    895   1.2     glass  * ultimately maps to `*'. In order to achieve this effect in an efficient
    896   1.2     glass  * manner (i.e. without multiple passes over the destination string), we
    897   1.2     glass  * loop over mapvec, starting with the initial source character. if the
    898   1.2     glass  * character value (dch) in this location is different than the source
    899   1.2     glass  * character (sch), sch becomes dch, once again to index into mapvec, until
    900   1.2     glass  * the character value stabilizes (i.e. sch = dch, in other words
    901   1.2     glass  * mapvec[n] == n). Even if the entry in the mapvec is null for an ordinary
    902   1.2     glass  * character, it will stabilize, since mapvec[0] == 0 at all times. At the
    903   1.2     glass  * end, we restore mapvec* back to normal where mapvec[n] == n for
    904   1.2     glass  * 0 <= n <= 127. This strategy, along with the restoration of mapvec, is
    905   1.2     glass  * about 5 times faster than any algorithm that makes multiple passes over
    906   1.2     glass  * destination string.
    907   1.2     glass  */
    908  1.14        tv static void
    909  1.20  christos map(char *dest, const char *src, const char *from, const char *to)
    910  1.14        tv {
    911  1.14        tv 	const char *tmp;
    912  1.14        tv 	unsigned char sch, dch;
    913  1.14        tv 	static char frombis[257];
    914  1.14        tv 	static char tobis[257];
    915  1.14        tv 	static unsigned char mapvec[256] = {
    916  1.14        tv 	    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
    917  1.14        tv 	    19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
    918  1.14        tv 	    36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
    919  1.14        tv 	    53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
    920  1.14        tv 	    70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
    921  1.14        tv 	    87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102,
    922  1.14        tv 	    103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
    923  1.14        tv 	    116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128,
    924  1.14        tv 	    129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141,
    925  1.14        tv 	    142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154,
    926  1.14        tv 	    155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167,
    927  1.14        tv 	    168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180,
    928  1.14        tv 	    181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193,
    929  1.14        tv 	    194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206,
    930  1.14        tv 	    207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
    931  1.14        tv 	    220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232,
    932  1.14        tv 	    233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245,
    933  1.14        tv 	    246, 247, 248, 249, 250, 251, 252, 253, 254, 255
    934   1.2     glass 	};
    935   1.2     glass 
    936   1.2     glass 	if (*src) {
    937  1.14        tv 		if (mimic_gnu) {
    938  1.14        tv 			/*
    939  1.14        tv 			 * expand character ranges on the fly
    940  1.14        tv 			 */
    941  1.14        tv 			from = handledash(frombis, frombis + 256, from);
    942  1.14        tv 			to = handledash(tobis, tobis + 256, to);
    943  1.14        tv 		}
    944   1.2     glass 		tmp = from;
    945   1.2     glass 	/*
    946   1.2     glass 	 * create a mapping between "from" and
    947   1.2     glass 	 * "to"
    948   1.2     glass 	 */
    949   1.2     glass 		while (*from)
    950  1.14        tv 			mapvec[(unsigned char)(*from++)] = (*to) ?
    951  1.14        tv 				(unsigned char)(*to++) : 0;
    952   1.2     glass 
    953   1.2     glass 		while (*src) {
    954  1.14        tv 			sch = (unsigned char)(*src++);
    955  1.14        tv 			dch = mapvec[sch];
    956   1.2     glass 			while (dch != sch) {
    957   1.2     glass 				sch = dch;
    958  1.14        tv 				dch = mapvec[sch];
    959   1.2     glass 			}
    960  1.14        tv 			if ((*dest = (char)dch))
    961   1.2     glass 				dest++;
    962   1.2     glass 		}
    963   1.2     glass 	/*
    964   1.2     glass 	 * restore all the changed characters
    965   1.2     glass 	 */
    966   1.2     glass 		while (*tmp) {
    967  1.14        tv 			mapvec[(unsigned char)(*tmp)] = (unsigned char)(*tmp);
    968   1.2     glass 			tmp++;
    969   1.2     glass 		}
    970   1.2     glass 	}
    971  1.14        tv 	*dest = '\0';
    972  1.14        tv }
    973  1.14        tv 
    974  1.14        tv 
    975  1.14        tv /*
    976  1.14        tv  * handledash:
    977  1.14        tv  *  use buffer to copy the src string, expanding character ranges
    978  1.14        tv  * on the way.
    979  1.14        tv  */
    980  1.14        tv static const char *
    981  1.20  christos handledash(char *buffer, char *end, const char *src)
    982  1.14        tv {
    983  1.14        tv 	char *p;
    984  1.14        tv 
    985  1.14        tv 	p = buffer;
    986  1.14        tv 	while(*src) {
    987  1.14        tv 		if (src[1] == '-' && src[2]) {
    988  1.14        tv 			unsigned char i;
    989  1.20  christos 			if ((unsigned char)src[0] <= (unsigned char)src[2]) {
    990  1.20  christos 				for (i = (unsigned char)src[0];
    991  1.20  christos 				    i <= (unsigned char)src[2]; i++) {
    992  1.20  christos 					*p++ = i;
    993  1.20  christos 					if (p == end) {
    994  1.20  christos 						*p = '\0';
    995  1.20  christos 						return buffer;
    996  1.20  christos 					}
    997  1.20  christos 				}
    998  1.20  christos 			} else {
    999  1.20  christos 				for (i = (unsigned char)src[0];
   1000  1.20  christos 				    i >= (unsigned char)src[2]; i--) {
   1001  1.20  christos 					*p++ = i;
   1002  1.20  christos 					if (p == end) {
   1003  1.20  christos 						*p = '\0';
   1004  1.20  christos 						return buffer;
   1005  1.20  christos 					}
   1006  1.14        tv 				}
   1007  1.14        tv 			}
   1008  1.14        tv 			src += 3;
   1009  1.14        tv 		} else
   1010  1.14        tv 			*p++ = *src++;
   1011  1.14        tv 		if (p == end)
   1012  1.14        tv 			break;
   1013  1.14        tv 	}
   1014  1.14        tv 	*p = '\0';
   1015  1.14        tv 	return buffer;
   1016   1.1       cgd }
   1017