2009-12-27 15:37:18 +00:00
|
|
|
|
/* regexp.c -- The regexp command. */
|
|
|
|
|
/*
|
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
|
|
|
|
* Copyright (C) 2005,2007 Free Software Foundation, Inc.
|
|
|
|
|
*
|
|
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <grub/dl.h>
|
|
|
|
|
#include <grub/misc.h>
|
|
|
|
|
#include <grub/mm.h>
|
2010-08-03 11:03:36 +00:00
|
|
|
|
#include <grub/err.h>
|
|
|
|
|
#include <grub/env.h>
|
2010-08-04 05:38:26 +00:00
|
|
|
|
#include <grub/extcmd.h>
|
2010-05-01 18:28:07 +00:00
|
|
|
|
#include <grub/i18n.h>
|
2010-07-29 18:02:56 +00:00
|
|
|
|
#include <grub/script_sh.h>
|
2010-03-28 20:19:06 +00:00
|
|
|
|
#include <regex.h>
|
2009-12-27 15:37:18 +00:00
|
|
|
|
|
2010-08-04 05:38:26 +00:00
|
|
|
|
static const struct grub_arg_option options[] =
|
|
|
|
|
{
|
|
|
|
|
{ "set", 's', GRUB_ARG_OPTION_REPEATABLE,
|
|
|
|
|
N_("Variable names to update with matches."),
|
|
|
|
|
N_("[NUMBER:]VARNAME"), ARG_TYPE_STRING },
|
|
|
|
|
{ 0, 0, 0, 0, 0, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static grub_err_t
|
|
|
|
|
set_matches (char **varnames, char *str, grub_size_t nmatches,
|
|
|
|
|
regmatch_t *matches)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
char ch;
|
|
|
|
|
char *p;
|
|
|
|
|
char *q;
|
|
|
|
|
grub_err_t err;
|
|
|
|
|
unsigned long j;
|
|
|
|
|
|
|
|
|
|
auto void setvar (char *v, regmatch_t *m);
|
|
|
|
|
void setvar (char *v, regmatch_t *m)
|
|
|
|
|
{
|
|
|
|
|
ch = str[m->rm_eo];
|
|
|
|
|
str[m->rm_eo] = '\0';
|
|
|
|
|
err = grub_env_set (v, str + m->rm_so);
|
|
|
|
|
str[m->rm_eo] = ch;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = 0; varnames && varnames[i]; i++)
|
|
|
|
|
{
|
|
|
|
|
if (! (p = grub_strchr (varnames[i], ':')))
|
|
|
|
|
{
|
|
|
|
|
/* varname w/o index defaults to 1 */
|
|
|
|
|
if (nmatches < 2 || matches[1].rm_so == -1)
|
|
|
|
|
grub_env_unset (varnames[i]);
|
|
|
|
|
else
|
|
|
|
|
setvar (varnames[i], &matches[1]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
j = grub_strtoul (varnames[i], &q, 10);
|
|
|
|
|
if (q != p)
|
|
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT,
|
|
|
|
|
"invalid variable name format %s", varnames[i]);
|
|
|
|
|
|
|
|
|
|
if (nmatches <= j || matches[j].rm_so == -1)
|
|
|
|
|
grub_env_unset (p + 1);
|
|
|
|
|
else
|
|
|
|
|
setvar (p + 1, &matches[j]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (err != GRUB_ERR_NONE)
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-27 15:37:18 +00:00
|
|
|
|
static grub_err_t
|
2010-08-04 05:38:26 +00:00
|
|
|
|
grub_cmd_regexp (grub_extcmd_context_t ctxt, int argc, char **args)
|
2009-12-27 15:37:18 +00:00
|
|
|
|
{
|
|
|
|
|
regex_t regex;
|
|
|
|
|
int ret;
|
|
|
|
|
grub_size_t s;
|
|
|
|
|
char *comperr;
|
|
|
|
|
grub_err_t err;
|
2010-08-03 11:03:36 +00:00
|
|
|
|
regmatch_t *matches = 0;
|
2009-12-27 15:37:18 +00:00
|
|
|
|
|
|
|
|
|
if (argc != 2)
|
|
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, "2 arguments expected");
|
|
|
|
|
|
2010-08-03 11:03:36 +00:00
|
|
|
|
ret = regcomp (®ex, args[0], REG_EXTENDED);
|
2009-12-27 15:37:18 +00:00
|
|
|
|
if (ret)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
2010-08-03 11:03:36 +00:00
|
|
|
|
matches = grub_zalloc (sizeof (*matches) * (regex.re_nsub + 1));
|
|
|
|
|
if (! matches)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
ret = regexec (®ex, args[1], regex.re_nsub + 1, matches, 0);
|
2009-12-27 15:37:18 +00:00
|
|
|
|
if (!ret)
|
|
|
|
|
{
|
2010-08-04 05:38:26 +00:00
|
|
|
|
err = set_matches (ctxt->state[0].args, args[1],
|
|
|
|
|
regex.re_nsub + 1, matches);
|
2009-12-27 15:37:18 +00:00
|
|
|
|
regfree (®ex);
|
2010-08-03 11:03:36 +00:00
|
|
|
|
grub_free (matches);
|
2010-08-04 05:38:26 +00:00
|
|
|
|
return err;
|
2009-12-27 15:37:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fail:
|
2010-08-03 11:03:36 +00:00
|
|
|
|
grub_free (matches);
|
2009-12-27 15:37:18 +00:00
|
|
|
|
s = regerror (ret, ®ex, 0, 0);
|
|
|
|
|
comperr = grub_malloc (s);
|
|
|
|
|
if (!comperr)
|
|
|
|
|
{
|
|
|
|
|
regfree (®ex);
|
|
|
|
|
return grub_errno;
|
|
|
|
|
}
|
|
|
|
|
regerror (ret, ®ex, comperr, s);
|
|
|
|
|
err = grub_error (GRUB_ERR_TEST_FAILURE, "%s", comperr);
|
|
|
|
|
regfree (®ex);
|
|
|
|
|
grub_free (comperr);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-04 05:38:26 +00:00
|
|
|
|
static grub_extcmd_t cmd;
|
2009-12-27 15:37:18 +00:00
|
|
|
|
|
|
|
|
|
GRUB_MOD_INIT(regexp)
|
|
|
|
|
{
|
2010-09-14 21:06:01 +00:00
|
|
|
|
cmd = grub_register_extcmd ("regexp", grub_cmd_regexp, 0, N_("REGEXP STRING"),
|
2010-08-04 05:38:26 +00:00
|
|
|
|
N_("Test if REGEXP matches STRING."), options);
|
2010-07-29 18:02:56 +00:00
|
|
|
|
|
|
|
|
|
/* Setup GRUB script wildcard translator. */
|
2010-09-03 15:05:23 +00:00
|
|
|
|
grub_wildcard_translator = &grub_filename_translator;
|
2009-12-27 15:37:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GRUB_MOD_FINI(regexp)
|
|
|
|
|
{
|
2010-08-04 05:38:26 +00:00
|
|
|
|
grub_unregister_extcmd (cmd);
|
2010-09-03 15:05:23 +00:00
|
|
|
|
grub_wildcard_translator = 0;
|
2009-12-27 15:37:18 +00:00
|
|
|
|
}
|