1bacd15cpeter/****************************************************************************
201cf73abapt * Copyright 2019,2020 Thomas E. Dickey                                     *
301cf73abapt * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
4bacd15cpeter *                                                                          *
5bacd15cpeter * Permission is hereby granted, free of charge, to any person obtaining a  *
6bacd15cpeter * copy of this software and associated documentation files (the            *
7bacd15cpeter * "Software"), to deal in the Software without restriction, including      *
8bacd15cpeter * without limitation the rights to use, copy, modify, merge, publish,      *
9bacd15cpeter * distribute, distribute with modifications, sublicense, and/or sell       *
10bacd15cpeter * copies of the Software, and to permit persons to whom the Software is    *
11bacd15cpeter * furnished to do so, subject to the following conditions:                 *
12bacd15cpeter *                                                                          *
13bacd15cpeter * The above copyright notice and this permission notice shall be included  *
14bacd15cpeter * in all copies or substantial portions of the Software.                   *
15bacd15cpeter *                                                                          *
16bacd15cpeter * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17bacd15cpeter * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18bacd15cpeter * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19bacd15cpeter * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20bacd15cpeter * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21bacd15cpeter * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22bacd15cpeter * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23bacd15cpeter *                                                                          *
24bacd15cpeter * Except as contained in this notice, the name(s) of the above copyright   *
25bacd15cpeter * holders shall not be used in advertising or otherwise to promote the     *
26bacd15cpeter * sale, use or other dealings in this Software without prior written       *
27bacd15cpeter * authorization.                                                           *
28bacd15cpeter ****************************************************************************/
29bacd15cpeter
30bacd15cpeter/*
31bacd15cpeter**	lib_addch.c
32bacd15cpeter**
33bacd15cpeter**	The routine waddch().
34bacd15cpeter**
35bacd15cpeter*/
36bacd15cpeter
37bacd15cpeter#include <curses.priv.h>
38bacd15cpeter#include <ctype.h>
39bacd15cpeter
4001cf73abaptMODULE_ID("$Id: lib_addch.c,v 1.134 2020/02/02 23:34:34 tom Exp $")
41de9cbefrafan
42de9cbefrafanstatic const NCURSES_CH_T blankchar = NewChar(BLANK_TEXT);
43bacd15cpeter
44bacd15cpeter/*
45bacd15cpeter * Ugly microtweaking alert.  Everything from here to end of module is
46bacd15cpeter * likely to be speed-critical -- profiling data sure says it is!
47bacd15cpeter * Most of the important screen-painting functions are shells around
48bacd15cpeter * waddch().  So we make every effort to reduce function-call overhead
49bacd15cpeter * by inlining stuff, even at the cost of making wrapped copies for
50bacd15cpeter * export.  Also we supply some internal versions that don't call the
51bacd15cpeter * window sync hook, for use by string-put functions.
52bacd15cpeter */
53bacd15cpeter
54bacd15cpeter/* Return bit mask for clearing color pair number if given ch has color */
55f52ca6ebapt#define COLOR_MASK(ch) (~(attr_t)(((ch) & A_COLOR) ? A_COLOR : 0))
56bacd15cpeter
57de9cbefrafanstatic NCURSES_INLINE NCURSES_CH_T
589a7523dpeterrender_char(WINDOW *win, NCURSES_CH_T ch)
59bacd15cpeter/* compute a rendition of the given char correct for the current context */
60bacd15cpeter{
61de9cbefrafan    attr_t a = WINDOW_ATTRS(win);
62de9cbefrafan    int pair = GetPair(ch);
63cc6a5ccpeter
64de9cbefrafan    if (ISBLANK(ch)
65de9cbefrafan	&& AttrOf(ch) == A_NORMAL
66de9cbefrafan	&& pair == 0) {
67de9cbefrafan	/* color/pair in attrs has precedence over bkgrnd */
689a7523dpeter	ch = win->_nc_bkgd;
69de9cbefrafan	SetAttr(ch, a | AttrOf(win->_nc_bkgd));
70de9cbefrafan	if ((pair = GET_WINDOW_PAIR(win)) == 0)
71de9cbefrafan	    pair = GetPair(win->_nc_bkgd);
72de9cbefrafan	SetPair(ch, pair);
73cc6a5ccpeter    } else {
749a7523dpeter	/* color in attrs has precedence over bkgrnd */
75de9cbefrafan	a |= AttrOf(win->_nc_bkgd) & COLOR_MASK(a);
76cc6a5ccpeter	/* color in ch has precedence */
77de9cbefrafan	if (pair == 0) {
78de9cbefrafan	    if ((pair = GET_WINDOW_PAIR(win)) == 0)
79de9cbefrafan		pair = GetPair(win->_nc_bkgd);
80de9cbefrafan	}
819a7523dpeter	AddAttr(ch, (a & COLOR_MASK(AttrOf(ch))));
82de9cbefrafan	SetPair(ch, pair);
83cc6a5ccpeter    }
84cc6a5ccpeter
85de9cbefrafan    TR(TRACE_VIRTPUT,
86de9cbefrafan       ("render_char bkg %s (%d), attrs %s (%d) -> ch %s (%d)",
87de9cbefrafan	_tracech_t2(1, CHREF(win->_nc_bkgd)),
88de9cbefrafan	GetPair(win->_nc_bkgd),
89de9cbefrafan	_traceattr(WINDOW_ATTRS(win)),
90de9cbefrafan	GET_WINDOW_PAIR(win),
91de9cbefrafan	_tracech_t2(3, CHREF(ch)),
92de9cbefrafan	GetPair(ch)));
93cc6a5ccpeter
94cc6a5ccpeter    return (ch);
95bacd15cpeter}
96bacd15cpeter
979a7523dpeterNCURSES_EXPORT(NCURSES_CH_T)
989a7523dpeter_nc_render(WINDOW *win, NCURSES_CH_T ch)
99bacd15cpeter/* make render_char() visible while still allowing us to inline it below */
100bacd15cpeter{
101cc6a5ccpeter    return render_char(win, ch);
102bacd15cpeter}
103bacd15cpeter
104bacd15cpeter/* check if position is legal; if not, return error */
105bacd15cpeter#ifndef NDEBUG			/* treat this like an assertion */
106bacd15cpeter#define CHECK_POSITION(win, x, y) \
107bacd15cpeter	if (y > win->_maxy \
108bacd15cpeter	 || x > win->_maxx \
109bacd15cpeter	 || y < 0 \
110bacd15cpeter	 || x < 0) { \
111bacd15cpeter		TR(TRACE_VIRTPUT, ("Alert! Win=%p _curx = %d, _cury = %d " \
112bacd15cpeter				   "(_maxx = %d, _maxy = %d)", win, x, y, \
113bacd15cpeter				   win->_maxx, win->_maxy)); \
114bacd15cpeter		return(ERR); \
115bacd15cpeter	}
116bacd15cpeter#else
117cc6a5ccpeter#define CHECK_POSITION(win, x, y)	/* nothing */
118bacd15cpeter#endif
119bacd15cpeter
120de9cbefrafanstatic bool
121f52ca6ebaptnewline_forces_scroll(WINDOW *win, NCURSES_SIZE_T *ypos)
122de9cbefrafan{
123de9cbefrafan    bool result = FALSE;
124de9cbefrafan
125f52ca6ebapt    if (*ypos >= win->_regtop && *ypos <= win->_regbottom) {
126f52ca6ebapt	if (*ypos == win->_regbottom) {
127f52ca6ebapt	    *ypos = win->_regbottom;
128f52ca6ebapt	    result = TRUE;
129f52ca6ebapt	} else if (*ypos < win->_maxy) {
130f52ca6ebapt	    *ypos = (NCURSES_SIZE_T) (*ypos + 1);
131f52ca6ebapt	}
132f52ca6ebapt    } else if (*ypos < win->_maxy) {
1331034bfcdelphij	*ypos = (NCURSES_SIZE_T) (*ypos + 1);
134de9cbefrafan    }
135de9cbefrafan    return result;
136de9cbefrafan}
137de9cbefrafan
138de9cbefrafan/*
139de9cbefrafan * The _WRAPPED flag is useful only for telling an application that we've just
140de9cbefrafan * wrapped the cursor.  We don't do anything with this flag except set it when
141de9cbefrafan * wrapping, and clear it whenever we move the cursor.  If we try to wrap at
142de9cbefrafan * the lower-right corner of a window, we cannot move the cursor (since that
143de9cbefrafan * wouldn't be legal).  So we return an error (which is what SVr4 does).
144de9cbefrafan * Unlike SVr4, we can successfully add a character to the lower-right corner
145de9cbefrafan * (Solaris 2.6 does this also, however).
146de9cbefrafan */
147de9cbefrafanstatic int
148de9cbefrafanwrap_to_next_line(WINDOW *win)
149de9cbefrafan{
150de9cbefrafan    win->_flags |= _WRAPPED;
151de9cbefrafan    if (newline_forces_scroll(win, &(win->_cury))) {
152de9cbefrafan	win->_curx = win->_maxx;
153de9cbefrafan	if (!win->_scroll)
154de9cbefrafan	    return (ERR);
155de9cbefrafan	scroll(win);
156de9cbefrafan    }
157de9cbefrafan    win->_curx = 0;
158de9cbefrafan    return (OK);
159de9cbefrafan}
160de9cbefrafan
161de9cbefrafan#if USE_WIDEC_SUPPORT
162de9cbefrafanstatic int waddch_literal(WINDOW *, NCURSES_CH_T);
163de9cbefrafan/*
164de9cbefrafan * Fill the given number of cells with blanks using the current background
165de9cbefrafan * rendition.  This saves/restores the current x-position.
166de9cbefrafan */
167de9cbefrafanstatic void
168de9cbefrafanfill_cells(WINDOW *win, int count)
169de9cbefrafan{
170de9cbefrafan    NCURSES_CH_T blank = blankchar;
171de9cbefrafan    int save_x = win->_curx;
172de9cbefrafan    int save_y = win->_cury;
173de9cbefrafan
174de9cbefrafan    while (count-- > 0) {
175de9cbefrafan	if (waddch_literal(win, blank) == ERR)
176de9cbefrafan	    break;
177de9cbefrafan    }
1781034bfcdelphij    win->_curx = (NCURSES_SIZE_T) save_x;
1791034bfcdelphij    win->_cury = (NCURSES_SIZE_T) save_y;
180de9cbefrafan}
181de9cbefrafan#endif
182de9cbefrafan
183de9cbefrafan/*
184de9cbefrafan * Build up the bytes for a multibyte character, returning the length when
185de9cbefrafan * complete (a positive number), -1 for error and -2 for incomplete.
186de9cbefrafan */
187de9cbefrafan#if USE_WIDEC_SUPPORT
188de9cbefrafanNCURSES_EXPORT(int)
189de9cbefrafan_nc_build_wch(WINDOW *win, ARG_CH_T ch)
190de9cbefrafan{
191de9cbefrafan    char *buffer = WINDOW_EXT(win, addch_work);
192de9cbefrafan    int len;
193de9cbefrafan    int x = win->_curx;
194de9cbefrafan    int y = win->_cury;
195de9cbefrafan    mbstate_t state;
196de9cbefrafan    wchar_t result;
197de9cbefrafan
198de9cbefrafan    if ((WINDOW_EXT(win, addch_used) != 0) &&
199de9cbefrafan	(WINDOW_EXT(win, addch_x) != x ||
200de9cbefrafan	 WINDOW_EXT(win, addch_y) != y)) {
201de9cbefrafan	/* discard the incomplete multibyte character */
202de9cbefrafan	WINDOW_EXT(win, addch_used) = 0;
203de9cbefrafan	TR(TRACE_VIRTPUT,
204de9cbefrafan	   ("Alert discarded multibyte on move (%d,%d) -> (%d,%d)",
205de9cbefrafan	    WINDOW_EXT(win, addch_y), WINDOW_EXT(win, addch_x),
206de9cbefrafan	    y, x));
207de9cbefrafan    }
208de9cbefrafan    WINDOW_EXT(win, addch_x) = x;
209de9cbefrafan    WINDOW_EXT(win, addch_y) = y;
210de9cbefrafan
211de9cbefrafan    init_mb(state);
212d078158rafan    buffer[WINDOW_EXT(win, addch_used)] = (char) CharOf(CHDEREF(ch));
213de9cbefrafan    WINDOW_EXT(win, addch_used) += 1;
214de9cbefrafan    buffer[WINDOW_EXT(win, addch_used)] = '\0';
2151034bfcdelphij    if ((len = (int) mbrtowc(&result,
2161034bfcdelphij			     buffer,
2177dbeededelphij			     (size_t) WINDOW_EXT(win, addch_used),
2187dbeededelphij			     &state)) > 0) {
219de9cbefrafan	attr_t attrs = AttrOf(CHDEREF(ch));
220d036c70rafan	if_EXT_COLORS(int pair = GetPair(CHDEREF(ch)));
221de9cbefrafan	SetChar(CHDEREF(ch), result, attrs);
222d036c70rafan	if_EXT_COLORS(SetPair(CHDEREF(ch), pair));
223de9cbefrafan	WINDOW_EXT(win, addch_used) = 0;
22432c0e5frafan    } else if (len == -1) {
22532c0e5frafan	/*
22632c0e5frafan	 * An error occurred.  We could either discard everything,
22732c0e5frafan	 * or assume that the error was in the previous input.
22832c0e5frafan	 * Try the latter.
22932c0e5frafan	 */
23032c0e5frafan	TR(TRACE_VIRTPUT, ("Alert! mbrtowc returns error"));
23132c0e5frafan	/* handle this with unctrl() */
23232c0e5frafan	WINDOW_EXT(win, addch_used) = 0;
233de9cbefrafan    }
234de9cbefrafan    return len;
235de9cbefrafan}
236de9cbefrafan#endif /* USE_WIDEC_SUPPORT */
237de9cbefrafan
238de9cbefrafanstatic
239de9cbefrafan#if !USE_WIDEC_SUPPORT		/* cannot be inline if it is recursive */
240de9cbefrafanNCURSES_INLINE
241de9cbefrafan#endif
242de9cbefrafanint
2439a7523dpeterwaddch_literal(WINDOW *win, NCURSES_CH_T ch)
244bacd15cpeter{
245cc6a5ccpeter    int x;
246de9cbefrafan    int y;
247cc6a5ccpeter    struct ldat *line;
248cc6a5ccpeter
249cc6a5ccpeter    x = win->_curx;
250de9cbefrafan    y = win->_cury;
251cc6a5ccpeter
252de9cbefrafan    CHECK_POSITION(win, x, y);
253cc6a5ccpeter
254cc6a5ccpeter    ch = render_char(win, ch);
255bacd15cpeter
256de9cbefrafan    line = win->_line + y;
257bacd15cpeter
258cc6a5ccpeter    CHANGED_CELL(line, x);
259bacd15cpeter
260de9cbefrafan    /*
261de9cbefrafan     * Build up multibyte characters until we have a wide-character.
262de9cbefrafan     */
2631034bfcdelphij#if NCURSES_SP_FUNCS
2641034bfcdelphij#define DeriveSP() SCREEN *sp = _nc_screen_of(win);
2651034bfcdelphij#else
2661034bfcdelphij#define DeriveSP()		/*nothing */
2671034bfcdelphij#endif
2689a7523dpeter    if_WIDEC({
2691034bfcdelphij	DeriveSP();
270de9cbefrafan	if (WINDOW_EXT(win, addch_used) != 0 || !Charable(ch)) {
271de9cbefrafan	    int len = _nc_build_wch(win, CHREF(ch));
272de9cbefrafan
27332c0e5frafan	    if (len >= -1) {
2741034bfcdelphij		attr_t attr = AttrOf(ch);
2751034bfcdelphij
2761034bfcdelphij		/* handle EILSEQ (i.e., when len >= -1) */
2771034bfcdelphij		if (len == -1 && is8bits(CharOf(ch))) {
2781034bfcdelphij		    const char *s = NCURSES_SP_NAME(unctrl)
2791034bfcdelphij		      (NCURSES_SP_ARGx (chtype) CharOf(ch));
2801034bfcdelphij
2811034bfcdelphij		    if (s[1] != '\0') {
282f52ca6ebapt			int rc = OK;
2831034bfcdelphij			while (*s != '\0') {
2841034bfcdelphij			    rc = waddch(win, UChar(*s) | attr);
2851034bfcdelphij			    if (rc != OK)
2861034bfcdelphij				break;
2871034bfcdelphij			    ++s;
2881034bfcdelphij			}
2891034bfcdelphij			return rc;
290de9cbefrafan		    }
291de9cbefrafan		}
29232c0e5frafan		if (len == -1)
2931034bfcdelphij		    return waddch(win, ' ' | attr);
294de9cbefrafan	    } else {
295de9cbefrafan		return OK;
296de9cbefrafan	    }
297de9cbefrafan	}
2989a7523dpeter    });
299cc6a5ccpeter
300de9cbefrafan    /*
301de9cbefrafan     * Non-spacing characters are added to the current cell.
302de9cbefrafan     *
303de9cbefrafan     * Spacing characters that are wider than one column require some display
304de9cbefrafan     * adjustments.
305de9cbefrafan     */
306de9cbefrafan    if_WIDEC({
307f52ca6ebapt	int len = _nc_wacs_width(CharOf(ch));
308de9cbefrafan	int i;
309de9cbefrafan	int j;
310d078158rafan	wchar_t *chars;
311de9cbefrafan
312de9cbefrafan	if (len == 0) {		/* non-spacing */
313de9cbefrafan	    if ((x > 0 && y >= 0)
314d078158rafan		|| (win->_maxx >= 0 && win->_cury >= 1)) {
315d078158rafan		if (x > 0 && y >= 0)
316d078158rafan		    chars = (win->_line[y].text[x - 1].chars);
317d078158rafan		else
318d078158rafan		    chars = (win->_line[y - 1].text[win->_maxx].chars);
319de9cbefrafan		for (i = 0; i < CCHARW_MAX; ++i) {
320de9cbefrafan		    if (chars[i] == 0) {
321de9cbefrafan			TR(TRACE_VIRTPUT,
322de9cbefrafan			   ("added non-spacing %d: %x",
323de9cbefrafan			    x, (int) CharOf(ch)));
324de9cbefrafan			chars[i] = CharOf(ch);
325de9cbefrafan			break;
326de9cbefrafan		    }
327de9cbefrafan		}
328de9cbefrafan	    }
329de9cbefrafan	    goto testwrapping;
330de9cbefrafan	} else if (len > 1) {	/* multi-column characters */
331de9cbefrafan	    /*
332de9cbefrafan	     * Check if the character will fit on the current line.  If it does
333de9cbefrafan	     * not fit, fill in the remainder of the line with blanks.  and
334de9cbefrafan	     * move to the next line.
335de9cbefrafan	     */
336de9cbefrafan	    if (len > win->_maxx + 1) {
337de9cbefrafan		TR(TRACE_VIRTPUT, ("character will not fit"));
338de9cbefrafan		return ERR;
339de9cbefrafan	    } else if (x + len > win->_maxx + 1) {
340de9cbefrafan		int count = win->_maxx + 1 - x;
341de9cbefrafan		TR(TRACE_VIRTPUT, ("fill %d remaining cells", count));
342de9cbefrafan		fill_cells(win, count);
343de9cbefrafan		if (wrap_to_next_line(win) == ERR)
344de9cbefrafan		    return ERR;
345de9cbefrafan		x = win->_curx;
346de9cbefrafan		y = win->_cury;
347f52ca6ebapt		CHECK_POSITION(win, x, y);
3481034bfcdelphij		line = win->_line + y;
349de9cbefrafan	    }
350de9cbefrafan	    /*
351de9cbefrafan	     * Check for cells which are orphaned by adding this character, set
352de9cbefrafan	     * those to blanks.
353de9cbefrafan	     *
354de9cbefrafan	     * FIXME: this actually could fill j-i cells, more complicated to
355de9cbefrafan	     * setup though.
356de9cbefrafan	     */
357de9cbefrafan	    for (i = 0; i < len; ++i) {
358de9cbefrafan		if (isWidecBase(win->_line[y].text[x + i])) {
359de9cbefrafan		    break;
360de9cbefrafan		} else if (isWidecExt(win->_line[y].text[x + i])) {
361de9cbefrafan		    for (j = i; x + j <= win->_maxx; ++j) {
362de9cbefrafan			if (!isWidecExt(win->_line[y].text[x + j])) {
363de9cbefrafan			    TR(TRACE_VIRTPUT, ("fill %d orphan cells", j));
364de9cbefrafan			    fill_cells(win, j);
365de9cbefrafan			    break;
366de9cbefrafan			}
367de9cbefrafan		    }
368de9cbefrafan		    break;
369de9cbefrafan		}
370de9cbefrafan	    }
371de9cbefrafan	    /*
372de9cbefrafan	     * Finally, add the cells for this character.
373de9cbefrafan	     */
374de9cbefrafan	    for (i = 0; i < len; ++i) {
375de9cbefrafan		NCURSES_CH_T value = ch;
376de9cbefrafan		SetWidecExt(value, i);
377de9cbefrafan		TR(TRACE_VIRTPUT, ("multicolumn %d:%d (%d,%d)",
378de9cbefrafan				   i + 1, len,
379de9cbefrafan				   win->_begy + y, win->_begx + x));
380de9cbefrafan		line->text[x] = value;
381de9cbefrafan		CHANGED_CELL(line, x);
382de9cbefrafan		++x;
383de9cbefrafan	    }
384de9cbefrafan	    goto testwrapping;
385bacd15cpeter	}
386de9cbefrafan    });
387de9cbefrafan
388de9cbefrafan    /*
389de9cbefrafan     * Single-column characters.
390de9cbefrafan     */
391de9cbefrafan    line->text[x++] = ch;
392de9cbefrafan    /*
393de9cbefrafan     * This label is used only for wide-characters.
394de9cbefrafan     */
395de9cbefrafan    if_WIDEC(
396de9cbefrafan  testwrapping:
397de9cbefrafan    );
398de9cbefrafan
399de9cbefrafan    TR(TRACE_VIRTPUT, ("cell (%ld, %ld..%d) = %s",
400de9cbefrafan		       (long) win->_cury, (long) win->_curx, x - 1,
401de9cbefrafan		       _tracech_t(CHREF(ch))));
402de9cbefrafan
403de9cbefrafan    if (x > win->_maxx) {
404de9cbefrafan	return wrap_to_next_line(win);
405cc6a5ccpeter    }
4061034bfcdelphij    win->_curx = (NCURSES_SIZE_T) x;
407cc6a5ccpeter    return OK;
408bacd15cpeter}
409bacd15cpeter
410de9cbefrafanstatic NCURSES_INLINE int
4119a7523dpeterwaddch_nosync(WINDOW *win, const NCURSES_CH_T ch)
412bacd15cpeter/* the workhorse function -- add a character to the given window */
413bacd15cpeter{
414de9cbefrafan    NCURSES_SIZE_T x, y;
4151034bfcdelphij    chtype t = (chtype) CharOf(ch);
4161034bfcdelphij#if USE_WIDEC_SUPPORT || NCURSES_SP_FUNCS || USE_REENTRANT
4171034bfcdelphij    SCREEN *sp = _nc_screen_of(win);
4181034bfcdelphij#endif
4191034bfcdelphij    const char *s = NCURSES_SP_NAME(unctrl) (NCURSES_SP_ARGx t);
4201034bfcdelphij    int tabsize = 8;
4217dbeededelphij
422de9cbefrafan    /*
423de9cbefrafan     * If we are using the alternate character set, forget about locale.
424de9cbefrafan     * Otherwise, if unctrl() returns a single-character or the locale
4257dbeededelphij     * claims the code is printable (and not also a control character),
4267dbeededelphij     * treat it that way.
427de9cbefrafan     */
4289a7523dpeter    if ((AttrOf(ch) & A_ALTCHARSET)
429de9cbefrafan	|| (
430de9cbefrafan#if USE_WIDEC_SUPPORT
4311034bfcdelphij	       (sp != 0 && sp->_legacy_coding) &&
432de9cbefrafan#endif
433de9cbefrafan	       s[1] == 0
434de9cbefrafan	)
435de9cbefrafan	|| (
436f52ca6ebapt	       (isprint((int) t) && !iscntrl((int) t))
437de9cbefrafan#if USE_WIDEC_SUPPORT
4381034bfcdelphij	       || ((sp == 0 || !sp->_legacy_coding) &&
439de9cbefrafan		   (WINDOW_EXT(win, addch_used)
440de9cbefrafan		    || !_nc_is_charable(CharOf(ch))))
441de9cbefrafan#endif
4427dbeededelphij	)) {
443cc6a5ccpeter	return waddch_literal(win, ch);
4447dbeededelphij    }
445cc6a5ccpeter
446de9cbefrafan    /*
447de9cbefrafan     * Handle carriage control and other codes that are not printable, or are
448de9cbefrafan     * known to expand to more than one character according to unctrl().
449de9cbefrafan     */
450cc6a5ccpeter    x = win->_curx;
451cc6a5ccpeter    y = win->_cury;
452f52ca6ebapt    CHECK_POSITION(win, x, y);
453cc6a5ccpeter
454cc6a5ccpeter    switch (t) {
455cc6a5ccpeter    case '\t':
4561034bfcdelphij#if USE_REENTRANT
4571034bfcdelphij	tabsize = *ptrTabsize(sp);
4581034bfcdelphij#else
4591034bfcdelphij	tabsize = TABSIZE;
4601034bfcdelphij#endif
4611034bfcdelphij	x = (NCURSES_SIZE_T) (x + (tabsize - (x % tabsize)));
462cc6a5ccpeter	/*
463cc6a5ccpeter	 * Space-fill the tab on the bottom line so that we'll get the
464cc6a5ccpeter	 * "correct" cursor position.
465cc6a5ccpeter	 */
466cc6a5ccpeter	if ((!win->_scroll && (y == win->_regbottom))
467cc6a5ccpeter	    || (x <= win->_maxx)) {
468de9cbefrafan	    NCURSES_CH_T blank = blankchar;
4699a7523dpeter	    AddAttr(blank, AttrOf(ch));
470cc6a5ccpeter	    while (win->_curx < x) {
471cc6a5ccpeter		if (waddch_literal(win, blank) == ERR)
472cc6a5ccpeter		    return (ERR);
473cc6a5ccpeter	    }
474cc6a5ccpeter	    break;
475cc6a5ccpeter	} else {
476cc6a5ccpeter	    wclrtoeol(win);
477cc6a5ccpeter	    win->_flags |= _WRAPPED;
478de9cbefrafan	    if (newline_forces_scroll(win, &y)) {
479cc6a5ccpeter		x = win->_maxx;
480cc6a5ccpeter		if (win->_scroll) {
481cc6a5ccpeter		    scroll(win);
482cc6a5ccpeter		    x = 0;
483bacd15cpeter		}
484cc6a5ccpeter	    } else {
485bacd15cpeter		x = 0;
486cc6a5ccpeter	    }
487bacd15cpeter	}
488cc6a5ccpeter	break;
489cc6a5ccpeter    case '\n':
490cc6a5ccpeter	wclrtoeol(win);
491de9cbefrafan	if (newline_forces_scroll(win, &y)) {
492cc6a5ccpeter	    if (win->_scroll)
493cc6a5ccpeter		scroll(win);
494cc6a5ccpeter	    else
495cc6a5ccpeter		return (ERR);
496cc6a5ccpeter	}
497cc6a5ccpeter	/* FALLTHRU */
498cc6a5ccpeter    case '\r':
499cc6a5ccpeter	x = 0;
500cc6a5ccpeter	win->_flags &= ~_WRAPPED;
501cc6a5ccpeter	break;
502cc6a5ccpeter    case '\b':
503cc6a5ccpeter	if (x == 0)
504cc6a5ccpeter	    return (OK);
505cc6a5ccpeter	x--;
506cc6a5ccpeter	win->_flags &= ~_WRAPPED;
507cc6a5ccpeter	break;
508cc6a5ccpeter    default:
5099a7523dpeter	while (*s) {
5109a7523dpeter	    NCURSES_CH_T sch;
511f52ca6ebapt	    SetChar(sch, UChar(*s++), AttrOf(ch));
512d036c70rafan	    if_EXT_COLORS(SetPair(sch, GetPair(ch)));
5139a7523dpeter	    if (waddch_literal(win, sch) == ERR)
514cc6a5ccpeter		return ERR;
5159a7523dpeter	}
516cc6a5ccpeter	return (OK);
517cc6a5ccpeter    }
518cc6a5ccpeter
519cc6a5ccpeter    win->_curx = x;
520cc6a5ccpeter    win->_cury = y;
521cc6a5ccpeter
522cc6a5ccpeter    return (OK);
523bacd15cpeter}
524bacd15cpeter
525b7ada7fpeterNCURSES_EXPORT(int)
5269a7523dpeter_nc_waddch_nosync(WINDOW *win, const NCURSES_CH_T c)
527bacd15cpeter/* export copy of waddch_nosync() so the string-put functions can use it */
528bacd15cpeter{
529cc6a5ccpeter    return (waddch_nosync(win, c));
530bacd15cpeter}
531bacd15cpeter
532bacd15cpeter/*
533de9cbefrafan * The versions below call _nc_synchook().  We wanted to avoid this in the
534bacd15cpeter * version exported for string puts; they'll call _nc_synchook once at end
535bacd15cpeter * of run.
536bacd15cpeter */
537bacd15cpeter
538bacd15cpeter/* These are actual entry points */
539bacd15cpeter
540b7ada7fpeterNCURSES_EXPORT(int)
5419a7523dpeterwaddch(WINDOW *win, const chtype ch)
542bacd15cpeter{
543cc6a5ccpeter    int code = ERR;
5449a7523dpeter    NCURSES_CH_T wch;
5459a7523dpeter    SetChar2(wch, ch);
546bacd15cpeter
5471034bfcdelphij    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("waddch(%p, %s)"), (void *) win,
548b7ada7fpeter				      _tracechtype(ch)));
549bacd15cpeter
5509a7523dpeter    if (win && (waddch_nosync(win, wch) != ERR)) {
551cc6a5ccpeter	_nc_synchook(win);
552cc6a5ccpeter	code = OK;
553cc6a5ccpeter    }
554bacd15cpeter
555cc6a5ccpeter    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
556cc6a5ccpeter    return (code);
557bacd15cpeter}
558bacd15cpeter
559b7ada7fpeterNCURSES_EXPORT(int)
5609a7523dpeterwechochar(WINDOW *win, const chtype ch)
561bacd15cpeter{
562cc6a5ccpeter    int code = ERR;
5639a7523dpeter    NCURSES_CH_T wch;
5649a7523dpeter    SetChar2(wch, ch);
565cc6a5ccpeter
5661034bfcdelphij    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wechochar(%p, %s)"),
5671034bfcdelphij				      (void *) win,
568b7ada7fpeter				      _tracechtype(ch)));
569cc6a5ccpeter
5709a7523dpeter    if (win && (waddch_nosync(win, wch) != ERR)) {
5719a7523dpeter	bool save_immed = win->_immed;
5729a7523dpeter	win->_immed = TRUE;
5739a7523dpeter	_nc_synchook(win);
5749a7523dpeter	win->_immed = save_immed;
5759a7523dpeter	code = OK;
5769a7523dpeter    }
5779a7523dpeter    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
5789a7523dpeter    return (code);
5799a7523dpeter}
580