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