6 #include <types/types.h>
7 #include <talloc/talloc.h>
10 #include "discover/parser.h"
14 static int builtin_set(struct grub2_script *script,
15 void *data __attribute__((unused)),
16 int argc, char *argv[])
18 char *name, *value, *p;
24 p = strchr(argv[1], '=');
28 name = talloc_strndup(script, argv[1], p - argv[1]);
29 value = talloc_strdup(script, p+1);
31 for (i = 2; i < argc; i++)
32 value = talloc_asprintf_append(value, " %s", argv[i]);
34 script_env_set(script, name, value);
39 static int builtin_linux(struct grub2_script *script,
40 void *data __attribute__((unused)),
41 int argc, char *argv[])
43 struct discover_boot_option *opt = script->opt;
48 pb_log("grub2 syntax error: 'linux' statement outside "
54 pb_log("grub2 syntax error: no filename provided to "
59 root = script_env_get(script, "root");
61 opt->boot_image = create_grub2_resource(opt, script->ctx->device,
63 opt->option->boot_args = NULL;
66 opt->option->boot_args = talloc_strdup(opt, argv[2]);
68 for (i = 3; i < argc; i++)
69 opt->option->boot_args = talloc_asprintf_append(
70 opt->option->boot_args,
75 static int builtin_initrd(struct grub2_script *script,
76 void *data __attribute__((unused)),
77 int argc, char *argv[])
79 struct discover_boot_option *opt = script->opt;
83 pb_log("grub2 syntax error: 'initrd' statement outside "
89 pb_log("grub2 syntax error: no filename provided to "
90 "initrd statement\n");
94 root = script_env_get(script, "root");
95 opt->initrd = create_grub2_resource(opt, script->ctx->device,
101 static int builtin_search(struct grub2_script *script,
102 void *data __attribute__((unused)),
103 int argc, char *argv[])
105 const char *env_var, *spec;
110 for (i = 1; i < argc - 1; i++) {
111 if (!strncmp(argv[i], "--set=", strlen("--set="))) {
112 env_var = argv[i] + strlen("--set=");
120 spec = argv[argc - 1];
122 script_env_set(script, env_var, spec);
127 static bool builtin_test_op_file(struct grub2_script *script, char op,
134 rc = parser_request_file(script->ctx, script->ctx->device,
141 /* -s: return true if file exists and has non-zero size */
145 /* -f: return true if file exists */
157 static bool builtin_test_op(struct grub2_script *script,
158 int argc, char **argv, int *consumed)
169 if (!strcmp(op, "=") || !strcmp(op, "==")) {
171 return !strcmp(a1, a2);
174 if (!strcmp(op, "!=")) {
176 return strcmp(a1, a2);
179 if (!strcmp(op, "<")) {
181 return strcmp(a1, a2) < 0;
184 if (!strcmp(op, ">")) {
186 return strcmp(a1, a2) > 0;
196 if (!strcmp(op, "-z")) {
198 return strlen(a1) == 0;
201 if (!strcmp(op, "-n")) {
203 return strlen(a1) != 0;
206 if (!strcmp(op, "-s") || !strcmp(op, "-f")) {
208 return builtin_test_op_file(script, op[1], a1);
214 return strlen(op) > 0;
217 static int builtin_test(struct grub2_script *script,
218 void *data __attribute__((unused)),
219 int argc, char *argv[])
224 if (!strcmp(argv[0], "[") && !strcmp(argv[argc - 1], "]"))
227 /* skip command name */
234 for (consumed = 0; argc > 0; argv += consumed, argc -= consumed) {
236 if (!strcmp(argv[0], "!")) {
242 if (!strcmp(argv[0], "-a")) {
249 if (!strcmp(argv[0], "-o")) {
256 rc = builtin_test_op(script, argc, argv, &consumed);
266 static int builtin_true(struct grub2_script *script __attribute__((unused)),
267 void *data __attribute__((unused)),
268 int argc __attribute__((unused)),
269 char *argv[] __attribute__((unused)))
274 static int builtin_false(struct grub2_script *script __attribute__((unused)),
275 void *data __attribute__((unused)),
276 int argc __attribute__((unused)),
277 char *argv[] __attribute__((unused)))
282 static int builtin_nop(struct grub2_script *script __attribute__((unused)),
283 void *data __attribute__((unused)),
284 int argc __attribute__((unused)),
285 char *argv[] __attribute__((unused)))
290 extern int builtin_load_env(struct grub2_script *script,
291 void *data __attribute__((unused)),
292 int argc, char *argv[]);
293 int builtin_save_env(struct grub2_script *script,
294 void *data __attribute__((unused)),
295 int argc, char *argv[]);
316 .fn = builtin_initrd,
320 .fn = builtin_search,
340 .fn = builtin_load_env,
344 .fn = builtin_save_env,
348 static const char *nops[] = {
349 "echo", "export", "insmod", "loadfont", "terminfo",
352 void register_builtins(struct grub2_script *script)
356 for (i = 0; i < ARRAY_SIZE(builtins); i++)
357 script_register_function(script, builtins[i].name,
358 builtins[i].fn, NULL);
360 for (i = 0; i < ARRAY_SIZE(nops); i++)
361 script_register_function(script, nops[i], builtin_nop, NULL);