]> git.proxmox.com Git - grub2.git/blame - normal/function.c
2007-07-22 Yoshinori K. Okuji <okuji@enbug.org>
[grub2.git] / normal / function.c
CommitLineData
daac212a 1/*
2 * GRUB -- GRand Unified Bootloader
5a79f472 3 * Copyright (C) 2005,2007 Free Software Foundation, Inc.
daac212a 4 *
5a79f472 5 * GRUB is free software: you can redistribute it and/or modify
daac212a 6 * it under the terms of the GNU General Public License as published by
5a79f472 7 * the Free Software Foundation, either version 3 of the License, or
daac212a 8 * (at your option) any later version.
9 *
5a79f472 10 * GRUB is distributed in the hope that it will be useful,
daac212a 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
5a79f472 16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
daac212a 17 */
18
19#include <grub/misc.h>
20#include <grub/script.h>
21#include <grub/parser.h>
22#include <grub/mm.h>
23
24static grub_script_function_t grub_script_function_list;
25
26grub_script_function_t
27grub_script_function_create (char *functionname, struct grub_script *cmd)
28{
29 grub_script_function_t func;
30 grub_script_function_t *p;
31
32 func = (grub_script_function_t) grub_malloc (sizeof (*func));
33 if (! func)
34 return 0;
35
36 func->name = grub_strdup (functionname);
37 if (! func->name)
38 {
39 grub_free (func);
40 return 0;
41 }
42
43 func->func = cmd;
44
45 /* Keep the list sorted for simplicity. */
46 p = &grub_script_function_list;
47 while (*p)
48 {
49 if (grub_strcmp ((*p)->name, functionname) >= 0)
50 break;
51
52 p = &((*p)->next);
53 }
54
55 /* If the function already exists, overwrite the old function. */
56 if (*p && grub_strcmp ((*p)->name, functionname) == 0)
57 {
58 grub_script_function_t q;
59
60 q = *p;
61 grub_script_free (q->func);
62 q->func = cmd;
63 grub_free (func);
64 func = q;
65 }
66 else
67 {
68 func->next = *p;
69 *p = func;
70 }
71
72 return func;
73}
74
75void
76grub_script_function_remove (const char *name)
77{
78 grub_script_function_t *p, q;
79
80 for (p = &grub_script_function_list, q = *p; q; p = &(q->next), q = q->next)
81 if (grub_strcmp (name, q->name) == 0)
82 {
83 *p = q->next;
84 grub_free (q->name);
85 grub_script_free (q->func);
86 grub_free (q);
87 break;
88 }
89}
90
91grub_script_function_t
92grub_script_function_find (char *functionname)
93{
94 grub_script_function_t func;
95
96 for (func = grub_script_function_list; func; func = func->next)
97 if (grub_strcmp (functionname, func->name) == 0)
98 break;
99
100 if (! func)
101 grub_error (GRUB_ERR_UNKNOWN_COMMAND, "unknown command `%s'", functionname);
102
103 return func;
104}
105
106int
107grub_script_function_iterate (int (*iterate) (grub_script_function_t))
108{
109 grub_script_function_t func;
110
111 for (func = grub_script_function_list; func; func = func->next)
112 if (iterate (func))
113 return 1;
114
115 return 0;
116}
117
118int
119grub_script_function_call (grub_script_function_t func,
120 int argc __attribute__((unused)),
121 char **args __attribute__((unused)))
122{
123 /* XXX: Arguments are not supported yet. */
124 return grub_script_execute (func->func);
125}