1 /* test.c -- The test command.. */
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/>.
21 #include <grub/misc.h>
25 #include <grub/device.h>
26 #include <grub/file.h>
27 #include <grub/command.h>
29 /* A simple implementation for signed numbers. */
31 grub_strtosl (char *arg
, char **end
, int base
)
34 return -grub_strtoul (arg
+ 1, end
, base
);
35 return grub_strtoul (arg
, end
, base
);
38 /* Parse a test expression starting from *argn. */
40 test_parse (char **args
, int *argn
, int argc
)
42 int ret
= 0, discard
= 0, invert
= 0;
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
)
53 ret
= invert
? ! val
: val
;
57 /* Check if file exists and fetch its information. */
58 void get_fileinfo (char *path
)
60 char *filename
, *pathname
;
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)
82 device_name
= grub_file_get_device_name (path
);
83 dev
= grub_device_open (device_name
);
86 grub_free (device_name
);
90 fs
= grub_fs_probe (dev
);
91 pathname
= grub_strchr (path
, ')');
97 /* Remove trailing '/'. */
98 while (*pathname
&& pathname
[grub_strlen (pathname
) - 1] == '/')
99 pathname
[grub_strlen (pathname
) - 1] = 0;
101 /* Split into path and filename. */
102 filename
= grub_strrchr (pathname
, '/');
105 path
= grub_strdup ("/");
111 path
= grub_strdup (pathname
);
112 path
[filename
- pathname
] = 0;
115 /* It's the whole device. */
119 grub_memset (&file_info
, 0, sizeof (file_info
));
120 /* Root is always a directory. */
123 /* Fetch writing time. */
124 file_info
.mtimeset
= 0;
127 if (! fs
->mtime (dev
, &file_info
.mtime
))
128 file_info
.mtimeset
= 1;
129 grub_errno
= GRUB_ERR_NONE
;
133 (fs
->dir
) (dev
, path
, find_file
);
135 grub_device_close (dev
);
137 grub_free (device_name
);
140 /* Here we have the real parsing. */
143 /* First try 3 argument tests. */
144 if (*argn
+ 2 < argc
)
147 if (grub_strcmp (args
[*argn
+ 1], "=") == 0
148 || grub_strcmp (args
[*argn
+ 1], "==") == 0)
150 update_val (grub_strcmp (args
[*argn
], args
[*argn
+ 2]) == 0);
155 if (grub_strcmp (args
[*argn
+ 1], "!=") == 0)
157 update_val (grub_strcmp (args
[*argn
], args
[*argn
+ 2]) != 0);
162 /* GRUB extension: lexicographical sorting. */
163 if (grub_strcmp (args
[*argn
+ 1], "<") == 0)
165 update_val (grub_strcmp (args
[*argn
], args
[*argn
+ 2]) < 0);
170 if (grub_strcmp (args
[*argn
+ 1], "<=") == 0)
172 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);
184 if (grub_strcmp (args
[*argn
+ 1], ">=") == 0)
186 update_val (grub_strcmp (args
[*argn
], args
[*argn
+ 2]) >= 0);
192 if (grub_strcmp (args
[*argn
+ 1], "-eq") == 0)
194 update_val (grub_strtosl (args
[*argn
], 0, 0)
195 == grub_strtosl (args
[*argn
+ 2], 0, 0));
200 if (grub_strcmp (args
[*argn
+ 1], "-ge") == 0)
202 update_val (grub_strtosl (args
[*argn
], 0, 0)
203 >= grub_strtosl (args
[*argn
+ 2], 0, 0));
208 if (grub_strcmp (args
[*argn
+ 1], "-gt") == 0)
210 update_val (grub_strtosl (args
[*argn
], 0, 0)
211 > grub_strtosl (args
[*argn
+ 2], 0, 0));
216 if (grub_strcmp (args
[*argn
+ 1], "-le") == 0)
218 update_val (grub_strtosl (args
[*argn
], 0, 0)
219 <= grub_strtosl (args
[*argn
+ 2], 0, 0));
224 if (grub_strcmp (args
[*argn
+ 1], "-lt") == 0)
226 update_val (grub_strtosl (args
[*argn
], 0, 0)
227 < grub_strtosl (args
[*argn
+ 2], 0, 0));
232 if (grub_strcmp (args
[*argn
+ 1], "-ne") == 0)
234 update_val (grub_strtosl (args
[*argn
], 0, 0)
235 != grub_strtosl (args
[*argn
+ 2], 0, 0));
240 /* GRUB extension: compare numbers skipping prefixes.
241 Useful for comparing versions. E.g. vmlinuz-2 -plt vmlinuz-11. */
242 if (grub_strcmp (args
[*argn
+ 1], "-pgt") == 0
243 || grub_strcmp (args
[*argn
+ 1], "-plt") == 0)
246 /* Skip common prefix. */
247 for (i
= 0; args
[*argn
][i
] == args
[*argn
+ 2][i
]
248 && args
[*argn
][i
]; i
++);
250 /* Go the digits back. */
252 while (grub_isdigit (args
[*argn
][i
]) && i
> 0)
256 if (grub_strcmp (args
[*argn
+ 1], "-pgt") == 0)
257 update_val (grub_strtoul (args
[*argn
] + i
, 0, 0)
258 > grub_strtoul (args
[*argn
+ 2] + i
, 0, 0));
260 update_val (grub_strtoul (args
[*argn
] + i
, 0, 0)
261 < grub_strtoul (args
[*argn
+ 2] + i
, 0, 0));
266 /* -nt and -ot tests. GRUB extension: when doing -?t<bias> bias
267 will be added to the first mtime. */
268 if (grub_memcmp (args
[*argn
+ 1], "-nt", 3) == 0
269 || grub_memcmp (args
[*argn
+ 1], "-ot", 3) == 0)
271 struct grub_dirhook_info file1
;
275 /* Fetch fileinfo. */
276 get_fileinfo (args
[*argn
]);
278 file1exists
= file_exists
;
279 get_fileinfo (args
[*argn
+ 2]);
281 if (args
[*argn
+ 1][3])
282 bias
= grub_strtosl (args
[*argn
+ 1] + 3, 0, 0);
284 if (grub_memcmp (args
[*argn
+ 1], "-nt", 3) == 0)
285 update_val ((file1exists
&& ! file_exists
)
286 || (file1
.mtimeset
&& file_info
.mtimeset
287 && file1
.mtime
+ bias
> file_info
.mtime
));
289 update_val ((! file1exists
&& file_exists
)
290 || (file1
.mtimeset
&& file_info
.mtimeset
291 && file1
.mtime
+ bias
< file_info
.mtime
));
297 /* Two-argument tests. */
298 if (*argn
+ 1 < argc
)
301 if (grub_strcmp (args
[*argn
], "-d") == 0)
303 get_fileinfo (args
[*argn
+ 1]);
304 update_val (file_exists
&& file_info
.dir
);
309 if (grub_strcmp (args
[*argn
], "-e") == 0)
311 get_fileinfo (args
[*argn
+ 1]);
312 update_val (file_exists
);
317 if (grub_strcmp (args
[*argn
], "-f") == 0)
319 get_fileinfo (args
[*argn
+ 1]);
320 /* FIXME: check for other types. */
321 update_val (file_exists
&& ! file_info
.dir
);
326 if (grub_strcmp (args
[*argn
], "-s") == 0)
329 file
= grub_file_open (args
[*argn
+ 1]);
330 update_val (file
&& (grub_file_size (file
) != 0));
332 grub_file_close (file
);
333 grub_errno
= GRUB_ERR_NONE
;
339 if (grub_strcmp (args
[*argn
], "-n") == 0)
341 update_val (args
[*argn
+ 1][0]);
346 if (grub_strcmp (args
[*argn
], "-z") == 0)
348 update_val (! args
[*argn
+ 1][0]);
354 /* Special modifiers. */
356 /* End of expression. return to parent. */
357 if (grub_strcmp (args
[*argn
], ")") == 0)
362 /* Recursively invoke if parenthesis. */
363 if (grub_strcmp (args
[*argn
], "(") == 0)
366 update_val (test_parse (args
, argn
, argc
));
370 if (grub_strcmp (args
[*argn
], "!") == 0)
376 if (grub_strcmp (args
[*argn
], "-a") == 0)
378 /* If current value is 0 second value is to be discarded. */
383 if (grub_strcmp (args
[*argn
], "-o") == 0)
385 /* If current value is 1 second value is to be discarded. */
391 /* No test found. Interpret if as just a string. */
392 update_val (args
[*argn
][0]);
399 grub_cmd_test (grub_command_t cmd
__attribute__ ((unused
)),
400 int argc
, char **args
)
404 if (argc
>= 1 && grub_strcmp (args
[argc
- 1], "]") == 0)
407 return test_parse (args
, &argn
, argc
) ? GRUB_ERR_NONE
408 : grub_error (GRUB_ERR_TEST_FAILURE
, "false");
411 static grub_command_t cmd_1
, cmd_2
;
415 cmd_1
= grub_register_command ("[", grub_cmd_test
,
416 "[ EXPRESSION ]", "Evaluate an expression");
417 cmd_2
= grub_register_command ("test", grub_cmd_test
,
418 "test EXPRESSION", "Evaluate an expression");
423 grub_unregister_command (cmd_1
);
424 grub_unregister_command (cmd_2
);