regexp.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/err.h>
  23. #include <grub/env.h>
  24. #include <grub/extcmd.h>
  25. #include <grub/i18n.h>
  26. #include <grub/script_sh.h>
  27. #include <regex.h>
  28. GRUB_MOD_LICENSE ("GPLv3+");
  29. static const struct grub_arg_option options[] =
  30. {
  31. { "set", 's', GRUB_ARG_OPTION_REPEATABLE,
  32. /* TRANSLATORS: in regexp you can mark some
  33. groups with parentheses. These groups are
  34. then numbered and you can save some of
  35. them in variables. In other programs
  36. those components aree often referenced with
  37. back slash, e.g. \1. Compare
  38. sed -e 's,\([a-z][a-z]*\),lowercase=\1,g'
  39. The whole matching component is saved in VARNAME, not its number.
  40. */
  41. N_("Store matched component NUMBER in VARNAME."),
  42. N_("[NUMBER:]VARNAME"), ARG_TYPE_STRING },
  43. { 0, 0, 0, 0, 0, 0 }
  44. };
  45. static grub_err_t
  46. setvar (char *str, char *v, regmatch_t *m)
  47. {
  48. char ch;
  49. grub_err_t err;
  50. ch = str[m->rm_eo];
  51. str[m->rm_eo] = '\0';
  52. err = grub_env_set (v, str + m->rm_so);
  53. str[m->rm_eo] = ch;
  54. return err;
  55. }
  56. static grub_err_t
  57. set_matches (char **varnames, char *str, grub_size_t nmatches,
  58. regmatch_t *matches)
  59. {
  60. int i;
  61. char *p;
  62. char *q;
  63. grub_err_t err;
  64. unsigned long j;
  65. for (i = 0; varnames && varnames[i]; i++)
  66. {
  67. err = GRUB_ERR_NONE;
  68. p = grub_strchr (varnames[i], ':');
  69. if (! p)
  70. {
  71. /* varname w/o index defaults to 1 */
  72. if (nmatches < 2 || matches[1].rm_so == -1)
  73. grub_env_unset (varnames[i]);
  74. else
  75. err = setvar (str, varnames[i], &matches[1]);
  76. }
  77. else
  78. {
  79. j = grub_strtoul (varnames[i], &q, 10);
  80. if (q != p)
  81. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  82. "invalid variable name format %s", varnames[i]);
  83. if (nmatches <= j || matches[j].rm_so == -1)
  84. grub_env_unset (p + 1);
  85. else
  86. err = setvar (str, p + 1, &matches[j]);
  87. }
  88. if (err != GRUB_ERR_NONE)
  89. return err;
  90. }
  91. return GRUB_ERR_NONE;
  92. }
  93. static grub_err_t
  94. grub_cmd_regexp (grub_extcmd_context_t ctxt, int argc, char **args)
  95. {
  96. regex_t regex;
  97. int ret;
  98. grub_size_t s;
  99. char *comperr;
  100. grub_err_t err;
  101. regmatch_t *matches = 0;
  102. if (argc != 2)
  103. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected"));
  104. ret = regcomp (&regex, args[0], REG_EXTENDED);
  105. if (ret)
  106. goto fail;
  107. matches = grub_zalloc (sizeof (*matches) * (regex.re_nsub + 1));
  108. if (! matches)
  109. goto fail;
  110. ret = regexec (&regex, args[1], regex.re_nsub + 1, matches, 0);
  111. if (!ret)
  112. {
  113. err = set_matches (ctxt->state[0].args, args[1],
  114. regex.re_nsub + 1, matches);
  115. regfree (&regex);
  116. grub_free (matches);
  117. return err;
  118. }
  119. fail:
  120. grub_free (matches);
  121. s = regerror (ret, &regex, 0, 0);
  122. comperr = grub_malloc (s);
  123. if (!comperr)
  124. {
  125. regfree (&regex);
  126. return grub_errno;
  127. }
  128. regerror (ret, &regex, comperr, s);
  129. err = grub_error (GRUB_ERR_TEST_FAILURE, "%s", comperr);
  130. regfree (&regex);
  131. grub_free (comperr);
  132. return err;
  133. }
  134. static grub_extcmd_t cmd;
  135. GRUB_MOD_INIT(regexp)
  136. {
  137. cmd = grub_register_extcmd ("regexp", grub_cmd_regexp, 0,
  138. /* TRANSLATORS: This are two arguments. So it's
  139. two separate units to translate and pay
  140. attention not to reverse them. */
  141. N_("REGEXP STRING"),
  142. N_("Test if REGEXP matches STRING."), options);
  143. /* Setup GRUB script wildcard translator. */
  144. grub_wildcard_translator = &grub_filename_translator;
  145. }
  146. GRUB_MOD_FINI(regexp)
  147. {
  148. grub_unregister_extcmd (cmd);
  149. grub_wildcard_translator = 0;
  150. }