fty_regex.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * THIS CODE IS SPECIFICALLY EXEMPTED FROM THE NCURSES PACKAGE COPYRIGHT.
  3. * You may freely copy it for use as a template for your own field types.
  4. * If you develop a field type that might be of general use, please send
  5. * it back to the ncurses maintainers for inclusion in the next version.
  6. */
  7. /***************************************************************************
  8. * *
  9. * Author : Juergen Pfeifer, juergen.pfeifer@gmx.net *
  10. * *
  11. ***************************************************************************/
  12. #include "form.priv.h"
  13. MODULE_ID("$Id$")
  14. #if HAVE_REGEX_H_FUNCS /* We prefer POSIX regex */
  15. #include <regex.h>
  16. typedef struct
  17. {
  18. regex_t *pRegExp;
  19. unsigned long *refCount;
  20. } RegExp_Arg;
  21. #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
  22. #undef RETURN
  23. static int reg_errno;
  24. static char *RegEx_Init(char *instring)
  25. {
  26. reg_errno = 0;
  27. return instring;
  28. }
  29. static char *RegEx_Error(int code)
  30. {
  31. reg_errno = code;
  32. return 0;
  33. }
  34. #define INIT char *sp = RegEx_Init(instring);
  35. #define GETC() (*sp++)
  36. #define PEEKC() (*sp)
  37. #define UNGETC(c) (--sp)
  38. #define RETURN(c) return(c)
  39. #define ERROR(c) return RegEx_Error(c)
  40. #if HAVE_REGEXP_H_FUNCS
  41. #include <regexp.h>
  42. #else
  43. #include <regexpr.h>
  44. #endif
  45. typedef struct
  46. {
  47. char *compiled_expression;
  48. unsigned long *refCount;
  49. } RegExp_Arg;
  50. /* Maximum Length we allow for a compiled regular expression */
  51. #define MAX_RX_LEN (2048)
  52. #define RX_INCREMENT (256)
  53. #endif
  54. /*---------------------------------------------------------------------------
  55. | Facility : libnform
  56. | Function : static void *Make_RegularExpression_Type(va_list * ap)
  57. |
  58. | Description : Allocate structure for regex type argument.
  59. |
  60. | Return Values : Pointer to argument structure or NULL on error
  61. +--------------------------------------------------------------------------*/
  62. static void *Make_RegularExpression_Type(va_list * ap)
  63. {
  64. #if HAVE_REGEX_H_FUNCS
  65. char *rx = va_arg(*ap,char *);
  66. RegExp_Arg *preg;
  67. preg = (RegExp_Arg*)malloc(sizeof(RegExp_Arg));
  68. if (preg)
  69. {
  70. if (((preg->pRegExp = (regex_t*)malloc(sizeof(regex_t))) != (regex_t*)0)
  71. && !regcomp(preg->pRegExp,rx,
  72. (REG_EXTENDED | REG_NOSUB | REG_NEWLINE) ))
  73. {
  74. preg->refCount = (unsigned long *)malloc(sizeof(unsigned long));
  75. *(preg->refCount) = 1;
  76. }
  77. else
  78. {
  79. if (preg->pRegExp)
  80. free(preg->pRegExp);
  81. free(preg);
  82. preg = (RegExp_Arg*)0;
  83. }
  84. }
  85. return((void *)preg);
  86. #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
  87. char *rx = va_arg(*ap,char *);
  88. RegExp_Arg *pArg;
  89. pArg = (RegExp_Arg *)malloc(sizeof(RegExp_Arg));
  90. if (pArg)
  91. {
  92. int blen = RX_INCREMENT;
  93. pArg->compiled_expression = NULL;
  94. pArg->refCount = (unsigned long *)malloc(sizeof(unsigned long));
  95. *(pArg->refCount) = 1;
  96. do {
  97. char *buf = (char *)malloc(blen);
  98. if (buf)
  99. {
  100. #if HAVE_REGEXP_H_FUNCS
  101. char *last_pos = compile (rx, buf, &buf[blen], '\0');
  102. #else /* HAVE_REGEXPR_H_FUNCS */
  103. char *last_pos = compile (rx, buf, &buf[blen]);
  104. #endif
  105. if (reg_errno)
  106. {
  107. free(buf);
  108. if (reg_errno==50)
  109. blen += RX_INCREMENT;
  110. else
  111. {
  112. free(pArg);
  113. pArg = NULL;
  114. break;
  115. }
  116. }
  117. else
  118. {
  119. pArg->compiled_expression = buf;
  120. break;
  121. }
  122. }
  123. } while( blen <= MAX_RX_LEN );
  124. }
  125. if (pArg && !pArg->compiled_expression)
  126. {
  127. free(pArg);
  128. pArg = NULL;
  129. }
  130. return (void *)pArg;
  131. #else
  132. ap=0; /* Silence unused parameter warning. */
  133. return 0;
  134. #endif
  135. }
  136. /*---------------------------------------------------------------------------
  137. | Facility : libnform
  138. | Function : static void *Copy_RegularExpression_Type(
  139. | const void * argp)
  140. |
  141. | Description : Copy structure for regex type argument.
  142. |
  143. | Return Values : Pointer to argument structure or NULL on error.
  144. +--------------------------------------------------------------------------*/
  145. static void *Copy_RegularExpression_Type(const void * argp)
  146. {
  147. #if (HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS)
  148. const RegExp_Arg *ap = (const RegExp_Arg *)argp;
  149. const RegExp_Arg *result = (const RegExp_Arg *)0;
  150. if (ap)
  151. {
  152. *(ap->refCount) += 1;
  153. result = ap;
  154. }
  155. return (void *)result;
  156. #else
  157. argp=0; /* Silence unused parameter warning. */
  158. return 0;
  159. #endif
  160. }
  161. /*---------------------------------------------------------------------------
  162. | Facility : libnform
  163. | Function : static void Free_RegularExpression_Type(void * argp)
  164. |
  165. | Description : Free structure for regex type argument.
  166. |
  167. | Return Values : -
  168. +--------------------------------------------------------------------------*/
  169. static void Free_RegularExpression_Type(void * argp)
  170. {
  171. #if HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
  172. RegExp_Arg *ap = (RegExp_Arg *)argp;
  173. if (ap)
  174. {
  175. if (--(*(ap->refCount)) == 0)
  176. {
  177. #if HAVE_REGEX_H_FUNCS
  178. if (ap->pRegExp)
  179. {
  180. free(ap->refCount);
  181. regfree(ap->pRegExp);
  182. }
  183. #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
  184. if (ap->compiled_expression)
  185. {
  186. free(ap->refCount);
  187. free(ap->compiled_expression);
  188. }
  189. #endif
  190. free(ap);
  191. }
  192. }
  193. #else
  194. argp=0; /* Silence unused parameter warning. */
  195. #endif
  196. }
  197. /*---------------------------------------------------------------------------
  198. | Facility : libnform
  199. | Function : static bool Check_RegularExpression_Field(
  200. | FIELD * field,
  201. | const void * argp)
  202. |
  203. | Description : Validate buffer content to be a valid regular expression
  204. |
  205. | Return Values : TRUE - field is valid
  206. | FALSE - field is invalid
  207. +--------------------------------------------------------------------------*/
  208. static bool Check_RegularExpression_Field(FIELD * field, const void * argp)
  209. {
  210. bool match = FALSE;
  211. #if HAVE_REGEX_H_FUNCS
  212. const RegExp_Arg *ap = (const RegExp_Arg*)argp;
  213. if (ap && ap->pRegExp)
  214. match = (regexec(ap->pRegExp,field_buffer(field,0),0,NULL,0) ? FALSE:TRUE);
  215. #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
  216. RegExp_Arg *ap = (RegExp_Arg *)argp;
  217. if (ap && ap->compiled_expression)
  218. match = (step(field_buffer(field,0),ap->compiled_expression) ? TRUE:FALSE);
  219. #else
  220. argp=0; /* Silence unused parameter warning. */
  221. field=0; /* Silence unused parameter warning. */
  222. #endif
  223. return match;
  224. }
  225. static FIELDTYPE typeREGEXP = {
  226. _HAS_ARGS | _RESIDENT,
  227. 1, /* this is mutable, so we can't be const */
  228. (FIELDTYPE *)0,
  229. (FIELDTYPE *)0,
  230. Make_RegularExpression_Type,
  231. Copy_RegularExpression_Type,
  232. Free_RegularExpression_Type,
  233. Check_RegularExpression_Field,
  234. NULL,
  235. NULL,
  236. NULL
  237. };
  238. FIELDTYPE* TYPE_REGEXP = &typeREGEXP;
  239. /* fty_regex.c ends here */