xref: /netbsd-src/lib/libcurses/printw.c (revision d9158b13b5dfe46201430699a3f7a235ecf28df3)
1 /*
2  * Copyright (c) 1981, 1993
3  *	The Regents of the University of California.  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 /* from: static char sccsid[] = "@(#)printw.c	8.2 (Berkeley) 10/5/93"; */
36 static char *rcsid = "$Id: printw.c,v 1.7 1994/01/24 08:36:54 cgd Exp $";
37 #endif	/* not lint */
38 
39 #include <curses.h>
40 
41 #ifdef __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 __winwrite __P((void *, const char *, int));
55 
56 /*
57  * printw --
58  *	Printf on the standard screen.
59  */
60 int
61 #ifdef __STDC__
62 printw(const char *fmt, ...)
63 #else
64 printw(fmt, va_alist)
65 	char *fmt;
66 	va_dcl
67 #endif
68 {
69 	va_list ap;
70 	int ret;
71 
72 #ifdef __STDC__
73 	va_start(ap, fmt);
74 #else
75 	va_start(ap);
76 #endif
77 	ret = vwprintw(stdscr, fmt, ap);
78 	va_end(ap);
79 	return (ret);
80 }
81 
82 /*
83  * wprintw --
84  *	Printf on the given window.
85  */
86 int
87 #ifdef __STDC__
88 wprintw(WINDOW * win, const char *fmt, ...)
89 #else
90 wprintw(win, fmt, va_alist)
91 	WINDOW *win;
92 	char *fmt;
93 	va_dcl
94 #endif
95 {
96 	va_list ap;
97 	int ret;
98 
99 #ifdef __STDC__
100 	va_start(ap, fmt);
101 #else
102 	va_start(ap);
103 #endif
104 	ret = vwprintw(win, fmt, ap);
105 	va_end(ap);
106 	return (ret);
107 }
108 
109 /*
110  * mvprintw, mvwprintw --
111  *	Implement the mvprintw commands.  Due to the variable number of
112  *	arguments, they cannot be macros.  Sigh....
113  */
114 int
115 #ifdef __STDC__
116 mvprintw(register int y, register int x, const char *fmt, ...)
117 #else
118 mvprintw(y, x, fmt, va_alist)
119 	register int y, x;
120 	char *fmt;
121 	va_dcl
122 #endif
123 {
124 	va_list ap;
125 	int ret;
126 
127 #ifdef __STDC__
128 	va_start(ap, fmt);
129 #else
130 	va_start(ap);
131 #endif
132 	if (move(y, x) != OK)
133 		return (ERR);
134 	ret = vwprintw(stdscr, fmt, ap);
135 	va_end(ap);
136 	return (ret);
137 }
138 
139 int
140 #ifdef __STDC__
141 mvwprintw(register WINDOW * win, register int y, register int x,
142     const char *fmt, ...)
143 #else
144 mvwprintw(win, y, x, fmt, va_alist)
145 	register WINDOW *win;
146 	register int y, x;
147 	char *fmt;
148 	va_dcl
149 #endif
150 {
151 	va_list ap;
152 	int ret;
153 
154 #ifdef __STDC__
155 	va_start(ap, fmt);
156 #else
157 	va_start(ap);
158 #endif
159 	if (wmove(win, y, x) != OK)
160 		return (ERR);
161 
162 	ret = vwprintw(win, fmt, ap);
163 	va_end(ap);
164 	return (ret);
165 }
166 
167 /*
168  * Internal write-buffer-to-window function.
169  */
170 static int
171 __winwrite(cookie, buf, n)
172 	void *cookie;
173 	register const char *buf;
174 	int n;
175 {
176 	register WINDOW *win;
177 	register int c;
178 
179 	for (c = n, win = cookie; --c >= 0;)
180 		if (waddch(win, *buf++) == ERR)
181 			return (-1);
182 	return (n);
183 }
184 
185 /*
186  * vwprintw --
187  *	This routine actually executes the printf and adds it to the window.
188  */
189 int
190 vwprintw(win, fmt, ap)
191 	WINDOW *win;
192 	const char *fmt;
193 	va_list ap;
194 {
195 	FILE *f;
196 
197 	if ((f = funopen(win, NULL, __winwrite, NULL, NULL)) == NULL)
198 		return (ERR);
199 	(void)vfprintf(f, fmt, ap);
200 	return (fclose(f) ? ERR : OK);
201 }
202