]> git.proxmox.com Git - grub2.git/blob - include/grub/script.h
2006-01-17 Marco Gerards <marco@gnu.org>
[grub2.git] / include / grub / script.h
1 /* script.h */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005 Free Software Foundation, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #ifndef GRUB_SCRIPT_HEADER
22 #define GRUB_SCRIPT_HEADER 1
23
24 #include <grub/types.h>
25 #include <grub/err.h>
26
27 struct grub_script_mem;
28
29 /* The generic header for each scripting command or structure. */
30 struct grub_script_cmd
31 {
32 /* This function is called to execute the command. */
33 grub_err_t (*exec) (struct grub_script_cmd *cmd);
34
35 /* The next command. This can be used by the parent to form a chain
36 of commands. */
37 struct grub_script_cmd *next;
38 };
39
40 struct grub_script
41 {
42 struct grub_script_mem *mem;
43 struct grub_script_cmd *cmd;
44 };
45 \f
46 typedef enum
47 {
48 GRUB_SCRIPT_ARG_TYPE_STR,
49 GRUB_SCRIPT_ARG_TYPE_VAR
50 } grub_script_arg_type_t;
51
52 /* A part of an argument. */
53 struct grub_script_arg
54 {
55 grub_script_arg_type_t type;
56
57 char *str;
58
59 /* Next argument part. */
60 struct grub_script_arg *next;
61 };
62
63 /* A complete argument. It consists of a list of one or more `struct
64 grub_script_arg's. */
65 struct grub_script_arglist
66 {
67 struct grub_script_arglist *next;
68 struct grub_script_arg *arg;
69 /* Only stored in the first link. */
70 int argcount;
71 };
72
73 /* A single command line. */
74 struct grub_script_cmdline
75 {
76 struct grub_script_cmd cmd;
77
78 /* The arguments for this command. */
79 struct grub_script_arglist *arglist;
80
81 /* The command name of this command. XXX: Perhaps an argument
82 should be used for this so we can use variables as command
83 name. */
84 char *cmdname;
85 };
86
87 /* A block of commands, this can be used to group commands. */
88 struct grub_script_cmdblock
89 {
90 struct grub_script_cmd cmd;
91
92 /* A chain of commands. */
93 struct grub_script_cmd *cmdlist;
94 };
95
96 /* An if statement. */
97 struct grub_script_cmdif
98 {
99 struct grub_script_cmd cmd;
100
101 /* The command used to check if the if is true or false. */
102 struct grub_script_cmd *bool;
103
104 /* The code executed in case the result if bool was true. */
105 struct grub_script_cmd *true;
106
107 /* The code executed in case the result if bool was false. */
108 struct grub_script_cmd *false;
109 };
110
111 /* A menu entry generate statement. */
112 struct grub_script_cmd_menuentry
113 {
114 struct grub_script_cmd cmd;
115
116 /* The title of the menu entry. */
117 struct grub_script_arg *title;
118
119 /* The sourcecode the entry will be generated from. */
120 const char *sourcecode;
121
122 /* Options. XXX: Not used yet. */
123 int options;
124 };
125
126 struct grub_script_arglist *
127 grub_script_create_arglist (void);
128
129 struct grub_script_arglist *
130 grub_script_add_arglist (struct grub_script_arglist *list,
131 struct grub_script_arg *arg);
132 struct grub_script_cmd *
133 grub_script_create_cmdline (char *cmdname,
134 struct grub_script_arglist *arglist);
135 struct grub_script_cmd *
136 grub_script_create_cmdblock (void);
137
138 struct grub_script_cmd *
139 grub_script_create_cmdif (struct grub_script_cmd *bool,
140 struct grub_script_cmd *true,
141 struct grub_script_cmd *false);
142
143 struct grub_script_cmd *
144 grub_script_create_cmdmenu (struct grub_script_arg *title,
145 char *sourcecode,
146 int options);
147
148 struct grub_script_cmd *
149 grub_script_add_cmd (struct grub_script_cmdblock *cmdblock,
150 struct grub_script_cmd *cmd);
151 struct grub_script_arg *
152 grub_script_arg_add (struct grub_script_arg *arg,
153 grub_script_arg_type_t type, char *str);
154
155 struct grub_script *grub_script_parse (char *script,
156 grub_err_t (*getline) (char **));
157 void grub_script_free (struct grub_script *script);
158 struct grub_script *grub_script_create (struct grub_script_cmd *cmd,
159 struct grub_script_mem *mem);
160
161 void grub_script_lexer_init (char *s, grub_err_t (*getline) (char **));
162 void grub_script_lexer_ref (void);
163 void grub_script_lexer_deref (void);
164 void grub_script_lexer_record_start (void);
165 char *grub_script_lexer_record_stop (void);
166
167 /* Functions to track allocated memory. */
168 void *grub_script_malloc (grub_size_t size);
169 struct grub_script_mem *grub_script_mem_record (void);
170 struct grub_script_mem *grub_script_mem_record_stop (struct grub_script_mem *restore);
171
172 /* Functions used by bison. */
173 int grub_script_yylex (void);
174 int grub_script_yyparse (void);
175 void grub_script_yyerror (char const *err);
176
177 /* Commands to execute, don't use these directly. */
178 grub_err_t grub_script_execute_cmdline (struct grub_script_cmd *cmd);
179 grub_err_t grub_script_execute_cmdblock (struct grub_script_cmd *cmd);
180 grub_err_t grub_script_execute_cmdif (struct grub_script_cmd *cmd);
181 grub_err_t grub_script_execute_menuentry (struct grub_script_cmd *cmd);
182
183 /* Execute any GRUB pre-parsed command or script. */
184 grub_err_t grub_script_execute (struct grub_script *script);
185
186 /* This variable points to the parsed command. This is used to
187 communicate with the bison code. */
188 extern struct grub_script_cmd *grub_script_parsed;
189
190 \f
191
192 /* The function description. */
193 struct grub_script_function
194 {
195 /* The name. */
196 char *name;
197
198 /* The script function. */
199 struct grub_script *func;
200
201 /* The flags. */
202 unsigned flags;
203
204 /* The next element. */
205 struct grub_script_function *next;
206
207 int references;
208 };
209 typedef struct grub_script_function *grub_script_function_t;
210
211 grub_script_function_t grub_script_function_create (char *functionname,
212 struct grub_script *cmd);
213 void grub_script_function_remove (const char *name);
214 grub_script_function_t grub_script_function_find (char *functionname);
215 int grub_script_function_iterate (int (*iterate) (grub_script_function_t));
216 int grub_script_function_call (grub_script_function_t func,
217 int argc, char **args);
218
219 #endif /* ! GRUB_SCRIPT_HEADER */