1 /* regexp.c -- The regexp command. */
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/>.
21 #include <grub/misc.h>
25 #include <grub/extcmd.h>
26 #include <grub/i18n.h>
27 #include <grub/script_sh.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
},
50 setvar (char *str
, char *v
, regmatch_t
*m
)
56 err
= grub_env_set (v
, str
+ m
->rm_so
);
62 set_matches (char **varnames
, char *str
, grub_size_t nmatches
,
71 for (i
= 0; varnames
&& varnames
[i
]; i
++)
74 p
= grub_strchr (varnames
[i
], ':');
77 /* varname w/o index defaults to 1 */
78 if (nmatches
< 2 || matches
[1].rm_so
== -1)
79 grub_env_unset (varnames
[i
]);
81 err
= setvar (str
, varnames
[i
], &matches
[1]);
85 j
= grub_strtoul (varnames
[i
], &q
, 10);
87 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
88 "invalid variable name format %s", varnames
[i
]);
90 if (nmatches
<= j
|| matches
[j
].rm_so
== -1)
91 grub_env_unset (p
+ 1);
93 err
= setvar (str
, p
+ 1, &matches
[j
]);
96 if (err
!= GRUB_ERR_NONE
)
103 grub_cmd_regexp (grub_extcmd_context_t ctxt
, int argc
, char **args
)
110 regmatch_t
*matches
= 0;
113 return grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("two arguments expected"));
115 ret
= regcomp (®ex
, args
[0], REG_EXTENDED
);
119 matches
= grub_zalloc (sizeof (*matches
) * (regex
.re_nsub
+ 1));
123 ret
= regexec (®ex
, args
[1], regex
.re_nsub
+ 1, matches
, 0);
126 err
= set_matches (ctxt
->state
[0].args
, args
[1],
127 regex
.re_nsub
+ 1, matches
);
135 s
= regerror (ret
, ®ex
, 0, 0);
136 comperr
= grub_malloc (s
);
142 regerror (ret
, ®ex
, comperr
, s
);
143 err
= grub_error (GRUB_ERR_TEST_FAILURE
, "%s", comperr
);
149 static grub_extcmd_t cmd
;
151 GRUB_MOD_INIT(regexp
)
153 cmd
= grub_register_extcmd ("regexp", grub_cmd_regexp
, 0,
154 /* TRANSLATORS: This are two arguments. So it's
155 two separate units to translate and pay
156 attention not to reverse them. */
158 N_("Test if REGEXP matches STRING."), options
);
160 /* Setup GRUB script wildcard translator. */
161 grub_wildcard_translator
= &grub_filename_translator
;
164 GRUB_MOD_FINI(regexp
)
166 grub_unregister_extcmd (cmd
);
167 grub_wildcard_translator
= 0;