initial revision
[ucgine.git] / tools / cfzy / libconfizery / cfzy_confnode_choiceconfig.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 "cfzy_log.h"
36 #include "cfzy_expr.h"
37 #include "cfzy_confnode.h"
38
39 #define LOG(level, fmt, args...)                                \
40         CFZY_LOG("confnode_choiceconfig", level, fmt, ##args)
41
42 /* Return a string identifying the node type ("config", "menuconfig",
43  * "choice", ...) */
44 static const char *choiceconfig_get_type_str(const struct cfzy_confnode *n)
45 {
46         (void)n;
47         return "choiceconfig";
48 }
49
50 static int choiceconfig_str2bool(const struct cfzy_confnode *n,
51                                  const char *value)
52 {
53         (void)n;
54
55         /* NULL or "n" means disabled */
56         if (value == NULL || !strcmp("n", value))
57                 return 0;
58
59         if (!strcmp("y", value))
60                 return 1;
61
62         return -1;
63 }
64
65 static int choiceconfig_set_uservalue(struct cfzy_confnode *n,
66                                       const char *value)
67 {
68         struct cfzy_confnode *c;
69
70         if (value == NULL)
71                 return cfzy_confnode_set_uservalue(n->parent, NULL);
72
73         if (!strcmp(value, "y"))
74                 return cfzy_confnode_set_uservalue(n->parent, n->name);
75
76         if (!strcmp(value, "n")) {
77                 /* node is already set to "n", do nothing so we won't
78                  * change the selected choice */
79                 if (!strcmp(n->user_value, "n"))
80                         return 0;
81
82                 /* else just set to 'y' the first brother that we find */
83                 TAILQ_FOREACH(c, &n->children, child_next) {
84                         if (c != n)
85                                 break;
86                 }
87
88                 /* no brother found */
89                 if (c == NULL)
90                         return -1;
91
92                 return cfzy_confnode_set_uservalue(c, "y");
93         }
94
95         return -1;
96 }
97
98 static struct cfzy_confnode_ops choiceconfig_ops = {
99         .free = NULL,
100         .add_attr = NULL,
101         .close = NULL,
102         .str2bool = choiceconfig_str2bool,
103         .dotconfig_write = NULL,
104         .c_hdr_write = NULL,
105         .get_type_str = choiceconfig_get_type_str,
106         .set_uservalue = choiceconfig_set_uservalue,
107 };
108
109 /* Create a new node */
110 enum cfzy_parse_return
111 cfzy_confnode_choiceconfig_new(struct cfzy_confnode *n,
112                                const struct cfzy_token_list *tklist)
113 {
114         struct cfzy_token *tok;
115
116         tok = TAILQ_FIRST(&tklist->list);
117
118         if (strcmp(tok->str, "choiceconfig") || tklist->n_token != 2)
119                 return NO_MATCH;
120
121         tok = TAILQ_NEXT(tok, next);
122         n->ops = &choiceconfig_ops;
123         n->flags = CFZY_F_NO_SET_DEFAULT | CFZY_F_VAL_IS_BOOL;
124
125         n->name = strdup(tok->str);
126         if (n->name == NULL)
127                 return ERROR;
128
129         return SUCCESS;
130 }