PageRenderTime 62ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/ncurses/form/fty_regex.c

https://github.com/okuoku/freebsd-head
C | 306 lines | 201 code | 35 blank | 70 comment | 23 complexity | cdc8e00bc82dfd51f6469dcd47e60351 MD5 | raw file
  1. /****************************************************************************
  2. * Copyright (c) 1998-2006,2007 Free Software Foundation, Inc. *
  3. * *
  4. * Permission is hereby granted, free of charge, to any person obtaining a *
  5. * copy of this software and associated documentation files (the *
  6. * "Software"), to deal in the Software without restriction, including *
  7. * without limitation the rights to use, copy, modify, merge, publish, *
  8. * distribute, distribute with modifications, sublicense, and/or sell *
  9. * copies of the Software, and to permit persons to whom the Software is *
  10. * furnished to do so, subject to the following conditions: *
  11. * *
  12. * The above copyright notice and this permission notice shall be included *
  13. * in all copies or substantial portions of the Software. *
  14. * *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
  18. * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  19. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  20. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
  21. * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
  22. * *
  23. * Except as contained in this notice, the name(s) of the above copyright *
  24. * holders shall not be used in advertising or otherwise to promote the *
  25. * sale, use or other dealings in this Software without prior written *
  26. * authorization. *
  27. ****************************************************************************/
  28. /***************************************************************************
  29. * *
  30. * Author : Juergen Pfeifer *
  31. * *
  32. ***************************************************************************/
  33. #include "form.priv.h"
  34. MODULE_ID("$Id: fty_regex.c,v 1.21 2007/10/13 19:33:50 tom Exp $")
  35. #if HAVE_REGEX_H_FUNCS /* We prefer POSIX regex */
  36. #include <regex.h>
  37. typedef struct
  38. {
  39. regex_t *pRegExp;
  40. unsigned long *refCount;
  41. }
  42. RegExp_Arg;
  43. #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
  44. #undef RETURN
  45. static int reg_errno;
  46. static char *
  47. RegEx_Init(char *instring)
  48. {
  49. reg_errno = 0;
  50. return instring;
  51. }
  52. static char *
  53. RegEx_Error(int code)
  54. {
  55. reg_errno = code;
  56. return 0;
  57. }
  58. #define INIT register char *sp = RegEx_Init(instring);
  59. #define GETC() (*sp++)
  60. #define PEEKC() (*sp)
  61. #define UNGETC(c) (--sp)
  62. #define RETURN(c) return(c)
  63. #define ERROR(c) return RegEx_Error(c)
  64. #if HAVE_REGEXP_H_FUNCS
  65. #include <regexp.h>
  66. #else
  67. #include <regexpr.h>
  68. #endif
  69. typedef struct
  70. {
  71. char *compiled_expression;
  72. unsigned long *refCount;
  73. }
  74. RegExp_Arg;
  75. /* Maximum Length we allow for a compiled regular expression */
  76. #define MAX_RX_LEN (2048)
  77. #define RX_INCREMENT (256)
  78. #endif
  79. /*---------------------------------------------------------------------------
  80. | Facility : libnform
  81. | Function : static void *Make_RegularExpression_Type(va_list * ap)
  82. |
  83. | Description : Allocate structure for regex type argument.
  84. |
  85. | Return Values : Pointer to argument structure or NULL on error
  86. +--------------------------------------------------------------------------*/
  87. static void *
  88. Make_RegularExpression_Type(va_list *ap)
  89. {
  90. #if HAVE_REGEX_H_FUNCS
  91. char *rx = va_arg(*ap, char *);
  92. RegExp_Arg *preg;
  93. preg = typeMalloc(RegExp_Arg, 1);
  94. if (preg)
  95. {
  96. T((T_CREATE("RegExp_Arg %p"), preg));
  97. if (((preg->pRegExp = typeMalloc(regex_t, 1)) != 0)
  98. && !regcomp(preg->pRegExp, rx,
  99. (REG_EXTENDED | REG_NOSUB | REG_NEWLINE)))
  100. {
  101. T((T_CREATE("regex_t %p"), preg->pRegExp));
  102. preg->refCount = typeMalloc(unsigned long, 1);
  103. *(preg->refCount) = 1;
  104. }
  105. else
  106. {
  107. if (preg->pRegExp)
  108. free(preg->pRegExp);
  109. free(preg);
  110. preg = (RegExp_Arg *)0;
  111. }
  112. }
  113. return ((void *)preg);
  114. #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
  115. char *rx = va_arg(*ap, char *);
  116. RegExp_Arg *pArg;
  117. pArg = typeMalloc(RegExp_Arg, 1);
  118. if (pArg)
  119. {
  120. int blen = RX_INCREMENT;
  121. T((T_CREATE("RegExp_Arg %p"), pArg));
  122. pArg->compiled_expression = NULL;
  123. pArg->refCount = typeMalloc(unsigned long, 1);
  124. *(pArg->refCount) = 1;
  125. do
  126. {
  127. char *buf = typeMalloc(char, blen);
  128. if (buf)
  129. {
  130. #if HAVE_REGEXP_H_FUNCS
  131. char *last_pos = compile(rx, buf, &buf[blen], '\0');
  132. #else /* HAVE_REGEXPR_H_FUNCS */
  133. char *last_pos = compile(rx, buf, &buf[blen]);
  134. #endif
  135. if (reg_errno)
  136. {
  137. free(buf);
  138. if (reg_errno == 50)
  139. blen += RX_INCREMENT;
  140. else
  141. {
  142. free(pArg);
  143. pArg = NULL;
  144. break;
  145. }
  146. }
  147. else
  148. {
  149. pArg->compiled_expression = buf;
  150. break;
  151. }
  152. }
  153. }
  154. while (blen <= MAX_RX_LEN);
  155. }
  156. if (pArg && !pArg->compiled_expression)
  157. {
  158. free(pArg);
  159. pArg = NULL;
  160. }
  161. return (void *)pArg;
  162. #else
  163. return 0;
  164. #endif
  165. }
  166. /*---------------------------------------------------------------------------
  167. | Facility : libnform
  168. | Function : static void *Copy_RegularExpression_Type(
  169. | const void * argp)
  170. |
  171. | Description : Copy structure for regex type argument.
  172. |
  173. | Return Values : Pointer to argument structure or NULL on error.
  174. +--------------------------------------------------------------------------*/
  175. static void *
  176. Copy_RegularExpression_Type(const void *argp)
  177. {
  178. #if (HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS)
  179. const RegExp_Arg *ap = (const RegExp_Arg *)argp;
  180. const RegExp_Arg *result = (const RegExp_Arg *)0;
  181. if (ap)
  182. {
  183. *(ap->refCount) += 1;
  184. result = ap;
  185. }
  186. return (void *)result;
  187. #else
  188. return 0;
  189. #endif
  190. }
  191. /*---------------------------------------------------------------------------
  192. | Facility : libnform
  193. | Function : static void Free_RegularExpression_Type(void * argp)
  194. |
  195. | Description : Free structure for regex type argument.
  196. |
  197. | Return Values : -
  198. +--------------------------------------------------------------------------*/
  199. static void
  200. Free_RegularExpression_Type(void *argp)
  201. {
  202. #if HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
  203. RegExp_Arg *ap = (RegExp_Arg *)argp;
  204. if (ap)
  205. {
  206. if (--(*(ap->refCount)) == 0)
  207. {
  208. #if HAVE_REGEX_H_FUNCS
  209. if (ap->pRegExp)
  210. {
  211. free(ap->refCount);
  212. regfree(ap->pRegExp);
  213. }
  214. #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
  215. if (ap->compiled_expression)
  216. {
  217. free(ap->refCount);
  218. free(ap->compiled_expression);
  219. }
  220. #endif
  221. free(ap);
  222. }
  223. }
  224. #endif
  225. }
  226. /*---------------------------------------------------------------------------
  227. | Facility : libnform
  228. | Function : static bool Check_RegularExpression_Field(
  229. | FIELD * field,
  230. | const void * argp)
  231. |
  232. | Description : Validate buffer content to be a valid regular expression
  233. |
  234. | Return Values : TRUE - field is valid
  235. | FALSE - field is invalid
  236. +--------------------------------------------------------------------------*/
  237. static bool
  238. Check_RegularExpression_Field(FIELD *field, const void *argp)
  239. {
  240. bool match = FALSE;
  241. #if HAVE_REGEX_H_FUNCS
  242. const RegExp_Arg *ap = (const RegExp_Arg *)argp;
  243. if (ap && ap->pRegExp)
  244. match = (regexec(ap->pRegExp, field_buffer(field, 0), 0, NULL, 0)
  245. ? FALSE
  246. : TRUE);
  247. #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
  248. RegExp_Arg *ap = (RegExp_Arg *)argp;
  249. if (ap && ap->compiled_expression)
  250. match = (step(field_buffer(field, 0), ap->compiled_expression)
  251. ? TRUE
  252. : FALSE);
  253. #endif
  254. return match;
  255. }
  256. static FIELDTYPE typeREGEXP =
  257. {
  258. _HAS_ARGS | _RESIDENT,
  259. 1, /* this is mutable, so we can't be const */
  260. (FIELDTYPE *)0,
  261. (FIELDTYPE *)0,
  262. Make_RegularExpression_Type,
  263. Copy_RegularExpression_Type,
  264. Free_RegularExpression_Type,
  265. Check_RegularExpression_Field,
  266. NULL,
  267. NULL,
  268. NULL
  269. };
  270. NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_REGEXP = &typeREGEXP;
  271. /* fty_regex.c ends here */