1 /* $OpenBSD: lib_insch.c,v 1.4 2023/10/17 09:52:08 nicm Exp $ */ 2 3 /**************************************************************************** 4 * Copyright 2020 Thomas E. Dickey * 5 * Copyright 1998-2013,2016 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: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 34 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 35 * and: Sven Verdoolaege * 36 * and: Thomas E. Dickey * 37 ****************************************************************************/ 38 39 /* 40 ** lib_insch.c 41 ** 42 ** The routine winsch(). 43 ** 44 */ 45 46 #include <curses.priv.h> 47 #include <ctype.h> 48 49 MODULE_ID("$Id: lib_insch.c,v 1.4 2023/10/17 09:52:08 nicm Exp $") 50 51 /* 52 * Insert the given character, updating the current location to simplify 53 * inserting a string. 54 */ 55 NCURSES_EXPORT(int) 56 _nc_insert_ch(SCREEN *sp, WINDOW *win, chtype ch) 57 { 58 int code = OK; 59 int ch8 = (int) ChCharOf(ch); 60 NCURSES_CH_T wch; 61 int count; 62 int tabsize = ( 63 #if USE_REENTRANT 64 sp->_TABSIZE 65 #else 66 TABSIZE 67 #endif 68 ); 69 70 switch (ch) { 71 case '\t': 72 for (count = (tabsize - (win->_curx % tabsize)); count > 0; count--) { 73 if ((code = _nc_insert_ch(sp, win, ' ')) != OK) 74 break; 75 } 76 break; 77 case '\n': 78 case '\r': 79 case '\b': 80 SetChar2(wch, ch); 81 _nc_waddch_nosync(win, wch); 82 break; 83 default: 84 if ( 85 #if USE_WIDEC_SUPPORT 86 WINDOW_EXT(win, addch_used) == 0 && 87 #endif 88 (isprint(ch8) || 89 (ChAttrOf(ch) & A_ALTCHARSET) || 90 (sp != 0 && sp->_legacy_coding && !iscntrl(ch8)))) { 91 if (win->_curx <= win->_maxx) { 92 struct ldat *line = &(win->_line[win->_cury]); 93 NCURSES_CH_T *end = &(line->text[win->_curx]); 94 NCURSES_CH_T *temp1 = &(line->text[win->_maxx]); 95 NCURSES_CH_T *temp2 = temp1 - 1; 96 97 SetChar2(wch, ch); 98 99 CHANGED_TO_EOL(line, win->_curx, win->_maxx); 100 while (temp1 > end) 101 *temp1-- = *temp2--; 102 103 *temp1 = _nc_render(win, wch); 104 win->_curx++; 105 } 106 } else if (iscntrl(ch8)) { 107 NCURSES_CONST char *s; 108 s = NCURSES_SP_NAME(unctrl) (NCURSES_SP_ARGx (chtype) ch8); 109 while (*s != '\0') { 110 code = _nc_insert_ch(sp, win, ChAttrOf(ch) | UChar(*s)); 111 if (code != OK) 112 break; 113 ++s; 114 } 115 } 116 #if USE_WIDEC_SUPPORT 117 else { 118 /* 119 * Handle multibyte characters here 120 */ 121 SetChar2(wch, ch); 122 wch = _nc_render(win, wch); 123 count = _nc_build_wch(win, &wch); 124 if (count > 0) { 125 code = _nc_insert_wch(win, &wch); 126 } else if (count == -1) { 127 NCURSES_CONST char *s; 128 /* handle EILSEQ */ 129 s = NCURSES_SP_NAME(unctrl) (NCURSES_SP_ARGx (chtype) ch8); 130 if (strlen(s) > 1) { 131 while (*s != '\0') { 132 code = _nc_insert_ch(sp, win, 133 ChAttrOf(ch) | UChar(*s)); 134 if (code != OK) 135 break; 136 ++s; 137 } 138 } else { 139 code = ERR; 140 } 141 } 142 } 143 #endif 144 break; 145 } 146 return code; 147 } 148 149 NCURSES_EXPORT(int) 150 winsch(WINDOW *win, chtype c) 151 { 152 int code = ERR; 153 154 T((T_CALLED("winsch(%p, %s)"), (void *) win, _tracechtype(c))); 155 156 if (win != 0) { 157 NCURSES_SIZE_T oy = win->_cury; 158 NCURSES_SIZE_T ox = win->_curx; 159 160 code = _nc_insert_ch(_nc_screen_of(win), win, c); 161 162 win->_curx = ox; 163 win->_cury = oy; 164 _nc_synchook(win); 165 } 166 returnCode(code); 167 } 168