xref: /minix3/lib/libform/internals.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: internals.c,v 1.37 2013/11/26 01:17:00 christos Exp $	*/
2a0e6850fSThomas Cort 
3a0e6850fSThomas Cort /*-
4a0e6850fSThomas Cort  * Copyright (c) 1998-1999 Brett Lymn
5a0e6850fSThomas Cort  *                         (blymn@baea.com.au, brett_lymn@yahoo.com.au)
6a0e6850fSThomas Cort  * All rights reserved.
7a0e6850fSThomas Cort  *
8a0e6850fSThomas Cort  * This code has been donated to The NetBSD Foundation by the Author.
9a0e6850fSThomas Cort  *
10a0e6850fSThomas Cort  * Redistribution and use in source and binary forms, with or without
11a0e6850fSThomas Cort  * modification, are permitted provided that the following conditions
12a0e6850fSThomas Cort  * are met:
13a0e6850fSThomas Cort  * 1. Redistributions of source code must retain the above copyright
14a0e6850fSThomas Cort  *    notice, this list of conditions and the following disclaimer.
15a0e6850fSThomas Cort  * 2. The name of the author may not be used to endorse or promote products
16a0e6850fSThomas Cort  *    derived from this software without specific prior written permission
17a0e6850fSThomas Cort  *
18a0e6850fSThomas Cort  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19a0e6850fSThomas Cort  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20a0e6850fSThomas Cort  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21a0e6850fSThomas Cort  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22a0e6850fSThomas Cort  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23a0e6850fSThomas Cort  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24a0e6850fSThomas Cort  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25a0e6850fSThomas Cort  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26a0e6850fSThomas Cort  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27a0e6850fSThomas Cort  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28a0e6850fSThomas Cort  *
29a0e6850fSThomas Cort  *
30a0e6850fSThomas Cort  */
31a0e6850fSThomas Cort 
32a0e6850fSThomas Cort #include <sys/cdefs.h>
33*84d9c625SLionel Sambuc __RCSID("$NetBSD: internals.c,v 1.37 2013/11/26 01:17:00 christos Exp $");
34a0e6850fSThomas Cort 
35a0e6850fSThomas Cort #include <limits.h>
36a0e6850fSThomas Cort #include <ctype.h>
37a0e6850fSThomas Cort #include <stdio.h>
38a0e6850fSThomas Cort #include <stdlib.h>
39a0e6850fSThomas Cort #include <strings.h>
40a0e6850fSThomas Cort #include <assert.h>
41a0e6850fSThomas Cort #include "internals.h"
42a0e6850fSThomas Cort #include "form.h"
43a0e6850fSThomas Cort 
44a0e6850fSThomas Cort #ifdef DEBUG
45a0e6850fSThomas Cort /*
46a0e6850fSThomas Cort  *  file handle to write debug info to, this will be initialised when
47a0e6850fSThomas Cort  *  the form is first posted.
48a0e6850fSThomas Cort  */
49a0e6850fSThomas Cort FILE *dbg = NULL;
50a0e6850fSThomas Cort 
51a0e6850fSThomas Cort /*
52a0e6850fSThomas Cort  * map the request numbers to strings for debug
53a0e6850fSThomas Cort  */
54a0e6850fSThomas Cort char *reqs[] = {
55a0e6850fSThomas Cort 	"NEXT_PAGE", "PREV_PAGE", "FIRST_PAGE",	"LAST_PAGE", "NEXT_FIELD",
56a0e6850fSThomas Cort 	"PREV_FIELD", "FIRST_FIELD", "LAST_FIELD", "SNEXT_FIELD",
57a0e6850fSThomas Cort 	"SPREV_FIELD", "SFIRST_FIELD", "SLAST_FIELD", "LEFT_FIELD",
58a0e6850fSThomas Cort 	"RIGHT_FIELD", "UP_FIELD", "DOWN_FIELD", "NEXT_CHAR", "PREV_CHAR",
59a0e6850fSThomas Cort 	"NEXT_LINE", "PREV_LINE", "NEXT_WORD", "PREV_WORD", "BEG_FIELD",
60a0e6850fSThomas Cort 	"END_FIELD", "BEG_LINE", "END_LINE", "LEFT_CHAR", "RIGHT_CHAR",
61a0e6850fSThomas Cort 	"UP_CHAR", "DOWN_CHAR", "NEW_LINE", "INS_CHAR", "INS_LINE",
62a0e6850fSThomas Cort 	"DEL_CHAR", "DEL_PREV", "DEL_LINE", "DEL_WORD", "CLR_EOL",
63a0e6850fSThomas Cort 	"CLR_EOF", "CLR_FIELD", "OVL_MODE", "INS_MODE", "SCR_FLINE",
64a0e6850fSThomas Cort 	"SCR_BLINE", "SCR_FPAGE", "SCR_BPAGE", "SCR_FHPAGE", "SCR_BHPAGE",
65a0e6850fSThomas Cort 	"SCR_FCHAR", "SCR_BCHAR", "SCR_HFLINE", "SCR_HBLINE", "SCR_HFHALF",
66a0e6850fSThomas Cort 	"SCR_HBHALF", "VALIDATION", "PREV_CHOICE", "NEXT_CHOICE" };
67a0e6850fSThomas Cort #endif
68a0e6850fSThomas Cort 
69a0e6850fSThomas Cort /* define our own min function - this is not generic but will do here
70a0e6850fSThomas Cort  * (don't believe me?  think about what value you would get
71a0e6850fSThomas Cort  * from min(x++, y++)
72a0e6850fSThomas Cort  */
73a0e6850fSThomas Cort #define min(a,b) (((a) > (b))? (b) : (a))
74a0e6850fSThomas Cort 
75a0e6850fSThomas Cort /* for the line joining function... */
76a0e6850fSThomas Cort #define JOIN_NEXT    1
77a0e6850fSThomas Cort #define JOIN_NEXT_NW 2 /* next join, don't wrap the joined line */
78a0e6850fSThomas Cort #define JOIN_PREV    3
79a0e6850fSThomas Cort #define JOIN_PREV_NW 4 /* previous join, don't wrap the joined line */
80a0e6850fSThomas Cort 
81a0e6850fSThomas Cort /* for the bump_lines function... */
82a0e6850fSThomas Cort #define _FORMI_USE_CURRENT -1 /* indicates current cursor pos to be used */
83a0e6850fSThomas Cort 
84a0e6850fSThomas Cort /* used in add_char for initial memory allocation for string in row */
85a0e6850fSThomas Cort #define INITIAL_LINE_ALLOC 16
86a0e6850fSThomas Cort 
87a0e6850fSThomas Cort unsigned
88a0e6850fSThomas Cort field_skip_blanks(unsigned int start, _FORMI_FIELD_LINES **rowp);
89a0e6850fSThomas Cort static void
90a0e6850fSThomas Cort _formi_do_char_validation(FIELD *field, FIELDTYPE *type, char c, int *ret_val);
91a0e6850fSThomas Cort static void
92a0e6850fSThomas Cort _formi_do_validation(FIELD *field, FIELDTYPE *type, int *ret_val);
93a0e6850fSThomas Cort static int
94a0e6850fSThomas Cort _formi_join_line(FIELD *field, _FORMI_FIELD_LINES **rowp, int direction);
95a0e6850fSThomas Cort void
96a0e6850fSThomas Cort _formi_hscroll_back(FIELD *field, _FORMI_FIELD_LINES *row, unsigned int amt);
97a0e6850fSThomas Cort void
98a0e6850fSThomas Cort _formi_hscroll_fwd(FIELD *field, _FORMI_FIELD_LINES *row, unsigned int amt);
99a0e6850fSThomas Cort static void
100a0e6850fSThomas Cort _formi_scroll_back(FIELD *field, unsigned int amt);
101a0e6850fSThomas Cort static void
102a0e6850fSThomas Cort _formi_scroll_fwd(FIELD *field, unsigned int amt);
103a0e6850fSThomas Cort static int
104a0e6850fSThomas Cort _formi_set_cursor_xpos(FIELD *field, int no_scroll);
105a0e6850fSThomas Cort static int
106a0e6850fSThomas Cort find_sow(unsigned int offset, _FORMI_FIELD_LINES **rowp);
107a0e6850fSThomas Cort static int
108a0e6850fSThomas Cort split_line(FIELD *field, bool hard_split, unsigned pos,
109a0e6850fSThomas Cort 	   _FORMI_FIELD_LINES **rowp);
110a0e6850fSThomas Cort static bool
111a0e6850fSThomas Cort check_field_size(FIELD *field);
112a0e6850fSThomas Cort static int
113a0e6850fSThomas Cort add_tab(FORM *form, _FORMI_FIELD_LINES *row, unsigned int i, char c);
114a0e6850fSThomas Cort static int
115a0e6850fSThomas Cort tab_size(_FORMI_FIELD_LINES *row, unsigned int i);
116a0e6850fSThomas Cort static unsigned int
117a0e6850fSThomas Cort tab_fit_len(_FORMI_FIELD_LINES *row, unsigned int len);
118a0e6850fSThomas Cort static int
119a0e6850fSThomas Cort tab_fit_window(FIELD *field, unsigned int pos, unsigned int window);
120a0e6850fSThomas Cort static void
121a0e6850fSThomas Cort add_to_free(FIELD *field, _FORMI_FIELD_LINES *line);
122a0e6850fSThomas Cort static void
123a0e6850fSThomas Cort adjust_ypos(FIELD *field, _FORMI_FIELD_LINES *line);
124a0e6850fSThomas Cort static _FORMI_FIELD_LINES *
125a0e6850fSThomas Cort copy_row(_FORMI_FIELD_LINES *row);
126a0e6850fSThomas Cort static void
127a0e6850fSThomas Cort destroy_row_list(_FORMI_FIELD_LINES *start);
128a0e6850fSThomas Cort 
129a0e6850fSThomas Cort /*
130a0e6850fSThomas Cort  * Calculate the cursor y position to make the given row appear on the
131a0e6850fSThomas Cort  * field.  This may be as simple as just changing the ypos (if at all) but
132a0e6850fSThomas Cort  * may encompass resetting the start_line of the field to place the line
133a0e6850fSThomas Cort  * at the bottom of the field.  The field is assumed to be a multi-line one.
134a0e6850fSThomas Cort  */
135a0e6850fSThomas Cort static void
adjust_ypos(FIELD * field,_FORMI_FIELD_LINES * line)136a0e6850fSThomas Cort adjust_ypos(FIELD *field, _FORMI_FIELD_LINES *line)
137a0e6850fSThomas Cort {
138a0e6850fSThomas Cort 	unsigned ypos;
139a0e6850fSThomas Cort 	_FORMI_FIELD_LINES *rs;
140a0e6850fSThomas Cort 
141a0e6850fSThomas Cort 	ypos = 0;
142a0e6850fSThomas Cort 	rs = field->alines;
143a0e6850fSThomas Cort 	while (rs != line) {
144a0e6850fSThomas Cort 		rs = rs->next;
145a0e6850fSThomas Cort 		ypos++;
146a0e6850fSThomas Cort 	}
147a0e6850fSThomas Cort 
148a0e6850fSThomas Cort 	field->cursor_ypos = ypos;
149a0e6850fSThomas Cort 	field->start_line = field->alines;
150a0e6850fSThomas Cort 	if (ypos > (field->rows - 1)) {
151a0e6850fSThomas Cort 		  /*
152a0e6850fSThomas Cort 		   * cur_line off the end of the field,
153a0e6850fSThomas Cort 		   * adjust start_line so fix this.
154a0e6850fSThomas Cort 		   */
155a0e6850fSThomas Cort 		field->cursor_ypos = field->rows - 1;
156a0e6850fSThomas Cort 		ypos = ypos - (field->rows - 1);
157a0e6850fSThomas Cort 		while (ypos > 0) {
158a0e6850fSThomas Cort 			ypos--;
159a0e6850fSThomas Cort 			field->start_line = field->start_line->next;
160a0e6850fSThomas Cort 		}
161a0e6850fSThomas Cort 	}
162a0e6850fSThomas Cort }
163a0e6850fSThomas Cort 
164a0e6850fSThomas Cort 
165a0e6850fSThomas Cort /*
166a0e6850fSThomas Cort  * Delete the given row and add it to the free list of the given field.
167a0e6850fSThomas Cort  */
168a0e6850fSThomas Cort static void
add_to_free(FIELD * field,_FORMI_FIELD_LINES * line)169a0e6850fSThomas Cort add_to_free(FIELD *field, _FORMI_FIELD_LINES *line)
170a0e6850fSThomas Cort {
171a0e6850fSThomas Cort 	_FORMI_FIELD_LINES *saved;
172a0e6850fSThomas Cort 
173a0e6850fSThomas Cort 	saved = line;
174a0e6850fSThomas Cort 
175a0e6850fSThomas Cort 	  /* don't remove if only one line... */
176a0e6850fSThomas Cort 	if ((line->prev == NULL) && (line->next == NULL))
177a0e6850fSThomas Cort 		return;
178a0e6850fSThomas Cort 
179a0e6850fSThomas Cort 	if (line->prev == NULL) {
180a0e6850fSThomas Cort 		/* handle top of list */
181a0e6850fSThomas Cort 		field->alines = line->next;
182a0e6850fSThomas Cort 		field->alines->prev = NULL;
183a0e6850fSThomas Cort 
184a0e6850fSThomas Cort 		if (field->cur_line == saved)
185a0e6850fSThomas Cort 			field->cur_line = field->alines;
186a0e6850fSThomas Cort 		if (field->start_line == saved)
187a0e6850fSThomas Cort 			field->start_line = saved;
188a0e6850fSThomas Cort 	} else if (line->next == NULL) {
189a0e6850fSThomas Cort 		/* handle bottom of list */
190a0e6850fSThomas Cort 		line->prev->next = NULL;
191a0e6850fSThomas Cort 		if (field->cur_line == saved)
192a0e6850fSThomas Cort 			field->cur_line = saved->prev;
193a0e6850fSThomas Cort 		if (field->start_line == saved)
194a0e6850fSThomas Cort 			field->cur_line = saved->prev;
195a0e6850fSThomas Cort 	} else {
196a0e6850fSThomas Cort 		saved->next->prev = saved->prev;
197a0e6850fSThomas Cort 		saved->prev->next = saved->next;
198a0e6850fSThomas Cort 		if (field->cur_line == saved)
199a0e6850fSThomas Cort 			field->cur_line = saved->prev;
200a0e6850fSThomas Cort 		if (field->start_line == saved)
201a0e6850fSThomas Cort 			field->start_line = saved;
202a0e6850fSThomas Cort 	}
203a0e6850fSThomas Cort 
204a0e6850fSThomas Cort 	saved->next = field->free;
205a0e6850fSThomas Cort 	field->free = saved;
206a0e6850fSThomas Cort 	saved->prev = NULL;
207a0e6850fSThomas Cort 	if (saved->next != NULL)
208a0e6850fSThomas Cort 		saved->next->prev = line;
209a0e6850fSThomas Cort }
210a0e6850fSThomas Cort 
211a0e6850fSThomas Cort /*
212a0e6850fSThomas Cort  * Duplicate the given row, return the pointer to the new copy or
213a0e6850fSThomas Cort  * NULL if the copy fails.
214a0e6850fSThomas Cort  */
215a0e6850fSThomas Cort static _FORMI_FIELD_LINES *
copy_row(_FORMI_FIELD_LINES * row)216a0e6850fSThomas Cort copy_row(_FORMI_FIELD_LINES *row)
217a0e6850fSThomas Cort {
218a0e6850fSThomas Cort 	_FORMI_FIELD_LINES *new;
219a0e6850fSThomas Cort 	_formi_tab_t *tp, *newt;
220a0e6850fSThomas Cort 
221a0e6850fSThomas Cort 	if ((new = (_FORMI_FIELD_LINES *) malloc(sizeof(_FORMI_FIELD_LINES)))
222a0e6850fSThomas Cort 	    == NULL) {
223a0e6850fSThomas Cort 		return NULL;
224a0e6850fSThomas Cort 	}
225a0e6850fSThomas Cort 
226a0e6850fSThomas Cort 	memcpy(new, row, sizeof(_FORMI_FIELD_LINES));
227a0e6850fSThomas Cort 
228a0e6850fSThomas Cort 	  /* nuke the pointers from the source row so we don't get confused */
229a0e6850fSThomas Cort 	new->next = NULL;
230a0e6850fSThomas Cort 	new->prev = NULL;
231a0e6850fSThomas Cort 	new->tabs = NULL;
232a0e6850fSThomas Cort 
233a0e6850fSThomas Cort 	if ((new->string = (char *) malloc((size_t)new->allocated)) == NULL) {
234a0e6850fSThomas Cort 		free(new);
235a0e6850fSThomas Cort 		return NULL;
236a0e6850fSThomas Cort 	}
237a0e6850fSThomas Cort 
238a0e6850fSThomas Cort 	memcpy(new->string, row->string, (size_t) row->length + 1);
239a0e6850fSThomas Cort 
240a0e6850fSThomas Cort 	if (row->tabs != NULL) {
241a0e6850fSThomas Cort 		tp = row->tabs;
242a0e6850fSThomas Cort 		if ((new->tabs = (_formi_tab_t *) malloc(sizeof(_formi_tab_t)))
243a0e6850fSThomas Cort 		    == NULL) {
244a0e6850fSThomas Cort 			free(new->string);
245a0e6850fSThomas Cort 			free(new);
246a0e6850fSThomas Cort 			return NULL;
247a0e6850fSThomas Cort 		}
248a0e6850fSThomas Cort 
249a0e6850fSThomas Cort 		memcpy(new->tabs, row->tabs, sizeof(_formi_tab_t));
250a0e6850fSThomas Cort 		new->tabs->back = NULL;
251a0e6850fSThomas Cort 		new->tabs->fwd = NULL;
252a0e6850fSThomas Cort 
253a0e6850fSThomas Cort 		tp = tp->fwd;
254a0e6850fSThomas Cort 		newt = new->tabs;
255a0e6850fSThomas Cort 
256a0e6850fSThomas Cort 		while (tp != NULL) {
257a0e6850fSThomas Cort 			if ((newt->fwd =
258a0e6850fSThomas Cort 			     (_formi_tab_t *) malloc(sizeof(_formi_tab_t)))
259a0e6850fSThomas Cort 			    == NULL) {
260a0e6850fSThomas Cort 				/* error... unwind allocations */
261a0e6850fSThomas Cort 				tp = new->tabs;
262a0e6850fSThomas Cort 				while (tp != NULL) {
263a0e6850fSThomas Cort 					newt = tp->fwd;
264a0e6850fSThomas Cort 					free(tp);
265a0e6850fSThomas Cort 					tp = newt;
266a0e6850fSThomas Cort 				}
267a0e6850fSThomas Cort 
268a0e6850fSThomas Cort 				free(new->string);
269a0e6850fSThomas Cort 				free(new);
270a0e6850fSThomas Cort 				return NULL;
271a0e6850fSThomas Cort 			}
272a0e6850fSThomas Cort 
273a0e6850fSThomas Cort 			memcpy(newt->fwd, tp, sizeof(_formi_tab_t));
274a0e6850fSThomas Cort 			newt->fwd->back = newt;
275a0e6850fSThomas Cort 			newt = newt->fwd;
276a0e6850fSThomas Cort 			newt->fwd = NULL;
277a0e6850fSThomas Cort 			tp = tp->fwd;
278a0e6850fSThomas Cort 		}
279a0e6850fSThomas Cort 	}
280a0e6850fSThomas Cort 
281a0e6850fSThomas Cort 	return new;
282a0e6850fSThomas Cort }
283a0e6850fSThomas Cort 
284a0e6850fSThomas Cort /*
285a0e6850fSThomas Cort  * Initialise the row offset for a field, depending on the type of
286a0e6850fSThomas Cort  * field it is and the type of justification used.  The justification
287a0e6850fSThomas Cort  * is only used on static single line fields, everything else will
288a0e6850fSThomas Cort  * have the cursor_xpos set to 0.
289a0e6850fSThomas Cort  */
290a0e6850fSThomas Cort void
_formi_init_field_xpos(FIELD * field)291a0e6850fSThomas Cort _formi_init_field_xpos(FIELD *field)
292a0e6850fSThomas Cort {
293a0e6850fSThomas Cort 	  /* not static or is multi-line which are not justified, so 0 it is */
294a0e6850fSThomas Cort 	if (((field->opts & O_STATIC) != O_STATIC) ||
295a0e6850fSThomas Cort 	    ((field->rows + field->nrows) != 1)) {
296a0e6850fSThomas Cort 		field->cursor_xpos = 0;
297a0e6850fSThomas Cort 		return;
298a0e6850fSThomas Cort 	}
299a0e6850fSThomas Cort 
300a0e6850fSThomas Cort 	switch (field->justification) {
301a0e6850fSThomas Cort 	case JUSTIFY_RIGHT:
302a0e6850fSThomas Cort 		field->cursor_xpos = field->cols - 1;
303a0e6850fSThomas Cort 		break;
304a0e6850fSThomas Cort 
305a0e6850fSThomas Cort 	case JUSTIFY_CENTER:
306a0e6850fSThomas Cort 		field->cursor_xpos = (field->cols - 1) / 2;
307a0e6850fSThomas Cort 		break;
308a0e6850fSThomas Cort 
309a0e6850fSThomas Cort 	default: /* assume left justify */
310a0e6850fSThomas Cort 		field->cursor_xpos = 0;
311a0e6850fSThomas Cort 		break;
312a0e6850fSThomas Cort 	}
313a0e6850fSThomas Cort }
314a0e6850fSThomas Cort 
315a0e6850fSThomas Cort 
316a0e6850fSThomas Cort /*
317a0e6850fSThomas Cort  * Open the debug file if it is not already open....
318a0e6850fSThomas Cort  */
319a0e6850fSThomas Cort #ifdef DEBUG
320a0e6850fSThomas Cort int
_formi_create_dbg_file(void)321a0e6850fSThomas Cort _formi_create_dbg_file(void)
322a0e6850fSThomas Cort {
323a0e6850fSThomas Cort 	if (dbg == NULL) {
324a0e6850fSThomas Cort 		dbg = fopen("___form_dbg.out", "w");
325a0e6850fSThomas Cort 		if (dbg == NULL) {
326a0e6850fSThomas Cort 			fprintf(stderr, "Cannot open debug file!\n");
327a0e6850fSThomas Cort 			return E_SYSTEM_ERROR;
328a0e6850fSThomas Cort 		}
329a0e6850fSThomas Cort 	}
330a0e6850fSThomas Cort 
331a0e6850fSThomas Cort 	return E_OK;
332a0e6850fSThomas Cort }
333a0e6850fSThomas Cort #endif
334a0e6850fSThomas Cort 
335a0e6850fSThomas Cort /*
336a0e6850fSThomas Cort  * Check the sizing of the field, if the maximum size is set for a
337a0e6850fSThomas Cort  * dynamic field then check that the number of rows or columns does
338a0e6850fSThomas Cort  * not exceed the set maximum.  The decision to check the rows or
339a0e6850fSThomas Cort  * columns is made on the basis of how many rows are in the field -
340a0e6850fSThomas Cort  * one row means the max applies to the number of columns otherwise it
341a0e6850fSThomas Cort  * applies to the number of rows.  If the row/column count is less
342a0e6850fSThomas Cort  * than the maximum then return TRUE.
343a0e6850fSThomas Cort  *
344a0e6850fSThomas Cort  */
345a0e6850fSThomas Cort static bool
check_field_size(FIELD * field)346a0e6850fSThomas Cort check_field_size(FIELD *field)
347a0e6850fSThomas Cort {
348a0e6850fSThomas Cort 	if ((field->opts & O_STATIC) != O_STATIC) {
349a0e6850fSThomas Cort 		  /* dynamic field */
350a0e6850fSThomas Cort 		if (field->max == 0) /* unlimited */
351a0e6850fSThomas Cort 			return TRUE;
352a0e6850fSThomas Cort 
353a0e6850fSThomas Cort 		if (field->rows == 1) {
354a0e6850fSThomas Cort 			return (field->alines->length < field->max);
355a0e6850fSThomas Cort 		} else {
356a0e6850fSThomas Cort 			return (field->row_count <= field->max);
357a0e6850fSThomas Cort 		}
358a0e6850fSThomas Cort 	} else {
359a0e6850fSThomas Cort 		if ((field->rows + field->nrows) == 1) {
360a0e6850fSThomas Cort 			return (field->alines->length <= field->cols);
361a0e6850fSThomas Cort 		} else {
362a0e6850fSThomas Cort 			return (field->row_count <= (field->rows
363a0e6850fSThomas Cort 						     + field->nrows));
364a0e6850fSThomas Cort 		}
365a0e6850fSThomas Cort 	}
366a0e6850fSThomas Cort }
367a0e6850fSThomas Cort 
368a0e6850fSThomas Cort /*
369a0e6850fSThomas Cort  * Set the form's current field to the first valid field on the page.
370a0e6850fSThomas Cort  * Assume the fields have been sorted and stitched.
371a0e6850fSThomas Cort  */
372a0e6850fSThomas Cort int
_formi_pos_first_field(FORM * form)373a0e6850fSThomas Cort _formi_pos_first_field(FORM *form)
374a0e6850fSThomas Cort {
375a0e6850fSThomas Cort 	FIELD *cur;
376a0e6850fSThomas Cort 	int old_page;
377a0e6850fSThomas Cort 
378a0e6850fSThomas Cort 	old_page = form->page;
379a0e6850fSThomas Cort 
380a0e6850fSThomas Cort 	  /* scan forward for an active page....*/
381a0e6850fSThomas Cort 	while (form->page_starts[form->page].in_use == 0) {
382a0e6850fSThomas Cort 		form->page++;
383a0e6850fSThomas Cort 		if (form->page > form->max_page) {
384a0e6850fSThomas Cort 			form->page = old_page;
385a0e6850fSThomas Cort 			return E_REQUEST_DENIED;
386a0e6850fSThomas Cort 		}
387a0e6850fSThomas Cort 	}
388a0e6850fSThomas Cort 
389a0e6850fSThomas Cort 	  /* then scan for a field we can use */
390a0e6850fSThomas Cort 	cur = form->fields[form->page_starts[form->page].first];
391a0e6850fSThomas Cort 	while ((cur->opts & (O_VISIBLE | O_ACTIVE))
392a0e6850fSThomas Cort 	       != (O_VISIBLE | O_ACTIVE)) {
393*84d9c625SLionel Sambuc 		cur = TAILQ_NEXT(cur, glue);
394*84d9c625SLionel Sambuc 		if (cur == NULL) {
395a0e6850fSThomas Cort 			form->page = old_page;
396a0e6850fSThomas Cort 			return E_REQUEST_DENIED;
397a0e6850fSThomas Cort 		}
398a0e6850fSThomas Cort 	}
399a0e6850fSThomas Cort 
400a0e6850fSThomas Cort 	form->cur_field = cur->index;
401a0e6850fSThomas Cort 	return E_OK;
402a0e6850fSThomas Cort }
403a0e6850fSThomas Cort 
404a0e6850fSThomas Cort /*
405a0e6850fSThomas Cort  * Set the field to the next active and visible field, the fields are
406a0e6850fSThomas Cort  * traversed in index order in the direction given.  If the parameter
407a0e6850fSThomas Cort  * use_sorted is TRUE then the sorted field list will be traversed instead
408a0e6850fSThomas Cort  * of using the field index.
409a0e6850fSThomas Cort  */
410a0e6850fSThomas Cort int
_formi_pos_new_field(FORM * form,unsigned direction,unsigned use_sorted)411a0e6850fSThomas Cort _formi_pos_new_field(FORM *form, unsigned direction, unsigned use_sorted)
412a0e6850fSThomas Cort {
413a0e6850fSThomas Cort 	FIELD *cur;
414a0e6850fSThomas Cort 	int i;
415a0e6850fSThomas Cort 
416a0e6850fSThomas Cort 	i = form->cur_field;
417a0e6850fSThomas Cort 	cur = form->fields[i];
418a0e6850fSThomas Cort 
419a0e6850fSThomas Cort 	do {
420a0e6850fSThomas Cort 		if (direction == _FORMI_FORWARD) {
421a0e6850fSThomas Cort 			if (use_sorted == TRUE) {
422a0e6850fSThomas Cort 				if ((form->wrap == FALSE) &&
423*84d9c625SLionel Sambuc 				    (cur == TAILQ_LAST(&form->sorted_fields,
424*84d9c625SLionel Sambuc 					_formi_sort_head)))
425a0e6850fSThomas Cort 					return E_REQUEST_DENIED;
426*84d9c625SLionel Sambuc 				cur = TAILQ_NEXT(cur, glue);
427a0e6850fSThomas Cort 				i = cur->index;
428a0e6850fSThomas Cort 			} else {
429a0e6850fSThomas Cort 				if ((form->wrap == FALSE) &&
430a0e6850fSThomas Cort 				    ((i + 1) >= form->field_count))
431a0e6850fSThomas Cort 					return E_REQUEST_DENIED;
432a0e6850fSThomas Cort 				i++;
433a0e6850fSThomas Cort 				if (i >= form->field_count)
434a0e6850fSThomas Cort 					i = 0;
435a0e6850fSThomas Cort 			}
436a0e6850fSThomas Cort 		} else {
437a0e6850fSThomas Cort 			if (use_sorted == TRUE) {
438a0e6850fSThomas Cort 				if ((form->wrap == FALSE) &&
439*84d9c625SLionel Sambuc 				    (cur == TAILQ_FIRST(&form->sorted_fields)))
440a0e6850fSThomas Cort 					return E_REQUEST_DENIED;
441*84d9c625SLionel Sambuc 				cur = TAILQ_PREV(cur, _formi_sort_head, glue);
442a0e6850fSThomas Cort 				i = cur->index;
443a0e6850fSThomas Cort 			} else {
444a0e6850fSThomas Cort 				if ((form->wrap == FALSE) && (i <= 0))
445a0e6850fSThomas Cort 					return E_REQUEST_DENIED;
446a0e6850fSThomas Cort 				i--;
447a0e6850fSThomas Cort 				if (i < 0)
448a0e6850fSThomas Cort 					i = form->field_count - 1;
449a0e6850fSThomas Cort 			}
450a0e6850fSThomas Cort 		}
451a0e6850fSThomas Cort 
452a0e6850fSThomas Cort 		if ((form->fields[i]->opts & (O_VISIBLE | O_ACTIVE))
453a0e6850fSThomas Cort 			== (O_VISIBLE | O_ACTIVE)) {
454a0e6850fSThomas Cort 			form->cur_field = i;
455a0e6850fSThomas Cort 			return E_OK;
456a0e6850fSThomas Cort 		}
457a0e6850fSThomas Cort 	}
458a0e6850fSThomas Cort 	while (i != form->cur_field);
459a0e6850fSThomas Cort 
460a0e6850fSThomas Cort 	return E_REQUEST_DENIED;
461a0e6850fSThomas Cort }
462a0e6850fSThomas Cort 
463a0e6850fSThomas Cort /*
464a0e6850fSThomas Cort  * Destroy the list of line structs passed by freeing all allocated
465a0e6850fSThomas Cort  * memory.
466a0e6850fSThomas Cort  */
467a0e6850fSThomas Cort static void
destroy_row_list(_FORMI_FIELD_LINES * start)468a0e6850fSThomas Cort destroy_row_list(_FORMI_FIELD_LINES *start)
469a0e6850fSThomas Cort {
470a0e6850fSThomas Cort 	_FORMI_FIELD_LINES *temp, *row;
471a0e6850fSThomas Cort 	_formi_tab_t *tt, *tp;
472a0e6850fSThomas Cort 
473a0e6850fSThomas Cort 	row = start;
474a0e6850fSThomas Cort 	while (row != NULL) {
475a0e6850fSThomas Cort 		if (row->tabs != NULL) {
476a0e6850fSThomas Cort 			  /* free up the tab linked list... */
477a0e6850fSThomas Cort 			tp = row->tabs;
478a0e6850fSThomas Cort 			while (tp != NULL) {
479a0e6850fSThomas Cort 				tt = tp->fwd;
480a0e6850fSThomas Cort 				free(tp);
481a0e6850fSThomas Cort 				tp = tt;
482a0e6850fSThomas Cort 			}
483a0e6850fSThomas Cort 		}
484a0e6850fSThomas Cort 
485a0e6850fSThomas Cort 		if (row->string != NULL)
486a0e6850fSThomas Cort 			free(row->string);
487a0e6850fSThomas Cort 
488a0e6850fSThomas Cort 		temp = row->next;
489a0e6850fSThomas Cort 		free(row);
490a0e6850fSThomas Cort 		row = temp;
491a0e6850fSThomas Cort 	}
492a0e6850fSThomas Cort }
493a0e6850fSThomas Cort 
494a0e6850fSThomas Cort /*
495a0e6850fSThomas Cort  * Word wrap the contents of the field's buffer 0 if this is allowed.
496a0e6850fSThomas Cort  * If the wrap is successful, that is, the row count nor the buffer
497a0e6850fSThomas Cort  * size is exceeded then the function will return E_OK, otherwise it
498a0e6850fSThomas Cort  * will return E_REQUEST_DENIED.
499a0e6850fSThomas Cort  */
500a0e6850fSThomas Cort int
_formi_wrap_field(FIELD * field,_FORMI_FIELD_LINES * loc)501a0e6850fSThomas Cort _formi_wrap_field(FIELD *field, _FORMI_FIELD_LINES *loc)
502a0e6850fSThomas Cort {
503a0e6850fSThomas Cort 	int width, wrap_err;
504a0e6850fSThomas Cort 	unsigned int pos, saved_xpos, saved_ypos, saved_cur_xpos;
505a0e6850fSThomas Cort 	unsigned int saved_row_count;
506a0e6850fSThomas Cort 	_FORMI_FIELD_LINES *saved_row, *row, *row_backup, *saved_cur_line;
507a0e6850fSThomas Cort 	_FORMI_FIELD_LINES *saved_start_line, *temp;
508a0e6850fSThomas Cort 
509a0e6850fSThomas Cort 	if ((field->opts & O_STATIC) == O_STATIC) {
510a0e6850fSThomas Cort 		if ((field->rows + field->nrows) == 1) {
511a0e6850fSThomas Cort 			return E_OK; /* cannot wrap a single line */
512a0e6850fSThomas Cort 		}
513a0e6850fSThomas Cort 		width = field->cols;
514a0e6850fSThomas Cort 	} else {
515a0e6850fSThomas Cort 		  /* if we are limited to one line then don't try to wrap */
516a0e6850fSThomas Cort 		if ((field->drows + field->nrows) == 1) {
517a0e6850fSThomas Cort 			return E_OK;
518a0e6850fSThomas Cort 		}
519a0e6850fSThomas Cort 
520a0e6850fSThomas Cort 		  /*
521a0e6850fSThomas Cort 		   * hueristic - if a dynamic field has more than one line
522a0e6850fSThomas Cort 		   * on the screen then the field grows rows, otherwise
523a0e6850fSThomas Cort 		   * it grows columns, effectively a single line field.
524a0e6850fSThomas Cort 		   * This is documented AT&T behaviour.
525a0e6850fSThomas Cort 		   */
526a0e6850fSThomas Cort 		if (field->rows > 1) {
527a0e6850fSThomas Cort 			width = field->cols;
528a0e6850fSThomas Cort 		} else {
529a0e6850fSThomas Cort 			return E_OK;
530a0e6850fSThomas Cort 		}
531a0e6850fSThomas Cort 	}
532a0e6850fSThomas Cort 
533a0e6850fSThomas Cort 	row = loc;
534a0e6850fSThomas Cort 
535a0e6850fSThomas Cort 	  /* if we are not at the top of the field then back up one
536a0e6850fSThomas Cort 	   * row because we may be able to merge the current row into
537a0e6850fSThomas Cort 	   * the one above.
538a0e6850fSThomas Cort 	   */
539a0e6850fSThomas Cort 	if (row->prev != NULL)
540a0e6850fSThomas Cort 		row = row->prev;
541a0e6850fSThomas Cort 
542a0e6850fSThomas Cort 	saved_row = row;
543a0e6850fSThomas Cort 	saved_xpos = field->row_xpos;
544a0e6850fSThomas Cort 	saved_cur_xpos = field->cursor_xpos;
545a0e6850fSThomas Cort 	saved_ypos = field->cursor_ypos;
546a0e6850fSThomas Cort 	saved_row_count = field->row_count;
547a0e6850fSThomas Cort 
548a0e6850fSThomas Cort 	  /*
549a0e6850fSThomas Cort 	   * Save a copy of the lines affected, just in case things
550a0e6850fSThomas Cort 	   * don't work out.
551a0e6850fSThomas Cort 	   */
552a0e6850fSThomas Cort 	if ((row_backup = copy_row(row)) == NULL)
553a0e6850fSThomas Cort 		return E_SYSTEM_ERROR;
554a0e6850fSThomas Cort 
555a0e6850fSThomas Cort 	temp = row_backup;
556a0e6850fSThomas Cort 	row = row->next;
557a0e6850fSThomas Cort 
558a0e6850fSThomas Cort 	saved_cur_line = temp;
559a0e6850fSThomas Cort 	saved_start_line = temp;
560a0e6850fSThomas Cort 
561a0e6850fSThomas Cort 	while (row != NULL) {
562a0e6850fSThomas Cort 		if ((temp->next = copy_row(row)) == NULL) {
563a0e6850fSThomas Cort 			  /* a row copy failed... free up allocations */
564a0e6850fSThomas Cort 			destroy_row_list(row_backup);
565a0e6850fSThomas Cort 			return E_SYSTEM_ERROR;
566a0e6850fSThomas Cort 		}
567a0e6850fSThomas Cort 
568a0e6850fSThomas Cort 		temp->next->prev = temp;
569a0e6850fSThomas Cort 		temp = temp->next;
570a0e6850fSThomas Cort 
571a0e6850fSThomas Cort 		if (row == field->start_line)
572a0e6850fSThomas Cort 			saved_start_line = temp;
573a0e6850fSThomas Cort 		if (row == field->cur_line)
574a0e6850fSThomas Cort 			saved_cur_line = temp;
575a0e6850fSThomas Cort 
576a0e6850fSThomas Cort 		row = row->next;
577a0e6850fSThomas Cort 	}
578a0e6850fSThomas Cort 
579a0e6850fSThomas Cort 	row = saved_row;
580a0e6850fSThomas Cort 	while (row != NULL) {
581a0e6850fSThomas Cort 		pos = row->length - 1;
582a0e6850fSThomas Cort 		if (row->expanded < width) {
583a0e6850fSThomas Cort 			  /* line may be too short, try joining some lines */
584a0e6850fSThomas Cort 			if ((row->hard_ret == TRUE) && (row->next != NULL)) {
585a0e6850fSThomas Cort 				/*
586a0e6850fSThomas Cort 				 * Skip the line if it has a hard return
587a0e6850fSThomas Cort 				 * and it is not the last, we cannot join
588a0e6850fSThomas Cort 				 * anything to it.
589a0e6850fSThomas Cort 				 */
590a0e6850fSThomas Cort 				row = row->next;
591a0e6850fSThomas Cort 				continue;
592a0e6850fSThomas Cort 			}
593a0e6850fSThomas Cort 
594a0e6850fSThomas Cort 			if (row->next == NULL) {
595a0e6850fSThomas Cort 				/*
596a0e6850fSThomas Cort 				 * If there are no more lines and this line
597a0e6850fSThomas Cort 				 * is too short then our job is over.
598a0e6850fSThomas Cort 				 */
599a0e6850fSThomas Cort 				break;
600a0e6850fSThomas Cort 			}
601a0e6850fSThomas Cort 
602a0e6850fSThomas Cort 			if (_formi_join_line(field, &row,
603a0e6850fSThomas Cort 					     JOIN_NEXT_NW) == E_OK) {
604a0e6850fSThomas Cort 				continue;
605a0e6850fSThomas Cort 			} else
606a0e6850fSThomas Cort 				break;
607a0e6850fSThomas Cort 		} else if (row->expanded > width) {
608a0e6850fSThomas Cort 			  /* line is too long, split it */
609a0e6850fSThomas Cort 
610a0e6850fSThomas Cort 			  /*
611a0e6850fSThomas Cort 			   * split on first whitespace before current word
612a0e6850fSThomas Cort 			   * if the line has tabs we need to work out where
613a0e6850fSThomas Cort 			   * the field border lies when the tabs are expanded.
614a0e6850fSThomas Cort 			   */
615a0e6850fSThomas Cort 			if (row->tabs == NULL) {
616a0e6850fSThomas Cort 				pos = width - 1;
617a0e6850fSThomas Cort 				if (pos >= row->expanded)
618a0e6850fSThomas Cort 					pos = row->expanded - 1;
619a0e6850fSThomas Cort 			} else {
620a0e6850fSThomas Cort 				pos = tab_fit_len(row, field->cols);
621a0e6850fSThomas Cort 			}
622a0e6850fSThomas Cort 
623a0e6850fSThomas Cort 			if ((!isblank((unsigned char)row->string[pos])) &&
624a0e6850fSThomas Cort 			    ((field->opts & O_WRAP) == O_WRAP)) {
625a0e6850fSThomas Cort 				if (!isblank((unsigned char)row->string[pos - 1]))
626a0e6850fSThomas Cort 					pos = find_sow((unsigned int) pos,
627a0e6850fSThomas Cort 						       &row);
628a0e6850fSThomas Cort 				/*
629a0e6850fSThomas Cort 				 * If we cannot split the line then return
630a0e6850fSThomas Cort 				 * NO_ROOM so the driver can tell that it
631a0e6850fSThomas Cort 				 * should not autoskip (if that is enabled)
632a0e6850fSThomas Cort 				 */
633a0e6850fSThomas Cort 				if ((pos == 0)
634a0e6850fSThomas Cort 				    || (!isblank((unsigned char)row->string[pos - 1]))) {
635a0e6850fSThomas Cort 					wrap_err = E_NO_ROOM;
636a0e6850fSThomas Cort 					goto restore_and_exit;
637a0e6850fSThomas Cort 				}
638a0e6850fSThomas Cort 			}
639a0e6850fSThomas Cort 
640a0e6850fSThomas Cort 			  /* if we are at the end of the string and it has
641a0e6850fSThomas Cort 			   * a trailing blank, don't wrap the blank.
642a0e6850fSThomas Cort 			   */
643a0e6850fSThomas Cort 			if ((row->next == NULL) && (pos == row->length - 1) &&
644a0e6850fSThomas Cort 			    (isblank((unsigned char)row->string[pos])) &&
645a0e6850fSThomas Cort 			    row->expanded <= field->cols)
646a0e6850fSThomas Cort 				continue;
647a0e6850fSThomas Cort 
648a0e6850fSThomas Cort 			  /*
649a0e6850fSThomas Cort 			   * otherwise, if we are still sitting on a
650a0e6850fSThomas Cort 			   * blank but not at the end of the line
651a0e6850fSThomas Cort 			   * move forward one char so the blank
652a0e6850fSThomas Cort 			   * is on the line boundary.
653a0e6850fSThomas Cort 			   */
654a0e6850fSThomas Cort 			if ((isblank((unsigned char)row->string[pos])) &&
655a0e6850fSThomas Cort 			    (pos != row->length - 1))
656a0e6850fSThomas Cort 				pos++;
657a0e6850fSThomas Cort 
658a0e6850fSThomas Cort 			if (split_line(field, FALSE, pos, &row) != E_OK) {
659a0e6850fSThomas Cort 				wrap_err = E_REQUEST_DENIED;
660a0e6850fSThomas Cort 				goto restore_and_exit;
661a0e6850fSThomas Cort 			}
662a0e6850fSThomas Cort 		} else
663a0e6850fSThomas Cort 			  /* line is exactly the right length, do next one */
664a0e6850fSThomas Cort 			row = row->next;
665a0e6850fSThomas Cort 	}
666a0e6850fSThomas Cort 
667a0e6850fSThomas Cort 	  /* Check if we have not run out of room */
668a0e6850fSThomas Cort 	if ((((field->opts & O_STATIC) == O_STATIC) &&
669a0e6850fSThomas Cort 	     field->row_count > (field->rows + field->nrows)) ||
670a0e6850fSThomas Cort 	    ((field->max != 0) && (field->row_count > field->max))) {
671a0e6850fSThomas Cort 
672a0e6850fSThomas Cort 		wrap_err = E_REQUEST_DENIED;
673a0e6850fSThomas Cort 
674a0e6850fSThomas Cort 	  restore_and_exit:
675a0e6850fSThomas Cort 		if (saved_row->prev == NULL) {
676a0e6850fSThomas Cort 			field->alines = row_backup;
677a0e6850fSThomas Cort 		} else {
678a0e6850fSThomas Cort 			saved_row->prev->next = row_backup;
679a0e6850fSThomas Cort 			row_backup->prev = saved_row->prev;
680a0e6850fSThomas Cort 		}
681a0e6850fSThomas Cort 
682a0e6850fSThomas Cort 		field->row_xpos = saved_xpos;
683a0e6850fSThomas Cort 		field->cursor_xpos = saved_cur_xpos;
684a0e6850fSThomas Cort 		field->cursor_ypos = saved_ypos;
685a0e6850fSThomas Cort 		field->row_count = saved_row_count;
686a0e6850fSThomas Cort 		field->start_line = saved_start_line;
687a0e6850fSThomas Cort 		field->cur_line = saved_cur_line;
688a0e6850fSThomas Cort 
689a0e6850fSThomas Cort 		destroy_row_list(saved_row);
690a0e6850fSThomas Cort 		return wrap_err;
691a0e6850fSThomas Cort 	}
692a0e6850fSThomas Cort 
693a0e6850fSThomas Cort 	destroy_row_list(row_backup);
694a0e6850fSThomas Cort 	return E_OK;
695a0e6850fSThomas Cort }
696a0e6850fSThomas Cort 
697a0e6850fSThomas Cort /*
698a0e6850fSThomas Cort  * Join the two lines that surround the location pos, the type
699a0e6850fSThomas Cort  * variable indicates the direction of the join, JOIN_NEXT will join
700a0e6850fSThomas Cort  * the next line to the current line, JOIN_PREV will join the current
701a0e6850fSThomas Cort  * line to the previous line, the new lines will be wrapped unless the
702a0e6850fSThomas Cort  * _NW versions of the directions are used.  Returns E_OK if the join
703a0e6850fSThomas Cort  * was successful or E_REQUEST_DENIED if the join cannot happen.
704a0e6850fSThomas Cort  */
705a0e6850fSThomas Cort static int
_formi_join_line(FIELD * field,_FORMI_FIELD_LINES ** rowp,int direction)706a0e6850fSThomas Cort _formi_join_line(FIELD *field, _FORMI_FIELD_LINES **rowp, int direction)
707a0e6850fSThomas Cort {
708a0e6850fSThomas Cort 	int old_len, count;
709a0e6850fSThomas Cort 	struct _formi_field_lines *saved;
710a0e6850fSThomas Cort 	char *newp;
711a0e6850fSThomas Cort 	_FORMI_FIELD_LINES *row = *rowp;
712a0e6850fSThomas Cort #ifdef DEBUG
713a0e6850fSThomas Cort 	int dbg_ok = FALSE;
714a0e6850fSThomas Cort 
715a0e6850fSThomas Cort 	if (_formi_create_dbg_file() == E_OK) {
716a0e6850fSThomas Cort 		dbg_ok = TRUE;
717a0e6850fSThomas Cort 	}
718a0e6850fSThomas Cort 
719a0e6850fSThomas Cort 	if (dbg_ok == TRUE) {
720a0e6850fSThomas Cort 		fprintf(dbg, "join_line: working on row %p, row_count = %d\n",
721a0e6850fSThomas Cort 			row, field->row_count);
722a0e6850fSThomas Cort 	}
723a0e6850fSThomas Cort #endif
724a0e6850fSThomas Cort 
725a0e6850fSThomas Cort 	if ((direction == JOIN_NEXT) || (direction == JOIN_NEXT_NW)) {
726a0e6850fSThomas Cort 		  /*
727a0e6850fSThomas Cort 		   * See if there is another line following, or if the
728a0e6850fSThomas Cort 		   * line contains a hard return then we don't join
729a0e6850fSThomas Cort 		   * any lines to it.
730a0e6850fSThomas Cort 		   */
731a0e6850fSThomas Cort 		if ((row->next == NULL) || (row->hard_ret == TRUE)) {
732a0e6850fSThomas Cort 			return E_REQUEST_DENIED;
733a0e6850fSThomas Cort 		}
734a0e6850fSThomas Cort 
735a0e6850fSThomas Cort #ifdef DEBUG
736a0e6850fSThomas Cort 		if (dbg_ok == TRUE) {
737a0e6850fSThomas Cort 			fprintf(dbg,
738a0e6850fSThomas Cort 			"join_line: join_next before length = %d, expanded = %d",
739a0e6850fSThomas Cort 				row->length, row->expanded);
740a0e6850fSThomas Cort 			fprintf(dbg,
741a0e6850fSThomas Cort 				" :: next row length = %d, expanded = %d\n",
742a0e6850fSThomas Cort 				row->length, row->expanded);
743a0e6850fSThomas Cort 		}
744a0e6850fSThomas Cort #endif
745a0e6850fSThomas Cort 
746a0e6850fSThomas Cort 		if (row->allocated < (row->length + row->next->length + 1)) {
747a0e6850fSThomas Cort 			if ((newp = realloc(row->string, (size_t)(row->length +
748a0e6850fSThomas Cort 							  row->next->length
749a0e6850fSThomas Cort 							  + 1))) == NULL)
750a0e6850fSThomas Cort 				return E_REQUEST_DENIED;
751a0e6850fSThomas Cort 			row->string = newp;
752a0e6850fSThomas Cort 			row->allocated = row->length + row->next->length + 1;
753a0e6850fSThomas Cort 		}
754a0e6850fSThomas Cort 
755a0e6850fSThomas Cort 		strcat(row->string, row->next->string);
756a0e6850fSThomas Cort 		old_len = row->length;
757a0e6850fSThomas Cort 		row->length += row->next->length;
758a0e6850fSThomas Cort 		if (row->length > 0)
759a0e6850fSThomas Cort 			row->expanded =
760a0e6850fSThomas Cort 				_formi_tab_expanded_length(row->string, 0,
761a0e6850fSThomas Cort 							   row->length - 1);
762a0e6850fSThomas Cort 		else
763a0e6850fSThomas Cort 			row->expanded = 0;
764a0e6850fSThomas Cort 
765a0e6850fSThomas Cort 		_formi_calculate_tabs(row);
766a0e6850fSThomas Cort 		row->hard_ret = row->next->hard_ret;
767a0e6850fSThomas Cort 
768a0e6850fSThomas Cort 		  /* adjust current line if it is on the row being eaten */
769a0e6850fSThomas Cort 		if (field->cur_line == row->next) {
770a0e6850fSThomas Cort 			field->cur_line = row;
771a0e6850fSThomas Cort 			field->row_xpos += old_len;
772a0e6850fSThomas Cort 			field->cursor_xpos =
773a0e6850fSThomas Cort 				_formi_tab_expanded_length(row->string, 0,
774a0e6850fSThomas Cort 							   field->row_xpos);
775a0e6850fSThomas Cort 			if (field->cursor_xpos > 0)
776a0e6850fSThomas Cort 				field->cursor_xpos--;
777a0e6850fSThomas Cort 
778a0e6850fSThomas Cort 			if (field->cursor_ypos > 0)
779a0e6850fSThomas Cort 				field->cursor_ypos--;
780a0e6850fSThomas Cort 			else {
781a0e6850fSThomas Cort 				if (field->start_line->prev != NULL)
782a0e6850fSThomas Cort 					field->start_line =
783a0e6850fSThomas Cort 						field->start_line->prev;
784a0e6850fSThomas Cort 			}
785a0e6850fSThomas Cort 		}
786a0e6850fSThomas Cort 
787a0e6850fSThomas Cort 		  /* remove joined line record from the row list */
788a0e6850fSThomas Cort 		add_to_free(field, row->next);
789a0e6850fSThomas Cort 
790a0e6850fSThomas Cort #ifdef DEBUG
791a0e6850fSThomas Cort 		if (dbg_ok == TRUE) {
792a0e6850fSThomas Cort 			fprintf(dbg,
793a0e6850fSThomas Cort 				"join_line: exit length = %d, expanded = %d\n",
794a0e6850fSThomas Cort 				row->length, row->expanded);
795a0e6850fSThomas Cort 		}
796a0e6850fSThomas Cort #endif
797a0e6850fSThomas Cort 	} else {
798a0e6850fSThomas Cort 		if (row->prev == NULL) {
799a0e6850fSThomas Cort 			return E_REQUEST_DENIED;
800a0e6850fSThomas Cort 		}
801a0e6850fSThomas Cort 
802a0e6850fSThomas Cort 		saved = row->prev;
803a0e6850fSThomas Cort 
804a0e6850fSThomas Cort 		  /*
805a0e6850fSThomas Cort 		   * Don't try to join if the line above has a hard
806a0e6850fSThomas Cort 		   * return on it.
807a0e6850fSThomas Cort 		   */
808a0e6850fSThomas Cort 		if (saved->hard_ret == TRUE) {
809a0e6850fSThomas Cort 			return E_REQUEST_DENIED;
810a0e6850fSThomas Cort 		}
811a0e6850fSThomas Cort 
812a0e6850fSThomas Cort #ifdef DEBUG
813a0e6850fSThomas Cort 		if (dbg_ok == TRUE) {
814a0e6850fSThomas Cort 			fprintf(dbg,
815a0e6850fSThomas Cort 			"join_line: join_prev before length = %d, expanded = %d",
816a0e6850fSThomas Cort 				row->length, row->expanded);
817a0e6850fSThomas Cort 			fprintf(dbg,
818a0e6850fSThomas Cort 				" :: prev row length = %d, expanded = %d\n",
819a0e6850fSThomas Cort 				saved->length, saved->expanded);
820a0e6850fSThomas Cort 		}
821a0e6850fSThomas Cort #endif
822a0e6850fSThomas Cort 
823a0e6850fSThomas Cort 		if (saved->allocated < (row->length + saved->length + 1)) {
824a0e6850fSThomas Cort 			if ((newp = realloc(saved->string,
825a0e6850fSThomas Cort 					    (size_t) (row->length +
826a0e6850fSThomas Cort 						      saved->length
827a0e6850fSThomas Cort 						      + 1))) == NULL)
828a0e6850fSThomas Cort 				return E_REQUEST_DENIED;
829a0e6850fSThomas Cort 			saved->string = newp;
830a0e6850fSThomas Cort 			saved->allocated = row->length + saved->length + 1;
831a0e6850fSThomas Cort 		}
832a0e6850fSThomas Cort 
833a0e6850fSThomas Cort 		strcat(saved->string, row->string);
834a0e6850fSThomas Cort 		old_len = saved->length;
835a0e6850fSThomas Cort 		saved->length += row->length;
836a0e6850fSThomas Cort 		if (saved->length > 0)
837a0e6850fSThomas Cort 			saved->expanded =
838a0e6850fSThomas Cort 				_formi_tab_expanded_length(saved->string, 0,
839a0e6850fSThomas Cort 							   saved->length - 1);
840a0e6850fSThomas Cort 		else
841a0e6850fSThomas Cort 			saved->length = 0;
842a0e6850fSThomas Cort 
843a0e6850fSThomas Cort 		saved->hard_ret = row->hard_ret;
844a0e6850fSThomas Cort 
845a0e6850fSThomas Cort 		  /* adjust current line if it was on the row being eaten */
846a0e6850fSThomas Cort 		if (field->cur_line == row) {
847a0e6850fSThomas Cort 			field->cur_line = saved;
848a0e6850fSThomas Cort 			field->row_xpos += old_len;
849a0e6850fSThomas Cort 			field->cursor_xpos =
850a0e6850fSThomas Cort 				_formi_tab_expanded_length(saved->string, 0,
851a0e6850fSThomas Cort 							   field->row_xpos);
852a0e6850fSThomas Cort 			if (field->cursor_xpos > 0)
853a0e6850fSThomas Cort 				field->cursor_xpos--;
854a0e6850fSThomas Cort 		}
855a0e6850fSThomas Cort 
856a0e6850fSThomas Cort 		add_to_free(field, row);
857a0e6850fSThomas Cort 
858a0e6850fSThomas Cort #ifdef DEBUG
859a0e6850fSThomas Cort 		if (dbg_ok == TRUE) {
860a0e6850fSThomas Cort 			fprintf(dbg,
861a0e6850fSThomas Cort 				"join_line: exit length = %d, expanded = %d\n",
862a0e6850fSThomas Cort 				saved->length, saved->expanded);
863a0e6850fSThomas Cort 		}
864a0e6850fSThomas Cort #endif
865a0e6850fSThomas Cort 		row = saved;
866a0e6850fSThomas Cort 	}
867a0e6850fSThomas Cort 
868a0e6850fSThomas Cort 
869a0e6850fSThomas Cort 	  /*
870a0e6850fSThomas Cort 	   * Work out where the line lies in the field in relation to
871a0e6850fSThomas Cort 	   * the cursor_ypos.  First count the rows from the start of
872a0e6850fSThomas Cort 	   * the field until we hit the row we just worked on.
873a0e6850fSThomas Cort 	   */
874a0e6850fSThomas Cort 	saved = field->start_line;
875a0e6850fSThomas Cort 	count = 0;
876a0e6850fSThomas Cort 	while (saved->next != NULL) {
877a0e6850fSThomas Cort 		if (saved == row)
878a0e6850fSThomas Cort 			break;
879a0e6850fSThomas Cort 		count++;
880a0e6850fSThomas Cort 		saved = saved->next;
881a0e6850fSThomas Cort 	}
882a0e6850fSThomas Cort 
883a0e6850fSThomas Cort 	  /* now check if we need to adjust cursor_ypos */
884a0e6850fSThomas Cort 	if (field->cursor_ypos > count) {
885a0e6850fSThomas Cort 		field->cursor_ypos--;
886a0e6850fSThomas Cort 	}
887a0e6850fSThomas Cort 
888a0e6850fSThomas Cort 	field->row_count--;
889a0e6850fSThomas Cort 	*rowp = row;
890a0e6850fSThomas Cort 
891a0e6850fSThomas Cort 	  /* wrap the field if required, if this fails undo the change */
892a0e6850fSThomas Cort 	if ((direction == JOIN_NEXT) || (direction == JOIN_PREV)) {
893a0e6850fSThomas Cort 		if (_formi_wrap_field(field, row) != E_OK) {
894a0e6850fSThomas Cort 			return E_REQUEST_DENIED;
895a0e6850fSThomas Cort 		}
896a0e6850fSThomas Cort 	}
897a0e6850fSThomas Cort 
898a0e6850fSThomas Cort 	return E_OK;
899a0e6850fSThomas Cort }
900a0e6850fSThomas Cort 
901a0e6850fSThomas Cort /*
902a0e6850fSThomas Cort  * Split the line at the given position, if possible.  If hard_split is
903a0e6850fSThomas Cort  * TRUE then split the line regardless of the position, otherwise don't
904a0e6850fSThomas Cort  * split at the beginning of a line.
905a0e6850fSThomas Cort  */
906a0e6850fSThomas Cort static int
split_line(FIELD * field,bool hard_split,unsigned pos,_FORMI_FIELD_LINES ** rowp)907a0e6850fSThomas Cort split_line(FIELD *field, bool hard_split, unsigned pos,
908a0e6850fSThomas Cort 	   _FORMI_FIELD_LINES **rowp)
909a0e6850fSThomas Cort {
910a0e6850fSThomas Cort 	struct _formi_field_lines *new_line;
911a0e6850fSThomas Cort 	char *newp;
912a0e6850fSThomas Cort 	_FORMI_FIELD_LINES *row = *rowp;
913a0e6850fSThomas Cort #ifdef DEBUG
914a0e6850fSThomas Cort 	short dbg_ok = FALSE;
915a0e6850fSThomas Cort #endif
916a0e6850fSThomas Cort 
917a0e6850fSThomas Cort 	  /* if asked to split right where the line already starts then
918a0e6850fSThomas Cort 	   * just return - nothing to do unless we are appending a line
919a0e6850fSThomas Cort 	   * to the buffer.
920a0e6850fSThomas Cort 	   */
921a0e6850fSThomas Cort 	if ((pos == 0) && (hard_split == FALSE))
922a0e6850fSThomas Cort 		return E_OK;
923a0e6850fSThomas Cort 
924a0e6850fSThomas Cort #ifdef DEBUG
925a0e6850fSThomas Cort 	if (_formi_create_dbg_file() == E_OK) {
926a0e6850fSThomas Cort 		fprintf(dbg, "split_line: splitting line at %d\n", pos);
927a0e6850fSThomas Cort 		dbg_ok = TRUE;
928a0e6850fSThomas Cort 	}
929a0e6850fSThomas Cort #endif
930a0e6850fSThomas Cort 
931a0e6850fSThomas Cort 	  /* Need an extra line struct, check free list first */
932a0e6850fSThomas Cort 	if (field->free != NULL) {
933a0e6850fSThomas Cort 		new_line = field->free;
934a0e6850fSThomas Cort 		field->free = new_line->next;
935a0e6850fSThomas Cort 		if (field->free != NULL)
936a0e6850fSThomas Cort 			field->free->prev = NULL;
937a0e6850fSThomas Cort 	} else {
938a0e6850fSThomas Cort 		if ((new_line = (struct _formi_field_lines *)
939a0e6850fSThomas Cort 		     malloc(sizeof(struct _formi_field_lines))) == NULL)
940a0e6850fSThomas Cort 			return E_SYSTEM_ERROR;
941a0e6850fSThomas Cort 		new_line->prev = NULL;
942a0e6850fSThomas Cort 		new_line->next = NULL;
943a0e6850fSThomas Cort 		new_line->allocated = 0;
944a0e6850fSThomas Cort 		new_line->length = 0;
945a0e6850fSThomas Cort 		new_line->expanded = 0;
946a0e6850fSThomas Cort 		new_line->string = NULL;
947a0e6850fSThomas Cort 		new_line->hard_ret = FALSE;
948a0e6850fSThomas Cort 		new_line->tabs = NULL;
949a0e6850fSThomas Cort 	}
950a0e6850fSThomas Cort 
951a0e6850fSThomas Cort #ifdef DEBUG
952a0e6850fSThomas Cort 	if (dbg_ok == TRUE) {
953a0e6850fSThomas Cort 		fprintf(dbg,
954a0e6850fSThomas Cort 	"split_line: enter: length = %d, expanded = %d\n",
955a0e6850fSThomas Cort 			row->length, row->expanded);
956a0e6850fSThomas Cort 	}
957a0e6850fSThomas Cort #endif
958a0e6850fSThomas Cort 
959a0e6850fSThomas Cort 	assert((row->length < INT_MAX) && (row->expanded < INT_MAX));
960a0e6850fSThomas Cort 
961a0e6850fSThomas Cort 
962a0e6850fSThomas Cort 	  /* add new line to the row list */
963a0e6850fSThomas Cort 	new_line->next = row->next;
964a0e6850fSThomas Cort 	new_line->prev = row;
965a0e6850fSThomas Cort 	row->next = new_line;
966a0e6850fSThomas Cort 	if (new_line->next != NULL)
967a0e6850fSThomas Cort 		new_line->next->prev = new_line;
968a0e6850fSThomas Cort 
969a0e6850fSThomas Cort 	new_line->length = row->length - pos;
970a0e6850fSThomas Cort 	if (new_line->length >= new_line->allocated) {
971a0e6850fSThomas Cort 		if ((newp = realloc(new_line->string,
972a0e6850fSThomas Cort 				    (size_t) new_line->length + 1)) == NULL)
973a0e6850fSThomas Cort 			return E_SYSTEM_ERROR;
974a0e6850fSThomas Cort 		new_line->string = newp;
975a0e6850fSThomas Cort 		new_line->allocated = new_line->length + 1;
976a0e6850fSThomas Cort 	}
977a0e6850fSThomas Cort 
978a0e6850fSThomas Cort 	strcpy(new_line->string, &row->string[pos]);
979a0e6850fSThomas Cort 
980a0e6850fSThomas Cort 	row->length = pos;
981a0e6850fSThomas Cort 	row->string[pos] = '\0';
982a0e6850fSThomas Cort 
983a0e6850fSThomas Cort 	if (row->length != 0)
984a0e6850fSThomas Cort 		row->expanded = _formi_tab_expanded_length(row->string, 0,
985a0e6850fSThomas Cort 							   row->length - 1);
986a0e6850fSThomas Cort 	else
987a0e6850fSThomas Cort 		row->expanded = 0;
988a0e6850fSThomas Cort 	_formi_calculate_tabs(row);
989a0e6850fSThomas Cort 
990a0e6850fSThomas Cort 	if (new_line->length != 0)
991a0e6850fSThomas Cort 		new_line->expanded =
992a0e6850fSThomas Cort 			_formi_tab_expanded_length(new_line->string, 0,
993a0e6850fSThomas Cort 						   new_line->length - 1);
994a0e6850fSThomas Cort 	else
995a0e6850fSThomas Cort 		new_line->expanded = 0;
996a0e6850fSThomas Cort 
997a0e6850fSThomas Cort 	_formi_calculate_tabs(new_line);
998a0e6850fSThomas Cort 
999a0e6850fSThomas Cort 	  /*
1000a0e6850fSThomas Cort 	   * If the given row was the current line then adjust the
1001a0e6850fSThomas Cort 	   * current line pointer if necessary
1002a0e6850fSThomas Cort 	   */
1003a0e6850fSThomas Cort 	if ((field->cur_line == row) && (field->row_xpos >= pos)) {
1004a0e6850fSThomas Cort 		field->cur_line = new_line;
1005a0e6850fSThomas Cort 		field->row_xpos -= pos;
1006a0e6850fSThomas Cort 		field->cursor_xpos =
1007a0e6850fSThomas Cort 			_formi_tab_expanded_length(new_line->string, 0,
1008a0e6850fSThomas Cort 						   field->row_xpos);
1009a0e6850fSThomas Cort 		if (field->cursor_xpos > 0)
1010a0e6850fSThomas Cort 			field->cursor_xpos--;
1011a0e6850fSThomas Cort 
1012a0e6850fSThomas Cort 		field->cursor_ypos++;
1013a0e6850fSThomas Cort 		if (field->cursor_ypos >= field->rows) {
1014a0e6850fSThomas Cort 			if (field->start_line->next != NULL) {
1015a0e6850fSThomas Cort 				field->start_line = field->start_line->next;
1016a0e6850fSThomas Cort 				field->cursor_ypos = field->rows - 1;
1017a0e6850fSThomas Cort 			}
1018a0e6850fSThomas Cort 			else
1019a0e6850fSThomas Cort 				assert(field->start_line->next == NULL);
1020a0e6850fSThomas Cort 		}
1021a0e6850fSThomas Cort 	}
1022a0e6850fSThomas Cort 
1023a0e6850fSThomas Cort 	  /*
1024a0e6850fSThomas Cort 	   * If the line split had a hard return then replace the
1025a0e6850fSThomas Cort 	   * current line's hard return with a soft return and carry
1026a0e6850fSThomas Cort 	   * the hard return onto the line after.
1027a0e6850fSThomas Cort 	   */
1028a0e6850fSThomas Cort 	if (row->hard_ret == TRUE) {
1029a0e6850fSThomas Cort 		new_line->hard_ret = TRUE;
1030a0e6850fSThomas Cort 		row->hard_ret = FALSE;
1031a0e6850fSThomas Cort 	}
1032a0e6850fSThomas Cort 
1033a0e6850fSThomas Cort 	  /*
1034a0e6850fSThomas Cort 	   * except where we are doing a hard split then the current
1035a0e6850fSThomas Cort 	   * row must have a hard return on it too...
1036a0e6850fSThomas Cort 	   */
1037a0e6850fSThomas Cort 	if (hard_split == TRUE) {
1038a0e6850fSThomas Cort 		row->hard_ret = TRUE;
1039a0e6850fSThomas Cort 	}
1040a0e6850fSThomas Cort 
1041a0e6850fSThomas Cort 	assert(((row->expanded < INT_MAX) &&
1042a0e6850fSThomas Cort 		(new_line->expanded < INT_MAX) &&
1043a0e6850fSThomas Cort 		(row->length < INT_MAX) &&
1044a0e6850fSThomas Cort 		(new_line->length < INT_MAX)));
1045a0e6850fSThomas Cort 
1046a0e6850fSThomas Cort #ifdef DEBUG
1047a0e6850fSThomas Cort 	if (dbg_ok == TRUE) {
1048a0e6850fSThomas Cort 		fprintf(dbg, "split_line: exit: ");
1049a0e6850fSThomas Cort 		fprintf(dbg, "row.length = %d, row.expanded = %d, ",
1050a0e6850fSThomas Cort 			row->length, row->expanded);
1051a0e6850fSThomas Cort 		fprintf(dbg,
1052a0e6850fSThomas Cort 			"next_line.length = %d, next_line.expanded = %d, ",
1053a0e6850fSThomas Cort 			new_line->length, new_line->expanded);
1054a0e6850fSThomas Cort 		fprintf(dbg, "row_count = %d\n", field->row_count + 1);
1055a0e6850fSThomas Cort 	}
1056a0e6850fSThomas Cort #endif
1057a0e6850fSThomas Cort 
1058a0e6850fSThomas Cort 	field->row_count++;
1059a0e6850fSThomas Cort 	*rowp = new_line;
1060a0e6850fSThomas Cort 
1061a0e6850fSThomas Cort 	return E_OK;
1062a0e6850fSThomas Cort }
1063a0e6850fSThomas Cort 
1064a0e6850fSThomas Cort /*
1065a0e6850fSThomas Cort  * skip the blanks in the given string, start at the index start and
1066a0e6850fSThomas Cort  * continue forward until either the end of the string or a non-blank
1067a0e6850fSThomas Cort  * character is found.  Return the index of either the end of the string or
1068a0e6850fSThomas Cort  * the first non-blank character.
1069a0e6850fSThomas Cort  */
1070a0e6850fSThomas Cort unsigned
_formi_skip_blanks(char * string,unsigned int start)1071a0e6850fSThomas Cort _formi_skip_blanks(char *string, unsigned int start)
1072a0e6850fSThomas Cort {
1073a0e6850fSThomas Cort 	unsigned int i;
1074a0e6850fSThomas Cort 
1075a0e6850fSThomas Cort 	i = start;
1076a0e6850fSThomas Cort 
1077a0e6850fSThomas Cort 	while ((string[i] != '\0') && isblank((unsigned char)string[i]))
1078a0e6850fSThomas Cort 		i++;
1079a0e6850fSThomas Cort 
1080a0e6850fSThomas Cort 	return i;
1081a0e6850fSThomas Cort }
1082a0e6850fSThomas Cort 
1083a0e6850fSThomas Cort /*
1084a0e6850fSThomas Cort  * Skip the blanks in the string associated with the given row, pass back
1085a0e6850fSThomas Cort  * the row and the offset at which the first non-blank is found.  If no
1086a0e6850fSThomas Cort  * non-blank character is found then return the index to the last
1087a0e6850fSThomas Cort  * character on the last line.
1088a0e6850fSThomas Cort  */
1089a0e6850fSThomas Cort 
1090a0e6850fSThomas Cort unsigned
field_skip_blanks(unsigned int start,_FORMI_FIELD_LINES ** rowp)1091a0e6850fSThomas Cort field_skip_blanks(unsigned int start, _FORMI_FIELD_LINES **rowp)
1092a0e6850fSThomas Cort {
1093a0e6850fSThomas Cort 	unsigned int i;
1094a0e6850fSThomas Cort 	_FORMI_FIELD_LINES *row, *last = NULL;
1095a0e6850fSThomas Cort 
1096a0e6850fSThomas Cort 	row = *rowp;
1097a0e6850fSThomas Cort 	i = start;
1098a0e6850fSThomas Cort 
1099a0e6850fSThomas Cort 	do {
1100a0e6850fSThomas Cort 		i = _formi_skip_blanks(&row->string[i], i);
1101a0e6850fSThomas Cort 		if (!isblank((unsigned char)row->string[i])) {
1102a0e6850fSThomas Cort 			last = row;
1103a0e6850fSThomas Cort 			row = row->next;
1104a0e6850fSThomas Cort 			  /*
1105a0e6850fSThomas Cort 			   * don't reset if last line otherwise we will
1106a0e6850fSThomas Cort 			   * not be at the end of the string.
1107a0e6850fSThomas Cort 			   */
1108a0e6850fSThomas Cort 			if (row != NULL)
1109a0e6850fSThomas Cort 				i = 0;
1110a0e6850fSThomas Cort 		} else
1111a0e6850fSThomas Cort 			break;
1112a0e6850fSThomas Cort 	}
1113a0e6850fSThomas Cort 	while (row != NULL);
1114a0e6850fSThomas Cort 
1115a0e6850fSThomas Cort 	  /*
1116a0e6850fSThomas Cort 	   * If we hit the end of the row list then point at the last row
1117a0e6850fSThomas Cort 	   * otherwise we return the row we found the blank on.
1118a0e6850fSThomas Cort 	   */
1119a0e6850fSThomas Cort 	if (row == NULL)
1120a0e6850fSThomas Cort 		*rowp = last;
1121a0e6850fSThomas Cort 	else
1122a0e6850fSThomas Cort 		*rowp = row;
1123a0e6850fSThomas Cort 
1124a0e6850fSThomas Cort 	return i;
1125a0e6850fSThomas Cort }
1126a0e6850fSThomas Cort 
1127a0e6850fSThomas Cort /*
1128a0e6850fSThomas Cort  * Return the index of the top left most field of the two given fields.
1129a0e6850fSThomas Cort  */
1130a0e6850fSThomas Cort static int
_formi_top_left(FORM * form,int a,int b)1131a0e6850fSThomas Cort _formi_top_left(FORM *form, int a, int b)
1132a0e6850fSThomas Cort {
1133a0e6850fSThomas Cort 	  /* lower row numbers always win here.... */
1134a0e6850fSThomas Cort 	if (form->fields[a]->form_row < form->fields[b]->form_row)
1135a0e6850fSThomas Cort 		return a;
1136a0e6850fSThomas Cort 
1137a0e6850fSThomas Cort 	if (form->fields[a]->form_row > form->fields[b]->form_row)
1138a0e6850fSThomas Cort 		return b;
1139a0e6850fSThomas Cort 
1140a0e6850fSThomas Cort 	  /* rows must be equal, check columns */
1141a0e6850fSThomas Cort 	if (form->fields[a]->form_col < form->fields[b]->form_col)
1142a0e6850fSThomas Cort 		return a;
1143a0e6850fSThomas Cort 
1144a0e6850fSThomas Cort 	if (form->fields[a]->form_col > form->fields[b]->form_col)
1145a0e6850fSThomas Cort 		return b;
1146a0e6850fSThomas Cort 
1147a0e6850fSThomas Cort 	  /* if we get here fields must be in exactly the same place, punt */
1148a0e6850fSThomas Cort 	return a;
1149a0e6850fSThomas Cort }
1150a0e6850fSThomas Cort 
1151a0e6850fSThomas Cort /*
1152a0e6850fSThomas Cort  * Return the index to the field that is the bottom-right-most of the
1153a0e6850fSThomas Cort  * two given fields.
1154a0e6850fSThomas Cort  */
1155a0e6850fSThomas Cort static int
_formi_bottom_right(FORM * form,int a,int b)1156a0e6850fSThomas Cort _formi_bottom_right(FORM *form, int a, int b)
1157a0e6850fSThomas Cort {
1158a0e6850fSThomas Cort 	  /* check the rows first, biggest row wins */
1159a0e6850fSThomas Cort 	if (form->fields[a]->form_row > form->fields[b]->form_row)
1160a0e6850fSThomas Cort 		return a;
1161a0e6850fSThomas Cort 	if (form->fields[a]->form_row < form->fields[b]->form_row)
1162a0e6850fSThomas Cort 		return b;
1163a0e6850fSThomas Cort 
1164a0e6850fSThomas Cort 	  /* rows must be equal, check cols, biggest wins */
1165a0e6850fSThomas Cort 	if (form->fields[a]->form_col > form->fields[b]->form_col)
1166a0e6850fSThomas Cort 		return a;
1167a0e6850fSThomas Cort 	if (form->fields[a]->form_col < form->fields[b]->form_col)
1168a0e6850fSThomas Cort 		return b;
1169a0e6850fSThomas Cort 
1170a0e6850fSThomas Cort 	  /* fields in the same place, punt */
1171a0e6850fSThomas Cort 	return a;
1172a0e6850fSThomas Cort }
1173a0e6850fSThomas Cort 
1174a0e6850fSThomas Cort /*
1175a0e6850fSThomas Cort  * Find the end of the current word in the string str, starting at
1176a0e6850fSThomas Cort  * offset - the end includes any trailing whitespace.  If the end of
1177a0e6850fSThomas Cort  * the string is found before a new word then just return the offset
1178a0e6850fSThomas Cort  * to the end of the string.  If do_join is TRUE then lines will be
1179a0e6850fSThomas Cort  * joined (without wrapping) until either the end of the field or the
1180a0e6850fSThomas Cort  * end of a word is found (whichever comes first).
1181a0e6850fSThomas Cort  */
1182a0e6850fSThomas Cort static int
find_eow(FIELD * cur,unsigned int offset,bool do_join,_FORMI_FIELD_LINES ** rowp)1183a0e6850fSThomas Cort find_eow(FIELD *cur, unsigned int offset, bool do_join,
1184a0e6850fSThomas Cort 	 _FORMI_FIELD_LINES **rowp)
1185a0e6850fSThomas Cort {
1186a0e6850fSThomas Cort 	int start;
1187a0e6850fSThomas Cort 	_FORMI_FIELD_LINES *row;
1188a0e6850fSThomas Cort 
1189a0e6850fSThomas Cort 	row = *rowp;
1190a0e6850fSThomas Cort 	start = offset;
1191a0e6850fSThomas Cort 
1192a0e6850fSThomas Cort 	do {
1193a0e6850fSThomas Cort 		  /* first skip any non-whitespace */
1194a0e6850fSThomas Cort 		while ((row->string[start] != '\0')
1195a0e6850fSThomas Cort 		       && !isblank((unsigned char)row->string[start]))
1196a0e6850fSThomas Cort 			start++;
1197a0e6850fSThomas Cort 
1198a0e6850fSThomas Cort 		  /* see if we hit the end of the string */
1199a0e6850fSThomas Cort 		if (row->string[start] == '\0') {
1200a0e6850fSThomas Cort 			if (do_join == TRUE) {
1201a0e6850fSThomas Cort 				if (row->next == NULL)
1202a0e6850fSThomas Cort 					return start;
1203a0e6850fSThomas Cort 
1204a0e6850fSThomas Cort 				if (_formi_join_line(cur, &row, JOIN_NEXT_NW)
1205a0e6850fSThomas Cort 				    != E_OK)
1206a0e6850fSThomas Cort 					return E_REQUEST_DENIED;
1207a0e6850fSThomas Cort 			} else {
1208a0e6850fSThomas Cort 				do {
1209a0e6850fSThomas Cort 					if (row->next == NULL) {
1210a0e6850fSThomas Cort 						*rowp = row;
1211a0e6850fSThomas Cort 						return start;
1212a0e6850fSThomas Cort 					} else {
1213a0e6850fSThomas Cort 						row = row->next;
1214a0e6850fSThomas Cort 						start = 0;
1215a0e6850fSThomas Cort 					}
1216a0e6850fSThomas Cort 				} while (row->length == 0);
1217a0e6850fSThomas Cort 			}
1218a0e6850fSThomas Cort 		}
1219a0e6850fSThomas Cort 	} while (!isblank((unsigned char)row->string[start]));
1220a0e6850fSThomas Cort 
1221a0e6850fSThomas Cort 	do {
1222a0e6850fSThomas Cort 		  /* otherwise skip the whitespace.... */
1223a0e6850fSThomas Cort 		while ((row->string[start] != '\0')
1224a0e6850fSThomas Cort 		       && isblank((unsigned char)row->string[start]))
1225a0e6850fSThomas Cort 			start++;
1226a0e6850fSThomas Cort 
1227a0e6850fSThomas Cort 		if (row->string[start] == '\0') {
1228a0e6850fSThomas Cort 			if (do_join == TRUE) {
1229a0e6850fSThomas Cort 				if (row->next == NULL)
1230a0e6850fSThomas Cort 					return start;
1231a0e6850fSThomas Cort 
1232a0e6850fSThomas Cort 				if (_formi_join_line(cur, &row, JOIN_NEXT_NW)
1233a0e6850fSThomas Cort 				    != E_OK)
1234a0e6850fSThomas Cort 					return E_REQUEST_DENIED;
1235a0e6850fSThomas Cort 			} else {
1236a0e6850fSThomas Cort 				do {
1237a0e6850fSThomas Cort 					if (row->next == NULL) {
1238a0e6850fSThomas Cort 						*rowp = row;
1239a0e6850fSThomas Cort 						return start;
1240a0e6850fSThomas Cort 					} else {
1241a0e6850fSThomas Cort 						row = row->next;
1242a0e6850fSThomas Cort 						start = 0;
1243a0e6850fSThomas Cort 					}
1244a0e6850fSThomas Cort 				} while (row->length == 0);
1245a0e6850fSThomas Cort 			}
1246a0e6850fSThomas Cort 		}
1247a0e6850fSThomas Cort 	} while (isblank((unsigned char)row->string[start]));
1248a0e6850fSThomas Cort 
1249a0e6850fSThomas Cort 	*rowp = row;
1250a0e6850fSThomas Cort 	return start;
1251a0e6850fSThomas Cort }
1252a0e6850fSThomas Cort 
1253a0e6850fSThomas Cort /*
1254a0e6850fSThomas Cort  * Find the beginning of the current word in the string str, starting
1255a0e6850fSThomas Cort  * at offset.
1256a0e6850fSThomas Cort  */
1257a0e6850fSThomas Cort static int
find_sow(unsigned int offset,_FORMI_FIELD_LINES ** rowp)1258a0e6850fSThomas Cort find_sow(unsigned int offset, _FORMI_FIELD_LINES **rowp)
1259a0e6850fSThomas Cort {
1260a0e6850fSThomas Cort 	int start;
1261a0e6850fSThomas Cort 	char *str;
1262a0e6850fSThomas Cort 	_FORMI_FIELD_LINES *row;
1263a0e6850fSThomas Cort 
1264a0e6850fSThomas Cort 	row = *rowp;
1265a0e6850fSThomas Cort 	str = row->string;
1266a0e6850fSThomas Cort 	start = offset;
1267a0e6850fSThomas Cort 
1268a0e6850fSThomas Cort 	do {
1269a0e6850fSThomas Cort 		if (start > 0) {
1270a0e6850fSThomas Cort 			if (isblank((unsigned char)str[start]) ||
1271a0e6850fSThomas Cort 			    isblank((unsigned char)str[start - 1])) {
1272a0e6850fSThomas Cort 				if (isblank((unsigned char)str[start - 1]))
1273a0e6850fSThomas Cort 					start--;
1274a0e6850fSThomas Cort 				  /* skip the whitespace.... */
1275a0e6850fSThomas Cort 				while ((start >= 0) &&
1276a0e6850fSThomas Cort 				    isblank((unsigned char)str[start]))
1277a0e6850fSThomas Cort 					start--;
1278a0e6850fSThomas Cort 			}
1279a0e6850fSThomas Cort 		}
1280a0e6850fSThomas Cort 
1281a0e6850fSThomas Cort 		  /* see if we hit the start of the string */
1282a0e6850fSThomas Cort 		if (start < 0) {
1283a0e6850fSThomas Cort 			do {
1284a0e6850fSThomas Cort 				if (row->prev == NULL) {
1285a0e6850fSThomas Cort 					*rowp = row;
1286a0e6850fSThomas Cort 					start = 0;
1287a0e6850fSThomas Cort 					return start;
1288a0e6850fSThomas Cort 				} else {
1289a0e6850fSThomas Cort 					row = row->prev;
1290a0e6850fSThomas Cort 					str = row->string;
1291a0e6850fSThomas Cort 					if (row->length > 0)
1292a0e6850fSThomas Cort 						start = row->length - 1;
1293a0e6850fSThomas Cort 					else
1294a0e6850fSThomas Cort 						start = 0;
1295a0e6850fSThomas Cort 				}
1296a0e6850fSThomas Cort 			} while (row->length == 0);
1297a0e6850fSThomas Cort 		}
1298a0e6850fSThomas Cort 	} while (isblank((unsigned char)row->string[start]));
1299a0e6850fSThomas Cort 
1300a0e6850fSThomas Cort 	  /* see if we hit the start of the string */
1301a0e6850fSThomas Cort 	if (start < 0) {
1302a0e6850fSThomas Cort 		*rowp = row;
1303a0e6850fSThomas Cort 		return 0;
1304a0e6850fSThomas Cort 	}
1305a0e6850fSThomas Cort 
1306a0e6850fSThomas Cort 	  /* now skip any non-whitespace */
1307a0e6850fSThomas Cort 	do {
1308a0e6850fSThomas Cort 		while ((start >= 0) && !isblank((unsigned char)str[start]))
1309a0e6850fSThomas Cort 			start--;
1310a0e6850fSThomas Cort 
1311a0e6850fSThomas Cort 
1312a0e6850fSThomas Cort 		if (start < 0) {
1313a0e6850fSThomas Cort 			do {
1314a0e6850fSThomas Cort 				if (row->prev == NULL) {
1315a0e6850fSThomas Cort 					*rowp = row;
1316a0e6850fSThomas Cort 					start = 0;
1317a0e6850fSThomas Cort 					return start;
1318a0e6850fSThomas Cort 				} else {
1319a0e6850fSThomas Cort 					row = row->prev;
1320a0e6850fSThomas Cort 					str = row->string;
1321a0e6850fSThomas Cort 					if (row->length > 0)
1322a0e6850fSThomas Cort 						start = row->length - 1;
1323a0e6850fSThomas Cort 					else
1324a0e6850fSThomas Cort 						start = 0;
1325a0e6850fSThomas Cort 				}
1326a0e6850fSThomas Cort 			} while (row->length == 0);
1327a0e6850fSThomas Cort 		}
1328a0e6850fSThomas Cort 	} while (!isblank((unsigned char)str[start]));
1329a0e6850fSThomas Cort 
1330a0e6850fSThomas Cort 	if (start > 0) {
1331a0e6850fSThomas Cort 		start++; /* last loop has us pointing at a space, adjust */
1332a0e6850fSThomas Cort 		if (start >= row->length) {
1333a0e6850fSThomas Cort 			if (row->next != NULL) {
1334a0e6850fSThomas Cort 				start = 0;
1335a0e6850fSThomas Cort 				row = row->next;
1336a0e6850fSThomas Cort 			} else {
1337a0e6850fSThomas Cort 				start = row->length - 1;
1338a0e6850fSThomas Cort 			}
1339a0e6850fSThomas Cort 		}
1340a0e6850fSThomas Cort 	}
1341a0e6850fSThomas Cort 
1342a0e6850fSThomas Cort 	if (start < 0)
1343a0e6850fSThomas Cort 		start = 0;
1344a0e6850fSThomas Cort 
1345a0e6850fSThomas Cort 	*rowp = row;
1346a0e6850fSThomas Cort 	return start;
1347a0e6850fSThomas Cort }
1348a0e6850fSThomas Cort 
1349a0e6850fSThomas Cort /*
1350a0e6850fSThomas Cort  * Scroll the field forward the given number of lines.
1351a0e6850fSThomas Cort  */
1352a0e6850fSThomas Cort static void
_formi_scroll_fwd(FIELD * field,unsigned int amt)1353a0e6850fSThomas Cort _formi_scroll_fwd(FIELD *field, unsigned int amt)
1354a0e6850fSThomas Cort {
1355a0e6850fSThomas Cort 	unsigned int count;
1356a0e6850fSThomas Cort 	_FORMI_FIELD_LINES *end_row;
1357a0e6850fSThomas Cort 
1358a0e6850fSThomas Cort 	end_row = field->start_line;
1359a0e6850fSThomas Cort 	  /* walk the line structs forward to find the bottom of the field */
1360a0e6850fSThomas Cort 	count = field->rows - 1;
1361a0e6850fSThomas Cort 	while ((count > 0) && (end_row->next != NULL))
1362a0e6850fSThomas Cort 	{
1363a0e6850fSThomas Cort 		count--;
1364a0e6850fSThomas Cort 		end_row = end_row->next;
1365a0e6850fSThomas Cort 	}
1366a0e6850fSThomas Cort 
1367a0e6850fSThomas Cort 	  /* check if there are lines to scroll */
1368a0e6850fSThomas Cort 	if ((count > 0) && (end_row->next == NULL))
1369a0e6850fSThomas Cort 		return;
1370a0e6850fSThomas Cort 
1371a0e6850fSThomas Cort 	  /*
1372a0e6850fSThomas Cort 	   * ok, lines to scroll - do this by walking both the start_line
1373a0e6850fSThomas Cort 	   * and the end_row at the same time for amt lines, we stop when
1374a0e6850fSThomas Cort 	   * either we have done the number of lines or end_row hits the
1375a0e6850fSThomas Cort 	   * last line in the field.
1376a0e6850fSThomas Cort 	   */
1377a0e6850fSThomas Cort 	count = amt;
1378a0e6850fSThomas Cort 	while ((count > 0) && (end_row->next != NULL)) {
1379a0e6850fSThomas Cort 		count--;
1380a0e6850fSThomas Cort 		field->start_line = field->start_line->next;
1381a0e6850fSThomas Cort 		end_row = end_row->next;
1382a0e6850fSThomas Cort 	}
1383a0e6850fSThomas Cort }
1384a0e6850fSThomas Cort 
1385a0e6850fSThomas Cort /*
1386a0e6850fSThomas Cort  * Scroll the field backward the given number of lines.
1387a0e6850fSThomas Cort  */
1388a0e6850fSThomas Cort static void
_formi_scroll_back(FIELD * field,unsigned int amt)1389a0e6850fSThomas Cort _formi_scroll_back(FIELD *field, unsigned int amt)
1390a0e6850fSThomas Cort {
1391a0e6850fSThomas Cort 	unsigned int count;
1392a0e6850fSThomas Cort 
1393a0e6850fSThomas Cort 	  /* check for lines above */
1394a0e6850fSThomas Cort 	if (field->start_line->prev == NULL)
1395a0e6850fSThomas Cort 		return;
1396a0e6850fSThomas Cort 
1397a0e6850fSThomas Cort 	  /*
1398a0e6850fSThomas Cort 	   * Backward scroll is easy, follow row struct chain backward until
1399a0e6850fSThomas Cort 	   * the number of lines done or we reach the top of the field.
1400a0e6850fSThomas Cort 	   */
1401a0e6850fSThomas Cort 	count = amt;
1402a0e6850fSThomas Cort 	while ((count > 0) && (field->start_line->prev != NULL)) {
1403a0e6850fSThomas Cort 		count--;
1404a0e6850fSThomas Cort 		field->start_line = field->start_line->prev;
1405a0e6850fSThomas Cort 	}
1406a0e6850fSThomas Cort }
1407a0e6850fSThomas Cort 
1408a0e6850fSThomas Cort /*
1409a0e6850fSThomas Cort  * Scroll the field forward the given number of characters.
1410a0e6850fSThomas Cort  */
1411a0e6850fSThomas Cort void
_formi_hscroll_fwd(FIELD * field,_FORMI_FIELD_LINES * row,int unsigned amt)1412a0e6850fSThomas Cort _formi_hscroll_fwd(FIELD *field, _FORMI_FIELD_LINES *row, int unsigned amt)
1413a0e6850fSThomas Cort {
1414a0e6850fSThomas Cort 	unsigned int end, scroll_amt, expanded;
1415a0e6850fSThomas Cort 	_formi_tab_t *ts;
1416a0e6850fSThomas Cort 
1417a0e6850fSThomas Cort 
1418a0e6850fSThomas Cort 	if ((row->tabs == NULL) || (row->tabs->in_use == FALSE)) {
1419a0e6850fSThomas Cort 		  /* if the line has no tabs things are easy... */
1420a0e6850fSThomas Cort 		end =  field->start_char + field->cols + amt - 1;
1421a0e6850fSThomas Cort 		scroll_amt = amt;
1422a0e6850fSThomas Cort 		if (end > row->length) {
1423a0e6850fSThomas Cort 			end = row->length;
1424a0e6850fSThomas Cort 			scroll_amt = end - field->start_char - field->cols + 1;
1425a0e6850fSThomas Cort 		}
1426a0e6850fSThomas Cort 	} else {
1427a0e6850fSThomas Cort 		  /*
1428a0e6850fSThomas Cort 		   * If there are tabs we need to add on the scroll amount,
1429a0e6850fSThomas Cort 		   * find the last char position that will fit into
1430a0e6850fSThomas Cort 		   * the field and finally fix up the start_char.  This
1431a0e6850fSThomas Cort 		   * is a lot of work but handling the case where there
1432a0e6850fSThomas Cort 		   * are not enough chars to scroll by amt is difficult.
1433a0e6850fSThomas Cort 		   */
1434a0e6850fSThomas Cort 		end = field->start_char + field->row_xpos + amt;
1435a0e6850fSThomas Cort 		if (end >= row->length)
1436a0e6850fSThomas Cort 			end = row->length - 1;
1437a0e6850fSThomas Cort 		else {
1438a0e6850fSThomas Cort 			expanded = _formi_tab_expanded_length(
1439a0e6850fSThomas Cort 				row->string,
1440a0e6850fSThomas Cort 				field->start_char + amt,
1441a0e6850fSThomas Cort 				field->start_char + field->row_xpos + amt);
1442a0e6850fSThomas Cort 			ts = row->tabs;
1443a0e6850fSThomas Cort 			  /* skip tabs to the lhs of our starting point */
1444a0e6850fSThomas Cort 			while ((ts != NULL) && (ts->in_use == TRUE)
1445a0e6850fSThomas Cort 			       && (ts->pos < end))
1446a0e6850fSThomas Cort 				ts = ts->fwd;
1447a0e6850fSThomas Cort 
1448a0e6850fSThomas Cort 			while ((expanded <= field->cols)
1449a0e6850fSThomas Cort 			       && (end < row->length)) {
1450a0e6850fSThomas Cort 				if (row->string[end] == '\t') {
1451a0e6850fSThomas Cort 					assert((ts != NULL)
1452a0e6850fSThomas Cort 					       && (ts->in_use == TRUE));
1453a0e6850fSThomas Cort 					if (ts->pos == end) {
1454a0e6850fSThomas Cort 						if ((expanded + ts->size)
1455a0e6850fSThomas Cort 						    > field->cols)
1456a0e6850fSThomas Cort 							break;
1457a0e6850fSThomas Cort 						expanded += ts->size;
1458a0e6850fSThomas Cort 						ts = ts->fwd;
1459a0e6850fSThomas Cort 					}
1460a0e6850fSThomas Cort 					else
1461a0e6850fSThomas Cort 						assert(ts->pos == end);
1462a0e6850fSThomas Cort 				} else
1463a0e6850fSThomas Cort 					expanded++;
1464a0e6850fSThomas Cort 				end++;
1465a0e6850fSThomas Cort 			}
1466a0e6850fSThomas Cort 		}
1467a0e6850fSThomas Cort 
1468a0e6850fSThomas Cort 		scroll_amt = tab_fit_window(field, end, field->cols);
1469a0e6850fSThomas Cort 		if (scroll_amt < field->start_char)
1470a0e6850fSThomas Cort 			scroll_amt = 1;
1471a0e6850fSThomas Cort 		else
1472a0e6850fSThomas Cort 			scroll_amt -= field->start_char;
1473a0e6850fSThomas Cort 
1474a0e6850fSThomas Cort 		scroll_amt = min(scroll_amt, amt);
1475a0e6850fSThomas Cort 	}
1476a0e6850fSThomas Cort 
1477a0e6850fSThomas Cort 	field->start_char += scroll_amt;
1478a0e6850fSThomas Cort 	field->cursor_xpos =
1479a0e6850fSThomas Cort 		_formi_tab_expanded_length(row->string,
1480a0e6850fSThomas Cort 					   field->start_char,
1481a0e6850fSThomas Cort 					   field->row_xpos
1482a0e6850fSThomas Cort 					   + field->start_char) - 1;
1483a0e6850fSThomas Cort 
1484a0e6850fSThomas Cort }
1485a0e6850fSThomas Cort 
1486a0e6850fSThomas Cort /*
1487a0e6850fSThomas Cort  * Scroll the field backward the given number of characters.
1488a0e6850fSThomas Cort  */
1489a0e6850fSThomas Cort void
_formi_hscroll_back(FIELD * field,_FORMI_FIELD_LINES * row,unsigned int amt)1490a0e6850fSThomas Cort _formi_hscroll_back(FIELD *field, _FORMI_FIELD_LINES *row, unsigned int amt)
1491a0e6850fSThomas Cort {
1492a0e6850fSThomas Cort 	field->start_char -= min(field->start_char, amt);
1493a0e6850fSThomas Cort 	field->cursor_xpos =
1494a0e6850fSThomas Cort 		_formi_tab_expanded_length(row->string, field->start_char,
1495a0e6850fSThomas Cort 					   field->row_xpos
1496a0e6850fSThomas Cort 					   + field->start_char) - 1;
1497a0e6850fSThomas Cort 	if (field->cursor_xpos >= field->cols) {
1498a0e6850fSThomas Cort 		field->row_xpos = 0;
1499a0e6850fSThomas Cort 		field->cursor_xpos = 0;
1500a0e6850fSThomas Cort 	}
1501a0e6850fSThomas Cort }
1502a0e6850fSThomas Cort 
1503a0e6850fSThomas Cort /*
1504a0e6850fSThomas Cort  * Find the different pages in the form fields and assign the form
1505a0e6850fSThomas Cort  * page_starts array with the information to find them.
1506a0e6850fSThomas Cort  */
1507a0e6850fSThomas Cort int
_formi_find_pages(FORM * form)1508a0e6850fSThomas Cort _formi_find_pages(FORM *form)
1509a0e6850fSThomas Cort {
1510a0e6850fSThomas Cort 	int i, cur_page = 0;
1511a0e6850fSThomas Cort 
1512a0e6850fSThomas Cort 	if ((form->page_starts = (_FORMI_PAGE_START *)
1513a0e6850fSThomas Cort 	     malloc((form->max_page + 1) * sizeof(_FORMI_PAGE_START))) == NULL)
1514a0e6850fSThomas Cort 		return E_SYSTEM_ERROR;
1515a0e6850fSThomas Cort 
1516a0e6850fSThomas Cort 	  /* initialise the page starts array */
1517a0e6850fSThomas Cort 	memset(form->page_starts, 0,
1518a0e6850fSThomas Cort 	       (form->max_page + 1) * sizeof(_FORMI_PAGE_START));
1519a0e6850fSThomas Cort 
1520a0e6850fSThomas Cort 	for (i =0; i < form->field_count; i++) {
1521a0e6850fSThomas Cort 		if (form->fields[i]->page_break == 1)
1522a0e6850fSThomas Cort 			cur_page++;
1523a0e6850fSThomas Cort 		if (form->page_starts[cur_page].in_use == 0) {
1524a0e6850fSThomas Cort 			form->page_starts[cur_page].in_use = 1;
1525a0e6850fSThomas Cort 			form->page_starts[cur_page].first = i;
1526a0e6850fSThomas Cort 			form->page_starts[cur_page].last = i;
1527a0e6850fSThomas Cort 			form->page_starts[cur_page].top_left = i;
1528a0e6850fSThomas Cort 			form->page_starts[cur_page].bottom_right = i;
1529a0e6850fSThomas Cort 		} else {
1530a0e6850fSThomas Cort 			form->page_starts[cur_page].last = i;
1531a0e6850fSThomas Cort 			form->page_starts[cur_page].top_left =
1532a0e6850fSThomas Cort 				_formi_top_left(form,
1533a0e6850fSThomas Cort 						form->page_starts[cur_page].top_left,
1534a0e6850fSThomas Cort 						i);
1535a0e6850fSThomas Cort 			form->page_starts[cur_page].bottom_right =
1536a0e6850fSThomas Cort 				_formi_bottom_right(form,
1537a0e6850fSThomas Cort 						    form->page_starts[cur_page].bottom_right,
1538a0e6850fSThomas Cort 						    i);
1539a0e6850fSThomas Cort 		}
1540a0e6850fSThomas Cort 	}
1541a0e6850fSThomas Cort 
1542a0e6850fSThomas Cort 	return E_OK;
1543a0e6850fSThomas Cort }
1544a0e6850fSThomas Cort 
1545a0e6850fSThomas Cort /*
1546a0e6850fSThomas Cort  * Completely redraw the field of the given form.
1547a0e6850fSThomas Cort  */
1548a0e6850fSThomas Cort void
_formi_redraw_field(FORM * form,int field)1549a0e6850fSThomas Cort _formi_redraw_field(FORM *form, int field)
1550a0e6850fSThomas Cort {
1551a0e6850fSThomas Cort 	unsigned int pre, post, flen, slen, i, j, start, line;
1552a0e6850fSThomas Cort 	unsigned int tab, cpos, len;
1553a0e6850fSThomas Cort 	char *str, c;
1554a0e6850fSThomas Cort 	FIELD *cur;
1555a0e6850fSThomas Cort 	_FORMI_FIELD_LINES *row;
1556a0e6850fSThomas Cort #ifdef DEBUG
1557a0e6850fSThomas Cort 	char buffer[100];
1558a0e6850fSThomas Cort #endif
1559a0e6850fSThomas Cort 
1560a0e6850fSThomas Cort 	cur = form->fields[field];
1561a0e6850fSThomas Cort 	flen = cur->cols;
1562a0e6850fSThomas Cort 	slen = 0;
1563a0e6850fSThomas Cort 	start = 0;
1564a0e6850fSThomas Cort 	line = 0;
1565a0e6850fSThomas Cort 
1566a0e6850fSThomas Cort 	for (row = cur->start_line; ((row != NULL) && (line < cur->rows));
1567a0e6850fSThomas Cort 	     row = row->next, line++) {
1568a0e6850fSThomas Cort 		wmove(form->scrwin, (int) (cur->form_row + line),
1569a0e6850fSThomas Cort 		      (int) cur->form_col);
1570a0e6850fSThomas Cort 		if ((cur->rows + cur->nrows) == 1) {
1571a0e6850fSThomas Cort 			if ((cur->cols + cur->start_char) >= row->length)
1572a0e6850fSThomas Cort 				len = row->length;
1573a0e6850fSThomas Cort 			else
1574a0e6850fSThomas Cort 				len = cur->cols + cur->start_char;
1575a0e6850fSThomas Cort 			if (row->string != NULL)
1576a0e6850fSThomas Cort 				slen = _formi_tab_expanded_length(
1577a0e6850fSThomas Cort 					row->string, cur->start_char, len);
1578a0e6850fSThomas Cort 			else
1579a0e6850fSThomas Cort 				slen = 0;
1580a0e6850fSThomas Cort 
1581a0e6850fSThomas Cort 			if (slen > cur->cols)
1582a0e6850fSThomas Cort 				slen = cur->cols;
1583a0e6850fSThomas Cort 			slen += cur->start_char;
1584a0e6850fSThomas Cort 		} else
1585a0e6850fSThomas Cort 			slen = row->expanded;
1586a0e6850fSThomas Cort 
1587a0e6850fSThomas Cort 		if ((cur->opts & O_STATIC) == O_STATIC) {
1588a0e6850fSThomas Cort 			switch (cur->justification) {
1589a0e6850fSThomas Cort 			case JUSTIFY_RIGHT:
1590a0e6850fSThomas Cort 				post = 0;
1591a0e6850fSThomas Cort 				if (flen < slen)
1592a0e6850fSThomas Cort 					pre = 0;
1593a0e6850fSThomas Cort 				else
1594a0e6850fSThomas Cort 					pre = flen - slen;
1595a0e6850fSThomas Cort 				break;
1596a0e6850fSThomas Cort 
1597a0e6850fSThomas Cort 			case JUSTIFY_CENTER:
1598a0e6850fSThomas Cort 				if (flen < slen) {
1599a0e6850fSThomas Cort 					pre = 0;
1600a0e6850fSThomas Cort 					post = 0;
1601a0e6850fSThomas Cort 				} else {
1602a0e6850fSThomas Cort 					pre = flen - slen;
1603a0e6850fSThomas Cort 					post = pre = pre / 2;
1604a0e6850fSThomas Cort 					  /* get padding right if
1605a0e6850fSThomas Cort 					     centring is not even */
1606a0e6850fSThomas Cort 					if ((post + pre + slen) < flen)
1607a0e6850fSThomas Cort 						post++;
1608a0e6850fSThomas Cort 				}
1609a0e6850fSThomas Cort 				break;
1610a0e6850fSThomas Cort 
1611a0e6850fSThomas Cort 			case NO_JUSTIFICATION:
1612a0e6850fSThomas Cort 			case JUSTIFY_LEFT:
1613a0e6850fSThomas Cort 			default:
1614a0e6850fSThomas Cort 				pre = 0;
1615a0e6850fSThomas Cort 				if (flen <= slen)
1616a0e6850fSThomas Cort 					post = 0;
1617a0e6850fSThomas Cort 				else {
1618a0e6850fSThomas Cort 					post = flen - slen;
1619a0e6850fSThomas Cort 					if (post > flen)
1620a0e6850fSThomas Cort 						post = flen;
1621a0e6850fSThomas Cort 				}
1622a0e6850fSThomas Cort 				break;
1623a0e6850fSThomas Cort 			}
1624a0e6850fSThomas Cort 		} else {
1625a0e6850fSThomas Cort 			  /* dynamic fields are not justified */
1626a0e6850fSThomas Cort 			pre = 0;
1627a0e6850fSThomas Cort 			if (flen <= slen)
1628a0e6850fSThomas Cort 				post = 0;
1629a0e6850fSThomas Cort 			else {
1630a0e6850fSThomas Cort 				post = flen - slen;
1631a0e6850fSThomas Cort 				if (post > flen)
1632a0e6850fSThomas Cort 					post = flen;
1633a0e6850fSThomas Cort 			}
1634a0e6850fSThomas Cort 
1635a0e6850fSThomas Cort 			  /* but they do scroll.... */
1636a0e6850fSThomas Cort 
1637a0e6850fSThomas Cort 			if (pre > cur->start_char - start)
1638a0e6850fSThomas Cort 				pre = pre - cur->start_char + start;
1639a0e6850fSThomas Cort 			else
1640a0e6850fSThomas Cort 				pre = 0;
1641a0e6850fSThomas Cort 
1642a0e6850fSThomas Cort 			if (slen > cur->start_char) {
1643a0e6850fSThomas Cort 				slen -= cur->start_char;
1644a0e6850fSThomas Cort 				if (slen > flen)
1645a0e6850fSThomas Cort 					post = 0;
1646a0e6850fSThomas Cort 				else
1647a0e6850fSThomas Cort 					post = flen - slen;
1648a0e6850fSThomas Cort 
1649a0e6850fSThomas Cort 				if (post > flen)
1650a0e6850fSThomas Cort 					post = flen;
1651a0e6850fSThomas Cort 			} else {
1652a0e6850fSThomas Cort 				slen = 0;
1653a0e6850fSThomas Cort 				post = flen - pre;
1654a0e6850fSThomas Cort 			}
1655a0e6850fSThomas Cort 		}
1656a0e6850fSThomas Cort 
1657a0e6850fSThomas Cort 		if (form->cur_field == field)
1658a0e6850fSThomas Cort 			wattrset(form->scrwin, cur->fore);
1659a0e6850fSThomas Cort 		else
1660a0e6850fSThomas Cort 			wattrset(form->scrwin, cur->back);
1661a0e6850fSThomas Cort 
1662a0e6850fSThomas Cort 		str = &row->string[cur->start_char];
1663a0e6850fSThomas Cort 
1664a0e6850fSThomas Cort #ifdef DEBUG
1665a0e6850fSThomas Cort 		if (_formi_create_dbg_file() == E_OK) {
1666a0e6850fSThomas Cort 			fprintf(dbg,
1667a0e6850fSThomas Cort   "redraw_field: start=%d, pre=%d, slen=%d, flen=%d, post=%d, start_char=%d\n",
1668a0e6850fSThomas Cort 				start, pre, slen, flen, post, cur->start_char);
1669a0e6850fSThomas Cort 			if (str != NULL) {
1670a0e6850fSThomas Cort 				if (row->expanded != 0) {
1671a0e6850fSThomas Cort 					strncpy(buffer, str, flen);
1672a0e6850fSThomas Cort 				} else {
1673a0e6850fSThomas Cort 					strcpy(buffer, "(empty)");
1674a0e6850fSThomas Cort 				}
1675a0e6850fSThomas Cort 			} else {
1676a0e6850fSThomas Cort 				strcpy(buffer, "(null)");
1677a0e6850fSThomas Cort 			}
1678a0e6850fSThomas Cort 			buffer[flen] = '\0';
1679a0e6850fSThomas Cort 			fprintf(dbg, "redraw_field: %s\n", buffer);
1680a0e6850fSThomas Cort 		}
1681a0e6850fSThomas Cort #endif
1682a0e6850fSThomas Cort 
1683a0e6850fSThomas Cort 		for (i = start + cur->start_char; i < pre; i++)
1684a0e6850fSThomas Cort 			waddch(form->scrwin, cur->pad);
1685a0e6850fSThomas Cort 
1686a0e6850fSThomas Cort #ifdef DEBUG
1687a0e6850fSThomas Cort 		fprintf(dbg, "redraw_field: will add %d chars\n",
1688a0e6850fSThomas Cort 			min(slen, flen));
1689a0e6850fSThomas Cort #endif
1690a0e6850fSThomas Cort 		for (i = 0, cpos = cur->start_char; i < min(slen, flen);
1691a0e6850fSThomas Cort 		     i++, str++, cpos++)
1692a0e6850fSThomas Cort 		{
1693a0e6850fSThomas Cort 			c = *str;
1694a0e6850fSThomas Cort 			tab = 0; /* just to shut gcc up */
1695a0e6850fSThomas Cort #ifdef DEBUG
1696a0e6850fSThomas Cort 			fprintf(dbg, "adding char str[%d]=%c\n",
1697a0e6850fSThomas Cort 				cpos + cur->start_char,	c);
1698a0e6850fSThomas Cort #endif
1699a0e6850fSThomas Cort 			if (((cur->opts & O_PUBLIC) != O_PUBLIC)) {
1700a0e6850fSThomas Cort 				if (c == '\t')
1701a0e6850fSThomas Cort 					tab = add_tab(form, row, cpos,
1702a0e6850fSThomas Cort 						      cur->pad);
1703a0e6850fSThomas Cort 				else
1704a0e6850fSThomas Cort 					waddch(form->scrwin, cur->pad);
1705a0e6850fSThomas Cort 			} else if ((cur->opts & O_VISIBLE) == O_VISIBLE) {
1706a0e6850fSThomas Cort 				if (c == '\t')
1707a0e6850fSThomas Cort 					tab = add_tab(form, row, cpos, ' ');
1708a0e6850fSThomas Cort 				else
1709a0e6850fSThomas Cort 					waddch(form->scrwin, c);
1710a0e6850fSThomas Cort 			} else {
1711a0e6850fSThomas Cort 				if (c == '\t')
1712a0e6850fSThomas Cort 					tab = add_tab(form, row, cpos, ' ');
1713a0e6850fSThomas Cort 				else
1714a0e6850fSThomas Cort 					waddch(form->scrwin, ' ');
1715a0e6850fSThomas Cort 			}
1716a0e6850fSThomas Cort 
1717a0e6850fSThomas Cort 			  /*
1718a0e6850fSThomas Cort 			   * If we have had a tab then skip forward
1719a0e6850fSThomas Cort 			   * the requisite number of chars to keep
1720a0e6850fSThomas Cort 			   * things in sync.
1721a0e6850fSThomas Cort 			   */
1722a0e6850fSThomas Cort 			if (c == '\t')
1723a0e6850fSThomas Cort 				i += tab - 1;
1724a0e6850fSThomas Cort 		}
1725a0e6850fSThomas Cort 
1726a0e6850fSThomas Cort 		for (i = 0; i < post; i++)
1727a0e6850fSThomas Cort 			waddch(form->scrwin, cur->pad);
1728a0e6850fSThomas Cort 	}
1729a0e6850fSThomas Cort 
1730a0e6850fSThomas Cort 	for (i = line; i < cur->rows; i++) {
1731a0e6850fSThomas Cort 		wmove(form->scrwin, (int) (cur->form_row + i),
1732a0e6850fSThomas Cort 		      (int) cur->form_col);
1733a0e6850fSThomas Cort 
1734a0e6850fSThomas Cort 		if (form->cur_field == field)
1735a0e6850fSThomas Cort 			wattrset(form->scrwin, cur->fore);
1736a0e6850fSThomas Cort 		else
1737a0e6850fSThomas Cort 			wattrset(form->scrwin, cur->back);
1738a0e6850fSThomas Cort 
1739a0e6850fSThomas Cort 		for (j = 0; j < cur->cols; j++) {
1740a0e6850fSThomas Cort 			waddch(form->scrwin, cur->pad);
1741a0e6850fSThomas Cort 		}
1742a0e6850fSThomas Cort 	}
1743a0e6850fSThomas Cort 
1744a0e6850fSThomas Cort 	wattrset(form->scrwin, cur->back);
1745a0e6850fSThomas Cort 	return;
1746a0e6850fSThomas Cort }
1747a0e6850fSThomas Cort 
1748a0e6850fSThomas Cort /*
1749a0e6850fSThomas Cort  * Add the correct number of the given character to simulate a tab
1750a0e6850fSThomas Cort  * in the field.
1751a0e6850fSThomas Cort  */
1752a0e6850fSThomas Cort static int
add_tab(FORM * form,_FORMI_FIELD_LINES * row,unsigned int i,char c)1753a0e6850fSThomas Cort add_tab(FORM *form, _FORMI_FIELD_LINES *row, unsigned int i, char c)
1754a0e6850fSThomas Cort {
1755a0e6850fSThomas Cort 	int j;
1756a0e6850fSThomas Cort 	_formi_tab_t *ts = row->tabs;
1757a0e6850fSThomas Cort 
1758a0e6850fSThomas Cort 	while ((ts != NULL) && (ts->pos != i))
1759a0e6850fSThomas Cort 		ts = ts->fwd;
1760a0e6850fSThomas Cort 
1761a0e6850fSThomas Cort 	assert(ts != NULL);
1762a0e6850fSThomas Cort 
1763a0e6850fSThomas Cort 	for (j = 0; j < ts->size; j++)
1764a0e6850fSThomas Cort 		waddch(form->scrwin, c);
1765a0e6850fSThomas Cort 
1766a0e6850fSThomas Cort 	return ts->size;
1767a0e6850fSThomas Cort }
1768a0e6850fSThomas Cort 
1769a0e6850fSThomas Cort 
1770a0e6850fSThomas Cort /*
1771a0e6850fSThomas Cort  * Display the fields attached to the form that are on the current page
1772a0e6850fSThomas Cort  * on the screen.
1773a0e6850fSThomas Cort  *
1774a0e6850fSThomas Cort  */
1775a0e6850fSThomas Cort int
_formi_draw_page(FORM * form)1776a0e6850fSThomas Cort _formi_draw_page(FORM *form)
1777a0e6850fSThomas Cort {
1778a0e6850fSThomas Cort 	int i;
1779a0e6850fSThomas Cort 
1780a0e6850fSThomas Cort 	if (form->page_starts[form->page].in_use == 0)
1781a0e6850fSThomas Cort 		return E_BAD_ARGUMENT;
1782a0e6850fSThomas Cort 
1783a0e6850fSThomas Cort 	wclear(form->scrwin);
1784a0e6850fSThomas Cort 
1785a0e6850fSThomas Cort 	for (i = form->page_starts[form->page].first;
1786a0e6850fSThomas Cort 	     i <= form->page_starts[form->page].last; i++)
1787a0e6850fSThomas Cort 		_formi_redraw_field(form, i);
1788a0e6850fSThomas Cort 
1789a0e6850fSThomas Cort 	return E_OK;
1790a0e6850fSThomas Cort }
1791a0e6850fSThomas Cort 
1792a0e6850fSThomas Cort /*
1793a0e6850fSThomas Cort  * Add the character c at the position pos in buffer 0 of the given field
1794a0e6850fSThomas Cort  */
1795a0e6850fSThomas Cort int
_formi_add_char(FIELD * field,unsigned int pos,char c)1796a0e6850fSThomas Cort _formi_add_char(FIELD *field, unsigned int pos, char c)
1797a0e6850fSThomas Cort {
1798a0e6850fSThomas Cort 	char *new, old_c;
1799a0e6850fSThomas Cort 	unsigned int new_size;
1800a0e6850fSThomas Cort 	int status;
1801a0e6850fSThomas Cort 	_FORMI_FIELD_LINES *row, *temp, *next_temp;
1802a0e6850fSThomas Cort 
1803a0e6850fSThomas Cort 	row = field->cur_line;
1804a0e6850fSThomas Cort 
1805a0e6850fSThomas Cort 	  /*
1806a0e6850fSThomas Cort 	   * If buffer has not had a string before, set it to a blank
1807a0e6850fSThomas Cort 	   * string.  Everything should flow from there....
1808a0e6850fSThomas Cort 	   */
1809a0e6850fSThomas Cort 	if (row->string == NULL) {
1810a0e6850fSThomas Cort 		if ((row->string = (char *) malloc((size_t)INITIAL_LINE_ALLOC))
1811a0e6850fSThomas Cort 		    == NULL)
1812a0e6850fSThomas Cort 			return E_SYSTEM_ERROR;
1813a0e6850fSThomas Cort 		row->string[0] = '\0';
1814a0e6850fSThomas Cort 		row->allocated = INITIAL_LINE_ALLOC;
1815a0e6850fSThomas Cort 		row->length = 0;
1816a0e6850fSThomas Cort 		row->expanded = 0;
1817a0e6850fSThomas Cort 	}
1818a0e6850fSThomas Cort 
1819a0e6850fSThomas Cort 	if (_formi_validate_char(field, c) != E_OK) {
1820a0e6850fSThomas Cort #ifdef DEBUG
1821a0e6850fSThomas Cort 		fprintf(dbg, "add_char: char %c failed char validation\n", c);
1822a0e6850fSThomas Cort #endif
1823a0e6850fSThomas Cort 		return E_INVALID_FIELD;
1824a0e6850fSThomas Cort 	}
1825a0e6850fSThomas Cort 
1826a0e6850fSThomas Cort 	if ((c == '\t') && (field->cols <= 8)) {
1827a0e6850fSThomas Cort #ifdef DEBUG
1828a0e6850fSThomas Cort 		fprintf(dbg, "add_char: field too small for a tab\n");
1829a0e6850fSThomas Cort #endif
1830a0e6850fSThomas Cort 		return E_NO_ROOM;
1831a0e6850fSThomas Cort 	}
1832a0e6850fSThomas Cort 
1833a0e6850fSThomas Cort #ifdef DEBUG
1834a0e6850fSThomas Cort 	fprintf(dbg, "add_char: pos=%d, char=%c\n", pos, c);
1835a0e6850fSThomas Cort 	fprintf(dbg, "add_char enter: xpos=%d, row_pos=%d, start=%d\n",
1836a0e6850fSThomas Cort 		field->cursor_xpos, field->row_xpos, field->start_char);
1837a0e6850fSThomas Cort 	fprintf(dbg, "add_char enter: length=%d(%d), allocated=%d\n",
1838a0e6850fSThomas Cort 		row->expanded, row->length, row->allocated);
1839a0e6850fSThomas Cort 	fprintf(dbg, "add_char enter: %s\n", row->string);
1840a0e6850fSThomas Cort 	fprintf(dbg, "add_char enter: buf0_status=%d\n", field->buf0_status);
1841a0e6850fSThomas Cort #endif
1842a0e6850fSThomas Cort 	if (((field->opts & O_BLANK) == O_BLANK) &&
1843a0e6850fSThomas Cort 	    (field->buf0_status == FALSE) &&
1844a0e6850fSThomas Cort 	    ((field->row_xpos + field->start_char) == 0)) {
1845a0e6850fSThomas Cort 		row = field->alines;
1846a0e6850fSThomas Cort 		if (row->next != NULL) {
1847a0e6850fSThomas Cort 			  /* shift all but one line structs to free list */
1848a0e6850fSThomas Cort 			temp = row->next;
1849a0e6850fSThomas Cort 			do {
1850a0e6850fSThomas Cort 				next_temp = temp->next;
1851a0e6850fSThomas Cort 				add_to_free(field, temp);
1852a0e6850fSThomas Cort 				temp = next_temp;
1853a0e6850fSThomas Cort 			} while (temp != NULL);
1854a0e6850fSThomas Cort 		}
1855a0e6850fSThomas Cort 
1856a0e6850fSThomas Cort 		row->length = 0;
1857a0e6850fSThomas Cort 		row->string[0] = '\0';
1858a0e6850fSThomas Cort 		pos = 0;
1859a0e6850fSThomas Cort 		field->start_char = 0;
1860a0e6850fSThomas Cort 		field->start_line = row;
1861a0e6850fSThomas Cort 		field->cur_line = row;
1862a0e6850fSThomas Cort 		field->row_count = 1;
1863a0e6850fSThomas Cort 		field->row_xpos = 0;
1864a0e6850fSThomas Cort 		field->cursor_ypos = 0;
1865a0e6850fSThomas Cort 		row->expanded = 0;
1866a0e6850fSThomas Cort 		row->length = 0;
1867a0e6850fSThomas Cort 		_formi_init_field_xpos(field);
1868a0e6850fSThomas Cort 	}
1869a0e6850fSThomas Cort 
1870a0e6850fSThomas Cort 
1871a0e6850fSThomas Cort 	if ((field->overlay == 0)
1872a0e6850fSThomas Cort 	    || ((field->overlay == 1) && (pos >= row->length))) {
1873a0e6850fSThomas Cort 		  /* first check if the field can have more chars...*/
1874a0e6850fSThomas Cort 		if (check_field_size(field) == FALSE)
1875a0e6850fSThomas Cort 			return E_REQUEST_DENIED;
1876a0e6850fSThomas Cort 
1877a0e6850fSThomas Cort 		if (row->length + 2
1878a0e6850fSThomas Cort 		    >= row->allocated) {
1879a0e6850fSThomas Cort 			new_size = row->allocated + 16 - (row->allocated % 16);
1880a0e6850fSThomas Cort 			if ((new = (char *) realloc(row->string,
1881a0e6850fSThomas Cort 						  (size_t) new_size )) == NULL)
1882a0e6850fSThomas Cort 				return E_SYSTEM_ERROR;
1883a0e6850fSThomas Cort 			row->allocated = new_size;
1884a0e6850fSThomas Cort 			row->string = new;
1885a0e6850fSThomas Cort 		}
1886a0e6850fSThomas Cort 	}
1887a0e6850fSThomas Cort 
1888a0e6850fSThomas Cort 	if ((field->overlay == 0) && (row->length > pos)) {
1889a0e6850fSThomas Cort 		bcopy(&row->string[pos], &row->string[pos + 1],
1890a0e6850fSThomas Cort 		      (size_t) (row->length - pos + 1));
1891a0e6850fSThomas Cort 	}
1892a0e6850fSThomas Cort 
1893a0e6850fSThomas Cort 	old_c = row->string[pos];
1894a0e6850fSThomas Cort 	row->string[pos] = c;
1895a0e6850fSThomas Cort 	if (pos >= row->length) {
1896a0e6850fSThomas Cort 		  /* make sure the string is terminated if we are at the
1897a0e6850fSThomas Cort 		   * end of the string, the terminator would be missing
1898a0e6850fSThomas Cort 		   * if we are are at the end of the field.
1899a0e6850fSThomas Cort 		   */
1900a0e6850fSThomas Cort 		row->string[pos + 1] = '\0';
1901a0e6850fSThomas Cort 	}
1902a0e6850fSThomas Cort 
1903a0e6850fSThomas Cort 	  /* only increment the length if we are inserting characters
1904a0e6850fSThomas Cort 	   * OR if we are at the end of the field in overlay mode.
1905a0e6850fSThomas Cort 	   */
1906a0e6850fSThomas Cort 	if ((field->overlay == 0)
1907a0e6850fSThomas Cort 	    || ((field->overlay == 1) && (pos >= row->length))) {
1908a0e6850fSThomas Cort 		row->length++;
1909a0e6850fSThomas Cort 	}
1910a0e6850fSThomas Cort 
1911a0e6850fSThomas Cort 	_formi_calculate_tabs(row);
1912a0e6850fSThomas Cort 	row->expanded = _formi_tab_expanded_length(row->string, 0,
1913a0e6850fSThomas Cort 						   row->length - 1);
1914a0e6850fSThomas Cort 
1915a0e6850fSThomas Cort 	  /* wrap the field, if needed */
1916a0e6850fSThomas Cort 	status = _formi_wrap_field(field, row);
1917a0e6850fSThomas Cort 
1918a0e6850fSThomas Cort 	row = field->cur_line;
1919a0e6850fSThomas Cort 	pos = field->row_xpos;
1920a0e6850fSThomas Cort 
1921a0e6850fSThomas Cort 	  /*
1922a0e6850fSThomas Cort 	   * check the wrap worked or that we have not exceeded the
1923a0e6850fSThomas Cort 	   * max field size - this can happen if the field is re-wrapped
1924a0e6850fSThomas Cort 	   * and the row count is increased past the set limit.
1925a0e6850fSThomas Cort 	   */
1926a0e6850fSThomas Cort 	if ((status != E_OK) || (check_field_size(field) == FALSE)) {
1927a0e6850fSThomas Cort 		if ((field->overlay == 0)
1928a0e6850fSThomas Cort 		    || ((field->overlay == 1)
1929a0e6850fSThomas Cort 			&& (pos >= (row->length - 1) /*XXXX- append check???*/))) {
1930a0e6850fSThomas Cort 			  /*
1931a0e6850fSThomas Cort 			   * wrap failed for some reason, back out the
1932a0e6850fSThomas Cort 			   * char insert
1933a0e6850fSThomas Cort 			   */
1934a0e6850fSThomas Cort 			bcopy(&row->string[pos + 1], &row->string[pos],
1935a0e6850fSThomas Cort 			      (size_t) (row->length - pos));
1936a0e6850fSThomas Cort 			row->length--;
1937a0e6850fSThomas Cort 			if (pos > 0)
1938a0e6850fSThomas Cort 				pos--;
1939a0e6850fSThomas Cort 		} else if (field->overlay == 1) {
1940a0e6850fSThomas Cort 			  /* back out character overlay */
1941a0e6850fSThomas Cort 			row->string[pos] = old_c;
1942a0e6850fSThomas Cort 		}
1943a0e6850fSThomas Cort 
1944a0e6850fSThomas Cort 		_formi_calculate_tabs(row);
1945a0e6850fSThomas Cort 
1946a0e6850fSThomas Cort 		_formi_wrap_field(field, row);
1947a0e6850fSThomas Cort 		  /*
1948a0e6850fSThomas Cort 		   * If we are here then either the status is bad or we
1949a0e6850fSThomas Cort 		   * simply ran out of room.  If the status is E_OK then
1950a0e6850fSThomas Cort 		   * we ran out of room, let the form driver know this.
1951a0e6850fSThomas Cort 		   */
1952a0e6850fSThomas Cort 		if (status == E_OK)
1953a0e6850fSThomas Cort 			status = E_REQUEST_DENIED;
1954a0e6850fSThomas Cort 
1955a0e6850fSThomas Cort 	} else {
1956a0e6850fSThomas Cort 		field->buf0_status = TRUE;
1957a0e6850fSThomas Cort 		field->row_xpos++;
1958a0e6850fSThomas Cort 		if ((field->rows + field->nrows) == 1) {
1959a0e6850fSThomas Cort 			status = _formi_set_cursor_xpos(field, FALSE);
1960a0e6850fSThomas Cort 		} else {
1961a0e6850fSThomas Cort 			field->cursor_xpos =
1962a0e6850fSThomas Cort 				_formi_tab_expanded_length(
1963a0e6850fSThomas Cort 					row->string, 0,	field->row_xpos - 1);
1964a0e6850fSThomas Cort 
1965a0e6850fSThomas Cort 			  /*
1966a0e6850fSThomas Cort 			   * Annoying corner case - if we are right in
1967a0e6850fSThomas Cort 			   * the bottom right corner of the field we
1968a0e6850fSThomas Cort 			   * need to scroll the field one line so the
1969a0e6850fSThomas Cort 			   * cursor is positioned correctly in the
1970a0e6850fSThomas Cort 			   * field.
1971a0e6850fSThomas Cort 			   */
1972a0e6850fSThomas Cort 			if ((field->cursor_xpos >= field->cols) &&
1973a0e6850fSThomas Cort 			    (field->cursor_ypos == (field->rows - 1))) {
1974a0e6850fSThomas Cort 				field->cursor_ypos--;
1975a0e6850fSThomas Cort 				field->start_line = field->start_line->next;
1976a0e6850fSThomas Cort 			}
1977a0e6850fSThomas Cort 		}
1978a0e6850fSThomas Cort 	}
1979a0e6850fSThomas Cort 
1980a0e6850fSThomas Cort 	assert((field->cursor_xpos <= field->cols)
1981a0e6850fSThomas Cort 	       && (field->cursor_ypos < 400000));
1982a0e6850fSThomas Cort 
1983a0e6850fSThomas Cort #ifdef DEBUG
1984a0e6850fSThomas Cort 	fprintf(dbg, "add_char exit: xpos=%d, row_pos=%d, start=%d\n",
1985a0e6850fSThomas Cort 		field->cursor_xpos, field->row_xpos, field->start_char);
1986a0e6850fSThomas Cort 	fprintf(dbg, "add_char_exit: length=%d(%d), allocated=%d\n",
1987a0e6850fSThomas Cort 		row->expanded, row->length, row->allocated);
1988a0e6850fSThomas Cort 	fprintf(dbg, "add_char exit: ypos=%d, start_line=%p\n",
1989a0e6850fSThomas Cort 		field->cursor_ypos, field->start_line);
1990a0e6850fSThomas Cort 	fprintf(dbg,"add_char exit: %s\n", row->string);
1991a0e6850fSThomas Cort 	fprintf(dbg, "add_char exit: buf0_status=%d\n", field->buf0_status);
1992a0e6850fSThomas Cort 	fprintf(dbg, "add_char exit: status = %s\n",
1993a0e6850fSThomas Cort 		(status == E_OK)? "OK" : "FAILED");
1994a0e6850fSThomas Cort #endif
1995a0e6850fSThomas Cort 	return status;
1996a0e6850fSThomas Cort }
1997a0e6850fSThomas Cort 
1998a0e6850fSThomas Cort /*
1999a0e6850fSThomas Cort  * Set the position of the cursor on the screen in the row depending on
2000a0e6850fSThomas Cort  * where the current position in the string is and the justification
2001a0e6850fSThomas Cort  * that is to be applied to the field.  Justification is only applied
2002a0e6850fSThomas Cort  * to single row, static fields.
2003a0e6850fSThomas Cort  */
2004a0e6850fSThomas Cort static int
_formi_set_cursor_xpos(FIELD * field,int noscroll)2005a0e6850fSThomas Cort _formi_set_cursor_xpos(FIELD *field, int noscroll)
2006a0e6850fSThomas Cort {
2007a0e6850fSThomas Cort 	int just, pos;
2008a0e6850fSThomas Cort 
2009a0e6850fSThomas Cort 	just = field->justification;
2010a0e6850fSThomas Cort 	pos = field->start_char + field->row_xpos;
2011a0e6850fSThomas Cort 
2012a0e6850fSThomas Cort #ifdef DEBUG
2013a0e6850fSThomas Cort 	fprintf(dbg,
2014a0e6850fSThomas Cort 	  "cursor_xpos enter: pos %d, start_char %d, row_xpos %d, xpos %d\n",
2015a0e6850fSThomas Cort 		pos, field->start_char, field->row_xpos, field->cursor_xpos);
2016a0e6850fSThomas Cort #endif
2017a0e6850fSThomas Cort 
2018a0e6850fSThomas Cort 	  /*
2019a0e6850fSThomas Cort 	   * make sure we apply the correct justification to non-static
2020a0e6850fSThomas Cort 	   * fields.
2021a0e6850fSThomas Cort 	   */
2022a0e6850fSThomas Cort 	if (((field->rows + field->nrows) != 1) ||
2023a0e6850fSThomas Cort 	    ((field->opts & O_STATIC) != O_STATIC))
2024a0e6850fSThomas Cort 		just = JUSTIFY_LEFT;
2025a0e6850fSThomas Cort 
2026a0e6850fSThomas Cort 	switch (just) {
2027a0e6850fSThomas Cort 	case JUSTIFY_RIGHT:
2028a0e6850fSThomas Cort 		field->cursor_xpos = field->cols - 1
2029a0e6850fSThomas Cort 			- _formi_tab_expanded_length(
2030a0e6850fSThomas Cort 				field->cur_line->string, 0,
2031a0e6850fSThomas Cort 				field->cur_line->length - 1)
2032a0e6850fSThomas Cort 			+ _formi_tab_expanded_length(
2033a0e6850fSThomas Cort 				field->cur_line->string, 0,
2034a0e6850fSThomas Cort 				field->row_xpos);
2035a0e6850fSThomas Cort 		break;
2036a0e6850fSThomas Cort 
2037a0e6850fSThomas Cort 	case JUSTIFY_CENTER:
2038a0e6850fSThomas Cort 		field->cursor_xpos = ((field->cols - 1)
2039a0e6850fSThomas Cort 			- _formi_tab_expanded_length(
2040a0e6850fSThomas Cort 				field->cur_line->string, 0,
2041a0e6850fSThomas Cort 				field->cur_line->length - 1) + 1) / 2
2042a0e6850fSThomas Cort 			+ _formi_tab_expanded_length(field->cur_line->string,
2043a0e6850fSThomas Cort 						     0, field->row_xpos);
2044a0e6850fSThomas Cort 
2045a0e6850fSThomas Cort 		if (field->cursor_xpos > (field->cols - 1))
2046a0e6850fSThomas Cort 			field->cursor_xpos = (field->cols - 1);
2047a0e6850fSThomas Cort 		break;
2048a0e6850fSThomas Cort 
2049a0e6850fSThomas Cort 	default:
2050a0e6850fSThomas Cort 		field->cursor_xpos = _formi_tab_expanded_length(
2051a0e6850fSThomas Cort 			field->cur_line->string,
2052a0e6850fSThomas Cort 			field->start_char,
2053a0e6850fSThomas Cort 			field->row_xpos	+ field->start_char);
2054a0e6850fSThomas Cort 		if ((field->cursor_xpos <= (field->cols - 1)) &&
2055a0e6850fSThomas Cort 		    ((field->start_char + field->row_xpos)
2056a0e6850fSThomas Cort 		     < field->cur_line->length))
2057a0e6850fSThomas Cort 			field->cursor_xpos--;
2058a0e6850fSThomas Cort 
2059a0e6850fSThomas Cort 		if (field->cursor_xpos > (field->cols - 1)) {
2060a0e6850fSThomas Cort 			if ((field->opts & O_STATIC) == O_STATIC) {
2061a0e6850fSThomas Cort 				field->start_char = 0;
2062a0e6850fSThomas Cort 
2063a0e6850fSThomas Cort 				if (field->row_xpos
2064a0e6850fSThomas Cort 				    == (field->cur_line->length - 1)) {
2065a0e6850fSThomas Cort 					field->cursor_xpos = field->cols - 1;
2066a0e6850fSThomas Cort 				} else {
2067a0e6850fSThomas Cort 					field->cursor_xpos =
2068a0e6850fSThomas Cort 						_formi_tab_expanded_length(
2069a0e6850fSThomas Cort 						  field->cur_line->string,
2070a0e6850fSThomas Cort 						  field->start_char,
2071a0e6850fSThomas Cort 						  field->row_xpos
2072a0e6850fSThomas Cort 						    + field->start_char
2073a0e6850fSThomas Cort 						  - 1) - 1;
2074a0e6850fSThomas Cort 				}
2075a0e6850fSThomas Cort 			} else {
2076a0e6850fSThomas Cort 				if (noscroll == FALSE) {
2077a0e6850fSThomas Cort 					field->start_char =
2078a0e6850fSThomas Cort 						tab_fit_window(
2079a0e6850fSThomas Cort 							field,
2080a0e6850fSThomas Cort 							field->start_char
2081a0e6850fSThomas Cort 							+ field->row_xpos,
2082a0e6850fSThomas Cort 							field->cols);
2083a0e6850fSThomas Cort 					field->row_xpos = pos
2084a0e6850fSThomas Cort 						- field->start_char;
2085a0e6850fSThomas Cort 					field->cursor_xpos =
2086a0e6850fSThomas Cort 						_formi_tab_expanded_length(
2087a0e6850fSThomas Cort 						   field->cur_line->string,
2088a0e6850fSThomas Cort 						   field->start_char,
2089a0e6850fSThomas Cort 						   field->row_xpos
2090a0e6850fSThomas Cort 						      + field->start_char - 1);
2091a0e6850fSThomas Cort 				} else {
2092a0e6850fSThomas Cort 					field->cursor_xpos = (field->cols - 1);
2093a0e6850fSThomas Cort 				}
2094a0e6850fSThomas Cort 			}
2095a0e6850fSThomas Cort 
2096a0e6850fSThomas Cort 		}
2097a0e6850fSThomas Cort 		break;
2098a0e6850fSThomas Cort 	}
2099a0e6850fSThomas Cort 
2100a0e6850fSThomas Cort #ifdef DEBUG
2101a0e6850fSThomas Cort 	fprintf(dbg,
2102a0e6850fSThomas Cort 	  "cursor_xpos exit: pos %d, start_char %d, row_xpos %d, xpos %d\n",
2103a0e6850fSThomas Cort 		pos, field->start_char, field->row_xpos, field->cursor_xpos);
2104a0e6850fSThomas Cort #endif
2105a0e6850fSThomas Cort 	return E_OK;
2106a0e6850fSThomas Cort }
2107a0e6850fSThomas Cort 
2108a0e6850fSThomas Cort /*
2109a0e6850fSThomas Cort  * Manipulate the text in a field, this takes the given form and performs
2110a0e6850fSThomas Cort  * the passed driver command on the current text field.  Returns 1 if the
2111a0e6850fSThomas Cort  * text field was modified.
2112a0e6850fSThomas Cort  */
2113a0e6850fSThomas Cort int
_formi_manipulate_field(FORM * form,int c)2114a0e6850fSThomas Cort _formi_manipulate_field(FORM *form, int c)
2115a0e6850fSThomas Cort {
2116a0e6850fSThomas Cort 	FIELD *cur;
2117a0e6850fSThomas Cort 	char *str, saved;
2118a0e6850fSThomas Cort 	unsigned int start, end, pos, status, old_count, size;
2119a0e6850fSThomas Cort 	unsigned int old_xpos, old_row_pos;
2120a0e6850fSThomas Cort 	int len, wb;
2121a0e6850fSThomas Cort 	bool eat_char;
2122a0e6850fSThomas Cort 	_FORMI_FIELD_LINES *row, *rs;
2123a0e6850fSThomas Cort 
2124a0e6850fSThomas Cort 	cur = form->fields[form->cur_field];
2125a0e6850fSThomas Cort 	if (cur->cur_line->string == NULL)
2126a0e6850fSThomas Cort 		return E_REQUEST_DENIED;
2127a0e6850fSThomas Cort 
2128a0e6850fSThomas Cort #ifdef DEBUG
2129a0e6850fSThomas Cort 	fprintf(dbg, "entry: request is REQ_%s\n", reqs[c - REQ_MIN_REQUEST]);
2130a0e6850fSThomas Cort 	fprintf(dbg,
2131a0e6850fSThomas Cort 	"entry: xpos=%d, row_pos=%d, start_char=%d, length=%d, allocated=%d\n",
2132a0e6850fSThomas Cort 		cur->cursor_xpos, cur->row_xpos, cur->start_char,
2133a0e6850fSThomas Cort 		cur->cur_line->length,	cur->cur_line->allocated);
2134a0e6850fSThomas Cort 	fprintf(dbg, "entry: start_line=%p, ypos=%d\n", cur->start_line,
2135a0e6850fSThomas Cort 		cur->cursor_ypos);
2136a0e6850fSThomas Cort 	fprintf(dbg, "entry: string=");
2137a0e6850fSThomas Cort 	if (cur->cur_line->string == NULL)
2138a0e6850fSThomas Cort 		fprintf(dbg, "(null)\n");
2139a0e6850fSThomas Cort 	else
2140a0e6850fSThomas Cort 		fprintf(dbg, "\"%s\"\n", cur->cur_line->string);
2141a0e6850fSThomas Cort #endif
2142a0e6850fSThomas Cort 
2143a0e6850fSThomas Cort 	  /* Cannot manipulate a null string! */
2144a0e6850fSThomas Cort 	if (cur->cur_line->string == NULL)
2145a0e6850fSThomas Cort 		return E_REQUEST_DENIED;
2146a0e6850fSThomas Cort 
2147a0e6850fSThomas Cort 	saved = '\0';
2148a0e6850fSThomas Cort 	row = cur->cur_line;
2149a0e6850fSThomas Cort 
2150a0e6850fSThomas Cort 	switch (c) {
2151a0e6850fSThomas Cort 	case REQ_RIGHT_CHAR:
2152a0e6850fSThomas Cort 		  /*
2153a0e6850fSThomas Cort 		   * The right_char request performs the same function
2154a0e6850fSThomas Cort 		   * as the next_char request except that the cursor is
2155a0e6850fSThomas Cort 		   * not wrapped if it is at the end of the line, so
2156a0e6850fSThomas Cort 		   * check if the cursor is at the end of the line and
2157a0e6850fSThomas Cort 		   * deny the request otherwise just fall through to
2158a0e6850fSThomas Cort 		   * the next_char request handler.
2159a0e6850fSThomas Cort 		   */
2160a0e6850fSThomas Cort 		if (cur->cursor_xpos >= cur->cols - 1)
2161a0e6850fSThomas Cort 			return E_REQUEST_DENIED;
2162a0e6850fSThomas Cort 
2163a0e6850fSThomas Cort 		  /* FALLTHRU */
2164a0e6850fSThomas Cort 
2165a0e6850fSThomas Cort 	case REQ_NEXT_CHAR:
2166a0e6850fSThomas Cort 		  /* for a dynamic field allow an offset of one more
2167a0e6850fSThomas Cort 		   * char so we can insert chars after end of string.
2168a0e6850fSThomas Cort 		   * Static fields cannot do this so deny request if
2169a0e6850fSThomas Cort 		   * cursor is at the end of the field.
2170a0e6850fSThomas Cort 		   */
2171a0e6850fSThomas Cort 		if (((cur->opts & O_STATIC) == O_STATIC) &&
2172a0e6850fSThomas Cort 		    (cur->row_xpos == cur->cols - 1) &&
2173a0e6850fSThomas Cort 		    ((cur->rows + cur->nrows) == 1))
2174a0e6850fSThomas Cort 			return E_REQUEST_DENIED;
2175a0e6850fSThomas Cort 
2176a0e6850fSThomas Cort 		if (((cur->rows + cur->nrows) == 1) &&
2177a0e6850fSThomas Cort 		    (cur->row_xpos + cur->start_char + 1) > row->length)
2178a0e6850fSThomas Cort 			return E_REQUEST_DENIED;
2179a0e6850fSThomas Cort 
2180a0e6850fSThomas Cort 		if ((cur->rows + cur->nrows) == 1) {
2181a0e6850fSThomas Cort 			cur->row_xpos++;
2182a0e6850fSThomas Cort 			_formi_set_cursor_xpos(cur, (c == REQ_RIGHT_CHAR));
2183a0e6850fSThomas Cort 		} else {
2184a0e6850fSThomas Cort 			if (cur->cursor_xpos >= (row->expanded - 1)) {
2185a0e6850fSThomas Cort 				if ((row->next == NULL) ||
2186a0e6850fSThomas Cort 				    (c == REQ_RIGHT_CHAR))
2187a0e6850fSThomas Cort 					return E_REQUEST_DENIED;
2188a0e6850fSThomas Cort 
2189a0e6850fSThomas Cort 				cur->cursor_xpos = 0;
2190a0e6850fSThomas Cort 				cur->row_xpos = 0;
2191a0e6850fSThomas Cort 				cur->cur_line = cur->cur_line->next;
2192a0e6850fSThomas Cort 				if (cur->cursor_ypos == (cur->rows - 1))
2193a0e6850fSThomas Cort 					cur->start_line =
2194a0e6850fSThomas Cort 						cur->start_line->next;
2195a0e6850fSThomas Cort 				else
2196a0e6850fSThomas Cort 					cur->cursor_ypos++;
2197a0e6850fSThomas Cort 			} else {
2198a0e6850fSThomas Cort 				old_xpos = cur->cursor_xpos;
2199a0e6850fSThomas Cort 				old_row_pos = cur->row_xpos;
2200a0e6850fSThomas Cort 				if (row->string[cur->row_xpos] == '\t')
2201a0e6850fSThomas Cort 					cur->cursor_xpos += tab_size(row,
2202a0e6850fSThomas Cort 							cur->row_xpos);
2203a0e6850fSThomas Cort 				else
2204a0e6850fSThomas Cort 					cur->cursor_xpos++;
2205a0e6850fSThomas Cort 				cur->row_xpos++;
2206a0e6850fSThomas Cort 				if (cur->cursor_xpos
2207a0e6850fSThomas Cort 				    >= row->expanded) {
2208a0e6850fSThomas Cort 					if ((row->next == NULL) ||
2209a0e6850fSThomas Cort 					    (c == REQ_RIGHT_CHAR)) {
2210a0e6850fSThomas Cort 						cur->cursor_xpos = old_xpos;
2211a0e6850fSThomas Cort 						cur->row_xpos = old_row_pos;
2212a0e6850fSThomas Cort 						return E_REQUEST_DENIED;
2213a0e6850fSThomas Cort 					}
2214a0e6850fSThomas Cort 
2215a0e6850fSThomas Cort 					cur->cursor_xpos = 0;
2216a0e6850fSThomas Cort 					cur->row_xpos = 0;
2217a0e6850fSThomas Cort 					cur->cur_line = cur->cur_line->next;
2218a0e6850fSThomas Cort 					if (cur->cursor_ypos
2219a0e6850fSThomas Cort 					    == (cur->rows - 1))
2220a0e6850fSThomas Cort 						cur->start_line =
2221a0e6850fSThomas Cort 							cur->start_line->next;
2222a0e6850fSThomas Cort 					else
2223a0e6850fSThomas Cort 						cur->cursor_ypos++;
2224a0e6850fSThomas Cort 				}
2225a0e6850fSThomas Cort 			}
2226a0e6850fSThomas Cort 		}
2227a0e6850fSThomas Cort 
2228a0e6850fSThomas Cort 		break;
2229a0e6850fSThomas Cort 
2230a0e6850fSThomas Cort 	case REQ_LEFT_CHAR:
2231a0e6850fSThomas Cort 		  /*
2232a0e6850fSThomas Cort 		   * The behaviour of left_char is the same as prev_char
2233a0e6850fSThomas Cort 		   * except that the cursor will not wrap if it has
2234a0e6850fSThomas Cort 		   * reached the LHS of the field, so just check this
2235a0e6850fSThomas Cort 		   * and fall through if we are not at the LHS.
2236a0e6850fSThomas Cort 		   */
2237a0e6850fSThomas Cort 		if (cur->cursor_xpos == 0)
2238a0e6850fSThomas Cort 			return E_REQUEST_DENIED;
2239a0e6850fSThomas Cort 
2240a0e6850fSThomas Cort 		  /* FALLTHRU */
2241a0e6850fSThomas Cort 	case REQ_PREV_CHAR:
2242a0e6850fSThomas Cort 		if ((cur->rows + cur->nrows) == 1) {
2243a0e6850fSThomas Cort 			if (cur->row_xpos == 0) {
2244a0e6850fSThomas Cort 				if (cur->start_char > 0)
2245a0e6850fSThomas Cort 					cur->start_char--;
2246a0e6850fSThomas Cort 				else
2247a0e6850fSThomas Cort 					return E_REQUEST_DENIED;
2248a0e6850fSThomas Cort 			} else {
2249a0e6850fSThomas Cort 				cur->row_xpos--;
2250a0e6850fSThomas Cort 				_formi_set_cursor_xpos(cur, FALSE);
2251a0e6850fSThomas Cort 			}
2252a0e6850fSThomas Cort 		} else {
2253a0e6850fSThomas Cort 			if ((cur->cursor_xpos == 0) &&
2254a0e6850fSThomas Cort 			    (cur->cursor_ypos == 0) &&
2255a0e6850fSThomas Cort 			    (cur->start_line->prev == NULL))
2256a0e6850fSThomas Cort 				return E_REQUEST_DENIED;
2257a0e6850fSThomas Cort 
2258a0e6850fSThomas Cort 			pos = cur->row_xpos;
2259a0e6850fSThomas Cort 			if (cur->cursor_xpos > 0) {
2260a0e6850fSThomas Cort 				if (row->string[pos] == '\t') {
2261a0e6850fSThomas Cort 					size = tab_size(row, pos);
2262a0e6850fSThomas Cort 					if (size > cur->cursor_xpos) {
2263a0e6850fSThomas Cort 						cur->cursor_xpos = 0;
2264a0e6850fSThomas Cort 						cur->row_xpos = 0;
2265a0e6850fSThomas Cort 					} else {
2266a0e6850fSThomas Cort 						cur->row_xpos--;
2267a0e6850fSThomas Cort 						cur->cursor_xpos -= size;
2268a0e6850fSThomas Cort 					}
2269a0e6850fSThomas Cort 				} else {
2270a0e6850fSThomas Cort 					cur->cursor_xpos--;
2271a0e6850fSThomas Cort 					cur->row_xpos--;
2272a0e6850fSThomas Cort 				}
2273a0e6850fSThomas Cort 			} else {
2274a0e6850fSThomas Cort 				cur->cur_line = cur->cur_line->prev;
2275a0e6850fSThomas Cort 				if (cur->cursor_ypos > 0)
2276a0e6850fSThomas Cort 					cur->cursor_ypos--;
2277a0e6850fSThomas Cort 				else
2278a0e6850fSThomas Cort 					cur->start_line =
2279a0e6850fSThomas Cort 						cur->start_line->prev;
2280a0e6850fSThomas Cort 				row = cur->cur_line;
2281a0e6850fSThomas Cort 				if (row->expanded > 0) {
2282a0e6850fSThomas Cort 					cur->cursor_xpos = row->expanded - 1;
2283a0e6850fSThomas Cort 				} else {
2284a0e6850fSThomas Cort 					cur->cursor_xpos = 0;
2285a0e6850fSThomas Cort 				}
2286a0e6850fSThomas Cort 
2287a0e6850fSThomas Cort 				if (row->length > 0)
2288a0e6850fSThomas Cort 					cur->row_xpos = row->length - 1;
2289a0e6850fSThomas Cort 				else
2290a0e6850fSThomas Cort 					cur->row_xpos = 0;
2291a0e6850fSThomas Cort 			}
2292a0e6850fSThomas Cort 		}
2293a0e6850fSThomas Cort 
2294a0e6850fSThomas Cort 		break;
2295a0e6850fSThomas Cort 
2296a0e6850fSThomas Cort 	case REQ_DOWN_CHAR:
2297a0e6850fSThomas Cort 		  /*
2298a0e6850fSThomas Cort 		   * The down_char request has the same functionality as
2299a0e6850fSThomas Cort 		   * the next_line request excepting that the field is not
2300a0e6850fSThomas Cort 		   * scrolled if the cursor is at the bottom of the field.
2301a0e6850fSThomas Cort 		   * Check to see if the cursor is at the bottom of the field
2302a0e6850fSThomas Cort 		   * and if it is then deny the request otherwise fall
2303a0e6850fSThomas Cort 		   * through to the next_line handler.
2304a0e6850fSThomas Cort 		   */
2305a0e6850fSThomas Cort 		if (cur->cursor_ypos >= cur->rows - 1)
2306a0e6850fSThomas Cort 			return E_REQUEST_DENIED;
2307a0e6850fSThomas Cort 
2308a0e6850fSThomas Cort 		  /* FALLTHRU */
2309a0e6850fSThomas Cort 
2310a0e6850fSThomas Cort 	case REQ_NEXT_LINE:
2311a0e6850fSThomas Cort 		if ((row->next == NULL) || (cur->cur_line->next == NULL))
2312a0e6850fSThomas Cort 			return E_REQUEST_DENIED;
2313a0e6850fSThomas Cort 
2314a0e6850fSThomas Cort 		cur->cur_line = cur->cur_line->next;
2315a0e6850fSThomas Cort 		if ((cur->cursor_ypos + 1) >= cur->rows) {
2316a0e6850fSThomas Cort 			cur->start_line = cur->start_line->next;
2317a0e6850fSThomas Cort 		} else
2318a0e6850fSThomas Cort 			cur->cursor_ypos++;
2319a0e6850fSThomas Cort 		row = cur->cur_line;
2320a0e6850fSThomas Cort 
2321a0e6850fSThomas Cort 		if (row->length == 0) {
2322a0e6850fSThomas Cort 			cur->row_xpos = 0;
2323a0e6850fSThomas Cort 			cur->cursor_xpos = 0;
2324a0e6850fSThomas Cort 		} else {
2325a0e6850fSThomas Cort 			if (cur->cursor_xpos > (row->expanded - 1))
2326a0e6850fSThomas Cort 				cur->cursor_xpos = row->expanded - 1;
2327a0e6850fSThomas Cort 
2328a0e6850fSThomas Cort 			cur->row_xpos =	tab_fit_len(row, cur->cursor_xpos + 1);
2329a0e6850fSThomas Cort 			if (cur->row_xpos == 0)
2330a0e6850fSThomas Cort 				cur->cursor_xpos = 0;
2331a0e6850fSThomas Cort 			else
2332a0e6850fSThomas Cort 				cur->cursor_xpos =
2333a0e6850fSThomas Cort 					_formi_tab_expanded_length(
2334a0e6850fSThomas Cort 						row->string, 0, cur->row_xpos);
2335a0e6850fSThomas Cort 			if (cur->cursor_xpos > 0)
2336a0e6850fSThomas Cort 				cur->cursor_xpos--;
2337a0e6850fSThomas Cort 		}
2338a0e6850fSThomas Cort 		break;
2339a0e6850fSThomas Cort 
2340a0e6850fSThomas Cort 	case REQ_UP_CHAR:
2341a0e6850fSThomas Cort 		  /*
2342a0e6850fSThomas Cort 		   * The up_char request has the same functionality as
2343a0e6850fSThomas Cort 		   * the prev_line request excepting the field is not
2344a0e6850fSThomas Cort 		   * scrolled, check if the cursor is at the top of the
2345a0e6850fSThomas Cort 		   * field, if it is deny the request otherwise fall
2346a0e6850fSThomas Cort 		   * through to the prev_line handler.
2347a0e6850fSThomas Cort 		   */
2348a0e6850fSThomas Cort 		if (cur->cursor_ypos == 0)
2349a0e6850fSThomas Cort 			return E_REQUEST_DENIED;
2350a0e6850fSThomas Cort 
2351a0e6850fSThomas Cort 		  /* FALLTHRU */
2352a0e6850fSThomas Cort 
2353a0e6850fSThomas Cort 	case REQ_PREV_LINE:
2354a0e6850fSThomas Cort 		if (cur->cur_line->prev == NULL)
2355a0e6850fSThomas Cort 			return E_REQUEST_DENIED;
2356a0e6850fSThomas Cort 
2357a0e6850fSThomas Cort 		if (cur->cursor_ypos == 0) {
2358a0e6850fSThomas Cort 			if (cur->start_line->prev == NULL)
2359a0e6850fSThomas Cort 				return E_REQUEST_DENIED;
2360a0e6850fSThomas Cort 			cur->start_line = cur->start_line->prev;
2361a0e6850fSThomas Cort 		} else
2362a0e6850fSThomas Cort 			cur->cursor_ypos--;
2363a0e6850fSThomas Cort 
2364a0e6850fSThomas Cort 		cur->cur_line = cur->cur_line->prev;
2365a0e6850fSThomas Cort 		row = cur->cur_line;
2366a0e6850fSThomas Cort 
2367a0e6850fSThomas Cort 		if (row->length == 0) {
2368a0e6850fSThomas Cort 			cur->row_xpos = 0;
2369a0e6850fSThomas Cort 			cur->cursor_xpos = 0;
2370a0e6850fSThomas Cort 		} else {
2371a0e6850fSThomas Cort 			if (cur->cursor_xpos > (row->expanded - 1))
2372a0e6850fSThomas Cort 				cur->cursor_xpos = row->expanded - 1;
2373a0e6850fSThomas Cort 
2374a0e6850fSThomas Cort 			cur->row_xpos =	tab_fit_len(row, cur->cursor_xpos + 1);
2375a0e6850fSThomas Cort 			cur->cursor_xpos =
2376a0e6850fSThomas Cort 				_formi_tab_expanded_length(row->string,
2377a0e6850fSThomas Cort 							   0, cur->row_xpos);
2378a0e6850fSThomas Cort 			if (cur->cursor_xpos > 0)
2379a0e6850fSThomas Cort 				cur->cursor_xpos--;
2380a0e6850fSThomas Cort 		}
2381a0e6850fSThomas Cort 		break;
2382a0e6850fSThomas Cort 
2383a0e6850fSThomas Cort 	case REQ_NEXT_WORD:
2384a0e6850fSThomas Cort 		start = cur->row_xpos + cur->start_char;
2385a0e6850fSThomas Cort 		str = row->string;
2386a0e6850fSThomas Cort 
2387a0e6850fSThomas Cort 		wb = find_eow(cur, start, FALSE, &row);
2388a0e6850fSThomas Cort 		if (wb < 0)
2389a0e6850fSThomas Cort 			return wb;
2390a0e6850fSThomas Cort 
2391a0e6850fSThomas Cort 		start = wb;
2392a0e6850fSThomas Cort 		  /* check if we hit the end */
2393a0e6850fSThomas Cort 		if (str[start] == '\0')
2394a0e6850fSThomas Cort 			return E_REQUEST_DENIED;
2395a0e6850fSThomas Cort 
2396a0e6850fSThomas Cort 		  /* otherwise we must have found the start of a word...*/
2397a0e6850fSThomas Cort 		if ((cur->rows + cur->nrows) == 1) {
2398a0e6850fSThomas Cort 			  /* single line field */
2399a0e6850fSThomas Cort 			size = _formi_tab_expanded_length(str,
2400a0e6850fSThomas Cort 				cur->start_char, start);
2401a0e6850fSThomas Cort 			if (size < cur->cols) {
2402a0e6850fSThomas Cort 				cur->row_xpos = start - cur->start_char;
2403a0e6850fSThomas Cort 			} else {
2404a0e6850fSThomas Cort 				cur->start_char = start;
2405a0e6850fSThomas Cort 				cur->row_xpos = 0;
2406a0e6850fSThomas Cort 			}
2407a0e6850fSThomas Cort 			_formi_set_cursor_xpos(cur, FALSE);
2408a0e6850fSThomas Cort 		} else {
2409a0e6850fSThomas Cort 			  /* multiline field */
2410a0e6850fSThomas Cort 			cur->cur_line = row;
2411a0e6850fSThomas Cort 			adjust_ypos(cur, row);
2412a0e6850fSThomas Cort 
2413a0e6850fSThomas Cort 			cur->row_xpos = start;
2414a0e6850fSThomas Cort 			cur->cursor_xpos =
2415a0e6850fSThomas Cort 				_formi_tab_expanded_length(
2416a0e6850fSThomas Cort 					row->string, 0, cur->row_xpos) - 1;
2417a0e6850fSThomas Cort 		}
2418a0e6850fSThomas Cort 		break;
2419a0e6850fSThomas Cort 
2420a0e6850fSThomas Cort 	case REQ_PREV_WORD:
2421a0e6850fSThomas Cort 		start = cur->start_char + cur->row_xpos;
2422a0e6850fSThomas Cort 		if (cur->start_char > 0)
2423a0e6850fSThomas Cort 			start--;
2424a0e6850fSThomas Cort 
2425a0e6850fSThomas Cort 		if ((start == 0) && (row->prev == NULL))
2426a0e6850fSThomas Cort 			return E_REQUEST_DENIED;
2427a0e6850fSThomas Cort 
2428a0e6850fSThomas Cort 		if (start == 0) {
2429a0e6850fSThomas Cort 			row = row->prev;
2430a0e6850fSThomas Cort 			if (row->length > 0)
2431a0e6850fSThomas Cort 				start = row->length - 1;
2432a0e6850fSThomas Cort 			else
2433a0e6850fSThomas Cort 				start = 0;
2434a0e6850fSThomas Cort 		}
2435a0e6850fSThomas Cort 
2436a0e6850fSThomas Cort 		str = row->string;
2437a0e6850fSThomas Cort 
2438a0e6850fSThomas Cort 		start = find_sow(start, &row);
2439a0e6850fSThomas Cort 
2440a0e6850fSThomas Cort 		if ((cur->rows + cur->nrows) == 1) {
2441a0e6850fSThomas Cort 			  /* single line field */
2442a0e6850fSThomas Cort 			size = _formi_tab_expanded_length(str,
2443a0e6850fSThomas Cort 				cur->start_char, start);
2444a0e6850fSThomas Cort 
2445a0e6850fSThomas Cort 			if (start > cur->start_char) {
2446a0e6850fSThomas Cort 				cur->row_xpos = start - cur->start_char;
2447a0e6850fSThomas Cort 			} else {
2448a0e6850fSThomas Cort 				cur->start_char = start;
2449a0e6850fSThomas Cort 				cur->row_xpos = 0;
2450a0e6850fSThomas Cort 			}
2451a0e6850fSThomas Cort 			_formi_set_cursor_xpos(cur, FALSE);
2452a0e6850fSThomas Cort 		} else {
2453a0e6850fSThomas Cort 			  /* multiline field */
2454a0e6850fSThomas Cort 			cur->cur_line = row;
2455a0e6850fSThomas Cort 			adjust_ypos(cur, row);
2456a0e6850fSThomas Cort 			cur->row_xpos = start;
2457a0e6850fSThomas Cort 			cur->cursor_xpos =
2458a0e6850fSThomas Cort 				_formi_tab_expanded_length(
2459a0e6850fSThomas Cort 					row->string, 0,
2460a0e6850fSThomas Cort 					cur->row_xpos) - 1;
2461a0e6850fSThomas Cort 		}
2462a0e6850fSThomas Cort 
2463a0e6850fSThomas Cort 		break;
2464a0e6850fSThomas Cort 
2465a0e6850fSThomas Cort 	case REQ_BEG_FIELD:
2466a0e6850fSThomas Cort 		cur->start_char = 0;
2467a0e6850fSThomas Cort 		while (cur->start_line->prev != NULL)
2468a0e6850fSThomas Cort 			cur->start_line = cur->start_line->prev;
2469a0e6850fSThomas Cort 		cur->cur_line = cur->start_line;
2470a0e6850fSThomas Cort 		cur->row_xpos = 0;
2471a0e6850fSThomas Cort 		_formi_init_field_xpos(cur);
2472a0e6850fSThomas Cort 		cur->cursor_ypos = 0;
2473a0e6850fSThomas Cort 		break;
2474a0e6850fSThomas Cort 
2475a0e6850fSThomas Cort 	case REQ_BEG_LINE:
2476a0e6850fSThomas Cort 		cur->row_xpos = 0;
2477a0e6850fSThomas Cort 		_formi_init_field_xpos(cur);
2478a0e6850fSThomas Cort 		cur->start_char = 0;
2479a0e6850fSThomas Cort 		break;
2480a0e6850fSThomas Cort 
2481a0e6850fSThomas Cort 	case REQ_END_FIELD:
2482a0e6850fSThomas Cort 		while (cur->cur_line->next != NULL)
2483a0e6850fSThomas Cort 			cur->cur_line = cur->cur_line->next;
2484a0e6850fSThomas Cort 
2485a0e6850fSThomas Cort 		if (cur->row_count > cur->rows) {
2486a0e6850fSThomas Cort 			cur->start_line = cur->cur_line;
2487a0e6850fSThomas Cort 			pos = cur->rows - 1;
2488a0e6850fSThomas Cort 			while (pos > 0) {
2489a0e6850fSThomas Cort 				cur->start_line = cur->start_line->prev;
2490a0e6850fSThomas Cort 				pos--;
2491a0e6850fSThomas Cort 			}
2492a0e6850fSThomas Cort 			cur->cursor_ypos = cur->rows - 1;
2493a0e6850fSThomas Cort 		} else {
2494a0e6850fSThomas Cort 			cur->cursor_ypos = cur->row_count - 1;
2495a0e6850fSThomas Cort 		}
2496a0e6850fSThomas Cort 
2497a0e6850fSThomas Cort 		  /* we fall through here deliberately, we are on the
2498a0e6850fSThomas Cort 		   * correct row, now we need to get to the end of the
2499a0e6850fSThomas Cort 		   * line.
2500a0e6850fSThomas Cort 		   */
2501a0e6850fSThomas Cort 		  /* FALLTHRU */
2502a0e6850fSThomas Cort 
2503a0e6850fSThomas Cort 	case REQ_END_LINE:
2504a0e6850fSThomas Cort 		row = cur->cur_line;
2505a0e6850fSThomas Cort 
2506a0e6850fSThomas Cort 		if ((cur->rows + cur->nrows) == 1) {
2507a0e6850fSThomas Cort 			if (row->expanded > cur->cols - 1) {
2508a0e6850fSThomas Cort 				if ((cur->opts & O_STATIC) != O_STATIC) {
2509a0e6850fSThomas Cort 					cur->start_char = tab_fit_window(
2510a0e6850fSThomas Cort 						cur, row->length,
2511a0e6850fSThomas Cort 						cur->cols) + 1;
2512a0e6850fSThomas Cort 					cur->row_xpos = row->length
2513a0e6850fSThomas Cort 						- cur->start_char;
2514a0e6850fSThomas Cort 				} else {
2515a0e6850fSThomas Cort 					cur->start_char = 0;
2516a0e6850fSThomas Cort 					cur->row_xpos = cur->cols - 1;
2517a0e6850fSThomas Cort 				}
2518a0e6850fSThomas Cort 			} else {
2519a0e6850fSThomas Cort 				cur->row_xpos = row->length + 1;
2520a0e6850fSThomas Cort 				cur->start_char = 0;
2521a0e6850fSThomas Cort 			}
2522a0e6850fSThomas Cort 			_formi_set_cursor_xpos(cur, FALSE);
2523a0e6850fSThomas Cort 		} else {
2524a0e6850fSThomas Cort 			cur->row_xpos = row->length - 1;
2525a0e6850fSThomas Cort 			cur->cursor_xpos = row->expanded - 1;
2526a0e6850fSThomas Cort 			if (row->next == NULL) {
2527a0e6850fSThomas Cort 				cur->row_xpos++;
2528a0e6850fSThomas Cort 				cur->cursor_xpos++;
2529a0e6850fSThomas Cort 			}
2530a0e6850fSThomas Cort 		}
2531a0e6850fSThomas Cort 		break;
2532a0e6850fSThomas Cort 
2533a0e6850fSThomas Cort 	case REQ_NEW_LINE:
2534a0e6850fSThomas Cort 		start = cur->start_char	+ cur->row_xpos;
2535a0e6850fSThomas Cort 		if ((status = split_line(cur, TRUE, start, &row)) != E_OK)
2536a0e6850fSThomas Cort 			return status;
2537a0e6850fSThomas Cort 		cur->cur_line->hard_ret = TRUE;
2538a0e6850fSThomas Cort 		cur->cursor_xpos = 0;
2539a0e6850fSThomas Cort 		cur->row_xpos = 0;
2540a0e6850fSThomas Cort 		break;
2541a0e6850fSThomas Cort 
2542a0e6850fSThomas Cort 	case REQ_INS_CHAR:
2543a0e6850fSThomas Cort 		if ((status = _formi_add_char(cur, cur->start_char
2544a0e6850fSThomas Cort 					      + cur->row_xpos,
2545a0e6850fSThomas Cort 					      cur->pad)) != E_OK)
2546a0e6850fSThomas Cort 			return status;
2547a0e6850fSThomas Cort 		break;
2548a0e6850fSThomas Cort 
2549a0e6850fSThomas Cort 	case REQ_INS_LINE:
2550a0e6850fSThomas Cort 		if ((status = split_line(cur, TRUE, 0, &row)) != E_OK)
2551a0e6850fSThomas Cort 			return status;
2552a0e6850fSThomas Cort 		cur->cur_line->hard_ret = TRUE;
2553a0e6850fSThomas Cort 		break;
2554a0e6850fSThomas Cort 
2555a0e6850fSThomas Cort 	case REQ_DEL_CHAR:
2556a0e6850fSThomas Cort 		row = cur->cur_line;
2557a0e6850fSThomas Cort 		start = cur->start_char + cur->row_xpos;
2558a0e6850fSThomas Cort 		end = row->length - 1;
2559a0e6850fSThomas Cort 		if ((start >= row->length) && (row->next == NULL))
2560a0e6850fSThomas Cort 			return E_REQUEST_DENIED;
2561a0e6850fSThomas Cort 
2562a0e6850fSThomas Cort 		if ((start == row->length - 1) || (row->length == 0)) {
2563a0e6850fSThomas Cort 			if ((cur->rows + cur->nrows) > 1) {
2564a0e6850fSThomas Cort 				/*
2565a0e6850fSThomas Cort 				 * Firstly, check if the current line has
2566a0e6850fSThomas Cort 				 * a hard return.  In this case we just
2567a0e6850fSThomas Cort 				 * want to "delete" the hard return and
2568a0e6850fSThomas Cort 				 * re-wrap the field.  The hard return
2569a0e6850fSThomas Cort 				 * does not occupy a character space in
2570a0e6850fSThomas Cort 				 * the buffer but we must make it appear
2571a0e6850fSThomas Cort 				 * like it does for a deletion.
2572a0e6850fSThomas Cort 				 */
2573a0e6850fSThomas Cort 				if (row->hard_ret == TRUE) {
2574a0e6850fSThomas Cort 					row->hard_ret = FALSE;
2575a0e6850fSThomas Cort 					if (_formi_join_line(cur, &row,
2576a0e6850fSThomas Cort 							     JOIN_NEXT)
2577a0e6850fSThomas Cort 					    != E_OK) {
2578a0e6850fSThomas Cort 						row->hard_ret = TRUE;
2579a0e6850fSThomas Cort 						return 0;
2580a0e6850fSThomas Cort 					} else {
2581a0e6850fSThomas Cort 						return 1;
2582a0e6850fSThomas Cort 					}
2583a0e6850fSThomas Cort 				}
2584a0e6850fSThomas Cort 
2585a0e6850fSThomas Cort 				/*
2586a0e6850fSThomas Cort 				 * If we have more than one row, join the
2587a0e6850fSThomas Cort 				 * next row to make things easier unless
2588a0e6850fSThomas Cort 				 * we are at the end of the string, in
2589a0e6850fSThomas Cort 				 * that case the join would fail but we
2590a0e6850fSThomas Cort 				 * really want to delete the last char
2591a0e6850fSThomas Cort 				 * in the field.
2592a0e6850fSThomas Cort 				 */
2593a0e6850fSThomas Cort 				if (row->next != NULL) {
2594a0e6850fSThomas Cort 					if (_formi_join_line(cur, &row,
2595a0e6850fSThomas Cort 							     JOIN_NEXT_NW)
2596a0e6850fSThomas Cort 					    != E_OK) {
2597a0e6850fSThomas Cort 						return E_REQUEST_DENIED;
2598a0e6850fSThomas Cort 					}
2599a0e6850fSThomas Cort 				}
2600a0e6850fSThomas Cort 			}
2601a0e6850fSThomas Cort 		}
2602a0e6850fSThomas Cort 
2603a0e6850fSThomas Cort 		saved = row->string[start];
2604a0e6850fSThomas Cort 		bcopy(&row->string[start + 1], &row->string[start],
2605a0e6850fSThomas Cort 		      (size_t) (end - start + 1));
2606a0e6850fSThomas Cort 		row->string[end] = '\0';
2607a0e6850fSThomas Cort 		row->length--;
2608a0e6850fSThomas Cort 		if (row->length > 0)
2609a0e6850fSThomas Cort 			row->expanded = _formi_tab_expanded_length(
2610a0e6850fSThomas Cort 				row->string, 0, row->length - 1);
2611a0e6850fSThomas Cort 		else
2612a0e6850fSThomas Cort 			row->expanded = 0;
2613a0e6850fSThomas Cort 
2614a0e6850fSThomas Cort 		  /*
2615a0e6850fSThomas Cort 		   * recalculate tabs for a single line field, multiline
2616a0e6850fSThomas Cort 		   * fields will do this when the field is wrapped.
2617a0e6850fSThomas Cort 		   */
2618a0e6850fSThomas Cort 		if ((cur->rows + cur->nrows) == 1)
2619a0e6850fSThomas Cort 			_formi_calculate_tabs(row);
2620a0e6850fSThomas Cort 		  /*
2621a0e6850fSThomas Cort 		   * if we are at the end of the string then back the
2622a0e6850fSThomas Cort 		   * cursor pos up one to stick on the end of the line
2623a0e6850fSThomas Cort 		   */
2624a0e6850fSThomas Cort 		if (start == row->length) {
2625a0e6850fSThomas Cort 			if (row->length > 1) {
2626a0e6850fSThomas Cort 				if ((cur->rows + cur->nrows) == 1) {
2627a0e6850fSThomas Cort 					pos = cur->row_xpos + cur->start_char;
2628a0e6850fSThomas Cort 					cur->start_char =
2629a0e6850fSThomas Cort 						tab_fit_window(
2630a0e6850fSThomas Cort 							cur,
2631a0e6850fSThomas Cort 							cur->start_char + cur->row_xpos,
2632a0e6850fSThomas Cort 							cur->cols);
2633a0e6850fSThomas Cort 					cur->row_xpos = pos - cur->start_char
2634a0e6850fSThomas Cort 						- 1;
2635a0e6850fSThomas Cort 					_formi_set_cursor_xpos(cur, FALSE);
2636a0e6850fSThomas Cort 				} else {
2637a0e6850fSThomas Cort 					if (cur->row_xpos == 0) {
2638a0e6850fSThomas Cort 						if (row->next != NULL) {
2639a0e6850fSThomas Cort 							if (_formi_join_line(
2640a0e6850fSThomas Cort 								cur, &row,
2641a0e6850fSThomas Cort 								JOIN_PREV_NW)
2642a0e6850fSThomas Cort 							    != E_OK) {
2643a0e6850fSThomas Cort 								return E_REQUEST_DENIED;
2644a0e6850fSThomas Cort 							}
2645a0e6850fSThomas Cort 						} else {
2646a0e6850fSThomas Cort 							if (cur->row_count > 1)
2647a0e6850fSThomas Cort 								cur->row_count--;
2648a0e6850fSThomas Cort 						}
2649a0e6850fSThomas Cort 
2650a0e6850fSThomas Cort 					}
2651a0e6850fSThomas Cort 
2652a0e6850fSThomas Cort 					cur->row_xpos = start - 1;
2653a0e6850fSThomas Cort 					cur->cursor_xpos =
2654a0e6850fSThomas Cort 						_formi_tab_expanded_length(
2655a0e6850fSThomas Cort 							row->string,
2656a0e6850fSThomas Cort 							0, cur->row_xpos - 1);
2657a0e6850fSThomas Cort 					if ((cur->cursor_xpos > 0)
2658a0e6850fSThomas Cort 					    && (start != (row->expanded - 1)))
2659a0e6850fSThomas Cort 						cur->cursor_xpos--;
2660a0e6850fSThomas Cort 				}
2661a0e6850fSThomas Cort 
2662a0e6850fSThomas Cort 				start--;
2663a0e6850fSThomas Cort 			} else {
2664a0e6850fSThomas Cort 				start = 0;
2665a0e6850fSThomas Cort 				cur->row_xpos = 0;
2666a0e6850fSThomas Cort 				_formi_init_field_xpos(cur);
2667a0e6850fSThomas Cort 			}
2668a0e6850fSThomas Cort 		}
2669a0e6850fSThomas Cort 
2670a0e6850fSThomas Cort 		if ((cur->rows + cur->nrows) > 1) {
2671a0e6850fSThomas Cort 			if (_formi_wrap_field(cur, row) != E_OK) {
2672a0e6850fSThomas Cort 				bcopy(&row->string[start],
2673a0e6850fSThomas Cort 				      &row->string[start + 1],
2674a0e6850fSThomas Cort 				      (size_t) (end - start));
2675a0e6850fSThomas Cort 				row->length++;
2676a0e6850fSThomas Cort 				row->string[start] = saved;
2677a0e6850fSThomas Cort 				_formi_wrap_field(cur, row);
2678a0e6850fSThomas Cort 				return E_REQUEST_DENIED;
2679a0e6850fSThomas Cort 			}
2680a0e6850fSThomas Cort 		}
2681a0e6850fSThomas Cort 		break;
2682a0e6850fSThomas Cort 
2683a0e6850fSThomas Cort 	case REQ_DEL_PREV:
2684a0e6850fSThomas Cort 		if ((cur->cursor_xpos == 0) && (cur->start_char == 0)
2685a0e6850fSThomas Cort 		    && (cur->start_line->prev == NULL)
2686a0e6850fSThomas Cort 		    && (cur->cursor_ypos == 0))
2687a0e6850fSThomas Cort 			   return E_REQUEST_DENIED;
2688a0e6850fSThomas Cort 
2689a0e6850fSThomas Cort 		row = cur->cur_line;
2690a0e6850fSThomas Cort 		start = cur->row_xpos + cur->start_char;
2691a0e6850fSThomas Cort 		end = row->length - 1;
2692a0e6850fSThomas Cort 		eat_char = TRUE;
2693a0e6850fSThomas Cort 
2694a0e6850fSThomas Cort 		if ((cur->start_char + cur->row_xpos) == 0) {
2695a0e6850fSThomas Cort 			if (row->prev == NULL)
2696a0e6850fSThomas Cort 				return E_REQUEST_DENIED;
2697a0e6850fSThomas Cort 
2698a0e6850fSThomas Cort 			  /*
2699a0e6850fSThomas Cort 			   * If we are a multiline field then check if
2700a0e6850fSThomas Cort 			   * the line above has a hard return.  If it does
2701a0e6850fSThomas Cort 			   * then just "eat" the hard return and re-wrap
2702a0e6850fSThomas Cort 			   * the field.
2703a0e6850fSThomas Cort 			   */
2704a0e6850fSThomas Cort 			if (row->prev->hard_ret == TRUE) {
2705a0e6850fSThomas Cort 				row->prev->hard_ret = FALSE;
2706a0e6850fSThomas Cort 				if (_formi_join_line(cur, &row,
2707a0e6850fSThomas Cort 						     JOIN_PREV) != E_OK) {
2708a0e6850fSThomas Cort 					row->prev->hard_ret = TRUE;
2709a0e6850fSThomas Cort 					return 0;
2710a0e6850fSThomas Cort 				}
2711a0e6850fSThomas Cort 
2712a0e6850fSThomas Cort 				eat_char = FALSE;
2713a0e6850fSThomas Cort 			} else {
2714a0e6850fSThomas Cort 				start = row->prev->length;
2715a0e6850fSThomas Cort 				  /*
2716a0e6850fSThomas Cort 				   * Join this line to the previous
2717a0e6850fSThomas Cort 				   * one.
2718a0e6850fSThomas Cort 				   */
2719a0e6850fSThomas Cort 				if (_formi_join_line(cur, &row,
2720a0e6850fSThomas Cort 						     JOIN_PREV_NW) != E_OK) {
2721a0e6850fSThomas Cort 					return 0;
2722a0e6850fSThomas Cort 				}
2723a0e6850fSThomas Cort 				end = row->length - 1;
2724a0e6850fSThomas Cort 			}
2725a0e6850fSThomas Cort 		}
2726a0e6850fSThomas Cort 
2727a0e6850fSThomas Cort 		if (eat_char == TRUE) {
2728a0e6850fSThomas Cort 			  /*
2729a0e6850fSThomas Cort 			   * eat a char from the buffer.  Normally we do
2730a0e6850fSThomas Cort 			   * this unless we have deleted a "hard return"
2731a0e6850fSThomas Cort 			   * in which case we just want to join the lines
2732a0e6850fSThomas Cort 			   * without losing a char.
2733a0e6850fSThomas Cort 			   */
2734a0e6850fSThomas Cort 			saved = row->string[start - 1];
2735a0e6850fSThomas Cort 			bcopy(&row->string[start], &row->string[start - 1],
2736a0e6850fSThomas Cort 			      (size_t) (end - start + 1));
2737a0e6850fSThomas Cort 			row->length--;
2738a0e6850fSThomas Cort 			row->string[row->length] = '\0';
2739a0e6850fSThomas Cort 			row->expanded = _formi_tab_expanded_length(
2740a0e6850fSThomas Cort 				row->string, 0, row->length - 1);
2741a0e6850fSThomas Cort 		}
2742a0e6850fSThomas Cort 
2743a0e6850fSThomas Cort 		if ((cur->rows + cur->nrows) == 1) {
2744a0e6850fSThomas Cort 			_formi_calculate_tabs(row);
2745a0e6850fSThomas Cort 			pos = cur->row_xpos + cur->start_char;
2746a0e6850fSThomas Cort 			if (pos > 0)
2747a0e6850fSThomas Cort 				pos--;
2748a0e6850fSThomas Cort 			cur->start_char =
2749a0e6850fSThomas Cort 				tab_fit_window(cur,
2750a0e6850fSThomas Cort 					       cur->start_char + cur->row_xpos,
2751a0e6850fSThomas Cort 					       cur->cols);
2752a0e6850fSThomas Cort 			cur->row_xpos = pos - cur->start_char;
2753a0e6850fSThomas Cort 			_formi_set_cursor_xpos(cur, FALSE);
2754a0e6850fSThomas Cort 		} else {
2755a0e6850fSThomas Cort 			if (eat_char == TRUE) {
2756a0e6850fSThomas Cort 				cur->row_xpos--;
2757a0e6850fSThomas Cort 				if (cur->row_xpos > 0)
2758a0e6850fSThomas Cort 					cur->cursor_xpos =
2759a0e6850fSThomas Cort 						_formi_tab_expanded_length(
2760a0e6850fSThomas Cort 							row->string, 0,
2761a0e6850fSThomas Cort 							cur->row_xpos - 1);
2762a0e6850fSThomas Cort 				else
2763a0e6850fSThomas Cort 					cur->cursor_xpos = 0;
2764a0e6850fSThomas Cort 			}
2765a0e6850fSThomas Cort 
2766a0e6850fSThomas Cort 			if ((_formi_wrap_field(cur, row) != E_OK)) {
2767a0e6850fSThomas Cort 				bcopy(&row->string[start - 1],
2768a0e6850fSThomas Cort 				      &row->string[start],
2769a0e6850fSThomas Cort 				      (size_t) (end - start));
2770a0e6850fSThomas Cort 				row->length++;
2771a0e6850fSThomas Cort 				row->string[start - 1] = saved;
2772a0e6850fSThomas Cort 				row->string[row->length] = '\0';
2773a0e6850fSThomas Cort 				_formi_wrap_field(cur, row);
2774a0e6850fSThomas Cort 				return E_REQUEST_DENIED;
2775a0e6850fSThomas Cort 			}
2776a0e6850fSThomas Cort 		}
2777a0e6850fSThomas Cort 		break;
2778a0e6850fSThomas Cort 
2779a0e6850fSThomas Cort 	case REQ_DEL_LINE:
2780a0e6850fSThomas Cort 		if (((cur->rows + cur->nrows) == 1) ||
2781a0e6850fSThomas Cort 		    (cur->row_count == 1)) {
2782a0e6850fSThomas Cort 			  /* single line case */
2783a0e6850fSThomas Cort 			row->length = 0;
2784a0e6850fSThomas Cort 			row->expanded = row->length = 0;
2785a0e6850fSThomas Cort 			cur->row_xpos = 0;
2786a0e6850fSThomas Cort 			_formi_init_field_xpos(cur);
2787a0e6850fSThomas Cort 			cur->cursor_ypos = 0;
2788a0e6850fSThomas Cort 		} else {
2789a0e6850fSThomas Cort 			  /* multiline field */
2790a0e6850fSThomas Cort 			old_count = cur->row_count;
2791a0e6850fSThomas Cort 			cur->row_count--;
2792a0e6850fSThomas Cort 			if (cur->row_count == 0)
2793a0e6850fSThomas Cort 				cur->row_count = 1;
2794a0e6850fSThomas Cort 
2795a0e6850fSThomas Cort 			if (old_count == 1) {
2796a0e6850fSThomas Cort 				row->expanded = row->length = 0;
2797a0e6850fSThomas Cort 				cur->cursor_xpos = 0;
2798a0e6850fSThomas Cort 				cur->row_xpos = 0;
2799a0e6850fSThomas Cort 				cur->cursor_ypos = 0;
2800a0e6850fSThomas Cort 			} else
2801a0e6850fSThomas Cort 				add_to_free(cur, row);
2802a0e6850fSThomas Cort 
2803a0e6850fSThomas Cort 			if (row->next == NULL) {
2804a0e6850fSThomas Cort 				if (cur->cursor_ypos == 0) {
2805a0e6850fSThomas Cort 					if (cur->start_line->prev != NULL) {
2806a0e6850fSThomas Cort 						cur->start_line =
2807a0e6850fSThomas Cort 							cur->start_line->prev;
2808a0e6850fSThomas Cort 					}
2809a0e6850fSThomas Cort 				} else {
2810a0e6850fSThomas Cort 					cur->cursor_ypos--;
2811a0e6850fSThomas Cort 				}
2812a0e6850fSThomas Cort 			}
2813a0e6850fSThomas Cort 
2814a0e6850fSThomas Cort 			if (old_count > 1) {
2815a0e6850fSThomas Cort 				if (cur->cursor_xpos > row->expanded) {
2816a0e6850fSThomas Cort 					cur->cursor_xpos = row->expanded - 1;
2817a0e6850fSThomas Cort 					cur->row_xpos = row->length - 1;
2818a0e6850fSThomas Cort 				}
2819a0e6850fSThomas Cort 
2820a0e6850fSThomas Cort 				cur->start_line = cur->alines;
2821a0e6850fSThomas Cort 				rs = cur->start_line;
2822a0e6850fSThomas Cort 				cur->cursor_ypos = 0;
2823a0e6850fSThomas Cort 				while (rs != row) {
2824a0e6850fSThomas Cort 					if (cur->cursor_ypos < cur->rows)
2825a0e6850fSThomas Cort 						cur->cursor_ypos++;
2826a0e6850fSThomas Cort 					else
2827a0e6850fSThomas Cort 						cur->start_line =
2828a0e6850fSThomas Cort 							cur->start_line->next;
2829a0e6850fSThomas Cort 					rs = rs->next;
2830a0e6850fSThomas Cort 				}
2831a0e6850fSThomas Cort 			}
2832a0e6850fSThomas Cort 		}
2833a0e6850fSThomas Cort 		break;
2834a0e6850fSThomas Cort 
2835a0e6850fSThomas Cort 	case REQ_DEL_WORD:
2836a0e6850fSThomas Cort 		start = cur->start_char + cur->row_xpos;
2837a0e6850fSThomas Cort 		str = row->string;
2838a0e6850fSThomas Cort 
2839a0e6850fSThomas Cort 		wb = find_eow(cur, start, TRUE, &row);
2840a0e6850fSThomas Cort 		if (wb < 0)
2841a0e6850fSThomas Cort 			return wb;
2842a0e6850fSThomas Cort 
2843a0e6850fSThomas Cort 		end = wb;
2844a0e6850fSThomas Cort 
2845a0e6850fSThomas Cort 		  /*
2846a0e6850fSThomas Cort 		   * If not at the start of a word then find the start,
2847a0e6850fSThomas Cort 		   * we cannot blindly call find_sow because this will
2848a0e6850fSThomas Cort 		   * skip back a word if we are already at the start of
2849a0e6850fSThomas Cort 		   * a word.
2850a0e6850fSThomas Cort 		   */
2851a0e6850fSThomas Cort 		if ((start > 0)
2852a0e6850fSThomas Cort 		    && !(isblank((unsigned char)str[start - 1]) &&
2853a0e6850fSThomas Cort 			!isblank((unsigned char)str[start])))
2854a0e6850fSThomas Cort 			start = find_sow(start, &row);
2855a0e6850fSThomas Cort 		str = row->string;
2856a0e6850fSThomas Cort 		  /* XXXX hmmmm what if start and end on diff rows? XXXX */
2857a0e6850fSThomas Cort 		bcopy(&str[end], &str[start],
2858a0e6850fSThomas Cort 		      (size_t) (row->length - end + 1));
2859a0e6850fSThomas Cort 		len = end - start;
2860a0e6850fSThomas Cort 		row->length -= len;
2861a0e6850fSThomas Cort 
2862a0e6850fSThomas Cort 		if ((cur->rows + cur->nrows) > 1) {
2863a0e6850fSThomas Cort 			row = cur->start_line + cur->cursor_ypos;
2864a0e6850fSThomas Cort 			if (row->next != NULL) {
2865a0e6850fSThomas Cort 				/*
2866a0e6850fSThomas Cort 				 * if not on the last row we need to
2867a0e6850fSThomas Cort 				 * join on the next row so the line
2868a0e6850fSThomas Cort 				 * will be re-wrapped.
2869a0e6850fSThomas Cort 				 */
2870a0e6850fSThomas Cort 				_formi_join_line(cur, &row, JOIN_NEXT_NW);
2871a0e6850fSThomas Cort 			}
2872a0e6850fSThomas Cort 			_formi_wrap_field(cur, row);
2873a0e6850fSThomas Cort 			cur->row_xpos = start;
2874a0e6850fSThomas Cort 			cur->cursor_xpos = _formi_tab_expanded_length(
2875a0e6850fSThomas Cort 				row->string, 0, cur->row_xpos);
2876a0e6850fSThomas Cort 			if (cur->cursor_xpos > 0)
2877a0e6850fSThomas Cort 				cur->cursor_xpos--;
2878a0e6850fSThomas Cort 		} else {
2879a0e6850fSThomas Cort 			_formi_calculate_tabs(row);
2880a0e6850fSThomas Cort 			cur->row_xpos = start - cur->start_char;
2881a0e6850fSThomas Cort 			if (cur->row_xpos > 0)
2882a0e6850fSThomas Cort 				cur->row_xpos--;
2883a0e6850fSThomas Cort 			_formi_set_cursor_xpos(cur, FALSE);
2884a0e6850fSThomas Cort 		}
2885a0e6850fSThomas Cort 		break;
2886a0e6850fSThomas Cort 
2887a0e6850fSThomas Cort 	case REQ_CLR_EOL:
2888a0e6850fSThomas Cort 		row->string[cur->row_xpos + 1] = '\0';
2889a0e6850fSThomas Cort 		row->length = cur->row_xpos + 1;
2890a0e6850fSThomas Cort 		row->expanded = cur->cursor_xpos + 1;
2891a0e6850fSThomas Cort 		break;
2892a0e6850fSThomas Cort 
2893a0e6850fSThomas Cort 	case REQ_CLR_EOF:
2894a0e6850fSThomas Cort 		row = cur->cur_line->next;
2895a0e6850fSThomas Cort 		while (row != NULL) {
2896a0e6850fSThomas Cort 			rs = row->next;
2897a0e6850fSThomas Cort 			add_to_free(cur, row);
2898a0e6850fSThomas Cort 			row = rs;
2899a0e6850fSThomas Cort 			cur->row_count--;
2900a0e6850fSThomas Cort 		}
2901a0e6850fSThomas Cort 		break;
2902a0e6850fSThomas Cort 
2903a0e6850fSThomas Cort 	case REQ_CLR_FIELD:
2904a0e6850fSThomas Cort 		row = cur->alines->next;
2905a0e6850fSThomas Cort 		cur->cur_line = cur->alines;
2906a0e6850fSThomas Cort 		cur->start_line = cur->alines;
2907a0e6850fSThomas Cort 
2908a0e6850fSThomas Cort 		while (row != NULL) {
2909a0e6850fSThomas Cort 			rs = row->next;
2910a0e6850fSThomas Cort 			add_to_free(cur, row);
2911a0e6850fSThomas Cort 			row = rs;
2912a0e6850fSThomas Cort 		}
2913a0e6850fSThomas Cort 
2914a0e6850fSThomas Cort 		cur->alines->string[0] = '\0';
2915a0e6850fSThomas Cort 		cur->alines->length = 0;
2916a0e6850fSThomas Cort 		cur->alines->expanded = 0;
2917a0e6850fSThomas Cort 		cur->row_count = 1;
2918a0e6850fSThomas Cort 		cur->cursor_ypos = 0;
2919a0e6850fSThomas Cort 		cur->row_xpos = 0;
2920a0e6850fSThomas Cort 		_formi_init_field_xpos(cur);
2921a0e6850fSThomas Cort 		cur->start_char = 0;
2922a0e6850fSThomas Cort 		break;
2923a0e6850fSThomas Cort 
2924a0e6850fSThomas Cort 	case REQ_OVL_MODE:
2925a0e6850fSThomas Cort 		cur->overlay = 1;
2926a0e6850fSThomas Cort 		break;
2927a0e6850fSThomas Cort 
2928a0e6850fSThomas Cort 	case REQ_INS_MODE:
2929a0e6850fSThomas Cort 		cur->overlay = 0;
2930a0e6850fSThomas Cort 		break;
2931a0e6850fSThomas Cort 
2932a0e6850fSThomas Cort 	case REQ_SCR_FLINE:
2933a0e6850fSThomas Cort 		_formi_scroll_fwd(cur, 1);
2934a0e6850fSThomas Cort 		break;
2935a0e6850fSThomas Cort 
2936a0e6850fSThomas Cort 	case REQ_SCR_BLINE:
2937a0e6850fSThomas Cort 		_formi_scroll_back(cur, 1);
2938a0e6850fSThomas Cort 		break;
2939a0e6850fSThomas Cort 
2940a0e6850fSThomas Cort 	case REQ_SCR_FPAGE:
2941a0e6850fSThomas Cort 		_formi_scroll_fwd(cur, cur->rows);
2942a0e6850fSThomas Cort 		break;
2943a0e6850fSThomas Cort 
2944a0e6850fSThomas Cort 	case REQ_SCR_BPAGE:
2945a0e6850fSThomas Cort 		_formi_scroll_back(cur, cur->rows);
2946a0e6850fSThomas Cort 		break;
2947a0e6850fSThomas Cort 
2948a0e6850fSThomas Cort 	case REQ_SCR_FHPAGE:
2949a0e6850fSThomas Cort 		_formi_scroll_fwd(cur, cur->rows / 2);
2950a0e6850fSThomas Cort 		break;
2951a0e6850fSThomas Cort 
2952a0e6850fSThomas Cort 	case REQ_SCR_BHPAGE:
2953a0e6850fSThomas Cort 		_formi_scroll_back(cur, cur->rows / 2);
2954a0e6850fSThomas Cort 		break;
2955a0e6850fSThomas Cort 
2956a0e6850fSThomas Cort 	case REQ_SCR_FCHAR:
2957a0e6850fSThomas Cort 		_formi_hscroll_fwd(cur, row, 1);
2958a0e6850fSThomas Cort 		break;
2959a0e6850fSThomas Cort 
2960a0e6850fSThomas Cort 	case REQ_SCR_BCHAR:
2961a0e6850fSThomas Cort 		_formi_hscroll_back(cur, row, 1);
2962a0e6850fSThomas Cort 		break;
2963a0e6850fSThomas Cort 
2964a0e6850fSThomas Cort 	case REQ_SCR_HFLINE:
2965a0e6850fSThomas Cort 		_formi_hscroll_fwd(cur, row, cur->cols);
2966a0e6850fSThomas Cort 		break;
2967a0e6850fSThomas Cort 
2968a0e6850fSThomas Cort 	case REQ_SCR_HBLINE:
2969a0e6850fSThomas Cort 		_formi_hscroll_back(cur, row, cur->cols);
2970a0e6850fSThomas Cort 		break;
2971a0e6850fSThomas Cort 
2972a0e6850fSThomas Cort 	case REQ_SCR_HFHALF:
2973a0e6850fSThomas Cort 		_formi_hscroll_fwd(cur, row, cur->cols / 2);
2974a0e6850fSThomas Cort 		break;
2975a0e6850fSThomas Cort 
2976a0e6850fSThomas Cort 	case REQ_SCR_HBHALF:
2977a0e6850fSThomas Cort 		_formi_hscroll_back(cur, row, cur->cols / 2);
2978a0e6850fSThomas Cort 		break;
2979a0e6850fSThomas Cort 
2980a0e6850fSThomas Cort 	default:
2981a0e6850fSThomas Cort 		return 0;
2982a0e6850fSThomas Cort 	}
2983a0e6850fSThomas Cort 
2984a0e6850fSThomas Cort #ifdef DEBUG
2985a0e6850fSThomas Cort 	fprintf(dbg,
2986a0e6850fSThomas Cort 	 "exit: cursor_xpos=%d, row_xpos=%d, start_char=%d, length=%d, allocated=%d\n",
2987a0e6850fSThomas Cort 		cur->cursor_xpos, cur->row_xpos, cur->start_char,
2988a0e6850fSThomas Cort 		cur->cur_line->length,	cur->cur_line->allocated);
2989a0e6850fSThomas Cort 	fprintf(dbg, "exit: start_line=%p, ypos=%d\n", cur->start_line,
2990a0e6850fSThomas Cort 		cur->cursor_ypos);
2991a0e6850fSThomas Cort 	fprintf(dbg, "exit: string=\"%s\"\n", cur->cur_line->string);
2992a0e6850fSThomas Cort 	assert ((cur->cursor_xpos < INT_MAX) && (cur->row_xpos < INT_MAX)
2993a0e6850fSThomas Cort 		&& (cur->cursor_xpos >= cur->row_xpos));
2994a0e6850fSThomas Cort #endif
2995a0e6850fSThomas Cort 	return 1;
2996a0e6850fSThomas Cort }
2997a0e6850fSThomas Cort 
2998a0e6850fSThomas Cort /*
2999*84d9c625SLionel Sambuc  * Validate the given character by passing it to any type character
3000a0e6850fSThomas Cort  * checking routines, if they exist.
3001a0e6850fSThomas Cort  */
3002a0e6850fSThomas Cort int
_formi_validate_char(FIELD * field,char c)3003a0e6850fSThomas Cort _formi_validate_char(FIELD *field, char c)
3004a0e6850fSThomas Cort {
3005a0e6850fSThomas Cort 	int ret_val;
3006a0e6850fSThomas Cort 
3007a0e6850fSThomas Cort 	if (field->type == NULL)
3008a0e6850fSThomas Cort 		return E_OK;
3009a0e6850fSThomas Cort 
3010a0e6850fSThomas Cort 	ret_val = E_INVALID_FIELD;
3011a0e6850fSThomas Cort 	_formi_do_char_validation(field, field->type, c, &ret_val);
3012a0e6850fSThomas Cort 
3013a0e6850fSThomas Cort 	return ret_val;
3014a0e6850fSThomas Cort }
3015a0e6850fSThomas Cort 
3016a0e6850fSThomas Cort 
3017a0e6850fSThomas Cort /*
3018a0e6850fSThomas Cort  * Perform the validation of the character, invoke all field_type validation
3019a0e6850fSThomas Cort  * routines.  If the field is ok then update ret_val to E_OK otherwise
3020a0e6850fSThomas Cort  * ret_val is not changed.
3021a0e6850fSThomas Cort  */
3022a0e6850fSThomas Cort static void
_formi_do_char_validation(FIELD * field,FIELDTYPE * type,char c,int * ret_val)3023a0e6850fSThomas Cort _formi_do_char_validation(FIELD *field, FIELDTYPE *type, char c, int *ret_val)
3024a0e6850fSThomas Cort {
3025a0e6850fSThomas Cort 	if ((type->flags & _TYPE_IS_LINKED) == _TYPE_IS_LINKED) {
3026a0e6850fSThomas Cort 		_formi_do_char_validation(field, type->link->next, c, ret_val);
3027a0e6850fSThomas Cort 		_formi_do_char_validation(field, type->link->prev, c, ret_val);
3028a0e6850fSThomas Cort 	} else {
3029a0e6850fSThomas Cort 		if (type->char_check == NULL)
3030a0e6850fSThomas Cort 			*ret_val = E_OK;
3031a0e6850fSThomas Cort 		else {
3032a0e6850fSThomas Cort 			if (type->char_check((int)(unsigned char) c,
3033a0e6850fSThomas Cort 					     field->args) == TRUE)
3034a0e6850fSThomas Cort 				*ret_val = E_OK;
3035a0e6850fSThomas Cort 		}
3036a0e6850fSThomas Cort 	}
3037a0e6850fSThomas Cort }
3038a0e6850fSThomas Cort 
3039a0e6850fSThomas Cort /*
3040a0e6850fSThomas Cort  * Validate the current field.  If the field validation returns success then
3041a0e6850fSThomas Cort  * return E_OK otherwise return E_INVALID_FIELD.
3042a0e6850fSThomas Cort  *
3043a0e6850fSThomas Cort  */
3044a0e6850fSThomas Cort int
_formi_validate_field(FORM * form)3045a0e6850fSThomas Cort _formi_validate_field(FORM *form)
3046a0e6850fSThomas Cort {
3047a0e6850fSThomas Cort 	FIELD *cur;
3048a0e6850fSThomas Cort 	int ret_val, count;
3049a0e6850fSThomas Cort 
3050a0e6850fSThomas Cort 
3051a0e6850fSThomas Cort 	if ((form == NULL) || (form->fields == NULL) ||
3052a0e6850fSThomas Cort 	    (form->fields[0] == NULL))
3053a0e6850fSThomas Cort 		return E_INVALID_FIELD;
3054a0e6850fSThomas Cort 
3055a0e6850fSThomas Cort 	cur = form->fields[form->cur_field];
3056a0e6850fSThomas Cort 
3057a0e6850fSThomas Cort 	  /*
3058a0e6850fSThomas Cort 	   * Sync the buffer if it has been modified so the field
3059a0e6850fSThomas Cort 	   * validation routines can use it and because this is
3060a0e6850fSThomas Cort 	   * the correct behaviour according to AT&T implementation.
3061a0e6850fSThomas Cort 	   */
3062a0e6850fSThomas Cort 	if ((cur->buf0_status == TRUE)
3063a0e6850fSThomas Cort 	    && ((ret_val = _formi_sync_buffer(cur)) != E_OK))
3064a0e6850fSThomas Cort 			return ret_val;
3065a0e6850fSThomas Cort 
3066a0e6850fSThomas Cort 	  /*
3067a0e6850fSThomas Cort 	   * If buffer is untouched then the string pointer may be
3068a0e6850fSThomas Cort 	   * NULL, see if this is ok or not.
3069a0e6850fSThomas Cort 	   */
3070a0e6850fSThomas Cort 	if (cur->buffers[0].string == NULL) {
3071a0e6850fSThomas Cort 		if ((cur->opts & O_NULLOK) == O_NULLOK)
3072a0e6850fSThomas Cort 			return E_OK;
3073a0e6850fSThomas Cort 		else
3074a0e6850fSThomas Cort 			return E_INVALID_FIELD;
3075a0e6850fSThomas Cort 	}
3076a0e6850fSThomas Cort 
3077a0e6850fSThomas Cort 	count = _formi_skip_blanks(cur->buffers[0].string, 0);
3078a0e6850fSThomas Cort 
3079a0e6850fSThomas Cort 	  /* check if we have a null field, depending on the nullok flag
3080a0e6850fSThomas Cort 	   * this may be acceptable or not....
3081a0e6850fSThomas Cort 	   */
3082a0e6850fSThomas Cort 	if (cur->buffers[0].string[count] == '\0') {
3083a0e6850fSThomas Cort 		if ((cur->opts & O_NULLOK) == O_NULLOK)
3084a0e6850fSThomas Cort 			return E_OK;
3085a0e6850fSThomas Cort 		else
3086a0e6850fSThomas Cort 			return E_INVALID_FIELD;
3087a0e6850fSThomas Cort 	}
3088a0e6850fSThomas Cort 
3089a0e6850fSThomas Cort 	  /* check if an unmodified field is ok */
3090a0e6850fSThomas Cort 	if (cur->buf0_status == 0) {
3091a0e6850fSThomas Cort 		if ((cur->opts & O_PASSOK) == O_PASSOK)
3092a0e6850fSThomas Cort 			return E_OK;
3093a0e6850fSThomas Cort 		else
3094a0e6850fSThomas Cort 			return E_INVALID_FIELD;
3095a0e6850fSThomas Cort 	}
3096a0e6850fSThomas Cort 
3097a0e6850fSThomas Cort 	  /* if there is no type then just accept the field */
3098a0e6850fSThomas Cort 	if (cur->type == NULL)
3099a0e6850fSThomas Cort 		return E_OK;
3100a0e6850fSThomas Cort 
3101a0e6850fSThomas Cort 	ret_val = E_INVALID_FIELD;
3102a0e6850fSThomas Cort 	_formi_do_validation(cur, cur->type, &ret_val);
3103a0e6850fSThomas Cort 
3104a0e6850fSThomas Cort 	return ret_val;
3105a0e6850fSThomas Cort }
3106a0e6850fSThomas Cort 
3107a0e6850fSThomas Cort /*
3108a0e6850fSThomas Cort  * Perform the validation of the field, invoke all field_type validation
3109a0e6850fSThomas Cort  * routines.  If the field is ok then update ret_val to E_OK otherwise
3110a0e6850fSThomas Cort  * ret_val is not changed.
3111a0e6850fSThomas Cort  */
3112a0e6850fSThomas Cort static void
_formi_do_validation(FIELD * field,FIELDTYPE * type,int * ret_val)3113a0e6850fSThomas Cort _formi_do_validation(FIELD *field, FIELDTYPE *type, int *ret_val)
3114a0e6850fSThomas Cort {
3115a0e6850fSThomas Cort 	if ((type->flags & _TYPE_IS_LINKED) == _TYPE_IS_LINKED) {
3116a0e6850fSThomas Cort 		_formi_do_validation(field, type->link->next, ret_val);
3117a0e6850fSThomas Cort 		_formi_do_validation(field, type->link->prev, ret_val);
3118a0e6850fSThomas Cort 	} else {
3119a0e6850fSThomas Cort 		if (type->field_check == NULL)
3120a0e6850fSThomas Cort 			*ret_val = E_OK;
3121a0e6850fSThomas Cort 		else {
3122a0e6850fSThomas Cort 			if (type->field_check(field, field_buffer(field, 0))
3123a0e6850fSThomas Cort 			    == TRUE)
3124a0e6850fSThomas Cort 				*ret_val = E_OK;
3125a0e6850fSThomas Cort 		}
3126a0e6850fSThomas Cort 	}
3127a0e6850fSThomas Cort }
3128a0e6850fSThomas Cort 
3129a0e6850fSThomas Cort /*
3130a0e6850fSThomas Cort  * Select the next/previous choice for the field, the driver command
3131a0e6850fSThomas Cort  * selecting the direction will be passed in c.  Return 1 if a choice
3132a0e6850fSThomas Cort  * selection succeeded, 0 otherwise.
3133a0e6850fSThomas Cort  */
3134a0e6850fSThomas Cort int
_formi_field_choice(FORM * form,int c)3135a0e6850fSThomas Cort _formi_field_choice(FORM *form, int c)
3136a0e6850fSThomas Cort {
3137a0e6850fSThomas Cort 	FIELDTYPE *type;
3138a0e6850fSThomas Cort 	FIELD *field;
3139a0e6850fSThomas Cort 
3140a0e6850fSThomas Cort 	if ((form == NULL) || (form->fields == NULL) ||
3141a0e6850fSThomas Cort 	    (form->fields[0] == NULL) ||
3142a0e6850fSThomas Cort 	    (form->fields[form->cur_field]->type == NULL))
3143a0e6850fSThomas Cort 		return 0;
3144a0e6850fSThomas Cort 
3145a0e6850fSThomas Cort 	field = form->fields[form->cur_field];
3146a0e6850fSThomas Cort 	type = field->type;
3147a0e6850fSThomas Cort 
3148a0e6850fSThomas Cort 	switch (c) {
3149a0e6850fSThomas Cort 	case REQ_NEXT_CHOICE:
3150a0e6850fSThomas Cort 		if (type->next_choice == NULL)
3151a0e6850fSThomas Cort 			return 0;
3152a0e6850fSThomas Cort 		else
3153a0e6850fSThomas Cort 			return type->next_choice(field,
3154a0e6850fSThomas Cort 						 field_buffer(field, 0));
3155a0e6850fSThomas Cort 
3156a0e6850fSThomas Cort 	case REQ_PREV_CHOICE:
3157a0e6850fSThomas Cort 		if (type->prev_choice == NULL)
3158a0e6850fSThomas Cort 			return 0;
3159a0e6850fSThomas Cort 		else
3160a0e6850fSThomas Cort 			return type->prev_choice(field,
3161a0e6850fSThomas Cort 						 field_buffer(field, 0));
3162a0e6850fSThomas Cort 
3163a0e6850fSThomas Cort 	default: /* should never happen! */
3164a0e6850fSThomas Cort 		return 0;
3165a0e6850fSThomas Cort 	}
3166a0e6850fSThomas Cort }
3167a0e6850fSThomas Cort 
3168a0e6850fSThomas Cort /*
3169a0e6850fSThomas Cort  * Update the fields if they have changed.  The parameter old has the
3170a0e6850fSThomas Cort  * previous current field as the current field may have been updated by
3171a0e6850fSThomas Cort  * the driver.  Return 1 if the form page needs updating.
3172a0e6850fSThomas Cort  *
3173a0e6850fSThomas Cort  */
3174a0e6850fSThomas Cort int
_formi_update_field(FORM * form,int old_field)3175a0e6850fSThomas Cort _formi_update_field(FORM *form, int old_field)
3176a0e6850fSThomas Cort {
3177a0e6850fSThomas Cort 	int cur, i;
3178a0e6850fSThomas Cort 
3179a0e6850fSThomas Cort 	cur = form->cur_field;
3180a0e6850fSThomas Cort 
3181a0e6850fSThomas Cort 	if (old_field != cur) {
3182a0e6850fSThomas Cort 		if (!((cur >= form->page_starts[form->page].first) &&
3183a0e6850fSThomas Cort 		      (cur <= form->page_starts[form->page].last))) {
3184a0e6850fSThomas Cort 			  /* not on same page any more */
3185a0e6850fSThomas Cort 			for (i = 0; i < form->max_page; i++) {
3186a0e6850fSThomas Cort 				if ((form->page_starts[i].in_use == 1) &&
3187a0e6850fSThomas Cort 				    (form->page_starts[i].first <= cur) &&
3188a0e6850fSThomas Cort 				    (form->page_starts[i].last >= cur)) {
3189a0e6850fSThomas Cort 					form->page = i;
3190a0e6850fSThomas Cort 					return 1;
3191a0e6850fSThomas Cort 				}
3192a0e6850fSThomas Cort 			}
3193a0e6850fSThomas Cort 		}
3194a0e6850fSThomas Cort 	}
3195a0e6850fSThomas Cort 
3196a0e6850fSThomas Cort 	_formi_redraw_field(form, old_field);
3197a0e6850fSThomas Cort 	_formi_redraw_field(form, form->cur_field);
3198a0e6850fSThomas Cort 	return 0;
3199a0e6850fSThomas Cort }
3200a0e6850fSThomas Cort 
3201a0e6850fSThomas Cort /*
3202a0e6850fSThomas Cort  * Compare function for the field sorting
3203a0e6850fSThomas Cort  *
3204a0e6850fSThomas Cort  */
3205a0e6850fSThomas Cort static int
field_sort_compare(const void * one,const void * two)3206a0e6850fSThomas Cort field_sort_compare(const void *one, const void *two)
3207a0e6850fSThomas Cort {
3208a0e6850fSThomas Cort 	const FIELD *a, *b;
3209a0e6850fSThomas Cort 	int tl;
3210a0e6850fSThomas Cort 
3211a0e6850fSThomas Cort 	  /* LINTED const castaway; we don't modify these! */
3212a0e6850fSThomas Cort 	a = (const FIELD *) *((const FIELD **) one);
3213a0e6850fSThomas Cort 	b = (const FIELD *) *((const FIELD **) two);
3214a0e6850fSThomas Cort 
3215a0e6850fSThomas Cort 	if (a == NULL)
3216a0e6850fSThomas Cort 		return 1;
3217a0e6850fSThomas Cort 
3218a0e6850fSThomas Cort 	if (b == NULL)
3219a0e6850fSThomas Cort 		return -1;
3220a0e6850fSThomas Cort 
3221a0e6850fSThomas Cort 	  /*
3222a0e6850fSThomas Cort 	   * First check the page, we want the fields sorted by page.
3223a0e6850fSThomas Cort 	   *
3224a0e6850fSThomas Cort 	   */
3225a0e6850fSThomas Cort 	if (a->page != b->page)
3226a0e6850fSThomas Cort 		return ((a->page > b->page)? 1 : -1);
3227a0e6850fSThomas Cort 
3228a0e6850fSThomas Cort 	tl = _formi_top_left(a->parent, a->index, b->index);
3229a0e6850fSThomas Cort 
3230a0e6850fSThomas Cort 	  /*
3231a0e6850fSThomas Cort 	   * sort fields left to right, top to bottom so the top left is
3232a0e6850fSThomas Cort 	   * the lesser value....
3233a0e6850fSThomas Cort 	   */
3234a0e6850fSThomas Cort 	return ((tl == a->index)? -1 : 1);
3235a0e6850fSThomas Cort }
3236a0e6850fSThomas Cort 
3237a0e6850fSThomas Cort /*
3238a0e6850fSThomas Cort  * Sort the fields in a form ready for driver traversal.
3239a0e6850fSThomas Cort  */
3240a0e6850fSThomas Cort void
_formi_sort_fields(FORM * form)3241a0e6850fSThomas Cort _formi_sort_fields(FORM *form)
3242a0e6850fSThomas Cort {
3243a0e6850fSThomas Cort 	FIELD **sort_area;
3244a0e6850fSThomas Cort 	int i;
3245a0e6850fSThomas Cort 
3246*84d9c625SLionel Sambuc 	TAILQ_INIT(&form->sorted_fields);
3247a0e6850fSThomas Cort 
3248*84d9c625SLionel Sambuc 	if ((sort_area = malloc(sizeof(*sort_area) * form->field_count))
3249a0e6850fSThomas Cort 	    == NULL)
3250a0e6850fSThomas Cort 		return;
3251a0e6850fSThomas Cort 
3252a0e6850fSThomas Cort 	bcopy(form->fields, sort_area,
3253a0e6850fSThomas Cort 	      (size_t) (sizeof(FIELD *) * form->field_count));
3254a0e6850fSThomas Cort 	qsort(sort_area, (size_t) form->field_count, sizeof(FIELD *),
3255a0e6850fSThomas Cort 	      field_sort_compare);
3256a0e6850fSThomas Cort 
3257a0e6850fSThomas Cort 	for (i = 0; i < form->field_count; i++)
3258*84d9c625SLionel Sambuc 		TAILQ_INSERT_TAIL(&form->sorted_fields, sort_area[i], glue);
3259a0e6850fSThomas Cort 
3260a0e6850fSThomas Cort 	free(sort_area);
3261a0e6850fSThomas Cort }
3262a0e6850fSThomas Cort 
3263a0e6850fSThomas Cort /*
3264a0e6850fSThomas Cort  * Set the neighbours for all the fields in the given form.
3265a0e6850fSThomas Cort  */
3266a0e6850fSThomas Cort void
_formi_stitch_fields(FORM * form)3267a0e6850fSThomas Cort _formi_stitch_fields(FORM *form)
3268a0e6850fSThomas Cort {
3269a0e6850fSThomas Cort 	int above_row, below_row, end_above, end_below, cur_row, real_end;
3270a0e6850fSThomas Cort 	FIELD *cur, *above, *below;
3271a0e6850fSThomas Cort 
3272a0e6850fSThomas Cort 	  /*
3273a0e6850fSThomas Cort 	   * check if the sorted fields circle queue is empty, just
3274a0e6850fSThomas Cort 	   * return if it is.
3275a0e6850fSThomas Cort 	   */
3276*84d9c625SLionel Sambuc 	if (TAILQ_EMPTY(&form->sorted_fields))
3277a0e6850fSThomas Cort 		return;
3278a0e6850fSThomas Cort 
3279a0e6850fSThomas Cort 	  /* initially nothing is above..... */
3280a0e6850fSThomas Cort 	above_row = -1;
3281a0e6850fSThomas Cort 	end_above = TRUE;
3282a0e6850fSThomas Cort 	above = NULL;
3283a0e6850fSThomas Cort 
3284a0e6850fSThomas Cort 	  /* set up the first field as the current... */
3285*84d9c625SLionel Sambuc 	cur = TAILQ_FIRST(&form->sorted_fields);
3286a0e6850fSThomas Cort 	cur_row = cur->form_row;
3287a0e6850fSThomas Cort 
3288a0e6850fSThomas Cort 	  /* find the first field on the next row if any */
3289*84d9c625SLionel Sambuc 	below = TAILQ_NEXT(cur, glue);
3290a0e6850fSThomas Cort 	below_row = -1;
3291a0e6850fSThomas Cort 	end_below = TRUE;
3292a0e6850fSThomas Cort 	real_end = TRUE;
3293*84d9c625SLionel Sambuc 	while (below != NULL) {
3294a0e6850fSThomas Cort 		if (below->form_row != cur_row) {
3295a0e6850fSThomas Cort 			below_row = below->form_row;
3296a0e6850fSThomas Cort 			end_below = FALSE;
3297a0e6850fSThomas Cort 			real_end = FALSE;
3298a0e6850fSThomas Cort 			break;
3299a0e6850fSThomas Cort 		}
3300*84d9c625SLionel Sambuc 		below = TAILQ_NEXT(below, glue);
3301a0e6850fSThomas Cort 	}
3302a0e6850fSThomas Cort 
3303a0e6850fSThomas Cort 	  /* walk the sorted fields, setting the neighbour pointers */
3304*84d9c625SLionel Sambuc 	while (cur != NULL) {
3305*84d9c625SLionel Sambuc 		if (cur == TAILQ_FIRST(&form->sorted_fields))
3306a0e6850fSThomas Cort 			cur->left = NULL;
3307a0e6850fSThomas Cort 		else
3308*84d9c625SLionel Sambuc 			cur->left = TAILQ_PREV(cur, _formi_sort_head, glue);
3309a0e6850fSThomas Cort 
3310*84d9c625SLionel Sambuc 		if (cur == TAILQ_LAST(&form->sorted_fields, _formi_sort_head))
3311a0e6850fSThomas Cort 			cur->right = NULL;
3312a0e6850fSThomas Cort 		else
3313*84d9c625SLionel Sambuc 			cur->right = TAILQ_NEXT(cur, glue);
3314a0e6850fSThomas Cort 
3315a0e6850fSThomas Cort 		if (end_above == TRUE)
3316a0e6850fSThomas Cort 			cur->up = NULL;
3317a0e6850fSThomas Cort 		else {
3318a0e6850fSThomas Cort 			cur->up = above;
3319*84d9c625SLionel Sambuc 			above = TAILQ_NEXT(above, glue);
3320a0e6850fSThomas Cort 			if (above_row != above->form_row) {
3321a0e6850fSThomas Cort 				end_above = TRUE;
3322a0e6850fSThomas Cort 				above_row = above->form_row;
3323a0e6850fSThomas Cort 			}
3324a0e6850fSThomas Cort 		}
3325a0e6850fSThomas Cort 
3326a0e6850fSThomas Cort 		if (end_below == TRUE)
3327a0e6850fSThomas Cort 			cur->down = NULL;
3328a0e6850fSThomas Cort 		else {
3329a0e6850fSThomas Cort 			cur->down = below;
3330*84d9c625SLionel Sambuc 			below = TAILQ_NEXT(below, glue);
3331*84d9c625SLionel Sambuc 			if (below == NULL) {
3332a0e6850fSThomas Cort 				end_below = TRUE;
3333a0e6850fSThomas Cort 				real_end = TRUE;
3334a0e6850fSThomas Cort 			} else if (below_row != below->form_row) {
3335a0e6850fSThomas Cort 				end_below = TRUE;
3336a0e6850fSThomas Cort 				below_row = below->form_row;
3337a0e6850fSThomas Cort 			}
3338a0e6850fSThomas Cort 		}
3339a0e6850fSThomas Cort 
3340*84d9c625SLionel Sambuc 		cur = TAILQ_NEXT(cur, glue);
3341*84d9c625SLionel Sambuc 		if ((cur != NULL)
3342a0e6850fSThomas Cort 		    && (cur_row != cur->form_row)) {
3343a0e6850fSThomas Cort 			cur_row = cur->form_row;
3344a0e6850fSThomas Cort 			if (end_above == FALSE) {
3345*84d9c625SLionel Sambuc 				for (; above !=
3346*84d9c625SLionel Sambuc 				    TAILQ_FIRST(&form->sorted_fields);
3347*84d9c625SLionel Sambuc 				    above = TAILQ_NEXT(above, glue)) {
3348a0e6850fSThomas Cort 					if (above->form_row != above_row) {
3349a0e6850fSThomas Cort 						above_row = above->form_row;
3350a0e6850fSThomas Cort 						break;
3351a0e6850fSThomas Cort 					}
3352a0e6850fSThomas Cort 				}
3353a0e6850fSThomas Cort 			} else if (above == NULL) {
3354*84d9c625SLionel Sambuc 				above = TAILQ_FIRST(&form->sorted_fields);
3355a0e6850fSThomas Cort 				end_above = FALSE;
3356a0e6850fSThomas Cort 				above_row = above->form_row;
3357a0e6850fSThomas Cort 			} else
3358a0e6850fSThomas Cort 				end_above = FALSE;
3359a0e6850fSThomas Cort 
3360a0e6850fSThomas Cort 			if (end_below == FALSE) {
3361a0e6850fSThomas Cort 				while (below_row == below->form_row) {
3362*84d9c625SLionel Sambuc 					below = TAILQ_NEXT(below, glue);
3363*84d9c625SLionel Sambuc 					if (below == NULL) {
3364a0e6850fSThomas Cort 						real_end = TRUE;
3365a0e6850fSThomas Cort 						end_below = TRUE;
3366a0e6850fSThomas Cort 						break;
3367a0e6850fSThomas Cort 					}
3368a0e6850fSThomas Cort 				}
3369a0e6850fSThomas Cort 
3370*84d9c625SLionel Sambuc 				if (below != NULL)
3371a0e6850fSThomas Cort 					below_row = below->form_row;
3372a0e6850fSThomas Cort 			} else if (real_end == FALSE)
3373a0e6850fSThomas Cort 				end_below = FALSE;
3374a0e6850fSThomas Cort 
3375a0e6850fSThomas Cort 		}
3376a0e6850fSThomas Cort 	}
3377a0e6850fSThomas Cort }
3378a0e6850fSThomas Cort 
3379a0e6850fSThomas Cort /*
3380a0e6850fSThomas Cort  * Calculate the length of the displayed line allowing for any tab
3381a0e6850fSThomas Cort  * characters that need to be expanded.  We assume that the tab stops
3382a0e6850fSThomas Cort  * are 8 characters apart.  The parameters start and end are the
3383a0e6850fSThomas Cort  * character positions in the string str we want to get the length of,
3384a0e6850fSThomas Cort  * the function returns the number of characters from the start
3385a0e6850fSThomas Cort  * position to the end position that should be displayed after any
3386a0e6850fSThomas Cort  * intervening tabs have been expanded.
3387a0e6850fSThomas Cort  */
3388a0e6850fSThomas Cort int
_formi_tab_expanded_length(char * str,unsigned int start,unsigned int end)3389a0e6850fSThomas Cort _formi_tab_expanded_length(char *str, unsigned int start, unsigned int end)
3390a0e6850fSThomas Cort {
3391a0e6850fSThomas Cort 	int len, start_len, i;
3392a0e6850fSThomas Cort 
3393a0e6850fSThomas Cort 	  /* if we have a null string then there is no length */
3394a0e6850fSThomas Cort 	if (str[0] == '\0')
3395a0e6850fSThomas Cort 		return 0;
3396a0e6850fSThomas Cort 
3397a0e6850fSThomas Cort 	len = 0;
3398a0e6850fSThomas Cort 	start_len = 0;
3399a0e6850fSThomas Cort 
3400a0e6850fSThomas Cort 	  /*
3401a0e6850fSThomas Cort 	   * preceding tabs affect the length tabs in the span, so
3402a0e6850fSThomas Cort 	   * we need to calculate the length including the stuff before
3403a0e6850fSThomas Cort 	   * start and then subtract off the unwanted bit.
3404a0e6850fSThomas Cort 	   */
3405a0e6850fSThomas Cort 	for (i = 0; i <= end; i++) {
3406a0e6850fSThomas Cort 		if (i == start) /* stash preamble length for later */
3407a0e6850fSThomas Cort 			start_len = len;
3408a0e6850fSThomas Cort 
3409a0e6850fSThomas Cort 		if (str[i] == '\0')
3410a0e6850fSThomas Cort 			break;
3411a0e6850fSThomas Cort 
3412a0e6850fSThomas Cort 		if (str[i] == '\t')
3413a0e6850fSThomas Cort 			len = len - (len % 8) + 8;
3414a0e6850fSThomas Cort 		else
3415a0e6850fSThomas Cort 			len++;
3416a0e6850fSThomas Cort 	}
3417a0e6850fSThomas Cort 
3418a0e6850fSThomas Cort #ifdef DEBUG
3419a0e6850fSThomas Cort 	if (dbg != NULL) {
3420a0e6850fSThomas Cort 		fprintf(dbg,
3421a0e6850fSThomas Cort 		    "tab_expanded: start=%d, end=%d, expanded=%d (diff=%d)\n",
3422a0e6850fSThomas Cort 			start, end, (len - start_len), (end - start));
3423a0e6850fSThomas Cort 	}
3424a0e6850fSThomas Cort #endif
3425a0e6850fSThomas Cort 
3426a0e6850fSThomas Cort 	return (len - start_len);
3427a0e6850fSThomas Cort }
3428a0e6850fSThomas Cort 
3429a0e6850fSThomas Cort /*
3430a0e6850fSThomas Cort  * Calculate the tab stops on a given line in the field and set up
3431a0e6850fSThomas Cort  * the tabs list with the results.  We do this by scanning the line for tab
3432a0e6850fSThomas Cort  * characters and if one is found, noting the position and the number of
3433a0e6850fSThomas Cort  * characters to get to the next tab stop.  This information is kept to
3434a0e6850fSThomas Cort  * make manipulating the field (scrolling and so on) easier to handle.
3435a0e6850fSThomas Cort  */
3436a0e6850fSThomas Cort void
_formi_calculate_tabs(_FORMI_FIELD_LINES * row)3437a0e6850fSThomas Cort _formi_calculate_tabs(_FORMI_FIELD_LINES *row)
3438a0e6850fSThomas Cort {
3439a0e6850fSThomas Cort 	_formi_tab_t *ts = row->tabs, *old_ts = NULL, **tsp;
3440a0e6850fSThomas Cort 	int i, j;
3441a0e6850fSThomas Cort 
3442a0e6850fSThomas Cort 	  /*
3443a0e6850fSThomas Cort 	   * If the line already has tabs then invalidate them by
3444a0e6850fSThomas Cort 	   * walking the list and killing the in_use flag.
3445a0e6850fSThomas Cort 	   */
3446a0e6850fSThomas Cort 	for (; ts != NULL; ts = ts->fwd)
3447a0e6850fSThomas Cort 		ts->in_use = FALSE;
3448a0e6850fSThomas Cort 
3449a0e6850fSThomas Cort 
3450a0e6850fSThomas Cort 	  /*
3451a0e6850fSThomas Cort 	   * Now look for tabs in the row and record the info...
3452a0e6850fSThomas Cort 	   */
3453a0e6850fSThomas Cort 	tsp = &row->tabs;
3454a0e6850fSThomas Cort 	for (i = 0, j = 0; i < row->length; i++, j++) {
3455a0e6850fSThomas Cort 		if (row->string[i] == '\t') {
3456a0e6850fSThomas Cort 			if (*tsp == NULL) {
3457a0e6850fSThomas Cort 				if ((*tsp = (_formi_tab_t *)
3458a0e6850fSThomas Cort 				     malloc(sizeof(_formi_tab_t))) == NULL)
3459a0e6850fSThomas Cort 					return;
3460a0e6850fSThomas Cort 				(*tsp)->back = old_ts;
3461a0e6850fSThomas Cort 				(*tsp)->fwd = NULL;
3462a0e6850fSThomas Cort 			}
3463a0e6850fSThomas Cort 
3464a0e6850fSThomas Cort 			(*tsp)->in_use = TRUE;
3465a0e6850fSThomas Cort 			(*tsp)->pos = i;
3466a0e6850fSThomas Cort 			(*tsp)->size = 8 - (j % 8);
3467a0e6850fSThomas Cort 			j += (*tsp)->size - 1;
3468a0e6850fSThomas Cort 			old_ts = *tsp;
3469a0e6850fSThomas Cort 			tsp = &(*tsp)->fwd;
3470a0e6850fSThomas Cort 		}
3471a0e6850fSThomas Cort 	}
3472a0e6850fSThomas Cort }
3473a0e6850fSThomas Cort 
3474a0e6850fSThomas Cort /*
3475a0e6850fSThomas Cort  * Return the size of the tab padding for a tab character at the given
3476a0e6850fSThomas Cort  * position.  Return 1 if there is not a tab char entry matching the
3477a0e6850fSThomas Cort  * given location.
3478a0e6850fSThomas Cort  */
3479a0e6850fSThomas Cort static int
tab_size(_FORMI_FIELD_LINES * row,unsigned int i)3480a0e6850fSThomas Cort tab_size(_FORMI_FIELD_LINES *row, unsigned int i)
3481a0e6850fSThomas Cort {
3482a0e6850fSThomas Cort 	_formi_tab_t *ts;
3483a0e6850fSThomas Cort 
3484a0e6850fSThomas Cort 	ts = row->tabs;
3485a0e6850fSThomas Cort 	while ((ts != NULL) && (ts->pos != i))
3486a0e6850fSThomas Cort 		ts = ts->fwd;
3487a0e6850fSThomas Cort 
3488a0e6850fSThomas Cort 	if (ts == NULL)
3489a0e6850fSThomas Cort 		return 1;
3490a0e6850fSThomas Cort 	else
3491a0e6850fSThomas Cort 		return ts->size;
3492a0e6850fSThomas Cort }
3493a0e6850fSThomas Cort 
3494a0e6850fSThomas Cort /*
3495a0e6850fSThomas Cort  * Find the character offset that corresponds to longest tab expanded
3496a0e6850fSThomas Cort  * string that will fit into the given window.  Walk the string backwards
3497a0e6850fSThomas Cort  * evaluating the sizes of any tabs that are in the string.  Note that
3498a0e6850fSThomas Cort  * using this function on a multi-line window will produce undefined
3499a0e6850fSThomas Cort  * results - it is really only required for a single row field.
3500a0e6850fSThomas Cort  */
3501a0e6850fSThomas Cort static int
tab_fit_window(FIELD * field,unsigned int pos,unsigned int window)3502a0e6850fSThomas Cort tab_fit_window(FIELD *field, unsigned int pos, unsigned int window)
3503a0e6850fSThomas Cort {
3504a0e6850fSThomas Cort 	int scroll_amt, i;
3505a0e6850fSThomas Cort 	_formi_tab_t *ts;
3506a0e6850fSThomas Cort 
3507a0e6850fSThomas Cort 	  /* first find the last tab */
3508a0e6850fSThomas Cort 	ts = field->alines->tabs;
3509a0e6850fSThomas Cort 
3510a0e6850fSThomas Cort 	  /*
3511a0e6850fSThomas Cort 	   * unless there are no tabs - just return the window size,
3512a0e6850fSThomas Cort 	   * if there is enough room, otherwise 0.
3513a0e6850fSThomas Cort 	   */
3514a0e6850fSThomas Cort 	if (ts == NULL) {
3515a0e6850fSThomas Cort 		if (field->alines->length < window)
3516a0e6850fSThomas Cort 			return 0;
3517a0e6850fSThomas Cort 		else
3518a0e6850fSThomas Cort 			return field->alines->length - window + 1;
3519a0e6850fSThomas Cort 	}
3520a0e6850fSThomas Cort 
3521a0e6850fSThomas Cort 	while ((ts->fwd != NULL) && (ts->fwd->in_use == TRUE))
3522a0e6850fSThomas Cort 		ts = ts->fwd;
3523a0e6850fSThomas Cort 
3524a0e6850fSThomas Cort 	  /*
3525a0e6850fSThomas Cort 	   * now walk backwards finding the first tab that is to the
3526a0e6850fSThomas Cort 	   * left of our starting pos.
3527a0e6850fSThomas Cort 	   */
3528a0e6850fSThomas Cort 	while ((ts != NULL) && (ts->in_use == TRUE) && (ts->pos > pos))
3529a0e6850fSThomas Cort 		ts = ts->back;
3530a0e6850fSThomas Cort 
3531a0e6850fSThomas Cort 	scroll_amt = 0;
3532a0e6850fSThomas Cort 	for (i = pos; i >= 0; i--) {
3533a0e6850fSThomas Cort 		if (field->alines->string[i] == '\t') {
3534a0e6850fSThomas Cort 			assert((ts != NULL) && (ts->in_use == TRUE));
3535a0e6850fSThomas Cort 			if (ts->pos == i) {
3536a0e6850fSThomas Cort 				if ((scroll_amt + ts->size) > window) {
3537a0e6850fSThomas Cort 					break;
3538a0e6850fSThomas Cort 				}
3539a0e6850fSThomas Cort 				scroll_amt += ts->size;
3540a0e6850fSThomas Cort 				ts = ts->back;
3541a0e6850fSThomas Cort 			}
3542a0e6850fSThomas Cort 			else
3543a0e6850fSThomas Cort 				assert(ts->pos == i);
3544a0e6850fSThomas Cort 		} else {
3545a0e6850fSThomas Cort 			scroll_amt++;
3546a0e6850fSThomas Cort 			if (scroll_amt > window)
3547a0e6850fSThomas Cort 				break;
3548a0e6850fSThomas Cort 		}
3549a0e6850fSThomas Cort 	}
3550a0e6850fSThomas Cort 
3551a0e6850fSThomas Cort 	return ++i;
3552a0e6850fSThomas Cort }
3553a0e6850fSThomas Cort 
3554a0e6850fSThomas Cort /*
3555a0e6850fSThomas Cort  * Return the position of the last character that will fit into the
3556a0e6850fSThomas Cort  * given width after tabs have been expanded for a given row of a given
3557a0e6850fSThomas Cort  * field.
3558a0e6850fSThomas Cort  */
3559a0e6850fSThomas Cort static unsigned int
tab_fit_len(_FORMI_FIELD_LINES * row,unsigned int width)3560a0e6850fSThomas Cort tab_fit_len(_FORMI_FIELD_LINES *row, unsigned int width)
3561a0e6850fSThomas Cort {
3562a0e6850fSThomas Cort 	unsigned int pos, len, row_pos;
3563a0e6850fSThomas Cort 	_formi_tab_t *ts;
3564a0e6850fSThomas Cort 
3565a0e6850fSThomas Cort 	ts = row->tabs;
3566a0e6850fSThomas Cort 	pos = 0;
3567a0e6850fSThomas Cort 	len = 0;
3568a0e6850fSThomas Cort 	row_pos = 0;
3569a0e6850fSThomas Cort 
3570a0e6850fSThomas Cort 	if (width == 0)
3571a0e6850fSThomas Cort 		return 0;
3572a0e6850fSThomas Cort 
3573a0e6850fSThomas Cort 	while ((len < width) && (pos < row->length)) {
3574a0e6850fSThomas Cort 		if (row->string[pos] == '\t') {
3575a0e6850fSThomas Cort 			assert((ts != NULL) && (ts->in_use == TRUE));
3576a0e6850fSThomas Cort 			if (ts->pos == row_pos) {
3577a0e6850fSThomas Cort 				if ((len + ts->size) > width)
3578a0e6850fSThomas Cort 					break;
3579a0e6850fSThomas Cort 				len += ts->size;
3580a0e6850fSThomas Cort 				ts = ts->fwd;
3581a0e6850fSThomas Cort 			}
3582a0e6850fSThomas Cort 			else
3583a0e6850fSThomas Cort 				assert(ts->pos == row_pos);
3584a0e6850fSThomas Cort 		} else
3585a0e6850fSThomas Cort 			len++;
3586a0e6850fSThomas Cort 		pos++;
3587a0e6850fSThomas Cort 		row_pos++;
3588a0e6850fSThomas Cort 	}
3589a0e6850fSThomas Cort 
3590a0e6850fSThomas Cort 	if (pos > 0)
3591a0e6850fSThomas Cort 		pos--;
3592a0e6850fSThomas Cort 	return pos;
3593a0e6850fSThomas Cort }
3594a0e6850fSThomas Cort 
3595a0e6850fSThomas Cort /*
3596a0e6850fSThomas Cort  * Sync the field line structures with the contents of buffer 0 for that
3597a0e6850fSThomas Cort  * field.  We do this by walking all the line structures and concatenating
3598a0e6850fSThomas Cort  * all the strings into one single string in buffer 0.
3599a0e6850fSThomas Cort  */
3600a0e6850fSThomas Cort int
_formi_sync_buffer(FIELD * field)3601a0e6850fSThomas Cort _formi_sync_buffer(FIELD *field)
3602a0e6850fSThomas Cort {
3603a0e6850fSThomas Cort 	_FORMI_FIELD_LINES *line;
3604a0e6850fSThomas Cort 	char *nstr, *tmp;
3605a0e6850fSThomas Cort 	unsigned length;
3606a0e6850fSThomas Cort 
3607a0e6850fSThomas Cort 	if (field->alines == NULL)
3608a0e6850fSThomas Cort 		return E_BAD_ARGUMENT;
3609a0e6850fSThomas Cort 
3610a0e6850fSThomas Cort 	if (field->alines->string == NULL)
3611a0e6850fSThomas Cort 		return E_BAD_ARGUMENT;
3612a0e6850fSThomas Cort 
3613a0e6850fSThomas Cort 	  /*
3614a0e6850fSThomas Cort 	   * init nstr up front, just in case there are no line contents,
3615a0e6850fSThomas Cort 	   * this could happen if the field just contains hard returns.
3616a0e6850fSThomas Cort 	   */
3617a0e6850fSThomas Cort 	if ((nstr = malloc(sizeof(char))) == NULL)
3618a0e6850fSThomas Cort 		return E_SYSTEM_ERROR;
3619a0e6850fSThomas Cort 	nstr[0] = '\0';
3620a0e6850fSThomas Cort 
3621a0e6850fSThomas Cort 	line = field->alines;
3622a0e6850fSThomas Cort 	length = 1; /* allow for terminating null */
3623a0e6850fSThomas Cort 
3624a0e6850fSThomas Cort 	while (line != NULL) {
3625a0e6850fSThomas Cort 		if (line->length != 0) {
3626a0e6850fSThomas Cort 			if ((tmp = realloc(nstr,
3627a0e6850fSThomas Cort 					   (size_t) (length + line->length)))
3628a0e6850fSThomas Cort 			    == NULL) {
3629a0e6850fSThomas Cort 				if (nstr != NULL)
3630a0e6850fSThomas Cort 					free(nstr);
3631a0e6850fSThomas Cort 				return (E_SYSTEM_ERROR);
3632a0e6850fSThomas Cort 			}
3633a0e6850fSThomas Cort 
3634a0e6850fSThomas Cort 			nstr = tmp;
3635a0e6850fSThomas Cort 			strcat(nstr, line->string);
3636a0e6850fSThomas Cort 			length += line->length;
3637a0e6850fSThomas Cort 		}
3638a0e6850fSThomas Cort 
3639a0e6850fSThomas Cort 		line = line->next;
3640a0e6850fSThomas Cort 	}
3641a0e6850fSThomas Cort 
3642a0e6850fSThomas Cort 	if (field->buffers[0].string != NULL)
3643a0e6850fSThomas Cort 		free(field->buffers[0].string);
3644a0e6850fSThomas Cort 	field->buffers[0].allocated = length;
3645a0e6850fSThomas Cort 	field->buffers[0].length = length - 1;
3646a0e6850fSThomas Cort 	field->buffers[0].string = nstr;
3647a0e6850fSThomas Cort 	return E_OK;
3648a0e6850fSThomas Cort }
3649a0e6850fSThomas Cort 
3650a0e6850fSThomas Cort 
3651a0e6850fSThomas Cort 
3652