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