]> git.proxmox.com Git - grub2.git/blame - commands/regexp.c
* script/execute.c (grub_script_execute_cmdline): Check for NULL
[grub2.git] / commands / regexp.c
CommitLineData
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
27static grub_err_t
28grub_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 (&regex, args[0], RE_SYNTAX_GNU_AWK);
43 if (ret)
44 goto fail;
45
46 ret = regexec (&regex, args[1], 0, 0, 0);
47 if (!ret)
48 {
49 regfree (&regex);
50 return GRUB_ERR_NONE;
51 }
52
53 fail:
54 s = regerror (ret, &regex, 0, 0);
55 comperr = grub_malloc (s);
56 if (!comperr)
57 {
58 regfree (&regex);
59 return grub_errno;
60 }
61 regerror (ret, &regex, comperr, s);
62 err = grub_error (GRUB_ERR_TEST_FAILURE, "%s", comperr);
63 regfree (&regex);
64 grub_free (comperr);
65 return err;
66}
67
68static grub_command_t cmd;
69\f
70GRUB_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
77GRUB_MOD_FINI(regexp)
78{
79 grub_unregister_command (cmd);
80}