Home | History | Annotate | Line # | Download | only in libcurses
addbytes.c revision 1.14.6.2
      1 /*	$NetBSD: addbytes.c,v 1.14.6.2 2000/03/05 23:25:17 jdc Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1987, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)addbytes.c	8.4 (Berkeley) 5/4/94";
     40 #else
     41 __RCSID("$NetBSD: addbytes.c,v 1.14.6.2 2000/03/05 23:25:17 jdc Exp $");
     42 #endif
     43 #endif				/* not lint */
     44 
     45 #include "curses.h"
     46 
     47 #define	SYNCH_IN	{y = win->cury; x = win->curx;}
     48 #define	SYNCH_OUT	{win->cury = y; win->curx = x;}
     49 
     50 /*
     51  * waddbytes --
     52  *	Add the character to the current position in the given window.
     53  */
     54 int
     55 __waddbytes(win, bytes, count, attr)
     56 	WINDOW		*win;
     57 	const char	*bytes;
     58 	int		 count;
     59 	attr_t		 attr;
     60 {
     61 	static char	 blanks[] = "        ";
     62 	int		 c, newx, x, y;
     63 	attr_t		 attributes;
     64 	__LINE		*lp;
     65 
     66 	SYNCH_IN;
     67 
     68 	while (count--) {
     69 		c = *bytes++;
     70 #ifdef DEBUG
     71 		__CTRACE("ADDBYTES('%c') at (%d, %d)\n", c, y, x);
     72 #endif
     73 		switch (c) {
     74 		case '\t':
     75 			SYNCH_OUT;
     76 			if (waddbytes(win, blanks, 8 - (x % 8)) == ERR)
     77 				return (ERR);
     78 			SYNCH_IN;
     79 			break;
     80 
     81 		default:
     82 #ifdef DEBUG
     83 			__CTRACE("ADDBYTES(%0.2o, %d, %d)\n", win, y, x);
     84 #endif
     85 
     86 			lp = win->lines[y];
     87 			if (lp->flags & __ISPASTEOL) {
     88 				lp->flags &= ~__ISPASTEOL;
     89 		newline:	if (y == win->maxy - 1) {
     90 					if (win->flags & __SCROLLOK) {
     91 						SYNCH_OUT;
     92 						scroll(win);
     93 						SYNCH_IN;
     94 						lp = win->lines[y];
     95 						x = 0;
     96 					} else
     97 						return (ERR);
     98 				} else {
     99 					y++;
    100 					lp = win->lines[y];
    101 					x = 0;
    102 				}
    103 				if (c == '\n')
    104 					break;
    105 			}
    106 
    107 			attributes = '\0';
    108 			if (win->wattr & __STANDOUT || attr & __STANDOUT)
    109 				attributes |= __STANDOUT;
    110 			if (win->wattr & __UNDERSCORE || attr & __UNDERSCORE)
    111 				attributes |= __UNDERSCORE;
    112 			if (win->wattr & __REVERSE || attr & __REVERSE)
    113 				attributes |= __REVERSE;
    114 			if (win->wattr & __BLINK || attr & __BLINK)
    115 				attributes |= __BLINK;
    116 			if (win->wattr & __DIM || attr & __DIM)
    117 				attributes |= __DIM;
    118 			if (win->wattr & __BOLD || attr & __BOLD)
    119 				attributes |= __BOLD;
    120 			if (win->wattr & __BLANK || attr & __BLANK)
    121 				attributes |= __BLANK;
    122 			if (win->wattr & __PROTECT || attr & __PROTECT)
    123 				attributes |= __PROTECT;
    124 			if (win->wattr & __ALTCHARSET || attr & __ALTCHARSET)
    125 				attributes |= __ALTCHARSET;
    126 #ifdef DEBUG
    127 			__CTRACE("ADDBYTES: 1: y = %d, x = %d, firstch = %d, lastch = %d\n",
    128 			    y, x, *win->lines[y]->firstchp,
    129 			    *win->lines[y]->lastchp);
    130 #endif
    131 			if (lp->line[x].ch != c ||
    132 			    !(lp->line[x].attr & attributes)) {
    133 				newx = x + win->ch_off;
    134 				if (!(lp->flags & __ISDIRTY)) {
    135 					lp->flags |= __ISDIRTY;
    136 					*lp->firstchp = *lp->lastchp = newx;
    137 				} else
    138 					if (newx < *lp->firstchp)
    139 						*lp->firstchp = newx;
    140 					else
    141 						if (newx > *lp->lastchp)
    142 							*lp->lastchp = newx;
    143 #ifdef DEBUG
    144 				__CTRACE("ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
    145 				    *lp->firstchp, *lp->lastchp,
    146 				    *lp->firstchp - win->ch_off,
    147 				    *lp->lastchp - win->ch_off);
    148 #endif
    149 			}
    150 			lp->line[x].ch = c;
    151 			if (attributes & __STANDOUT)
    152 				lp->line[x].attr |= __STANDOUT;
    153 			else
    154 				lp->line[x].attr &= ~__STANDOUT;
    155 			if (attributes & __UNDERSCORE)
    156 				lp->line[x].attr |= __UNDERSCORE;
    157 			else
    158 				lp->line[x].attr &= ~__UNDERSCORE;
    159 			if (attributes & __REVERSE)
    160 				lp->line[x].attr |= __REVERSE;
    161 			else
    162 				lp->line[x].attr &= ~__REVERSE;
    163 			if (attributes & __BLINK)
    164 				lp->line[x].attr |= __BLINK;
    165 			else
    166 				lp->line[x].attr &= ~__BLINK;
    167 			if (attributes & __DIM)
    168 				lp->line[x].attr |= __DIM;
    169 			else
    170 				lp->line[x].attr &= ~__DIM;
    171 			if (attributes & __BOLD)
    172 				lp->line[x].attr |= __BOLD;
    173 			else
    174 				lp->line[x].attr &= ~__BOLD;
    175 			if (attributes & __BLANK)
    176 				lp->line[x].attr |= __BLANK;
    177 			else
    178 				lp->line[x].attr &= ~__BLANK;
    179 			if (attributes & __PROTECT)
    180 				lp->line[x].attr |= __PROTECT;
    181 			else
    182 				lp->line[x].attr &= ~__PROTECT;
    183 			if (attributes & __ALTCHARSET)
    184 				lp->line[x].attr |= __ALTCHARSET;
    185 			else
    186 				lp->line[x].attr &= ~__ALTCHARSET;
    187 			if (x == win->maxx - 1)
    188 				lp->flags |= __ISPASTEOL;
    189 			else
    190 				x++;
    191 #ifdef DEBUG
    192 			__CTRACE("ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n",
    193 			    y, x, *win->lines[y]->firstchp,
    194 			    *win->lines[y]->lastchp);
    195 #endif
    196 			break;
    197 		case '\n':
    198 			SYNCH_OUT;
    199 			wclrtoeol(win);
    200 			SYNCH_IN;
    201 			if (!NONL)
    202 				x = 0;
    203 			goto newline;
    204 		case '\r':
    205 			x = 0;
    206 			break;
    207 		case '\b':
    208 			if (--x < 0)
    209 				x = 0;
    210 			break;
    211 		}
    212 	}
    213 	SYNCH_OUT;
    214 	return (OK);
    215 }
    216