summaryrefslogtreecommitdiff
path: root/lib/libform/field.c
blob: 9e2b096f5417c3b258d66f1e2779e1709cf2b9f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
/*	$NetBSD: field.c,v 1.29 2015/09/07 15:50:49 joerg Exp $	*/
/*-
 * Copyright (c) 1998-1999 Brett Lymn
 *                         (blymn@baea.com.au, brett_lymn@yahoo.com.au)
 * All rights reserved.
 *
 * This code has been donated to The NetBSD Foundation by the Author.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 */

#include <sys/cdefs.h>
__RCSID("$NetBSD: field.c,v 1.29 2015/09/07 15:50:49 joerg Exp $");

#include <sys/param.h>
#include <stdlib.h>
#include <strings.h>
#include <stdarg.h>
#include <form.h>
#include "internals.h"

extern FORM _formi_default_form;

FIELD _formi_default_field = {
	0, /* rows in the field */
	0, /* columns in the field */
	0, /* dynamic rows */
	0, /* dynamic columns */
	0, /* maximum growth */
	0, /* starting row in the form subwindow */
	0, /* starting column in the form subwindow */
	0, /* number of off screen rows */
	0, /* index of this field in form fields array. */
	0, /* number of buffers associated with this field */
	FALSE, /* set to true if buffer 0 has changed. */
	NO_JUSTIFICATION, /* justification style of the field */
	FALSE, /* set to true if field is in overlay mode */
	NULL, /* pointer to the current line cursor is on */
	0, /* starting char in string (horiz scroll) */
	NULL, /* starting line in field (vert scroll) */
	0, /* number of rows actually used in field */
	0, /* actual pos of cursor in row, not same as x pos due to tabs */
	0, /* x pos of cursor in field */
	0, /* y pos of cursor in field */
	0, /* start of a new page on the form if 1 */
	0, /* number of the page this field is on */
	A_NORMAL, /* character attributes for the foreground */
	A_NORMAL, /* character attributes for the background */
	' ', /* padding character */
	DEFAULT_FORM_OPTS, /* options for the field */
	NULL, /* the form this field is bound to, if any */
	NULL, /* field above this one */
	NULL, /* field below this one */
	NULL, /* field to the left of this one */
	NULL, /* field to the right of this one */
	NULL,  /* user defined pointer. */
	NULL, /* used if fields are linked */
	NULL, /* type struct for the field */
	{NULL, NULL}, /* circle queue glue for sorting fields */
	NULL, /* args for field type. */
	NULL, /* pointer to the array of lines structures. */
	NULL, /* list of lines available for reuse */
	NULL, /* array of buffers for the field */
};

/* internal function prototypes */
static int
field_buffer_init(FIELD *field, int buffer, unsigned int len);
static FIELD *
_formi_create_field(FIELD *, int, int, int, int, int, int);

   
/*
 * Set the userptr for the field
 */
int
set_field_userptr(FIELD *field, void *ptr)
{
	FIELD *fp = (field == NULL) ? &_formi_default_field : field;

	fp->userptr = ptr;

	return E_OK;
}

/*
 * Return the userptr for the field.
 */

void *
field_userptr(FIELD *field)
{
	if (field == NULL)
		return _formi_default_field.userptr;
	else
		return field->userptr;
}

/*
 * Set the options for the designated field.
 */
int
set_field_opts(FIELD *field, Form_Options options)
{
	int i;
	
	FIELD *fp = (field == NULL) ? &_formi_default_field : field;

	  /* not allowed to set opts if the field is the current one */
	if ((field != NULL) && (field->parent != NULL) &&
	    (field->parent->cur_field == field->index))
		return E_CURRENT;
	
	if ((options & O_STATIC) == O_STATIC) {
		for (i = 0; i < fp->nbuf; i++) {
			if (fp->buffers[i].length > fp->cols)
				fp->buffers[i].string[fp->cols] = '\0';
		}
	}
	
	fp->opts = options;

	  /* if appropriate, redraw the field */
	if ((field != NULL) && (field->parent != NULL)
	    && (field->parent->posted == 1)) {
		_formi_redraw_field(field->parent, field->index);
		pos_form_cursor(field->parent);
		wrefresh(field->parent->scrwin);
	}
	
	return E_OK;
}

/*
 * Turn on the passed field options.
 */
int
field_opts_on(FIELD *field, Form_Options options)
{
	int i;
	
	FIELD *fp = (field == NULL) ? &_formi_default_field : field;

	  /* not allowed to set opts if the field is the current one */
	if ((field != NULL) && (field->parent != NULL) &&
	    (field->parent->cur_field == field->index))
		return E_CURRENT;
	
	if ((options & O_STATIC) == O_STATIC) {
		for (i = 0; i < fp->nbuf; i++) {
			if (fp->buffers[i].length > fp->cols)
				fp->buffers[i].string[fp->cols] = '\0';
		}
	}
	
	fp->opts |= options;
	
	  /* if appropriate, redraw the field */
	if ((field != NULL) && (field->parent != NULL)
	    && (field->parent->posted == 1)) {
		_formi_redraw_field(field->parent, field->index);
		pos_form_cursor(field->parent);
		wrefresh(field->parent->scrwin);
	}

	return E_OK;
}

/*
 * Turn off the passed field options.
 */
int
field_opts_off(FIELD *field, Form_Options options)
{
	FIELD *fp = (field == NULL) ? &_formi_default_field : field;

	  /* not allowed to set opts if the field is the current one */
	if ((field != NULL) && (field->parent != NULL) &&
	    (field->parent->cur_field == field->index))
		return E_CURRENT;
	
	fp->opts &= ~options;
	
	  /* if appropriate, redraw the field */
	if ((field != NULL) && (field->parent != NULL)
	    && (field->parent->posted == 1)) {
		_formi_redraw_field(field->parent, field->index);
		pos_form_cursor(field->parent);
		wrefresh(field->parent->scrwin);
	}
	
	return E_OK;
}

/*
 * Return the field options associated with the passed field.
 */
Form_Options
field_opts(FIELD *field)
{
	if (field == NULL)
		return _formi_default_field.opts;
	else
		return field->opts;
}

/*
 * Set the justification for the passed field.
 */
int
set_field_just(FIELD *field, int justification)
{
	FIELD *fp = (field == NULL) ? &_formi_default_field : field;

	  /*
	   * not allowed to set justification if the field is
	   * the current one
	   */
	if ((field != NULL) && (field->parent != NULL) &&
	    (field->parent->cur_field == field->index))
		return E_CURRENT;
	
	if ((justification < MIN_JUST_STYLE) /* check justification valid */
	    || (justification > MAX_JUST_STYLE))
		return E_BAD_ARGUMENT;

	  /* only allow justification on static, single row fields */
	if (((fp->opts & O_STATIC) != O_STATIC) ||
	    ((fp->rows + fp->nrows) > 1))
		return E_BAD_ARGUMENT;
	
	fp->justification = justification;

	_formi_init_field_xpos(fp);
	
	return E_OK;
}

/*
 * Return the justification style of the field passed.
 */
int
field_just(FIELD *field)
{
	if (field == NULL)
		return _formi_default_field.justification;
	else
		return field->justification;
}

/*
 * Return information about the field passed.
 */
int
field_info(FIELD *field, int *rows, int *cols, int *frow, int *fcol,
	   int *nrow, int *nbuf)
{
	if (field == NULL)
		return E_BAD_ARGUMENT;

	*rows = field->rows;
	*cols = field->cols;
	*frow = field->form_row;
	*fcol = field->form_col;
	*nrow = field->nrows;
	*nbuf = field->nbuf;

	return E_OK;
}

/*
 * Report the dynamic field information.
 */
int
dynamic_field_info(FIELD *field, int *drows, int *dcols, int *max)
{
	if (field == NULL)
		return E_BAD_ARGUMENT;

	if ((field->opts & O_STATIC) == O_STATIC) {
		*drows = field->rows;
		*dcols = field->cols;
	} else {
		*drows = field->drows;
		*dcols = field->dcols;
	}
	
	*max = field->max;

	return E_OK;
}

/*
 * Init all the field variables, perform wrapping and other tasks
 * after the field buffer is set.
 */
static int
field_buffer_init(FIELD *field, int buffer, unsigned int len)
{
	int status;
	char *newp;
	
	if (buffer == 0) {
		field->start_char = 0;
		field->start_line = 0;
		field->row_xpos = 0;
		field->cursor_xpos = 0;
		field->cursor_ypos = 0;
		field->row_count = 1; /* must be at least one row  XXX need to shift old rows (if any) to free list??? */
		field->alines->length = len;
		if ((newp = realloc(field->alines->string,
				    (size_t) len + 1)) == NULL)
			return E_SYSTEM_ERROR;
		field->alines->string = newp;
		field->alines->allocated = len + 1;
		strlcpy(field->alines->string, field->buffers[buffer].string,
			(size_t) len + 1);
		field->alines->expanded =
			_formi_tab_expanded_length(field->alines->string,
						   0, field->alines->length);

		field->start_line = field->alines;
		field->cur_line = field->alines;
		
		  /* we have to hope the wrap works - if it does not then the
		     buffer is pretty much borked */
		status = _formi_wrap_field(field, field->cur_line);
		if (status != E_OK)
			return status;

		  /*
		   * calculate the tabs for a single row field, the
		   * multiline case is handled when the wrap is done.
		   */
		if (field->row_count == 1)
			_formi_calculate_tabs(field->alines);

		  /* redraw the field to reflect the new contents. If the field
		   * is attached....
		   */
		if ((field->parent != NULL) && (field->parent->posted == 1)) {
			_formi_redraw_field(field->parent, field->index);
			  /* make sure cursor goes back to current field */
			pos_form_cursor(field->parent);
		}
	}

	return E_OK;
}


/*
 * Set the field buffer to the string that results from processing
 * the given format (fmt) using sprintf.
 */
int
set_field_printf(FIELD *field, int buffer, char *fmt, ...)
{
	int len;
	va_list args;
	
	if (field == NULL)
		return E_BAD_ARGUMENT;

	if (buffer >= field->nbuf)
		return E_BAD_ARGUMENT;

	va_start(args, fmt);
	  /* check for buffer already existing, free the storage */
	if (field->buffers[buffer].allocated != 0)
		free(field->buffers[buffer].string);

	len = vasprintf(&field->buffers[buffer].string, fmt, args);
	va_end(args);
	if (len < 0)
		return E_SYSTEM_ERROR;
	
	field->buffers[buffer].length = len;
	field->buffers[buffer].allocated = len + 1;
	if (((field->opts & O_STATIC) == O_STATIC) && (len > field->cols)
	    && ((field->rows + field->nrows) == 1))
		len = field->cols;

	field->buffers[buffer].string[len] = '\0';
	return field_buffer_init(field, buffer, (unsigned int) len);
}
	
/*
 * Set the value of the field buffer to the value given.
 */

int
set_field_buffer(FIELD *field, int buffer, const char *value)
{
	unsigned int len;
	int status;
	
	if (field == NULL)
		return E_BAD_ARGUMENT;

	if (buffer >= field->nbuf) /* make sure buffer is valid */
		return E_BAD_ARGUMENT;

	len = (unsigned int) strlen(value);
	if (((field->opts & O_STATIC) == O_STATIC) && (len > field->cols)
	    && ((field->rows + field->nrows) == 1))
		len = field->cols;

#ifdef DEBUG
	if (_formi_create_dbg_file() != E_OK)
		return E_SYSTEM_ERROR;

	fprintf(dbg,
		"set_field_buffer: entry: len = %d, value = %s, buffer=%d\n",
		len, value, buffer);
	fprintf(dbg, "set_field_buffer: entry: string = ");
	if (field->buffers[buffer].string != NULL)
		fprintf(dbg, "%s, len = %d\n", field->buffers[buffer].string,
			field->buffers[buffer].length);
	else
		fprintf(dbg, "(null), len = 0\n");
	fprintf(dbg, "set_field_buffer: entry: lines.len = %d\n",
		field->alines[0].length);
#endif
	
	if ((field->buffers[buffer].string =
	     (char *) realloc(field->buffers[buffer].string,
			      (size_t) len + 1)) == NULL)
		return E_SYSTEM_ERROR;

	strlcpy(field->buffers[buffer].string, value, (size_t) len + 1);
	field->buffers[buffer].length = len;
	field->buffers[buffer].allocated = len + 1;
	status = field_buffer_init(field, buffer, len);

#ifdef DEBUG
	fprintf(dbg, "set_field_buffer: exit: len = %d, value = %s\n",
		len, value);
	fprintf(dbg, "set_field_buffer: exit: string = %s, len = %d\n",
		field->buffers[buffer].string, field->buffers[buffer].length);
	fprintf(dbg, "set_field_buffer: exit: lines.len = %d\n",
		field->alines[0].length);
#endif

	return status;
}

/*
 * Return the requested field buffer to the caller.
 */
char *
field_buffer(FIELD *field, int buffer)
{

	char *reformat, *p;
	_FORMI_FIELD_LINES *linep;
	size_t bufsize, pos;
	
	if (field == NULL)
		return NULL;

	if (buffer >= field->nbuf)
		return NULL;

	  /*
	   * We force a sync from the line structs to the buffer here.
	   * Traditional libform say we don't need to because it is
	   * done on a REQ_VALIDATE but NetBSD libform previously did
	   * not enforce this because the buffer contents were always
	   * current.  Changes to line handling make this no longer so
	   * - the line structs may contain different data to the
	   * buffer if unsynced.
	   */
	if (_formi_sync_buffer(field) != E_OK)
		return NULL;
	
	if ((field->opts & O_REFORMAT) != O_REFORMAT)
		return field->buffers[buffer].string;

	if (field->row_count <= 1)
		return strdup(field->buffers[buffer].string);

	/*
	 * create a single string containing each line,
	 * separated by newline, last line having no
	 * newline, but NUL terminated.
	 */
	bufsize = pos = 0;
	reformat = NULL;
	for (linep = field->alines; linep; linep = linep->next) {
		size_t len = strlen(linep->string);
		if (len + 1 >= bufsize - pos) {
			bufsize += MAX(1024, 2 * len);
			p = realloc(reformat, bufsize);
			if (p == NULL) {
				free(reformat);
				return NULL;
			}
			reformat = p;
		}
		memcpy(reformat + pos, linep->string, len);
		pos += len;
		reformat[pos++] = linep->next ? '\n' : '\0';
	}
	return reformat;
}

/*
 * Set the buffer 0 field status.
 */
int
set_field_status(FIELD *field, int status)
{

	if (field == NULL)
		return E_BAD_ARGUMENT;

	if (status != FALSE)
		field->buf0_status = TRUE;
	else
		field->buf0_status = FALSE;

	return E_OK;
}

/*
 * Return the buffer 0 status flag for the given field.
 */
int
field_status(FIELD *field)
{

	if (field == NULL) /* the default buffer 0 never changes :-) */
		return FALSE;

	return field->buf0_status;
}

/*
 * Set the maximum growth for a dynamic field.
 */
int
set_max_field(FIELD *fptr, int max)
{
	FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;

	if ((field->opts & O_STATIC) == O_STATIC) /* check if field dynamic */
		return E_BAD_ARGUMENT;

	if (max < 0) /* negative numbers are bad.... */
		return E_BAD_ARGUMENT;
	
	field->max = max;
	return E_OK;
}

/*
 * Set the field foreground character attributes.
 */
int
set_field_fore(FIELD *fptr, chtype attribute)
{
	FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;

	field->fore = attribute;
	return E_OK;
}

/*
 * Return the foreground character attribute for the given field.
 */
chtype
field_fore(FIELD *field)
{
	if (field == NULL)
		return _formi_default_field.fore;
	else
		return field->fore;
}

/*
 * Set the background character attribute for the given field.
 */
int
set_field_back(FIELD *field, chtype attribute)
{
	if (field == NULL)
		_formi_default_field.back = attribute;
	else
		field->back = attribute;

	return E_OK;
}

/*
 * Get the background character attribute for the given field.
 */
chtype
field_back(FIELD *field)
{
	if (field == NULL)
		return _formi_default_field.back;
	else
		return field->back;
}

/*
 * Set the pad character for the given field.
 */
int
set_field_pad(FIELD *field, int pad)
{
	if (field == NULL)
		_formi_default_field.pad = pad;
	else
		field->pad = pad;

	return E_OK;
}

/*
 * Return the padding character for the given field.
 */
int
field_pad(FIELD *field)
{
	if (field == NULL)
		return _formi_default_field.pad;
	else
		return field->pad;
}

/*
 * Set the field initialisation function hook.
 */
int
set_field_init(FORM *form, Form_Hook function)
{
	if (form == NULL)
		_formi_default_form.field_init = function;
	else
		form->field_init = function;

	return E_OK;
}

/*
 * Return the function hook for the field initialisation.
 */
Form_Hook
field_init(FORM *form)
{
	if (form == NULL)
		return _formi_default_form.field_init;
	else
		return form->field_init;
}

/*
 * Set the field termination function hook.
 */
int
set_field_term(FORM *form, Form_Hook function)
{
	if (form == NULL)
		_formi_default_form.field_term = function;
	else
		form->field_term = function;

	return E_OK;
}

/*
 * Return the function hook defined for the field termination.
 */
Form_Hook
field_term(FORM *form)
{
	if (form == NULL)
		return _formi_default_form.field_term;
	else
		return form->field_term;
}

/*
 * Set the page flag on the given field to indicate it is the start of a
 * new page.
 */
int
set_new_page(FIELD *fptr, int page)
{
	FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;

	if (field->parent != NULL) /* check if field is connected to a form */
		return E_CONNECTED;
	
	field->page_break = (page != FALSE);
	return E_OK;
}

/*
 * Return the page status for the given field.  TRUE is returned if the
 * field is the start of a new page.
 */
int
new_page(FIELD *field)
{
	if (field == NULL)
		return _formi_default_field.page_break;
	else
		return field->page_break;
}

/*
 * Return the index of the field in the form fields array.
 */
int
field_index(FIELD *field)
{
	if (field == NULL)
		return E_BAD_ARGUMENT;

	if (field->parent == NULL)
		return E_NOT_CONNECTED;

	return field->index;
}

/*
 * Internal function that does most of the work to create a new field.
 * The new field is initialised from the information in the prototype
 * field passed.
 * Returns NULL on error.
 */
static FIELD *
_formi_create_field(FIELD *prototype, int rows, int cols, int frow,
		    int fcol, int nrows, int nbuf)
{
	FIELD *new;
	
	if ((rows <= 0) || (cols <= 0) || (frow < 0) || (fcol < 0) ||
	    (nrows < 0) || (nbuf < 0))
		return NULL;
	
	if ((new = (FIELD *)malloc(sizeof(FIELD))) == NULL) {
		return NULL;
	}

	  /* copy in the default field info */
	bcopy(prototype, new, sizeof(FIELD));

	new->nbuf = nbuf + 1;
	new->rows = rows;
	new->cols = cols;
	new->form_row = frow;
	new->form_col = fcol;
	new->nrows = nrows;
	new->link = new;
	return new;
}

/*
 * Create a new field structure.
 */
FIELD *
new_field(int rows, int cols, int frow, int fcol, int nrows, int nbuf)
{
	FIELD *new;
	size_t buf_len;
	int i;
	

	if ((new = _formi_create_field(&_formi_default_field, rows, cols,
				       frow, fcol, nrows, nbuf)) == NULL)
		return NULL;
	
	buf_len = (nbuf + 1) * sizeof(FORM_STR);
	
	if ((new->buffers = (FORM_STR *)malloc(buf_len)) == NULL) {
		free(new);
		return NULL;
	}

	  /* Initialise the strings to a zero length string */
	for (i = 0; i < nbuf + 1; i++) {
		if ((new->buffers[i].string =
		     (char *) malloc(sizeof(char))) == NULL) {
			free(new->buffers);
			free(new);
			return NULL;
		}
		new->buffers[i].string[0] = '\0';
		new->buffers[i].length = 0;
		new->buffers[i].allocated = 1;
	}

	if ((new->alines = (_FORMI_FIELD_LINES *)
	     malloc(sizeof(struct _formi_field_lines))) == NULL) {
		free(new->buffers);
		free(new);
		return NULL;
	}

	new->alines->prev = NULL;
	new->alines->next = NULL;
	new->alines->allocated = 0;
	new->alines->length = 0;
	new->alines->expanded = 0;
	new->alines->string = NULL;
	new->alines->hard_ret = FALSE;
	new->alines->tabs = NULL;
	new->start_line = new->alines;
	new->cur_line = new->alines;
	
	return new;
}

/*
 * Duplicate the given field, including its buffers.
 */
FIELD *
dup_field(FIELD *field, int frow, int fcol)
{
	FIELD *new;
	size_t row_len, buf_len;
	
	if (field == NULL)
		return NULL;

	  /* XXXX this right???? */
	if ((new = _formi_create_field(field, (int) field->rows,
				       (int ) field->cols,
				       frow, fcol, (int) field->nrows,
				       field->nbuf - 1)) == NULL)
		return NULL;

	row_len = (field->rows + field->nrows + 1) * field->cols;
	buf_len = (field->nbuf + 1) * row_len * sizeof(FORM_STR);
	
	if ((new->buffers = (FORM_STR *)malloc(buf_len)) == NULL) {
		free(new);
		return NULL;
	}

	  /* copy the buffers from the source field into the new copy */
	bcopy(field->buffers, new->buffers, buf_len);

	return new;
}

/*
 * Create a new field at the specified location by duplicating the given
 * field.  The buffers are shared with the parent field.
 */
FIELD *
link_field(FIELD *field, int frow, int fcol)
{
	FIELD *new;

	if (field == NULL)
		return NULL;
	
	if ((new = _formi_create_field(field, (int) field->rows,
				       (int) field->cols,
				       frow, fcol, (int) field->nrows,
				       field->nbuf - 1)) == NULL)
		return NULL;

	new->link = field->link;
	field->link = new;
	
	  /* we are done.  The buffer pointer was copied during the field
	     creation. */
	return new;
}

/*
 * Release all storage allocated to the field
 */
int
free_field(FIELD *field)
{
	FIELD *flink;
	int i;
	_formi_tab_t *ts, *nts;
	
	if (field == NULL)
		return E_BAD_ARGUMENT;

	if (field->parent != NULL)
		return E_CONNECTED;

	if (field->link == field) { /* check if field linked */
		  /* no it is not - release the buffers */
		free(field->buffers);
		  /* free the tab structures */
		for (i = 0; i < field->row_count - 1; i++) {
			if (field->alines[i].tabs != NULL) {
				ts = field->alines[i].tabs;
				while (ts != NULL) {
					nts = ts->fwd;
					free(ts);
					ts = nts;
				}
			}
		}
	} else {
		  /* is linked, traverse the links to find the field referring
		   * to the one to be freed.
		   */
		for (flink = field->link; flink != field; flink = flink->link);
		flink->link = field->link;
	}

	free(field);
	return E_OK;
}