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