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