xref: /original-bsd/lib/libcurses/printw.c (revision f8557398)
1 /*
2  * Copyright (c) 1981, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)printw.c	8.3 (Berkeley) 05/04/94";
10 #endif	/* not lint */
11 
12 #ifdef __STDC__
13 #include <stdarg.h>
14 #else
15 #include <varargs.h>
16 #endif
17 
18 #include "curses.h"
19 
20 /*
21  * printw and friends.
22  *
23  * These routines make nonportable assumptions about varargs if __STDC__
24  * is not in effect.
25  */
26 
27 static int __winwrite __P((void *, const char *, int));
28 
29 /*
30  * printw --
31  *	Printf on the standard screen.
32  */
33 int
34 #ifdef __STDC__
printw(const char * fmt,...)35 printw(const char *fmt, ...)
36 #else
37 printw(fmt, va_alist)
38 	char *fmt;
39 	va_dcl
40 #endif
41 {
42 	va_list ap;
43 	int ret;
44 
45 #ifdef __STDC__
46 	va_start(ap, fmt);
47 #else
48 	va_start(ap);
49 #endif
50 	ret = vwprintw(stdscr, fmt, ap);
51 	va_end(ap);
52 	return (ret);
53 }
54 
55 /*
56  * wprintw --
57  *	Printf on the given window.
58  */
59 int
60 #ifdef __STDC__
wprintw(WINDOW * win,const char * fmt,...)61 wprintw(WINDOW * win, const char *fmt, ...)
62 #else
63 wprintw(win, fmt, va_alist)
64 	WINDOW *win;
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(win, fmt, ap);
78 	va_end(ap);
79 	return (ret);
80 }
81 
82 /*
83  * mvprintw, mvwprintw --
84  *	Implement the mvprintw commands.  Due to the variable number of
85  *	arguments, they cannot be macros.  Sigh....
86  */
87 int
88 #ifdef __STDC__
mvprintw(register int y,register int x,const char * fmt,...)89 mvprintw(register int y, register int x, const char *fmt, ...)
90 #else
91 mvprintw(y, x, fmt, va_alist)
92 	register int y, x;
93 	char *fmt;
94 	va_dcl
95 #endif
96 {
97 	va_list ap;
98 	int ret;
99 
100 #ifdef __STDC__
101 	va_start(ap, fmt);
102 #else
103 	va_start(ap);
104 #endif
105 	if (move(y, x) != OK)
106 		return (ERR);
107 	ret = vwprintw(stdscr, fmt, ap);
108 	va_end(ap);
109 	return (ret);
110 }
111 
112 int
113 #ifdef __STDC__
mvwprintw(register WINDOW * win,register int y,register int x,const char * fmt,...)114 mvwprintw(register WINDOW * win, register int y, register int x,
115     const char *fmt, ...)
116 #else
117 mvwprintw(win, y, x, fmt, va_alist)
118 	register WINDOW *win;
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 (wmove(win, y, x) != OK)
133 		return (ERR);
134 
135 	ret = vwprintw(win, fmt, ap);
136 	va_end(ap);
137 	return (ret);
138 }
139 
140 /*
141  * Internal write-buffer-to-window function.
142  */
143 static int
__winwrite(cookie,buf,n)144 __winwrite(cookie, buf, n)
145 	void *cookie;
146 	register const char *buf;
147 	int n;
148 {
149 	register WINDOW *win;
150 	register int c;
151 
152 	for (c = n, win = cookie; --c >= 0;)
153 		if (waddch(win, *buf++) == ERR)
154 			return (-1);
155 	return (n);
156 }
157 
158 /*
159  * vwprintw --
160  *	This routine actually executes the printf and adds it to the window.
161  */
162 int
vwprintw(win,fmt,ap)163 vwprintw(win, fmt, ap)
164 	WINDOW *win;
165 	const char *fmt;
166 	va_list ap;
167 {
168 	FILE *f;
169 
170 	if ((f = funopen(win, NULL, __winwrite, NULL, NULL)) == NULL)
171 		return (ERR);
172 	(void)vfprintf(f, fmt, ap);
173 	return (fclose(f) ? ERR : OK);
174 }
175