xref: /netbsd/lib/libcurses/instr.c (revision 6550d01e)
1 /*	$NetBSD: instr.c,v 1.3 2009/07/22 16:57:15 roy Exp $	*/
2 
3 /*
4  * Copyright 2001 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Simon Burge for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed for the NetBSD Project by
20  *      Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND ANY
26  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC. BE
29  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
35  * THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 #ifndef lint
40 __RCSID("$NetBSD: instr.c,v 1.3 2009/07/22 16:57:15 roy Exp $");
41 #endif				/* not lint */
42 
43 #include "curses.h"
44 #include "curses_private.h"
45 
46 #ifndef _CURSES_USE_MACROS
47 
48 /*
49  * instr, innstr --
50  *	Return a string of characters at cursor position from stdscr.
51  */
52 __warn_references(instr,
53     "warning: this program uses instr(), which is unsafe.")
54 int
55 instr(char *str)
56 {
57 	return winstr(stdscr, str);
58 }
59 
60 int
61 innstr(char *str, int n)
62 {
63 	return winnstr(stdscr, str, n);
64 }
65 
66 /*
67  * mvinstr, mvinnstr --
68  *      Return a string of characters at position (y, x) from stdscr.
69  *	XXX: should be multi-byte characters for SUSv2.
70  */
71 __warn_references(mvinstr,
72     "warning: this program uses mvinstr(), which is unsafe.")
73 int
74 mvinstr(int y, int x, char *str)
75 {
76 	return mvwinstr(stdscr, y, x, str);
77 }
78 
79 int
80 mvinnstr(int y, int x, char *str, int n)
81 {
82 	return mvwinnstr(stdscr, y, x, str, n);
83 }
84 
85 /*
86  * mvwinstr, mvwinnstr --
87  *      Return an array characters at position (y, x) from the given window.
88  *	XXX: should be multi-byte characters for SUSv2.
89  */
90 __warn_references(mvwinstr,
91     "warning: this program uses mvwinstr(), which is unsafe.")
92 int
93 mvwinstr(WINDOW *win, int y, int x, char *str)
94 {
95 	if (wmove(win, y, x) == ERR)
96 		return ERR;
97 
98 	return winstr(win, str);
99 }
100 
101 int
102 mvwinnstr(WINDOW *win, int y, int x, char *str, int n)
103 {
104 	if (wmove(win, y, x) == ERR)
105 		return ERR;
106 
107 	return winnstr(win, str, n);
108 }
109 
110 #endif	/* _CURSES_USE_MACROS */
111 
112 /*
113  * winstr, winnstr --
114  *	Return a string of characters at cursor position.
115  *	XXX: should be multi-byte characters for SUSv2.
116  */
117 __warn_references(winstr,
118     "warning: this program uses winstr(), which is unsafe.")
119 int
120 winstr(WINDOW *win, char *str)
121 {
122 
123 	return winnstr(win, str, -1);
124 }
125 
126 /*
127  * XXX: This should go in a manpage!
128  * - winnstr() returns the number of characters copied only of if it is
129  *   called with n >= 0 (ie, as inchnstr(), mvinchnstr(), mvwinchnstr()
130  *   or winchnstr()).  If N < 0, it returns `OK'.
131  * - SUSv2/xcurses doesn't document whether the trailing NUL is included
132  *   in the length count or not.  For safety's sake it _is_ included.
133  * - This implementation does not (yet) support multi-byte characters
134  *   strings.
135  */
136 int
137 winnstr(WINDOW *win, char *str, int n)
138 {
139 	__LDATA	*end, *start;
140 	int epos;
141 
142 	if (str == NULL)
143 		return ERR;
144 
145 	start = &win->alines[win->cury]->line[win->curx];
146 	/* (n - 1) to leave room for the trailing NUL */
147 	if (n < 0 || (n - 1) > win->maxx - win->curx - 1) {
148 		epos = win->maxx - 1;
149 		n = win->maxx - win->curx;
150 	} else {
151 		/* extra -1 for trailing NUL */
152 		epos = win->curx + n - 1 - 1;
153 		n--;
154 	}
155 	end = &win->alines[win->cury]->line[epos];
156 
157 	while (start <= end) {
158 		*str = start->ch & __CHARTEXT;
159 		str++;
160 		start++;
161 	}
162 	*str = '\0';
163 
164 	if (n < 0)
165 		return OK;
166 	else
167 		return n;
168 }
169