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>
36 #include "cfzy_expr.h"
37 #include "cfzy_confnode.h"
39 #define LOG(level, fmt, args...) \
40 CFZY_LOG("confnode_choiceconfig", level, fmt, ##args)
42 /* Return a string identifying the node type ("config", "menuconfig",
44 static const char *choiceconfig_get_type_str(const struct cfzy_confnode *n)
47 return "choiceconfig";
50 static int choiceconfig_str2bool(const struct cfzy_confnode *n,
55 /* NULL or "n" means disabled */
56 if (value == NULL || !strcmp("n", value))
59 if (!strcmp("y", value))
65 static int choiceconfig_set_uservalue(struct cfzy_confnode *n,
68 struct cfzy_confnode *c;
71 return cfzy_confnode_set_uservalue(n->parent, NULL);
73 if (!strcmp(value, "y"))
74 return cfzy_confnode_set_uservalue(n->parent, n->name);
76 if (!strcmp(value, "n")) {
77 /* node is already set to "n", do nothing so we won't
78 * change the selected choice */
79 if (!strcmp(n->user_value, "n"))
82 /* else just set to 'y' the first brother that we find */
83 TAILQ_FOREACH(c, &n->children, child_next) {
88 /* no brother found */
92 return cfzy_confnode_set_uservalue(c, "y");
98 static struct cfzy_confnode_ops choiceconfig_ops = {
102 .str2bool = choiceconfig_str2bool,
103 .dotconfig_write = NULL,
105 .get_type_str = choiceconfig_get_type_str,
106 .set_uservalue = choiceconfig_set_uservalue,
109 /* Create a new node */
110 enum cfzy_parse_return
111 cfzy_confnode_choiceconfig_new(struct cfzy_confnode *n,
112 const struct cfzy_token_list *tklist)
114 struct cfzy_token *tok;
116 tok = TAILQ_FIRST(&tklist->list);
118 if (strcmp(tok->str, "choiceconfig") || tklist->n_token != 2)
121 tok = TAILQ_NEXT(tok, next);
122 n->ops = &choiceconfig_ops;
123 n->flags = CFZY_F_NO_SET_DEFAULT | CFZY_F_VAL_IS_BOOL;
125 n->name = strdup(tok->str);