xref: /onnv-gate/usr/src/lib/libcurses/screen/wgetstr.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*LINTLIBRARY*/
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #include	<sys/types.h>
45*0Sstevel@tonic-gate #include	"curses_inc.h"
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate #define		LENGTH	256
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate /* This routine gets a string starting at (_cury, _curx) */
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate int
wgetstr(WINDOW * win,char * str)52*0Sstevel@tonic-gate wgetstr(WINDOW *win, char *str)
53*0Sstevel@tonic-gate {
54*0Sstevel@tonic-gate 	return ((wgetnstr(win, str, LENGTH) == ERR) ? ERR : OK);
55*0Sstevel@tonic-gate }
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate int
wgetnstr(WINDOW * win,char * str,int n)58*0Sstevel@tonic-gate wgetnstr(WINDOW *win, char *str, int n)
59*0Sstevel@tonic-gate {
60*0Sstevel@tonic-gate 	int	cpos = 0, ch;
61*0Sstevel@tonic-gate 	int	nbyte = 0;
62*0Sstevel@tonic-gate 	int	tbyte = 0;
63*0Sstevel@tonic-gate 	int	byte[LENGTH];
64*0Sstevel@tonic-gate 	int	eucw, scrw;
65*0Sstevel@tonic-gate 	int	docont = 0;
66*0Sstevel@tonic-gate 	char	*cp = str;
67*0Sstevel@tonic-gate 	int	i = 0;
68*0Sstevel@tonic-gate 	int	total = 0;
69*0Sstevel@tonic-gate 	char	myerase, mykill;
70*0Sstevel@tonic-gate 	char	rownum[LENGTH], colnum[LENGTH], length[LENGTH];
71*0Sstevel@tonic-gate 	int	doecho = SP->fl_echoit;
72*0Sstevel@tonic-gate 	int	savecb = cur_term->_fl_rawmode;
73*0Sstevel@tonic-gate 	bool	savsync, savimmed, savleave;
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate #ifdef	DEBUG
76*0Sstevel@tonic-gate 	if (outf)
77*0Sstevel@tonic-gate 		fprintf(outf, "doecho %d, savecb %d\n", doecho, savecb);
78*0Sstevel@tonic-gate #endif	/* DEBUG */
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	myerase = erasechar();
81*0Sstevel@tonic-gate 	mykill = killchar();
82*0Sstevel@tonic-gate 	if (!savecb)
83*0Sstevel@tonic-gate 		(void) cbreak();
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	if (doecho) {
86*0Sstevel@tonic-gate 		SP->fl_echoit = FALSE;
87*0Sstevel@tonic-gate 		savsync = win->_sync;
88*0Sstevel@tonic-gate 		savimmed = win->_immed;
89*0Sstevel@tonic-gate 		savleave = win->_leave;
90*0Sstevel@tonic-gate 		win->_immed = win->_sync = win->_leave = FALSE;
91*0Sstevel@tonic-gate 		(void) wrefresh(win);
92*0Sstevel@tonic-gate 		if (n > LENGTH)
93*0Sstevel@tonic-gate 			n = LENGTH;
94*0Sstevel@tonic-gate 	}
95*0Sstevel@tonic-gate 	n--;
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	while (nbyte < n) {
98*0Sstevel@tonic-gate 		if (doecho && !docont) {
99*0Sstevel@tonic-gate 			rownum[cpos] = win->_cury;
100*0Sstevel@tonic-gate 			colnum[cpos] = win->_curx;
101*0Sstevel@tonic-gate 		}
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 		ch = wgetch(win);
104*0Sstevel@tonic-gate 		if (docont)
105*0Sstevel@tonic-gate 			goto cont;
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 		if ((ch == ERR) || (ch == '\n') || (ch == '\r') ||
108*0Sstevel@tonic-gate 		    (ch == KEY_ENTER))
109*0Sstevel@tonic-gate 			break;
110*0Sstevel@tonic-gate 		if ((ch == myerase) || (ch == KEY_LEFT) ||
111*0Sstevel@tonic-gate 		    (ch == KEY_BACKSPACE) || (ch == mykill)) {
112*0Sstevel@tonic-gate 			if (cpos > 0) {
113*0Sstevel@tonic-gate 				if (ch == mykill) {
114*0Sstevel@tonic-gate 					i = total;
115*0Sstevel@tonic-gate 					total = cpos = 0;
116*0Sstevel@tonic-gate 					nbyte = 0;
117*0Sstevel@tonic-gate 					cp = str;
118*0Sstevel@tonic-gate 				} else {
119*0Sstevel@tonic-gate 					cpos--;
120*0Sstevel@tonic-gate 					cp -= byte[cpos];
121*0Sstevel@tonic-gate 					if (doecho)
122*0Sstevel@tonic-gate 						total -= (i = length[cpos]);
123*0Sstevel@tonic-gate 				}
124*0Sstevel@tonic-gate 				if (doecho) {
125*0Sstevel@tonic-gate 					(void) wmove(win, rownum[cpos],
126*0Sstevel@tonic-gate 					    colnum[cpos]);
127*0Sstevel@tonic-gate 					/* Add the correct amount of blanks. */
128*0Sstevel@tonic-gate 					for (; i > 0; i--)
129*0Sstevel@tonic-gate 						(void) waddch(win, ' ');
130*0Sstevel@tonic-gate 					/* Move back after the blanks are */
131*0Sstevel@tonic-gate 					/* put in. */
132*0Sstevel@tonic-gate 					(void) wmove(win, rownum[cpos],
133*0Sstevel@tonic-gate 					    colnum[cpos]);
134*0Sstevel@tonic-gate 					/* Update total. */
135*0Sstevel@tonic-gate 					(void) wrefresh(win);
136*0Sstevel@tonic-gate 				}
137*0Sstevel@tonic-gate 			} else
138*0Sstevel@tonic-gate 				if (doecho)
139*0Sstevel@tonic-gate 					(void) beep();
140*0Sstevel@tonic-gate 		} else if ((KEY_MIN <= ch) && (ch <= KEY_MAX))
141*0Sstevel@tonic-gate 				(void) beep();
142*0Sstevel@tonic-gate 			else {
143*0Sstevel@tonic-gate cont:
144*0Sstevel@tonic-gate 				/* LINTED */
145*0Sstevel@tonic-gate 				*cp++ = (char)ch;
146*0Sstevel@tonic-gate 				if (docont) {
147*0Sstevel@tonic-gate 					tbyte++;
148*0Sstevel@tonic-gate 				} else if (ISMBIT(ch)) {
149*0Sstevel@tonic-gate 					docont = 1;
150*0Sstevel@tonic-gate 					tbyte = 1;
151*0Sstevel@tonic-gate 					scrw = mbscrw(ch);
152*0Sstevel@tonic-gate 					eucw = mbeucw(ch);
153*0Sstevel@tonic-gate 				}
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 				if (docont && (tbyte >= eucw)) {
156*0Sstevel@tonic-gate 					docont = 0;
157*0Sstevel@tonic-gate 					tbyte = 0;
158*0Sstevel@tonic-gate 					if (doecho) {
159*0Sstevel@tonic-gate 						byte[cpos] = eucw;
160*0Sstevel@tonic-gate 						/* LINTED */
161*0Sstevel@tonic-gate 						length[cpos] = (char)scrw;
162*0Sstevel@tonic-gate 						(void) wechochar(win,
163*0Sstevel@tonic-gate 						    (chtype) ch);
164*0Sstevel@tonic-gate 					}
165*0Sstevel@tonic-gate 				} else if (doecho) {
166*0Sstevel@tonic-gate 					/* Add the length of the */
167*0Sstevel@tonic-gate 					/* character to total. */
168*0Sstevel@tonic-gate 					byte[cpos] = 1;
169*0Sstevel@tonic-gate 					if (ch >= ' ')
170*0Sstevel@tonic-gate 						length[cpos] = 1;
171*0Sstevel@tonic-gate 					else
172*0Sstevel@tonic-gate 						if (ch == '\t')
173*0Sstevel@tonic-gate 							length[cpos] = TABSIZE-
174*0Sstevel@tonic-gate 							    (colnum[cpos] %
175*0Sstevel@tonic-gate 							    TABSIZE);
176*0Sstevel@tonic-gate 						else
177*0Sstevel@tonic-gate 							length[cpos] = 2;
178*0Sstevel@tonic-gate 					total += length[cpos];
179*0Sstevel@tonic-gate 					(void) wechochar(win, (chtype) ch);
180*0Sstevel@tonic-gate 				}
181*0Sstevel@tonic-gate 				if (!docont)
182*0Sstevel@tonic-gate 					cpos++;
183*0Sstevel@tonic-gate 				nbyte++;
184*0Sstevel@tonic-gate 			}
185*0Sstevel@tonic-gate 	}
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	*cp = '\0';
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	if (!savecb)
190*0Sstevel@tonic-gate 		(void) nocbreak();
191*0Sstevel@tonic-gate 	/*
192*0Sstevel@tonic-gate 	 * The following code is equivalent to waddch(win, '\n')
193*0Sstevel@tonic-gate 	 * except that it does not do a wclrtoeol.
194*0Sstevel@tonic-gate 	 */
195*0Sstevel@tonic-gate 	if (doecho) {
196*0Sstevel@tonic-gate 		SP->fl_echoit = TRUE;
197*0Sstevel@tonic-gate 		win->_curx = 0;
198*0Sstevel@tonic-gate 		if (win->_cury + 1 > win->_bmarg)
199*0Sstevel@tonic-gate 			(void) wscrl(win, 1);
200*0Sstevel@tonic-gate 		else
201*0Sstevel@tonic-gate 			win->_cury++;
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 		win->_sync = savsync;
204*0Sstevel@tonic-gate 		win->_immed = savimmed;
205*0Sstevel@tonic-gate 		win->_leave = savleave;
206*0Sstevel@tonic-gate 		(void) wrefresh(win);
207*0Sstevel@tonic-gate 	}
208*0Sstevel@tonic-gate 	return (ch);
209*0Sstevel@tonic-gate }
210