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