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