xref: /original-bsd/lib/libcurses/printw.c (revision a6d8c59f)
1 /*
2  * Copyright (c) 1981 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)printw.c	5.11 (Berkeley) 08/31/92";
10 #endif	/* not lint */
11 
12 #include <curses.h>
13 
14 #if __STDC__
15 #include <stdarg.h>
16 #else
17 #include <varargs.h>
18 #endif
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 __sprintw __P((WINDOW *, const char *, va_list));
28 static int __winwrite __P((void *, const char *, int));
29 
30 /*
31  * printw --
32  *	Printf on the standard screen.
33  */
34 int
35 #if __STDC__
36 printw(const char *fmt, ...)
37 #else
38 printw(fmt, va_alist)
39 	char *fmt;
40 	va_dcl
41 #endif
42 {
43 	va_list ap;
44 	int ret;
45 
46 #if __STDC__
47 	va_start(ap, fmt);
48 #else
49 	va_start(ap);
50 #endif
51 	ret = __sprintw(stdscr, fmt, ap);
52 	va_end(ap);
53 	return (ret);
54 }
55 
56 /*
57  * wprintw --
58  *	Printf on the given window.
59  */
60 int
61 #if __STDC__
62 wprintw(WINDOW * win, const char *fmt, ...)
63 #else
64 wprintw(win, fmt, va_alist)
65 	WINDOW *win;
66 	char *fmt;
67 	va_dcl
68 #endif
69 {
70 	va_list ap;
71 	int ret;
72 
73 #ifdef __STDC__
74 	va_start(ap, fmt);
75 #else
76 	va_start(ap);
77 #endif
78 	ret = __sprintw(win, fmt, ap);
79 	va_end(ap);
80 	return (ret);
81 }
82 
83 /*
84  * mvprintw, mvwprintw --
85  *	Implement the mvprintw commands.  Due to the variable number of
86  *	arguments, they cannot be macros.  Sigh....
87  */
88 int
89 #if __STDC__
90 mvprintw(register int y, register int x, const char *fmt, ...)
91 #else
92 mvprintw(y, x, fmt, va_alist)
93 	register int y, x;
94 	char *fmt;
95 	va_dcl
96 #endif
97 {
98 	va_list ap;
99 	int ret;
100 
101 #if __STDC__
102 	va_start(ap, fmt);
103 #else
104 	va_start(ap);
105 #endif
106 	if (move(y, x) != OK)
107 		return (ERR);
108 	ret = __sprintw(stdscr, fmt, ap);
109 	va_end(ap);
110 	return (ret);
111 }
112 
113 int
114 #if __STDC__
115 mvwprintw(register WINDOW * win, register int y, register int x,
116     const char *fmt, ...)
117 #else
118 mvwprintw(win, y, x, fmt, va_alist)
119 	register WINDOW *win;
120 	register int y, x;
121 	char *fmt;
122 	va_dcl
123 #endif
124 {
125 	va_list ap;
126 	int ret;
127 
128 #if __STDC__
129 	va_start(ap, fmt);
130 #else
131 	va_start(ap);
132 #endif
133 	if (wmove(win, y, x) != OK)
134 		return (ERR);
135 
136 	ret = __sprintw(win, fmt, ap);
137 	va_end(ap);
138 	return (ret);
139 }
140 
141 /*
142  * Internal write-buffer-to-window function.
143  */
144 static int
145 __winwrite(cookie, buf, n)
146 	void *cookie;
147 	register const char *buf;
148 	int n;
149 {
150 	register WINDOW *win;
151 	register int c;
152 
153 	for (c = n, win = cookie; --c >= 0;)
154 		if (waddch(win, *buf++) == ERR)
155 			return (-1);
156 	return (n);
157 }
158 
159 /*
160  * __sprintw --
161  *	This routine actually executes the printf and adds it to the window.
162  *	It must not be declared static as it is used in mvprintw.c.
163  *	THIS SHOULD BE RENAMED vwprintw AND EXPORTED
164  */
165 static int
166 __sprintw(win, fmt, ap)
167 	WINDOW *win;
168 	const char *fmt;
169 	va_list ap;
170 {
171 	FILE *f;
172 
173 	if ((f = funopen(win, NULL, __winwrite, NULL, NULL)) == NULL)
174 		return (ERR);
175 	(void)vfprintf(f, fmt, ap);
176 	return (fclose(f) ? ERR : OK);
177 }
178