Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/config Create a struct condexpr type to hold conditi...



details:   https://anonhg.NetBSD.org/src/rev/0cf0ac21079b
branches:  trunk
changeset: 777974:0cf0ac21079b
user:      dholland <dholland%NetBSD.org@localhost>
date:      Sun Mar 11 08:21:53 2012 +0000

description:
Create a struct condexpr type to hold condition expressions, instead
of abusing struct nvlist to make trees.

(These are the a|b and a&b constructs.)

diffstat:

 usr.bin/config/defs.h       |  48 +++++++++++++++++++-------
 usr.bin/config/files.c      |  65 +++++++++--------------------------
 usr.bin/config/gram.y       |  81 +++++++++++++++++++++++++++++++++++++-------
 usr.bin/config/main.c       |   6 +-
 usr.bin/config/mkmakefile.c |  12 +++--
 usr.bin/config/sem.c        |  12 +++---
 usr.bin/config/sem.h        |   4 +-
 usr.bin/config/util.c       |  71 ++++++++++++++++++++++++++++++++++++++-
 8 files changed, 207 insertions(+), 92 deletions(-)

diffs (truncated from 592 to 300 lines):

diff -r 83b99f47f00e -r 0cf0ac21079b usr.bin/config/defs.h
--- a/usr.bin/config/defs.h     Sun Mar 11 07:46:47 2012 +0000
+++ b/usr.bin/config/defs.h     Sun Mar 11 08:21:53 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: defs.h,v 1.38 2012/03/11 07:32:41 dholland Exp $       */
+/*     $NetBSD: defs.h,v 1.39 2012/03/11 08:21:53 dholland Exp $       */
 
 /*
  * Copyright (c) 1992, 1993
@@ -317,7 +317,7 @@
        TAILQ_ENTRY(files) fi_next;
        const  char *fi_tail;   /* name, i.e., strrchr(fi_path, '/') + 1 */
        const  char *fi_base;   /* tail minus ".c" (or whatever) */
-       struct nvlist *fi_optx; /* options expression */
+       struct condexpr *fi_optx; /* options expression */
        struct nvlist *fi_optf; /* flattened version of above, if needed */
        const  char *fi_mkrule; /* special make rule, if any */
 };
@@ -341,7 +341,7 @@
 struct objects {
        struct  filetype oi_fit;
        TAILQ_ENTRY(objects) oi_next;
-       struct  nvlist *oi_optx;/* options expression */
+       struct condexpr *oi_optx;       /* condition expression */
        struct  nvlist *oi_optf;/* flattened version of above, if needed */
 };
 
@@ -356,10 +356,31 @@
 #define        OI_SEL          0x01    /* selected */
 #define        OI_NEEDSFLAG    0x02    /* needs-flag */
 
-#define        FX_ATOM         0       /* atom (in nv_name) */
-#define        FX_NOT          1       /* NOT expr (subexpression in nv_next) */
-#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) */
+/*
+ * Condition expressions.
+ */
+
+enum condexpr_types {
+       CX_ATOM,
+       CX_NOT,
+       CX_AND,
+       CX_OR,
+};
+struct condexpr {
+       enum condexpr_types cx_type;
+       union {
+               const char *atom;
+               struct condexpr *not;
+               struct {
+                       struct condexpr *left;
+                       struct condexpr *right;
+               } and, or;
+       } cx_u;
+};
+#define cx_atom        cx_u.atom
+#define cx_not cx_u.not
+#define cx_and cx_u.and
+#define cx_or  cx_u.or
 
 /*
  * File/object prefixes.  These are arranged in a stack, and affect
@@ -380,7 +401,7 @@
        const char      *dm_name;       /* [bc]devsw name */
        devmajor_t      dm_cmajor;      /* character major */
        devmajor_t      dm_bmajor;      /* block major */
-       struct nvlist   *dm_opts;       /* options */
+       struct condexpr *dm_opts;       /* options */
        struct nvlist   *dm_devnodes;   /* information on /dev nodes */
 };
 
@@ -477,10 +498,9 @@
 int    fixfiles(void);         /* finalize */
 int    fixobjects(void);
 int    fixdevsw(void);
-void   addfile(const char *, struct nvlist *, int, const char *);
-void   addobject(const char *, struct nvlist *, int);
-int    expr_eval(struct nvlist *, int (*)(const char *, void *), void *);
-void   expr_free(struct nvlist *);
+void   addfile(const char *, struct condexpr *, int, const char *);
+void   addobject(const char *, struct condexpr *, int);
+int    expr_eval(struct condexpr *, int (*)(const char *, void *), void *);
 
 /* hash.c */
 struct hashtab *ht_new(void);
@@ -505,7 +525,7 @@
 void   addfsoption(const char *);
 void   addmkoption(const char *, const char *);
 void   appendmkoption(const char *, const char *);
-void   appendcondmkoption(struct nvlist *, const char *, const char *);
+void   appendcondmkoption(struct condexpr *, const char *, const char *);
 void   deffilesystem(struct nvlist *, struct nvlist *);
 void   defoption(const char *, struct nvlist *, struct nvlist *);
 void   defflag(const char *, struct nvlist *, struct nvlist *, int);
@@ -584,6 +604,8 @@
 struct attrlist *attrlist_cons(struct attrlist *, struct attr *);
 void attrlist_destroy(struct attrlist *);
 void attrlist_destroyall(struct attrlist *);
+struct condexpr *condexpr_create(enum condexpr_types);
+void condexpr_destroy(struct condexpr *);
 
 /* liby */
 void   yyerror(const char *);
diff -r 83b99f47f00e -r 0cf0ac21079b usr.bin/config/files.c
--- a/usr.bin/config/files.c    Sun Mar 11 07:46:47 2012 +0000
+++ b/usr.bin/config/files.c    Sun Mar 11 08:21:53 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: files.c,v 1.10 2009/03/13 18:24:41 cube Exp $  */
+/*     $NetBSD: files.c,v 1.11 2012/03/11 08:21:53 dholland Exp $      */
 
 /*
  * Copyright (c) 1992, 1993
@@ -82,7 +82,7 @@
 }
 
 void
-addfile(const char *path, struct nvlist *optx, int flags, const char *rule)
+addfile(const char *path, struct condexpr *optx, int flags, const char *rule)
 {
        struct files *fi;
        const char *dotp, *tail;
@@ -160,11 +160,11 @@
        TAILQ_INSERT_TAIL(&allfiles, fi, fi_next);
        return;
  bad:
-       expr_free(optx);
+       condexpr_destroy(optx);
 }
 
 void
-addobject(const char *path, struct nvlist *optx, int flags)
+addobject(const char *path, struct condexpr *optx, int flags)
 {
        struct objects *oi;
 
@@ -494,64 +494,33 @@
  * our mixing of C's bitwise & boolean here may give surprises).
  */
 int
-expr_eval(struct nvlist *expr, int (*fn)(const char *, void *), void *context)
+expr_eval(struct condexpr *expr, int (*fn)(const char *, void *), void *ctx)
 {
        int lhs, rhs;
 
-       switch (expr->nv_num) {
+       switch (expr->cx_type) {
 
-       case FX_ATOM:
-               return ((*fn)(expr->nv_name, context));
+       case CX_ATOM:
+               return ((*fn)(expr->cx_atom, ctx));
 
-       case FX_NOT:
-               return (!expr_eval(expr->nv_next, fn, context));
+       case CX_NOT:
+               return (!expr_eval(expr->cx_not, fn, ctx));
 
-       case FX_AND:
-               lhs = expr_eval(expr->nv_ptr, fn, context);
-               rhs = expr_eval(expr->nv_next, fn, context);
+       case CX_AND:
+               lhs = expr_eval(expr->cx_and.left, fn, ctx);
+               rhs = expr_eval(expr->cx_and.right, fn, ctx);
                return (lhs & rhs);
 
-       case FX_OR:
-               lhs = expr_eval(expr->nv_ptr, fn, context);
-               rhs = expr_eval(expr->nv_next, fn, context);
+       case CX_OR:
+               lhs = expr_eval(expr->cx_or.left, fn, ctx);
+               rhs = expr_eval(expr->cx_or.right, fn, ctx);
                return (lhs | rhs);
        }
-       panic("expr_eval %lld", expr->nv_num);
+       panic("invalid condexpr type %d", (int)expr->cx_type);
        /* NOTREACHED */
        return (0);
 }
 
-/*
- * Free an expression tree.
- */
-void
-expr_free(struct nvlist *expr)
-{
-       struct nvlist *rhs;
-
-       /* This loop traverses down the RHS of each subexpression. */
-       for (; expr != NULL; expr = rhs) {
-               switch (expr->nv_num) {
-
-               /* Atoms and !-exprs have no left hand side. */
-               case FX_ATOM:
-               case FX_NOT:
-                       break;
-
-               /* For AND and OR nodes, free the LHS. */
-               case FX_AND:
-               case FX_OR:
-                       expr_free(expr->nv_ptr);
-                       break;
-
-               default:
-                       panic("expr_free %lld", expr->nv_num);
-               }
-               rhs = expr->nv_next;
-               nvfree(expr);
-       }
-}
-
 #ifdef DEBUG
 /*
  * Print expression tree.
diff -r 83b99f47f00e -r 0cf0ac21079b usr.bin/config/gram.y
--- a/usr.bin/config/gram.y     Sun Mar 11 07:46:47 2012 +0000
+++ b/usr.bin/config/gram.y     Sun Mar 11 08:21:53 2012 +0000
@@ -1,5 +1,5 @@
 %{
-/*     $NetBSD: gram.y,v 1.33 2012/03/11 07:46:47 dholland Exp $       */
+/*     $NetBSD: gram.y,v 1.34 2012/03/11 08:21:53 dholland Exp $       */
 
 /*
  * Copyright (c) 1992, 1993
@@ -71,6 +71,7 @@
  */
 #define WRAP_CODE_nvlist       1
 #define WRAP_CODE_attrlist     2
+#define WRAP_CODE_condexpr     3
 
 /*
  * The allocation wrappers themselves
@@ -79,6 +80,10 @@
 
 DECL_ALLOCWRAP(nvlist);
 DECL_ALLOCWRAP(attrlist);
+DECL_ALLOCWRAP(condexpr);
+
+/* allow shorter names */
+#define wrap_mk_cx(p) wrap_mk_condexpr(p)
 
 /*
  * Macros for allocating new objects
@@ -99,21 +104,24 @@
 #define        new_nsx(n,s,x)  new0(n, s, NULL, 0, x)
 #define        new_i(i)        new0(NULL, NULL, NULL, i, NULL)
 
-#define        fx_atom(s)      new0(s, NULL, NULL, FX_ATOM, NULL)
-#define        fx_not(e)       new0(NULL, NULL, NULL, FX_NOT, e)
-#define        fx_and(e1, e2)  new0(NULL, NULL, e1, FX_AND, e2)
-#define        fx_or(e1, e2)   new0(NULL, NULL, e1, FX_OR, e2)
-
-/* new style, type-polymorphic */
+/* new style, type-polymorphic; ordinary and for types with multiple flavors */
 #define MK0(t)         wrap_mk_##t(mk_##t())
 #define MK1(t, a0)     wrap_mk_##t(mk_##t(a0))
 #define MK2(t, a0, a1) wrap_mk_##t(mk_##t(a0, a1))
 
+#define MKF0(t, f)             wrap_mk_##t(mk_##t##_##f())
+#define MKF1(t, f, a0)         wrap_mk_##t(mk_##t##_##f(a0))
+#define MKF2(t, f, a0, a1)     wrap_mk_##t(mk_##t##_##f(a0, a1))
+
 /*
  * Data constructors
  */
 
 static struct attrlist *mk_attrlist(struct attrlist *, struct attr *);
+static struct condexpr *mk_cx_atom(const char *);
+static struct condexpr *mk_cx_not(struct condexpr *);
+static struct condexpr *mk_cx_and(struct condexpr *, struct condexpr *);
+static struct condexpr *mk_cx_or(struct condexpr *, struct condexpr *);
 
 /*
  * Other private functions
@@ -135,6 +143,7 @@
        struct  deva *deva;
        struct  nvlist *list;
        struct attrlist *attrlist;
+       struct condexpr *condexpr;
        const char *str;
        struct  numconst num;
        int64_t val;
@@ -162,8 +171,9 @@
 %token <str> PATHNAME QSTRING WORD EMPTYSTRING
 %token ENDDEFS
 
-%type  <list>  fopts condexpr condatom
-%type  <list>  cond_or_expr cond_and_expr cond_prefix_expr cond_base_expr
+%type  <condexpr>      fopts condexpr condatom
+%type  <condexpr>      cond_or_expr cond_and_expr cond_prefix_expr
+%type  <condexpr>       cond_base_expr
 %type  <str>   fs_spec
 %type  <val>   fflgs fflag oflgs oflag
 %type  <str>   rule
@@ -798,29 +808,29 @@
 
 cond_or_expr:
          cond_and_expr
-       | cond_or_expr '|' cond_and_expr        { $$ = fx_or($1, $3); }
+       | cond_or_expr '|' cond_and_expr        { $$ = MKF2(cx, or, $1, $3); }



Home | Main Index | Thread Index | Old Index