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