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*60058Sbostic static char sccsid[] = "@(#)addbytes.c 5.21 (Berkeley) 05/16/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 1831681Sminshall /* 1955944Sbostic * waddbytes -- 2055944Sbostic * Add the character to the current position in the given window. 2131681Sminshall */ 2255983Sbostic int 2356596Selan __waddbytes(win, bytes, count, so) 2455944Sbostic register WINDOW *win; 2557942Sbostic register const char *bytes; 2655944Sbostic register int count; 2756596Selan int so; 2831681Sminshall { 2955944Sbostic static char blanks[] = " "; 3055944Sbostic register int c, newx, x, y; 3156596Selan char stand; 3256647Selan __LINE *lp; 3331681Sminshall 3455944Sbostic SYNCH_IN; 3556073Selan 3655944Sbostic #ifdef DEBUG 37*60058Sbostic __CTRACE("ADDBYTES('%c') at (%d, %d)\n", c, y, x); 3855944Sbostic #endif 3931681Sminshall while (count--) { 4055944Sbostic c = *bytes++; 4155944Sbostic switch (c) { 4255944Sbostic case '\t': 4355945Sbostic SYNCH_OUT; 4457472Sbostic if (waddbytes(win, blanks, 8 - (x % 8)) == ERR) 4557472Sbostic return (ERR); 4655945Sbostic SYNCH_IN; 4755944Sbostic break; 4831681Sminshall 4955944Sbostic default: 5055944Sbostic #ifdef DEBUG 51*60058Sbostic __CTRACE("ADDBYTES(%0.2o, %d, %d)\n", win, y, x); 5255944Sbostic #endif 5356301Selan 5456238Selan lp = win->lines[y]; 5556301Selan if (lp->flags & __ISPASTEOL) { 5656301Selan lp->flags &= ~__ISPASTEOL; 5756301Selan newline: if (y == win->maxy - 1) { 5856301Selan if (win->flags & __SCROLLOK) { 5956301Selan SYNCH_OUT; 6056301Selan scroll(win); 6156301Selan SYNCH_IN; 6256301Selan lp = win->lines[y]; 6356596Selan x = 0; 6456301Selan } 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 78*60058Sbostic __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 93*60058Sbostic __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 109*60058Sbostic __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; 11757713Sbostic if (__orig_termios.c_oflag & ONLCR) 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