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