xref: /csrg-svn/lib/libcurses/addbytes.c (revision 57961)
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*57961Storek static char sccsid[] = "@(#)addbytes.c	5.19 (Berkeley) 02/12/93";
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 
1856596Selan int
1956596Selan waddbytes(win, bytes, count)
2056596Selan 	WINDOW *win;
2157942Sbostic 	const char *bytes;
2256596Selan 	int count;
2356596Selan {
2456596Selan 	__waddbytes(win, bytes, count, 0);
2556596Selan }
2656596Selan 
2731681Sminshall /*
2855944Sbostic  * waddbytes --
2955944Sbostic  *	Add the character to the current position in the given window.
3031681Sminshall  */
3155983Sbostic int
3256596Selan __waddbytes(win, bytes, count, so)
3355944Sbostic 	register WINDOW *win;
3457942Sbostic 	register const char *bytes;
3555944Sbostic 	register int count;
3656596Selan 	int so;
3731681Sminshall {
3855944Sbostic 	static char blanks[] = "        ";
3955944Sbostic 	register int c, newx, x, y;
4056596Selan 	char stand;
4156647Selan 	__LINE *lp;
4231681Sminshall 
4355944Sbostic 	SYNCH_IN;
4456073Selan 
4555944Sbostic #ifdef DEBUG
4655944Sbostic 	__TRACE("ADDBYTES('%c') at (%d, %d)\n", c, y, x);
4755944Sbostic #endif
4831681Sminshall 	while (count--) {
4955944Sbostic 		c = *bytes++;
5055944Sbostic 		switch (c) {
5155944Sbostic 		case '\t':
5255945Sbostic 			SYNCH_OUT;
5357472Sbostic 			if (waddbytes(win, blanks, 8 - (x % 8)) == ERR)
5457472Sbostic 				return (ERR);
5555945Sbostic 			SYNCH_IN;
5655944Sbostic 			break;
5731681Sminshall 
5855944Sbostic 		default:
5955944Sbostic #ifdef DEBUG
6055944Sbostic 	__TRACE("ADDBYTES(%0.2o, %d, %d)\n", win, y, x);
6155944Sbostic #endif
6256301Selan 
6356238Selan 			lp = win->lines[y];
6456301Selan 			if (lp->flags & __ISPASTEOL) {
6556301Selan 				lp->flags &= ~__ISPASTEOL;
6656301Selan newline:			if (y == win->maxy - 1) {
6756301Selan 					if (win->flags & __SCROLLOK) {
6856301Selan 						SYNCH_OUT;
6956301Selan 						scroll(win);
7056301Selan 						SYNCH_IN;
7156301Selan 						lp = win->lines[y];
7256596Selan 					        x = 0;
7356301Selan 					}
7456301Selan 				} else {
7556301Selan 					y++;
7656301Selan 					lp = win->lines[y];
7756301Selan 					x = 0;
7856301Selan 				}
79*57961Storek 				if (c == '\n')
80*57961Storek 					break;
8156301Selan 			}
8256301Selan 
8356596Selan 			stand = '\0';
8456596Selan 			if (win->flags & __WSTANDOUT || so)
8556596Selan 				stand |= __STANDOUT;
8656301Selan #ifdef DEBUG
8756301Selan 	__TRACE("ADDBYTES: 1: y = %d, x = %d, firstch = %d, lastch = %d\n",
8856715Selan 	    y, x, *win->lines[y]->firstchp, *win->lines[y]->lastchp);
8956301Selan #endif
9056647Selan 			if (lp->line[x].ch != c ||
9156647Selan 			    !(lp->line[x].attr & stand)) {
9256238Selan 				newx = x + win->ch_off;
9356238Selan 				if (!(lp->flags & __ISDIRTY)) {
9456238Selan 					lp->flags |= __ISDIRTY;
9556715Selan 					*lp->firstchp = *lp->lastchp = newx;
9656238Selan 				}
9756715Selan 				else if (newx < *lp->firstchp)
9856715Selan 					*lp->firstchp = newx;
9956715Selan 				else if (newx > *lp->lastchp)
10056715Selan 					*lp->lastchp = newx;
10155945Sbostic #ifdef DEBUG
10255944Sbostic 	__TRACE("ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
10356715Selan 	    *lp->firstchp, *lp->lastchp,
10456715Selan 	    *lp->firstchp - win->ch_off,
10556715Selan 	    *lp->lastchp - win->ch_off);
10655944Sbostic #endif
10755944Sbostic 			}
10856647Selan 			lp->line[x].ch = c;
10956596Selan 			if (stand)
11056647Selan 				lp->line[x].attr |= __STANDOUT;
11156596Selan 			else
11256647Selan 				lp->line[x].attr &= ~__STANDOUT;
11356301Selan 			if (x == win->maxx - 1)
11456301Selan 				lp->flags |= __ISPASTEOL;
11556301Selan 			else
11656301Selan 				x++;
11755944Sbostic #ifdef DEBUG
11855944Sbostic 	__TRACE("ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n",
11956715Selan 	    y, x, *win->lines[y]->firstchp, *win->lines[y]->lastchp);
12055944Sbostic #endif
12155944Sbostic 			break;
12255944Sbostic 		case '\n':
12355944Sbostic 			SYNCH_OUT;
12455944Sbostic 			wclrtoeol(win);
12555944Sbostic 			SYNCH_IN;
12657713Sbostic 			if (__orig_termios.c_oflag & ONLCR)
12755944Sbostic 				x = 0;
12855944Sbostic 			goto newline;
12955944Sbostic 		case '\r':
13055944Sbostic 			x = 0;
13155944Sbostic 			break;
13255944Sbostic 		case '\b':
13355944Sbostic 			if (--x < 0)
13455944Sbostic 				x = 0;
13555944Sbostic 			break;
13655944Sbostic 		}
13755944Sbostic 	}
13855944Sbostic 	SYNCH_OUT;
13957472Sbostic 	return (OK);
14031681Sminshall }
141