printw.c revision 1.23 1 1.23 christos /* $NetBSD: printw.c,v 1.23 2016/10/23 21:20:56 christos Exp $ */
2 1.9 mikel
3 1.1 cgd /*
4 1.8 cgd * Copyright (c) 1981, 1993, 1994
5 1.5 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.19 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.9 mikel #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.9 mikel #if 0
35 1.8 cgd static char sccsid[] = "@(#)printw.c 8.3 (Berkeley) 5/4/94";
36 1.9 mikel #else
37 1.23 christos __RCSID("$NetBSD: printw.c,v 1.23 2016/10/23 21:20:56 christos Exp $");
38 1.9 mikel #endif
39 1.11 mrg #endif /* not lint */
40 1.1 cgd
41 1.4 mycroft #include <stdarg.h>
42 1.8 cgd
43 1.8 cgd #include "curses.h"
44 1.12 blymn #include "curses_private.h"
45 1.4 mycroft
46 1.1 cgd /*
47 1.1 cgd * printw and friends.
48 1.1 cgd */
49 1.1 cgd
50 1.1 cgd /*
51 1.4 mycroft * printw --
52 1.4 mycroft * Printf on the standard screen.
53 1.1 cgd */
54 1.4 mycroft int
55 1.13 jdc printw(const char *fmt,...)
56 1.1 cgd {
57 1.4 mycroft va_list ap;
58 1.11 mrg int ret;
59 1.1 cgd
60 1.1 cgd va_start(ap, fmt);
61 1.21 joerg ret = vw_printw(stdscr, fmt, ap);
62 1.1 cgd va_end(ap);
63 1.1 cgd return (ret);
64 1.1 cgd }
65 1.1 cgd /*
66 1.4 mycroft * wprintw --
67 1.4 mycroft * Printf on the given window.
68 1.1 cgd */
69 1.4 mycroft int
70 1.15 blymn wprintw(WINDOW *win, const char *fmt,...)
71 1.1 cgd {
72 1.4 mycroft va_list ap;
73 1.11 mrg int ret;
74 1.1 cgd
75 1.1 cgd va_start(ap, fmt);
76 1.21 joerg ret = vw_printw(win, fmt, ap);
77 1.4 mycroft va_end(ap);
78 1.4 mycroft return (ret);
79 1.4 mycroft }
80 1.4 mycroft /*
81 1.4 mycroft * mvprintw, mvwprintw --
82 1.4 mycroft * Implement the mvprintw commands. Due to the variable number of
83 1.4 mycroft * arguments, they cannot be macros. Sigh....
84 1.4 mycroft */
85 1.4 mycroft int
86 1.13 jdc mvprintw(int y, int x, const char *fmt,...)
87 1.4 mycroft {
88 1.4 mycroft va_list ap;
89 1.11 mrg int ret;
90 1.4 mycroft
91 1.17 wiz if (move(y, x) != OK)
92 1.17 wiz return (ERR);
93 1.4 mycroft va_start(ap, fmt);
94 1.21 joerg ret = vw_printw(stdscr, fmt, ap);
95 1.4 mycroft va_end(ap);
96 1.4 mycroft return (ret);
97 1.4 mycroft }
98 1.4 mycroft
99 1.4 mycroft int
100 1.14 simonb mvwprintw(WINDOW * win, int y, int x, const char *fmt,...)
101 1.4 mycroft {
102 1.4 mycroft va_list ap;
103 1.11 mrg int ret;
104 1.4 mycroft
105 1.17 wiz if (wmove(win, y, x) != OK)
106 1.17 wiz return (ERR);
107 1.17 wiz
108 1.4 mycroft va_start(ap, fmt);
109 1.21 joerg ret = vw_printw(win, fmt, ap);
110 1.1 cgd va_end(ap);
111 1.1 cgd return (ret);
112 1.1 cgd }
113 1.1 cgd /*
114 1.4 mycroft * Internal write-buffer-to-window function.
115 1.1 cgd */
116 1.23 christos static ssize_t
117 1.23 christos winwrite(void *cookie, const void *vbuf, size_t n)
118 1.1 cgd {
119 1.10 perry WINDOW *win;
120 1.23 christos size_t c;
121 1.23 christos const char *buf = vbuf;
122 1.1 cgd
123 1.23 christos for (c = 0, win = cookie; c < n; c++) {
124 1.12 blymn #ifdef DEBUG
125 1.20 jdc __CTRACE(__CTRACE_MISC, "__winwrite: %c\n", *buf);
126 1.12 blymn #endif
127 1.12 blymn if (waddch(win, (chtype) (*buf++ & __CHARTEXT)) == ERR)
128 1.1 cgd return (-1);
129 1.12 blymn }
130 1.23 christos return (ssize_t)n;
131 1.1 cgd }
132 1.1 cgd /*
133 1.21 joerg * vw_printw --
134 1.1 cgd * This routine actually executes the printf and adds it to the window.
135 1.1 cgd */
136 1.5 cgd int
137 1.22 joerg vw_printw(WINDOW *win, const char *fmt, va_list ap)
138 1.1 cgd {
139 1.23 christos if (win->fp == NULL) {
140 1.23 christos win->fp = funopen2(win, NULL, winwrite, NULL, NULL, NULL);
141 1.23 christos if (win->fp == NULL)
142 1.23 christos return ERR;
143 1.23 christos }
144 1.23 christos vfprintf(win->fp, fmt, ap);
145 1.23 christos fflush(win->fp);
146 1.23 christos return OK;
147 1.1 cgd }
148 1.21 joerg
149 1.21 joerg __strong_alias(vwprintw, vw_printw)
150