1 /* $NetBSD: printw.c,v 1.11 1999/04/13 14:08:18 mrg Exp $ */ 2 3 /* 4 * Copyright (c) 1981, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)printw.c 8.3 (Berkeley) 5/4/94"; 40 #else 41 __RCSID("$NetBSD: printw.c,v 1.11 1999/04/13 14:08:18 mrg Exp $"); 42 #endif 43 #endif /* not lint */ 44 45 #ifdef __STDC__ 46 #include <stdarg.h> 47 #else 48 #include <varargs.h> 49 #endif 50 51 #include "curses.h" 52 53 /* 54 * printw and friends. 55 * 56 * These routines make nonportable assumptions about varargs if __STDC__ 57 * is not in effect. 58 */ 59 60 static int __winwrite __P((void *, const char *, int)); 61 62 /* 63 * printw -- 64 * Printf on the standard screen. 65 */ 66 int 67 #ifdef __STDC__ 68 printw(const char *fmt,...) 69 #else 70 printw(fmt, va_alist) 71 char *fmt; 72 va_dcl 73 #endif 74 { 75 va_list ap; 76 int ret; 77 78 #ifdef __STDC__ 79 va_start(ap, fmt); 80 #else 81 va_start(ap); 82 #endif 83 ret = vwprintw(stdscr, fmt, ap); 84 va_end(ap); 85 return (ret); 86 } 87 /* 88 * wprintw -- 89 * Printf on the given window. 90 */ 91 int 92 #ifdef __STDC__ 93 wprintw(WINDOW * win, const char *fmt,...) 94 #else 95 wprintw(win, fmt, va_alist) 96 WINDOW *win; 97 char *fmt; 98 va_dcl 99 #endif 100 { 101 va_list ap; 102 int ret; 103 104 #ifdef __STDC__ 105 va_start(ap, fmt); 106 #else 107 va_start(ap); 108 #endif 109 ret = vwprintw(win, fmt, ap); 110 va_end(ap); 111 return (ret); 112 } 113 /* 114 * mvprintw, mvwprintw -- 115 * Implement the mvprintw commands. Due to the variable number of 116 * arguments, they cannot be macros. Sigh.... 117 */ 118 int 119 #ifdef __STDC__ 120 mvprintw(int y, int x, const char *fmt,...) 121 #else 122 mvprintw(y, x, fmt, va_alist) 123 int y, x; 124 char *fmt; 125 va_dcl 126 #endif 127 { 128 va_list ap; 129 int ret; 130 131 #ifdef __STDC__ 132 va_start(ap, fmt); 133 #else 134 va_start(ap); 135 #endif 136 if (move(y, x) != OK) 137 return (ERR); 138 ret = vwprintw(stdscr, fmt, ap); 139 va_end(ap); 140 return (ret); 141 } 142 143 int 144 #ifdef __STDC__ 145 mvwprintw(WINDOW * win, int y, int x, 146 const char *fmt,...) 147 #else 148 mvwprintw(win, y, x, fmt, va_alist) 149 WINDOW *win; 150 int y, x; 151 char *fmt; 152 va_dcl 153 #endif 154 { 155 va_list ap; 156 int ret; 157 158 #ifdef __STDC__ 159 va_start(ap, fmt); 160 #else 161 va_start(ap); 162 #endif 163 if (wmove(win, y, x) != OK) 164 return (ERR); 165 166 ret = vwprintw(win, fmt, ap); 167 va_end(ap); 168 return (ret); 169 } 170 /* 171 * Internal write-buffer-to-window function. 172 */ 173 static int 174 __winwrite(cookie, buf, n) 175 void *cookie; 176 const char *buf; 177 int n; 178 { 179 WINDOW *win; 180 int c; 181 182 for (c = n, win = cookie; --c >= 0;) 183 if (waddch(win, *buf++) == ERR) 184 return (-1); 185 return (n); 186 } 187 /* 188 * vwprintw -- 189 * This routine actually executes the printf and adds it to the window. 190 */ 191 int 192 vwprintw(win, fmt, ap) 193 WINDOW *win; 194 const char *fmt; 195 va_list ap; 196 { 197 FILE *f; 198 199 if ((f = funopen(win, NULL, __winwrite, NULL, NULL)) == NULL) 200 return (ERR); 201 (void) vfprintf(f, fmt, ap); 202 return (fclose(f) ? ERR : OK); 203 } 204