1*0a6a1f1dSLionel Sambuc /* $NetBSD: field.c,v 1.29 2015/09/07 15:50:49 joerg Exp $ */
2a0e6850fSThomas Cort /*-
3a0e6850fSThomas Cort * Copyright (c) 1998-1999 Brett Lymn
4a0e6850fSThomas Cort * (blymn@baea.com.au, brett_lymn@yahoo.com.au)
5a0e6850fSThomas Cort * All rights reserved.
6a0e6850fSThomas Cort *
7a0e6850fSThomas Cort * This code has been donated to The NetBSD Foundation by the Author.
8a0e6850fSThomas Cort *
9a0e6850fSThomas Cort * Redistribution and use in source and binary forms, with or without
10a0e6850fSThomas Cort * modification, are permitted provided that the following conditions
11a0e6850fSThomas Cort * are met:
12a0e6850fSThomas Cort * 1. Redistributions of source code must retain the above copyright
13a0e6850fSThomas Cort * notice, this list of conditions and the following disclaimer.
14a0e6850fSThomas Cort * 2. The name of the author may not be used to endorse or promote products
15a0e6850fSThomas Cort * derived from this software without specific prior written permission
16a0e6850fSThomas Cort *
17a0e6850fSThomas Cort * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18a0e6850fSThomas Cort * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19a0e6850fSThomas Cort * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20a0e6850fSThomas Cort * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21a0e6850fSThomas Cort * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22a0e6850fSThomas Cort * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23a0e6850fSThomas Cort * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24a0e6850fSThomas Cort * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25a0e6850fSThomas Cort * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26a0e6850fSThomas Cort * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27a0e6850fSThomas Cort *
28a0e6850fSThomas Cort *
29a0e6850fSThomas Cort */
30a0e6850fSThomas Cort
31a0e6850fSThomas Cort #include <sys/cdefs.h>
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: field.c,v 1.29 2015/09/07 15:50:49 joerg Exp $");
33a0e6850fSThomas Cort
3484d9c625SLionel Sambuc #include <sys/param.h>
35a0e6850fSThomas Cort #include <stdlib.h>
36a0e6850fSThomas Cort #include <strings.h>
37a0e6850fSThomas Cort #include <stdarg.h>
38a0e6850fSThomas Cort #include <form.h>
39a0e6850fSThomas Cort #include "internals.h"
40a0e6850fSThomas Cort
41a0e6850fSThomas Cort extern FORM _formi_default_form;
42a0e6850fSThomas Cort
43a0e6850fSThomas Cort FIELD _formi_default_field = {
44a0e6850fSThomas Cort 0, /* rows in the field */
45a0e6850fSThomas Cort 0, /* columns in the field */
46a0e6850fSThomas Cort 0, /* dynamic rows */
47a0e6850fSThomas Cort 0, /* dynamic columns */
48a0e6850fSThomas Cort 0, /* maximum growth */
49a0e6850fSThomas Cort 0, /* starting row in the form subwindow */
50a0e6850fSThomas Cort 0, /* starting column in the form subwindow */
51a0e6850fSThomas Cort 0, /* number of off screen rows */
52a0e6850fSThomas Cort 0, /* index of this field in form fields array. */
53a0e6850fSThomas Cort 0, /* number of buffers associated with this field */
54a0e6850fSThomas Cort FALSE, /* set to true if buffer 0 has changed. */
55a0e6850fSThomas Cort NO_JUSTIFICATION, /* justification style of the field */
56a0e6850fSThomas Cort FALSE, /* set to true if field is in overlay mode */
57a0e6850fSThomas Cort NULL, /* pointer to the current line cursor is on */
58a0e6850fSThomas Cort 0, /* starting char in string (horiz scroll) */
59a0e6850fSThomas Cort NULL, /* starting line in field (vert scroll) */
60a0e6850fSThomas Cort 0, /* number of rows actually used in field */
61a0e6850fSThomas Cort 0, /* actual pos of cursor in row, not same as x pos due to tabs */
62a0e6850fSThomas Cort 0, /* x pos of cursor in field */
63a0e6850fSThomas Cort 0, /* y pos of cursor in field */
64a0e6850fSThomas Cort 0, /* start of a new page on the form if 1 */
65a0e6850fSThomas Cort 0, /* number of the page this field is on */
66a0e6850fSThomas Cort A_NORMAL, /* character attributes for the foreground */
67a0e6850fSThomas Cort A_NORMAL, /* character attributes for the background */
68a0e6850fSThomas Cort ' ', /* padding character */
69a0e6850fSThomas Cort DEFAULT_FORM_OPTS, /* options for the field */
70a0e6850fSThomas Cort NULL, /* the form this field is bound to, if any */
71a0e6850fSThomas Cort NULL, /* field above this one */
72a0e6850fSThomas Cort NULL, /* field below this one */
73a0e6850fSThomas Cort NULL, /* field to the left of this one */
74a0e6850fSThomas Cort NULL, /* field to the right of this one */
75a0e6850fSThomas Cort NULL, /* user defined pointer. */
76a0e6850fSThomas Cort NULL, /* used if fields are linked */
77a0e6850fSThomas Cort NULL, /* type struct for the field */
78a0e6850fSThomas Cort {NULL, NULL}, /* circle queue glue for sorting fields */
79a0e6850fSThomas Cort NULL, /* args for field type. */
80a0e6850fSThomas Cort NULL, /* pointer to the array of lines structures. */
81a0e6850fSThomas Cort NULL, /* list of lines available for reuse */
82a0e6850fSThomas Cort NULL, /* array of buffers for the field */
83a0e6850fSThomas Cort };
84a0e6850fSThomas Cort
85a0e6850fSThomas Cort /* internal function prototypes */
86a0e6850fSThomas Cort static int
87a0e6850fSThomas Cort field_buffer_init(FIELD *field, int buffer, unsigned int len);
88a0e6850fSThomas Cort static FIELD *
89a0e6850fSThomas Cort _formi_create_field(FIELD *, int, int, int, int, int, int);
90a0e6850fSThomas Cort
91a0e6850fSThomas Cort
92a0e6850fSThomas Cort /*
93a0e6850fSThomas Cort * Set the userptr for the field
94a0e6850fSThomas Cort */
95a0e6850fSThomas Cort int
set_field_userptr(FIELD * field,void * ptr)96a0e6850fSThomas Cort set_field_userptr(FIELD *field, void *ptr)
97a0e6850fSThomas Cort {
98a0e6850fSThomas Cort FIELD *fp = (field == NULL) ? &_formi_default_field : field;
99a0e6850fSThomas Cort
100a0e6850fSThomas Cort fp->userptr = ptr;
101a0e6850fSThomas Cort
102a0e6850fSThomas Cort return E_OK;
103a0e6850fSThomas Cort }
104a0e6850fSThomas Cort
105a0e6850fSThomas Cort /*
106a0e6850fSThomas Cort * Return the userptr for the field.
107a0e6850fSThomas Cort */
108a0e6850fSThomas Cort
109a0e6850fSThomas Cort void *
field_userptr(FIELD * field)110a0e6850fSThomas Cort field_userptr(FIELD *field)
111a0e6850fSThomas Cort {
112a0e6850fSThomas Cort if (field == NULL)
113a0e6850fSThomas Cort return _formi_default_field.userptr;
114a0e6850fSThomas Cort else
115a0e6850fSThomas Cort return field->userptr;
116a0e6850fSThomas Cort }
117a0e6850fSThomas Cort
118a0e6850fSThomas Cort /*
119a0e6850fSThomas Cort * Set the options for the designated field.
120a0e6850fSThomas Cort */
121a0e6850fSThomas Cort int
set_field_opts(FIELD * field,Form_Options options)122a0e6850fSThomas Cort set_field_opts(FIELD *field, Form_Options options)
123a0e6850fSThomas Cort {
124a0e6850fSThomas Cort int i;
125a0e6850fSThomas Cort
126a0e6850fSThomas Cort FIELD *fp = (field == NULL) ? &_formi_default_field : field;
127a0e6850fSThomas Cort
128a0e6850fSThomas Cort /* not allowed to set opts if the field is the current one */
129a0e6850fSThomas Cort if ((field != NULL) && (field->parent != NULL) &&
130a0e6850fSThomas Cort (field->parent->cur_field == field->index))
131a0e6850fSThomas Cort return E_CURRENT;
132a0e6850fSThomas Cort
133a0e6850fSThomas Cort if ((options & O_STATIC) == O_STATIC) {
134a0e6850fSThomas Cort for (i = 0; i < fp->nbuf; i++) {
135a0e6850fSThomas Cort if (fp->buffers[i].length > fp->cols)
136a0e6850fSThomas Cort fp->buffers[i].string[fp->cols] = '\0';
137a0e6850fSThomas Cort }
138a0e6850fSThomas Cort }
139a0e6850fSThomas Cort
140a0e6850fSThomas Cort fp->opts = options;
141a0e6850fSThomas Cort
142a0e6850fSThomas Cort /* if appropriate, redraw the field */
143a0e6850fSThomas Cort if ((field != NULL) && (field->parent != NULL)
144a0e6850fSThomas Cort && (field->parent->posted == 1)) {
145a0e6850fSThomas Cort _formi_redraw_field(field->parent, field->index);
146a0e6850fSThomas Cort pos_form_cursor(field->parent);
147a0e6850fSThomas Cort wrefresh(field->parent->scrwin);
148a0e6850fSThomas Cort }
149a0e6850fSThomas Cort
150a0e6850fSThomas Cort return E_OK;
151a0e6850fSThomas Cort }
152a0e6850fSThomas Cort
153a0e6850fSThomas Cort /*
154a0e6850fSThomas Cort * Turn on the passed field options.
155a0e6850fSThomas Cort */
156a0e6850fSThomas Cort int
field_opts_on(FIELD * field,Form_Options options)157a0e6850fSThomas Cort field_opts_on(FIELD *field, Form_Options options)
158a0e6850fSThomas Cort {
159a0e6850fSThomas Cort int i;
160a0e6850fSThomas Cort
161a0e6850fSThomas Cort FIELD *fp = (field == NULL) ? &_formi_default_field : field;
162a0e6850fSThomas Cort
163a0e6850fSThomas Cort /* not allowed to set opts if the field is the current one */
164a0e6850fSThomas Cort if ((field != NULL) && (field->parent != NULL) &&
165a0e6850fSThomas Cort (field->parent->cur_field == field->index))
166a0e6850fSThomas Cort return E_CURRENT;
167a0e6850fSThomas Cort
168a0e6850fSThomas Cort if ((options & O_STATIC) == O_STATIC) {
169a0e6850fSThomas Cort for (i = 0; i < fp->nbuf; i++) {
170a0e6850fSThomas Cort if (fp->buffers[i].length > fp->cols)
171a0e6850fSThomas Cort fp->buffers[i].string[fp->cols] = '\0';
172a0e6850fSThomas Cort }
173a0e6850fSThomas Cort }
174a0e6850fSThomas Cort
175a0e6850fSThomas Cort fp->opts |= options;
176a0e6850fSThomas Cort
177a0e6850fSThomas Cort /* if appropriate, redraw the field */
178a0e6850fSThomas Cort if ((field != NULL) && (field->parent != NULL)
179a0e6850fSThomas Cort && (field->parent->posted == 1)) {
180a0e6850fSThomas Cort _formi_redraw_field(field->parent, field->index);
181a0e6850fSThomas Cort pos_form_cursor(field->parent);
182a0e6850fSThomas Cort wrefresh(field->parent->scrwin);
183a0e6850fSThomas Cort }
184a0e6850fSThomas Cort
185a0e6850fSThomas Cort return E_OK;
186a0e6850fSThomas Cort }
187a0e6850fSThomas Cort
188a0e6850fSThomas Cort /*
189a0e6850fSThomas Cort * Turn off the passed field options.
190a0e6850fSThomas Cort */
191a0e6850fSThomas Cort int
field_opts_off(FIELD * field,Form_Options options)192a0e6850fSThomas Cort field_opts_off(FIELD *field, Form_Options options)
193a0e6850fSThomas Cort {
194a0e6850fSThomas Cort FIELD *fp = (field == NULL) ? &_formi_default_field : field;
195a0e6850fSThomas Cort
196a0e6850fSThomas Cort /* not allowed to set opts if the field is the current one */
197a0e6850fSThomas Cort if ((field != NULL) && (field->parent != NULL) &&
198a0e6850fSThomas Cort (field->parent->cur_field == field->index))
199a0e6850fSThomas Cort return E_CURRENT;
200a0e6850fSThomas Cort
201a0e6850fSThomas Cort fp->opts &= ~options;
202a0e6850fSThomas Cort
203a0e6850fSThomas Cort /* if appropriate, redraw the field */
204a0e6850fSThomas Cort if ((field != NULL) && (field->parent != NULL)
205a0e6850fSThomas Cort && (field->parent->posted == 1)) {
206a0e6850fSThomas Cort _formi_redraw_field(field->parent, field->index);
207a0e6850fSThomas Cort pos_form_cursor(field->parent);
208a0e6850fSThomas Cort wrefresh(field->parent->scrwin);
209a0e6850fSThomas Cort }
210a0e6850fSThomas Cort
211a0e6850fSThomas Cort return E_OK;
212a0e6850fSThomas Cort }
213a0e6850fSThomas Cort
214a0e6850fSThomas Cort /*
215a0e6850fSThomas Cort * Return the field options associated with the passed field.
216a0e6850fSThomas Cort */
217a0e6850fSThomas Cort Form_Options
field_opts(FIELD * field)218a0e6850fSThomas Cort field_opts(FIELD *field)
219a0e6850fSThomas Cort {
220a0e6850fSThomas Cort if (field == NULL)
221a0e6850fSThomas Cort return _formi_default_field.opts;
222a0e6850fSThomas Cort else
223a0e6850fSThomas Cort return field->opts;
224a0e6850fSThomas Cort }
225a0e6850fSThomas Cort
226a0e6850fSThomas Cort /*
227a0e6850fSThomas Cort * Set the justification for the passed field.
228a0e6850fSThomas Cort */
229a0e6850fSThomas Cort int
set_field_just(FIELD * field,int justification)230a0e6850fSThomas Cort set_field_just(FIELD *field, int justification)
231a0e6850fSThomas Cort {
232a0e6850fSThomas Cort FIELD *fp = (field == NULL) ? &_formi_default_field : field;
233a0e6850fSThomas Cort
234a0e6850fSThomas Cort /*
235a0e6850fSThomas Cort * not allowed to set justification if the field is
236a0e6850fSThomas Cort * the current one
237a0e6850fSThomas Cort */
238a0e6850fSThomas Cort if ((field != NULL) && (field->parent != NULL) &&
239a0e6850fSThomas Cort (field->parent->cur_field == field->index))
240a0e6850fSThomas Cort return E_CURRENT;
241a0e6850fSThomas Cort
242a0e6850fSThomas Cort if ((justification < MIN_JUST_STYLE) /* check justification valid */
243a0e6850fSThomas Cort || (justification > MAX_JUST_STYLE))
244a0e6850fSThomas Cort return E_BAD_ARGUMENT;
245a0e6850fSThomas Cort
246a0e6850fSThomas Cort /* only allow justification on static, single row fields */
247a0e6850fSThomas Cort if (((fp->opts & O_STATIC) != O_STATIC) ||
248a0e6850fSThomas Cort ((fp->rows + fp->nrows) > 1))
249a0e6850fSThomas Cort return E_BAD_ARGUMENT;
250a0e6850fSThomas Cort
251a0e6850fSThomas Cort fp->justification = justification;
252a0e6850fSThomas Cort
253a0e6850fSThomas Cort _formi_init_field_xpos(fp);
254a0e6850fSThomas Cort
255a0e6850fSThomas Cort return E_OK;
256a0e6850fSThomas Cort }
257a0e6850fSThomas Cort
258a0e6850fSThomas Cort /*
259a0e6850fSThomas Cort * Return the justification style of the field passed.
260a0e6850fSThomas Cort */
261a0e6850fSThomas Cort int
field_just(FIELD * field)262a0e6850fSThomas Cort field_just(FIELD *field)
263a0e6850fSThomas Cort {
264a0e6850fSThomas Cort if (field == NULL)
265a0e6850fSThomas Cort return _formi_default_field.justification;
266a0e6850fSThomas Cort else
267a0e6850fSThomas Cort return field->justification;
268a0e6850fSThomas Cort }
269a0e6850fSThomas Cort
270a0e6850fSThomas Cort /*
271a0e6850fSThomas Cort * Return information about the field passed.
272a0e6850fSThomas Cort */
273a0e6850fSThomas Cort int
field_info(FIELD * field,int * rows,int * cols,int * frow,int * fcol,int * nrow,int * nbuf)274a0e6850fSThomas Cort field_info(FIELD *field, int *rows, int *cols, int *frow, int *fcol,
275a0e6850fSThomas Cort int *nrow, int *nbuf)
276a0e6850fSThomas Cort {
277a0e6850fSThomas Cort if (field == NULL)
278a0e6850fSThomas Cort return E_BAD_ARGUMENT;
279a0e6850fSThomas Cort
280a0e6850fSThomas Cort *rows = field->rows;
281a0e6850fSThomas Cort *cols = field->cols;
282a0e6850fSThomas Cort *frow = field->form_row;
283a0e6850fSThomas Cort *fcol = field->form_col;
284a0e6850fSThomas Cort *nrow = field->nrows;
285a0e6850fSThomas Cort *nbuf = field->nbuf;
286a0e6850fSThomas Cort
287a0e6850fSThomas Cort return E_OK;
288a0e6850fSThomas Cort }
289a0e6850fSThomas Cort
290a0e6850fSThomas Cort /*
291a0e6850fSThomas Cort * Report the dynamic field information.
292a0e6850fSThomas Cort */
293a0e6850fSThomas Cort int
dynamic_field_info(FIELD * field,int * drows,int * dcols,int * max)294a0e6850fSThomas Cort dynamic_field_info(FIELD *field, int *drows, int *dcols, int *max)
295a0e6850fSThomas Cort {
296a0e6850fSThomas Cort if (field == NULL)
297a0e6850fSThomas Cort return E_BAD_ARGUMENT;
298a0e6850fSThomas Cort
299a0e6850fSThomas Cort if ((field->opts & O_STATIC) == O_STATIC) {
300a0e6850fSThomas Cort *drows = field->rows;
301a0e6850fSThomas Cort *dcols = field->cols;
302a0e6850fSThomas Cort } else {
303a0e6850fSThomas Cort *drows = field->drows;
304a0e6850fSThomas Cort *dcols = field->dcols;
305a0e6850fSThomas Cort }
306a0e6850fSThomas Cort
307a0e6850fSThomas Cort *max = field->max;
308a0e6850fSThomas Cort
309a0e6850fSThomas Cort return E_OK;
310a0e6850fSThomas Cort }
311a0e6850fSThomas Cort
312a0e6850fSThomas Cort /*
313a0e6850fSThomas Cort * Init all the field variables, perform wrapping and other tasks
314a0e6850fSThomas Cort * after the field buffer is set.
315a0e6850fSThomas Cort */
316a0e6850fSThomas Cort static int
field_buffer_init(FIELD * field,int buffer,unsigned int len)317a0e6850fSThomas Cort field_buffer_init(FIELD *field, int buffer, unsigned int len)
318a0e6850fSThomas Cort {
319a0e6850fSThomas Cort int status;
320a0e6850fSThomas Cort char *newp;
321a0e6850fSThomas Cort
322a0e6850fSThomas Cort if (buffer == 0) {
323a0e6850fSThomas Cort field->start_char = 0;
324a0e6850fSThomas Cort field->start_line = 0;
325a0e6850fSThomas Cort field->row_xpos = 0;
326a0e6850fSThomas Cort field->cursor_xpos = 0;
327a0e6850fSThomas Cort field->cursor_ypos = 0;
328a0e6850fSThomas Cort field->row_count = 1; /* must be at least one row XXX need to shift old rows (if any) to free list??? */
329a0e6850fSThomas Cort field->alines->length = len;
330a0e6850fSThomas Cort if ((newp = realloc(field->alines->string,
331a0e6850fSThomas Cort (size_t) len + 1)) == NULL)
332a0e6850fSThomas Cort return E_SYSTEM_ERROR;
333a0e6850fSThomas Cort field->alines->string = newp;
334a0e6850fSThomas Cort field->alines->allocated = len + 1;
335a0e6850fSThomas Cort strlcpy(field->alines->string, field->buffers[buffer].string,
336a0e6850fSThomas Cort (size_t) len + 1);
337a0e6850fSThomas Cort field->alines->expanded =
338a0e6850fSThomas Cort _formi_tab_expanded_length(field->alines->string,
339a0e6850fSThomas Cort 0, field->alines->length);
340a0e6850fSThomas Cort
341a0e6850fSThomas Cort field->start_line = field->alines;
342a0e6850fSThomas Cort field->cur_line = field->alines;
343a0e6850fSThomas Cort
344a0e6850fSThomas Cort /* we have to hope the wrap works - if it does not then the
345a0e6850fSThomas Cort buffer is pretty much borked */
346a0e6850fSThomas Cort status = _formi_wrap_field(field, field->cur_line);
347a0e6850fSThomas Cort if (status != E_OK)
348a0e6850fSThomas Cort return status;
349a0e6850fSThomas Cort
350a0e6850fSThomas Cort /*
351a0e6850fSThomas Cort * calculate the tabs for a single row field, the
352a0e6850fSThomas Cort * multiline case is handled when the wrap is done.
353a0e6850fSThomas Cort */
354a0e6850fSThomas Cort if (field->row_count == 1)
355a0e6850fSThomas Cort _formi_calculate_tabs(field->alines);
356a0e6850fSThomas Cort
357a0e6850fSThomas Cort /* redraw the field to reflect the new contents. If the field
358a0e6850fSThomas Cort * is attached....
359a0e6850fSThomas Cort */
360a0e6850fSThomas Cort if ((field->parent != NULL) && (field->parent->posted == 1)) {
361a0e6850fSThomas Cort _formi_redraw_field(field->parent, field->index);
362a0e6850fSThomas Cort /* make sure cursor goes back to current field */
363a0e6850fSThomas Cort pos_form_cursor(field->parent);
364a0e6850fSThomas Cort }
365a0e6850fSThomas Cort }
366a0e6850fSThomas Cort
367a0e6850fSThomas Cort return E_OK;
368a0e6850fSThomas Cort }
369a0e6850fSThomas Cort
370a0e6850fSThomas Cort
371a0e6850fSThomas Cort /*
372a0e6850fSThomas Cort * Set the field buffer to the string that results from processing
373a0e6850fSThomas Cort * the given format (fmt) using sprintf.
374a0e6850fSThomas Cort */
375a0e6850fSThomas Cort int
set_field_printf(FIELD * field,int buffer,char * fmt,...)376a0e6850fSThomas Cort set_field_printf(FIELD *field, int buffer, char *fmt, ...)
377a0e6850fSThomas Cort {
378a0e6850fSThomas Cort int len;
379a0e6850fSThomas Cort va_list args;
380a0e6850fSThomas Cort
381a0e6850fSThomas Cort if (field == NULL)
382a0e6850fSThomas Cort return E_BAD_ARGUMENT;
383a0e6850fSThomas Cort
384a0e6850fSThomas Cort if (buffer >= field->nbuf)
385a0e6850fSThomas Cort return E_BAD_ARGUMENT;
386a0e6850fSThomas Cort
387a0e6850fSThomas Cort va_start(args, fmt);
388a0e6850fSThomas Cort /* check for buffer already existing, free the storage */
389a0e6850fSThomas Cort if (field->buffers[buffer].allocated != 0)
390a0e6850fSThomas Cort free(field->buffers[buffer].string);
391a0e6850fSThomas Cort
392a0e6850fSThomas Cort len = vasprintf(&field->buffers[buffer].string, fmt, args);
393a0e6850fSThomas Cort va_end(args);
394a0e6850fSThomas Cort if (len < 0)
395a0e6850fSThomas Cort return E_SYSTEM_ERROR;
396a0e6850fSThomas Cort
397a0e6850fSThomas Cort field->buffers[buffer].length = len;
398a0e6850fSThomas Cort field->buffers[buffer].allocated = len + 1;
399a0e6850fSThomas Cort if (((field->opts & O_STATIC) == O_STATIC) && (len > field->cols)
400a0e6850fSThomas Cort && ((field->rows + field->nrows) == 1))
401a0e6850fSThomas Cort len = field->cols;
402a0e6850fSThomas Cort
403a0e6850fSThomas Cort field->buffers[buffer].string[len] = '\0';
404a0e6850fSThomas Cort return field_buffer_init(field, buffer, (unsigned int) len);
405a0e6850fSThomas Cort }
406a0e6850fSThomas Cort
407a0e6850fSThomas Cort /*
408a0e6850fSThomas Cort * Set the value of the field buffer to the value given.
409a0e6850fSThomas Cort */
410a0e6850fSThomas Cort
411a0e6850fSThomas Cort int
set_field_buffer(FIELD * field,int buffer,const char * value)412*0a6a1f1dSLionel Sambuc set_field_buffer(FIELD *field, int buffer, const char *value)
413a0e6850fSThomas Cort {
414a0e6850fSThomas Cort unsigned int len;
415a0e6850fSThomas Cort int status;
416a0e6850fSThomas Cort
417a0e6850fSThomas Cort if (field == NULL)
418a0e6850fSThomas Cort return E_BAD_ARGUMENT;
419a0e6850fSThomas Cort
420a0e6850fSThomas Cort if (buffer >= field->nbuf) /* make sure buffer is valid */
421a0e6850fSThomas Cort return E_BAD_ARGUMENT;
422a0e6850fSThomas Cort
423a0e6850fSThomas Cort len = (unsigned int) strlen(value);
424a0e6850fSThomas Cort if (((field->opts & O_STATIC) == O_STATIC) && (len > field->cols)
425a0e6850fSThomas Cort && ((field->rows + field->nrows) == 1))
426a0e6850fSThomas Cort len = field->cols;
427a0e6850fSThomas Cort
428a0e6850fSThomas Cort #ifdef DEBUG
429a0e6850fSThomas Cort if (_formi_create_dbg_file() != E_OK)
430a0e6850fSThomas Cort return E_SYSTEM_ERROR;
431a0e6850fSThomas Cort
432a0e6850fSThomas Cort fprintf(dbg,
433a0e6850fSThomas Cort "set_field_buffer: entry: len = %d, value = %s, buffer=%d\n",
434a0e6850fSThomas Cort len, value, buffer);
435a0e6850fSThomas Cort fprintf(dbg, "set_field_buffer: entry: string = ");
436a0e6850fSThomas Cort if (field->buffers[buffer].string != NULL)
437a0e6850fSThomas Cort fprintf(dbg, "%s, len = %d\n", field->buffers[buffer].string,
438a0e6850fSThomas Cort field->buffers[buffer].length);
439a0e6850fSThomas Cort else
440a0e6850fSThomas Cort fprintf(dbg, "(null), len = 0\n");
441a0e6850fSThomas Cort fprintf(dbg, "set_field_buffer: entry: lines.len = %d\n",
442a0e6850fSThomas Cort field->alines[0].length);
443a0e6850fSThomas Cort #endif
444a0e6850fSThomas Cort
445a0e6850fSThomas Cort if ((field->buffers[buffer].string =
446a0e6850fSThomas Cort (char *) realloc(field->buffers[buffer].string,
447a0e6850fSThomas Cort (size_t) len + 1)) == NULL)
448a0e6850fSThomas Cort return E_SYSTEM_ERROR;
449a0e6850fSThomas Cort
450a0e6850fSThomas Cort strlcpy(field->buffers[buffer].string, value, (size_t) len + 1);
451a0e6850fSThomas Cort field->buffers[buffer].length = len;
452a0e6850fSThomas Cort field->buffers[buffer].allocated = len + 1;
453a0e6850fSThomas Cort status = field_buffer_init(field, buffer, len);
454a0e6850fSThomas Cort
455a0e6850fSThomas Cort #ifdef DEBUG
456a0e6850fSThomas Cort fprintf(dbg, "set_field_buffer: exit: len = %d, value = %s\n",
457a0e6850fSThomas Cort len, value);
458a0e6850fSThomas Cort fprintf(dbg, "set_field_buffer: exit: string = %s, len = %d\n",
459a0e6850fSThomas Cort field->buffers[buffer].string, field->buffers[buffer].length);
460a0e6850fSThomas Cort fprintf(dbg, "set_field_buffer: exit: lines.len = %d\n",
461a0e6850fSThomas Cort field->alines[0].length);
462a0e6850fSThomas Cort #endif
463a0e6850fSThomas Cort
464a0e6850fSThomas Cort return status;
465a0e6850fSThomas Cort }
466a0e6850fSThomas Cort
467a0e6850fSThomas Cort /*
468a0e6850fSThomas Cort * Return the requested field buffer to the caller.
469a0e6850fSThomas Cort */
470a0e6850fSThomas Cort char *
field_buffer(FIELD * field,int buffer)471a0e6850fSThomas Cort field_buffer(FIELD *field, int buffer)
472a0e6850fSThomas Cort {
473a0e6850fSThomas Cort
474a0e6850fSThomas Cort char *reformat, *p;
475a0e6850fSThomas Cort _FORMI_FIELD_LINES *linep;
47684d9c625SLionel Sambuc size_t bufsize, pos;
477a0e6850fSThomas Cort
478a0e6850fSThomas Cort if (field == NULL)
479a0e6850fSThomas Cort return NULL;
480a0e6850fSThomas Cort
481a0e6850fSThomas Cort if (buffer >= field->nbuf)
482a0e6850fSThomas Cort return NULL;
483a0e6850fSThomas Cort
484a0e6850fSThomas Cort /*
485a0e6850fSThomas Cort * We force a sync from the line structs to the buffer here.
486a0e6850fSThomas Cort * Traditional libform say we don't need to because it is
487a0e6850fSThomas Cort * done on a REQ_VALIDATE but NetBSD libform previously did
488a0e6850fSThomas Cort * not enforce this because the buffer contents were always
489a0e6850fSThomas Cort * current. Changes to line handling make this no longer so
490a0e6850fSThomas Cort * - the line structs may contain different data to the
491a0e6850fSThomas Cort * buffer if unsynced.
492a0e6850fSThomas Cort */
493a0e6850fSThomas Cort if (_formi_sync_buffer(field) != E_OK)
494a0e6850fSThomas Cort return NULL;
495a0e6850fSThomas Cort
49684d9c625SLionel Sambuc if ((field->opts & O_REFORMAT) != O_REFORMAT)
497a0e6850fSThomas Cort return field->buffers[buffer].string;
498a0e6850fSThomas Cort
49984d9c625SLionel Sambuc if (field->row_count <= 1)
50084d9c625SLionel Sambuc return strdup(field->buffers[buffer].string);
501a0e6850fSThomas Cort
502a0e6850fSThomas Cort /*
50384d9c625SLionel Sambuc * create a single string containing each line,
50484d9c625SLionel Sambuc * separated by newline, last line having no
50584d9c625SLionel Sambuc * newline, but NUL terminated.
506a0e6850fSThomas Cort */
50784d9c625SLionel Sambuc bufsize = pos = 0;
50884d9c625SLionel Sambuc reformat = NULL;
50984d9c625SLionel Sambuc for (linep = field->alines; linep; linep = linep->next) {
51084d9c625SLionel Sambuc size_t len = strlen(linep->string);
51184d9c625SLionel Sambuc if (len + 1 >= bufsize - pos) {
51284d9c625SLionel Sambuc bufsize += MAX(1024, 2 * len);
51384d9c625SLionel Sambuc p = realloc(reformat, bufsize);
51484d9c625SLionel Sambuc if (p == NULL) {
51584d9c625SLionel Sambuc free(reformat);
51684d9c625SLionel Sambuc return NULL;
517a0e6850fSThomas Cort }
51884d9c625SLionel Sambuc reformat = p;
519a0e6850fSThomas Cort }
52084d9c625SLionel Sambuc memcpy(reformat + pos, linep->string, len);
52184d9c625SLionel Sambuc pos += len;
52284d9c625SLionel Sambuc reformat[pos++] = linep->next ? '\n' : '\0';
52384d9c625SLionel Sambuc }
524a0e6850fSThomas Cort return reformat;
525a0e6850fSThomas Cort }
526a0e6850fSThomas Cort
527a0e6850fSThomas Cort /*
528a0e6850fSThomas Cort * Set the buffer 0 field status.
529a0e6850fSThomas Cort */
530a0e6850fSThomas Cort int
set_field_status(FIELD * field,int status)531a0e6850fSThomas Cort set_field_status(FIELD *field, int status)
532a0e6850fSThomas Cort {
533a0e6850fSThomas Cort
534a0e6850fSThomas Cort if (field == NULL)
535a0e6850fSThomas Cort return E_BAD_ARGUMENT;
536a0e6850fSThomas Cort
537a0e6850fSThomas Cort if (status != FALSE)
538a0e6850fSThomas Cort field->buf0_status = TRUE;
539a0e6850fSThomas Cort else
540a0e6850fSThomas Cort field->buf0_status = FALSE;
541a0e6850fSThomas Cort
542a0e6850fSThomas Cort return E_OK;
543a0e6850fSThomas Cort }
544a0e6850fSThomas Cort
545a0e6850fSThomas Cort /*
546a0e6850fSThomas Cort * Return the buffer 0 status flag for the given field.
547a0e6850fSThomas Cort */
548a0e6850fSThomas Cort int
field_status(FIELD * field)549a0e6850fSThomas Cort field_status(FIELD *field)
550a0e6850fSThomas Cort {
551a0e6850fSThomas Cort
552a0e6850fSThomas Cort if (field == NULL) /* the default buffer 0 never changes :-) */
553a0e6850fSThomas Cort return FALSE;
554a0e6850fSThomas Cort
555a0e6850fSThomas Cort return field->buf0_status;
556a0e6850fSThomas Cort }
557a0e6850fSThomas Cort
558a0e6850fSThomas Cort /*
559a0e6850fSThomas Cort * Set the maximum growth for a dynamic field.
560a0e6850fSThomas Cort */
561a0e6850fSThomas Cort int
set_max_field(FIELD * fptr,int max)562a0e6850fSThomas Cort set_max_field(FIELD *fptr, int max)
563a0e6850fSThomas Cort {
564a0e6850fSThomas Cort FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;
565a0e6850fSThomas Cort
566a0e6850fSThomas Cort if ((field->opts & O_STATIC) == O_STATIC) /* check if field dynamic */
567a0e6850fSThomas Cort return E_BAD_ARGUMENT;
568a0e6850fSThomas Cort
569a0e6850fSThomas Cort if (max < 0) /* negative numbers are bad.... */
570a0e6850fSThomas Cort return E_BAD_ARGUMENT;
571a0e6850fSThomas Cort
572a0e6850fSThomas Cort field->max = max;
573a0e6850fSThomas Cort return E_OK;
574a0e6850fSThomas Cort }
575a0e6850fSThomas Cort
576a0e6850fSThomas Cort /*
577a0e6850fSThomas Cort * Set the field foreground character attributes.
578a0e6850fSThomas Cort */
579a0e6850fSThomas Cort int
set_field_fore(FIELD * fptr,chtype attribute)580a0e6850fSThomas Cort set_field_fore(FIELD *fptr, chtype attribute)
581a0e6850fSThomas Cort {
582a0e6850fSThomas Cort FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;
583a0e6850fSThomas Cort
584a0e6850fSThomas Cort field->fore = attribute;
585a0e6850fSThomas Cort return E_OK;
586a0e6850fSThomas Cort }
587a0e6850fSThomas Cort
588a0e6850fSThomas Cort /*
589a0e6850fSThomas Cort * Return the foreground character attribute for the given field.
590a0e6850fSThomas Cort */
591a0e6850fSThomas Cort chtype
field_fore(FIELD * field)592a0e6850fSThomas Cort field_fore(FIELD *field)
593a0e6850fSThomas Cort {
594a0e6850fSThomas Cort if (field == NULL)
595a0e6850fSThomas Cort return _formi_default_field.fore;
596a0e6850fSThomas Cort else
597a0e6850fSThomas Cort return field->fore;
598a0e6850fSThomas Cort }
599a0e6850fSThomas Cort
600a0e6850fSThomas Cort /*
601a0e6850fSThomas Cort * Set the background character attribute for the given field.
602a0e6850fSThomas Cort */
603a0e6850fSThomas Cort int
set_field_back(FIELD * field,chtype attribute)604a0e6850fSThomas Cort set_field_back(FIELD *field, chtype attribute)
605a0e6850fSThomas Cort {
606a0e6850fSThomas Cort if (field == NULL)
607a0e6850fSThomas Cort _formi_default_field.back = attribute;
608a0e6850fSThomas Cort else
609a0e6850fSThomas Cort field->back = attribute;
610a0e6850fSThomas Cort
611a0e6850fSThomas Cort return E_OK;
612a0e6850fSThomas Cort }
613a0e6850fSThomas Cort
614a0e6850fSThomas Cort /*
615a0e6850fSThomas Cort * Get the background character attribute for the given field.
616a0e6850fSThomas Cort */
617a0e6850fSThomas Cort chtype
field_back(FIELD * field)618a0e6850fSThomas Cort field_back(FIELD *field)
619a0e6850fSThomas Cort {
620a0e6850fSThomas Cort if (field == NULL)
621a0e6850fSThomas Cort return _formi_default_field.back;
622a0e6850fSThomas Cort else
623a0e6850fSThomas Cort return field->back;
624a0e6850fSThomas Cort }
625a0e6850fSThomas Cort
626a0e6850fSThomas Cort /*
627a0e6850fSThomas Cort * Set the pad character for the given field.
628a0e6850fSThomas Cort */
629a0e6850fSThomas Cort int
set_field_pad(FIELD * field,int pad)630a0e6850fSThomas Cort set_field_pad(FIELD *field, int pad)
631a0e6850fSThomas Cort {
632a0e6850fSThomas Cort if (field == NULL)
633a0e6850fSThomas Cort _formi_default_field.pad = pad;
634a0e6850fSThomas Cort else
635a0e6850fSThomas Cort field->pad = pad;
636a0e6850fSThomas Cort
637a0e6850fSThomas Cort return E_OK;
638a0e6850fSThomas Cort }
639a0e6850fSThomas Cort
640a0e6850fSThomas Cort /*
641a0e6850fSThomas Cort * Return the padding character for the given field.
642a0e6850fSThomas Cort */
643a0e6850fSThomas Cort int
field_pad(FIELD * field)644a0e6850fSThomas Cort field_pad(FIELD *field)
645a0e6850fSThomas Cort {
646a0e6850fSThomas Cort if (field == NULL)
647a0e6850fSThomas Cort return _formi_default_field.pad;
648a0e6850fSThomas Cort else
649a0e6850fSThomas Cort return field->pad;
650a0e6850fSThomas Cort }
651a0e6850fSThomas Cort
652a0e6850fSThomas Cort /*
653a0e6850fSThomas Cort * Set the field initialisation function hook.
654a0e6850fSThomas Cort */
655a0e6850fSThomas Cort int
set_field_init(FORM * form,Form_Hook function)656a0e6850fSThomas Cort set_field_init(FORM *form, Form_Hook function)
657a0e6850fSThomas Cort {
658a0e6850fSThomas Cort if (form == NULL)
659a0e6850fSThomas Cort _formi_default_form.field_init = function;
660a0e6850fSThomas Cort else
661a0e6850fSThomas Cort form->field_init = function;
662a0e6850fSThomas Cort
663a0e6850fSThomas Cort return E_OK;
664a0e6850fSThomas Cort }
665a0e6850fSThomas Cort
666a0e6850fSThomas Cort /*
667a0e6850fSThomas Cort * Return the function hook for the field initialisation.
668a0e6850fSThomas Cort */
669a0e6850fSThomas Cort Form_Hook
field_init(FORM * form)670a0e6850fSThomas Cort field_init(FORM *form)
671a0e6850fSThomas Cort {
672a0e6850fSThomas Cort if (form == NULL)
673a0e6850fSThomas Cort return _formi_default_form.field_init;
674a0e6850fSThomas Cort else
675a0e6850fSThomas Cort return form->field_init;
676a0e6850fSThomas Cort }
677a0e6850fSThomas Cort
678a0e6850fSThomas Cort /*
679a0e6850fSThomas Cort * Set the field termination function hook.
680a0e6850fSThomas Cort */
681a0e6850fSThomas Cort int
set_field_term(FORM * form,Form_Hook function)682a0e6850fSThomas Cort set_field_term(FORM *form, Form_Hook function)
683a0e6850fSThomas Cort {
684a0e6850fSThomas Cort if (form == NULL)
685a0e6850fSThomas Cort _formi_default_form.field_term = function;
686a0e6850fSThomas Cort else
687a0e6850fSThomas Cort form->field_term = function;
688a0e6850fSThomas Cort
689a0e6850fSThomas Cort return E_OK;
690a0e6850fSThomas Cort }
691a0e6850fSThomas Cort
692a0e6850fSThomas Cort /*
693a0e6850fSThomas Cort * Return the function hook defined for the field termination.
694a0e6850fSThomas Cort */
695a0e6850fSThomas Cort Form_Hook
field_term(FORM * form)696a0e6850fSThomas Cort field_term(FORM *form)
697a0e6850fSThomas Cort {
698a0e6850fSThomas Cort if (form == NULL)
699a0e6850fSThomas Cort return _formi_default_form.field_term;
700a0e6850fSThomas Cort else
701a0e6850fSThomas Cort return form->field_term;
702a0e6850fSThomas Cort }
703a0e6850fSThomas Cort
704a0e6850fSThomas Cort /*
705a0e6850fSThomas Cort * Set the page flag on the given field to indicate it is the start of a
706a0e6850fSThomas Cort * new page.
707a0e6850fSThomas Cort */
708a0e6850fSThomas Cort int
set_new_page(FIELD * fptr,int page)709a0e6850fSThomas Cort set_new_page(FIELD *fptr, int page)
710a0e6850fSThomas Cort {
711a0e6850fSThomas Cort FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;
712a0e6850fSThomas Cort
713a0e6850fSThomas Cort if (field->parent != NULL) /* check if field is connected to a form */
714a0e6850fSThomas Cort return E_CONNECTED;
715a0e6850fSThomas Cort
716a0e6850fSThomas Cort field->page_break = (page != FALSE);
717a0e6850fSThomas Cort return E_OK;
718a0e6850fSThomas Cort }
719a0e6850fSThomas Cort
720a0e6850fSThomas Cort /*
721a0e6850fSThomas Cort * Return the page status for the given field. TRUE is returned if the
722a0e6850fSThomas Cort * field is the start of a new page.
723a0e6850fSThomas Cort */
724a0e6850fSThomas Cort int
new_page(FIELD * field)725a0e6850fSThomas Cort new_page(FIELD *field)
726a0e6850fSThomas Cort {
727a0e6850fSThomas Cort if (field == NULL)
728a0e6850fSThomas Cort return _formi_default_field.page_break;
729a0e6850fSThomas Cort else
730a0e6850fSThomas Cort return field->page_break;
731a0e6850fSThomas Cort }
732a0e6850fSThomas Cort
733a0e6850fSThomas Cort /*
734a0e6850fSThomas Cort * Return the index of the field in the form fields array.
735a0e6850fSThomas Cort */
736a0e6850fSThomas Cort int
field_index(FIELD * field)737a0e6850fSThomas Cort field_index(FIELD *field)
738a0e6850fSThomas Cort {
739a0e6850fSThomas Cort if (field == NULL)
740a0e6850fSThomas Cort return E_BAD_ARGUMENT;
741a0e6850fSThomas Cort
742a0e6850fSThomas Cort if (field->parent == NULL)
743a0e6850fSThomas Cort return E_NOT_CONNECTED;
744a0e6850fSThomas Cort
745a0e6850fSThomas Cort return field->index;
746a0e6850fSThomas Cort }
747a0e6850fSThomas Cort
748a0e6850fSThomas Cort /*
749a0e6850fSThomas Cort * Internal function that does most of the work to create a new field.
750a0e6850fSThomas Cort * The new field is initialised from the information in the prototype
751a0e6850fSThomas Cort * field passed.
752a0e6850fSThomas Cort * Returns NULL on error.
753a0e6850fSThomas Cort */
754a0e6850fSThomas Cort static FIELD *
_formi_create_field(FIELD * prototype,int rows,int cols,int frow,int fcol,int nrows,int nbuf)755a0e6850fSThomas Cort _formi_create_field(FIELD *prototype, int rows, int cols, int frow,
756a0e6850fSThomas Cort int fcol, int nrows, int nbuf)
757a0e6850fSThomas Cort {
758a0e6850fSThomas Cort FIELD *new;
759a0e6850fSThomas Cort
760a0e6850fSThomas Cort if ((rows <= 0) || (cols <= 0) || (frow < 0) || (fcol < 0) ||
761a0e6850fSThomas Cort (nrows < 0) || (nbuf < 0))
762a0e6850fSThomas Cort return NULL;
763a0e6850fSThomas Cort
764a0e6850fSThomas Cort if ((new = (FIELD *)malloc(sizeof(FIELD))) == NULL) {
765a0e6850fSThomas Cort return NULL;
766a0e6850fSThomas Cort }
767a0e6850fSThomas Cort
768a0e6850fSThomas Cort /* copy in the default field info */
769a0e6850fSThomas Cort bcopy(prototype, new, sizeof(FIELD));
770a0e6850fSThomas Cort
771a0e6850fSThomas Cort new->nbuf = nbuf + 1;
772a0e6850fSThomas Cort new->rows = rows;
773a0e6850fSThomas Cort new->cols = cols;
774a0e6850fSThomas Cort new->form_row = frow;
775a0e6850fSThomas Cort new->form_col = fcol;
776a0e6850fSThomas Cort new->nrows = nrows;
777a0e6850fSThomas Cort new->link = new;
778a0e6850fSThomas Cort return new;
779a0e6850fSThomas Cort }
780a0e6850fSThomas Cort
781a0e6850fSThomas Cort /*
782a0e6850fSThomas Cort * Create a new field structure.
783a0e6850fSThomas Cort */
784a0e6850fSThomas Cort FIELD *
new_field(int rows,int cols,int frow,int fcol,int nrows,int nbuf)785a0e6850fSThomas Cort new_field(int rows, int cols, int frow, int fcol, int nrows, int nbuf)
786a0e6850fSThomas Cort {
787a0e6850fSThomas Cort FIELD *new;
788a0e6850fSThomas Cort size_t buf_len;
789a0e6850fSThomas Cort int i;
790a0e6850fSThomas Cort
791a0e6850fSThomas Cort
792a0e6850fSThomas Cort if ((new = _formi_create_field(&_formi_default_field, rows, cols,
793a0e6850fSThomas Cort frow, fcol, nrows, nbuf)) == NULL)
794a0e6850fSThomas Cort return NULL;
795a0e6850fSThomas Cort
796a0e6850fSThomas Cort buf_len = (nbuf + 1) * sizeof(FORM_STR);
797a0e6850fSThomas Cort
798a0e6850fSThomas Cort if ((new->buffers = (FORM_STR *)malloc(buf_len)) == NULL) {
799a0e6850fSThomas Cort free(new);
800a0e6850fSThomas Cort return NULL;
801a0e6850fSThomas Cort }
802a0e6850fSThomas Cort
803a0e6850fSThomas Cort /* Initialise the strings to a zero length string */
804a0e6850fSThomas Cort for (i = 0; i < nbuf + 1; i++) {
805a0e6850fSThomas Cort if ((new->buffers[i].string =
806a0e6850fSThomas Cort (char *) malloc(sizeof(char))) == NULL) {
807a0e6850fSThomas Cort free(new->buffers);
808a0e6850fSThomas Cort free(new);
809a0e6850fSThomas Cort return NULL;
810a0e6850fSThomas Cort }
811a0e6850fSThomas Cort new->buffers[i].string[0] = '\0';
812a0e6850fSThomas Cort new->buffers[i].length = 0;
813a0e6850fSThomas Cort new->buffers[i].allocated = 1;
814a0e6850fSThomas Cort }
815a0e6850fSThomas Cort
816a0e6850fSThomas Cort if ((new->alines = (_FORMI_FIELD_LINES *)
817a0e6850fSThomas Cort malloc(sizeof(struct _formi_field_lines))) == NULL) {
818a0e6850fSThomas Cort free(new->buffers);
819a0e6850fSThomas Cort free(new);
820a0e6850fSThomas Cort return NULL;
821a0e6850fSThomas Cort }
822a0e6850fSThomas Cort
823a0e6850fSThomas Cort new->alines->prev = NULL;
824a0e6850fSThomas Cort new->alines->next = NULL;
825a0e6850fSThomas Cort new->alines->allocated = 0;
826a0e6850fSThomas Cort new->alines->length = 0;
827a0e6850fSThomas Cort new->alines->expanded = 0;
828a0e6850fSThomas Cort new->alines->string = NULL;
829a0e6850fSThomas Cort new->alines->hard_ret = FALSE;
830a0e6850fSThomas Cort new->alines->tabs = NULL;
831a0e6850fSThomas Cort new->start_line = new->alines;
832a0e6850fSThomas Cort new->cur_line = new->alines;
833a0e6850fSThomas Cort
834a0e6850fSThomas Cort return new;
835a0e6850fSThomas Cort }
836a0e6850fSThomas Cort
837a0e6850fSThomas Cort /*
838*0a6a1f1dSLionel Sambuc * Duplicate the given field, including its buffers.
839a0e6850fSThomas Cort */
840a0e6850fSThomas Cort FIELD *
dup_field(FIELD * field,int frow,int fcol)841a0e6850fSThomas Cort dup_field(FIELD *field, int frow, int fcol)
842a0e6850fSThomas Cort {
843a0e6850fSThomas Cort FIELD *new;
844a0e6850fSThomas Cort size_t row_len, buf_len;
845a0e6850fSThomas Cort
846a0e6850fSThomas Cort if (field == NULL)
847a0e6850fSThomas Cort return NULL;
848a0e6850fSThomas Cort
849a0e6850fSThomas Cort /* XXXX this right???? */
850a0e6850fSThomas Cort if ((new = _formi_create_field(field, (int) field->rows,
851a0e6850fSThomas Cort (int ) field->cols,
852a0e6850fSThomas Cort frow, fcol, (int) field->nrows,
853a0e6850fSThomas Cort field->nbuf - 1)) == NULL)
854a0e6850fSThomas Cort return NULL;
855a0e6850fSThomas Cort
856a0e6850fSThomas Cort row_len = (field->rows + field->nrows + 1) * field->cols;
857a0e6850fSThomas Cort buf_len = (field->nbuf + 1) * row_len * sizeof(FORM_STR);
858a0e6850fSThomas Cort
859a0e6850fSThomas Cort if ((new->buffers = (FORM_STR *)malloc(buf_len)) == NULL) {
860a0e6850fSThomas Cort free(new);
861a0e6850fSThomas Cort return NULL;
862a0e6850fSThomas Cort }
863a0e6850fSThomas Cort
864a0e6850fSThomas Cort /* copy the buffers from the source field into the new copy */
865a0e6850fSThomas Cort bcopy(field->buffers, new->buffers, buf_len);
866a0e6850fSThomas Cort
867a0e6850fSThomas Cort return new;
868a0e6850fSThomas Cort }
869a0e6850fSThomas Cort
870a0e6850fSThomas Cort /*
871a0e6850fSThomas Cort * Create a new field at the specified location by duplicating the given
872a0e6850fSThomas Cort * field. The buffers are shared with the parent field.
873a0e6850fSThomas Cort */
874a0e6850fSThomas Cort FIELD *
link_field(FIELD * field,int frow,int fcol)875a0e6850fSThomas Cort link_field(FIELD *field, int frow, int fcol)
876a0e6850fSThomas Cort {
877a0e6850fSThomas Cort FIELD *new;
878a0e6850fSThomas Cort
879a0e6850fSThomas Cort if (field == NULL)
880a0e6850fSThomas Cort return NULL;
881a0e6850fSThomas Cort
882a0e6850fSThomas Cort if ((new = _formi_create_field(field, (int) field->rows,
883a0e6850fSThomas Cort (int) field->cols,
884a0e6850fSThomas Cort frow, fcol, (int) field->nrows,
885a0e6850fSThomas Cort field->nbuf - 1)) == NULL)
886a0e6850fSThomas Cort return NULL;
887a0e6850fSThomas Cort
888a0e6850fSThomas Cort new->link = field->link;
889a0e6850fSThomas Cort field->link = new;
890a0e6850fSThomas Cort
891a0e6850fSThomas Cort /* we are done. The buffer pointer was copied during the field
892a0e6850fSThomas Cort creation. */
893a0e6850fSThomas Cort return new;
894a0e6850fSThomas Cort }
895a0e6850fSThomas Cort
896a0e6850fSThomas Cort /*
897a0e6850fSThomas Cort * Release all storage allocated to the field
898a0e6850fSThomas Cort */
899a0e6850fSThomas Cort int
free_field(FIELD * field)900a0e6850fSThomas Cort free_field(FIELD *field)
901a0e6850fSThomas Cort {
902a0e6850fSThomas Cort FIELD *flink;
903a0e6850fSThomas Cort int i;
904a0e6850fSThomas Cort _formi_tab_t *ts, *nts;
905a0e6850fSThomas Cort
906a0e6850fSThomas Cort if (field == NULL)
907a0e6850fSThomas Cort return E_BAD_ARGUMENT;
908a0e6850fSThomas Cort
909a0e6850fSThomas Cort if (field->parent != NULL)
910a0e6850fSThomas Cort return E_CONNECTED;
911a0e6850fSThomas Cort
912a0e6850fSThomas Cort if (field->link == field) { /* check if field linked */
913a0e6850fSThomas Cort /* no it is not - release the buffers */
914a0e6850fSThomas Cort free(field->buffers);
915a0e6850fSThomas Cort /* free the tab structures */
916a0e6850fSThomas Cort for (i = 0; i < field->row_count - 1; i++) {
917a0e6850fSThomas Cort if (field->alines[i].tabs != NULL) {
918a0e6850fSThomas Cort ts = field->alines[i].tabs;
919a0e6850fSThomas Cort while (ts != NULL) {
920a0e6850fSThomas Cort nts = ts->fwd;
921a0e6850fSThomas Cort free(ts);
922a0e6850fSThomas Cort ts = nts;
923a0e6850fSThomas Cort }
924a0e6850fSThomas Cort }
925a0e6850fSThomas Cort }
926a0e6850fSThomas Cort } else {
927a0e6850fSThomas Cort /* is linked, traverse the links to find the field referring
928a0e6850fSThomas Cort * to the one to be freed.
929a0e6850fSThomas Cort */
930a0e6850fSThomas Cort for (flink = field->link; flink != field; flink = flink->link);
931a0e6850fSThomas Cort flink->link = field->link;
932a0e6850fSThomas Cort }
933a0e6850fSThomas Cort
934a0e6850fSThomas Cort free(field);
935a0e6850fSThomas Cort return E_OK;
936a0e6850fSThomas Cort }
937a0e6850fSThomas Cort
938a0e6850fSThomas Cort
939