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);
34 cft_strg, cft_flag, cft_end
44 #define MAX_VAR_NAME MAX_TOKEN
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_strg, "sysmap", NULL},
95 {cft_end, NULL, NULL}};
98 static char *last_token = NULL, *last_item = NULL, *last_value = NULL;
100 static int back = 0; /* can go back by one char */
101 static char *currp = NULL;
102 static char *endp = NULL;
103 static char *file_name = NULL;
104 static CONFIG *curr_table = cf_options;
105 static int ignore_entry;
108 static struct IMAGES {
109 CONFIG table[sizeof (cf_image) / sizeof (cf_image[0])];
114 void cfg_error (char *msg,...)
119 prom_printf ("Config file error: ");
120 prom_vprintf (msg, ap);
122 prom_printf (" near line %d in file %s\n", line_num, file_name);
126 void cfg_warn (char *msg,...)
131 prom_printf ("Config file warning: ");
132 prom_vprintf (msg, ap);
134 prom_printf (" near line %d in file %s\n", line_num, file_name);
144 #define next_raw next
145 static int next (void)
156 static void again (int ch)
161 static char *cfg_get_token (void)
163 char buf[MAX_TOKEN + 1];
173 while (ch = next (), ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
174 if (ch == '\n' || ch == '\r')
176 if (ch == EOF || ch == (int)NULL)
180 while (ch = next_raw (), (ch != '\n' && ch != '\r'))
189 while (here - buf < MAX_TOKEN) {
190 if ((ch = next ()) == EOF)
191 cfg_error ("EOF in quoted string");
204 while ((ch = next ()), ch == ' ' || ch == '\t');
214 cfg_error ("Bad use of \\ in quoted string");
216 } else if ((ch == '\n') || (ch == '\r'))
217 cfg_error ("newline is not allowed in quoted strings");
220 cfg_error ("Quoted string is too long");
221 return 0; /* not reached */
225 while (here - buf < MAX_TOKEN) {
228 cfg_error ("\\ precedes EOF");
232 *here++ = ch == '\t' ? ' ' : ch;
235 if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '#' ||
236 ch == '=' || ch == EOF) {
241 if (!(escaped = (ch == '\\')))
246 cfg_error ("Token is too long");
247 return 0; /* not reached */
250 static void cfg_return_token (char *token)
255 static int cfg_next (char **item, char **value)
266 if (!(*item = cfg_get_token ()))
268 if (!strcmp (*item, "="))
269 cfg_error ("Syntax error");
270 if (!(this = cfg_get_token ()))
272 if (strcmp (this, "=")) {
273 cfg_return_token (this);
276 if (!(*value = cfg_get_token ()))
277 cfg_error ("Value expected at EOF");
278 if (!strcmp (*value, "="))
279 cfg_error ("Syntax error after %s", *item);
283 static char *cfg_get_strg_i (CONFIG * table, char *item)
287 for (walk = table; walk->type != cft_end; walk++)
288 if (walk->name && !strcasecmp (walk->name, item))
294 // The one and only call to this procedure is commented out
295 // below, so we don't need this unless we decide to use it again.
296 static void cfg_return (char *item, char *value)
303 static int match_arch(const char *name)
305 static prom_handle root;
306 static char model[256], *p;
309 if (!(root = prom_finddevice("/")))
314 if (!prom_getprop(root, "compatible", model, sizeof(model)))
318 for (p = model; *p; p += strlen(p) + 1) {
319 if (!strcasecmp(p, name))
326 static void check_for_obsolete(const char *label)
331 /* Make sure our current entry isn't obsolete (ignored) */
332 for (p = images; p; p = p->next) {
333 if (curr_table == p->table && p->obsolete)
337 for (p = images; p; p = p->next) {
338 if (curr_table == p->table)
341 cur_label = cfg_get_strg_i (p->table, "label");
343 cur_label = cfg_get_strg_i (p->table, "image");
348 if (!strcasecmp(cur_label, label))
353 static int cfg_set (char *item, char *value)
357 if (!strncasecmp (item, "image", 5)) {
358 struct IMAGES **p = &images;
361 if (item[5] == '[' && item[strlen(item) - 1] == ']') {
364 /* Get rid of braces */
365 item[strlen(item) - 1] = 0;
368 for (s = item + 6; q; s = q) {
376 /* This just creates an unused table. It will get ignored */
384 *p = (struct IMAGES *)malloc (sizeof (struct IMAGES));
386 prom_printf("malloc error in cfg_set\n");
390 (*p)->obsolete = ignore;
391 curr_table = ((*p)->table);
392 memcpy (curr_table, cf_image, sizeof (cf_image));
396 for (walk = curr_table; walk->type != cft_end; walk++) {
397 if (walk->name && !strcasecmp (walk->name, item)) {
398 if (value && walk->type != cft_strg)
399 cfg_warn ("'%s' doesn't have a value", walk->name);
400 else if (!value && walk->type == cft_strg)
401 cfg_warn ("Value expected for '%s'", walk->name);
403 if (!strcasecmp (item, "label"))
404 check_for_obsolete(value);
406 cfg_warn ("Duplicate entry '%s'", walk->name);
407 if (walk->type == cft_flag)
408 walk->data = &flag_set;
409 else if (walk->type == cft_strg)
416 if (walk->type != cft_end)
419 //cfg_return (item, value);
424 int cfg_parse (char *cfg_file, char *buff, int len)
428 file_name = cfg_file;
435 if (!cfg_next (&item, &value))
437 if (!cfg_set (item, value)) {
439 prom_printf("Can't set item %s to value %s\n", item, value);
446 static char *cfg_get_strg_i (CONFIG * table, char *item)
450 for (walk = table; walk->type != cft_end; walk++)
451 if (walk->name && !strcasecmp (walk->name, item))
456 char *cfg_get_strg (char *image, char *item)
463 return cfg_get_strg_i (cf_options, item);
464 for (p = images; p; p = p->next) {
468 label = cfg_get_strg_i (p->table, "label");
470 label = cfg_get_strg_i (p->table, "image");
471 alias = strrchr (label, '/');
475 alias = cfg_get_strg_i (p->table, "alias");
476 if (!strcmp (label, image) || (alias && !strcmp (alias, image))) {
477 ret = cfg_get_strg_i (p->table, item);
479 ret = cfg_get_strg_i (cf_options, item);
486 int cfg_get_flag (char *image, char *item)
488 return !!cfg_get_strg (image, item);
491 static int printl_count = 0;
492 static void printlabel (char *label, int defflag)
494 int len = strlen (label);
498 prom_printf ("%s %s",defflag?"*":" ", label);
502 if (printl_count == 3)
506 void cfg_print_images (void)
511 char *ret = cfg_get_strg_i (cf_options, "default");
515 for (p = images; p; p = p->next) {
519 label = cfg_get_strg_i (p->table, "label");
521 label = cfg_get_strg_i (p->table, "image");
522 alias = strrchr (label, '/');
526 if(!strcmp(ret,label))
530 alias = cfg_get_strg_i (p->table, "alias");
531 printlabel (label, defflag);
533 printlabel (alias, 0);
538 char *cfg_get_default (void)
542 char *ret = cfg_get_strg_i (cf_options, "default");
549 for (p = images; p && p->obsolete; p = p->next);
553 ret = cfg_get_strg_i (p->table, "label");
555 ret = cfg_get_strg_i (p->table, "image");
556 label = strrchr (ret, '/');
565 * c-file-style: "k&r"