1
0
mirror of https://github.com/openbsd/src.git synced 2025-01-10 06:47:55 -08:00

Implement -I option and M4PATH environment variable

This commit is contained in:
espie 1999-09-14 08:21:32 +00:00
parent 6728c2c060
commit 4a7693888e
7 changed files with 173 additions and 14 deletions

View File

@ -1,11 +1,11 @@
# $OpenBSD: Makefile,v 1.3 1997/09/21 11:49:47 deraadt Exp $
# $OpenBSD: Makefile,v 1.4 1999/09/14 08:21:32 espie Exp $
# -DEXTENDED
# if you want the paste & spaste macros.
PROG= m4
CFLAGS+=-DEXTENDED
SRCS= eval.c expr.c look.c main.c misc.c
SRCS= eval.c expr.c look.c main.c misc.c gnum4.c
MAN= m4.1
.include <bsd.prog.mk>

View File

@ -1,4 +1,4 @@
/* $OpenBSD: eval.c,v 1.14 1999/09/06 13:24:59 espie Exp $ */
/* $OpenBSD: eval.c,v 1.15 1999/09/14 08:21:36 espie Exp $ */
/* $NetBSD: eval.c,v 1.7 1996/11/10 21:21:29 pk Exp $ */
/*
@ -41,7 +41,7 @@
#if 0
static char sccsid[] = "@(#)eval.c 8.2 (Berkeley) 4/27/95";
#else
static char rcsid[] = "$OpenBSD: eval.c,v 1.14 1999/09/06 13:24:59 espie Exp $";
static char rcsid[] = "$OpenBSD: eval.c,v 1.15 1999/09/14 08:21:36 espie Exp $";
#endif
#endif /* not lint */
@ -576,7 +576,7 @@ char *ifile;
{
if (ilevel + 1 == MAXINP)
errx(1, "too many include files.");
if ((infile[ilevel + 1] = fopen(ifile, "r")) != NULL) {
if ((infile[ilevel + 1] = fopen_trypath(ifile)) != NULL) {
ilevel++;
bbase[ilevel] = bufbase = bp;
return (1);

View File

@ -1,4 +1,4 @@
/* $OpenBSD: extern.h,v 1.8 1999/09/06 13:29:32 espie Exp $ */
/* $OpenBSD: extern.h,v 1.9 1999/09/14 08:21:36 espie Exp $ */
/* $NetBSD: extern.h,v 1.3 1996/01/13 23:25:24 pk Exp $ */
/*-
@ -93,3 +93,7 @@ extern char *m4wraps; /* m4wrap string default. */
extern char *null; /* as it says.. just a null. */
extern char rquote[]; /* right quote character (') */
extern char scommt[]; /* start character for comment */
/* gnum4.c */
extern FILE *fopen_trypath __P((const char *filename));
extern void addtoincludepath __P((const char *dirname));

145
usr.bin/m4/gnum4.c Normal file
View File

@ -0,0 +1,145 @@
/* $OpenBSD: gnum4.c,v 1.1 1999/09/14 08:21:36 espie Exp $ */
/*
* Copyright (c) 1999 Marc Espie
*
* 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.
*
* 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.
*/
/*
* functions needed to support gnu-m4 extensions, including a fake freezing
*/
#include <sys/param.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <err.h>
#include "mdef.h"
#include "stdd.h"
#include "extern.h"
/*
* Support for include path search
* First search in the the current directory.
* If not found, and the path is not absolute, include path kicks in.
* First, -I options, in the order found on the command line.
* Then M4PATH env variable
*/
struct path_entry {
char *name;
struct path_entry *next;
} *first, *last;
static struct path_entry *
new_path_entry(dirname)
char *dirname;
{
struct path_entry *n;
n = malloc(sizeof(struct path_entry));
if (!n)
errx(1, "out of memory");
n->name = strdup(dirname);
if (!n->name)
errx(1, "out of memory");
n->next = 0;
return n;
}
void
addtoincludepath(dirname)
const char *dirname;
{
struct path_entry *n;
n = new_path_entry(dirname);
if (last) {
last->next = n;
last = n;
}
else
last = first = n;
}
static void
ensure_m4path()
{
static int envpathdone = 0;
char *envpath;
char *sweep;
char *path;
if (envpathdone)
return;
envpathdone = TRUE;
envpath = getenv("M4PATH");
if (!envpath)
return;
/* for portability: getenv result is read-only */
envpath = strdup(envpath);
if (!envpath)
errx(1, "out of memory");
for (sweep = envpath;
(path = strsep(&sweep, ":")) != NULL;)
addtoincludepath(path);
free(envpath);
}
static
FILE *
dopath(const char *filename)
{
char path[MAXPATHLEN];
struct path_entry *pe;
FILE *file;
for (pe = first; pe; pe = pe->next) {
snprintf(path, sizeof(path), "%s/%s", pe->name, filename);
if ((file = fopen(path, "r")) != 0)
return file;
}
return NULL;
}
FILE *
fopen_trypath(filename)
const char *filename;
{
FILE *f;
f = fopen(filename, "r");
if (f)
return f;
if (filename[0] == '/')
return NULL;
ensure_m4path();
return dopath(filename);
}

View File

@ -1,4 +1,4 @@
.\" @(#) $OpenBSD: m4.1,v 1.6 1999/09/06 13:07:17 espie Exp $
.\" @(#) $OpenBSD: m4.1,v 1.7 1999/09/14 08:21:37 espie Exp $
.\"
.\"
.Dd January 26, 1993
@ -13,6 +13,7 @@
.Fl D Ns Ar name Ns Op Ar =value
.Oc
.Op Fl U Ns Ar name
.Op Fl I Ar dirname
.Sh DESCRIPTION
The
.Nm m4
@ -50,7 +51,10 @@ to have some value (or NULL).
.It Fl "U" Ns Ar "name"
Undefine the symbol
.Ar name .
.El
.It Fl I Ar "dirname"
Add directory
.Ar dirname
to the include path.
.Sh SYNTAX
.Nm m4
provides the following built-in macros. They may be
@ -120,6 +124,12 @@ zero or one arguments left, either this last argument or NULL is
returned if no other matches were found.
.It Ic include
Returns the contents of the file specified in the first argument.
If the file is not found as is, look through the include path:
first the directories specified with
.Fl I
on the command line, then the environment variable
.Va M4PATH ,
as a colon-separated list of directories.
Include aborts with an error message if the file cannot be included.
.It Ic incr
Increments the argument by 1. The argument must be a valid numeric string.

View File

@ -1,4 +1,4 @@
/* $OpenBSD: main.c,v 1.14 1999/09/06 13:29:32 espie Exp $ */
/* $OpenBSD: main.c,v 1.15 1999/09/14 08:21:37 espie Exp $ */
/* $NetBSD: main.c,v 1.12 1997/02/08 23:54:49 cgd Exp $ */
/*-
@ -47,7 +47,7 @@ static char copyright[] =
#if 0
static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
#else
static char rcsid[] = "$OpenBSD: main.c,v 1.14 1999/09/06 13:29:32 espie Exp $";
static char rcsid[] = "$OpenBSD: main.c,v 1.15 1999/09/14 08:21:37 espie Exp $";
#endif
#endif /* not lint */
@ -192,7 +192,7 @@ main(argc,argv)
p = *argv;
if (p[0] == '-' && p[1] == EOS)
ifp = stdin;
else if ((ifp = fopen(p, "r")) == NULL)
else if ((ifp = fopen_trypath(p)) == NULL)
err(1, "%s", p);
sp = -1;
fp = 0;

View File

@ -1,4 +1,4 @@
/* $OpenBSD: misc.c,v 1.10 1999/09/09 22:18:19 espie Exp $ */
/* $OpenBSD: misc.c,v 1.11 1999/09/14 08:21:37 espie Exp $ */
/* $NetBSD: misc.c,v 1.6 1995/09/28 05:37:41 tls Exp $ */
/*
@ -41,7 +41,7 @@
#if 0
static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 6/6/93";
#else
static char rcsid[] = "$OpenBSD: misc.c,v 1.10 1999/09/09 22:18:19 espie Exp $";
static char rcsid[] = "$OpenBSD: misc.c,v 1.11 1999/09/14 08:21:37 espie Exp $";
#endif
#endif /* not lint */
@ -290,7 +290,7 @@ const char *s;
void
usage()
{
fprintf(stderr, "usage: m4 [-Dname[=val]] [-Uname]\n");
fprintf(stderr, "usage: m4 [-Dname[=val]] [-Uname] [-I dirname...]\n");
exit(1);
}