xref: /original-bsd/usr.bin/window/ttoutput.c (revision da6ea800)
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  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)ttoutput.c	8.1 (Berkeley) 06/06/93";
13 #endif /* not lint */
14 
15 #include "ww.h"
16 #include "tt.h"
17 #include <sys/errno.h>
18 
19 /*
20  * Buffered output package.
21  * We need this because stdio fails on non-blocking writes.
22  */
23 
24 ttflush()
25 {
26 	register char *p;
27 	register n = tt_obp - tt_ob;
28 	extern errno;
29 
30 	if (n == 0)
31 		return;
32 	if (tt.tt_checksum)
33 		(*tt.tt_checksum)(tt_ob, n);
34 	if (tt.tt_flush) {
35 		(*tt.tt_flush)();
36 		return;
37 	}
38 	wwnflush++;
39 	for (p = tt_ob; p < tt_obp;) {
40 		wwnwr++;
41 		n = write(1, p, tt_obp - p);
42 		if (n < 0) {
43 			wwnwre++;
44 			if (errno != EWOULDBLOCK) {
45 				/* can't deal with this */
46 				p = tt_obp;
47 			}
48 		} else if (n == 0) {
49 			/* what to do? */
50 			wwnwrz++;
51 		} else {
52 			wwnwrc += n;
53 			p += n;
54 		}
55 	}
56 	tt_obp = tt_ob;
57 }
58 
59 ttputs(s)
60 register char *s;
61 {
62 	while (*s)
63 		ttputc(*s++);
64 }
65 
66 ttwrite(s, n)
67 	register char *s;
68 	register n;
69 {
70 	switch (n) {
71 	case 0:
72 		break;
73 	case 1:
74 		ttputc(*s);
75 		break;
76 	case 2:
77 		if (tt_obe - tt_obp < 2)
78 			ttflush();
79 		*tt_obp++ = *s++;
80 		*tt_obp++ = *s;
81 		break;
82 	case 3:
83 		if (tt_obe - tt_obp < 3)
84 			ttflush();
85 		*tt_obp++ = *s++;
86 		*tt_obp++ = *s++;
87 		*tt_obp++ = *s;
88 		break;
89 	case 4:
90 		if (tt_obe - tt_obp < 4)
91 			ttflush();
92 		*tt_obp++ = *s++;
93 		*tt_obp++ = *s++;
94 		*tt_obp++ = *s++;
95 		*tt_obp++ = *s;
96 		break;
97 	case 5:
98 		if (tt_obe - tt_obp < 5)
99 			ttflush();
100 		*tt_obp++ = *s++;
101 		*tt_obp++ = *s++;
102 		*tt_obp++ = *s++;
103 		*tt_obp++ = *s++;
104 		*tt_obp++ = *s;
105 		break;
106 	default:
107 		while (n > 0) {
108 			register m;
109 
110 			while ((m = tt_obe - tt_obp) == 0)
111 				ttflush();
112 			if ((m = tt_obe - tt_obp) > n)
113 				m = n;
114 			bcopy(s, tt_obp, m);
115 			tt_obp += m;
116 			s += m;
117 			n -= m;
118 		}
119 	}
120 }
121