1 /* test.c -- The test command.. */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2007,2009 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
21 #include <grub/misc.h>
25 #include <grub/device.h>
26 #include <grub/file.h>
27 #include <grub/command.h>
28 #include <grub/i18n.h>
30 GRUB_MOD_LICENSE ("GPLv3+");
32 /* A simple implementation for signed numbers. */
34 grub_strtosl (char *arg
, char **end
, int base
)
37 return -grub_strtoul (arg
+ 1, end
, base
);
38 return grub_strtoul (arg
, end
, base
);
41 /* Context for test_parse. */
47 struct grub_dirhook_info file_info
;
51 /* Take care of discarding and inverting. */
53 update_val (int val
, struct test_parse_ctx
*ctx
)
55 ctx
->and = ctx
->and && (ctx
->invert
? ! val
: val
);
59 /* A hook for iterating directories. */
61 find_file (const char *cur_filename
, const struct grub_dirhook_info
*info
,
64 struct test_parse_ctx
*ctx
= data
;
66 if ((info
->case_insensitive
? grub_strcasecmp (cur_filename
, ctx
->filename
)
67 : grub_strcmp (cur_filename
, ctx
->filename
)) == 0)
69 ctx
->file_info
= *info
;
76 /* Check if file exists and fetch its information. */
78 get_fileinfo (char *path
, struct test_parse_ctx
*ctx
)
86 device_name
= grub_file_get_device_name (path
);
87 dev
= grub_device_open (device_name
);
90 grub_free (device_name
);
94 fs
= grub_fs_probe (dev
);
97 grub_free (device_name
);
98 grub_device_close (dev
);
102 pathname
= grub_strchr (path
, ')');
108 /* Remove trailing '/'. */
109 while (*pathname
&& pathname
[grub_strlen (pathname
) - 1] == '/')
110 pathname
[grub_strlen (pathname
) - 1] = 0;
112 /* Split into path and filename. */
113 ctx
->filename
= grub_strrchr (pathname
, '/');
116 path
= grub_strdup ("/");
117 ctx
->filename
= pathname
;
122 path
= grub_strdup (pathname
);
123 path
[ctx
->filename
- pathname
] = 0;
126 /* It's the whole device. */
129 ctx
->file_exists
= 1;
130 grub_memset (&ctx
->file_info
, 0, sizeof (ctx
->file_info
));
131 /* Root is always a directory. */
132 ctx
->file_info
.dir
= 1;
134 /* Fetch writing time. */
135 ctx
->file_info
.mtimeset
= 0;
138 if (! fs
->mtime (dev
, &ctx
->file_info
.mtime
))
139 ctx
->file_info
.mtimeset
= 1;
140 grub_errno
= GRUB_ERR_NONE
;
144 (fs
->dir
) (dev
, path
, find_file
, ctx
);
146 grub_device_close (dev
);
148 grub_free (device_name
);
151 /* Parse a test expression starting from *argn. */
153 test_parse (char **args
, int *argn
, int argc
)
155 struct test_parse_ctx ctx
= {
161 /* Here we have the real parsing. */
164 /* First try 3 argument tests. */
165 if (*argn
+ 2 < argc
)
168 if (grub_strcmp (args
[*argn
+ 1], "=") == 0
169 || grub_strcmp (args
[*argn
+ 1], "==") == 0)
171 update_val (grub_strcmp (args
[*argn
], args
[*argn
+ 2]) == 0,
177 if (grub_strcmp (args
[*argn
+ 1], "!=") == 0)
179 update_val (grub_strcmp (args
[*argn
], args
[*argn
+ 2]) != 0,
185 /* GRUB extension: lexicographical sorting. */
186 if (grub_strcmp (args
[*argn
+ 1], "<") == 0)
188 update_val (grub_strcmp (args
[*argn
], args
[*argn
+ 2]) < 0,
194 if (grub_strcmp (args
[*argn
+ 1], "<=") == 0)
196 update_val (grub_strcmp (args
[*argn
], args
[*argn
+ 2]) <= 0,
202 if (grub_strcmp (args
[*argn
+ 1], ">") == 0)
204 update_val (grub_strcmp (args
[*argn
], args
[*argn
+ 2]) > 0,
210 if (grub_strcmp (args
[*argn
+ 1], ">=") == 0)
212 update_val (grub_strcmp (args
[*argn
], args
[*argn
+ 2]) >= 0,
219 if (grub_strcmp (args
[*argn
+ 1], "-eq") == 0)
221 update_val (grub_strtosl (args
[*argn
], 0, 0)
222 == grub_strtosl (args
[*argn
+ 2], 0, 0), &ctx
);
227 if (grub_strcmp (args
[*argn
+ 1], "-ge") == 0)
229 update_val (grub_strtosl (args
[*argn
], 0, 0)
230 >= grub_strtosl (args
[*argn
+ 2], 0, 0), &ctx
);
235 if (grub_strcmp (args
[*argn
+ 1], "-gt") == 0)
237 update_val (grub_strtosl (args
[*argn
], 0, 0)
238 > grub_strtosl (args
[*argn
+ 2], 0, 0), &ctx
);
243 if (grub_strcmp (args
[*argn
+ 1], "-le") == 0)
245 update_val (grub_strtosl (args
[*argn
], 0, 0)
246 <= grub_strtosl (args
[*argn
+ 2], 0, 0), &ctx
);
251 if (grub_strcmp (args
[*argn
+ 1], "-lt") == 0)
253 update_val (grub_strtosl (args
[*argn
], 0, 0)
254 < grub_strtosl (args
[*argn
+ 2], 0, 0), &ctx
);
259 if (grub_strcmp (args
[*argn
+ 1], "-ne") == 0)
261 update_val (grub_strtosl (args
[*argn
], 0, 0)
262 != grub_strtosl (args
[*argn
+ 2], 0, 0), &ctx
);
267 /* GRUB extension: compare numbers skipping prefixes.
268 Useful for comparing versions. E.g. vmlinuz-2 -plt vmlinuz-11. */
269 if (grub_strcmp (args
[*argn
+ 1], "-pgt") == 0
270 || grub_strcmp (args
[*argn
+ 1], "-plt") == 0)
273 /* Skip common prefix. */
274 for (i
= 0; args
[*argn
][i
] == args
[*argn
+ 2][i
]
275 && args
[*argn
][i
]; i
++);
277 /* Go the digits back. */
279 while (grub_isdigit (args
[*argn
][i
]) && i
> 0)
283 if (grub_strcmp (args
[*argn
+ 1], "-pgt") == 0)
284 update_val (grub_strtoul (args
[*argn
] + i
, 0, 0)
285 > grub_strtoul (args
[*argn
+ 2] + i
, 0, 0), &ctx
);
287 update_val (grub_strtoul (args
[*argn
] + i
, 0, 0)
288 < grub_strtoul (args
[*argn
+ 2] + i
, 0, 0), &ctx
);
293 /* -nt and -ot tests. GRUB extension: when doing -?t<bias> bias
294 will be added to the first mtime. */
295 if (grub_memcmp (args
[*argn
+ 1], "-nt", 3) == 0
296 || grub_memcmp (args
[*argn
+ 1], "-ot", 3) == 0)
298 struct grub_dirhook_info file1
;
302 /* Fetch fileinfo. */
303 get_fileinfo (args
[*argn
], &ctx
);
304 file1
= ctx
.file_info
;
305 file1exists
= ctx
.file_exists
;
306 get_fileinfo (args
[*argn
+ 2], &ctx
);
308 if (args
[*argn
+ 1][3])
309 bias
= grub_strtosl (args
[*argn
+ 1] + 3, 0, 0);
311 if (grub_memcmp (args
[*argn
+ 1], "-nt", 3) == 0)
312 update_val ((file1exists
&& ! ctx
.file_exists
)
313 || (file1
.mtimeset
&& ctx
.file_info
.mtimeset
314 && file1
.mtime
+ bias
> ctx
.file_info
.mtime
),
317 update_val ((! file1exists
&& ctx
.file_exists
)
318 || (file1
.mtimeset
&& ctx
.file_info
.mtimeset
319 && file1
.mtime
+ bias
< ctx
.file_info
.mtime
),
326 /* Two-argument tests. */
327 if (*argn
+ 1 < argc
)
330 if (grub_strcmp (args
[*argn
], "-d") == 0)
332 get_fileinfo (args
[*argn
+ 1], &ctx
);
333 update_val (ctx
.file_exists
&& ctx
.file_info
.dir
, &ctx
);
338 if (grub_strcmp (args
[*argn
], "-e") == 0)
340 get_fileinfo (args
[*argn
+ 1], &ctx
);
341 update_val (ctx
.file_exists
, &ctx
);
346 if (grub_strcmp (args
[*argn
], "-f") == 0)
348 get_fileinfo (args
[*argn
+ 1], &ctx
);
349 /* FIXME: check for other types. */
350 update_val (ctx
.file_exists
&& ! ctx
.file_info
.dir
, &ctx
);
355 if (grub_strcmp (args
[*argn
], "-s") == 0)
358 grub_file_filter_disable_compression ();
359 file
= grub_file_open (args
[*argn
+ 1]);
360 update_val (file
&& (grub_file_size (file
) != 0), &ctx
);
362 grub_file_close (file
);
363 grub_errno
= GRUB_ERR_NONE
;
369 if (grub_strcmp (args
[*argn
], "-n") == 0)
371 update_val (args
[*argn
+ 1][0], &ctx
);
376 if (grub_strcmp (args
[*argn
], "-z") == 0)
378 update_val (! args
[*argn
+ 1][0], &ctx
);
384 /* Special modifiers. */
386 /* End of expression. return to parent. */
387 if (grub_strcmp (args
[*argn
], ")") == 0)
390 return ctx
.or || ctx
.and;
392 /* Recursively invoke if parenthesis. */
393 if (grub_strcmp (args
[*argn
], "(") == 0)
396 update_val (test_parse (args
, argn
, argc
), &ctx
);
400 if (grub_strcmp (args
[*argn
], "!") == 0)
402 ctx
.invert
= ! ctx
.invert
;
406 if (grub_strcmp (args
[*argn
], "-a") == 0)
411 if (grub_strcmp (args
[*argn
], "-o") == 0)
413 ctx
.or = ctx
.or || ctx
.and;
419 /* No test found. Interpret if as just a string. */
420 update_val (args
[*argn
][0], &ctx
);
423 return ctx
.or || ctx
.and;
427 grub_cmd_test (grub_command_t cmd
__attribute__ ((unused
)),
428 int argc
, char **args
)
432 if (argc
>= 1 && grub_strcmp (args
[argc
- 1], "]") == 0)
435 return test_parse (args
, &argn
, argc
) ? GRUB_ERR_NONE
436 : grub_error (GRUB_ERR_TEST_FAILURE
, N_("false"));
439 static grub_command_t cmd_1
, cmd_2
;
443 cmd_1
= grub_register_command ("[", grub_cmd_test
,
444 N_("EXPRESSION ]"), N_("Evaluate an expression."));
445 cmd_1
->flags
|= GRUB_COMMAND_FLAG_EXTRACTOR
;
446 cmd_2
= grub_register_command ("test", grub_cmd_test
,
447 N_("EXPRESSION"), N_("Evaluate an expression."));
448 cmd_2
->flags
|= GRUB_COMMAND_FLAG_EXTRACTOR
;
453 grub_unregister_command (cmd_1
);
454 grub_unregister_command (cmd_2
);