1 /*
2  * Copyright (c) 1981, 1993, 1994
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef lint
31 static char sccsid[] = "@(#)printw.c	8.3 (Berkeley) 5/4/94";
32 #endif	/* not lint */
33 
34 #include <stdarg.h>
35 
36 #include "curses.h"
37 
38 /*
39  * printw and friends.
40  */
41 
42 static int __winwrite(void *, const char *, int);
43 
44 /*
45  * printw --
46  *	Printf on the standard screen.
47  */
48 int
printw(const char * fmt,...)49 printw(const char *fmt, ...)
50 {
51 	va_list ap;
52 	int ret;
53 
54 	va_start(ap, fmt);
55 	ret = vwprintw(stdscr, fmt, ap);
56 	va_end(ap);
57 	return (ret);
58 }
59 
60 /*
61  * wprintw --
62  *	Printf on the given window.
63  */
64 int
wprintw(WINDOW * win,const char * fmt,...)65 wprintw(WINDOW * win, const char *fmt, ...)
66 {
67 	va_list ap;
68 	int ret;
69 
70 	va_start(ap, fmt);
71 	ret = vwprintw(win, fmt, ap);
72 	va_end(ap);
73 	return (ret);
74 }
75 
76 /*
77  * mvprintw, mvwprintw --
78  *	Implement the mvprintw commands.  Due to the variable number of
79  *	arguments, they cannot be macros.  Sigh....
80  */
81 int
mvprintw(register int y,register int x,const char * fmt,...)82 mvprintw(register int y, register int x, const char *fmt, ...)
83 {
84 	va_list ap;
85 	int ret;
86 
87 	va_start(ap, fmt);
88 	if (move(y, x) != OK)
89 		return (ERR);
90 	ret = vwprintw(stdscr, fmt, ap);
91 	va_end(ap);
92 	return (ret);
93 }
94 
95 int
mvwprintw(register WINDOW * win,register int y,register int x,const char * fmt,...)96 mvwprintw(register WINDOW * win, register int y, register int x,
97     const char *fmt, ...)
98 {
99 	va_list ap;
100 	int ret;
101 
102 	va_start(ap, fmt);
103 	if (wmove(win, y, x) != OK)
104 		return (ERR);
105 
106 	ret = vwprintw(win, fmt, ap);
107 	va_end(ap);
108 	return (ret);
109 }
110 
111 /*
112  * Internal write-buffer-to-window function.
113  */
114 static int
__winwrite(cookie,buf,n)115 __winwrite(cookie, buf, n)
116 	void *cookie;
117 	register const char *buf;
118 	int n;
119 {
120 	register WINDOW *win;
121 	register int c;
122 
123 	for (c = n, win = cookie; --c >= 0;)
124 		if (waddch(win, *buf++) == ERR)
125 			return (-1);
126 	return (n);
127 }
128 
129 /*
130  * vwprintw --
131  *	This routine actually executes the printf and adds it to the window.
132  */
133 int
vwprintw(win,fmt,ap)134 vwprintw(win, fmt, ap)
135 	WINDOW *win;
136 	const char *fmt;
137 	va_list ap;
138 {
139 	FILE *f;
140 
141 	if ((f = funopen(win, NULL, __winwrite, NULL, NULL)) == NULL)
142 		return (ERR);
143 	(void)vfprintf(f, fmt, ap);
144 	return (fclose(f) ? ERR : OK);
145 }
146