Home | History | Annotate | Line # | Download | only in libcurses
addbytes.c revision 1.15
      1 /*	$NetBSD: addbytes.c,v 1.15 2000/04/11 13:57:08 blymn 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.15 2000/04/11 13:57:08 blymn Exp $");
     42 #endif
     43 #endif				/* not lint */
     44 
     45 #include "curses.h"
     46 #include "curses_private.h"
     47 
     48 #define	SYNCH_IN	{y = win->cury; x = win->curx;}
     49 #define	SYNCH_OUT	{win->cury = y; win->curx = x;}
     50 
     51 /*
     52  * waddbytes --
     53  *	Add the character to the current position in the given window.
     54  */
     55 int
     56 __waddbytes(win, bytes, count, attr)
     57 	WINDOW		*win;
     58 	const char	*bytes;
     59 	int		 count;
     60 	attr_t		 attr;
     61 {
     62 	static char	 blanks[] = "        ";
     63 	int		 c, newx, x, y;
     64 	attr_t		 attributes;
     65 	__LINE		*lp;
     66 
     67 	SYNCH_IN;
     68 
     69 	while (count--) {
     70 		c = *bytes++;
     71 #ifdef DEBUG
     72 		__CTRACE("ADDBYTES('%c') at (%d, %d)\n", c, y, x);
     73 #endif
     74 		switch (c) {
     75 		case '\t':
     76 			SYNCH_OUT;
     77 			if (waddbytes(win, blanks, 8 - (x % 8)) == ERR)
     78 				return (ERR);
     79 			SYNCH_IN;
     80 			break;
     81 
     82 		default:
     83 #ifdef DEBUG
     84 			__CTRACE("ADDBYTES(%0.2o, %d, %d)\n", win, y, x);
     85 #endif
     86 
     87 			lp = win->lines[y];
     88 			if (lp->flags & __ISPASTEOL) {
     89 				lp->flags &= ~__ISPASTEOL;
     90 		newline:	if (y == win->maxy - 1) {
     91 					if (win->flags & __SCROLLOK) {
     92 						SYNCH_OUT;
     93 						scroll(win);
     94 						SYNCH_IN;
     95 						lp = win->lines[y];
     96 						x = 0;
     97 					} else
     98 						return (ERR);
     99 				} else {
    100 					y++;
    101 					lp = win->lines[y];
    102 					x = 0;
    103 				}
    104 				if (c == '\n')
    105 					break;
    106 			}
    107 
    108 			attributes = '\0';
    109 			if (win->wattr & __STANDOUT || attr & __STANDOUT)
    110 				attributes |= __STANDOUT;
    111 			if (win->wattr & __UNDERSCORE || attr & __UNDERSCORE)
    112 				attributes |= __UNDERSCORE;
    113 			if (win->wattr & __REVERSE || attr & __REVERSE)
    114 				attributes |= __REVERSE;
    115 			if (win->wattr & __BLINK || attr & __BLINK)
    116 				attributes |= __BLINK;
    117 			if (win->wattr & __DIM || attr & __DIM)
    118 				attributes |= __DIM;
    119 			if (win->wattr & __BOLD || attr & __BOLD)
    120 				attributes |= __BOLD;
    121 			if (win->wattr & __BLANK || attr & __BLANK)
    122 				attributes |= __BLANK;
    123 			if (win->wattr & __PROTECT || attr & __PROTECT)
    124 				attributes |= __PROTECT;
    125 			if (win->wattr & __ALTCHARSET || attr & __ALTCHARSET)
    126 				attributes |= __ALTCHARSET;
    127 #ifdef DEBUG
    128 			__CTRACE("ADDBYTES: 1: y = %d, x = %d, firstch = %d, lastch = %d\n",
    129 			    y, x, *win->lines[y]->firstchp,
    130 			    *win->lines[y]->lastchp);
    131 #endif
    132 			if (lp->line[x].ch != c ||
    133 			    !(lp->line[x].attr & attributes)) {
    134 				newx = x + win->ch_off;
    135 				if (!(lp->flags & __ISDIRTY)) {
    136 					lp->flags |= __ISDIRTY;
    137 					*lp->firstchp = *lp->lastchp = newx;
    138 				} else
    139 					if (newx < *lp->firstchp)
    140 						*lp->firstchp = newx;
    141 					else
    142 						if (newx > *lp->lastchp)
    143 							*lp->lastchp = newx;
    144 #ifdef DEBUG
    145 				__CTRACE("ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
    146 				    *lp->firstchp, *lp->lastchp,
    147 				    *lp->firstchp - win->ch_off,
    148 				    *lp->lastchp - win->ch_off);
    149 #endif
    150 			}
    151 			lp->line[x].ch = c;
    152 			if (attributes & __STANDOUT)
    153 				lp->line[x].attr |= __STANDOUT;
    154 			else
    155 				lp->line[x].attr &= ~__STANDOUT;
    156 			if (attributes & __UNDERSCORE)
    157 				lp->line[x].attr |= __UNDERSCORE;
    158 			else
    159 				lp->line[x].attr &= ~__UNDERSCORE;
    160 			if (attributes & __REVERSE)
    161 				lp->line[x].attr |= __REVERSE;
    162 			else
    163 				lp->line[x].attr &= ~__REVERSE;
    164 			if (attributes & __BLINK)
    165 				lp->line[x].attr |= __BLINK;
    166 			else
    167 				lp->line[x].attr &= ~__BLINK;
    168 			if (attributes & __DIM)
    169 				lp->line[x].attr |= __DIM;
    170 			else
    171 				lp->line[x].attr &= ~__DIM;
    172 			if (attributes & __BOLD)
    173 				lp->line[x].attr |= __BOLD;
    174 			else
    175 				lp->line[x].attr &= ~__BOLD;
    176 			if (attributes & __BLANK)
    177 				lp->line[x].attr |= __BLANK;
    178 			else
    179 				lp->line[x].attr &= ~__BLANK;
    180 			if (attributes & __PROTECT)
    181 				lp->line[x].attr |= __PROTECT;
    182 			else
    183 				lp->line[x].attr &= ~__PROTECT;
    184 			if (attributes & __ALTCHARSET)
    185 				lp->line[x].attr |= __ALTCHARSET;
    186 			else
    187 				lp->line[x].attr &= ~__ALTCHARSET;
    188 			if (x == win->maxx - 1)
    189 				lp->flags |= __ISPASTEOL;
    190 			else
    191 				x++;
    192 #ifdef DEBUG
    193 			__CTRACE("ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n",
    194 			    y, x, *win->lines[y]->firstchp,
    195 			    *win->lines[y]->lastchp);
    196 #endif
    197 			break;
    198 		case '\n':
    199 			SYNCH_OUT;
    200 			wclrtoeol(win);
    201 			SYNCH_IN;
    202 			if (!NONL)
    203 				x = 0;
    204 			goto newline;
    205 		case '\r':
    206 			x = 0;
    207 			break;
    208 		case '\b':
    209 			if (--x < 0)
    210 				x = 0;
    211 			break;
    212 		}
    213 	}
    214 	SYNCH_OUT;
    215 	return (OK);
    216 }
    217