1 /****************************************************************************
2 * Copyright (c) 1998-2007,2010 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.38 2010/01/23 21:14:35 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
*)
69 _nc_Default_Field
= &default_field
;
71 /*---------------------------------------------------------------------------
73 | Function : TypeArgument *_nc_Make_Argument(
74 | const FIELDTYPE *typ,
78 | Description : Create an argument structure for the specified type.
79 | Use the type-dependent argument list to construct
82 | Return Values : Pointer to argument structure. Maybe NULL.
83 | In case of an error in *err an error counter is increased.
84 +--------------------------------------------------------------------------*/
85 NCURSES_EXPORT(TypeArgument
*)
86 _nc_Make_Argument(const FIELDTYPE
*typ
, va_list *ap
, int *err
)
88 TypeArgument
*res
= (TypeArgument
*)0;
91 if (typ
!= 0 && (typ
->status
& _HAS_ARGS
) != 0)
93 assert(err
!= 0 && ap
!= (va_list *)0);
94 if ((typ
->status
& _LINKED_TYPE
) != 0)
96 p
= typeMalloc(TypeArgument
, 1);
100 p
->left
= _nc_Make_Argument(typ
->left
, ap
, err
);
101 p
->right
= _nc_Make_Argument(typ
->right
, ap
, err
);
111 assert(typ
->makearg
!= (void *)0);
112 if (!(res
= (TypeArgument
*)typ
->makearg(ap
)))
121 /*---------------------------------------------------------------------------
122 | Facility : libnform
123 | Function : TypeArgument *_nc_Copy_Argument(const FIELDTYPE *typ,
124 | const TypeArgument *argp,
127 | Description : Create a copy of an argument structure for the specified
130 | Return Values : Pointer to argument structure. Maybe NULL.
131 | In case of an error in *err an error counter is increased.
132 +--------------------------------------------------------------------------*/
133 NCURSES_EXPORT(TypeArgument
*)
134 _nc_Copy_Argument(const FIELDTYPE
*typ
, const TypeArgument
*argp
, int *err
)
136 TypeArgument
*res
= (TypeArgument
*)0;
139 if (typ
!= 0 && (typ
->status
& _HAS_ARGS
) != 0)
141 assert(err
!= 0 && argp
!= 0);
142 if ((typ
->status
& _LINKED_TYPE
) != 0)
144 p
= typeMalloc(TypeArgument
, 1);
148 p
->left
= _nc_Copy_Argument(typ
, argp
->left
, err
);
149 p
->right
= _nc_Copy_Argument(typ
, argp
->right
, err
);
156 if (typ
->copyarg
!= (void *)0)
158 if (!(res
= (TypeArgument
*)(typ
->copyarg((const void *)argp
))))
165 res
= (TypeArgument
*)argp
;
172 /*---------------------------------------------------------------------------
173 | Facility : libnform
174 | Function : void _nc_Free_Argument(const FIELDTYPE *typ,
175 | TypeArgument * argp )
177 | Description : Release memory associated with the argument structure
178 | for the given fieldtype.
181 +--------------------------------------------------------------------------*/
183 _nc_Free_Argument(const FIELDTYPE
*typ
, TypeArgument
*argp
)
185 if (typ
!= 0 && (typ
->status
& _HAS_ARGS
) != 0)
187 if ((typ
->status
& _LINKED_TYPE
) != 0)
190 _nc_Free_Argument(typ
->left
, argp
->left
);
191 _nc_Free_Argument(typ
->right
, argp
->right
);
196 if (typ
->freearg
!= (void *)0)
198 typ
->freearg((void *)argp
);
204 /*---------------------------------------------------------------------------
205 | Facility : libnform
206 | Function : bool _nc_Copy_Type( FIELD *dst, FIELD const *src )
208 | Description : Copy argument structure of field src to field dst
210 | Return Values : TRUE - copy worked
211 | FALSE - error occurred
212 +--------------------------------------------------------------------------*/
214 _nc_Copy_Type(FIELD
*dst
, FIELD
const *src
)
218 assert(dst
!= 0 && src
!= 0);
220 dst
->type
= src
->type
;
221 dst
->arg
= (void *)_nc_Copy_Argument(src
->type
, (TypeArgument
*)(src
->arg
), &err
);
225 _nc_Free_Argument(dst
->type
, (TypeArgument
*)(dst
->arg
));
226 dst
->type
= (FIELDTYPE
*)0;
227 dst
->arg
= (void *)0;
240 /*---------------------------------------------------------------------------
241 | Facility : libnform
242 | Function : void _nc_Free_Type( FIELD *field )
244 | Description : Release Argument structure for this field
247 +--------------------------------------------------------------------------*/
249 _nc_Free_Type(FIELD
*field
)
252 if (field
->type
!= 0)
255 _nc_Free_Argument(field
->type
, (TypeArgument
*)(field
->arg
));
259 /*---------------------------------------------------------------------------
260 | Facility : libnform
261 | Function : FIELD *new_field( int rows, int cols,
262 | int frow, int fcol,
263 | int nrow, int nbuf )
265 | Description : Create a new field with this many 'rows' and 'cols',
266 | starting at 'frow/fcol' in the subwindow of the form.
267 | Allocate 'nrow' off-screen rows and 'nbuf' additional
268 | buffers. If an error occurs, errno is set to
270 | E_BAD_ARGUMENT - invalid argument
271 | E_SYSTEM_ERROR - system error
273 | Return Values : Pointer to the new field or NULL if failure.
274 +--------------------------------------------------------------------------*/
275 NCURSES_EXPORT(FIELD
*)
276 new_field(int rows
, int cols
, int frow
, int fcol
, int nrow
, int nbuf
)
278 static const FIELD_CELL blank
= BLANK
;
279 static const FIELD_CELL zeros
= ZEROS
;
281 FIELD
*New_Field
= (FIELD
*)0;
282 int err
= E_BAD_ARGUMENT
;
284 T((T_CALLED("new_field(%d,%d,%d,%d,%d,%d)"), rows
, cols
, frow
, fcol
, nrow
, nbuf
));
291 ((err
= E_SYSTEM_ERROR
) != 0) && /* trick: this resets the default error */
292 (New_Field
= typeMalloc(FIELD
, 1)) != 0)
294 T((T_CREATE("field %p"), (void *)New_Field
));
295 *New_Field
= default_field
;
296 New_Field
->rows
= rows
;
297 New_Field
->cols
= cols
;
298 New_Field
->drows
= rows
+ nrow
;
299 New_Field
->dcols
= cols
;
300 New_Field
->frow
= frow
;
301 New_Field
->fcol
= fcol
;
302 New_Field
->nrow
= nrow
;
303 New_Field
->nbuf
= nbuf
;
304 New_Field
->link
= New_Field
;
306 #if USE_WIDEC_SUPPORT
307 New_Field
->working
= newpad(1, Buffer_Length(New_Field
) + 1);
308 New_Field
->expanded
= typeCalloc(char *, 1 + (unsigned)nbuf
);
311 if (_nc_Copy_Type(New_Field
, &default_field
))
315 len
= Total_Buffer_Size(New_Field
);
316 if ((New_Field
->buf
= (FIELD_CELL
*)malloc(len
)))
318 /* Prefill buffers with blanks and insert terminating zeroes
321 int cells
= Buffer_Length(New_Field
);
323 for (i
= 0; i
<= New_Field
->nbuf
; i
++)
325 FIELD_CELL
*buffer
= &(New_Field
->buf
[(cells
+ 1) * i
]);
327 for (j
= 0; j
< cells
; ++j
)
333 returnField(New_Field
);
339 free_field(New_Field
);
342 returnField((FIELD
*)0);
345 /*---------------------------------------------------------------------------
346 | Facility : libnform
347 | Function : int free_field( FIELD *field )
349 | Description : Frees the storage allocated for the field.
351 | Return Values : E_OK - success
352 | E_BAD_ARGUMENT - invalid field pointer
353 | E_CONNECTED - field is connected
354 +--------------------------------------------------------------------------*/
356 free_field(FIELD
*field
)
358 T((T_CALLED("free_field(%p)"), (void *)field
));
361 RETURN(E_BAD_ARGUMENT
);
363 else if (field
->form
!= 0)
367 else if (field
== field
->link
)
376 for (f
= field
; f
->link
!= field
; f
= f
->link
)
379 f
->link
= field
->link
;
381 _nc_Free_Type(field
);
382 #if USE_WIDEC_SUPPORT
383 if (field
->expanded
!= 0)
387 for (n
= 0; n
<= field
->nbuf
; ++n
)
389 FreeIfNeeded(field
->expanded
[n
]);
391 free(field
->expanded
);
392 (void)delwin(field
->working
);
399 /* fld_def.c ends here */