4eafa4bd736c84ed1d7df6059205a15401bdf713
[libcmdline.git] / src / genconf / confnode_menu.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_menu_new(struct confnode *n, const struct line *line);
43 static int confnode_menu_close_dir(struct confnode *parent,
44                                    const struct line *line);
45 static int confnode_menu_dotconfig_write(const struct confnode *n, FILE *f);
46 static const char *confnode_menu_get_type_str(const struct confnode *n);
47 static void confnode_menu_display_short(const struct confnode *n);
48 static void confnode_menu_display_long(const struct confnode *n);
49
50 static struct confnode_type confnode_menu = {
51         .ops = {
52                 .new = confnode_menu_new,
53                 .free = NULL,
54                 .add_attr = NULL,
55                 .close_dir = confnode_menu_close_dir,
56                 .dotconfig_write = confnode_menu_dotconfig_write,
57                 .strvalue_to_boolvalue = NULL,
58                 .set_user_strvalue = NULL,
59                 .get_user_strvalue = NULL,
60                 .set_default_strvalue = NULL,
61                 .get_default_strvalue = NULL,
62                 .get_type_str = confnode_menu_get_type_str,
63                 .display_short = confnode_menu_display_short,
64                 .display_long = confnode_menu_display_long,
65         }
66 };
67
68 /* Create a new node */
69 static int confnode_menu_new(struct confnode *n, const struct line *line)
70 {
71         struct token *tok;
72         tok = TAILQ_FIRST(&line->token_list);
73
74         if (strcmp(tok->str, "menu") || line->n_token != 2)
75                 return -1;
76
77         tok = TAILQ_NEXT(tok, next);
78         snprintf(n->name, sizeof(n->name), "%s", tok->str);
79         n->ops = &confnode_menu.ops;
80         n->flags = CONFNODE_F_IS_DIR;
81         return 0;
82 }
83
84 /* Parse a line and check if it closes the current node ,
85  * "endmenu" closes "menu" node): in this case return 0. Else, return
86  * -1. */
87 static int confnode_menu_close_dir(struct confnode *parent,
88                                    const struct line *line)
89 {
90         struct token *tok;
91
92         tok = TAILQ_FIRST(&line->token_list);
93         if (tok == NULL)
94                 return -1; /* should not happen */
95
96         /* syntax is: endmenu */
97         if (!strcmp(tok->str, "endmenu") && line->n_token == 1) {
98                 //printf("endmenu\n");
99                 return 0;
100         }
101         return -1;
102 }
103
104 /* write config value in file f in a dotconfig-like manner. Return 0
105  * on success. */
106 static int confnode_menu_dotconfig_write(const struct confnode *n, FILE *f)
107 {
108         const struct confnode *c;
109         int val;
110
111         val = confnode_get_value(n, NULL, 0);
112         if (val < 0)
113                 return -1;
114         if (val == 0)
115                 return 0;
116
117         TAILQ_FOREACH(c, &n->children, next) {
118                 if (confnode_dotconfig_write(c, f) < 0)
119                         return -1;
120         }
121         return 0;
122 }
123
124 /* Return a string identifying the node type ("config", "menuconfig",
125  * "choice", ...) */
126 static const char *confnode_menu_get_type_str(const struct confnode *n)
127 {
128         return "menu";
129 }
130
131 /* Print a one-line summary of the node. Used in case of 'ls'. */
132 static void confnode_menu_display_short(const struct confnode *n)
133 {
134         printf("    -> %s: %s\n", n->name, n->prompt);
135 }
136
137 /* Print a detailed view of the node. */
138 static void confnode_menu_display_long(const struct confnode *n)
139 {
140         printf("menu <%s>\n", n->name);
141         printf("    path ");
142         conf_display_path(n);
143         printf("    \"%s\"\n", n->prompt);
144         printf("    no value\n");
145 }
146
147 /* register the node type */
148 void confnode_menu_register(void)
149 {
150         TAILQ_INSERT_TAIL(&confnode_type_list, &confnode_menu, next);
151 }