addbytes.c revision 1.11 1 1.11 mikel /* $NetBSD: addbytes.c,v 1.11 1997/07/22 07:36:20 mikel Exp $ */
2 1.11 mikel
3 1.1 cgd /*
4 1.10 cgd * Copyright (c) 1987, 1993, 1994
5 1.7 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.11 mikel #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.11 mikel #if 0
39 1.10 cgd static char sccsid[] = "@(#)addbytes.c 8.4 (Berkeley) 5/4/94";
40 1.11 mikel #else
41 1.11 mikel __RCSID("$NetBSD: addbytes.c,v 1.11 1997/07/22 07:36:20 mikel Exp $");
42 1.11 mikel #endif
43 1.5 mycroft #endif /* not lint */
44 1.1 cgd
45 1.10 cgd #include "curses.h"
46 1.1 cgd
47 1.7 cgd #define SYNCH_IN {y = win->cury; x = win->curx;}
48 1.7 cgd #define SYNCH_OUT {win->cury = y; win->curx = x;}
49 1.3 alm
50 1.1 cgd /*
51 1.5 mycroft * waddbytes --
52 1.5 mycroft * Add the character to the current position in the given window.
53 1.1 cgd */
54 1.5 mycroft int
55 1.7 cgd __waddbytes(win, bytes, count, so)
56 1.5 mycroft register WINDOW *win;
57 1.6 mycroft register const char *bytes;
58 1.5 mycroft register int count;
59 1.7 cgd int so;
60 1.1 cgd {
61 1.5 mycroft static char blanks[] = " ";
62 1.5 mycroft register int c, newx, x, y;
63 1.7 cgd char stand;
64 1.7 cgd __LINE *lp;
65 1.5 mycroft
66 1.5 mycroft SYNCH_IN;
67 1.1 cgd
68 1.11 mikel while (count--) {
69 1.11 mikel c = *bytes++;
70 1.5 mycroft #ifdef DEBUG
71 1.7 cgd __CTRACE("ADDBYTES('%c') at (%d, %d)\n", c, y, x);
72 1.5 mycroft #endif
73 1.5 mycroft switch (c) {
74 1.5 mycroft case '\t':
75 1.5 mycroft SYNCH_OUT;
76 1.5 mycroft if (waddbytes(win, blanks, 8 - (x % 8)) == ERR)
77 1.5 mycroft return (ERR);
78 1.5 mycroft SYNCH_IN;
79 1.5 mycroft break;
80 1.5 mycroft
81 1.5 mycroft default:
82 1.5 mycroft #ifdef DEBUG
83 1.7 cgd __CTRACE("ADDBYTES(%0.2o, %d, %d)\n", win, y, x);
84 1.5 mycroft #endif
85 1.7 cgd
86 1.7 cgd lp = win->lines[y];
87 1.7 cgd if (lp->flags & __ISPASTEOL) {
88 1.7 cgd lp->flags &= ~__ISPASTEOL;
89 1.7 cgd newline: if (y == win->maxy - 1) {
90 1.7 cgd if (win->flags & __SCROLLOK) {
91 1.7 cgd SYNCH_OUT;
92 1.7 cgd scroll(win);
93 1.7 cgd SYNCH_IN;
94 1.7 cgd lp = win->lines[y];
95 1.7 cgd x = 0;
96 1.10 cgd } else
97 1.10 cgd return (ERR);
98 1.7 cgd } else {
99 1.7 cgd y++;
100 1.7 cgd lp = win->lines[y];
101 1.7 cgd x = 0;
102 1.7 cgd }
103 1.7 cgd if (c == '\n')
104 1.7 cgd break;
105 1.7 cgd }
106 1.7 cgd
107 1.7 cgd stand = '\0';
108 1.7 cgd if (win->flags & __WSTANDOUT || so)
109 1.7 cgd stand |= __STANDOUT;
110 1.5 mycroft #ifdef DEBUG
111 1.7 cgd __CTRACE("ADDBYTES: 1: y = %d, x = %d, firstch = %d, lastch = %d\n",
112 1.7 cgd y, x, *win->lines[y]->firstchp, *win->lines[y]->lastchp);
113 1.5 mycroft #endif
114 1.7 cgd if (lp->line[x].ch != c ||
115 1.7 cgd !(lp->line[x].attr & stand)) {
116 1.7 cgd newx = x + win->ch_off;
117 1.7 cgd if (!(lp->flags & __ISDIRTY)) {
118 1.7 cgd lp->flags |= __ISDIRTY;
119 1.7 cgd *lp->firstchp = *lp->lastchp = newx;
120 1.7 cgd }
121 1.7 cgd else if (newx < *lp->firstchp)
122 1.7 cgd *lp->firstchp = newx;
123 1.7 cgd else if (newx > *lp->lastchp)
124 1.7 cgd *lp->lastchp = newx;
125 1.5 mycroft #ifdef DEBUG
126 1.7 cgd __CTRACE("ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
127 1.7 cgd *lp->firstchp, *lp->lastchp,
128 1.7 cgd *lp->firstchp - win->ch_off,
129 1.7 cgd *lp->lastchp - win->ch_off);
130 1.5 mycroft #endif
131 1.5 mycroft }
132 1.7 cgd lp->line[x].ch = c;
133 1.7 cgd if (stand)
134 1.7 cgd lp->line[x].attr |= __STANDOUT;
135 1.7 cgd else
136 1.7 cgd lp->line[x].attr &= ~__STANDOUT;
137 1.7 cgd if (x == win->maxx - 1)
138 1.7 cgd lp->flags |= __ISPASTEOL;
139 1.7 cgd else
140 1.7 cgd x++;
141 1.5 mycroft #ifdef DEBUG
142 1.7 cgd __CTRACE("ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n",
143 1.7 cgd y, x, *win->lines[y]->firstchp, *win->lines[y]->lastchp);
144 1.5 mycroft #endif
145 1.5 mycroft break;
146 1.5 mycroft case '\n':
147 1.5 mycroft SYNCH_OUT;
148 1.5 mycroft wclrtoeol(win);
149 1.5 mycroft SYNCH_IN;
150 1.7 cgd if (!NONL)
151 1.5 mycroft x = 0;
152 1.5 mycroft goto newline;
153 1.5 mycroft case '\r':
154 1.5 mycroft x = 0;
155 1.5 mycroft break;
156 1.5 mycroft case '\b':
157 1.5 mycroft if (--x < 0)
158 1.5 mycroft x = 0;
159 1.5 mycroft break;
160 1.5 mycroft }
161 1.5 mycroft }
162 1.5 mycroft SYNCH_OUT;
163 1.5 mycroft return (OK);
164 1.1 cgd }
165