]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.bin/window/ttoutput.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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
37 #ifndef lint
38 static char sccsid[] = "@(#)ttoutput.c  8.1 (Berkeley) 6/6/93";
39 static char rcsid[] =
40   "$FreeBSD$";
41 #endif /* not lint */
42
43 #include "ww.h"
44 #include "tt.h"
45 #include <errno.h>
46 #include <unistd.h>
47
48 /*
49  * Buffered output package.
50  * We need this because stdio fails on non-blocking writes.
51  */
52
53 ttflush()
54 {
55         register char *p;
56         register n = tt_obp - tt_ob;
57
58         if (n == 0)
59                 return;
60         if (tt.tt_checksum)
61                 (*tt.tt_checksum)(tt_ob, n);
62         if (tt.tt_flush) {
63                 (*tt.tt_flush)();
64                 return;
65         }
66         wwnflush++;
67         for (p = tt_ob; p < tt_obp;) {
68                 wwnwr++;
69                 n = write(STDOUT_FILENO, p, tt_obp - p);
70                 if (n < 0) {
71                         wwnwre++;
72                         if (errno != EWOULDBLOCK) {
73                                 /* can't deal with this */
74                                 p = tt_obp;
75                         }
76                 } else if (n == 0) {
77                         /* what to do? */
78                         wwnwrz++;
79                 } else {
80                         wwnwrc += n;
81                         p += n;
82                 }
83         }
84         tt_obp = tt_ob;
85 }
86
87 ttputs(s)
88 register char *s;
89 {
90         while (*s)
91                 ttputc(*s++);
92 }
93
94 ttwrite(s, n)
95         register char *s;
96         register n;
97 {
98         switch (n) {
99         case 0:
100                 break;
101         case 1:
102                 ttputc(*s);
103                 break;
104         case 2:
105                 if (tt_obe - tt_obp < 2)
106                         ttflush();
107                 *tt_obp++ = *s++;
108                 *tt_obp++ = *s;
109                 break;
110         case 3:
111                 if (tt_obe - tt_obp < 3)
112                         ttflush();
113                 *tt_obp++ = *s++;
114                 *tt_obp++ = *s++;
115                 *tt_obp++ = *s;
116                 break;
117         case 4:
118                 if (tt_obe - tt_obp < 4)
119                         ttflush();
120                 *tt_obp++ = *s++;
121                 *tt_obp++ = *s++;
122                 *tt_obp++ = *s++;
123                 *tt_obp++ = *s;
124                 break;
125         case 5:
126                 if (tt_obe - tt_obp < 5)
127                         ttflush();
128                 *tt_obp++ = *s++;
129                 *tt_obp++ = *s++;
130                 *tt_obp++ = *s++;
131                 *tt_obp++ = *s++;
132                 *tt_obp++ = *s;
133                 break;
134         default:
135                 while (n > 0) {
136                         register m;
137
138                         while ((m = tt_obe - tt_obp) == 0)
139                                 ttflush();
140                         if ((m = tt_obe - tt_obp) > n)
141                                 m = n;
142                         bcopy(s, tt_obp, m);
143                         tt_obp += m;
144                         s += m;
145                         n -= m;
146                 }
147         }
148 }