Home | History | Annotate | Line # | Download | only in libcurses
inwstr.c revision 1.3.28.1
      1  1.3.28.1  pgoyette /*   $NetBSD: inwstr.c,v 1.3.28.1 2017/01/07 08:56:04 pgoyette 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.3.28.1  pgoyette __RCSID("$NetBSD: inwstr.c,v 1.3.28.1 2017/01/07 08:56:04 pgoyette 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  * inwstr, innwstr --
     47       1.1     blymn  *	Return a string of wide characters at cursor position from stdscr.
     48       1.1     blymn  */
     49       1.1     blymn __warn_references(inwstr,
     50       1.1     blymn 	"warning: this program uses inwstr(), which is unsafe.")
     51       1.1     blymn int
     52       1.1     blymn inwstr(wchar_t *wstr)
     53       1.1     blymn {
     54       1.1     blymn #ifndef HAVE_WCHAR
     55       1.1     blymn 	return ERR;
     56       1.1     blymn #else
     57       1.1     blymn 	return winwstr(stdscr, wstr);
     58       1.1     blymn #endif /* HAVE_WCHAR */
     59       1.1     blymn }
     60       1.1     blymn 
     61       1.1     blymn int
     62       1.1     blymn innwstr(wchar_t *wstr, int n)
     63       1.1     blymn {
     64       1.1     blymn #ifndef HAVE_WCHAR
     65       1.1     blymn 	return ERR;
     66       1.1     blymn #else
     67       1.1     blymn 	return winnwstr(stdscr, wstr, n);
     68       1.1     blymn #endif /* HAVE_WCHAR */
     69       1.1     blymn }
     70       1.1     blymn 
     71       1.1     blymn /*
     72       1.1     blymn  * mvinwstr, mvinnwstr --
     73       1.1     blymn  *  Return a string of wide characters at position (y, x) from stdscr.
     74       1.1     blymn  */
     75       1.1     blymn __warn_references(mvinwstr,
     76       1.1     blymn 	"warning: this program uses mvinwstr(), which is unsafe.")
     77       1.1     blymn int
     78       1.1     blymn mvinwstr(int y, int x, wchar_t *wstr)
     79       1.1     blymn {
     80       1.1     blymn #ifndef HAVE_WCHAR
     81       1.1     blymn 	return ERR;
     82       1.1     blymn #else
     83       1.1     blymn 	return mvwinwstr(stdscr, y, x, wstr);
     84       1.1     blymn #endif /* HAVE_WCHAR */
     85       1.1     blymn }
     86       1.1     blymn 
     87       1.1     blymn int
     88       1.1     blymn mvinnwstr(int y, int x, wchar_t *wstr, int n)
     89       1.1     blymn {
     90       1.1     blymn #ifndef HAVE_WCHAR
     91       1.1     blymn 	return ERR;
     92       1.1     blymn #else
     93       1.1     blymn 	return mvwinnwstr(stdscr, y, x, wstr, n);
     94       1.1     blymn #endif /* HAVE_WCHAR */
     95       1.1     blymn }
     96       1.1     blymn 
     97       1.1     blymn /*
     98       1.1     blymn  * mvwinwstr, mvwinnwstr --
     99       1.1     blymn  *  Return an array wide characters at position (y, x) from the given window.
    100       1.1     blymn  */
    101       1.1     blymn __warn_references(mvwinwstr,
    102       1.1     blymn 	"warning: this program uses mvwinwstr(), which is unsafe.")
    103       1.1     blymn int
    104       1.1     blymn mvwinwstr(WINDOW *win, int y, int x, wchar_t *wstr)
    105       1.1     blymn {
    106       1.1     blymn #ifndef HAVE_WCHAR
    107       1.1     blymn 	return ERR;
    108       1.1     blymn #else
    109       1.1     blymn 	if (wmove(win, y, x) == ERR)
    110       1.1     blymn 		return ERR;
    111       1.1     blymn 
    112       1.1     blymn 	return winwstr(win, wstr);
    113       1.1     blymn #endif /* HAVE_WCHAR */
    114       1.1     blymn }
    115       1.1     blymn 
    116       1.1     blymn int
    117       1.1     blymn mvwinnwstr(WINDOW *win, int y, int x, wchar_t *wstr, int n)
    118       1.1     blymn {
    119       1.1     blymn #ifndef HAVE_WCHAR
    120       1.1     blymn 	return ERR;
    121       1.1     blymn #else
    122       1.1     blymn 	if (wmove(win, y, x) == ERR)
    123       1.1     blymn 		return ERR;
    124       1.1     blymn 
    125       1.1     blymn 	return winnwstr(win, wstr, n);
    126       1.1     blymn #endif /* HAVE_WCHAR */
    127       1.1     blymn }
    128       1.1     blymn 
    129       1.1     blymn /*
    130       1.1     blymn  * winwstr, winnwstr --
    131       1.1     blymn  *	Return a string of wide characters at cursor position.
    132       1.1     blymn  */
    133       1.1     blymn __warn_references(winwstr,
    134       1.1     blymn 	"warning: this program uses winwstr(), which is unsafe.")
    135       1.1     blymn int
    136       1.1     blymn winwstr(WINDOW *win, wchar_t *wstr)
    137       1.1     blymn {
    138       1.1     blymn #ifndef HAVE_WCHAR
    139       1.1     blymn 	return ERR;
    140       1.1     blymn #else
    141       1.1     blymn 
    142       1.1     blymn 	return winnwstr(win, wstr, -1);
    143       1.1     blymn #endif /* HAVE_WCHAR */
    144       1.1     blymn }
    145       1.1     blymn 
    146       1.1     blymn /*
    147       1.1     blymn  * - winnwstr() returns the number of characters copied only of if it is
    148       1.1     blymn  *   called with n >= 0 (ie, as in_wchnstr(), mvin_wchnstr(), mvwin_wchnstr()
    149       1.1     blymn  *   or win_wchnstr()).  If N < 0, it returns `OK'.
    150       1.1     blymn  * - SUSv2/xcurses doesn't document whether the trailing NUL is included
    151       1.1     blymn  *   in the length count or not.  For safety's sake it _is_ included.
    152       1.1     blymn  * - This implementation does not (yet) support multi-byte characters
    153       1.1     blymn  *   strings.
    154       1.1     blymn  */
    155       1.1     blymn int
    156       1.1     blymn winnwstr(WINDOW *win, wchar_t *wstr, int n)
    157       1.1     blymn {
    158       1.1     blymn #ifndef HAVE_WCHAR
    159       1.1     blymn 	return ERR;
    160       1.1     blymn #else
    161       1.1     blymn 	__LDATA	*start;
    162       1.1     blymn 	int x, cw, cnt;
    163       1.1     blymn 	wchar_t *wcp;
    164       1.1     blymn 
    165       1.1     blymn 	if (wstr == NULL)
    166       1.1     blymn 		return ERR;
    167       1.1     blymn 
    168       1.3       roy 	start = &win->alines[win->cury]->line[win->curx];
    169       1.1     blymn 	x = win->curx;
    170  1.3.28.1  pgoyette 	cw = WCOL(*start);
    171       1.1     blymn 	if (cw < 0) {
    172       1.1     blymn 		start += cw;
    173       1.1     blymn 		x += cw;
    174       1.1     blymn 	}
    175       1.1     blymn     cnt = 0;
    176       1.1     blymn 	wcp = wstr;
    177       1.1     blymn 	/* (n - 1) to leave room for the trailing 0 element */
    178       1.1     blymn 	while ((x < win->maxx) && ((n < 0) || ((n > 1) && (cnt < n - 1)))) {
    179  1.3.28.1  pgoyette 		cw = WCOL(*start);
    180       1.1     blymn 		*wcp = start->ch;
    181       1.1     blymn 		wcp++;
    182       1.1     blymn 		cnt++;
    183       1.1     blymn 		x += cw;
    184  1.3.28.1  pgoyette 		if (x < win->maxx)
    185       1.1     blymn 			start += cw;
    186       1.1     blymn 	}
    187       1.1     blymn 	*wcp = L'\0';
    188       1.1     blymn 
    189       1.1     blymn 	if (n < 0)
    190       1.1     blymn 		return OK;
    191       1.1     blymn 	else
    192       1.1     blymn 		return cnt;
    193       1.1     blymn #endif /* HAVE_WCHAR */
    194       1.1     blymn }
    195