1 /* Handling and parsing of silo.conf
3 Copyright (C) 1995 Werner Almesberger
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
27 /* Imported functions */
28 extern int strcasecmp(const char *s1, const char *s2);
31 cft_strg, cft_flag, cft_end
41 #define MAX_VAR_NAME MAX_TOKEN
46 {cft_strg, "device", NULL},
47 {cft_strg, "partition", NULL},
48 {cft_strg, "default", NULL},
49 {cft_strg, "timeout", NULL},
50 {cft_strg, "password", NULL},
51 {cft_flag, "restricted", NULL},
52 {cft_strg, "message", NULL},
53 {cft_strg, "root", NULL},
54 {cft_strg, "ramdisk", NULL},
55 {cft_flag, "read-only", NULL},
56 {cft_flag, "read-write", NULL},
57 {cft_strg, "append", NULL},
58 {cft_strg, "initrd", NULL},
59 {cft_flag, "initrd-prompt", NULL},
60 {cft_strg, "initrd-size", NULL},
61 {cft_flag, "pause-after", NULL},
62 {cft_strg, "pause-message", NULL},
63 {cft_strg, "init-code", NULL},
64 {cft_strg, "init-message", NULL},
65 {cft_strg, "fgcolor", NULL},
66 {cft_strg, "bgcolor", NULL},
67 {cft_end, NULL, NULL}};
71 {cft_strg, "image", NULL},
72 {cft_strg, "label", NULL},
73 {cft_strg, "alias", NULL},
74 {cft_flag, "single-key", NULL},
75 {cft_flag, "restricted", NULL},
76 {cft_strg, "device", NULL},
77 {cft_strg, "partition", NULL},
78 {cft_strg, "root", NULL},
79 {cft_strg, "ramdisk", NULL},
80 {cft_flag, "read-only", NULL},
81 {cft_flag, "read-write", NULL},
82 {cft_strg, "append", NULL},
83 {cft_strg, "literal", NULL},
84 {cft_strg, "initrd", NULL},
85 {cft_flag, "initrd-prompt", NULL},
86 {cft_strg, "initrd-size", NULL},
87 {cft_flag, "pause-after", NULL},
88 {cft_strg, "pause-message", NULL},
89 {cft_flag, "novideo", NULL},
90 {cft_strg, "sysmap", NULL},
91 {cft_end, NULL, NULL}};
94 static char *last_token = NULL, *last_item = NULL, *last_value = NULL;
96 static int back = 0; /* can go back by one char */
97 static char *currp = NULL;
98 static char *endp = NULL;
99 static char *file_name = NULL;
100 static CONFIG *curr_table = cf_options;
103 static struct IMAGES {
104 CONFIG table[sizeof (cf_image) / sizeof (cf_image[0])];
108 void cfg_error (char *msg,...)
113 prom_printf ("Config file error: ");
114 prom_vprintf (msg, ap);
116 prom_printf (" near line %d in file %s\n", line_num, file_name);
120 void cfg_warn (char *msg,...)
125 prom_printf ("Config file warning: ");
126 prom_vprintf (msg, ap);
128 prom_printf (" near line %d in file %s\n", line_num, file_name);
138 #define next_raw next
139 static int next (void)
150 static void again (int ch)
155 static char *cfg_get_token (void)
157 char buf[MAX_TOKEN + 1];
167 while (ch = next (), ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
168 if (ch == '\n' || ch == '\r')
170 if (ch == EOF || ch == (int)NULL)
174 while (ch = next_raw (), (ch != '\n' && ch != '\r'))
183 while (here - buf < MAX_TOKEN) {
184 if ((ch = next ()) == EOF)
185 cfg_error ("EOF in quoted string");
198 while ((ch = next ()), ch == ' ' || ch == '\t');
208 cfg_error ("Bad use of \\ in quoted string");
210 } else if ((ch == '\n') || (ch == '\r'))
211 cfg_error ("newline is not allowed in quoted strings");
214 cfg_error ("Quoted string is too long");
215 return 0; /* not reached */
219 while (here - buf < MAX_TOKEN) {
222 cfg_error ("\\ precedes EOF");
226 *here++ = ch == '\t' ? ' ' : ch;
229 if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '#' ||
230 ch == '=' || ch == EOF) {
235 if (!(escaped = (ch == '\\')))
240 cfg_error ("Token is too long");
241 return 0; /* not reached */
244 static void cfg_return_token (char *token)
249 static int cfg_next (char **item, char **value)
260 if (!(*item = cfg_get_token ()))
262 if (!strcmp (*item, "="))
263 cfg_error ("Syntax error");
264 if (!(this = cfg_get_token ()))
266 if (strcmp (this, "=")) {
267 cfg_return_token (this);
270 if (!(*value = cfg_get_token ()))
271 cfg_error ("Value expected at EOF");
272 if (!strcmp (*value, "="))
273 cfg_error ("Syntax error after %s", *item);
278 // The one and only call to this procedure is commented out
279 // below, so we don't need this unless we decide to use it again.
280 static void cfg_return (char *item, char *value)
287 static int cfg_set (char *item, char *value)
291 if (!strcasecmp (item, "image")) {
292 struct IMAGES **p = &images;
296 *p = (struct IMAGES *)malloc (sizeof (struct IMAGES));
298 prom_printf("malloc error in cfg_set\n");
302 curr_table = ((*p)->table);
303 memcpy (curr_table, cf_image, sizeof (cf_image));
305 for (walk = curr_table; walk->type != cft_end; walk++) {
306 if (walk->name && !strcasecmp (walk->name, item)) {
307 if (value && walk->type != cft_strg)
308 cfg_warn ("'%s' doesn't have a value", walk->name);
309 else if (!value && walk->type == cft_strg)
310 cfg_warn ("Value expected for '%s'", walk->name);
313 cfg_warn ("Duplicate entry '%s'", walk->name);
314 if (walk->type == cft_flag)
315 walk->data = &flag_set;
316 else if (walk->type == cft_strg)
322 if (walk->type != cft_end)
324 // cfg_return (item, value);
328 int cfg_parse (char *cfg_file, char *buff, int len)
332 file_name = cfg_file;
339 if (!cfg_next (&item, &value))
341 if (!cfg_set (item, value)) {
343 prom_printf("Can't set item %s to value %s\n", item, value);
350 static char *cfg_get_strg_i (CONFIG * table, char *item)
354 for (walk = table; walk->type != cft_end; walk++)
355 if (walk->name && !strcasecmp (walk->name, item))
360 char *cfg_get_strg (char *image, char *item)
367 return cfg_get_strg_i (cf_options, item);
368 for (p = images; p; p = p->next) {
369 label = cfg_get_strg_i (p->table, "label");
371 label = cfg_get_strg_i (p->table, "image");
372 alias = strrchr (label, '/');
376 alias = cfg_get_strg_i (p->table, "alias");
377 if (!strcmp (label, image) || (alias && !strcmp (alias, image))) {
378 ret = cfg_get_strg_i (p->table, item);
380 ret = cfg_get_strg_i (cf_options, item);
387 int cfg_get_flag (char *image, char *item)
389 return !!cfg_get_strg (image, item);
392 static int printl_count = 0;
393 static void printlabel (char *label, int defflag)
395 int len = strlen (label);
399 prom_printf ("%s %s",defflag?"*":" ", label);
403 if (printl_count == 3)
407 void cfg_print_images (void)
412 char *ret = cfg_get_strg_i (cf_options, "default");
416 for (p = images; p; p = p->next) {
417 label = cfg_get_strg_i (p->table, "label");
419 label = cfg_get_strg_i (p->table, "image");
420 alias = strrchr (label, '/');
424 if(!strcmp(ret,label))
428 alias = cfg_get_strg_i (p->table, "alias");
429 printlabel (label, defflag);
431 printlabel (alias, 0);
436 char *cfg_get_default (void)
439 char *ret = cfg_get_strg_i (cf_options, "default");
445 ret = cfg_get_strg_i (images->table, "label");
447 ret = cfg_get_strg_i (images->table, "image");
448 label = strrchr (ret, '/');