xref: /netbsd-src/lib/libcurses/printw.c (revision e9d867ef5010fbab8d48045c13025636f5cd7479)
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.8 (Berkeley) 4/15/91";*/
36 static char rcsid[] = "$Id: printw.c,v 1.3 1993/08/01 18:35:28 mycroft Exp $";
37 #endif /* not lint */
38 
39 /*
40  * printw and friends.
41  *
42  * These routines make nonportable assumptions about varargs if __STDC__
43  * is not in effect.
44  */
45 
46 #if __STDC__
47 #include <stdarg.h>
48 #else
49 #include <varargs.h>
50 #endif
51 #include "curses.ext"
52 
53 /*
54  *	This routine implements a printf on the standard screen.
55  */
56 #if __STDC__
57 printw(const char *fmt, ...)
58 #else
59 printw(fmt, va_alist)
60 	char *fmt;
61 	va_dcl
62 #endif
63 {
64 	va_list	ap;
65 	int	ret;
66 
67 #if __STDC__
68 	va_start(ap, fmt);
69 #else
70 	va_start(ap);
71 #endif
72 	ret = _sprintw(stdscr, fmt, ap);
73 	va_end(ap);
74 	return (ret);
75 }
76 
77 /*
78  *	This routine implements a printf on the given window.
79  */
80 #if __STDC__
81 wprintw(WINDOW *win, const char *fmt, ...)
82 #else
83 wprintw(win, fmt, va_alist)
84 	WINDOW *win;
85 	char *fmt;
86 	va_dcl
87 #endif
88 {
89 	va_list	ap;
90 	int	ret;
91 
92 #ifdef __STDC__
93 	va_start(ap, fmt);
94 #else
95 	va_start(ap);
96 #endif
97 	ret = _sprintw(win, fmt, ap);
98 	va_end(ap);
99 	return (ret);
100 }
101 
102 /*
103  *	Internal write-buffer-to-window function.
104  */
105 static int
106 _winwrite(cookie, buf, n)
107 	void *cookie;
108 	register char *buf;
109 	int n;
110 {
111 	register WINDOW *win = (WINDOW *)cookie;
112 	register int c = n;
113 
114 	while (--c >= 0) {
115 		if (waddch(win, (unsigned char) *buf++) == ERR)
116 			return (-1);
117 	}
118 	return n;
119 }
120 
121 /*
122  *	This routine actually executes the printf and adds it to the window.
123  *	It must not be declared static as it is used in mvprintw.c.
124  *	THIS SHOULD BE RENAMED vwprintw AND EXPORTED
125  */
126 _sprintw(win, fmt, ap)
127 	WINDOW *win;
128 #if __STDC__
129 	const char *fmt;
130 #else
131 	char *fmt;
132 #endif
133 	va_list	ap;
134 {
135 	FILE *f;
136 
137 	if ((f = fwopen((void *)win, _winwrite)) == NULL)
138 		return ERR;
139 	(void) vfprintf(f, fmt, ap);
140 	return fclose(f) ? ERR : OK;
141 }
142