169 lines
4.4 KiB
C
169 lines
4.4 KiB
C
/* regexp.c -- The regexp command. */
|
||
/*
|
||
* VasEBoot -- GRand Unified Bootloader
|
||
* Copyright (C) 2005,2007 Free Software Foundation, Inc.
|
||
*
|
||
* VasEBoot 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.
|
||
*
|
||
* VasEBoot 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 VasEBoot. If not, see <http://www.gnu.org/licenses/>.
|
||
*/
|
||
|
||
#include <VasEBoot/dl.h>
|
||
#include <VasEBoot/misc.h>
|
||
#include <VasEBoot/mm.h>
|
||
#include <VasEBoot/err.h>
|
||
#include <VasEBoot/env.h>
|
||
#include <VasEBoot/extcmd.h>
|
||
#include <VasEBoot/i18n.h>
|
||
#include <VasEBoot/script_sh.h>
|
||
#include <regex.h>
|
||
|
||
VasEBoot_MOD_LICENSE ("GPLv3+");
|
||
|
||
static const struct VasEBoot_arg_option options[] =
|
||
{
|
||
{ "set", 's', VasEBoot_ARG_OPTION_REPEATABLE,
|
||
/* TRANSLATORS: in regexp you can mark some
|
||
groups with parentheses. These groups are
|
||
then numbered and you can save some of
|
||
them in variables. In other programs
|
||
those components aree often referenced with
|
||
back slash, e.g. \1. Compare
|
||
sed -e 's,\([a-z][a-z]*\),lowercase=\1,g'
|
||
The whole matching component is saved in VARNAME, not its number.
|
||
*/
|
||
N_("Store matched component NUMBER in VARNAME."),
|
||
N_("[NUMBER:]VARNAME"), ARG_TYPE_STRING },
|
||
{ 0, 0, 0, 0, 0, 0 }
|
||
};
|
||
|
||
static VasEBoot_err_t
|
||
setvar (char *str, char *v, regmatch_t *m)
|
||
{
|
||
char ch;
|
||
VasEBoot_err_t err;
|
||
ch = str[m->rm_eo];
|
||
str[m->rm_eo] = '\0';
|
||
err = VasEBoot_env_set (v, str + m->rm_so);
|
||
str[m->rm_eo] = ch;
|
||
return err;
|
||
}
|
||
|
||
static VasEBoot_err_t
|
||
set_matches (char **varnames, char *str, VasEBoot_size_t nmatches,
|
||
regmatch_t *matches)
|
||
{
|
||
int i;
|
||
char *p;
|
||
char *q;
|
||
VasEBoot_err_t err;
|
||
unsigned long j;
|
||
|
||
for (i = 0; varnames && varnames[i]; i++)
|
||
{
|
||
err = VasEBoot_ERR_NONE;
|
||
p = VasEBoot_strchr (varnames[i], ':');
|
||
if (! p)
|
||
{
|
||
/* varname w/o index defaults to 1 */
|
||
if (nmatches < 2 || matches[1].rm_so == -1)
|
||
VasEBoot_env_unset (varnames[i]);
|
||
else
|
||
err = setvar (str, varnames[i], &matches[1]);
|
||
}
|
||
else
|
||
{
|
||
j = VasEBoot_strtoul (varnames[i], &q, 10);
|
||
if (q != p)
|
||
return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT,
|
||
"invalid variable name format %s", varnames[i]);
|
||
|
||
if (nmatches <= j || matches[j].rm_so == -1)
|
||
VasEBoot_env_unset (p + 1);
|
||
else
|
||
err = setvar (str, p + 1, &matches[j]);
|
||
}
|
||
|
||
if (err != VasEBoot_ERR_NONE)
|
||
return err;
|
||
}
|
||
return VasEBoot_ERR_NONE;
|
||
}
|
||
|
||
static VasEBoot_err_t
|
||
VasEBoot_cmd_regexp (VasEBoot_extcmd_context_t ctxt, int argc, char **args)
|
||
{
|
||
regex_t regex;
|
||
int ret;
|
||
VasEBoot_size_t s;
|
||
char *comperr;
|
||
VasEBoot_err_t err;
|
||
regmatch_t *matches = 0;
|
||
|
||
if (argc != 2)
|
||
return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, N_("two arguments expected"));
|
||
|
||
ret = regcomp (®ex, args[0], REG_EXTENDED);
|
||
if (ret)
|
||
goto fail;
|
||
|
||
matches = VasEBoot_zalloc (sizeof (*matches) * (regex.re_nsub + 1));
|
||
if (! matches)
|
||
goto fail;
|
||
|
||
ret = regexec (®ex, args[1], regex.re_nsub + 1, matches, 0);
|
||
if (!ret)
|
||
{
|
||
err = set_matches (ctxt->state[0].args, args[1],
|
||
regex.re_nsub + 1, matches);
|
||
regfree (®ex);
|
||
VasEBoot_free (matches);
|
||
return err;
|
||
}
|
||
|
||
fail:
|
||
VasEBoot_free (matches);
|
||
s = regerror (ret, ®ex, 0, 0);
|
||
comperr = VasEBoot_malloc (s);
|
||
if (!comperr)
|
||
{
|
||
regfree (®ex);
|
||
return VasEBoot_errno;
|
||
}
|
||
regerror (ret, ®ex, comperr, s);
|
||
err = VasEBoot_error (VasEBoot_ERR_TEST_FAILURE, "%s", comperr);
|
||
regfree (®ex);
|
||
VasEBoot_free (comperr);
|
||
return err;
|
||
}
|
||
|
||
static VasEBoot_extcmd_t cmd;
|
||
|
||
VasEBoot_MOD_INIT(regexp)
|
||
{
|
||
cmd = VasEBoot_register_extcmd ("regexp", VasEBoot_cmd_regexp, 0,
|
||
/* TRANSLATORS: This are two arguments. So it's
|
||
two separate units to translate and pay
|
||
attention not to reverse them. */
|
||
N_("REGEXP STRING"),
|
||
N_("Test if REGEXP matches STRING."), options);
|
||
|
||
/* Setup VasEBoot script wildcard translator. */
|
||
VasEBoot_wildcard_translator = &VasEBoot_filename_translator;
|
||
}
|
||
|
||
VasEBoot_MOD_FINI(regexp)
|
||
{
|
||
VasEBoot_unregister_extcmd (cmd);
|
||
VasEBoot_wildcard_translator = 0;
|
||
}
|