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

Use a proper abstract interface for file accesses, so that we can record

the file name and line number.

This yields more meaningful error messages, and the possibility for yet
more.
This commit is contained in:
espie 2000-01-12 17:49:53 +00:00
parent ad0c6836c2
commit 0d3ffe1d03
6 changed files with 99 additions and 43 deletions

View File

@ -1,4 +1,4 @@
/* $OpenBSD: eval.c,v 1.23 2000/01/05 16:06:14 espie Exp $ */
/* $OpenBSD: eval.c,v 1.24 2000/01/12 17:49:53 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.23 2000/01/05 16:06:14 espie Exp $";
static char rcsid[] = "$OpenBSD: eval.c,v 1.24 2000/01/12 17:49:53 espie Exp $";
#endif
#endif /* not lint */
@ -112,7 +112,8 @@ eval(argv, argc, td)
#endif
if (td & RECDEF)
errx(1, "expanding recursive definition for %s", argv[1]);
errx(1, "%s at line %lu: expanding recursive definition for %s",
CURRENT_NAME, CURRENT_LINE, argv[1]);
/*
* if argc == 3 and argv[2] is null, then we
* have macro-or-builtin() type call. We adjust
@ -211,7 +212,8 @@ eval(argv, argc, td)
case INCLTYPE:
if (argc > 2)
if (!doincl(argv[2]))
err(1, "%s", argv[2]);
err(1, "%s at line %lu: include(%s)",
CURRENT_NAME, CURRENT_LINE, argv[2]);
break;
case SINCTYPE:
@ -222,7 +224,8 @@ eval(argv, argc, td)
case PASTTYPE:
if (argc > 2)
if (!dopaste(argv[2]))
err(1, "%s", argv[2]);
err(1, "%s at line %lu: paste(%s)",
CURRENT_NAME, CURRENT_LINE, argv[2]);
break;
case SPASTYPE:
@ -320,7 +323,9 @@ eval(argv, argc, td)
fd = mkstemp(temp);
if (fd == -1)
err(1, "couldn't make temp file %s", argv[2]);
err(1,
"%s at line %lu: couldn't make temp file %s",
CURRENT_NAME, CURRENT_LINE, argv[2]);
close(fd);
pbstr(temp);
free(temp);
@ -403,7 +408,8 @@ eval(argv, argc, td)
pbstr(lquote);
break;
default:
errx(1, "eval: major botch.");
errx(1, "%s at line %lu: eval: major botch.",
CURRENT_NAME, CURRENT_LINE);
break;
}
}
@ -492,7 +498,8 @@ dodefine(name, defn)
ndptr p;
if (!*name)
errx(1, "null definition.");
errx(1, "%s at line %lu: null definition.", CURRENT_NAME,
CURRENT_LINE);
if ((p = lookup(name)) == nil)
p = addent(name);
else if (p->defn != null)
@ -538,7 +545,8 @@ dopushdef(name, defn)
ndptr p;
if (!*name)
errx(1, "null definition");
errx(1, "%s at line %lu: null definition", CURRENT_NAME,
CURRENT_LINE);
p = addent(name);
if (!*defn)
p->defn = null;
@ -605,8 +613,9 @@ doincl(ifile)
const char *ifile;
{
if (ilevel + 1 == MAXINP)
errx(1, "too many include files.");
if ((infile[ilevel + 1] = fopen_trypath(ifile)) != NULL) {
errx(1, "%s at line %lu: too many include files.",
CURRENT_NAME, CURRENT_LINE);
if (fopen_trypath(infile+ilevel+1, ifile) != NULL) {
ilevel++;
bbase[ilevel] = bufbase = bp;
return (1);

View File

@ -1,4 +1,4 @@
/* $OpenBSD: extern.h,v 1.14 2000/01/11 14:06:11 espie Exp $ */
/* $OpenBSD: extern.h,v 1.15 2000/01/12 17:49:53 espie Exp $ */
/* $NetBSD: extern.h,v 1.3 1996/01/13 23:25:24 pk Exp $ */
/*-
@ -49,7 +49,7 @@ extern int expr __P((const char *));
/* gnum4.c */
extern void addtoincludepath __P((const char *dirname));
extern FILE *fopen_trypath __P((const char *filename));
extern struct input_file *fopen_trypath __P((struct input_file *, const char *filename));
/* look.c */
extern ndptr addent __P((const char *));
@ -75,10 +75,15 @@ extern char *xalloc __P((size_t));
extern char *xstrdup __P((const char *));
extern void usage __P((void));
extern int obtain_char __P((struct input_file *));
extern void set_input __P((struct input_file *, FILE *, const char *));
extern void release_input __P((struct input_file *));
extern ndptr hashtab[]; /* hash table for macros etc. */
extern stae mstack[]; /* stack of m4 machine */
extern FILE *active; /* active output file pointer */
extern FILE *infile[]; /* input file stack (0=stdin) */
extern struct input_file infile[];/* input file stack (0=stdin) */
extern FILE *outfile[]; /* diversion array(0=bitbucket) */
extern int fp; /* m4 call frame pointer */
extern int ilevel; /* input file stack pointer */

View File

@ -1,4 +1,4 @@
/* $OpenBSD: gnum4.c,v 1.3 1999/11/17 15:34:13 espie Exp $ */
/* $OpenBSD: gnum4.c,v 1.4 2000/01/12 17:49:53 espie Exp $ */
/*
* Copyright (c) 1999 Marc Espie
@ -54,7 +54,7 @@ struct path_entry {
static struct path_entry *new_path_entry __P((const char *));
static void ensure_m4path __P((void));
static FILE *dopath __P((const char *));
static struct input_file *dopath __P((struct input_file *, const char *));
static struct path_entry *
new_path_entry(dirname)
@ -113,38 +113,42 @@ ensure_m4path()
}
static
FILE *
dopath(filename)
struct input_file *
dopath(i, filename)
struct input_file *i;
const char *filename;
{
char path[MAXPATHLEN];
struct path_entry *pe;
FILE *file;
FILE *f;
for (pe = first; pe; pe = pe->next) {
snprintf(path, sizeof(path), "%s/%s", pe->name, filename);
if ((file = fopen(path, "r")) != 0)
return file;
if ((f = fopen(path, "r")) != 0) {
set_input(i, f, path);
return i;
}
}
return NULL;
}
FILE *
fopen_trypath(filename)
struct input_file *
fopen_trypath(i, filename)
struct input_file *i;
const char *filename;
{
FILE *f;
f = fopen(filename, "r");
if (f)
return f;
if (f != NULL) {
set_input(i, f, filename);
return i;
}
if (filename[0] == '/')
return NULL;
ensure_m4path();
return dopath(filename);
return dopath(i, filename);
}

View File

@ -1,4 +1,4 @@
/* $OpenBSD: main.c,v 1.25 2000/01/11 14:10:01 espie Exp $ */
/* $OpenBSD: main.c,v 1.26 2000/01/12 17:49:53 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.25 2000/01/11 14:10:01 espie Exp $";
static char rcsid[] = "$OpenBSD: main.c,v 1.26 2000/01/12 17:49:53 espie Exp $";
#endif
#endif /* not lint */
@ -76,7 +76,7 @@ ndptr hashtab[HASHSIZE]; /* hash table for macros etc. */
stae mstack[STACKMAX+1]; /* stack of m4 machine */
int sp; /* current m4 stack pointer */
int fp; /* m4 call frame pointer */
FILE *infile[MAXINP]; /* input file stack (0=stdin) */
struct input_file infile[MAXINP];/* input file stack (0=stdin) */
FILE *outfile[MAXOUT]; /* diversion array(0=bitbucket)*/
FILE *active; /* active output file pointer */
int ilevel = 0; /* input file stack pointer */
@ -192,21 +192,20 @@ main(argc,argv)
if (!argc) {
sp = -1; /* stack pointer initialized */
fp = 0; /* frame pointer initialized */
infile[0] = stdin; /* default input (naturally) */
set_input(infile+0, stdin, "stdin");
/* default input (naturally) */
macro();
} else
for (; argc--; ++argv) {
p = *argv;
if (p[0] == '-' && p[1] == EOS)
ifp = stdin;
else if ((ifp = fopen_trypath(p)) == NULL)
set_input(infile, stdin, "stdin");
else if (fopen_trypath(infile, p) == NULL)
err(1, "%s", p);
sp = -1;
fp = 0;
infile[0] = ifp;
macro();
if (ifp != stdin)
(void)fclose(ifp);
release_input(infile);
}
if (*m4wraps) { /* anything for rundown ?? */
@ -309,8 +308,7 @@ macro()
errx(1, "unexpected end of input");
if (ilevel <= 0)
break; /* all done thanks.. */
--ilevel;
(void) fclose(infile[ilevel+1]);
release_input(infile+ilevel--);
bufbase = bbase[ilevel];
continue;
}

View File

@ -1,4 +1,4 @@
/* $OpenBSD: mdef.h,v 1.11 2000/01/11 14:06:12 espie Exp $ */
/* $OpenBSD: mdef.h,v 1.12 2000/01/12 17:49:53 espie Exp $ */
/* $NetBSD: mdef.h,v 1.7 1996/01/13 23:25:27 pk Exp $ */
/*
@ -147,6 +147,15 @@ typedef union { /* stack structure */
typedef short pbent; /* pushback entry; needs to hold chars + EOF */
struct input_file {
FILE *file;
char *name;
unsigned long lineno;
int c;
};
#define CURRENT_NAME (infile[ilevel].name)
#define CURRENT_LINE (infile[ilevel].lineno)
/*
* macros for readibility and/or speed
*
@ -154,7 +163,7 @@ typedef short pbent; /* pushback entry; needs to hold chars + EOF */
* pushf() - push a call frame entry onto stack
* pushs() - push a string pointer onto stack
*/
#define gpbc() (bp > bufbase) ? *--bp : getc(infile[ilevel])
#define gpbc() (bp > bufbase) ? *--bp : obtain_char(infile+ilevel)
#define pushf(x) if (sp < STACKMAX) mstack[++sp].sfra = (x)
#define pushs(x) if (sp < STACKMAX) mstack[++sp].sstr = (x)

View File

@ -1,4 +1,4 @@
/* $OpenBSD: misc.c,v 1.14 1999/11/17 15:34:13 espie Exp $ */
/* $OpenBSD: misc.c,v 1.15 2000/01/12 17:49:53 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.14 1999/11/17 15:34:13 espie Exp $";
static char rcsid[] = "$OpenBSD: misc.c,v 1.15 2000/01/12 17:49:53 espie Exp $";
#endif
#endif /* not lint */
@ -295,3 +295,34 @@ usage()
exit(1);
}
int
obtain_char(f)
struct input_file *f;
{
if (f->c == '\n')
f->lineno++;
f->c = fgetc(f->file);
return f->c;
}
void
set_input(f, real, name)
struct input_file *f;
FILE *real;
const char *name;
{
f->file = real;
f->lineno = 1;
f->c = 0;
f->name = xstrdup(name);
}
void
release_input(f)
struct input_file *f;
{
if (f->file != stdin)
fclose(f->file);
free(f->name);
}