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