1 /* $OpenBSD: fld_def.c,v 1.10 2023/10/17 09:52:10 nicm Exp $ */
2 /****************************************************************************
3 * Copyright 2020,2021 Thomas E. Dickey *
4 * Copyright 1998-2012,2014 Free Software Foundation, Inc. *
5 * *
6 * Permission is hereby granted, free of charge, to any person obtaining a *
7 * copy of this software and associated documentation files (the *
8 * "Software"), to deal in the Software without restriction, including *
9 * without limitation the rights to use, copy, modify, merge, publish, *
10 * distribute, distribute with modifications, sublicense, and/or sell *
11 * copies of the Software, and to permit persons to whom the Software is *
12 * furnished to do so, subject to the following conditions: *
13 * *
14 * The above copyright notice and this permission notice shall be included *
15 * in all copies or substantial portions of the Software. *
16 * *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * *
25 * Except as contained in this notice, the name(s) of the above copyright *
26 * holders shall not be used in advertising or otherwise to promote the *
27 * sale, use or other dealings in this Software without prior written *
28 * authorization. *
29 ****************************************************************************/
30
31 /****************************************************************************
32 * Author: Juergen Pfeifer, 1995,1997 *
33 ****************************************************************************/
34
35 #include "form.priv.h"
36
37 MODULE_ID("$Id: fld_def.c,v 1.10 2023/10/17 09:52:10 nicm Exp $")
38
39 /* this can't be readonly */
40 static FIELD default_field =
41 {
42 0, /* status */
43 0, /* rows */
44 0, /* cols */
45 0, /* frow */
46 0, /* fcol */
47 0, /* drows */
48 0, /* dcols */
49 0, /* maxgrow */
50 0, /* nrow */
51 0, /* nbuf */
52 NO_JUSTIFICATION, /* just */
53 0, /* page */
54 0, /* index */
55 (int)' ', /* pad */
56 A_NORMAL, /* fore */
57 A_NORMAL, /* back */
58 STD_FIELD_OPTS, /* opts */
59 (FIELD *)0, /* snext */
60 (FIELD *)0, /* sprev */
61 (FIELD *)0, /* link */
62 (FORM *)0, /* form */
63 (FIELDTYPE *)0, /* type */
64 (char *)0, /* arg */
65 (FIELD_CELL *)0, /* buf */
66 (char *)0 /* usrptr */
67 NCURSES_FIELD_EXTENSION
68 };
69
70 FORM_EXPORT_VAR(FIELD *) _nc_Default_Field = &default_field;
71
72 /*---------------------------------------------------------------------------
73 | Facility : libnform
74 | Function : TypeArgument *_nc_Make_Argument(
75 | const FIELDTYPE *typ,
76 | va_list *ap,
77 | int *err )
78 |
79 | Description : Create an argument structure for the specified type.
80 | Use the type-dependent argument list to construct
81 | it.
82 |
83 | Return Values : Pointer to argument structure. Maybe NULL.
84 | In case of an error in *err an error counter is increased.
85 +--------------------------------------------------------------------------*/
86 FORM_EXPORT(TypeArgument *)
_nc_Make_Argument(const FIELDTYPE * typ,va_list * ap,int * err)87 _nc_Make_Argument(const FIELDTYPE *typ, va_list *ap, int *err)
88 {
89 TypeArgument *res = (TypeArgument *)0;
90
91 if (typ != 0 && (typ->status & _HAS_ARGS) != 0)
92 {
93 assert(err != 0 && ap != (va_list *)0);
94 if ((typ->status & _LINKED_TYPE) != 0)
95 {
96 TypeArgument *p = typeMalloc(TypeArgument, 1);
97
98 if (p != 0)
99 {
100 p->left = _nc_Make_Argument(typ->left, ap, err);
101 p->right = _nc_Make_Argument(typ->right, ap, err);
102 return p;
103 }
104 else
105 {
106 *err += 1;
107 }
108 }
109 else
110 {
111 assert(typ->makearg != (void *)0);
112 if (!(res = (TypeArgument *)typ->makearg(ap)))
113 {
114 *err += 1;
115 }
116 }
117 }
118 return res;
119 }
120
121 /*---------------------------------------------------------------------------
122 | Facility : libnform
123 | Function : TypeArgument *_nc_Copy_Argument(const FIELDTYPE *typ,
124 | const TypeArgument *argp,
125 | int *err )
126 |
127 | Description : Create a copy of an argument structure for the specified
128 | type.
129 |
130 | Return Values : Pointer to argument structure. Maybe NULL.
131 | In case of an error in *err an error counter is increased.
132 +--------------------------------------------------------------------------*/
133 FORM_EXPORT(TypeArgument *)
_nc_Copy_Argument(const FIELDTYPE * typ,const TypeArgument * argp,int * err)134 _nc_Copy_Argument(const FIELDTYPE *typ, const TypeArgument *argp, int *err)
135 {
136 TypeArgument *res = (TypeArgument *)0;
137
138 if (typ != 0 && (typ->status & _HAS_ARGS) != 0)
139 {
140 assert(err != 0 && argp != 0);
141 if ((typ->status & _LINKED_TYPE) != 0)
142 {
143 TypeArgument *p = typeMalloc(TypeArgument, 1);
144
145 if (p != 0)
146 {
147 p->left = _nc_Copy_Argument(typ, argp->left, err);
148 p->right = _nc_Copy_Argument(typ, argp->right, err);
149 return p;
150 }
151 *err += 1;
152 }
153 else
154 {
155 if (typ->copyarg != (void *)0)
156 {
157 if (!(res = (TypeArgument *)(typ->copyarg((const void *)argp))))
158 {
159 *err += 1;
160 }
161 }
162 else
163 {
164 res = (TypeArgument *)argp;
165 }
166 }
167 }
168 return res;
169 }
170
171 /*---------------------------------------------------------------------------
172 | Facility : libnform
173 | Function : void _nc_Free_Argument(const FIELDTYPE *typ,
174 | TypeArgument * argp )
175 |
176 | Description : Release memory associated with the argument structure
177 | for the given fieldtype.
178 |
179 | Return Values : -
180 +--------------------------------------------------------------------------*/
181 FORM_EXPORT(void)
_nc_Free_Argument(const FIELDTYPE * typ,TypeArgument * argp)182 _nc_Free_Argument(const FIELDTYPE *typ, TypeArgument *argp)
183 {
184 if (typ != 0 && (typ->status & _HAS_ARGS) != 0)
185 {
186 if ((typ->status & _LINKED_TYPE) != 0)
187 {
188 if (argp != 0)
189 {
190 _nc_Free_Argument(typ->left, argp->left);
191 _nc_Free_Argument(typ->right, argp->right);
192 free(argp);
193 }
194 }
195 else
196 {
197 if (typ->freearg != (void *)0)
198 {
199 typ->freearg((void *)argp);
200 }
201 }
202 }
203 }
204
205 /*---------------------------------------------------------------------------
206 | Facility : libnform
207 | Function : bool _nc_Copy_Type( FIELD *dst, FIELD const *src )
208 |
209 | Description : Copy argument structure of field src to field dst
210 |
211 | Return Values : TRUE - copy worked
212 | FALSE - error occurred
213 +--------------------------------------------------------------------------*/
214 FORM_EXPORT(bool)
_nc_Copy_Type(FIELD * dst,FIELD const * src)215 _nc_Copy_Type(FIELD *dst, FIELD const *src)
216 {
217 int err = 0;
218
219 assert(dst != 0 && src != 0);
220
221 dst->type = src->type;
222 dst->arg = (void *)_nc_Copy_Argument(src->type, (TypeArgument *)(src->arg), &err);
223
224 if (err != 0)
225 {
226 _nc_Free_Argument(dst->type, (TypeArgument *)(dst->arg));
227 dst->type = (FIELDTYPE *)0;
228 dst->arg = (void *)0;
229 return FALSE;
230 }
231 else
232 {
233 if (dst->type != 0)
234 {
235 dst->type->ref++;
236 }
237 return TRUE;
238 }
239 }
240
241 /*---------------------------------------------------------------------------
242 | Facility : libnform
243 | Function : void _nc_Free_Type( FIELD *field )
244 |
245 | Description : Release Argument structure for this field
246 |
247 | Return Values : -
248 +--------------------------------------------------------------------------*/
249 FORM_EXPORT(void)
_nc_Free_Type(FIELD * field)250 _nc_Free_Type(FIELD *field)
251 {
252 assert(field != 0);
253 if (field->type != 0)
254 {
255 field->type->ref--;
256 _nc_Free_Argument(field->type, (TypeArgument *)(field->arg));
257 }
258 }
259
260 /*---------------------------------------------------------------------------
261 | Facility : libnform
262 | Function : FIELD *new_field( int rows, int cols,
263 | int frow, int fcol,
264 | int nrow, int nbuf )
265 |
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
270 |
271 | E_BAD_ARGUMENT - invalid argument
272 | E_SYSTEM_ERROR - system error
273 |
274 | Return Values : Pointer to the new field or NULL if failure.
275 +--------------------------------------------------------------------------*/
276 FORM_EXPORT(FIELD *)
new_field(int rows,int cols,int frow,int fcol,int nrow,int nbuf)277 new_field(int rows, int cols, int frow, int fcol, int nrow, int nbuf)
278 {
279 static const FIELD_CELL blank = BLANK;
280 static const FIELD_CELL zeros = ZEROS;
281
282 FIELD *New_Field = (FIELD *)0;
283 int err = E_BAD_ARGUMENT;
284
285 T((T_CALLED("new_field(%d,%d,%d,%d,%d,%d)"), rows, cols, frow, fcol, nrow, nbuf));
286 if (rows > 0 &&
287 cols > 0 &&
288 frow >= 0 &&
289 fcol >= 0 &&
290 nrow >= 0 &&
291 nbuf >= 0 &&
292 ((err = E_SYSTEM_ERROR) != 0) && /* trick: this resets the default error */
293 (New_Field = typeMalloc(FIELD, 1)) != 0)
294 {
295 T((T_CREATE("field %p"), (void *)New_Field));
296 *New_Field = default_field;
297 New_Field->rows = (short)rows;
298 New_Field->cols = (short)cols;
299 New_Field->drows = rows + nrow;
300 New_Field->dcols = cols;
301 New_Field->frow = (short)frow;
302 New_Field->fcol = (short)fcol;
303 New_Field->nrow = nrow;
304 New_Field->nbuf = (short)nbuf;
305 New_Field->link = New_Field;
306
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);
310 #endif
311
312 if (_nc_Copy_Type(New_Field, &default_field))
313 {
314 size_t len;
315
316 len = Total_Buffer_Size(New_Field);
317 if ((New_Field->buf = (FIELD_CELL *)malloc(len)))
318 {
319 /* Prefill buffers with blanks and insert terminating zeroes
320 between buffers */
321 int i, j;
322 int cells = Buffer_Length(New_Field);
323
324 for (i = 0; i <= New_Field->nbuf; i++)
325 {
326 FIELD_CELL *buffer = &(New_Field->buf[(cells + 1) * i]);
327
328 for (j = 0; j < cells; ++j)
329 {
330 buffer[j] = blank;
331 }
332 buffer[j] = zeros;
333 }
334 returnField(New_Field);
335 }
336 }
337 }
338
339 if (New_Field)
340 free_field(New_Field);
341
342 SET_ERROR(err);
343 returnField((FIELD *)0);
344 }
345
346 /*---------------------------------------------------------------------------
347 | Facility : libnform
348 | Function : int free_field( FIELD *field )
349 |
350 | Description : Frees the storage allocated for the field.
351 |
352 | Return Values : E_OK - success
353 | E_BAD_ARGUMENT - invalid field pointer
354 | E_CONNECTED - field is connected
355 +--------------------------------------------------------------------------*/
356 FORM_EXPORT(int)
free_field(FIELD * field)357 free_field(FIELD *field)
358 {
359 T((T_CALLED("free_field(%p)"), (void *)field));
360 if (!field)
361 {
362 RETURN(E_BAD_ARGUMENT);
363 }
364 else if (field->form != 0)
365 {
366 RETURN(E_CONNECTED);
367 }
368 else if (field == field->link)
369 {
370 if (field->buf != 0)
371 free(field->buf);
372 }
373 else
374 {
375 FIELD *f;
376
377 for (f = field; f->link != field; f = f->link)
378 {
379 }
380 f->link = field->link;
381 }
382 _nc_Free_Type(field);
383 #if USE_WIDEC_SUPPORT
384 if (field->expanded != 0)
385 {
386 int n;
387
388 for (n = 0; n <= field->nbuf; ++n)
389 {
390 FreeIfNeeded(field->expanded[n]);
391 }
392 free(field->expanded);
393 (void)delwin(field->working);
394 }
395 #endif
396 free(field);
397 RETURN(E_OK);
398 }
399
400 /* fld_def.c ends here */
401