]> git.proxmox.com Git - grub2.git/blame - commands/regexp.c
resynced with gnulib. Cleaned up wrapping layer
[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>
24#include <grub/regex.h>
25
26static grub_err_t
27grub_cmd_regexp (grub_command_t cmd __attribute__ ((unused)),
28 int argc, char **args)
29{
30 int argn = 0;
31 int matches = 0;
32 regex_t regex;
33 int ret;
34 grub_size_t s;
35 char *comperr;
36 grub_err_t err;
37
38 if (argc != 2)
39 return grub_error (GRUB_ERR_BAD_ARGUMENT, "2 arguments expected");
40
41 ret = regcomp (&regex, args[0], RE_SYNTAX_GNU_AWK);
42 if (ret)
43 goto fail;
44
45 ret = regexec (&regex, args[1], 0, 0, 0);
46 if (!ret)
47 {
48 regfree (&regex);
49 return GRUB_ERR_NONE;
50 }
51
52 fail:
53 s = regerror (ret, &regex, 0, 0);
54 comperr = grub_malloc (s);
55 if (!comperr)
56 {
57 regfree (&regex);
58 return grub_errno;
59 }
60 regerror (ret, &regex, comperr, s);
61 err = grub_error (GRUB_ERR_TEST_FAILURE, "%s", comperr);
62 regfree (&regex);
63 grub_free (comperr);
64 return err;
65}
66
67static grub_command_t cmd;
68\f
69GRUB_MOD_INIT(regexp)
70{
71 cmd = grub_register_command ("regexp", grub_cmd_regexp,
72 "REGEXP STRING",
73 "Test if REGEXP matches STRING.");
74}
75
76GRUB_MOD_FINI(regexp)
77{
78 grub_unregister_command (cmd);
79}