xref: /openbsd-src/lib/libcurses/base/lib_addstr.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: lib_addstr.c,v 1.4 2001/01/22 18:01:37 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34  ****************************************************************************/
35 
36 /*
37 **	lib_addstr.c
38 *
39 **	The routines waddnstr(), waddchnstr().
40 **
41 */
42 
43 #include <curses.priv.h>
44 
45 MODULE_ID("$From: lib_addstr.c,v 1.19 2000/12/10 01:24:50 tom Exp $")
46 
47 NCURSES_EXPORT(int)
48 waddnstr
49 (WINDOW *win, const char *const astr, int n)
50 {
51     unsigned const char *str = (unsigned const char *) astr;
52     int code = ERR;
53 
54     T((T_CALLED("waddnstr(%p,%s,%d)"), win, _nc_visbuf(astr), n));
55 
56     if (win && (str != 0)) {
57 	TR(TRACE_VIRTPUT | TRACE_ATTRS, ("... current %s", _traceattr(win->_attrs)));
58 	TR(TRACE_VIRTPUT, ("str is not null"));
59 	code = OK;
60 	if (n < 0)
61 	    n = (int) strlen(astr);
62 
63 	while ((n-- > 0) && (*str != '\0')) {
64 	    TR(TRACE_VIRTPUT, ("*str = %#x", *str));
65 	    if (_nc_waddch_nosync(win, (chtype) * str++) == ERR) {
66 		code = ERR;
67 		break;
68 	    }
69 	}
70 	_nc_synchook(win);
71     }
72     TR(TRACE_VIRTPUT, ("waddnstr returns %d", code));
73     returnCode(code);
74 }
75 
76 NCURSES_EXPORT(int)
77 waddchnstr
78 (WINDOW *win, const chtype * const astr, int n)
79 {
80     NCURSES_SIZE_T y = win->_cury;
81     NCURSES_SIZE_T x = win->_curx;
82     int code = OK;
83     struct ldat *line;
84 
85     T((T_CALLED("waddchnstr(%p,%p,%d)"), win, astr, n));
86 
87     if (!win)
88 	returnCode(ERR);
89 
90     if (n < 0) {
91 	const chtype *str;
92 	n = 0;
93 	for (str = (const chtype *) astr; *str != 0; str++)
94 	    n++;
95     }
96     if (n > win->_maxx - x + 1)
97 	n = win->_maxx - x + 1;
98     if (n == 0)
99 	returnCode(code);
100 
101     line = &(win->_line[y]);
102     memcpy(line->text + x, astr, n * sizeof(*astr));
103     CHANGED_RANGE(line, x, x + n - 1);
104 
105     _nc_synchook(win);
106     returnCode(code);
107 }
108