157497Storek /*
2*61818Sbostic * Copyright (c) 1992, 1993
3*61818Sbostic * The Regents of the University of California. All rights reserved.
457497Storek *
557497Storek * This software was developed by the Computer Systems Engineering group
657497Storek * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
757497Storek * contributed to Berkeley.
857497Storek *
957497Storek * All advertising materials mentioning features or use of this software
1057497Storek * must display the following acknowledgement:
1157497Storek * This product includes software developed by the University of
1257497Storek * California, Lawrence Berkeley Laboratories.
1357497Storek *
1457497Storek * %sccs.include.redist.c%
1557497Storek *
16*61818Sbostic * @(#)util.c 8.1 (Berkeley) 06/06/93
1757497Storek */
1857497Storek
1957497Storek #include <ctype.h>
2057497Storek #include <stdio.h>
2157497Storek #include <stdlib.h>
2257497Storek #if __STDC__
2357497Storek #include <stdarg.h>
2457497Storek #else
2557497Storek #include <varargs.h>
2657497Storek #endif
2757497Storek #include "config.h"
2857497Storek
2957497Storek static void nomem __P((void));
3057497Storek static void vxerror __P((const char *, int, const char *, va_list));
3157497Storek
3257497Storek /*
3357497Storek * Malloc, with abort on error.
3457497Storek */
3557497Storek void *
emalloc(size)3657497Storek emalloc(size)
3757497Storek size_t size;
3857497Storek {
3957497Storek void *p;
4057497Storek
4157497Storek if ((p = malloc(size)) == NULL)
4257497Storek nomem();
4357497Storek return (p);
4457497Storek }
4557497Storek
4657497Storek /*
4757497Storek * Realloc, with abort on error.
4857497Storek */
4957497Storek void *
erealloc(p,size)5057497Storek erealloc(p, size)
5157497Storek void *p;
5257497Storek size_t size;
5357497Storek {
5457497Storek
5557497Storek if ((p = realloc(p, size)) == NULL)
5657497Storek nomem();
5757497Storek return (p);
5857497Storek }
5957497Storek
6057497Storek static void
nomem()6157497Storek nomem()
6257497Storek {
6357497Storek
6457497Storek (void)fprintf(stderr, "config: out of memory\n");
6557497Storek exit(1);
6657497Storek }
6757497Storek
6857497Storek /*
6957497Storek * Prepend the compilation directory to a file name.
7057497Storek */
7157497Storek char *
path(file)7257497Storek path(file)
7357497Storek const char *file;
7457497Storek {
7557497Storek register char *cp;
7657497Storek #define CDIR "../../compile/"
7757497Storek
7857497Storek if (file == NULL) {
7957497Storek cp = emalloc(sizeof(CDIR) + strlen(confdirbase));
8057497Storek (void)sprintf(cp, "%s%s", CDIR, confdirbase);
8157497Storek } else {
8257497Storek cp = emalloc(sizeof(CDIR) + strlen(confdirbase) + 1 +
8357497Storek strlen(file));
8457497Storek (void)sprintf(cp, "%s%s/%s", CDIR, confdirbase, file);
8557497Storek }
8657497Storek return (cp);
8757497Storek }
8857497Storek
8957497Storek static struct nvlist *nvhead;
9057497Storek
9157497Storek struct nvlist *
newnv(name,str,ptr,i)9257497Storek newnv(name, str, ptr, i)
9357497Storek const char *name, *str;
9457497Storek void *ptr;
9557497Storek int i;
9657497Storek {
9757497Storek register struct nvlist *nv;
9857497Storek
9957497Storek if ((nv = nvhead) == NULL)
10057497Storek nv = emalloc(sizeof(*nv));
10157497Storek else
10257497Storek nvhead = nv->nv_next;
10357497Storek nv->nv_next = NULL;
10457497Storek nv->nv_name = name;
10557497Storek if (ptr == NULL)
10657497Storek nv->nv_str = str;
10757497Storek else {
10857497Storek if (str != NULL)
10957497Storek panic("newnv");
11057497Storek nv->nv_ptr = ptr;
11157497Storek }
11257497Storek nv->nv_int = i;
11357497Storek return (nv);
11457497Storek }
11557497Storek
11657497Storek /*
11757497Storek * Free an nvlist structure (just one).
11857497Storek */
11957497Storek void
nvfree(nv)12057497Storek nvfree(nv)
12157497Storek register struct nvlist *nv;
12257497Storek {
12357497Storek
12457497Storek nv->nv_next = nvhead;
12557497Storek nvhead = nv;
12657497Storek }
12757497Storek
12857497Storek /*
12957497Storek * Free an nvlist (the whole list).
13057497Storek */
13157497Storek void
nvfreel(nv)13257497Storek nvfreel(nv)
13357497Storek register struct nvlist *nv;
13457497Storek {
13557497Storek register struct nvlist *next;
13657497Storek
13757497Storek for (; nv != NULL; nv = next) {
13857497Storek next = nv->nv_next;
13957497Storek nv->nv_next = nvhead;
14057497Storek nvhead = nv;
14157497Storek }
14257497Storek }
14357497Storek
14457497Storek /*
14557497Storek * External (config file) error. Complain, using current file
14657497Storek * and line number.
14757497Storek */
14857497Storek void
14957497Storek #if __STDC__
error(const char * fmt,...)15057497Storek error(const char *fmt, ...)
15157497Storek #else
15257497Storek error(fmt, va_alist)
15357497Storek const char *fmt;
15457497Storek va_dcl
15557497Storek #endif
15657497Storek {
15757497Storek va_list ap;
15857497Storek extern const char *yyfile;
15957497Storek
16057497Storek #if __STDC__
16157497Storek va_start(ap, fmt);
16257497Storek #else
16357497Storek va_start(ap);
16457497Storek #endif
16557497Storek vxerror(yyfile, currentline(), fmt, ap);
16657497Storek va_end(ap);
16757497Storek }
16857497Storek
16957497Storek /*
17057497Storek * Delayed config file error (i.e., something was wrong but we could not
17157497Storek * find out about it until later).
17257497Storek */
17357497Storek void
17457497Storek #if __STDC__
xerror(const char * file,int line,const char * fmt,...)17557497Storek xerror(const char *file, int line, const char *fmt, ...)
17657497Storek #else
17757497Storek xerror(file, line, fmt, va_alist)
17857497Storek const char *file;
17957497Storek int line;
18057497Storek const char *fmt;
18157497Storek va_dcl
18257497Storek #endif
18357497Storek {
18457497Storek va_list ap;
18557497Storek
18657497Storek #if __STDC__
18757497Storek va_start(ap, fmt);
18857497Storek #else
18957497Storek va_start(ap);
19057497Storek #endif
19157497Storek vxerror(file, line, fmt, ap);
19257497Storek va_end(ap);
19357497Storek }
19457497Storek
19557497Storek /*
19657497Storek * Internal form of error() and xerror().
19757497Storek */
19857497Storek static void
vxerror(file,line,fmt,ap)19957497Storek vxerror(file, line, fmt, ap)
20057497Storek const char *file;
20157497Storek int line;
20257497Storek const char *fmt;
20357497Storek va_list ap;
20457497Storek {
20557497Storek
20657497Storek (void)fprintf(stderr, "%s:%d: ", file, line);
20757497Storek (void)vfprintf(stderr, fmt, ap);
20857497Storek (void)putc('\n', stderr);
20957497Storek errors++;
21057497Storek }
21157497Storek
21257497Storek /*
21357497Storek * Internal error, abort.
21457497Storek */
21557497Storek __dead void
21657497Storek #if __STDC__
panic(const char * fmt,...)21757497Storek panic(const char *fmt, ...)
21857497Storek #else
21957497Storek panic(fmt, va_alist)
22057497Storek const char *fmt;
22157497Storek va_dcl
22257497Storek #endif
22357497Storek {
22457497Storek va_list ap;
22557497Storek
22657497Storek #if __STDC__
22757497Storek va_start(ap, fmt);
22857497Storek #else
22957497Storek va_start(ap);
23057497Storek #endif
23157497Storek (void)fprintf(stderr, "config: panic: ");
23257497Storek (void)vfprintf(stderr, fmt, ap);
23357497Storek (void)putc('\n', stderr);
23457497Storek va_end(ap);
23557497Storek exit(2);
23657497Storek }
237