1 /****************************************************************************
2 * Copyright (c) 1998-2010,2011 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Juergen Pfeifer, 1995,1997 *
31 ****************************************************************************/
33 #include "form.priv.h"
35 MODULE_ID("$Id: fld_def.c,v 1.39 2011/07/16 22:26:11 tom Exp $")
37 /* this can't be readonly */
38 static FIELD default_field =
50 NO_JUSTIFICATION, /* just */
56 ALL_FIELD_OPTS, /* opts */
57 (FIELD *)0, /* snext */
58 (FIELD *)0, /* sprev */
59 (FIELD *)0, /* link */
61 (FIELDTYPE *)0, /* type */
63 (FIELD_CELL *)0, /* buf */
64 (char *)0 /* usrptr */
65 NCURSES_FIELD_EXTENSION
68 NCURSES_EXPORT_VAR(FIELD *) _nc_Default_Field = &default_field;
70 /*---------------------------------------------------------------------------
72 | Function : TypeArgument *_nc_Make_Argument(
73 | const FIELDTYPE *typ,
77 | Description : Create an argument structure for the specified type.
78 | Use the type-dependent argument list to construct
81 | Return Values : Pointer to argument structure. Maybe NULL.
82 | In case of an error in *err an error counter is increased.
83 +--------------------------------------------------------------------------*/
84 NCURSES_EXPORT(TypeArgument *)
85 _nc_Make_Argument(const FIELDTYPE *typ, va_list *ap, int *err)
87 TypeArgument *res = (TypeArgument *)0;
90 if (typ != 0 && (typ->status & _HAS_ARGS) != 0)
92 assert(err != 0 && ap != (va_list *)0);
93 if ((typ->status & _LINKED_TYPE) != 0)
95 p = typeMalloc(TypeArgument, 1);
99 p->left = _nc_Make_Argument(typ->left, ap, err);
100 p->right = _nc_Make_Argument(typ->right, ap, err);
110 assert(typ->makearg != (void *)0);
111 if (!(res = (TypeArgument *)typ->makearg(ap)))
120 /*---------------------------------------------------------------------------
121 | Facility : libnform
122 | Function : TypeArgument *_nc_Copy_Argument(const FIELDTYPE *typ,
123 | const TypeArgument *argp,
126 | Description : Create a copy of an argument structure for the specified
129 | Return Values : Pointer to argument structure. Maybe NULL.
130 | In case of an error in *err an error counter is increased.
131 +--------------------------------------------------------------------------*/
132 NCURSES_EXPORT(TypeArgument *)
133 _nc_Copy_Argument(const FIELDTYPE *typ, const TypeArgument *argp, int *err)
135 TypeArgument *res = (TypeArgument *)0;
138 if (typ != 0 && (typ->status & _HAS_ARGS) != 0)
140 assert(err != 0 && argp != 0);
141 if ((typ->status & _LINKED_TYPE) != 0)
143 p = typeMalloc(TypeArgument, 1);
147 p->left = _nc_Copy_Argument(typ, argp->left, err);
148 p->right = _nc_Copy_Argument(typ, argp->right, err);
155 if (typ->copyarg != (void *)0)
157 if (!(res = (TypeArgument *)(typ->copyarg((const void *)argp))))
164 res = (TypeArgument *)argp;
171 /*---------------------------------------------------------------------------
172 | Facility : libnform
173 | Function : void _nc_Free_Argument(const FIELDTYPE *typ,
174 | TypeArgument * argp )
176 | Description : Release memory associated with the argument structure
177 | for the given fieldtype.
180 +--------------------------------------------------------------------------*/
182 _nc_Free_Argument(const FIELDTYPE *typ, TypeArgument *argp)
184 if (typ != 0 && (typ->status & _HAS_ARGS) != 0)
186 if ((typ->status & _LINKED_TYPE) != 0)
190 _nc_Free_Argument(typ->left, argp->left);
191 _nc_Free_Argument(typ->right, argp->right);
197 if (typ->freearg != (void *)0)
199 typ->freearg((void *)argp);
205 /*---------------------------------------------------------------------------
206 | Facility : libnform
207 | Function : bool _nc_Copy_Type( FIELD *dst, FIELD const *src )
209 | Description : Copy argument structure of field src to field dst
211 | Return Values : TRUE - copy worked
212 | FALSE - error occurred
213 +--------------------------------------------------------------------------*/
215 _nc_Copy_Type(FIELD *dst, FIELD const *src)
219 assert(dst != 0 && src != 0);
221 dst->type = src->type;
222 dst->arg = (void *)_nc_Copy_Argument(src->type, (TypeArgument *)(src->arg), &err);
226 _nc_Free_Argument(dst->type, (TypeArgument *)(dst->arg));
227 dst->type = (FIELDTYPE *)0;
228 dst->arg = (void *)0;
241 /*---------------------------------------------------------------------------
242 | Facility : libnform
243 | Function : void _nc_Free_Type( FIELD *field )
245 | Description : Release Argument structure for this field
248 +--------------------------------------------------------------------------*/
250 _nc_Free_Type(FIELD *field)
253 if (field->type != 0)
256 _nc_Free_Argument(field->type, (TypeArgument *)(field->arg));
260 /*---------------------------------------------------------------------------
261 | Facility : libnform
262 | Function : FIELD *new_field( int rows, int cols,
263 | int frow, int fcol,
264 | int nrow, int nbuf )
266 | Description : Create a new field with this many 'rows' and 'cols',
267 | starting at 'frow/fcol' in the subwindow of the form.
268 | Allocate 'nrow' off-screen rows and 'nbuf' additional
269 | buffers. If an error occurs, errno is set to
271 | E_BAD_ARGUMENT - invalid argument
272 | E_SYSTEM_ERROR - system error
274 | Return Values : Pointer to the new field or NULL if failure.
275 +--------------------------------------------------------------------------*/
276 NCURSES_EXPORT(FIELD *)
277 new_field(int rows, int cols, int frow, int fcol, int nrow, int nbuf)
279 static const FIELD_CELL blank = BLANK;
280 static const FIELD_CELL zeros = ZEROS;
282 FIELD *New_Field = (FIELD *)0;
283 int err = E_BAD_ARGUMENT;
285 T((T_CALLED("new_field(%d,%d,%d,%d,%d,%d)"), rows, cols, frow, fcol, nrow, nbuf));
292 ((err = E_SYSTEM_ERROR) != 0) && /* trick: this resets the default error */
293 (New_Field = typeMalloc(FIELD, 1)) != 0)
295 T((T_CREATE("field %p"), (void *)New_Field));
296 *New_Field = default_field;
297 New_Field->rows = rows;
298 New_Field->cols = cols;
299 New_Field->drows = rows + nrow;
300 New_Field->dcols = cols;
301 New_Field->frow = frow;
302 New_Field->fcol = fcol;
303 New_Field->nrow = nrow;
304 New_Field->nbuf = nbuf;
305 New_Field->link = New_Field;
307 #if USE_WIDEC_SUPPORT
308 New_Field->working = newpad(1, Buffer_Length(New_Field) + 1);
309 New_Field->expanded = typeCalloc(char *, 1 + (unsigned)nbuf);
312 if (_nc_Copy_Type(New_Field, &default_field))
316 len = Total_Buffer_Size(New_Field);
317 if ((New_Field->buf = (FIELD_CELL *)malloc(len)))
319 /* Prefill buffers with blanks and insert terminating zeroes
322 int cells = Buffer_Length(New_Field);
324 for (i = 0; i <= New_Field->nbuf; i++)
326 FIELD_CELL *buffer = &(New_Field->buf[(cells + 1) * i]);
328 for (j = 0; j < cells; ++j)
334 returnField(New_Field);
340 free_field(New_Field);
343 returnField((FIELD *)0);
346 /*---------------------------------------------------------------------------
347 | Facility : libnform
348 | Function : int free_field( FIELD *field )
350 | Description : Frees the storage allocated for the field.
352 | Return Values : E_OK - success
353 | E_BAD_ARGUMENT - invalid field pointer
354 | E_CONNECTED - field is connected
355 +--------------------------------------------------------------------------*/
357 free_field(FIELD *field)
359 T((T_CALLED("free_field(%p)"), (void *)field));
362 RETURN(E_BAD_ARGUMENT);
364 else if (field->form != 0)
368 else if (field == field->link)
377 for (f = field; f->link != field; f = f->link)
380 f->link = field->link;
382 _nc_Free_Type(field);
383 #if USE_WIDEC_SUPPORT
384 if (field->expanded != 0)
388 for (n = 0; n <= field->nbuf; ++n)
390 FreeIfNeeded(field->expanded[n]);
392 free(field->expanded);
393 (void)delwin(field->working);
400 /* fld_def.c ends here */