initial revision
[ucgine.git] / tools / cfzy / libconfizery / cfzy_confnode_if.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_list.h"
36 #include "cfzy_expr.h"
37 #include "cfzy_confnode.h"
38
39 /* write config value in file f in a dotconfig-like manner. Return -1
40  * on error. */
41 static int if_dotconfig_write(const struct cfzy_confnode *n, FILE *f)
42 {
43         const struct cfzy_confnode *c;
44         int val;
45
46         val = cfzy_confnode_str2bool(n, n->effective_value);
47         if (val < 0)
48                 return -1;
49
50         /* condition is wrong, nothing to dump */
51         if (val == 0)
52                 return 0;
53
54         /* else, just dump children */
55         TAILQ_FOREACH(c, &n->children, child_next) {
56                 if (cfzy_confnode_dotconfig_write(c, f) < 0)
57                         return -1;
58         }
59         return 0;
60 }
61
62 /* Return a string identifying the node type ("config", "menuconfig",
63  * "choice", ...) */
64 static const char *if_get_type_str(const struct cfzy_confnode *n)
65 {
66         (void)n;
67         return "if";
68 }
69
70 /* evaluate the expression stored in the node */
71 static int if_str2bool(const struct cfzy_confnode *n, const char *value)
72 {
73         struct cfzy_list_elt *e;
74         struct cfzy_expr *expr;
75         int (*get_boolvalue)(const char *, const struct cfzy_conftree *);
76         int (*get_boolvalue_casted)(const char *, void *);
77
78         (void)n;
79
80         /* should not happen */
81         if (value != NULL)
82                 return -1;
83
84         e = TAILQ_FIRST(n->expr_deps);
85         if (e == NULL)
86                 return -1;
87         expr = e->ptr;
88
89         /* we use an intermediate variable to have a compiler warning
90          * if the prototype of cfzy_confnode_get_boolvalue changes. We
91          * know that that these types are compatible here. */
92         get_boolvalue = cfzy_confnode_get_boolvalue;
93         get_boolvalue_casted = (void *)get_boolvalue;
94
95         return cfzy_expr_eval(expr, get_boolvalue_casted, n->conftree);
96 }
97
98 static struct cfzy_confnode_ops if_ops = {
99         .free = NULL,
100         .add_attr = NULL,
101         .close = NULL,
102         .str2bool = if_str2bool,
103         .dotconfig_write = if_dotconfig_write,
104         .c_hdr_write = NULL,
105         .get_type_str = if_get_type_str,
106         .set_uservalue = NULL,
107 };
108
109 /* Create a new node */
110 enum cfzy_parse_return cfzy_confnode_if_new(struct cfzy_confnode *n,
111                                             const struct cfzy_token_list *tklist)
112 {
113         struct cfzy_token *tok;
114         struct cfzy_expr *exp;
115
116         tok = TAILQ_FIRST(&tklist->list);
117
118         if (strcmp(tok->str, "if") || tklist->n_token < 2)
119                 return NO_MATCH;
120
121         tok = TAILQ_NEXT(tok, next); /* points to expression */
122         exp = cfzy_expr_parse(tklist->linebuf + tok->offset);
123         if (exp == NULL)
124                 return ERROR;
125
126         if (cfzy_list_add_tail(n->expr_deps, exp) < 0) {
127                 cfzy_expr_free(exp);
128                 return ERROR;
129         }
130
131         n->ops = &if_ops;
132         n->flags = CFZY_F_INVISIBLE |
133                 CFZY_F_NO_NAME |
134                 CFZY_F_NO_SET_DEPS |
135                 CFZY_F_NO_SET_PROMPT |
136                 CFZY_F_NO_SET_DEFAULT |
137                 CFZY_F_NO_SET_VALUE |
138                 CFZY_F_IS_DIR;
139
140         return SUCCESS;
141 }