150276Speter/****************************************************************************
2174993Srafan * Copyright (c) 1998-2005,2007 Free Software Foundation, Inc.              *
350276Speter *                                                                          *
450276Speter * Permission is hereby granted, free of charge, to any person obtaining a  *
550276Speter * copy of this software and associated documentation files (the            *
650276Speter * "Software"), to deal in the Software without restriction, including      *
750276Speter * without limitation the rights to use, copy, modify, merge, publish,      *
850276Speter * distribute, distribute with modifications, sublicense, and/or sell       *
950276Speter * copies of the Software, and to permit persons to whom the Software is    *
1050276Speter * furnished to do so, subject to the following conditions:                 *
1150276Speter *                                                                          *
1250276Speter * The above copyright notice and this permission notice shall be included  *
1350276Speter * in all copies or substantial portions of the Software.                   *
1450276Speter *                                                                          *
1550276Speter * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1650276Speter * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
1750276Speter * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
1850276Speter * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
1950276Speter * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2050276Speter * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2150276Speter * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2250276Speter *                                                                          *
2350276Speter * Except as contained in this notice, the name(s) of the above copyright   *
2450276Speter * holders shall not be used in advertising or otherwise to promote the     *
2550276Speter * sale, use or other dealings in this Software without prior written       *
2650276Speter * authorization.                                                           *
2750276Speter ****************************************************************************/
2850276Speter
2950276Speter/****************************************************************************
3050276Speter *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
3150276Speter *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32166124Srafan *     and: Thomas E. Dickey                        1996-on                 *
3350276Speter ****************************************************************************/
3450276Speter
3550276Speter/*
3650276Speter**	lib_instr.c
3750276Speter**
3850276Speter**	The routine winnstr().
3950276Speter**
4050276Speter*/
4150276Speter
4250276Speter#include <curses.priv.h>
4350276Speter
44174993SrafanMODULE_ID("$Id: lib_instr.c,v 1.16 2007/07/21 20:18:10 tom Exp $")
4550276Speter
4676726SpeterNCURSES_EXPORT(int)
4776726Speterwinnstr(WINDOW *win, char *str, int n)
4850276Speter{
4976726Speter    int i = 0, row, col;
5050276Speter
5176726Speter    T((T_CALLED("winnstr(%p,%p,%d)"), win, str, n));
5250276Speter
5376726Speter    if (!str)
5476726Speter	returnCode(0);
5550276Speter
5676726Speter    if (win) {
5776726Speter	getyx(win, row, col);
5876726Speter
5976726Speter	if (n < 0)
6050276Speter	    n = win->_maxx - win->_curx + 1;
6150276Speter
6276726Speter	for (; i < n;) {
63166124Srafan#if USE_WIDEC_SUPPORT
64166124Srafan	    cchar_t *cell = &(win->_line[row].text[col]);
65166124Srafan	    wchar_t *wch;
66166124Srafan	    attr_t attrs;
67166124Srafan	    short pair;
68166124Srafan	    int n2;
69166124Srafan	    bool done = FALSE;
70166124Srafan	    mbstate_t state;
71166124Srafan	    size_t i3, n3;
72166124Srafan	    char *tmp;
73166124Srafan
74166124Srafan	    if (!isWidecExt(*cell)) {
75166124Srafan		n2 = getcchar(cell, 0, 0, 0, 0);
76166124Srafan		if (n2 > 0
77166124Srafan		    && (wch = typeCalloc(wchar_t, (unsigned) n2 + 1)) != 0) {
78166124Srafan		    if (getcchar(cell, wch, &attrs, &pair, 0) == OK) {
79166124Srafan
80166124Srafan			init_mb(state);
81166124Srafan			n3 = wcstombs(0, wch, 0);
82166124Srafan			if (isEILSEQ(n3) || (n3 == 0)) {
83166124Srafan			    ;
84174993Srafan			} else if ((int) (n3 + i) > n) {
85166124Srafan			    done = TRUE;
86166124Srafan			} else if ((tmp = typeCalloc(char, n3 + 10)) == 0) {
87166124Srafan			    done = TRUE;
88166124Srafan			} else {
89166124Srafan			    init_mb(state);
90166124Srafan			    wcstombs(tmp, wch, n3);
91166124Srafan			    for (i3 = 0; i3 < n3; ++i3)
92166124Srafan				str[i++] = tmp[i3];
93166124Srafan			    free(tmp);
94166124Srafan			}
95166124Srafan		    }
96166124Srafan		    free(wch);
97166124Srafan		    if (done)
98166124Srafan			break;
99166124Srafan		}
100166124Srafan	    }
101166124Srafan#else
102166124Srafan	    str[i++] = (char) CharOf(win->_line[row].text[col]);
103166124Srafan#endif
10450276Speter	    if (++col > win->_maxx) {
105174993Srafan		break;
10650276Speter	    }
10750276Speter	}
10876726Speter    }
10976726Speter    str[i] = '\0';		/* SVr4 does not seem to count the null */
110166124Srafan    T(("winnstr returns %s", _nc_visbuf(str)));
11176726Speter    returnCode(i);
11250276Speter}
113