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 1997 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 /*LINTLIBRARY*/
41 
42 #include <sys/types.h>
43 #include <stdio.h>
44 
45 /*
46  *	Convert a character, making appropriate changes to make it printable
47  *	for a terminfo source entry. Change escape to \E, tab to \t, backspace
48  *	to \b, formfeed to \f, newline to \n, and return to \r. Change other
49  *	control characters into ^X notation. Change meta characters into octal
50  *	(\nnn) notation. Also place a backslash in front of commas,
51  *	carets(^), and backslashes(\). Return the number of characters printed.
52  */
53 
54 #define	BACKSLASH	'\\'
55 #define	BACKBACK	"\\\\"
56 
57 static char retbuffer[1024];
58 
59 /*
60  *  Expand a string taking terminfo sequences into consideration.
61  */
62 char
iexpand(char * string)63 *iexpand(char *string)
64 {
65 	int	c;
66 	char	*retptr = retbuffer;
67 
68 	retbuffer[0] = '\0';
69 	while ((c = *string++) != 0) {
70 		/* should check here to make sure that there is enough room */
71 		/* in retbuffer and realloc it if necessary. */
72 		c &= 0377;
73 		/* we ignore the return value from sprintf because BSD/V7 */
74 		/* systems return a (char *) rather than an int. */
75 		if (c >= 0200) {
76 			(void) sprintf(retptr, "\\%.3o", c); retptr += 4; }
77 		else if (c == '\033') {
78 			(void) sprintf(retptr, "\\E"); retptr += 2; }
79 		else if (c == '\t') {
80 			(void) sprintf(retptr, "\\t"); retptr += 2; }
81 		else if (c == '\b') {
82 			(void) sprintf(retptr, "\\b"); retptr += 2; }
83 		else if (c == '\f') {
84 			(void) sprintf(retptr, "\\f"); retptr += 2; }
85 		else if (c == '\n') {
86 			(void) sprintf(retptr, "\\n"); retptr += 2; }
87 		else if (c == '\r') {
88 			(void) sprintf(retptr, "\\r"); retptr += 2; }
89 		else if (c == ',') {
90 			(void) sprintf(retptr, "\\,"); retptr += 2; }
91 		else if (c == '^') {
92 			(void) sprintf(retptr, "\\^"); retptr += 2; }
93 		else if (c == BACKSLASH) {
94 			(void) sprintf(retptr, BACKBACK); retptr += 2; }
95 		else if (c == ' ') {
96 			(void) sprintf(retptr, "\\s"); retptr += 2; }
97 		else if (c < ' ' || c == 0177) {
98 			(void) sprintf(retptr, "^%c", c ^ 0100); retptr += 2; }
99 		else {
100 			(void) sprintf(retptr, "%c", c); retptr++; }
101 	}
102 	*retptr = '\0';
103 	return (retbuffer);
104 }
105 
106 /*
107  *  Print out a string onto a stream, changing unprintables into
108  *  terminfo printables.
109  */
110 void
tpr(FILE * stream,char * string)111 tpr(FILE *stream, char *string)
112 {
113 	if (string != NULL)
114 		(void) fprintf(stream, "%s", iexpand(string));
115 }
116