Merge from vendor branch BZIP:
[dragonfly.git] / usr.bin / window / ttoutput.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  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  * @(#)ttoutput.c       8.1 (Berkeley) 6/6/93
37  * $FreeBSD: src/usr.bin/window/ttoutput.c,v 1.1.1.1.14.2 2001/05/17 09:45:01 obrien Exp $
38  * $DragonFly: src/usr.bin/window/ttoutput.c,v 1.2 2003/06/17 04:29:34 dillon Exp $
39  */
40
41 #include "ww.h"
42 #include "tt.h"
43 #include <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 = tt_obp - tt_ob;
54
55         if (n == 0)
56                 return;
57         if (tt.tt_checksum)
58                 (*tt.tt_checksum)(tt_ob, n);
59         if (tt.tt_flush) {
60                 (*tt.tt_flush)();
61                 return;
62         }
63         wwnflush++;
64         for (p = tt_ob; p < tt_obp;) {
65                 wwnwr++;
66                 n = write(1, p, tt_obp - p);
67                 if (n < 0) {
68                         wwnwre++;
69                         if (errno != EWOULDBLOCK) {
70                                 /* can't deal with this */
71                                 p = tt_obp;
72                         }
73                 } else if (n == 0) {
74                         /* what to do? */
75                         wwnwrz++;
76                 } else {
77                         wwnwrc += n;
78                         p += n;
79                 }
80         }
81         tt_obp = tt_ob;
82 }
83
84 ttputs(s)
85 register char *s;
86 {
87         while (*s)
88                 ttputc(*s++);
89 }
90
91 ttwrite(s, n)
92         register char *s;
93         register n;
94 {
95         switch (n) {
96         case 0:
97                 break;
98         case 1:
99                 ttputc(*s);
100                 break;
101         case 2:
102                 if (tt_obe - tt_obp < 2)
103                         ttflush();
104                 *tt_obp++ = *s++;
105                 *tt_obp++ = *s;
106                 break;
107         case 3:
108                 if (tt_obe - tt_obp < 3)
109                         ttflush();
110                 *tt_obp++ = *s++;
111                 *tt_obp++ = *s++;
112                 *tt_obp++ = *s;
113                 break;
114         case 4:
115                 if (tt_obe - tt_obp < 4)
116                         ttflush();
117                 *tt_obp++ = *s++;
118                 *tt_obp++ = *s++;
119                 *tt_obp++ = *s++;
120                 *tt_obp++ = *s;
121                 break;
122         case 5:
123                 if (tt_obe - tt_obp < 5)
124                         ttflush();
125                 *tt_obp++ = *s++;
126                 *tt_obp++ = *s++;
127                 *tt_obp++ = *s++;
128                 *tt_obp++ = *s++;
129                 *tt_obp++ = *s;
130                 break;
131         default:
132                 while (n > 0) {
133                         register m;
134
135                         while ((m = tt_obe - tt_obp) == 0)
136                                 ttflush();
137                         if ((m = tt_obe - tt_obp) > n)
138                                 m = n;
139                         bcopy(s, tt_obp, m);
140                         tt_obp += m;
141                         s += m;
142                         n -= m;
143                 }
144         }
145 }