1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  *      Copyright (c) 1997, by Sun Microsystems, Inc.
28*7c478bd9Sstevel@tonic-gate  *      All rights reserved.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
34*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
35*7c478bd9Sstevel@tonic-gate #include "utility.h"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #define	AT_BOTTOM(f)	(Y(f) == Ymax(f) - 1)		/* last line	*/
38*7c478bd9Sstevel@tonic-gate #define	AT_END(f)	(Y(f) == Ymax(f) - 1 && X(f) == Xmax(f) - 1)
39*7c478bd9Sstevel@tonic-gate 							/* last char */
40*7c478bd9Sstevel@tonic-gate #define	AT_BEGINNING(f)	(Y(f) == 0 && X(f) == 0)	/* first char	*/
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate static int
room_for_line(FORM * f)43*7c478bd9Sstevel@tonic-gate room_for_line(FORM *f)
44*7c478bd9Sstevel@tonic-gate {
45*7c478bd9Sstevel@tonic-gate 	char *v;
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate 	_sync_buffer(f);
48*7c478bd9Sstevel@tonic-gate 	v = LineBuf(C(f), Ymax(f) - 1);
49*7c478bd9Sstevel@tonic-gate 	return (v == _data_end(v, Xmax(f)));	/* check for empty line */
50*7c478bd9Sstevel@tonic-gate }
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate static int
room_for_char(FORM * f)53*7c478bd9Sstevel@tonic-gate room_for_char(FORM *f)
54*7c478bd9Sstevel@tonic-gate {
55*7c478bd9Sstevel@tonic-gate 	WINDOW * w = W(f);
56*7c478bd9Sstevel@tonic-gate 	int c;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	(void) wmove(w, Y(f), Xmax(f) - 1);
59*7c478bd9Sstevel@tonic-gate 	c = (int)(winch(w) & A_CHARTEXT);
60*7c478bd9Sstevel@tonic-gate 	(void) wmove(w, Y(f), X(f));
61*7c478bd9Sstevel@tonic-gate 	return (c == Pad(C(f)));	/* check for empty char */
62*7c478bd9Sstevel@tonic-gate }
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate static int
extra_padding(char * str,int nstr)65*7c478bd9Sstevel@tonic-gate extra_padding(char *str, int nstr)		/* used for word wrapping */
66*7c478bd9Sstevel@tonic-gate {
67*7c478bd9Sstevel@tonic-gate 	int c = *(str + nstr - 1);
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	if (c == '"' || c == '\'')
70*7c478bd9Sstevel@tonic-gate 		c = *(str + nstr - 2);
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	return ((c == '.' || c == '?' || c == '!' || c == ':') ? 2 : 1);
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate }
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate BOOLEAN
_grow_field(FIELD * c,int chunks)77*7c478bd9Sstevel@tonic-gate _grow_field(FIELD *c, int chunks)
78*7c478bd9Sstevel@tonic-gate {
79*7c478bd9Sstevel@tonic-gate 	/* This function handles the growth of dymanically growable fields */
80*7c478bd9Sstevel@tonic-gate 	/* Returns TRUE if successful, FALSE otherwise	*/
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	FORM		*f = c->form;
83*7c478bd9Sstevel@tonic-gate 	WINDOW		*w = W(f);
84*7c478bd9Sstevel@tonic-gate 	BOOLEAN		current = Status(f, POSTED) && c == C(f);
85*7c478bd9Sstevel@tonic-gate 	char		*old_buf;
86*7c478bd9Sstevel@tonic-gate 	char		*new_buf;
87*7c478bd9Sstevel@tonic-gate 	char		*save;
88*7c478bd9Sstevel@tonic-gate 	int		old_len = BufSize(c);
89*7c478bd9Sstevel@tonic-gate 	int		grow;
90*7c478bd9Sstevel@tonic-gate 	int		lcv;
91*7c478bd9Sstevel@tonic-gate 	int		max = c->maxgrow;
92*7c478bd9Sstevel@tonic-gate 	int		i;
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	if (current && Status(f, WIN_CHG)) {
95*7c478bd9Sstevel@tonic-gate 		_win_to_buf(w, c);
96*7c478bd9Sstevel@tonic-gate 		Clr(f, WIN_CHG);
97*7c478bd9Sstevel@tonic-gate 		Set(f, BUF_CHG);
98*7c478bd9Sstevel@tonic-gate 	}
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	if (OneRow(c)) {
101*7c478bd9Sstevel@tonic-gate 		grow = chunks * c->cols;
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 		if (max)
104*7c478bd9Sstevel@tonic-gate 			grow = MIN(max - c->dcols, grow);
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 		c->dcols += grow;
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 		if (c->dcols == max)
109*7c478bd9Sstevel@tonic-gate 			Clr(c, GROWABLE);
110*7c478bd9Sstevel@tonic-gate 	} else {
111*7c478bd9Sstevel@tonic-gate 		grow = chunks * (c->rows + c->nrow);
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 		if (max)
114*7c478bd9Sstevel@tonic-gate 			grow = MIN(max - c->drows, grow);
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 		c->drows += grow;
117*7c478bd9Sstevel@tonic-gate 		grow *= c->cols;
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 		if (c->drows == max)
120*7c478bd9Sstevel@tonic-gate 			Clr(c, GROWABLE);
121*7c478bd9Sstevel@tonic-gate 	}
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	save = old_buf = Buf(c);
124*7c478bd9Sstevel@tonic-gate 	new_buf = Buf(c) = malloc(TotalBuf(c));
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	if (!new_buf)
127*7c478bd9Sstevel@tonic-gate 		return (FALSE);
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	lcv = c->nbuf + 1;
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < lcv; i++) {
132*7c478bd9Sstevel@tonic-gate 		(void) memcpy(new_buf, old_buf, old_len);
133*7c478bd9Sstevel@tonic-gate 		(void) memset(new_buf + old_len, ' ', grow);
134*7c478bd9Sstevel@tonic-gate 		old_buf += old_len + 1;
135*7c478bd9Sstevel@tonic-gate 		new_buf += old_len + grow;
136*7c478bd9Sstevel@tonic-gate 		*new_buf++ = '\0';
137*7c478bd9Sstevel@tonic-gate 	}
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	free(save);	/* delete old buffer */
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	if (current) {
142*7c478bd9Sstevel@tonic-gate 		(void) delwin(w);
143*7c478bd9Sstevel@tonic-gate 		W(f) = w = newwin(c->drows, c->dcols, 0, 0);
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 		if (!w)
146*7c478bd9Sstevel@tonic-gate 			return (FALSE);
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 		wbkgdset(w, Pad(c) | Back(c));
149*7c478bd9Sstevel@tonic-gate 		(void) wattrset(w, Fore(c));
150*7c478bd9Sstevel@tonic-gate 		(void) werase(w);
151*7c478bd9Sstevel@tonic-gate 		_buf_to_win(c, w);
152*7c478bd9Sstevel@tonic-gate 		(void) untouchwin(w);
153*7c478bd9Sstevel@tonic-gate 		(void) wmove(w, Y(f), X(f));
154*7c478bd9Sstevel@tonic-gate 	}
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	if (c->link != c) {
157*7c478bd9Sstevel@tonic-gate 		FIELD	*p = c->link;
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 		while (p != c) {
160*7c478bd9Sstevel@tonic-gate 			Buf(p) = Buf(c);
161*7c478bd9Sstevel@tonic-gate 			p->drows = c->drows;
162*7c478bd9Sstevel@tonic-gate 			p->dcols = c->dcols;
163*7c478bd9Sstevel@tonic-gate 			/* _sync_field(p) */
164*7c478bd9Sstevel@tonic-gate 			p = p->link;
165*7c478bd9Sstevel@tonic-gate 		}
166*7c478bd9Sstevel@tonic-gate 	}
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 	return (TRUE);
169*7c478bd9Sstevel@tonic-gate }
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate static int
insert_str(FORM * f,int y,int off,int nstr)172*7c478bd9Sstevel@tonic-gate insert_str(FORM *f, int y, int off, int nstr)	/* used for word wrapping */
173*7c478bd9Sstevel@tonic-gate {
174*7c478bd9Sstevel@tonic-gate 	WINDOW		*w	= W(f);
175*7c478bd9Sstevel@tonic-gate 	FIELD		*c	= C(f);
176*7c478bd9Sstevel@tonic-gate 	char		*vbeg	= LineBuf(c, y);
177*7c478bd9Sstevel@tonic-gate 	char		*v	= _data_end(vbeg, Xmax(f));
178*7c478bd9Sstevel@tonic-gate 	int		x	= (int)(v - vbeg);
179*7c478bd9Sstevel@tonic-gate 	int		n	= Xmax(f) - x;
180*7c478bd9Sstevel@tonic-gate 	int		pad	= extra_padding(Buf(c) + off, nstr);
181*7c478bd9Sstevel@tonic-gate 	int		siz	= nstr + 1 + pad;
182*7c478bd9Sstevel@tonic-gate 	int		ret 	= E_REQUEST_DENIED;
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	if (n >= siz) {	/* check for fit on this line */
185*7c478bd9Sstevel@tonic-gate 		(void) wmove(w, y, 0);
186*7c478bd9Sstevel@tonic-gate 		(void) winsnstr(w, Buf(c) + off, nstr);
187*7c478bd9Sstevel@tonic-gate 		(void) wmove(w, y, nstr);
188*7c478bd9Sstevel@tonic-gate 		(void) winsnstr(w, "  ", pad);
189*7c478bd9Sstevel@tonic-gate 	} else {		/* wrap */
190*7c478bd9Sstevel@tonic-gate 		if (y == Ymax(f) - 1 && Status(c, GROWABLE)) {
191*7c478bd9Sstevel@tonic-gate 			if (!_grow_field(c, 1))
192*7c478bd9Sstevel@tonic-gate 				return (E_SYSTEM_ERROR);
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 			vbeg = LineBuf(c, y);	/* grow changes buffer */
195*7c478bd9Sstevel@tonic-gate 			w = W(f);		/* grow changes window */
196*7c478bd9Sstevel@tonic-gate 		}
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 		v = _data_beg(vbeg + Xmax(f) - siz, siz);
199*7c478bd9Sstevel@tonic-gate 		v = _whsp_end(vbeg, (int)(v - vbeg));
200*7c478bd9Sstevel@tonic-gate 		x = (int)(v - vbeg);
201*7c478bd9Sstevel@tonic-gate 		n = Xmax(f) - x - n;
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 		if (y < Ymax(f) - 1 && (ret =
204*7c478bd9Sstevel@tonic-gate 		    insert_str(f, y+1, (int)(v - Buf(c)), n)) == E_OK) {
205*7c478bd9Sstevel@tonic-gate 			(void) wmove(w, y, x);
206*7c478bd9Sstevel@tonic-gate 			(void) wclrtoeol(w);
207*7c478bd9Sstevel@tonic-gate 			(void) wmove(w, y, 0);
208*7c478bd9Sstevel@tonic-gate 			(void) winsnstr(w, Buf(c) + off, nstr);
209*7c478bd9Sstevel@tonic-gate 			(void) wmove(w, y, nstr);
210*7c478bd9Sstevel@tonic-gate 			(void) winsnstr(w, "  ", pad);
211*7c478bd9Sstevel@tonic-gate 		} else
212*7c478bd9Sstevel@tonic-gate 			return (ret);	/* no room for wrap */
213*7c478bd9Sstevel@tonic-gate 	}
214*7c478bd9Sstevel@tonic-gate 	return (E_OK);
215*7c478bd9Sstevel@tonic-gate }
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate static int
wrap_ok(FORM * f)218*7c478bd9Sstevel@tonic-gate wrap_ok(FORM *f)		/* used for word wrapping */
219*7c478bd9Sstevel@tonic-gate {
220*7c478bd9Sstevel@tonic-gate /*
221*7c478bd9Sstevel@tonic-gate  * when this routine is called a char has already been added/inserted
222*7c478bd9Sstevel@tonic-gate  * on the screen at Y(f), X(f).  this routine checks to see if the current
223*7c478bd9Sstevel@tonic-gate  * line needs wrapping and if so attempts the wrap.  if unsuccessful
224*7c478bd9Sstevel@tonic-gate  * it deletes the char at Y(f), X(f) and returns FALSE.
225*7c478bd9Sstevel@tonic-gate  */
226*7c478bd9Sstevel@tonic-gate 	FIELD		*c = C(f);
227*7c478bd9Sstevel@tonic-gate 	BOOLEAN		at_bottom = AT_BOTTOM(f);
228*7c478bd9Sstevel@tonic-gate 	int		ret = E_REQUEST_DENIED;
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 	if (Opt(c, O_WRAP) && !OneRow(c) && !room_for_char(f) &&
231*7c478bd9Sstevel@tonic-gate 	    (!at_bottom || Status(c, GROWABLE))) {
232*7c478bd9Sstevel@tonic-gate 		WINDOW *w;
233*7c478bd9Sstevel@tonic-gate 		char *vbeg;
234*7c478bd9Sstevel@tonic-gate 		char *v;
235*7c478bd9Sstevel@tonic-gate 		int x, n;
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 		if (at_bottom && !_grow_field(c, 1))
238*7c478bd9Sstevel@tonic-gate 			return (E_SYSTEM_ERROR);
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 		vbeg = LineBuf(c, Y(f));
241*7c478bd9Sstevel@tonic-gate 		w = W(f);
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 		_win_to_buf(w, c);	/* sync buffer without changing flags */
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 		v = _whsp_end(vbeg, Xmax(f));
246*7c478bd9Sstevel@tonic-gate 		x = (int)(v - vbeg);
247*7c478bd9Sstevel@tonic-gate 		n = Xmax(f) - x;
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 		if (x && (ret = insert_str(f, Y(f)+1, (int)(v - Buf(c)), n)) ==
250*7c478bd9Sstevel@tonic-gate 		    E_OK) {
251*7c478bd9Sstevel@tonic-gate 			w = W(f);	/* window may change in insert_str */
252*7c478bd9Sstevel@tonic-gate 			(void) wmove(w, Y(f), x);
253*7c478bd9Sstevel@tonic-gate 			(void) wclrtoeol(w);
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 			if (X(f) >= x) {
256*7c478bd9Sstevel@tonic-gate 				++Y(f);
257*7c478bd9Sstevel@tonic-gate 				X(f) = X(f) - x;
258*7c478bd9Sstevel@tonic-gate 			}
259*7c478bd9Sstevel@tonic-gate 		} else {	/* error condition */
260*7c478bd9Sstevel@tonic-gate 			if (ret == E_SYSTEM_ERROR)
261*7c478bd9Sstevel@tonic-gate 				return (E_SYSTEM_ERROR);
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 			(void) wmove(w, Y(f), X(f));
264*7c478bd9Sstevel@tonic-gate 			(void) wdelch(w);	/* delete the char */
265*7c478bd9Sstevel@tonic-gate 			_win_to_buf(w, c);	/* restore buffer  */
266*7c478bd9Sstevel@tonic-gate 			return (E_REQUEST_DENIED);
267*7c478bd9Sstevel@tonic-gate 		}
268*7c478bd9Sstevel@tonic-gate 	}
269*7c478bd9Sstevel@tonic-gate 	return (E_OK);
270*7c478bd9Sstevel@tonic-gate }
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate int
_new_line(FORM * f)273*7c478bd9Sstevel@tonic-gate _new_line(FORM *f)
274*7c478bd9Sstevel@tonic-gate {
275*7c478bd9Sstevel@tonic-gate /*
276*7c478bd9Sstevel@tonic-gate  *		overloaded operation
277*7c478bd9Sstevel@tonic-gate  *
278*7c478bd9Sstevel@tonic-gate  *	if at beginning of field
279*7c478bd9Sstevel@tonic-gate  *		move to next field
280*7c478bd9Sstevel@tonic-gate  *
281*7c478bd9Sstevel@tonic-gate  *	else if in OVERLAY mode
282*7c478bd9Sstevel@tonic-gate  *		if on last line of field
283*7c478bd9Sstevel@tonic-gate  *			clear to eol and move to next field
284*7c478bd9Sstevel@tonic-gate  *		else
285*7c478bd9Sstevel@tonic-gate  *			clear to eol and move to beginning of next line
286*7c478bd9Sstevel@tonic-gate  *
287*7c478bd9Sstevel@tonic-gate  *	else if in INSERT mode
288*7c478bd9Sstevel@tonic-gate  *		if on last line of field
289*7c478bd9Sstevel@tonic-gate  *			move to next field
290*7c478bd9Sstevel@tonic-gate  *		else
291*7c478bd9Sstevel@tonic-gate  *			move text from cursor to eol to new line
292*7c478bd9Sstevel@tonic-gate  */
293*7c478bd9Sstevel@tonic-gate 	BOOLEAN		at_bottom = AT_BOTTOM(f);
294*7c478bd9Sstevel@tonic-gate 	FIELD *		c = C(f);
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate 	if (Opt(f, O_NL_OVERLOAD) && AT_BEGINNING(f))
297*7c478bd9Sstevel@tonic-gate 		return (_field_navigation(_next_field, f));
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate 	if (!Opt(c, O_EDIT))
300*7c478bd9Sstevel@tonic-gate 		return (E_REQUEST_DENIED);
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate 	if (Status(f, OVERLAY)) {		/* OVERLAY mode	*/
303*7c478bd9Sstevel@tonic-gate 		if (at_bottom && (!Status(c, GROWABLE) || OneRow(c))) {
304*7c478bd9Sstevel@tonic-gate 			if (Opt(f, O_NL_OVERLOAD)) {
305*7c478bd9Sstevel@tonic-gate 				(void) wclrtoeol(W(f));
306*7c478bd9Sstevel@tonic-gate 				Set(f, WIN_CHG);
307*7c478bd9Sstevel@tonic-gate 				return (_field_navigation(_next_field, f));
308*7c478bd9Sstevel@tonic-gate 			} else
309*7c478bd9Sstevel@tonic-gate 				return (E_REQUEST_DENIED);
310*7c478bd9Sstevel@tonic-gate 		}
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate 		if (at_bottom && !_grow_field(c, 1))
313*7c478bd9Sstevel@tonic-gate 			return (E_SYSTEM_ERROR);
314*7c478bd9Sstevel@tonic-gate 
315*7c478bd9Sstevel@tonic-gate 		(void) wclrtoeol(W(f));
316*7c478bd9Sstevel@tonic-gate 		++Y(f); X(f) = 0;
317*7c478bd9Sstevel@tonic-gate 	} else {		/* INSERT mode	*/
318*7c478bd9Sstevel@tonic-gate 		BOOLEAN		room;
319*7c478bd9Sstevel@tonic-gate 
320*7c478bd9Sstevel@tonic-gate 		if (at_bottom && (!Status(c, GROWABLE) || OneRow(c))) {
321*7c478bd9Sstevel@tonic-gate 			if (Opt(f, O_NL_OVERLOAD))
322*7c478bd9Sstevel@tonic-gate 				return (_field_navigation(_next_field, f));
323*7c478bd9Sstevel@tonic-gate 			else
324*7c478bd9Sstevel@tonic-gate 				return (E_REQUEST_DENIED);
325*7c478bd9Sstevel@tonic-gate 		}
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 		room = !at_bottom && room_for_line(f);
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate 		if (room || Status(c, GROWABLE)) {
330*7c478bd9Sstevel@tonic-gate 			WINDOW	*w;
331*7c478bd9Sstevel@tonic-gate 			char *v;
332*7c478bd9Sstevel@tonic-gate 			char *vend;
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate 			if (!room && !_grow_field(c, 1))
335*7c478bd9Sstevel@tonic-gate 				return (E_SYSTEM_ERROR);
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate 			w = W(f);
338*7c478bd9Sstevel@tonic-gate 			v = LineBuf(c, Y(f)) + X(f);
339*7c478bd9Sstevel@tonic-gate 			vend = _data_end(v, Xmax(f) - X(f));
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 			(void) wclrtoeol(w);
342*7c478bd9Sstevel@tonic-gate 			++Y(f); X(f) = 0;
343*7c478bd9Sstevel@tonic-gate 			(void) wmove(w, Y(f), X(f));
344*7c478bd9Sstevel@tonic-gate 			(void) winsertln(w);
345*7c478bd9Sstevel@tonic-gate 			(void) waddnstr(w, v, (int)(vend - v));
346*7c478bd9Sstevel@tonic-gate 		} else
347*7c478bd9Sstevel@tonic-gate 			return (E_REQUEST_DENIED);
348*7c478bd9Sstevel@tonic-gate 	}
349*7c478bd9Sstevel@tonic-gate 	Set(f, WIN_CHG);
350*7c478bd9Sstevel@tonic-gate 	return (E_OK);
351*7c478bd9Sstevel@tonic-gate }
352*7c478bd9Sstevel@tonic-gate 
353*7c478bd9Sstevel@tonic-gate /* _ins_char - insert blank char with error on overflow */
354*7c478bd9Sstevel@tonic-gate int
_ins_char(FORM * f)355*7c478bd9Sstevel@tonic-gate _ins_char(FORM *f)
356*7c478bd9Sstevel@tonic-gate {
357*7c478bd9Sstevel@tonic-gate 	FIELD	*c = C(f);
358*7c478bd9Sstevel@tonic-gate 	BOOLEAN	room = room_for_char(f);
359*7c478bd9Sstevel@tonic-gate 
360*7c478bd9Sstevel@tonic-gate 	if (CheckChar(c, ' ') && (room || (OneRow(c) &&
361*7c478bd9Sstevel@tonic-gate 	    Status(c, GROWABLE)))) {
362*7c478bd9Sstevel@tonic-gate 		if (!room && !_grow_field(c, 1))
363*7c478bd9Sstevel@tonic-gate 			return (E_SYSTEM_ERROR);
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate 		(void) winsch(W(f), ' ');
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate 		return (wrap_ok(f));
368*7c478bd9Sstevel@tonic-gate 	}
369*7c478bd9Sstevel@tonic-gate 	return (E_REQUEST_DENIED);
370*7c478bd9Sstevel@tonic-gate }
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate /* _ins_line -  insert blank line with error on overflow */
373*7c478bd9Sstevel@tonic-gate int
_ins_line(FORM * f)374*7c478bd9Sstevel@tonic-gate _ins_line(FORM *f)
375*7c478bd9Sstevel@tonic-gate {
376*7c478bd9Sstevel@tonic-gate 	BOOLEAN		room = !AT_BOTTOM(f) && room_for_line(f);
377*7c478bd9Sstevel@tonic-gate 	FIELD		*c = C(f);
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 	if (CheckChar(c, ' ') && !OneRow(c) && (room || Status(c, GROWABLE))) {
380*7c478bd9Sstevel@tonic-gate 		if (!room && !_grow_field(c, 1))
381*7c478bd9Sstevel@tonic-gate 			return (E_SYSTEM_ERROR);
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate 		X(f) = 0;
384*7c478bd9Sstevel@tonic-gate 		(void) winsertln(W(f));
385*7c478bd9Sstevel@tonic-gate 		return (E_OK);
386*7c478bd9Sstevel@tonic-gate 	}
387*7c478bd9Sstevel@tonic-gate 	return (E_REQUEST_DENIED);
388*7c478bd9Sstevel@tonic-gate }
389*7c478bd9Sstevel@tonic-gate 
390*7c478bd9Sstevel@tonic-gate /* _del_char - delete char at cursor */
391*7c478bd9Sstevel@tonic-gate int
_del_char(FORM * f)392*7c478bd9Sstevel@tonic-gate _del_char(FORM *f)
393*7c478bd9Sstevel@tonic-gate {
394*7c478bd9Sstevel@tonic-gate 	(void) wdelch(W(f));
395*7c478bd9Sstevel@tonic-gate 	return (E_OK);
396*7c478bd9Sstevel@tonic-gate }
397*7c478bd9Sstevel@tonic-gate 
398*7c478bd9Sstevel@tonic-gate int
_del_prev(FORM * f)399*7c478bd9Sstevel@tonic-gate _del_prev(FORM *f)
400*7c478bd9Sstevel@tonic-gate {
401*7c478bd9Sstevel@tonic-gate /*
402*7c478bd9Sstevel@tonic-gate  *		overloaded operation
403*7c478bd9Sstevel@tonic-gate  *
404*7c478bd9Sstevel@tonic-gate  *	if at beginning of field
405*7c478bd9Sstevel@tonic-gate  *		move to previous field
406*7c478bd9Sstevel@tonic-gate  *
407*7c478bd9Sstevel@tonic-gate  *	else if in OVERLAY mode
408*7c478bd9Sstevel@tonic-gate  *		if at beginning of line
409*7c478bd9Sstevel@tonic-gate  *			error
410*7c478bd9Sstevel@tonic-gate  *		else
411*7c478bd9Sstevel@tonic-gate  *			delete previous char
412*7c478bd9Sstevel@tonic-gate  *
413*7c478bd9Sstevel@tonic-gate  *	else if in INSERT mode
414*7c478bd9Sstevel@tonic-gate  *		if at beginning of line
415*7c478bd9Sstevel@tonic-gate  *			if current line can fit on preceding
416*7c478bd9Sstevel@tonic-gate  *				join current line with preceding line
417*7c478bd9Sstevel@tonic-gate  *			else
418*7c478bd9Sstevel@tonic-gate  *				error
419*7c478bd9Sstevel@tonic-gate  *		else
420*7c478bd9Sstevel@tonic-gate  *			delete previous char
421*7c478bd9Sstevel@tonic-gate  */
422*7c478bd9Sstevel@tonic-gate 	WINDOW *	w = W(f);
423*7c478bd9Sstevel@tonic-gate 	FIELD *		c = C(f);
424*7c478bd9Sstevel@tonic-gate 
425*7c478bd9Sstevel@tonic-gate 	if (AT_BEGINNING(f)) {
426*7c478bd9Sstevel@tonic-gate 		if (Opt(f, O_BS_OVERLOAD))
427*7c478bd9Sstevel@tonic-gate 			return (_field_navigation(_prev_field, f));
428*7c478bd9Sstevel@tonic-gate 		else
429*7c478bd9Sstevel@tonic-gate 			return (E_REQUEST_DENIED);
430*7c478bd9Sstevel@tonic-gate 	}
431*7c478bd9Sstevel@tonic-gate 	if (!Opt(c, O_EDIT))
432*7c478bd9Sstevel@tonic-gate 		return (E_REQUEST_DENIED);
433*7c478bd9Sstevel@tonic-gate 
434*7c478bd9Sstevel@tonic-gate 	if (--X(f) < 0) {
435*7c478bd9Sstevel@tonic-gate 		++X(f);
436*7c478bd9Sstevel@tonic-gate 
437*7c478bd9Sstevel@tonic-gate 		if (Status(f, OVERLAY))	/* OVERLAY mode	*/
438*7c478bd9Sstevel@tonic-gate 			return (E_REQUEST_DENIED);
439*7c478bd9Sstevel@tonic-gate 		else {			/* INSERT mode	*/
440*7c478bd9Sstevel@tonic-gate 			char *p = LineBuf(c, Y(f) - 1);
441*7c478bd9Sstevel@tonic-gate 			char *v = LineBuf(c, Y(f));
442*7c478bd9Sstevel@tonic-gate 			char *pend;
443*7c478bd9Sstevel@tonic-gate 			char *vend;
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate 			_sync_buffer(f);
446*7c478bd9Sstevel@tonic-gate 			pend = _data_end(p, Xmax(f));
447*7c478bd9Sstevel@tonic-gate 			vend = _data_end(v, Xmax(f));
448*7c478bd9Sstevel@tonic-gate 
449*7c478bd9Sstevel@tonic-gate 			if ((vend - v) > (Xmax(f) - (pend - p)))
450*7c478bd9Sstevel@tonic-gate 				return (E_REQUEST_DENIED);
451*7c478bd9Sstevel@tonic-gate 			else {
452*7c478bd9Sstevel@tonic-gate 				(void) wdeleteln(w);
453*7c478bd9Sstevel@tonic-gate 				_adjust_cursor(f, pend);
454*7c478bd9Sstevel@tonic-gate 				(void) wmove(w, Y(f), X(f));
455*7c478bd9Sstevel@tonic-gate 				(void) waddnstr(w, v, (int)(vend - v));
456*7c478bd9Sstevel@tonic-gate 			}
457*7c478bd9Sstevel@tonic-gate 		}
458*7c478bd9Sstevel@tonic-gate 	} else {
459*7c478bd9Sstevel@tonic-gate 		(void) wmove(w, Y(f), X(f));
460*7c478bd9Sstevel@tonic-gate 		(void) wdelch(w);
461*7c478bd9Sstevel@tonic-gate 	}
462*7c478bd9Sstevel@tonic-gate 	Set(f, WIN_CHG);
463*7c478bd9Sstevel@tonic-gate 	return (E_OK);
464*7c478bd9Sstevel@tonic-gate }
465*7c478bd9Sstevel@tonic-gate 
466*7c478bd9Sstevel@tonic-gate /* _del_line - delete current line */
467*7c478bd9Sstevel@tonic-gate int
_del_line(FORM * f)468*7c478bd9Sstevel@tonic-gate _del_line(FORM *f)
469*7c478bd9Sstevel@tonic-gate {
470*7c478bd9Sstevel@tonic-gate 	X(f) = 0;
471*7c478bd9Sstevel@tonic-gate 	(void) wdeleteln(W(f));
472*7c478bd9Sstevel@tonic-gate 	return (E_OK);
473*7c478bd9Sstevel@tonic-gate }
474*7c478bd9Sstevel@tonic-gate 
475*7c478bd9Sstevel@tonic-gate /* _del_word - delete word under cursor plus trailing blanks */
476*7c478bd9Sstevel@tonic-gate int
_del_word(FORM * f)477*7c478bd9Sstevel@tonic-gate _del_word(FORM *f)
478*7c478bd9Sstevel@tonic-gate {
479*7c478bd9Sstevel@tonic-gate 	FIELD *c = C(f);
480*7c478bd9Sstevel@tonic-gate 	WINDOW *w = W(f);
481*7c478bd9Sstevel@tonic-gate 	char *y = LineBuf(c, Y(f));
482*7c478bd9Sstevel@tonic-gate 	char *t = y + Xmax(f);
483*7c478bd9Sstevel@tonic-gate 	char *v = y + X(f);
484*7c478bd9Sstevel@tonic-gate 	char *x = v;
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate 	_sync_buffer(f);
487*7c478bd9Sstevel@tonic-gate 
488*7c478bd9Sstevel@tonic-gate 	if (*v == ' ')
489*7c478bd9Sstevel@tonic-gate 		return (E_REQUEST_DENIED);
490*7c478bd9Sstevel@tonic-gate 
491*7c478bd9Sstevel@tonic-gate 	_adjust_cursor(f, _whsp_end(y, X(f)));
492*7c478bd9Sstevel@tonic-gate 	(void) wmove(w, Y(f), X(f));
493*7c478bd9Sstevel@tonic-gate 	(void) wclrtoeol(w);
494*7c478bd9Sstevel@tonic-gate 
495*7c478bd9Sstevel@tonic-gate 	v = _whsp_beg(v, (int)(t - v));
496*7c478bd9Sstevel@tonic-gate 	v = _data_beg(v, (int)(t - v));
497*7c478bd9Sstevel@tonic-gate 
498*7c478bd9Sstevel@tonic-gate 	if (v != x && *v != ' ')
499*7c478bd9Sstevel@tonic-gate 		(void) waddnstr(w, v, (int)(_data_end(v, (int)(t - v)) - v));
500*7c478bd9Sstevel@tonic-gate 
501*7c478bd9Sstevel@tonic-gate 	return (E_OK);
502*7c478bd9Sstevel@tonic-gate }
503*7c478bd9Sstevel@tonic-gate 
504*7c478bd9Sstevel@tonic-gate /* _clr_eol - clear to end of line */
505*7c478bd9Sstevel@tonic-gate int
_clr_eol(FORM * f)506*7c478bd9Sstevel@tonic-gate _clr_eol(FORM *f)
507*7c478bd9Sstevel@tonic-gate {
508*7c478bd9Sstevel@tonic-gate 	(void) wclrtoeol(W(f));
509*7c478bd9Sstevel@tonic-gate 	return (E_OK);
510*7c478bd9Sstevel@tonic-gate }
511*7c478bd9Sstevel@tonic-gate 
512*7c478bd9Sstevel@tonic-gate /* _clr_eof - clear to end of field */
513*7c478bd9Sstevel@tonic-gate int
_clr_eof(FORM * f)514*7c478bd9Sstevel@tonic-gate _clr_eof(FORM *f)
515*7c478bd9Sstevel@tonic-gate {
516*7c478bd9Sstevel@tonic-gate 	(void) wclrtobot(W(f));
517*7c478bd9Sstevel@tonic-gate 	return (E_OK);
518*7c478bd9Sstevel@tonic-gate }
519*7c478bd9Sstevel@tonic-gate 
520*7c478bd9Sstevel@tonic-gate /* _clr_field - clear entire field */
521*7c478bd9Sstevel@tonic-gate int
_clr_field(FORM * f)522*7c478bd9Sstevel@tonic-gate _clr_field(FORM *f)
523*7c478bd9Sstevel@tonic-gate {
524*7c478bd9Sstevel@tonic-gate 	X(f) = 0; Y(f) = 0;
525*7c478bd9Sstevel@tonic-gate 	(void) werase(W(f));
526*7c478bd9Sstevel@tonic-gate 	return (E_OK);
527*7c478bd9Sstevel@tonic-gate }
528*7c478bd9Sstevel@tonic-gate 
529*7c478bd9Sstevel@tonic-gate /* _ovl_mode - go into overlay mode */
530*7c478bd9Sstevel@tonic-gate int
_ovl_mode(FORM * f)531*7c478bd9Sstevel@tonic-gate _ovl_mode(FORM *f)
532*7c478bd9Sstevel@tonic-gate {
533*7c478bd9Sstevel@tonic-gate 	Set(f, OVERLAY);
534*7c478bd9Sstevel@tonic-gate 	return (E_OK);
535*7c478bd9Sstevel@tonic-gate }
536*7c478bd9Sstevel@tonic-gate 
537*7c478bd9Sstevel@tonic-gate /* _ins_mode - go into insert mode */
538*7c478bd9Sstevel@tonic-gate int
_ins_mode(FORM * f)539*7c478bd9Sstevel@tonic-gate _ins_mode(FORM *f)
540*7c478bd9Sstevel@tonic-gate {
541*7c478bd9Sstevel@tonic-gate 	Clr(f, OVERLAY);
542*7c478bd9Sstevel@tonic-gate 	return (E_OK);
543*7c478bd9Sstevel@tonic-gate }
544*7c478bd9Sstevel@tonic-gate 
545*7c478bd9Sstevel@tonic-gate /* _validation - apply validation function associated with field type */
546*7c478bd9Sstevel@tonic-gate int
_validation(FORM * f)547*7c478bd9Sstevel@tonic-gate _validation(FORM *f)
548*7c478bd9Sstevel@tonic-gate {
549*7c478bd9Sstevel@tonic-gate 	return (_validate(f) ? E_OK : E_INVALID_FIELD);
550*7c478bd9Sstevel@tonic-gate }
551*7c478bd9Sstevel@tonic-gate 
552*7c478bd9Sstevel@tonic-gate /* _next_choice - apply next choice function associated with field type */
553*7c478bd9Sstevel@tonic-gate int
_next_choice(FORM * f)554*7c478bd9Sstevel@tonic-gate _next_choice(FORM *f)
555*7c478bd9Sstevel@tonic-gate {
556*7c478bd9Sstevel@tonic-gate 	_sync_buffer(f);
557*7c478bd9Sstevel@tonic-gate 	return (NextChoice(C(f)) ? E_OK : E_REQUEST_DENIED);
558*7c478bd9Sstevel@tonic-gate }
559*7c478bd9Sstevel@tonic-gate 
560*7c478bd9Sstevel@tonic-gate /* _prev_choice - apply previous choice function associated with field type */
561*7c478bd9Sstevel@tonic-gate int
_prev_choice(FORM * f)562*7c478bd9Sstevel@tonic-gate _prev_choice(FORM *f)
563*7c478bd9Sstevel@tonic-gate {
564*7c478bd9Sstevel@tonic-gate 	_sync_buffer(f);
565*7c478bd9Sstevel@tonic-gate 	return (PrevChoice(C(f)) ? E_OK : E_REQUEST_DENIED);
566*7c478bd9Sstevel@tonic-gate }
567*7c478bd9Sstevel@tonic-gate 
568*7c478bd9Sstevel@tonic-gate /*
569*7c478bd9Sstevel@tonic-gate  * _data_entry - enter printable ascii char ch
570*7c478bd9Sstevel@tonic-gate  * in current field at cursor position
571*7c478bd9Sstevel@tonic-gate  */
572*7c478bd9Sstevel@tonic-gate int
_data_entry(FORM * f,int ch)573*7c478bd9Sstevel@tonic-gate _data_entry(FORM *f, int ch)
574*7c478bd9Sstevel@tonic-gate {
575*7c478bd9Sstevel@tonic-gate 	FIELD *		c = C(f);	/* current field	*/
576*7c478bd9Sstevel@tonic-gate 	WINDOW *	w = W(f);	/* field window		*/
577*7c478bd9Sstevel@tonic-gate 	BOOLEAN		at_end;
578*7c478bd9Sstevel@tonic-gate 	int		ret;
579*7c478bd9Sstevel@tonic-gate 
580*7c478bd9Sstevel@tonic-gate 	if (!Opt(c, O_EDIT))
581*7c478bd9Sstevel@tonic-gate 		return (E_REQUEST_DENIED);
582*7c478bd9Sstevel@tonic-gate 
583*7c478bd9Sstevel@tonic-gate 	if (AT_BEGINNING(f) && Opt(c, O_BLANK) && ! Status(f, BUF_CHG) &&
584*7c478bd9Sstevel@tonic-gate 	    !Status(f, WIN_CHG))
585*7c478bd9Sstevel@tonic-gate 		(void) werase(w);
586*7c478bd9Sstevel@tonic-gate 
587*7c478bd9Sstevel@tonic-gate 	if (Status(f, OVERLAY))	/* OVERLAY mode	*/
588*7c478bd9Sstevel@tonic-gate 		(void) waddch(w, (chtype) ch);
589*7c478bd9Sstevel@tonic-gate 	else {				/* INSERT mode	*/
590*7c478bd9Sstevel@tonic-gate 		BOOLEAN	room = room_for_char(f);
591*7c478bd9Sstevel@tonic-gate 
592*7c478bd9Sstevel@tonic-gate 		if (room || (OneRow(c) && Status(c, GROWABLE))) {
593*7c478bd9Sstevel@tonic-gate 			if (!room && !_grow_field(c, 1))
594*7c478bd9Sstevel@tonic-gate 				return (E_SYSTEM_ERROR);
595*7c478bd9Sstevel@tonic-gate 
596*7c478bd9Sstevel@tonic-gate 			(void) winsch(w, (chtype) ch);
597*7c478bd9Sstevel@tonic-gate 		} else
598*7c478bd9Sstevel@tonic-gate 			return (E_REQUEST_DENIED);
599*7c478bd9Sstevel@tonic-gate 	}
600*7c478bd9Sstevel@tonic-gate 
601*7c478bd9Sstevel@tonic-gate 	if ((ret = wrap_ok(f)) != E_OK)
602*7c478bd9Sstevel@tonic-gate 		return (ret);
603*7c478bd9Sstevel@tonic-gate 
604*7c478bd9Sstevel@tonic-gate 	Set(f, WIN_CHG);
605*7c478bd9Sstevel@tonic-gate 
606*7c478bd9Sstevel@tonic-gate 	at_end = AT_END(f);
607*7c478bd9Sstevel@tonic-gate 
608*7c478bd9Sstevel@tonic-gate 	if (at_end && !Status(c, GROWABLE) && Opt(c, O_AUTOSKIP))
609*7c478bd9Sstevel@tonic-gate 		return (_field_navigation(_next_field, f));
610*7c478bd9Sstevel@tonic-gate 
611*7c478bd9Sstevel@tonic-gate 	if (at_end && Status(c, GROWABLE) && !_grow_field(c, 1))
612*7c478bd9Sstevel@tonic-gate 		return (E_SYSTEM_ERROR);
613*7c478bd9Sstevel@tonic-gate 
614*7c478bd9Sstevel@tonic-gate 	(void) _next_char(f);
615*7c478bd9Sstevel@tonic-gate 	return (E_OK);
616*7c478bd9Sstevel@tonic-gate }
617