2001-03-05 23:19:54 +03:00
|
|
|
/* $NetBSD: misc.c,v 1.11 2001/03/05 20:19:54 wiz Exp $ */
|
1995-09-28 08:37:28 +03:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Ozan Yigit at York University.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-19 08:39:51 +04:00
|
|
|
#include <sys/cdefs.h>
|
1994-06-19 07:58:22 +04:00
|
|
|
#ifndef lint
|
1995-09-28 08:37:28 +03:00
|
|
|
#if 0
|
1994-06-19 07:58:22 +04:00
|
|
|
static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 6/6/93";
|
1995-09-28 08:37:28 +03:00
|
|
|
#else
|
2001-03-05 23:19:54 +03:00
|
|
|
__RCSID("$NetBSD: misc.c,v 1.11 2001/03/05 20:19:54 wiz Exp $");
|
1995-09-28 08:37:28 +03:00
|
|
|
#endif
|
1994-06-19 07:58:22 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
1997-10-19 08:39:51 +04:00
|
|
|
#include <err.h>
|
1994-06-19 07:58:22 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "mdef.h"
|
1994-06-19 07:58:22 +04:00
|
|
|
#include "stdd.h"
|
|
|
|
#include "extern.h"
|
|
|
|
#include "pathnames.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* find the index of second str in the first str.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
indx(s1, s2)
|
1997-10-19 08:39:51 +04:00
|
|
|
char *s1;
|
|
|
|
char *s2;
|
1994-06-19 07:58:22 +04:00
|
|
|
{
|
1997-10-19 08:39:51 +04:00
|
|
|
char *t;
|
1993-06-19 01:50:26 +04:00
|
|
|
|
1997-10-19 08:39:51 +04:00
|
|
|
t = strstr(s1, s2);
|
|
|
|
if (t == NULL)
|
|
|
|
return (-1);
|
|
|
|
return (t - s1);
|
1994-06-19 07:58:22 +04:00
|
|
|
}
|
1997-10-19 08:39:51 +04:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
/*
|
|
|
|
* putback - push character back onto input
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
putback(c)
|
2001-03-05 23:19:54 +03:00
|
|
|
int c;
|
1994-06-19 07:58:22 +04:00
|
|
|
{
|
|
|
|
if (bp < endpbb)
|
|
|
|
*bp++ = c;
|
|
|
|
else
|
1997-10-19 08:39:51 +04:00
|
|
|
errx(1, "too many characters pushed back");
|
1994-06-19 07:58:22 +04:00
|
|
|
}
|
|
|
|
|
1997-12-29 22:52:55 +03:00
|
|
|
/*
|
|
|
|
* putbackeof - push EOF back onto input
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
putbackeof()
|
|
|
|
{
|
|
|
|
if (bp < endpbb)
|
|
|
|
*bp++ = EOF;
|
|
|
|
else
|
|
|
|
errx(1, "too many characters pushed back");
|
|
|
|
}
|
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
/*
|
|
|
|
* pbstr - push string back onto input
|
|
|
|
* putback is replicated to improve
|
|
|
|
* performance.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pbstr(s)
|
1997-10-19 08:39:51 +04:00
|
|
|
char *s;
|
1994-06-19 07:58:22 +04:00
|
|
|
{
|
1997-10-19 08:39:51 +04:00
|
|
|
char *es;
|
1997-12-03 01:34:00 +03:00
|
|
|
pbent *zp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
es = s;
|
1993-03-21 12:45:37 +03:00
|
|
|
zp = bp;
|
1993-06-19 01:50:26 +04:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
while (*es)
|
|
|
|
es++;
|
|
|
|
es--;
|
|
|
|
while (es >= s)
|
|
|
|
if (zp < endpbb)
|
1997-12-29 22:52:55 +03:00
|
|
|
*zp++ = (unsigned char)*es--;
|
1994-06-19 07:58:22 +04:00
|
|
|
if ((bp = zp) == endpbb)
|
1997-10-19 08:39:51 +04:00
|
|
|
errx(1, "too many characters pushed back");
|
1994-06-19 07:58:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pbnum - convert number to string, push back on input.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pbnum(n)
|
1997-10-19 08:39:51 +04:00
|
|
|
int n;
|
1994-06-19 07:58:22 +04:00
|
|
|
{
|
1997-10-19 08:39:51 +04:00
|
|
|
int num;
|
1993-06-19 01:50:26 +04:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
num = (n < 0) ? -n : n;
|
1993-06-19 01:50:26 +04:00
|
|
|
do {
|
1994-06-19 07:58:22 +04:00
|
|
|
putback(num % 10 + '0');
|
1993-06-19 01:50:26 +04:00
|
|
|
}
|
1994-06-19 07:58:22 +04:00
|
|
|
while ((num /= 10) > 0);
|
|
|
|
|
|
|
|
if (n < 0)
|
|
|
|
putback('-');
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* chrsave - put single char on string space
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
chrsave(c)
|
1997-10-19 08:39:51 +04:00
|
|
|
char c;
|
1994-06-19 07:58:22 +04:00
|
|
|
{
|
|
|
|
if (ep < endest)
|
|
|
|
*ep++ = c;
|
|
|
|
else
|
1997-10-19 08:39:51 +04:00
|
|
|
errx(1, "string space overflow");
|
1994-06-19 07:58:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* read in a diversion file, and dispose it.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
getdiv(n)
|
1997-10-19 08:39:51 +04:00
|
|
|
int n;
|
1994-06-19 07:58:22 +04:00
|
|
|
{
|
1997-10-19 08:39:51 +04:00
|
|
|
int c;
|
|
|
|
FILE *dfil;
|
1993-06-19 01:50:26 +04:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
if (active == outfile[n])
|
1997-10-19 08:39:51 +04:00
|
|
|
errx(1, "undivert: diversion still active");
|
1994-06-19 07:58:22 +04:00
|
|
|
(void) fclose(outfile[n]);
|
|
|
|
outfile[n] = NULL;
|
|
|
|
m4temp[UNIQUE] = n + '0';
|
1993-06-19 01:50:26 +04:00
|
|
|
if ((dfil = fopen(m4temp, "r")) == NULL)
|
1997-10-19 08:39:51 +04:00
|
|
|
err(1, "%s: cannot undivert", m4temp);
|
1994-06-19 07:58:22 +04:00
|
|
|
else
|
|
|
|
while ((c = getc(dfil)) != EOF)
|
|
|
|
putc(c, active);
|
1993-06-19 01:50:26 +04:00
|
|
|
(void) fclose(dfil);
|
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
#ifdef vms
|
|
|
|
if (remove(m4temp))
|
1993-06-19 01:50:26 +04:00
|
|
|
#else
|
1994-06-19 07:58:22 +04:00
|
|
|
if (unlink(m4temp) == -1)
|
1993-06-19 01:50:26 +04:00
|
|
|
#endif
|
1997-10-19 08:39:51 +04:00
|
|
|
err(1, "%s: cannot unlink", m4temp);
|
1994-06-19 07:58:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
onintr(signo)
|
|
|
|
int signo;
|
|
|
|
{
|
1997-10-19 08:39:51 +04:00
|
|
|
errx(1, "interrupted");
|
1994-06-19 07:58:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* killdiv - get rid of the diversion files
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
killdiv()
|
|
|
|
{
|
1997-10-19 08:39:51 +04:00
|
|
|
int n;
|
1993-06-19 01:50:26 +04:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
for (n = 0; n < MAXOUT; n++)
|
|
|
|
if (outfile[n] != NULL) {
|
|
|
|
(void) fclose(outfile[n]);
|
|
|
|
m4temp[UNIQUE] = n + '0';
|
|
|
|
#ifdef vms
|
|
|
|
(void) remove(m4temp);
|
1993-06-19 01:50:26 +04:00
|
|
|
#else
|
1994-06-19 07:58:22 +04:00
|
|
|
(void) unlink(m4temp);
|
1993-06-19 01:50:26 +04:00
|
|
|
#endif
|
1994-06-19 07:58:22 +04:00
|
|
|
}
|
|
|
|
}
|
1993-06-19 01:50:26 +04:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
char *
|
|
|
|
xalloc(n)
|
1997-10-19 08:39:51 +04:00
|
|
|
unsigned long n;
|
1994-06-19 07:58:22 +04:00
|
|
|
{
|
1997-10-19 08:39:51 +04:00
|
|
|
char *p = malloc(n);
|
1993-06-19 01:50:26 +04:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
if (p == NULL)
|
1997-10-19 08:39:51 +04:00
|
|
|
err(1, "malloc");
|
1994-06-19 07:58:22 +04:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
xstrdup(s)
|
1997-10-19 08:39:51 +04:00
|
|
|
const char *s;
|
1994-06-19 07:58:22 +04:00
|
|
|
{
|
1997-10-19 08:39:51 +04:00
|
|
|
char *p = strdup(s);
|
1994-06-19 07:58:22 +04:00
|
|
|
if (p == NULL)
|
1997-10-19 08:39:51 +04:00
|
|
|
err(1, "strdup");
|
1994-06-19 07:58:22 +04:00
|
|
|
return p;
|
|
|
|
}
|
1993-06-19 01:50:26 +04:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
char *
|
|
|
|
basename(s)
|
1997-10-19 08:39:51 +04:00
|
|
|
char *s;
|
1994-06-19 07:58:22 +04:00
|
|
|
{
|
1997-10-19 08:39:51 +04:00
|
|
|
char *p;
|
1993-06-19 01:50:26 +04:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
if ((p = strrchr(s, '/')) == NULL)
|
|
|
|
return s;
|
1993-06-19 01:50:26 +04:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
return ++p;
|
|
|
|
}
|
1993-06-19 01:50:26 +04:00
|
|
|
|
1994-06-19 07:58:22 +04:00
|
|
|
void
|
|
|
|
usage()
|
|
|
|
{
|
2000-05-08 04:52:18 +04:00
|
|
|
fprintf(stderr, "usage: m4 [-P] [-Dname[=val]] [-Uname]\n");
|
1993-06-19 01:50:26 +04:00
|
|
|
exit(1);
|
1994-06-19 07:58:22 +04:00
|
|
|
}
|