]> git.ozlabs.org Git - yaboot.git/blob - second/cfg.c
read boot-once, zero out, set bootoncelabel as default
[yaboot.git] / second / cfg.c
1 /*
2  *  cfg.c - Handling and parsing of yaboot.conf
3  *
4  *  Copyright (C) 1995 Werner Almesberger
5  *                1996 Jakub Jelinek
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include "setjmp.h"
23 #include "stdarg.h"
24 #include "stdlib.h"
25 #include "string.h"
26 #include "types.h"
27 #include "prom.h"
28
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
33 typedef enum {
34      cft_strg, cft_flag, cft_end
35 } CONFIG_TYPE;
36
37 typedef struct {
38      CONFIG_TYPE type;
39      char *name;
40      void *data;
41 } CONFIG;
42
43 #define MAX_TOKEN 200
44 #define MAX_VAR_NAME MAX_TOKEN
45 #define EOF -1
46
47 CONFIG cf_options[] =
48 {
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}};
72
73 CONFIG cf_image[] =
74 {
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}};
96
97 static char flag_set;
98 static char *last_token = NULL, *last_item = NULL, *last_value = NULL;
99 static int line_num;
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;
106 static jmp_buf env;
107
108 static struct IMAGES {
109      CONFIG table[sizeof (cf_image) / sizeof (cf_image[0])];
110      int obsolete;
111      struct IMAGES *next;
112 } *images = NULL;
113
114 void cfg_error (char *msg,...)
115 {
116      va_list ap;
117
118      va_start (ap, msg);
119      prom_printf ("Config file error: ");
120      prom_vprintf (msg, ap);
121      va_end (ap);
122      prom_printf (" near line %d in file %s\n", line_num, file_name);
123      longjmp (env, 1);
124 }
125
126 void cfg_warn (char *msg,...)
127 {
128      va_list ap;
129
130      va_start (ap, msg);
131      prom_printf ("Config file warning: ");
132      prom_vprintf (msg, ap);
133      va_end (ap);
134      prom_printf (" near line %d in file %s\n", line_num, file_name);
135 }
136
137 inline int getc ()
138 {
139      if (currp == endp)
140           return EOF;
141      return *currp++;
142 }
143
144 #define next_raw next
145 static int next (void)
146 {
147      int ch;
148
149      if (!back)
150           return getc ();
151      ch = back;
152      back = 0;
153      return ch;
154 }
155
156 static void again (int ch)
157 {
158      back = ch;
159 }
160
161 static char *cfg_get_token (void)
162 {
163      char buf[MAX_TOKEN + 1];
164      char *here;
165      int ch, escaped;
166
167      if (last_token) {
168           here = last_token;
169           last_token = NULL;
170           return here;
171      }
172      while (1) {
173           while (ch = next (), ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
174                if (ch == '\n' || ch == '\r')
175                     line_num++;
176           if (ch == EOF || ch == (int)NULL)
177                return NULL;
178           if (ch != '#')
179                break;
180           while (ch = next_raw (), (ch != '\n' && ch != '\r'))
181                if (ch == EOF)
182                     return NULL;
183           line_num++;
184      }
185      if (ch == '=')
186           return strdup ("=");
187      if (ch == '"') {
188           here = buf;
189           while (here - buf < MAX_TOKEN) {
190                if ((ch = next ()) == EOF)
191                     cfg_error ("EOF in quoted string");
192                if (ch == '"') {
193                     *here = 0;
194                     return strdup (buf);
195                }
196                if (ch == '\\') {
197                     ch = next ();
198                     switch (ch) {
199                     case '"':
200                     case '\\':
201                          break;
202                     case '\n':
203                     case '\r':
204                          while ((ch = next ()), ch == ' ' || ch == '\t');
205                          if (!ch)
206                               continue;
207                          again (ch);
208                          ch = ' ';
209                          break;
210                     case 'n':
211                          ch = '\n';
212                          break;
213                     default:
214                          cfg_error ("Bad use of \\ in quoted string");
215                     }
216                } else if ((ch == '\n') || (ch == '\r'))
217                     cfg_error ("newline is not allowed in quoted strings");
218                *here++ = ch;
219           }
220           cfg_error ("Quoted string is too long");
221           return 0;             /* not reached */
222      }
223      here = buf;
224      escaped = 0;
225      while (here - buf < MAX_TOKEN) {
226           if (escaped) {
227                if (ch == EOF)
228                     cfg_error ("\\ precedes EOF");
229                if (ch == '\n')
230                     line_num++;
231                else
232                     *here++ = ch == '\t' ? ' ' : ch;
233                escaped = 0;
234           } else {
235                if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '#' ||
236                    ch == '=' || ch == EOF) {
237                     again (ch);
238                     *here = 0;
239                     return strdup (buf);
240                }
241                if (!(escaped = (ch == '\\')))
242                     *here++ = ch;
243           }
244           ch = next ();
245      }
246      cfg_error ("Token is too long");
247      return 0;                  /* not reached */
248 }
249
250 static void cfg_return_token (char *token)
251 {
252      last_token = token;
253 }
254
255 static int cfg_next (char **item, char **value)
256 {
257      char *this;
258
259      if (last_item) {
260           *item = last_item;
261           *value = last_value;
262           last_item = NULL;
263           return 1;
264      }
265      *value = NULL;
266      if (!(*item = cfg_get_token ()))
267           return 0;
268      if (!strcmp (*item, "="))
269           cfg_error ("Syntax error");
270      if (!(this = cfg_get_token ()))
271           return 1;
272      if (strcmp (this, "=")) {
273           cfg_return_token (this);
274           return 1;
275      }
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);
280      return 1;
281 }
282
283 static char *cfg_get_strg_i (CONFIG * table, char *item)
284 {
285      CONFIG *walk;
286
287      for (walk = table; walk->type != cft_end; walk++)
288           if (walk->name && !strcasecmp (walk->name, item))
289                return walk->data;
290      return 0;
291 }
292
293 #if 0
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)
297 {
298      last_item = item;
299      last_value = value;
300 }
301 #endif
302
303 static int match_arch(const char *name)
304 {
305         static prom_handle root;
306         static char model[256], *p;
307
308         if (!root) {
309                 if (!(root = prom_finddevice("/")))
310                         return 0;
311         }
312
313         if (!model[0]) {
314                 if (!prom_getprop(root, "compatible", model, sizeof(model)))
315                         return 0;
316         }
317
318         for (p = model; *p; p += strlen(p) + 1) {
319                 if (!strcasecmp(p, name))
320                         return 1;
321         }
322
323         return 0;
324 }
325
326 static void check_for_obsolete(const char *label)
327 {
328         struct IMAGES *p;
329         char *cur_label;
330
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)
334                         return;
335         }
336
337         for (p = images; p; p = p->next) {
338                 if (curr_table == p->table)
339                         continue;
340
341                 cur_label = cfg_get_strg_i (p->table, "label");
342                 if (!cur_label)
343                         cur_label = cfg_get_strg_i (p->table, "image");
344
345                 if (!cur_label)
346                         continue;
347
348                 if (!strcasecmp(cur_label, label))
349                         p->obsolete = 1;
350         }
351 }
352
353 static int cfg_set (char *item, char *value)
354 {
355      CONFIG *walk;
356
357      if (!strncasecmp (item, "image", 5)) {
358           struct IMAGES **p = &images;
359           int ignore = 0;
360
361           if (item[5] == '[' && item[strlen(item) - 1] == ']') {
362                 char *s, *q = item;
363
364                 /* Get rid of braces */
365                 item[strlen(item) - 1] = 0;
366                 item[5] = 0;
367
368                 for (s = item + 6; q; s = q) {
369                         q = strchr(s, '|');
370                         if (q)
371                                 *q++ = 0;
372
373                         if (match_arch(s))
374                                 goto cfg_set_cont;
375                 }
376                 /* This just creates an unused table. It will get ignored */
377                 ignore = 1;
378           } else if (item[5])
379                 goto cfg_set_redo;
380
381 cfg_set_cont:
382           while (*p)
383                p = &((*p)->next);
384           *p = (struct IMAGES *)malloc (sizeof (struct IMAGES));
385           if (*p == NULL) {
386                prom_printf("malloc error in cfg_set\n");
387                return -1;
388           }
389           (*p)->next = 0;
390           (*p)->obsolete = ignore;
391           curr_table = ((*p)->table);
392           memcpy (curr_table, cf_image, sizeof (cf_image));
393      }
394
395 cfg_set_redo:
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);
402                else {
403                     if (!strcasecmp (item, "label"))
404                          check_for_obsolete(value);
405                     if (walk->data)
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)
410                          walk->data = value;
411                }
412                break;
413           }
414      }
415
416      if (walk->type != cft_end)
417           return 1;
418
419      //cfg_return (item, value);
420
421      return 0;
422 }
423
424 int cfg_parse (char *cfg_file, char *buff, int len)
425 {
426      char *item, *value;
427
428      file_name = cfg_file;
429      currp = buff;
430      endp = currp + len;
431
432      if (setjmp (env))
433           return -1;
434      while (1) {
435           if (!cfg_next (&item, &value))
436                return 0;
437           if (!cfg_set (item, value)) {
438 #if DEBUG
439                prom_printf("Can't set item %s to value %s\n", item, value);
440 #endif
441           }
442           free (item);
443      }
444 }
445
446 char *cfg_get_strg (char *image, char *item)
447 {
448      struct IMAGES *p;
449      char *label, *alias;
450      char *ret;
451
452      if (!image)
453           return cfg_get_strg_i (cf_options, item);
454      for (p = images; p; p = p->next) {
455           if (p->obsolete)
456                   continue;
457
458           label = cfg_get_strg_i (p->table, "label");
459           if (!label) {
460                label = cfg_get_strg_i (p->table, "image");
461                alias = strrchr (label, '/');
462                if (alias)
463                     label = alias + 1;
464           }
465           alias = cfg_get_strg_i (p->table, "alias");
466           if (!strcmp (label, image) || (alias && !strcmp (alias, image))) {
467                ret = cfg_get_strg_i (p->table, item);
468                if (!ret)
469                     ret = cfg_get_strg_i (cf_options, item);
470                return ret;
471           }
472      }
473      return 0;
474 }
475
476 int cfg_get_flag (char *image, char *item)
477 {
478      return !!cfg_get_strg (image, item);
479 }
480
481 static int printl_count = 0;
482 static void printlabel (char *label, int defflag)
483 {
484      int len = strlen (label);
485
486      if (!printl_count)
487           prom_printf ("\n");
488      prom_printf ("%s %s",defflag?"*":" ", label);
489      while (len++ < 25)
490           prom_putchar (' ');
491      printl_count++;
492      if (printl_count == 3)
493           printl_count = 0;
494 }
495
496 void cfg_print_images (void)
497 {
498      struct IMAGES *p;
499      char *label, *alias;
500
501      char *ret = cfg_get_strg_i (cf_options, "default");
502      int defflag=0;
503
504      printl_count = 0;
505      for (p = images; p; p = p->next) {
506           if (p->obsolete)
507                   continue;
508
509           label = cfg_get_strg_i (p->table, "label");
510           if (!label) {
511                label = cfg_get_strg_i (p->table, "image");
512                alias = strrchr (label, '/');
513                if (alias)
514                     label = alias + 1;
515           }
516           if(!strcmp(ret,label))
517                defflag=1;
518           else
519                defflag=0;
520           alias = cfg_get_strg_i (p->table, "alias");
521           printlabel (label, defflag);
522           if (alias)
523                printlabel (alias, 0);
524      }
525      prom_printf("\n");
526 }
527
528 char *cfg_get_default (void)
529 {
530      char *label;
531      struct IMAGES *p;
532      char *ret = cfg_get_strg_i (cf_options, "default");
533
534      if (ret)
535           return ret;
536      if (!images)
537           return 0;
538
539      for (p = images; p && p->obsolete; p = p->next);
540      if (!p)
541              return 0;
542
543      ret = cfg_get_strg_i (p->table, "label");
544      if (!ret) {
545           ret = cfg_get_strg_i (p->table, "image");
546           label = strrchr (ret, '/');
547           if (label)
548                ret = label + 1;
549      }
550      return ret;
551 }
552
553 /*
554  * Local variables:
555  * c-file-style: "k&r"
556  * c-basic-offset: 5
557  * End:
558  */