1 /* $NetBSD: printw.c,v 1.17 2001/09/24 13:22:30 wiz 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.17 2001/09/24 13:22:30 wiz 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 #include "curses_private.h" 53 54 /* 55 * printw and friends. 56 * 57 * These routines make nonportable assumptions about varargs if __STDC__ 58 * is not in effect. 59 */ 60 61 static int __winwrite __P((void *, const char *, int)); 62 63 /* 64 * printw -- 65 * Printf on the standard screen. 66 */ 67 int 68 #ifdef __STDC__ 69 printw(const char *fmt,...) 70 #else 71 printw(fmt, va_alist) 72 const char *fmt; 73 va_dcl 74 #endif 75 { 76 va_list ap; 77 int ret; 78 79 #ifdef __STDC__ 80 va_start(ap, fmt); 81 #else 82 va_start(ap); 83 #endif 84 ret = vwprintw(stdscr, fmt, ap); 85 va_end(ap); 86 return (ret); 87 } 88 /* 89 * wprintw -- 90 * Printf on the given window. 91 */ 92 int 93 #ifdef __STDC__ 94 wprintw(WINDOW *win, const char *fmt,...) 95 #else 96 wprintw(win, fmt, va_alist) 97 WINDOW *win; 98 const char *fmt; 99 va_dcl 100 #endif 101 { 102 va_list ap; 103 int ret; 104 105 #ifdef __STDC__ 106 va_start(ap, fmt); 107 #else 108 va_start(ap); 109 #endif 110 ret = vwprintw(win, fmt, ap); 111 va_end(ap); 112 return (ret); 113 } 114 /* 115 * mvprintw, mvwprintw -- 116 * Implement the mvprintw commands. Due to the variable number of 117 * arguments, they cannot be macros. Sigh.... 118 */ 119 int 120 #ifdef __STDC__ 121 mvprintw(int y, int x, const char *fmt,...) 122 #else 123 mvprintw(y, x, fmt, va_alist) 124 int y, x; 125 const char *fmt; 126 va_dcl 127 #endif 128 { 129 va_list ap; 130 int ret; 131 132 if (move(y, x) != OK) 133 return (ERR); 134 #ifdef __STDC__ 135 va_start(ap, fmt); 136 #else 137 va_start(ap); 138 #endif 139 ret = vwprintw(stdscr, fmt, ap); 140 va_end(ap); 141 return (ret); 142 } 143 144 int 145 #ifdef __STDC__ 146 mvwprintw(WINDOW * win, int y, int x, const char *fmt,...) 147 #else 148 mvwprintw(win, y, x, fmt, va_alist) 149 WINDOW *win; 150 int y, x; 151 const char *fmt; 152 va_dcl 153 #endif 154 { 155 va_list ap; 156 int ret; 157 158 if (wmove(win, y, x) != OK) 159 return (ERR); 160 161 #ifdef __STDC__ 162 va_start(ap, fmt); 163 #else 164 va_start(ap); 165 #endif 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 { 184 #ifdef DEBUG 185 __CTRACE("__winwrite: %c\n", *buf); 186 #endif 187 if (waddch(win, (chtype) (*buf++ & __CHARTEXT)) == ERR) 188 return (-1); 189 } 190 return (n); 191 } 192 /* 193 * vwprintw -- 194 * This routine actually executes the printf and adds it to the window. 195 */ 196 int 197 vwprintw(WINDOW *win, const char *fmt, _BSD_VA_LIST_ ap) 198 { 199 FILE *f; 200 201 if ((f = funopen(win, NULL, __winwrite, NULL, NULL)) == NULL) 202 return (ERR); 203 (void) vfprintf(f, fmt, ap); 204 return (fclose(f) ? ERR : OK); 205 } 206