1 /****************************************************************************
2  * Copyright 2019,2020 Thomas E. Dickey                                     *
3  * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 
30 /****************************************************************************
31  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
32  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
33  *                                                                          *
34  *  Rewritten 2001-2004 to support wide-characters by                       *
35  *	Sven Verdoolaege                                                    *
36  *	Thomas Dickey                                                       *
37  ****************************************************************************/
38 
39 /*
40 **	lib_addstr.c
41 *
42 **	The routines waddnstr(), waddchnstr().
43 **
44 */
45 
46 #include <curses.priv.h>
47 
48 MODULE_ID("$Id: lib_addstr.c,v 1.56 2020/02/02 23:34:34 tom Exp $")
49 
50 NCURSES_EXPORT(int)
51 waddnstr(WINDOW *win, const char *astr, int n)
52 {
53     const char *str = astr;
54     int code = ERR;
55 
56     T((T_CALLED("waddnstr(%p,%s,%d)"), (void *) win, _nc_visbufn(astr, n), n));
57 
58     if (win && (str != 0)) {
59 	TR(TRACE_VIRTPUT | TRACE_ATTRS,
60 	   ("... current %s", _traceattr(WINDOW_ATTRS(win))));
61 	code = OK;
62 	if (n < 0)
63 	    n = (int) strlen(astr);
64 
65 	TR(TRACE_VIRTPUT, ("str is not null, length = %d", n));
66 	while ((n-- > 0) && (*str != '\0')) {
67 	    NCURSES_CH_T ch;
68 	    TR(TRACE_VIRTPUT, ("*str = %#o", UChar(*str)));
69 	    SetChar(ch, UChar(*str++), A_NORMAL);
70 	    if (_nc_waddch_nosync(win, ch) == ERR) {
71 		code = ERR;
72 		break;
73 	    }
74 	}
75 	_nc_synchook(win);
76     }
77     TR(TRACE_VIRTPUT, ("waddnstr returns %d", code));
78     returnCode(code);
79 }
80 
81 NCURSES_EXPORT(int)
82 waddchnstr(WINDOW *win, const chtype *astr, int n)
83 {
84     NCURSES_SIZE_T y, x;
85     int code = OK;
86     int i;
87     struct ldat *line;
88 
89     T((T_CALLED("waddchnstr(%p,%p,%d)"), (void *) win, (const void *) astr, n));
90 
91     if (!win || !astr)
92 	returnCode(ERR);
93 
94     y = win->_cury;
95     x = win->_curx;
96     if (n < 0) {
97 	const chtype *str;
98 	n = 0;
99 	for (str = (const chtype *) astr; *str != 0; str++)
100 	    n++;
101     }
102     if (n > win->_maxx - x + 1)
103 	n = win->_maxx - x + 1;
104     if (n == 0)
105 	returnCode(code);
106 
107     line = &(win->_line[y]);
108     for (i = 0; i < n && ChCharOf(astr[i]) != '\0'; ++i) {
109 	SetChar2(line->text[i + x], astr[i]);
110     }
111     CHANGED_RANGE(line, x, (NCURSES_SIZE_T) (x + n - 1));
112 
113     _nc_synchook(win);
114     returnCode(code);
115 }
116 
117 #if USE_WIDEC_SUPPORT
118 
119 NCURSES_EXPORT(int)
120 _nc_wchstrlen(const cchar_t *s)
121 {
122     int result = 0;
123     if (s != 0) {
124 	while (CharOf(s[result]) != L'\0') {
125 	    result++;
126 	}
127     }
128     return result;
129 }
130 
131 NCURSES_EXPORT(int)
132 wadd_wchnstr(WINDOW *win, const cchar_t *astr, int n)
133 {
134     static const NCURSES_CH_T blank = NewChar(BLANK_TEXT);
135     NCURSES_SIZE_T y;
136     NCURSES_SIZE_T x;
137     int code = OK;
138     struct ldat *line;
139     int i, j, start, len, end;
140 
141     T((T_CALLED("wadd_wchnstr(%p,%s,%d)"),
142        (void *) win,
143        _nc_viscbuf(astr, n),
144        n));
145 
146     if (!win)
147 	returnCode(ERR);
148 
149     y = win->_cury;
150     x = win->_curx;
151     if (n < 0) {
152 	n = _nc_wchstrlen(astr);
153     }
154     if (n > win->_maxx - x + 1)
155 	n = win->_maxx - x + 1;
156     if (n == 0)
157 	returnCode(code);
158 
159     line = &(win->_line[y]);
160     start = x;
161     end = x + n - 1;
162 
163     /*
164      * Reset orphaned cells of multi-column characters that extend up to the
165      * new string's location to blanks.
166      */
167     if (x > 0 && isWidecExt(line->text[x])) {
168 	for (i = 0; i <= x; ++i) {
169 	    if (!isWidecExt(line->text[x - i])) {
170 		/* must be isWidecBase() */
171 		start -= i;
172 		while (i > 0) {
173 		    line->text[x - i--] = _nc_render(win, blank);
174 		}
175 		break;
176 	    }
177 	}
178     }
179 
180     /*
181      * Copy the new string to the window.
182      */
183     for (i = 0; i < n && CharOf(astr[i]) != L'\0' && x <= win->_maxx; ++i) {
184 	if (isWidecExt(astr[i]))
185 	    continue;
186 
187 	len = _nc_wacs_width(CharOf(astr[i]));
188 
189 	if (x + len - 1 <= win->_maxx) {
190 	    line->text[x] = _nc_render(win, astr[i]);
191 	    if (len > 1) {
192 		for (j = 0; j < len; ++j) {
193 		    if (j != 0) {
194 			line->text[x + j] = line->text[x];
195 		    }
196 		    SetWidecExt(line->text[x + j], j);
197 		}
198 	    } else {
199 		len = 1;
200 	    }
201 	    x = (NCURSES_SIZE_T) (x + len);
202 	    end += len - 1;
203 	} else {
204 	    break;
205 	}
206     }
207 
208     /*
209      * Set orphaned cells of multi-column characters which lie after the new
210      * string to blanks.
211      */
212     while (x <= win->_maxx && isWidecExt(line->text[x])) {
213 	line->text[x] = _nc_render(win, blank);
214 	++end;
215 	++x;
216     }
217     CHANGED_RANGE(line, start, end);
218 
219     _nc_synchook(win);
220     returnCode(code);
221 }
222 
223 NCURSES_EXPORT(int)
224 waddnwstr(WINDOW *win, const wchar_t *str, int n)
225 {
226     int code = ERR;
227 
228     T((T_CALLED("waddnwstr(%p,%s,%d)"), (void *) win, _nc_viswbufn(str, n), n));
229 
230     if (win && (str != 0)) {
231 	TR(TRACE_VIRTPUT | TRACE_ATTRS,
232 	   ("... current %s", _traceattr(WINDOW_ATTRS(win))));
233 	code = OK;
234 	if (n < 0)
235 	    n = (int) wcslen(str);
236 
237 	TR(TRACE_VIRTPUT, ("str is not null, length = %d", n));
238 	while ((n-- > 0) && (*str != L('\0'))) {
239 	    NCURSES_CH_T ch;
240 	    TR(TRACE_VIRTPUT, ("*str[0] = %#lx", (unsigned long) *str));
241 	    SetChar(ch, *str++, A_NORMAL);
242 	    if (wadd_wch(win, &ch) == ERR) {
243 		code = ERR;
244 		break;
245 	    }
246 	}
247 	_nc_synchook(win);
248     }
249     TR(TRACE_VIRTPUT, ("waddnwstr returns %d", code));
250     returnCode(code);
251 }
252 
253 #endif
254