Home | History | Annotate | Line # | Download | only in libcurses
addbytes.c revision 1.30.6.2
      1 /*	$NetBSD: addbytes.c,v 1.30.6.2 2007/01/21 17:43:35 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 <assert.h>
     33 #include <sys/cdefs.h>
     34 #ifndef lint
     35 #if 0
     36 static char sccsid[] = "@(#)addbytes.c	8.4 (Berkeley) 5/4/94";
     37 #else
     38 __RCSID("$NetBSD: addbytes.c,v 1.30.6.2 2007/01/21 17:43:35 jdc Exp $");
     39 #endif
     40 #endif				/* not lint */
     41 
     42 #include <stdlib.h>
     43 #include "curses.h"
     44 #include "curses_private.h"
     45 
     46 #define	SYNCH_IN	{y = win->cury; x = win->curx;}
     47 #define	SYNCH_OUT	{win->cury = y; win->curx = x;}
     48 
     49 #ifndef _CURSES_USE_MACROS
     50 
     51 /*
     52  * addbytes --
     53  *      Add the character to the current position in stdscr.
     54  */
     55 int
     56 addbytes(const char *bytes, int count)
     57 {
     58 	return __waddbytes(stdscr, bytes, count, 0);
     59 }
     60 
     61 /*
     62  * waddbytes --
     63  *      Add the character to the current position in the given window.
     64  */
     65 int
     66 waddbytes(WINDOW *win, const char *bytes, int count)
     67 {
     68 	return __waddbytes(win, bytes, count, 0);
     69 }
     70 
     71 /*
     72  * mvaddbytes --
     73  *      Add the characters to stdscr at the location given.
     74  */
     75 int
     76 mvaddbytes(int y, int x, const char *bytes, int count)
     77 {
     78 	return mvwaddbytes(stdscr, y, x, bytes, count);
     79 }
     80 
     81 /*
     82  * mvwaddbytes --
     83  *      Add the characters to the given window at the location given.
     84  */
     85 int
     86 mvwaddbytes(WINDOW *win, int y, int x, const char *bytes, int count)
     87 {
     88 	if (wmove(win, y, x) == ERR)
     89 		return ERR;
     90 
     91 	return __waddbytes(win, bytes, count, 0);
     92 }
     93 
     94 #endif
     95 
     96 /*
     97  * waddbytes --
     98  *	Add the character to the current position in the given window.
     99  */
    100 int
    101 __waddbytes(WINDOW *win, const char *bytes, int count, attr_t attr)
    102 {
    103 	static char	 blanks[] = "        ";
    104 	int		 c, newx, x, y;
    105 	attr_t		 attributes;
    106 	__LINE		*lp;
    107 #ifdef DEBUG
    108 	int             i;
    109 
    110 	for (i = 0; i < win->maxy; i++) {
    111 		assert(win->lines[i]->sentinel == SENTINEL_VALUE);
    112 	}
    113 #endif
    114 
    115 	SYNCH_IN;
    116 	lp = win->lines[y];
    117 
    118 	while (count--) {
    119 		c = *bytes++;
    120 #ifdef DEBUG
    121 		__CTRACE(__CTRACE_INPUT, "ADDBYTES('%c', %x) at (%d, %d)\n",
    122 		    c, attr, y, x);
    123 #endif
    124 		switch (c) {
    125 		case '\t':
    126 			SYNCH_OUT;
    127 			if (waddbytes(win, blanks, 8 - (x % 8)) == ERR)
    128 				return (ERR);
    129 			SYNCH_IN;
    130 			break;
    131 
    132 		default:
    133 #ifdef DEBUG
    134 			__CTRACE(__CTRACE_INPUT, "ADDBYTES(%p, %d, %d)\n",
    135 			    win, y, x);
    136 #endif
    137 
    138 			if (lp->flags & __ISPASTEOL) {
    139 		newline:
    140 				x = 0;
    141 				lp->flags &= ~__ISPASTEOL;
    142 				if (y == win->scr_b) {
    143 #ifdef DEBUG
    144 					__CTRACE(__CTRACE_INPUT,
    145 					    "ADDBYTES - on bottom "
    146 					    "of scrolling region\n");
    147 #endif
    148 					if (!(win->flags & __SCROLLOK))
    149 						return ERR;
    150 					SYNCH_OUT;
    151 					scroll(win);
    152 					SYNCH_IN;
    153 				} else {
    154 					y++;
    155 				}
    156 				lp = win->lines[y];
    157 				if (c == '\n')
    158 					break;
    159 			}
    160 
    161 			attributes = (win->wattr | attr) &
    162 			    (__ATTRIBUTES & ~__COLOR);
    163 			if (attr & __COLOR)
    164 				attributes |= attr & __COLOR;
    165 			else if (win->wattr & __COLOR)
    166 				attributes |= win->wattr & __COLOR;
    167 #ifdef DEBUG
    168 			__CTRACE(__CTRACE_INPUT,
    169 			    "ADDBYTES: 1: y = %d, x = %d, firstch = %d, "
    170 			    "lastch = %d\n",
    171 			    y, x, *win->lines[y]->firstchp,
    172 			    *win->lines[y]->lastchp);
    173 #endif
    174 			/*
    175 			 * Always update the change pointers.  Otherwise,
    176 			 * we could end up not displaying 'blank' characters
    177 			 * when overlapping windows are displayed.
    178 			 */
    179 			newx = x + win->ch_off;
    180 			lp->flags |= __ISDIRTY;
    181 			/*
    182 			 * firstchp/lastchp are shared between
    183 			 * parent window and sub-window.
    184 			 */
    185 			if (newx < *lp->firstchp)
    186 				*lp->firstchp = newx;
    187 			if (newx > *lp->lastchp)
    188 				*lp->lastchp = newx;
    189 #ifdef DEBUG
    190 			__CTRACE(__CTRACE_INPUT,
    191 			    "ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
    192 			    *lp->firstchp, *lp->lastchp,
    193 			    *lp->firstchp - win->ch_off,
    194 			    *lp->lastchp - win->ch_off);
    195 #endif
    196 			if (win->bch != ' ' && c == ' ')
    197 				lp->line[x].ch = win->bch;
    198 			else
    199 				lp->line[x].ch = c;
    200 
    201 #ifdef HAVE_WCHAR
    202 			if (_cursesi_copy_nsp(win->bnsp, &lp->line[x]) == ERR)
    203 				return ERR;
    204 #endif /* HAVE_WCHAR */
    205 
    206 			if (attributes & __COLOR)
    207 				lp->line[x].attr =
    208 				    attributes | (win->battr & ~__COLOR);
    209 			else
    210 				lp->line[x].attr = attributes | win->battr;
    211 #ifdef HAVE_WCHAR
    212 			SET_WCOL(lp->line[x], 1);
    213 #endif /* HAVE_WCHAR */
    214 
    215 			if (x == win->maxx - 1)
    216 				lp->flags |= __ISPASTEOL;
    217 			else
    218 				x++;
    219 #ifdef DEBUG
    220 			__CTRACE(__CTRACE_INPUT,
    221 			    "ADDBYTES: 2: y = %d, x = %d, firstch = %d, "
    222 			    "lastch = %d\n",
    223 			    y, x, *win->lines[y]->firstchp,
    224 			    *win->lines[y]->lastchp);
    225 #endif
    226 			break;
    227 		case '\n':
    228 			SYNCH_OUT;
    229 			wclrtoeol(win);
    230 			SYNCH_IN;
    231 			goto newline;
    232 		case '\r':
    233 			x = 0;
    234 			continue;
    235 		case '\b':
    236 			if (--x < 0)
    237 				x = 0;
    238 			break;
    239 		}
    240 	}
    241 	SYNCH_OUT;
    242 
    243 #ifdef DEBUG
    244 	for (i = 0; i < win->maxy; i++) {
    245 		assert(win->lines[i]->sentinel == SENTINEL_VALUE);
    246 	}
    247 #endif
    248 
    249 	return (OK);
    250 }
    251