xref: /netbsd-src/lib/libcurses/printw.c (revision 4b30c543a0b21e3ba94f2c569e9a82b4fdb2075f)
1 /*
2  * Copyright (c) 1981 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 /*static char sccsid[] = "from: @(#)printw.c	5.11 (Berkeley) 8/31/92";*/
36 static char rcsid[] = "$Id: printw.c,v 1.4 1993/08/07 05:49:03 mycroft Exp $";
37 #endif	/* not lint */
38 
39 #include <curses.h>
40 
41 #if __STDC__
42 #include <stdarg.h>
43 #else
44 #include <varargs.h>
45 #endif
46 
47 /*
48  * printw and friends.
49  *
50  * These routines make nonportable assumptions about varargs if __STDC__
51  * is not in effect.
52  */
53 
54 static int __sprintw __P((WINDOW *, const char *, va_list));
55 static int __winwrite __P((void *, const char *, int));
56 
57 /*
58  * printw --
59  *	Printf on the standard screen.
60  */
61 int
62 #if __STDC__
63 printw(const char *fmt, ...)
64 #else
65 printw(fmt, va_alist)
66 	char *fmt;
67 	va_dcl
68 #endif
69 {
70 	va_list ap;
71 	int ret;
72 
73 #if __STDC__
74 	va_start(ap, fmt);
75 #else
76 	va_start(ap);
77 #endif
78 	ret = __sprintw(stdscr, fmt, ap);
79 	va_end(ap);
80 	return (ret);
81 }
82 
83 /*
84  * wprintw --
85  *	Printf on the given window.
86  */
87 int
88 #if __STDC__
89 wprintw(WINDOW * win, const char *fmt, ...)
90 #else
91 wprintw(win, fmt, va_alist)
92 	WINDOW *win;
93 	char *fmt;
94 	va_dcl
95 #endif
96 {
97 	va_list ap;
98 	int ret;
99 
100 #ifdef __STDC__
101 	va_start(ap, fmt);
102 #else
103 	va_start(ap);
104 #endif
105 	ret = __sprintw(win, fmt, ap);
106 	va_end(ap);
107 	return (ret);
108 }
109 
110 /*
111  * mvprintw, mvwprintw --
112  *	Implement the mvprintw commands.  Due to the variable number of
113  *	arguments, they cannot be macros.  Sigh....
114  */
115 int
116 #if __STDC__
117 mvprintw(register int y, register int x, const char *fmt, ...)
118 #else
119 mvprintw(y, x, fmt, va_alist)
120 	register int y, x;
121 	char *fmt;
122 	va_dcl
123 #endif
124 {
125 	va_list ap;
126 	int ret;
127 
128 #if __STDC__
129 	va_start(ap, fmt);
130 #else
131 	va_start(ap);
132 #endif
133 	if (move(y, x) != OK)
134 		return (ERR);
135 	ret = __sprintw(stdscr, fmt, ap);
136 	va_end(ap);
137 	return (ret);
138 }
139 
140 int
141 #if __STDC__
142 mvwprintw(register WINDOW * win, register int y, register int x,
143     const char *fmt, ...)
144 #else
145 mvwprintw(win, y, x, fmt, va_alist)
146 	register WINDOW *win;
147 	register int y, x;
148 	char *fmt;
149 	va_dcl
150 #endif
151 {
152 	va_list ap;
153 	int ret;
154 
155 #if __STDC__
156 	va_start(ap, fmt);
157 #else
158 	va_start(ap);
159 #endif
160 	if (wmove(win, y, x) != OK)
161 		return (ERR);
162 
163 	ret = __sprintw(win, fmt, ap);
164 	va_end(ap);
165 	return (ret);
166 }
167 
168 /*
169  * Internal write-buffer-to-window function.
170  */
171 static int
172 __winwrite(cookie, buf, n)
173 	void *cookie;
174 	register const char *buf;
175 	int n;
176 {
177 	register WINDOW *win;
178 	register int c;
179 
180 	for (c = n, win = cookie; --c >= 0;)
181 		if (waddch(win, *buf++) == ERR)
182 			return (-1);
183 	return (n);
184 }
185 
186 /*
187  * __sprintw --
188  *	This routine actually executes the printf and adds it to the window.
189  *	It must not be declared static as it is used in mvprintw.c.
190  *	THIS SHOULD BE RENAMED vwprintw AND EXPORTED
191  */
192 static int
193 __sprintw(win, fmt, ap)
194 	WINDOW *win;
195 	const char *fmt;
196 	va_list ap;
197 {
198 	FILE *f;
199 
200 	if ((f = funopen(win, NULL, __winwrite, NULL, NULL)) == NULL)
201 		return (ERR);
202 	(void)vfprintf(f, fmt, ap);
203 	return (fclose(f) ? ERR : OK);
204 }
205