Fix mdoc(7)/man(7) mix up.
[netbsd-mini2440.git] / lib / libcurses / addchnstr.c
blob17ce130aeaca43bf20b4a7b926bef85656cf8d1c
1 /* $NetBSD: addchnstr.c,v 1.3 2007/01/21 13:25:36 jdc Exp $ */
3 /*
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Douwe Kiela (virtus@wanadoo.nl).
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.
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.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: addchnstr.c,v 1.3 2007/01/21 13:25:36 jdc Exp $");
35 #endif /* not lint */
37 #include <stdlib.h>
39 #include "curses.h"
40 #include "curses_private.h"
42 #ifndef _CURSES_USE_MACROS
45 * addchstr --
46 * Add a string to stdscr starting at (_cury, _curx).
48 int
49 addchstr(const chtype *chstr)
51 return waddchnstr(stdscr, chstr, -1);
55 * waddchstr --
56 * Add a string to the given window starting at (_cury, _curx).
58 int
59 waddchstr(WINDOW *win, const chtype *chstr)
61 return waddchnstr(win, chstr, -1);
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.
69 int
70 addchnstr(const chtype *chstr, int n)
72 return waddchnstr(stdscr, chstr, n);
76 * mvaddchstr --
77 * Add a string to stdscr starting at (y, x)
79 int
80 mvaddchstr(int y, int x, const chtype *chstr)
82 return mvwaddchnstr(stdscr, y, x, chstr, -1);
86 * mvwaddchstr --
87 * Add a string to the given window starting at (y, x)
89 int
90 mvwaddchstr(WINDOW *win, int y, int x, const chtype *chstr)
92 return mvwaddchnstr(win, y, x, chstr, -1);
96 * mvaddchnstr --
97 * Add a string of at most n characters to stdscr
98 * starting at (y, x).
101 mvaddchnstr(int y, int x, const chtype *chstr, int n)
103 return mvwaddchnstr(stdscr, y, x, chstr, n);
107 * mvwaddchnstr --
108 * Add a string of at most n characters to the given window
109 * starting at (y, x).
112 mvwaddchnstr(WINDOW *win, int y, int x, const chtype *chstr, int n)
114 if (wmove(win, y, x) == ERR)
115 return ERR;
117 return waddchnstr(win, chstr, n);
120 #endif
123 * waddchnstr --
124 * Add a string (at most n characters) to the given window
125 * starting at (_cury, _curx). If n is negative, add the
126 * entire string.
129 waddchnstr(WINDOW *win, const chtype *chstr, int n)
131 size_t len;
132 const chtype *chp;
133 attr_t attr;
134 char *ocp, *cp, *start;
135 int i, ret;
137 #ifdef DEBUG
138 __CTRACE(__CTRACE_INPUT, "waddchnstr: win = %p, chstr = %p, n = %d\n",
139 win, chstr, n);
140 #endif
142 if (n >= 0)
143 for (chp = chstr, len = 0; n-- && *chp++; ++len);
144 else
145 for (chp = chstr, len = 0; *chp++; ++len);
147 if ((ocp = malloc(len + 1)) == NULL)
148 return ERR;
149 chp = chstr;
150 cp = ocp;
151 start = ocp;
152 i = 0;
153 attr = (*chp) & __ATTRIBUTES;
154 while (len) {
155 *cp = (*chp) & __CHARTEXT;
156 cp++;
157 chp++;
158 i++;
159 len--;
160 if (((*chp) & __ATTRIBUTES) != attr) {
161 *cp = '\0';
162 if (__waddbytes(win, start, i, attr) == ERR) {
163 free(ocp);
164 return ERR;
166 attr = (*chp) & __ATTRIBUTES;
167 start = cp;
168 i = 0;
171 *cp = '\0';
172 ret = __waddbytes(win, start, i, attr);
173 free(ocp);
174 return ret;