xref: /netbsd/lib/libcurses/printw.c (revision bf9ec67e)
1 /*	$NetBSD: printw.c,v 1.18 2002/05/26 17:01:38 wiz 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.18 2002/05/26 17:01:38 wiz Exp $");
42 #endif
43 #endif				/* not lint */
44 
45 #include <stdarg.h>
46 
47 #include "curses.h"
48 #include "curses_private.h"
49 
50 /*
51  * printw and friends.
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 printw(const char *fmt,...)
62 {
63 	va_list ap;
64 	int     ret;
65 
66 	va_start(ap, fmt);
67 	ret = vwprintw(stdscr, fmt, ap);
68 	va_end(ap);
69 	return (ret);
70 }
71 /*
72  * wprintw --
73  *	Printf on the given window.
74  */
75 int
76 wprintw(WINDOW *win, const char *fmt,...)
77 {
78 	va_list ap;
79 	int     ret;
80 
81 	va_start(ap, fmt);
82 	ret = vwprintw(win, fmt, ap);
83 	va_end(ap);
84 	return (ret);
85 }
86 /*
87  * mvprintw, mvwprintw --
88  *	Implement the mvprintw commands.  Due to the variable number of
89  *	arguments, they cannot be macros.  Sigh....
90  */
91 int
92 mvprintw(int y, int x, const char *fmt,...)
93 {
94 	va_list ap;
95 	int     ret;
96 
97 	if (move(y, x) != OK)
98 		return (ERR);
99 	va_start(ap, fmt);
100 	ret = vwprintw(stdscr, fmt, ap);
101 	va_end(ap);
102 	return (ret);
103 }
104 
105 int
106 mvwprintw(WINDOW * win, int y, int x, const char *fmt,...)
107 {
108 	va_list ap;
109 	int     ret;
110 
111 	if (wmove(win, y, x) != OK)
112 		return (ERR);
113 
114 	va_start(ap, fmt);
115 	ret = vwprintw(win, fmt, ap);
116 	va_end(ap);
117 	return (ret);
118 }
119 /*
120  * Internal write-buffer-to-window function.
121  */
122 static int
123 __winwrite(cookie, buf, n)
124 	void   *cookie;
125 	const char *buf;
126 	int     n;
127 {
128 	WINDOW *win;
129 	int     c;
130 
131 	for (c = n, win = cookie; --c >= 0;)
132 	{
133 #ifdef DEBUG
134 		__CTRACE("__winwrite: %c\n", *buf);
135 #endif
136 		if (waddch(win, (chtype) (*buf++ & __CHARTEXT)) == ERR)
137 			return (-1);
138 	}
139 	return (n);
140 }
141 /*
142  * vwprintw --
143  *	This routine actually executes the printf and adds it to the window.
144  */
145 int
146 vwprintw(WINDOW *win, const char *fmt, _BSD_VA_LIST_ ap)
147 {
148 	FILE   *f;
149 
150 	if ((f = funopen(win, NULL, __winwrite, NULL, NULL)) == NULL)
151 		return (ERR);
152 	(void) vfprintf(f, fmt, ap);
153 	return (fclose(f) ? ERR : OK);
154 }
155