replace error handling with goto

This commit is contained in:
BVK Chaitanya 2010-05-21 15:43:24 +05:30
parent f532a574a6
commit 1702fbc15d

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]); if (grub_script_argv_split_append (&result,
scope->argv.args[num - 1]))
goto fail;
}
else else
errors += grub_script_argv_append (&result, if (grub_script_argv_append (&result, scope->argv.args[num - 1]))
scope->argv.args[num - 1]); goto fail;
} }
} }
if (errors)
{
grub_script_argv_free (&result);
return 0;
}
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,35 +186,41 @@ 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;
error += grub_script_argv_append (&result, values[i]);
if (grub_script_argv_append (&result, values[i]))
goto fail;
} }
grub_free (values); grub_free (values);
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 (&result, arg->str); if (grub_script_argv_append (&result, arg->str))
goto fail;
break; break;
} }
arg = arg->next; arg = arg->next;
} }
} }
if (error)
return 1;
if (! result.args[result.argc - 1]) if (! result.args[result.argc - 1])
result.argc--; result.argc--;
*argv = result; *argv = result;
return 0; return 0;
fail:
grub_script_argv_free (&result);
return 1;
} }
static grub_err_t static grub_err_t
@ -356,7 +373,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))
@ -398,7 +415,7 @@ grub_err_t
grub_script_execute_menuentry (struct grub_script_cmd *cmd) grub_script_execute_menuentry (struct grub_script_cmd *cmd)
{ {
struct grub_script_cmd_menuentry *cmd_menuentry; struct grub_script_cmd_menuentry *cmd_menuentry;
struct grub_script_argv argv = {0, 0}; struct grub_script_argv argv = { 0, 0 };
cmd_menuentry = (struct grub_script_cmd_menuentry *) cmd; cmd_menuentry = (struct grub_script_cmd_menuentry *) cmd;