Home | History | Annotate | Line # | Download | only in config
util.c revision 1.11
      1  1.11  dholland /*	$NetBSD: util.c,v 1.11 2012/03/11 08:21:53 dholland Exp $	*/
      2   1.1   thorpej 
      3   1.1   thorpej /*
      4   1.1   thorpej  * Copyright (c) 1992, 1993
      5   1.1   thorpej  *	The Regents of the University of California.  All rights reserved.
      6   1.1   thorpej  *
      7   1.1   thorpej  * This software was developed by the Computer Systems Engineering group
      8   1.1   thorpej  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9   1.1   thorpej  * contributed to Berkeley.
     10   1.1   thorpej  *
     11   1.1   thorpej  * All advertising materials mentioning features or use of this software
     12   1.1   thorpej  * must display the following acknowledgement:
     13   1.1   thorpej  *	This product includes software developed by the University of
     14   1.1   thorpej  *	California, Lawrence Berkeley Laboratories.
     15   1.1   thorpej  *
     16   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     17   1.1   thorpej  * modification, are permitted provided that the following conditions
     18   1.1   thorpej  * are met:
     19   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     20   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     21   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     22   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     23   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     24   1.1   thorpej  * 3. Neither the name of the University nor the names of its contributors
     25   1.1   thorpej  *    may be used to endorse or promote products derived from this software
     26   1.1   thorpej  *    without specific prior written permission.
     27   1.1   thorpej  *
     28   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29   1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30   1.1   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31   1.1   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32   1.1   thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33   1.1   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34   1.1   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35   1.1   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36   1.1   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37   1.1   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38   1.1   thorpej  * SUCH DAMAGE.
     39   1.1   thorpej  *
     40   1.1   thorpej  *	from: @(#)util.c	8.1 (Berkeley) 6/6/93
     41   1.1   thorpej  */
     42   1.1   thorpej 
     43   1.1   thorpej #if HAVE_NBTOOL_CONFIG_H
     44   1.1   thorpej #include "nbtool_config.h"
     45   1.1   thorpej #endif
     46   1.1   thorpej 
     47   1.3  christos #include <sys/types.h>
     48  1.10  dholland #include <assert.h>
     49   1.1   thorpej #include <ctype.h>
     50   1.1   thorpej #include <stdio.h>
     51   1.1   thorpej #include <stdlib.h>
     52   1.1   thorpej #include <string.h>
     53   1.1   thorpej #include <stdarg.h>
     54   1.3  christos #include <util.h>
     55   1.6  christos #include <err.h>
     56   1.1   thorpej #include "defs.h"
     57   1.1   thorpej 
     58   1.6  christos static void cfgvxerror(const char *, int, const char *, va_list)
     59   1.9  dholland 	     __printflike(3, 0);
     60   1.6  christos static void cfgvxwarn(const char *, int, const char *, va_list)
     61   1.9  dholland 	     __printflike(3, 0);
     62   1.6  christos static void cfgvxmsg(const char *, int, const char *, const char *, va_list)
     63   1.9  dholland      __printflike(4, 0);
     64   1.1   thorpej 
     65   1.1   thorpej /*
     66   1.1   thorpej  * Push a prefix onto the prefix stack.
     67   1.1   thorpej  */
     68   1.1   thorpej void
     69   1.1   thorpej prefix_push(const char *path)
     70   1.1   thorpej {
     71   1.1   thorpej 	struct prefix *pf;
     72   1.1   thorpej 	char *cp;
     73   1.1   thorpej 
     74   1.1   thorpej 	pf = ecalloc(1, sizeof(struct prefix));
     75   1.1   thorpej 
     76   1.1   thorpej 	if (! SLIST_EMPTY(&prefixes) && *path != '/') {
     77   1.1   thorpej 		cp = emalloc(strlen(SLIST_FIRST(&prefixes)->pf_prefix) + 1 +
     78   1.1   thorpej 		    strlen(path) + 1);
     79   1.1   thorpej 		(void) sprintf(cp, "%s/%s",
     80   1.1   thorpej 		    SLIST_FIRST(&prefixes)->pf_prefix, path);
     81   1.1   thorpej 		pf->pf_prefix = intern(cp);
     82   1.1   thorpej 		free(cp);
     83   1.1   thorpej 	} else
     84   1.1   thorpej 		pf->pf_prefix = intern(path);
     85   1.1   thorpej 
     86   1.1   thorpej 	SLIST_INSERT_HEAD(&prefixes, pf, pf_next);
     87   1.1   thorpej }
     88   1.1   thorpej 
     89   1.1   thorpej /*
     90   1.1   thorpej  * Pop a prefix off the prefix stack.
     91   1.1   thorpej  */
     92   1.1   thorpej void
     93   1.1   thorpej prefix_pop(void)
     94   1.1   thorpej {
     95   1.1   thorpej 	struct prefix *pf;
     96   1.1   thorpej 
     97   1.1   thorpej 	if ((pf = SLIST_FIRST(&prefixes)) == NULL) {
     98   1.6  christos 		cfgerror("no prefixes on the stack to pop");
     99   1.1   thorpej 		return;
    100   1.1   thorpej 	}
    101   1.1   thorpej 
    102   1.1   thorpej 	SLIST_REMOVE_HEAD(&prefixes, pf_next);
    103   1.1   thorpej 	/* Remember this prefix for emitting -I... directives later. */
    104   1.1   thorpej 	SLIST_INSERT_HEAD(&allprefixes, pf, pf_next);
    105   1.1   thorpej }
    106   1.1   thorpej 
    107   1.1   thorpej /*
    108   1.1   thorpej  * Prepend the source path to a file name.
    109   1.1   thorpej  */
    110   1.1   thorpej char *
    111   1.1   thorpej sourcepath(const char *file)
    112   1.1   thorpej {
    113   1.1   thorpej 	size_t len;
    114   1.1   thorpej 	char *cp;
    115   1.1   thorpej 	struct prefix *pf;
    116   1.1   thorpej 
    117   1.1   thorpej 	pf = SLIST_EMPTY(&prefixes) ? NULL : SLIST_FIRST(&prefixes);
    118   1.1   thorpej 	if (pf != NULL && *pf->pf_prefix == '/')
    119   1.1   thorpej 		len = strlen(pf->pf_prefix) + 1 + strlen(file) + 1;
    120   1.1   thorpej 	else {
    121   1.1   thorpej 		len = strlen(srcdir) + 1 + strlen(file) + 1;
    122   1.1   thorpej 		if (pf != NULL)
    123   1.1   thorpej 			len += strlen(pf->pf_prefix) + 1;
    124   1.1   thorpej 	}
    125   1.1   thorpej 
    126   1.1   thorpej 	cp = emalloc(len);
    127   1.1   thorpej 
    128   1.1   thorpej 	if (pf != NULL) {
    129   1.1   thorpej 		if (*pf->pf_prefix == '/')
    130   1.1   thorpej 			(void) sprintf(cp, "%s/%s", pf->pf_prefix, file);
    131   1.1   thorpej 		else
    132   1.1   thorpej 			(void) sprintf(cp, "%s/%s/%s", srcdir,
    133   1.1   thorpej 			    pf->pf_prefix, file);
    134   1.1   thorpej 	} else
    135   1.1   thorpej 		(void) sprintf(cp, "%s/%s", srcdir, file);
    136   1.1   thorpej 	return (cp);
    137   1.1   thorpej }
    138   1.1   thorpej 
    139   1.1   thorpej struct nvlist *
    140   1.8  christos newnv(const char *name, const char *str, void *ptr, long long i, struct nvlist *next)
    141   1.1   thorpej {
    142   1.1   thorpej 	struct nvlist *nv;
    143   1.1   thorpej 
    144   1.4       dsl 	nv = ecalloc(1, sizeof(*nv));
    145   1.1   thorpej 	nv->nv_next = next;
    146   1.1   thorpej 	nv->nv_name = name;
    147   1.4       dsl 	nv->nv_str = str;
    148   1.4       dsl 	nv->nv_ptr = ptr;
    149   1.8  christos 	nv->nv_num = i;
    150   1.4       dsl 	return nv;
    151   1.1   thorpej }
    152   1.1   thorpej 
    153   1.1   thorpej /*
    154   1.1   thorpej  * Free an nvlist structure (just one).
    155   1.1   thorpej  */
    156   1.1   thorpej void
    157   1.1   thorpej nvfree(struct nvlist *nv)
    158   1.1   thorpej {
    159   1.1   thorpej 
    160   1.4       dsl 	free(nv);
    161   1.1   thorpej }
    162   1.1   thorpej 
    163   1.1   thorpej /*
    164   1.1   thorpej  * Free an nvlist (the whole list).
    165   1.1   thorpej  */
    166   1.1   thorpej void
    167   1.1   thorpej nvfreel(struct nvlist *nv)
    168   1.1   thorpej {
    169   1.1   thorpej 	struct nvlist *next;
    170   1.1   thorpej 
    171   1.1   thorpej 	for (; nv != NULL; nv = next) {
    172   1.1   thorpej 		next = nv->nv_next;
    173   1.4       dsl 		free(nv);
    174   1.1   thorpej 	}
    175   1.1   thorpej }
    176   1.1   thorpej 
    177   1.5      cube struct nvlist *
    178   1.5      cube nvcat(struct nvlist *nv1, struct nvlist *nv2)
    179   1.5      cube {
    180   1.5      cube 	struct nvlist *nv;
    181   1.5      cube 
    182   1.5      cube 	if (nv1 == NULL)
    183   1.5      cube 		return nv2;
    184   1.5      cube 
    185   1.5      cube 	for (nv = nv1; nv->nv_next != NULL; nv = nv->nv_next);
    186   1.5      cube 
    187   1.5      cube 	nv->nv_next = nv2;
    188   1.5      cube 	return nv1;
    189   1.5      cube }
    190   1.5      cube 
    191  1.10  dholland struct attrlist *
    192  1.10  dholland attrlist_create(void)
    193  1.10  dholland {
    194  1.10  dholland 	struct attrlist *al;
    195  1.10  dholland 
    196  1.10  dholland 	al = emalloc(sizeof(*al));
    197  1.10  dholland 	al->al_next = NULL;
    198  1.10  dholland 	al->al_this = NULL;
    199  1.10  dholland 	return al;
    200  1.10  dholland }
    201  1.10  dholland 
    202  1.10  dholland struct attrlist *
    203  1.10  dholland attrlist_cons(struct attrlist *next, struct attr *a)
    204  1.10  dholland {
    205  1.10  dholland 	struct attrlist *al;
    206  1.10  dholland 
    207  1.10  dholland 	al = attrlist_create();
    208  1.10  dholland 	al->al_next = next;
    209  1.10  dholland 	al->al_this = a;
    210  1.10  dholland 	return al;
    211  1.10  dholland }
    212  1.10  dholland 
    213  1.10  dholland void
    214  1.10  dholland attrlist_destroy(struct attrlist *al)
    215  1.10  dholland {
    216  1.10  dholland 	assert(al->al_next == NULL);
    217  1.10  dholland 	assert(al->al_this == NULL);
    218  1.10  dholland 	free(al);
    219  1.10  dholland }
    220  1.10  dholland 
    221  1.10  dholland void
    222  1.10  dholland attrlist_destroyall(struct attrlist *al)
    223  1.10  dholland {
    224  1.10  dholland 	struct attrlist *next;
    225  1.10  dholland 
    226  1.10  dholland 	while (al != NULL) {
    227  1.10  dholland 		next = al->al_next;
    228  1.10  dholland 		al->al_next = NULL;
    229  1.10  dholland 		/* XXX should we make the caller guarantee this? */
    230  1.10  dholland 		al->al_this = NULL;
    231  1.10  dholland 		attrlist_destroy(al);
    232  1.10  dholland 		al = next;
    233  1.10  dholland 	}
    234  1.10  dholland }
    235  1.10  dholland 
    236  1.11  dholland /*
    237  1.11  dholland  * Create an expression node.
    238  1.11  dholland  */
    239  1.11  dholland struct condexpr *
    240  1.11  dholland condexpr_create(enum condexpr_types type)
    241  1.11  dholland {
    242  1.11  dholland 	struct condexpr *cx;
    243  1.11  dholland 
    244  1.11  dholland 	cx = emalloc(sizeof(*cx));
    245  1.11  dholland 	cx->cx_type = type;
    246  1.11  dholland 	switch (type) {
    247  1.11  dholland 
    248  1.11  dholland 	    case CX_ATOM:
    249  1.11  dholland 		cx->cx_atom = NULL;
    250  1.11  dholland 		break;
    251  1.11  dholland 
    252  1.11  dholland 	    case CX_NOT:
    253  1.11  dholland 		cx->cx_not = NULL;
    254  1.11  dholland 		break;
    255  1.11  dholland 
    256  1.11  dholland 	    case CX_AND:
    257  1.11  dholland 		cx->cx_and.left = NULL;
    258  1.11  dholland 		cx->cx_and.right = NULL;
    259  1.11  dholland 		break;
    260  1.11  dholland 
    261  1.11  dholland 	    case CX_OR:
    262  1.11  dholland 		cx->cx_or.left = NULL;
    263  1.11  dholland 		cx->cx_or.right = NULL;
    264  1.11  dholland 		break;
    265  1.11  dholland 
    266  1.11  dholland 	    default:
    267  1.11  dholland 		panic("condexpr_create: invalid expr type %d", (int)type);
    268  1.11  dholland 	}
    269  1.11  dholland 	return cx;
    270  1.11  dholland }
    271  1.11  dholland 
    272  1.11  dholland /*
    273  1.11  dholland  * Free an expression tree.
    274  1.11  dholland  */
    275  1.11  dholland void
    276  1.11  dholland condexpr_destroy(struct condexpr *expr)
    277  1.11  dholland {
    278  1.11  dholland 	switch (expr->cx_type) {
    279  1.11  dholland 
    280  1.11  dholland 	    case CX_ATOM:
    281  1.11  dholland 		/* nothing */
    282  1.11  dholland 		break;
    283  1.11  dholland 
    284  1.11  dholland 	    case CX_NOT:
    285  1.11  dholland 		condexpr_destroy(expr->cx_not);
    286  1.11  dholland 		break;
    287  1.11  dholland 
    288  1.11  dholland 	    case CX_AND:
    289  1.11  dholland 		condexpr_destroy(expr->cx_and.left);
    290  1.11  dholland 		condexpr_destroy(expr->cx_and.right);
    291  1.11  dholland 		break;
    292  1.11  dholland 
    293  1.11  dholland 	    case CX_OR:
    294  1.11  dholland 		condexpr_destroy(expr->cx_or.left);
    295  1.11  dholland 		condexpr_destroy(expr->cx_or.right);
    296  1.11  dholland 		break;
    297  1.11  dholland 
    298  1.11  dholland 	    default:
    299  1.11  dholland 		panic("condexpr_destroy: invalid expr type %d",
    300  1.11  dholland 		      (int)expr->cx_type);
    301  1.11  dholland 	}
    302  1.11  dholland 	free(expr);
    303  1.11  dholland }
    304  1.11  dholland 
    305   1.1   thorpej void
    306   1.6  christos cfgwarn(const char *fmt, ...)
    307   1.1   thorpej {
    308   1.1   thorpej 	va_list ap;
    309   1.1   thorpej 	extern const char *yyfile;
    310   1.1   thorpej 
    311   1.1   thorpej 	va_start(ap, fmt);
    312   1.6  christos 	cfgvxwarn(yyfile, currentline(), fmt, ap);
    313   1.1   thorpej 	va_end(ap);
    314   1.1   thorpej }
    315   1.1   thorpej 
    316   1.2      cube void
    317   1.6  christos cfgxwarn(const char *file, int line, const char *fmt, ...)
    318   1.2      cube {
    319   1.2      cube 	va_list ap;
    320   1.2      cube 
    321   1.2      cube 	va_start(ap, fmt);
    322   1.6  christos 	cfgvxwarn(file, line, fmt, ap);
    323   1.2      cube 	va_end(ap);
    324   1.2      cube }
    325   1.1   thorpej 
    326   1.1   thorpej static void
    327   1.6  christos cfgvxwarn(const char *file, int line, const char *fmt, va_list ap)
    328   1.1   thorpej {
    329   1.6  christos 	cfgvxmsg(file, line, "warning: ", fmt, ap);
    330   1.1   thorpej }
    331   1.1   thorpej 
    332   1.1   thorpej /*
    333   1.1   thorpej  * External (config file) error.  Complain, using current file
    334   1.1   thorpej  * and line number.
    335   1.1   thorpej  */
    336   1.1   thorpej void
    337   1.6  christos cfgerror(const char *fmt, ...)
    338   1.1   thorpej {
    339   1.1   thorpej 	va_list ap;
    340   1.1   thorpej 	extern const char *yyfile;
    341   1.1   thorpej 
    342   1.1   thorpej 	va_start(ap, fmt);
    343   1.6  christos 	cfgvxerror(yyfile, currentline(), fmt, ap);
    344   1.1   thorpej 	va_end(ap);
    345   1.1   thorpej }
    346   1.1   thorpej 
    347   1.1   thorpej /*
    348   1.1   thorpej  * Delayed config file error (i.e., something was wrong but we could not
    349   1.1   thorpej  * find out about it until later).
    350   1.1   thorpej  */
    351   1.1   thorpej void
    352   1.6  christos cfgxerror(const char *file, int line, const char *fmt, ...)
    353   1.1   thorpej {
    354   1.1   thorpej 	va_list ap;
    355   1.1   thorpej 
    356   1.1   thorpej 	va_start(ap, fmt);
    357   1.6  christos 	cfgvxerror(file, line, fmt, ap);
    358   1.1   thorpej 	va_end(ap);
    359   1.1   thorpej }
    360   1.1   thorpej 
    361   1.1   thorpej /*
    362   1.1   thorpej  * Internal form of error() and xerror().
    363   1.1   thorpej  */
    364   1.1   thorpej static void
    365   1.6  christos cfgvxerror(const char *file, int line, const char *fmt, va_list ap)
    366   1.1   thorpej {
    367   1.6  christos 	cfgvxmsg(file, line, "", fmt, ap);
    368   1.1   thorpej 	errors++;
    369   1.1   thorpej }
    370   1.1   thorpej 
    371   1.1   thorpej 
    372   1.1   thorpej /*
    373   1.1   thorpej  * Internal error, abort.
    374   1.1   thorpej  */
    375   1.1   thorpej __dead void
    376   1.1   thorpej panic(const char *fmt, ...)
    377   1.1   thorpej {
    378   1.1   thorpej 	va_list ap;
    379   1.1   thorpej 
    380   1.1   thorpej 	va_start(ap, fmt);
    381   1.6  christos 	(void)fprintf(stderr, "%s: panic: ", getprogname());
    382   1.1   thorpej 	(void)vfprintf(stderr, fmt, ap);
    383   1.1   thorpej 	(void)putc('\n', stderr);
    384   1.1   thorpej 	va_end(ap);
    385   1.1   thorpej 	exit(2);
    386   1.1   thorpej }
    387   1.1   thorpej 
    388   1.1   thorpej /*
    389   1.1   thorpej  * Internal form of error() and xerror().
    390   1.1   thorpej  */
    391   1.1   thorpej static void
    392   1.6  christos cfgvxmsg(const char *file, int line, const char *msgclass, const char *fmt,
    393   1.1   thorpej       va_list ap)
    394   1.1   thorpej {
    395   1.1   thorpej 
    396   1.1   thorpej 	(void)fprintf(stderr, "%s:%d: %s", file, line, msgclass);
    397   1.1   thorpej 	(void)vfprintf(stderr, fmt, ap);
    398   1.1   thorpej 	(void)putc('\n', stderr);
    399   1.1   thorpej }
    400   1.7     lukem 
    401   1.7     lukem void
    402   1.7     lukem autogen_comment(FILE *fp, const char *targetfile)
    403   1.7     lukem {
    404   1.7     lukem 
    405   1.7     lukem 	(void)fprintf(fp,
    406   1.7     lukem 	    "/*\n"
    407   1.7     lukem 	    " * MACHINE GENERATED: DO NOT EDIT\n"
    408   1.7     lukem 	    " *\n"
    409   1.7     lukem 	    " * %s, from \"%s\"\n"
    410   1.7     lukem 	    " */\n\n",
    411   1.7     lukem 	    targetfile, conffile);
    412   1.7     lukem }
    413