xref: /titanic_52/usr/src/lib/libcurses/screen/iexpand.c (revision c7158ae983f5a04c4a998f468ecefba6d23ba721)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*LINTLIBRARY*/
43 
44 #include <sys/types.h>
45 #include <stdio.h>
46 
47 /*
48  *	Convert a character, making appropriate changes to make it printable
49  *	for a terminfo source entry. Change escape to \E, tab to \t, backspace
50  *	to \b, formfeed to \f, newline to \n, and return to \r. Change other
51  *	control characters into ^X notation. Change meta characters into octal
52  *	(\nnn) notation. Also place a backslash in front of commas,
53  *	carets(^), and backslashes(\). Return the number of characters printed.
54  */
55 
56 #define	BACKSLASH	'\\'
57 #define	BACKBACK	"\\\\"
58 
59 static char retbuffer[1024];
60 
61 /*
62  *  Expand a string taking terminfo sequences into consideration.
63  */
64 char
65 *iexpand(char *string)
66 {
67 	int	c;
68 	char	*retptr = retbuffer;
69 
70 	retbuffer[0] = '\0';
71 	while ((c = *string++) != 0) {
72 		/* should check here to make sure that there is enough room */
73 		/* in retbuffer and realloc it if necessary. */
74 		c &= 0377;
75 		/* we ignore the return value from sprintf because BSD/V7 */
76 		/* systems return a (char *) rather than an int. */
77 		if (c >= 0200) {
78 			(void) sprintf(retptr, "\\%.3o", c); retptr += 4; }
79 		else if (c == '\033') {
80 			(void) sprintf(retptr, "\\E"); retptr += 2; }
81 		else if (c == '\t') {
82 			(void) sprintf(retptr, "\\t"); retptr += 2; }
83 		else if (c == '\b') {
84 			(void) sprintf(retptr, "\\b"); retptr += 2; }
85 		else if (c == '\f') {
86 			(void) sprintf(retptr, "\\f"); retptr += 2; }
87 		else if (c == '\n') {
88 			(void) sprintf(retptr, "\\n"); retptr += 2; }
89 		else if (c == '\r') {
90 			(void) sprintf(retptr, "\\r"); retptr += 2; }
91 		else if (c == ',') {
92 			(void) sprintf(retptr, "\\,"); retptr += 2; }
93 		else if (c == '^') {
94 			(void) sprintf(retptr, "\\^"); retptr += 2; }
95 		else if (c == BACKSLASH) {
96 			(void) sprintf(retptr, BACKBACK); retptr += 2; }
97 		else if (c == ' ') {
98 			(void) sprintf(retptr, "\\s"); retptr += 2; }
99 		else if (c < ' ' || c == 0177) {
100 			(void) sprintf(retptr, "^%c", c ^ 0100); retptr += 2; }
101 		else {
102 			(void) sprintf(retptr, "%c", c); retptr++; }
103 	}
104 	*retptr = '\0';
105 	return (retbuffer);
106 }
107 
108 /*
109  *  Print out a string onto a stream, changing unprintables into
110  *  terminfo printables.
111  */
112 void
113 tpr(FILE *stream, char *string)
114 {
115 	if (string != NULL)
116 		(void) fprintf(stream, "%s", iexpand(string));
117 }
118