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 #define MAX_DISP_SIZE 16
44 static int confnode_choice_new(struct confnode *n, const struct line *line);
45 static int confnode_choice_close_dir(struct confnode *parent,
46 const struct line *line);
47 static int confnode_choice_strvalue_to_boolvalue(const struct confnode *n,
48 const char *strvalue);
49 static int confnode_choice_set_user_strvalue(struct confnode *n,
50 const char *strvalue);
51 static int confnode_choice_get_user_strvalue(const struct confnode *n, char *buf,
53 static int confnode_choice_set_default_strvalue(struct confnode *n,
54 const char *strvalue);
55 static int confnode_choice_get_default_strvalue(const struct confnode *n, char *buf,
57 static const char *confnode_choice_get_type_str(const struct confnode *n);
58 static void confnode_choice_display_short(const struct confnode *n);
60 static struct confnode_type confnode_choice = {
62 .new = confnode_choice_new,
65 .close_dir = confnode_choice_close_dir,
66 .dotconfig_write = NULL,
67 .strvalue_to_boolvalue = confnode_choice_strvalue_to_boolvalue,
68 .set_user_strvalue = confnode_choice_set_user_strvalue,
69 .get_user_strvalue = confnode_choice_get_user_strvalue,
70 .set_default_strvalue = confnode_choice_set_default_strvalue,
71 .get_default_strvalue = confnode_choice_get_default_strvalue,
72 .get_type_str = confnode_choice_get_type_str,
73 .display_short = confnode_choice_display_short,
78 /* Create a new node */
79 static int confnode_choice_new(struct confnode *n, const struct line *line)
82 tok = TAILQ_FIRST(&line->token_list);
84 if (strcmp(tok->str, "choice") || line->n_token != 2)
87 tok = TAILQ_NEXT(tok, next);
88 snprintf(n->name, sizeof(n->name), "%s", tok->str);
89 n->ops = &confnode_choice.ops;
90 n->flags = CONFNODE_F_IS_DIR |
91 /* CONFNODE_F_NO_SET_DEPS | */ /* XXX ? */
93 CONFNODE_F_QUOTE_VALUE;
97 /* Parse a line and check if it closes the current node ,
98 * "endmenu" closes "menu" node): in this case return 0. Else, return
100 static int confnode_choice_close_dir(struct confnode *parent,
101 const struct line *line)
105 tok = TAILQ_FIRST(&line->token_list);
107 return -1; /* should not happen */
109 /* syntax is: endchoice */
110 if (!strcmp(tok->str, "endchoice") && line->n_token == 1) {
111 //printf("endchoice\n");
117 /* Convert string value into boolean value (mainly used for
118 * dependancies). Return -1 on error, else return the boolean value (0
120 static int confnode_choice_strvalue_to_boolvalue(const struct confnode *n,
121 const char *strvalue)
127 /* Set the string value of the node n. Return 0 on success, or -1 if
128 * the value cannot be set. */
129 static int confnode_choice_set_user_strvalue(struct confnode *n,
130 const char *strvalue)
134 TAILQ_FOREACH(c, &n->children, next) {
135 if (strcmp(c->prompt, strvalue) == 0)
142 snprintf(n->value, sizeof(n->value), "%s", strvalue);
146 /* Get the string value that the user asked to set for this node
147 * 'n'. This function does not check if dependancies are met. Return 0
148 * on success, in this case the string is copied in buffer 'buf' of
149 * len 'buflen'. Return -1 if the value cannot be read. */
150 static int confnode_choice_get_user_strvalue(const struct confnode *n, char *buf,
153 if (strcmp("", n->value) == 0)
155 snprintf(buf, buflen, n->value);
159 /* Set the default string value of the node n. Return 0 on success, or
160 * -1 if the value cannot be set. */
161 static int confnode_choice_set_default_strvalue(struct confnode *n,
162 const char *strvalue)
164 snprintf(n->default_value, sizeof(n->default_value), "%s", strvalue);
168 /* Get the default string value for this node 'n'. This function does
169 * not check if dependancies are met. Return 0 on success, in this
170 * case the string is copied in buffer 'buf' of len 'buflen'. Return
171 * -1 if the value cannot be read. */
172 static int confnode_choice_get_default_strvalue(const struct confnode *n, char *buf,
177 if (TAILQ_EMPTY(&n->children))
180 /* this is the "default" default :) */
181 snprintf(buf, buflen, "%s", TAILQ_FIRST(&n->children)->prompt);
183 TAILQ_FOREACH(c, &n->children, next) {
184 if (strcmp(c->prompt, buf) == 0) {
185 snprintf(buf, buflen, "%s", c->prompt);
192 /* Return a string identifying the node type ("config", "menuconfig",
194 static const char *confnode_choice_get_type_str(const struct confnode *n)
199 /* Print a one-line summary of the node. Used in case of 'ls'. */
200 static void confnode_choice_display_short(const struct confnode *n)
204 printf("choice -> %s ", n->name);
205 if (confnode_get_value(n, buf, sizeof(buf)) >= 0) {
206 if (strlen(buf) > MAX_DISP_SIZE) {
207 buf[MAX_DISP_SIZE-3] = '.';
208 buf[MAX_DISP_SIZE-2] = '.';
209 buf[MAX_DISP_SIZE-1] = '.';
210 buf[MAX_DISP_SIZE] = '\0';
213 printf("(%s): %s\n", buf, n->prompt);
216 /* register the node type */
217 void confnode_choice_register(void)
219 TAILQ_INSERT_TAIL(&confnode_type_list, &confnode_choice, next);