xref: /csrg-svn/usr.bin/window/ttoutput.c (revision 34909)
118756Sedward /*
233514Sbostic  * Copyright (c) 1983 Regents of the University of California.
333514Sbostic  * All rights reserved.
433514Sbostic  *
533514Sbostic  * Redistribution and use in source and binary forms are permitted
6*34909Sbostic  * provided that the above copyright notice and this paragraph are
7*34909Sbostic  * duplicated in all such forms and that any documentation,
8*34909Sbostic  * advertising materials, and other materials related to such
9*34909Sbostic  * distribution and use acknowledge that the software was developed
10*34909Sbostic  * by the University of California, Berkeley.  The name of the
11*34909Sbostic  * University may not be used to endorse or promote products derived
12*34909Sbostic  * from this software without specific prior written permission.
13*34909Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34909Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34909Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1618756Sedward  */
1718756Sedward 
1833514Sbostic #ifndef lint
19*34909Sbostic static char sccsid[] = "@(#)ttoutput.c	3.6 (Berkeley) 06/29/88";
2033514Sbostic #endif /* not lint */
2133514Sbostic 
2216125Sedward #include "ww.h"
2316125Sedward #include "tt.h"
2416125Sedward #include <sys/errno.h>
2516125Sedward 
2616125Sedward /*
2716125Sedward  * Buffered output package.
2816125Sedward  * We need this because stdio fails on non-blocking writes.
2916125Sedward  */
3016125Sedward 
3116125Sedward ttflush()
3216125Sedward {
3316125Sedward 	register char *p;
3416125Sedward 	register n;
3516125Sedward 	extern errno;
3616125Sedward 
3716125Sedward 	wwnflush++;
3816125Sedward 	for (p = tt_ob; p < tt_obp;) {
3916125Sedward 		wwnwr++;
4016125Sedward 		n = write(1, p, tt_obp - p);
4116125Sedward 		if (n < 0) {
4216125Sedward 			wwnwre++;
4316125Sedward 			if (errno != EWOULDBLOCK) {
4416125Sedward 				/* can't deal with this */
4516125Sedward 				p = tt_obp;
4616125Sedward 			}
4716125Sedward 		} else if (n == 0) {
4816125Sedward 			/* what to do? */
4916125Sedward 			wwnwrz++;
5016125Sedward 		} else {
5116125Sedward 			wwnwrc += n;
5216125Sedward 			p += n;
5316125Sedward 		}
5416125Sedward 	}
5516125Sedward 	tt_obp = tt_ob;
5616125Sedward }
5716125Sedward 
5816125Sedward ttputs(s)
5916125Sedward register char *s;
6016125Sedward {
6116125Sedward 	while (*s)
6216125Sedward 		ttputc(*s++);
6316125Sedward }
6424959Sedward 
6524959Sedward ttwrite(s, n)
6624959Sedward 	register char *s;
6724959Sedward 	register n;
6824959Sedward {
6924959Sedward 	switch (n) {
7024959Sedward 	case 0:
7124959Sedward 		break;
7224959Sedward 	case 1:
7324959Sedward 		ttputc(*s);
7424959Sedward 		break;
7524959Sedward 	case 2:
7624959Sedward 		if (tt_obe - tt_obp < 2)
7724959Sedward 			ttflush();
7824959Sedward 		*tt_obp++ = *s++;
7924959Sedward 		*tt_obp++ = *s;
8024959Sedward 		break;
8124959Sedward 	case 3:
8224959Sedward 		if (tt_obe - tt_obp < 3)
8324959Sedward 			ttflush();
8424959Sedward 		*tt_obp++ = *s++;
8524959Sedward 		*tt_obp++ = *s++;
8624959Sedward 		*tt_obp++ = *s;
8724959Sedward 		break;
8824959Sedward 	case 4:
8924959Sedward 		if (tt_obe - tt_obp < 4)
9024959Sedward 			ttflush();
9124959Sedward 		*tt_obp++ = *s++;
9224959Sedward 		*tt_obp++ = *s++;
9324959Sedward 		*tt_obp++ = *s++;
9424959Sedward 		*tt_obp++ = *s;
9524959Sedward 		break;
9624959Sedward 	case 5:
9724959Sedward 		if (tt_obe - tt_obp < 5)
9824959Sedward 			ttflush();
9924959Sedward 		*tt_obp++ = *s++;
10024959Sedward 		*tt_obp++ = *s++;
10124959Sedward 		*tt_obp++ = *s++;
10224959Sedward 		*tt_obp++ = *s++;
10324959Sedward 		*tt_obp++ = *s;
10424959Sedward 		break;
10524959Sedward 	default:
10624959Sedward 		while (n > 0) {
10724959Sedward 			register m;
10824959Sedward 
10924959Sedward 			while ((m = tt_obe - tt_obp) == 0)
11024959Sedward 				ttflush();
11124959Sedward 			if ((m = tt_obe - tt_obp) > n)
11224959Sedward 				m = n;
11324959Sedward 			bcopy(s, tt_obp, m);
11424959Sedward 			tt_obp += m;
11524959Sedward 			s += m;
11624959Sedward 			n -= m;
11724959Sedward 		}
11824959Sedward 	}
11924959Sedward }
120