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