discover/grub2: Function infrastructure improvements
[petitboot] / discover / grub2 / builtins.c
1
2 #include <stdio.h>
3 #include <string.h>
4
5 #include <talloc/talloc.h>
6 #include <array-size/array-size.h>
7
8 #include "grub2.h"
9
10
11 static int builtin_set(struct grub2_script *script,
12                 void *data __attribute__((unused)),
13                 int argc, char *argv[])
14 {
15         char *name, *value, *p;
16         int i;
17
18         if (argc < 2)
19                 return -1;
20
21         p = strchr(argv[1], '=');
22         if (!p)
23                 return -1;
24
25         name = talloc_strndup(script, argv[1], p - argv[1]);
26         value = talloc_strdup(script, p+1);
27
28         for (i = 2; i < argc; i++)
29                 value = talloc_asprintf_append(value, " %s", argv[i]);
30
31         script_env_set(script, name, value);
32
33         return 0;
34 }
35
36 static struct {
37         const char *name;
38         grub2_function fn;
39 } builtins[] = {
40         {
41                 .name = "set",
42                 .fn = builtin_set
43         },
44 };
45
46 void register_builtins(struct grub2_script *script)
47 {
48         unsigned int i;
49
50         for (i = 0; i < ARRAY_SIZE(builtins); i++)
51                 script_register_function(script, builtins[i].name,
52                                 builtins[i].fn, NULL);
53 }