remove all trailing whitespace
[grub2/phcoder/solaris.git] / script / sh / function.c
blob1e49c709fade893a59049ce91f5805d3c26dcd55
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2005,2007,2009 Free Software Foundation, Inc.
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.
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.
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/>.
19 #include <grub/misc.h>
20 #include <grub/script_sh.h>
21 #include <grub/parser.h>
22 #include <grub/mm.h>
24 static grub_script_function_t grub_script_function_list;
26 grub_script_function_t
27 grub_script_function_create (struct grub_script_arg *functionname_arg,
28 struct grub_script *cmd)
30 grub_script_function_t func;
31 grub_script_function_t *p;
33 func = (grub_script_function_t) grub_malloc (sizeof (*func));
34 if (! func)
35 return 0;
37 func->name = grub_script_execute_argument_to_string (functionname_arg);
38 if (! func->name)
40 grub_free (func);
41 return 0;
44 func->func = cmd;
46 /* Keep the list sorted for simplicity. */
47 p = &grub_script_function_list;
48 while (*p)
50 if (grub_strcmp ((*p)->name, func->name) >= 0)
51 break;
53 p = &((*p)->next);
56 /* If the function already exists, overwrite the old function. */
57 if (*p && grub_strcmp ((*p)->name, func->name) == 0)
59 grub_script_function_t q;
61 q = *p;
62 grub_script_free (q->func);
63 q->func = cmd;
64 grub_free (func);
65 func = q;
67 else
69 func->next = *p;
70 *p = func;
73 return func;
76 void
77 grub_script_function_remove (const char *name)
79 grub_script_function_t *p, q;
81 for (p = &grub_script_function_list, q = *p; q; p = &(q->next), q = q->next)
82 if (grub_strcmp (name, q->name) == 0)
84 *p = q->next;
85 grub_free (q->name);
86 grub_script_free (q->func);
87 grub_free (q);
88 break;
92 grub_script_function_t
93 grub_script_function_find (char *functionname)
95 grub_script_function_t func;
97 for (func = grub_script_function_list; func; func = func->next)
98 if (grub_strcmp (functionname, func->name) == 0)
99 break;
101 if (! func)
102 grub_error (GRUB_ERR_UNKNOWN_COMMAND, "unknown command `%s'", functionname);
104 return func;
108 grub_script_function_iterate (int (*iterate) (grub_script_function_t))
110 grub_script_function_t func;
112 for (func = grub_script_function_list; func; func = func->next)
113 if (iterate (func))
114 return 1;
116 return 0;
120 grub_script_function_call (grub_script_function_t func,
121 int argc __attribute__((unused)),
122 char **args __attribute__((unused)))
124 /* XXX: Arguments are not supported yet. */
125 return grub_script_execute (func->func);