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_choiceconfig_new(struct confnode *n, const struct line *line);
43 static int confnode_choiceconfig_set_user_strvalue(struct confnode *n,
44 const char *strvalue);
45 static int confnode_choiceconfig_get_user_strvalue(const struct confnode *n, char *buf,
47 static int confnode_choiceconfig_set_default_strvalue(struct confnode *n,
48 const char *strvalue);
49 static int confnode_choiceconfig_get_default_strvalue(const struct confnode *n, char *buf,
51 static const char *confnode_choiceconfig_get_type_str(const struct confnode *n);
52 static void confnode_choiceconfig_display_short(const struct confnode *n);
54 static struct confnode_type confnode_choiceconfig = {
56 .new = confnode_choiceconfig_new,
60 .dotconfig_write = NULL,
61 .strvalue_to_boolvalue = NULL,
62 .set_user_strvalue = confnode_choiceconfig_set_user_strvalue,
63 .get_user_strvalue = confnode_choiceconfig_get_user_strvalue,
64 .set_default_strvalue = confnode_choiceconfig_set_default_strvalue,
65 .get_default_strvalue = confnode_choiceconfig_get_default_strvalue,
66 .get_type_str = confnode_choiceconfig_get_type_str,
67 .display_short = confnode_choiceconfig_display_short,
72 /* Create a new node */
73 static int confnode_choiceconfig_new(struct confnode *n, const struct line *line)
76 tok = TAILQ_FIRST(&line->token_list);
78 /* XXX we should have the parent node */
80 if (strcmp(tok->str, "choiceconfig") || line->n_token != 2)
83 tok = TAILQ_NEXT(tok, next);
84 snprintf(n->name, sizeof(n->name), "%s", tok->str);
85 n->ops = &confnode_choiceconfig.ops;
86 n->flags = CONFNODE_F_BOOL |
87 CONFNODE_F_NO_SET_DEFAULT;
89 snprintf(n->default_value, sizeof(n->default_value), "n");
93 /* Set the string value of the node n. Return 0 on success, or -1 if
94 * the value cannot be set. */
95 static int confnode_choiceconfig_set_user_strvalue(struct confnode *n,
98 if (strcmp(strvalue, "y") && strcmp(strvalue, "n"))
100 return confnode_set_user_strvalue(n->parent, n->prompt);
103 /* Get the string value that the user asked to set for this node
104 * 'n'. This function does not check if dependancies are met. Return 0
105 * on success, in this case the string is copied in buffer 'buf' of
106 * len 'buflen'. Return -1 if the value cannot be read. */
107 static int confnode_choiceconfig_get_user_strvalue(const struct confnode *n, char *buf,
110 if (confnode_get_user_strvalue(n->parent, buf, buflen) < 0)
112 if (strcmp(buf, n->prompt) == 0)
113 snprintf(buf, buflen, "y");
115 snprintf(buf, buflen, "n");
119 /* Set the default string value of the node n. Return 0 on success, or
120 * -1 if the value cannot be set. */
121 static int confnode_choiceconfig_set_default_strvalue(struct confnode *n,
122 const char *strvalue)
124 if (strcmp(strvalue, "y") && strcmp(strvalue, "n"))
126 return confnode_set_default_strvalue(n->parent, n->prompt);
129 /* Get the default string value for this node 'n'. This function does
130 * not check if dependancies are met. Return 0 on success, in this
131 * case the string is copied in buffer 'buf' of len 'buflen'. Return
132 * -1 if the value cannot be read. */
133 static int confnode_choiceconfig_get_default_strvalue(const struct confnode *n, char *buf,
136 if (confnode_get_default_strvalue(n->parent, buf, buflen) < 0)
138 if (strcmp(buf, n->prompt) == 0)
139 snprintf(buf, buflen, "y");
141 snprintf(buf, buflen, "n");
145 /* Return a string identifying the node type ("config", "menuconfig",
147 static const char *confnode_choiceconfig_get_type_str(const struct confnode *n)
149 return "choiceconfig";
152 /* Print a one-line summary of the node. Used in case of 'ls'. */
153 static void confnode_choiceconfig_display_short(const struct confnode *n)
156 if (confnode_get_boolvalue(n) == 0) {
157 if (confnode_get_user_boolvalue(n) <= 0)
166 printf("[%c] %s: %s\n", c, n->name, n->prompt);
169 /* register the node type */
170 void confnode_choiceconfig_register(void)
172 TAILQ_INSERT_TAIL(&confnode_type_list, &confnode_choiceconfig, next);