pull-in func-params branch

This commit is contained in:
BVK Chaitanya 2010-05-21 16:15:09 +05:30
commit 486411dd54
2 changed files with 79 additions and 69 deletions

View file

@ -86,28 +86,18 @@ grub_script_argv_next (struct grub_script_argv *argv)
{ {
char **p = argv->args; char **p = argv->args;
if (argv->argc == 0) if (argv->args && argv->args[argv->argc - 1] == 0)
{
p = grub_malloc (2 * sizeof (char *));
if (! p)
return 1;
argv->argc = 1;
argv->args = p;
argv->args[0] = 0;
argv->args[1] = 0;
return 0;
}
if (! argv->args[argv->argc - 1])
return 0; return 0;
p = grub_realloc (p, round_up_exp ((argv->argc + 1) * sizeof (char *))); p = grub_realloc (p, round_up_exp ((argv->argc + 2) * sizeof (char *)));
if (! p) if (! p)
return 1; return 1;
argv->argc++; argv->argc++;
argv->args = p; argv->args = p;
if (argv->argc == 1)
argv->args[0] = 0;
argv->args[argv->argc] = 0; argv->args[argv->argc] = 0;
return 0; return 0;
} }

View file

@ -51,65 +51,77 @@ grub_env_special (const char *name)
static char ** static char **
grub_script_env_get (const char *name, grub_script_arg_type_t type) grub_script_env_get (const char *name, grub_script_arg_type_t type)
{ {
int errors = 0;
struct grub_script_argv result = { 0, 0 }; struct grub_script_argv result = { 0, 0 };
errors += grub_script_argv_next (&result); if (grub_script_argv_next (&result))
goto fail;
if (! grub_env_special (name)) if (! grub_env_special (name))
{ {
char *v = grub_env_get (name); char *v = grub_env_get (name);
if (v && v[0]) if (v && v[0])
{ {
if (type == GRUB_SCRIPT_ARG_TYPE_VAR) if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
errors += grub_script_argv_split_append (&result, v); {
if (grub_script_argv_split_append (&result, v))
goto fail;
}
else else
errors += grub_script_argv_append (&result, v); if (grub_script_argv_append (&result, v))
goto fail;
} }
} }
else if (! scope) else if (! scope)
errors += grub_script_argv_append (&result, 0); {
if (grub_script_argv_append (&result, 0))
goto fail;
}
else if (grub_strcmp (name, "#") == 0) else if (grub_strcmp (name, "#") == 0)
{ {
char buffer[ERRNO_DIGITS_MAX + 1]; char buffer[ERRNO_DIGITS_MAX + 1];
grub_snprintf (buffer, sizeof (buffer), "%u", scope->argv.argc); grub_snprintf (buffer, sizeof (buffer), "%u", scope->argv.argc);
errors += grub_script_argv_append (&result, buffer); if (grub_script_argv_append (&result, buffer))
goto fail;
} }
else if (grub_strcmp (name, "*") == 0) else if (grub_strcmp (name, "*") == 0)
{ {
unsigned i; unsigned i;
for (i = 0; ! errors && i < scope->argv.argc; i++) for (i = 0; i < scope->argv.argc; i++)
if (type == GRUB_SCRIPT_ARG_TYPE_VAR) if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
{ {
if (i != 0) if (i != 0 && grub_script_argv_next (&result))
errors += grub_script_argv_next (&result); goto fail;
errors += grub_script_argv_split_append (&result,
scope->argv.args[i]); if (grub_script_argv_split_append (&result, scope->argv.args[i]))
goto fail;
} }
else else
{ {
if (i != 0) if (i != 0 && grub_script_argv_append (&result, " "))
errors += grub_script_argv_append (&result, " "); goto fail;
errors += grub_script_argv_append (&result,
scope->argv.args[i]); if (grub_script_argv_append (&result, scope->argv.args[i]))
goto fail;
} }
} }
else if (grub_strcmp (name, "@") == 0) else if (grub_strcmp (name, "@") == 0)
{ {
unsigned i; unsigned i;
for (i = 0; ! errors && i < scope->argv.argc; i++) for (i = 0; i < scope->argv.argc; i++)
{ {
if (i != 0) if (i != 0 && grub_script_argv_next (&result))
errors += grub_script_argv_next (&result); goto fail;
if (type == GRUB_SCRIPT_ARG_TYPE_VAR) if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
errors += grub_script_argv_split_append (&result, {
scope->argv.args[i]); if (grub_script_argv_split_append (&result, scope->argv.args[i]))
goto fail;
}
else else
errors += grub_script_argv_append (&result, if (grub_script_argv_append (&result, scope->argv.args[i]))
scope->argv.args[i]); goto fail;
} }
} }
else else
@ -121,21 +133,23 @@ grub_script_env_get (const char *name, grub_script_arg_type_t type)
else if (num <= scope->argv.argc) else if (num <= scope->argv.argc)
{ {
if (type == GRUB_SCRIPT_ARG_TYPE_VAR) if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
errors += grub_script_argv_split_append (&result,
scope->argv.args[num - 1]);
else
errors += grub_script_argv_append (&result,
scope->argv.args[num - 1]);
}
}
if (errors)
{ {
grub_script_argv_free (&result); if (grub_script_argv_split_append (&result,
return 0; scope->argv.args[num - 1]))
goto fail;
}
else
if (grub_script_argv_append (&result, scope->argv.args[num - 1]))
goto fail;
}
} }
return result.args; return result.args;
fail:
grub_script_argv_free (&result);
return 0;
} }
static grub_err_t static grub_err_t
@ -153,21 +167,18 @@ grub_script_arglist_to_argv (struct grub_script_arglist *arglist,
struct grub_script_argv *argv) struct grub_script_argv *argv)
{ {
int i; int i;
int error = 0;
char **values = 0; char **values = 0;
struct grub_script_arg *arg = 0; struct grub_script_arg *arg = 0;
struct grub_script_argv result = { 0, 0 }; struct grub_script_argv result = { 0, 0 };
for (; error == 0 && arglist && arglist->arg; arglist = arglist->next) for (; arglist && arglist->arg; arglist = arglist->next)
{ {
error += grub_script_argv_next (&result); if (grub_script_argv_next (&result))
goto fail;
arg = arglist->arg; arg = arglist->arg;
while (arg) while (arg)
{ {
if (error)
break;
switch (arg->type) switch (arg->type)
{ {
case GRUB_SCRIPT_ARG_TYPE_VAR: case GRUB_SCRIPT_ARG_TYPE_VAR:
@ -175,13 +186,19 @@ grub_script_arglist_to_argv (struct grub_script_arglist *arglist,
values = grub_script_env_get (arg->str, arg->type); values = grub_script_env_get (arg->str, arg->type);
for (i = 0; values && values[i]; i++) for (i = 0; values && values[i]; i++)
{ {
if (i != 0) if (i != 0 && grub_script_argv_next (&result))
error += grub_script_argv_next (&result); goto fail;
if (arg->type == GRUB_SCRIPT_ARG_TYPE_VAR) if (arg->type == GRUB_SCRIPT_ARG_TYPE_VAR)
error += grub_script_argv_append (&result, values[i]); {
if (grub_script_argv_append (&result, values[i]))
goto fail;
}
else else
error += grub_script_argv_append_escaped (&result, values[i]); {
if (grub_script_argv_append_escaped (&result, values[i]))
goto fail;
}
grub_free (values[i]); grub_free (values[i]);
} }
@ -189,13 +206,15 @@ grub_script_arglist_to_argv (struct grub_script_arglist *arglist,
break; break;
case GRUB_SCRIPT_ARG_TYPE_TEXT: case GRUB_SCRIPT_ARG_TYPE_TEXT:
if (grub_strlen (arg->str)) if (grub_strlen (arg->str) &&
error += grub_script_argv_append (&result, arg->str); grub_script_argv_append (&result, arg->str))
goto fail;
break; break;
case GRUB_SCRIPT_ARG_TYPE_DQSTR: case GRUB_SCRIPT_ARG_TYPE_DQSTR:
case GRUB_SCRIPT_ARG_TYPE_SQSTR: case GRUB_SCRIPT_ARG_TYPE_SQSTR:
error += grub_script_argv_append_escaped (&result, arg->str); if (grub_script_argv_append (&result, arg->str))
goto fail;
break; break;
} }
arg = arg->next; arg = arg->next;
@ -205,15 +224,16 @@ grub_script_arglist_to_argv (struct grub_script_arglist *arglist,
if (! result.args[result.argc - 1]) if (! result.args[result.argc - 1])
result.argc--; result.argc--;
error += grub_script_argv_expand (&result); if (grub_script_argv_expand (&result))
if (error) goto fail;
{
grub_script_argv_free (&result);
return 1;
}
*argv = result; *argv = result;
return 0; return 0;
fail:
grub_script_argv_free (&result);
return 1;
} }
static grub_err_t static grub_err_t
@ -366,7 +386,7 @@ grub_script_execute_cmdfor (struct grub_script_cmd *cmd)
{ {
unsigned i; unsigned i;
grub_err_t result; grub_err_t result;
struct grub_script_argv argv; struct grub_script_argv argv = { 0, 0 };
struct grub_script_cmdfor *cmdfor = (struct grub_script_cmdfor *) cmd; struct grub_script_cmdfor *cmdfor = (struct grub_script_cmdfor *) cmd;
if (grub_script_arglist_to_argv (cmdfor->words, &argv)) if (grub_script_arglist_to_argv (cmdfor->words, &argv))