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_intconfig_new(struct confnode *n, const struct line *line);
43 static int confnode_intconfig_strvalue_to_boolvalue(const struct confnode *n,
44 const char *strvalue);
45 static int confnode_intconfig_set_user_strvalue(struct confnode *n,
46 const char *strvalue);
47 static int confnode_intconfig_get_user_strvalue(const struct confnode *n, char *buf,
49 static int confnode_intconfig_set_default_strvalue(struct confnode *n,
50 const char *strvalue);
51 static int confnode_intconfig_get_default_strvalue(const struct confnode *n, char *buf,
53 static const char *confnode_intconfig_get_type_str(const struct confnode *n);
54 static void confnode_intconfig_display_short(const struct confnode *n);
56 static struct confnode_type confnode_intconfig = {
58 .new = confnode_intconfig_new,
60 .add_attr = NULL, /* XXX range, displayhex */
62 .dotconfig_write = NULL,
63 .strvalue_to_boolvalue = confnode_intconfig_strvalue_to_boolvalue,
64 .set_user_strvalue = confnode_intconfig_set_user_strvalue,
65 .get_user_strvalue = confnode_intconfig_get_user_strvalue,
66 .set_default_strvalue = confnode_intconfig_set_default_strvalue,
67 .get_default_strvalue = confnode_intconfig_get_default_strvalue,
68 .get_type_str = confnode_intconfig_get_type_str,
69 .display_short = confnode_intconfig_display_short,
74 /* Create a new node */
75 static int confnode_intconfig_new(struct confnode *n, const struct line *line)
78 tok = TAILQ_FIRST(&line->token_list);
80 if (strcmp(tok->str, "intconfig") || line->n_token != 2)
83 tok = TAILQ_NEXT(tok, next);
84 snprintf(n->name, sizeof(n->name), "%s", tok->str);
85 snprintf(n->default_value, sizeof(n->default_value), "0");
87 n->ops = &confnode_intconfig.ops;
88 n->flags = CONFNODE_F_INT;
89 snprintf(n->default_value, sizeof(n->default_value), "0");
93 /* Convert string value into a boolean value (mainly used for
94 * dependancies). Return -1 on error, else return the boolean value (0
96 static int confnode_intconfig_strvalue_to_boolvalue(const struct confnode *n,
102 /* XXX support hex and bin */
103 val = strtoul(strvalue, &end, 0);
104 if ((strvalue[0] == '\0') || (end == NULL) || (*end != '\0'))
109 /* Set the string value of the node n. Return 0 on success, or -1 if
110 * the value cannot be set. */
111 static int confnode_intconfig_set_user_strvalue(struct confnode *n,
112 const char *strvalue)
115 int val __attribute__((unused));
117 /* XXX support hex and bin */
118 val = strtoul(strvalue, &end, 0);
119 if ((strvalue[0] == '\0') || (end == NULL) || (*end != '\0'))
121 snprintf(n->value, sizeof(n->value), "%s", strvalue);
125 /* Get the string value that the user asked to set for this node
126 * 'n'. This function does not check if dependancies are met. Return 0
127 * on success, in this case the string is copied in buffer 'buf' of
128 * len 'buflen'. Return -1 if the value cannot be read. */
129 static int confnode_intconfig_get_user_strvalue(const struct confnode *n, char *buf,
132 if (strcmp(n->value, "") == 0)
134 snprintf(buf, buflen, "%s", n->value);
138 /* Set the default string value of the node n. Return 0 on success, or
139 * -1 if the value cannot be set. */
140 static int confnode_intconfig_set_default_strvalue(struct confnode *n,
141 const char *strvalue)
144 int val __attribute__((unused));
146 /* XXX support hex and bin */
147 val = strtoul(strvalue, &end, 0);
148 if ((strvalue[0] == '\0') || (end == NULL) || (*end != '\0'))
150 snprintf(n->default_value, sizeof(n->default_value), "%s", strvalue);
154 /* Get the default string value for this node 'n'. This function does
155 * not check if dependancies are met. Return 0 on success, in this
156 * case the string is copied in buffer 'buf' of len 'buflen'. Return
157 * -1 if the value cannot be read. */
158 static int confnode_intconfig_get_default_strvalue(const struct confnode *n, char *buf,
161 if (strcmp(n->default_value, "") == 0)
163 snprintf(buf, buflen, "%s", n->default_value);
167 /* Return a string identifying the node type ("config", "menuconfig",
169 static const char *confnode_intconfig_get_type_str(const struct confnode *n)
174 /* Print a one-line summary of the node. Used in case of 'ls'. */
175 static void confnode_intconfig_display_short(const struct confnode *n)
180 memset(buf, 0, sizeof(buf));
183 confnode_get_value(n, buf, 0);
184 confnode_get_value(n, &buf[1], INTBUFLEN-1);
185 if (strlen(buf) == INTBUFLEN-1) { /* buffer full */
186 buf[INTBUFLEN-4] = '.';
187 buf[INTBUFLEN-3] = '.';
188 buf[INTBUFLEN-2] = '\0';
192 while (strlen(buf) < sizeof(buf) - 1)
195 printf("%s %s: %s\n", buf, n->name, n->prompt);
198 /* register the node type */
199 void confnode_intconfig_register(void)
201 TAILQ_INSERT_TAIL(&confnode_type_list, &confnode_intconfig, next);