xref: /freebsd/contrib/ncurses/form/fld_def.c (revision f0a75d274af375d15b97b830966b99a02b7db911)
1 /****************************************************************************
2  * Copyright (c) 1998-2004,2005 Free Software Foundation, Inc.              *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 
29 /****************************************************************************
30  *   Author:  Juergen Pfeifer, 1995,1997                                    *
31  ****************************************************************************/
32 
33 #include "form.priv.h"
34 
35 MODULE_ID("$Id: fld_def.c,v 1.33 2005/04/16 17:31:17 tom Exp $")
36 
37 /* this can't be readonly */
38 static FIELD default_field =
39 {
40   0,				/* status  */
41   0,				/* rows    */
42   0,				/* cols    */
43   0,				/* frow    */
44   0,				/* fcol    */
45   0,				/* drows   */
46   0,				/* dcols   */
47   0,				/* maxgrow */
48   0,				/* nrow    */
49   0,				/* nbuf    */
50   NO_JUSTIFICATION,		/* just    */
51   0,				/* page    */
52   0,				/* index   */
53   (int)' ',			/* pad     */
54   A_NORMAL,			/* fore    */
55   A_NORMAL,			/* back    */
56   ALL_FIELD_OPTS,		/* opts    */
57   (FIELD *)0,			/* snext   */
58   (FIELD *)0,			/* sprev   */
59   (FIELD *)0,			/* link    */
60   (FORM *)0,			/* form    */
61   (FIELDTYPE *)0,		/* type    */
62   (char *)0,			/* arg     */
63   (FIELD_CELL *)0,		/* buf     */
64   (char *)0			/* usrptr  */
65   NCURSES_FIELD_EXTENSION
66 };
67 
68 NCURSES_EXPORT_VAR(FIELD *)
69 _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 NCURSES_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 = (TypeArgument *)malloc(sizeof(TypeArgument));
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 NCURSES_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 = (TypeArgument *)malloc(sizeof(TypeArgument));
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 NCURSES_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 	  assert(argp != 0);
190 	  _nc_Free_Argument(typ->left, argp->left);
191 	  _nc_Free_Argument(typ->right, argp->right);
192 	  free(argp);
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 NCURSES_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 NCURSES_EXPORT(void)
249 _nc_Free_Type(FIELD *field)
250 {
251   assert(field != 0);
252   if (field->type != 0)
253     {
254       field->type->ref--;
255     }
256   _nc_Free_Argument(field->type, (TypeArgument *)(field->arg));
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 NCURSES_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 = (FIELD *)malloc(sizeof(FIELD))) != 0)
293     {
294       *New_Field = default_field;
295       New_Field->rows = rows;
296       New_Field->cols = cols;
297       New_Field->drows = rows + nrow;
298       New_Field->dcols = cols;
299       New_Field->frow = frow;
300       New_Field->fcol = fcol;
301       New_Field->nrow = nrow;
302       New_Field->nbuf = nbuf;
303       New_Field->link = New_Field;
304 
305 #if USE_WIDEC_SUPPORT
306       New_Field->working = newpad(1, Buffer_Length(New_Field) + 1);
307       New_Field->expanded = (char **)calloc(1 + (unsigned)rows, sizeof(char *));
308 #endif
309 
310       if (_nc_Copy_Type(New_Field, &default_field))
311 	{
312 	  size_t len;
313 
314 	  len = Total_Buffer_Size(New_Field);
315 	  if ((New_Field->buf = (FIELD_CELL *)malloc(len)))
316 	    {
317 	      /* Prefill buffers with blanks and insert terminating zeroes
318 	         between buffers */
319 	      int i, j;
320 	      int cells = Buffer_Length(New_Field);
321 
322 	      for (i = 0; i <= New_Field->nbuf; i++)
323 		{
324 		  FIELD_CELL *buffer = &(New_Field->buf[(cells + 1) * i]);
325 
326 		  for (j = 0; j < cells; ++j)
327 		    {
328 		      buffer[j] = blank;
329 		    }
330 		  buffer[j] = zeros;
331 		}
332 	      returnField(New_Field);
333 	    }
334 	}
335     }
336 
337   if (New_Field)
338     free_field(New_Field);
339 
340   SET_ERROR(err);
341   returnField((FIELD *)0);
342 }
343 
344 /*---------------------------------------------------------------------------
345 |   Facility      :  libnform
346 |   Function      :  int free_field( FIELD *field )
347 |
348 |   Description   :  Frees the storage allocated for the field.
349 |
350 |   Return Values :  E_OK           - success
351 |                    E_BAD_ARGUMENT - invalid field pointer
352 |                    E_CONNECTED    - field is connected
353 +--------------------------------------------------------------------------*/
354 NCURSES_EXPORT(int)
355 free_field(FIELD *field)
356 {
357   T((T_CALLED("free_field(%p)"), field));
358   if (!field)
359     {
360       RETURN(E_BAD_ARGUMENT);
361     }
362   else if (field->form != 0)
363     {
364       RETURN(E_CONNECTED);
365     }
366   else if (field == field->link)
367     {
368       if (field->buf != 0)
369 	free(field->buf);
370     }
371   else
372     {
373       FIELD *f;
374 
375       for (f = field; f->link != field; f = f->link)
376 	{
377 	}
378       f->link = field->link;
379     }
380   _nc_Free_Type(field);
381 #if USE_WIDEC_SUPPORT
382   if (field->expanded != 0)
383     {
384       int n;
385 
386       for (n = 0; n <= field->nbuf; ++n)
387 	{
388 	  FreeIfNeeded(field->expanded[n]);
389 	}
390       free(field->expanded);
391       (void)delwin(field->working);
392     }
393 #endif
394   free(field);
395   RETURN(E_OK);
396 }
397 
398 /* fld_def.c ends here */
399