xref: /freebsd/contrib/ncurses/form/fld_def.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /****************************************************************************
2  * Copyright 2020 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.43 2020/05/24 01:40:20 anonymous.maarten 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   TypeArgument *p;
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 	  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 *)
134 _nc_Copy_Argument(const FIELDTYPE *typ, const TypeArgument *argp, int *err)
135 {
136   TypeArgument *res = (TypeArgument *)0;
137   TypeArgument *p;
138 
139   if (typ != 0 && (typ->status & _HAS_ARGS) != 0)
140     {
141       assert(err != 0 && argp != 0);
142       if ((typ->status & _LINKED_TYPE) != 0)
143 	{
144 	  p = typeMalloc(TypeArgument, 1);
145 
146 	  if (p != 0)
147 	    {
148 	      p->left = _nc_Copy_Argument(typ, argp->left, err);
149 	      p->right = _nc_Copy_Argument(typ, argp->right, err);
150 	      return p;
151 	    }
152 	  *err += 1;
153 	}
154       else
155 	{
156 	  if (typ->copyarg != (void *)0)
157 	    {
158 	      if (!(res = (TypeArgument *)(typ->copyarg((const void *)argp))))
159 		{
160 		  *err += 1;
161 		}
162 	    }
163 	  else
164 	    {
165 	      res = (TypeArgument *)argp;
166 	    }
167 	}
168     }
169   return res;
170 }
171 
172 /*---------------------------------------------------------------------------
173 |   Facility      :  libnform
174 |   Function      :  void _nc_Free_Argument(const FIELDTYPE *typ,
175 |                                           TypeArgument * argp )
176 |
177 |   Description   :  Release memory associated with the argument structure
178 |                    for the given fieldtype.
179 |
180 |   Return Values :  -
181 +--------------------------------------------------------------------------*/
182 FORM_EXPORT(void)
183 _nc_Free_Argument(const FIELDTYPE *typ, TypeArgument *argp)
184 {
185   if (typ != 0 && (typ->status & _HAS_ARGS) != 0)
186     {
187       if ((typ->status & _LINKED_TYPE) != 0)
188 	{
189 	  if (argp != 0)
190 	    {
191 	      _nc_Free_Argument(typ->left, argp->left);
192 	      _nc_Free_Argument(typ->right, argp->right);
193 	      free(argp);
194 	    }
195 	}
196       else
197 	{
198 	  if (typ->freearg != (void *)0)
199 	    {
200 	      typ->freearg((void *)argp);
201 	    }
202 	}
203     }
204 }
205 
206 /*---------------------------------------------------------------------------
207 |   Facility      :  libnform
208 |   Function      :  bool _nc_Copy_Type( FIELD *dst, FIELD const *src )
209 |
210 |   Description   :  Copy argument structure of field src to field dst
211 |
212 |   Return Values :  TRUE       - copy worked
213 |                    FALSE      - error occurred
214 +--------------------------------------------------------------------------*/
215 FORM_EXPORT(bool)
216 _nc_Copy_Type(FIELD *dst, FIELD const *src)
217 {
218   int err = 0;
219 
220   assert(dst != 0 && src != 0);
221 
222   dst->type = src->type;
223   dst->arg = (void *)_nc_Copy_Argument(src->type, (TypeArgument *)(src->arg), &err);
224 
225   if (err != 0)
226     {
227       _nc_Free_Argument(dst->type, (TypeArgument *)(dst->arg));
228       dst->type = (FIELDTYPE *)0;
229       dst->arg = (void *)0;
230       return FALSE;
231     }
232   else
233     {
234       if (dst->type != 0)
235 	{
236 	  dst->type->ref++;
237 	}
238       return TRUE;
239     }
240 }
241 
242 /*---------------------------------------------------------------------------
243 |   Facility      :  libnform
244 |   Function      :  void _nc_Free_Type( FIELD *field )
245 |
246 |   Description   :  Release Argument structure for this field
247 |
248 |   Return Values :  -
249 +--------------------------------------------------------------------------*/
250 FORM_EXPORT(void)
251 _nc_Free_Type(FIELD *field)
252 {
253   assert(field != 0);
254   if (field->type != 0)
255     {
256       field->type->ref--;
257       _nc_Free_Argument(field->type, (TypeArgument *)(field->arg));
258     }
259 }
260 
261 /*---------------------------------------------------------------------------
262 |   Facility      :  libnform
263 |   Function      :  FIELD *new_field( int rows, int cols,
264 |                                      int frow, int fcol,
265 |                                      int nrow, int nbuf )
266 |
267 |   Description   :  Create a new field with this many 'rows' and 'cols',
268 |                    starting at 'frow/fcol' in the subwindow of the form.
269 |                    Allocate 'nrow' off-screen rows and 'nbuf' additional
270 |                    buffers. If an error occurs, errno is set to
271 |
272 |                    E_BAD_ARGUMENT - invalid argument
273 |                    E_SYSTEM_ERROR - system error
274 |
275 |   Return Values :  Pointer to the new field or NULL if failure.
276 +--------------------------------------------------------------------------*/
277 FORM_EXPORT(FIELD *)
278 new_field(int rows, int cols, int frow, int fcol, int nrow, int nbuf)
279 {
280   static const FIELD_CELL blank = BLANK;
281   static const FIELD_CELL zeros = ZEROS;
282 
283   FIELD *New_Field = (FIELD *)0;
284   int err = E_BAD_ARGUMENT;
285 
286   T((T_CALLED("new_field(%d,%d,%d,%d,%d,%d)"), rows, cols, frow, fcol, nrow, nbuf));
287   if (rows > 0 &&
288       cols > 0 &&
289       frow >= 0 &&
290       fcol >= 0 &&
291       nrow >= 0 &&
292       nbuf >= 0 &&
293       ((err = E_SYSTEM_ERROR) != 0) &&	/* trick: this resets the default error */
294       (New_Field = typeMalloc(FIELD, 1)) != 0)
295     {
296       T((T_CREATE("field %p"), (void *)New_Field));
297       *New_Field = default_field;
298       New_Field->rows = (short)rows;
299       New_Field->cols = (short)cols;
300       New_Field->drows = rows + nrow;
301       New_Field->dcols = cols;
302       New_Field->frow = (short)frow;
303       New_Field->fcol = (short)fcol;
304       New_Field->nrow = nrow;
305       New_Field->nbuf = (short)nbuf;
306       New_Field->link = New_Field;
307 
308 #if USE_WIDEC_SUPPORT
309       New_Field->working = newpad(1, Buffer_Length(New_Field) + 1);
310       New_Field->expanded = typeCalloc(char *, 1 + (unsigned)nbuf);
311 #endif
312 
313       if (_nc_Copy_Type(New_Field, &default_field))
314 	{
315 	  size_t len;
316 
317 	  len = Total_Buffer_Size(New_Field);
318 	  if ((New_Field->buf = (FIELD_CELL *)malloc(len)))
319 	    {
320 	      /* Prefill buffers with blanks and insert terminating zeroes
321 	         between buffers */
322 	      int i, j;
323 	      int cells = Buffer_Length(New_Field);
324 
325 	      for (i = 0; i <= New_Field->nbuf; i++)
326 		{
327 		  FIELD_CELL *buffer = &(New_Field->buf[(cells + 1) * i]);
328 
329 		  for (j = 0; j < cells; ++j)
330 		    {
331 		      buffer[j] = blank;
332 		    }
333 		  buffer[j] = zeros;
334 		}
335 	      returnField(New_Field);
336 	    }
337 	}
338     }
339 
340   if (New_Field)
341     free_field(New_Field);
342 
343   SET_ERROR(err);
344   returnField((FIELD *)0);
345 }
346 
347 /*---------------------------------------------------------------------------
348 |   Facility      :  libnform
349 |   Function      :  int free_field( FIELD *field )
350 |
351 |   Description   :  Frees the storage allocated for the field.
352 |
353 |   Return Values :  E_OK           - success
354 |                    E_BAD_ARGUMENT - invalid field pointer
355 |                    E_CONNECTED    - field is connected
356 +--------------------------------------------------------------------------*/
357 FORM_EXPORT(int)
358 free_field(FIELD *field)
359 {
360   T((T_CALLED("free_field(%p)"), (void *)field));
361   if (!field)
362     {
363       RETURN(E_BAD_ARGUMENT);
364     }
365   else if (field->form != 0)
366     {
367       RETURN(E_CONNECTED);
368     }
369   else if (field == field->link)
370     {
371       if (field->buf != 0)
372 	free(field->buf);
373     }
374   else
375     {
376       FIELD *f;
377 
378       for (f = field; f->link != field; f = f->link)
379 	{
380 	}
381       f->link = field->link;
382     }
383   _nc_Free_Type(field);
384 #if USE_WIDEC_SUPPORT
385   if (field->expanded != 0)
386     {
387       int n;
388 
389       for (n = 0; n <= field->nbuf; ++n)
390 	{
391 	  FreeIfNeeded(field->expanded[n]);
392 	}
393       free(field->expanded);
394       (void)delwin(field->working);
395     }
396 #endif
397   free(field);
398   RETURN(E_OK);
399 }
400 
401 /* fld_def.c ends here */
402