xref: /netbsd/lib/libcurses/addchnstr.c (revision dd26fe31)
1 /*	$NetBSD: addchnstr.c,v 1.9 2021/09/06 07:03:49 rin Exp $	*/
2 
3 /*
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Douwe Kiela (virtus@wanadoo.nl).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: addchnstr.c,v 1.9 2021/09/06 07:03:49 rin Exp $");
35 #endif				/* not lint */
36 
37 #include <stdlib.h>
38 
39 #include "curses.h"
40 #include "curses_private.h"
41 
42 #ifndef _CURSES_USE_MACROS
43 
44 /*
45  * addchstr --
46  *      Add a string to stdscr starting at (_cury, _curx).
47  */
48 int
addchstr(const chtype * chstr)49 addchstr(const chtype *chstr)
50 {
51 	return waddchnstr(stdscr, chstr, -1);
52 }
53 
54 /*
55  * waddchstr --
56  *      Add a string to the given window starting at (_cury, _curx).
57  */
58 int
waddchstr(WINDOW * win,const chtype * chstr)59 waddchstr(WINDOW *win, const chtype *chstr)
60 {
61 	return waddchnstr(win, chstr, -1);
62 }
63 
64 /*
65  * addchnstr --
66  *      Add a string (at most n characters) to stdscr starting
67  *	at (_cury, _curx).  If n is negative, add the entire string.
68  */
69 int
addchnstr(const chtype * chstr,int n)70 addchnstr(const chtype *chstr, int n)
71 {
72 	return waddchnstr(stdscr, chstr, n);
73 }
74 
75 /*
76  * mvaddchstr --
77  *      Add a string to stdscr starting at (y, x)
78  */
79 int
mvaddchstr(int y,int x,const chtype * chstr)80 mvaddchstr(int y, int x, const chtype *chstr)
81 {
82 	return mvwaddchnstr(stdscr, y, x, chstr, -1);
83 }
84 
85 /*
86  * mvwaddchstr --
87  *      Add a string to the given window starting at (y, x)
88  */
89 int
mvwaddchstr(WINDOW * win,int y,int x,const chtype * chstr)90 mvwaddchstr(WINDOW *win, int y, int x, const chtype *chstr)
91 {
92 	return mvwaddchnstr(win, y, x, chstr, -1);
93 }
94 
95 /*
96  * mvaddchnstr --
97  *      Add a string of at most n characters to stdscr
98  *      starting at (y, x).
99  */
100 int
mvaddchnstr(int y,int x,const chtype * chstr,int n)101 mvaddchnstr(int y, int x, const chtype *chstr, int n)
102 {
103 	return mvwaddchnstr(stdscr, y, x, chstr, n);
104 }
105 
106 /*
107  * mvwaddchnstr --
108  *      Add a string of at most n characters to the given window
109  *      starting at (y, x).
110  */
111 int
mvwaddchnstr(WINDOW * win,int y,int x,const chtype * chstr,int n)112 mvwaddchnstr(WINDOW *win, int y, int x, const chtype *chstr, int n)
113 {
114 	if (wmove(win, y, x) == ERR)
115 		return ERR;
116 
117 	return waddchnstr(win, chstr, n);
118 }
119 
120 #endif
121 
122 /*
123  * waddchnstr --
124  *	Add a string (at most n characters) to the given window
125  *	starting at (_cury, _curx) until the end of line is reached or
126  *      n characters have been added.  If n is negative, add as much
127  *	of the string that will fit on the current line.  SUSv2 says
128  *      that the addchnstr family does not wrap and strings are truncated
129  *      to the RHS of the window.
130  */
131 int
waddchnstr(WINDOW * win,const chtype * chstr,int n)132 waddchnstr(WINDOW *win, const chtype *chstr, int n)
133 {
134 	size_t  len;
135 	const chtype *chp;
136 	attr_t	attr;
137 	char	*ocp, *cp, *start;
138 	int i, ret, ox, oy;
139 
140 	__CTRACE(__CTRACE_INPUT, "waddchnstr: win = %p, chstr = %p, n = %d\n",
141 	    win, chstr, n);
142 
143 	if (n >= 0)
144 		for (chp = chstr, len = 0; n-- && *chp++; ++len);
145 	else
146 		for (chp = chstr, len = 0; *chp++; ++len);
147 
148 	/* check if string is too long for current location */
149 	if (len > (win->maxx - win->curx))
150 		len = win->maxx - win->curx;
151 
152 	if ((ocp = malloc(len + 1)) == NULL)
153 		return ERR;
154 	chp = chstr;
155 	cp = ocp;
156 	start = ocp;
157 	i = 0;
158 	attr = (*chp) & __ATTRIBUTES;
159 	ox = win->curx;
160 	oy = win->cury;
161 	while (len) {
162 		*cp = (*chp) & __CHARTEXT;
163 		cp++;
164 		chp++;
165 		i++;
166 		len--;
167 		if (((*chp) & __ATTRIBUTES) != attr) {
168 			*cp = '\0';
169 			if (_cursesi_waddbytes(win, start, i, attr, 0) == ERR) {
170 				free(ocp);
171 				return ERR;
172 			}
173 			attr = (*chp) & __ATTRIBUTES;
174 			start = cp;
175 			i = 0;
176 		}
177 	}
178 	*cp = '\0';
179 	ret = _cursesi_waddbytes(win, start, i, attr, 0);
180 	free(ocp);
181 	wmove(win, oy, ox);
182 	return ret;
183 }
184