xref: /illumos-gate/usr/src/lib/libcurses/screen/wgetwstr.c (revision 763f1f5f97e4c16840af2ced98915f0ed0f46616)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*  Copyright (c) 1988 AT&T */
23 /*    All Rights Reserved   */
24 
25 
26 /*
27  *      Copyright (c) 1997, by Sun Microsystems, Inc.
28  *      All rights reserved.
29  */
30 
31 /*LINTLIBRARY*/
32 
33 #include	<sys/types.h>
34 #include	"curses_inc.h"
35 
36 #define		LENGTH	256
37 
38 /* This routine gets a string starting at (_cury, _curx) */
39 int
40 wgetwstr(WINDOW *win, wchar_t *str)
41 {
42 	return ((wgetnwstr(win, str, LENGTH) == ERR) ? ERR : OK);
43 }
44 
45 int
46 wgetnwstr(WINDOW *win, wchar_t *str, int n)
47 {
48 	int	cpos = 0, ch;
49 	wchar_t	*cp = str;
50 	int	i = 0;
51 	int	total = 0;
52 	char	myerase, mykill;
53 	char	rownum[LENGTH], colnum[LENGTH], length[LENGTH];
54 	int	doecho = SP->fl_echoit;
55 	int	savecb = cur_term->_fl_rawmode;
56 	bool	savsync, savimmed, savleave;
57 
58 #ifdef	DEBUG
59 	if (outf)
60 		fprintf(outf, "doecho %d, savecb %d\n", doecho, savecb);
61 #endif	/* DEBUG */
62 
63 	myerase = erasechar();
64 	mykill = killchar();
65 	if (!savecb)
66 		(void) cbreak();
67 
68 	if (doecho) {
69 		SP->fl_echoit = FALSE;
70 		savsync = win->_sync;
71 		savimmed = win->_immed;
72 		savleave = win->_leave;
73 		win->_immed = win->_sync = win->_leave = FALSE;
74 		(void) wrefresh(win);
75 		if (n > LENGTH)
76 			n = LENGTH;
77 	}
78 	n--;
79 
80 	while (cpos < n) {
81 		if (doecho) {
82 			rownum[cpos] = win->_cury;
83 			colnum[cpos] = win->_curx;
84 		}
85 
86 		ch = wgetwch(win);
87 		if ((ch == ERR) || (ch == '\n') || (ch == '\r') ||
88 		    (ch == KEY_ENTER))
89 			break;
90 		if ((ch == myerase) || (ch == KEY_LEFT) ||
91 		    (ch == KEY_BACKSPACE) || (ch == mykill)) {
92 			if (cpos > 0) {
93 				if (ch == mykill) {
94 					i = total;
95 					total = cpos = 0;
96 					cp = str;
97 				} else {
98 					cp--;
99 					cpos--;
100 					if (doecho)
101 						total -= (i = length[cpos]);
102 				}
103 				if (doecho) {
104 					(void) wmove(win, rownum[cpos],
105 					    colnum[cpos]);
106 					/* Add the correct amount of blanks. */
107 					for (; i > 0; i--)
108 						(void) waddch(win, ' ');
109 					/* Move back after the blanks */
110 					/* are put in. */
111 					(void) wmove(win, rownum[cpos],
112 					    colnum[cpos]);
113 					/* Update total. */
114 					(void) wrefresh(win);
115 				}
116 			} else
117 				if (doecho)
118 					(void) beep();
119 		} else
120 			if ((KEY_MIN <= ch) && (ch <= KEY_MAX))
121 				(void) beep();
122 			else {
123 				*cp++ = ch;
124 				if (doecho) {
125 					/* Add the length of the */
126 					/* character to total. */
127 					if ((ch & EUCMASK) != P00)
128 						length[cpos] = wcscrw(ch);
129 					else if (ch >= ' ')
130 						length[cpos] = 1;
131 					else if (ch == '\t')
132 						length[cpos] = TABSIZE -
133 						    (colnum[cpos] % TABSIZE);
134 					else
135 						length[cpos] = 2;
136 					total += length[cpos];
137 					(void) wechowchar(win, (chtype) ch);
138 				}
139 				cpos++;
140 			}
141 	}
142 
143 	*cp = '\0';
144 
145 	if (!savecb)
146 		(void) nocbreak();
147 	/*
148 	 * The following code is equivalent to waddch(win, '\n')
149 	 * except that it does not do a wclrtoeol.
150 	 */
151 	if (doecho) {
152 		SP->fl_echoit = TRUE;
153 		win->_curx = 0;
154 		if (win->_cury + 1 > win->_bmarg)
155 			(void) wscrl(win, 1);
156 		else
157 			win->_cury++;
158 
159 		win->_sync = savsync;
160 		win->_immed = savimmed;
161 		win->_leave = savleave;
162 		(void) wrefresh(win);
163 	}
164 	return (ch);
165 }
166