9c31664ecf6ce05281256d4f5c06da9771bf37e1
[libcmdline.git] / src / genconf / confnode_intconfig.c
1 /*
2  * Copyright (c) 2010, Olivier MATZ <zer0@droids-corp.org>
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
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.
15  *
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.
26  */
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <ctype.h>
32 #include <unistd.h>
33 #include <sys/queue.h>
34
35 #include "expression.h"
36 #include "conf_parser.h"
37 #include "conf_htable.h"
38 #include "parser_common.h"
39 #include "dotconfig.h"
40 #include "confnode.h"
41
42 static int confnode_intconfig_new(struct confnode *n, const struct line *line);
43 static int confnode_intconfig_set_user_strvalue(struct confnode *n,
44                                               const char *strvalue);
45 static int confnode_intconfig_get_user_strvalue(const struct confnode *n, char *buf,
46                                               unsigned buflen);
47 static int confnode_intconfig_set_default_strvalue(struct confnode *n,
48                                               const char *strvalue);
49 static int confnode_intconfig_get_default_strvalue(const struct confnode *n, char *buf,
50                                               unsigned buflen);
51 static const char *confnode_intconfig_get_type_str(const struct confnode *n);
52 static void confnode_intconfig_display_short(const struct confnode *n);
53
54 static struct confnode_type confnode_intconfig = {
55         .ops = {
56                 .new = confnode_intconfig_new,
57                 .free = NULL,
58                 .add_attr = NULL, /* XXX range, displayhex */
59                 .close_dir = NULL,
60                 .dotconfig_write = NULL,
61                 .strvalue_to_boolvalue = NULL,
62                 .set_user_strvalue = confnode_intconfig_set_user_strvalue,
63                 .get_user_strvalue = confnode_intconfig_get_user_strvalue,
64                 .set_default_strvalue = confnode_intconfig_set_default_strvalue,
65                 .get_default_strvalue = confnode_intconfig_get_default_strvalue,
66                 .get_type_str = confnode_intconfig_get_type_str,
67                 .display_short = confnode_intconfig_display_short,
68                 .display_long = NULL,
69         }
70 };
71
72 /* Create a new node */
73 static int confnode_intconfig_new(struct confnode *n, const struct line *line)
74 {
75         struct token *tok;
76         tok = TAILQ_FIRST(&line->token_list);
77
78         if (strcmp(tok->str, "intconfig") || line->n_token != 2)
79                 return -1;
80
81         tok = TAILQ_NEXT(tok, next);
82         snprintf(n->name, sizeof(n->name), "%s", tok->str);
83         snprintf(n->default_value, sizeof(n->default_value), "0");
84
85         n->ops = &confnode_intconfig.ops;
86         n->flags = CONFNODE_F_INT;
87         snprintf(n->default_value, sizeof(n->default_value), "0");
88         return 0;
89 }
90
91 /* Set the string value of the node n. Return 0 on success, or -1 if
92  * the value cannot be set. */
93 static int confnode_intconfig_set_user_strvalue(struct confnode *n,
94                                                 const char *strvalue)
95 {
96         char *end = NULL;
97         int val;
98         /* XXX support hex and bin */
99         val = strtoul(strvalue, &end, 0);
100         if ((strvalue[0] == '\0') || (end == NULL) || (*end != '\0'))
101                 return -1;
102         snprintf(n->value, sizeof(n->value), "%s", strvalue);
103         return 0;
104 }
105
106 /* Get the string value that the user asked to set for this node
107  * 'n'. This function does not check if dependancies are met. Return 0
108  * on success, in this case the string is copied in buffer 'buf' of
109  * len 'buflen'. Return -1 if the value cannot be read. */
110 static int confnode_intconfig_get_user_strvalue(const struct confnode *n, char *buf,
111                                                 unsigned buflen)
112 {
113         if (strcmp(n->value, "") == 0)
114                 return -1;
115         snprintf(buf, buflen, "%s", n->value);
116         return 0;
117 }
118
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_intconfig_set_default_strvalue(struct confnode *n,
122                                               const char *strvalue)
123 {
124         char *end = NULL;
125         int val;
126         /* XXX support hex and bin */
127         val = strtoul(strvalue, &end, 0);
128         if ((strvalue[0] == '\0') || (end == NULL) || (*end != '\0'))
129                 return -1;
130         snprintf(n->default_value, sizeof(n->default_value), "%s", strvalue);
131         return 0;
132 }
133
134 /* Get the default string value for this node 'n'. This function does
135  * not check if dependancies are met. Return 0 on success, in this
136  * case the string is copied in buffer 'buf' of len 'buflen'. Return
137  * -1 if the value cannot be read. */
138 static int confnode_intconfig_get_default_strvalue(const struct confnode *n, char *buf,
139                                               unsigned buflen)
140 {
141         if (strcmp(n->default_value, "") == 0)
142                 return -1;
143         snprintf(buf, buflen, "%s", n->default_value);
144         return 0;
145 }
146
147 /* Return a string identifying the node type ("config", "menuconfig",
148  * "choice", ...) */
149 static const char *confnode_intconfig_get_type_str(const struct confnode *n)
150 {
151         return "intconfig";
152 }
153
154 /* Print a one-line summary of the node. Used in case of 'ls'. */
155 static void confnode_intconfig_display_short(const struct confnode *n)
156 {
157         int val;
158         val = confnode_get_value(n, NULL, 0);
159         printf("[%4d] %s: %s\n", val, n->name, n->prompt);
160 }
161
162 /* register the node type */
163 void confnode_intconfig_register(void)
164 {
165         TAILQ_INSERT_TAIL(&confnode_type_list, &confnode_intconfig, next);
166 }