Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / commands / regexp.c
blobb0706d06943a446197501e2668ce24b324c30da9
1 /* regexp.c -- The regexp command. */
2 /*
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/>.
20 #include <grub/dl.h>
21 #include <grub/misc.h>
22 #include <grub/mm.h>
23 #include <grub/err.h>
24 #include <grub/env.h>
25 #include <grub/extcmd.h>
26 #include <grub/i18n.h>
27 #include <grub/script_sh.h>
28 #include <regex.h>
30 GRUB_MOD_LICENSE ("GPLv3+");
32 static const struct grub_arg_option options[] =
34 { "set", 's', GRUB_ARG_OPTION_REPEATABLE,
35 /* TRANSLATORS: in regexp you can mark some
36 groups with parentheses. These groups are
37 then numbered and you can save some of
38 them in variables. In other programs
39 those components aree often referenced with
40 back slash, e.g. \1. Compare
41 sed -e 's,\([a-z][a-z]*\),lowercase=\1,g'
42 The whole matching component is saved in VARNAME, not its number.
44 N_("Store matched component NUMBER in VARNAME."),
45 N_("[NUMBER:]VARNAME"), ARG_TYPE_STRING },
46 { 0, 0, 0, 0, 0, 0 }
49 static grub_err_t
50 set_matches (char **varnames, char *str, grub_size_t nmatches,
51 regmatch_t *matches)
53 int i;
54 char *p;
55 char *q;
56 grub_err_t err;
57 unsigned long j;
59 auto void setvar (char *v, regmatch_t *m);
60 void setvar (char *v, regmatch_t *m)
62 char ch;
63 ch = str[m->rm_eo];
64 str[m->rm_eo] = '\0';
65 err = grub_env_set (v, str + m->rm_so);
66 str[m->rm_eo] = ch;
69 for (i = 0; varnames && varnames[i]; i++)
71 p = grub_strchr (varnames[i], ':');
72 if (! p)
74 /* varname w/o index defaults to 1 */
75 if (nmatches < 2 || matches[1].rm_so == -1)
76 grub_env_unset (varnames[i]);
77 else
78 setvar (varnames[i], &matches[1]);
80 else
82 j = grub_strtoul (varnames[i], &q, 10);
83 if (q != p)
84 return grub_error (GRUB_ERR_BAD_ARGUMENT,
85 "invalid variable name format %s", varnames[i]);
87 if (nmatches <= j || matches[j].rm_so == -1)
88 grub_env_unset (p + 1);
89 else
90 setvar (p + 1, &matches[j]);
93 if (err != GRUB_ERR_NONE)
94 return err;
96 return GRUB_ERR_NONE;
99 static grub_err_t
100 grub_cmd_regexp (grub_extcmd_context_t ctxt, int argc, char **args)
102 regex_t regex;
103 int ret;
104 grub_size_t s;
105 char *comperr;
106 grub_err_t err;
107 regmatch_t *matches = 0;
109 if (argc != 2)
110 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected"));
112 ret = regcomp (&regex, args[0], REG_EXTENDED);
113 if (ret)
114 goto fail;
116 matches = grub_zalloc (sizeof (*matches) * (regex.re_nsub + 1));
117 if (! matches)
118 goto fail;
120 ret = regexec (&regex, args[1], regex.re_nsub + 1, matches, 0);
121 if (!ret)
123 err = set_matches (ctxt->state[0].args, args[1],
124 regex.re_nsub + 1, matches);
125 regfree (&regex);
126 grub_free (matches);
127 return err;
130 fail:
131 grub_free (matches);
132 s = regerror (ret, &regex, 0, 0);
133 comperr = grub_malloc (s);
134 if (!comperr)
136 regfree (&regex);
137 return grub_errno;
139 regerror (ret, &regex, comperr, s);
140 err = grub_error (GRUB_ERR_TEST_FAILURE, "%s", comperr);
141 regfree (&regex);
142 grub_free (comperr);
143 return err;
146 static grub_extcmd_t cmd;
148 GRUB_MOD_INIT(regexp)
150 cmd = grub_register_extcmd ("regexp", grub_cmd_regexp, 0,
151 /* TRANSLATORS: This are two arguments. So it's
152 two separate units to translate and pay
153 attention not to reverse them. */
154 N_("REGEXP STRING"),
155 N_("Test if REGEXP matches STRING."), options);
157 /* Setup GRUB script wildcard translator. */
158 grub_wildcard_translator = &grub_filename_translator;
161 GRUB_MOD_FINI(regexp)
163 grub_unregister_extcmd (cmd);
164 grub_wildcard_translator = 0;