xref: /illumos-gate/usr/src/lib/libxcurses/src/tic/untic.c (revision 7c478bd9)
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 (c) 1996, by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  *	untic.c			CURSES Library
31  *
32  *	Copyright 1990, 1992 by Mortice Kern Systems Inc.  All rights reserved.
33  *
34  *	Portions of this code Copyright 1982 by Pavel Curtis.
35  *
36  */
37 
38 #ifdef M_RCSID
39 #ifndef lint
40 static char rcsID[] = "$Header: /rd/src/tic/rcs/untic.c 1.18 1995/06/22 20:04:01 ant Exp $";
41 #endif
42 #endif
43 
44 #include <mks.h>
45 #include <ctype.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <stdarg.h>
49 #include <term.h>
50 #include <unistd.h>
51 #include <m_ord.h>
52 
53 #ifdef _XOPEN_CURSES
54 /*
55  * MKS XCurses to be conforming has to avoid name space pollution
56  * by using reserved prefixes.  Map the pre-XCurses names to the
57  * new ones.
58  */
59 #define BOOLCOUNT       __COUNT_BOOL
60 #define NUMCOUNT        __COUNT_NUM
61 #define STRCOUNT        __COUNT_STR
62 #define boolnames       __m_boolnames
63 #define boolcodes       __m_boolcodes
64 #define boolfnames      __m_boolfnames
65 #define numnames        __m_numnames
66 #define numcodes        __m_numcodes
67 #define numfnames       __m_numfnames
68 #define strnames        __m_strnames
69 #define strcodes        __m_strcodes
70 #define strfnames       __m_strfnames
71 #define __t_term_header terminfo_header_t
72 #define TERMINFO_MAGIC  __TERMINFO_MAGIC
73 #define Booleans        _bool
74 #define Numbers         _num
75 #define Strings         _str
76 #define term_names	_names
77 #endif
78 
79 extern char *_cmdname;
80 
81 /* Exit Status */
82 #define SUCCESS		0
83 #define NOT_DEFINED	1
84 #define USAGE		2
85 #define BAD_TERMINAL	3
86 #define NOT_VALID	4
87 #define ERROR		5
88 
89 STATIC char *escape ANSI((int));
90 STATIC void error ANSI((char *, ...));		/* GENTEXT: error */
91 STATIC void untic ANSI((TERMINAL *));
92 
93 char **Bool;
94 char **Num;
95 char **Str;
96 
97 char usage[] = m_textstr(3137, "usage:  %s [-CILV] [term_name ...]\n", "U _");
98 char version[] = m_textstr(
99 	3138, "%s - Display compiled terminfo database entry.  Oct 92\n", "I _"
100 );
101 
102 
103 int
main(argc,argv)104 main(argc, argv)
105 int argc;
106 char **argv;
107 {
108 	int err;
109 	char *ap, **av = argv;
110 	setlocale(LC_ALL, "");
111 	_cmdname = *argv;
112 	Bool = boolnames;
113 	Num = numnames;
114 	Str = strnames;
115 	for (--argc, ++argv; 0 < argc && **argv == '-'; --argc, ++argv) {
116 		ap = &argv[0][1];
117 		if (*ap == '-' && ap[1] == '\0') {
118 			--argc;
119 			++argv;
120 			break;
121 		}
122 		while (*ap != '\0') {
123 			switch (*ap++) {
124 			case 'C':
125 				Bool = boolcodes;
126 				Num = numcodes;
127 				Str = strcodes;
128 				break;
129 			case 'I':
130 				Bool = boolnames;
131 				Num = numnames;
132 				Str = strnames;
133 				break;
134 			case 'L':
135 				Bool = boolfnames;
136 				Num = numfnames;
137 				Str = strfnames;
138 				break;
139 			case 'V':
140 				(void) fprintf(
141 					stderr, m_strmsg(version), _cmdname
142 				);
143 				break;
144 			default:
145 				(void) fprintf(
146 					stderr, m_strmsg(usage), _cmdname
147 				);
148 				return (USAGE);
149 			}
150 			break;
151 		}
152 	}
153 	if (argc <= 0) {
154 		if ((ap = getenv("TERM")) == NULL) {
155 			(void) fprintf(stderr, m_strmsg(usage), _cmdname);
156 			return (USAGE);
157 		}
158 		/* Assume that, even if there were no parameters, space
159 		 * for argv[0] (the command name) and argv[1] (NULL) would
160 		 * have been put aside.  We can use this space to fake a
161 		 * a single default parameter.
162 		 */
163 		argc = 1;
164 		argv[0] = ap;
165 		argv[1] = NULL;
166 
167 	}
168 	use_env(0);
169 	for (; 0 < argc; --argc, ++argv) {
170 		(void) setupterm(*argv, STDOUT_FILENO, &err);
171 		switch (err) {
172 		case 1:
173 			untic(cur_term);
174 			(void) del_curterm(cur_term);
175 			break;
176 		case 0:
177 			error(
178 				m_textmsg(202, "Unknown terminal \"%s\".\n", "E term"),
179 				*argv
180 			);
181 			return (BAD_TERMINAL);
182 		case -1:
183 			error(m_textmsg(203, "No terminfo database.\n", "E"));
184 			return (BAD_TERMINAL);
185 		}
186 	}
187 	return (SUCCESS);
188 }
189 
190 /*f
191  *	Dump the contents of a compiled terminfo file into a
192  *	human readable format.
193  */
194 STATIC void
untic(tp)195 untic(tp)
196 TERMINAL *tp;
197 {
198 	int i;
199 	char *p;
200 	(void) printf("%s,\n", tp->term_names);
201 	for (i = 0; i < BOOLCOUNT; ++i) {
202 		if (tp->Booleans[i])
203 			(void) printf("\t%s,\n", Bool[i]);
204 	}
205 	for (i = 0; i < NUMCOUNT; ++i) {
206 		if (tp->Numbers[i] != -1)
207 			(void) printf("\t%s#%d,\n", Num[i],tp->Numbers[i]);
208 	}
209 	for (i = 0; i < STRCOUNT; ++i) {
210 		if (tp->Strings[i] != NULL) {
211 			(void) printf("\t%s=", Str[i]);
212 			for (p = tp->Strings[i]; *p != '\0'; ++p)
213 				(void) fputs(escape(*p), stdout);
214 			(void) fputs(",\n", stdout);
215 		}
216 	}
217 	(void) putchar('\n');
218 }
219 
220 /*f
221  *	Display error message.
222  */
223 STATIC void
VARARG1(char *,fmt)224 error VARARG1(char*, fmt)
225 {
226 	va_list ap;
227 	(void) fprintf(stderr, "%s: ", _cmdname);
228 	va_start(ap, fmt);
229 	(void) vfprintf(stderr, fmt, ap);
230 	va_end(ap);
231 }
232 
233 /*f
234  *	This routine is a codeset independent method of specifying a translation
235  *	from an internal binary value, to an unambiguous printable format.
236  *	This mapping is defined by Table 2-13 in section 2-12 of POSIX.2.
237  *
238  * 	This table has been extended to account for tic/infocmp specification
239  *	of additional characters: <escape>, <space>, <colon>, <caret>, <comma>
240  */
241 char *
escape(c)242 escape(c)
243 int c;
244 {
245 	int i;
246 	static char buf[5];
247 	static int cntl_code[] = {
248 		'\0', '\\', M_ALERT, '\b', '\f', '\n', '\r', '\t',
249 		M_VTAB, M_ESCAPE, ' ', ':', '^', ',',
250 		-1
251 	};
252 	static char *cntl_str[] = {
253 		"\\0", "\\\\", "\\a", "\\b", "\\f", "\\n", "\\r", "\\t",
254 		"\\v", "\\E", "\\s", "\\:", "\\^", "\\,"
255 	};
256 	for (i = 0; cntl_code[i] != -1; ++i)
257 		if (c == cntl_code[i])
258 			return (cntl_str[i]);
259 	if (!isprint(c))
260 		(void) sprintf(buf, "\\%03.3o", (unsigned char) c);
261 	else
262 		buf[0] = c, buf[1] = '\0';
263 	return (buf);
264 }
265