1 1.10 blymn /* $NetBSD: addchnstr.c,v 1.10 2024/12/23 02:58:03 blymn Exp $ */ 2 1.1 jdc 3 1.1 jdc /* 4 1.1 jdc * Copyright (c) 2003 The NetBSD Foundation, Inc. 5 1.1 jdc * All rights reserved. 6 1.1 jdc * 7 1.1 jdc * This code is derived from software contributed to The NetBSD Foundation 8 1.1 jdc * by Douwe Kiela (virtus (at) wanadoo.nl). 9 1.1 jdc * 10 1.1 jdc * Redistribution and use in source and binary forms, with or without 11 1.1 jdc * modification, are permitted provided that the following conditions 12 1.1 jdc * are met: 13 1.1 jdc * 1. Redistributions of source code must retain the above copyright 14 1.1 jdc * notice, this list of conditions and the following disclaimer. 15 1.1 jdc * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 jdc * notice, this list of conditions and the following disclaimer in the 17 1.1 jdc * documentation and/or other materials provided with the distribution. 18 1.1 jdc * 19 1.1 jdc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 jdc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 jdc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 jdc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 jdc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 jdc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 jdc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 jdc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 jdc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 jdc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 jdc * POSSIBILITY OF SUCH DAMAGE. 30 1.1 jdc */ 31 1.1 jdc 32 1.1 jdc #include <sys/cdefs.h> 33 1.1 jdc #ifndef lint 34 1.10 blymn __RCSID("$NetBSD: addchnstr.c,v 1.10 2024/12/23 02:58:03 blymn Exp $"); 35 1.1 jdc #endif /* not lint */ 36 1.1 jdc 37 1.1 jdc #include <stdlib.h> 38 1.1 jdc 39 1.1 jdc #include "curses.h" 40 1.1 jdc #include "curses_private.h" 41 1.1 jdc 42 1.1 jdc #ifndef _CURSES_USE_MACROS 43 1.1 jdc 44 1.1 jdc /* 45 1.1 jdc * addchstr -- 46 1.1 jdc * Add a string to stdscr starting at (_cury, _curx). 47 1.1 jdc */ 48 1.1 jdc int 49 1.1 jdc addchstr(const chtype *chstr) 50 1.1 jdc { 51 1.1 jdc return waddchnstr(stdscr, chstr, -1); 52 1.1 jdc } 53 1.1 jdc 54 1.1 jdc /* 55 1.1 jdc * waddchstr -- 56 1.1 jdc * Add a string to the given window starting at (_cury, _curx). 57 1.1 jdc */ 58 1.1 jdc int 59 1.1 jdc waddchstr(WINDOW *win, const chtype *chstr) 60 1.1 jdc { 61 1.1 jdc return waddchnstr(win, chstr, -1); 62 1.1 jdc } 63 1.1 jdc 64 1.1 jdc /* 65 1.1 jdc * addchnstr -- 66 1.1 jdc * Add a string (at most n characters) to stdscr starting 67 1.1 jdc * at (_cury, _curx). If n is negative, add the entire string. 68 1.1 jdc */ 69 1.1 jdc int 70 1.1 jdc addchnstr(const chtype *chstr, int n) 71 1.1 jdc { 72 1.1 jdc return waddchnstr(stdscr, chstr, n); 73 1.1 jdc } 74 1.1 jdc 75 1.1 jdc /* 76 1.1 jdc * mvaddchstr -- 77 1.1 jdc * Add a string to stdscr starting at (y, x) 78 1.1 jdc */ 79 1.1 jdc int 80 1.1 jdc mvaddchstr(int y, int x, const chtype *chstr) 81 1.1 jdc { 82 1.1 jdc return mvwaddchnstr(stdscr, y, x, chstr, -1); 83 1.1 jdc } 84 1.1 jdc 85 1.1 jdc /* 86 1.1 jdc * mvwaddchstr -- 87 1.1 jdc * Add a string to the given window starting at (y, x) 88 1.1 jdc */ 89 1.1 jdc int 90 1.1 jdc mvwaddchstr(WINDOW *win, int y, int x, const chtype *chstr) 91 1.1 jdc { 92 1.1 jdc return mvwaddchnstr(win, y, x, chstr, -1); 93 1.1 jdc } 94 1.1 jdc 95 1.1 jdc /* 96 1.1 jdc * mvaddchnstr -- 97 1.1 jdc * Add a string of at most n characters to stdscr 98 1.1 jdc * starting at (y, x). 99 1.1 jdc */ 100 1.1 jdc int 101 1.1 jdc mvaddchnstr(int y, int x, const chtype *chstr, int n) 102 1.1 jdc { 103 1.1 jdc return mvwaddchnstr(stdscr, y, x, chstr, n); 104 1.1 jdc } 105 1.1 jdc 106 1.1 jdc /* 107 1.1 jdc * mvwaddchnstr -- 108 1.1 jdc * Add a string of at most n characters to the given window 109 1.1 jdc * starting at (y, x). 110 1.1 jdc */ 111 1.1 jdc int 112 1.1 jdc mvwaddchnstr(WINDOW *win, int y, int x, const chtype *chstr, int n) 113 1.1 jdc { 114 1.8 blymn if (wmove(win, y, x) == ERR) 115 1.1 jdc return ERR; 116 1.1 jdc 117 1.1 jdc return waddchnstr(win, chstr, n); 118 1.1 jdc } 119 1.1 jdc 120 1.1 jdc #endif 121 1.1 jdc 122 1.1 jdc /* 123 1.1 jdc * waddchnstr -- 124 1.1 jdc * Add a string (at most n characters) to the given window 125 1.5 blymn * starting at (_cury, _curx) until the end of line is reached or 126 1.5 blymn * n characters have been added. If n is negative, add as much 127 1.5 blymn * of the string that will fit on the current line. SUSv2 says 128 1.5 blymn * that the addchnstr family does not wrap and strings are truncated 129 1.5 blymn * to the RHS of the window. 130 1.1 jdc */ 131 1.1 jdc int 132 1.1 jdc waddchnstr(WINDOW *win, const chtype *chstr, int n) 133 1.1 jdc { 134 1.2 wiz size_t len; 135 1.1 jdc const chtype *chp; 136 1.1 jdc attr_t attr; 137 1.1 jdc char *ocp, *cp, *start; 138 1.6 blymn int i, ret, ox, oy; 139 1.1 jdc 140 1.3 jdc __CTRACE(__CTRACE_INPUT, "waddchnstr: win = %p, chstr = %p, n = %d\n", 141 1.3 jdc win, chstr, n); 142 1.1 jdc 143 1.10 blymn if (__predict_false(win == NULL)) 144 1.10 blymn return ERR; 145 1.10 blymn 146 1.1 jdc if (n >= 0) 147 1.1 jdc for (chp = chstr, len = 0; n-- && *chp++; ++len); 148 1.1 jdc else 149 1.1 jdc for (chp = chstr, len = 0; *chp++; ++len); 150 1.1 jdc 151 1.5 blymn /* check if string is too long for current location */ 152 1.5 blymn if (len > (win->maxx - win->curx)) 153 1.5 blymn len = win->maxx - win->curx; 154 1.5 blymn 155 1.1 jdc if ((ocp = malloc(len + 1)) == NULL) 156 1.1 jdc return ERR; 157 1.1 jdc chp = chstr; 158 1.1 jdc cp = ocp; 159 1.1 jdc start = ocp; 160 1.1 jdc i = 0; 161 1.1 jdc attr = (*chp) & __ATTRIBUTES; 162 1.6 blymn ox = win->curx; 163 1.6 blymn oy = win->cury; 164 1.1 jdc while (len) { 165 1.1 jdc *cp = (*chp) & __CHARTEXT; 166 1.1 jdc cp++; 167 1.1 jdc chp++; 168 1.1 jdc i++; 169 1.1 jdc len--; 170 1.1 jdc if (((*chp) & __ATTRIBUTES) != attr) { 171 1.1 jdc *cp = '\0'; 172 1.6 blymn if (_cursesi_waddbytes(win, start, i, attr, 0) == ERR) { 173 1.1 jdc free(ocp); 174 1.1 jdc return ERR; 175 1.1 jdc } 176 1.1 jdc attr = (*chp) & __ATTRIBUTES; 177 1.1 jdc start = cp; 178 1.1 jdc i = 0; 179 1.1 jdc } 180 1.1 jdc } 181 1.1 jdc *cp = '\0'; 182 1.6 blymn ret = _cursesi_waddbytes(win, start, i, attr, 0); 183 1.1 jdc free(ocp); 184 1.8 blymn wmove(win, oy, ox); 185 1.1 jdc return ret; 186 1.1 jdc } 187