xref: /csrg-svn/usr.bin/window/ttoutput.c (revision 33514)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)ttoutput.c	3.5 (Berkeley) 02/21/88";
15 #endif /* not lint */
16 
17 #include "ww.h"
18 #include "tt.h"
19 #include <sys/errno.h>
20 
21 /*
22  * Buffered output package.
23  * We need this because stdio fails on non-blocking writes.
24  */
25 
26 ttflush()
27 {
28 	register char *p;
29 	register n;
30 	extern errno;
31 
32 	wwnflush++;
33 	for (p = tt_ob; p < tt_obp;) {
34 		wwnwr++;
35 		n = write(1, p, tt_obp - p);
36 		if (n < 0) {
37 			wwnwre++;
38 			if (errno != EWOULDBLOCK) {
39 				/* can't deal with this */
40 				p = tt_obp;
41 			}
42 		} else if (n == 0) {
43 			/* what to do? */
44 			wwnwrz++;
45 		} else {
46 			wwnwrc += n;
47 			p += n;
48 		}
49 	}
50 	tt_obp = tt_ob;
51 }
52 
53 ttputs(s)
54 register char *s;
55 {
56 	while (*s)
57 		ttputc(*s++);
58 }
59 
60 ttwrite(s, n)
61 	register char *s;
62 	register n;
63 {
64 	switch (n) {
65 	case 0:
66 		break;
67 	case 1:
68 		ttputc(*s);
69 		break;
70 	case 2:
71 		if (tt_obe - tt_obp < 2)
72 			ttflush();
73 		*tt_obp++ = *s++;
74 		*tt_obp++ = *s;
75 		break;
76 	case 3:
77 		if (tt_obe - tt_obp < 3)
78 			ttflush();
79 		*tt_obp++ = *s++;
80 		*tt_obp++ = *s++;
81 		*tt_obp++ = *s;
82 		break;
83 	case 4:
84 		if (tt_obe - tt_obp < 4)
85 			ttflush();
86 		*tt_obp++ = *s++;
87 		*tt_obp++ = *s++;
88 		*tt_obp++ = *s++;
89 		*tt_obp++ = *s;
90 		break;
91 	case 5:
92 		if (tt_obe - tt_obp < 5)
93 			ttflush();
94 		*tt_obp++ = *s++;
95 		*tt_obp++ = *s++;
96 		*tt_obp++ = *s++;
97 		*tt_obp++ = *s++;
98 		*tt_obp++ = *s;
99 		break;
100 	default:
101 		while (n > 0) {
102 			register m;
103 
104 			while ((m = tt_obe - tt_obp) == 0)
105 				ttflush();
106 			if ((m = tt_obe - tt_obp) > n)
107 				m = n;
108 			bcopy(s, tt_obp, m);
109 			tt_obp += m;
110 			s += m;
111 			n -= m;
112 		}
113 	}
114 }
115