2009-11-21 Samuel Thibault <samuel.thibault@ens-lyon.org>
[grub2.git] / commands / test.c
blob9c813c82047b9c0da876a9bf7e3c0d0e7ee2585c
1 /* test.c -- The test command.. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2007 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/>.
20 #include <grub/dl.h>
21 #include <grub/misc.h>
22 #include <grub/mm.h>
23 #include <grub/env.h>
24 #include <grub/fs.h>
25 #include <grub/device.h>
26 #include <grub/file.h>
27 #include <grub/command.h>
29 /* A simple implementation for signed numbers. */
30 static int
31 grub_strtosl (char *arg, char **end, int base)
33 if (arg[0] == '-')
34 return -grub_strtoul (arg + 1, end, base);
35 return grub_strtoul (arg, end, base);
38 /* Parse a test expression starting from *argn. */
39 static int
40 test_parse (char **args, int *argn, int argc)
42 int ret = 0, discard = 0, invert = 0;
43 int file_exists;
44 struct grub_dirhook_info file_info;
46 auto void update_val (int val);
47 auto void get_fileinfo (char *pathname);
49 /* Take care of discarding and inverting. */
50 void update_val (int val)
52 if (! discard)
53 ret = invert ? ! val : val;
54 invert = discard = 0;
57 /* Check if file exists and fetch its information. */
58 void get_fileinfo (char *path)
60 char *filename, *pathname;
61 char *device_name;
62 grub_fs_t fs;
63 grub_device_t dev;
65 /* A hook for iterating directories. */
66 auto int find_file (const char *cur_filename,
67 const struct grub_dirhook_info *info);
68 int find_file (const char *cur_filename,
69 const struct grub_dirhook_info *info)
71 if ((info->case_insensitive ? grub_strcasecmp (cur_filename, filename)
72 : grub_strcmp (cur_filename, filename)) == 0)
74 file_info = *info;
75 file_exists = 1;
76 return 1;
78 return 0;
81 file_exists = 0;
82 device_name = grub_file_get_device_name (path);
83 dev = grub_device_open (device_name);
84 if (! dev)
86 grub_free (device_name);
87 return;
90 fs = grub_fs_probe (dev);
91 if (! fs)
93 grub_free (device_name);
94 grub_device_close (dev);
95 return;
98 pathname = grub_strchr (path, ')');
99 if (! pathname)
100 pathname = path;
101 else
102 pathname++;
104 /* Remove trailing '/'. */
105 while (*pathname && pathname[grub_strlen (pathname) - 1] == '/')
106 pathname[grub_strlen (pathname) - 1] = 0;
108 /* Split into path and filename. */
109 filename = grub_strrchr (pathname, '/');
110 if (! filename)
112 path = grub_strdup ("/");
113 filename = pathname;
115 else
117 filename++;
118 path = grub_strdup (pathname);
119 path[filename - pathname] = 0;
122 /* It's the whole device. */
123 if (! *pathname)
125 file_exists = 1;
126 grub_memset (&file_info, 0, sizeof (file_info));
127 /* Root is always a directory. */
128 file_info.dir = 1;
130 /* Fetch writing time. */
131 file_info.mtimeset = 0;
132 if (fs->mtime)
134 if (! fs->mtime (dev, &file_info.mtime))
135 file_info.mtimeset = 1;
136 grub_errno = GRUB_ERR_NONE;
139 else
140 (fs->dir) (dev, path, find_file);
142 grub_device_close (dev);
143 grub_free (path);
144 grub_free (device_name);
147 /* Here we have the real parsing. */
148 while (*argn < argc)
150 /* First try 3 argument tests. */
151 if (*argn + 2 < argc)
153 /* String tests. */
154 if (grub_strcmp (args[*argn + 1], "=") == 0
155 || grub_strcmp (args[*argn + 1], "==") == 0)
157 update_val (grub_strcmp (args[*argn], args[*argn + 2]) == 0);
158 (*argn) += 3;
159 continue;
162 if (grub_strcmp (args[*argn + 1], "!=") == 0)
164 update_val (grub_strcmp (args[*argn], args[*argn + 2]) != 0);
165 (*argn) += 3;
166 continue;
169 /* GRUB extension: lexicographical sorting. */
170 if (grub_strcmp (args[*argn + 1], "<") == 0)
172 update_val (grub_strcmp (args[*argn], args[*argn + 2]) < 0);
173 (*argn) += 3;
174 continue;
177 if (grub_strcmp (args[*argn + 1], "<=") == 0)
179 update_val (grub_strcmp (args[*argn], args[*argn + 2]) <= 0);
180 (*argn) += 3;
181 continue;
184 if (grub_strcmp (args[*argn + 1], ">") == 0)
186 update_val (grub_strcmp (args[*argn], args[*argn + 2]) > 0);
187 (*argn) += 3;
188 continue;
191 if (grub_strcmp (args[*argn + 1], ">=") == 0)
193 update_val (grub_strcmp (args[*argn], args[*argn + 2]) >= 0);
194 (*argn) += 3;
195 continue;
198 /* Number tests. */
199 if (grub_strcmp (args[*argn + 1], "-eq") == 0)
201 update_val (grub_strtosl (args[*argn], 0, 0)
202 == grub_strtosl (args[*argn + 2], 0, 0));
203 (*argn) += 3;
204 continue;
207 if (grub_strcmp (args[*argn + 1], "-ge") == 0)
209 update_val (grub_strtosl (args[*argn], 0, 0)
210 >= grub_strtosl (args[*argn + 2], 0, 0));
211 (*argn) += 3;
212 continue;
215 if (grub_strcmp (args[*argn + 1], "-gt") == 0)
217 update_val (grub_strtosl (args[*argn], 0, 0)
218 > grub_strtosl (args[*argn + 2], 0, 0));
219 (*argn) += 3;
220 continue;
223 if (grub_strcmp (args[*argn + 1], "-le") == 0)
225 update_val (grub_strtosl (args[*argn], 0, 0)
226 <= grub_strtosl (args[*argn + 2], 0, 0));
227 (*argn) += 3;
228 continue;
231 if (grub_strcmp (args[*argn + 1], "-lt") == 0)
233 update_val (grub_strtosl (args[*argn], 0, 0)
234 < grub_strtosl (args[*argn + 2], 0, 0));
235 (*argn) += 3;
236 continue;
239 if (grub_strcmp (args[*argn + 1], "-ne") == 0)
241 update_val (grub_strtosl (args[*argn], 0, 0)
242 != grub_strtosl (args[*argn + 2], 0, 0));
243 (*argn) += 3;
244 continue;
247 /* GRUB extension: compare numbers skipping prefixes.
248 Useful for comparing versions. E.g. vmlinuz-2 -plt vmlinuz-11. */
249 if (grub_strcmp (args[*argn + 1], "-pgt") == 0
250 || grub_strcmp (args[*argn + 1], "-plt") == 0)
252 int i;
253 /* Skip common prefix. */
254 for (i = 0; args[*argn][i] == args[*argn + 2][i]
255 && args[*argn][i]; i++);
257 /* Go the digits back. */
258 i--;
259 while (grub_isdigit (args[*argn][i]) && i > 0)
260 i--;
261 i++;
263 if (grub_strcmp (args[*argn + 1], "-pgt") == 0)
264 update_val (grub_strtoul (args[*argn] + i, 0, 0)
265 > grub_strtoul (args[*argn + 2] + i, 0, 0));
266 else
267 update_val (grub_strtoul (args[*argn] + i, 0, 0)
268 < grub_strtoul (args[*argn + 2] + i, 0, 0));
269 (*argn) += 3;
270 continue;
273 /* -nt and -ot tests. GRUB extension: when doing -?t<bias> bias
274 will be added to the first mtime. */
275 if (grub_memcmp (args[*argn + 1], "-nt", 3) == 0
276 || grub_memcmp (args[*argn + 1], "-ot", 3) == 0)
278 struct grub_dirhook_info file1;
279 int file1exists;
280 int bias = 0;
282 /* Fetch fileinfo. */
283 get_fileinfo (args[*argn]);
284 file1 = file_info;
285 file1exists = file_exists;
286 get_fileinfo (args[*argn + 2]);
288 if (args[*argn + 1][3])
289 bias = grub_strtosl (args[*argn + 1] + 3, 0, 0);
291 if (grub_memcmp (args[*argn + 1], "-nt", 3) == 0)
292 update_val ((file1exists && ! file_exists)
293 || (file1.mtimeset && file_info.mtimeset
294 && file1.mtime + bias > file_info.mtime));
295 else
296 update_val ((! file1exists && file_exists)
297 || (file1.mtimeset && file_info.mtimeset
298 && file1.mtime + bias < file_info.mtime));
299 (*argn) += 3;
300 continue;
304 /* Two-argument tests. */
305 if (*argn + 1 < argc)
307 /* File tests. */
308 if (grub_strcmp (args[*argn], "-d") == 0)
310 get_fileinfo (args[*argn + 1]);
311 update_val (file_exists && file_info.dir);
312 (*argn) += 2;
313 return ret;
316 if (grub_strcmp (args[*argn], "-e") == 0)
318 get_fileinfo (args[*argn + 1]);
319 update_val (file_exists);
320 (*argn) += 2;
321 return ret;
324 if (grub_strcmp (args[*argn], "-f") == 0)
326 get_fileinfo (args[*argn + 1]);
327 /* FIXME: check for other types. */
328 update_val (file_exists && ! file_info.dir);
329 (*argn) += 2;
330 return ret;
333 if (grub_strcmp (args[*argn], "-s") == 0)
335 grub_file_t file;
336 file = grub_file_open (args[*argn + 1]);
337 update_val (file && (grub_file_size (file) != 0));
338 if (file)
339 grub_file_close (file);
340 grub_errno = GRUB_ERR_NONE;
341 (*argn) += 2;
342 return ret;
345 /* String tests. */
346 if (grub_strcmp (args[*argn], "-n") == 0)
348 update_val (args[*argn + 1][0]);
350 (*argn) += 2;
351 continue;
353 if (grub_strcmp (args[*argn], "-z") == 0)
355 update_val (! args[*argn + 1][0]);
356 (*argn) += 2;
357 continue;
361 /* Special modifiers. */
363 /* End of expression. return to parent. */
364 if (grub_strcmp (args[*argn], ")") == 0)
366 (*argn)++;
367 return ret;
369 /* Recursively invoke if parenthesis. */
370 if (grub_strcmp (args[*argn], "(") == 0)
372 (*argn)++;
373 update_val (test_parse (args, argn, argc));
374 continue;
377 if (grub_strcmp (args[*argn], "!") == 0)
379 invert = ! invert;
380 (*argn)++;
381 continue;
383 if (grub_strcmp (args[*argn], "-a") == 0)
385 /* If current value is 0 second value is to be discarded. */
386 discard = ! ret;
387 (*argn)++;
388 continue;
390 if (grub_strcmp (args[*argn], "-o") == 0)
392 /* If current value is 1 second value is to be discarded. */
393 discard = ret;
394 (*argn)++;
395 continue;
398 /* No test found. Interpret if as just a string. */
399 update_val (args[*argn][0]);
400 (*argn)++;
402 return ret;
405 static grub_err_t
406 grub_cmd_test (grub_command_t cmd __attribute__ ((unused)),
407 int argc, char **args)
409 int argn = 0;
411 if (argc >= 1 && grub_strcmp (args[argc - 1], "]") == 0)
412 argc--;
414 return test_parse (args, &argn, argc) ? GRUB_ERR_NONE
415 : grub_error (GRUB_ERR_TEST_FAILURE, "false");
418 static grub_command_t cmd_1, cmd_2;
420 GRUB_MOD_INIT(test)
422 cmd_1 = grub_register_command ("[", grub_cmd_test,
423 "[ EXPRESSION ]", "Evaluate an expression");
424 cmd_2 = grub_register_command ("test", grub_cmd_test,
425 "test EXPRESSION", "Evaluate an expression");
428 GRUB_MOD_FINI(test)
430 grub_unregister_command (cmd_1);
431 grub_unregister_command (cmd_2);