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_strconfig_new(struct confnode *n, const struct line *line);
43 static int confnode_strconfig_strvalue_to_boolvalue(const struct confnode *n,
44 const char *strvalue);
45 static int confnode_strconfig_set_user_strvalue(struct confnode *n,
46 const char *strvalue);
47 static int confnode_strconfig_get_user_strvalue(const struct confnode *n, char *buf,
49 static int confnode_strconfig_set_default_strvalue(struct confnode *n,
50 const char *strvalue);
51 static int confnode_strconfig_get_default_strvalue(const struct confnode *n, char *buf,
53 static const char *confnode_strconfig_get_type_str(const struct confnode *n);
54 static void confnode_strconfig_display_short(const struct confnode *n);
56 static struct confnode_type confnode_strconfig = {
58 .new = confnode_strconfig_new,
62 .dotconfig_write = NULL,
63 .strvalue_to_boolvalue = confnode_strconfig_strvalue_to_boolvalue,
64 .set_user_strvalue = confnode_strconfig_set_user_strvalue,
65 .get_user_strvalue = confnode_strconfig_get_user_strvalue,
66 .set_default_strvalue = confnode_strconfig_set_default_strvalue,
67 .get_default_strvalue = confnode_strconfig_get_default_strvalue,
68 .get_type_str = confnode_strconfig_get_type_str,
69 .display_short = confnode_strconfig_display_short,
74 /* Create a new node */
75 static int confnode_strconfig_new(struct confnode *n, const struct line *line)
78 tok = TAILQ_FIRST(&line->token_list);
80 if (strcmp(tok->str, "strconfig") || line->n_token != 2)
83 tok = TAILQ_NEXT(tok, next);
84 snprintf(n->name, sizeof(n->name), "%s", tok->str);
85 n->ops = &confnode_strconfig.ops;
86 n->flags = CONFNODE_F_STR |
87 CONFNODE_F_QUOTE_VALUE;
88 n->default_value[0] = '\0';
92 /* Convert string value into boolean value (mainly used for
93 * dependancies). Return -1 on error, else return the boolean value (0
95 static int confnode_strconfig_strvalue_to_boolvalue(const struct confnode *n,
98 if (strcmp("", strvalue) == 0)
103 /* Set the string value of the node n. Return 0 on success, or -1 if
104 * the value cannot be set. */
105 static int confnode_strconfig_set_user_strvalue(struct confnode *n,
106 const char *strvalue)
108 snprintf(n->value, sizeof(n->value), "%s", strvalue);
112 /* Get the string value that the user asked to set for this node
113 * 'n'. This function does not check if dependancies are met. Return 0
114 * on success, in this case the string is copied in buffer 'buf' of
115 * len 'buflen'. Return -1 if the value cannot be read. */
116 static int confnode_strconfig_get_user_strvalue(const struct confnode *n, char *buf,
119 snprintf(buf, buflen, "%s", n->value);
123 /* Set the default string value of the node n. Return 0 on success, or
124 * -1 if the value cannot be set. */
125 static int confnode_strconfig_set_default_strvalue(struct confnode *n,
126 const char *strvalue)
128 snprintf(n->default_value, sizeof(n->default_value), "%s", strvalue);
132 /* Get the default string value for this node 'n'. This function does
133 * not check if dependancies are met. Return 0 on success, in this
134 * case the string is copied in buffer 'buf' of len 'buflen'. Return
135 * -1 if the value cannot be read. */
136 static int confnode_strconfig_get_default_strvalue(const struct confnode *n, char *buf,
139 snprintf(buf, buflen, "%s", n->default_value);
143 /* Return a string identifying the node type ("config", "menuconfig",
145 static const char *confnode_strconfig_get_type_str(const struct confnode *n)
150 /* Print a one-line summary of the node. Used in case of 'ls'. */
151 static void confnode_strconfig_display_short(const struct confnode *n)
156 memset(buf, 0, sizeof(buf));
159 confnode_get_value(n, &buf[1], STRBUFLEN-1);
160 if (strlen(buf) == STRBUFLEN-1) { /* buffer full */
161 buf[STRBUFLEN-4] = '.';
162 buf[STRBUFLEN-3] = '.';
163 buf[STRBUFLEN-2] = '\0';
167 while (strlen(buf) < sizeof(buf) - 1)
170 printf("%s %s: %s\n", buf, n->name, n->prompt);
173 /* register the node type */
174 void confnode_strconfig_register(void)
176 TAILQ_INSERT_TAIL(&confnode_type_list, &confnode_strconfig, next);