addbytes.c revision 1.30.6.1 1 /* $NetBSD: addbytes.c,v 1.30.6.1 2007/01/21 11:38:59 blymn Exp $ */
2
3 /*
4 * Copyright (c) 1987, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)addbytes.c 8.4 (Berkeley) 5/4/94";
36 #else
37 __RCSID("$NetBSD: addbytes.c,v 1.30.6.1 2007/01/21 11:38:59 blymn Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <stdlib.h>
42 #include "curses.h"
43 #include "curses_private.h"
44
45 #define SYNCH_IN {y = win->cury; x = win->curx;}
46 #define SYNCH_OUT {win->cury = y; win->curx = x;}
47
48 #ifndef _CURSES_USE_MACROS
49
50 /*
51 * addbytes --
52 * Add the character to the current position in stdscr.
53 */
54 int
55 addbytes(const char *bytes, int count)
56 {
57 return __waddbytes(stdscr, bytes, count, 0);
58 }
59
60 /*
61 * waddbytes --
62 * Add the character to the current position in the given window.
63 */
64 int
65 waddbytes(WINDOW *win, const char *bytes, int count)
66 {
67 return __waddbytes(win, bytes, count, 0);
68 }
69
70 /*
71 * mvaddbytes --
72 * Add the characters to stdscr at the location given.
73 */
74 int
75 mvaddbytes(int y, int x, const char *bytes, int count)
76 {
77 return mvwaddbytes(stdscr, y, x, bytes, count);
78 }
79
80 /*
81 * mvwaddbytes --
82 * Add the characters to the given window at the location given.
83 */
84 int
85 mvwaddbytes(WINDOW *win, int y, int x, const char *bytes, int count)
86 {
87 if (wmove(win, y, x) == ERR)
88 return ERR;
89
90 return __waddbytes(win, bytes, count, 0);
91 }
92
93 #endif
94
95 /*
96 * waddbytes --
97 * Add the character to the current position in the given window.
98 */
99 int
100 __waddbytes(WINDOW *win, const char *bytes, int count, attr_t attr)
101 {
102 static char blanks[] = " ";
103 int c, newx, x, y;
104 attr_t attributes;
105 __LINE *lp;
106 #ifdef DEBUG
107 int i;
108
109 for (i = 0; i < win->maxy; i++) {
110 assert(win->lines[i]->sentinel == SENTINEL_VALUE);
111 }
112 #endif
113
114 SYNCH_IN;
115 lp = win->lines[y];
116
117 while (count--) {
118 c = *bytes++;
119 #ifdef DEBUG
120 __CTRACE("ADDBYTES('%c', %x) at (%d, %d)\n", c, attr, y, x);
121 #endif
122 switch (c) {
123 case '\t':
124 SYNCH_OUT;
125 if (waddbytes(win, blanks, 8 - (x % 8)) == ERR)
126 return (ERR);
127 SYNCH_IN;
128 break;
129
130 default:
131 #ifdef DEBUG
132 __CTRACE("ADDBYTES(%p, %d, %d)\n", win, y, x);
133 #endif
134
135 if (lp->flags & __ISPASTEOL) {
136 newline:
137 x = 0;
138 lp->flags &= ~__ISPASTEOL;
139 if (y == win->scr_b) {
140 #ifdef DEBUG
141 __CTRACE("ADDBYTES - on bottom "
142 "of scrolling region\n");
143 #endif
144 if (!(win->flags & __SCROLLOK))
145 return ERR;
146 SYNCH_OUT;
147 scroll(win);
148 SYNCH_IN;
149 } else {
150 y++;
151 }
152 lp = win->lines[y];
153 if (c == '\n')
154 break;
155 }
156
157 attributes = (win->wattr | attr) &
158 (__ATTRIBUTES & ~__COLOR);
159 if (attr & __COLOR)
160 attributes |= attr & __COLOR;
161 else if (win->wattr & __COLOR)
162 attributes |= win->wattr & __COLOR;
163 #ifdef DEBUG
164 __CTRACE("ADDBYTES: 1: y = %d, x = %d, firstch = %d, "
165 "lastch = %d\n",
166 y, x, *win->lines[y]->firstchp,
167 *win->lines[y]->lastchp);
168 #endif
169 /*
170 * Always update the change pointers. Otherwise,
171 * we could end up not displaying 'blank' characters
172 * when overlapping windows are displayed.
173 */
174 newx = x + win->ch_off;
175 lp->flags |= __ISDIRTY;
176 /*
177 * firstchp/lastchp are shared between
178 * parent window and sub-window.
179 */
180 if (newx < *lp->firstchp)
181 *lp->firstchp = newx;
182 if (newx > *lp->lastchp)
183 *lp->lastchp = newx;
184 #ifdef DEBUG
185 __CTRACE("ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
186 *lp->firstchp, *lp->lastchp,
187 *lp->firstchp - win->ch_off,
188 *lp->lastchp - win->ch_off);
189 #endif
190 if (win->bch != ' ' && c == ' ')
191 lp->line[x].ch = win->bch;
192 else
193 lp->line[x].ch = c;
194
195 #ifdef HAVE_WCHAR
196 if (_cursesi_copy_nsp(win->bnsp, &lp->line[x]) == ERR)
197 return ERR;
198 #endif /* HAVE_WCHAR */
199
200 if (attributes & __COLOR)
201 lp->line[x].attr =
202 attributes | (win->battr & ~__COLOR);
203 else
204 lp->line[x].attr = attributes | win->battr;
205 #ifdef HAVE_WCHAR
206 SET_WCOL(lp->line[x], 1);
207 #endif /* HAVE_WCHAR */
208
209 if (x == win->maxx - 1)
210 lp->flags |= __ISPASTEOL;
211 else
212 x++;
213 #ifdef DEBUG
214 __CTRACE("ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n",
215 y, x, *win->lines[y]->firstchp,
216 *win->lines[y]->lastchp);
217 #endif
218 break;
219 case '\n':
220 SYNCH_OUT;
221 wclrtoeol(win);
222 SYNCH_IN;
223 goto newline;
224 case '\r':
225 x = 0;
226 continue;
227 case '\b':
228 if (--x < 0)
229 x = 0;
230 break;
231 }
232 }
233 SYNCH_OUT;
234
235 #ifdef DEBUG
236 for (i = 0; i < win->maxy; i++) {
237 assert(win->lines[i]->sentinel == SENTINEL_VALUE);
238 }
239 #endif
240
241 return (OK);
242 }
243