1 /* $OpenBSD: lib_ins_wch.c,v 1.2 2023/10/17 09:52:09 nicm Exp $ */ 2 3 /**************************************************************************** 4 * Copyright 2019,2020 Thomas E. Dickey * 5 * Copyright 2002-2016,2017 Free Software Foundation, Inc. * 6 * * 7 * Permission is hereby granted, free of charge, to any person obtaining a * 8 * copy of this software and associated documentation files (the * 9 * "Software"), to deal in the Software without restriction, including * 10 * without limitation the rights to use, copy, modify, merge, publish, * 11 * distribute, distribute with modifications, sublicense, and/or sell * 12 * copies of the Software, and to permit persons to whom the Software is * 13 * furnished to do so, subject to the following conditions: * 14 * * 15 * The above copyright notice and this permission notice shall be included * 16 * in all copies or substantial portions of the Software. * 17 * * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 21 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 24 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 25 * * 26 * Except as contained in this notice, the name(s) of the above copyright * 27 * holders shall not be used in advertising or otherwise to promote the * 28 * sale, use or other dealings in this Software without prior written * 29 * authorization. * 30 ****************************************************************************/ 31 32 /**************************************************************************** 33 * Author: Thomas Dickey 2002 * 34 ****************************************************************************/ 35 36 /* 37 ** lib_ins_wch.c 38 ** 39 ** The routine wins_wch(). 40 ** 41 */ 42 43 #include <curses.priv.h> 44 45 MODULE_ID("$Id: lib_ins_wch.c,v 1.2 2023/10/17 09:52:09 nicm Exp $") 46 47 /* 48 * Insert the given character, updating the current location to simplify 49 * inserting a string. 50 */ 51 NCURSES_EXPORT(int) 52 _nc_insert_wch(WINDOW *win, const cchar_t *wch) 53 { 54 int cells = _nc_wacs_width(CharOf(CHDEREF(wch))); 55 int code = OK; 56 57 if (cells < 0) { 58 code = winsch(win, (chtype) CharOf(CHDEREF(wch))); 59 } else { 60 if (cells == 0) 61 cells = 1; 62 63 if (win->_curx <= win->_maxx) { 64 int cell; 65 struct ldat *line = &(win->_line[win->_cury]); 66 NCURSES_CH_T *end = &(line->text[win->_curx]); 67 NCURSES_CH_T *temp1 = &(line->text[win->_maxx]); 68 NCURSES_CH_T *temp2 = temp1 - cells; 69 70 CHANGED_TO_EOL(line, win->_curx, win->_maxx); 71 while (temp1 > end) 72 *temp1-- = *temp2--; 73 74 *temp1 = _nc_render(win, *wch); 75 for (cell = 1; cell < cells; ++cell) { 76 SetWidecExt(temp1[cell], cell); 77 } 78 79 win->_curx = (NCURSES_SIZE_T) (win->_curx + cells); 80 } 81 } 82 return code; 83 } 84 85 NCURSES_EXPORT(int) 86 wins_wch(WINDOW *win, const cchar_t *wch) 87 { 88 int code = ERR; 89 90 T((T_CALLED("wins_wch(%p, %s)"), (void *) win, _tracecchar_t(wch))); 91 92 if (win != 0) { 93 NCURSES_SIZE_T oy = win->_cury; 94 NCURSES_SIZE_T ox = win->_curx; 95 96 code = _nc_insert_wch(win, wch); 97 98 win->_curx = ox; 99 win->_cury = oy; 100 _nc_synchook(win); 101 } 102 returnCode(code); 103 } 104 105 NCURSES_EXPORT(int) 106 wins_nwstr(WINDOW *win, const wchar_t *wstr, int n) 107 { 108 int code = ERR; 109 110 T((T_CALLED("wins_nwstr(%p,%s,%d)"), 111 (void *) win, _nc_viswbufn(wstr, n), n)); 112 113 if (win != 0 114 && wstr != 0) { 115 if (n < 1) 116 n = INT_MAX; 117 code = OK; 118 119 if (n > 0) { 120 const wchar_t *cp; 121 SCREEN *sp = _nc_screen_of(win); 122 NCURSES_SIZE_T oy = win->_cury; 123 NCURSES_SIZE_T ox = win->_curx; 124 125 for (cp = wstr; (*cp != L'\0') && ((cp - wstr) < n); cp++) { 126 int len = _nc_wacs_width(*cp); 127 128 if ((len >= 0 && len != 1) || !is7bits(*cp)) { 129 cchar_t tmp_cchar; 130 wchar_t tmp_wchar = *cp; 131 memset(&tmp_cchar, 0, sizeof(tmp_cchar)); 132 (void) setcchar(&tmp_cchar, 133 &tmp_wchar, 134 WA_NORMAL, 135 (short) 0, 136 (void *) 0); 137 code = _nc_insert_wch(win, &tmp_cchar); 138 } else { 139 /* tabs, other ASCII stuff */ 140 code = _nc_insert_ch(sp, win, (chtype) (*cp)); 141 } 142 if (code != OK) 143 break; 144 } 145 146 win->_curx = ox; 147 win->_cury = oy; 148 _nc_synchook(win); 149 } 150 } 151 returnCode(code); 152 } 153