fld_def.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /****************************************************************************
  2. * Copyright (c) 1998-2005,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. * Author: Juergen Pfeifer, 1995,1997 *
  30. ****************************************************************************/
  31. #include "form.priv.h"
  32. MODULE_ID("$Id: fld_def.c,v 1.36 2007/10/13 19:29:58 tom Exp $")
  33. /* this can't be readonly */
  34. static FIELD default_field =
  35. {
  36. 0, /* status */
  37. 0, /* rows */
  38. 0, /* cols */
  39. 0, /* frow */
  40. 0, /* fcol */
  41. 0, /* drows */
  42. 0, /* dcols */
  43. 0, /* maxgrow */
  44. 0, /* nrow */
  45. 0, /* nbuf */
  46. NO_JUSTIFICATION, /* just */
  47. 0, /* page */
  48. 0, /* index */
  49. (int)' ', /* pad */
  50. A_NORMAL, /* fore */
  51. A_NORMAL, /* back */
  52. ALL_FIELD_OPTS, /* opts */
  53. (FIELD *)0, /* snext */
  54. (FIELD *)0, /* sprev */
  55. (FIELD *)0, /* link */
  56. (FORM *)0, /* form */
  57. (FIELDTYPE *)0, /* type */
  58. (char *)0, /* arg */
  59. (FIELD_CELL *)0, /* buf */
  60. (char *)0 /* usrptr */
  61. NCURSES_FIELD_EXTENSION
  62. };
  63. NCURSES_EXPORT_VAR(FIELD *)
  64. _nc_Default_Field = &default_field;
  65. /*---------------------------------------------------------------------------
  66. | Facility : libnform
  67. | Function : TypeArgument *_nc_Make_Argument(
  68. | const FIELDTYPE *typ,
  69. | va_list *ap,
  70. | int *err )
  71. |
  72. | Description : Create an argument structure for the specified type.
  73. | Use the type-dependent argument list to construct
  74. | it.
  75. |
  76. | Return Values : Pointer to argument structure. Maybe NULL.
  77. | In case of an error in *err an error counter is increased.
  78. +--------------------------------------------------------------------------*/
  79. NCURSES_EXPORT(TypeArgument *)
  80. _nc_Make_Argument(const FIELDTYPE *typ, va_list *ap, int *err)
  81. {
  82. TypeArgument *res = (TypeArgument *)0;
  83. TypeArgument *p;
  84. if (typ != 0 && (typ->status & _HAS_ARGS) != 0)
  85. {
  86. assert(err != 0 && ap != (va_list *)0);
  87. if ((typ->status & _LINKED_TYPE) != 0)
  88. {
  89. p = typeMalloc(TypeArgument, 1);
  90. if (p != 0)
  91. {
  92. p->left = _nc_Make_Argument(typ->left, ap, err);
  93. p->right = _nc_Make_Argument(typ->right, ap, err);
  94. return p;
  95. }
  96. else
  97. {
  98. *err += 1;
  99. }
  100. }
  101. else
  102. {
  103. assert(typ->makearg != (void *)0);
  104. if (!(res = (TypeArgument *)typ->makearg(ap)))
  105. {
  106. *err += 1;
  107. }
  108. }
  109. }
  110. return res;
  111. }
  112. /*---------------------------------------------------------------------------
  113. | Facility : libnform
  114. | Function : TypeArgument *_nc_Copy_Argument(const FIELDTYPE *typ,
  115. | const TypeArgument *argp,
  116. | int *err )
  117. |
  118. | Description : Create a copy of an argument structure for the specified
  119. | type.
  120. |
  121. | Return Values : Pointer to argument structure. Maybe NULL.
  122. | In case of an error in *err an error counter is increased.
  123. +--------------------------------------------------------------------------*/
  124. NCURSES_EXPORT(TypeArgument *)
  125. _nc_Copy_Argument(const FIELDTYPE *typ, const TypeArgument *argp, int *err)
  126. {
  127. TypeArgument *res = (TypeArgument *)0;
  128. TypeArgument *p;
  129. if (typ != 0 && (typ->status & _HAS_ARGS) != 0)
  130. {
  131. assert(err != 0 && argp != 0);
  132. if ((typ->status & _LINKED_TYPE) != 0)
  133. {
  134. p = typeMalloc(TypeArgument, 1);
  135. if (p != 0)
  136. {
  137. p->left = _nc_Copy_Argument(typ, argp->left, err);
  138. p->right = _nc_Copy_Argument(typ, argp->right, err);
  139. return p;
  140. }
  141. *err += 1;
  142. }
  143. else
  144. {
  145. if (typ->copyarg != (void *)0)
  146. {
  147. if (!(res = (TypeArgument *)(typ->copyarg((const void *)argp))))
  148. {
  149. *err += 1;
  150. }
  151. }
  152. else
  153. {
  154. res = (TypeArgument *)argp;
  155. }
  156. }
  157. }
  158. return res;
  159. }
  160. /*---------------------------------------------------------------------------
  161. | Facility : libnform
  162. | Function : void _nc_Free_Argument(const FIELDTYPE *typ,
  163. | TypeArgument * argp )
  164. |
  165. | Description : Release memory associated with the argument structure
  166. | for the given fieldtype.
  167. |
  168. | Return Values : -
  169. +--------------------------------------------------------------------------*/
  170. NCURSES_EXPORT(void)
  171. _nc_Free_Argument(const FIELDTYPE *typ, TypeArgument *argp)
  172. {
  173. if (typ != 0 && (typ->status & _HAS_ARGS) != 0)
  174. {
  175. if ((typ->status & _LINKED_TYPE) != 0)
  176. {
  177. assert(argp != 0);
  178. _nc_Free_Argument(typ->left, argp->left);
  179. _nc_Free_Argument(typ->right, argp->right);
  180. free(argp);
  181. }
  182. else
  183. {
  184. if (typ->freearg != (void *)0)
  185. {
  186. typ->freearg((void *)argp);
  187. }
  188. }
  189. }
  190. }
  191. /*---------------------------------------------------------------------------
  192. | Facility : libnform
  193. | Function : bool _nc_Copy_Type( FIELD *dst, FIELD const *src )
  194. |
  195. | Description : Copy argument structure of field src to field dst
  196. |
  197. | Return Values : TRUE - copy worked
  198. | FALSE - error occurred
  199. +--------------------------------------------------------------------------*/
  200. NCURSES_EXPORT(bool)
  201. _nc_Copy_Type(FIELD *dst, FIELD const *src)
  202. {
  203. int err = 0;
  204. assert(dst != 0 && src != 0);
  205. dst->type = src->type;
  206. dst->arg = (void *)_nc_Copy_Argument(src->type, (TypeArgument *)(src->arg), &err);
  207. if (err != 0)
  208. {
  209. _nc_Free_Argument(dst->type, (TypeArgument *)(dst->arg));
  210. dst->type = (FIELDTYPE *)0;
  211. dst->arg = (void *)0;
  212. return FALSE;
  213. }
  214. else
  215. {
  216. if (dst->type != 0)
  217. {
  218. dst->type->ref++;
  219. }
  220. return TRUE;
  221. }
  222. }
  223. /*---------------------------------------------------------------------------
  224. | Facility : libnform
  225. | Function : void _nc_Free_Type( FIELD *field )
  226. |
  227. | Description : Release Argument structure for this field
  228. |
  229. | Return Values : -
  230. +--------------------------------------------------------------------------*/
  231. NCURSES_EXPORT(void)
  232. _nc_Free_Type(FIELD *field)
  233. {
  234. assert(field != 0);
  235. if (field->type != 0)
  236. {
  237. field->type->ref--;
  238. }
  239. _nc_Free_Argument(field->type, (TypeArgument *)(field->arg));
  240. }
  241. /*---------------------------------------------------------------------------
  242. | Facility : libnform
  243. | Function : FIELD *new_field( int rows, int cols,
  244. | int frow, int fcol,
  245. | int nrow, int nbuf )
  246. |
  247. | Description : Create a new field with this many 'rows' and 'cols',
  248. | starting at 'frow/fcol' in the subwindow of the form.
  249. | Allocate 'nrow' off-screen rows and 'nbuf' additional
  250. | buffers. If an error occurs, errno is set to
  251. |
  252. | E_BAD_ARGUMENT - invalid argument
  253. | E_SYSTEM_ERROR - system error
  254. |
  255. | Return Values : Pointer to the new field or NULL if failure.
  256. +--------------------------------------------------------------------------*/
  257. NCURSES_EXPORT(FIELD *)
  258. new_field(int rows, int cols, int frow, int fcol, int nrow, int nbuf)
  259. {
  260. static const FIELD_CELL blank = BLANK;
  261. static const FIELD_CELL zeros = ZEROS;
  262. FIELD *New_Field = (FIELD *)0;
  263. int err = E_BAD_ARGUMENT;
  264. T((T_CALLED("new_field(%d,%d,%d,%d,%d,%d)"), rows, cols, frow, fcol, nrow, nbuf));
  265. if (rows > 0 &&
  266. cols > 0 &&
  267. frow >= 0 &&
  268. fcol >= 0 &&
  269. nrow >= 0 &&
  270. nbuf >= 0 &&
  271. ((err = E_SYSTEM_ERROR) != 0) && /* trick: this resets the default error */
  272. (New_Field = typeMalloc(FIELD, 1)) != 0)
  273. {
  274. T((T_CREATE("field %p"), New_Field));
  275. *New_Field = default_field;
  276. New_Field->rows = rows;
  277. New_Field->cols = cols;
  278. New_Field->drows = rows + nrow;
  279. New_Field->dcols = cols;
  280. New_Field->frow = frow;
  281. New_Field->fcol = fcol;
  282. New_Field->nrow = nrow;
  283. New_Field->nbuf = nbuf;
  284. New_Field->link = New_Field;
  285. #if USE_WIDEC_SUPPORT
  286. New_Field->working = newpad(1, Buffer_Length(New_Field) + 1);
  287. New_Field->expanded = typeCalloc(char *, 1 + (unsigned)nbuf);
  288. #endif
  289. if (_nc_Copy_Type(New_Field, &default_field))
  290. {
  291. size_t len;
  292. len = Total_Buffer_Size(New_Field);
  293. if ((New_Field->buf = (FIELD_CELL *)malloc(len)))
  294. {
  295. /* Prefill buffers with blanks and insert terminating zeroes
  296. between buffers */
  297. int i, j;
  298. int cells = Buffer_Length(New_Field);
  299. for (i = 0; i <= New_Field->nbuf; i++)
  300. {
  301. FIELD_CELL *buffer = &(New_Field->buf[(cells + 1) * i]);
  302. for (j = 0; j < cells; ++j)
  303. {
  304. buffer[j] = blank;
  305. }
  306. buffer[j] = zeros;
  307. }
  308. returnField(New_Field);
  309. }
  310. }
  311. }
  312. if (New_Field)
  313. free_field(New_Field);
  314. SET_ERROR(err);
  315. returnField((FIELD *)0);
  316. }
  317. /*---------------------------------------------------------------------------
  318. | Facility : libnform
  319. | Function : int free_field( FIELD *field )
  320. |
  321. | Description : Frees the storage allocated for the field.
  322. |
  323. | Return Values : E_OK - success
  324. | E_BAD_ARGUMENT - invalid field pointer
  325. | E_CONNECTED - field is connected
  326. +--------------------------------------------------------------------------*/
  327. NCURSES_EXPORT(int)
  328. free_field(FIELD *field)
  329. {
  330. T((T_CALLED("free_field(%p)"), field));
  331. if (!field)
  332. {
  333. RETURN(E_BAD_ARGUMENT);
  334. }
  335. else if (field->form != 0)
  336. {
  337. RETURN(E_CONNECTED);
  338. }
  339. else if (field == field->link)
  340. {
  341. if (field->buf != 0)
  342. free(field->buf);
  343. }
  344. else
  345. {
  346. FIELD *f;
  347. for (f = field; f->link != field; f = f->link)
  348. {
  349. }
  350. f->link = field->link;
  351. }
  352. _nc_Free_Type(field);
  353. #if USE_WIDEC_SUPPORT
  354. if (field->expanded != 0)
  355. {
  356. int n;
  357. for (n = 0; n <= field->nbuf; ++n)
  358. {
  359. FreeIfNeeded(field->expanded[n]);
  360. }
  361. free(field->expanded);
  362. (void)delwin(field->working);
  363. }
  364. #endif
  365. free(field);
  366. RETURN(E_OK);
  367. }
  368. /* fld_def.c ends here */