From 152af09e655071840f3131ab0c6863d0b6093a31 Mon Sep 17 00:00:00 2001 From: thorpej Date: Fri, 9 Jul 1999 06:44:58 +0000 Subject: [PATCH] Add a mechanism to specify prefixes that are transparently prepended to file specifications. The prefixes are arranged in a stack, and nest, so that file, object, and include specifications are normalized, and all end up relative to the kernel compile directory. For example, in the kernel config file: # Pull in config fragments for kernel crypto prefix ../crypto-us/sys # push it cinclude "conf/files.crypto-us" # include it if it's there prefix # pop it and in files.crypto-us: file netinet6/esp_core.c ipsec & ipsec_esp file netinet6/esp_output.c ipsec & ipsec_esp file netinet6/esp_input.c ipsec & ipsec_esp ...generates the following in the kernel Makefile: $S/../crypto-us/sys/netinet6/esp_core.c \ $S/../crypto-us/sys/netinet6/esp_output.c \ $S/../crypto-us/sys/netinet6/esp_input.c \ By placing this all in the kernel config file, all the magic involved in reaching into non-standard kernel source directories is placed into a file that the user is expected to edit anyway, and reasonable examples (and sane defaults, for typical source checkouts) can be provided. --- usr.sbin/config/config.h | 20 +++++++++- usr.sbin/config/files.c | 4 +- usr.sbin/config/gram.y | 10 +++-- usr.sbin/config/main.c | 4 +- usr.sbin/config/mkheaders.c | 6 +-- usr.sbin/config/mkmakefile.c | 46 ++++++++++++++++++----- usr.sbin/config/scan.l | 5 ++- usr.sbin/config/util.c | 72 ++++++++++++++++++++++++++++++++++-- 8 files changed, 141 insertions(+), 26 deletions(-) diff --git a/usr.sbin/config/config.h b/usr.sbin/config/config.h index 5597c532f345..88cd92ad5481 100644 --- a/usr.sbin/config/config.h +++ b/usr.sbin/config/config.h @@ -1,4 +1,4 @@ -/* $NetBSD: config.h,v 1.43 1999/07/07 00:02:09 thorpej Exp $ */ +/* $NetBSD: config.h,v 1.44 1999/07/09 06:44:58 thorpej Exp $ */ /* * Copyright (c) 1992, 1993 @@ -255,6 +255,7 @@ struct files { const char *fi_path; /* full file path */ const char *fi_tail; /* name, i.e., strrchr(fi_path, '/') + 1 */ const char *fi_base; /* tail minus ".c" (or whatever) */ + const char *fi_prefix; /* any file prefix */ struct nvlist *fi_optx;/* options expression */ struct nvlist *fi_optf;/* flattened version of above, if needed */ const char *fi_mkrule; /* special make rule, if any */ @@ -276,6 +277,7 @@ struct objects { u_char oi_flags; /* as below */ char oi_lastc; /* last char from path */ const char *oi_path; /* full object path */ + const char *oi_prefix; /* any file prefix */ struct nvlist *oi_optx;/* options expression */ struct nvlist *oi_optf;/* flattened version of above, if needed */ }; @@ -288,6 +290,15 @@ struct objects { #define FX_AND 2 /* AND expr (lhs in nv_ptr, rhs in nv_next) */ #define FX_OR 3 /* OR expr (lhs in nv_ptr, rhs in nv_next) */ +/* + * File/object prefixes. These are arranged in a stack, and affect + * the behavior of the source path. + */ +struct prefix { + struct prefix *pf_next; /* next prefix in stack */ + const char *pf_prefix; /* the actual prefix */ +}; + /* * Hash tables look up name=value pairs. The pointer value of the name * is assumed to be constant forever; this can be arranged by interning @@ -334,7 +345,9 @@ int ndevi; /* number of devi's (before packing) */ int npseudo; /* number of pseudo's */ struct files *allfiles; /* list of all kernel source files */ -struct objects *allobjects; /* list of all kernel object and library files */ +struct objects *allobjects; /* list of all kernel object and library + files */ +struct prefix *prefixes; /* prefix stack */ struct devi **packed; /* arrayified table for packed devi's */ int npacked; /* size of packed table, <= ndevi */ @@ -417,6 +430,9 @@ void initsem __P((void)); /* util.c */ void *emalloc __P((size_t)); void *erealloc __P((void *, size_t)); +char *estrdup __P((const char *)); +void prefix_push __P((const char *)); +void prefix_pop __P((void)); char *sourcepath __P((const char *)); void warn __P((const char *, ...)); /* immediate warns */ void error __P((const char *, ...)); /* immediate errs */ diff --git a/usr.sbin/config/files.c b/usr.sbin/config/files.c index 217dc7730e08..ae0b23d70eec 100644 --- a/usr.sbin/config/files.c +++ b/usr.sbin/config/files.c @@ -1,4 +1,4 @@ -/* $NetBSD: files.c,v 1.9 1997/10/18 07:59:06 lukem Exp $ */ +/* $NetBSD: files.c,v 1.10 1999/07/09 06:44:58 thorpej Exp $ */ /* * Copyright (c) 1992, 1993 @@ -146,6 +146,7 @@ addfile(path, optx, flags, rule) fi->fi_path = path; fi->fi_tail = tail; fi->fi_base = intern(base); + fi->fi_prefix = (prefixes != NULL) ? prefixes->pf_prefix : NULL; fi->fi_optx = optx; fi->fi_optf = NULL; fi->fi_mkrule = rule; @@ -182,6 +183,7 @@ addobject(path, optx, flags) oi->oi_srcline = currentline(); oi->oi_flags = flags; oi->oi_path = path; + oi->oi_prefix = (prefixes != NULL) ? prefixes->pf_prefix : NULL; oi->oi_optx = optx; oi->oi_optf = NULL; *nextobject = oi; diff --git a/usr.sbin/config/gram.y b/usr.sbin/config/gram.y index 7431cfb807fa..a04aa609ae9c 100644 --- a/usr.sbin/config/gram.y +++ b/usr.sbin/config/gram.y @@ -1,5 +1,5 @@ %{ -/* $NetBSD: gram.y,v 1.26 1999/07/07 00:02:09 thorpej Exp $ */ +/* $NetBSD: gram.y,v 1.27 1999/07/09 06:44:58 thorpej Exp $ */ /* * Copyright (c) 1992, 1993 @@ -104,8 +104,8 @@ static struct nvlist *mk_ns __P((const char *, struct nvlist *)); %token AND AT ATTACH BUILD CINCLUDE COMPILE_WITH CONFIG DEFFS DEFINE DEFOPT %token DEFPARAM DEFFLAG DEFPSEUDO DEVICE DEVCLASS DUMPS ENDFILE XFILE XOBJECT %token FILE_SYSTEM FLAGS INCLUDE XMACHINE MAJOR MAKEOPTIONS -%token MAXUSERS MAXPARTITIONS MINOR ON OPTIONS PSEUDO_DEVICE ROOT SOURCE -%token TYPE WITH NEEDS_COUNT NEEDS_FLAG +%token MAXUSERS MAXPARTITIONS MINOR ON OPTIONS PREFIX PSEUDO_DEVICE ROOT +%token SOURCE TYPE WITH NEEDS_COUNT NEEDS_FLAG %token NUMBER %token PATHNAME WORD EMPTY @@ -225,6 +225,9 @@ include: INCLUDE WORD { (void) include($2, 0, 0); } | CINCLUDE WORD { (void) include($2, 0, 1); }; +prefix: + PREFIX PATHNAME { prefix_push($2); } | + PREFIX { prefix_pop(); }; /* * The machine definitions grammar. @@ -242,6 +245,7 @@ one_def: file | object | include | + prefix | DEVCLASS WORD { (void)defattr($2, NULL, 1); } | DEFFS fsoptfile_opt deffses { deffilesystem($2, $3); } | DEFINE WORD interface_opt { (void)defattr($2, $3, 0); } | diff --git a/usr.sbin/config/main.c b/usr.sbin/config/main.c index 6378445144fd..6b18f51be2d5 100644 --- a/usr.sbin/config/main.c +++ b/usr.sbin/config/main.c @@ -1,4 +1,4 @@ -/* $NetBSD: main.c,v 1.39 1999/04/02 06:36:30 gwr Exp $ */ +/* $NetBSD: main.c,v 1.40 1999/07/09 06:44:58 thorpej Exp $ */ /* * Copyright (c) 1992, 1993 @@ -339,7 +339,7 @@ mksymlinks() p = sourcepath(buf); q = machinearch; } else { - p = strdup("machine"); + p = estrdup("machine"); q = machine; } (void)unlink(q); diff --git a/usr.sbin/config/mkheaders.c b/usr.sbin/config/mkheaders.c index d3c2ba0cc7a2..6e4a15d57665 100644 --- a/usr.sbin/config/mkheaders.c +++ b/usr.sbin/config/mkheaders.c @@ -1,4 +1,4 @@ -/* $NetBSD: mkheaders.c,v 1.22 1999/01/21 13:10:09 pk Exp $ */ +/* $NetBSD: mkheaders.c,v 1.23 1999/07/09 06:44:59 thorpej Exp $ */ /* * Copyright (c) 1992, 1993 @@ -213,7 +213,7 @@ locators_print(name, value, arg) * usable defines, so ignore it. */ return 0; - locdup = strdup(name); + locdup = estrdup(name); for (cp = locdup; *cp; cp++) if (islower(*cp)) *cp = toupper(*cp); @@ -228,7 +228,7 @@ locators_print(name, value, arg) * usable defines, so ignore it. */ continue; - namedup = strdup(nv->nv_name); + namedup = estrdup(nv->nv_name); for (cp = namedup; *cp; cp++) if (islower(*cp)) *cp = toupper(*cp); diff --git a/usr.sbin/config/mkmakefile.c b/usr.sbin/config/mkmakefile.c index 3e01926481c2..7b98402796cd 100644 --- a/usr.sbin/config/mkmakefile.c +++ b/usr.sbin/config/mkmakefile.c @@ -1,4 +1,4 @@ -/* $NetBSD: mkmakefile.c,v 1.39 1999/05/23 19:30:30 mycroft Exp $ */ +/* $NetBSD: mkmakefile.c,v 1.40 1999/07/09 06:44:59 thorpej Exp $ */ /* * Copyright (c) 1992, 1993 @@ -247,17 +247,30 @@ emitobjs(fp) if ((oi->oi_flags & OI_SEL) == 0) continue; len = strlen(oi->oi_path); - if (*oi->oi_path != '/') - len += 3; + if (*oi->oi_path != '/') { + len += 3; /* "$S/" */ + if (oi->oi_prefix != NULL) + len += strlen(oi->oi_prefix) + 1; + } if (lpos + len > 72) { if (fputs(" \\\n", fp) < 0) return (1); sp = '\t'; lpos = 7; } - if (fprintf(fp, "%c%s%s", sp, *oi->oi_path != '/' ? "$S/" : "", - oi->oi_path) < 0) - return (1); + if (*oi->oi_path == '/') { + if (fprintf(fp, "%c%s", sp, oi->oi_path) < 0) + return (1); + } else { + if (oi->oi_prefix != NULL) { + if (fprintf(fp, "%c$S/%s/%s", sp, oi->oi_prefix, + oi->oi_path) < 0) + return (1); + } else { + if (fprintf(fp, "%c$S/%s", sp, oi->oi_path) < 0) + return (1); + } + } lpos += len + 1; sp = ' '; } @@ -305,17 +318,30 @@ emitfiles(fp, suffix) len = strlen(fpath); if (fpath[len - 1] != suffix) continue; - if (*fpath != '/') + if (*fpath != '/') { len += 3; /* "$S/" */ + if (fi->fi_prefix != NULL) + len += strlen(fi->fi_prefix) + 1; + } if (lpos + len > 72) { if (fputs(" \\\n", fp) < 0) return (1); sp = '\t'; lpos = 7; } - if (fprintf(fp, "%c%s%s", sp, *fpath != '/' ? "$S/" : "", - fpath) < 0) - return (1); + if (*fi->fi_path == '/') { + if (fprintf(fp, "%c%s", sp, fi->fi_path) < 0) + return (1); + } else { + if (fi->fi_prefix != NULL) { + if (fprintf(fp, "%c$S/%s/%s", sp, fi->fi_prefix, + fi->fi_path) < 0) + return (1); + } else { + if (fprintf(fp, "%c$S/%s", sp, fi->fi_path) < 0) + return (1); + } + } lpos += len + 1; sp = ' '; } diff --git a/usr.sbin/config/scan.l b/usr.sbin/config/scan.l index 567edad01cad..31fe16659a0f 100644 --- a/usr.sbin/config/scan.l +++ b/usr.sbin/config/scan.l @@ -1,5 +1,5 @@ %{ -/* $NetBSD: scan.l,v 1.24 1999/07/09 02:37:26 thorpej Exp $ */ +/* $NetBSD: scan.l,v 1.25 1999/07/09 06:44:59 thorpej Exp $ */ /* * Copyright (c) 1992, 1993 @@ -113,6 +113,7 @@ needs-flag return NEEDS_FLAG; object return XOBJECT; on return ON; options return OPTIONS; +prefix return PREFIX; pseudo-device return PSEUDO_DEVICE; root return ROOT; source return SOURCE; @@ -217,7 +218,7 @@ include(fname, ateof, conditional) setupdirs(); } - s = (*fname == '/') ? strdup(fname) : sourcepath(fname); + s = (*fname == '/') ? estrdup(fname) : sourcepath(fname); if ((fp = fopen(s, "r")) == NULL) { if (conditional == 0) error("cannot open %s for reading: %s\n", s, diff --git a/usr.sbin/config/util.c b/usr.sbin/config/util.c index 62f49f0a0a07..7f590d61b848 100644 --- a/usr.sbin/config/util.c +++ b/usr.sbin/config/util.c @@ -1,4 +1,4 @@ -/* $NetBSD: util.c,v 1.7 1998/06/24 11:20:55 jonathan Exp $ */ +/* $NetBSD: util.c,v 1.8 1999/07/09 06:44:59 thorpej Exp $ */ /* * Copyright (c) 1992, 1993 @@ -90,6 +90,20 @@ erealloc(p, size) return (p); } +/* + * Strdup, with abort on error. + */ +char * +estrdup(p) + const char *p; +{ + char *cp; + + if ((cp = strdup(p)) == NULL) + nomem(); + return (cp); +} + static void nomem() { @@ -98,6 +112,48 @@ nomem() exit(1); } +/* + * Push a prefix onto the prefix stack. + */ +void +prefix_push(path) + const char *path; +{ + struct prefix *pf; + char *cp; + + pf = emalloc(sizeof(struct prefix)); + + if (prefixes != NULL) { + cp = emalloc(strlen(prefixes->pf_prefix) + 1 + + strlen(path) + 1); + (void) sprintf(cp, "%s/%s", prefixes->pf_prefix, path); + pf->pf_prefix = intern(cp); + free(cp); + } else + pf->pf_prefix = intern(path); + + pf->pf_next = prefixes; + prefixes = pf; +} + +/* + * Pop a prefix off the prefix stack. + */ +void +prefix_pop() +{ + struct prefix *pf; + + if ((pf = prefixes) == NULL) { + error("no prefixes on the stack to pop"); + return; + } + + prefixes = pf->pf_next; + free(pf); +} + /* * Prepend the source path to a file name. */ @@ -105,10 +161,20 @@ char * sourcepath(file) const char *file; { + size_t len; char *cp; - cp = emalloc(strlen(srcdir) + 1 + strlen(file) + 1); - (void)sprintf(cp, "%s/%s", srcdir, file); + len = strlen(srcdir) + 1 + strlen(file) + 1; + if (prefixes != NULL) + len += strlen(prefixes->pf_prefix) + 1; + + cp = emalloc(len); + + if (prefixes != NULL) + (void) sprintf(cp, "%s/%s/%s", srcdir, + prefixes->pf_prefix, file); + else + (void) sprintf(cp, "%s/%s", srcdir, file); return (cp); }