xref: /titanic_41/usr/src/lib/libcurses/screen/infotocap.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 2004 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 /*
43*7c478bd9Sstevel@tonic-gate     Routines to convert terminfo string parameters to termcap form.
44*7c478bd9Sstevel@tonic-gate     Not all terminfo strings will be, or could be, converted.
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate     The following parameter forms will at least be handled.
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate 	%p1%d			-> %d
49*7c478bd9Sstevel@tonic-gate 	%p1%2.2d		-> %2
50*7c478bd9Sstevel@tonic-gate 	%p1%3.3d		-> %3
51*7c478bd9Sstevel@tonic-gate 	%p1%02d			-> %2
52*7c478bd9Sstevel@tonic-gate 	%p1%03d			-> %3
53*7c478bd9Sstevel@tonic-gate 	%p1%c			-> %.
54*7c478bd9Sstevel@tonic-gate 	%p1%'x'%+%c		-> %+x
55*7c478bd9Sstevel@tonic-gate 	%i			-> %i
56*7c478bd9Sstevel@tonic-gate 	%p2 before %p1		-> %r
57*7c478bd9Sstevel@tonic-gate 	%p1%'x'% > %+%p1%'y'%+%;	-> % > xy
58*7c478bd9Sstevel@tonic-gate */
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate #include "curses.h"
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate /* externs from libc */
63*7c478bd9Sstevel@tonic-gate extern char *strcpy();
64*7c478bd9Sstevel@tonic-gate #if !defined(SYSV) && !defined(USG) && !defined(strchr)
65*7c478bd9Sstevel@tonic-gate 	/* handle both Sys Vr2 and Vr3 curses */
66*7c478bd9Sstevel@tonic-gate #define	strchr index
67*7c478bd9Sstevel@tonic-gate #endif /* SYSV || USG */
68*7c478bd9Sstevel@tonic-gate extern char *strchr();
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate #define	NULLPTR	((char *) 0)
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate /*
73*7c478bd9Sstevel@tonic-gate     lookat looks at a string such as "%p1%d" and a pattern such as "%p*%d",
74*7c478bd9Sstevel@tonic-gate     where '*' is the only wild character. Each place that the star matches,
75*7c478bd9Sstevel@tonic-gate     the corresponding character in the string is placed in args. If the
76*7c478bd9Sstevel@tonic-gate     pattern matches the string, 1 is returned.
77*7c478bd9Sstevel@tonic-gate */
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate static int
lookat(char * string,char * pattern,char * args)80*7c478bd9Sstevel@tonic-gate lookat(char *string, char *pattern, char *args)
81*7c478bd9Sstevel@tonic-gate {
82*7c478bd9Sstevel@tonic-gate 	int val, pat;
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	while ((pat = *pattern++) && (val = *string++))
85*7c478bd9Sstevel@tonic-gate 		if (pat == '*')
86*7c478bd9Sstevel@tonic-gate 			*args++ = val;
87*7c478bd9Sstevel@tonic-gate 		else if (val != pat)
88*7c478bd9Sstevel@tonic-gate 			return (0);
89*7c478bd9Sstevel@tonic-gate 	if (pat == '\0')
90*7c478bd9Sstevel@tonic-gate 		return (1);
91*7c478bd9Sstevel@tonic-gate 	else
92*7c478bd9Sstevel@tonic-gate 		return (0);
93*7c478bd9Sstevel@tonic-gate }
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate static int currentparm, otherparm, reversedparms;
96*7c478bd9Sstevel@tonic-gate static char *newvalue;
97*7c478bd9Sstevel@tonic-gate static char _newvalue[1024] = "!!! MUST CHANGE BY HAND !!!";
98*7c478bd9Sstevel@tonic-gate #define	BYHANDMSGLEN 27
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate static void setparms();
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate /*
103*7c478bd9Sstevel@tonic-gate     Setparms() and checkparms() are used by infotocap() to make
104*7c478bd9Sstevel@tonic-gate     sure that the parameters in the terminfo entry alternate and are
105*7c478bd9Sstevel@tonic-gate     only '1' or '2'. If the order has been reversed, then %r is added
106*7c478bd9Sstevel@tonic-gate     in to the new value being built.
107*7c478bd9Sstevel@tonic-gate  */
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate static void
setparms()110*7c478bd9Sstevel@tonic-gate setparms()
111*7c478bd9Sstevel@tonic-gate {
112*7c478bd9Sstevel@tonic-gate 	currentparm = 1;
113*7c478bd9Sstevel@tonic-gate 	otherparm = 2;
114*7c478bd9Sstevel@tonic-gate 	reversedparms = 0;
115*7c478bd9Sstevel@tonic-gate 	newvalue = &_newvalue[BYHANDMSGLEN];
116*7c478bd9Sstevel@tonic-gate 	return;
117*7c478bd9Sstevel@tonic-gate }
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate static int
checkparms(int arg)120*7c478bd9Sstevel@tonic-gate checkparms(int arg)
121*7c478bd9Sstevel@tonic-gate {
122*7c478bd9Sstevel@tonic-gate 	arg -= '0';
123*7c478bd9Sstevel@tonic-gate 	if (arg != 1 && arg != 2)
124*7c478bd9Sstevel@tonic-gate 		return (1);
125*7c478bd9Sstevel@tonic-gate 	else if (arg != currentparm)
126*7c478bd9Sstevel@tonic-gate 		if (reversedparms)
127*7c478bd9Sstevel@tonic-gate 			return (1);
128*7c478bd9Sstevel@tonic-gate 		else if (!reversedparms && arg == otherparm) {
129*7c478bd9Sstevel@tonic-gate 			(void) strcpy(newvalue, "%r");
130*7c478bd9Sstevel@tonic-gate 			newvalue += 2;
131*7c478bd9Sstevel@tonic-gate 			reversedparms = TRUE;
132*7c478bd9Sstevel@tonic-gate 		} else
133*7c478bd9Sstevel@tonic-gate 			return (1);
134*7c478bd9Sstevel@tonic-gate 	else {
135*7c478bd9Sstevel@tonic-gate 		otherparm = currentparm;
136*7c478bd9Sstevel@tonic-gate 		currentparm = 3 - currentparm;
137*7c478bd9Sstevel@tonic-gate 	}
138*7c478bd9Sstevel@tonic-gate 	return (0);
139*7c478bd9Sstevel@tonic-gate }
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate /*
142*7c478bd9Sstevel@tonic-gate     Infotocap looks at the string capability to see if it has any
143*7c478bd9Sstevel@tonic-gate     stack manipulation operators. If possible, they are converted to
144*7c478bd9Sstevel@tonic-gate     termcap form. If any operator is found that cannot be modified,
145*7c478bd9Sstevel@tonic-gate     prepend a message to the beginning of the original value and
146*7c478bd9Sstevel@tonic-gate     set err to 1. A pointer to the new copy of the string is returned
147*7c478bd9Sstevel@tonic-gate     in either case.
148*7c478bd9Sstevel@tonic-gate */
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate char
infotocap(char * value,int * err)151*7c478bd9Sstevel@tonic-gate *infotocap(char *value, int *err)
152*7c478bd9Sstevel@tonic-gate {
153*7c478bd9Sstevel@tonic-gate 	char args[4];
154*7c478bd9Sstevel@tonic-gate 	char *savevalue;
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	*err = 0;
157*7c478bd9Sstevel@tonic-gate 	if (strchr(value, '%') == NULLPTR)
158*7c478bd9Sstevel@tonic-gate 		return (value);
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	setparms();
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	savevalue = value;
163*7c478bd9Sstevel@tonic-gate 	while (*value)
164*7c478bd9Sstevel@tonic-gate 		if (*value != '%')
165*7c478bd9Sstevel@tonic-gate 			*newvalue++ = *value++;
166*7c478bd9Sstevel@tonic-gate 		else if (lookat(value, "%p*%d", args)) {
167*7c478bd9Sstevel@tonic-gate 			if (checkparms(args[0]))
168*7c478bd9Sstevel@tonic-gate 				goto dobyhand;
169*7c478bd9Sstevel@tonic-gate 			(void) strcpy(newvalue, "%d");
170*7c478bd9Sstevel@tonic-gate 			newvalue += 2;
171*7c478bd9Sstevel@tonic-gate 			value += 5;
172*7c478bd9Sstevel@tonic-gate 		} else if (lookat(value, "%p*%02d", args)) {
173*7c478bd9Sstevel@tonic-gate 			if (checkparms(args[0]))
174*7c478bd9Sstevel@tonic-gate 				goto dobyhand;
175*7c478bd9Sstevel@tonic-gate 			(void) strcpy(newvalue, "%2");
176*7c478bd9Sstevel@tonic-gate 			newvalue += 2;
177*7c478bd9Sstevel@tonic-gate 			value += 7;
178*7c478bd9Sstevel@tonic-gate 		} else if (lookat(value, "%p*%03d", args)) {
179*7c478bd9Sstevel@tonic-gate 			if (checkparms(args[0]))
180*7c478bd9Sstevel@tonic-gate 				goto dobyhand;
181*7c478bd9Sstevel@tonic-gate 			(void) strcpy(newvalue, "%3");
182*7c478bd9Sstevel@tonic-gate 			newvalue += 2;
183*7c478bd9Sstevel@tonic-gate 			value += 7;
184*7c478bd9Sstevel@tonic-gate 		} else if (lookat(value, "%p*%2.2d", args)) {
185*7c478bd9Sstevel@tonic-gate 			if (checkparms(args[0]))
186*7c478bd9Sstevel@tonic-gate 				goto dobyhand;
187*7c478bd9Sstevel@tonic-gate 			(void) strcpy(newvalue, "%2");
188*7c478bd9Sstevel@tonic-gate 			newvalue += 2;
189*7c478bd9Sstevel@tonic-gate 			value += 8;
190*7c478bd9Sstevel@tonic-gate 		} else if (lookat(value, "%p*%3.3d", args)) {
191*7c478bd9Sstevel@tonic-gate 			if (checkparms(args[0]))
192*7c478bd9Sstevel@tonic-gate 				goto dobyhand;
193*7c478bd9Sstevel@tonic-gate 			(void) strcpy(newvalue, "%3");
194*7c478bd9Sstevel@tonic-gate 			newvalue += 2;
195*7c478bd9Sstevel@tonic-gate 			value += 8;
196*7c478bd9Sstevel@tonic-gate 		} else if (lookat(value, "%p*%c", args)) {
197*7c478bd9Sstevel@tonic-gate 			if (checkparms(args[0]))
198*7c478bd9Sstevel@tonic-gate 				goto dobyhand;
199*7c478bd9Sstevel@tonic-gate 			(void) strcpy(newvalue, "%.");
200*7c478bd9Sstevel@tonic-gate 			newvalue += 2;
201*7c478bd9Sstevel@tonic-gate 			value += 5;
202*7c478bd9Sstevel@tonic-gate 		} else if (lookat(value, "%p*%'*'%+%c", args)) {
203*7c478bd9Sstevel@tonic-gate 			if (checkparms(args[0]))
204*7c478bd9Sstevel@tonic-gate 				goto dobyhand;
205*7c478bd9Sstevel@tonic-gate 			(void) sprintf(newvalue, "%%+%c", args[1]);
206*7c478bd9Sstevel@tonic-gate 			newvalue += 3;
207*7c478bd9Sstevel@tonic-gate 			value += 11;
208*7c478bd9Sstevel@tonic-gate 		} else if (lookat(value, "%i", args)) {
209*7c478bd9Sstevel@tonic-gate 			(void) strcpy(newvalue, "%i");
210*7c478bd9Sstevel@tonic-gate 			newvalue += 2;
211*7c478bd9Sstevel@tonic-gate 			value += 2;
212*7c478bd9Sstevel@tonic-gate 		} else if (lookat(value, "%%", args)) {
213*7c478bd9Sstevel@tonic-gate 			(void) strcpy(newvalue, "%%");
214*7c478bd9Sstevel@tonic-gate 			newvalue += 2;
215*7c478bd9Sstevel@tonic-gate 			value += 2;
216*7c478bd9Sstevel@tonic-gate 		} else if (lookat(value, "p*%'*'%>%+%p*%'*'%+%;", args)) {
217*7c478bd9Sstevel@tonic-gate 			if (args[0] != args[2])
218*7c478bd9Sstevel@tonic-gate 				goto dobyhand;
219*7c478bd9Sstevel@tonic-gate 			if (checkparms(args[0]))
220*7c478bd9Sstevel@tonic-gate 				goto dobyhand;
221*7c478bd9Sstevel@tonic-gate 			(void) sprintf(newvalue, "%%>%c%c", args[1], args[3]);
222*7c478bd9Sstevel@tonic-gate 			newvalue += 2;
223*7c478bd9Sstevel@tonic-gate 			value += 21;
224*7c478bd9Sstevel@tonic-gate 		} else
225*7c478bd9Sstevel@tonic-gate 			goto dobyhand;
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 	*newvalue = '\0';
228*7c478bd9Sstevel@tonic-gate 	return (&_newvalue[BYHANDMSGLEN]);
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate dobyhand:
231*7c478bd9Sstevel@tonic-gate 	(void) strcpy(&_newvalue[BYHANDMSGLEN], savevalue);
232*7c478bd9Sstevel@tonic-gate 	*err = 1;
233*7c478bd9Sstevel@tonic-gate 	return (_newvalue);
234*7c478bd9Sstevel@tonic-gate }
235