]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/script/function.c
keep up with 1.0.67 (#1464)
[Ventoy.git] / GRUB2 / MOD_SRC / grub-2.04 / grub-core / script / function.c
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2005,2007,2009,2010 Free Software Foundation, Inc.
4 *
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * GRUB is distributed in the hope that it will be useful,
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
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <grub/misc.h>
20 #include <grub/script_sh.h>
21 #include <grub/parser.h>
22 #include <grub/mm.h>
23 #include <grub/charset.h>
24
25 grub_script_function_t grub_script_function_list;
26
27 grub_script_function_t
28 grub_script_function_create (struct grub_script_arg *functionname_arg,
29 struct grub_script *cmd)
30 {
31 grub_script_function_t func;
32 grub_script_function_t *p;
33
34 func = (grub_script_function_t) grub_malloc (sizeof (*func));
35 if (! func)
36 return 0;
37
38 func->name = grub_strdup (functionname_arg->str);
39 if (! func->name)
40 {
41 grub_free (func);
42 return 0;
43 }
44
45 func->func = cmd;
46
47 /* Keep the list sorted for simplicity. */
48 p = &grub_script_function_list;
49 while (*p)
50 {
51 if (grub_strcmp ((*p)->name, func->name) >= 0)
52 break;
53
54 p = &((*p)->next);
55 }
56
57 /* If the function already exists, overwrite the old function. */
58 if (*p && grub_strcmp ((*p)->name, func->name) == 0)
59 {
60 grub_script_function_t q;
61
62 q = *p;
63 grub_script_free (q->func);
64 q->func = cmd;
65 grub_free (func);
66 func = q;
67 }
68 else
69 {
70 func->next = *p;
71 *p = func;
72 }
73
74 return func;
75 }
76
77 void
78 grub_script_function_remove (const char *name)
79 {
80 grub_script_function_t *p, q;
81
82 for (p = &grub_script_function_list, q = *p; q; p = &(q->next), q = q->next)
83 if (grub_strcmp (name, q->name) == 0)
84 {
85 *p = q->next;
86 grub_free (q->name);
87 grub_script_free (q->func);
88 grub_free (q);
89 break;
90 }
91 }
92
93 grub_script_function_t
94 grub_script_function_find (char *functionname)
95 {
96 grub_script_function_t func;
97
98 for (func = grub_script_function_list; func; func = func->next)
99 if (grub_strcmp (functionname, func->name) == 0)
100 break;
101
102 if (! func)
103 {
104 char tmp[64];
105 grub_strncpy (tmp, functionname, 63);
106 tmp[63] = 0;
107 /* Avoid truncating inside UTF-8 character. */
108 tmp[grub_getend (tmp, tmp + grub_strlen (tmp))] = 0;
109 grub_error (GRUB_ERR_UNKNOWN_COMMAND, N_("can't find command `%s'"), tmp);
110 }
111
112 return func;
113 }