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