]> git.proxmox.com Git - grub2.git/blob - grub-core/commands/test.c
* grub-core/commands/ls.c (grub_ls_list_devices): Disable
[grub2.git] / grub-core / commands / test.c
1 /* test.c -- The test command.. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2007,2009 Free Software Foundation, Inc.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
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>
28 #include <grub/i18n.h>
29
30 GRUB_MOD_LICENSE ("GPLv3+");
31
32 /* A simple implementation for signed numbers. */
33 static int
34 grub_strtosl (char *arg, char **end, int base)
35 {
36 if (arg[0] == '-')
37 return -grub_strtoul (arg + 1, end, base);
38 return grub_strtoul (arg, end, base);
39 }
40
41 /* Parse a test expression starting from *argn. */
42 static int
43 test_parse (char **args, int *argn, int argc)
44 {
45 int ret = 0, discard = 0, invert = 0;
46 int file_exists;
47 struct grub_dirhook_info file_info;
48
49 auto void update_val (int val);
50 auto void get_fileinfo (char *pathname);
51
52 /* Take care of discarding and inverting. */
53 void update_val (int val)
54 {
55 if (! discard)
56 ret = invert ? ! val : val;
57 invert = discard = 0;
58 }
59
60 /* Check if file exists and fetch its information. */
61 void get_fileinfo (char *path)
62 {
63 char *filename, *pathname;
64 char *device_name;
65 grub_fs_t fs;
66 grub_device_t dev;
67
68 /* A hook for iterating directories. */
69 auto int find_file (const char *cur_filename,
70 const struct grub_dirhook_info *info);
71 int find_file (const char *cur_filename,
72 const struct grub_dirhook_info *info)
73 {
74 if ((info->case_insensitive ? grub_strcasecmp (cur_filename, filename)
75 : grub_strcmp (cur_filename, filename)) == 0)
76 {
77 file_info = *info;
78 file_exists = 1;
79 return 1;
80 }
81 return 0;
82 }
83
84 file_exists = 0;
85 device_name = grub_file_get_device_name (path);
86 dev = grub_device_open (device_name);
87 if (! dev)
88 {
89 grub_free (device_name);
90 return;
91 }
92
93 fs = grub_fs_probe (dev);
94 if (! fs)
95 {
96 grub_free (device_name);
97 grub_device_close (dev);
98 return;
99 }
100
101 pathname = grub_strchr (path, ')');
102 if (! pathname)
103 pathname = path;
104 else
105 pathname++;
106
107 /* Remove trailing '/'. */
108 while (*pathname && pathname[grub_strlen (pathname) - 1] == '/')
109 pathname[grub_strlen (pathname) - 1] = 0;
110
111 /* Split into path and filename. */
112 filename = grub_strrchr (pathname, '/');
113 if (! filename)
114 {
115 path = grub_strdup ("/");
116 filename = pathname;
117 }
118 else
119 {
120 filename++;
121 path = grub_strdup (pathname);
122 path[filename - pathname] = 0;
123 }
124
125 /* It's the whole device. */
126 if (! *pathname)
127 {
128 file_exists = 1;
129 grub_memset (&file_info, 0, sizeof (file_info));
130 /* Root is always a directory. */
131 file_info.dir = 1;
132
133 /* Fetch writing time. */
134 file_info.mtimeset = 0;
135 if (fs->mtime)
136 {
137 if (! fs->mtime (dev, &file_info.mtime))
138 file_info.mtimeset = 1;
139 grub_errno = GRUB_ERR_NONE;
140 }
141 }
142 else
143 (fs->dir) (dev, path, find_file);
144
145 grub_device_close (dev);
146 grub_free (path);
147 grub_free (device_name);
148 }
149
150 /* Here we have the real parsing. */
151 while (*argn < argc)
152 {
153 /* First try 3 argument tests. */
154 if (*argn + 2 < argc)
155 {
156 /* String tests. */
157 if (grub_strcmp (args[*argn + 1], "=") == 0
158 || grub_strcmp (args[*argn + 1], "==") == 0)
159 {
160 update_val (grub_strcmp (args[*argn], args[*argn + 2]) == 0);
161 (*argn) += 3;
162 continue;
163 }
164
165 if (grub_strcmp (args[*argn + 1], "!=") == 0)
166 {
167 update_val (grub_strcmp (args[*argn], args[*argn + 2]) != 0);
168 (*argn) += 3;
169 continue;
170 }
171
172 /* GRUB extension: lexicographical sorting. */
173 if (grub_strcmp (args[*argn + 1], "<") == 0)
174 {
175 update_val (grub_strcmp (args[*argn], args[*argn + 2]) < 0);
176 (*argn) += 3;
177 continue;
178 }
179
180 if (grub_strcmp (args[*argn + 1], "<=") == 0)
181 {
182 update_val (grub_strcmp (args[*argn], args[*argn + 2]) <= 0);
183 (*argn) += 3;
184 continue;
185 }
186
187 if (grub_strcmp (args[*argn + 1], ">") == 0)
188 {
189 update_val (grub_strcmp (args[*argn], args[*argn + 2]) > 0);
190 (*argn) += 3;
191 continue;
192 }
193
194 if (grub_strcmp (args[*argn + 1], ">=") == 0)
195 {
196 update_val (grub_strcmp (args[*argn], args[*argn + 2]) >= 0);
197 (*argn) += 3;
198 continue;
199 }
200
201 /* Number tests. */
202 if (grub_strcmp (args[*argn + 1], "-eq") == 0)
203 {
204 update_val (grub_strtosl (args[*argn], 0, 0)
205 == grub_strtosl (args[*argn + 2], 0, 0));
206 (*argn) += 3;
207 continue;
208 }
209
210 if (grub_strcmp (args[*argn + 1], "-ge") == 0)
211 {
212 update_val (grub_strtosl (args[*argn], 0, 0)
213 >= grub_strtosl (args[*argn + 2], 0, 0));
214 (*argn) += 3;
215 continue;
216 }
217
218 if (grub_strcmp (args[*argn + 1], "-gt") == 0)
219 {
220 update_val (grub_strtosl (args[*argn], 0, 0)
221 > grub_strtosl (args[*argn + 2], 0, 0));
222 (*argn) += 3;
223 continue;
224 }
225
226 if (grub_strcmp (args[*argn + 1], "-le") == 0)
227 {
228 update_val (grub_strtosl (args[*argn], 0, 0)
229 <= grub_strtosl (args[*argn + 2], 0, 0));
230 (*argn) += 3;
231 continue;
232 }
233
234 if (grub_strcmp (args[*argn + 1], "-lt") == 0)
235 {
236 update_val (grub_strtosl (args[*argn], 0, 0)
237 < grub_strtosl (args[*argn + 2], 0, 0));
238 (*argn) += 3;
239 continue;
240 }
241
242 if (grub_strcmp (args[*argn + 1], "-ne") == 0)
243 {
244 update_val (grub_strtosl (args[*argn], 0, 0)
245 != grub_strtosl (args[*argn + 2], 0, 0));
246 (*argn) += 3;
247 continue;
248 }
249
250 /* GRUB extension: compare numbers skipping prefixes.
251 Useful for comparing versions. E.g. vmlinuz-2 -plt vmlinuz-11. */
252 if (grub_strcmp (args[*argn + 1], "-pgt") == 0
253 || grub_strcmp (args[*argn + 1], "-plt") == 0)
254 {
255 int i;
256 /* Skip common prefix. */
257 for (i = 0; args[*argn][i] == args[*argn + 2][i]
258 && args[*argn][i]; i++);
259
260 /* Go the digits back. */
261 i--;
262 while (grub_isdigit (args[*argn][i]) && i > 0)
263 i--;
264 i++;
265
266 if (grub_strcmp (args[*argn + 1], "-pgt") == 0)
267 update_val (grub_strtoul (args[*argn] + i, 0, 0)
268 > grub_strtoul (args[*argn + 2] + i, 0, 0));
269 else
270 update_val (grub_strtoul (args[*argn] + i, 0, 0)
271 < grub_strtoul (args[*argn + 2] + i, 0, 0));
272 (*argn) += 3;
273 continue;
274 }
275
276 /* -nt and -ot tests. GRUB extension: when doing -?t<bias> bias
277 will be added to the first mtime. */
278 if (grub_memcmp (args[*argn + 1], "-nt", 3) == 0
279 || grub_memcmp (args[*argn + 1], "-ot", 3) == 0)
280 {
281 struct grub_dirhook_info file1;
282 int file1exists;
283 int bias = 0;
284
285 /* Fetch fileinfo. */
286 get_fileinfo (args[*argn]);
287 file1 = file_info;
288 file1exists = file_exists;
289 get_fileinfo (args[*argn + 2]);
290
291 if (args[*argn + 1][3])
292 bias = grub_strtosl (args[*argn + 1] + 3, 0, 0);
293
294 if (grub_memcmp (args[*argn + 1], "-nt", 3) == 0)
295 update_val ((file1exists && ! file_exists)
296 || (file1.mtimeset && file_info.mtimeset
297 && file1.mtime + bias > file_info.mtime));
298 else
299 update_val ((! file1exists && file_exists)
300 || (file1.mtimeset && file_info.mtimeset
301 && file1.mtime + bias < file_info.mtime));
302 (*argn) += 3;
303 continue;
304 }
305 }
306
307 /* Two-argument tests. */
308 if (*argn + 1 < argc)
309 {
310 /* File tests. */
311 if (grub_strcmp (args[*argn], "-d") == 0)
312 {
313 get_fileinfo (args[*argn + 1]);
314 update_val (file_exists && file_info.dir);
315 (*argn) += 2;
316 return ret;
317 }
318
319 if (grub_strcmp (args[*argn], "-e") == 0)
320 {
321 get_fileinfo (args[*argn + 1]);
322 update_val (file_exists);
323 (*argn) += 2;
324 return ret;
325 }
326
327 if (grub_strcmp (args[*argn], "-f") == 0)
328 {
329 get_fileinfo (args[*argn + 1]);
330 /* FIXME: check for other types. */
331 update_val (file_exists && ! file_info.dir);
332 (*argn) += 2;
333 return ret;
334 }
335
336 if (grub_strcmp (args[*argn], "-s") == 0)
337 {
338 grub_file_t file;
339 grub_file_filter_disable_compression ();
340 file = grub_file_open (args[*argn + 1]);
341 update_val (file && (grub_file_size (file) != 0));
342 if (file)
343 grub_file_close (file);
344 grub_errno = GRUB_ERR_NONE;
345 (*argn) += 2;
346 return ret;
347 }
348
349 /* String tests. */
350 if (grub_strcmp (args[*argn], "-n") == 0)
351 {
352 update_val (args[*argn + 1][0]);
353
354 (*argn) += 2;
355 continue;
356 }
357 if (grub_strcmp (args[*argn], "-z") == 0)
358 {
359 update_val (! args[*argn + 1][0]);
360 (*argn) += 2;
361 continue;
362 }
363 }
364
365 /* Special modifiers. */
366
367 /* End of expression. return to parent. */
368 if (grub_strcmp (args[*argn], ")") == 0)
369 {
370 (*argn)++;
371 return ret;
372 }
373 /* Recursively invoke if parenthesis. */
374 if (grub_strcmp (args[*argn], "(") == 0)
375 {
376 (*argn)++;
377 update_val (test_parse (args, argn, argc));
378 continue;
379 }
380
381 if (grub_strcmp (args[*argn], "!") == 0)
382 {
383 invert = ! invert;
384 (*argn)++;
385 continue;
386 }
387 if (grub_strcmp (args[*argn], "-a") == 0)
388 {
389 /* If current value is 0 second value is to be discarded. */
390 discard = ! ret;
391 (*argn)++;
392 continue;
393 }
394 if (grub_strcmp (args[*argn], "-o") == 0)
395 {
396 /* If current value is 1 second value is to be discarded. */
397 discard = ret;
398 (*argn)++;
399 continue;
400 }
401
402 /* No test found. Interpret if as just a string. */
403 update_val (args[*argn][0]);
404 (*argn)++;
405 }
406 return ret;
407 }
408
409 static grub_err_t
410 grub_cmd_test (grub_command_t cmd __attribute__ ((unused)),
411 int argc, char **args)
412 {
413 int argn = 0;
414
415 if (argc >= 1 && grub_strcmp (args[argc - 1], "]") == 0)
416 argc--;
417
418 return test_parse (args, &argn, argc) ? GRUB_ERR_NONE
419 : grub_error (GRUB_ERR_TEST_FAILURE, N_("false"));
420 }
421
422 static grub_command_t cmd_1, cmd_2;
423 \f
424 GRUB_MOD_INIT(test)
425 {
426 cmd_1 = grub_register_command ("[", grub_cmd_test,
427 N_("EXPRESSION ]"), N_("Evaluate an expression."));
428 cmd_1->flags |= GRUB_COMMAND_FLAG_EXTRACTOR;
429 cmd_2 = grub_register_command ("test", grub_cmd_test,
430 N_("EXPRESSION"), N_("Evaluate an expression."));
431 cmd_2->flags |= GRUB_COMMAND_FLAG_EXTRACTOR;
432 }
433
434 GRUB_MOD_FINI(test)
435 {
436 grub_unregister_command (cmd_1);
437 grub_unregister_command (cmd_2);
438 }