2 * Copyright (c) 2010, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/queue.h>
35 #include "cfzy_list.h"
36 #include "cfzy_expr.h"
37 #include "cfzy_confnode.h"
39 /* write config value in file f in a dotconfig-like manner. Return -1
41 static int if_dotconfig_write(const struct cfzy_confnode *n, FILE *f)
43 const struct cfzy_confnode *c;
46 val = cfzy_confnode_str2bool(n, n->effective_value);
50 /* condition is wrong, nothing to dump */
54 /* else, just dump children */
55 TAILQ_FOREACH(c, &n->children, child_next) {
56 if (cfzy_confnode_dotconfig_write(c, f) < 0)
62 /* Return a string identifying the node type ("config", "menuconfig",
64 static const char *if_get_type_str(const struct cfzy_confnode *n)
70 /* evaluate the expression stored in the node */
71 static int if_str2bool(const struct cfzy_confnode *n, const char *value)
73 struct cfzy_list_elt *e;
74 struct cfzy_expr *expr;
75 int (*get_boolvalue)(const char *, const struct cfzy_conftree *);
76 int (*get_boolvalue_casted)(const char *, void *);
80 /* should not happen */
84 e = TAILQ_FIRST(n->expr_deps);
89 /* we use an intermediate variable to have a compiler warning
90 * if the prototype of cfzy_confnode_get_boolvalue changes. We
91 * know that that these types are compatible here. */
92 get_boolvalue = cfzy_confnode_get_boolvalue;
93 get_boolvalue_casted = (void *)get_boolvalue;
95 return cfzy_expr_eval(expr, get_boolvalue_casted, n->conftree);
98 static struct cfzy_confnode_ops if_ops = {
102 .str2bool = if_str2bool,
103 .dotconfig_write = if_dotconfig_write,
105 .get_type_str = if_get_type_str,
106 .set_uservalue = NULL,
109 /* Create a new node */
110 enum cfzy_parse_return cfzy_confnode_if_new(struct cfzy_confnode *n,
111 const struct cfzy_token_list *tklist)
113 struct cfzy_token *tok;
114 struct cfzy_expr *exp;
116 tok = TAILQ_FIRST(&tklist->list);
118 if (strcmp(tok->str, "if") || tklist->n_token < 2)
121 tok = TAILQ_NEXT(tok, next); /* points to expression */
122 exp = cfzy_expr_parse(tklist->linebuf + tok->offset);
126 if (cfzy_list_add_tail(n->expr_deps, exp) < 0) {
132 n->flags = CFZY_F_INVISIBLE |
135 CFZY_F_NO_SET_PROMPT |
136 CFZY_F_NO_SET_DEFAULT |
137 CFZY_F_NO_SET_VALUE |