]> git.ipfire.org Git - thirdparty/grub.git/commitdiff
regexp sets matches to $match*
authorBVK Chaitanya <bvk@dbook>
Tue, 3 Aug 2010 11:03:36 +0000 (16:33 +0530)
committerBVK Chaitanya <bvk@dbook>
Tue, 3 Aug 2010 11:03:36 +0000 (16:33 +0530)
commands/regexp.c

index e8e8243b5e6eefc65ead4bc20c5f15ecb7b7b35b..2e84c3b0dda9967e95b71767d7ea5ebaa1a86779 100644 (file)
@@ -20,6 +20,8 @@
 #include <grub/dl.h>
 #include <grub/misc.h>
 #include <grub/mm.h>
+#include <grub/err.h>
+#include <grub/env.h>
 #include <grub/command.h>
 #include <grub/i18n.h>
 #include <regex.h>
@@ -29,28 +31,50 @@ grub_cmd_regexp (grub_command_t cmd __attribute__ ((unused)),
                 int argc, char **args)
 {
   int argn = 0;
-  int matches = 0;
   regex_t regex;
   int ret;
   grub_size_t s;
   char *comperr;
   grub_err_t err;
+  regmatch_t *matches = 0;
 
   if (argc != 2)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "2 arguments expected");
 
-  ret = regcomp (&regex, args[0], RE_SYNTAX_GNU_AWK);
+  ret = regcomp (&regex, args[0], REG_EXTENDED);
   if (ret)
     goto fail;
 
-  ret = regexec (&regex, args[1], 0, 0, 0);
+  matches = grub_zalloc (sizeof (*matches) * (regex.re_nsub + 1));
+  if (! matches)
+    goto fail;
+
+  ret = regexec (&regex, args[1], regex.re_nsub + 1, matches, 0);
   if (!ret)
     {
+      int i;
+      char ch;
+      char buf[5 + sizeof (size_t) * 3];
+
+      for (i = 0; i <= regex.re_nsub && matches[i].rm_so != -1; i++)
+       {
+         ch = args[1][matches[i].rm_eo];
+         args[1][matches[i].rm_eo] = '\0';
+
+         grub_snprintf (buf, sizeof (buf), "%s%u", "match", i);
+         if (grub_env_set (buf, args[1] + matches[i].rm_so))
+           break;
+
+         args[1][matches[i].rm_eo] = ch;
+       }
+
       regfree (&regex);
+      grub_free (matches);
       return GRUB_ERR_NONE;
     }
 
  fail:
+  grub_free (matches);
   s = regerror (ret, &regex, 0, 0);
   comperr = grub_malloc (s);
   if (!comperr)