xref: /netbsd-src/lib/libcurses/addchnstr.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: addchnstr.c,v 1.2 2003/05/22 16:09:09 wiz 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __RCSID("$NetBSD: addchnstr.c,v 1.2 2003/05/22 16:09:09 wiz Exp $");
42 #endif				/* not lint */
43 
44 #include <stdlib.h>
45 
46 #include "curses.h"
47 #include "curses_private.h"
48 
49 #ifndef _CURSES_USE_MACROS
50 
51 /*
52  * addchstr --
53  *      Add a string to stdscr starting at (_cury, _curx).
54  */
55 int
56 addchstr(const chtype *chstr)
57 {
58 	return waddchnstr(stdscr, chstr, -1);
59 }
60 
61 /*
62  * waddchstr --
63  *      Add a string to the given window starting at (_cury, _curx).
64  */
65 int
66 waddchstr(WINDOW *win, const chtype *chstr)
67 {
68 	return waddchnstr(win, chstr, -1);
69 }
70 
71 /*
72  * addchnstr --
73  *      Add a string (at most n characters) to stdscr starting
74  *	at (_cury, _curx).  If n is negative, add the entire string.
75  */
76 int
77 addchnstr(const chtype *chstr, int n)
78 {
79 	return waddchnstr(stdscr, chstr, n);
80 }
81 
82 /*
83  * mvaddchstr --
84  *      Add a string to stdscr starting at (y, x)
85  */
86 int
87 mvaddchstr(int y, int x, const chtype *chstr)
88 {
89 	return mvwaddchnstr(stdscr, y, x, chstr, -1);
90 }
91 
92 /*
93  * mvwaddchstr --
94  *      Add a string to the given window starting at (y, x)
95  */
96 int
97 mvwaddchstr(WINDOW *win, int y, int x, const chtype *chstr)
98 {
99 	return mvwaddchnstr(win, y, x, chstr, -1);
100 }
101 
102 /*
103  * mvaddchnstr --
104  *      Add a string of at most n characters to stdscr
105  *      starting at (y, x).
106  */
107 int
108 mvaddchnstr(int y, int x, const chtype *chstr, int n)
109 {
110 	return mvwaddchnstr(stdscr, y, x, chstr, n);
111 }
112 
113 /*
114  * mvwaddchnstr --
115  *      Add a string of at most n characters to the given window
116  *      starting at (y, x).
117  */
118 int
119 mvwaddchnstr(WINDOW *win, int y, int x, const chtype *chstr, int n)
120 {
121 	if (wmove(win, y, x) == ERR)
122 		return ERR;
123 
124 	return waddchnstr(win, chstr, n);
125 }
126 
127 #endif
128 
129 /*
130  * waddchnstr --
131  *	Add a string (at most n characters) to the given window
132  *	starting at (_cury, _curx).  If n is negative, add the
133  *	entire string.
134  */
135 int
136 waddchnstr(WINDOW *win, const chtype *chstr, int n)
137 {
138 	size_t  len;
139 	const chtype *chp;
140 	attr_t	attr;
141 	char	*ocp, *cp, *start;
142 	int i, ret;
143 
144 #ifdef DEBUG
145 	__CTRACE("waddchnstr: win = %p, chstr = %p, n = %d\n", win, chstr, n);
146 #endif
147 
148 	if (n >= 0)
149 		for (chp = chstr, len = 0; n-- && *chp++; ++len);
150 	else
151 		for (chp = chstr, len = 0; *chp++; ++len);
152 
153 	if ((ocp = malloc(len + 1)) == NULL)
154 		return ERR;
155 	chp = chstr;
156 	cp = ocp;
157 	start = ocp;
158 	i = 0;
159 	attr = (*chp) & __ATTRIBUTES;
160 	while (len) {
161 		*cp = (*chp) & __CHARTEXT;
162 		cp++;
163 		chp++;
164 		i++;
165 		len--;
166 		if (((*chp) & __ATTRIBUTES) != attr) {
167 			*cp = '\0';
168 			if (__waddbytes(win, start, i, attr) == ERR) {
169 				free(ocp);
170 				return ERR;
171 			}
172 			attr = (*chp) & __ATTRIBUTES;
173 			start = cp;
174 			i = 0;
175 		}
176 	}
177 	*cp = '\0';
178 	ret = __waddbytes(win, start, i, attr);
179 	free(ocp);
180 	return ret;
181 }
182