xref: /netbsd-src/lib/libcurses/addwstr.c (revision 6348e3f32a4431b45d07e1da16fd7b12725a45e7)
1*6348e3f3Sblymn /*   $NetBSD: addwstr.c,v 1.9 2024/12/23 02:58:03 blymn Exp $ */
2fa0b432bSblymn 
3fa0b432bSblymn /*
4fa0b432bSblymn  * Copyright (c) 2005 The NetBSD Foundation Inc.
5fa0b432bSblymn  * All rights reserved.
6fa0b432bSblymn  *
7fa0b432bSblymn  * This code is derived from code donated to the NetBSD Foundation
8fa0b432bSblymn  * by Ruibiao Qiu <ruibiao@arl.wustl.edu,ruibiao@gmail.com>.
9fa0b432bSblymn  *
10fa0b432bSblymn  *
11fa0b432bSblymn  * Redistribution and use in source and binary forms, with or without
12fa0b432bSblymn  * modification, are permitted provided that the following conditions
13fa0b432bSblymn  * are met:
14fa0b432bSblymn  * 1. Redistributions of source code must retain the above copyright
15fa0b432bSblymn  *	notice, this list of conditions and the following disclaimer.
16fa0b432bSblymn  * 2. Redistributions in binary form must reproduce the above copyright
17fa0b432bSblymn  *	notice, this list of conditions and the following disclaimer in the
18fa0b432bSblymn  *	documentation and/or other materials provided with the distribution.
19fa0b432bSblymn  * 3. Neither the name of the NetBSD Foundation nor the names of its
20fa0b432bSblymn  *	contributors may be used to endorse or promote products derived
21fa0b432bSblymn  *	from this software without specific prior written permission.
22fa0b432bSblymn  *
23fa0b432bSblymn  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
24fa0b432bSblymn  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25fa0b432bSblymn  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26fa0b432bSblymn  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27fa0b432bSblymn  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28fa0b432bSblymn  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29fa0b432bSblymn  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30fa0b432bSblymn  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31fa0b432bSblymn  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32fa0b432bSblymn  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33fa0b432bSblymn  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34fa0b432bSblymn  * SUCH DAMAGE.
35fa0b432bSblymn  */
36fa0b432bSblymn 
37fa0b432bSblymn #include <sys/cdefs.h>
38fa0b432bSblymn #ifndef lint
39*6348e3f3Sblymn __RCSID("$NetBSD: addwstr.c,v 1.9 2024/12/23 02:58:03 blymn Exp $");
40fa0b432bSblymn #endif						  /* not lint */
41fa0b432bSblymn 
42fa0b432bSblymn #include <string.h>
43fa0b432bSblymn 
44fa0b432bSblymn #include "curses.h"
45fa0b432bSblymn #include "curses_private.h"
46fa0b432bSblymn 
47fa0b432bSblymn /*
48fa0b432bSblymn  * addwstr --
49fa0b432bSblymn  *	  Add a string to stdscr starting at (_cury, _curx).
50fa0b432bSblymn  */
51fa0b432bSblymn int
52fa0b432bSblymn addwstr(const wchar_t *s)
53fa0b432bSblymn {
54fa0b432bSblymn 	return waddnwstr(stdscr, s, -1);
55fa0b432bSblymn }
56fa0b432bSblymn 
57fa0b432bSblymn /*
58fa0b432bSblymn  * waddwstr --
59fa0b432bSblymn  *	  Add a string to the given window starting at (_cury, _curx).
60fa0b432bSblymn  */
61fa0b432bSblymn int
62fa0b432bSblymn waddwstr(WINDOW *win, const wchar_t *s)
63fa0b432bSblymn {
64fa0b432bSblymn 	return waddnwstr(win, s, -1);
65fa0b432bSblymn }
66fa0b432bSblymn 
67fa0b432bSblymn /*
68fa0b432bSblymn  * addnwstr --
69fa0b432bSblymn  *	  Add a string (at most n characters) to stdscr starting
70fa0b432bSblymn  *	at (_cury, _curx).  If n is negative, add the entire string.
71fa0b432bSblymn  */
72fa0b432bSblymn int
73fa0b432bSblymn addnwstr(const wchar_t *str, int n)
74fa0b432bSblymn {
75fa0b432bSblymn 	return waddnwstr(stdscr, str, n);
76fa0b432bSblymn }
77fa0b432bSblymn 
78fa0b432bSblymn /*
79fa0b432bSblymn  * mvaddwstr --
80fa0b432bSblymn  *	  Add a string to stdscr starting at (y, x)
81fa0b432bSblymn  */
82fa0b432bSblymn int
83fa0b432bSblymn mvaddwstr(int y, int x, const wchar_t *str)
84fa0b432bSblymn {
85fa0b432bSblymn 	return mvwaddnwstr(stdscr, y, x, str, -1);
86fa0b432bSblymn }
87fa0b432bSblymn 
88fa0b432bSblymn /*
89fa0b432bSblymn  * mvwaddwstr --
90fa0b432bSblymn  *	  Add a string to the given window starting at (y, x)
91fa0b432bSblymn  */
92fa0b432bSblymn int
93fa0b432bSblymn mvwaddwstr(WINDOW *win, int y, int x, const wchar_t *str)
94fa0b432bSblymn {
95fa0b432bSblymn 	return mvwaddnwstr(win, y, x, str, -1);
96fa0b432bSblymn }
97fa0b432bSblymn 
98fa0b432bSblymn /*
99fa0b432bSblymn  * mvaddnwstr --
100fa0b432bSblymn  *	  Add a string of at most n characters to stdscr
101fa0b432bSblymn  *	  starting at (y, x).
102fa0b432bSblymn  */
103fa0b432bSblymn int
104fa0b432bSblymn mvaddnwstr(int y, int x, const wchar_t *str, int count)
105fa0b432bSblymn {
106fa0b432bSblymn 	return mvwaddnwstr(stdscr, y, x, str, count);
107fa0b432bSblymn }
108fa0b432bSblymn 
109fa0b432bSblymn /*
110fa0b432bSblymn  * mvwaddnwstr --
111fa0b432bSblymn  *	  Add a string of at most n characters to the given window
112fa0b432bSblymn  *	  starting at (y, x).
113fa0b432bSblymn  */
114fa0b432bSblymn int
115fa0b432bSblymn mvwaddnwstr(WINDOW *win, int y, int x, const wchar_t *str, int count)
116fa0b432bSblymn {
1172a780e62Sblymn 	if (wmove(win, y, x) == ERR)
118fa0b432bSblymn 		return ERR;
119fa0b432bSblymn 
120fa0b432bSblymn 	return waddnwstr(win, str, count);
121fa0b432bSblymn }
122fa0b432bSblymn 
123fa0b432bSblymn /*
124fa0b432bSblymn  * waddnwstr --
125fa0b432bSblymn  *	Add a string (at most n characters) to the given window
126fa0b432bSblymn  *	starting at (_cury, _curx).  If n is negative, add the
127fa0b432bSblymn  *	entire string.
128fa0b432bSblymn  */
129fa0b432bSblymn int
130fa0b432bSblymn waddnwstr(WINDOW *win, const wchar_t *s, int n)
131fa0b432bSblymn {
132fa0b432bSblymn 	size_t  len;
133fa0b432bSblymn 	const wchar_t *p;
134fa0b432bSblymn 	cchar_t cc;
135fa0b432bSblymn 	wchar_t wc[2];
136fa0b432bSblymn 
137*6348e3f3Sblymn 	if (__predict_false(win == NULL))
138*6348e3f3Sblymn 		return ERR;
139*6348e3f3Sblymn 
140fa0b432bSblymn 	/*
141fa0b432bSblymn 	 * BSD curses: if (n > 0) then "at most n", else "len = strlen(s)"
142fa0b432bSblymn 	 * ncurses: if (n >= 0) then "at most n", else "len = strlen(s)"
143fa0b432bSblymn 	 * XCURSES: if (n != -1) then "at most n", else "len = strlen(s)"
144fa0b432bSblymn 	 */
145fa0b432bSblymn 	/* compute the length and column width of string */
146fa0b432bSblymn 	if (n < -1)
147fa0b432bSblymn 		return ERR;
148fa0b432bSblymn 	if (n >= 0)
149fa0b432bSblymn 		for (p = s, len = 0; n-- && *p++; ++len);
150fa0b432bSblymn 	else
151fa0b432bSblymn 		len = wcslen(s);
152b09154d5Srin 	__CTRACE(__CTRACE_INPUT, "waddnwstr: string len=%zu\n", len);
153fa0b432bSblymn 
154fa0b432bSblymn 	p = s;
155fa0b432bSblymn 	while (len) {
156fa0b432bSblymn 		wc[0] = *p;
157fa0b432bSblymn 		wc[1] = L'\0';
158fa0b432bSblymn 		if (setcchar( &cc, wc, win->wattr, 0, NULL ) == ERR)
159fa0b432bSblymn 			return ERR;
160fa0b432bSblymn 		if (wadd_wch( win, &cc ) == ERR)
161fa0b432bSblymn 			return ERR;
162e124de36Sblymn 		__CTRACE(__CTRACE_INPUT, "waddnwstr: (%x,%x,%d) added\n",
163fa0b432bSblymn 		    cc.vals[0], cc.attributes, cc.elements);
164fa0b432bSblymn 		p++, len--;
165fa0b432bSblymn 	}
166fa0b432bSblymn 
167fa0b432bSblymn 	return OK;
168fa0b432bSblymn }
169