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