2 * cfg.c - Handling and parsing of yaboot.conf
4 * Copyright (C) 1995 Werner Almesberger
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /* Imported functions */
30 extern int strcasecmp(const char *s1, const char *s2);
31 extern int strncasecmp(const char *cs, const char *ct, size_t n);
32 extern char bootoncelabel[1024];
35 cft_strg, cft_flag, cft_end
49 {cft_strg, "device", NULL},
50 {cft_strg, "partition", NULL},
51 {cft_strg, "default", NULL},
52 {cft_strg, "timeout", NULL},
53 {cft_strg, "password", NULL},
54 {cft_flag, "restricted", NULL},
55 {cft_strg, "message", NULL},
56 {cft_strg, "root", NULL},
57 {cft_strg, "ramdisk", NULL},
58 {cft_flag, "read-only", NULL},
59 {cft_flag, "read-write", NULL},
60 {cft_strg, "append", NULL},
61 {cft_strg, "initrd", NULL},
62 {cft_flag, "initrd-prompt", NULL},
63 {cft_strg, "initrd-size", NULL},
64 {cft_flag, "pause-after", NULL},
65 {cft_strg, "pause-message", NULL},
66 {cft_strg, "init-code", NULL},
67 {cft_strg, "init-message", NULL},
68 {cft_strg, "fgcolor", NULL},
69 {cft_strg, "bgcolor", NULL},
70 {cft_strg, "ptypewarning", NULL},
71 {cft_end, NULL, NULL}};
75 {cft_strg, "image", NULL},
76 {cft_strg, "label", NULL},
77 {cft_strg, "alias", NULL},
78 {cft_flag, "single-key", NULL},
79 {cft_flag, "restricted", NULL},
80 {cft_strg, "device", NULL},
81 {cft_strg, "partition", NULL},
82 {cft_strg, "root", NULL},
83 {cft_strg, "ramdisk", NULL},
84 {cft_flag, "read-only", NULL},
85 {cft_flag, "read-write", NULL},
86 {cft_strg, "append", NULL},
87 {cft_strg, "literal", NULL},
88 {cft_strg, "initrd", NULL},
89 {cft_flag, "initrd-prompt", NULL},
90 {cft_strg, "initrd-size", NULL},
91 {cft_flag, "pause-after", NULL},
92 {cft_strg, "pause-message", NULL},
93 {cft_flag, "novideo", NULL},
94 {cft_end, NULL, NULL}};
97 static char *last_token = NULL, *last_item = NULL, *last_value = NULL;
99 static int back = 0; /* can go back by one char */
100 static char *currp = NULL;
101 static char *endp = NULL;
102 static char *file_name = NULL;
103 static CONFIG *curr_table = cf_options;
106 static struct IMAGES {
107 CONFIG table[sizeof (cf_image) / sizeof (cf_image[0])];
112 void cfg_error (char *msg,...)
117 prom_printf ("Config file error: ");
118 prom_vprintf (msg, ap);
120 prom_printf (" near line %d in file %s\n", line_num, file_name);
124 void cfg_warn (char *msg,...)
129 prom_printf ("Config file warning: ");
130 prom_vprintf (msg, ap);
132 prom_printf (" near line %d in file %s\n", line_num, file_name);
142 #define next_raw next
143 static int next (void)
154 static void again (int ch)
159 static char *cfg_get_token (void)
161 char buf[MAX_TOKEN + 1];
171 while (ch = next (), ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
172 if (ch == '\n' || ch == '\r')
174 if (ch == EOF || ch == (int)NULL)
178 while (ch = next_raw (), (ch != '\n' && ch != '\r'))
187 while (here - buf < MAX_TOKEN) {
188 if ((ch = next ()) == EOF)
189 cfg_error ("EOF in quoted string");
202 while ((ch = next ()), ch == ' ' || ch == '\t');
212 cfg_error ("Bad use of \\ in quoted string");
214 } else if ((ch == '\n') || (ch == '\r'))
215 cfg_error ("newline is not allowed in quoted strings");
218 cfg_error ("Quoted string is too long");
219 return 0; /* not reached */
223 while (here - buf < MAX_TOKEN) {
226 cfg_error ("\\ precedes EOF");
230 *here++ = ch == '\t' ? ' ' : ch;
233 if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '#' ||
234 ch == '=' || ch == EOF) {
239 if (!(escaped = (ch == '\\')))
244 cfg_error ("Token is too long");
245 return 0; /* not reached */
248 static void cfg_return_token (char *token)
253 static int cfg_next (char **item, char **value)
264 if (!(*item = cfg_get_token ()))
266 if (!strcmp (*item, "="))
267 cfg_error ("Syntax error");
268 if (!(this = cfg_get_token ()))
270 if (strcmp (this, "=")) {
271 cfg_return_token (this);
274 if (!(*value = cfg_get_token ()))
275 cfg_error ("Value expected at EOF");
276 if (!strcmp (*value, "="))
277 cfg_error ("Syntax error after %s", *item);
281 static char *cfg_get_strg_i (CONFIG * table, char *item)
285 for (walk = table; walk->type != cft_end; walk++)
286 if (walk->name && !strcasecmp (walk->name, item))
292 // The one and only call to this procedure is commented out
293 // below, so we don't need this unless we decide to use it again.
294 static void cfg_return (char *item, char *value)
301 static int match_arch(const char *name)
303 static prom_handle root;
304 static char model[256], *p;
307 if (!(root = prom_finddevice("/")))
312 if (!prom_getprop(root, "compatible", model, sizeof(model)))
316 for (p = model; *p; p += strlen(p) + 1) {
317 if (!strcasecmp(p, name))
324 static void check_for_obsolete(const char *label)
329 /* Make sure our current entry isn't obsolete (ignored) */
330 for (p = images; p; p = p->next) {
331 if (curr_table == p->table && p->obsolete)
335 for (p = images; p; p = p->next) {
336 if (curr_table == p->table)
339 cur_label = cfg_get_strg_i (p->table, "label");
341 cur_label = cfg_get_strg_i (p->table, "image");
346 if (!strcasecmp(cur_label, label))
351 static int cfg_set (char *item, char *value)
355 if (!strncasecmp (item, "image", 5)) {
356 struct IMAGES **p = &images;
359 if (item[5] == '[' && item[strlen(item) - 1] == ']') {
362 /* Get rid of braces */
363 item[strlen(item) - 1] = 0;
366 for (s = item + 6; q; s = q) {
374 /* This just creates an unused table. It will get ignored */
382 *p = (struct IMAGES *)malloc (sizeof (struct IMAGES));
384 prom_printf("malloc error in cfg_set\n");
388 (*p)->obsolete = ignore;
389 curr_table = ((*p)->table);
390 memcpy (curr_table, cf_image, sizeof (cf_image));
394 for (walk = curr_table; walk->type != cft_end; walk++) {
395 if (walk->name && !strcasecmp (walk->name, item)) {
396 if (value && walk->type != cft_strg)
397 cfg_warn ("'%s' doesn't have a value", walk->name);
398 else if (!value && walk->type == cft_strg)
399 cfg_warn ("Value expected for '%s'", walk->name);
401 if (!strcasecmp (item, "label"))
402 check_for_obsolete(value);
404 cfg_warn ("Duplicate entry '%s'", walk->name);
405 if (walk->type == cft_flag)
406 walk->data = &flag_set;
407 else if (walk->type == cft_strg)
414 if (walk->type != cft_end)
417 //cfg_return (item, value);
422 static int cfg_reset ()
426 prom_printf("Resetting image table\n");
431 curr_table = cf_options;
432 for (walk = curr_table; walk->type != cft_end; walk++) {
434 prom_printf("ItemA %s = %s\n", walk->name, (char *)walk->data);
436 if (walk->data != NULL)
439 prom_printf("ItemB %s = %s\n\n", walk->name, (char *)walk->data);
446 int cfg_parse (char *cfg_file, char *buff, int len)
450 file_name = cfg_file;
459 if (!cfg_next (&item, &value))
461 if (!cfg_set (item, value)) {
463 prom_printf("Can't set item %s to value %s\n", item, value);
470 char *cfg_get_strg (char *image, char *item)
477 return cfg_get_strg_i (cf_options, item);
478 for (p = images; p; p = p->next) {
482 label = cfg_get_strg_i (p->table, "label");
484 label = cfg_get_strg_i (p->table, "image");
485 alias = strrchr (label, '/');
489 alias = cfg_get_strg_i (p->table, "alias");
490 if (!strcmp (label, image) || (alias && !strcmp (alias, image))) {
491 ret = cfg_get_strg_i (p->table, item);
493 ret = cfg_get_strg_i (cf_options, item);
500 int cfg_get_flag (char *image, char *item)
502 return !!cfg_get_strg (image, item);
505 static int printl_count = 0;
506 static void printlabel (char *label, int defflag)
508 int len = strlen (label);
514 case 1: a='*'; break;
515 case 2: a='&'; break;
516 default: a=' '; break;
518 prom_printf ("%c %s", a, label);
522 if (printl_count == 3)
526 void cfg_print_images (void)
531 char *ret = cfg_get_strg_i (cf_options, "default");
535 for (p = images; p; p = p->next) {
539 label = cfg_get_strg_i (p->table, "label");
541 label = cfg_get_strg_i (p->table, "image");
542 alias = strrchr (label, '/');
546 if(!strcmp(bootoncelabel,label))
548 else if(!strcmp(ret,label))
552 alias = cfg_get_strg_i (p->table, "alias");
553 printlabel (label, defflag);
555 printlabel (alias, 0);
560 char *cfg_get_default (void)
564 char *ret = cfg_get_strg_i (cf_options, "default");
571 for (p = images; p && p->obsolete; p = p->next);
575 ret = cfg_get_strg_i (p->table, "label");
577 ret = cfg_get_strg_i (p->table, "image");
578 label = strrchr (ret, '/');
586 * cfg_set_default_by_mac ()
587 * return 1 if the default cf_option was changed to label with the MAC addr
588 * return 0 if not changed
590 int cfg_set_default_by_mac (char *mac_addr)
597 /* check if there is an image label equal to mac_addr */
598 for (tmp = images; tmp; tmp = tmp->next) {
599 label = cfg_get_strg_i (tmp->table, "label");
600 if (!strcmp(label,mac_addr)){
609 * if there is an image label equal to mac_addr, change the default
610 * cf_options to this image label
612 for (walk = cf_options; walk->type != cft_end; walk++) {
613 if (!strcasecmp(walk->name,"default")) {
614 walk->data = mac_addr;
624 * c-file-style: "k&r"