Home | History | Annotate | Line # | Download | only in libform
field.c revision 1.18
      1 /*	$NetBSD: field.c,v 1.18 2002/07/31 01:28:32 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 <stdlib.h>
     32 #include <strings.h>
     33 #include <form.h>
     34 #include "internals.h"
     35 
     36 extern FORM _formi_default_form;
     37 
     38 FIELD _formi_default_field = {
     39 	0, /* rows in the field */
     40 	0, /* columns in the field */
     41 	0, /* dynamic rows */
     42 	0, /* dynamic columns */
     43 	0, /* maximum growth */
     44 	0, /* starting row in the form subwindow */
     45 	0, /* starting column in the form subwindow */
     46 	0, /* number of off screen rows */
     47 	0, /* index of this field in form fields array. */
     48 	0, /* number of buffers associated with this field */
     49 	FALSE, /* set to true if buffer 0 has changed. */
     50 	NO_JUSTIFICATION, /* justification style of the field */
     51 	FALSE, /* set to true if field is in overlay mode */
     52 	0, /* starting char in string (horiz scroll) */
     53 	0, /* starting line in field (vert scroll) */
     54 	0, /* number of rows actually used in field */
     55 	0, /* actual pos of cursor in row, not same as x pos due to tabs */
     56 	0, /* x pos of cursor in field */
     57 	0, /* y pos of cursor in field */
     58 	0, /* start of a new page on the form if 1 */
     59 	0, /* number of the page this field is on */
     60 	A_NORMAL, /* character attributes for the foreground */
     61 	A_NORMAL, /* character attributes for the background */
     62 	' ', /* padding character */
     63 	DEFAULT_FORM_OPTS, /* options for the field */
     64 	NULL, /* the form this field is bound to, if any */
     65 	NULL, /* field above this one */
     66 	NULL, /* field below this one */
     67 	NULL, /* field to the left of this one */
     68 	NULL, /* field to the right of this one */
     69 	NULL,  /* user defined pointer. */
     70 	NULL, /* used if fields are linked */
     71 	NULL, /* type struct for the field */
     72 	{NULL, NULL}, /* circle queue glue for sorting fields */
     73 	NULL, /* args for field type. */
     74 	0,    /* number of allocated slots in lines array */
     75 	NULL, /* pointer to the array of lines structures. */
     76 	NULL, /* array of buffers for the field */
     77 };
     78 
     79 /* internal function prototypes */
     80 static FIELD *
     81 _formi_create_field(FIELD *, int, int, int, int, int, int);
     82 
     83 
     84 /*
     85  * Set the userptr for the field
     86  */
     87 int
     88 set_field_userptr(FIELD *field, void *ptr)
     89 {
     90 	FIELD *fp = (field == NULL) ? &_formi_default_field : field;
     91 
     92 	fp->userptr = ptr;
     93 
     94 	return E_OK;
     95 }
     96 
     97 /*
     98  * Return the userptr for the field.
     99  */
    100 
    101 void *
    102 field_userptr(FIELD *field)
    103 {
    104 	if (field == NULL)
    105 		return _formi_default_field.userptr;
    106 	else
    107 		return field->userptr;
    108 }
    109 
    110 /*
    111  * Set the options for the designated field.
    112  */
    113 int
    114 set_field_opts(FIELD *field, Form_Options options)
    115 {
    116 	int i;
    117 
    118 	FIELD *fp = (field == NULL) ? &_formi_default_field : field;
    119 
    120 	  /* not allowed to set opts if the field is the current one */
    121 	if ((field != NULL) && (field->parent != NULL) &&
    122 	    (field->parent->cur_field == field->index))
    123 		return E_CURRENT;
    124 
    125 	if ((options & O_STATIC) == O_STATIC) {
    126 		for (i = 0; i < field->nbuf; i++) {
    127 			if (field->buffers[i].length > field->cols)
    128 				field->buffers[i].string[field->cols] = '\0';
    129 		}
    130 	}
    131 
    132 	fp->opts = options;
    133 
    134 	return E_OK;
    135 }
    136 
    137 /*
    138  * Turn on the passed field options.
    139  */
    140 int
    141 field_opts_on(FIELD *field, Form_Options options)
    142 {
    143 	int i;
    144 
    145 	FIELD *fp = (field == NULL) ? &_formi_default_field : field;
    146 
    147 	  /* not allowed to set opts if the field is the current one */
    148 	if ((field != NULL) && (field->parent != NULL) &&
    149 	    (field->parent->cur_field == field->index))
    150 		return E_CURRENT;
    151 
    152 	if ((options & O_STATIC) == O_STATIC) {
    153 		for (i = 0; i < field->nbuf; i++) {
    154 			if (field->buffers[i].length > field->cols)
    155 				field->buffers[i].string[field->cols] = '\0';
    156 		}
    157 	}
    158 
    159 	fp->opts |= options;
    160 
    161 	return E_OK;
    162 }
    163 
    164 /*
    165  * Turn off the passed field options.
    166  */
    167 int
    168 field_opts_off(FIELD *field, Form_Options options)
    169 {
    170 	FIELD *fp = (field == NULL) ? &_formi_default_field : field;
    171 
    172 	  /* not allowed to set opts if the field is the current one */
    173 	if ((field != NULL) && (field->parent != NULL) &&
    174 	    (field->parent->cur_field == field->index))
    175 		return E_CURRENT;
    176 
    177 	fp->opts &= ~options;
    178 	return E_OK;
    179 }
    180 
    181 /*
    182  * Return the field options associated with the passed field.
    183  */
    184 Form_Options
    185 field_opts(FIELD *field)
    186 {
    187 	if (field == NULL)
    188 		return _formi_default_field.opts;
    189 	else
    190 		return field->opts;
    191 }
    192 
    193 /*
    194  * Set the justification for the passed field.
    195  */
    196 int
    197 set_field_just(FIELD *field, int justification)
    198 {
    199 	FIELD *fp = (field == NULL) ? &_formi_default_field : field;
    200 
    201 	  /*
    202 	   * not allowed to set justification if the field is
    203 	   * the current one
    204 	   */
    205 	if ((field != NULL) && (field->parent != NULL) &&
    206 	    (field->parent->cur_field == field->index))
    207 		return E_CURRENT;
    208 
    209 	if ((justification < MIN_JUST_STYLE) /* check justification valid */
    210 	    || (justification > MAX_JUST_STYLE))
    211 		return E_BAD_ARGUMENT;
    212 
    213 	  /* only allow justification on static, single row fields */
    214 	if (((fp->opts & O_STATIC) != O_STATIC) ||
    215 	    ((fp->rows + fp->nrows) > 1))
    216 		return E_BAD_ARGUMENT;
    217 
    218 	fp->justification = justification;
    219 
    220 	_formi_init_field_xpos(fp);
    221 
    222 	return E_OK;
    223 }
    224 
    225 /*
    226  * Return the justification style of the field passed.
    227  */
    228 int
    229 field_just(FIELD *field)
    230 {
    231 	if (field == NULL)
    232 		return _formi_default_field.justification;
    233 	else
    234 		return field->justification;
    235 }
    236 
    237 /*
    238  * Return information about the field passed.
    239  */
    240 int
    241 field_info(FIELD *field, int *rows, int *cols, int *frow, int *fcol,
    242 	   int *nrow, int *nbuf)
    243 {
    244 	if (field == NULL)
    245 		return E_BAD_ARGUMENT;
    246 
    247 	*rows = field->rows;
    248 	*cols = field->cols;
    249 	*frow = field->form_row;
    250 	*fcol = field->form_col;
    251 	*nrow = field->nrows;
    252 	*nbuf = field->nbuf;
    253 
    254 	return E_OK;
    255 }
    256 
    257 /*
    258  * Report the dynamic field information.
    259  */
    260 int
    261 dynamic_field_info(FIELD *field, int *drows, int *dcols, int *max)
    262 {
    263 	if (field == NULL)
    264 		return E_BAD_ARGUMENT;
    265 
    266 	if ((field->opts & O_STATIC) == O_STATIC) {
    267 		*drows = field->rows;
    268 		*dcols = field->cols;
    269 	} else {
    270 		*drows = field->drows;
    271 		*dcols = field->dcols;
    272 	}
    273 
    274 	*max = field->max;
    275 
    276 	return E_OK;
    277 }
    278 
    279 /*
    280  * Set the value of the field buffer to the value given.
    281  */
    282 
    283 int
    284 set_field_buffer(FIELD *field, int buffer, char *value)
    285 {
    286 	unsigned len;
    287 	int status;
    288 
    289 	if (field == NULL)
    290 		return E_BAD_ARGUMENT;
    291 
    292 	if (buffer >= field->nbuf) /* make sure buffer is valid */
    293 		return E_BAD_ARGUMENT;
    294 
    295 	len = strlen(value);
    296 	if (((field->opts & O_STATIC) == O_STATIC) && (len > field->cols)
    297 	    && ((field->rows + field->nrows) == 1))
    298 		len = field->cols;
    299 
    300 #ifdef DEBUG
    301 	if (_formi_create_dbg_file() != E_OK)
    302 		return E_SYSTEM_ERROR;
    303 
    304 	fprintf(dbg,
    305 		"set_field_buffer: entry: len = %d, value = %s, buffer=%d\n",
    306 		len, value, buffer);
    307 	fprintf(dbg, "set_field_buffer: entry: string = ");
    308 	if (field->buffers[buffer].string != NULL)
    309 		fprintf(dbg, "%s, len = %d\n", field->buffers[buffer].string,
    310 			field->buffers[buffer].length);
    311 	else
    312 		fprintf(dbg, "(null), len = 0\n");
    313 	fprintf(dbg, "set_field_buffer: entry: lines.len = %d\n",
    314 		field->lines[0].length);
    315 #endif
    316 
    317 	if ((field->buffers[buffer].string =
    318 	     (char *) realloc(field->buffers[buffer].string, len + 1)) == NULL)
    319 		return E_SYSTEM_ERROR;
    320 
    321 	strlcpy(field->buffers[buffer].string, value, len + 1);
    322 	field->buffers[buffer].length = len;
    323 	field->buffers[buffer].allocated = len + 1;
    324 
    325 	if (buffer == 0) {
    326 		field->start_char = 0;
    327 		field->start_line = 0;
    328 		field->row_xpos = 0;
    329 		field->cursor_xpos = 0;
    330 		field->cursor_ypos = 0;
    331 		field->row_count = 1; /* must be at least one row */
    332 		field->lines[0].start = 0;
    333 		field->lines[0].end = (len > 0)? (len - 1) : 0;
    334 		field->lines[0].length =
    335 			_formi_tab_expanded_length(field->buffers[0].string,
    336 						   0, field->lines[0].end);
    337 
    338 		  /* we have to hope the wrap works - if it does not then the
    339 		     buffer is pretty much borked */
    340 		status = _formi_wrap_field(field, 0);
    341 		if (status != E_OK)
    342 			return status;
    343 
    344 		  /*
    345 		   * calculate the tabs for a single row field, the
    346 		   * multiline case is handled when the wrap is done.
    347 		   */
    348 		if (field->row_count == 1)
    349 			_formi_calculate_tabs(field, 0);
    350 
    351 		  /* redraw the field to reflect the new contents. If the field
    352 		   * is attached....
    353 		   */
    354 		if ((field->parent != NULL) && (field->parent->posted == 1)) {
    355 			_formi_redraw_field(field->parent, field->index);
    356 			  /* make sure cursor goes back to current field */
    357 			pos_form_cursor(field->parent);
    358 		}
    359 
    360 	}
    361 
    362 #ifdef DEBUG
    363 	fprintf(dbg, "set_field_buffer: exit: len = %d, value = %s\n",
    364 		len, value);
    365 	fprintf(dbg, "set_field_buffer: exit: string = %s, len = %d\n",
    366 		field->buffers[buffer].string, field->buffers[buffer].length);
    367 	fprintf(dbg, "set_field_buffer: exit: lines.len = %d\n",
    368 		field->lines[0].length);
    369 #endif
    370 
    371 	return E_OK;
    372 }
    373 
    374 /*
    375  * Return the requested field buffer to the caller.
    376  */
    377 char *
    378 field_buffer(FIELD *field, int buffer)
    379 {
    380 
    381 	if (field == NULL)
    382 		return NULL;
    383 
    384 	if (buffer >= field->nbuf)
    385 		return NULL;
    386 
    387 	return field->buffers[buffer].string;
    388 }
    389 
    390 /*
    391  * Set the buffer 0 field status.
    392  */
    393 int
    394 set_field_status(FIELD *field, int status)
    395 {
    396 
    397 	if (field == NULL)
    398 		return E_BAD_ARGUMENT;
    399 
    400 	if (status != FALSE)
    401 		field->buf0_status = TRUE;
    402 	else
    403 		field->buf0_status = FALSE;
    404 
    405 	return E_OK;
    406 }
    407 
    408 /*
    409  * Return the buffer 0 status flag for the given field.
    410  */
    411 int
    412 field_status(FIELD *field)
    413 {
    414 
    415 	if (field == NULL) /* the default buffer 0 never changes :-) */
    416 		return FALSE;
    417 
    418 	return field->buf0_status;
    419 }
    420 
    421 /*
    422  * Set the maximum growth for a dynamic field.
    423  */
    424 int
    425 set_max_field(FIELD *fptr, int max)
    426 {
    427 	FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;
    428 
    429 	if ((field->opts & O_STATIC) == O_STATIC) /* check if field dynamic */
    430 		return E_BAD_ARGUMENT;
    431 
    432 	if (max < 0) /* negative numbers are bad.... */
    433 		return E_BAD_ARGUMENT;
    434 
    435 	field->max = max;
    436 	return E_OK;
    437 }
    438 
    439 /*
    440  * Set the field foreground character attributes.
    441  */
    442 int
    443 set_field_fore(FIELD *fptr, chtype attribute)
    444 {
    445 	FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;
    446 
    447 	field->fore = attribute;
    448 	return E_OK;
    449 }
    450 
    451 /*
    452  * Return the foreground character attribute for the given field.
    453  */
    454 chtype
    455 field_fore(FIELD *field)
    456 {
    457 	if (field == NULL)
    458 		return _formi_default_field.fore;
    459 	else
    460 		return field->fore;
    461 }
    462 
    463 /*
    464  * Set the background character attribute for the given field.
    465  */
    466 int
    467 set_field_back(FIELD *field, chtype attribute)
    468 {
    469 	if (field == NULL)
    470 		_formi_default_field.back = attribute;
    471 	else
    472 		field->back = attribute;
    473 
    474 	return E_OK;
    475 }
    476 
    477 /*
    478  * Get the background character attribute for the given field.
    479  */
    480 chtype
    481 field_back(FIELD *field)
    482 {
    483 	if (field == NULL)
    484 		return _formi_default_field.back;
    485 	else
    486 		return field->back;
    487 }
    488 
    489 /*
    490  * Set the pad character for the given field.
    491  */
    492 int
    493 set_field_pad(FIELD *field, int pad)
    494 {
    495 	if (field == NULL)
    496 		_formi_default_field.pad = pad;
    497 	else
    498 		field->pad = pad;
    499 
    500 	return E_OK;
    501 }
    502 
    503 /*
    504  * Return the padding character for the given field.
    505  */
    506 int
    507 field_pad(FIELD *field)
    508 {
    509 	if (field == NULL)
    510 		return _formi_default_field.pad;
    511 	else
    512 		return field->pad;
    513 }
    514 
    515 /*
    516  * Set the field initialisation function hook.
    517  */
    518 int
    519 set_field_init(FORM *form, Form_Hook function)
    520 {
    521 	if (form == NULL)
    522 		_formi_default_form.field_init = function;
    523 	else
    524 		form->field_init = function;
    525 
    526 	return E_OK;
    527 }
    528 
    529 /*
    530  * Return the function hook for the field initialisation.
    531  */
    532 Form_Hook
    533 field_init(FORM *form)
    534 {
    535 	if (form == NULL)
    536 		return _formi_default_form.field_init;
    537 	else
    538 		return form->field_init;
    539 }
    540 
    541 /*
    542  * Set the field termination function hook.
    543  */
    544 int
    545 set_field_term(FORM *form, Form_Hook function)
    546 {
    547 	if (form == NULL)
    548 		_formi_default_form.field_term = function;
    549 	else
    550 		form->field_term = function;
    551 
    552 	return E_OK;
    553 }
    554 
    555 /*
    556  * Return the function hook defined for the field termination.
    557  */
    558 Form_Hook
    559 field_term(FORM *form)
    560 {
    561 	if (form == NULL)
    562 		return _formi_default_form.field_term;
    563 	else
    564 		return form->field_term;
    565 }
    566 
    567 /*
    568  * Set the page flag on the given field to indicate it is the start of a
    569  * new page.
    570  */
    571 int
    572 set_new_page(FIELD *fptr, int page)
    573 {
    574 	FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;
    575 
    576 	if (field->parent != NULL) /* check if field is connected to a form */
    577 		return E_CONNECTED;
    578 
    579 	field->page_break = (page != FALSE);
    580 	return E_OK;
    581 }
    582 
    583 /*
    584  * Return the page status for the given field.  TRUE is returned if the
    585  * field is the start of a new page.
    586  */
    587 int
    588 new_page(FIELD *field)
    589 {
    590 	if (field == NULL)
    591 		return _formi_default_field.page_break;
    592 	else
    593 		return field->page_break;
    594 }
    595 
    596 /*
    597  * Return the index of the field in the form fields array.
    598  */
    599 int
    600 field_index(FIELD *field)
    601 {
    602 	if (field == NULL)
    603 		return E_BAD_ARGUMENT;
    604 
    605 	if (field->parent == NULL)
    606 		return E_NOT_CONNECTED;
    607 
    608 	return field->index;
    609 }
    610 
    611 /*
    612  * Internal function that does most of the work to create a new field.
    613  * The new field is initialised from the information in the prototype
    614  * field passed.
    615  * Returns NULL on error.
    616  */
    617 static FIELD *
    618 _formi_create_field(FIELD *prototype, int rows, int cols, int frow,
    619 		    int fcol, int nrows, int nbuf)
    620 {
    621 	FIELD *new;
    622 
    623 	if ((rows <= 0) || (cols <= 0) || (frow < 0) || (fcol < 0) ||
    624 	    (nrows < 0) || (nbuf < 0))
    625 		return NULL;
    626 
    627 	if ((new = (FIELD *)malloc(sizeof(FIELD))) == NULL) {
    628 		return NULL;
    629 	}
    630 
    631 	  /* copy in the default field info */
    632 	bcopy(prototype, new, sizeof(FIELD));
    633 
    634 	new->nbuf = nbuf + 1;
    635 	new->rows = rows;
    636 	new->cols = cols;
    637 	new->form_row = frow;
    638 	new->form_col = fcol;
    639 	new->nrows = nrows;
    640 	new->link = new;
    641 	return new;
    642 }
    643 
    644 /*
    645  * Create a new field structure.
    646  */
    647 FIELD *
    648 new_field(int rows, int cols, int frow, int fcol, int nrows, int nbuf)
    649 {
    650 	FIELD *new;
    651 	size_t buf_len;
    652 	int i;
    653 
    654 
    655 	if ((new = _formi_create_field(&_formi_default_field, rows, cols,
    656 				       frow, fcol, nrows, nbuf)) == NULL)
    657 		return NULL;
    658 
    659 	buf_len = (nbuf + 1) * sizeof(FORM_STR);
    660 
    661 	if ((new->buffers = (FORM_STR *)malloc(buf_len)) == NULL) {
    662 		free(new);
    663 		return NULL;
    664 	}
    665 
    666 	  /* Initialise the strings to a zero length string */
    667 	for (i = 0; i < nbuf + 1; i++) {
    668 		if ((new->buffers[i].string =
    669 		     (char *) malloc(sizeof(char))) == NULL) {
    670 			free(new->buffers);
    671 			free(new);
    672 			return NULL;
    673 		}
    674 		new->buffers[i].string[0] = '\0';
    675 		new->buffers[i].length = 0;
    676 		new->buffers[i].allocated = 1;
    677 	}
    678 
    679 	if ((new->lines = (_FORMI_FIELD_LINES *)
    680 	     malloc(sizeof(struct _formi_field_lines))) == NULL) {
    681 		free(new->buffers);
    682 		free(new);
    683 		return NULL;
    684 	}
    685 
    686 	new->lines_alloced = 1;
    687 	new->lines[0].length = 0;
    688 	new->lines[0].start = 0;
    689 	new->lines[0].end = 0;
    690 	new->lines[0].tabs = NULL;
    691 
    692 	return new;
    693 }
    694 
    695 /*
    696  * Duplicate the given field, including it's buffers.
    697  */
    698 FIELD *
    699 dup_field(FIELD *field, int frow, int fcol)
    700 {
    701 	FIELD *new;
    702 	size_t row_len, buf_len;
    703 
    704 	if (field == NULL)
    705 		return NULL;
    706 
    707 	  /* XXXX this right???? */
    708 	if ((new = _formi_create_field(field, (int) field->rows,
    709 				       (int ) field->cols,
    710 				       frow, fcol, (int) field->nrows,
    711 				       field->nbuf - 1)) == NULL)
    712 		return NULL;
    713 
    714 	row_len = (field->rows + field->nrows + 1) * field->cols;
    715 	buf_len = (field->nbuf + 1) * row_len * sizeof(FORM_STR);
    716 
    717 	if ((new->buffers = (FORM_STR *)malloc(buf_len)) == NULL) {
    718 		free(new);
    719 		return NULL;
    720 	}
    721 
    722 	  /* copy the buffers from the source field into the new copy */
    723 	bcopy(field->buffers, new->buffers, buf_len);
    724 
    725 	return new;
    726 }
    727 
    728 /*
    729  * Create a new field at the specified location by duplicating the given
    730  * field.  The buffers are shared with the parent field.
    731  */
    732 FIELD *
    733 link_field(FIELD *field, int frow, int fcol)
    734 {
    735 	FIELD *new;
    736 
    737 	if (field == NULL)
    738 		return NULL;
    739 
    740 	if ((new = _formi_create_field(field, (int) field->rows,
    741 				       (int) field->cols,
    742 				       frow, fcol, (int) field->nrows,
    743 				       field->nbuf - 1)) == NULL)
    744 		return NULL;
    745 
    746 	new->link = field->link;
    747 	field->link = new;
    748 
    749 	  /* we are done.  The buffer pointer was copied during the field
    750 	     creation. */
    751 	return new;
    752 }
    753 
    754 /*
    755  * Release all storage allocated to the field
    756  */
    757 int
    758 free_field(FIELD *field)
    759 {
    760 	FIELD *flink;
    761 	int i;
    762 	_formi_tab_t *ts, *nts;
    763 
    764 	if (field == NULL)
    765 		return E_BAD_ARGUMENT;
    766 
    767 	if (field->parent != NULL)
    768 		return E_CONNECTED;
    769 
    770 	if (field->link == field) { /* check if field linked */
    771 		  /* no it is not - release the buffers */
    772 		free(field->buffers);
    773 		  /* free the tab structures */
    774 		for (i = 0; i < field->row_count - 1; i++) {
    775 			if (field->lines[i].tabs != NULL) {
    776 				ts = field->lines[i].tabs;
    777 				while (ts != NULL) {
    778 					nts = ts->fwd;
    779 					free(ts);
    780 					ts = nts;
    781 				}
    782 			}
    783 		}
    784 	} else {
    785 		  /* is linked, traverse the links to find the field referring
    786 		   * to the one to be freed.
    787 		   */
    788 		for (flink = field->link; flink != field; flink = flink->link);
    789 		flink->link = field->link;
    790 	}
    791 
    792 	free(field);
    793 	return E_OK;
    794 }
    795 
    796 
    797