xref: /original-bsd/lib/libcurses/addbytes.c (revision be1f24e8)
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)addbytes.c	5.10 (Berkeley) 09/21/92";
10 #endif	/* not lint */
11 
12 #include <curses.h>
13 #include <termios.h>
14 
15 #define	SYNCH_IN	{y = win->cury; x = win->curx;}
16 #define	SYNCH_OUT	{win->cury = y; win->curx = x;}
17 
18 /*
19  * waddbytes --
20  *	Add the character to the current position in the given window.
21  */
22 int
23 waddbytes(win, bytes, count)
24 	register WINDOW *win;
25 	register char *bytes;
26 	register int count;
27 {
28 	static char blanks[] = "        ";
29 	register int c, newx, x, y;
30 	LINE *lp;
31 
32 	SYNCH_IN;
33 
34 #ifdef DEBUG
35 	__TRACE("ADDBYTES('%c') at (%d, %d)\n", c, y, x);
36 #endif
37 	while (count--) {
38 		c = *bytes++;
39 		switch (c) {
40 		case '\t':
41 			SYNCH_OUT;
42 			if (waddbytes(win, blanks, 8 - (x % 8)) == ERR)
43 				return (ERR);
44 			SYNCH_IN;
45 			break;
46 
47 		default:
48 			if (win->flags & __WSTANDOUT)
49 				c |= __STANDOUT;
50 #ifdef DEBUG
51 	__TRACE("ADDBYTES(%0.2o, %d, %d)\n", win, y, x);
52 #endif
53 
54 			lp = win->lines[y];
55 			if (lp->flags & __ISPASTEOL) {
56 				lp->flags &= ~__ISPASTEOL;
57 newline:			if (y == win->maxy - 1) {
58 					if (win->flags & __SCROLLOK) {
59 					        x = 0;
60 						SYNCH_OUT;
61 						scroll(win);
62 						SYNCH_IN;
63 						lp = win->lines[y];
64 					}
65 				} else {
66 					y++;
67 					lp = win->lines[y];
68 					x = 0;
69 				}
70 			}
71 
72 
73 #ifdef DEBUG
74 	__TRACE("ADDBYTES: 1: y = %d, x = %d, firstch = %d, lastch = %d\n",
75 	    y, x, win->lines[y]->firstch, win->lines[y]->lastch);
76 #endif
77 			if (lp->line[x] != c) {
78 				newx = x + win->ch_off;
79 				if (!(lp->flags & __ISDIRTY)) {
80 					lp->flags |= __ISDIRTY;
81 					lp->firstch = lp->lastch = newx;
82 				}
83 				else if (newx < lp->firstch)
84 					lp->firstch = newx;
85 				else if (newx > lp->lastch)
86 					lp->lastch = newx;
87 #ifdef DEBUG
88 	__TRACE("ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
89 	    lp->firstch, lp->lastch,
90 	    lp->firstch - win->ch_off,
91 	    lp->lastch - win->ch_off);
92 #endif
93 			}
94 			lp->line[x] = c;
95 			if (x == win->maxx - 1)
96 				lp->flags |= __ISPASTEOL;
97 			else
98 				x++;
99 #ifdef DEBUG
100 	__TRACE("ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n",
101 	    y, x, win->lines[y]->firstch, win->lines[y]->lastch);
102 #endif
103 			break;
104 		case '\n':
105 			SYNCH_OUT;
106 			wclrtoeol(win);
107 			SYNCH_IN;
108 			if (origtermio.c_oflag & ONLCR)
109 				x = 0;
110 			goto newline;
111 		case '\r':
112 			x = 0;
113 			break;
114 		case '\b':
115 			if (--x < 0)
116 				x = 0;
117 			break;
118 		}
119 	}
120 	SYNCH_OUT;
121 	return (OK);
122 }
123 
124