Home | History | Annotate | Line # | Download | only in libcurses
insstr.c revision 1.11
      1  1.11  blymn /*   $NetBSD: insstr.c,v 1.11 2022/10/19 06:09:27 blymn Exp $ */
      2   1.1  blymn 
      3   1.1  blymn /*
      4   1.1  blymn  * Copyright (c) 2005 The NetBSD Foundation Inc.
      5   1.1  blymn  * All rights reserved.
      6   1.1  blymn  *
      7   1.1  blymn  * This code is derived from code donated to the NetBSD Foundation
      8   1.1  blymn  * by Ruibiao Qiu <ruibiao (at) arl.wustl.edu,ruibiao (at) gmail.com>.
      9   1.1  blymn  *
     10   1.1  blymn  *
     11   1.1  blymn  * Redistribution and use in source and binary forms, with or without
     12   1.1  blymn  * modification, are permitted provided that the following conditions
     13   1.1  blymn  * are met:
     14   1.1  blymn  * 1. Redistributions of source code must retain the above copyright
     15   1.1  blymn  *	notice, this list of conditions and the following disclaimer.
     16   1.1  blymn  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1  blymn  *	notice, this list of conditions and the following disclaimer in the
     18   1.1  blymn  *	documentation and/or other materials provided with the distribution.
     19   1.1  blymn  * 3. Neither the name of the NetBSD Foundation nor the names of its
     20   1.1  blymn  *	contributors may be used to endorse or promote products derived
     21   1.1  blymn  *	from this software without specific prior written permission.
     22   1.1  blymn  *
     23   1.1  blymn  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
     24   1.1  blymn  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     25   1.1  blymn  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     26   1.1  blymn  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27   1.1  blymn  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28   1.1  blymn  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29   1.1  blymn  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30   1.1  blymn  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31   1.1  blymn  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32   1.1  blymn  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33   1.1  blymn  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34   1.1  blymn  * SUCH DAMAGE.
     35   1.1  blymn  */
     36   1.1  blymn 
     37   1.1  blymn #include <sys/cdefs.h>
     38   1.1  blymn #ifndef lint
     39  1.11  blymn __RCSID("$NetBSD: insstr.c,v 1.11 2022/10/19 06:09:27 blymn Exp $");
     40   1.1  blymn #endif						  /* not lint */
     41   1.1  blymn 
     42   1.1  blymn #include <string.h>
     43   1.1  blymn #include <stdlib.h>
     44   1.1  blymn 
     45   1.1  blymn #include "curses.h"
     46   1.1  blymn #include "curses_private.h"
     47   1.1  blymn 
     48   1.1  blymn #ifndef _CURSES_USE_MACROS
     49   1.1  blymn 
     50   1.1  blymn /*
     51   1.1  blymn  * insstr --
     52   1.1  blymn  *	insert a multi-byte character string into the current window
     53   1.1  blymn  */
     54   1.1  blymn int
     55   1.1  blymn insstr(const char *str)
     56   1.1  blymn {
     57   1.5    roy 
     58   1.1  blymn 	return winsstr(stdscr, str);
     59   1.1  blymn }
     60   1.1  blymn 
     61   1.1  blymn /*
     62   1.1  blymn  * insnstr --
     63   1.1  blymn  *	insert a multi-byte character string into the current window
     64   1.1  blymn  *	with at most n characters
     65   1.1  blymn  */
     66   1.1  blymn int
     67   1.1  blymn insnstr(const char *str, int n)
     68   1.1  blymn {
     69   1.5    roy 
     70   1.1  blymn 	return winsnstr(stdscr, str, n);
     71   1.1  blymn }
     72   1.1  blymn 
     73   1.1  blymn /*
     74   1.1  blymn  * mvinsstr --
     75   1.1  blymn  *	  Do an insert-string on the line at (y, x).
     76   1.1  blymn  */
     77   1.1  blymn int
     78   1.1  blymn mvinsstr(int y, int x, const char *str)
     79   1.1  blymn {
     80   1.5    roy 
     81   1.1  blymn 	return mvwinsstr(stdscr, y, x, str);
     82   1.1  blymn }
     83   1.1  blymn 
     84   1.1  blymn /*
     85   1.1  blymn  * mvinsnstr --
     86   1.1  blymn  *	  Do an insert-n-string on the line at (y, x).
     87   1.1  blymn  */
     88   1.1  blymn int
     89   1.1  blymn mvinsnstr(int y, int x, const char *str, int n)
     90   1.1  blymn {
     91   1.5    roy 
     92   1.1  blymn 	return mvwinsnstr(stdscr, y, x, str, n);
     93   1.1  blymn }
     94   1.1  blymn 
     95   1.1  blymn /*
     96   1.1  blymn  * mvwinsstr --
     97   1.1  blymn  *	  Do an insert-string on the line at (y, x) in the given window.
     98   1.1  blymn  */
     99   1.1  blymn int
    100   1.1  blymn mvwinsstr(WINDOW *win, int y, int x, const char *str)
    101   1.1  blymn {
    102   1.5    roy 
    103   1.7  blymn 	if (wmove(win, y, x) == ERR)
    104   1.1  blymn 		return ERR;
    105   1.1  blymn 
    106   1.8    uwe 	return winsstr(win, str);
    107   1.1  blymn }
    108   1.1  blymn 
    109   1.1  blymn /*
    110   1.1  blymn  * mvwinsnstr --
    111   1.1  blymn  *	  Do an insert-n-string on the line at (y, x) in the given window.
    112   1.1  blymn  */
    113   1.1  blymn int
    114   1.1  blymn mvwinsnstr(WINDOW *win, int y, int x, const char *str, int n)
    115   1.1  blymn {
    116   1.5    roy 
    117   1.7  blymn 	if (wmove(win, y, x) == ERR)
    118   1.1  blymn 		return ERR;
    119   1.1  blymn 
    120   1.8    uwe 	return winsnstr(win, str, n);
    121   1.1  blymn }
    122   1.1  blymn 
    123   1.1  blymn #endif
    124   1.1  blymn 
    125   1.1  blymn /*
    126   1.1  blymn  * winsstr --
    127   1.1  blymn  *	Do an insert-string on the line, leaving (cury, curx) unchanged.
    128   1.1  blymn  *	No wrapping.
    129   1.1  blymn  */
    130   1.1  blymn int
    131   1.1  blymn winsstr(WINDOW *win, const char *str)
    132   1.1  blymn {
    133   1.5    roy 
    134   1.5    roy 	return winsnstr(win, str, -1);
    135   1.1  blymn }
    136   1.1  blymn 
    137   1.1  blymn /*
    138   1.1  blymn  * winsnstr --
    139   1.1  blymn  *	Do an insert-n-string on the line, leaving (cury, curx) unchanged.
    140   1.1  blymn  *	Performs wrapping.
    141   1.1  blymn  */
    142   1.1  blymn int
    143   1.1  blymn winsnstr(WINDOW *win, const char *str, int n)
    144   1.1  blymn {
    145   1.1  blymn 	__LDATA	*end, *temp1, *temp2;
    146   1.1  blymn 	const char *scp;
    147   1.1  blymn 	int len, x;
    148   1.1  blymn 	__LINE *lnp;
    149   1.1  blymn #ifdef HAVE_WCHAR
    150   1.1  blymn 	nschar_t *np, *tnp;
    151   1.1  blymn #endif /* HAVE_WCHAR */
    152   1.1  blymn 
    153   1.1  blymn 	/* find string length */
    154   1.5    roy 	if (n > 0)
    155   1.5    roy 		for (scp = str, len = 0; n-- && *scp++; ++len);
    156   1.1  blymn 	else
    157   1.5    roy 		for (scp = str, len = 0; *scp++; ++len);
    158   1.2  blymn 	__CTRACE(__CTRACE_INPUT, "winsnstr: len = %d\n", len);
    159   1.1  blymn 
    160   1.1  blymn 	/* move string */
    161   1.3    roy 	end = &win->alines[win->cury]->line[win->curx];
    162   1.5    roy 	if (len < win->maxx - win->curx) {
    163   1.2  blymn 		__CTRACE(__CTRACE_INPUT, "winsnstr: shift %d cells\n", len);
    164   1.3    roy 		temp1 = &win->alines[win->cury]->line[win->maxx - 1];
    165   1.1  blymn 		temp2 = temp1 - len;
    166   1.1  blymn 		while (temp2 >= end) {
    167   1.1  blymn #ifdef HAVE_WCHAR
    168   1.1  blymn 			np = temp1->nsp;
    169   1.1  blymn 			if (np){
    170   1.5    roy 				while (np) {
    171   1.1  blymn 					tnp = np->next;
    172   1.5    roy 					free(np);
    173   1.1  blymn 					np = tnp;
    174   1.1  blymn 				}
    175   1.1  blymn 				temp1->nsp = NULL;
    176   1.1  blymn 			}
    177   1.1  blymn #endif /* HAVE_WCHAR */
    178   1.5    roy 			(void)memcpy(temp1, temp2, sizeof(__LDATA));
    179   1.1  blymn 			temp1--, temp2--;
    180   1.1  blymn 		}
    181   1.1  blymn 	}
    182   1.1  blymn 
    183   1.5    roy 	for (scp = str, temp1 = end, x = win->curx;
    184   1.5    roy 	     *scp && x < len + win->curx && x < win->maxx;
    185   1.5    roy 	     scp++, temp1++, x++)
    186   1.5    roy 	{
    187   1.1  blymn 		temp1->ch = (wchar_t)*scp & __CHARTEXT;
    188   1.1  blymn 		temp1->attr = win->wattr;
    189  1.11  blymn 		temp1->cflags &= ~CA_BACKGROUND;
    190  1.11  blymn 		temp1->cflags &= ~CA_CONTINUATION;
    191   1.1  blymn #ifdef HAVE_WCHAR
    192  1.10  blymn 		temp1->wcols = 1;
    193   1.1  blymn #endif /* HAVE_WCHAR */
    194   1.1  blymn 	}
    195   1.1  blymn #ifdef DEBUG
    196   1.1  blymn 	{
    197   1.1  blymn 		int i;
    198   1.1  blymn 
    199   1.5    roy 		for (i = win->curx; i < win->curx + len; i++) {
    200   1.2  blymn 			__CTRACE(__CTRACE_INPUT,
    201   1.2  blymn 			    "winsnstr: (%d,%d)=('%c',%x)\n", win->cury, i,
    202   1.3    roy 			    win->alines[win->cury]->line[i].ch,
    203   1.3    roy 			    win->alines[win->cury]->line[i].attr);
    204   1.1  blymn 		}
    205   1.1  blymn 	}
    206   1.1  blymn #endif /* DEBUG */
    207   1.5    roy 	lnp = win->alines[win->cury];
    208   1.1  blymn 	lnp->flags |= __ISDIRTY;
    209   1.5    roy 	if (win->ch_off < *lnp->firstchp)
    210   1.1  blymn 		*lnp->firstchp = win->ch_off;
    211   1.5    roy 	if (win->ch_off + win->maxx - 1 > *lnp->lastchp)
    212   1.1  blymn 		*lnp->lastchp = win->ch_off + win->maxx - 1;
    213   1.1  blymn 	__touchline(win, (int)win->cury, (int)win->curx, (int)win->maxx - 1);
    214   1.4    roy 	__sync(win);
    215   1.1  blymn 	return OK;
    216   1.1  blymn }
    217