xref: /openbsd/lib/libcurses/base/lib_instr.c (revision 73471bf0)
1 /* $OpenBSD: lib_instr.c,v 1.3 2010/01/12 23:22:06 nicm Exp $ */
2 
3 /****************************************************************************
4  * Copyright (c) 1998-2005,2007 Free Software Foundation, Inc.              *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34  *     and: Thomas E. Dickey                        1996-on                 *
35  ****************************************************************************/
36 
37 /*
38 **	lib_instr.c
39 **
40 **	The routine winnstr().
41 **
42 */
43 
44 #include <curses.priv.h>
45 
46 MODULE_ID("$Id: lib_instr.c,v 1.3 2010/01/12 23:22:06 nicm Exp $")
47 
48 NCURSES_EXPORT(int)
49 winnstr(WINDOW *win, char *str, int n)
50 {
51     int i = 0, row, col;
52 
53     T((T_CALLED("winnstr(%p,%p,%d)"), win, str, n));
54 
55     if (!str)
56 	returnCode(0);
57 
58     if (win) {
59 	getyx(win, row, col);
60 
61 	if (n < 0)
62 	    n = win->_maxx - win->_curx + 1;
63 
64 	for (; i < n;) {
65 #if USE_WIDEC_SUPPORT
66 	    cchar_t *cell = &(win->_line[row].text[col]);
67 	    wchar_t *wch;
68 	    attr_t attrs;
69 	    short pair;
70 	    int n2;
71 	    bool done = FALSE;
72 	    mbstate_t state;
73 	    size_t i3, n3;
74 	    char *tmp;
75 
76 	    if (!isWidecExt(*cell)) {
77 		n2 = getcchar(cell, 0, 0, 0, 0);
78 		if (n2 > 0
79 		    && (wch = typeCalloc(wchar_t, (unsigned) n2 + 1)) != 0) {
80 		    if (getcchar(cell, wch, &attrs, &pair, 0) == OK) {
81 
82 			init_mb(state);
83 			n3 = wcstombs(0, wch, 0);
84 			if (isEILSEQ(n3) || (n3 == 0)) {
85 			    ;
86 			} else if ((int) (n3 + i) > n) {
87 			    done = TRUE;
88 			} else if ((tmp = typeCalloc(char, n3 + 10)) == 0) {
89 			    done = TRUE;
90 			} else {
91 			    init_mb(state);
92 			    wcstombs(tmp, wch, n3);
93 			    for (i3 = 0; i3 < n3; ++i3)
94 				str[i++] = tmp[i3];
95 			    free(tmp);
96 			}
97 		    }
98 		    free(wch);
99 		    if (done)
100 			break;
101 		}
102 	    }
103 #else
104 	    str[i++] = (char) CharOf(win->_line[row].text[col]);
105 #endif
106 	    if (++col > win->_maxx) {
107 		break;
108 	    }
109 	}
110     }
111     str[i] = '\0';		/* SVr4 does not seem to count the null */
112     T(("winnstr returns %s", _nc_visbuf(str)));
113     returnCode(i);
114 }
115