regex_match.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * @file regex_match.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Regular expression matching module.
  32. *
  33. * Synopsis: regex_match(string input, string regex)
  34. * Variables:
  35. * succeeded - "true" or "false", indicating whether input matched regex
  36. * matchN - for N=0,1,2,..., the matching data for the N-th subexpression
  37. * (match0 = whole match)
  38. */
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <regex.h>
  42. #include <misc/string_begins_with.h>
  43. #include <misc/parse_number.h>
  44. #include <ncd/NCDModule.h>
  45. #include <generated/blog_channel_ncd_regex_match.h>
  46. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  47. #define MAX_MATCHES 64
  48. struct instance {
  49. NCDModuleInst *i;
  50. char *input;
  51. int succeeded;
  52. int num_matches;
  53. regmatch_t matches[MAX_MATCHES];
  54. };
  55. static void func_new (NCDModuleInst *i)
  56. {
  57. // allocate instance
  58. struct instance *o = malloc(sizeof(*o));
  59. if (!o) {
  60. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  61. goto fail0;
  62. }
  63. NCDModuleInst_Backend_SetUser(i, o);
  64. // init arguments
  65. o->i = i;
  66. // read arguments
  67. NCDValue *input_arg;
  68. NCDValue *regex_arg;
  69. if (!NCDValue_ListRead(o->i->args, 2, &input_arg, &regex_arg)) {
  70. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  71. goto fail1;
  72. }
  73. if (NCDValue_Type(input_arg) != NCDVALUE_STRING || !NCDValue_IsStringNoNulls(regex_arg)) {
  74. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  75. goto fail1;
  76. }
  77. o->input = NCDValue_StringValue(input_arg);
  78. char *regex = NCDValue_StringValue(regex_arg);
  79. // compile regex
  80. regex_t preg;
  81. int ret;
  82. if ((ret = regcomp(&preg, regex, REG_EXTENDED)) != 0) {
  83. ModuleLog(o->i, BLOG_ERROR, "regcomp failed (error=%d)", ret);
  84. goto fail1;
  85. }
  86. // execute match
  87. if (NCDValue_StringHasNulls(input_arg)) {
  88. ModuleLog(o->i, BLOG_ERROR, "string has nulls");
  89. o->succeeded = 0;
  90. } else {
  91. o->succeeded = (regexec(&preg, o->input, MAX_MATCHES, o->matches, 0) == 0);
  92. }
  93. // free regex
  94. regfree(&preg);
  95. // signal up
  96. NCDModuleInst_Backend_Up(o->i);
  97. return;
  98. fail1:
  99. free(o);
  100. fail0:
  101. NCDModuleInst_Backend_SetError(i);
  102. NCDModuleInst_Backend_Dead(i);
  103. }
  104. static void func_die (void *vo)
  105. {
  106. struct instance *o = vo;
  107. NCDModuleInst *i = o->i;
  108. // free instance
  109. free(o);
  110. NCDModuleInst_Backend_Dead(i);
  111. }
  112. static int func_getvar (void *vo, const char *name, NCDValue *out)
  113. {
  114. struct instance *o = vo;
  115. if (!strcmp(name, "succeeded")) {
  116. if (!NCDValue_InitString(out, (o->succeeded ? "true" : "false"))) {
  117. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  118. return 0;
  119. }
  120. return 1;
  121. }
  122. size_t pos;
  123. uintmax_t n;
  124. if ((pos = string_begins_with(name, "match")) && parse_unsigned_integer(name + pos, &n)) {
  125. if (o->succeeded && n < MAX_MATCHES && o->matches[n].rm_so >= 0) {
  126. regmatch_t *m = &o->matches[n];
  127. ASSERT(m->rm_so <= strlen(o->input))
  128. ASSERT(m->rm_eo >= m->rm_so)
  129. ASSERT(m->rm_eo <= strlen(o->input))
  130. size_t len = m->rm_eo - m->rm_so;
  131. char *str = malloc(len + 1);
  132. if (!str) {
  133. ModuleLog(o->i, BLOG_ERROR, "malloc failed");
  134. return 0;
  135. }
  136. memcpy(str, o->input + m->rm_so, len);
  137. str[len] = '\0';
  138. if (!NCDValue_InitString(out, str)) {
  139. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  140. free(str);
  141. return 0;
  142. }
  143. free(str);
  144. return 1;
  145. }
  146. }
  147. return 0;
  148. }
  149. static const struct NCDModule modules[] = {
  150. {
  151. .type = "regex_match",
  152. .func_new = func_new,
  153. .func_die = func_die,
  154. .func_getvar = func_getvar
  155. }, {
  156. .type = NULL
  157. }
  158. };
  159. const struct NCDModuleGroup ncdmodule_regex_match = {
  160. .modules = modules
  161. };