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