Home | History | Annotate | Line # | Download | only in libcurses
      1  1.12   blymn /*	$NetBSD: inchstr.c,v 1.12 2024/12/23 02:58:03 blymn Exp $	*/
      2   1.1  simonb 
      3   1.1  simonb /*
      4   1.1  simonb  * Copyright 2001 Wasabi Systems, Inc.
      5   1.1  simonb  * All rights reserved.
      6   1.1  simonb  *
      7   1.1  simonb  * Written by Simon Burge for Wasabi Systems, Inc.
      8   1.1  simonb  *
      9   1.1  simonb  * Redistribution and use in source and binary forms, with or without
     10   1.1  simonb  * modification, are permitted provided that the following conditions
     11   1.1  simonb  * are met:
     12   1.1  simonb  * 1. Redistributions of source code must retain the above copyright
     13   1.1  simonb  *    notice, this list of conditions and the following disclaimer.
     14   1.1  simonb  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1  simonb  *    notice, this list of conditions and the following disclaimer in the
     16   1.1  simonb  *    documentation and/or other materials provided with the distribution.
     17   1.1  simonb  * 3. All advertising materials mentioning features or use of this software
     18   1.1  simonb  *    must display the following acknowledgement:
     19   1.1  simonb  *      This product includes software developed for the NetBSD Project by
     20   1.1  simonb  *      Wasabi Systems, Inc.
     21   1.1  simonb  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22   1.1  simonb  *    or promote products derived from this software without specific prior
     23   1.1  simonb  *    written permission.
     24   1.1  simonb  *
     25   1.1  simonb  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND ANY
     26   1.5   blymn  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27   1.1  simonb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28   1.5   blymn  * ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC. BE
     29   1.5   blymn  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30   1.5   blymn  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31   1.1  simonb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32   1.1  simonb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33   1.1  simonb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34   1.5   blymn  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     35   1.1  simonb  * THE POSSIBILITY OF SUCH DAMAGE.
     36   1.1  simonb  */
     37   1.1  simonb 
     38   1.1  simonb #include <sys/cdefs.h>
     39   1.1  simonb #ifndef lint
     40  1.12   blymn __RCSID("$NetBSD: inchstr.c,v 1.12 2024/12/23 02:58:03 blymn Exp $");
     41   1.1  simonb #endif				/* not lint */
     42   1.1  simonb 
     43   1.1  simonb #include "curses.h"
     44   1.1  simonb #include "curses_private.h"
     45   1.1  simonb 
     46   1.1  simonb #ifndef _CURSES_USE_MACROS
     47   1.1  simonb 
     48   1.1  simonb /*
     49   1.1  simonb  * inchstr, inchnstr --
     50   1.1  simonb  *	Return an array of characters at cursor position from stdscr.
     51   1.1  simonb  */
     52   1.1  simonb __warn_references(inchstr,
     53  1.11  rillig     "warning: this program uses inchstr(), which is unsafe.")
     54   1.1  simonb int
     55   1.1  simonb inchstr(chtype *chstr)
     56   1.1  simonb {
     57   1.7     roy 
     58   1.1  simonb 	return winchstr(stdscr, chstr);
     59   1.1  simonb }
     60   1.1  simonb 
     61   1.1  simonb int
     62   1.1  simonb inchnstr(chtype *chstr, int n)
     63   1.1  simonb {
     64   1.7     roy 
     65   1.1  simonb 	return winchnstr(stdscr, chstr, n);
     66   1.1  simonb }
     67   1.1  simonb 
     68   1.1  simonb /*
     69   1.1  simonb  * mvinchstr, mvinchnstr --
     70   1.1  simonb  *      Return an array of characters at position (y, x) from stdscr.
     71   1.1  simonb  */
     72   1.1  simonb __warn_references(mvinchstr,
     73  1.11  rillig     "warning: this program uses mvinchstr(), which is unsafe.")
     74   1.1  simonb int
     75   1.1  simonb mvinchstr(int y, int x, chtype *chstr)
     76   1.1  simonb {
     77   1.7     roy 
     78   1.1  simonb 	return mvwinchstr(stdscr, y, x, chstr);
     79   1.1  simonb }
     80   1.1  simonb 
     81   1.1  simonb int
     82   1.1  simonb mvinchnstr(int y, int x, chtype *chstr, int n)
     83   1.1  simonb {
     84   1.7     roy 
     85   1.1  simonb 	return mvwinchnstr(stdscr, y, x, chstr, n);
     86   1.1  simonb }
     87   1.1  simonb 
     88   1.1  simonb /*
     89   1.1  simonb  * mvwinchstr, mvwinchnstr --
     90   1.1  simonb  *      Return an array characters at position (y, x) from the given window.
     91   1.1  simonb  */
     92   1.1  simonb __warn_references(mvwinchstr,
     93  1.11  rillig     "warning: this program uses mvwinchstr(), which is unsafe.")
     94   1.1  simonb int
     95   1.1  simonb mvwinchstr(WINDOW *win, int y, int x, chtype *chstr)
     96   1.1  simonb {
     97   1.7     roy 
     98  1.10   blymn 	if (wmove(win, y, x) == ERR)
     99   1.1  simonb 		return ERR;
    100   1.2   blymn 
    101   1.1  simonb 	return winchstr(win, chstr);
    102   1.1  simonb }
    103   1.1  simonb 
    104   1.1  simonb int
    105   1.1  simonb mvwinchnstr(WINDOW *win, int y, int x, chtype *chstr, int n)
    106   1.1  simonb {
    107   1.7     roy 
    108  1.10   blymn 	if (wmove(win, y, x) == ERR)
    109   1.1  simonb 		return ERR;
    110   1.2   blymn 
    111   1.1  simonb 	return winchnstr(win, chstr, n);
    112   1.1  simonb }
    113   1.1  simonb 
    114   1.1  simonb #endif	/* _CURSES_USE_MACROS */
    115   1.1  simonb 
    116   1.1  simonb /*
    117   1.1  simonb  * winchstr, winchnstr --
    118   1.1  simonb  *	Return an array of characters at cursor position.
    119   1.1  simonb  */
    120   1.1  simonb __warn_references(winchstr,
    121  1.11  rillig     "warning: this program uses winchstr(), which is unsafe.")
    122   1.1  simonb int
    123   1.1  simonb winchstr(WINDOW *win, chtype *chstr)
    124   1.1  simonb {
    125   1.1  simonb 
    126   1.1  simonb 	return winchnstr(win, chstr, -1);
    127   1.1  simonb }
    128   1.1  simonb 
    129   1.1  simonb /*
    130   1.1  simonb  * XXX: This should go in a manpage!
    131   1.1  simonb  * - SUSv2/xcurses doesn't document whether the trailing 0 is included
    132   1.1  simonb  *   in the length count or not.  For safety's sake it _is_ included.
    133   1.1  simonb  */
    134   1.1  simonb int
    135   1.1  simonb winchnstr(WINDOW *win, chtype *chstr, int n)
    136   1.1  simonb {
    137   1.1  simonb 	__LDATA	*end, *start;
    138   1.1  simonb 	int epos;
    139   1.1  simonb 
    140  1.12   blymn 	if (__predict_false(win == NULL))
    141  1.12   blymn 		return ERR;
    142  1.12   blymn 
    143   1.1  simonb 	if (chstr == NULL)
    144   1.1  simonb 		return ERR;
    145   1.1  simonb 
    146   1.3     roy 	start = &win->alines[win->cury]->line[win->curx];
    147   1.1  simonb 	/* (n - 1) to leave room for the trailing 0 element */
    148   1.1  simonb 	if (n < 0 || (n - 1) > win->maxx - win->curx - 1)
    149   1.1  simonb 		epos = win->maxx - 1;
    150   1.1  simonb 	else
    151   1.1  simonb 		/* extra -1 for trailing NUL */
    152   1.1  simonb 		epos = win->curx + n -1 - 1;
    153   1.3     roy 	end = &win->alines[win->cury]->line[epos];
    154   1.1  simonb 
    155   1.1  simonb 	while (start <= end) {
    156   1.4   blymn 		/* or in the attributes but strip out internal flags */
    157   1.6   blymn #ifdef HAVE_WCHAR
    158   1.4   blymn 		*chstr = start->ch | (start->attr & ~__ACS_IS_WACS);
    159   1.6   blymn #else
    160   1.6   blymn 		*chstr = start->ch | start->attr;
    161   1.6   blymn #endif
    162   1.1  simonb 		chstr++;
    163   1.1  simonb 		start++;
    164   1.1  simonb 	}
    165   1.1  simonb 	*chstr = 0;
    166   1.1  simonb 
    167   1.1  simonb 	return OK;
    168   1.1  simonb }
    169