xref: /titanic_44/usr/src/cmd/infocmp/infocmp.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 /*	Copyright (c) 1988 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.13	*/
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /*
34*7c478bd9Sstevel@tonic-gate     NAME
35*7c478bd9Sstevel@tonic-gate 	infocmp - compare terminfo descriptions, or dump a terminfo
36*7c478bd9Sstevel@tonic-gate 	description
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate     AUTHOR
39*7c478bd9Sstevel@tonic-gate 	Tony Hansen, February 23, 1984.
40*7c478bd9Sstevel@tonic-gate */
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include "curses.h"
43*7c478bd9Sstevel@tonic-gate #include "term.h"
44*7c478bd9Sstevel@tonic-gate #include "print.h"
45*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
46*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate /* externs from libcurses */
49*7c478bd9Sstevel@tonic-gate extern char *boolnames[];
50*7c478bd9Sstevel@tonic-gate extern char *boolcodes[];
51*7c478bd9Sstevel@tonic-gate extern char *boolfnames[];
52*7c478bd9Sstevel@tonic-gate extern char *numnames[];
53*7c478bd9Sstevel@tonic-gate extern char *numcodes[];
54*7c478bd9Sstevel@tonic-gate extern char *numfnames[];
55*7c478bd9Sstevel@tonic-gate extern char *strnames[];
56*7c478bd9Sstevel@tonic-gate extern char *strcodes[];
57*7c478bd9Sstevel@tonic-gate extern char *strfnames[];
58*7c478bd9Sstevel@tonic-gate extern char ttytype[];
59*7c478bd9Sstevel@tonic-gate extern int tgetflag();
60*7c478bd9Sstevel@tonic-gate extern int tgetnum();
61*7c478bd9Sstevel@tonic-gate extern char *tgetstr();
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate /* externs from libc */
64*7c478bd9Sstevel@tonic-gate extern void exit();
65*7c478bd9Sstevel@tonic-gate extern void qsort();
66*7c478bd9Sstevel@tonic-gate extern char *getenv();
67*7c478bd9Sstevel@tonic-gate extern int getopt();
68*7c478bd9Sstevel@tonic-gate extern int optind;
69*7c478bd9Sstevel@tonic-gate extern char *optarg;
70*7c478bd9Sstevel@tonic-gate extern char *strncpy(), *strcpy();
71*7c478bd9Sstevel@tonic-gate extern int strcmp(), strlen();
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate /* data structures for this program */
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate struct boolstruct {
76*7c478bd9Sstevel@tonic-gate     char *infoname;			/* the terminfo capability name */
77*7c478bd9Sstevel@tonic-gate     char *capname;			/* the termcap capability name */
78*7c478bd9Sstevel@tonic-gate     char *fullname;			/* the long C variable name */
79*7c478bd9Sstevel@tonic-gate     char *secondname;			/* the use= terminal w/ this value */
80*7c478bd9Sstevel@tonic-gate     char val;				/* the value */
81*7c478bd9Sstevel@tonic-gate     char secondval;			/* the value in the use= terminal */
82*7c478bd9Sstevel@tonic-gate     char changed;			/* a use= terminal changed the value */
83*7c478bd9Sstevel@tonic-gate     char seenagain;			/* a use= terminal had this entry */
84*7c478bd9Sstevel@tonic-gate     };
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate struct numstruct {
87*7c478bd9Sstevel@tonic-gate     char *infoname;			/* ditto from above */
88*7c478bd9Sstevel@tonic-gate     char *capname;
89*7c478bd9Sstevel@tonic-gate     char *fullname;
90*7c478bd9Sstevel@tonic-gate     char *secondname;
91*7c478bd9Sstevel@tonic-gate     short val;
92*7c478bd9Sstevel@tonic-gate     short secondval;
93*7c478bd9Sstevel@tonic-gate     char changed;
94*7c478bd9Sstevel@tonic-gate     char seenagain;
95*7c478bd9Sstevel@tonic-gate     };
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate struct strstruct {
98*7c478bd9Sstevel@tonic-gate     char *infoname;			/* ditto from above */
99*7c478bd9Sstevel@tonic-gate     char *capname;
100*7c478bd9Sstevel@tonic-gate     char *fullname;
101*7c478bd9Sstevel@tonic-gate     char *secondname;
102*7c478bd9Sstevel@tonic-gate     char *val;
103*7c478bd9Sstevel@tonic-gate     char *secondval;
104*7c478bd9Sstevel@tonic-gate     char changed;
105*7c478bd9Sstevel@tonic-gate     char seenagain;
106*7c478bd9Sstevel@tonic-gate     };
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate /* globals for this file */
109*7c478bd9Sstevel@tonic-gate char *progname;			/* argv[0], the name of the program */
110*7c478bd9Sstevel@tonic-gate static struct boolstruct *ibool; /* array of char information */
111*7c478bd9Sstevel@tonic-gate static struct numstruct *num;	/* array of number information */
112*7c478bd9Sstevel@tonic-gate static struct strstruct *str;	/* array of string information */
113*7c478bd9Sstevel@tonic-gate static char *used;		/* usage statistics */
114*7c478bd9Sstevel@tonic-gate static int numbools;		/* how many booleans there are */
115*7c478bd9Sstevel@tonic-gate static int numnums;		/* how many numbers there are */
116*7c478bd9Sstevel@tonic-gate static int numstrs;		/* how many strings there are */
117*7c478bd9Sstevel@tonic-gate #define	TTYLEN 255
118*7c478bd9Sstevel@tonic-gate static char *firstterm;		/* the name of the first terminal */
119*7c478bd9Sstevel@tonic-gate static char *savettytype;	/* the synonyms of the first terminal */
120*7c478bd9Sstevel@tonic-gate static char _savettytype[TTYLEN+1]; /* the place to save those names */
121*7c478bd9Sstevel@tonic-gate static int devnull;		/* open("/dev/null") for setupterm */
122*7c478bd9Sstevel@tonic-gate #define	trace stderr		/* send trace messages to stderr */
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate /* options */
125*7c478bd9Sstevel@tonic-gate static int verbose = 0;		/* debugging printing level */
126*7c478bd9Sstevel@tonic-gate static int diff = 0;		/* produce diff listing, the default */
127*7c478bd9Sstevel@tonic-gate static int common = 0;		/* produce common listing */
128*7c478bd9Sstevel@tonic-gate static int neither = 0;		/* list caps in neither entry */
129*7c478bd9Sstevel@tonic-gate static int use = 0;		/* produce use= comparison listing */
130*7c478bd9Sstevel@tonic-gate static enum printtypes printing	/* doing any of above printing at all */
131*7c478bd9Sstevel@tonic-gate 	= pr_none;
132*7c478bd9Sstevel@tonic-gate enum { none, by_database, by_terminfo, by_longnames, by_cap }
133*7c478bd9Sstevel@tonic-gate     sortorder = none;		/* sort the fields for printing */
134*7c478bd9Sstevel@tonic-gate static char *term1info, *term2info;	/* $TERMINFO settings */
135*7c478bd9Sstevel@tonic-gate static int Aflag = 0, Bflag = 0;	/* $TERMINFO was set with -A/-B */
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate #define	EQUAL(s1, s2)	(((s1 == NULL) && (s2 == NULL)) || \
138*7c478bd9Sstevel@tonic-gate 			((s1 != NULL) && (s2 != NULL) && \
139*7c478bd9Sstevel@tonic-gate 			(strcmp(s1, s2) == 0)))
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate static void sortnames();
142*7c478bd9Sstevel@tonic-gate int numcompare(const void *, const void *);
143*7c478bd9Sstevel@tonic-gate int boolcompare(const void *, const void *);
144*7c478bd9Sstevel@tonic-gate int strcompare(const void *, const void *);
145*7c478bd9Sstevel@tonic-gate static void check_nth_terminal(char *, int);
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate void
badmalloc()148*7c478bd9Sstevel@tonic-gate badmalloc()
149*7c478bd9Sstevel@tonic-gate {
150*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: malloc is out of space!\n", progname);
151*7c478bd9Sstevel@tonic-gate 	exit(-1);
152*7c478bd9Sstevel@tonic-gate }
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate /*
155*7c478bd9Sstevel@tonic-gate     Allocate and initialize the global data structures and variables.
156*7c478bd9Sstevel@tonic-gate */
157*7c478bd9Sstevel@tonic-gate void
allocvariables(int argc,int firstoptind)158*7c478bd9Sstevel@tonic-gate allocvariables(int argc, int firstoptind)
159*7c478bd9Sstevel@tonic-gate {
160*7c478bd9Sstevel@tonic-gate 	register int i, nullseen;
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	/* find out how many names we are dealing with */
163*7c478bd9Sstevel@tonic-gate 	for (numbools = 0; boolnames[numbools]; numbools++)
164*7c478bd9Sstevel@tonic-gate 		;
165*7c478bd9Sstevel@tonic-gate 	for (numnums = 0; numnames[numnums]; numnums++)
166*7c478bd9Sstevel@tonic-gate 		;
167*7c478bd9Sstevel@tonic-gate 	for (numstrs = 0; strnames[numstrs]; numstrs++)
168*7c478bd9Sstevel@tonic-gate 		;
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	if (verbose) {
171*7c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "There are %d boolean capabilities.\n",
172*7c478bd9Sstevel@tonic-gate 		    numbools);
173*7c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "There are %d numeric capabilities.\n",
174*7c478bd9Sstevel@tonic-gate 		    numnums);
175*7c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "There are %d string capabilities.\n",
176*7c478bd9Sstevel@tonic-gate 		    numstrs);
177*7c478bd9Sstevel@tonic-gate 	}
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	/* Allocate storage for the names and their values */
180*7c478bd9Sstevel@tonic-gate 	ibool = (struct boolstruct  *) malloc((unsigned) numbools *
181*7c478bd9Sstevel@tonic-gate 	    sizeof (struct boolstruct));
182*7c478bd9Sstevel@tonic-gate 	num = (struct numstruct *) malloc((unsigned) numnums *
183*7c478bd9Sstevel@tonic-gate 	    sizeof (struct numstruct));
184*7c478bd9Sstevel@tonic-gate 	str = (struct strstruct *) malloc((unsigned) numstrs *
185*7c478bd9Sstevel@tonic-gate 	    sizeof (struct strstruct));
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 	/* Allocate array to keep track of which names have been used. */
188*7c478bd9Sstevel@tonic-gate 	if (use)
189*7c478bd9Sstevel@tonic-gate 		used = (char *) malloc((unsigned) (argc - firstoptind) *
190*7c478bd9Sstevel@tonic-gate 		    sizeof (char));
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	if ((ibool == NULL) || (num == NULL) || (str == NULL) ||
193*7c478bd9Sstevel@tonic-gate 	    (use && (used == NULL)))
194*7c478bd9Sstevel@tonic-gate 		badmalloc();
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 	/* Fill in the names and initialize the structures. */
197*7c478bd9Sstevel@tonic-gate 	nullseen = FALSE;
198*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numbools; i++) {
199*7c478bd9Sstevel@tonic-gate 		ibool[i].infoname = boolnames[i];
200*7c478bd9Sstevel@tonic-gate 		ibool[i].capname = boolcodes[i];
201*7c478bd9Sstevel@tonic-gate 		/* This is necessary until fnames.c is */
202*7c478bd9Sstevel@tonic-gate 		/* incorporated into standard curses. */
203*7c478bd9Sstevel@tonic-gate 		if (nullseen || (boolfnames[i] == NULL)) {
204*7c478bd9Sstevel@tonic-gate 			ibool[i].fullname = "unknown_boolean";
205*7c478bd9Sstevel@tonic-gate 			nullseen = TRUE;
206*7c478bd9Sstevel@tonic-gate 		} else
207*7c478bd9Sstevel@tonic-gate 			ibool[i].fullname = boolfnames[i];
208*7c478bd9Sstevel@tonic-gate 		ibool[i].changed = FALSE;
209*7c478bd9Sstevel@tonic-gate 		ibool[i].seenagain = FALSE;
210*7c478bd9Sstevel@tonic-gate 	}
211*7c478bd9Sstevel@tonic-gate 	nullseen = 0;
212*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numnums; i++) {
213*7c478bd9Sstevel@tonic-gate 		num[i].infoname = numnames[i];
214*7c478bd9Sstevel@tonic-gate 		num[i].capname = numcodes[i];
215*7c478bd9Sstevel@tonic-gate 		if (nullseen || (numfnames[i] == NULL)) {
216*7c478bd9Sstevel@tonic-gate 			ibool[i].fullname = "unknown_number";
217*7c478bd9Sstevel@tonic-gate 			nullseen = TRUE;
218*7c478bd9Sstevel@tonic-gate 		} else
219*7c478bd9Sstevel@tonic-gate 			num[i].fullname = numfnames[i];
220*7c478bd9Sstevel@tonic-gate 		num[i].changed = FALSE;
221*7c478bd9Sstevel@tonic-gate 		num[i].seenagain = FALSE;
222*7c478bd9Sstevel@tonic-gate 	}
223*7c478bd9Sstevel@tonic-gate 	nullseen = 0;
224*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numstrs; i++) {
225*7c478bd9Sstevel@tonic-gate 		str[i].infoname = strnames[i];
226*7c478bd9Sstevel@tonic-gate 		str[i].capname = strcodes[i];
227*7c478bd9Sstevel@tonic-gate 		if (nullseen || (strfnames[i] == NULL)) {
228*7c478bd9Sstevel@tonic-gate 			str[i].fullname = "unknown_string";
229*7c478bd9Sstevel@tonic-gate 			nullseen = TRUE;
230*7c478bd9Sstevel@tonic-gate 		} else
231*7c478bd9Sstevel@tonic-gate 			str[i].fullname = strfnames[i];
232*7c478bd9Sstevel@tonic-gate 		str[i].changed = FALSE;
233*7c478bd9Sstevel@tonic-gate 		str[i].seenagain = FALSE;
234*7c478bd9Sstevel@tonic-gate 	}
235*7c478bd9Sstevel@tonic-gate }
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate /*
238*7c478bd9Sstevel@tonic-gate 	Routines to be passed to qsort(3) for comparison of the structures.
239*7c478bd9Sstevel@tonic-gate */
240*7c478bd9Sstevel@tonic-gate int
boolcompare(const void * x,const void * y)241*7c478bd9Sstevel@tonic-gate boolcompare(const void *x, const void *y)
242*7c478bd9Sstevel@tonic-gate {
243*7c478bd9Sstevel@tonic-gate 	struct boolstruct *a;
244*7c478bd9Sstevel@tonic-gate 	struct boolstruct *b;
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 	a = (struct boolstruct *)x;
247*7c478bd9Sstevel@tonic-gate 	b = (struct boolstruct *)y;
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 	switch ((int) sortorder) {
250*7c478bd9Sstevel@tonic-gate 		case (int) by_terminfo:
251*7c478bd9Sstevel@tonic-gate 			return (strcmp(a->infoname, b->infoname));
252*7c478bd9Sstevel@tonic-gate 		case (int) by_cap:
253*7c478bd9Sstevel@tonic-gate 			return (strcmp(a->capname, b->capname));
254*7c478bd9Sstevel@tonic-gate 		case (int) by_longnames:
255*7c478bd9Sstevel@tonic-gate 			return (strcmp(a->fullname, b->fullname));
256*7c478bd9Sstevel@tonic-gate 		default:
257*7c478bd9Sstevel@tonic-gate 			return (0);
258*7c478bd9Sstevel@tonic-gate 	}
259*7c478bd9Sstevel@tonic-gate }
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate int
numcompare(const void * x,const void * y)262*7c478bd9Sstevel@tonic-gate numcompare(const void *x, const void *y)
263*7c478bd9Sstevel@tonic-gate {
264*7c478bd9Sstevel@tonic-gate 	struct numstruct *a;
265*7c478bd9Sstevel@tonic-gate 	struct numstruct *b;
266*7c478bd9Sstevel@tonic-gate 
267*7c478bd9Sstevel@tonic-gate 	a = (struct numstruct *)x;
268*7c478bd9Sstevel@tonic-gate 	b = (struct numstruct *)y;
269*7c478bd9Sstevel@tonic-gate 	switch ((int) sortorder) {
270*7c478bd9Sstevel@tonic-gate 		case (int) by_terminfo:
271*7c478bd9Sstevel@tonic-gate 			return (strcmp(a->infoname, b->infoname));
272*7c478bd9Sstevel@tonic-gate 		case (int) by_cap:
273*7c478bd9Sstevel@tonic-gate 			return (strcmp(a->capname, b->capname));
274*7c478bd9Sstevel@tonic-gate 		case (int) by_longnames:
275*7c478bd9Sstevel@tonic-gate 			return (strcmp(a->fullname, b->fullname));
276*7c478bd9Sstevel@tonic-gate 		default:
277*7c478bd9Sstevel@tonic-gate 			return (0);
278*7c478bd9Sstevel@tonic-gate 	}
279*7c478bd9Sstevel@tonic-gate }
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate int
strcompare(const void * x,const void * y)282*7c478bd9Sstevel@tonic-gate strcompare(const void *x, const void *y)
283*7c478bd9Sstevel@tonic-gate {
284*7c478bd9Sstevel@tonic-gate 	struct strstruct *a;
285*7c478bd9Sstevel@tonic-gate 	struct strstruct *b;
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 	a = (struct strstruct *)x;
288*7c478bd9Sstevel@tonic-gate 	b = (struct strstruct *)y;
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate 	switch ((int) sortorder) {
291*7c478bd9Sstevel@tonic-gate 		case (int) by_terminfo:
292*7c478bd9Sstevel@tonic-gate 			return (strcmp(a->infoname, b->infoname));
293*7c478bd9Sstevel@tonic-gate 		case (int) by_cap:
294*7c478bd9Sstevel@tonic-gate 			return (strcmp(a->capname, b->capname));
295*7c478bd9Sstevel@tonic-gate 		case (int) by_longnames:
296*7c478bd9Sstevel@tonic-gate 			return (strcmp(a->fullname, b->fullname));
297*7c478bd9Sstevel@tonic-gate 		default:
298*7c478bd9Sstevel@tonic-gate 			return (0);
299*7c478bd9Sstevel@tonic-gate 	}
300*7c478bd9Sstevel@tonic-gate }
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate /*
303*7c478bd9Sstevel@tonic-gate 	Sort the entries by their terminfo name.
304*7c478bd9Sstevel@tonic-gate */
305*7c478bd9Sstevel@tonic-gate static void
sortnames()306*7c478bd9Sstevel@tonic-gate sortnames()
307*7c478bd9Sstevel@tonic-gate {
308*7c478bd9Sstevel@tonic-gate 	if (sortorder != by_database) {
309*7c478bd9Sstevel@tonic-gate 		qsort((char *) ibool, (unsigned) numbools,
310*7c478bd9Sstevel@tonic-gate 			sizeof (struct boolstruct), boolcompare);
311*7c478bd9Sstevel@tonic-gate 		qsort((char *) num, (unsigned) numnums,
312*7c478bd9Sstevel@tonic-gate 			sizeof (struct numstruct), numcompare);
313*7c478bd9Sstevel@tonic-gate 		qsort((char *) str, (unsigned) numstrs,
314*7c478bd9Sstevel@tonic-gate 			sizeof (struct strstruct), strcompare);
315*7c478bd9Sstevel@tonic-gate 	}
316*7c478bd9Sstevel@tonic-gate 	return;
317*7c478bd9Sstevel@tonic-gate }
318*7c478bd9Sstevel@tonic-gate 
319*7c478bd9Sstevel@tonic-gate /*
320*7c478bd9Sstevel@tonic-gate 	Print out a string, or "NULL" if it's not defined.
321*7c478bd9Sstevel@tonic-gate */
322*7c478bd9Sstevel@tonic-gate void
PR(FILE * stream,char * string)323*7c478bd9Sstevel@tonic-gate PR(FILE *stream, char *string)
324*7c478bd9Sstevel@tonic-gate {
325*7c478bd9Sstevel@tonic-gate 	if (string == NULL)
326*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stream, "NULL");
327*7c478bd9Sstevel@tonic-gate 	else
328*7c478bd9Sstevel@tonic-gate 		tpr(stream, string);
329*7c478bd9Sstevel@tonic-gate }
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate /*
332*7c478bd9Sstevel@tonic-gate 	Output the 'ko' termcap string. This is a list of all of the input
333*7c478bd9Sstevel@tonic-gate 	keys that input the same thing as the corresponding output strings.
334*7c478bd9Sstevel@tonic-gate */
335*7c478bd9Sstevel@tonic-gate int kncounter;
336*7c478bd9Sstevel@tonic-gate char kobuffer[512];
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate char
addko(char * output,char * input,char * koptr)339*7c478bd9Sstevel@tonic-gate *addko(char *output, char *input, char *koptr)
340*7c478bd9Sstevel@tonic-gate {
341*7c478bd9Sstevel@tonic-gate 	char *inptr, *outptr, padbuffer[512];
342*7c478bd9Sstevel@tonic-gate 	inptr = tgetstr(input, (char **)0);
343*7c478bd9Sstevel@tonic-gate 	if (inptr == NULL)
344*7c478bd9Sstevel@tonic-gate 		return (koptr);
345*7c478bd9Sstevel@tonic-gate 	outptr = tgetstr(output, (char **)0);
346*7c478bd9Sstevel@tonic-gate 	if (outptr == NULL)
347*7c478bd9Sstevel@tonic-gate 		return (koptr);
348*7c478bd9Sstevel@tonic-gate 	outptr = rmpadding(outptr, padbuffer, (int *) 0);
349*7c478bd9Sstevel@tonic-gate 	if (strcmp(inptr, outptr) == 0) {
350*7c478bd9Sstevel@tonic-gate 		*koptr++ = *output++;
351*7c478bd9Sstevel@tonic-gate 		*koptr++ = *output++;
352*7c478bd9Sstevel@tonic-gate 		*koptr++ = ',';
353*7c478bd9Sstevel@tonic-gate 		kncounter++;
354*7c478bd9Sstevel@tonic-gate 	}
355*7c478bd9Sstevel@tonic-gate 	return (koptr);
356*7c478bd9Sstevel@tonic-gate }
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate void
setupknko()359*7c478bd9Sstevel@tonic-gate setupknko()
360*7c478bd9Sstevel@tonic-gate {
361*7c478bd9Sstevel@tonic-gate 	char *koptr;
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate 	kncounter = 0;
364*7c478bd9Sstevel@tonic-gate 	koptr = kobuffer;
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate 	koptr = addko("bs", "kb", koptr);	/* key_backspace */
367*7c478bd9Sstevel@tonic-gate 	koptr = addko("bt", "kB", koptr);	/* key_btab */
368*7c478bd9Sstevel@tonic-gate 	koptr = addko("cl", "kC", koptr);	/* key_clear */
369*7c478bd9Sstevel@tonic-gate 	koptr = addko("le", "kl", koptr);	/* key_left */
370*7c478bd9Sstevel@tonic-gate 	koptr = addko("do", "kd", koptr);	/* key_down */
371*7c478bd9Sstevel@tonic-gate 	koptr = addko("nd", "kr", koptr);	/* key_right */
372*7c478bd9Sstevel@tonic-gate 	koptr = addko("up", "ku", koptr);	/* key_up */
373*7c478bd9Sstevel@tonic-gate 	koptr = addko("dc", "kD", koptr);	/* key_dc */
374*7c478bd9Sstevel@tonic-gate 	koptr = addko("dl", "kL", koptr);	/* key_dl */
375*7c478bd9Sstevel@tonic-gate 	koptr = addko("cd", "kS", koptr);	/* key_eos */
376*7c478bd9Sstevel@tonic-gate 	koptr = addko("ce", "kE", koptr);	/* key_eol */
377*7c478bd9Sstevel@tonic-gate 	koptr = addko("ho", "kh", koptr);	/* key_home */
378*7c478bd9Sstevel@tonic-gate 	koptr = addko("st", "kT", koptr);	/* key_stab */
379*7c478bd9Sstevel@tonic-gate 	koptr = addko("ic", "kI", koptr);	/* key_ic */
380*7c478bd9Sstevel@tonic-gate 	koptr = addko("im", "kI", koptr);	/* key_ic */
381*7c478bd9Sstevel@tonic-gate 	koptr = addko("al", "kA", koptr);	/* key_il */
382*7c478bd9Sstevel@tonic-gate 	koptr = addko("sf", "kF", koptr);	/* key_sf */
383*7c478bd9Sstevel@tonic-gate 	koptr = addko("ll", "kH", koptr);	/* key_ll */
384*7c478bd9Sstevel@tonic-gate 	koptr = addko("sr", "kR", koptr);	/* key_sr */
385*7c478bd9Sstevel@tonic-gate 	koptr = addko("ei", "kM", koptr);	/* key_eic */
386*7c478bd9Sstevel@tonic-gate 	koptr = addko("ct", "ka", koptr);	/* key_catab */
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate 	/* get rid of comma */
389*7c478bd9Sstevel@tonic-gate 	if (koptr != kobuffer)
390*7c478bd9Sstevel@tonic-gate 		*(--koptr) = '\0';
391*7c478bd9Sstevel@tonic-gate }
392*7c478bd9Sstevel@tonic-gate 
393*7c478bd9Sstevel@tonic-gate void
pr_kn()394*7c478bd9Sstevel@tonic-gate pr_kn()
395*7c478bd9Sstevel@tonic-gate {
396*7c478bd9Sstevel@tonic-gate 	if (kncounter > 0)
397*7c478bd9Sstevel@tonic-gate 		pr_number((char *)0, "kn", (char *)0, kncounter);
398*7c478bd9Sstevel@tonic-gate }
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate void
pr_ko()401*7c478bd9Sstevel@tonic-gate pr_ko()
402*7c478bd9Sstevel@tonic-gate {
403*7c478bd9Sstevel@tonic-gate 	if (kncounter > 0)
404*7c478bd9Sstevel@tonic-gate 		pr_string((char *)0, "ko", (char *)0, kobuffer);
405*7c478bd9Sstevel@tonic-gate }
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate void
pr_bcaps()408*7c478bd9Sstevel@tonic-gate pr_bcaps()
409*7c478bd9Sstevel@tonic-gate {
410*7c478bd9Sstevel@tonic-gate 	char *retptr;
411*7c478bd9Sstevel@tonic-gate 	char padbuffer[512];
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate 	if (verbose)
414*7c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'bs'\n");
415*7c478bd9Sstevel@tonic-gate 	retptr = cconvert(rmpadding(cursor_left, padbuffer, (int *) 0));
416*7c478bd9Sstevel@tonic-gate 	if (strcmp("\\b", retptr) == 0)
417*7c478bd9Sstevel@tonic-gate 		pr_boolean((char *)0, "bs", (char *)0, 1);
418*7c478bd9Sstevel@tonic-gate 
419*7c478bd9Sstevel@tonic-gate 	if (verbose)
420*7c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'pt'\n");
421*7c478bd9Sstevel@tonic-gate 	retptr = cconvert(rmpadding(tab, padbuffer, (int *) 0));
422*7c478bd9Sstevel@tonic-gate 	if (strcmp("\\t", retptr) == 0)
423*7c478bd9Sstevel@tonic-gate 		pr_boolean((char *)0, "pt", (char *)0, 1);
424*7c478bd9Sstevel@tonic-gate 
425*7c478bd9Sstevel@tonic-gate 	if (verbose)
426*7c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'nc'\n");
427*7c478bd9Sstevel@tonic-gate 	retptr = cconvert(rmpadding(carriage_return, padbuffer, (int *) 0));
428*7c478bd9Sstevel@tonic-gate 	if (strcmp("\\r", retptr) != 0)
429*7c478bd9Sstevel@tonic-gate 		pr_boolean((char *)0, "nc", (char *)0, 1);
430*7c478bd9Sstevel@tonic-gate 
431*7c478bd9Sstevel@tonic-gate 	if (verbose)
432*7c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'ns'\n");
433*7c478bd9Sstevel@tonic-gate 	if (scroll_forward == NULL)
434*7c478bd9Sstevel@tonic-gate 		pr_boolean((char *)0, "ns", (char *)0, 1);
435*7c478bd9Sstevel@tonic-gate 
436*7c478bd9Sstevel@tonic-gate 	/* Ignore "xr": Return acts like ce \r \n (Delta Data) */
437*7c478bd9Sstevel@tonic-gate }
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate void
pr_ncaps()440*7c478bd9Sstevel@tonic-gate pr_ncaps()
441*7c478bd9Sstevel@tonic-gate {
442*7c478bd9Sstevel@tonic-gate 	char padbuffer[512];
443*7c478bd9Sstevel@tonic-gate 	int padding;
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate 	if (verbose)
446*7c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'ug'\n");
447*7c478bd9Sstevel@tonic-gate 	/* Duplicate sg for ug: Number of blank chars left by us or ue */
448*7c478bd9Sstevel@tonic-gate 	if (magic_cookie_glitch > -1)
449*7c478bd9Sstevel@tonic-gate 		pr_number((char *)0, "ug", (char *)0, magic_cookie_glitch);
450*7c478bd9Sstevel@tonic-gate 
451*7c478bd9Sstevel@tonic-gate 	if (verbose)
452*7c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'dB'\n");
453*7c478bd9Sstevel@tonic-gate 	/* Number of millisec of bs delay needed */
454*7c478bd9Sstevel@tonic-gate 	(void) rmpadding(cursor_left, padbuffer, &padding);
455*7c478bd9Sstevel@tonic-gate 	if (padding > 0)
456*7c478bd9Sstevel@tonic-gate 		pr_number((char *)0, "dB", (char *)0, padding);
457*7c478bd9Sstevel@tonic-gate 
458*7c478bd9Sstevel@tonic-gate 	if (verbose)
459*7c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'dC'\n");
460*7c478bd9Sstevel@tonic-gate 	/* Number of millisec of cr delay needed */
461*7c478bd9Sstevel@tonic-gate 	(void) rmpadding(carriage_return, padbuffer, &padding);
462*7c478bd9Sstevel@tonic-gate 	if (padding > 0)
463*7c478bd9Sstevel@tonic-gate 		pr_number((char *)0, "dC", (char *)0, padding);
464*7c478bd9Sstevel@tonic-gate 
465*7c478bd9Sstevel@tonic-gate 	if (verbose)
466*7c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'dF'\n");
467*7c478bd9Sstevel@tonic-gate 	/* Number of millisec of ff delay needed */
468*7c478bd9Sstevel@tonic-gate 	(void) rmpadding(form_feed, padbuffer, &padding);
469*7c478bd9Sstevel@tonic-gate 	if (padding > 0)
470*7c478bd9Sstevel@tonic-gate 		pr_number((char *)0, "dF", (char *)0, padding);
471*7c478bd9Sstevel@tonic-gate 
472*7c478bd9Sstevel@tonic-gate 	if (verbose)
473*7c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'dN'\n");
474*7c478bd9Sstevel@tonic-gate 	/* Number of millisec of nl delay needed */
475*7c478bd9Sstevel@tonic-gate 	(void) rmpadding(cursor_down, padbuffer, &padding);
476*7c478bd9Sstevel@tonic-gate 	if (padding > 0)
477*7c478bd9Sstevel@tonic-gate 		pr_number((char *)0, "dN", (char *)0, padding);
478*7c478bd9Sstevel@tonic-gate 
479*7c478bd9Sstevel@tonic-gate 	if (verbose)
480*7c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'dT'\n");
481*7c478bd9Sstevel@tonic-gate 	/* Number of millisec of tab delay needed */
482*7c478bd9Sstevel@tonic-gate 	(void) rmpadding(tab, padbuffer, &padding);
483*7c478bd9Sstevel@tonic-gate 	if (padding > 0)
484*7c478bd9Sstevel@tonic-gate 		pr_number((char *)0, "dT", (char *)0, padding);
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate 	/* Handle "kn": Number of "other" keys */
487*7c478bd9Sstevel@tonic-gate 	setupknko();
488*7c478bd9Sstevel@tonic-gate 	pr_kn();
489*7c478bd9Sstevel@tonic-gate }
490*7c478bd9Sstevel@tonic-gate 
491*7c478bd9Sstevel@tonic-gate void
pr_scaps()492*7c478bd9Sstevel@tonic-gate pr_scaps()
493*7c478bd9Sstevel@tonic-gate {
494*7c478bd9Sstevel@tonic-gate 	char *retptr;
495*7c478bd9Sstevel@tonic-gate 	char padbuffer[512];
496*7c478bd9Sstevel@tonic-gate 
497*7c478bd9Sstevel@tonic-gate 	/* Backspace if not "^H" */
498*7c478bd9Sstevel@tonic-gate 	if (verbose)
499*7c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'bc'\n");
500*7c478bd9Sstevel@tonic-gate 	retptr = cconvert(rmpadding(cursor_left, padbuffer, (int *) 0));
501*7c478bd9Sstevel@tonic-gate 	if (strcmp("\\b", retptr) != 0)
502*7c478bd9Sstevel@tonic-gate 		pr_string((char *)0, "bc", (char *)0, cursor_left);
503*7c478bd9Sstevel@tonic-gate 
504*7c478bd9Sstevel@tonic-gate 	/* Newline character (default "\n") */
505*7c478bd9Sstevel@tonic-gate 	if (verbose)
506*7c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'nl'\n");
507*7c478bd9Sstevel@tonic-gate 	retptr = cconvert(rmpadding(cursor_down, padbuffer, (int *) 0));
508*7c478bd9Sstevel@tonic-gate 	if (strcmp("\\n", retptr) != 0)
509*7c478bd9Sstevel@tonic-gate 		pr_string((char *)0, "nl", (char *)0, cursor_down);
510*7c478bd9Sstevel@tonic-gate 
511*7c478bd9Sstevel@tonic-gate 	/* Handle "ko" here: Termcap entries for other non-function keys */
512*7c478bd9Sstevel@tonic-gate 	pr_ko();
513*7c478bd9Sstevel@tonic-gate 
514*7c478bd9Sstevel@tonic-gate 	/* Ignore "ma": Arrow key map, used by vi version 2 only */
515*7c478bd9Sstevel@tonic-gate }
516*7c478bd9Sstevel@tonic-gate 
517*7c478bd9Sstevel@tonic-gate /*
518*7c478bd9Sstevel@tonic-gate 	Set up the first terminal and save the values from it.
519*7c478bd9Sstevel@tonic-gate */
520*7c478bd9Sstevel@tonic-gate void
initfirstterm(char * term)521*7c478bd9Sstevel@tonic-gate initfirstterm(char *term)
522*7c478bd9Sstevel@tonic-gate {
523*7c478bd9Sstevel@tonic-gate 	register int i;
524*7c478bd9Sstevel@tonic-gate 
525*7c478bd9Sstevel@tonic-gate 	if (verbose)
526*7c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "setting up terminal type '%s'.\n",
527*7c478bd9Sstevel@tonic-gate 		    term);
528*7c478bd9Sstevel@tonic-gate 
529*7c478bd9Sstevel@tonic-gate 	(void) setupterm(term, devnull, (int *) 0);
530*7c478bd9Sstevel@tonic-gate 
531*7c478bd9Sstevel@tonic-gate 	/* Save the name for later use. */
532*7c478bd9Sstevel@tonic-gate 	if (use) {
533*7c478bd9Sstevel@tonic-gate 		register unsigned int length;
534*7c478bd9Sstevel@tonic-gate 		savettytype = _savettytype;
535*7c478bd9Sstevel@tonic-gate 		if ((length = strlen(ttytype)) >= TTYLEN) {
536*7c478bd9Sstevel@tonic-gate 			savettytype = malloc(length);
537*7c478bd9Sstevel@tonic-gate 			if (savettytype == NULL) {
538*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "%s: malloc is out "
539*7c478bd9Sstevel@tonic-gate 				    "of space\n", progname);
540*7c478bd9Sstevel@tonic-gate 				(void) strncpy(_savettytype, ttytype,
541*7c478bd9Sstevel@tonic-gate 				    TTYLEN-1);
542*7c478bd9Sstevel@tonic-gate 				_savettytype[TTYLEN] = '\0';
543*7c478bd9Sstevel@tonic-gate 				savettytype = _savettytype;
544*7c478bd9Sstevel@tonic-gate 			}
545*7c478bd9Sstevel@tonic-gate 		} else
546*7c478bd9Sstevel@tonic-gate 			(void) strcpy(_savettytype, ttytype);
547*7c478bd9Sstevel@tonic-gate 	}
548*7c478bd9Sstevel@tonic-gate 
549*7c478bd9Sstevel@tonic-gate 	if (printing != pr_none) {
550*7c478bd9Sstevel@tonic-gate 		pr_heading(term, ttytype);
551*7c478bd9Sstevel@tonic-gate 		pr_bheading();
552*7c478bd9Sstevel@tonic-gate 	}
553*7c478bd9Sstevel@tonic-gate 
554*7c478bd9Sstevel@tonic-gate 	/* Save the values for the first terminal. */
555*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numbools; i++) {
556*7c478bd9Sstevel@tonic-gate 		if ((ibool[i].val = tgetflag(ibool[i].capname)) &&
557*7c478bd9Sstevel@tonic-gate 		    printing != pr_none)
558*7c478bd9Sstevel@tonic-gate 			pr_boolean(ibool[i].infoname, ibool[i].capname,
559*7c478bd9Sstevel@tonic-gate 			    ibool[i].fullname, 1);
560*7c478bd9Sstevel@tonic-gate 		if (verbose)
561*7c478bd9Sstevel@tonic-gate 			(void) fprintf(trace, "%s=%d.\n", ibool[i].infoname,
562*7c478bd9Sstevel@tonic-gate 			    ibool[i].val);
563*7c478bd9Sstevel@tonic-gate 	}
564*7c478bd9Sstevel@tonic-gate 
565*7c478bd9Sstevel@tonic-gate 	if (printing != pr_none) {
566*7c478bd9Sstevel@tonic-gate 		if (printing == pr_cap)
567*7c478bd9Sstevel@tonic-gate 			pr_bcaps();
568*7c478bd9Sstevel@tonic-gate 		pr_bfooting();
569*7c478bd9Sstevel@tonic-gate 		pr_nheading();
570*7c478bd9Sstevel@tonic-gate 	}
571*7c478bd9Sstevel@tonic-gate 
572*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numnums; i++) {
573*7c478bd9Sstevel@tonic-gate 		if (((num[i].val = tgetnum(num[i].capname)) > -1) &&
574*7c478bd9Sstevel@tonic-gate 		    printing != pr_none)
575*7c478bd9Sstevel@tonic-gate 			pr_number(num[i].infoname, num[i].capname,
576*7c478bd9Sstevel@tonic-gate 			    num[i].fullname, num[i].val);
577*7c478bd9Sstevel@tonic-gate 		if (verbose)
578*7c478bd9Sstevel@tonic-gate 			(void) fprintf(trace, "%s=%d.\n", num[i].infoname,
579*7c478bd9Sstevel@tonic-gate 			    num[i].val);
580*7c478bd9Sstevel@tonic-gate 	}
581*7c478bd9Sstevel@tonic-gate 
582*7c478bd9Sstevel@tonic-gate 	if (printing != pr_none) {
583*7c478bd9Sstevel@tonic-gate 		if (printing == pr_cap)
584*7c478bd9Sstevel@tonic-gate 			pr_ncaps();
585*7c478bd9Sstevel@tonic-gate 		pr_nfooting();
586*7c478bd9Sstevel@tonic-gate 		pr_sheading();
587*7c478bd9Sstevel@tonic-gate 	}
588*7c478bd9Sstevel@tonic-gate 
589*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numstrs; i++) {
590*7c478bd9Sstevel@tonic-gate 		str[i].val = tgetstr(str[i].capname, (char **)0);
591*7c478bd9Sstevel@tonic-gate 		if ((str[i].val != NULL) && printing != pr_none)
592*7c478bd9Sstevel@tonic-gate 			pr_string(str[i].infoname, str[i].capname,
593*7c478bd9Sstevel@tonic-gate 			    str[i].fullname, str[i].val);
594*7c478bd9Sstevel@tonic-gate 		if (verbose) {
595*7c478bd9Sstevel@tonic-gate 			(void) fprintf(trace, "%s='", str[i].infoname);
596*7c478bd9Sstevel@tonic-gate 			PR(trace, str[i].val);
597*7c478bd9Sstevel@tonic-gate 			(void) fprintf(trace, "'.\n");
598*7c478bd9Sstevel@tonic-gate 		}
599*7c478bd9Sstevel@tonic-gate 	}
600*7c478bd9Sstevel@tonic-gate 
601*7c478bd9Sstevel@tonic-gate 	if (printing == pr_cap)
602*7c478bd9Sstevel@tonic-gate 		pr_scaps();
603*7c478bd9Sstevel@tonic-gate 
604*7c478bd9Sstevel@tonic-gate 	if (printing != pr_none)
605*7c478bd9Sstevel@tonic-gate 		pr_sfooting();
606*7c478bd9Sstevel@tonic-gate }
607*7c478bd9Sstevel@tonic-gate 
608*7c478bd9Sstevel@tonic-gate /*
609*7c478bd9Sstevel@tonic-gate 	Set up the n'th terminal.
610*7c478bd9Sstevel@tonic-gate */
611*7c478bd9Sstevel@tonic-gate static void
check_nth_terminal(char * nterm,int n)612*7c478bd9Sstevel@tonic-gate check_nth_terminal(char *nterm, int n)
613*7c478bd9Sstevel@tonic-gate {
614*7c478bd9Sstevel@tonic-gate 	register char boolval;
615*7c478bd9Sstevel@tonic-gate 	register short numval;
616*7c478bd9Sstevel@tonic-gate 	register char *strval;
617*7c478bd9Sstevel@tonic-gate 	register int i;
618*7c478bd9Sstevel@tonic-gate 
619*7c478bd9Sstevel@tonic-gate 	if (use)
620*7c478bd9Sstevel@tonic-gate 		used[n] = FALSE;
621*7c478bd9Sstevel@tonic-gate 
622*7c478bd9Sstevel@tonic-gate 	if (verbose)
623*7c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "adding in terminal type '%s'.\n",
624*7c478bd9Sstevel@tonic-gate 		    nterm);
625*7c478bd9Sstevel@tonic-gate 
626*7c478bd9Sstevel@tonic-gate 	(void) setupterm(nterm, devnull, (int *) 0);
627*7c478bd9Sstevel@tonic-gate 
628*7c478bd9Sstevel@tonic-gate 	if (printing != pr_none) {
629*7c478bd9Sstevel@tonic-gate 		pr_heading(nterm, ttytype);
630*7c478bd9Sstevel@tonic-gate 		pr_bheading();
631*7c478bd9Sstevel@tonic-gate 	}
632*7c478bd9Sstevel@tonic-gate 
633*7c478bd9Sstevel@tonic-gate 	if (diff || common || neither) {
634*7c478bd9Sstevel@tonic-gate 		if (Aflag && Bflag)
635*7c478bd9Sstevel@tonic-gate 			(void) printf("comparing %s (TERMINFO=%s) to %s "
636*7c478bd9Sstevel@tonic-gate 			    "(TERMINFO=%s).\n",
637*7c478bd9Sstevel@tonic-gate 			firstterm, term1info, nterm, term2info);
638*7c478bd9Sstevel@tonic-gate 		else if (Aflag)
639*7c478bd9Sstevel@tonic-gate 			(void) printf("comparing %s (TERMINFO=%s) to %s.\n",
640*7c478bd9Sstevel@tonic-gate 			    firstterm, term1info, nterm);
641*7c478bd9Sstevel@tonic-gate 		else if (Bflag)
642*7c478bd9Sstevel@tonic-gate 			(void) printf("comparing %s to %s (TERMINFO=%s).\n",
643*7c478bd9Sstevel@tonic-gate 			    firstterm, nterm, term2info);
644*7c478bd9Sstevel@tonic-gate 		else
645*7c478bd9Sstevel@tonic-gate 			(void) printf("comparing %s to %s.\n",
646*7c478bd9Sstevel@tonic-gate 			    firstterm, nterm);
647*7c478bd9Sstevel@tonic-gate 		(void) printf("    comparing booleans.\n");
648*7c478bd9Sstevel@tonic-gate 	}
649*7c478bd9Sstevel@tonic-gate 
650*7c478bd9Sstevel@tonic-gate 	/* save away the values for the nth terminal */
651*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numbools; i++) {
652*7c478bd9Sstevel@tonic-gate 		boolval = tgetflag(ibool[i].capname);
653*7c478bd9Sstevel@tonic-gate 		if (use) {
654*7c478bd9Sstevel@tonic-gate 			if (ibool[i].seenagain) {
655*7c478bd9Sstevel@tonic-gate 			/*
656*7c478bd9Sstevel@tonic-gate 			** We do not have to worry about this impossible case
657*7c478bd9Sstevel@tonic-gate 			** since booleans can have only two values: true and
658*7c478bd9Sstevel@tonic-gate 			** false.
659*7c478bd9Sstevel@tonic-gate 			** if (boolval && (boolval != ibool[i].secondval))
660*7c478bd9Sstevel@tonic-gate 			**  {
661*7c478bd9Sstevel@tonic-gate 			**  (void) fprintf(trace, "use= order dependency"
662*7c478bd9Sstevel@tonic-gate 			**  "found:\n");
663*7c478bd9Sstevel@tonic-gate 			**  (void) fprintf(trace, "    %s: %s has %d, %s has"
664*7c478bd9Sstevel@tonic-gate 			**   " %d.\n",
665*7c478bd9Sstevel@tonic-gate 			**	ibool[i].capname, ibool[i].secondname,
666*7c478bd9Sstevel@tonic-gate 			**	ibool[i].secondval, nterm, boolval);
667*7c478bd9Sstevel@tonic-gate 			**  }
668*7c478bd9Sstevel@tonic-gate 			*/
669*7c478bd9Sstevel@tonic-gate 			} else {
670*7c478bd9Sstevel@tonic-gate 				if (boolval == TRUE) {
671*7c478bd9Sstevel@tonic-gate 					ibool[i].seenagain = TRUE;
672*7c478bd9Sstevel@tonic-gate 					ibool[i].secondval = boolval;
673*7c478bd9Sstevel@tonic-gate 					ibool[i].secondname = nterm;
674*7c478bd9Sstevel@tonic-gate 					if (ibool[i].val != boolval)
675*7c478bd9Sstevel@tonic-gate 						ibool[i].changed = TRUE;
676*7c478bd9Sstevel@tonic-gate 					else
677*7c478bd9Sstevel@tonic-gate 						used[n] = TRUE;
678*7c478bd9Sstevel@tonic-gate 				}
679*7c478bd9Sstevel@tonic-gate 			}
680*7c478bd9Sstevel@tonic-gate 		}
681*7c478bd9Sstevel@tonic-gate 		if (boolval) {
682*7c478bd9Sstevel@tonic-gate 			if (printing != pr_none)
683*7c478bd9Sstevel@tonic-gate 				pr_boolean(ibool[i].infoname, ibool[i].capname,
684*7c478bd9Sstevel@tonic-gate 				    ibool[i].fullname, 1);
685*7c478bd9Sstevel@tonic-gate 			if (common && (ibool[i].val == boolval))
686*7c478bd9Sstevel@tonic-gate 				(void) printf("\t%s= T.\n", ibool[i].infoname);
687*7c478bd9Sstevel@tonic-gate 		} else if (neither && !ibool[i].val)
688*7c478bd9Sstevel@tonic-gate 			(void) printf("\t!%s.\n", ibool[i].infoname);
689*7c478bd9Sstevel@tonic-gate 		if (diff && (ibool[i].val != boolval))
690*7c478bd9Sstevel@tonic-gate 			(void) printf("\t%s: %c:%c.\n", ibool[i].infoname,
691*7c478bd9Sstevel@tonic-gate 			    ibool[i].val?'T':'F', boolval?'T':'F');
692*7c478bd9Sstevel@tonic-gate 		if (verbose)
693*7c478bd9Sstevel@tonic-gate 			(void) fprintf(trace, "%s: %d:%d, changed=%d, "
694*7c478bd9Sstevel@tonic-gate 			    "seen=%d.\n", ibool[i].infoname, ibool[i].val,
695*7c478bd9Sstevel@tonic-gate 			    boolval, ibool[i].changed, ibool[i].seenagain);
696*7c478bd9Sstevel@tonic-gate 	}
697*7c478bd9Sstevel@tonic-gate 
698*7c478bd9Sstevel@tonic-gate 	if (printing != pr_none) {
699*7c478bd9Sstevel@tonic-gate 		if (printing == pr_cap)
700*7c478bd9Sstevel@tonic-gate 			pr_bcaps();
701*7c478bd9Sstevel@tonic-gate 		pr_bfooting();
702*7c478bd9Sstevel@tonic-gate 		pr_nheading();
703*7c478bd9Sstevel@tonic-gate 	}
704*7c478bd9Sstevel@tonic-gate 
705*7c478bd9Sstevel@tonic-gate 	if (diff || common || neither)
706*7c478bd9Sstevel@tonic-gate 		(void) printf("    comparing numbers.\n");
707*7c478bd9Sstevel@tonic-gate 
708*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numnums; i++) {
709*7c478bd9Sstevel@tonic-gate 		numval = tgetnum(num[i].capname);
710*7c478bd9Sstevel@tonic-gate 		if (use) {
711*7c478bd9Sstevel@tonic-gate 			if (num[i].seenagain) {
712*7c478bd9Sstevel@tonic-gate 				if ((numval > -1) &&
713*7c478bd9Sstevel@tonic-gate 				    (numval != num[i].secondval)) {
714*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
715*7c478bd9Sstevel@tonic-gate 					    "%s: use = order dependency "
716*7c478bd9Sstevel@tonic-gate 					    "found:\n", progname);
717*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, "    %s: %s "
718*7c478bd9Sstevel@tonic-gate 					    "has %d, %s has %d.\n",
719*7c478bd9Sstevel@tonic-gate 					    num[i].capname, num[i].secondname,
720*7c478bd9Sstevel@tonic-gate 					    num[i].secondval, nterm, numval);
721*7c478bd9Sstevel@tonic-gate 				}
722*7c478bd9Sstevel@tonic-gate 			} else {
723*7c478bd9Sstevel@tonic-gate 				if (numval > -1) {
724*7c478bd9Sstevel@tonic-gate 					num[i].seenagain = TRUE;
725*7c478bd9Sstevel@tonic-gate 					num[i].secondval = numval;
726*7c478bd9Sstevel@tonic-gate 					num[i].secondname = nterm;
727*7c478bd9Sstevel@tonic-gate 					if ((numval > -1) &&
728*7c478bd9Sstevel@tonic-gate 					    (num[i].val != numval))
729*7c478bd9Sstevel@tonic-gate 						num[i].changed = TRUE;
730*7c478bd9Sstevel@tonic-gate 					else
731*7c478bd9Sstevel@tonic-gate 						used[n] = TRUE;
732*7c478bd9Sstevel@tonic-gate 				}
733*7c478bd9Sstevel@tonic-gate 			}
734*7c478bd9Sstevel@tonic-gate 		}
735*7c478bd9Sstevel@tonic-gate 		if (numval > -1) {
736*7c478bd9Sstevel@tonic-gate 			if (printing != pr_none)
737*7c478bd9Sstevel@tonic-gate 				pr_number(num[i].infoname, num[i].capname,
738*7c478bd9Sstevel@tonic-gate 				    num[i].fullname, numval);
739*7c478bd9Sstevel@tonic-gate 			if (common && (num[i].val == numval))
740*7c478bd9Sstevel@tonic-gate 				(void) printf("\t%s= %d.\n", num[i].infoname,
741*7c478bd9Sstevel@tonic-gate 				    numval);
742*7c478bd9Sstevel@tonic-gate 			} else if (neither && (num[i].val == -1))
743*7c478bd9Sstevel@tonic-gate 				(void) printf("\t!%s.\n", num[i].infoname);
744*7c478bd9Sstevel@tonic-gate 			if (diff && (num[i].val != numval))
745*7c478bd9Sstevel@tonic-gate 				(void) printf("\t%s: %d:%d.\n",
746*7c478bd9Sstevel@tonic-gate 				    num[i].infoname, num[i].val, numval);
747*7c478bd9Sstevel@tonic-gate 			if (verbose)
748*7c478bd9Sstevel@tonic-gate 				(void) fprintf(trace, "%s: %d:%d, "
749*7c478bd9Sstevel@tonic-gate 				    "changed = %d, seen = %d.\n",
750*7c478bd9Sstevel@tonic-gate 				    num[i].infoname, num[i].val, numval,
751*7c478bd9Sstevel@tonic-gate 				    num[i].changed, num[i].seenagain);
752*7c478bd9Sstevel@tonic-gate 	}
753*7c478bd9Sstevel@tonic-gate 
754*7c478bd9Sstevel@tonic-gate 	if (printing != pr_none) {
755*7c478bd9Sstevel@tonic-gate 		if (printing == pr_cap)
756*7c478bd9Sstevel@tonic-gate 			pr_ncaps();
757*7c478bd9Sstevel@tonic-gate 		pr_nfooting();
758*7c478bd9Sstevel@tonic-gate 		pr_sheading();
759*7c478bd9Sstevel@tonic-gate 	}
760*7c478bd9Sstevel@tonic-gate 
761*7c478bd9Sstevel@tonic-gate 	if (diff || common || neither)
762*7c478bd9Sstevel@tonic-gate 		(void) printf("    comparing strings.\n");
763*7c478bd9Sstevel@tonic-gate 
764*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numstrs; i++) {
765*7c478bd9Sstevel@tonic-gate 		strval = tgetstr(str[i].capname, (char **)0);
766*7c478bd9Sstevel@tonic-gate 		if (use) {
767*7c478bd9Sstevel@tonic-gate 			if (str[i].seenagain && (strval != NULL)) {
768*7c478bd9Sstevel@tonic-gate 				if (!EQUAL(strval, str[i].secondval)) {
769*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
770*7c478bd9Sstevel@tonic-gate 					    "use= order dependency"
771*7c478bd9Sstevel@tonic-gate 					    "  found:\n");
772*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
773*7c478bd9Sstevel@tonic-gate 					    "    %s: %s has '",
774*7c478bd9Sstevel@tonic-gate 					    str[i].capname, str[i].secondname);
775*7c478bd9Sstevel@tonic-gate 					PR(stderr, str[i].secondval);
776*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
777*7c478bd9Sstevel@tonic-gate 					    "', %s has '", nterm);
778*7c478bd9Sstevel@tonic-gate 					PR(stderr, strval);
779*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, "'.\n");
780*7c478bd9Sstevel@tonic-gate 				}
781*7c478bd9Sstevel@tonic-gate 			} else {
782*7c478bd9Sstevel@tonic-gate 				if (strval != NULL) {
783*7c478bd9Sstevel@tonic-gate 					str[i].seenagain = TRUE;
784*7c478bd9Sstevel@tonic-gate 					str[i].secondval = strval;
785*7c478bd9Sstevel@tonic-gate 					str[i].secondname = nterm;
786*7c478bd9Sstevel@tonic-gate 					if (!EQUAL(str[i].val, strval))
787*7c478bd9Sstevel@tonic-gate 						str[i].changed = TRUE;
788*7c478bd9Sstevel@tonic-gate 					else
789*7c478bd9Sstevel@tonic-gate 						used[n] = TRUE;
790*7c478bd9Sstevel@tonic-gate 				}
791*7c478bd9Sstevel@tonic-gate 			}
792*7c478bd9Sstevel@tonic-gate 		}
793*7c478bd9Sstevel@tonic-gate 		if (strval != NULL) {
794*7c478bd9Sstevel@tonic-gate 			if (printing != pr_none)
795*7c478bd9Sstevel@tonic-gate 				pr_string(str[i].infoname, str[i].capname,
796*7c478bd9Sstevel@tonic-gate 				    str[i].fullname, strval);
797*7c478bd9Sstevel@tonic-gate 			if (common && EQUAL(str[i].val, strval)) {
798*7c478bd9Sstevel@tonic-gate 				(void) printf("\t%s= '", str[i].infoname);
799*7c478bd9Sstevel@tonic-gate 				PR(stdout, strval);
800*7c478bd9Sstevel@tonic-gate 				(void) printf("'.\n");
801*7c478bd9Sstevel@tonic-gate 			}
802*7c478bd9Sstevel@tonic-gate 		} else if (neither && (str[i].val == NULL))
803*7c478bd9Sstevel@tonic-gate 			(void) printf("\t!%s.\n", str[i].infoname);
804*7c478bd9Sstevel@tonic-gate 		if (diff && !EQUAL(str[i].val, strval)) {
805*7c478bd9Sstevel@tonic-gate 			(void) printf("\t%s: '", str[i].infoname);
806*7c478bd9Sstevel@tonic-gate 			PR(stdout, str[i].val);
807*7c478bd9Sstevel@tonic-gate 			(void) printf("','");
808*7c478bd9Sstevel@tonic-gate 			PR(stdout, strval);
809*7c478bd9Sstevel@tonic-gate 			(void) printf("'.\n");
810*7c478bd9Sstevel@tonic-gate 		}
811*7c478bd9Sstevel@tonic-gate 		if (verbose) {
812*7c478bd9Sstevel@tonic-gate 			(void) fprintf(trace, "%s: '", str[i].infoname);
813*7c478bd9Sstevel@tonic-gate 			PR(trace, str[i].val);
814*7c478bd9Sstevel@tonic-gate 			(void) fprintf(trace, "':'");
815*7c478bd9Sstevel@tonic-gate 			PR(trace, strval);
816*7c478bd9Sstevel@tonic-gate 			(void) fprintf(trace, "',changed=%d,seen=%d.\n",
817*7c478bd9Sstevel@tonic-gate 			    str[i].changed, str[i].seenagain);
818*7c478bd9Sstevel@tonic-gate 		}
819*7c478bd9Sstevel@tonic-gate 	}
820*7c478bd9Sstevel@tonic-gate 
821*7c478bd9Sstevel@tonic-gate 	if (printing == pr_cap)
822*7c478bd9Sstevel@tonic-gate 		pr_scaps();
823*7c478bd9Sstevel@tonic-gate 
824*7c478bd9Sstevel@tonic-gate 	if (printing != pr_none)
825*7c478bd9Sstevel@tonic-gate 		pr_sfooting();
826*7c478bd9Sstevel@tonic-gate 
827*7c478bd9Sstevel@tonic-gate 	return;
828*7c478bd9Sstevel@tonic-gate }
829*7c478bd9Sstevel@tonic-gate 
830*7c478bd9Sstevel@tonic-gate /*
831*7c478bd9Sstevel@tonic-gate 	A capability gets an at-sign if it no longer exists, but
832*7c478bd9Sstevel@tonic-gate 	one of the relative entries contains a value for it.
833*7c478bd9Sstevel@tonic-gate 	It gets printed if the original value is not seen in ANY
834*7c478bd9Sstevel@tonic-gate 	of the relative entries, or if the FIRST relative entry that has
835*7c478bd9Sstevel@tonic-gate 	the capability gives a DIFFERENT value for the capability.
836*7c478bd9Sstevel@tonic-gate */
837*7c478bd9Sstevel@tonic-gate void
dorelative(int firstoptind,int argc,char ** argv)838*7c478bd9Sstevel@tonic-gate dorelative(int firstoptind, int argc, char **argv)
839*7c478bd9Sstevel@tonic-gate {
840*7c478bd9Sstevel@tonic-gate 	register int i;
841*7c478bd9Sstevel@tonic-gate 
842*7c478bd9Sstevel@tonic-gate 	/* turn off printing of termcap and long names */
843*7c478bd9Sstevel@tonic-gate 	pr_init(pr_terminfo);
844*7c478bd9Sstevel@tonic-gate 
845*7c478bd9Sstevel@tonic-gate 	/* print out the entry name */
846*7c478bd9Sstevel@tonic-gate 	pr_heading((char *)0, savettytype);
847*7c478bd9Sstevel@tonic-gate 
848*7c478bd9Sstevel@tonic-gate 	pr_bheading();
849*7c478bd9Sstevel@tonic-gate 
850*7c478bd9Sstevel@tonic-gate 	/* Print out all bools that are different. */
851*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numbools; i++)
852*7c478bd9Sstevel@tonic-gate 		if (!ibool[i].val && ibool[i].changed)
853*7c478bd9Sstevel@tonic-gate 			pr_boolean(ibool[i].infoname, (char *)0,
854*7c478bd9Sstevel@tonic-gate 			    (char *)0, -1);
855*7c478bd9Sstevel@tonic-gate 		else if (ibool[i].val && (ibool[i].changed ||
856*7c478bd9Sstevel@tonic-gate 		    !ibool[i].seenagain))
857*7c478bd9Sstevel@tonic-gate 			pr_boolean(ibool[i].infoname, (char *)0, (char *)0, 1);
858*7c478bd9Sstevel@tonic-gate 
859*7c478bd9Sstevel@tonic-gate 	pr_bfooting();
860*7c478bd9Sstevel@tonic-gate 	pr_nheading();
861*7c478bd9Sstevel@tonic-gate 
862*7c478bd9Sstevel@tonic-gate 	/* Print out all nums that are different. */
863*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numnums; i++)
864*7c478bd9Sstevel@tonic-gate 		if (num[i].val < 0 && num[i].changed)
865*7c478bd9Sstevel@tonic-gate 			pr_number(num[i].infoname, (char *)0, (char *)0, -1);
866*7c478bd9Sstevel@tonic-gate 		else if (num[i].val >= 0 && (num[i].changed ||
867*7c478bd9Sstevel@tonic-gate 		    !num[i].seenagain))
868*7c478bd9Sstevel@tonic-gate 			pr_number(num[i].infoname, (char *)0,
869*7c478bd9Sstevel@tonic-gate 			    (char *)0, num[i].val);
870*7c478bd9Sstevel@tonic-gate 
871*7c478bd9Sstevel@tonic-gate 	pr_nfooting();
872*7c478bd9Sstevel@tonic-gate 	pr_sheading();
873*7c478bd9Sstevel@tonic-gate 
874*7c478bd9Sstevel@tonic-gate 	/* Print out all strs that are different. */
875*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numstrs; i++)
876*7c478bd9Sstevel@tonic-gate 		if (str[i].val == NULL && str[i].changed)
877*7c478bd9Sstevel@tonic-gate 			pr_string(str[i].infoname, (char *)0, (char *)0,
878*7c478bd9Sstevel@tonic-gate 			    (char *)0);
879*7c478bd9Sstevel@tonic-gate 		else if ((str[i].val != NULL) &&
880*7c478bd9Sstevel@tonic-gate 		    (str[i].changed || !str[i].seenagain))
881*7c478bd9Sstevel@tonic-gate 	pr_string(str[i].infoname, (char *)0, (char *)0, str[i].val);
882*7c478bd9Sstevel@tonic-gate 
883*7c478bd9Sstevel@tonic-gate 	pr_sfooting();
884*7c478bd9Sstevel@tonic-gate 
885*7c478bd9Sstevel@tonic-gate 	/* Finish it up. */
886*7c478bd9Sstevel@tonic-gate 	for (i = firstoptind; i < argc; i++)
887*7c478bd9Sstevel@tonic-gate 		if (used[i - firstoptind])
888*7c478bd9Sstevel@tonic-gate 			(void) printf("\tuse=%s,\n", argv[i]);
889*7c478bd9Sstevel@tonic-gate 		else
890*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
891*7c478bd9Sstevel@tonic-gate 			    "%s: 'use=%s' did not add anything to the "
892*7c478bd9Sstevel@tonic-gate 			    "description.\n", progname, argv[i]);
893*7c478bd9Sstevel@tonic-gate }
894*7c478bd9Sstevel@tonic-gate 
895*7c478bd9Sstevel@tonic-gate void
local_setenv(char * termNinfo)896*7c478bd9Sstevel@tonic-gate local_setenv(char *termNinfo)
897*7c478bd9Sstevel@tonic-gate {
898*7c478bd9Sstevel@tonic-gate 	extern char **environ;
899*7c478bd9Sstevel@tonic-gate 	static char *newenviron[2] = { 0, 0 };
900*7c478bd9Sstevel@tonic-gate 	static unsigned int termsize = BUFSIZ;
901*7c478bd9Sstevel@tonic-gate 	static char _terminfo[BUFSIZ];
902*7c478bd9Sstevel@tonic-gate 	static char *terminfo = &_terminfo[0];
903*7c478bd9Sstevel@tonic-gate 	register int termlen;
904*7c478bd9Sstevel@tonic-gate 
905*7c478bd9Sstevel@tonic-gate 	if (termNinfo && *termNinfo) {
906*7c478bd9Sstevel@tonic-gate 		if (verbose)
907*7c478bd9Sstevel@tonic-gate 			(void) fprintf(trace, "setting TERMINFO=%s.\n",
908*7c478bd9Sstevel@tonic-gate 			    termNinfo);
909*7c478bd9Sstevel@tonic-gate 		termlen = strlen(termNinfo);
910*7c478bd9Sstevel@tonic-gate 		if (termlen + 10 > termsize) {
911*7c478bd9Sstevel@tonic-gate 			termsize = termlen + 20;
912*7c478bd9Sstevel@tonic-gate 			terminfo = (char *) malloc(termsize * sizeof (char));
913*7c478bd9Sstevel@tonic-gate 		}
914*7c478bd9Sstevel@tonic-gate 		if (terminfo == (char *) NULL)
915*7c478bd9Sstevel@tonic-gate 			badmalloc();
916*7c478bd9Sstevel@tonic-gate 		(void) sprintf(terminfo, "TERMINFO=%s", termNinfo);
917*7c478bd9Sstevel@tonic-gate 		newenviron[0] = terminfo;
918*7c478bd9Sstevel@tonic-gate 	} else
919*7c478bd9Sstevel@tonic-gate 		newenviron[0] = (char *) 0;
920*7c478bd9Sstevel@tonic-gate 	environ = newenviron;
921*7c478bd9Sstevel@tonic-gate }
922*7c478bd9Sstevel@tonic-gate 
923*7c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)924*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
925*7c478bd9Sstevel@tonic-gate {
926*7c478bd9Sstevel@tonic-gate 	int i, c, firstoptind;
927*7c478bd9Sstevel@tonic-gate 	char *tempargv[2];
928*7c478bd9Sstevel@tonic-gate 	char *term = getenv("TERM");
929*7c478bd9Sstevel@tonic-gate 
930*7c478bd9Sstevel@tonic-gate 	term1info = term2info = getenv("TERMINFO");
931*7c478bd9Sstevel@tonic-gate 	progname = argv[0];
932*7c478bd9Sstevel@tonic-gate 
933*7c478bd9Sstevel@tonic-gate 	/* parse options */
934*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "ducnILCvV1rw:s:A:B:")) != EOF)
935*7c478bd9Sstevel@tonic-gate 		switch (c) {
936*7c478bd9Sstevel@tonic-gate 			case 'v':	verbose++;
937*7c478bd9Sstevel@tonic-gate 					break;
938*7c478bd9Sstevel@tonic-gate 			case '1':	pr_onecolumn(1);
939*7c478bd9Sstevel@tonic-gate 					break;
940*7c478bd9Sstevel@tonic-gate 			case 'w':	pr_width(atoi(optarg));
941*7c478bd9Sstevel@tonic-gate 					break;
942*7c478bd9Sstevel@tonic-gate 			case 'd':	diff++;
943*7c478bd9Sstevel@tonic-gate 					break;
944*7c478bd9Sstevel@tonic-gate 			case 'c':	common++;
945*7c478bd9Sstevel@tonic-gate 					break;
946*7c478bd9Sstevel@tonic-gate 			case 'n':	neither++;
947*7c478bd9Sstevel@tonic-gate 					break;
948*7c478bd9Sstevel@tonic-gate 			case 'u':	use++;
949*7c478bd9Sstevel@tonic-gate 					break;
950*7c478bd9Sstevel@tonic-gate 			case 'L':	pr_init(printing = pr_longnames);
951*7c478bd9Sstevel@tonic-gate 					break;
952*7c478bd9Sstevel@tonic-gate 			case 'I':	pr_init(printing = pr_terminfo);
953*7c478bd9Sstevel@tonic-gate 					break;
954*7c478bd9Sstevel@tonic-gate 			case 'C':	pr_init(printing = pr_cap);
955*7c478bd9Sstevel@tonic-gate 					break;
956*7c478bd9Sstevel@tonic-gate 			case 'A':	term1info = optarg; Aflag++;
957*7c478bd9Sstevel@tonic-gate 					break;
958*7c478bd9Sstevel@tonic-gate 			case 'B':	term2info = optarg; Bflag++;
959*7c478bd9Sstevel@tonic-gate 					break;
960*7c478bd9Sstevel@tonic-gate 			case 'r':	pr_caprestrict(0);
961*7c478bd9Sstevel@tonic-gate 					break;
962*7c478bd9Sstevel@tonic-gate 			case 's':
963*7c478bd9Sstevel@tonic-gate 				if (strcmp(optarg, "d") == 0)
964*7c478bd9Sstevel@tonic-gate 					sortorder = by_database;
965*7c478bd9Sstevel@tonic-gate 				else if (strcmp(optarg, "i") == 0)
966*7c478bd9Sstevel@tonic-gate 					sortorder = by_terminfo;
967*7c478bd9Sstevel@tonic-gate 				else if (strcmp(optarg, "l") == 0)
968*7c478bd9Sstevel@tonic-gate 					sortorder = by_longnames;
969*7c478bd9Sstevel@tonic-gate 				else if (strcmp(optarg, "c") == 0)
970*7c478bd9Sstevel@tonic-gate 					sortorder = by_cap;
971*7c478bd9Sstevel@tonic-gate 				else
972*7c478bd9Sstevel@tonic-gate 					goto usage;
973*7c478bd9Sstevel@tonic-gate 				break;
974*7c478bd9Sstevel@tonic-gate 			case 'V':
975*7c478bd9Sstevel@tonic-gate 				(void) printf("%s: version %s\n", progname,
976*7c478bd9Sstevel@tonic-gate 				    "@(#)curses:screen/infocmp.c	1.13");
977*7c478bd9Sstevel@tonic-gate 				exit(0);
978*7c478bd9Sstevel@tonic-gate 			case '?':
979*7c478bd9Sstevel@tonic-gate 				usage:
980*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
981*7c478bd9Sstevel@tonic-gate 				    "usage: %s [-ducn] [-ILC] [-1Vv] "
982*7c478bd9Sstevel@tonic-gate 				    "[-s d|i|l|c] [-A directory] "
983*7c478bd9Sstevel@tonic-gate 				    "[-B directory] term-names ...\n",
984*7c478bd9Sstevel@tonic-gate 				    progname);
985*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-d\tprint "
986*7c478bd9Sstevel@tonic-gate 				    "differences (the default for >1 "
987*7c478bd9Sstevel@tonic-gate 				    "term-name)\n");
988*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-u\tproduce "
989*7c478bd9Sstevel@tonic-gate 				    "relative description\n");
990*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-c\tprint common "
991*7c478bd9Sstevel@tonic-gate 				    "entries\n");
992*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-n\tprint entries "
993*7c478bd9Sstevel@tonic-gate 				    "in neither\n");
994*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-I\tprint terminfo "
995*7c478bd9Sstevel@tonic-gate 				    "entries (the default for 1 term-name)\n");
996*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-C\tprint termcap "
997*7c478bd9Sstevel@tonic-gate 				    "entries\n");
998*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-L\tprint long C "
999*7c478bd9Sstevel@tonic-gate 				    "variable names\n");
1000*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-1\tsingle column "
1001*7c478bd9Sstevel@tonic-gate 				    "output\n");
1002*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-V\tprint program "
1003*7c478bd9Sstevel@tonic-gate 				    "version\n");
1004*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-v\tverbose "
1005*7c478bd9Sstevel@tonic-gate 				    "debugging output\n");
1006*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-s\tchange sort "
1007*7c478bd9Sstevel@tonic-gate 				    "order\n");
1008*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-A\tset $TERMINFO "
1009*7c478bd9Sstevel@tonic-gate 				    "for first term-name\n");
1010*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-B\tset $TERMINFO "
1011*7c478bd9Sstevel@tonic-gate 				    "for other term-names\n");
1012*7c478bd9Sstevel@tonic-gate 				exit(-1);
1013*7c478bd9Sstevel@tonic-gate 		}
1014*7c478bd9Sstevel@tonic-gate 
1015*7c478bd9Sstevel@tonic-gate 	argc -= optind;
1016*7c478bd9Sstevel@tonic-gate 	argv += optind;
1017*7c478bd9Sstevel@tonic-gate 	optind = 0;
1018*7c478bd9Sstevel@tonic-gate 
1019*7c478bd9Sstevel@tonic-gate 	/* Default to $TERM for -n, -I, -C and -L options. */
1020*7c478bd9Sstevel@tonic-gate 	/* This is done by faking argv[][], argc and optind. */
1021*7c478bd9Sstevel@tonic-gate 	if (neither && (argc == 0 || argc == 1)) {
1022*7c478bd9Sstevel@tonic-gate 		if (argc == 0)
1023*7c478bd9Sstevel@tonic-gate 			tempargv[0] = term;
1024*7c478bd9Sstevel@tonic-gate 		else
1025*7c478bd9Sstevel@tonic-gate 			tempargv[0] = argv[optind];
1026*7c478bd9Sstevel@tonic-gate 		tempargv[1] = term;
1027*7c478bd9Sstevel@tonic-gate 		argc = 2;
1028*7c478bd9Sstevel@tonic-gate 		argv = tempargv;
1029*7c478bd9Sstevel@tonic-gate 		optind = 0;
1030*7c478bd9Sstevel@tonic-gate 	} else if ((printing != pr_none) && (argc == 0)) {
1031*7c478bd9Sstevel@tonic-gate 		tempargv[0] = term;
1032*7c478bd9Sstevel@tonic-gate 		argc = 1;
1033*7c478bd9Sstevel@tonic-gate 		argv = tempargv;
1034*7c478bd9Sstevel@tonic-gate 		optind = 0;
1035*7c478bd9Sstevel@tonic-gate 	}
1036*7c478bd9Sstevel@tonic-gate 
1037*7c478bd9Sstevel@tonic-gate 	/* Check for enough names. */
1038*7c478bd9Sstevel@tonic-gate 	if ((use || diff || common) && (argc <= 1)) {
1039*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1040*7c478bd9Sstevel@tonic-gate 		    "%s: must have at least two terminal names for a "
1041*7c478bd9Sstevel@tonic-gate 		    "comparison to be done.\n", progname);
1042*7c478bd9Sstevel@tonic-gate 		goto usage;
1043*7c478bd9Sstevel@tonic-gate 	}
1044*7c478bd9Sstevel@tonic-gate 
1045*7c478bd9Sstevel@tonic-gate 	/* Set the default of diff -d or print -I */
1046*7c478bd9Sstevel@tonic-gate 	if (!use && (printing == pr_none) && !common && !neither) {
1047*7c478bd9Sstevel@tonic-gate 		if (argc == 0 || argc == 1) {
1048*7c478bd9Sstevel@tonic-gate 			if (argc == 0) {
1049*7c478bd9Sstevel@tonic-gate 				tempargv[0] = term;
1050*7c478bd9Sstevel@tonic-gate 				argc = 1;
1051*7c478bd9Sstevel@tonic-gate 				argv = tempargv;
1052*7c478bd9Sstevel@tonic-gate 				optind = 0;
1053*7c478bd9Sstevel@tonic-gate 			}
1054*7c478bd9Sstevel@tonic-gate 			pr_init(printing = pr_terminfo);
1055*7c478bd9Sstevel@tonic-gate 		} else
1056*7c478bd9Sstevel@tonic-gate 			diff++;
1057*7c478bd9Sstevel@tonic-gate 	}
1058*7c478bd9Sstevel@tonic-gate 
1059*7c478bd9Sstevel@tonic-gate 	/* Set the default sorting order. */
1060*7c478bd9Sstevel@tonic-gate 	if (sortorder == none)
1061*7c478bd9Sstevel@tonic-gate 		switch ((int) printing) {
1062*7c478bd9Sstevel@tonic-gate 			case (int) pr_cap:
1063*7c478bd9Sstevel@tonic-gate 				sortorder = by_cap; break;
1064*7c478bd9Sstevel@tonic-gate 			case (int) pr_longnames:
1065*7c478bd9Sstevel@tonic-gate 				sortorder = by_longnames; break;
1066*7c478bd9Sstevel@tonic-gate 			case (int) pr_terminfo:
1067*7c478bd9Sstevel@tonic-gate 			case (int) pr_none:
1068*7c478bd9Sstevel@tonic-gate 				sortorder = by_terminfo; break;
1069*7c478bd9Sstevel@tonic-gate 		}
1070*7c478bd9Sstevel@tonic-gate 
1071*7c478bd9Sstevel@tonic-gate 	firstterm = argv[optind++];
1072*7c478bd9Sstevel@tonic-gate 	firstoptind = optind;
1073*7c478bd9Sstevel@tonic-gate 
1074*7c478bd9Sstevel@tonic-gate 	allocvariables(argc, firstoptind);
1075*7c478bd9Sstevel@tonic-gate 	sortnames();
1076*7c478bd9Sstevel@tonic-gate 
1077*7c478bd9Sstevel@tonic-gate 	devnull = open("/dev/null", O_RDWR);
1078*7c478bd9Sstevel@tonic-gate 	local_setenv(term1info);
1079*7c478bd9Sstevel@tonic-gate 	initfirstterm(firstterm);
1080*7c478bd9Sstevel@tonic-gate 	local_setenv(term2info);
1081*7c478bd9Sstevel@tonic-gate 	for (i = 0; optind < argc; optind++, i++)
1082*7c478bd9Sstevel@tonic-gate 		check_nth_terminal(argv[optind], i);
1083*7c478bd9Sstevel@tonic-gate 
1084*7c478bd9Sstevel@tonic-gate 	if (use)
1085*7c478bd9Sstevel@tonic-gate 		dorelative(firstoptind, argc, argv);
1086*7c478bd9Sstevel@tonic-gate 
1087*7c478bd9Sstevel@tonic-gate 	return (0);
1088*7c478bd9Sstevel@tonic-gate }
1089