6 #include <talloc/talloc.h>
13 struct list_item list;
16 static const char *env_lookup(struct grub2_script *script,
17 const char *name, int name_len)
19 struct env_entry *entry;
22 str = talloc_strndup(script, name, name_len);
23 printf("%s: %s\n", __func__, str);
25 list_for_each_entry(&script->environment, entry, list)
26 if (!strncmp(entry->name, name, name_len))
32 static bool expand_word(struct grub2_script *script, struct grub2_word *word)
34 const char *val, *src;
41 n = regexec(&script->var_re, src, 1, &match, 0);
45 val = env_lookup(script, src + match.rm_so,
46 match.rm_eo - match.rm_so);
50 dest = talloc_strndup(script, src, match.rm_so);
51 dest = talloc_asprintf_append(dest, "%s%s", val, src + match.rm_eo);
57 /* iterate through the words in an argv, looking for expansions. If a
58 * word is marked with expand == true, then we process any variable
61 * Once that's done, we may (if split == true) have to split the word to create
64 static void process_expansions(struct grub2_script *script,
65 struct grub2_argv *argv)
67 struct grub2_word *word;
69 list_for_each_entry(&argv->words, word, argv_list) {
73 expand_word(script, word);
77 static int script_destroy(void *p)
79 struct grub2_script *script = p;
80 regfree(&script->var_re);
84 struct grub2_script *create_script(void *ctx)
86 struct grub2_script *script;
89 script = talloc(ctx, struct grub2_script);
91 rc = regcomp(&script->var_re,
92 "\\$\\{?([[:alpha:]][_[:alnum:]]*|[0-9]|[\\?@\\*#])\\}?",
96 regerror(rc, &script->var_re, err, sizeof(err));
97 fprintf(stderr, "RE error %d: %s\n", rc, err);
102 talloc_set_destructor(script, script_destroy);
104 list_init(&script->environment);