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 "expression.h"
36 #include "conf_parser.h"
37 #include "conf_htable.h"
38 #include "parser_common.h"
39 #include "dotconfig.h"
42 static int confnode_if_new(struct confnode *n, const struct line *line);
43 static int confnode_if_close_dir(struct confnode *parent,
44 const struct line *line);
45 static int confnode_if_dotconfig_write(const struct confnode *n, FILE *f);
46 static int confnode_if_get_default_strvalue(const struct confnode *n, char *buf,
48 static const char *confnode_if_get_type_str(const struct confnode *n);
49 static void confnode_if_display_short(const struct confnode *n);
50 static void confnode_if_display_long(const struct confnode *n);
52 static struct confnode_type confnode_if = {
54 .new = confnode_if_new,
57 .close_dir = confnode_if_close_dir,
58 .dotconfig_write = confnode_if_dotconfig_write,
59 .strvalue_to_boolvalue = NULL,
60 .set_user_strvalue = NULL,
61 .get_user_strvalue = NULL,
62 .set_default_strvalue = NULL,
63 .get_default_strvalue = confnode_if_get_default_strvalue,
64 .get_type_str = confnode_if_get_type_str,
65 .display_short = confnode_if_display_short,
66 .display_long = confnode_if_display_long,
70 /* Create a new node */
71 static int confnode_if_new(struct confnode *n, const struct line *line)
74 tok = TAILQ_FIRST(&line->token_list);
76 if (strcmp(tok->str, "if") || line->n_token != 2)
79 tok = TAILQ_NEXT(tok, next);
80 /* XXX only supports an OPTNAME, not an expression */
81 /* condition is stored in value field, not in name */
82 snprintf(n->value, sizeof(n->value), "%s", tok->str);
83 n->ops = &confnode_if.ops;
84 n->flags = CONFNODE_F_INVISIBLE |
85 CONFNODE_F_FORCE_CHILD_DEPS |
87 CONFNODE_F_IS_DIR; /* XXX */
91 /* Parse a line and check if it closes the current node ,
92 * "endmenu" closes "menu" node): in this case return 0. Else, return
94 static int confnode_if_close_dir(struct confnode *parent,
95 const struct line *line)
99 tok = TAILQ_FIRST(&line->token_list);
101 return -1; /* should not happen */
103 /* syntax is: endif */
104 if (!strcmp(tok->str, "endif") && line->n_token == 1) {
111 /* write config value in file f in a dotconfig-like manner. Return -1
113 static int confnode_if_dotconfig_write(const struct confnode *n, FILE *f)
115 const struct confnode *c;
118 val = confnode_get_value(n, NULL, 0);
122 /* condition is wrong, nothing to dump */
126 /* else, just dump children */
127 TAILQ_FOREACH(c, &n->children, next) {
128 if (confnode_dotconfig_write(c, f) < 0)
134 static int confnode_if_get_default_strvalue(const struct confnode *n, char *buf,
137 const struct confnode *n2;
139 n2 = conf_htable_lookup(n->value);
143 /* XXX evaluate expression here instead */
144 return confnode_get_value(n2, buf, buflen);
147 /* Return a string identifying the node type ("config", "menuconfig",
149 static const char *confnode_if_get_type_str(const struct confnode *n)
154 /* Print a one-line summary of the node. Used in case of 'ls'. */
155 static void confnode_if_display_short(const struct confnode *n)
160 /* Print a detailed view of the node. */
161 static void confnode_if_display_long(const struct confnode *n)
166 /* register the node type */
167 void confnode_if_register(void)
169 TAILQ_INSERT_TAIL(&confnode_type_list, &confnode_if, next);