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*56715Selan static char sccsid[] = "@(#)addbytes.c 5.13 (Berkeley) 11/11/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 1856596Selan int 1956596Selan waddbytes(win, bytes, count) 2056596Selan WINDOW *win; 2156596Selan char *bytes; 2256596Selan int count; 2356596Selan { 2456596Selan __waddbytes(win, bytes, count, 0); 2556596Selan } 2656596Selan 2756596Selan 2856596Selan 2931681Sminshall /* 3055944Sbostic * waddbytes -- 3155944Sbostic * Add the character to the current position in the given window. 3231681Sminshall */ 3355983Sbostic int 3456596Selan __waddbytes(win, bytes, count, so) 3555944Sbostic register WINDOW *win; 3655944Sbostic register char *bytes; 3755944Sbostic register int count; 3856596Selan int so; 3931681Sminshall { 4055944Sbostic static char blanks[] = " "; 4155944Sbostic register int c, newx, x, y; 4256596Selan char stand; 4356647Selan __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]; 7456596Selan x = 0; 7556301Selan } 7656301Selan } else { 7756301Selan y++; 7856301Selan lp = win->lines[y]; 7956301Selan x = 0; 8056301Selan } 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", 88*56715Selan 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; 95*56715Selan *lp->firstchp = *lp->lastchp = newx; 9656238Selan } 97*56715Selan else if (newx < *lp->firstchp) 98*56715Selan *lp->firstchp = newx; 99*56715Selan else if (newx > *lp->lastchp) 100*56715Selan *lp->lastchp = newx; 10155945Sbostic #ifdef DEBUG 10255944Sbostic __TRACE("ADDBYTES: change gives f/l: %d/%d [%d/%d]\n", 103*56715Selan *lp->firstchp, *lp->lastchp, 104*56715Selan *lp->firstchp - win->ch_off, 105*56715Selan *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", 119*56715Selan 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; 12656073Selan if (origtermio.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; 13955944Sbostic return (OK); 14031681Sminshall } 14156301Selan 142