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