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*56596Selan static char sccsid[] = "@(#)addbytes.c 5.11 (Berkeley) 10/20/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 18*56596Selan int 19*56596Selan waddbytes(win, bytes, count) 20*56596Selan WINDOW *win; 21*56596Selan char *bytes; 22*56596Selan int count; 23*56596Selan { 24*56596Selan __waddbytes(win, bytes, count, 0); 25*56596Selan } 26*56596Selan 27*56596Selan 28*56596Selan 2931681Sminshall /* 3055944Sbostic * waddbytes -- 3155944Sbostic * Add the character to the current position in the given window. 3231681Sminshall */ 3355983Sbostic int 34*56596Selan __waddbytes(win, bytes, count, so) 3555944Sbostic register WINDOW *win; 3655944Sbostic register char *bytes; 3755944Sbostic register int count; 38*56596Selan int so; 3931681Sminshall { 4055944Sbostic static char blanks[] = " "; 4155944Sbostic register int c, newx, x, y; 42*56596Selan char stand; 4356238Selan LINE *lp; 4431681Sminshall 4555944Sbostic SYNCH_IN; 4656073Selan 4755944Sbostic #ifdef DEBUG 4855944Sbostic __TRACE("ADDBYTES('%c') at (%d, %d)\n", c, y, x); 4955944Sbostic #endif 5031681Sminshall while (count--) { 5155944Sbostic c = *bytes++; 5255944Sbostic switch (c) { 5355944Sbostic case '\t': 5455945Sbostic SYNCH_OUT; 5555944Sbostic if (waddbytes(win, blanks, 8 - (x % 8)) == ERR) 5655944Sbostic return (ERR); 5755945Sbostic SYNCH_IN; 5855944Sbostic break; 5931681Sminshall 6055944Sbostic default: 6155944Sbostic #ifdef DEBUG 6255944Sbostic __TRACE("ADDBYTES(%0.2o, %d, %d)\n", win, y, x); 6355944Sbostic #endif 6456301Selan 6556238Selan lp = win->lines[y]; 6656301Selan if (lp->flags & __ISPASTEOL) { 6756301Selan lp->flags &= ~__ISPASTEOL; 6856301Selan newline: if (y == win->maxy - 1) { 6956301Selan if (win->flags & __SCROLLOK) { 7056301Selan SYNCH_OUT; 7156301Selan scroll(win); 7256301Selan SYNCH_IN; 7356301Selan lp = win->lines[y]; 74*56596Selan x = 0; 7556301Selan } 7656301Selan } else { 7756301Selan y++; 7856301Selan lp = win->lines[y]; 7956301Selan x = 0; 8056301Selan } 8156301Selan } 8256301Selan 83*56596Selan stand = '\0'; 84*56596Selan if (win->flags & __WSTANDOUT || so) 85*56596Selan stand |= __STANDOUT; 8656301Selan #ifdef DEBUG 8756301Selan __TRACE("ADDBYTES: 1: y = %d, x = %d, firstch = %d, lastch = %d\n", 8856301Selan y, x, win->lines[y]->firstch, win->lines[y]->lastch); 8956301Selan #endif 90*56596Selan if (lp->line[x] != c || !(lp->standout[x] & stand)) { 9156238Selan newx = x + win->ch_off; 9256238Selan if (!(lp->flags & __ISDIRTY)) { 9356238Selan lp->flags |= __ISDIRTY; 9456238Selan lp->firstch = lp->lastch = newx; 9556238Selan } 9656238Selan else if (newx < lp->firstch) 9756238Selan lp->firstch = newx; 9856238Selan else if (newx > lp->lastch) 9956238Selan lp->lastch = newx; 10055945Sbostic #ifdef DEBUG 10155944Sbostic __TRACE("ADDBYTES: change gives f/l: %d/%d [%d/%d]\n", 10256238Selan lp->firstch, lp->lastch, 10356238Selan lp->firstch - win->ch_off, 10456238Selan lp->lastch - win->ch_off); 10555944Sbostic #endif 10655944Sbostic } 10756238Selan lp->line[x] = c; 108*56596Selan if (stand) 109*56596Selan lp->standout[x] |= __STANDOUT; 110*56596Selan else 111*56596Selan lp->standout[x] &= ~__STANDOUT; 11256301Selan if (x == win->maxx - 1) 11356301Selan lp->flags |= __ISPASTEOL; 11456301Selan else 11556301Selan x++; 11655944Sbostic #ifdef DEBUG 11755944Sbostic __TRACE("ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n", 11856238Selan y, x, win->lines[y]->firstch, win->lines[y]->lastch); 11955944Sbostic #endif 12055944Sbostic break; 12155944Sbostic case '\n': 12255944Sbostic SYNCH_OUT; 12355944Sbostic wclrtoeol(win); 12455944Sbostic SYNCH_IN; 12556073Selan if (origtermio.c_oflag & ONLCR) 12655944Sbostic x = 0; 12755944Sbostic goto newline; 12855944Sbostic case '\r': 12955944Sbostic x = 0; 13055944Sbostic break; 13155944Sbostic case '\b': 13255944Sbostic if (--x < 0) 13355944Sbostic x = 0; 13455944Sbostic break; 13555944Sbostic } 13655944Sbostic } 13755944Sbostic SYNCH_OUT; 13855944Sbostic return (OK); 13931681Sminshall } 14056301Selan 141