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