GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libform/fld_def.c Lines: 0 93 0.0 %
Date: 2017-11-07 Branches: 0 68 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: fld_def.c,v 1.9 2015/01/23 22:48:51 krw Exp $	*/
2
/****************************************************************************
3
 * Copyright (c) 1998-2005,2007 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.9 2015/01/23 22:48:51 krw 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
  ALL_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
NCURSES_EXPORT_VAR(FIELD *)
70
_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
NCURSES_EXPORT(TypeArgument *)
87
_nc_Make_Argument(const FIELDTYPE *typ, va_list *ap, int *err)
88
{
89
  TypeArgument *res = (TypeArgument *)0;
90
  TypeArgument *p;
91
92
  if (typ != 0 && (typ->status & _HAS_ARGS) != 0)
93
    {
94
      assert(err != 0 && ap != (va_list *)0);
95
      if ((typ->status & _LINKED_TYPE) != 0)
96
	{
97
	  p = typeMalloc(TypeArgument, 1);
98
99
	  if (p != 0)
100
	    {
101
	      p->left = _nc_Make_Argument(typ->left, ap, err);
102
	      p->right = _nc_Make_Argument(typ->right, ap, err);
103
	      return p;
104
	    }
105
	  else
106
	    {
107
	      *err += 1;
108
	    }
109
	}
110
      else
111
	{
112
	  assert(typ->makearg != (void *)0);
113
	  if (!(res = (TypeArgument *)typ->makearg(ap)))
114
	    {
115
	      *err += 1;
116
	    }
117
	}
118
    }
119
  return res;
120
}
121
122
/*---------------------------------------------------------------------------
123
|   Facility      :  libnform
124
|   Function      :  TypeArgument *_nc_Copy_Argument(const FIELDTYPE *typ,
125
|                                                    const TypeArgument *argp,
126
|                                                    int *err )
127
|
128
|   Description   :  Create a copy of an argument structure for the specified
129
|                    type.
130
|
131
|   Return Values :  Pointer to argument structure. Maybe NULL.
132
|                    In case of an error in *err an error counter is increased.
133
+--------------------------------------------------------------------------*/
134
NCURSES_EXPORT(TypeArgument *)
135
_nc_Copy_Argument(const FIELDTYPE *typ, const TypeArgument *argp, int *err)
136
{
137
  TypeArgument *res = (TypeArgument *)0;
138
  TypeArgument *p;
139
140
  if (typ != 0 && (typ->status & _HAS_ARGS) != 0)
141
    {
142
      assert(err != 0 && argp != 0);
143
      if ((typ->status & _LINKED_TYPE) != 0)
144
	{
145
	  p = typeMalloc(TypeArgument, 1);
146
147
	  if (p != 0)
148
	    {
149
	      p->left = _nc_Copy_Argument(typ, argp->left, err);
150
	      p->right = _nc_Copy_Argument(typ, argp->right, err);
151
	      return p;
152
	    }
153
	  *err += 1;
154
	}
155
      else
156
	{
157
	  if (typ->copyarg != (void *)0)
158
	    {
159
	      if (!(res = (TypeArgument *)(typ->copyarg((const void *)argp))))
160
		{
161
		  *err += 1;
162
		}
163
	    }
164
	  else
165
	    {
166
	      res = (TypeArgument *)argp;
167
	    }
168
	}
169
    }
170
  return res;
171
}
172
173
/*---------------------------------------------------------------------------
174
|   Facility      :  libnform
175
|   Function      :  void _nc_Free_Argument(const FIELDTYPE *typ,
176
|                                           TypeArgument * argp )
177
|
178
|   Description   :  Release memory associated with the argument structure
179
|                    for the given fieldtype.
180
|
181
|   Return Values :  -
182
+--------------------------------------------------------------------------*/
183
NCURSES_EXPORT(void)
184
_nc_Free_Argument(const FIELDTYPE *typ, TypeArgument *argp)
185
{
186
  if (typ != 0 && (typ->status & _HAS_ARGS) != 0)
187
    {
188
      if ((typ->status & _LINKED_TYPE) != 0)
189
	{
190
	  assert(argp != 0);
191
	  _nc_Free_Argument(typ->left, argp->left);
192
	  _nc_Free_Argument(typ->right, argp->right);
193
	  free(argp);
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
NCURSES_EXPORT(bool)
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
NCURSES_EXPORT(void)
250
_nc_Free_Type(FIELD *field)
251
{
252
  assert(field != 0);
253
  if (field->type != 0)
254
    {
255
      field->type->ref--;
256
    }
257
  _nc_Free_Argument(field->type, (TypeArgument *)(field->arg));
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
NCURSES_EXPORT(FIELD *)
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"), New_Field));
296
      *New_Field = default_field;
297
      New_Field->rows = rows;
298
      New_Field->cols = cols;
299
      New_Field->drows = rows + nrow;
300
      New_Field->dcols = cols;
301
      New_Field->frow = frow;
302
      New_Field->fcol = fcol;
303
      New_Field->nrow = nrow;
304
      New_Field->nbuf = 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
NCURSES_EXPORT(int)
357
free_field(FIELD *field)
358
{
359
  T((T_CALLED("free_field(%p)"), 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 */