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