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