Home | History | Annotate | Line # | Download | only in m4
misc.c revision 1.9
      1  1.9    cgd /*	$NetBSD: misc.c,v 1.9 1997/12/29 19:52:57 cgd Exp $	*/
      2  1.6    tls 
      3  1.5  glass /*
      4  1.5  glass  * Copyright (c) 1989, 1993
      5  1.5  glass  *	The Regents of the University of California.  All rights reserved.
      6  1.5  glass  *
      7  1.5  glass  * This code is derived from software contributed to Berkeley by
      8  1.5  glass  * Ozan Yigit at York University.
      9  1.5  glass  *
     10  1.5  glass  * Redistribution and use in source and binary forms, with or without
     11  1.5  glass  * modification, are permitted provided that the following conditions
     12  1.5  glass  * are met:
     13  1.5  glass  * 1. Redistributions of source code must retain the above copyright
     14  1.5  glass  *    notice, this list of conditions and the following disclaimer.
     15  1.5  glass  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.5  glass  *    notice, this list of conditions and the following disclaimer in the
     17  1.5  glass  *    documentation and/or other materials provided with the distribution.
     18  1.5  glass  * 3. All advertising materials mentioning features or use of this software
     19  1.5  glass  *    must display the following acknowledgement:
     20  1.5  glass  *	This product includes software developed by the University of
     21  1.5  glass  *	California, Berkeley and its contributors.
     22  1.5  glass  * 4. Neither the name of the University nor the names of its contributors
     23  1.5  glass  *    may be used to endorse or promote products derived from this software
     24  1.5  glass  *    without specific prior written permission.
     25  1.5  glass  *
     26  1.5  glass  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.5  glass  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.5  glass  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.5  glass  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.5  glass  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.5  glass  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.5  glass  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.5  glass  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.5  glass  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.5  glass  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.5  glass  * SUCH DAMAGE.
     37  1.5  glass  */
     38  1.5  glass 
     39  1.7  lukem #include <sys/cdefs.h>
     40  1.5  glass #ifndef lint
     41  1.6    tls #if 0
     42  1.5  glass static char sccsid[] = "@(#)misc.c	8.1 (Berkeley) 6/6/93";
     43  1.6    tls #else
     44  1.9    cgd __RCSID("$NetBSD: misc.c,v 1.9 1997/12/29 19:52:57 cgd Exp $");
     45  1.6    tls #endif
     46  1.5  glass #endif /* not lint */
     47  1.5  glass 
     48  1.5  glass #include <sys/types.h>
     49  1.7  lukem #include <err.h>
     50  1.5  glass #include <errno.h>
     51  1.5  glass #include <unistd.h>
     52  1.5  glass #include <stdio.h>
     53  1.5  glass #include <stdlib.h>
     54  1.5  glass #include <string.h>
     55  1.1    cgd #include "mdef.h"
     56  1.5  glass #include "stdd.h"
     57  1.5  glass #include "extern.h"
     58  1.5  glass #include "pathnames.h"
     59  1.5  glass 
     60  1.5  glass /*
     61  1.5  glass  * find the index of second str in the first str.
     62  1.5  glass  */
     63  1.5  glass int
     64  1.5  glass indx(s1, s2)
     65  1.7  lukem 	char *s1;
     66  1.7  lukem 	char *s2;
     67  1.5  glass {
     68  1.7  lukem 	char *t;
     69  1.7  lukem 
     70  1.7  lukem 	t = strstr(s1, s2);
     71  1.7  lukem 	if (t == NULL)
     72  1.7  lukem 		return (-1);
     73  1.7  lukem 	return (t - s1);
     74  1.5  glass }
     75  1.7  lukem 
     76  1.5  glass /*
     77  1.5  glass  *  putback - push character back onto input
     78  1.5  glass  */
     79  1.5  glass void
     80  1.5  glass putback(c)
     81  1.9    cgd 	unsigned char c;
     82  1.5  glass {
     83  1.5  glass 	if (bp < endpbb)
     84  1.5  glass 		*bp++ = c;
     85  1.5  glass 	else
     86  1.7  lukem 		errx(1, "too many characters pushed back");
     87  1.5  glass }
     88  1.5  glass 
     89  1.5  glass /*
     90  1.9    cgd  *  putbackeof - push EOF back onto input
     91  1.9    cgd  */
     92  1.9    cgd void
     93  1.9    cgd putbackeof()
     94  1.9    cgd {
     95  1.9    cgd 	if (bp < endpbb)
     96  1.9    cgd 		*bp++ = EOF;
     97  1.9    cgd 	else
     98  1.9    cgd 		errx(1, "too many characters pushed back");
     99  1.9    cgd }
    100  1.9    cgd 
    101  1.9    cgd /*
    102  1.5  glass  *  pbstr - push string back onto input
    103  1.5  glass  *          putback is replicated to improve
    104  1.5  glass  *          performance.
    105  1.5  glass  */
    106  1.5  glass void
    107  1.5  glass pbstr(s)
    108  1.7  lukem 	char *s;
    109  1.5  glass {
    110  1.7  lukem 	char *es;
    111  1.8    cgd 	pbent *zp;
    112  1.2  glass 
    113  1.5  glass 	es = s;
    114  1.2  glass 	zp = bp;
    115  1.1    cgd 
    116  1.5  glass 	while (*es)
    117  1.5  glass 		es++;
    118  1.5  glass 	es--;
    119  1.5  glass 	while (es >= s)
    120  1.5  glass 		if (zp < endpbb)
    121  1.9    cgd 			*zp++ = (unsigned char)*es--;
    122  1.5  glass 	if ((bp = zp) == endpbb)
    123  1.7  lukem 		errx(1, "too many characters pushed back");
    124  1.5  glass }
    125  1.5  glass 
    126  1.5  glass /*
    127  1.5  glass  *  pbnum - convert number to string, push back on input.
    128  1.5  glass  */
    129  1.5  glass void
    130  1.5  glass pbnum(n)
    131  1.7  lukem 	int n;
    132  1.5  glass {
    133  1.7  lukem 	int num;
    134  1.2  glass 
    135  1.5  glass 	num = (n < 0) ? -n : n;
    136  1.2  glass 	do {
    137  1.5  glass 		putback(num % 10 + '0');
    138  1.2  glass 	}
    139  1.5  glass 	while ((num /= 10) > 0);
    140  1.2  glass 
    141  1.5  glass 	if (n < 0)
    142  1.5  glass 		putback('-');
    143  1.5  glass }
    144  1.5  glass 
    145  1.5  glass /*
    146  1.5  glass  *  chrsave - put single char on string space
    147  1.5  glass  */
    148  1.5  glass void
    149  1.5  glass chrsave(c)
    150  1.7  lukem 	char c;
    151  1.5  glass {
    152  1.5  glass 	if (ep < endest)
    153  1.5  glass 		*ep++ = c;
    154  1.5  glass 	else
    155  1.7  lukem 		errx(1, "string space overflow");
    156  1.5  glass }
    157  1.5  glass 
    158  1.5  glass /*
    159  1.5  glass  * read in a diversion file, and dispose it.
    160  1.5  glass  */
    161  1.5  glass void
    162  1.5  glass getdiv(n)
    163  1.7  lukem 	int n;
    164  1.5  glass {
    165  1.7  lukem 	int c;
    166  1.7  lukem 	FILE *dfil;
    167  1.2  glass 
    168  1.5  glass 	if (active == outfile[n])
    169  1.7  lukem 		errx(1, "undivert: diversion still active");
    170  1.5  glass 	(void) fclose(outfile[n]);
    171  1.5  glass 	outfile[n] = NULL;
    172  1.5  glass 	m4temp[UNIQUE] = n + '0';
    173  1.2  glass 	if ((dfil = fopen(m4temp, "r")) == NULL)
    174  1.7  lukem 		err(1, "%s: cannot undivert", m4temp);
    175  1.5  glass 	else
    176  1.5  glass 		while ((c = getc(dfil)) != EOF)
    177  1.5  glass 			putc(c, active);
    178  1.2  glass 	(void) fclose(dfil);
    179  1.2  glass 
    180  1.5  glass #ifdef vms
    181  1.5  glass 	if (remove(m4temp))
    182  1.2  glass #else
    183  1.5  glass 	if (unlink(m4temp) == -1)
    184  1.2  glass #endif
    185  1.7  lukem 		err(1, "%s: cannot unlink", m4temp);
    186  1.5  glass }
    187  1.2  glass 
    188  1.5  glass void
    189  1.5  glass onintr(signo)
    190  1.5  glass 	int signo;
    191  1.5  glass {
    192  1.7  lukem 	errx(1, "interrupted");
    193  1.5  glass }
    194  1.5  glass 
    195  1.5  glass /*
    196  1.5  glass  * killdiv - get rid of the diversion files
    197  1.5  glass  */
    198  1.5  glass void
    199  1.5  glass killdiv()
    200  1.5  glass {
    201  1.7  lukem 	int n;
    202  1.2  glass 
    203  1.5  glass 	for (n = 0; n < MAXOUT; n++)
    204  1.5  glass 		if (outfile[n] != NULL) {
    205  1.5  glass 			(void) fclose(outfile[n]);
    206  1.5  glass 			m4temp[UNIQUE] = n + '0';
    207  1.5  glass #ifdef vms
    208  1.5  glass 			(void) remove(m4temp);
    209  1.2  glass #else
    210  1.5  glass 			(void) unlink(m4temp);
    211  1.2  glass #endif
    212  1.5  glass 		}
    213  1.5  glass }
    214  1.2  glass 
    215  1.5  glass char *
    216  1.5  glass xalloc(n)
    217  1.7  lukem 	unsigned long n;
    218  1.5  glass {
    219  1.7  lukem 	char *p = malloc(n);
    220  1.2  glass 
    221  1.5  glass 	if (p == NULL)
    222  1.7  lukem 		err(1, "malloc");
    223  1.5  glass 	return p;
    224  1.5  glass }
    225  1.2  glass 
    226  1.5  glass char *
    227  1.5  glass xstrdup(s)
    228  1.7  lukem 	const char *s;
    229  1.5  glass {
    230  1.7  lukem 	char *p = strdup(s);
    231  1.5  glass 	if (p == NULL)
    232  1.7  lukem 		err(1, "strdup");
    233  1.5  glass 	return p;
    234  1.5  glass }
    235  1.2  glass 
    236  1.5  glass char *
    237  1.5  glass basename(s)
    238  1.7  lukem 	char *s;
    239  1.5  glass {
    240  1.7  lukem 	char *p;
    241  1.2  glass 
    242  1.5  glass 	if ((p = strrchr(s, '/')) == NULL)
    243  1.5  glass 		return s;
    244  1.2  glass 
    245  1.5  glass 	return ++p;
    246  1.5  glass }
    247  1.2  glass 
    248  1.5  glass void
    249  1.5  glass usage()
    250  1.5  glass {
    251  1.5  glass 	fprintf(stderr, "usage: m4 [-Dname[=val]] [-Uname]\n");
    252  1.2  glass 	exit(1);
    253  1.5  glass }
    254