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 set_matches (char **varnames
, char *str
, grub_size_t nmatches
,
59 auto void setvar (char *v
, regmatch_t
*m
);
60 void setvar (char *v
, regmatch_t
*m
)
65 err
= grub_env_set (v
, str
+ m
->rm_so
);
69 for (i
= 0; varnames
&& varnames
[i
]; i
++)
71 p
= grub_strchr (varnames
[i
], ':');
74 /* varname w/o index defaults to 1 */
75 if (nmatches
< 2 || matches
[1].rm_so
== -1)
76 grub_env_unset (varnames
[i
]);
78 setvar (varnames
[i
], &matches
[1]);
82 j
= grub_strtoul (varnames
[i
], &q
, 10);
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);
90 setvar (p
+ 1, &matches
[j
]);
93 if (err
!= GRUB_ERR_NONE
)
100 grub_cmd_regexp (grub_extcmd_context_t ctxt
, int argc
, char **args
)
107 regmatch_t
*matches
= 0;
110 return grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("two arguments expected"));
112 ret
= regcomp (®ex
, args
[0], REG_EXTENDED
);
116 matches
= grub_zalloc (sizeof (*matches
) * (regex
.re_nsub
+ 1));
120 ret
= regexec (®ex
, args
[1], regex
.re_nsub
+ 1, matches
, 0);
123 err
= set_matches (ctxt
->state
[0].args
, args
[1],
124 regex
.re_nsub
+ 1, matches
);
132 s
= regerror (ret
, ®ex
, 0, 0);
133 comperr
= grub_malloc (s
);
139 regerror (ret
, ®ex
, comperr
, s
);
140 err
= grub_error (GRUB_ERR_TEST_FAILURE
, "%s", comperr
);
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. */
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;