]> git.ozlabs.org Git - petitboot/blob - discover/parser-conf.c
ui/ncurses: Add status log UI
[petitboot] / discover / parser-conf.c
1 /*
2  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
3  *  Copyright 2009 Sony Corp.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; version 2 of the License.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #if defined(HAVE_CONFIG_H)
20 #include "config.h"
21 #endif
22
23 #include <assert.h>
24 #include <ctype.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31
32 #include "log/log.h"
33 #include "talloc/talloc.h"
34 #include "parser-conf.h"
35 #include "parser-utils.h"
36 #include "paths.h"
37
38 /**
39  * conf_strip_str - Remove quotes and/or whitespace around a string.
40  *
41  * Returns the next byte to process, or NULL if the string is empty.
42  */
43
44 char *conf_strip_str(char *s)
45 {
46         char *e;
47
48         if (!s)
49                 return NULL;
50
51         if (!strlen(s))
52                 return NULL;
53
54         while (*s == '"' || *s == '\'' || isspace(*s))
55                 s++;
56
57         e = s + strlen(s) - 1;
58
59         while (*e == '"' || *e == '\'' || isspace(*e))
60                 *(e--) = 0;
61
62         return strlen(s) ? s : NULL;
63 }
64
65 /**
66  * conf_replace_char - replace one char with another.
67  */
68
69 char *conf_replace_char(char *s, char from, char to)
70 {
71         if (!s)
72                 return NULL;
73
74         for ( ; *s; s++)
75                 if (*s == from)
76                         *s = to;
77
78         return s;
79 }
80
81 /**
82  * conf_get_pair - Get the next 'name/value' parameter pair.
83  * @str: The string to process.
84  * @name_out: Returns a pointer to the name.
85  * @value_out: Returns a pointer to the value.
86  * @tdelimiter: The pair separator.
87  * @terminator: The pair terminator.
88  *
89  * Parses a name=value pair returning pointers in @name_out and @value_out.
90  * The pair can be terminated by @terminator or a zero.
91  * If no '=' character is found @value_out is set and @name_out is
92  * set to NULL.
93  * If the value is empty *value_out is set to NULL.
94  * The string is modified in place.
95  *
96  * Returns the next byte to process, or NULL if we've hit the end of the
97  * string.
98  */
99
100 char *conf_get_pair(struct conf_context __attribute__((unused)) *conf, char *str,
101         char **name_out, char **value_out, char delimiter, char terminator)
102 {
103         char *sep, *end;
104
105         *name_out = *value_out = NULL;
106
107         /* terminate the value */
108         end = strchr(str, terminator);
109
110         if (end)
111                 *end = 0;
112
113         conf_replace_char(str, '\t', ' ');
114
115         str = conf_strip_str(str);
116
117         if (!str)
118                 goto exit;
119
120         sep = strchr(str, delimiter);
121
122         if (!sep) {
123                 *name_out = NULL;
124                 *value_out = conf_strip_str(str);
125         } else {
126                 *sep = 0;
127                 *name_out = conf_strip_str(str);
128                 *value_out = conf_strip_str(sep + 1);
129         }
130
131 exit:
132         return end ? end + 1 : NULL;
133 }
134
135 /**
136  * conf_param_in_list - Search a list of strings for an entry.
137  * @list: A NULL treminated array of pointers to strings.
138  * @param: A string to search for.
139  *
140  * Retuns 1 if @param is found in @list, 0 if @param is not found.
141  */
142
143 int conf_param_in_list(const char *const *list, const char *param)
144 {
145         const char *const *str;
146
147         for (str = list; *str; str++)
148                 if (streq(*str, param))
149                         return 1;
150         return 0;
151 }
152
153 /**
154  * conf_init_global_options - Zero the global option table.
155  */
156
157 void conf_init_global_options(struct conf_context *conf)
158 {
159         int i;
160
161         if (!conf->global_options)
162                 return;
163
164         for (i = 0; conf->global_options[i].name; i++)
165                 conf->global_options[i].value = NULL;
166 }
167
168 /**
169  * conf_set_global_option - Set a value in the global option table.
170  *
171  * Check if an option (name=value) is a global option. If so, store it in
172  * the global options table, and return 1. Otherwise, return 0.
173  */
174
175 int conf_set_global_option(struct conf_context *conf, const char *name,
176         const char *value)
177 {
178         int i;
179
180         assert(conf->global_options);
181
182         for (i = 0; conf->global_options[i].name; i++) {
183                 if (streq(name, conf->global_options[i].name)) {
184                         conf->global_options[i].value
185                                 = talloc_strdup(conf, value);
186                         pb_debug("%s: %s = '%s'\n", __func__, name, value);
187                         return 1;
188                 }
189         }
190         return 0;
191 }
192
193 /**
194  * conf_get_global_option - Get a value from the global option table.
195  * @conf: The parser struct conf_context.
196  * @name: The name of the (name:value) to retrieve.
197  *
198  * Returns the value if @name is found in the table, or NULL if @name
199  * is not found.
200  */
201
202 const char *conf_get_global_option(struct conf_context *conf,
203         const char *name)
204 {
205         int i;
206
207         assert(conf->global_options);
208
209         for (i = 0; conf->global_options[i].name ;i++)
210                 if (streq(name, conf->global_options[i].name))
211                         return conf->global_options[i].value;
212
213         assert(0 && "unknown global name");
214         return NULL;
215 }
216
217 /**
218  * conf_parse_buf - The main parser loop.
219  *
220  * Called from conf_parse() with data read from a conf file.
221  */
222
223 void conf_parse_buf(struct conf_context *conf, char *buf,
224                 int len __attribute__((unused)))
225 {
226         char *pos, *name, *value;
227
228         assert(conf->get_pair);
229         assert(conf->process_pair);
230
231         for (pos = buf; pos;) {
232                 pos = conf->get_pair(conf, pos, &name, &value, '\n');
233
234                 if (!value)
235                         continue;
236
237                 if (name && *name == '#')
238                         continue;
239
240                 if (*value == '#')
241                         continue;
242
243                 conf->process_pair(conf, name, value);
244         }
245
246         if (conf->finish)
247                 conf->finish(conf);
248 }