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