home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / lib / libcurses / printw.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-31  |  4.4 KB  |  204 lines

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