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
);
93 grub_free (device_name
);
94 grub_device_close (dev
);
98 pathname
= grub_strchr (path
, ')');
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
, '/');
112 path
= grub_strdup ("/");
118 path
= grub_strdup (pathname
);
119 path
[filename
- pathname
] = 0;
122 /* It's the whole device. */
126 grub_memset (&file_info
, 0, sizeof (file_info
));
127 /* Root is always a directory. */
130 /* Fetch writing time. */
131 file_info
.mtimeset
= 0;
134 if (! fs
->mtime (dev
, &file_info
.mtime
))
135 file_info
.mtimeset
= 1;
136 grub_errno
= GRUB_ERR_NONE
;
140 (fs
->dir
) (dev
, path
, find_file
);
142 grub_device_close (dev
);
144 grub_free (device_name
);
147 /* Here we have the real parsing. */
150 /* First try 3 argument tests. */
151 if (*argn
+ 2 < argc
)
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);
162 if (grub_strcmp (args
[*argn
+ 1], "!=") == 0)
164 update_val (grub_strcmp (args
[*argn
], args
[*argn
+ 2]) != 0);
169 /* GRUB extension: lexicographical sorting. */
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);
191 if (grub_strcmp (args
[*argn
+ 1], ">=") == 0)
193 update_val (grub_strcmp (args
[*argn
], args
[*argn
+ 2]) >= 0);
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));
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));
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));
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));
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));
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));
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)
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. */
259 while (grub_isdigit (args
[*argn
][i
]) && i
> 0)
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));
267 update_val (grub_strtoul (args
[*argn
] + i
, 0, 0)
268 < grub_strtoul (args
[*argn
+ 2] + i
, 0, 0));
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
;
282 /* Fetch fileinfo. */
283 get_fileinfo (args
[*argn
]);
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
));
296 update_val ((! file1exists
&& file_exists
)
297 || (file1
.mtimeset
&& file_info
.mtimeset
298 && file1
.mtime
+ bias
< file_info
.mtime
));
304 /* Two-argument tests. */
305 if (*argn
+ 1 < argc
)
308 if (grub_strcmp (args
[*argn
], "-d") == 0)
310 get_fileinfo (args
[*argn
+ 1]);
311 update_val (file_exists
&& file_info
.dir
);
316 if (grub_strcmp (args
[*argn
], "-e") == 0)
318 get_fileinfo (args
[*argn
+ 1]);
319 update_val (file_exists
);
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
);
333 if (grub_strcmp (args
[*argn
], "-s") == 0)
336 file
= grub_file_open (args
[*argn
+ 1]);
337 update_val (file
&& (grub_file_size (file
) != 0));
339 grub_file_close (file
);
340 grub_errno
= GRUB_ERR_NONE
;
346 if (grub_strcmp (args
[*argn
], "-n") == 0)
348 update_val (args
[*argn
+ 1][0]);
353 if (grub_strcmp (args
[*argn
], "-z") == 0)
355 update_val (! args
[*argn
+ 1][0]);
361 /* Special modifiers. */
363 /* End of expression. return to parent. */
364 if (grub_strcmp (args
[*argn
], ")") == 0)
369 /* Recursively invoke if parenthesis. */
370 if (grub_strcmp (args
[*argn
], "(") == 0)
373 update_val (test_parse (args
, argn
, argc
));
377 if (grub_strcmp (args
[*argn
], "!") == 0)
383 if (grub_strcmp (args
[*argn
], "-a") == 0)
385 /* If current value is 0 second value is to be discarded. */
390 if (grub_strcmp (args
[*argn
], "-o") == 0)
392 /* If current value is 1 second value is to be discarded. */
398 /* No test found. Interpret if as just a string. */
399 update_val (args
[*argn
][0]);
406 grub_cmd_test (grub_command_t cmd
__attribute__ ((unused
)),
407 int argc
, char **args
)
411 if (argc
>= 1 && grub_strcmp (args
[argc
- 1], "]") == 0)
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
;
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");
430 grub_unregister_command (cmd_1
);
431 grub_unregister_command (cmd_2
);