Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / lib / legacy_parse.c
blob775eaad1f7ce968a7ac4b5d965894f3eb434a598
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 1999,2000,2001,2002,2003,2004,2010 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/types.h>
20 #include <grub/misc.h>
21 #include <grub/mm.h>
22 #include <grub/err.h>
23 #include <grub/legacy_parse.h>
24 #include <grub/i386/pc/vesa_modes_table.h>
25 #include <grub/i18n.h>
27 struct legacy_command
29 const char *name;
30 const char *map;
31 const char *suffix;
32 unsigned suffixarg;
33 unsigned argc;
34 enum arg_type {
35 TYPE_VERBATIM,
36 TYPE_FORCE_OPTION,
37 TYPE_NOAPM_OPTION,
38 TYPE_TYPE_OR_NOMEM_OPTION,
39 TYPE_OPTION,
40 TYPE_FILE,
41 TYPE_FILE_NO_CONSUME,
42 TYPE_PARTITION,
43 TYPE_BOOL,
44 TYPE_INT,
45 TYPE_REST_VERBATIM,
46 TYPE_VBE_MODE,
47 TYPE_WITH_CONFIGFILE_OPTION
48 } argt[4];
49 enum {
50 FLAG_IGNORE_REST = 0x001,
51 FLAG_FALLBACK_AVAILABLE = 0x004,
52 FLAG_FALLBACK = 0x008,
53 FLAG_COLOR_INVERT = 0x010,
54 FLAG_NO_MENUENTRY = 0x020,
55 FLAG_MENUENTRY_ONLY = 0x040,
56 FLAG_TERMINAL = 0x080,
57 FLAG_TITLE = 0x100,
58 } flags;
59 const char *shortdesc;
60 const char *longdesc;
63 /* Help texts are kept here mostly for reference. They are never shown. So
64 no need to gettextize.
66 static struct legacy_command legacy_commands[] =
68 {"blocklist", "blocklist '%s'\n", NULL, 0, 1, {TYPE_FILE}, 0, "FILE",
69 "Print the blocklist notation of the file FILE."},
70 {"boot", "boot\n", NULL, 0, 0, {}, 0, 0,
71 "Boot the OS/chain-loader which has been loaded."},
72 {"bootp", "net_bootp; net_ls_addr; if [ x%s = x--with-configfile ]; then "
73 "if net_get_dhcp_option configfile_name pxe 150 string; then "
74 "configfile $configfile_name; fi; fi\n", NULL, 0, 1,
75 {TYPE_WITH_CONFIGFILE_OPTION}, FLAG_IGNORE_REST, "[--with-configfile]",
76 "Initialize a network device via BOOTP. If the option `--with-configfile'"
77 " is given, try to load a configuration file specified by the 150 vendor"
78 " tag."},
79 {"cat", "cat '%s'\n", NULL, 0, 1, {TYPE_FILE}, 0, "FILE",
80 "Print the contents of the file FILE."},
81 {"chainloader", "chainloader %s '%s'\n", NULL, 0,
82 2, {TYPE_FORCE_OPTION, TYPE_FILE}, 0, "[--force] FILE",
83 "Load the chain-loader FILE. If --force is specified, then load it"
84 " forcibly, whether the boot loader signature is present or not."},
85 {"cmp", "cmp '%s' '%s'\n", NULL, 0,
86 2, {TYPE_FILE, TYPE_FILE}, FLAG_IGNORE_REST, "FILE1 FILE2",
87 "Compare the file FILE1 with the FILE2 and inform the different values"
88 " if any."},
89 {"color", "set color_normal='%s'; set color_highlight='%s'\n", NULL, 0,
90 2, {TYPE_VERBATIM, TYPE_VERBATIM},
91 FLAG_IGNORE_REST | FLAG_FALLBACK_AVAILABLE, "NORMAL [HIGHLIGHT]",
92 "Change the menu colors. The color NORMAL is used for most"
93 " lines in the menu, and the color HIGHLIGHT is used to highlight the"
94 " line where the cursor points. If you omit HIGHLIGHT, then the"
95 " inverted color of NORMAL is used for the highlighted line."
96 " The format of a color is \"FG/BG\". FG and BG are symbolic color names."
97 " A symbolic color name must be one of these: black, blue, green,"
98 " cyan, red, magenta, brown, light-gray, dark-gray, light-blue,"
99 " light-green, light-cyan, light-red, light-magenta, yellow and white."
100 " But only the first eight names can be used for BG. You can prefix"
101 " \"blink-\" to FG if you want a blinking foreground color."},
102 {"color", "set color_normal='%s'; set color_highlight='%s'\n", NULL, 0,
103 1, {TYPE_VERBATIM},
104 FLAG_IGNORE_REST | FLAG_FALLBACK | FLAG_COLOR_INVERT, NULL, NULL},
105 {"configfile", "legacy_configfile '%s'\n", NULL, 0, 1, {TYPE_FILE},
106 0, "FILE", "Load FILE as the configuration file."},
107 {"debug",
108 "if [ -z \"$debug\" ]; then set debug=all; else set debug=; fi\n", NULL, 0,
109 0, {}, 0, 0, "Turn on/off the debug mode."},
110 {"default",
111 "set default='%s'; if [ x\"$default\" = xsaved ]; then load_env; "
112 "set default=\"$saved_entry\"; fi\n", NULL, 0, 1, {TYPE_VERBATIM}, 0,
113 "[NUM | `saved']",
114 "Set the default entry to entry number NUM (if not specified, it is"
115 " 0, the first entry) or the entry number saved by savedefault."},
116 {"dhcp", "net_bootp; net_ls_addr; if [ x%s = x--with-configfile ]; then "
117 "if net_get_dhcp_option configfile_name pxe 150 string; then "
118 "configfile $configfile_name; fi; fi\n", NULL, 0, 1,
119 {TYPE_WITH_CONFIGFILE_OPTION}, FLAG_IGNORE_REST, "[--with-configfile]",
120 "Initialize a network device via BOOTP. If the option `--with-configfile'"
121 " is given, try to load a configuration file specified by the 150 vendor"
122 " tag."},
123 {"displayapm", "lsapm\n", NULL, 0, 0, {}, 0, 0,
124 "Display APM BIOS information."},
125 {"displaymem", "lsmmap\n", NULL, 0, 0, {}, 0, 0,
126 "Display what GRUB thinks the system address space map of the"
127 " machine is, including all regions of physical RAM installed."},
128 /* NOTE: embed unsupported. */
129 {"fallback", "set fallback='%s'\n", NULL, 0,
130 1, {TYPE_VERBATIM}, 0, "NUM...",
131 "Go into unattended boot mode: if the default boot entry has any"
132 " errors, instead of waiting for the user to do anything, it"
133 " immediately starts over using the NUM entry (same numbering as the"
134 " `default' command). This obviously won't help if the machine"
135 " was rebooted by a kernel that GRUB loaded."},
136 {"find", "search -f '%s'\n", NULL, 0, 1, {TYPE_FILE}, 0, "FILENAME",
137 "Search for the filename FILENAME in all of partitions and print the list of"
138 " the devices which contain the file."},
139 /* FIXME: fstest unsupported. */
140 /* NOTE: The obsolete C/H/S geometry isn't shown anymore. */
141 {"geometry", "insmod regexp; ls -l (%s*)\n", NULL, 0, 1, {TYPE_VERBATIM}, 0, "DRIVE",
142 "Print the information for a drive DRIVE. "},
143 {"halt", "halt %s\n", NULL, 0, 1, {TYPE_NOAPM_OPTION}, 0, "[--no-apm]",
144 "Halt your system. If APM is available on it, turn off the power using"
145 " the APM BIOS, unless you specify the option `--no-apm'."},
146 /* FIXME: help unsupported. */ /* NUL_TERMINATE */
147 {"hiddenmenu", NULL,
148 "if sleep -i $timeout; then timeout=0; else timeout=-1; fi\n", 0,
149 0, {}, 0, "", "Hide the menu."},
150 {"hide", "parttool '%s' hidden+\n", NULL, 0, 1, {TYPE_PARTITION},
151 0, "PARTITION",
152 "Hide PARTITION by setting the \"hidden\" bit in"
153 " its partition type code."},
154 /* FIXME: ifconfig unsupported. */
155 /* FIXME: impsprobe unsupported. */
156 {"initrd", "legacy_initrd '%s' %s\n", NULL, 0, 2, {TYPE_FILE_NO_CONSUME,
157 TYPE_REST_VERBATIM}, 0,
158 "FILE [ARG ...]",
159 "Load an initial ramdisk FILE for a Linux format boot image and set the"
160 " appropriate parameters in the Linux setup area in memory."},
161 /* NOTE: install unsupported. */
162 /* FIXME: ioprobe unsupported. */
163 /* FIXME: really support --no-mem-option. */
164 {"kernel", "legacy_kernel %s %s '%s' %s\n", NULL, 0,
165 4, {TYPE_TYPE_OR_NOMEM_OPTION, TYPE_TYPE_OR_NOMEM_OPTION,
166 TYPE_FILE_NO_CONSUME, TYPE_REST_VERBATIM}, 0,
167 "[--no-mem-option] [--type=TYPE] FILE [ARG ...]",
168 "Attempt to load the primary boot image from FILE. The rest of the"
169 " line is passed verbatim as the \"kernel command line\". Any modules"
170 " must be reloaded after using this command. The option --type is used"
171 " to suggest what type of kernel to be loaded. TYPE must be either of"
172 " \"netbsd\", \"freebsd\", \"openbsd\", \"linux\", \"biglinux\" and"
173 " \"multiboot\". The option --no-mem-option tells GRUB not to pass a"
174 " Linux's mem option automatically."},
175 {"lock", "if ! authenticate legacy; then return; fi", NULL, 0, 0, {}, 0,
176 0, "Break a command execution unless the user is authenticated."},
177 {"makeactive", "parttool \"$root\" boot+\n", NULL, 0, 0, {}, 0, 0,
178 "Set the active partition on the root disk to GRUB's root device."
179 " This command is limited to _primary_ PC partitions on a hard disk."},
180 {"map", "drivemap '%s' '%s'\n", NULL, 0,
181 2, {TYPE_PARTITION, TYPE_PARTITION},
182 FLAG_IGNORE_REST, "TO_DRIVE FROM_DRIVE",
183 "Map the drive FROM_DRIVE to the drive TO_DRIVE. This is necessary"
184 " when you chain-load some operating systems, such as DOS, if such an"
185 " OS resides at a non-first drive."},
186 /* NOTE: md5crypt unsupported since GRUB has not enough entropy and this
187 hash shouldn't be used anymore. */
188 {"module", "legacy_initrd '%s' %s\n", NULL, 0, 2, {TYPE_FILE_NO_CONSUME,
189 TYPE_REST_VERBATIM}, 0,
190 "FILE [ARG ...]",
191 "Load a boot module FILE for a Multiboot format boot image (no"
192 " interpretation of the file contents is made, so users of this"
193 " command must know what the kernel in question expects). The"
194 " rest of the line is passed as the \"module command line\", like"
195 " the `kernel' command."},
196 {"modulenounzip", "legacy_initrd_nounzip '%s' %s\n", NULL, 0, 2,
197 {TYPE_FILE_NO_CONSUME, TYPE_REST_VERBATIM}, 0,
198 "FILE [ARG ...]",
199 "The same as `module', except that automatic decompression is"
200 " disabled."},
201 {"pager", "set pager=%s; if [ \"$pager\" = 0 ]; then "
202 " echo Internal pager is now off; else "
203 "echo Internal pager is now on; fi\n", NULL, 0,
204 1, {TYPE_BOOL}, FLAG_FALLBACK_AVAILABLE, "[FLAG]",
205 "Toggle pager mode with no argument. If FLAG is given and its value"
206 " is `on', turn on the mode. If FLAG is `off', turn off the mode."},
207 {"pager",
208 "if [ \"$pager\" = 1 ]; then pager=0; echo Internal pager is now off;"
209 "else pager=1; echo Internal pager is now on; fi\n", NULL, 0, 0, {},
210 FLAG_FALLBACK, NULL, NULL},
211 /* FIXME: partnew unsupported. */
212 {"parttype", "parttool '%s' type=%s\n", NULL, 0,
213 2, {TYPE_PARTITION, TYPE_INT}, 0,
214 "PART TYPE", "Change the type of the partition PART to TYPE."},
215 {"password", "if [ \"$superusers\" = "" ]; then superusers=legacy; fi;\n"
216 "legacy_password %s '%s'\n",
217 "menuentry \"Superuser menu\" --users \"legacy\" { configfile '%s'; }\n",
218 2, 3, {TYPE_OPTION, TYPE_VERBATIM, TYPE_FILE},
219 FLAG_IGNORE_REST | FLAG_FALLBACK_AVAILABLE | FLAG_NO_MENUENTRY,
220 "[--md5] PASSWD [FILE]",
221 "If used in the first section of a menu file, disable all"
222 " interactive editing control (menu entry editor and"
223 " command line). If the password PASSWD is entered, it loads the"
224 " FILE as a new config file and restarts the GRUB Stage 2. If you"
225 " omit the argument FILE, then GRUB just unlocks privileged"
226 " instructions. You can also use it in the script section, in"
227 " which case it will ask for the password, before continuing."
228 " The option --md5 tells GRUB that PASSWD is encrypted with"
229 " md5crypt."},
230 {"password", "if [ \"$superusers\" = "" ]; then superusers=legacy; fi;\n"
231 "legacy_password %s '%s'\n", NULL, 0, 2, {TYPE_OPTION, TYPE_VERBATIM},
232 FLAG_IGNORE_REST | FLAG_FALLBACK | FLAG_NO_MENUENTRY, NULL, NULL},
233 {"password", "if legacy_check_password %s '%s'; then configfile '%s'; "
234 "else return; fi\n", NULL, 2, 3, {TYPE_OPTION, TYPE_VERBATIM, TYPE_FILE},
235 FLAG_IGNORE_REST | FLAG_FALLBACK_AVAILABLE | FLAG_MENUENTRY_ONLY,
236 NULL, NULL},
237 {"password", "if ! legacy_check_password %s '%s'; then return fi;\n",
238 NULL, 0, 2, {TYPE_OPTION, TYPE_VERBATIM},
239 FLAG_IGNORE_REST | FLAG_FALLBACK | FLAG_MENUENTRY_ONLY, NULL, NULL},
240 /* NOTE: GRUB2 has a design principle of not eternally waiting for user
241 input. 60 seconds should be enough.
243 {"pause", "echo %s; if ! sleep -i 60; then return; fi\n", NULL, 0, 1,
244 {TYPE_REST_VERBATIM}, 0,
245 "[MESSAGE ...]", "Print MESSAGE, then wait until a key is pressed."},
246 /* FIXME: rarp unsupported. */
247 {"read", "read_dword %s\n", NULL, 0, 1, {TYPE_INT}, 0, "ADDR",
248 "Read a 32-bit value from memory at address ADDR and"
249 " display it in hex format."},
250 {"reboot", "reboot\n", NULL, 0, 0, {}, 0, 0, "Reboot your system."},
251 {"root", "set root='%s'; set legacy_hdbias='%s'\n", NULL, 0,
252 2, {TYPE_PARTITION, TYPE_INT}, FLAG_FALLBACK_AVAILABLE,
253 "[DEVICE [HDBIAS]]",
254 "Set the current \"root device\" to the device DEVICE, then"
255 " attempt to mount it to get the partition size (for passing the"
256 " partition descriptor in `ES:ESI', used by some chain-loaded"
257 " bootloaders), the BSD drive-type (for booting BSD kernels using"
258 " their native boot format), and correctly determine "
259 " the PC partition where a BSD sub-partition is located. The"
260 " optional HDBIAS parameter is a number to tell a BSD kernel"
261 " how many BIOS drive numbers are on controllers before the current"
262 " one. For example, if there is an IDE disk and a SCSI disk, and your"
263 " FreeBSD root partition is on the SCSI disk, then use a `1' for HDBIAS."},
264 {"root", "echo \"$root\"\n", NULL, 0, 0, {}, FLAG_FALLBACK, NULL, NULL},
265 {"rootnoverify", "set root='%s'; set legacy_hdbias='%s'\n", NULL, 0,
266 2, {TYPE_PARTITION, TYPE_INT}, 0,
267 "[DEVICE [HDBIAS]]",
268 "Similar to `root', but don't attempt to mount the partition. This"
269 " is useful for when an OS is outside of the area of the disk that"
270 " GRUB can read, but setting the correct root device is still"
271 " desired. Note that the items mentioned in `root' which"
272 " derived from attempting the mount will NOT work correctly."},
273 {"rootnoverify", "echo \"$root\"\n", NULL, 0,
274 0, {}, FLAG_FALLBACK, NULL, NULL},
275 /* FIXME: support saving NUM and fallback. */
276 {"savedefault", "saved_entry=${chosen}; save_env saved_entry\n", NULL, 0,
277 0, {}, 0, "[NUM | `fallback']",
278 "Save the current entry as the default boot entry if no argument is"
279 " specified. If a number is specified, this number is saved. If"
280 " `fallback' is used, next fallback entry is saved."},
281 {"serial", "serial %s\n", NULL, 0, 1, {TYPE_REST_VERBATIM}, 0,
282 "[--unit=UNIT] [--port=PORT] [--speed=SPEED] [--word=WORD] "
283 "[--parity=PARITY] [--stop=STOP] [--device=DEV]",
284 "Initialize a serial device. UNIT is a digit that specifies which serial"
285 " device is used (e.g. 0 == COM1). If you need to specify the port number,"
286 " set it by --port. SPEED is the DTE-DTE speed. WORD is the word length,"
287 " PARITY is the type of parity, which is one of `no', `odd' and `even'."
288 " STOP is the length of stop bit(s). The option --device can be used only"
289 " in the grub shell, which specifies the file name of a tty device. The"
290 " default values are COM1, 9600, 8N1."},
291 /* FIXME: setkey unsupported. */ /* NUL_TERMINATE */
292 /* NOTE: setup unsupported. */
293 /* FIXME: --no-echo, --no-edit, hercules unsupported. */
294 /* NOTE: both terminals are activated so --silent and --timeout
295 are useless. */
296 {"terminal", NULL, NULL, 0, 0, {}, FLAG_TERMINAL | FLAG_IGNORE_REST,
297 "[--dumb] [--no-echo] [--no-edit] [--timeout=SECS] [--lines=LINES] "
298 "[--silent] [console] [serial] [hercules]",
299 "Select a terminal. When multiple terminals are specified, wait until"
300 " you push any key to continue. If both console and serial are specified,"
301 " the terminal to which you input a key first will be selected. If no"
302 " argument is specified, print current setting. The option --dumb"
303 " specifies that your terminal is dumb, otherwise, vt100-compatibility"
304 " is assumed. If you specify --no-echo, input characters won't be echoed."
305 " If you specify --no-edit, the BASH-like editing feature will be disabled."
306 " If --timeout is present, this command will wait at most for SECS"
307 " seconds. The option --lines specifies the maximum number of lines."
308 " The option --silent is used to suppress messages."},
309 /* FIXME: terminfo unsupported. */ /* NUL_TERMINATE */
310 {"testload", "cat '%s'\n", NULL, 0, 1, {TYPE_FILE}, 0, "FILE",
311 "Read the entire contents of FILE in several different ways and"
312 " compares them, to test the filesystem code. "
313 " If this test succeeds, then a good next"
314 " step is to try loading a kernel."},
315 {"testvbe", "insmod vbe; videotest '%s'\n", NULL, 0, 1, {TYPE_VBE_MODE}, 0,
316 "MODE", "Test the VBE mode MODE. Hit any key to return."},
317 /* FIXME: tftpserver unsupported. */
318 {"timeout", "set timeout=%s\n", NULL, 0, 1, {TYPE_INT}, 0, "SEC",
319 "Set a timeout, in SEC seconds, before automatically booting the"
320 " default entry (normally the first entry defined)."},
321 {"title", NULL, NULL, 0, 0, {}, FLAG_TITLE, "NAME ...",
322 "Start a new boot entry, and set its name to the contents of the"
323 " rest of the line, starting with the first non-space character."},
324 {"unhide", "parttool '%s' hidden-\n", NULL, 0,
325 1, {TYPE_PARTITION}, 0, "PARTITION",
326 "Unhide PARTITION by clearing the \"hidden\" bit in its"
327 " partition type code."},
328 /* FIXME: uppermem unsupported. */
329 {"uuid", "search --set=root --fs-uuid '%s'\n", NULL, 0, 1, {TYPE_VERBATIM},
330 0, "UUID", "Find root by UUID"},
331 {"vbeprobe", "insmod vbe; videoinfo '%s'\n", NULL, 0, 1, {TYPE_VBE_MODE},
332 FLAG_FALLBACK_AVAILABLE, "[MODE]",
333 "Probe VBE information. If the mode number MODE is specified, show only"
334 " the information about only the mode."},
335 {"vbeprobe", "insmod vbe; videoinfo\n", NULL, 0, 0, {},
336 FLAG_FALLBACK, NULL, NULL}
339 char *
340 grub_legacy_escape (const char *in, grub_size_t len)
342 char *ptr;
343 char *ret;
344 char saved;
345 int overhead = 0;
347 for (ptr = (char*)in; ptr < in + len && *ptr; ptr++)
348 if (*ptr == '\'')
349 overhead += 3;
350 ret = grub_malloc (ptr - in + overhead + 1);
351 if (!ret)
352 return NULL;
354 ptr = (char*)in;
355 saved = ptr[len];
356 ptr[len] = '\0';
357 grub_strchrsub (ret, ptr, '\'', "'\\''");
358 ptr[len] = saved;
359 return ret;
362 static char *
363 adjust_file (const char *in, grub_size_t len)
365 const char *comma, *ptr, *rest;
366 char *ret, *outptr;
367 int overhead = 0;
368 int part = -1, subpart = -1;
369 if (in[0] != '(')
370 return grub_legacy_escape (in, len);
371 for (ptr = in + 1; ptr < in + len && *ptr && *ptr != ')'
372 && *ptr != ','; ptr++)
373 if (*ptr == '\'' || *ptr == '\\')
374 overhead++;
375 comma = ptr;
376 if (*comma != ',')
377 return grub_legacy_escape (in, len);
378 part = grub_strtoull (comma + 1, (char **) &rest, 0);
379 if (rest[0] == ',' && rest[1] >= 'a' && rest[1] <= 'z')
381 subpart = rest[1] - 'a';
382 rest += 2;
384 for (ptr = rest; ptr < in + len && *ptr; ptr++)
385 if (*ptr == '\'' || *ptr == '\\')
386 overhead++;
388 /* 35 is enough for any 2 numbers. */
389 ret = grub_malloc (ptr - in + overhead + 35);
390 if (!ret)
391 return NULL;
393 outptr = ret;
394 for (ptr = in; ptr < in + len && ptr <= comma; ptr++)
396 if (*ptr == '\'' || *ptr == '\\')
397 *outptr++ = '\\';
399 *outptr++ = *ptr;
401 if (subpart != -1)
402 grub_snprintf (outptr, 35, "%d,%d", part + 1, subpart + 1);
403 else
404 grub_snprintf (outptr, 35, "%d", part + 1);
405 while (*outptr)
406 outptr++;
407 for (ptr = rest; ptr < in + len; ptr++)
409 if (*ptr == '\'' || *ptr == '\\')
410 *outptr++ = '\\';
412 *outptr++ = *ptr;
414 *outptr = 0;
415 return ret;
418 static int
419 check_option (const char *a, const char *b, grub_size_t len)
421 if (grub_strlen (b) != len)
422 return 0;
423 return grub_strncmp (a, b, len) == 0;
426 static int
427 is_option (enum arg_type opt, const char *curarg, grub_size_t len)
429 switch (opt)
431 case TYPE_WITH_CONFIGFILE_OPTION:
432 return check_option (curarg, "--with-configfile", len);
433 case TYPE_NOAPM_OPTION:
434 return check_option (curarg, "--no-apm", len);
435 case TYPE_FORCE_OPTION:
436 return check_option (curarg, "--force", len);
437 case TYPE_TYPE_OR_NOMEM_OPTION:
438 return check_option (curarg, "--type=netbsd", len)
439 || check_option (curarg, "--type=freebsd", len)
440 || check_option (curarg, "--type=openbsd", len)
441 || check_option (curarg, "--type=linux", len)
442 || check_option (curarg, "--type=biglinux", len)
443 || check_option (curarg, "--type=multiboot", len)
444 || check_option (curarg, "--no-mem-option", len);
445 case TYPE_OPTION:
446 return (len >= 2 && curarg[0] == '-' && curarg[1] == '-');
447 default:
448 return 0;
452 char *
453 grub_legacy_parse (const char *buf, char **entryname, char **suffix)
455 const char *ptr;
456 const char *cmdname;
457 unsigned i, cmdnum;
458 char *args[ARRAY_SIZE (legacy_commands[0].argt)];
460 *suffix = NULL;
462 for (ptr = buf; *ptr && grub_isspace (*ptr); ptr++);
463 if (!*ptr || *ptr == '#')
465 char *ret;
466 int len = grub_strlen (buf);
467 ret = grub_malloc (len + 2);
468 grub_memcpy (ret, buf, len);
469 if (len && ret[len - 1] == '\n')
470 ret[len] = 0;
471 else
473 ret[len] = '\n';
474 ret[len + 1] = 0;
476 return ret;
479 cmdname = ptr;
480 for (ptr = buf; *ptr && !grub_isspace (*ptr) && *ptr != '='; ptr++);
482 for (cmdnum = 0; cmdnum < ARRAY_SIZE (legacy_commands); cmdnum++)
483 if (grub_strncmp (legacy_commands[cmdnum].name, cmdname, ptr - cmdname) == 0
484 && legacy_commands[cmdnum].name[ptr - cmdname] == 0
485 && (!(*entryname != NULL && (legacy_commands[cmdnum].flags
486 & FLAG_NO_MENUENTRY)))
487 && (!(*entryname == NULL && (legacy_commands[cmdnum].flags
488 & FLAG_MENUENTRY_ONLY))))
489 break;
490 if (cmdnum == ARRAY_SIZE (legacy_commands))
491 return grub_xasprintf ("# Unsupported legacy command: %s\n", buf);
493 for (; grub_isspace (*ptr) || *ptr == '='; ptr++);
495 if (legacy_commands[cmdnum].flags & FLAG_TITLE)
497 const char *ptr2;
498 ptr2 = ptr + grub_strlen (ptr);
499 while (ptr2 > ptr && grub_isspace (*(ptr2 - 1)))
500 ptr2--;
501 *entryname = grub_strndup (ptr, ptr2 - ptr);
502 return NULL;
505 if (legacy_commands[cmdnum].flags & FLAG_TERMINAL)
507 int dumb = 0, lines = 24;
508 #ifdef TODO
509 int no_echo = 0, no_edit = 0;
510 int hercules = 0;
511 #endif
512 int console = 0, serial = 0;
513 /* Big enough for any possible resulting command. */
514 char outbuf[256] = "";
515 char *outptr;
516 while (*ptr)
518 /* "[--timeout=SECS] [--silent]"
519 " [console] [serial] [hercules]"*/
520 if (grub_memcmp (ptr, "--dumb", sizeof ("--dumb") - 1) == 0)
521 dumb = 1;
522 #ifdef TODO
523 if (grub_memcmp (ptr, "--no-echo", sizeof ("--no-echo") - 1) == 0)
524 no_echo = 1;
526 if (grub_memcmp (ptr, "--no-edit", sizeof ("--no-edit") - 1) == 0)
527 no_edit = 1;
528 #endif
529 if (grub_memcmp (ptr, "--lines=", sizeof ("--lines=") - 1) == 0)
531 lines = grub_strtoul (ptr + sizeof ("--lines=") - 1, 0, 0);
532 if (grub_errno)
534 lines = 24;
535 grub_errno = GRUB_ERR_NONE;
539 if (grub_memcmp (ptr, "console", sizeof ("console") - 1) == 0)
540 console = 1;
542 if (grub_memcmp (ptr, "serial", sizeof ("serial") - 1) == 0)
543 serial = 1;
544 #ifdef TODO
545 if (grub_memcmp (ptr, "hercules", sizeof ("hercules") - 1) == 0)
546 hercules = 1;
547 #endif
548 while (*ptr && !grub_isspace (*ptr))
549 ptr++;
550 while (*ptr && grub_isspace (*ptr))
551 ptr++;
554 if (!console && !serial)
555 return grub_strdup ("terminal_input; terminal_output; terminfo\n");
557 grub_strcpy (outbuf, "terminal_input ");
558 outptr = outbuf + grub_strlen (outbuf);
559 if (serial)
561 grub_strcpy (outptr, "serial ");
562 outptr += grub_strlen (outptr);
564 if (console)
566 grub_strcpy (outptr, "console ");
567 outptr += grub_strlen (outptr);
569 grub_strcpy (outptr, "; terminal_output ");
570 outptr += grub_strlen (outptr);
571 if (serial)
573 grub_strcpy (outptr, "serial ");
574 outptr += grub_strlen (outptr);
576 if (console)
578 grub_strcpy (outptr, "console ");
579 outptr += grub_strlen (outptr);
581 grub_strcpy (outptr, "; ");
582 outptr += grub_strlen (outptr);
583 if (serial)
585 grub_snprintf (outptr, outbuf + sizeof (outbuf) - outptr,
586 "terminfo serial -g 80x%d %s; ",
587 lines, dumb ? "dumb" : "vt100");
588 outptr += grub_strlen (outptr);
591 grub_strcpy (outptr, "\n");
593 return grub_strdup (outbuf);
596 grub_memset (args, 0, sizeof (args));
599 int hold_arg = 0;
600 const char *curarg = NULL;
601 for (i = 0; i < legacy_commands[cmdnum].argc; i++)
603 grub_size_t curarglen;
604 if (hold_arg)
606 ptr = curarg;
607 hold_arg = 0;
609 for (; grub_isspace (*ptr); ptr++);
610 curarg = ptr;
611 if (!*curarg)
612 break;
613 for (; *ptr && !grub_isspace (*ptr); ptr++);
614 if (i != legacy_commands[cmdnum].argc - 1
615 || (legacy_commands[cmdnum].flags & FLAG_IGNORE_REST))
616 curarglen = ptr - curarg;
617 else
619 curarglen = grub_strlen (curarg);
620 while (curarglen > 0 && grub_isspace (curarg[curarglen - 1]))
621 curarglen--;
623 if (*ptr)
624 ptr++;
625 switch (legacy_commands[cmdnum].argt[i])
627 case TYPE_FILE_NO_CONSUME:
628 hold_arg = 1;
629 case TYPE_PARTITION:
630 case TYPE_FILE:
631 args[i] = adjust_file (curarg, curarglen);
632 break;
634 case TYPE_REST_VERBATIM:
636 char *outptr, *outptr0;
637 int overhead = 3;
638 ptr = curarg;
639 while (*ptr)
641 for (; *ptr && grub_isspace (*ptr); ptr++);
642 for (; *ptr && !grub_isspace (*ptr); ptr++)
643 if (*ptr == '\'')
644 overhead += 3;
645 if (*ptr)
646 ptr++;
647 overhead += 3;
650 outptr0 = args[i] = grub_malloc (overhead + (ptr - curarg));
651 if (!outptr0)
652 return NULL;
653 ptr = curarg;
654 outptr = outptr0;
655 while (*ptr)
657 for (; *ptr && grub_isspace (*ptr); ptr++);
658 if (outptr != outptr0)
659 *outptr++ = ' ';
660 *outptr++ = '\'';
661 for (; *ptr && !grub_isspace (*ptr); ptr++)
663 if (*ptr == '\'')
665 *outptr++ = '\'';
666 *outptr++ = '\\';
667 *outptr++ = '\'';
668 *outptr++ = '\'';
670 else
671 *outptr++ = *ptr;
673 *outptr++ = '\'';
674 if (*ptr)
675 ptr++;
677 *outptr++ = 0;
679 break;
681 case TYPE_VERBATIM:
682 args[i] = grub_legacy_escape (curarg, curarglen);
683 break;
684 case TYPE_WITH_CONFIGFILE_OPTION:
685 case TYPE_FORCE_OPTION:
686 case TYPE_NOAPM_OPTION:
687 case TYPE_TYPE_OR_NOMEM_OPTION:
688 case TYPE_OPTION:
689 if (is_option (legacy_commands[cmdnum].argt[i], curarg, curarglen))
691 args[i] = grub_strndup (curarg, curarglen);
692 break;
694 args[i] = grub_strdup ("");
695 hold_arg = 1;
696 break;
697 case TYPE_INT:
699 const char *brk;
700 int base = 10;
701 brk = curarg;
702 if (brk[0] == '0' && brk[1] == 'x')
704 base = 16;
705 brk += 2;
707 else if (brk[0] == '0')
708 base = 8;
709 for (; *brk && brk < curarg + curarglen; brk++)
711 if (base == 8 && (*brk == '8' || *brk == '9'))
712 break;
713 if (grub_isdigit (*brk))
714 continue;
715 if (base != 16)
716 break;
717 if (!(*brk >= 'a' && *brk <= 'f')
718 && !(*brk >= 'A' && *brk <= 'F'))
719 break;
721 if (brk == curarg)
722 args[i] = grub_strdup ("0");
723 else
724 args[i] = grub_strndup (curarg, brk - curarg);
726 break;
727 case TYPE_VBE_MODE:
729 unsigned mod;
730 struct grub_vesa_mode_table_entry *modedesc;
732 mod = grub_strtoul (curarg, 0, 0);
733 if (grub_errno)
735 mod = 0;
736 grub_errno = GRUB_ERR_NONE;
738 if (mod < GRUB_VESA_MODE_TABLE_START
739 || mod > GRUB_VESA_MODE_TABLE_END)
741 args[i] = grub_strdup ("auto");
742 break;
744 modedesc = &grub_vesa_mode_table[mod - GRUB_VESA_MODE_TABLE_START];
745 if (!modedesc->width)
747 args[i] = grub_strdup ("auto");
748 break;
750 args[i] = grub_xasprintf ("%ux%ux%u",
751 modedesc->width, modedesc->height,
752 modedesc->depth);
753 break;
755 case TYPE_BOOL:
756 if (curarglen == 2 && curarg[0] == 'o' && curarg[1] == 'n')
757 args[i] = grub_strdup ("1");
758 else
759 args[i] = grub_strdup ("0");
760 break;
765 while (legacy_commands[cmdnum].argc > 0
766 && args[legacy_commands[cmdnum].argc - 1] == NULL
767 && (legacy_commands[cmdnum].flags & FLAG_FALLBACK_AVAILABLE)
768 && args[legacy_commands[cmdnum + 1].argc] == NULL)
769 cmdnum++;
771 for (; i < legacy_commands[cmdnum].argc; i++)
772 switch (legacy_commands[cmdnum].argt[i])
774 case TYPE_FILE_NO_CONSUME:
775 case TYPE_PARTITION:
776 case TYPE_FILE:
777 case TYPE_REST_VERBATIM:
778 case TYPE_VERBATIM:
779 case TYPE_WITH_CONFIGFILE_OPTION:
780 case TYPE_FORCE_OPTION:
781 case TYPE_NOAPM_OPTION:
782 case TYPE_TYPE_OR_NOMEM_OPTION:
783 case TYPE_OPTION:
784 args[i] = grub_strdup ("");
785 break;
786 case TYPE_BOOL:
787 case TYPE_INT:
788 args[i] = grub_strdup ("0");
789 break;
790 case TYPE_VBE_MODE:
791 args[i] = grub_strdup ("auto");
792 break;
795 if (legacy_commands[cmdnum].flags & FLAG_COLOR_INVERT)
797 char *corig = args[legacy_commands[cmdnum].argc - 1];
798 char *slash = grub_strchr (corig, '/');
799 char *invert;
800 grub_size_t len;
802 len = grub_strlen (corig);
803 if (!slash)
805 grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid color specification `%s'"),
806 args[0]);
807 return NULL;
809 invert = grub_malloc (len + 1);
810 if (!invert)
811 return NULL;
812 grub_memcpy (invert, slash + 1, len - (slash - corig) - 1);
813 invert[len - (slash - args[0]) - 1] = '/';
814 grub_memcpy (invert + len - (slash - corig), corig, slash - corig);
815 invert[len] = 0;
816 args[legacy_commands[cmdnum].argc] = invert;
819 if (legacy_commands[cmdnum].suffix)
821 *suffix = grub_xasprintf (legacy_commands[cmdnum].suffix,
822 args[legacy_commands[cmdnum].suffixarg]);
823 if (*suffix)
824 return NULL;
828 char *ret = grub_xasprintf (legacy_commands[cmdnum].map, args[0], args[1],
829 args[2], args[3]);
830 grub_free (args[0]);
831 grub_free (args[1]);
832 grub_free (args[2]);
833 grub_free (args[3]);
834 return ret;