02eb4255f5ba4c5d6b9a5ef752589c166c1ff36c
[silc.git] / apps / irssi / src / lib-config / parse.c
1 /*
2  parse.c : irssi configuration - parse configuration file
3
4     Copyright (C) 1999 Timo Sirainen
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "module.h"
22
23 static int g_istr_equal(gconstpointer v, gconstpointer v2)
24 {
25         return g_strcasecmp((const char *) v, (const char *) v2) == 0;
26 }
27
28 /* a char* hash function from ASU */
29 static unsigned int g_istr_hash(gconstpointer v)
30 {
31         const char *s = (const char *) v;
32         unsigned int h = 0, g;
33
34         while (*s != '\0') {
35                 h = (h << 4) + i_toupper(*s);
36                 if ((g = h & 0xf0000000UL)) {
37                         h = h ^ (g >> 24);
38                         h = h ^ g;
39                 }
40                 s++;
41         }
42
43         return h /* % M */;
44 }
45
46 int config_error(CONFIG_REC *rec, const char *msg)
47 {
48         g_free_and_null(rec->last_error);
49         rec->last_error = g_strdup(msg);
50         return -1;
51 }
52
53 static int node_add_comment(CONFIG_NODE *parent, const char *str)
54 {
55         CONFIG_NODE *node;
56
57         g_return_val_if_fail(parent != NULL, -1);
58
59         if (!is_node_list(parent))
60                 return -1;
61
62         node = g_new0(CONFIG_NODE, 1);
63         node->type = NODE_TYPE_COMMENT;
64         node->value = str == NULL ? NULL : g_strdup(str);
65
66         parent->value = g_slist_append(parent->value, node);
67         return 0;
68 }
69
70 /* same as g_scanner_get_next_token() except skips and reads the comments */
71 static void config_parse_get_token(GScanner *scanner, CONFIG_NODE *node)
72 {
73         int prev_empty = FALSE;
74
75         for (;;) {
76                 g_scanner_get_next_token(scanner);
77
78                 if (scanner->token == G_TOKEN_COMMENT_SINGLE)
79                         node_add_comment(node, scanner->value.v_string);
80                 else if (scanner->token == '\n') {
81                         if (prev_empty) node_add_comment(node, NULL);
82                 } else {
83                         if (scanner->token == G_TOKEN_INT) {
84                                 scanner->token = G_TOKEN_STRING;
85                                 scanner->value.v_string = g_strdup_printf("%lu", scanner->value.v_int);
86                         }
87                         break;
88                 }
89
90                 prev_empty = TRUE;
91         }
92 }
93
94 /* same as g_scanner_peek_next_token() except skips and reads the comments */
95 static void config_parse_peek_token(GScanner *scanner, CONFIG_NODE *node)
96 {
97         int prev_empty = FALSE;
98
99         for (;;) {
100                 g_scanner_peek_next_token(scanner);
101
102                 if (scanner->next_token == G_TOKEN_COMMENT_SINGLE)
103                         node_add_comment(node, scanner->next_value.v_string);
104                 else if (scanner->next_token == '\n') {
105                         if (prev_empty) node_add_comment(node, NULL);
106                 } else
107                         break;
108
109                 prev_empty = TRUE;
110                 g_scanner_get_next_token(scanner);
111         }
112 }
113
114 /* get optional token, optionally warn if it's missing */
115 static void config_parse_warn_missing(CONFIG_REC *rec, CONFIG_NODE *node,
116                                       GTokenType expected_token, int print_warning)
117 {
118         config_parse_peek_token(rec->scanner, node);
119         if (rec->scanner->next_token == expected_token) {
120                 g_scanner_get_next_token(rec->scanner);
121                 return;
122         }
123
124         if (print_warning)
125                 g_scanner_warn(rec->scanner, "Warning: missing '%c'", expected_token);
126 }
127
128 static void config_parse_loop(CONFIG_REC *rec, CONFIG_NODE *node, GTokenType expect);
129
130 static GTokenType config_parse_symbol(CONFIG_REC *rec, CONFIG_NODE *node)
131 {
132         CONFIG_NODE *newnode;
133         GTokenType last_char;
134         int print_warning;
135         char *key;
136
137         g_return_val_if_fail(rec != NULL, G_TOKEN_ERROR);
138         g_return_val_if_fail(node != NULL, G_TOKEN_ERROR);
139
140         config_parse_get_token(rec->scanner, node);
141
142         last_char = (GTokenType) (node->type == NODE_TYPE_LIST ? ',' : ';');
143
144         /* key */
145         key = NULL;
146         if (node->type != NODE_TYPE_LIST &&
147             (rec->scanner->token == G_TOKEN_STRING)) {
148                 key = g_strdup(rec->scanner->value.v_string);
149
150                 config_parse_warn_missing(rec, node, '=', TRUE);
151                 config_parse_get_token(rec->scanner, node);
152         }
153
154         switch (rec->scanner->token) {
155         case G_TOKEN_STRING:
156                 /* value */
157                 config_node_set_str(rec, node, key, rec->scanner->value.v_string);
158                 g_free_not_null(key);
159
160                 print_warning = TRUE;
161                 if (node->type == NODE_TYPE_LIST) {
162                         /* if it's last item it doesn't need comma */
163                         config_parse_peek_token(rec->scanner, node);
164                         if (rec->scanner->next_token == ')')
165                                 print_warning = FALSE;
166                 }
167
168                 config_parse_warn_missing(rec, node, last_char, print_warning);
169                 break;
170
171         case '{':
172                 /* block */
173                 if (key == NULL && node->type != NODE_TYPE_LIST)
174                         return G_TOKEN_ERROR;
175
176                 newnode = config_node_section(node, key, NODE_TYPE_BLOCK);
177                 config_parse_loop(rec, newnode, (GTokenType) '}');
178                 g_free_not_null(key);
179
180                 config_parse_get_token(rec->scanner, node);
181                 if (rec->scanner->token != '}')
182                         return (GTokenType) '}';
183
184                 config_parse_warn_missing(rec, node, last_char, FALSE);
185                 break;
186
187         case '(':
188                 /* list */
189                 if (key == NULL)
190                         return G_TOKEN_ERROR;
191                 newnode = config_node_section(node, key, NODE_TYPE_LIST);
192                 config_parse_loop(rec, newnode, (GTokenType) ')');
193                 g_free_not_null(key);
194
195                 config_parse_get_token(rec->scanner, node);
196                 if (rec->scanner->token != ')')
197                         return (GTokenType) ')';
198
199                 config_parse_warn_missing(rec, node, last_char, FALSE);
200                 break;
201
202         default:
203                 /* error */
204                 g_free_not_null(key);
205                 return G_TOKEN_STRING;
206         }
207
208         return G_TOKEN_NONE;
209 }
210
211 static void config_parse_loop(CONFIG_REC *rec, CONFIG_NODE *node, GTokenType expect)
212 {
213         GTokenType expected_token;
214
215         g_return_if_fail(rec != NULL);
216         g_return_if_fail(node != NULL);
217
218         for (;;) {
219                 config_parse_peek_token(rec->scanner, node);
220                 if (rec->scanner->next_token == expect ||
221                     rec->scanner->next_token == G_TOKEN_EOF) break;
222
223                 expected_token = config_parse_symbol(rec, node);
224                 if (expected_token != G_TOKEN_NONE) {
225                         if (expected_token == G_TOKEN_ERROR)
226                                 expected_token = G_TOKEN_NONE;
227                         g_scanner_unexp_token(rec->scanner, expected_token, NULL, "symbol", NULL, NULL, TRUE);
228                 }
229         }
230 }
231
232 static void config_parse_error_func(GScanner *scanner, char *message, int is_error)
233 {
234         CONFIG_REC *rec = scanner->user_data;
235         char *old;
236
237         old = rec->last_error;
238         rec->last_error = g_strdup_printf("%s%s:%d: %s%s\n",
239                                           old == NULL ? "" : old,
240                                           scanner->input_name, scanner->line,
241                                           is_error ? "error: " : "",
242                                           message);
243         g_free_not_null(old);
244 }
245
246 void config_parse_init(CONFIG_REC *rec, const char *name)
247 {
248         GScanner *scanner;
249
250         g_free_and_null(rec->last_error);
251         config_nodes_remove_all(rec);
252
253         rec->scanner = scanner = g_scanner_new(NULL);
254         scanner->config->skip_comment_single = FALSE;
255         scanner->config->cset_skip_characters = " \t";
256         scanner->config->scan_binary = FALSE;
257         scanner->config->scan_octal = FALSE;
258         scanner->config->scan_float = FALSE;
259         scanner->config->scan_string_sq = TRUE;
260         scanner->config->scan_string_dq = TRUE;
261         scanner->config->scan_identifier_1char = TRUE;
262         scanner->config->identifier_2_string = TRUE;
263
264         scanner->user_data = rec;
265         scanner->input_name = name;
266         scanner->msg_handler = (GScannerMsgFunc) config_parse_error_func;
267 }
268
269 /* Parse configuration file */
270 int config_parse(CONFIG_REC *rec)
271 {
272         g_return_val_if_fail(rec != NULL, -1);
273         g_return_val_if_fail(rec->fname != NULL, -1);
274
275         rec->handle = open(rec->fname, O_RDONLY);
276         if (rec->handle == -1)
277                 return config_error(rec, g_strerror(errno));
278
279         config_parse_init(rec, rec->fname);
280         g_scanner_input_file(rec->scanner, rec->handle);
281         config_parse_loop(rec, rec->mainnode, G_TOKEN_EOF);
282         g_scanner_destroy(rec->scanner);
283
284         close(rec->handle);
285         rec->handle = -1;
286
287         return rec->last_error == NULL ? 0 : -1;
288 }
289
290 /* Parse configuration found from `data'. `input_name' is specifies the
291    "configuration name" which is displayed in error messages. */
292 int config_parse_data(CONFIG_REC *rec, const char *data, const char *input_name)
293 {
294         config_parse_init(rec, input_name);
295         g_scanner_input_text(rec->scanner, data, strlen(data));
296         config_parse_loop(rec, rec->mainnode, G_TOKEN_EOF);
297         g_scanner_destroy(rec->scanner);
298
299         return rec->last_error == NULL ? 0 : -1;
300 }
301
302 /* Open configuration. The file is created if it doesn't exist, unless
303    `create_mode' is -1. `fname' can be NULL if you just want to use
304    config_parse_data() */
305 CONFIG_REC *config_open(const char *fname, int create_mode)
306 {
307         CONFIG_REC *rec;
308         int f;
309
310         if (fname != NULL) {
311                 f = open(fname, O_RDONLY | (create_mode != -1 ? O_CREAT : 0), create_mode);
312                 if (f == -1) return NULL;
313                 close(f);
314         }
315
316         rec = g_new0(CONFIG_REC, 1);
317         rec->fname = fname == NULL ? NULL : g_strdup(fname);
318         rec->handle = -1;
319         rec->create_mode = create_mode;
320         rec->mainnode = g_new0(CONFIG_NODE, 1);
321         rec->mainnode->type = NODE_TYPE_BLOCK;
322         rec->cache = g_hash_table_new((GHashFunc) g_istr_hash, (GCompareFunc) g_istr_equal);
323         rec->cache_nodes = g_hash_table_new((GHashFunc) g_direct_hash, (GCompareFunc) g_direct_equal);
324
325         return rec;
326 }
327
328 /* Release all memory used by configuration */
329 void config_close(CONFIG_REC *rec)
330 {
331         g_return_if_fail(rec != NULL);
332
333         config_nodes_remove_all(rec);
334         g_free(rec->mainnode);
335
336         if (rec->handle != -1) close(rec->handle);
337         g_hash_table_foreach(rec->cache, (GHFunc) g_free, NULL);
338         g_hash_table_destroy(rec->cache);
339         g_hash_table_destroy(rec->cache_nodes);
340         g_free_not_null(rec->last_error);
341         g_free_not_null(rec->fname);
342         g_free(rec);
343 }
344
345 /* Change file name of config file */
346 void config_change_file_name(CONFIG_REC *rec, const char *fname, int create_mode)
347 {
348         g_return_if_fail(rec != NULL);
349         g_return_if_fail(fname != NULL);
350
351         g_free_not_null(rec->fname);
352         rec->fname = g_strdup(fname);
353
354         if (create_mode != -1)
355                 rec->create_mode = create_mode;
356 }