xref: /csrg-svn/lib/libcurses/addbytes.c (revision 56301)
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*56301Selan static char sccsid[] = "@(#)addbytes.c	5.10 (Berkeley) 09/21/92";
1055944Sbostic #endif	/* not lint */
1131681Sminshall 
1255944Sbostic #include <curses.h>
1356073Selan #include <termios.h>
1431681Sminshall 
1556238Selan #define	SYNCH_IN	{y = win->cury; x = win->curx;}
1656238Selan #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;
3056238Selan 	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:
4856238Selan 			if (win->flags & __WSTANDOUT)
4956238Selan 				c |= __STANDOUT;
5055944Sbostic #ifdef DEBUG
5155944Sbostic 	__TRACE("ADDBYTES(%0.2o, %d, %d)\n", win, y, x);
5255944Sbostic #endif
53*56301Selan 
5456238Selan 			lp = win->lines[y];
55*56301Selan 			if (lp->flags & __ISPASTEOL) {
56*56301Selan 				lp->flags &= ~__ISPASTEOL;
57*56301Selan newline:			if (y == win->maxy - 1) {
58*56301Selan 					if (win->flags & __SCROLLOK) {
59*56301Selan 					        x = 0;
60*56301Selan 						SYNCH_OUT;
61*56301Selan 						scroll(win);
62*56301Selan 						SYNCH_IN;
63*56301Selan 						lp = win->lines[y];
64*56301Selan 					}
65*56301Selan 				} else {
66*56301Selan 					y++;
67*56301Selan 					lp = win->lines[y];
68*56301Selan 					x = 0;
69*56301Selan 				}
70*56301Selan 			}
71*56301Selan 
72*56301Selan 
73*56301Selan #ifdef DEBUG
74*56301Selan 	__TRACE("ADDBYTES: 1: y = %d, x = %d, firstch = %d, lastch = %d\n",
75*56301Selan 	    y, x, win->lines[y]->firstch, win->lines[y]->lastch);
76*56301Selan #endif
7756238Selan 			if (lp->line[x] != c) {
7856238Selan 				newx = x + win->ch_off;
7956238Selan 				if (!(lp->flags & __ISDIRTY)) {
8056238Selan 					lp->flags |= __ISDIRTY;
8156238Selan 					lp->firstch = lp->lastch = newx;
8256238Selan 				}
8356238Selan 				else if (newx < lp->firstch)
8456238Selan 					lp->firstch = newx;
8556238Selan 				else if (newx > lp->lastch)
8656238Selan 					lp->lastch = newx;
8755945Sbostic #ifdef DEBUG
8855944Sbostic 	__TRACE("ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
8956238Selan 	    lp->firstch, lp->lastch,
9056238Selan 	    lp->firstch - win->ch_off,
9156238Selan 	    lp->lastch - win->ch_off);
9255944Sbostic #endif
9355944Sbostic 			}
9456238Selan 			lp->line[x] = c;
95*56301Selan 			if (x == win->maxx - 1)
96*56301Selan 				lp->flags |= __ISPASTEOL;
97*56301Selan 			else
98*56301Selan 				x++;
9955944Sbostic #ifdef DEBUG
10055944Sbostic 	__TRACE("ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n",
10156238Selan 	    y, x, win->lines[y]->firstch, win->lines[y]->lastch);
10255944Sbostic #endif
10355944Sbostic 			break;
10455944Sbostic 		case '\n':
10555944Sbostic 			SYNCH_OUT;
10655944Sbostic 			wclrtoeol(win);
10755944Sbostic 			SYNCH_IN;
10856073Selan 			if (origtermio.c_oflag & ONLCR)
10955944Sbostic 				x = 0;
11055944Sbostic 			goto newline;
11155944Sbostic 		case '\r':
11255944Sbostic 			x = 0;
11355944Sbostic 			break;
11455944Sbostic 		case '\b':
11555944Sbostic 			if (--x < 0)
11655944Sbostic 				x = 0;
11755944Sbostic 			break;
11855944Sbostic 		}
11955944Sbostic 	}
12055944Sbostic 	SYNCH_OUT;
12155944Sbostic 	return (OK);
12231681Sminshall }
123*56301Selan 
124