1 /****************************************************************************
2 * Copyright 2019-2021,2023 Thomas E. Dickey *
3 * Copyright 2004-2011,2016 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 ** lib_add_wch.c
32 **
33 ** The routine wadd_wch().
34 **
35 */
36
37 #include <curses.priv.h>
38
39 #if HAVE_WCTYPE_H
40 #include <wctype.h>
41 #endif
42
43 MODULE_ID("$Id: lib_add_wch.c,v 1.18 2023/07/15 17:34:12 tom Exp $")
44
45 /* clone/adapt lib_addch.c */
46 static const cchar_t blankchar = NewChar(BLANK_TEXT);
47
48 /*
49 * Ugly microtweaking alert. Everything from here to end of module is
50 * likely to be speed-critical -- profiling data sure says it is!
51 * Most of the important screen-painting functions are shells around
52 * wadd_wch(). So we make every effort to reduce function-call overhead
53 * by inlining stuff, even at the cost of making wrapped copies for
54 * export. Also we supply some internal versions that don't call the
55 * window sync hook, for use by string-put functions.
56 */
57
58 /* Return bit mask for clearing color pair number if given ch has color */
59 #define COLOR_MASK(ch) (~(attr_t)(((ch) & A_COLOR) ? A_COLOR : 0))
60
61 static NCURSES_INLINE cchar_t
render_char(WINDOW * win,cchar_t ch)62 render_char(WINDOW *win, cchar_t ch)
63 /* compute a rendition of the given char correct for the current context */
64 {
65 attr_t a = WINDOW_ATTRS(win);
66 int pair = GetPair(ch);
67
68 if (ISBLANK(ch)
69 && AttrOf(ch) == A_NORMAL
70 && pair == 0) {
71 /* color/pair in attrs has precedence over bkgrnd */
72 ch = win->_nc_bkgd;
73 SetAttr(ch, a | AttrOf(win->_nc_bkgd));
74 if ((pair = GET_WINDOW_PAIR(win)) == 0)
75 pair = GetPair(win->_nc_bkgd);
76 SetPair(ch, pair);
77 } else {
78 /* color in attrs has precedence over bkgrnd */
79 a |= AttrOf(win->_nc_bkgd) & COLOR_MASK(a);
80 /* color in ch has precedence */
81 if (pair == 0) {
82 if ((pair = GET_WINDOW_PAIR(win)) == 0)
83 pair = GetPair(win->_nc_bkgd);
84 }
85 AddAttr(ch, (a & COLOR_MASK(AttrOf(ch))));
86 SetPair(ch, pair);
87 }
88
89 TR(TRACE_VIRTPUT,
90 ("render_char bkg %s (%d), attrs %s (%d) -> ch %s (%d)",
91 _tracech_t2(1, CHREF(win->_nc_bkgd)),
92 GetPair(win->_nc_bkgd),
93 _traceattr(WINDOW_ATTRS(win)),
94 GET_WINDOW_PAIR(win),
95 _tracech_t2(3, CHREF(ch)),
96 GetPair(ch)));
97
98 return (ch);
99 }
100
101 /* check if position is legal; if not, return error */
102 #ifndef NDEBUG /* treat this like an assertion */
103 #define CHECK_POSITION(win, x, y) \
104 if (y > win->_maxy \
105 || x > win->_maxx \
106 || y < 0 \
107 || x < 0) { \
108 TR(TRACE_VIRTPUT, ("Alert! Win=%p _curx = %d, _cury = %d " \
109 "(_maxx = %d, _maxy = %d)", win, x, y, \
110 win->_maxx, win->_maxy)); \
111 return(ERR); \
112 }
113 #else
114 #define CHECK_POSITION(win, x, y) /* nothing */
115 #endif
116
117 static bool
newline_forces_scroll(WINDOW * win,NCURSES_SIZE_T * ypos)118 newline_forces_scroll(WINDOW *win, NCURSES_SIZE_T *ypos)
119 {
120 bool result = FALSE;
121
122 if (*ypos >= win->_regtop && *ypos == win->_regbottom) {
123 *ypos = win->_regbottom;
124 result = TRUE;
125 } else {
126 *ypos = (NCURSES_SIZE_T) (*ypos + 1);
127 }
128 return result;
129 }
130
131 /*
132 * The _WRAPPED flag is useful only for telling an application that we've just
133 * wrapped the cursor. We don't do anything with this flag except set it when
134 * wrapping, and clear it whenever we move the cursor. If we try to wrap at
135 * the lower-right corner of a window, we cannot move the cursor (since that
136 * wouldn't be legal). So we return an error (which is what SVr4 does).
137 * Unlike SVr4, we can successfully add a character to the lower-right corner
138 * (Solaris 2.6 does this also, however).
139 */
140 static int
wrap_to_next_line(WINDOW * win)141 wrap_to_next_line(WINDOW *win)
142 {
143 win->_flags |= _WRAPPED;
144 if (newline_forces_scroll(win, &(win->_cury))) {
145 win->_curx = win->_maxx;
146 if (!win->_scroll)
147 return (ERR);
148 scroll(win);
149 }
150 win->_curx = 0;
151 return (OK);
152 }
153
154 static int wadd_wch_literal(WINDOW *, cchar_t);
155 /*
156 * Fill the given number of cells with blanks using the current background
157 * rendition. This saves/restores the current x-position.
158 */
159 static void
fill_cells(WINDOW * win,int count)160 fill_cells(WINDOW *win, int count)
161 {
162 cchar_t blank = blankchar;
163 int save_x = win->_curx;
164 int save_y = win->_cury;
165
166 while (count-- > 0) {
167 if (wadd_wch_literal(win, blank) == ERR)
168 break;
169 }
170 win->_curx = (NCURSES_SIZE_T) save_x;
171 win->_cury = (NCURSES_SIZE_T) save_y;
172 }
173
174 static int
wadd_wch_literal(WINDOW * win,cchar_t ch)175 wadd_wch_literal(WINDOW *win, cchar_t ch)
176 {
177 int x;
178 int y;
179 struct ldat *line;
180
181 x = win->_curx;
182 y = win->_cury;
183
184 CHECK_POSITION(win, x, y);
185
186 ch = render_char(win, ch);
187
188 line = win->_line + y;
189
190 CHANGED_CELL(line, x);
191
192 /*
193 * Non-spacing characters are added to the current cell.
194 *
195 * Spacing characters that are wider than one column require some display
196 * adjustments.
197 */
198 {
199 int len = _nc_wacs_width(CharOf(ch));
200 int i;
201 int j;
202 wchar_t *chars;
203
204 if (len == 0) { /* non-spacing */
205 if ((x > 0 && y >= 0)
206 || (win->_maxx >= 0 && win->_cury >= 1)) {
207 if (x > 0 && y >= 0) {
208 for (j = x - 1; j > 0; --j) {
209 if (!isWidecExt(win->_line[y].text[j])) {
210 break;
211 }
212 }
213 chars = (win->_line[y].text[j].chars);
214 } else {
215 chars = (win->_line[y - 1].text[win->_maxx].chars);
216 }
217 for (i = 0; i < CCHARW_MAX; ++i) {
218 if (chars[i] == 0) {
219 TR(TRACE_VIRTPUT,
220 ("added non-spacing %d: %x",
221 x, (int) CharOf(ch)));
222 chars[i] = CharOf(ch);
223 break;
224 }
225 }
226 }
227 goto testwrapping;
228 } else if (len > 1) { /* multi-column characters */
229 /*
230 * Check if the character will fit on the current line. If it does
231 * not fit, fill in the remainder of the line with blanks. and
232 * move to the next line.
233 */
234 if (len > win->_maxx + 1) {
235 TR(TRACE_VIRTPUT, ("character will not fit"));
236 return ERR;
237 } else if (x + len > win->_maxx + 1) {
238 int count = win->_maxx + 1 - x;
239 TR(TRACE_VIRTPUT, ("fill %d remaining cells", count));
240 fill_cells(win, count);
241 if (wrap_to_next_line(win) == ERR)
242 return ERR;
243 x = win->_curx;
244 y = win->_cury;
245 line = win->_line + y;
246 }
247 /*
248 * Check for cells which are orphaned by adding this character, set
249 * those to blanks.
250 *
251 * FIXME: this actually could fill j-i cells, more complicated to
252 * setup though.
253 */
254 for (i = 0; i < len; ++i) {
255 if (isWidecBase(win->_line[y].text[x + i])) {
256 break;
257 } else if (isWidecExt(win->_line[y].text[x + i])) {
258 for (j = i; x + j <= win->_maxx; ++j) {
259 if (!isWidecExt(win->_line[y].text[x + j])) {
260 TR(TRACE_VIRTPUT, ("fill %d orphan cells", j));
261 fill_cells(win, j);
262 break;
263 }
264 }
265 break;
266 }
267 }
268 /*
269 * Finally, add the cells for this character.
270 */
271 for (i = 0; i < len; ++i) {
272 cchar_t value = ch;
273 SetWidecExt(value, i);
274 TR(TRACE_VIRTPUT, ("multicolumn %d:%d (%d,%d)",
275 i + 1, len,
276 win->_begy + y, win->_begx + x));
277 line->text[x] = value;
278 CHANGED_CELL(line, x);
279 ++x;
280 }
281 goto testwrapping;
282 }
283 }
284
285 /*
286 * Single-column characters.
287 */
288 line->text[x++] = ch;
289 /*
290 * This label is used only for wide-characters.
291 */
292 testwrapping:
293
294 TR(TRACE_VIRTPUT, ("cell (%ld, %ld..%d) = %s",
295 (long) win->_cury, (long) win->_curx, x - 1,
296 _tracech_t(CHREF(ch))));
297
298 if (x > win->_maxx) {
299 return wrap_to_next_line(win);
300 }
301 win->_curx = (NCURSES_SIZE_T) x;
302 return OK;
303 }
304
305 static NCURSES_INLINE int
wadd_wch_nosync(WINDOW * win,cchar_t ch)306 wadd_wch_nosync(WINDOW *win, cchar_t ch)
307 /* the workhorse function -- add a character to the given window */
308 {
309 NCURSES_SIZE_T x, y;
310 wchar_t *s;
311 int tabsize = 8;
312 #if USE_REENTRANT
313 SCREEN *sp = _nc_screen_of(win);
314 #endif
315
316 /*
317 * If we are using the alternate character set, forget about locale.
318 * Otherwise, if the locale claims the code is printable, treat it that
319 * way.
320 */
321 if ((AttrOf(ch) & A_ALTCHARSET)
322 || iswprint((wint_t) CharOf(ch)))
323 return wadd_wch_literal(win, ch);
324
325 /*
326 * Handle carriage control and other codes that are not printable, or are
327 * known to expand to more than one character according to unctrl().
328 */
329 x = win->_curx;
330 y = win->_cury;
331
332 switch (CharOf(ch)) {
333 case '\t':
334 #if USE_REENTRANT
335 tabsize = *ptrTabsize(sp);
336 #else
337 tabsize = TABSIZE;
338 #endif
339 x = (NCURSES_SIZE_T) (x + (tabsize - (x % tabsize)));
340 /*
341 * Space-fill the tab on the bottom line so that we'll get the
342 * "correct" cursor position.
343 */
344 if ((!win->_scroll && (y == win->_regbottom))
345 || (x <= win->_maxx)) {
346 cchar_t blank = blankchar;
347 AddAttr(blank, AttrOf(ch));
348 while (win->_curx < x) {
349 if (wadd_wch_literal(win, blank) == ERR)
350 return (ERR);
351 }
352 break;
353 } else {
354 wclrtoeol(win);
355 win->_flags |= _WRAPPED;
356 if (newline_forces_scroll(win, &y)) {
357 x = win->_maxx;
358 if (win->_scroll) {
359 scroll(win);
360 x = 0;
361 }
362 } else {
363 x = 0;
364 }
365 }
366 break;
367 case '\n':
368 wclrtoeol(win);
369 if (newline_forces_scroll(win, &y)) {
370 if (win->_scroll)
371 scroll(win);
372 else
373 return (ERR);
374 }
375 /* FALLTHRU */
376 case '\r':
377 x = 0;
378 win->_flags &= ~_WRAPPED;
379 break;
380 case '\b':
381 if (x == 0)
382 return (OK);
383 x--;
384 win->_flags &= ~_WRAPPED;
385 break;
386 default:
387 if ((s = wunctrl(&ch)) != 0) {
388 while (*s) {
389 cchar_t sch;
390 SetChar(sch, *s++, AttrOf(ch));
391 if_EXT_COLORS(SetPair(sch, GetPair(ch)));
392 if (wadd_wch_literal(win, sch) == ERR)
393 return ERR;
394 }
395 return OK;
396 }
397 return ERR;
398 }
399
400 win->_curx = x;
401 win->_cury = y;
402
403 return OK;
404 }
405
406 /*
407 * The versions below call _nc_synchook(). We wanted to avoid this in the
408 * version exported for string puts; they'll call _nc_synchook once at end
409 * of run.
410 */
411
412 /* These are actual entry points */
413
414 NCURSES_EXPORT(int)
wadd_wch(WINDOW * win,const cchar_t * wch)415 wadd_wch(WINDOW *win, const cchar_t *wch)
416 {
417 int code = ERR;
418
419 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wadd_wch(%p, %s)"),
420 (void *) win,
421 _tracecchar_t(wch)));
422
423 if (win && (wadd_wch_nosync(win, *wch) != ERR)) {
424 _nc_synchook(win);
425 code = OK;
426 }
427
428 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
429 return (code);
430 }
431
432 NCURSES_EXPORT(int)
wecho_wchar(WINDOW * win,const cchar_t * wch)433 wecho_wchar(WINDOW *win, const cchar_t *wch)
434 {
435 int code = ERR;
436
437 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wechochar(%p, %s)"),
438 (void *) win,
439 _tracecchar_t(wch)));
440
441 if (win && (wadd_wch_nosync(win, *wch) != ERR)) {
442 bool save_immed = win->_immed;
443 win->_immed = TRUE;
444 _nc_synchook(win);
445 win->_immed = save_immed;
446 code = OK;
447 }
448 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
449 return (code);
450 }
451