home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / window / ttoutput.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-18  |  3.4 KB  |  139 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Edward Wang at The University of California, Berkeley.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #ifndef lint
  38. static char sccsid[] = "@(#)ttoutput.c    3.9 (Berkeley) 6/6/90";
  39. #endif /* not lint */
  40.  
  41. #include "ww.h"
  42. #include "tt.h"
  43. #include <sys/errno.h>
  44.  
  45. /*
  46.  * Buffered output package.
  47.  * We need this because stdio fails on non-blocking writes.
  48.  */
  49.  
  50. ttflush()
  51. {
  52.     register char *p;
  53.     register n;
  54.     extern errno;
  55.  
  56.     wwnflush++;
  57.     for (p = tt_ob; p < tt_obp;) {
  58.         wwnwr++;
  59.         n = write(1, p, tt_obp - p);
  60.         if (n < 0) {
  61.             wwnwre++;
  62.             if (errno != EWOULDBLOCK) {
  63.                 /* can't deal with this */
  64.                 p = tt_obp;
  65.             }
  66.         } else if (n == 0) {
  67.             /* what to do? */
  68.             wwnwrz++;
  69.         } else {
  70.             wwnwrc += n;
  71.             p += n;
  72.         }
  73.     }
  74.     tt_obp = tt_ob;
  75. }
  76.  
  77. ttputs(s)
  78. register char *s;
  79. {
  80.     while (*s)
  81.         ttputc(*s++);
  82. }
  83.  
  84. ttwrite(s, n)
  85.     register char *s;
  86.     register n;
  87. {
  88.     switch (n) {
  89.     case 0:
  90.         break;
  91.     case 1:
  92.         ttputc(*s);
  93.         break;
  94.     case 2:
  95.         if (tt_obe - tt_obp < 2)
  96.             (*tt.tt_flush)();
  97.         *tt_obp++ = *s++;
  98.         *tt_obp++ = *s;
  99.         break;
  100.     case 3:
  101.         if (tt_obe - tt_obp < 3)
  102.             (*tt.tt_flush)();
  103.         *tt_obp++ = *s++;
  104.         *tt_obp++ = *s++;
  105.         *tt_obp++ = *s;
  106.         break;
  107.     case 4:
  108.         if (tt_obe - tt_obp < 4)
  109.             (*tt.tt_flush)();
  110.         *tt_obp++ = *s++;
  111.         *tt_obp++ = *s++;
  112.         *tt_obp++ = *s++;
  113.         *tt_obp++ = *s;
  114.         break;
  115.     case 5:
  116.         if (tt_obe - tt_obp < 5)
  117.             (*tt.tt_flush)();
  118.         *tt_obp++ = *s++;
  119.         *tt_obp++ = *s++;
  120.         *tt_obp++ = *s++;
  121.         *tt_obp++ = *s++;
  122.         *tt_obp++ = *s;
  123.         break;
  124.     default:
  125.         while (n > 0) {
  126.             register m;
  127.  
  128.             while ((m = tt_obe - tt_obp) == 0)
  129.                 (*tt.tt_flush)();
  130.             if ((m = tt_obe - tt_obp) > n)
  131.                 m = n;
  132.             bcopy(s, tt_obp, m);
  133.             tt_obp += m;
  134.             s += m;
  135.             n -= m;
  136.         }
  137.     }
  138. }
  139.