xref: /onnv-gate/usr/src/lib/libcurses/screen/infotocap.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate     Routines to convert terminfo string parameters to termcap form.
44*0Sstevel@tonic-gate     Not all terminfo strings will be, or could be, converted.
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate     The following parameter forms will at least be handled.
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate 	%p1%d			-> %d
49*0Sstevel@tonic-gate 	%p1%2.2d		-> %2
50*0Sstevel@tonic-gate 	%p1%3.3d		-> %3
51*0Sstevel@tonic-gate 	%p1%02d			-> %2
52*0Sstevel@tonic-gate 	%p1%03d			-> %3
53*0Sstevel@tonic-gate 	%p1%c			-> %.
54*0Sstevel@tonic-gate 	%p1%'x'%+%c		-> %+x
55*0Sstevel@tonic-gate 	%i			-> %i
56*0Sstevel@tonic-gate 	%p2 before %p1		-> %r
57*0Sstevel@tonic-gate 	%p1%'x'% > %+%p1%'y'%+%;	-> % > xy
58*0Sstevel@tonic-gate */
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate #include "curses.h"
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate /* externs from libc */
63*0Sstevel@tonic-gate extern char *strcpy();
64*0Sstevel@tonic-gate #if !defined(SYSV) && !defined(USG) && !defined(strchr)
65*0Sstevel@tonic-gate 	/* handle both Sys Vr2 and Vr3 curses */
66*0Sstevel@tonic-gate #define	strchr index
67*0Sstevel@tonic-gate #endif /* SYSV || USG */
68*0Sstevel@tonic-gate extern char *strchr();
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate #define	NULLPTR	((char *) 0)
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate /*
73*0Sstevel@tonic-gate     lookat looks at a string such as "%p1%d" and a pattern such as "%p*%d",
74*0Sstevel@tonic-gate     where '*' is the only wild character. Each place that the star matches,
75*0Sstevel@tonic-gate     the corresponding character in the string is placed in args. If the
76*0Sstevel@tonic-gate     pattern matches the string, 1 is returned.
77*0Sstevel@tonic-gate */
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate static int
lookat(char * string,char * pattern,char * args)80*0Sstevel@tonic-gate lookat(char *string, char *pattern, char *args)
81*0Sstevel@tonic-gate {
82*0Sstevel@tonic-gate 	int val, pat;
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	while ((pat = *pattern++) && (val = *string++))
85*0Sstevel@tonic-gate 		if (pat == '*')
86*0Sstevel@tonic-gate 			*args++ = val;
87*0Sstevel@tonic-gate 		else if (val != pat)
88*0Sstevel@tonic-gate 			return (0);
89*0Sstevel@tonic-gate 	if (pat == '\0')
90*0Sstevel@tonic-gate 		return (1);
91*0Sstevel@tonic-gate 	else
92*0Sstevel@tonic-gate 		return (0);
93*0Sstevel@tonic-gate }
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate static int currentparm, otherparm, reversedparms;
96*0Sstevel@tonic-gate static char *newvalue;
97*0Sstevel@tonic-gate static char _newvalue[1024] = "!!! MUST CHANGE BY HAND !!!";
98*0Sstevel@tonic-gate #define	BYHANDMSGLEN 27
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate static void setparms();
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate /*
103*0Sstevel@tonic-gate     Setparms() and checkparms() are used by infotocap() to make
104*0Sstevel@tonic-gate     sure that the parameters in the terminfo entry alternate and are
105*0Sstevel@tonic-gate     only '1' or '2'. If the order has been reversed, then %r is added
106*0Sstevel@tonic-gate     in to the new value being built.
107*0Sstevel@tonic-gate  */
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate static void
setparms()110*0Sstevel@tonic-gate setparms()
111*0Sstevel@tonic-gate {
112*0Sstevel@tonic-gate 	currentparm = 1;
113*0Sstevel@tonic-gate 	otherparm = 2;
114*0Sstevel@tonic-gate 	reversedparms = 0;
115*0Sstevel@tonic-gate 	newvalue = &_newvalue[BYHANDMSGLEN];
116*0Sstevel@tonic-gate 	return;
117*0Sstevel@tonic-gate }
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate static int
checkparms(int arg)120*0Sstevel@tonic-gate checkparms(int arg)
121*0Sstevel@tonic-gate {
122*0Sstevel@tonic-gate 	arg -= '0';
123*0Sstevel@tonic-gate 	if (arg != 1 && arg != 2)
124*0Sstevel@tonic-gate 		return (1);
125*0Sstevel@tonic-gate 	else if (arg != currentparm)
126*0Sstevel@tonic-gate 		if (reversedparms)
127*0Sstevel@tonic-gate 			return (1);
128*0Sstevel@tonic-gate 		else if (!reversedparms && arg == otherparm) {
129*0Sstevel@tonic-gate 			(void) strcpy(newvalue, "%r");
130*0Sstevel@tonic-gate 			newvalue += 2;
131*0Sstevel@tonic-gate 			reversedparms = TRUE;
132*0Sstevel@tonic-gate 		} else
133*0Sstevel@tonic-gate 			return (1);
134*0Sstevel@tonic-gate 	else {
135*0Sstevel@tonic-gate 		otherparm = currentparm;
136*0Sstevel@tonic-gate 		currentparm = 3 - currentparm;
137*0Sstevel@tonic-gate 	}
138*0Sstevel@tonic-gate 	return (0);
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate /*
142*0Sstevel@tonic-gate     Infotocap looks at the string capability to see if it has any
143*0Sstevel@tonic-gate     stack manipulation operators. If possible, they are converted to
144*0Sstevel@tonic-gate     termcap form. If any operator is found that cannot be modified,
145*0Sstevel@tonic-gate     prepend a message to the beginning of the original value and
146*0Sstevel@tonic-gate     set err to 1. A pointer to the new copy of the string is returned
147*0Sstevel@tonic-gate     in either case.
148*0Sstevel@tonic-gate */
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate char
infotocap(char * value,int * err)151*0Sstevel@tonic-gate *infotocap(char *value, int *err)
152*0Sstevel@tonic-gate {
153*0Sstevel@tonic-gate 	char args[4];
154*0Sstevel@tonic-gate 	char *savevalue;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	*err = 0;
157*0Sstevel@tonic-gate 	if (strchr(value, '%') == NULLPTR)
158*0Sstevel@tonic-gate 		return (value);
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	setparms();
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 	savevalue = value;
163*0Sstevel@tonic-gate 	while (*value)
164*0Sstevel@tonic-gate 		if (*value != '%')
165*0Sstevel@tonic-gate 			*newvalue++ = *value++;
166*0Sstevel@tonic-gate 		else if (lookat(value, "%p*%d", args)) {
167*0Sstevel@tonic-gate 			if (checkparms(args[0]))
168*0Sstevel@tonic-gate 				goto dobyhand;
169*0Sstevel@tonic-gate 			(void) strcpy(newvalue, "%d");
170*0Sstevel@tonic-gate 			newvalue += 2;
171*0Sstevel@tonic-gate 			value += 5;
172*0Sstevel@tonic-gate 		} else if (lookat(value, "%p*%02d", args)) {
173*0Sstevel@tonic-gate 			if (checkparms(args[0]))
174*0Sstevel@tonic-gate 				goto dobyhand;
175*0Sstevel@tonic-gate 			(void) strcpy(newvalue, "%2");
176*0Sstevel@tonic-gate 			newvalue += 2;
177*0Sstevel@tonic-gate 			value += 7;
178*0Sstevel@tonic-gate 		} else if (lookat(value, "%p*%03d", args)) {
179*0Sstevel@tonic-gate 			if (checkparms(args[0]))
180*0Sstevel@tonic-gate 				goto dobyhand;
181*0Sstevel@tonic-gate 			(void) strcpy(newvalue, "%3");
182*0Sstevel@tonic-gate 			newvalue += 2;
183*0Sstevel@tonic-gate 			value += 7;
184*0Sstevel@tonic-gate 		} else if (lookat(value, "%p*%2.2d", args)) {
185*0Sstevel@tonic-gate 			if (checkparms(args[0]))
186*0Sstevel@tonic-gate 				goto dobyhand;
187*0Sstevel@tonic-gate 			(void) strcpy(newvalue, "%2");
188*0Sstevel@tonic-gate 			newvalue += 2;
189*0Sstevel@tonic-gate 			value += 8;
190*0Sstevel@tonic-gate 		} else if (lookat(value, "%p*%3.3d", args)) {
191*0Sstevel@tonic-gate 			if (checkparms(args[0]))
192*0Sstevel@tonic-gate 				goto dobyhand;
193*0Sstevel@tonic-gate 			(void) strcpy(newvalue, "%3");
194*0Sstevel@tonic-gate 			newvalue += 2;
195*0Sstevel@tonic-gate 			value += 8;
196*0Sstevel@tonic-gate 		} else if (lookat(value, "%p*%c", args)) {
197*0Sstevel@tonic-gate 			if (checkparms(args[0]))
198*0Sstevel@tonic-gate 				goto dobyhand;
199*0Sstevel@tonic-gate 			(void) strcpy(newvalue, "%.");
200*0Sstevel@tonic-gate 			newvalue += 2;
201*0Sstevel@tonic-gate 			value += 5;
202*0Sstevel@tonic-gate 		} else if (lookat(value, "%p*%'*'%+%c", args)) {
203*0Sstevel@tonic-gate 			if (checkparms(args[0]))
204*0Sstevel@tonic-gate 				goto dobyhand;
205*0Sstevel@tonic-gate 			(void) sprintf(newvalue, "%%+%c", args[1]);
206*0Sstevel@tonic-gate 			newvalue += 3;
207*0Sstevel@tonic-gate 			value += 11;
208*0Sstevel@tonic-gate 		} else if (lookat(value, "%i", args)) {
209*0Sstevel@tonic-gate 			(void) strcpy(newvalue, "%i");
210*0Sstevel@tonic-gate 			newvalue += 2;
211*0Sstevel@tonic-gate 			value += 2;
212*0Sstevel@tonic-gate 		} else if (lookat(value, "%%", args)) {
213*0Sstevel@tonic-gate 			(void) strcpy(newvalue, "%%");
214*0Sstevel@tonic-gate 			newvalue += 2;
215*0Sstevel@tonic-gate 			value += 2;
216*0Sstevel@tonic-gate 		} else if (lookat(value, "p*%'*'%>%+%p*%'*'%+%;", args)) {
217*0Sstevel@tonic-gate 			if (args[0] != args[2])
218*0Sstevel@tonic-gate 				goto dobyhand;
219*0Sstevel@tonic-gate 			if (checkparms(args[0]))
220*0Sstevel@tonic-gate 				goto dobyhand;
221*0Sstevel@tonic-gate 			(void) sprintf(newvalue, "%%>%c%c", args[1], args[3]);
222*0Sstevel@tonic-gate 			newvalue += 2;
223*0Sstevel@tonic-gate 			value += 21;
224*0Sstevel@tonic-gate 		} else
225*0Sstevel@tonic-gate 			goto dobyhand;
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	*newvalue = '\0';
228*0Sstevel@tonic-gate 	return (&_newvalue[BYHANDMSGLEN]);
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate dobyhand:
231*0Sstevel@tonic-gate 	(void) strcpy(&_newvalue[BYHANDMSGLEN], savevalue);
232*0Sstevel@tonic-gate 	*err = 1;
233*0Sstevel@tonic-gate 	return (_newvalue);
234*0Sstevel@tonic-gate }
235