Home | History | Annotate | Line # | Download | only in libcurses
addbytes.c revision 1.30.6.5
      1 /*	$NetBSD: addbytes.c,v 1.30.6.5 2007/01/22 11:14:05 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)addbytes.c	8.4 (Berkeley) 5/4/94";
     36 #else
     37 __RCSID("$NetBSD: addbytes.c,v 1.30.6.5 2007/01/22 11:14:05 jdc Exp $");
     38 #endif
     39 #endif				/* not lint */
     40 
     41 #include <stdlib.h>
     42 #include "curses.h"
     43 #include "curses_private.h"
     44 #ifdef DEBUG
     45 #include <assert.h>
     46 #endif
     47 
     48 #define	SYNCH_IN	{y = win->cury; x = win->curx;}
     49 #define	SYNCH_OUT	{win->cury = y; win->curx = x;}
     50 
     51 #ifndef _CURSES_USE_MACROS
     52 
     53 /*
     54  * addbytes --
     55  *      Add the character to the current position in stdscr.
     56  */
     57 int
     58 addbytes(const char *bytes, int count)
     59 {
     60 	return __waddbytes(stdscr, bytes, count, 0);
     61 }
     62 
     63 /*
     64  * waddbytes --
     65  *      Add the character to the current position in the given window.
     66  */
     67 int
     68 waddbytes(WINDOW *win, const char *bytes, int count)
     69 {
     70 	return __waddbytes(win, bytes, count, 0);
     71 }
     72 
     73 /*
     74  * mvaddbytes --
     75  *      Add the characters to stdscr at the location given.
     76  */
     77 int
     78 mvaddbytes(int y, int x, const char *bytes, int count)
     79 {
     80 	return mvwaddbytes(stdscr, y, x, bytes, count);
     81 }
     82 
     83 /*
     84  * mvwaddbytes --
     85  *      Add the characters to the given window at the location given.
     86  */
     87 int
     88 mvwaddbytes(WINDOW *win, int y, int x, const char *bytes, int count)
     89 {
     90 	if (wmove(win, y, x) == ERR)
     91 		return ERR;
     92 
     93 	return __waddbytes(win, bytes, count, 0);
     94 }
     95 
     96 #endif
     97 
     98 /*
     99  * waddbytes --
    100  *	Add the character to the current position in the given window.
    101  */
    102 int
    103 __waddbytes(WINDOW *win, const char *bytes, int count, attr_t attr)
    104 {
    105 	static char	 blanks[] = "        ";
    106 	int		 c, newx, x, y;
    107 	attr_t		 attributes;
    108 	__LINE		*lp;
    109 #ifdef DEBUG
    110 	int             i;
    111 
    112 	for (i = 0; i < win->maxy; i++) {
    113 		assert(win->lines[i]->sentinel == SENTINEL_VALUE);
    114 	}
    115 #endif
    116 
    117 	SYNCH_IN;
    118 	lp = win->lines[y];
    119 
    120 	while (count--) {
    121 		c = *bytes++;
    122 #ifdef DEBUG
    123 		__CTRACE(__CTRACE_INPUT, "ADDBYTES('%c', %x) at (%d, %d)\n",
    124 		    c, attr, y, x);
    125 #endif
    126 		switch (c) {
    127 		case '\t':
    128 			SYNCH_OUT;
    129 			if (waddbytes(win, blanks, 8 - (x % 8)) == ERR)
    130 				return (ERR);
    131 			SYNCH_IN;
    132 			break;
    133 
    134 		default:
    135 #ifdef DEBUG
    136 			__CTRACE(__CTRACE_INPUT, "ADDBYTES(%p, %d, %d)\n",
    137 			    win, y, x);
    138 #endif
    139 
    140 			if (lp->flags & __ISPASTEOL) {
    141 		newline:
    142 				x = 0;
    143 				lp->flags &= ~__ISPASTEOL;
    144 				if (y == win->scr_b) {
    145 #ifdef DEBUG
    146 					__CTRACE(__CTRACE_INPUT,
    147 					    "ADDBYTES - on bottom "
    148 					    "of scrolling region\n");
    149 #endif
    150 					if (!(win->flags & __SCROLLOK))
    151 						return ERR;
    152 					SYNCH_OUT;
    153 					scroll(win);
    154 					SYNCH_IN;
    155 				} else {
    156 					y++;
    157 				}
    158 				lp = win->lines[y];
    159 				if (c == '\n')
    160 					break;
    161 			}
    162 
    163 			attributes = (win->wattr | attr) &
    164 			    (__ATTRIBUTES & ~__COLOR);
    165 			if (attr & __COLOR)
    166 				attributes |= attr & __COLOR;
    167 			else if (win->wattr & __COLOR)
    168 				attributes |= win->wattr & __COLOR;
    169 #ifdef DEBUG
    170 			__CTRACE(__CTRACE_INPUT,
    171 			    "ADDBYTES: 1: y = %d, x = %d, firstch = %d, "
    172 			    "lastch = %d\n",
    173 			    y, x, *win->lines[y]->firstchp,
    174 			    *win->lines[y]->lastchp);
    175 #endif
    176 			/*
    177 			 * Always update the change pointers.  Otherwise,
    178 			 * we could end up not displaying 'blank' characters
    179 			 * when overlapping windows are displayed.
    180 			 */
    181 			newx = x + win->ch_off;
    182 			lp->flags |= __ISDIRTY;
    183 			/*
    184 			 * firstchp/lastchp are shared between
    185 			 * parent window and sub-window.
    186 			 */
    187 			if (newx < *lp->firstchp)
    188 				*lp->firstchp = newx;
    189 			if (newx > *lp->lastchp)
    190 				*lp->lastchp = newx;
    191 #ifdef DEBUG
    192 			__CTRACE(__CTRACE_INPUT,
    193 			    "ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
    194 			    *lp->firstchp, *lp->lastchp,
    195 			    *lp->firstchp - win->ch_off,
    196 			    *lp->lastchp - win->ch_off);
    197 #endif
    198 			if (win->bch != ' ' && c == ' ')
    199 				lp->line[x].ch = win->bch;
    200 			else
    201 				lp->line[x].ch = c;
    202 
    203 #ifdef HAVE_WCHAR
    204 			if (_cursesi_copy_nsp(win->bnsp, &lp->line[x]) == ERR)
    205 				return ERR;
    206 			SET_WCOL(lp->line[x], 1);
    207 #endif /* HAVE_WCHAR */
    208 
    209 			if (attributes & __COLOR)
    210 				lp->line[x].attr =
    211 				    attributes | (win->battr & ~__COLOR);
    212 			else
    213 				lp->line[x].attr = attributes | win->battr;
    214 #ifdef HAVE_WCHAR
    215 			SET_WCOL(lp->line[x], 1);
    216 #endif /* HAVE_WCHAR */
    217 
    218 			if (x == win->maxx - 1)
    219 				lp->flags |= __ISPASTEOL;
    220 			else
    221 				x++;
    222 #ifdef DEBUG
    223 			__CTRACE(__CTRACE_INPUT,
    224 			    "ADDBYTES: 2: y = %d, x = %d, firstch = %d, "
    225 			    "lastch = %d\n",
    226 			    y, x, *win->lines[y]->firstchp,
    227 			    *win->lines[y]->lastchp);
    228 #endif
    229 			break;
    230 		case '\n':
    231 			SYNCH_OUT;
    232 			wclrtoeol(win);
    233 			SYNCH_IN;
    234 			goto newline;
    235 		case '\r':
    236 			x = 0;
    237 			continue;
    238 		case '\b':
    239 			if (--x < 0)
    240 				x = 0;
    241 			break;
    242 		}
    243 	}
    244 	SYNCH_OUT;
    245 
    246 #ifdef DEBUG
    247 	for (i = 0; i < win->maxy; i++) {
    248 		assert(win->lines[i]->sentinel == SENTINEL_VALUE);
    249 	}
    250 #endif
    251 
    252 	return (OK);
    253 }
    254