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*57942Sbostic static char sccsid[] = "@(#)addbytes.c 5.18 (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; 21*57942Sbostic 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; 34*57942Sbostic 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 } 7956301Selan } 8056301Selan 8156596Selan stand = '\0'; 8256596Selan if (win->flags & __WSTANDOUT || so) 8356596Selan stand |= __STANDOUT; 8456301Selan #ifdef DEBUG 8556301Selan __TRACE("ADDBYTES: 1: y = %d, x = %d, firstch = %d, lastch = %d\n", 8656715Selan y, x, *win->lines[y]->firstchp, *win->lines[y]->lastchp); 8756301Selan #endif 8856647Selan if (lp->line[x].ch != c || 8956647Selan !(lp->line[x].attr & stand)) { 9056238Selan newx = x + win->ch_off; 9156238Selan if (!(lp->flags & __ISDIRTY)) { 9256238Selan lp->flags |= __ISDIRTY; 9356715Selan *lp->firstchp = *lp->lastchp = newx; 9456238Selan } 9556715Selan else if (newx < *lp->firstchp) 9656715Selan *lp->firstchp = newx; 9756715Selan else if (newx > *lp->lastchp) 9856715Selan *lp->lastchp = newx; 9955945Sbostic #ifdef DEBUG 10055944Sbostic __TRACE("ADDBYTES: change gives f/l: %d/%d [%d/%d]\n", 10156715Selan *lp->firstchp, *lp->lastchp, 10256715Selan *lp->firstchp - win->ch_off, 10356715Selan *lp->lastchp - win->ch_off); 10455944Sbostic #endif 10555944Sbostic } 10656647Selan lp->line[x].ch = c; 10756596Selan if (stand) 10856647Selan lp->line[x].attr |= __STANDOUT; 10956596Selan else 11056647Selan lp->line[x].attr &= ~__STANDOUT; 11156301Selan if (x == win->maxx - 1) 11256301Selan lp->flags |= __ISPASTEOL; 11356301Selan else 11456301Selan x++; 11555944Sbostic #ifdef DEBUG 11655944Sbostic __TRACE("ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n", 11756715Selan y, x, *win->lines[y]->firstchp, *win->lines[y]->lastchp); 11855944Sbostic #endif 11955944Sbostic break; 12055944Sbostic case '\n': 12155944Sbostic SYNCH_OUT; 12255944Sbostic wclrtoeol(win); 12355944Sbostic SYNCH_IN; 12457713Sbostic if (__orig_termios.c_oflag & ONLCR) 12555944Sbostic x = 0; 12655944Sbostic goto newline; 12755944Sbostic case '\r': 12855944Sbostic x = 0; 12955944Sbostic break; 13055944Sbostic case '\b': 13155944Sbostic if (--x < 0) 13255944Sbostic x = 0; 13355944Sbostic break; 13455944Sbostic } 13555944Sbostic } 13655944Sbostic SYNCH_OUT; 13757472Sbostic return (OK); 13831681Sminshall } 139