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