regexp.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * $Id$
  3. *
  4. * Regular expression functions
  5. *
  6. * Copyright (C) 2003 Juha Heinanen
  7. *
  8. * This file is part of Kamailio, a free SIP server.
  9. *
  10. * Kamailio is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * Kamailio is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. */
  25. /*!
  26. * \file
  27. * \brief Regular Expression functions
  28. */
  29. #include <sys/types.h>
  30. #include <string.h>
  31. #include <regex.h>
  32. #include <ctype.h>
  33. #include "regexp.h"
  34. #include "../../dprint.h"
  35. /*! \brief Replace in replacement tokens \\d with substrings of string pointed by
  36. * pmatch.
  37. */
  38. int replace(regmatch_t* pmatch, char* string, char* replacement, str* result)
  39. {
  40. int len, i, j, digit, size;
  41. len = strlen(replacement);
  42. j = 0;
  43. for (i = 0; i < len; i++) {
  44. if (replacement[i] == '\\') {
  45. if (i < len - 1) {
  46. if (isdigit((unsigned char)replacement[i+1])) {
  47. digit = replacement[i+1] - '0';
  48. if (pmatch[digit].rm_so != -1) {
  49. size = pmatch[digit].rm_eo - pmatch[digit].rm_so;
  50. if (j + size < result->len) {
  51. memcpy(&(result->s[j]), string+pmatch[digit].rm_so, size);
  52. j = j + size;
  53. } else {
  54. return -1;
  55. }
  56. } else {
  57. return -2;
  58. }
  59. i = i + 1;
  60. continue;
  61. } else {
  62. i = i + 1;
  63. }
  64. } else {
  65. return -3;
  66. }
  67. }
  68. if (j + 1 < result->len) {
  69. result->s[j] = replacement[i];
  70. j = j + 1;
  71. } else {
  72. return -4;
  73. }
  74. }
  75. result->len = j;
  76. return 1;
  77. }
  78. /*! \brief Match pattern against string and store result in pmatch */
  79. int reg_match(char *pattern, char *string, regmatch_t *pmatch)
  80. {
  81. regex_t preg;
  82. if (regcomp(&preg, pattern, REG_EXTENDED | REG_NEWLINE)) {
  83. return -1;
  84. }
  85. if (preg.re_nsub > MAX_MATCH) {
  86. regfree(&preg);
  87. return -2;
  88. }
  89. if (regexec(&preg, string, MAX_MATCH, pmatch, 0)) {
  90. regfree(&preg);
  91. return -3;
  92. }
  93. regfree(&preg);
  94. return 0;
  95. }
  96. /*! \brief Match pattern against string and, if match succeeds, and replace string
  97. * with replacement substituting tokens \\d with matched substrings.
  98. */
  99. int reg_replace(char *pattern, char *replacement, char *string, str *result)
  100. {
  101. regmatch_t pmatch[MAX_MATCH];
  102. LM_DBG("pattern: '%s', replacement: '%s', string: '%s'\n",
  103. pattern, replacement, string);
  104. if (reg_match(pattern, string, &(pmatch[0]))) {
  105. return -1;
  106. }
  107. return replace(&pmatch[0], string, replacement, result);
  108. }