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