xref: /titanic_41/usr/src/lib/libcurses/screen/cexpand.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  * contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include <stdio.h>
43*7c478bd9Sstevel@tonic-gate #include <ctype.h>
44*7c478bd9Sstevel@tonic-gate #include <string.h>
45*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
46*7c478bd9Sstevel@tonic-gate #include "print.h"
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #define	BACKSLASH	'\\'
49*7c478bd9Sstevel@tonic-gate #define	BACKBACK	"\\\\"
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate extern char *strcat();
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate static char retbuffer[1024];
54*7c478bd9Sstevel@tonic-gate static char ret2buffer[1024];
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate /*
57*7c478bd9Sstevel@tonic-gate  *  Remove the padding sequences from the input string.
58*7c478bd9Sstevel@tonic-gate  *  Return the new string without the padding sequences
59*7c478bd9Sstevel@tonic-gate  *  and the padding itself in padbuffer.
60*7c478bd9Sstevel@tonic-gate  */
61*7c478bd9Sstevel@tonic-gate char
rmpadding(char * str,char * padbuffer,int * padding)62*7c478bd9Sstevel@tonic-gate *rmpadding(char *str, char *padbuffer, int *padding)
63*7c478bd9Sstevel@tonic-gate {
64*7c478bd9Sstevel@tonic-gate 	static char rmbuffer[1024];
65*7c478bd9Sstevel@tonic-gate 	register char ch;
66*7c478bd9Sstevel@tonic-gate 	register char *pbufptr;
67*7c478bd9Sstevel@tonic-gate 	register char *retptr = rmbuffer;
68*7c478bd9Sstevel@tonic-gate 	char *svbufptr;
69*7c478bd9Sstevel@tonic-gate 	int padbylines = 0;
70*7c478bd9Sstevel@tonic-gate 	int paddigits = 0;
71*7c478bd9Sstevel@tonic-gate 	int paddecimal = 0;
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	padbuffer[0] = rmbuffer[0] = '\0';
74*7c478bd9Sstevel@tonic-gate 	if (padding)
75*7c478bd9Sstevel@tonic-gate 		*padding = 0;
76*7c478bd9Sstevel@tonic-gate 	if (str == NULL)
77*7c478bd9Sstevel@tonic-gate 		return (rmbuffer);
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	while (ch = (*str++ & 0377))
80*7c478bd9Sstevel@tonic-gate 		switch (ch) {
81*7c478bd9Sstevel@tonic-gate 			case '$':
82*7c478bd9Sstevel@tonic-gate 				if (*str == '<') {
83*7c478bd9Sstevel@tonic-gate 					svbufptr = ++str;	/* skip '<' */
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 					/* copy number */
86*7c478bd9Sstevel@tonic-gate 					pbufptr = padbuffer;
87*7c478bd9Sstevel@tonic-gate 					for (; *str && isdigit(*str); str++) {
88*7c478bd9Sstevel@tonic-gate 						*svbufptr++ = *str;
89*7c478bd9Sstevel@tonic-gate 						*pbufptr++ = *str;
90*7c478bd9Sstevel@tonic-gate 					}
91*7c478bd9Sstevel@tonic-gate 					*pbufptr = '\0';
92*7c478bd9Sstevel@tonic-gate 					paddigits += atoi(padbuffer);
93*7c478bd9Sstevel@tonic-gate 					/* check for decimal */
94*7c478bd9Sstevel@tonic-gate 					if (*str == '.') {
95*7c478bd9Sstevel@tonic-gate 						str++;
96*7c478bd9Sstevel@tonic-gate 						pbufptr = padbuffer;
97*7c478bd9Sstevel@tonic-gate 						for (; *str && isdigit(ch);
98*7c478bd9Sstevel@tonic-gate 						    str++) {
99*7c478bd9Sstevel@tonic-gate 							*svbufptr++ = *str;
100*7c478bd9Sstevel@tonic-gate 							*pbufptr++ = *str;
101*7c478bd9Sstevel@tonic-gate 						}
102*7c478bd9Sstevel@tonic-gate 						*pbufptr = '\0';
103*7c478bd9Sstevel@tonic-gate 						paddecimal += atoi(padbuffer);
104*7c478bd9Sstevel@tonic-gate 					}
105*7c478bd9Sstevel@tonic-gate 					for (; (*str == '*') || (*str == '/');
106*7c478bd9Sstevel@tonic-gate 					    str++) {
107*7c478bd9Sstevel@tonic-gate 						if (*str == '*')
108*7c478bd9Sstevel@tonic-gate 							padbylines = 1;
109*7c478bd9Sstevel@tonic-gate 			/* Termcap does not support mandatory padding */
110*7c478bd9Sstevel@tonic-gate 			/* marked with /. Just remove it. */
111*7c478bd9Sstevel@tonic-gate 						else {
112*7c478bd9Sstevel@tonic-gate 							extern char *progname;
113*7c478bd9Sstevel@tonic-gate 							(void) fprintf(stderr,
114*7c478bd9Sstevel@tonic-gate 							    "%s: mandatory "
115*7c478bd9Sstevel@tonic-gate 							    "padding removed\n",
116*7c478bd9Sstevel@tonic-gate 							    progname);
117*7c478bd9Sstevel@tonic-gate 						}
118*7c478bd9Sstevel@tonic-gate 					}
119*7c478bd9Sstevel@tonic-gate 			/* oops, not a padding spec after all */
120*7c478bd9Sstevel@tonic-gate 			/* put us back after the '$<' */
121*7c478bd9Sstevel@tonic-gate 					if (*str != '>') {
122*7c478bd9Sstevel@tonic-gate 						str = svbufptr;
123*7c478bd9Sstevel@tonic-gate 						*retptr++ = '$';
124*7c478bd9Sstevel@tonic-gate 						*retptr++ = '<';
125*7c478bd9Sstevel@tonic-gate 					} else
126*7c478bd9Sstevel@tonic-gate 						str++;	/* skip the '>' */
127*7c478bd9Sstevel@tonic-gate 			/* Flag padding info that is not at the end */
128*7c478bd9Sstevel@tonic-gate 			/* of the string. */
129*7c478bd9Sstevel@tonic-gate 					if (*str != '\0') {
130*7c478bd9Sstevel@tonic-gate 						extern char *progname;
131*7c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
132*7c478bd9Sstevel@tonic-gate 						    "%s: padding information "
133*7c478bd9Sstevel@tonic-gate 						    "moved to end\n", progname);
134*7c478bd9Sstevel@tonic-gate 					}
135*7c478bd9Sstevel@tonic-gate 				} else
136*7c478bd9Sstevel@tonic-gate 					*retptr++ = ch;
137*7c478bd9Sstevel@tonic-gate 				break;
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 			default:
140*7c478bd9Sstevel@tonic-gate 				*retptr++ = ch;
141*7c478bd9Sstevel@tonic-gate 		}
142*7c478bd9Sstevel@tonic-gate 	*retptr = '\0';
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	if (paddecimal > 10) {
145*7c478bd9Sstevel@tonic-gate 		paddigits += paddecimal / 10;
146*7c478bd9Sstevel@tonic-gate 		paddecimal %= 10;
147*7c478bd9Sstevel@tonic-gate 	}
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	if (paddigits > 0 && paddecimal > 0)
150*7c478bd9Sstevel@tonic-gate 		(void) sprintf(padbuffer, "%d.%d", paddigits, paddecimal);
151*7c478bd9Sstevel@tonic-gate 	else if (paddigits > 0)
152*7c478bd9Sstevel@tonic-gate 		(void) sprintf(padbuffer, "%d", paddigits);
153*7c478bd9Sstevel@tonic-gate 	else if (paddecimal > 0)
154*7c478bd9Sstevel@tonic-gate 		(void) sprintf(padbuffer, ".%d", paddecimal);
155*7c478bd9Sstevel@tonic-gate 	if (padbylines)
156*7c478bd9Sstevel@tonic-gate 		(void) strcat(padbuffer, "*");
157*7c478bd9Sstevel@tonic-gate 	if (padding)
158*7c478bd9Sstevel@tonic-gate 		*padding = paddecimal;
159*7c478bd9Sstevel@tonic-gate 	return (rmbuffer);
160*7c478bd9Sstevel@tonic-gate }
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate /*
163*7c478bd9Sstevel@tonic-gate  *  Convert a character, making appropriate changes to make it printable
164*7c478bd9Sstevel@tonic-gate  *  for a termcap source entry. Change escape, tab, etc., into their
165*7c478bd9Sstevel@tonic-gate  *  appropriate equivalents. Return the number of characters printed.
166*7c478bd9Sstevel@tonic-gate  */
167*7c478bd9Sstevel@tonic-gate char
cconvert(char * string)168*7c478bd9Sstevel@tonic-gate *cconvert(char *string)
169*7c478bd9Sstevel@tonic-gate {
170*7c478bd9Sstevel@tonic-gate 	register int c;
171*7c478bd9Sstevel@tonic-gate 	register char *retptr = retbuffer;
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	retbuffer[0] = '\0';
174*7c478bd9Sstevel@tonic-gate 	if (string == NULL)
175*7c478bd9Sstevel@tonic-gate 		return (retbuffer);
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	while (c = *string++) {
178*7c478bd9Sstevel@tonic-gate 		/* should check here to make sure that there is enough room */
179*7c478bd9Sstevel@tonic-gate 		/* in retbuffer and realloc it if necessary. */
180*7c478bd9Sstevel@tonic-gate 		c &= 0377;
181*7c478bd9Sstevel@tonic-gate 		/* we ignore the return value from sprintf because BSD/V7 */
182*7c478bd9Sstevel@tonic-gate 		/* systems return a (char *) rather than an int. */
183*7c478bd9Sstevel@tonic-gate 		if (c >= 0200) {
184*7c478bd9Sstevel@tonic-gate 			(void) sprintf(retptr, "\\%.3o", c); retptr += 4; }
185*7c478bd9Sstevel@tonic-gate 		else if (c == '\033') {
186*7c478bd9Sstevel@tonic-gate 			(void) sprintf(retptr, "\\E"); retptr += 2; }
187*7c478bd9Sstevel@tonic-gate 		else if (c == '\t') {
188*7c478bd9Sstevel@tonic-gate 			(void) sprintf(retptr, "\\t"); retptr += 2; }
189*7c478bd9Sstevel@tonic-gate 		else if (c == '\b') {
190*7c478bd9Sstevel@tonic-gate 			(void) sprintf(retptr, "\\b"); retptr += 2; }
191*7c478bd9Sstevel@tonic-gate 		else if (c == '\f') {
192*7c478bd9Sstevel@tonic-gate 			(void) sprintf(retptr, "\\f"); retptr += 2; }
193*7c478bd9Sstevel@tonic-gate 		else if (c == '\n') {
194*7c478bd9Sstevel@tonic-gate 			(void) sprintf(retptr, "\\n"); retptr += 2; }
195*7c478bd9Sstevel@tonic-gate 		else if (c == '\r') {
196*7c478bd9Sstevel@tonic-gate 			(void) sprintf(retptr, "\\r"); retptr += 2; }
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 		/* unfortunately \: did not work */
199*7c478bd9Sstevel@tonic-gate 		else if (c == ':') {
200*7c478bd9Sstevel@tonic-gate 			(void) sprintf(retptr, "\\072"); retptr += 4; }
201*7c478bd9Sstevel@tonic-gate 		else if (c == '^') {
202*7c478bd9Sstevel@tonic-gate 			(void) sprintf(retptr, "\\^"); retptr += 2; }
203*7c478bd9Sstevel@tonic-gate 		else if (c == BACKSLASH) {
204*7c478bd9Sstevel@tonic-gate 			(void) sprintf(retptr, BACKBACK); retptr += 2; }
205*7c478bd9Sstevel@tonic-gate 		else if (c < ' ' || c == 0177) {
206*7c478bd9Sstevel@tonic-gate 			(void) sprintf(retptr, "^%c", c ^ 0100); retptr += 2; }
207*7c478bd9Sstevel@tonic-gate 		else {
208*7c478bd9Sstevel@tonic-gate 			(void) sprintf(retptr, "%c", c); retptr++; }
209*7c478bd9Sstevel@tonic-gate 	}
210*7c478bd9Sstevel@tonic-gate 	*retptr = '\0';
211*7c478bd9Sstevel@tonic-gate 	return (retbuffer);
212*7c478bd9Sstevel@tonic-gate }
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate /*
215*7c478bd9Sstevel@tonic-gate  *  Convert the terminfo string into a termcap string.
216*7c478bd9Sstevel@tonic-gate  *  Most of the work is done by rmpadding() above and cconvert(); this
217*7c478bd9Sstevel@tonic-gate  *  function mainly just pieces things back together. A pointer to the
218*7c478bd9Sstevel@tonic-gate  *  return buffer is returned.
219*7c478bd9Sstevel@tonic-gate  *
220*7c478bd9Sstevel@tonic-gate  *  NOTE: Some things can not be done at all: converting the terminfo
221*7c478bd9Sstevel@tonic-gate  *  parameterized strings into termcap parameterized strings.
222*7c478bd9Sstevel@tonic-gate  */
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate char
cexpand(char * str)225*7c478bd9Sstevel@tonic-gate *cexpand(char *str)
226*7c478bd9Sstevel@tonic-gate {
227*7c478bd9Sstevel@tonic-gate 	char padbuffer[512];
228*7c478bd9Sstevel@tonic-gate 	char *retptr;
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 	retptr = rmpadding(str, padbuffer, (int *)0);
231*7c478bd9Sstevel@tonic-gate 	(void) sprintf(ret2buffer, "%s%s", padbuffer, cconvert(retptr));
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 	return (ret2buffer);
234*7c478bd9Sstevel@tonic-gate }
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate /*
237*7c478bd9Sstevel@tonic-gate  *  Print out a string onto a stream, changing unprintables into
238*7c478bd9Sstevel@tonic-gate  *  termcap printables.
239*7c478bd9Sstevel@tonic-gate  */
240*7c478bd9Sstevel@tonic-gate int
cpr(FILE * stream,char * string)241*7c478bd9Sstevel@tonic-gate cpr(FILE *stream, char *string)
242*7c478bd9Sstevel@tonic-gate {
243*7c478bd9Sstevel@tonic-gate 	register char *ret;
244*7c478bd9Sstevel@tonic-gate 	if (string != NULL) {
245*7c478bd9Sstevel@tonic-gate 		ret = cexpand(string);
246*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stream, "%s", ret);
247*7c478bd9Sstevel@tonic-gate 		return (strlen(ret));
248*7c478bd9Sstevel@tonic-gate 	} else
249*7c478bd9Sstevel@tonic-gate 		return (0);
250*7c478bd9Sstevel@tonic-gate }
251