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