Fix test -a and -o precedence.

Reported by: adrian15.
This commit is contained in:
Vladimir 'phcoder' Serbinenko 2013-05-04 13:38:59 +02:00
parent 82ae74d7bd
commit daf01d61ff
4 changed files with 40 additions and 16 deletions

View file

@ -1,3 +1,8 @@
2013-05-04 Vladimir Serbinenko <phcoder@gmail.com>
Fix test -a and -o precedence.
Reported by: adrian15.
2013-05-04 Vladimir Serbinenko <phcoder@gmail.com> 2013-05-04 Vladimir Serbinenko <phcoder@gmail.com>
* grub-core/font/font.c (grub_font_construct_glyph): Fix memory leak. * grub-core/font/font.c (grub_font_construct_glyph): Fix memory leak.

View file

@ -581,6 +581,12 @@ script = {
common = tests/example_grub_script_test.in; common = tests/example_grub_script_test.in;
}; };
script = {
testcase;
name = grub_script_test;
common = tests/grub_script_test.in;
};
script = { script = {
testcase; testcase;
name = grub_script_echo1; name = grub_script_echo1;

View file

@ -41,7 +41,8 @@ grub_strtosl (char *arg, char **end, int base)
/* Context for test_parse. */ /* Context for test_parse. */
struct test_parse_ctx struct test_parse_ctx
{ {
int ret, discard, invert; int invert;
int or, and;
int file_exists; int file_exists;
struct grub_dirhook_info file_info; struct grub_dirhook_info file_info;
char *filename; char *filename;
@ -51,9 +52,8 @@ struct test_parse_ctx
static void static void
update_val (int val, struct test_parse_ctx *ctx) update_val (int val, struct test_parse_ctx *ctx)
{ {
if (! ctx->discard) ctx->and = ctx->and && (ctx->invert ? ! val : val);
ctx->ret = ctx->invert ? ! val : val; ctx->invert = 0;
ctx->invert = ctx->discard = 0;
} }
/* A hook for iterating directories. */ /* A hook for iterating directories. */
@ -153,8 +153,8 @@ static int
test_parse (char **args, int *argn, int argc) test_parse (char **args, int *argn, int argc)
{ {
struct test_parse_ctx ctx = { struct test_parse_ctx ctx = {
.ret = 0, .and = 1,
.discard = 0, .or = 0,
.invert = 0 .invert = 0
}; };
@ -332,7 +332,7 @@ test_parse (char **args, int *argn, int argc)
get_fileinfo (args[*argn + 1], &ctx); get_fileinfo (args[*argn + 1], &ctx);
update_val (ctx.file_exists && ctx.file_info.dir, &ctx); update_val (ctx.file_exists && ctx.file_info.dir, &ctx);
(*argn) += 2; (*argn) += 2;
return ctx.ret; return ctx.or || ctx.and;
} }
if (grub_strcmp (args[*argn], "-e") == 0) if (grub_strcmp (args[*argn], "-e") == 0)
@ -340,7 +340,7 @@ test_parse (char **args, int *argn, int argc)
get_fileinfo (args[*argn + 1], &ctx); get_fileinfo (args[*argn + 1], &ctx);
update_val (ctx.file_exists, &ctx); update_val (ctx.file_exists, &ctx);
(*argn) += 2; (*argn) += 2;
return ctx.ret; return ctx.or || ctx.and;
} }
if (grub_strcmp (args[*argn], "-f") == 0) if (grub_strcmp (args[*argn], "-f") == 0)
@ -349,7 +349,7 @@ test_parse (char **args, int *argn, int argc)
/* FIXME: check for other types. */ /* FIXME: check for other types. */
update_val (ctx.file_exists && ! ctx.file_info.dir, &ctx); update_val (ctx.file_exists && ! ctx.file_info.dir, &ctx);
(*argn) += 2; (*argn) += 2;
return ctx.ret; return ctx.or || ctx.and;
} }
if (grub_strcmp (args[*argn], "-s") == 0) if (grub_strcmp (args[*argn], "-s") == 0)
@ -362,7 +362,7 @@ test_parse (char **args, int *argn, int argc)
grub_file_close (file); grub_file_close (file);
grub_errno = GRUB_ERR_NONE; grub_errno = GRUB_ERR_NONE;
(*argn) += 2; (*argn) += 2;
return ctx.ret; return ctx.or || ctx.and;
} }
/* String tests. */ /* String tests. */
@ -387,7 +387,7 @@ test_parse (char **args, int *argn, int argc)
if (grub_strcmp (args[*argn], ")") == 0) if (grub_strcmp (args[*argn], ")") == 0)
{ {
(*argn)++; (*argn)++;
return ctx.ret; return ctx.or || ctx.and;
} }
/* Recursively invoke if parenthesis. */ /* Recursively invoke if parenthesis. */
if (grub_strcmp (args[*argn], "(") == 0) if (grub_strcmp (args[*argn], "(") == 0)
@ -405,15 +405,13 @@ test_parse (char **args, int *argn, int argc)
} }
if (grub_strcmp (args[*argn], "-a") == 0) if (grub_strcmp (args[*argn], "-a") == 0)
{ {
/* If current value is 0 second value is to be discarded. */
ctx.discard = ! ctx.ret;
(*argn)++; (*argn)++;
continue; continue;
} }
if (grub_strcmp (args[*argn], "-o") == 0) if (grub_strcmp (args[*argn], "-o") == 0)
{ {
/* If current value is 1 second value is to be discarded. */ ctx.or = ctx.or || ctx.and;
ctx.discard = ctx.ret; ctx.and = 1;
(*argn)++; (*argn)++;
continue; continue;
} }
@ -422,7 +420,7 @@ test_parse (char **args, int *argn, int argc)
update_val (args[*argn][0], &ctx); update_val (args[*argn][0], &ctx);
(*argn)++; (*argn)++;
} }
return ctx.ret; return ctx.or || ctx.and;
} }
static grub_err_t static grub_err_t

15
tests/grub_script_test.in Normal file
View file

@ -0,0 +1,15 @@
#! @builddir@/grub-shell-tester
for device in 'hd0' 'fd0'; do
# But search them if their search has been inforced
set fd0search="no"
if [ "$device" != "fd0" -a "$device" != "cd" \
-o \
"$device" = "fd0" -a "$fd0search" = "yes" ]\
; then
echo "Yes"
else
echo "No"
fi
done