Home | History | Annotate | Line # | Download | only in libform
field.c revision 1.17
      1 /*	$NetBSD: field.c,v 1.17 2002/07/29 05:17:37 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 	}
    357 
    358 #ifdef DEBUG
    359 	fprintf(dbg, "set_field_buffer: exit: len = %d, value = %s\n",
    360 		len, value);
    361 	fprintf(dbg, "set_field_buffer: exit: string = %s, len = %d\n",
    362 		field->buffers[buffer].string, field->buffers[buffer].length);
    363 	fprintf(dbg, "set_field_buffer: exit: lines.len = %d\n",
    364 		field->lines[0].length);
    365 #endif
    366 
    367 	return E_OK;
    368 }
    369 
    370 /*
    371  * Return the requested field buffer to the caller.
    372  */
    373 char *
    374 field_buffer(FIELD *field, int buffer)
    375 {
    376 
    377 	if (field == NULL)
    378 		return NULL;
    379 
    380 	if (buffer >= field->nbuf)
    381 		return NULL;
    382 
    383 	return field->buffers[buffer].string;
    384 }
    385 
    386 /*
    387  * Set the buffer 0 field status.
    388  */
    389 int
    390 set_field_status(FIELD *field, int status)
    391 {
    392 
    393 	if (field == NULL)
    394 		return E_BAD_ARGUMENT;
    395 
    396 	if (status != FALSE)
    397 		field->buf0_status = TRUE;
    398 	else
    399 		field->buf0_status = FALSE;
    400 
    401 	return E_OK;
    402 }
    403 
    404 /*
    405  * Return the buffer 0 status flag for the given field.
    406  */
    407 int
    408 field_status(FIELD *field)
    409 {
    410 
    411 	if (field == NULL) /* the default buffer 0 never changes :-) */
    412 		return FALSE;
    413 
    414 	return field->buf0_status;
    415 }
    416 
    417 /*
    418  * Set the maximum growth for a dynamic field.
    419  */
    420 int
    421 set_max_field(FIELD *fptr, int max)
    422 {
    423 	FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;
    424 
    425 	if ((field->opts & O_STATIC) == O_STATIC) /* check if field dynamic */
    426 		return E_BAD_ARGUMENT;
    427 
    428 	if (max < 0) /* negative numbers are bad.... */
    429 		return E_BAD_ARGUMENT;
    430 
    431 	field->max = max;
    432 	return E_OK;
    433 }
    434 
    435 /*
    436  * Set the field foreground character attributes.
    437  */
    438 int
    439 set_field_fore(FIELD *fptr, chtype attribute)
    440 {
    441 	FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;
    442 
    443 	field->fore = attribute;
    444 	return E_OK;
    445 }
    446 
    447 /*
    448  * Return the foreground character attribute for the given field.
    449  */
    450 chtype
    451 field_fore(FIELD *field)
    452 {
    453 	if (field == NULL)
    454 		return _formi_default_field.fore;
    455 	else
    456 		return field->fore;
    457 }
    458 
    459 /*
    460  * Set the background character attribute for the given field.
    461  */
    462 int
    463 set_field_back(FIELD *field, chtype attribute)
    464 {
    465 	if (field == NULL)
    466 		_formi_default_field.back = attribute;
    467 	else
    468 		field->back = attribute;
    469 
    470 	return E_OK;
    471 }
    472 
    473 /*
    474  * Get the background character attribute for the given field.
    475  */
    476 chtype
    477 field_back(FIELD *field)
    478 {
    479 	if (field == NULL)
    480 		return _formi_default_field.back;
    481 	else
    482 		return field->back;
    483 }
    484 
    485 /*
    486  * Set the pad character for the given field.
    487  */
    488 int
    489 set_field_pad(FIELD *field, int pad)
    490 {
    491 	if (field == NULL)
    492 		_formi_default_field.pad = pad;
    493 	else
    494 		field->pad = pad;
    495 
    496 	return E_OK;
    497 }
    498 
    499 /*
    500  * Return the padding character for the given field.
    501  */
    502 int
    503 field_pad(FIELD *field)
    504 {
    505 	if (field == NULL)
    506 		return _formi_default_field.pad;
    507 	else
    508 		return field->pad;
    509 }
    510 
    511 /*
    512  * Set the field initialisation function hook.
    513  */
    514 int
    515 set_field_init(FORM *form, Form_Hook function)
    516 {
    517 	if (form == NULL)
    518 		_formi_default_form.field_init = function;
    519 	else
    520 		form->field_init = function;
    521 
    522 	return E_OK;
    523 }
    524 
    525 /*
    526  * Return the function hook for the field initialisation.
    527  */
    528 Form_Hook
    529 field_init(FORM *form)
    530 {
    531 	if (form == NULL)
    532 		return _formi_default_form.field_init;
    533 	else
    534 		return form->field_init;
    535 }
    536 
    537 /*
    538  * Set the field termination function hook.
    539  */
    540 int
    541 set_field_term(FORM *form, Form_Hook function)
    542 {
    543 	if (form == NULL)
    544 		_formi_default_form.field_term = function;
    545 	else
    546 		form->field_term = function;
    547 
    548 	return E_OK;
    549 }
    550 
    551 /*
    552  * Return the function hook defined for the field termination.
    553  */
    554 Form_Hook
    555 field_term(FORM *form)
    556 {
    557 	if (form == NULL)
    558 		return _formi_default_form.field_term;
    559 	else
    560 		return form->field_term;
    561 }
    562 
    563 /*
    564  * Set the page flag on the given field to indicate it is the start of a
    565  * new page.
    566  */
    567 int
    568 set_new_page(FIELD *fptr, int page)
    569 {
    570 	FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;
    571 
    572 	if (field->parent != NULL) /* check if field is connected to a form */
    573 		return E_CONNECTED;
    574 
    575 	field->page_break = (page != FALSE);
    576 	return E_OK;
    577 }
    578 
    579 /*
    580  * Return the page status for the given field.  TRUE is returned if the
    581  * field is the start of a new page.
    582  */
    583 int
    584 new_page(FIELD *field)
    585 {
    586 	if (field == NULL)
    587 		return _formi_default_field.page_break;
    588 	else
    589 		return field->page_break;
    590 }
    591 
    592 /*
    593  * Return the index of the field in the form fields array.
    594  */
    595 int
    596 field_index(FIELD *field)
    597 {
    598 	if (field == NULL)
    599 		return E_BAD_ARGUMENT;
    600 
    601 	if (field->parent == NULL)
    602 		return E_NOT_CONNECTED;
    603 
    604 	return field->index;
    605 }
    606 
    607 /*
    608  * Internal function that does most of the work to create a new field.
    609  * The new field is initialised from the information in the prototype
    610  * field passed.
    611  * Returns NULL on error.
    612  */
    613 static FIELD *
    614 _formi_create_field(FIELD *prototype, int rows, int cols, int frow,
    615 		    int fcol, int nrows, int nbuf)
    616 {
    617 	FIELD *new;
    618 
    619 	if ((rows <= 0) || (cols <= 0) || (frow < 0) || (fcol < 0) ||
    620 	    (nrows < 0) || (nbuf < 0))
    621 		return NULL;
    622 
    623 	if ((new = (FIELD *)malloc(sizeof(FIELD))) == NULL) {
    624 		return NULL;
    625 	}
    626 
    627 	  /* copy in the default field info */
    628 	bcopy(prototype, new, sizeof(FIELD));
    629 
    630 	new->nbuf = nbuf + 1;
    631 	new->rows = rows;
    632 	new->cols = cols;
    633 	new->form_row = frow;
    634 	new->form_col = fcol;
    635 	new->nrows = nrows;
    636 	new->link = new;
    637 	return new;
    638 }
    639 
    640 /*
    641  * Create a new field structure.
    642  */
    643 FIELD *
    644 new_field(int rows, int cols, int frow, int fcol, int nrows, int nbuf)
    645 {
    646 	FIELD *new;
    647 	size_t buf_len;
    648 	int i;
    649 
    650 
    651 	if ((new = _formi_create_field(&_formi_default_field, rows, cols,
    652 				       frow, fcol, nrows, nbuf)) == NULL)
    653 		return NULL;
    654 
    655 	buf_len = (nbuf + 1) * sizeof(FORM_STR);
    656 
    657 	if ((new->buffers = (FORM_STR *)malloc(buf_len)) == NULL) {
    658 		free(new);
    659 		return NULL;
    660 	}
    661 
    662 	  /* Initialise the strings to a zero length string */
    663 	for (i = 0; i < nbuf + 1; i++) {
    664 		if ((new->buffers[i].string =
    665 		     (char *) malloc(sizeof(char))) == NULL) {
    666 			free(new->buffers);
    667 			free(new);
    668 			return NULL;
    669 		}
    670 		new->buffers[i].string[0] = '\0';
    671 		new->buffers[i].length = 0;
    672 		new->buffers[i].allocated = 1;
    673 	}
    674 
    675 	if ((new->lines = (_FORMI_FIELD_LINES *)
    676 	     malloc(sizeof(struct _formi_field_lines))) == NULL) {
    677 		free(new->buffers);
    678 		free(new);
    679 		return NULL;
    680 	}
    681 
    682 	new->lines_alloced = 1;
    683 	new->lines[0].length = 0;
    684 	new->lines[0].start = 0;
    685 	new->lines[0].end = 0;
    686 	new->lines[0].tabs = NULL;
    687 
    688 	return new;
    689 }
    690 
    691 /*
    692  * Duplicate the given field, including it's buffers.
    693  */
    694 FIELD *
    695 dup_field(FIELD *field, int frow, int fcol)
    696 {
    697 	FIELD *new;
    698 	size_t row_len, buf_len;
    699 
    700 	if (field == NULL)
    701 		return NULL;
    702 
    703 	  /* XXXX this right???? */
    704 	if ((new = _formi_create_field(field, (int) field->rows,
    705 				       (int ) field->cols,
    706 				       frow, fcol, (int) field->nrows,
    707 				       field->nbuf - 1)) == NULL)
    708 		return NULL;
    709 
    710 	row_len = (field->rows + field->nrows + 1) * field->cols;
    711 	buf_len = (field->nbuf + 1) * row_len * sizeof(FORM_STR);
    712 
    713 	if ((new->buffers = (FORM_STR *)malloc(buf_len)) == NULL) {
    714 		free(new);
    715 		return NULL;
    716 	}
    717 
    718 	  /* copy the buffers from the source field into the new copy */
    719 	bcopy(field->buffers, new->buffers, buf_len);
    720 
    721 	return new;
    722 }
    723 
    724 /*
    725  * Create a new field at the specified location by duplicating the given
    726  * field.  The buffers are shared with the parent field.
    727  */
    728 FIELD *
    729 link_field(FIELD *field, int frow, int fcol)
    730 {
    731 	FIELD *new;
    732 
    733 	if (field == NULL)
    734 		return NULL;
    735 
    736 	if ((new = _formi_create_field(field, (int) field->rows,
    737 				       (int) field->cols,
    738 				       frow, fcol, (int) field->nrows,
    739 				       field->nbuf - 1)) == NULL)
    740 		return NULL;
    741 
    742 	new->link = field->link;
    743 	field->link = new;
    744 
    745 	  /* we are done.  The buffer pointer was copied during the field
    746 	     creation. */
    747 	return new;
    748 }
    749 
    750 /*
    751  * Release all storage allocated to the field
    752  */
    753 int
    754 free_field(FIELD *field)
    755 {
    756 	FIELD *flink;
    757 	int i;
    758 	_formi_tab_t *ts, *nts;
    759 
    760 	if (field == NULL)
    761 		return E_BAD_ARGUMENT;
    762 
    763 	if (field->parent != NULL)
    764 		return E_CONNECTED;
    765 
    766 	if (field->link == field) { /* check if field linked */
    767 		  /* no it is not - release the buffers */
    768 		free(field->buffers);
    769 		  /* free the tab structures */
    770 		for (i = 0; i < field->row_count - 1; i++) {
    771 			if (field->lines[i].tabs != NULL) {
    772 				ts = field->lines[i].tabs;
    773 				while (ts != NULL) {
    774 					nts = ts->fwd;
    775 					free(ts);
    776 					ts = nts;
    777 				}
    778 			}
    779 		}
    780 	} else {
    781 		  /* is linked, traverse the links to find the field referring
    782 		   * to the one to be freed.
    783 		   */
    784 		for (flink = field->link; flink != field; flink = flink->link);
    785 		flink->link = field->link;
    786 	}
    787 
    788 	free(field);
    789 	return E_OK;
    790 }
    791 
    792 
    793