2005-10-24 Marco Gerards <mgerards@xs4all.nl>
* include/grub/parser.h: New file. * kern/parser.c: Likewise. * conf/i386-pc.rmk (kernel_img_SOURCES): Add `kern/parser.c'. (grub_setup_SOURCES): Likewise. (grub_probefs_SOURCES): Likewise. (grub_emu_SOURCES): Likewise. (kernel_img_HEADERS): Add `parser.h'. * conf/powerpc-ieee1275.rmk (grubof_HEADERS): Add `parser.h'. (grub_emu_SOURCES): Add `kern/parser.c'. (grubof_SOURCES): Likewise. * conf/sparc64-ieee1275.rmk (grubof_HEADERS): Add `parser.h'. (grubof_SOURCES): Add `kern/parser.c'. * include/grub/misc.h (grub_split_cmdline): Removed prototype. * kern/misc.c (grub_split_cmdline): Removed function. * kern/rescue.c: Include <grub/parser.h>. (grub_enter_rescue_mode): Use `grub_parser_split_cmdline' instead of `grub_split_cmdline'. * normal/command.c: Include <grub/parser.h>. (grub_command_execute): Use `grub_parser_split_cmdline' instead of `grub_split_cmdline'. * normal/completion.c: Include <grub/parser.h>. (cmdline_state): New variable. (iterate_dir): End the filename with a quote depending on the command line state. (get_state): new function. (grub_normal_do_completion): Use `grub_parser_split_cmdline' to split the arguments and determine the current argument. When the argument string is not quoted, escape all spaces.
This commit is contained in:
parent
6d8f4b0e60
commit
04ccf3ec6f
14 changed files with 531 additions and 324 deletions
224
kern/misc.c
224
kern/misc.c
|
@ -1,7 +1,7 @@
|
|||
/* misc.c - definitions of misc functions */
|
||||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc.
|
||||
* Copyright (C) 1999,2000,2001,2002,2003,2004,2005 Free Software Foundation, Inc.
|
||||
*
|
||||
* GRUB is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
|
@ -874,225 +874,3 @@ grub_utf8_to_ucs4 (grub_uint32_t *dest, const grub_uint8_t *src,
|
|||
|
||||
return p - dest;
|
||||
}
|
||||
|
||||
grub_err_t
|
||||
grub_split_cmdline (const char *cmdline, grub_err_t (*getline) (char **),
|
||||
int *argc, char ***argv)
|
||||
{
|
||||
/* XXX: Fixed size buffer, perhaps this buffer should be dynamically
|
||||
allocated. */
|
||||
char buffer[1024];
|
||||
char *bp = buffer;
|
||||
char *rd = (char *) cmdline;
|
||||
char unputbuf;
|
||||
int unput = 0;
|
||||
char *args;
|
||||
int i;
|
||||
|
||||
auto char getchar (void);
|
||||
auto void unputc (char c);
|
||||
auto void getenvvar (void);
|
||||
auto int getarg (void);
|
||||
|
||||
/* Get one character from the commandline. If the caller reads
|
||||
beyond the end of the string a new line will be read. This
|
||||
function will not chech for errors, the caller has to check for
|
||||
grub_errno. */
|
||||
char getchar (void)
|
||||
{
|
||||
int c;
|
||||
if (unput)
|
||||
{
|
||||
unput = 0;
|
||||
return unputbuf;
|
||||
}
|
||||
|
||||
if (! rd)
|
||||
{
|
||||
getline (&rd);
|
||||
/* Error is ignored here, the caller will check for this
|
||||
when it reads beyond the EOL. */
|
||||
c = *(rd)++;
|
||||
return c;
|
||||
}
|
||||
|
||||
c = *(rd)++;
|
||||
if (! c)
|
||||
{
|
||||
rd = 0;
|
||||
return '\n';
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
void unputc (char c)
|
||||
{
|
||||
unputbuf = c;
|
||||
unput = 1;
|
||||
}
|
||||
|
||||
/* Read a variable name from the commandline and insert its content
|
||||
into the buffer. */
|
||||
void getenvvar (void)
|
||||
{
|
||||
char varname[100];
|
||||
char *p = varname;
|
||||
char *val;
|
||||
char c;
|
||||
|
||||
c = getchar ();
|
||||
if (c == '{')
|
||||
while ((c = getchar ()) != '}')
|
||||
*(p++) = c;
|
||||
else
|
||||
{
|
||||
/* XXX: An env. variable can have characters and digits in
|
||||
its name, are more characters allowed here? */
|
||||
while (c && (grub_isalpha (c) || grub_isdigit (c)))
|
||||
{
|
||||
*(p++) = c;
|
||||
c = getchar ();
|
||||
}
|
||||
unputc (c);
|
||||
}
|
||||
*p = '\0';
|
||||
|
||||
/* The variable does not exist. */
|
||||
val = grub_env_get (varname);
|
||||
if (! val)
|
||||
return;
|
||||
|
||||
/* Copy the contents of the variable into the buffer. */
|
||||
for (p = val; *p; p++)
|
||||
*(bp++) = *p;
|
||||
}
|
||||
|
||||
/* Read one argument. Return 1 if no variables can be read anymore,
|
||||
otherwise return 0. If there is an error, return 1, the caller
|
||||
has to check grub_errno. */
|
||||
int getarg (void)
|
||||
{
|
||||
char c;
|
||||
|
||||
/* Skip all whitespaces before an argument. */
|
||||
do {
|
||||
c = getchar ();
|
||||
} while (c == ' ' || c == '\t');
|
||||
|
||||
do {
|
||||
switch (c)
|
||||
{
|
||||
case '"':
|
||||
/* Double quote. */
|
||||
while ((c = getchar ()))
|
||||
{
|
||||
if (grub_errno)
|
||||
return 1;
|
||||
/* Read in an escaped character. */
|
||||
if (c == '\\')
|
||||
{
|
||||
c = getchar ();
|
||||
*(bp++) = c;
|
||||
continue;
|
||||
}
|
||||
else if (c == '"')
|
||||
break;
|
||||
/* Read a variable. */
|
||||
if (c == '$')
|
||||
{
|
||||
getenvvar ();
|
||||
continue;
|
||||
}
|
||||
*(bp++) = c;
|
||||
}
|
||||
break;
|
||||
|
||||
case '\'':
|
||||
/* Single quote. */
|
||||
while ((c = getchar ()) != '\'')
|
||||
{
|
||||
if (grub_errno)
|
||||
return 1;
|
||||
|
||||
*(bp++) = c;
|
||||
}
|
||||
break;
|
||||
|
||||
case '\n':
|
||||
/* This was not a argument afterall. */
|
||||
return 1;
|
||||
|
||||
default:
|
||||
/* A normal option. */
|
||||
while (c && (grub_isalpha (c)
|
||||
|| grub_isdigit (c) || grub_isgraph (c)))
|
||||
{
|
||||
/* Read in an escaped character. */
|
||||
if (c == '\\')
|
||||
{
|
||||
c = getchar ();
|
||||
*(bp++) = c;
|
||||
c = getchar ();
|
||||
continue;
|
||||
}
|
||||
/* Read a variable. */
|
||||
if (c == '$')
|
||||
{
|
||||
getenvvar ();
|
||||
c = getchar ();
|
||||
continue;
|
||||
}
|
||||
*(bp++) = c;
|
||||
c = getchar ();
|
||||
}
|
||||
unputc (c);
|
||||
|
||||
break;
|
||||
}
|
||||
} while (! grub_isspace (c) && c != '\'' && c != '"');
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Read in all arguments and count them. */
|
||||
*argc = 0;
|
||||
while (1)
|
||||
{
|
||||
if (getarg ())
|
||||
break;
|
||||
*(bp++) = '\0';
|
||||
(*argc)++;
|
||||
}
|
||||
|
||||
/* Check if there were no errors. */
|
||||
if (grub_errno)
|
||||
return grub_errno;
|
||||
|
||||
/* Reserve memory for the return values. */
|
||||
args = grub_malloc (bp - buffer);
|
||||
if (! args)
|
||||
return grub_errno;
|
||||
grub_memcpy (args, buffer, bp - buffer);
|
||||
|
||||
*argv = grub_malloc (sizeof (char *) * (*argc + 1));
|
||||
if (! *argv)
|
||||
{
|
||||
grub_free (args);
|
||||
return grub_errno;
|
||||
}
|
||||
|
||||
/* The arguments are separated with 0's, setup argv so it points to
|
||||
the right values. */
|
||||
bp = args;
|
||||
for (i = 0; i < *argc; i++)
|
||||
{
|
||||
(*argv)[i] = bp;
|
||||
while (*bp)
|
||||
bp++;
|
||||
bp++;
|
||||
}
|
||||
|
||||
(*argc)--;
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue