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