regexp.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* regexp.c -- The regexp command. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2005,2007 Free Software Foundation, Inc.
  5. *
  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.
  10. *
  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.
  15. *
  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/>.
  18. */
  19. #include <grub/dl.h>
  20. #include <grub/misc.h>
  21. #include <grub/mm.h>
  22. #include <grub/command.h>
  23. #include <grub/i18n.h>
  24. #include <regex.h>
  25. static grub_err_t
  26. grub_cmd_regexp (grub_command_t cmd __attribute__ ((unused)),
  27. int argc, char **args)
  28. {
  29. int argn = 0;
  30. int matches = 0;
  31. regex_t regex;
  32. int ret;
  33. grub_size_t s;
  34. char *comperr;
  35. grub_err_t err;
  36. if (argc != 2)
  37. return grub_error (GRUB_ERR_BAD_ARGUMENT, "2 arguments expected");
  38. ret = regcomp (&regex, args[0], RE_SYNTAX_GNU_AWK);
  39. if (ret)
  40. goto fail;
  41. ret = regexec (&regex, args[1], 0, 0, 0);
  42. if (!ret)
  43. {
  44. regfree (&regex);
  45. return GRUB_ERR_NONE;
  46. }
  47. fail:
  48. s = regerror (ret, &regex, 0, 0);
  49. comperr = grub_malloc (s);
  50. if (!comperr)
  51. {
  52. regfree (&regex);
  53. return grub_errno;
  54. }
  55. regerror (ret, &regex, comperr, s);
  56. err = grub_error (GRUB_ERR_TEST_FAILURE, "%s", comperr);
  57. regfree (&regex);
  58. grub_free (comperr);
  59. return err;
  60. }
  61. static grub_command_t cmd;
  62. GRUB_MOD_INIT(regexp)
  63. {
  64. cmd = grub_register_command ("regexp", grub_cmd_regexp,
  65. N_("REGEXP STRING"),
  66. N_("Test if REGEXP matches STRING."));
  67. }
  68. GRUB_MOD_FINI(regexp)
  69. {
  70. grub_unregister_command (cmd);
  71. }