Home | History | Annotate | Line # | Download | only in libform
internals.c revision 1.5
      1 /*	$NetBSD: internals.c,v 1.5 2001/01/18 23:53:10 blymn Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998-1999 Brett Lymn
      5  *                         (blymn (at) baea.com.au, brett_lymn (at) yahoo.com.au)
      6  * All rights reserved.
      7  *
      8  * This code has been donated to The NetBSD Foundation by the Author.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. The name of the author may not be used to endorse or promote products
     16  *    derived from this software withough specific prior written permission
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  *
     29  *
     30  */
     31 
     32 #include <ctype.h>
     33 #include <stdio.h>
     34 #include <stdlib.h>
     35 #include <strings.h>
     36 #include "internals.h"
     37 #include "form.h"
     38 
     39 #ifdef DEBUG
     40 /*
     41  *  file handle to write debug info to, this will be initialised when
     42  *  the form is first posted.
     43  */
     44 FILE *dbg = NULL;
     45 #endif
     46 
     47 /* define our own min function - this is not generic but will do here
     48  * (don't believe me?  think about what value you would get
     49  * from min(x++, y++)
     50  */
     51 #define min(a,b) (((a) > (b))? (b) : (a))
     52 
     53 /* for the line joining function... */
     54 #define JOIN_NEXT    1
     55 #define JOIN_NEXT_NW 2 /* next join, don't wrap the joined line */
     56 #define JOIN_PREV    3
     57 #define JOIN_PREV_NW 4 /* previous join, don't wrap the joined line */
     58 
     59 static void
     60 _formi_do_validation(FIELD *field, FIELDTYPE *type, int *ret_val);
     61 static int
     62 _formi_join_line(FIELD *field, char *str, unsigned int pos, int direction);
     63 static int
     64 _formi_wrap_field(FIELD *field, unsigned int pos);
     65 static void
     66 _formi_redraw_field(FORM *form, int field);
     67 void
     68 _formi_hscroll_back(FIELD *field, unsigned int amt);
     69 void
     70 _formi_hscroll_fwd(FIELD *field, unsigned int amt);
     71 static void
     72 _formi_scroll_back(FIELD *field, unsigned int amt);
     73 static void
     74 _formi_scroll_fwd(FIELD *field, unsigned int amt);
     75 static int
     76 find_sow(char *str, unsigned int offset);
     77 static int
     78 find_cur_line(FIELD *cur);
     79 
     80 /*
     81  * Open the debug file if it is not already open....
     82  */
     83 #ifdef DEBUG
     84 int
     85 _formi_create_dbg_file(void)
     86 {
     87 	if (dbg == NULL) {
     88 		dbg = fopen("___form_dbg.out", "w");
     89 		if (dbg == NULL) {
     90 			fprintf(stderr, "Cannot open debug file!\n");
     91 			return E_SYSTEM_ERROR;
     92 		}
     93 	}
     94 
     95 	return E_OK;
     96 }
     97 #endif
     98 
     99 /*
    100  * Set the form's current field to the first valid field on the page.
    101  * Assume the fields have been sorted and stitched.
    102  */
    103 int
    104 _formi_pos_first_field(FORM *form)
    105 {
    106 	FIELD *cur;
    107 	int old_page;
    108 
    109 	old_page = form->page;
    110 
    111 	  /* scan forward for an active page....*/
    112 	while (form->page_starts[form->page].in_use == 0) {
    113 		form->page++;
    114 		if (form->page > form->max_page) {
    115 			form->page = old_page;
    116 			return E_REQUEST_DENIED;
    117 		}
    118 	}
    119 
    120 	cur = form->fields[form->page_starts[form->page].first];
    121 	while ((cur->opts & (O_VISIBLE | O_ACTIVE))
    122 	       != (O_VISIBLE | O_ACTIVE)) {
    123 		cur = CIRCLEQ_NEXT(cur, glue);
    124 		if (cur == (void *) &form->sorted_fields) {
    125 			form->page = old_page;
    126 			return E_REQUEST_DENIED;
    127 		}
    128 	}
    129 
    130 	form->cur_field = cur->index;
    131 	return E_OK;
    132 }
    133 
    134 /*
    135  * Set the field to the next active and visible field, the fields are
    136  * traversed in index order in the direction given.  If the parameter
    137  * use_sorted is TRUE then the sorted field list will be traversed instead
    138  * of using the field index.
    139  */
    140 int
    141 _formi_pos_new_field(FORM *form, unsigned direction, unsigned use_sorted)
    142 {
    143 	FIELD *cur;
    144 	int i;
    145 
    146 	i = form->cur_field;
    147 	cur = form->fields[i];
    148 
    149 	do {
    150 		if (direction == _FORMI_FORWARD) {
    151 			if (use_sorted == TRUE) {
    152 				if ((form->wrap == FALSE) &&
    153 				    (cur == CIRCLEQ_LAST(&form->sorted_fields)))
    154 					return E_REQUEST_DENIED;
    155 				cur = CIRCLEQ_NEXT(cur, glue);
    156 				i = cur->index;
    157 			} else {
    158 				if ((form->wrap == FALSE) &&
    159 				    ((i + 1) >= form->field_count))
    160 					return E_REQUEST_DENIED;
    161 				i++;
    162 				if (i >= form->field_count)
    163 					i = 0;
    164 			}
    165 		} else {
    166 			if (use_sorted == TRUE) {
    167 				if ((form->wrap == FALSE) &&
    168 				    (cur == CIRCLEQ_FIRST(&form->sorted_fields)))
    169 					return E_REQUEST_DENIED;
    170 				cur = CIRCLEQ_PREV(cur, glue);
    171 				i = cur->index;
    172 			} else {
    173 				if ((form->wrap == FALSE) && (i <= 0))
    174 					return E_REQUEST_DENIED;
    175 				i--;
    176 				if (i < 0)
    177 					i = form->field_count - 1;
    178 			}
    179 		}
    180 
    181 		if ((form->fields[i]->opts & (O_VISIBLE | O_ACTIVE))
    182 			== (O_VISIBLE | O_ACTIVE)) {
    183 			form->cur_field = i;
    184 			return E_OK;
    185 		}
    186 	}
    187 	while (i != form->cur_field);
    188 
    189 	return E_REQUEST_DENIED;
    190 }
    191 
    192 /*
    193  * Find the line in a field that the cursor is currently on.
    194  */
    195 static int
    196 find_cur_line(FIELD *cur)
    197 {
    198 	unsigned start, end, pos, row;
    199 	const char *str;
    200 
    201 	str = cur->buffers[0].string;
    202 	pos = cur->start_char + cur->hscroll + cur->cursor_xpos;
    203 
    204 	start = 0;
    205 	end = 0;
    206 
    207 	for (row = 0; row < cur->row_count; row++) {
    208 		start = _formi_find_bol(str, start);
    209 		end = _formi_find_eol(str, end);
    210 		if ((pos >= start) && (pos <= end))
    211 			return row;
    212 	}
    213 
    214 	return 0;
    215 }
    216 
    217 
    218 /*
    219  * Word wrap the contents of the field's buffer 0 if this is allowed.
    220  * If the wrap is successful, that is, the row count nor the buffer
    221  * size is exceeded then the function will return E_OK, otherwise it
    222  * will return E_REQUEST_DENIED.
    223  */
    224 static int
    225 _formi_wrap_field(FIELD *field, unsigned int pos)
    226 {
    227 	char *str, *new;
    228 	int width, length, allocated, row_count, sol, eol, wrapped;
    229 	size_t new_size;
    230 
    231 	if ((field->opts & O_WRAP) != O_WRAP)
    232 		return E_REQUEST_DENIED;
    233 
    234 	wrapped = FALSE;
    235 	row_count = 0;
    236 	allocated = field->buffers[0].allocated;
    237 	length = field->buffers[0].length;
    238 	if ((str = (char *) malloc(sizeof(char) * allocated)) == NULL)
    239 		return E_SYSTEM_ERROR;
    240 
    241 	strcpy(str,field->buffers[0].string);
    242 
    243 	if ((field->opts & O_STATIC) == O_STATIC)
    244 		width = field->cols;
    245 	else
    246 		width = field->dcols;
    247 
    248 	while (str[pos] != '\0') {
    249 		row_count++;
    250 		sol = _formi_find_bol(str, pos);
    251 		eol = _formi_find_eol(str, pos);
    252 		if ((eol - sol) <= width) {
    253 			  /* line may be too short, try joining some lines */
    254 			pos = eol;
    255 			if ((eol - sol) == width) {
    256 				/* if line is just right then don't wrap */
    257 				pos++;
    258 				continue;
    259 			}
    260 
    261 			if (_formi_join_line(field, str, pos, JOIN_NEXT_NW)
    262 			    == E_OK) {
    263 				row_count--; /* cuz we just joined a line */
    264 				wrapped = TRUE;
    265 			} else
    266 				break;
    267 		} else {
    268 			  /* line is too long, split it - maybe */
    269 			  /* split on first whitespace before current word */
    270 			pos = sol + width;
    271 			if (!isblank(str[pos]))
    272 				pos = find_sow(str, pos);
    273 
    274 			if (pos != sol) {
    275 				if (length + 1 >= allocated) {
    276 					new_size = allocated + 64
    277 						- (allocated % 64);
    278 
    279 					if ((new = (char *) realloc(str,
    280 								    sizeof(char) * new_size)
    281 					     ) == NULL) {
    282 						free(str);
    283 						return E_SYSTEM_ERROR;
    284 					}
    285 					str = new;
    286 					allocated = new_size;
    287 				}
    288 
    289 				bcopy(&str[pos], &str[pos + 1],
    290 				      (unsigned) length - pos - 1);
    291 				str[pos] = '\n';
    292 				pos = pos + 1;
    293 				length++;
    294 				wrapped = TRUE;
    295 			} else
    296 				break;
    297 		}
    298 	}
    299 
    300 	if (row_count > field->rows) {
    301 		free(str);
    302 		return E_REQUEST_DENIED;
    303 	}
    304 
    305 	if (wrapped == TRUE) {
    306 		field->buffers[0].length = length;
    307 		field->buffers[0].allocated = allocated;
    308 		free(field->buffers[0].string);
    309 		field->buffers[0].string = str;
    310 	} else /* all that work was in vain.... */
    311 		free(str);
    312 
    313 	return E_OK;
    314 }
    315 
    316 /*
    317  * Join the two lines that surround the location pos, the type
    318  * variable indicates the direction of the join.  Note that pos is
    319  * assumed to be at either the end of the line for a JOIN_NEXT or at
    320  * the beginning of the line for a JOIN_PREV.  We need to check the
    321  * field options to ensure the join does not overflow the line limit
    322  * (if wrap is off) or wrap the field buffer again.  Returns E_OK if
    323  * the join was successful or E_REQUEST_DENIED if the join cannot
    324  * happen.
    325  */
    326 static int
    327 _formi_join_line(FIELD *field, char *str, unsigned int pos, int direction)
    328 {
    329 	unsigned int len, sol, eol, npos, start, dest;
    330 
    331 	npos = pos;
    332 
    333 	if ((direction == JOIN_NEXT) || (direction == JOIN_NEXT_NW)) {
    334 		sol = _formi_find_bol(str, pos);
    335 		npos++;
    336 		  /* see if there is another line following... */
    337 		if (str[npos] == '\0')
    338 			return E_REQUEST_DENIED;
    339 		eol = _formi_find_eol(str, npos);
    340 
    341 		start = npos;
    342 		dest = pos;
    343 		len = eol - npos;
    344 	} else {
    345 		if (pos == 0)
    346 			return E_REQUEST_DENIED;
    347 		eol = _formi_find_eol(str, pos);
    348 		npos--;
    349 		sol = _formi_find_bol(str, npos);
    350 
    351 		start = pos;
    352 		dest = npos;
    353 		len = eol - pos;
    354 	}
    355 
    356 
    357 	  /* if we cannot wrap and the length of the resultant line
    358 	   * is bigger than our field width we shall deny the request.
    359 	   */
    360 	if (((field->opts & O_WRAP) != O_WRAP) && /* XXXXX check for dynamic field */
    361 	    ((sol + eol - 1) > field->cols))
    362 		return E_REQUEST_DENIED;
    363 
    364 	bcopy(&str[start], &str[dest], (unsigned) len);
    365 
    366 	  /* wrap the field if required, if this fails undo the change */
    367 	if ((direction == JOIN_NEXT) || (direction == JOIN_PREV)) {
    368 		if (_formi_wrap_field(field, (unsigned int) pos) != E_OK) {
    369 			bcopy(&str[dest], &str[start], (unsigned) len);
    370 			str[dest] = '\n';
    371 			return E_REQUEST_DENIED;
    372 		}
    373 	}
    374 
    375 	return E_OK;
    376 }
    377 
    378 /*
    379  * skip the blanks in the given string, start at the index start and
    380  * continue forward until either the end of the string or a non-blank
    381  * character is found.  Return the index of either the end of the string or
    382  * the first non-blank character.
    383  */
    384 unsigned
    385 skip_blanks(char *string, unsigned int start)
    386 {
    387 	unsigned int i;
    388 
    389 	i = start;
    390 
    391 	while ((string[i] != '\0') && isblank(string[i]))
    392 		i++;
    393 
    394 	return i;
    395 }
    396 
    397 /*
    398  * Return the index of the top left most field of the two given fields.
    399  */
    400 static int
    401 _formi_top_left(FORM *form, int a, int b)
    402 {
    403 	  /* lower row numbers always win here.... */
    404 	if (form->fields[a]->form_row < form->fields[b]->form_row)
    405 		return a;
    406 
    407 	if (form->fields[a]->form_row > form->fields[b]->form_row)
    408 		return b;
    409 
    410 	  /* rows must be equal, check columns */
    411 	if (form->fields[a]->form_col < form->fields[b]->form_col)
    412 		return a;
    413 
    414 	if (form->fields[a]->form_col > form->fields[b]->form_col)
    415 		return b;
    416 
    417 	  /* if we get here fields must be in exactly the same place, punt */
    418 	return a;
    419 }
    420 
    421 /*
    422  * Return the index to the field that is the bottom-right-most of the
    423  * two given fields.
    424  */
    425 static int
    426 _formi_bottom_right(FORM *form, int a, int b)
    427 {
    428 	  /* check the rows first, biggest row wins */
    429 	if (form->fields[a]->form_row > form->fields[b]->form_row)
    430 		return a;
    431 	if (form->fields[a]->form_row < form->fields[b]->form_row)
    432 		return b;
    433 
    434 	  /* rows must be equal, check cols, biggest wins */
    435 	if (form->fields[a]->form_col > form->fields[b]->form_col)
    436 		return a;
    437 	if (form->fields[a]->form_col < form->fields[b]->form_col)
    438 		return b;
    439 
    440 	  /* fields in the same place, punt */
    441 	return a;
    442 }
    443 
    444 /*
    445  * Find the next '\n' character in the given string starting at offset
    446  * if there are no newlines found then return the index to the end of the
    447  * string.
    448  */
    449 int
    450 _formi_find_eol(const char *string, unsigned int offset)
    451 {
    452 	char *location;
    453 	int eol;
    454 
    455 	if ((location = index(&string[offset], '\n')) != NULL)
    456 		eol  = location - string;
    457 	else
    458 		eol = strlen(string);
    459 
    460 	if (eol > 0)
    461 		eol--;
    462 
    463 	return eol;
    464 }
    465 
    466 /*
    467  * Find the previous '\n' character in the given string starting at offset
    468  * if there are no newlines found then return 0.
    469  */
    470 int
    471 _formi_find_bol(const char *string, unsigned int offset)
    472 {
    473 	int cnt;
    474 
    475 	cnt = offset;
    476 	while ((cnt > 0) && (string[cnt] != '\n'))
    477 		cnt--;
    478 
    479 	  /* if we moved and found a newline go forward one to point at the
    480 	   * actual start of the line....
    481 	   */
    482 	if ((cnt != offset) && (string[cnt] == '\n'))
    483 		cnt++;
    484 
    485 	return cnt;
    486 }
    487 
    488 /*
    489  * Find the end of the current word in the string str, starting at
    490  * offset - the end includes any trailing whitespace.  If the end of
    491  * the string is found before a new word then just return the offset
    492  * to the end of the string.
    493  */
    494 static int
    495 find_eow(char *str, unsigned int offset)
    496 {
    497 	int start;
    498 
    499 	start = offset;
    500 	  /* first skip any non-whitespace */
    501 	while ((str[start] != '\0') && !isblank(str[start]))
    502 		start++;
    503 
    504 	  /* see if we hit the end of the string */
    505 	if (str[start] == '\0')
    506 		return start;
    507 
    508 	  /* otherwise skip the whitespace.... */
    509 	while ((str[start] != '\0') && isblank(str[start]))
    510 		start++;
    511 
    512 	return start;
    513 }
    514 
    515 /*
    516  * Find the beginning of the current word in the string str, starting
    517  * at offset.
    518  */
    519 static int
    520 find_sow(char *str, unsigned int offset)
    521 {
    522 	int start;
    523 
    524 	start = offset;
    525 
    526 	if (start > 0) {
    527 		if (isblank(str[start]) || isblank(str[start - 1])) {
    528 			if (isblank(str[start - 1]))
    529 				start--;
    530 			  /* skip the whitespace.... */
    531 			while ((start >= 0) && isblank(str[start]))
    532 				start--;
    533 		}
    534 	}
    535 
    536 	  /* see if we hit the start of the string */
    537 	if (start < 0)
    538 		return 0;
    539 
    540 	  /* now skip any non-whitespace */
    541 	while ((start >= 0) && !isblank(str[start]))
    542 		start--;
    543 
    544 	if (start > 0)
    545 		start++; /* last loop has us pointing at a space, adjust */
    546 
    547 	if (start < 0)
    548 		start = 0;
    549 
    550 	return start;
    551 }
    552 
    553 /*
    554  * Scroll the field forward the given number of lines.
    555  */
    556 static void
    557 _formi_scroll_fwd(FIELD *field, unsigned int amt)
    558 {
    559 	  /* check if we have lines to scroll */
    560 	if (field->row_count < (field->start_line + field->rows))
    561 		return;
    562 
    563 	field->start_line += min(amt,
    564 				 field->row_count - field->start_line
    565 				 - field->rows);
    566 }
    567 
    568 /*
    569  * Scroll the field backward the given number of lines.
    570  */
    571 static void
    572 _formi_scroll_back(FIELD *field, unsigned int amt)
    573 {
    574 	if (field->start_line == 0)
    575 		return;
    576 
    577 	field->start_line -= min(field->start_line, amt);
    578 }
    579 
    580 /*
    581  * Scroll the field forward the given number of characters.
    582  */
    583 void
    584 _formi_hscroll_fwd(FIELD *field, int unsigned amt)
    585 {
    586 	int end, scroll_amt;
    587 
    588 	end = _formi_find_eol(field->buffers[0].string,
    589 		       field->start_char + field->hscroll
    590 		       + field->cursor_xpos) - field->start_char
    591 		- field->hscroll - field->cursor_xpos;
    592 
    593 	scroll_amt = min(amt, end);
    594 	if (scroll_amt < 0)
    595 		scroll_amt = 0;
    596 
    597 	field->hscroll += scroll_amt;
    598 	if (amt > field->cursor_xpos)
    599 		field->cursor_xpos = 0;
    600 	else
    601 		field->cursor_xpos -= scroll_amt;
    602 }
    603 
    604 /*
    605  * Scroll the field backward the given number of characters.
    606  */
    607 void
    608 _formi_hscroll_back(FIELD *field, unsigned int amt)
    609 {
    610 	int flen, sa;
    611 
    612 	sa = min(field->hscroll, amt);
    613 	field->hscroll -= sa;
    614 	field->cursor_xpos += sa;
    615 	flen = field->cols;
    616 	if (field->start_char > 0)
    617 		flen--;
    618 	if (field->cursor_xpos > flen)
    619 		field->cursor_xpos = flen;
    620 }
    621 
    622 /*
    623  * Find the different pages in the form fields and assign the form
    624  * page_starts array with the information to find them.
    625  */
    626 int
    627 _formi_find_pages(FORM *form)
    628 {
    629 	int i, cur_page = 0;
    630 
    631 	if ((form->page_starts = (_FORMI_PAGE_START *)
    632 	     malloc((form->max_page + 1) * sizeof(_FORMI_PAGE_START))) == NULL)
    633 		return E_SYSTEM_ERROR;
    634 
    635 	  /* initialise the page starts array */
    636 	memset(form->page_starts, 0,
    637 	       (form->max_page + 1) * sizeof(_FORMI_PAGE_START));
    638 
    639 	for (i =0; i < form->field_count; i++) {
    640 		if (form->fields[i]->page_break == 1)
    641 			cur_page++;
    642 		if (form->page_starts[cur_page].in_use == 0) {
    643 			form->page_starts[cur_page].in_use = 1;
    644 			form->page_starts[cur_page].first = i;
    645 			form->page_starts[cur_page].last = i;
    646 			form->page_starts[cur_page].top_left = i;
    647 			form->page_starts[cur_page].bottom_right = i;
    648 		} else {
    649 			form->page_starts[cur_page].last = i;
    650 			form->page_starts[cur_page].top_left =
    651 				_formi_top_left(form,
    652 						form->page_starts[cur_page].top_left,
    653 						i);
    654 			form->page_starts[cur_page].bottom_right =
    655 				_formi_bottom_right(form,
    656 						    form->page_starts[cur_page].bottom_right,
    657 						    i);
    658 		}
    659 	}
    660 
    661 	return E_OK;
    662 }
    663 
    664 /*
    665  * Completely redraw the field of the given form.
    666  */
    667 static void
    668 _formi_redraw_field(FORM *form, int field)
    669 {
    670 	unsigned int pre, post, flen, slen, i, row, start, end, offset;
    671 	char *str;
    672 	FIELD *cur;
    673 #ifdef DEBUG
    674 	char buffer[100];
    675 #endif
    676 
    677 	cur = form->fields[field];
    678 	str = cur->buffers[0].string;
    679 	flen = cur->cols;
    680 	slen = 0;
    681 	start = 0;
    682 	end = 0;
    683 
    684 	wmove(form->subwin, (int) cur->form_row, (int) cur->form_col);
    685 	for (row = 0; row <= cur->row_count; row++) {
    686 		if (str == NULL) {
    687 			start = end = 0;
    688 		} else {
    689 			if ((str[end] == '\0') || (str[end + 1] == '\0')
    690 			    || (row == 0))
    691 				start = end;
    692 			else
    693 				start = end + 1;
    694 		}
    695 
    696 		if (cur->buffers[0].length > 0) {
    697 			end = _formi_find_eol(str, start);
    698 			slen = end - start + 1;
    699 		} else
    700 			slen = 0;
    701 
    702 		switch (cur->justification) {
    703 		case JUSTIFY_RIGHT:
    704 			post = 0;
    705 			if (flen < slen)
    706 				pre = 0;
    707 			else
    708 				pre = flen - slen;
    709 
    710 			break;
    711 
    712 		case JUSTIFY_CENTER:
    713 			if (flen < slen) {
    714 				pre = 0;
    715 				post = 0;
    716 			} else {
    717 				pre = flen - slen;
    718 				post = pre = pre / 2;
    719 				  /* get padding right if centring is not even */
    720 				if ((post + pre + slen) < flen)
    721 					post++;
    722 			}
    723 			break;
    724 
    725 		case NO_JUSTIFICATION:
    726 		case JUSTIFY_LEFT:
    727 		default:
    728 			pre = 0;
    729 			if (flen <= slen)
    730 				post = 0;
    731 			else {
    732 				post = flen - slen;
    733 				if (post > flen)
    734 					post = flen;
    735 			}
    736 			break;
    737 		}
    738 
    739 		if (pre > cur->hscroll - start)
    740 			pre = pre - cur->hscroll + start;
    741 		else
    742 			pre = 0;
    743 
    744 		if (slen > cur->hscroll) {
    745 			slen -= cur->hscroll;
    746 			post += cur->hscroll;
    747 			if (post > flen)
    748 				post = flen;
    749 		} else {
    750 			slen = 0;
    751 			post = flen - pre;
    752 		}
    753 
    754 		if (form->cur_field == field)
    755 			wattrset(form->subwin, cur->fore);
    756 		else
    757 			wattrset(form->subwin, cur->back);
    758 
    759 #ifdef DEBUG
    760 		fprintf(dbg, "redraw_field: start=%d, pre=%d, slen=%d, flen=%d, post=%d, hscroll=%d\n",
    761 			start, pre, slen, flen, post, cur->hscroll);
    762 		if (str != NULL) {
    763 			strncpy(buffer, &str[cur->start_char], flen);
    764 		} else {
    765 			strcpy(buffer, "(null)");
    766 		}
    767 		buffer[flen] = '\0';
    768 		fprintf(dbg, "redraw_field: %s\n", buffer);
    769 #endif
    770 
    771 		for (i = start + cur->hscroll; i < pre; i++)
    772 			waddch(form->subwin, cur->pad);
    773 
    774 		offset = cur->hscroll;
    775 		if (cur->start_char > 0)
    776 			offset += cur->start_char - 1;
    777 
    778 		if (flen > cur->hscroll + 1) {
    779 			if (flen > slen)
    780 				flen -= cur->hscroll + 1;
    781 		} else
    782 			flen = 0;
    783 
    784 #ifdef DEBUG
    785 		fprintf(dbg, "redraw_field: will add %d chars, offset is %d\n",
    786 			min(slen, flen), offset);
    787 #endif
    788 		for (i = 0;
    789 		     i < min(slen, flen); i++)
    790 		{
    791 #ifdef DEBUG
    792 			fprintf(dbg, "adding char str[%d]=%c\n",
    793 				i + offset, str[i + offset]);
    794 #endif
    795 			waddch(form->subwin,
    796 			       ((cur->opts & O_PUBLIC) == O_PUBLIC)?
    797 			       str[i + offset] : cur->pad);
    798 		}
    799 
    800 		for (i = 0; i < post; i++)
    801 			waddch(form->subwin, cur->pad);
    802 	}
    803 
    804 	return;
    805 }
    806 
    807 /*
    808  * Display the fields attached to the form that are on the current page
    809  * on the screen.
    810  *
    811  */
    812 int
    813 _formi_draw_page(FORM *form)
    814 {
    815 	int i;
    816 
    817 	if (form->page_starts[form->page].in_use == 0)
    818 		return E_BAD_ARGUMENT;
    819 
    820 	wclear(form->subwin);
    821 
    822 	for (i = form->page_starts[form->page].first;
    823 	     i <= form->page_starts[form->page].last; i++)
    824 		_formi_redraw_field(form, i);
    825 
    826 	return E_OK;
    827 }
    828 
    829 /*
    830  * Add the character c at the position pos in buffer 0 of the given field
    831  */
    832 int
    833 _formi_add_char(FIELD *field, unsigned int pos, char c)
    834 {
    835 	char *new;
    836 	unsigned int new_size;
    837 	int status;
    838 
    839 	  /*
    840 	   * If buffer has not had a string before, set it to a blank
    841 	   * string.  Everything should flow from there....
    842 	   */
    843 	if (field->buffers[0].string == NULL) {
    844 		set_field_buffer(field, 0, "");
    845 	}
    846 
    847 #ifdef DEBUG
    848 	fprintf(dbg, "add_char: pos=%d, char=%c\n", pos, c);
    849 	fprintf(dbg,
    850 	   "add_char enter: xpos=%d, start=%d, length=%d(%d), allocated=%d\n",
    851 		field->cursor_xpos, field->start_char,
    852 		field->buffers[0].length, strlen(field->buffers[0].string),
    853 		field->buffers[0].allocated);
    854 	fprintf(dbg, "add_char enter: %s\n", field->buffers[0].string);
    855 #endif
    856 	if (((field->opts & O_BLANK) == O_BLANK) &&
    857 	    (field->buf0_status == FALSE)) {
    858 		field->buffers[0].length = 0;
    859 		field->buffers[0].string[0] = '\0';
    860 		pos = 0;
    861 		field->start_char = 0;
    862 		field->start_line = 0;
    863 		field->hscroll = 0;
    864 		field->row_count = 0;
    865 		field->cursor_xpos = 0;
    866 		field->cursor_ypos = 0;
    867 	}
    868 
    869 
    870 	if ((field->overlay == 0)
    871 	    || ((field->overlay == 1) && (pos >= field->buffers[0].length))) {
    872 		if (field->buffers[0].length + 1
    873 		    >= field->buffers[0].allocated) {
    874 			new_size = field->buffers[0].allocated + 64
    875 				- (field->buffers[0].allocated % 64);
    876 			if ((new = (char *) realloc(field->buffers[0].string,
    877 						    new_size )) == NULL)
    878 				return E_SYSTEM_ERROR;
    879 			field->buffers[0].allocated = new_size;
    880 			field->buffers[0].string = new;
    881 		}
    882 	}
    883 
    884 	if ((field->overlay == 0) && (field->buffers[0].length > pos)) {
    885 		bcopy(&field->buffers[0].string[pos],
    886 		      &field->buffers[0].string[pos + 1],
    887 		      field->buffers[0].length - pos + 1);
    888 	}
    889 
    890 	field->buffers[0].string[pos] = c;
    891 	if (pos >= field->buffers[0].length) {
    892 		  /* make sure the string is terminated if we are at the
    893 		   * end of the string, the terminator would be missing
    894 		   * if we are are at the end of the field.
    895 		   */
    896 		field->buffers[0].string[pos + 1] = '\0';
    897 	}
    898 
    899 	  /* only increment the length if we are inserting characters
    900 	   * OR if we are at the end of the field in overlay mode.
    901 	   */
    902 	if ((field->overlay == 0)
    903 	    || ((field->overlay == 1) && (pos >= field->buffers[0].length)))
    904 			field->buffers[0].length++;
    905 
    906 	  /* wrap the field, if needed */
    907 	status = _formi_wrap_field(field, pos);
    908 	if (status != E_OK) {
    909 		  /* wrap failed for some reason, back out the char insert */
    910 		bcopy(&field->buffers[0].string[pos + 1],
    911 		      &field->buffers[0].string[pos],
    912 		      field->buffers[0].length - pos);
    913 		field->buffers[0].length--;
    914 	} else {
    915 		field->buf0_status = TRUE;
    916 		field->cursor_xpos++;
    917 		if (field->cursor_xpos >= field->cols - 1) {
    918 			field->start_char++;
    919 			field->cursor_xpos = field->cols - 1;
    920 		}
    921 	}
    922 
    923 #ifdef DEBUG
    924 	fprintf(dbg,
    925 	    "add_char exit: xpos=%d, start=%d, length=%d(%d), allocated=%d\n",
    926 		field->cursor_xpos, field->start_char,
    927 		field->buffers[0].length, strlen(field->buffers[0].string),
    928 		field->buffers[0].allocated);
    929 	fprintf(dbg,"add_char exit: %s\n", field->buffers[0].string);
    930 	fprintf(dbg, "add_char exit: status = %s\n",
    931 		(status == E_OK)? "OK" : "FAILED");
    932 #endif
    933 	return (status == E_OK);
    934 }
    935 
    936 /*
    937  * Manipulate the text in a field, this takes the given form and performs
    938  * the passed driver command on the current text field.  Returns 1 if the
    939  * text field was modified.
    940  */
    941 int
    942 _formi_manipulate_field(FORM *form, int c)
    943 {
    944 	FIELD *cur;
    945 	char *str;
    946 	unsigned int i, start, end, pos;
    947 
    948 	cur = form->fields[form->cur_field];
    949 
    950 #ifdef DEBUG
    951 	fprintf(dbg,
    952 		"entry: xpos=%d, start_char=%d, length=%d, allocated=%d\n",
    953 		cur->cursor_xpos, cur->start_char, cur->buffers[0].length,
    954 		cur->buffers[0].allocated);
    955 	fprintf(dbg, "entry: string=\"%s\"\n", cur->buffers[0].string);
    956 #endif
    957 	switch (c) {
    958 	case REQ_NEXT_CHAR:
    959 		if ((cur->cursor_xpos + cur->start_char
    960 		     - ((cur->start_char > 0)? 1 : 0) + cur->hscroll + 1)
    961 		    > cur->buffers[0].length) {
    962 			return E_REQUEST_DENIED;
    963 		}
    964 		cur->cursor_xpos++;
    965 		if (cur->cursor_xpos >= cur->cols - cur->hscroll - 1) {
    966 			if (cur->cols < (cur->hscroll  + 1))
    967 				cur->cursor_xpos = 0;
    968 			else
    969 				cur->cursor_xpos = cur->cols
    970 					- cur->hscroll - 1;
    971 			cur->start_char++;
    972 		}
    973 		break;
    974 
    975 	case REQ_PREV_CHAR:
    976 		if (cur->cursor_xpos == 0) {
    977 			if (cur->start_char > 0)
    978 				cur->start_char--;
    979 			else if (cur->hscroll > 0)
    980 				cur->hscroll--;
    981 			else
    982 				return E_REQUEST_DENIED;
    983 		} else
    984 			cur->cursor_xpos--;
    985 		break;
    986 
    987 	case REQ_NEXT_LINE:
    988 		cur->cursor_ypos++;
    989 		if (cur->cursor_ypos > cur->rows) {
    990 			if ((cur->opts & O_STATIC) == O_STATIC) {
    991 				if (cur->start_line + cur->cursor_ypos
    992 				    > cur->drows) {
    993 					cur->cursor_ypos--;
    994 					return E_REQUEST_DENIED;
    995 				}
    996 			} else {
    997 				if (cur->start_line + cur->cursor_ypos
    998 				    > cur->nrows + cur->rows) {
    999 					cur->cursor_ypos--;
   1000 					return E_REQUEST_DENIED;
   1001 				}
   1002 			}
   1003 			cur->start_line++;
   1004 		}
   1005 		break;
   1006 
   1007 	case REQ_PREV_LINE:
   1008 		if (cur->cursor_ypos == 0) {
   1009 			if (cur->start_line == 0)
   1010 				return E_REQUEST_DENIED;
   1011 			cur->start_line--;
   1012 		} else
   1013 			cur->cursor_ypos--;
   1014 		break;
   1015 
   1016 	case REQ_NEXT_WORD:
   1017 		start = cur->start_char + cur->cursor_xpos;
   1018 		str = cur->buffers[0].string;
   1019 
   1020 		start = find_eow(str, start);
   1021 
   1022 		  /* check if we hit the end */
   1023 		if (str[start] == '\0')
   1024 			return E_REQUEST_DENIED;
   1025 
   1026 		  /* otherwise we must have found the start of a word...*/
   1027 		if (start - cur->start_char < cur->cols) {
   1028 			cur->cursor_xpos = start;
   1029 		} else {
   1030 			cur->start_char = start;
   1031 			cur->cursor_xpos = 0;
   1032 		}
   1033 		break;
   1034 
   1035 	case REQ_PREV_WORD:
   1036 		start = cur->start_char + cur->cursor_xpos;
   1037 		if (cur->start_char > 0)
   1038 			start--;
   1039 
   1040 		if (start == 0)
   1041 			return E_REQUEST_DENIED;
   1042 
   1043 		str = cur->buffers[0].string;
   1044 
   1045 		start = find_sow(str, start);
   1046 
   1047 		if (start - cur->start_char > 0) {
   1048 			cur->cursor_xpos = start;
   1049 		} else {
   1050 			cur->start_char = start;
   1051 			cur->cursor_xpos = 0;
   1052 		}
   1053 		break;
   1054 
   1055 	case REQ_BEG_FIELD:
   1056 		cur->start_char = 0;
   1057 		cur->start_line = 0;
   1058 		cur->cursor_xpos = 0;
   1059 		cur->cursor_ypos = 0;
   1060 		break;
   1061 
   1062 	case REQ_END_FIELD:
   1063 		if (cur->row_count > cur->rows) {
   1064 			cur->start_line = cur->row_count - cur->rows;
   1065 			cur->cursor_ypos = cur->rows - 1;
   1066 		} else {
   1067 			cur->start_line = 0;
   1068 			cur->cursor_ypos = cur->row_count - 1;
   1069 		}
   1070 
   1071 		if ((str = rindex(cur->buffers[0].string, '\n')) == NULL) {
   1072 			cur->cursor_xpos = cur->cols - 1;
   1073 			if (cur->start_char < (cur->buffers[0].length +
   1074 					       cur->cols)) {
   1075 				cur->start_char = 0;
   1076 				cur->cursor_xpos = cur->buffers[0].length;
   1077 			} else {
   1078 				cur->start_char = cur->buffers[0].length -
   1079 					cur->cols;
   1080 			}
   1081 		} else {
   1082 			cur->start_char = (str - cur->buffers[0].string);
   1083 			if (strlen(str) > cur->cols)
   1084 				cur->cursor_xpos = cur->cols;
   1085 			else
   1086 				cur->cursor_xpos = strlen(str);
   1087 		}
   1088 		break;
   1089 
   1090 	case REQ_BEG_LINE:
   1091 		start = cur->start_char + cur->cursor_xpos;
   1092 		if (cur->buffers[0].string[start] == '\n') {
   1093 			if (start > 0)
   1094 				start--;
   1095 			else
   1096 				return E_REQUEST_DENIED;
   1097 		}
   1098 
   1099 		while ((start > 0)
   1100 		       && (cur->buffers[0].string[start] != '\n'))
   1101 			start--;
   1102 
   1103 		if (start > 0)
   1104 			start++;
   1105 
   1106 		cur->start_char = start;
   1107 		cur->cursor_xpos = 0;
   1108 		break;
   1109 
   1110 	case REQ_END_LINE:
   1111 		start = cur->start_char + cur->cursor_xpos;
   1112 		end = _formi_find_eol(cur->buffers[0].string, start);
   1113 		start = _formi_find_bol(cur->buffers[0].string, start);
   1114 
   1115 		if (end - start > cur->cols - 1) {
   1116 			cur->cursor_xpos = cur->cols - 1;
   1117 			cur->start_char = end - cur->cols + 3;
   1118 		} else {
   1119 			cur->cursor_xpos = end - start + 1;
   1120 			cur->start_char = start;
   1121 		}
   1122 		break;
   1123 
   1124 	case REQ_LEFT_CHAR:
   1125 		if ((cur->cursor_xpos == 0) && (cur->start_char == 0))
   1126 			return E_REQUEST_DENIED;
   1127 
   1128 		if (cur->cursor_xpos == 0) {
   1129 			cur->start_char--;
   1130 			if (cur->buffers[0].string[cur->start_char] == '\n') {
   1131 				if ((cur->cursor_ypos == 0) &&
   1132 				    (cur->start_line == 0))
   1133 				{
   1134 					cur->start_char++;
   1135 					return E_REQUEST_DENIED;
   1136 				}
   1137 
   1138 				if (cur->cursor_ypos == 0)
   1139 					cur->start_line--;
   1140 				else
   1141 					cur->cursor_ypos--;
   1142 
   1143 				end = _formi_find_eol(cur->buffers[0].string,
   1144 					       cur->start_char);
   1145 				start = _formi_find_bol(cur->buffers[0].string,
   1146 						 cur->start_char);
   1147 				if (end - start >= cur->cols) {
   1148 					cur->cursor_xpos = cur->cols - 1;
   1149 					cur->start_char = end - cur->cols;
   1150 				} else {
   1151 					cur->cursor_xpos = end - start;
   1152 					cur->start_char = start;
   1153 				}
   1154 			}
   1155 		} else
   1156 			cur->cursor_xpos--;
   1157 		break;
   1158 
   1159 	case REQ_RIGHT_CHAR:
   1160 		pos = cur->start_char + cur->cursor_xpos;
   1161 		if (cur->buffers[0].string[pos] == '\0')
   1162 			return E_REQUEST_DENIED;
   1163 
   1164 #ifdef DEBUG
   1165 		fprintf(dbg, "req_right_char enter: start=%d, xpos=%d, c=%c\n",
   1166 			cur->start_char, cur->cursor_xpos,
   1167 			cur->buffers[0].string[pos]);
   1168 #endif
   1169 
   1170 		if (cur->buffers[0].string[pos] == '\n') {
   1171 			start = pos + 1;
   1172 			if (cur->buffers[0].string[start] == 0)
   1173 				return E_REQUEST_DENIED;
   1174 			end = _formi_find_eol(cur->buffers[0].string, start);
   1175 			if (end - start > cur->cols) {
   1176 				cur->cursor_xpos = cur->cols - 1;
   1177 				cur->start_char = end - cur->cols - 1;
   1178 			} else {
   1179 				cur->cursor_xpos = end - start;
   1180 				cur->start_char = start;
   1181 			}
   1182 		} else {
   1183 			if (cur->cursor_xpos == cur->cols - 1)
   1184 				cur->start_char++;
   1185 			else
   1186 				cur->cursor_xpos++;
   1187 		}
   1188 #ifdef DEBUG
   1189 		fprintf(dbg, "req_right_char exit: start=%d, xpos=%d, c=%c\n",
   1190 			cur->start_char, cur->cursor_xpos,
   1191 			cur->buffers[0].string[cur->start_char +
   1192 					      cur->cursor_xpos]);
   1193 #endif
   1194 		break;
   1195 
   1196 	case REQ_UP_CHAR:
   1197 		if (cur->cursor_ypos == 0) {
   1198 			if (cur->start_line == 0)
   1199 				return E_REQUEST_DENIED;
   1200 
   1201 			cur->start_line--;
   1202 		} else
   1203 			cur->cursor_ypos--;
   1204 
   1205 		start = find_cur_line(cur);
   1206 		end = _formi_find_eol(cur->buffers[0].string, start);
   1207 		cur->start_char = start;
   1208 		if (cur->cursor_xpos > end - start)
   1209 			cur->cursor_xpos = end - start;
   1210 		break;
   1211 
   1212 	case REQ_DOWN_CHAR:
   1213 		if (cur->cursor_ypos == cur->rows - 1) {
   1214 			if (cur->start_line + cur->rows == cur->row_count)
   1215 				return E_REQUEST_DENIED;
   1216 			cur->start_line++;
   1217 		} else
   1218 			cur->cursor_ypos++;
   1219 
   1220 		start = find_cur_line(cur);
   1221 		end = _formi_find_eol(cur->buffers[0].string, start);
   1222 		cur->start_char = start;
   1223 		if (cur->cursor_xpos > end - start)
   1224 			cur->cursor_xpos = end - start;
   1225 		break;
   1226 
   1227 	case REQ_NEW_LINE:
   1228 		_formi_add_char(cur, cur->start_char + cur->cursor_xpos, '\n');
   1229 		cur->row_count++;
   1230 		break;
   1231 
   1232 	case REQ_INS_CHAR:
   1233 		_formi_add_char(cur, cur->start_char + cur->cursor_xpos,
   1234 				cur->pad);
   1235 		break;
   1236 
   1237 	case REQ_INS_LINE:
   1238 		start = _formi_find_bol(cur->buffers[0].string, cur->start_char);
   1239 		_formi_add_char(cur, start, '\n');
   1240 		cur->row_count++;
   1241 		break;
   1242 
   1243 	case REQ_DEL_CHAR:
   1244 		if (cur->buffers[0].string[cur->start_char + cur->cursor_xpos]
   1245 		    == '\0')
   1246 			return E_REQUEST_DENIED;
   1247 
   1248 		start = cur->start_char + cur->cursor_xpos;
   1249 		end = cur->buffers[0].length;
   1250 		if (cur->buffers[0].string[start] == '\n') {
   1251 			if (cur->row_count > 0) {
   1252 				cur->row_count--;
   1253 				_formi_join_line(cur, cur->buffers[0].string,
   1254 						 start, JOIN_NEXT);
   1255 			} else
   1256 				cur->buffers[0].string[start] = '\0';
   1257 		} else {
   1258 			bcopy(&cur->buffers[0].string[start + 1],
   1259 			      &cur->buffers[0].string[start],
   1260 			      (unsigned) end - start + 1);
   1261 		}
   1262 
   1263 		cur->buffers[0].length--;
   1264 		break;
   1265 
   1266 	case REQ_DEL_PREV:
   1267 		if ((cur->cursor_xpos == 0) && (cur->start_char == 0))
   1268 			   return E_REQUEST_DENIED;
   1269 
   1270 		start = cur->cursor_xpos + cur->start_char;
   1271 		end = cur->buffers[0].length;
   1272 
   1273 		if (cur->buffers[0].string[cur->start_char + cur->cursor_xpos] == '\n') {
   1274 			_formi_join_line(cur, cur->buffers[0].string,
   1275 					 cur->start_char + cur->cursor_xpos,
   1276 					 JOIN_PREV);
   1277 			cur->row_count--;
   1278 		} else {
   1279 			bcopy(&cur->buffers[0].string[start],
   1280 			      &cur->buffers[0].string[start - 1],
   1281 			      (unsigned) end - start + 1);
   1282 		}
   1283 
   1284 		cur->buffers[0].length--;
   1285 		if ((cur->cursor_xpos == 0) && (cur->start_char > 0))
   1286 			cur->start_char--;
   1287 		else if ((cur->cursor_xpos == cur->cols - 1)
   1288 			 && (cur->start_char > 0))
   1289 			cur->start_char--;
   1290 		else if (cur->cursor_xpos > 0)
   1291 			cur->cursor_xpos--;
   1292 
   1293 		break;
   1294 
   1295 	case REQ_DEL_LINE:
   1296 		start = cur->start_char + cur->cursor_xpos;
   1297 		end = _formi_find_eol(cur->buffers[0].string, start);
   1298 		start = _formi_find_bol(cur->buffers[0].string, start);
   1299 		bcopy(&cur->buffers[0].string[end + 1],
   1300 		      &cur->buffers[0].string[start],
   1301 		      (unsigned) cur->buffers[0].length - end + 1);
   1302 		if (cur->row_count > 0)
   1303 			cur->row_count--;
   1304 		break;
   1305 
   1306 	case REQ_DEL_WORD:
   1307 		start = cur->start_char + cur->cursor_xpos;
   1308 		end = find_eow(cur->buffers[0].string, start);
   1309 		start = find_sow(cur->buffers[0].string, start);
   1310 		bcopy(&cur->buffers[0].string[end + 1],
   1311 		      &cur->buffers[0].string[start],
   1312 		      (unsigned) cur->buffers[0].length - end + 1);
   1313 		cur->buffers[0].length -= end - start;
   1314 		break;
   1315 
   1316 	case REQ_CLR_EOL:
   1317 		  /*XXXX this right or should we just toast the chars? */
   1318 		start = cur->start_char + cur->cursor_xpos;
   1319 		end = _formi_find_eol(cur->buffers[0].string, start);
   1320 		for (i = start; i < end; i++)
   1321 			cur->buffers[0].string[i] = cur->pad;
   1322 		break;
   1323 
   1324 	case REQ_CLR_EOF:
   1325 		for (i = cur->start_char + cur->cursor_xpos;
   1326 		     i < cur->buffers[0].length; i++)
   1327 			cur->buffers[0].string[i] = cur->pad;
   1328 		break;
   1329 
   1330 	case REQ_CLR_FIELD:
   1331 		for (i = 0; i < cur->buffers[0].length; i++)
   1332 			cur->buffers[0].string[i] = cur->pad;
   1333 		break;
   1334 
   1335 	case REQ_OVL_MODE:
   1336 		cur->overlay = 1;
   1337 		break;
   1338 
   1339 	case REQ_INS_MODE:
   1340 		cur->overlay = 0;
   1341 		break;
   1342 
   1343 	case REQ_SCR_FLINE:
   1344 		_formi_scroll_fwd(cur, 1);
   1345 		break;
   1346 
   1347 	case REQ_SCR_BLINE:
   1348 		_formi_scroll_back(cur, 1);
   1349 		break;
   1350 
   1351 	case REQ_SCR_FPAGE:
   1352 		_formi_scroll_fwd(cur, cur->rows);
   1353 		break;
   1354 
   1355 	case REQ_SCR_BPAGE:
   1356 		_formi_scroll_back(cur, cur->rows);
   1357 		break;
   1358 
   1359 	case REQ_SCR_FHPAGE:
   1360 		_formi_scroll_fwd(cur, cur->rows / 2);
   1361 		break;
   1362 
   1363 	case REQ_SCR_BHPAGE:
   1364 		_formi_scroll_back(cur, cur->rows / 2);
   1365 		break;
   1366 
   1367 	case REQ_SCR_FCHAR:
   1368 		_formi_hscroll_fwd(cur, 1);
   1369 		break;
   1370 
   1371 	case REQ_SCR_BCHAR:
   1372 		_formi_hscroll_back(cur, 1);
   1373 		break;
   1374 
   1375 	case REQ_SCR_HFLINE:
   1376 		_formi_hscroll_fwd(cur, cur->cols);
   1377 		break;
   1378 
   1379 	case REQ_SCR_HBLINE:
   1380 		_formi_hscroll_back(cur, cur->cols);
   1381 		break;
   1382 
   1383 	case REQ_SCR_HFHALF:
   1384 		_formi_hscroll_fwd(cur, cur->cols / 2);
   1385 		break;
   1386 
   1387 	case REQ_SCR_HBHALF:
   1388 		_formi_hscroll_back(cur, cur->cols / 2);
   1389 		break;
   1390 
   1391 	default:
   1392 		return 0;
   1393 	}
   1394 
   1395 #ifdef DEBUG
   1396 	fprintf(dbg, "exit: xpos=%d, start_char=%d, length=%d, allocated=%d\n",
   1397 		cur->cursor_xpos, cur->start_char, cur->buffers[0].length,
   1398 		cur->buffers[0].allocated);
   1399 	fprintf(dbg, "exit: string=\"%s\"\n", cur->buffers[0].string);
   1400 #endif
   1401 	return 1;
   1402 }
   1403 
   1404 /*
   1405  * Validate the current field.  If the field validation returns success then
   1406  * return E_OK otherwise return E_INVALID_FIELD.
   1407  *
   1408  */
   1409 int
   1410 _formi_validate_field(FORM *form)
   1411 {
   1412 	FIELD *cur;
   1413 	int ret_val;
   1414 
   1415 
   1416 	if ((form == NULL) || (form->fields == NULL) ||
   1417 	    (form->fields[0] == NULL))
   1418 		return E_INVALID_FIELD;
   1419 
   1420 	cur = form->fields[form->cur_field];
   1421 
   1422 	if (((form->opts & O_PASSOK) == O_PASSOK) && (cur->buf0_status = 0))
   1423 		return E_OK;
   1424 
   1425 	if (((form->opts & O_NULLOK) == O_NULLOK) &&
   1426 	    (cur->buffers[0].string[0] == '\0'))
   1427 		return E_OK;
   1428 
   1429 	  /* if there is no type then just accept the field */
   1430 	if (cur->type == NULL)
   1431 		return E_OK;
   1432 
   1433 	ret_val = E_INVALID_FIELD;
   1434 	_formi_do_validation(cur, cur->type, &ret_val);
   1435 
   1436 	return ret_val;
   1437 }
   1438 
   1439 /*
   1440  * Perform the validation of the field, invoke all field_type validation
   1441  * routines.  If the field is ok then update ret_val to E_OK otherwise
   1442  * ret_val is not changed.
   1443  */
   1444 static void
   1445 _formi_do_validation(FIELD *field, FIELDTYPE *type, int *ret_val)
   1446 {
   1447 	if ((type->flags & _TYPE_IS_LINKED) == _TYPE_IS_LINKED) {
   1448 		_formi_do_validation(field, type->link->next, ret_val);
   1449 		_formi_do_validation(field, type->link->prev, ret_val);
   1450 	} else {
   1451 		if (type->field_check == NULL)
   1452 			*ret_val = E_OK;
   1453 		else {
   1454 			if (type->field_check(field, field_buffer(field, 0))
   1455 			    == TRUE)
   1456 				*ret_val = E_OK;
   1457 		}
   1458 	}
   1459 }
   1460 
   1461 /*
   1462  * Select the next/previous choice for the field, the driver command
   1463  * selecting the direction will be passed in c.  Return 1 if a choice
   1464  * selection succeeded, 0 otherwise.
   1465  */
   1466 int
   1467 _formi_field_choice(FORM *form, int c)
   1468 {
   1469 	FIELDTYPE *type;
   1470 	FIELD *field;
   1471 
   1472 	if ((form == NULL) || (form->fields == NULL) ||
   1473 	    (form->fields[0] == NULL) ||
   1474 	    (form->fields[form->cur_field]->type == NULL))
   1475 		return 0;
   1476 
   1477 	field = form->fields[form->cur_field];
   1478 	type = field->type;
   1479 
   1480 	switch (c) {
   1481 	case REQ_NEXT_CHOICE:
   1482 		if (type->next_choice == NULL)
   1483 			return 0;
   1484 		else
   1485 			return type->next_choice(field,
   1486 						 field_buffer(field, 0));
   1487 
   1488 	case REQ_PREV_CHOICE:
   1489 		if (type->prev_choice == NULL)
   1490 			return 0;
   1491 		else
   1492 			return type->prev_choice(field,
   1493 						 field_buffer(field, 0));
   1494 
   1495 	default: /* should never happen! */
   1496 		return 0;
   1497 	}
   1498 }
   1499 
   1500 /*
   1501  * Update the fields if they have changed.  The parameter old has the
   1502  * previous current field as the current field may have been updated by
   1503  * the driver.  Return 1 if the form page needs updating.
   1504  *
   1505  */
   1506 int
   1507 _formi_update_field(FORM *form, int old_field)
   1508 {
   1509 	int cur, i;
   1510 
   1511 	cur = form->cur_field;
   1512 
   1513 	if (old_field != cur) {
   1514 		if (!((cur >= form->page_starts[form->page].first) &&
   1515 		      (cur <= form->page_starts[form->page].last))) {
   1516 			  /* not on same page any more */
   1517 			for (i = 0; i < form->max_page; i++) {
   1518 				if ((form->page_starts[i].in_use == 1) &&
   1519 				    (form->page_starts[i].first <= cur) &&
   1520 				    (form->page_starts[i].last >= cur)) {
   1521 					form->page = i;
   1522 					return 1;
   1523 				}
   1524 			}
   1525 		}
   1526 	}
   1527 
   1528 	_formi_redraw_field(form, old_field);
   1529 	_formi_redraw_field(form, form->cur_field);
   1530 	return 0;
   1531 }
   1532 
   1533 /*
   1534  * Compare function for the field sorting
   1535  *
   1536  */
   1537 static int
   1538 field_sort_compare(const void *one, const void *two)
   1539 {
   1540 	const FIELD *a, *b;
   1541 	int tl;
   1542 
   1543 	  /* LINTED const castaway; we don't modify these! */
   1544 	a = (const FIELD *) *((const FIELD **) one);
   1545 	b = (const FIELD *) *((const FIELD **) two);
   1546 
   1547 	if (a == NULL)
   1548 		return 1;
   1549 
   1550 	if (b == NULL)
   1551 		return -1;
   1552 
   1553 	  /*
   1554 	   * First check the page, we want the fields sorted by page.
   1555 	   *
   1556 	   */
   1557 	if (a->page != b->page)
   1558 		return ((a->page > b->page)? 1 : -1);
   1559 
   1560 	tl = _formi_top_left(a->parent, a->index, b->index);
   1561 
   1562 	  /*
   1563 	   * sort fields left to right, top to bottom so the top left is
   1564 	   * the less than value....
   1565 	   */
   1566 	return ((tl == a->index)? -1 : 1);
   1567 }
   1568 
   1569 /*
   1570  * Sort the fields in a form ready for driver traversal.
   1571  */
   1572 void
   1573 _formi_sort_fields(FORM *form)
   1574 {
   1575 	FIELD **sort_area;
   1576 	int i;
   1577 
   1578 	CIRCLEQ_INIT(&form->sorted_fields);
   1579 
   1580 	if ((sort_area = (FIELD **) malloc(sizeof(FIELD *) * form->field_count))
   1581 	    == NULL)
   1582 		return;
   1583 
   1584 	bcopy(form->fields, sort_area, sizeof(FIELD *) * form->field_count);
   1585 	qsort(sort_area, (unsigned) form->field_count, sizeof(FIELD *),
   1586 	      field_sort_compare);
   1587 
   1588 	for (i = 0; i < form->field_count; i++)
   1589 		CIRCLEQ_INSERT_TAIL(&form->sorted_fields, sort_area[i], glue);
   1590 
   1591 	free(sort_area);
   1592 }
   1593 
   1594 /*
   1595  * Set the neighbours for all the fields in the given form.
   1596  */
   1597 void
   1598 _formi_stitch_fields(FORM *form)
   1599 {
   1600 	int above_row, below_row, end_above, end_below, cur_row, real_end;
   1601 	FIELD *cur, *above, *below;
   1602 
   1603 	  /*
   1604 	   * check if the sorted fields circle queue is empty, just
   1605 	   * return if it is.
   1606 	   */
   1607 	if (CIRCLEQ_EMPTY(&form->sorted_fields))
   1608 		return;
   1609 
   1610 	  /* initially nothing is above..... */
   1611 	above_row = -1;
   1612 	end_above = TRUE;
   1613 	above = NULL;
   1614 
   1615 	  /* set up the first field as the current... */
   1616 	cur = CIRCLEQ_FIRST(&form->sorted_fields);
   1617 	cur_row = cur->form_row;
   1618 
   1619 	  /* find the first field on the next row if any */
   1620 	below = CIRCLEQ_NEXT(cur, glue);
   1621 	below_row = -1;
   1622 	end_below = TRUE;
   1623 	real_end = TRUE;
   1624 	while (below != (void *)&form->sorted_fields) {
   1625 		if (below->form_row != cur_row) {
   1626 			below_row = below->form_row;
   1627 			end_below = FALSE;
   1628 			real_end = FALSE;
   1629 			break;
   1630 		}
   1631 		below = CIRCLEQ_NEXT(below, glue);
   1632 	}
   1633 
   1634 	  /* walk the sorted fields, setting the neighbour pointers */
   1635 	while (cur != (void *) &form->sorted_fields) {
   1636 		if (cur == CIRCLEQ_FIRST(&form->sorted_fields))
   1637 			cur->left = NULL;
   1638 		else
   1639 			cur->left = CIRCLEQ_PREV(cur, glue);
   1640 
   1641 		if (cur == CIRCLEQ_LAST(&form->sorted_fields))
   1642 			cur->right = NULL;
   1643 		else
   1644 			cur->right = CIRCLEQ_NEXT(cur, glue);
   1645 
   1646 		if (end_above == TRUE)
   1647 			cur->up = NULL;
   1648 		else {
   1649 			cur->up = above;
   1650 			above = CIRCLEQ_NEXT(above, glue);
   1651 			if (above_row != above->form_row) {
   1652 				end_above = TRUE;
   1653 				above_row = above->form_row;
   1654 			}
   1655 		}
   1656 
   1657 		if (end_below == TRUE)
   1658 			cur->down = NULL;
   1659 		else {
   1660 			cur->down = below;
   1661 			below = CIRCLEQ_NEXT(below, glue);
   1662 			if (below == (void *) &form->sorted_fields) {
   1663 				end_below = TRUE;
   1664 				real_end = TRUE;
   1665 			} else if (below_row != below->form_row) {
   1666 				end_below = TRUE;
   1667 				below_row = below->form_row;
   1668 			}
   1669 		}
   1670 
   1671 		cur = CIRCLEQ_NEXT(cur, glue);
   1672 		if ((cur != (void *) &form->sorted_fields)
   1673 		    && (cur_row != cur->form_row)) {
   1674 			cur_row = cur->form_row;
   1675 			if (end_above == FALSE) {
   1676 				for (; above != CIRCLEQ_FIRST(&form->sorted_fields);
   1677 				     above = CIRCLEQ_NEXT(above, glue)) {
   1678 					if (above->form_row != above_row) {
   1679 						above_row = above->form_row;
   1680 						break;
   1681 					}
   1682 				}
   1683 			} else if (above == NULL) {
   1684 				above = CIRCLEQ_FIRST(&form->sorted_fields);
   1685 				end_above = FALSE;
   1686 				above_row = above->form_row;
   1687 			} else
   1688 				end_above = FALSE;
   1689 
   1690 			if (end_below == FALSE) {
   1691 				while (below_row == below->form_row) {
   1692 					below = CIRCLEQ_NEXT(below,
   1693 							     glue);
   1694 					if (below ==
   1695 					    (void *)&form->sorted_fields) {
   1696 						real_end = TRUE;
   1697 						end_below = TRUE;
   1698 						break;
   1699 					}
   1700 				}
   1701 
   1702 				if (below != (void *)&form->sorted_fields)
   1703 					below_row = below->form_row;
   1704 			} else if (real_end == FALSE)
   1705 				end_below = FALSE;
   1706 
   1707 		}
   1708 	}
   1709 }
   1710