regexp.c 4.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* 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>
23 24
#include <grub/err.h>
#include <grub/env.h>
25
#include <grub/extcmd.h>
26
#include <grub/i18n.h>
27
#include <grub/script_sh.h>
28
#include <regex.h>
29

30 31
GRUB_MOD_LICENSE ("GPLv3+");

32 33 34
static const struct grub_arg_option options[] =
  {
    { "set", 's', GRUB_ARG_OPTION_REPEATABLE,
35 36 37 38 39 40
      /* 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
41 42
	 sed -e 's,\([a-z][a-z]*\),lowercase=\1,g'
	 The whole matching component is saved in VARNAME, not its number.
43
       */
44
      N_("Store matched component NUMBER in VARNAME."),
45 46 47 48
      N_("[NUMBER:]VARNAME"), ARG_TYPE_STRING },
    { 0, 0, 0, 0, 0, 0 }
  };

49 50 51 52 53 54 55 56 57 58 59 60
static grub_err_t
setvar (char *str, char *v, regmatch_t *m)
{
  char ch;
  grub_err_t err;
  ch = str[m->rm_eo];
  str[m->rm_eo] = '\0';
  err = grub_env_set (v, str + m->rm_so);
  str[m->rm_eo] = ch;
  return err;
}

61 62 63 64 65 66 67 68 69 70 71 72
static grub_err_t
set_matches (char **varnames, char *str, grub_size_t nmatches,
	     regmatch_t *matches)
{
  int i;
  char *p;
  char *q;
  grub_err_t err;
  unsigned long j;

  for (i = 0; varnames && varnames[i]; i++)
    {
73
      err = GRUB_ERR_NONE;
74 75
      p = grub_strchr (varnames[i], ':');
      if (! p)
76 77 78 79 80
	{
	  /* varname w/o index defaults to 1 */
	  if (nmatches < 2 || matches[1].rm_so == -1)
	    grub_env_unset (varnames[i]);
	  else
81
	    err = setvar (str, varnames[i], &matches[1]);
82 83 84 85 86 87 88 89 90 91 92
	}
      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
93
	    err = setvar (str, p + 1, &matches[j]);
94 95 96 97 98 99 100 101
	}

      if (err != GRUB_ERR_NONE)
	return err;
    }
  return GRUB_ERR_NONE;
}

102
static grub_err_t
103
grub_cmd_regexp (grub_extcmd_context_t ctxt, int argc, char **args)
104 105 106 107 108 109
{
  regex_t regex;
  int ret;
  grub_size_t s;
  char *comperr;
  grub_err_t err;
110
  regmatch_t *matches = 0;
111 112

  if (argc != 2)
113
    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected"));
114

115
  ret = regcomp (&regex, args[0], REG_EXTENDED);
116 117 118
  if (ret)
    goto fail;

119 120 121 122 123
  matches = grub_zalloc (sizeof (*matches) * (regex.re_nsub + 1));
  if (! matches)
    goto fail;

  ret = regexec (&regex, args[1], regex.re_nsub + 1, matches, 0);
124 125
  if (!ret)
    {
126 127
      err = set_matches (ctxt->state[0].args, args[1],
			 regex.re_nsub + 1, matches);
128
      regfree (&regex);
129
      grub_free (matches);
130
      return err;
131 132 133
    }

 fail:
134
  grub_free (matches);
135 136 137 138 139 140 141 142 143 144 145 146 147 148
  s = regerror (ret, &regex, 0, 0);
  comperr = grub_malloc (s);
  if (!comperr)
    {
      regfree (&regex);
      return grub_errno;
    }
  regerror (ret, &regex, comperr, s);
  err = grub_error (GRUB_ERR_TEST_FAILURE, "%s", comperr);
  regfree (&regex);
  grub_free (comperr);
  return err;
}

149
static grub_extcmd_t cmd;
150 151 152

GRUB_MOD_INIT(regexp)
{
153 154 155 156 157
  cmd = grub_register_extcmd ("regexp", grub_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"),
158
			      N_("Test if REGEXP matches STRING."), options);
159 160

  /* Setup GRUB script wildcard translator.  */
BVK Chaitanya's avatar
BVK Chaitanya committed
161
  grub_wildcard_translator = &grub_filename_translator;
162 163 164 165
}

GRUB_MOD_FINI(regexp)
{
166
  grub_unregister_extcmd (cmd);
BVK Chaitanya's avatar
BVK Chaitanya committed
167
  grub_wildcard_translator = 0;
168
}