xref: /original-bsd/usr.bin/window/ttoutput.c (revision a141c157)
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 the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)ttoutput.c	3.6 (Berkeley) 06/29/88";
20 #endif /* not lint */
21 
22 #include "ww.h"
23 #include "tt.h"
24 #include <sys/errno.h>
25 
26 /*
27  * Buffered output package.
28  * We need this because stdio fails on non-blocking writes.
29  */
30 
31 ttflush()
32 {
33 	register char *p;
34 	register n;
35 	extern errno;
36 
37 	wwnflush++;
38 	for (p = tt_ob; p < tt_obp;) {
39 		wwnwr++;
40 		n = write(1, p, tt_obp - p);
41 		if (n < 0) {
42 			wwnwre++;
43 			if (errno != EWOULDBLOCK) {
44 				/* can't deal with this */
45 				p = tt_obp;
46 			}
47 		} else if (n == 0) {
48 			/* what to do? */
49 			wwnwrz++;
50 		} else {
51 			wwnwrc += n;
52 			p += n;
53 		}
54 	}
55 	tt_obp = tt_ob;
56 }
57 
58 ttputs(s)
59 register char *s;
60 {
61 	while (*s)
62 		ttputc(*s++);
63 }
64 
65 ttwrite(s, n)
66 	register char *s;
67 	register n;
68 {
69 	switch (n) {
70 	case 0:
71 		break;
72 	case 1:
73 		ttputc(*s);
74 		break;
75 	case 2:
76 		if (tt_obe - tt_obp < 2)
77 			ttflush();
78 		*tt_obp++ = *s++;
79 		*tt_obp++ = *s;
80 		break;
81 	case 3:
82 		if (tt_obe - tt_obp < 3)
83 			ttflush();
84 		*tt_obp++ = *s++;
85 		*tt_obp++ = *s++;
86 		*tt_obp++ = *s;
87 		break;
88 	case 4:
89 		if (tt_obe - tt_obp < 4)
90 			ttflush();
91 		*tt_obp++ = *s++;
92 		*tt_obp++ = *s++;
93 		*tt_obp++ = *s++;
94 		*tt_obp++ = *s;
95 		break;
96 	case 5:
97 		if (tt_obe - tt_obp < 5)
98 			ttflush();
99 		*tt_obp++ = *s++;
100 		*tt_obp++ = *s++;
101 		*tt_obp++ = *s++;
102 		*tt_obp++ = *s++;
103 		*tt_obp++ = *s;
104 		break;
105 	default:
106 		while (n > 0) {
107 			register m;
108 
109 			while ((m = tt_obe - tt_obp) == 0)
110 				ttflush();
111 			if ((m = tt_obe - tt_obp) > n)
112 				m = n;
113 			bcopy(s, tt_obp, m);
114 			tt_obp += m;
115 			s += m;
116 			n -= m;
117 		}
118 	}
119 }
120