150276Speter/****************************************************************************
2262685Sdelphij * Copyright (c) 1998-2012,2013 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: Sven Verdoolaege                                                *
33166124Srafan *     and: Thomas E. Dickey                                                *
3450276Speter ****************************************************************************/
3550276Speter
3650276Speter/*
3750276Speter**	lib_insch.c
3850276Speter**
3950276Speter**	The routine winsch().
4050276Speter**
4150276Speter*/
4250276Speter
4350276Speter#include <curses.priv.h>
44166124Srafan#include <ctype.h>
4550276Speter
46262685SdelphijMODULE_ID("$Id: lib_insch.c,v 1.35 2013/05/18 21:58:56 tom Exp $")
4750276Speter
48166124Srafan/*
49166124Srafan * Insert the given character, updating the current location to simplify
50166124Srafan * inserting a string.
51166124Srafan */
5276726SpeterNCURSES_EXPORT(int)
53262629Sdelphij_nc_insert_ch(SCREEN *sp, WINDOW *win, chtype ch)
54166124Srafan{
55166124Srafan    int code = OK;
56262685Sdelphij    int ch8 = (int) ChCharOf(ch);
57166124Srafan    NCURSES_CH_T wch;
58166124Srafan    int count;
59166124Srafan    NCURSES_CONST char *s;
60262629Sdelphij    int tabsize = (
61262629Sdelphij#if USE_REENTRANT
62262629Sdelphij		      sp->_TABSIZE
63262629Sdelphij#else
64262629Sdelphij		      TABSIZE
65262629Sdelphij#endif
66262629Sdelphij    );
67166124Srafan
68166124Srafan    switch (ch) {
69166124Srafan    case '\t':
70262629Sdelphij	for (count = (tabsize - (win->_curx % tabsize)); count > 0; count--) {
71262629Sdelphij	    if ((code = _nc_insert_ch(sp, win, ' ')) != OK)
72166124Srafan		break;
73166124Srafan	}
74166124Srafan	break;
75166124Srafan    case '\n':
76166124Srafan    case '\r':
77166124Srafan    case '\b':
78166124Srafan	SetChar2(wch, ch);
79166124Srafan	_nc_waddch_nosync(win, wch);
80166124Srafan	break;
81166124Srafan    default:
82166124Srafan	if (
83166124Srafan#if USE_WIDEC_SUPPORT
84166124Srafan	       WINDOW_EXT(win, addch_used) == 0 &&
85166124Srafan#endif
86262685Sdelphij	       (isprint(ch8) ||
87262629Sdelphij		(ChAttrOf(ch) & A_ALTCHARSET) ||
88262685Sdelphij		(sp != 0 && sp->_legacy_coding && !iscntrl(ch8)))) {
89166124Srafan	    if (win->_curx <= win->_maxx) {
90166124Srafan		struct ldat *line = &(win->_line[win->_cury]);
91166124Srafan		NCURSES_CH_T *end = &(line->text[win->_curx]);
92166124Srafan		NCURSES_CH_T *temp1 = &(line->text[win->_maxx]);
93166124Srafan		NCURSES_CH_T *temp2 = temp1 - 1;
94166124Srafan
95166124Srafan		SetChar2(wch, ch);
96166124Srafan
97166124Srafan		CHANGED_TO_EOL(line, win->_curx, win->_maxx);
98166124Srafan		while (temp1 > end)
99166124Srafan		    *temp1-- = *temp2--;
100166124Srafan
101166124Srafan		*temp1 = _nc_render(win, wch);
102166124Srafan		win->_curx++;
103166124Srafan	    }
104262685Sdelphij	} else if (iscntrl(ch8)) {
105262685Sdelphij	    s = NCURSES_SP_NAME(unctrl) (NCURSES_SP_ARGx (chtype) ch8);
106166124Srafan	    while (*s != '\0') {
107262629Sdelphij		code = _nc_insert_ch(sp, win, ChAttrOf(ch) | UChar(*s));
108176187Srafan		if (code != OK)
109166124Srafan		    break;
110166124Srafan		++s;
111166124Srafan	    }
112166124Srafan	}
113166124Srafan#if USE_WIDEC_SUPPORT
114166124Srafan	else {
115166124Srafan	    /*
116166124Srafan	     * Handle multibyte characters here
117166124Srafan	     */
118166124Srafan	    SetChar2(wch, ch);
119166124Srafan	    wch = _nc_render(win, wch);
120176187Srafan	    count = _nc_build_wch(win, &wch);
121176187Srafan	    if (count > 0) {
122262629Sdelphij		code = _nc_insert_wch(win, &wch);
123176187Srafan	    } else if (count == -1) {
124176187Srafan		/* handle EILSEQ */
125262685Sdelphij		s = NCURSES_SP_NAME(unctrl) (NCURSES_SP_ARGx (chtype) ch8);
126262685Sdelphij		if (strlen(s) > 1) {
127262685Sdelphij		    while (*s != '\0') {
128262685Sdelphij			code = _nc_insert_ch(sp, win,
129262685Sdelphij					     ChAttrOf(ch) | UChar(*s));
130262685Sdelphij			if (code != OK)
131262685Sdelphij			    break;
132262685Sdelphij			++s;
133176187Srafan		    }
134176187Srafan		} else {
135176187Srafan		    code = ERR;
136176187Srafan		}
137176187Srafan	    }
138166124Srafan	}
139166124Srafan#endif
140166124Srafan	break;
141166124Srafan    }
142166124Srafan    return code;
143166124Srafan}
144166124Srafan
145166124SrafanNCURSES_EXPORT(int)
14676726Speterwinsch(WINDOW *win, chtype c)
14750276Speter{
148166124Srafan    NCURSES_SIZE_T oy;
149166124Srafan    NCURSES_SIZE_T ox;
15076726Speter    int code = ERR;
15150276Speter
152262629Sdelphij    T((T_CALLED("winsch(%p, %s)"), (void *) win, _tracechtype(c)));
15350276Speter
154166124Srafan    if (win != 0) {
155166124Srafan	oy = win->_cury;
156166124Srafan	ox = win->_curx;
15750276Speter
158262629Sdelphij	code = _nc_insert_ch(_nc_screen_of(win), win, c);
15950276Speter
160166124Srafan	win->_curx = ox;
161166124Srafan	win->_cury = oy;
162166124Srafan	_nc_synchook(win);
16376726Speter    }
16476726Speter    returnCode(code);
16550276Speter}
166