xref: /illumos-gate/usr/src/lib/libxcurses2/src/libc/xcurses/tputs.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1995-1998 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /* LINTLIBRARY */
30 
31 /*
32  * tputs.c
33  *
34  * XCurses Library
35  *
36  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
37  *
38  */
39 
40 #ifdef M_RCSID
41 #ifndef lint
42 static char rcsID[] =
43 "$Header: /team/ps/sun_xcurses/archive/local_changes/xcurses/src/lib/"
44 "libxcurses/src/libc/xcurses/rcs/tputs.c 1.4 1998/06/03 12:57:02 "
45 "cbates Exp $";
46 #endif
47 #endif
48 
49 #include <private.h>
50 #include <ctype.h>
51 #include <string.h>
52 #include <stdlib.h>
53 
54 int
55 __m_putchar(int byte)
56 {
57 	return (putchar(byte));
58 }
59 
60 #undef putp
61 
62 int
63 putp(const char *s)
64 {
65 	int	code;
66 
67 	code = tputs(s, 1, __m_putchar);
68 
69 	return (code);
70 }
71 
72 int
73 tputs(const char *string, int affcnt, int (*putout)(int))
74 {
75 	(void) __m_tputs(string, affcnt, putout);
76 
77 	return (OK);
78 }
79 
80 /*
81  * Apply padding information to a string and write it out.
82  * Note the '/' option is not supported.
83  */
84 int
85 __m_tputs(const char *string, int affcnt, int (*putout)(int))
86 {
87 	char	*mark;
88 	int	i, baud, len, null, number;
89 
90 	baud = baudrate();
91 	null = pad_char == NULL ? '\0' : pad_char[0];
92 
93 	for (len = 0; *string; ++string) {
94 		/* Look for "$<num.????>" */
95 		if (*string == '$' && string[1] == '<' &&
96 			(isdigit(string[2]) || string[2] == '.') &&
97 			(mark = strchr(string, '>'))) {
98 			number = atoi(string + 2) * 10;
99 			if ((string = strchr(string, '.')) != NULL)
100 				number += *++string - '0';
101 			string = mark;
102 			if (*--mark == '*')
103 				number *= affcnt;
104 #ifdef	BREAKS_ftputs_ftputs1_2
105 			if (padding_baud_rate && baud >= padding_baud_rate &&
106 				!xon_xoff) {
107 #else	/* BREAKS_ftputs_ftputs1_2 */
108 			if (baud >= padding_baud_rate) {
109 #endif	/* BREAKS_ftputs_ftputs1_2 */
110 				number = (baud / 8 * number) / 10000;
111 				len += number;
112 				if (putout != (int (*)(int)) NULL) {
113 					for (i = 0; i < number; i++)
114 						(void) (*putout)(null);
115 				}
116 			}
117 		} else {
118 			++len;
119 			if (putout != (int (*)(int)) NULL)
120 				(void) (*putout)(*string);
121 		}
122 	}
123 
124 	return (len);
125 }
126