genconf: fix dotconfig_write methods
[libcmdline.git] / src / genconf / confnode_menuconfig.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_menuconfig_new(struct confnode *n, const struct line *line);
43 static int confnode_menuconfig_close_dir(struct confnode *parent,
44                                          const struct line *line);
45 static int confnode_menuconfig_dotconfig_write(const struct confnode *n, FILE *f);
46 static int confnode_menuconfig_set_user_strvalue(struct confnode *n,
47                                               const char *strvalue);
48 static int confnode_menuconfig_get_user_strvalue(const struct confnode *n, char *buf,
49                                               unsigned buflen);
50 static int confnode_menuconfig_set_default_strvalue(struct confnode *n,
51                                               const char *strvalue);
52 static int confnode_menuconfig_get_default_strvalue(const struct confnode *n, char *buf,
53                                               unsigned buflen);
54 static const char *confnode_menuconfig_get_type_str(const struct confnode *n);
55 static void confnode_menuconfig_display_short(const struct confnode *n);
56
57 static struct confnode_type confnode_menuconfig = {
58         .ops = {
59                 .new = confnode_menuconfig_new,
60                 .free = NULL,
61                 .add_attr = NULL,
62                 .close_dir = confnode_menuconfig_close_dir,
63                 .dotconfig_write = confnode_menuconfig_dotconfig_write,
64                 .strvalue_to_boolvalue = NULL,
65                 .set_user_strvalue = confnode_menuconfig_set_user_strvalue,
66                 .get_user_strvalue = confnode_menuconfig_get_user_strvalue,
67                 .set_default_strvalue = confnode_menuconfig_set_default_strvalue,
68                 .get_default_strvalue = confnode_menuconfig_get_default_strvalue,
69                 .get_type_str = confnode_menuconfig_get_type_str,
70                 .display_short = confnode_menuconfig_display_short,
71                 .display_long = NULL,
72         }
73 };
74
75 /* Create a new node */
76 static int confnode_menuconfig_new(struct confnode *n, const struct line *line)
77 {
78         struct token *tok;
79         tok = TAILQ_FIRST(&line->token_list);
80
81         if (strcmp(tok->str, "menuconfig") || line->n_token != 2)
82                 return -1;
83
84         tok = TAILQ_NEXT(tok, next);
85         snprintf(n->name, sizeof(n->name), "%s", tok->str);
86         n->ops = &confnode_menuconfig.ops;
87         n->flags = CONFNODE_F_IS_DIR |
88                 CONFNODE_F_FORCE_CHILD_DEPS |
89                 CONFNODE_F_BOOL;
90         return 0;
91 }
92
93 /* Parse a line and check if it closes the current node ,
94  * "endmenu" closes "menu" node): in this case return 0. Else, return
95  * -1. */
96 static int confnode_menuconfig_close_dir(struct confnode *parent,
97                                          const struct line *line)
98 {
99         struct token *tok;
100
101         tok = TAILQ_FIRST(&line->token_list);
102         if (tok == NULL)
103                 return -1; /* should not happen */
104
105         /* syntax is: endmenuconfig */
106         if (!strcmp(tok->str, "endmenuconfig") && line->n_token == 1) {
107                 //printf("endmenuconfig\n");
108                 return 0;
109         }
110         return -1;
111 }
112
113 /* write config value in file f in a dotconfig-like manner. Return 0
114  * on success. */
115 static int confnode_menuconfig_dotconfig_write(const struct confnode *n, FILE *f)
116 {
117         const struct confnode *c;
118         int val;
119
120         if (fprintf(f, "#\n# -- %s\n#\n", n->prompt) < 0)
121                 return -1;
122
123         val = confnode_get_value(n, NULL, 0);
124         if (val < 0)
125                 return -1;
126
127         if (val == 0) {
128                 if (fprintf(f, "# CONFIG_%s is not set\n", n->name) < 0)
129                         return -1;
130                 return 0;
131         }
132
133         if (fprintf(f, "CONFIG_%s=y\n", n->name) < 0)
134                 return -1;
135
136         TAILQ_FOREACH(c, &n->children, next) {
137                 if (confnode_dotconfig_write(c, f) < 0)
138                         return -1;
139         }
140         return 0;
141 }
142
143 /* Set the string value of the node n. Return 0 on success, or -1 if
144  * the value cannot be set. */
145 static int confnode_menuconfig_set_user_strvalue(struct confnode *n,
146                                               const char *strvalue)
147 {
148         if (strcmp(strvalue, "y") && strcmp(strvalue, "n"))
149                 return -1;
150         snprintf(n->value, sizeof(n->value), "%s", strvalue);
151         return 0;
152 }
153
154 /* Get the string value that the user asked to set for this node
155  * 'n'. This function does not check if dependancies are met. Return 0
156  * on success, in this case the string is copied in buffer 'buf' of
157  * len 'buflen'. Return -1 if the value cannot be read. */
158 static int confnode_menuconfig_get_user_strvalue(const struct confnode *n, char *buf,
159                                               unsigned buflen)
160 {
161         if (strcmp(n->value, "") == 0)
162                 return -1;
163         if (!strcmp(n->value, "y")) {
164                 snprintf(buf, buflen, "y");
165                 return 0;
166         }
167         else {
168                 snprintf(buf, buflen, "n");
169                 return 0;
170         }
171 }
172
173 /* Set the default string value of the node n. Return 0 on success, or
174  * -1 if the value cannot be set. */
175 static int confnode_menuconfig_set_default_strvalue(struct confnode *n,
176                                               const char *strvalue)
177 {
178         if (strcmp(strvalue, "y") && strcmp(strvalue, "n"))
179                 return -1;
180         snprintf(n->default_value, sizeof(n->default_value), "%s", strvalue);
181         return 0;
182 }
183
184 /* Get the default string value for this node 'n'. This function does
185  * not check if dependancies are met. Return 0 on success, in this
186  * case the string is copied in buffer 'buf' of len 'buflen'. Return
187  * -1 if the value cannot be read. */
188 static int confnode_menuconfig_get_default_strvalue(const struct confnode *n, char *buf,
189                                               unsigned buflen)
190 {
191         if (!strcmp(n->default_value, "y")) {
192                 snprintf(buf, buflen, "y");
193                 return 0;
194         }
195         else {
196                 snprintf(buf, buflen, "n");
197                 return 0;
198         }
199 }
200
201 /* Return a string identifying the node type ("config", "menuconfig",
202  * "choice", ...) */
203 static const char *confnode_menuconfig_get_type_str(const struct confnode *n)
204 {
205         return "menuconfig";
206 }
207
208 /* Print a one-line summary of the node. Used in case of 'ls'. */
209 static void confnode_menuconfig_display_short(const struct confnode *n)
210 {
211         char c;
212         if (confnode_get_boolvalue(n) == 0) {
213                 if (confnode_get_user_boolvalue(n) <= 0)
214                         c = ' ';
215                 else
216                         c = 'd';
217         }
218         else {
219                 c = '*';
220         }
221
222         printf("[%c]    -> %s: %s\n", c, n->name, n->prompt);
223 }
224
225 /* register the node type */
226 void confnode_menuconfig_register(void)
227 {
228         TAILQ_INSERT_TAIL(&confnode_type_list, &confnode_menuconfig, next);
229 }