xref: /original-bsd/lib/libterm/tputs.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)tputs.c	8.1 (Berkeley) 06/04/93";
10 #endif /* not lint */
11 
12 #include <sgtty.h>
13 #include <ctype.h>
14 
15 /*
16  * The following array gives the number of tens of milliseconds per
17  * character for each speed as returned by gtty.  Thus since 300
18  * baud returns a 7, there are 33.3 milliseconds per char at 300 baud.
19  */
20 static
21 short	tmspc10[] = {
22 	0, 2000, 1333, 909, 743, 666, 500, 333, 166, 83, 55, 41, 20, 10, 5
23 };
24 
25 short	ospeed;
26 char	PC;
27 
28 /*
29  * Put the character string cp out, with padding.
30  * The number of affected lines is affcnt, and the routine
31  * used to output one character is outc.
32  */
33 tputs(cp, affcnt, outc)
34 	register char *cp;
35 	int affcnt;
36 	int (*outc)();
37 {
38 	register int i = 0;
39 	register int mspc10;
40 
41 	if (cp == 0)
42 		return;
43 
44 	/*
45 	 * Convert the number representing the delay.
46 	 */
47 	if (isdigit(*cp)) {
48 		do
49 			i = i * 10 + *cp++ - '0';
50 		while (isdigit(*cp));
51 	}
52 	i *= 10;
53 	if (*cp == '.') {
54 		cp++;
55 		if (isdigit(*cp))
56 			i += *cp - '0';
57 		/*
58 		 * Only one digit to the right of the decimal point.
59 		 */
60 		while (isdigit(*cp))
61 			cp++;
62 	}
63 
64 	/*
65 	 * If the delay is followed by a `*', then
66 	 * multiply by the affected lines count.
67 	 */
68 	if (*cp == '*')
69 		cp++, i *= affcnt;
70 
71 	/*
72 	 * The guts of the string.
73 	 */
74 	while (*cp)
75 		(*outc)(*cp++);
76 
77 	/*
78 	 * If no delay needed, or output speed is
79 	 * not comprehensible, then don't try to delay.
80 	 */
81 	if (i == 0)
82 		return;
83 	if (ospeed <= 0 || ospeed >= (sizeof tmspc10 / sizeof tmspc10[0]))
84 		return;
85 
86 	/*
87 	 * Round up by a half a character frame,
88 	 * and then do the delay.
89 	 * Too bad there are no user program accessible programmed delays.
90 	 * Transmitting pad characters slows many
91 	 * terminals down and also loads the system.
92 	 */
93 	mspc10 = tmspc10[ospeed];
94 	i += mspc10 / 2;
95 	for (i /= mspc10; i > 0; i--)
96 		(*outc)(PC);
97 }
98