]>
Commit | Line | Data |
---|---|---|
3251f0f8 VS |
1 | /* regexp.c -- The regexp command. */ |
2 | /* | |
3 | * GRUB -- GRand Unified Bootloader | |
4 | * Copyright (C) 2005,2007 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/command.h> | |
d8b5cd40 | 24 | #include <grub/i18n.h> |
74f34747 | 25 | #include <regex.h> |
3251f0f8 VS |
26 | |
27 | static grub_err_t | |
28 | grub_cmd_regexp (grub_command_t cmd __attribute__ ((unused)), | |
29 | int argc, char **args) | |
30 | { | |
31 | int argn = 0; | |
32 | int matches = 0; | |
33 | regex_t regex; | |
34 | int ret; | |
35 | grub_size_t s; | |
36 | char *comperr; | |
37 | grub_err_t err; | |
38 | ||
39 | if (argc != 2) | |
40 | return grub_error (GRUB_ERR_BAD_ARGUMENT, "2 arguments expected"); | |
41 | ||
42 | ret = regcomp (®ex, args[0], RE_SYNTAX_GNU_AWK); | |
43 | if (ret) | |
44 | goto fail; | |
45 | ||
46 | ret = regexec (®ex, args[1], 0, 0, 0); | |
47 | if (!ret) | |
48 | { | |
49 | regfree (®ex); | |
50 | return GRUB_ERR_NONE; | |
51 | } | |
52 | ||
53 | fail: | |
54 | s = regerror (ret, ®ex, 0, 0); | |
55 | comperr = grub_malloc (s); | |
56 | if (!comperr) | |
57 | { | |
58 | regfree (®ex); | |
59 | return grub_errno; | |
60 | } | |
61 | regerror (ret, ®ex, comperr, s); | |
62 | err = grub_error (GRUB_ERR_TEST_FAILURE, "%s", comperr); | |
63 | regfree (®ex); | |
64 | grub_free (comperr); | |
65 | return err; | |
66 | } | |
67 | ||
68 | static grub_command_t cmd; | |
69 | \f | |
70 | GRUB_MOD_INIT(regexp) | |
71 | { | |
72 | cmd = grub_register_command ("regexp", grub_cmd_regexp, | |
d8b5cd40 VS |
73 | N_("REGEXP STRING"), |
74 | N_("Test if REGEXP matches STRING.")); | |
3251f0f8 VS |
75 | } |
76 | ||
77 | GRUB_MOD_FINI(regexp) | |
78 | { | |
79 | grub_unregister_command (cmd); | |
80 | } |