1 1.9 blymn /* $NetBSD: instr.c,v 1.9 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.1 simonb * 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.1 simonb * ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC. BE 29 1.1 simonb * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 1.1 simonb * 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.1 simonb * 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.9 blymn __RCSID("$NetBSD: instr.c,v 1.9 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 * instr, innstr -- 50 1.1 simonb * Return a string of characters at cursor position from stdscr. 51 1.1 simonb */ 52 1.1 simonb __warn_references(instr, 53 1.8 rillig "warning: this program uses instr(), which is unsafe.") 54 1.1 simonb int 55 1.1 simonb instr(char *str) 56 1.1 simonb { 57 1.1 simonb return winstr(stdscr, str); 58 1.1 simonb } 59 1.1 simonb 60 1.1 simonb int 61 1.1 simonb innstr(char *str, int n) 62 1.1 simonb { 63 1.1 simonb return winnstr(stdscr, str, n); 64 1.1 simonb } 65 1.1 simonb 66 1.1 simonb /* 67 1.1 simonb * mvinstr, mvinnstr -- 68 1.1 simonb * Return a string of characters at position (y, x) from stdscr. 69 1.1 simonb * XXX: should be multi-byte characters for SUSv2. 70 1.1 simonb */ 71 1.1 simonb __warn_references(mvinstr, 72 1.8 rillig "warning: this program uses mvinstr(), which is unsafe.") 73 1.1 simonb int 74 1.1 simonb mvinstr(int y, int x, char *str) 75 1.1 simonb { 76 1.1 simonb return mvwinstr(stdscr, y, x, str); 77 1.1 simonb } 78 1.1 simonb 79 1.1 simonb int 80 1.1 simonb mvinnstr(int y, int x, char *str, int n) 81 1.1 simonb { 82 1.1 simonb return mvwinnstr(stdscr, y, x, str, n); 83 1.1 simonb } 84 1.1 simonb 85 1.1 simonb /* 86 1.1 simonb * mvwinstr, mvwinnstr -- 87 1.1 simonb * Return an array characters at position (y, x) from the given window. 88 1.1 simonb * XXX: should be multi-byte characters for SUSv2. 89 1.1 simonb */ 90 1.1 simonb __warn_references(mvwinstr, 91 1.8 rillig "warning: this program uses mvwinstr(), which is unsafe.") 92 1.1 simonb int 93 1.1 simonb mvwinstr(WINDOW *win, int y, int x, char *str) 94 1.1 simonb { 95 1.7 blymn if (wmove(win, y, x) == ERR) 96 1.1 simonb return ERR; 97 1.2 blymn 98 1.1 simonb return winstr(win, str); 99 1.1 simonb } 100 1.1 simonb 101 1.1 simonb int 102 1.1 simonb mvwinnstr(WINDOW *win, int y, int x, char *str, int n) 103 1.1 simonb { 104 1.7 blymn if (wmove(win, y, x) == ERR) 105 1.1 simonb return ERR; 106 1.2 blymn 107 1.1 simonb return winnstr(win, str, n); 108 1.1 simonb } 109 1.1 simonb 110 1.1 simonb #endif /* _CURSES_USE_MACROS */ 111 1.1 simonb 112 1.1 simonb /* 113 1.1 simonb * winstr, winnstr -- 114 1.1 simonb * Return a string of characters at cursor position. 115 1.1 simonb * XXX: should be multi-byte characters for SUSv2. 116 1.1 simonb */ 117 1.1 simonb __warn_references(winstr, 118 1.8 rillig "warning: this program uses winstr(), which is unsafe.") 119 1.1 simonb int 120 1.1 simonb winstr(WINDOW *win, char *str) 121 1.1 simonb { 122 1.1 simonb 123 1.1 simonb return winnstr(win, str, -1); 124 1.1 simonb } 125 1.1 simonb 126 1.1 simonb /* 127 1.1 simonb * XXX: This should go in a manpage! 128 1.1 simonb * - winnstr() returns the number of characters copied only of if it is 129 1.1 simonb * called with n >= 0 (ie, as inchnstr(), mvinchnstr(), mvwinchnstr() 130 1.1 simonb * or winchnstr()). If N < 0, it returns `OK'. 131 1.1 simonb * - SUSv2/xcurses doesn't document whether the trailing NUL is included 132 1.1 simonb * in the length count or not. For safety's sake it _is_ included. 133 1.1 simonb * - This implementation does not (yet) support multi-byte characters 134 1.1 simonb * strings. 135 1.1 simonb */ 136 1.1 simonb int 137 1.1 simonb winnstr(WINDOW *win, char *str, int n) 138 1.1 simonb { 139 1.1 simonb __LDATA *end, *start; 140 1.4 blymn int epos, sn; 141 1.1 simonb 142 1.9 blymn if ((win == NULL) || (str == NULL)) 143 1.1 simonb return ERR; 144 1.1 simonb 145 1.4 blymn sn = n; 146 1.3 roy start = &win->alines[win->cury]->line[win->curx]; 147 1.1 simonb /* (n - 1) to leave room for the trailing NUL */ 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 n = win->maxx - win->curx; 151 1.1 simonb } else { 152 1.1 simonb /* extra -1 for trailing NUL */ 153 1.1 simonb epos = win->curx + n - 1 - 1; 154 1.1 simonb n--; 155 1.1 simonb } 156 1.3 roy end = &win->alines[win->cury]->line[epos]; 157 1.1 simonb 158 1.1 simonb while (start <= end) { 159 1.1 simonb *str = start->ch & __CHARTEXT; 160 1.1 simonb str++; 161 1.1 simonb start++; 162 1.1 simonb } 163 1.1 simonb *str = '\0'; 164 1.1 simonb 165 1.4 blymn if (sn < 0) 166 1.1 simonb return OK; 167 1.1 simonb else 168 1.1 simonb return n; 169 1.1 simonb } 170