xref: /openbsd/lib/libcurses/base/lib_insnstr.c (revision c7ef0cfc)
1*c7ef0cfcSnicm /* $OpenBSD: lib_insnstr.c,v 1.2 2023/10/17 09:52:08 nicm Exp $ */
281d8c4e1Snicm 
381d8c4e1Snicm /****************************************************************************
4*c7ef0cfcSnicm  * Copyright 2018-2020,2022 Thomas E. Dickey                                *
5*c7ef0cfcSnicm  * Copyright 2004-2009,2016 Free Software Foundation, Inc.                  *
681d8c4e1Snicm  *                                                                          *
781d8c4e1Snicm  * Permission is hereby granted, free of charge, to any person obtaining a  *
881d8c4e1Snicm  * copy of this software and associated documentation files (the            *
981d8c4e1Snicm  * "Software"), to deal in the Software without restriction, including      *
1081d8c4e1Snicm  * without limitation the rights to use, copy, modify, merge, publish,      *
1181d8c4e1Snicm  * distribute, distribute with modifications, sublicense, and/or sell       *
1281d8c4e1Snicm  * copies of the Software, and to permit persons to whom the Software is    *
1381d8c4e1Snicm  * furnished to do so, subject to the following conditions:                 *
1481d8c4e1Snicm  *                                                                          *
1581d8c4e1Snicm  * The above copyright notice and this permission notice shall be included  *
1681d8c4e1Snicm  * in all copies or substantial portions of the Software.                   *
1781d8c4e1Snicm  *                                                                          *
1881d8c4e1Snicm  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1981d8c4e1Snicm  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
2081d8c4e1Snicm  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
2181d8c4e1Snicm  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
2281d8c4e1Snicm  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2381d8c4e1Snicm  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2481d8c4e1Snicm  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2581d8c4e1Snicm  *                                                                          *
2681d8c4e1Snicm  * Except as contained in this notice, the name(s) of the above copyright   *
2781d8c4e1Snicm  * holders shall not be used in advertising or otherwise to promote the     *
2881d8c4e1Snicm  * sale, use or other dealings in this Software without prior written       *
2981d8c4e1Snicm  * authorization.                                                           *
3081d8c4e1Snicm  ****************************************************************************/
3181d8c4e1Snicm 
3281d8c4e1Snicm /****************************************************************************
3381d8c4e1Snicm  *  Author: Thomas E. Dickey                                                *
3481d8c4e1Snicm  ****************************************************************************/
3581d8c4e1Snicm 
3681d8c4e1Snicm /*
3781d8c4e1Snicm **	lib_insnstr.c
3881d8c4e1Snicm **
3981d8c4e1Snicm **	The routine winsnstr().
4081d8c4e1Snicm **
4181d8c4e1Snicm */
4281d8c4e1Snicm 
4381d8c4e1Snicm #include <curses.priv.h>
4481d8c4e1Snicm #include <ctype.h>
4581d8c4e1Snicm 
46*c7ef0cfcSnicm MODULE_ID("$Id: lib_insnstr.c,v 1.2 2023/10/17 09:52:08 nicm Exp $")
4781d8c4e1Snicm 
NCURSES_EXPORT(int)4881d8c4e1Snicm NCURSES_EXPORT(int)
4981d8c4e1Snicm winsnstr(WINDOW *win, const char *s, int n)
5081d8c4e1Snicm {
5181d8c4e1Snicm     int code = ERR;
5281d8c4e1Snicm     const unsigned char *str = (const unsigned char *) s;
5381d8c4e1Snicm 
54*c7ef0cfcSnicm     T((T_CALLED("winsnstr(%p,%s,%d)"), (void *) win, _nc_visbufn(s, n), n));
5581d8c4e1Snicm 
5681d8c4e1Snicm     if (win != 0 && str != 0) {
57*c7ef0cfcSnicm 	SCREEN *sp = _nc_screen_of(win);
58*c7ef0cfcSnicm #if USE_WIDEC_SUPPORT
59*c7ef0cfcSnicm 	/*
60*c7ef0cfcSnicm 	 * If the output contains "wide" (multibyte) characters, we will not
61*c7ef0cfcSnicm 	 * really know the width of a character until we get the last byte
62*c7ef0cfcSnicm 	 * of the character.  Since the preceding byte(s) may use more columns
63*c7ef0cfcSnicm 	 * on the screen than the final character, it is best to route the
64*c7ef0cfcSnicm 	 * call to the wins_nwstr() function.
65*c7ef0cfcSnicm 	 */
66*c7ef0cfcSnicm 	if (sp->_screen_unicode) {
67*c7ef0cfcSnicm 	    size_t nn = (n > 0) ? (size_t) n : strlen(s);
68*c7ef0cfcSnicm 	    wchar_t *buffer = typeMalloc(wchar_t, nn + 1);
69*c7ef0cfcSnicm 	    if (buffer != 0) {
70*c7ef0cfcSnicm 		mbstate_t state;
71*c7ef0cfcSnicm 		size_t n3;
72*c7ef0cfcSnicm 		init_mb(state);
73*c7ef0cfcSnicm 		n3 = mbstowcs(buffer, s, nn);
74*c7ef0cfcSnicm 		if (n3 != (size_t) (-1)) {
75*c7ef0cfcSnicm 		    buffer[n3] = '\0';
76*c7ef0cfcSnicm 		    code = wins_nwstr(win, buffer, (int) n3);
77*c7ef0cfcSnicm 		}
78*c7ef0cfcSnicm 		free(buffer);
79*c7ef0cfcSnicm 	    }
80*c7ef0cfcSnicm 	}
81*c7ef0cfcSnicm 	if (code == ERR)
82*c7ef0cfcSnicm #endif
83*c7ef0cfcSnicm 	{
84*c7ef0cfcSnicm 	    NCURSES_SIZE_T oy = win->_cury;
85*c7ef0cfcSnicm 	    NCURSES_SIZE_T ox = win->_curx;
86*c7ef0cfcSnicm 	    const unsigned char *cp;
87*c7ef0cfcSnicm 
88*c7ef0cfcSnicm 	    for (cp = str; (n <= 0 || (cp - str) < n) && *cp; cp++) {
89*c7ef0cfcSnicm 		_nc_insert_ch(sp, win, (chtype) UChar(*cp));
9081d8c4e1Snicm 	    }
9181d8c4e1Snicm 	    win->_curx = ox;
9281d8c4e1Snicm 	    win->_cury = oy;
9381d8c4e1Snicm 	    _nc_synchook(win);
9481d8c4e1Snicm 	    code = OK;
9581d8c4e1Snicm 	}
96*c7ef0cfcSnicm     }
9781d8c4e1Snicm     returnCode(code);
9881d8c4e1Snicm }
99