]> git.ozlabs.org Git - petitboot/blob - discover/native-parser.c
Fix parsing for OpenSUSE
[petitboot] / discover / native-parser.c
1
2 #include "parser.h"
3 #include "params.h"
4 #include "paths.h"
5
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9
10 const char *conf_filename = "/boot/petitboot.conf";
11
12 static struct boot_option *cur_opt;
13 static struct device *dev;
14 static const char *devpath;
15 int device_added;
16
17 int check_and_add_device(struct device *dev)
18 {
19         if (!dev->icon_file)
20                 dev->icon_file = strdup(generic_icon_file(guess_device_type()));
21
22         return !add_device(dev);
23 }
24
25 static int section(char *section_name)
26 {
27         if (!device_added++ && !check_and_add_device(dev))
28                 return 0;
29
30         if (cur_opt) {
31                 add_boot_option(cur_opt);
32                 free_boot_option(cur_opt);
33         }
34
35         cur_opt = malloc(sizeof(*cur_opt));
36         memset(cur_opt, 0, sizeof(*cur_opt));
37         return 1;
38 }
39
40
41 static void set_boot_option_parameter(struct boot_option *opt,
42                 const char *name, const char *value)
43 {
44         if (streq(name, "name"))
45                 opt->name = strdup(value);
46
47         else if (streq(name, "description"))
48                 opt->description = strdup(value);
49
50         else if (streq(name, "image"))
51                 opt->boot_image_file = resolve_path(value, devpath);
52
53         else if (streq(name, "icon"))
54                 opt->icon_file = resolve_path(value, devpath);
55
56         else if (streq(name, "initrd"))
57                 opt->initrd_file =resolve_path(value, devpath);
58
59         else if (streq(name, "args"))
60                 opt->boot_args = strdup(value);
61
62         else
63                 fprintf(stderr, "Unknown parameter %s\n", name);
64 }
65
66 static void set_device_parameter(struct device *dev,
67                 const char *name, const char *value)
68 {
69         if (streq(name, "name"))
70                 dev->name = strdup(value);
71
72         else if (streq(name, "description"))
73                 dev->description = strdup(value);
74
75         else if (streq(name, "icon"))
76                 dev->icon_file = resolve_path(value, devpath);
77 }
78
79 static int parameter(char *param_name, char *param_value)
80 {
81         if (cur_opt)
82                 set_boot_option_parameter(cur_opt, param_name, param_value);
83         else
84                 set_device_parameter(dev, param_name, param_value);
85         return 1;
86 }
87
88
89 int parse(const char *device)
90 {
91         char *filepath;
92         int rc;
93
94         filepath = resolve_path(conf_filename, device);
95
96         cur_opt = NULL;
97         dev = malloc(sizeof(*dev));
98         memset(dev, 0, sizeof(*dev));
99         dev->id = strdup(device);
100
101         rc = pm_process(filepath, section, parameter);
102         if (!rc)
103                 return 0;
104
105         if (cur_opt) {
106                 add_boot_option(cur_opt);
107                 free_boot_option(cur_opt);
108         }
109
110         cur_opt = NULL;
111
112         free(filepath);
113
114         return 1;
115 }
116
117 struct parser native_parser = {
118         .name = "native petitboot parser",
119         .priority = 100,
120         .parse    = parse
121 };
122
123
124