printw.c revision 1.31 1 1.31 blymn /* $NetBSD: printw.c,v 1.31 2024/12/23 02:58:04 blymn 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.31 blymn __RCSID("$NetBSD: printw.c,v 1.31 2024/12/23 02:58:04 blymn 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.24 roy 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.31 blymn if (__predict_false(win == NULL))
76 1.31 blymn return ERR;
77 1.31 blymn
78 1.1 cgd va_start(ap, fmt);
79 1.21 joerg ret = vw_printw(win, fmt, ap);
80 1.4 mycroft va_end(ap);
81 1.24 roy return ret;
82 1.4 mycroft }
83 1.4 mycroft /*
84 1.4 mycroft * mvprintw, mvwprintw --
85 1.4 mycroft * Implement the mvprintw commands. Due to the variable number of
86 1.4 mycroft * arguments, they cannot be macros. Sigh....
87 1.4 mycroft */
88 1.4 mycroft int
89 1.13 jdc mvprintw(int y, int x, const char *fmt,...)
90 1.4 mycroft {
91 1.4 mycroft va_list ap;
92 1.11 mrg int ret;
93 1.4 mycroft
94 1.17 wiz if (move(y, x) != OK)
95 1.24 roy return ERR;
96 1.4 mycroft va_start(ap, fmt);
97 1.21 joerg ret = vw_printw(stdscr, fmt, ap);
98 1.4 mycroft va_end(ap);
99 1.24 roy return ret;
100 1.4 mycroft }
101 1.4 mycroft
102 1.4 mycroft int
103 1.14 simonb mvwprintw(WINDOW * win, int y, int x, const char *fmt,...)
104 1.4 mycroft {
105 1.4 mycroft va_list ap;
106 1.11 mrg int ret;
107 1.4 mycroft
108 1.29 blymn if (wmove(win, y, x) != OK)
109 1.24 roy return ERR;
110 1.17 wiz
111 1.4 mycroft va_start(ap, fmt);
112 1.21 joerg ret = vw_printw(win, fmt, ap);
113 1.1 cgd va_end(ap);
114 1.24 roy return ret;
115 1.1 cgd }
116 1.1 cgd
117 1.1 cgd /*
118 1.21 joerg * vw_printw --
119 1.1 cgd * This routine actually executes the printf and adds it to the window.
120 1.1 cgd */
121 1.5 cgd int
122 1.22 joerg vw_printw(WINDOW *win, const char *fmt, va_list ap)
123 1.1 cgd {
124 1.27 roy int n;
125 1.27 roy
126 1.30 blymn __CTRACE(__CTRACE_INPUT, "vw_printw: win %p\n", win);
127 1.30 blymn
128 1.31 blymn if (__predict_false(win == NULL))
129 1.31 blymn return ERR;
130 1.31 blymn
131 1.23 christos if (win->fp == NULL) {
132 1.27 roy win->fp = open_memstream(&win->buf, &win->buflen);
133 1.27 roy if (__predict_false(win->fp == NULL))
134 1.23 christos return ERR;
135 1.27 roy } else
136 1.27 roy rewind(win->fp);
137 1.27 roy
138 1.27 roy n = vfprintf(win->fp, fmt, ap);
139 1.27 roy if (__predict_false(n == 0))
140 1.27 roy return OK;
141 1.27 roy if (__predict_false(n == -1))
142 1.27 roy return ERR;
143 1.27 roy if (__predict_false(fflush(win->fp) != 0))
144 1.27 roy return ERR;
145 1.27 roy return waddnstr(win, win->buf, n);
146 1.1 cgd }
147 1.21 joerg
148 1.21 joerg __strong_alias(vwprintw, vw_printw)
149