Home | History | Annotate | Line # | Download | only in libcurses
in_wchstr.c revision 1.4.14.1
      1  1.4.14.1  christos /*   $NetBSD: in_wchstr.c,v 1.4.14.1 2019/06/10 22:05:22 christos 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.4.14.1  christos __RCSID("$NetBSD: in_wchstr.c,v 1.4.14.1 2019/06/10 22:05:22 christos Exp $");
     40       1.1     blymn #endif						  /* not lint */
     41       1.1     blymn 
     42       1.1     blymn #include "curses.h"
     43       1.1     blymn #include "curses_private.h"
     44       1.1     blymn 
     45       1.1     blymn /*
     46       1.1     blymn  * in_wchstr, in_wchnstr --
     47       1.1     blymn  *	Return an array of wide characters at cursor position from stdscr.
     48       1.1     blymn  */
     49       1.1     blymn __warn_references(in_wchstr,
     50  1.4.14.1  christos 	"warning: this program uses in_wchstr(), which is unsafe.");
     51       1.1     blymn int
     52       1.1     blymn in_wchstr(cchar_t *wchstr)
     53       1.1     blymn {
     54       1.1     blymn 	return win_wchstr(stdscr, wchstr);
     55       1.1     blymn }
     56       1.1     blymn 
     57       1.1     blymn int
     58       1.1     blymn in_wchnstr(cchar_t *wchstr, int n)
     59       1.1     blymn {
     60       1.1     blymn 	return win_wchnstr(stdscr, wchstr, n);
     61       1.1     blymn }
     62       1.1     blymn 
     63       1.1     blymn /*
     64       1.1     blymn  * mvin_wchstr, mv_winchnstr --
     65       1.1     blymn  *  Return an array of wide characters at position (y, x) from stdscr.
     66       1.1     blymn  */
     67       1.1     blymn __warn_references(mvin_wchstr,
     68  1.4.14.1  christos 	"warning: this program uses mvin_wchstr(), which is unsafe.");
     69       1.1     blymn int
     70       1.1     blymn mvin_wchstr(int y, int x, cchar_t *wchstr)
     71       1.1     blymn {
     72       1.1     blymn 	return mvwin_wchstr(stdscr, y, x, wchstr);
     73       1.1     blymn }
     74       1.1     blymn 
     75       1.1     blymn int
     76       1.1     blymn mvin_wchnstr(int y, int x, cchar_t *wchstr, int n)
     77       1.1     blymn {
     78       1.1     blymn 	return mvwin_wchnstr(stdscr, y, x, wchstr, n);
     79       1.1     blymn }
     80       1.1     blymn 
     81       1.1     blymn /*
     82       1.1     blymn  * mvwin_wchstr, mvwin_wchnstr --
     83       1.1     blymn  *  Return an array wide characters at position (y, x) from the given window.
     84       1.1     blymn  */
     85       1.1     blymn __warn_references(mvwin_wchstr,
     86  1.4.14.1  christos 	"warning: this program uses mvwin_wchstr(), which is unsafe.");
     87       1.1     blymn int
     88       1.1     blymn mvwin_wchstr(WINDOW *win, int y, int x, cchar_t *wchstr)
     89       1.1     blymn {
     90       1.1     blymn 	if (wmove(win, y, x) == ERR)
     91       1.1     blymn 		return ERR;
     92       1.1     blymn 
     93       1.1     blymn 	return win_wchstr(win, wchstr);
     94       1.1     blymn }
     95       1.1     blymn 
     96       1.1     blymn int
     97       1.1     blymn mvwin_wchnstr(WINDOW *win, int y, int x, cchar_t *wchstr, int n)
     98       1.1     blymn {
     99       1.1     blymn 	if (wmove(win, y, x) == ERR)
    100       1.1     blymn 		return ERR;
    101       1.1     blymn 
    102       1.1     blymn 	return win_wchnstr(win, wchstr, n);
    103       1.1     blymn }
    104       1.1     blymn 
    105       1.1     blymn /*
    106       1.1     blymn  * win_wchstr, win_wchnstr --
    107       1.1     blymn  *	Return an array of characters at cursor position.
    108       1.1     blymn  */
    109       1.1     blymn __warn_references(win_wchstr,
    110  1.4.14.1  christos 	"warning: this program uses win_wchstr(), which is unsafe.");
    111       1.1     blymn int
    112       1.1     blymn win_wchstr(WINDOW *win, cchar_t *wchstr)
    113       1.1     blymn {
    114       1.1     blymn 
    115       1.1     blymn 	return win_wchnstr(win, wchstr, -1);
    116       1.1     blymn }
    117       1.1     blymn 
    118       1.1     blymn /*
    119       1.1     blymn  * - SUSv2/xcurses doesn't document whether the trailing 0 is included
    120       1.1     blymn  *   in the length count or not.  For safety's sake it _is_ included.
    121       1.1     blymn  */
    122       1.1     blymn int
    123       1.1     blymn win_wchnstr(WINDOW *win, cchar_t *wchstr, int n)
    124       1.1     blymn {
    125       1.1     blymn 	__LDATA	*start;
    126       1.1     blymn 	int x = 0, cw = 0, cnt = 0;
    127       1.1     blymn 	cchar_t *wcp;
    128       1.1     blymn 	nschar_t *np;
    129       1.1     blymn 
    130       1.1     blymn 	if (wchstr == NULL)
    131       1.1     blymn 		return ERR;
    132       1.1     blymn 
    133       1.3       roy 	start = &win->alines[win->cury]->line[win->curx];
    134       1.1     blymn 	x = win->curx;
    135       1.1     blymn 	cw = WCOL(*start);
    136       1.1     blymn 	if (cw < 0) {
    137       1.1     blymn 		start += cw;
    138       1.1     blymn 		x += cw;
    139       1.1     blymn 	}
    140       1.1     blymn 	wcp = wchstr;
    141       1.1     blymn 	/* (n - 1) to leave room for the trailing 0 element */
    142       1.1     blymn 	while ((x < win->maxx) && ((n < 0) || ((n > 1) && (cnt < n - 1)))) {
    143       1.4       roy 		cw = WCOL(*start);
    144       1.4       roy 		wcp->vals[0] = start->ch;
    145       1.1     blymn 		wcp->attributes = start->attr;
    146       1.1     blymn 		wcp->elements = 1;
    147       1.1     blymn 		np = start->nsp;
    148       1.1     blymn 		if (np) {
    149       1.1     blymn 			do {
    150       1.4       roy 				wcp->vals[wcp->elements++] = np->ch;
    151       1.1     blymn 				np = np->next;
    152       1.4       roy 			} while (np);
    153       1.1     blymn 		}
    154       1.1     blymn 		wcp++;
    155       1.1     blymn 		cnt++;
    156       1.1     blymn 		x += cw;
    157       1.4       roy 		if (x < win->maxx)
    158       1.1     blymn 			start += cw;
    159       1.1     blymn 	}
    160       1.4       roy 	wcp->vals[0] = L'\0';
    161       1.1     blymn 	wcp->elements = 1;
    162       1.1     blymn 	wcp->attributes = win->wattr;
    163       1.1     blymn 
    164       1.1     blymn 	return OK;
    165       1.1     blymn }
    166