xref: /csrg-svn/lib/libcurses/addbytes.c (revision 56238)
131681Sminshall /*
231681Sminshall  * Copyright (c) 1987 Regents of the University of California.
334677Sbostic  * All rights reserved.
434677Sbostic  *
542651Sbostic  * %sccs.include.redist.c%
631681Sminshall  */
731681Sminshall 
831681Sminshall #ifndef lint
9*56238Selan static char sccsid[] = "@(#)addbytes.c	5.9 (Berkeley) 09/14/92";
1055944Sbostic #endif	/* not lint */
1131681Sminshall 
1255944Sbostic #include <curses.h>
1356073Selan #include <termios.h>
1431681Sminshall 
15*56238Selan #define	SYNCH_IN	{y = win->cury; x = win->curx;}
16*56238Selan #define	SYNCH_OUT	{win->cury = y; win->curx = x;}
1755944Sbostic 
1831681Sminshall /*
1955944Sbostic  * waddbytes --
2055944Sbostic  *	Add the character to the current position in the given window.
2131681Sminshall  */
2255983Sbostic int
2331681Sminshall waddbytes(win, bytes, count)
2455944Sbostic 	register WINDOW *win;
2555944Sbostic 	register char *bytes;
2655944Sbostic 	register int count;
2731681Sminshall {
2855944Sbostic 	static char blanks[] = "        ";
2955944Sbostic 	register int c, newx, x, y;
30*56238Selan 	LINE *lp;
3131681Sminshall 
3255944Sbostic 	SYNCH_IN;
3356073Selan 
3455944Sbostic #ifdef DEBUG
3555944Sbostic 	__TRACE("ADDBYTES('%c') at (%d, %d)\n", c, y, x);
3655944Sbostic #endif
3731681Sminshall 	while (count--) {
3855944Sbostic 		c = *bytes++;
3955944Sbostic 		switch (c) {
4055944Sbostic 		case '\t':
4155945Sbostic 			SYNCH_OUT;
4255944Sbostic 			if (waddbytes(win, blanks, 8 - (x % 8)) == ERR)
4355944Sbostic 				return (ERR);
4455945Sbostic 			SYNCH_IN;
4555944Sbostic 			break;
4631681Sminshall 
4755944Sbostic 		default:
4855944Sbostic #ifdef DEBUG
4955944Sbostic 	__TRACE("ADDBYTES: 1: y = %d, x = %d, firstch = %d, lastch = %d\n",
50*56238Selan 	    y, x, win->lines[y]->firstch, win->lines[y]->lastch);
5155944Sbostic #endif
52*56238Selan 			if (win->flags & __WSTANDOUT)
53*56238Selan 				c |= __STANDOUT;
5455944Sbostic #ifdef DEBUG
5555944Sbostic 	__TRACE("ADDBYTES(%0.2o, %d, %d)\n", win, y, x);
5655944Sbostic #endif
57*56238Selan 			lp = win->lines[y];
58*56238Selan 
59*56238Selan 			if (lp->line[x] != c) {
60*56238Selan 				newx = x + win->ch_off;
61*56238Selan 				if (!(lp->flags & __ISDIRTY)) {
62*56238Selan 					lp->flags |= __ISDIRTY;
63*56238Selan 					lp->firstch = lp->lastch = newx;
64*56238Selan 				}
65*56238Selan 				else if (newx < lp->firstch)
66*56238Selan 					lp->firstch = newx;
67*56238Selan 				else if (newx > lp->lastch)
68*56238Selan 					lp->lastch = newx;
6955945Sbostic #ifdef DEBUG
7055944Sbostic 	__TRACE("ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
71*56238Selan 	    lp->firstch, lp->lastch,
72*56238Selan 	    lp->firstch - win->ch_off,
73*56238Selan 	    lp->lastch - win->ch_off);
7455944Sbostic #endif
7555944Sbostic 			}
76*56238Selan 			lp->line[x] = c;
77*56238Selan 			if (++x >= win->maxx) {
7855944Sbostic 				x = 0;
79*56238Selan newline:			if (++y >= win->maxy)
80*56238Selan 					if (win->flags & __SCROLLOK) {
8155944Sbostic 						SYNCH_OUT;
8255944Sbostic 						scroll(win);
8355944Sbostic 						SYNCH_IN;
8455944Sbostic 						--y;
8555944Sbostic 					} else
8655944Sbostic 						return (ERR);
8755944Sbostic 			}
8855944Sbostic #ifdef DEBUG
8955944Sbostic 	__TRACE("ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n",
90*56238Selan 	    y, x, win->lines[y]->firstch, win->lines[y]->lastch);
9155944Sbostic #endif
9255944Sbostic 			break;
9355944Sbostic 		case '\n':
9455944Sbostic 			SYNCH_OUT;
9555944Sbostic 			wclrtoeol(win);
9655944Sbostic 			SYNCH_IN;
9756073Selan 			if (origtermio.c_oflag & ONLCR)
9855944Sbostic 				x = 0;
9955944Sbostic 			goto newline;
10055944Sbostic 		case '\r':
10155944Sbostic 			x = 0;
10255944Sbostic 			break;
10355944Sbostic 		case '\b':
10455944Sbostic 			if (--x < 0)
10555944Sbostic 				x = 0;
10655944Sbostic 			break;
10755944Sbostic 		}
10855944Sbostic 	}
10955944Sbostic 	SYNCH_OUT;
11055944Sbostic 	return (OK);
11131681Sminshall }
112