xref: /netbsd-src/lib/libcurses/addbytes.c (revision e9d867ef5010fbab8d48045c13025636f5cd7479)
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  * 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 /*static char sccsid[] = "from: @(#)addbytes.c	5.4 (Berkeley) 6/1/90";*/
36 static char rcsid[] = "$Id: addbytes.c,v 1.4 1993/08/01 18:35:58 mycroft Exp $";
37 #endif /* not lint */
38 
39 # include	"curses.ext"
40 
41 waddbytes(win, bytes, count)
42 reg WINDOW	*win;
43 reg char        *bytes;
44 int         count;
45 {
46 	chtype c;
47 	reg int i;
48 
49 	for (i = 0; i < count; i++) {
50 		c = (unsigned char) *bytes++;
51 		if (_waddbytes(win, &c, 1) == ERR)
52 			return ERR;
53 	}
54 	return OK;
55 }
56 
57 /*
58  *	This routine adds the character to the current position
59  *
60  */
61 _waddbytes(win, bytes, count)
62 reg WINDOW	*win;
63 reg chtype      *bytes;
64 reg int		count;
65 {
66 #define	SYNCH_OUT()	{win->_cury = y; win->_curx = x;}
67 #define	SYNCH_IN()	{y = win->_cury; x = win->_curx;}
68 	reg int		x, y;
69 	reg int		newx;
70 
71 	SYNCH_IN();
72 	while (count--) {
73 	    register chtype c;
74 	    static chtype blanks[] = {' ',' ',' ',' ',' ',' ',' ',' '};
75 
76 	    c = *bytes++;
77 	    switch (c) {
78 	      case '\t':
79 		    SYNCH_OUT();
80 		    if (_waddbytes(win, blanks, 8-(x%8)) == ERR) {
81 			return ERR;
82 		    }
83 		    SYNCH_IN();
84 		    break;
85 
86 	      default:
87 # ifdef FULLDEBUG
88 		    fprintf(outf, "ADDBYTES: 1: y = %d, x = %d, firstch = %d, lastch = %d\n", y, x, win->_firstch[y], win->_lastch[y]);
89 # endif
90 		    if (win->_flags & _STANDOUT)
91 			    c |= _STANDOUT;
92 		    {
93 # ifdef	FULLDEBUG
94 			    fprintf(outf, "ADDBYTES(%0.2o, %d, %d)\n", win, y, x);
95 # endif
96 			    if (win->_y[y][x] != c) {
97 				    newx = x + win->_ch_off;
98 				    if (win->_firstch[y] == _NOCHANGE) {
99 					    win->_firstch[y] =
100 							    win->_lastch[y] = newx;
101 				    } else if (newx < win->_firstch[y])
102 					    win->_firstch[y] = newx;
103 				    else if (newx > win->_lastch[y])
104 					    win->_lastch[y] = newx;
105 # ifdef FULLDEBUG
106 				    fprintf(outf, "ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
107 					    win->_firstch[y], win->_lastch[y],
108 					    win->_firstch[y] - win->_ch_off,
109 					    win->_lastch[y] - win->_ch_off);
110 # endif
111 			    }
112 		    }
113 		    win->_y[y][x++] = c;
114 		    if (x >= win->_maxx) {
115 			    x = 0;
116     newline:
117 			    if (++y >= win->_maxy)
118 				    if (win->_scroll) {
119 					    --y;
120 					    SYNCH_OUT();
121 					    scroll(win);
122 					    SYNCH_IN();
123 				    }
124 				    else
125 					    return ERR;
126 		    }
127 # ifdef FULLDEBUG
128 		    fprintf(outf, "ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n", y, x, win->_firstch[y], win->_lastch[y]);
129 # endif
130 		    break;
131 	      case '\n':
132 		    SYNCH_OUT();
133 		    wclrtoeol(win);
134 		    SYNCH_IN();
135 		    if (!NONL)
136 			    x = 0;
137 		    goto newline;
138 	      case '\r':
139 		    x = 0;
140 		    break;
141 	      case '\b':
142 		    if (--x < 0)
143 			    x = 0;
144 		    break;
145 	    }
146     }
147     SYNCH_OUT();
148     return OK;
149 }
150