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