From 1e21ea8a40224fc52071f47c5150846f483d227e Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 24 May 2012 00:05:21 -0300 Subject: [PATCH] b2/param.c: new function merge_vars to merge prioritized lists of variables --- b2/param.c | 32 ++++++++++++++++++++++++++++++++ b2/param.h | 1 + 2 files changed, 33 insertions(+) diff --git a/b2/param.c b/b2/param.c index 64855cc..0fb7f64 100644 --- a/b2/param.c +++ b/b2/param.c @@ -144,6 +144,38 @@ const char *var_lookup(const struct param *vars, const char *name) } +/* + * The variables in list "a" have lower priority than those in list "b", + * i.e., a variable in "a" is only picked if there is no variable with the + * same name in "b". + */ + +struct param *merge_vars(const struct param *a, const struct param *b) +{ + const struct param *v; + struct param *res, **last = &res; + + while (a) { + for (v = b; v; v = v->next) + if (a->u.name == v->u.name) + break; + if (!v) { + *last = alloc_type(struct param); + **last = *a; + last = &(*last)->next; + } + a = a->next; + } + for (v = b; v; v = v->next) { + *last = alloc_type(struct param); + **last = *v; + last = &(*last)->next; + } + *last = NULL; + return res; +} + + void free_vars(struct param *vars) { struct param *next; diff --git a/b2/param.h b/b2/param.h index 49ee927..a4d884a 100644 --- a/b2/param.h +++ b/b2/param.h @@ -120,6 +120,7 @@ extern const struct param_ops param_ops_rel; struct param *make_var(const char *name, enum relop op, const char *val); const char *var_lookup(const struct param *vars, const char *name); +struct param *merge_vars(const struct param *a, const struct param *b); void free_vars(struct param *vars); #endif /* !PARAM_H */