xref: /csrg-svn/lib/libcurses/addbytes.c (revision 67082)
131681Sminshall /*
2*67082Sbostic  * Copyright (c) 1987, 1993, 1994
363266Sbostic  *	The Regents of the University of California.  All rights reserved.
434677Sbostic  *
542651Sbostic  * %sccs.include.redist.c%
631681Sminshall  */
731681Sminshall 
831681Sminshall #ifndef lint
9*67082Sbostic static char sccsid[] = "@(#)addbytes.c	8.4 (Berkeley) 05/04/94";
1055944Sbostic #endif	/* not lint */
1131681Sminshall 
12*67082Sbostic #include "curses.h"
1331681Sminshall 
1456238Selan #define	SYNCH_IN	{y = win->cury; x = win->curx;}
1556238Selan #define	SYNCH_OUT	{win->cury = y; win->curx = x;}
1655944Sbostic 
1731681Sminshall /*
1855944Sbostic  * waddbytes --
1955944Sbostic  *	Add the character to the current position in the given window.
2031681Sminshall  */
2155983Sbostic int
__waddbytes(win,bytes,count,so)2256596Selan __waddbytes(win, bytes, count, so)
2355944Sbostic 	register WINDOW *win;
2457942Sbostic 	register const char *bytes;
2555944Sbostic 	register int count;
2656596Selan 	int so;
2731681Sminshall {
2855944Sbostic 	static char blanks[] = "        ";
2955944Sbostic 	register int c, newx, x, y;
3056596Selan 	char stand;
3156647Selan 	__LINE *lp;
3231681Sminshall 
3355944Sbostic 	SYNCH_IN;
3456073Selan 
3555944Sbostic #ifdef DEBUG
3660058Sbostic 	__CTRACE("ADDBYTES('%c') at (%d, %d)\n", c, y, x);
3755944Sbostic #endif
3831681Sminshall 	while (count--) {
3955944Sbostic 		c = *bytes++;
4055944Sbostic 		switch (c) {
4155944Sbostic 		case '\t':
4255945Sbostic 			SYNCH_OUT;
4357472Sbostic 			if (waddbytes(win, blanks, 8 - (x % 8)) == ERR)
4457472Sbostic 				return (ERR);
4555945Sbostic 			SYNCH_IN;
4655944Sbostic 			break;
4731681Sminshall 
4855944Sbostic 		default:
4955944Sbostic #ifdef DEBUG
5060058Sbostic 	__CTRACE("ADDBYTES(%0.2o, %d, %d)\n", win, y, x);
5155944Sbostic #endif
5256301Selan 
5356238Selan 			lp = win->lines[y];
5456301Selan 			if (lp->flags & __ISPASTEOL) {
5556301Selan 				lp->flags &= ~__ISPASTEOL;
5656301Selan newline:			if (y == win->maxy - 1) {
5756301Selan 					if (win->flags & __SCROLLOK) {
5856301Selan 						SYNCH_OUT;
5956301Selan 						scroll(win);
6056301Selan 						SYNCH_IN;
6156301Selan 						lp = win->lines[y];
6256596Selan 					        x = 0;
6366392Sbostic 					} else
6466392Sbostic 						return (ERR);
6556301Selan 				} else {
6656301Selan 					y++;
6756301Selan 					lp = win->lines[y];
6856301Selan 					x = 0;
6956301Selan 				}
7057961Storek 				if (c == '\n')
7157961Storek 					break;
7256301Selan 			}
7356301Selan 
7456596Selan 			stand = '\0';
7556596Selan 			if (win->flags & __WSTANDOUT || so)
7656596Selan 				stand |= __STANDOUT;
7756301Selan #ifdef DEBUG
7860058Sbostic 	__CTRACE("ADDBYTES: 1: y = %d, x = %d, firstch = %d, lastch = %d\n",
7956715Selan 	    y, x, *win->lines[y]->firstchp, *win->lines[y]->lastchp);
8056301Selan #endif
8156647Selan 			if (lp->line[x].ch != c ||
8256647Selan 			    !(lp->line[x].attr & stand)) {
8356238Selan 				newx = x + win->ch_off;
8456238Selan 				if (!(lp->flags & __ISDIRTY)) {
8556238Selan 					lp->flags |= __ISDIRTY;
8656715Selan 					*lp->firstchp = *lp->lastchp = newx;
8756238Selan 				}
8856715Selan 				else if (newx < *lp->firstchp)
8956715Selan 					*lp->firstchp = newx;
9056715Selan 				else if (newx > *lp->lastchp)
9156715Selan 					*lp->lastchp = newx;
9255945Sbostic #ifdef DEBUG
9360058Sbostic 	__CTRACE("ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
9456715Selan 	    *lp->firstchp, *lp->lastchp,
9556715Selan 	    *lp->firstchp - win->ch_off,
9656715Selan 	    *lp->lastchp - win->ch_off);
9755944Sbostic #endif
9855944Sbostic 			}
9956647Selan 			lp->line[x].ch = c;
10056596Selan 			if (stand)
10156647Selan 				lp->line[x].attr |= __STANDOUT;
10256596Selan 			else
10356647Selan 				lp->line[x].attr &= ~__STANDOUT;
10456301Selan 			if (x == win->maxx - 1)
10556301Selan 				lp->flags |= __ISPASTEOL;
10656301Selan 			else
10756301Selan 				x++;
10855944Sbostic #ifdef DEBUG
10960058Sbostic 	__CTRACE("ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n",
11056715Selan 	    y, x, *win->lines[y]->firstchp, *win->lines[y]->lastchp);
11155944Sbostic #endif
11255944Sbostic 			break;
11355944Sbostic 		case '\n':
11455944Sbostic 			SYNCH_OUT;
11555944Sbostic 			wclrtoeol(win);
11655944Sbostic 			SYNCH_IN;
11763265Sbostic 			if (!NONL)
11855944Sbostic 				x = 0;
11955944Sbostic 			goto newline;
12055944Sbostic 		case '\r':
12155944Sbostic 			x = 0;
12255944Sbostic 			break;
12355944Sbostic 		case '\b':
12455944Sbostic 			if (--x < 0)
12555944Sbostic 				x = 0;
12655944Sbostic 			break;
12755944Sbostic 		}
12855944Sbostic 	}
12955944Sbostic 	SYNCH_OUT;
13057472Sbostic 	return (OK);
13131681Sminshall }
132