fty_enum.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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_enum.c,v 1.22 2007/10/13 19:32:26 tom Exp $")
  35. typedef struct
  36. {
  37. char **kwds;
  38. int count;
  39. bool checkcase;
  40. bool checkunique;
  41. }
  42. enumARG;
  43. /*---------------------------------------------------------------------------
  44. | Facility : libnform
  45. | Function : static void *Make_Enum_Type( va_list * ap )
  46. |
  47. | Description : Allocate structure for enumeration type argument.
  48. |
  49. | Return Values : Pointer to argument structure or NULL on error
  50. +--------------------------------------------------------------------------*/
  51. static void *
  52. Make_Enum_Type(va_list *ap)
  53. {
  54. enumARG *argp = typeMalloc(enumARG, 1);
  55. if (argp)
  56. {
  57. int cnt = 0;
  58. char **kp = (char **)0;
  59. int ccase, cunique;
  60. T((T_CREATE("enumARG %p"), argp));
  61. argp->kwds = va_arg(*ap, char **);
  62. ccase = va_arg(*ap, int);
  63. cunique = va_arg(*ap, int);
  64. argp->checkcase = ccase ? TRUE : FALSE;
  65. argp->checkunique = cunique ? TRUE : FALSE;
  66. kp = argp->kwds;
  67. while (kp && (*kp++))
  68. cnt++;
  69. argp->count = cnt;
  70. }
  71. return (void *)argp;
  72. }
  73. /*---------------------------------------------------------------------------
  74. | Facility : libnform
  75. | Function : static void *Copy_Enum_Type( const void * argp )
  76. |
  77. | Description : Copy structure for enumeration type argument.
  78. |
  79. | Return Values : Pointer to argument structure or NULL on error.
  80. +--------------------------------------------------------------------------*/
  81. static void *
  82. Copy_Enum_Type(const void *argp)
  83. {
  84. enumARG *result = (enumARG *)0;
  85. if (argp)
  86. {
  87. const enumARG *ap = (const enumARG *)argp;
  88. result = typeMalloc(enumARG, 1);
  89. if (result)
  90. {
  91. T((T_CREATE("enumARG %p"), result));
  92. *result = *ap;
  93. }
  94. }
  95. return (void *)result;
  96. }
  97. /*---------------------------------------------------------------------------
  98. | Facility : libnform
  99. | Function : static void Free_Enum_Type( void * argp )
  100. |
  101. | Description : Free structure for enumeration type argument.
  102. |
  103. | Return Values : -
  104. +--------------------------------------------------------------------------*/
  105. static void
  106. Free_Enum_Type(void *argp)
  107. {
  108. if (argp)
  109. free(argp);
  110. }
  111. #define SKIP_SPACE(x) while(((*(x))!='\0') && (is_blank(*(x)))) (x)++
  112. #define NOMATCH 0
  113. #define PARTIAL 1
  114. #define EXACT 2
  115. /*---------------------------------------------------------------------------
  116. | Facility : libnform
  117. | Function : static int Compare(const unsigned char * s,
  118. | const unsigned char * buf,
  119. | bool ccase )
  120. |
  121. | Description : Check whether or not the text in 'buf' matches the
  122. | text in 's', at least partial.
  123. |
  124. | Return Values : NOMATCH - buffer doesn't match
  125. | PARTIAL - buffer matches partially
  126. | EXACT - buffer matches exactly
  127. +--------------------------------------------------------------------------*/
  128. static int
  129. Compare(const unsigned char *s, const unsigned char *buf,
  130. bool ccase)
  131. {
  132. SKIP_SPACE(buf); /* Skip leading spaces in both texts */
  133. SKIP_SPACE(s);
  134. if (*buf == '\0')
  135. {
  136. return (((*s) != '\0') ? NOMATCH : EXACT);
  137. }
  138. else
  139. {
  140. if (ccase)
  141. {
  142. while (*s++ == *buf)
  143. {
  144. if (*buf++ == '\0')
  145. return EXACT;
  146. }
  147. }
  148. else
  149. {
  150. while (toupper(*s++) == toupper(*buf))
  151. {
  152. if (*buf++ == '\0')
  153. return EXACT;
  154. }
  155. }
  156. }
  157. /* At this location buf points to the first character where it no longer
  158. matches with s. So if only blanks are following, we have a partial
  159. match otherwise there is no match */
  160. SKIP_SPACE(buf);
  161. if (*buf)
  162. return NOMATCH;
  163. /* If it happens that the reference buffer is at its end, the partial
  164. match is actually an exact match. */
  165. return ((s[-1] != '\0') ? PARTIAL : EXACT);
  166. }
  167. /*---------------------------------------------------------------------------
  168. | Facility : libnform
  169. | Function : static bool Check_Enum_Field(
  170. | FIELD * field,
  171. | const void * argp)
  172. |
  173. | Description : Validate buffer content to be a valid enumeration value
  174. |
  175. | Return Values : TRUE - field is valid
  176. | FALSE - field is invalid
  177. +--------------------------------------------------------------------------*/
  178. static bool
  179. Check_Enum_Field(FIELD *field, const void *argp)
  180. {
  181. char **kwds = ((const enumARG *)argp)->kwds;
  182. bool ccase = ((const enumARG *)argp)->checkcase;
  183. bool unique = ((const enumARG *)argp)->checkunique;
  184. unsigned char *bp = (unsigned char *)field_buffer(field, 0);
  185. char *s, *t, *p;
  186. int res;
  187. while (kwds && (s = (*kwds++)))
  188. {
  189. if ((res = Compare((unsigned char *)s, bp, ccase)) != NOMATCH)
  190. {
  191. p = t = s; /* t is at least a partial match */
  192. if ((unique && res != EXACT))
  193. {
  194. while (kwds && (p = *kwds++))
  195. {
  196. if ((res = Compare((unsigned char *)p, bp, ccase)) != NOMATCH)
  197. {
  198. if (res == EXACT)
  199. {
  200. t = p;
  201. break;
  202. }
  203. else
  204. t = (char *)0;
  205. }
  206. }
  207. }
  208. if (t)
  209. {
  210. set_field_buffer(field, 0, t);
  211. return TRUE;
  212. }
  213. if (!p)
  214. break;
  215. }
  216. }
  217. return FALSE;
  218. }
  219. static const char *dummy[] =
  220. {(char *)0};
  221. /*---------------------------------------------------------------------------
  222. | Facility : libnform
  223. | Function : static bool Next_Enum(FIELD * field,
  224. | const void * argp)
  225. |
  226. | Description : Check for the next enumeration value
  227. |
  228. | Return Values : TRUE - next value found and loaded
  229. | FALSE - no next value loaded
  230. +--------------------------------------------------------------------------*/
  231. static bool
  232. Next_Enum(FIELD *field, const void *argp)
  233. {
  234. const enumARG *args = (const enumARG *)argp;
  235. char **kwds = args->kwds;
  236. bool ccase = args->checkcase;
  237. int cnt = args->count;
  238. unsigned char *bp = (unsigned char *)field_buffer(field, 0);
  239. if (kwds)
  240. {
  241. while (cnt--)
  242. {
  243. if (Compare((unsigned char *)(*kwds++), bp, ccase) == EXACT)
  244. break;
  245. }
  246. if (cnt <= 0)
  247. kwds = args->kwds;
  248. if ((cnt >= 0) || (Compare((const unsigned char *)dummy, bp, ccase) == EXACT))
  249. {
  250. set_field_buffer(field, 0, *kwds);
  251. return TRUE;
  252. }
  253. }
  254. return FALSE;
  255. }
  256. /*---------------------------------------------------------------------------
  257. | Facility : libnform
  258. | Function : static bool Previous_Enum(
  259. | FIELD * field,
  260. | const void * argp)
  261. |
  262. | Description : Check for the previous enumeration value
  263. |
  264. | Return Values : TRUE - previous value found and loaded
  265. | FALSE - no previous value loaded
  266. +--------------------------------------------------------------------------*/
  267. static bool
  268. Previous_Enum(FIELD *field, const void *argp)
  269. {
  270. const enumARG *args = (const enumARG *)argp;
  271. int cnt = args->count;
  272. char **kwds = &args->kwds[cnt - 1];
  273. bool ccase = args->checkcase;
  274. unsigned char *bp = (unsigned char *)field_buffer(field, 0);
  275. if (kwds)
  276. {
  277. while (cnt--)
  278. {
  279. if (Compare((unsigned char *)(*kwds--), bp, ccase) == EXACT)
  280. break;
  281. }
  282. if (cnt <= 0)
  283. kwds = &args->kwds[args->count - 1];
  284. if ((cnt >= 0) || (Compare((const unsigned char *)dummy, bp, ccase) == EXACT))
  285. {
  286. set_field_buffer(field, 0, *kwds);
  287. return TRUE;
  288. }
  289. }
  290. return FALSE;
  291. }
  292. static FIELDTYPE typeENUM =
  293. {
  294. _HAS_ARGS | _HAS_CHOICE | _RESIDENT,
  295. 1, /* this is mutable, so we can't be const */
  296. (FIELDTYPE *)0,
  297. (FIELDTYPE *)0,
  298. Make_Enum_Type,
  299. Copy_Enum_Type,
  300. Free_Enum_Type,
  301. Check_Enum_Field,
  302. NULL,
  303. Next_Enum,
  304. Previous_Enum
  305. };
  306. NCURSES_EXPORT_VAR(FIELDTYPE *)
  307. TYPE_ENUM = &typeENUM;
  308. /* fty_enum.c ends here */