regex_match.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_Type(regex_arg) != NCDVALUE_STRING) {
  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. o->succeeded = (regexec(&preg, o->input, MAX_MATCHES, o->matches, 0) == 0);
  88. // free regex
  89. regfree(&preg);
  90. // signal up
  91. NCDModuleInst_Backend_Up(o->i);
  92. return;
  93. fail1:
  94. free(o);
  95. fail0:
  96. NCDModuleInst_Backend_SetError(i);
  97. NCDModuleInst_Backend_Dead(i);
  98. }
  99. static void func_die (void *vo)
  100. {
  101. struct instance *o = vo;
  102. NCDModuleInst *i = o->i;
  103. // free instance
  104. free(o);
  105. NCDModuleInst_Backend_Dead(i);
  106. }
  107. static int func_getvar (void *vo, const char *name, NCDValue *out)
  108. {
  109. struct instance *o = vo;
  110. if (!strcmp(name, "succeeded")) {
  111. if (!NCDValue_InitString(out, (o->succeeded ? "true" : "false"))) {
  112. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  113. return 0;
  114. }
  115. return 1;
  116. }
  117. size_t pos;
  118. uintmax_t n;
  119. if ((pos = string_begins_with(name, "match")) && parse_unsigned_integer(name + pos, &n)) {
  120. if (o->succeeded && n < MAX_MATCHES && o->matches[n].rm_so >= 0) {
  121. regmatch_t *m = &o->matches[n];
  122. ASSERT(m->rm_so <= strlen(o->input))
  123. ASSERT(m->rm_eo >= m->rm_so)
  124. ASSERT(m->rm_eo <= strlen(o->input))
  125. size_t len = m->rm_eo - m->rm_so;
  126. char *str = malloc(len + 1);
  127. if (!str) {
  128. ModuleLog(o->i, BLOG_ERROR, "malloc failed");
  129. return 0;
  130. }
  131. memcpy(str, o->input + m->rm_so, len);
  132. str[len] = '\0';
  133. if (!NCDValue_InitString(out, str)) {
  134. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  135. free(str);
  136. return 0;
  137. }
  138. free(str);
  139. return 1;
  140. }
  141. }
  142. return 0;
  143. }
  144. static const struct NCDModule modules[] = {
  145. {
  146. .type = "regex_match",
  147. .func_new = func_new,
  148. .func_die = func_die,
  149. .func_getvar = func_getvar
  150. }, {
  151. .type = NULL
  152. }
  153. };
  154. const struct NCDModuleGroup ncdmodule_regex_match = {
  155. .modules = modules
  156. };