xref: /illumos-gate/usr/src/cmd/infocmp/infocmp.c (revision bc54f855)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
29*bc54f855SJohn Levon  *
30*bc54f855SJohn Levon  * Copyright (c) 2019, Joyent, Inc.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate     NAME
357c478bd9Sstevel@tonic-gate 	infocmp - compare terminfo descriptions, or dump a terminfo
367c478bd9Sstevel@tonic-gate 	description
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate     AUTHOR
397c478bd9Sstevel@tonic-gate 	Tony Hansen, February 23, 1984.
407c478bd9Sstevel@tonic-gate */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include "curses.h"
437c478bd9Sstevel@tonic-gate #include "term.h"
447c478bd9Sstevel@tonic-gate #include "print.h"
457c478bd9Sstevel@tonic-gate #include <fcntl.h>
467c478bd9Sstevel@tonic-gate #include <stdlib.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /* externs from libcurses */
497c478bd9Sstevel@tonic-gate extern char *boolnames[];
507c478bd9Sstevel@tonic-gate extern char *boolcodes[];
517c478bd9Sstevel@tonic-gate extern char *boolfnames[];
527c478bd9Sstevel@tonic-gate extern char *numnames[];
537c478bd9Sstevel@tonic-gate extern char *numcodes[];
547c478bd9Sstevel@tonic-gate extern char *numfnames[];
557c478bd9Sstevel@tonic-gate extern char *strnames[];
567c478bd9Sstevel@tonic-gate extern char *strcodes[];
577c478bd9Sstevel@tonic-gate extern char *strfnames[];
587c478bd9Sstevel@tonic-gate extern char ttytype[];
597c478bd9Sstevel@tonic-gate extern int tgetflag();
607c478bd9Sstevel@tonic-gate extern int tgetnum();
617c478bd9Sstevel@tonic-gate extern char *tgetstr();
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /* externs from libc */
647c478bd9Sstevel@tonic-gate extern void exit();
657c478bd9Sstevel@tonic-gate extern void qsort();
667c478bd9Sstevel@tonic-gate extern char *getenv();
677c478bd9Sstevel@tonic-gate extern int getopt();
687c478bd9Sstevel@tonic-gate extern int optind;
697c478bd9Sstevel@tonic-gate extern char *optarg;
707c478bd9Sstevel@tonic-gate extern char *strncpy(), *strcpy();
717c478bd9Sstevel@tonic-gate extern int strcmp(), strlen();
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /* data structures for this program */
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate struct boolstruct {
767c478bd9Sstevel@tonic-gate     char *infoname;			/* the terminfo capability name */
777c478bd9Sstevel@tonic-gate     char *capname;			/* the termcap capability name */
787c478bd9Sstevel@tonic-gate     char *fullname;			/* the long C variable name */
797c478bd9Sstevel@tonic-gate     char *secondname;			/* the use= terminal w/ this value */
807c478bd9Sstevel@tonic-gate     char val;				/* the value */
817c478bd9Sstevel@tonic-gate     char secondval;			/* the value in the use= terminal */
827c478bd9Sstevel@tonic-gate     char changed;			/* a use= terminal changed the value */
837c478bd9Sstevel@tonic-gate     char seenagain;			/* a use= terminal had this entry */
847c478bd9Sstevel@tonic-gate     };
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate struct numstruct {
877c478bd9Sstevel@tonic-gate     char *infoname;			/* ditto from above */
887c478bd9Sstevel@tonic-gate     char *capname;
897c478bd9Sstevel@tonic-gate     char *fullname;
907c478bd9Sstevel@tonic-gate     char *secondname;
917c478bd9Sstevel@tonic-gate     short val;
927c478bd9Sstevel@tonic-gate     short secondval;
937c478bd9Sstevel@tonic-gate     char changed;
947c478bd9Sstevel@tonic-gate     char seenagain;
957c478bd9Sstevel@tonic-gate     };
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate struct strstruct {
987c478bd9Sstevel@tonic-gate     char *infoname;			/* ditto from above */
997c478bd9Sstevel@tonic-gate     char *capname;
1007c478bd9Sstevel@tonic-gate     char *fullname;
1017c478bd9Sstevel@tonic-gate     char *secondname;
1027c478bd9Sstevel@tonic-gate     char *val;
1037c478bd9Sstevel@tonic-gate     char *secondval;
1047c478bd9Sstevel@tonic-gate     char changed;
1057c478bd9Sstevel@tonic-gate     char seenagain;
1067c478bd9Sstevel@tonic-gate     };
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate /* globals for this file */
1097c478bd9Sstevel@tonic-gate char *progname;			/* argv[0], the name of the program */
1107c478bd9Sstevel@tonic-gate static struct boolstruct *ibool; /* array of char information */
1117c478bd9Sstevel@tonic-gate static struct numstruct *num;	/* array of number information */
1127c478bd9Sstevel@tonic-gate static struct strstruct *str;	/* array of string information */
1137c478bd9Sstevel@tonic-gate static char *used;		/* usage statistics */
1147c478bd9Sstevel@tonic-gate static int numbools;		/* how many booleans there are */
1157c478bd9Sstevel@tonic-gate static int numnums;		/* how many numbers there are */
1167c478bd9Sstevel@tonic-gate static int numstrs;		/* how many strings there are */
1177c478bd9Sstevel@tonic-gate #define	TTYLEN 255
1187c478bd9Sstevel@tonic-gate static char *firstterm;		/* the name of the first terminal */
1197c478bd9Sstevel@tonic-gate static char *savettytype;	/* the synonyms of the first terminal */
1207c478bd9Sstevel@tonic-gate static char _savettytype[TTYLEN+1]; /* the place to save those names */
1217c478bd9Sstevel@tonic-gate static int devnull;		/* open("/dev/null") for setupterm */
1227c478bd9Sstevel@tonic-gate #define	trace stderr		/* send trace messages to stderr */
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /* options */
1257c478bd9Sstevel@tonic-gate static int verbose = 0;		/* debugging printing level */
1267c478bd9Sstevel@tonic-gate static int diff = 0;		/* produce diff listing, the default */
1277c478bd9Sstevel@tonic-gate static int common = 0;		/* produce common listing */
1287c478bd9Sstevel@tonic-gate static int neither = 0;		/* list caps in neither entry */
1297c478bd9Sstevel@tonic-gate static int use = 0;		/* produce use= comparison listing */
1307c478bd9Sstevel@tonic-gate static enum printtypes printing	/* doing any of above printing at all */
1317c478bd9Sstevel@tonic-gate 	= pr_none;
1327c478bd9Sstevel@tonic-gate enum { none, by_database, by_terminfo, by_longnames, by_cap }
1337c478bd9Sstevel@tonic-gate     sortorder = none;		/* sort the fields for printing */
1347c478bd9Sstevel@tonic-gate static char *term1info, *term2info;	/* $TERMINFO settings */
1357c478bd9Sstevel@tonic-gate static int Aflag = 0, Bflag = 0;	/* $TERMINFO was set with -A/-B */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate #define	EQUAL(s1, s2)	(((s1 == NULL) && (s2 == NULL)) || \
1387c478bd9Sstevel@tonic-gate 			((s1 != NULL) && (s2 != NULL) && \
1397c478bd9Sstevel@tonic-gate 			(strcmp(s1, s2) == 0)))
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate static void sortnames();
1427c478bd9Sstevel@tonic-gate int numcompare(const void *, const void *);
1437c478bd9Sstevel@tonic-gate int boolcompare(const void *, const void *);
1447c478bd9Sstevel@tonic-gate int strcompare(const void *, const void *);
1457c478bd9Sstevel@tonic-gate static void check_nth_terminal(char *, int);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate void
badmalloc()1487c478bd9Sstevel@tonic-gate badmalloc()
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: malloc is out of space!\n", progname);
1517c478bd9Sstevel@tonic-gate 	exit(-1);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate     Allocate and initialize the global data structures and variables.
1567c478bd9Sstevel@tonic-gate */
1577c478bd9Sstevel@tonic-gate void
allocvariables(int argc,int firstoptind)1587c478bd9Sstevel@tonic-gate allocvariables(int argc, int firstoptind)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	register int i, nullseen;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	/* find out how many names we are dealing with */
1637c478bd9Sstevel@tonic-gate 	for (numbools = 0; boolnames[numbools]; numbools++)
1647c478bd9Sstevel@tonic-gate 		;
1657c478bd9Sstevel@tonic-gate 	for (numnums = 0; numnames[numnums]; numnums++)
1667c478bd9Sstevel@tonic-gate 		;
1677c478bd9Sstevel@tonic-gate 	for (numstrs = 0; strnames[numstrs]; numstrs++)
1687c478bd9Sstevel@tonic-gate 		;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (verbose) {
1717c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "There are %d boolean capabilities.\n",
1727c478bd9Sstevel@tonic-gate 		    numbools);
1737c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "There are %d numeric capabilities.\n",
1747c478bd9Sstevel@tonic-gate 		    numnums);
1757c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "There are %d string capabilities.\n",
1767c478bd9Sstevel@tonic-gate 		    numstrs);
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	/* Allocate storage for the names and their values */
1807c478bd9Sstevel@tonic-gate 	ibool = (struct boolstruct  *) malloc((unsigned) numbools *
1817c478bd9Sstevel@tonic-gate 	    sizeof (struct boolstruct));
1827c478bd9Sstevel@tonic-gate 	num = (struct numstruct *) malloc((unsigned) numnums *
1837c478bd9Sstevel@tonic-gate 	    sizeof (struct numstruct));
1847c478bd9Sstevel@tonic-gate 	str = (struct strstruct *) malloc((unsigned) numstrs *
1857c478bd9Sstevel@tonic-gate 	    sizeof (struct strstruct));
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	/* Allocate array to keep track of which names have been used. */
188*bc54f855SJohn Levon 	if (use) {
1897c478bd9Sstevel@tonic-gate 		used = (char *) malloc((unsigned) (argc - firstoptind) *
1907c478bd9Sstevel@tonic-gate 		    sizeof (char));
191*bc54f855SJohn Levon 	}
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	if ((ibool == NULL) || (num == NULL) || (str == NULL) ||
1947c478bd9Sstevel@tonic-gate 	    (use && (used == NULL)))
1957c478bd9Sstevel@tonic-gate 		badmalloc();
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/* Fill in the names and initialize the structures. */
1987c478bd9Sstevel@tonic-gate 	nullseen = FALSE;
1997c478bd9Sstevel@tonic-gate 	for (i = 0; i < numbools; i++) {
2007c478bd9Sstevel@tonic-gate 		ibool[i].infoname = boolnames[i];
2017c478bd9Sstevel@tonic-gate 		ibool[i].capname = boolcodes[i];
2027c478bd9Sstevel@tonic-gate 		/* This is necessary until fnames.c is */
2037c478bd9Sstevel@tonic-gate 		/* incorporated into standard curses. */
2047c478bd9Sstevel@tonic-gate 		if (nullseen || (boolfnames[i] == NULL)) {
2057c478bd9Sstevel@tonic-gate 			ibool[i].fullname = "unknown_boolean";
2067c478bd9Sstevel@tonic-gate 			nullseen = TRUE;
207*bc54f855SJohn Levon 		} else {
2087c478bd9Sstevel@tonic-gate 			ibool[i].fullname = boolfnames[i];
209*bc54f855SJohn Levon 		}
2107c478bd9Sstevel@tonic-gate 		ibool[i].changed = FALSE;
2117c478bd9Sstevel@tonic-gate 		ibool[i].seenagain = FALSE;
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 	nullseen = 0;
2147c478bd9Sstevel@tonic-gate 	for (i = 0; i < numnums; i++) {
2157c478bd9Sstevel@tonic-gate 		num[i].infoname = numnames[i];
2167c478bd9Sstevel@tonic-gate 		num[i].capname = numcodes[i];
2177c478bd9Sstevel@tonic-gate 		if (nullseen || (numfnames[i] == NULL)) {
2187c478bd9Sstevel@tonic-gate 			ibool[i].fullname = "unknown_number";
2197c478bd9Sstevel@tonic-gate 			nullseen = TRUE;
220*bc54f855SJohn Levon 		} else {
2217c478bd9Sstevel@tonic-gate 			num[i].fullname = numfnames[i];
222*bc54f855SJohn Levon 		}
2237c478bd9Sstevel@tonic-gate 		num[i].changed = FALSE;
2247c478bd9Sstevel@tonic-gate 		num[i].seenagain = FALSE;
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 	nullseen = 0;
2277c478bd9Sstevel@tonic-gate 	for (i = 0; i < numstrs; i++) {
2287c478bd9Sstevel@tonic-gate 		str[i].infoname = strnames[i];
2297c478bd9Sstevel@tonic-gate 		str[i].capname = strcodes[i];
2307c478bd9Sstevel@tonic-gate 		if (nullseen || (strfnames[i] == NULL)) {
2317c478bd9Sstevel@tonic-gate 			str[i].fullname = "unknown_string";
2327c478bd9Sstevel@tonic-gate 			nullseen = TRUE;
233*bc54f855SJohn Levon 		} else {
2347c478bd9Sstevel@tonic-gate 			str[i].fullname = strfnames[i];
235*bc54f855SJohn Levon 		}
2367c478bd9Sstevel@tonic-gate 		str[i].changed = FALSE;
2377c478bd9Sstevel@tonic-gate 		str[i].seenagain = FALSE;
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate /*
2427c478bd9Sstevel@tonic-gate 	Routines to be passed to qsort(3) for comparison of the structures.
2437c478bd9Sstevel@tonic-gate */
2447c478bd9Sstevel@tonic-gate int
boolcompare(const void * x,const void * y)2457c478bd9Sstevel@tonic-gate boolcompare(const void *x, const void *y)
2467c478bd9Sstevel@tonic-gate {
2477c478bd9Sstevel@tonic-gate 	struct boolstruct *a;
2487c478bd9Sstevel@tonic-gate 	struct boolstruct *b;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	a = (struct boolstruct *)x;
2517c478bd9Sstevel@tonic-gate 	b = (struct boolstruct *)y;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	switch ((int) sortorder) {
2547c478bd9Sstevel@tonic-gate 		case (int) by_terminfo:
2557c478bd9Sstevel@tonic-gate 			return (strcmp(a->infoname, b->infoname));
2567c478bd9Sstevel@tonic-gate 		case (int) by_cap:
2577c478bd9Sstevel@tonic-gate 			return (strcmp(a->capname, b->capname));
2587c478bd9Sstevel@tonic-gate 		case (int) by_longnames:
2597c478bd9Sstevel@tonic-gate 			return (strcmp(a->fullname, b->fullname));
2607c478bd9Sstevel@tonic-gate 		default:
2617c478bd9Sstevel@tonic-gate 			return (0);
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate int
numcompare(const void * x,const void * y)2667c478bd9Sstevel@tonic-gate numcompare(const void *x, const void *y)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate 	struct numstruct *a;
2697c478bd9Sstevel@tonic-gate 	struct numstruct *b;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	a = (struct numstruct *)x;
2727c478bd9Sstevel@tonic-gate 	b = (struct numstruct *)y;
2737c478bd9Sstevel@tonic-gate 	switch ((int) sortorder) {
2747c478bd9Sstevel@tonic-gate 		case (int) by_terminfo:
2757c478bd9Sstevel@tonic-gate 			return (strcmp(a->infoname, b->infoname));
2767c478bd9Sstevel@tonic-gate 		case (int) by_cap:
2777c478bd9Sstevel@tonic-gate 			return (strcmp(a->capname, b->capname));
2787c478bd9Sstevel@tonic-gate 		case (int) by_longnames:
2797c478bd9Sstevel@tonic-gate 			return (strcmp(a->fullname, b->fullname));
2807c478bd9Sstevel@tonic-gate 		default:
2817c478bd9Sstevel@tonic-gate 			return (0);
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate int
strcompare(const void * x,const void * y)2867c478bd9Sstevel@tonic-gate strcompare(const void *x, const void *y)
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate 	struct strstruct *a;
2897c478bd9Sstevel@tonic-gate 	struct strstruct *b;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	a = (struct strstruct *)x;
2927c478bd9Sstevel@tonic-gate 	b = (struct strstruct *)y;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	switch ((int) sortorder) {
2957c478bd9Sstevel@tonic-gate 		case (int) by_terminfo:
2967c478bd9Sstevel@tonic-gate 			return (strcmp(a->infoname, b->infoname));
2977c478bd9Sstevel@tonic-gate 		case (int) by_cap:
2987c478bd9Sstevel@tonic-gate 			return (strcmp(a->capname, b->capname));
2997c478bd9Sstevel@tonic-gate 		case (int) by_longnames:
3007c478bd9Sstevel@tonic-gate 			return (strcmp(a->fullname, b->fullname));
3017c478bd9Sstevel@tonic-gate 		default:
3027c478bd9Sstevel@tonic-gate 			return (0);
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate /*
3077c478bd9Sstevel@tonic-gate 	Sort the entries by their terminfo name.
3087c478bd9Sstevel@tonic-gate */
3097c478bd9Sstevel@tonic-gate static void
sortnames()3107c478bd9Sstevel@tonic-gate sortnames()
3117c478bd9Sstevel@tonic-gate {
3127c478bd9Sstevel@tonic-gate 	if (sortorder != by_database) {
3137c478bd9Sstevel@tonic-gate 		qsort((char *) ibool, (unsigned) numbools,
3147c478bd9Sstevel@tonic-gate 			sizeof (struct boolstruct), boolcompare);
3157c478bd9Sstevel@tonic-gate 		qsort((char *) num, (unsigned) numnums,
3167c478bd9Sstevel@tonic-gate 			sizeof (struct numstruct), numcompare);
3177c478bd9Sstevel@tonic-gate 		qsort((char *) str, (unsigned) numstrs,
3187c478bd9Sstevel@tonic-gate 			sizeof (struct strstruct), strcompare);
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 	return;
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate /*
3247c478bd9Sstevel@tonic-gate 	Print out a string, or "NULL" if it's not defined.
3257c478bd9Sstevel@tonic-gate */
3267c478bd9Sstevel@tonic-gate void
PR(FILE * stream,char * string)3277c478bd9Sstevel@tonic-gate PR(FILE *stream, char *string)
3287c478bd9Sstevel@tonic-gate {
3297c478bd9Sstevel@tonic-gate 	if (string == NULL)
3307c478bd9Sstevel@tonic-gate 		(void) fprintf(stream, "NULL");
3317c478bd9Sstevel@tonic-gate 	else
3327c478bd9Sstevel@tonic-gate 		tpr(stream, string);
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate /*
3367c478bd9Sstevel@tonic-gate 	Output the 'ko' termcap string. This is a list of all of the input
3377c478bd9Sstevel@tonic-gate 	keys that input the same thing as the corresponding output strings.
3387c478bd9Sstevel@tonic-gate */
3397c478bd9Sstevel@tonic-gate int kncounter;
3407c478bd9Sstevel@tonic-gate char kobuffer[512];
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate char
addko(char * output,char * input,char * koptr)3437c478bd9Sstevel@tonic-gate *addko(char *output, char *input, char *koptr)
3447c478bd9Sstevel@tonic-gate {
3457c478bd9Sstevel@tonic-gate 	char *inptr, *outptr, padbuffer[512];
3467c478bd9Sstevel@tonic-gate 	inptr = tgetstr(input, (char **)0);
3477c478bd9Sstevel@tonic-gate 	if (inptr == NULL)
3487c478bd9Sstevel@tonic-gate 		return (koptr);
3497c478bd9Sstevel@tonic-gate 	outptr = tgetstr(output, (char **)0);
3507c478bd9Sstevel@tonic-gate 	if (outptr == NULL)
3517c478bd9Sstevel@tonic-gate 		return (koptr);
3527c478bd9Sstevel@tonic-gate 	outptr = rmpadding(outptr, padbuffer, (int *) 0);
3537c478bd9Sstevel@tonic-gate 	if (strcmp(inptr, outptr) == 0) {
3547c478bd9Sstevel@tonic-gate 		*koptr++ = *output++;
3557c478bd9Sstevel@tonic-gate 		*koptr++ = *output++;
3567c478bd9Sstevel@tonic-gate 		*koptr++ = ',';
3577c478bd9Sstevel@tonic-gate 		kncounter++;
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 	return (koptr);
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate void
setupknko()3637c478bd9Sstevel@tonic-gate setupknko()
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate 	char *koptr;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	kncounter = 0;
3687c478bd9Sstevel@tonic-gate 	koptr = kobuffer;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	koptr = addko("bs", "kb", koptr);	/* key_backspace */
3717c478bd9Sstevel@tonic-gate 	koptr = addko("bt", "kB", koptr);	/* key_btab */
3727c478bd9Sstevel@tonic-gate 	koptr = addko("cl", "kC", koptr);	/* key_clear */
3737c478bd9Sstevel@tonic-gate 	koptr = addko("le", "kl", koptr);	/* key_left */
3747c478bd9Sstevel@tonic-gate 	koptr = addko("do", "kd", koptr);	/* key_down */
3757c478bd9Sstevel@tonic-gate 	koptr = addko("nd", "kr", koptr);	/* key_right */
3767c478bd9Sstevel@tonic-gate 	koptr = addko("up", "ku", koptr);	/* key_up */
3777c478bd9Sstevel@tonic-gate 	koptr = addko("dc", "kD", koptr);	/* key_dc */
3787c478bd9Sstevel@tonic-gate 	koptr = addko("dl", "kL", koptr);	/* key_dl */
3797c478bd9Sstevel@tonic-gate 	koptr = addko("cd", "kS", koptr);	/* key_eos */
3807c478bd9Sstevel@tonic-gate 	koptr = addko("ce", "kE", koptr);	/* key_eol */
3817c478bd9Sstevel@tonic-gate 	koptr = addko("ho", "kh", koptr);	/* key_home */
3827c478bd9Sstevel@tonic-gate 	koptr = addko("st", "kT", koptr);	/* key_stab */
3837c478bd9Sstevel@tonic-gate 	koptr = addko("ic", "kI", koptr);	/* key_ic */
3847c478bd9Sstevel@tonic-gate 	koptr = addko("im", "kI", koptr);	/* key_ic */
3857c478bd9Sstevel@tonic-gate 	koptr = addko("al", "kA", koptr);	/* key_il */
3867c478bd9Sstevel@tonic-gate 	koptr = addko("sf", "kF", koptr);	/* key_sf */
3877c478bd9Sstevel@tonic-gate 	koptr = addko("ll", "kH", koptr);	/* key_ll */
3887c478bd9Sstevel@tonic-gate 	koptr = addko("sr", "kR", koptr);	/* key_sr */
3897c478bd9Sstevel@tonic-gate 	koptr = addko("ei", "kM", koptr);	/* key_eic */
3907c478bd9Sstevel@tonic-gate 	koptr = addko("ct", "ka", koptr);	/* key_catab */
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	/* get rid of comma */
3937c478bd9Sstevel@tonic-gate 	if (koptr != kobuffer)
3947c478bd9Sstevel@tonic-gate 		*(--koptr) = '\0';
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate void
pr_kn()3987c478bd9Sstevel@tonic-gate pr_kn()
3997c478bd9Sstevel@tonic-gate {
4007c478bd9Sstevel@tonic-gate 	if (kncounter > 0)
4017c478bd9Sstevel@tonic-gate 		pr_number((char *)0, "kn", (char *)0, kncounter);
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate void
pr_ko()4057c478bd9Sstevel@tonic-gate pr_ko()
4067c478bd9Sstevel@tonic-gate {
4077c478bd9Sstevel@tonic-gate 	if (kncounter > 0)
4087c478bd9Sstevel@tonic-gate 		pr_string((char *)0, "ko", (char *)0, kobuffer);
4097c478bd9Sstevel@tonic-gate }
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate void
pr_bcaps()4127c478bd9Sstevel@tonic-gate pr_bcaps()
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate 	char *retptr;
4157c478bd9Sstevel@tonic-gate 	char padbuffer[512];
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	if (verbose)
4187c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'bs'\n");
4197c478bd9Sstevel@tonic-gate 	retptr = cconvert(rmpadding(cursor_left, padbuffer, (int *) 0));
4207c478bd9Sstevel@tonic-gate 	if (strcmp("\\b", retptr) == 0)
4217c478bd9Sstevel@tonic-gate 		pr_boolean((char *)0, "bs", (char *)0, 1);
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	if (verbose)
4247c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'pt'\n");
4257c478bd9Sstevel@tonic-gate 	retptr = cconvert(rmpadding(tab, padbuffer, (int *) 0));
4267c478bd9Sstevel@tonic-gate 	if (strcmp("\\t", retptr) == 0)
4277c478bd9Sstevel@tonic-gate 		pr_boolean((char *)0, "pt", (char *)0, 1);
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	if (verbose)
4307c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'nc'\n");
4317c478bd9Sstevel@tonic-gate 	retptr = cconvert(rmpadding(carriage_return, padbuffer, (int *) 0));
4327c478bd9Sstevel@tonic-gate 	if (strcmp("\\r", retptr) != 0)
4337c478bd9Sstevel@tonic-gate 		pr_boolean((char *)0, "nc", (char *)0, 1);
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	if (verbose)
4367c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'ns'\n");
4377c478bd9Sstevel@tonic-gate 	if (scroll_forward == NULL)
4387c478bd9Sstevel@tonic-gate 		pr_boolean((char *)0, "ns", (char *)0, 1);
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	/* Ignore "xr": Return acts like ce \r \n (Delta Data) */
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate void
pr_ncaps()4447c478bd9Sstevel@tonic-gate pr_ncaps()
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate 	char padbuffer[512];
4477c478bd9Sstevel@tonic-gate 	int padding;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	if (verbose)
4507c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'ug'\n");
4517c478bd9Sstevel@tonic-gate 	/* Duplicate sg for ug: Number of blank chars left by us or ue */
4527c478bd9Sstevel@tonic-gate 	if (magic_cookie_glitch > -1)
4537c478bd9Sstevel@tonic-gate 		pr_number((char *)0, "ug", (char *)0, magic_cookie_glitch);
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	if (verbose)
4567c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'dB'\n");
4577c478bd9Sstevel@tonic-gate 	/* Number of millisec of bs delay needed */
4587c478bd9Sstevel@tonic-gate 	(void) rmpadding(cursor_left, padbuffer, &padding);
4597c478bd9Sstevel@tonic-gate 	if (padding > 0)
4607c478bd9Sstevel@tonic-gate 		pr_number((char *)0, "dB", (char *)0, padding);
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	if (verbose)
4637c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'dC'\n");
4647c478bd9Sstevel@tonic-gate 	/* Number of millisec of cr delay needed */
4657c478bd9Sstevel@tonic-gate 	(void) rmpadding(carriage_return, padbuffer, &padding);
4667c478bd9Sstevel@tonic-gate 	if (padding > 0)
4677c478bd9Sstevel@tonic-gate 		pr_number((char *)0, "dC", (char *)0, padding);
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	if (verbose)
4707c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'dF'\n");
4717c478bd9Sstevel@tonic-gate 	/* Number of millisec of ff delay needed */
4727c478bd9Sstevel@tonic-gate 	(void) rmpadding(form_feed, padbuffer, &padding);
4737c478bd9Sstevel@tonic-gate 	if (padding > 0)
4747c478bd9Sstevel@tonic-gate 		pr_number((char *)0, "dF", (char *)0, padding);
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	if (verbose)
4777c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'dN'\n");
4787c478bd9Sstevel@tonic-gate 	/* Number of millisec of nl delay needed */
4797c478bd9Sstevel@tonic-gate 	(void) rmpadding(cursor_down, padbuffer, &padding);
4807c478bd9Sstevel@tonic-gate 	if (padding > 0)
4817c478bd9Sstevel@tonic-gate 		pr_number((char *)0, "dN", (char *)0, padding);
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	if (verbose)
4847c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'dT'\n");
4857c478bd9Sstevel@tonic-gate 	/* Number of millisec of tab delay needed */
4867c478bd9Sstevel@tonic-gate 	(void) rmpadding(tab, padbuffer, &padding);
4877c478bd9Sstevel@tonic-gate 	if (padding > 0)
4887c478bd9Sstevel@tonic-gate 		pr_number((char *)0, "dT", (char *)0, padding);
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	/* Handle "kn": Number of "other" keys */
4917c478bd9Sstevel@tonic-gate 	setupknko();
4927c478bd9Sstevel@tonic-gate 	pr_kn();
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate void
pr_scaps()4967c478bd9Sstevel@tonic-gate pr_scaps()
4977c478bd9Sstevel@tonic-gate {
4987c478bd9Sstevel@tonic-gate 	char *retptr;
4997c478bd9Sstevel@tonic-gate 	char padbuffer[512];
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	/* Backspace if not "^H" */
5027c478bd9Sstevel@tonic-gate 	if (verbose)
5037c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'bc'\n");
5047c478bd9Sstevel@tonic-gate 	retptr = cconvert(rmpadding(cursor_left, padbuffer, (int *) 0));
5057c478bd9Sstevel@tonic-gate 	if (strcmp("\\b", retptr) != 0)
5067c478bd9Sstevel@tonic-gate 		pr_string((char *)0, "bc", (char *)0, cursor_left);
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	/* Newline character (default "\n") */
5097c478bd9Sstevel@tonic-gate 	if (verbose)
5107c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "looking at 'nl'\n");
5117c478bd9Sstevel@tonic-gate 	retptr = cconvert(rmpadding(cursor_down, padbuffer, (int *) 0));
5127c478bd9Sstevel@tonic-gate 	if (strcmp("\\n", retptr) != 0)
5137c478bd9Sstevel@tonic-gate 		pr_string((char *)0, "nl", (char *)0, cursor_down);
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	/* Handle "ko" here: Termcap entries for other non-function keys */
5167c478bd9Sstevel@tonic-gate 	pr_ko();
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	/* Ignore "ma": Arrow key map, used by vi version 2 only */
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate /*
5227c478bd9Sstevel@tonic-gate 	Set up the first terminal and save the values from it.
5237c478bd9Sstevel@tonic-gate */
5247c478bd9Sstevel@tonic-gate void
initfirstterm(char * term)5257c478bd9Sstevel@tonic-gate initfirstterm(char *term)
5267c478bd9Sstevel@tonic-gate {
5277c478bd9Sstevel@tonic-gate 	register int i;
5287c478bd9Sstevel@tonic-gate 
529*bc54f855SJohn Levon 	if (verbose) {
5307c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "setting up terminal type '%s'.\n",
5317c478bd9Sstevel@tonic-gate 		    term);
532*bc54f855SJohn Levon 	}
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	(void) setupterm(term, devnull, (int *) 0);
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	/* Save the name for later use. */
5377c478bd9Sstevel@tonic-gate 	if (use) {
5387c478bd9Sstevel@tonic-gate 		register unsigned int length;
5397c478bd9Sstevel@tonic-gate 		savettytype = _savettytype;
5407c478bd9Sstevel@tonic-gate 		if ((length = strlen(ttytype)) >= TTYLEN) {
5417c478bd9Sstevel@tonic-gate 			savettytype = malloc(length);
5427c478bd9Sstevel@tonic-gate 			if (savettytype == NULL) {
5437c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "%s: malloc is out "
5447c478bd9Sstevel@tonic-gate 				    "of space\n", progname);
5457c478bd9Sstevel@tonic-gate 				(void) strncpy(_savettytype, ttytype,
5467c478bd9Sstevel@tonic-gate 				    TTYLEN-1);
5477c478bd9Sstevel@tonic-gate 				_savettytype[TTYLEN] = '\0';
5487c478bd9Sstevel@tonic-gate 				savettytype = _savettytype;
5497c478bd9Sstevel@tonic-gate 			}
550*bc54f855SJohn Levon 		} else {
5517c478bd9Sstevel@tonic-gate 			(void) strcpy(_savettytype, ttytype);
552*bc54f855SJohn Levon 		}
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	if (printing != pr_none) {
5567c478bd9Sstevel@tonic-gate 		pr_heading(term, ttytype);
5577c478bd9Sstevel@tonic-gate 		pr_bheading();
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	/* Save the values for the first terminal. */
5617c478bd9Sstevel@tonic-gate 	for (i = 0; i < numbools; i++) {
5627c478bd9Sstevel@tonic-gate 		if ((ibool[i].val = tgetflag(ibool[i].capname)) &&
563*bc54f855SJohn Levon 		    printing != pr_none) {
5647c478bd9Sstevel@tonic-gate 			pr_boolean(ibool[i].infoname, ibool[i].capname,
5657c478bd9Sstevel@tonic-gate 			    ibool[i].fullname, 1);
566*bc54f855SJohn Levon 		}
567*bc54f855SJohn Levon 
568*bc54f855SJohn Levon 		if (verbose) {
5697c478bd9Sstevel@tonic-gate 			(void) fprintf(trace, "%s=%d.\n", ibool[i].infoname,
5707c478bd9Sstevel@tonic-gate 			    ibool[i].val);
571*bc54f855SJohn Levon 		}
5727c478bd9Sstevel@tonic-gate 	}
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	if (printing != pr_none) {
5757c478bd9Sstevel@tonic-gate 		if (printing == pr_cap)
5767c478bd9Sstevel@tonic-gate 			pr_bcaps();
5777c478bd9Sstevel@tonic-gate 		pr_bfooting();
5787c478bd9Sstevel@tonic-gate 		pr_nheading();
5797c478bd9Sstevel@tonic-gate 	}
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	for (i = 0; i < numnums; i++) {
5827c478bd9Sstevel@tonic-gate 		if (((num[i].val = tgetnum(num[i].capname)) > -1) &&
583*bc54f855SJohn Levon 		    printing != pr_none) {
5847c478bd9Sstevel@tonic-gate 			pr_number(num[i].infoname, num[i].capname,
5857c478bd9Sstevel@tonic-gate 			    num[i].fullname, num[i].val);
586*bc54f855SJohn Levon 		}
587*bc54f855SJohn Levon 
588*bc54f855SJohn Levon 		if (verbose) {
5897c478bd9Sstevel@tonic-gate 			(void) fprintf(trace, "%s=%d.\n", num[i].infoname,
5907c478bd9Sstevel@tonic-gate 			    num[i].val);
591*bc54f855SJohn Levon 		}
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	if (printing != pr_none) {
5957c478bd9Sstevel@tonic-gate 		if (printing == pr_cap)
5967c478bd9Sstevel@tonic-gate 			pr_ncaps();
5977c478bd9Sstevel@tonic-gate 		pr_nfooting();
5987c478bd9Sstevel@tonic-gate 		pr_sheading();
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	for (i = 0; i < numstrs; i++) {
6027c478bd9Sstevel@tonic-gate 		str[i].val = tgetstr(str[i].capname, (char **)0);
603*bc54f855SJohn Levon 		if ((str[i].val != NULL) && printing != pr_none) {
6047c478bd9Sstevel@tonic-gate 			pr_string(str[i].infoname, str[i].capname,
6057c478bd9Sstevel@tonic-gate 			    str[i].fullname, str[i].val);
606*bc54f855SJohn Levon 		}
607*bc54f855SJohn Levon 
6087c478bd9Sstevel@tonic-gate 		if (verbose) {
6097c478bd9Sstevel@tonic-gate 			(void) fprintf(trace, "%s='", str[i].infoname);
6107c478bd9Sstevel@tonic-gate 			PR(trace, str[i].val);
6117c478bd9Sstevel@tonic-gate 			(void) fprintf(trace, "'.\n");
6127c478bd9Sstevel@tonic-gate 		}
6137c478bd9Sstevel@tonic-gate 	}
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	if (printing == pr_cap)
6167c478bd9Sstevel@tonic-gate 		pr_scaps();
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	if (printing != pr_none)
6197c478bd9Sstevel@tonic-gate 		pr_sfooting();
6207c478bd9Sstevel@tonic-gate }
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate /*
6237c478bd9Sstevel@tonic-gate 	Set up the n'th terminal.
6247c478bd9Sstevel@tonic-gate */
6257c478bd9Sstevel@tonic-gate static void
check_nth_terminal(char * nterm,int n)6267c478bd9Sstevel@tonic-gate check_nth_terminal(char *nterm, int n)
6277c478bd9Sstevel@tonic-gate {
6287c478bd9Sstevel@tonic-gate 	register char boolval;
6297c478bd9Sstevel@tonic-gate 	register short numval;
6307c478bd9Sstevel@tonic-gate 	register char *strval;
6317c478bd9Sstevel@tonic-gate 	register int i;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	if (use)
6347c478bd9Sstevel@tonic-gate 		used[n] = FALSE;
6357c478bd9Sstevel@tonic-gate 
636*bc54f855SJohn Levon 	if (verbose) {
6377c478bd9Sstevel@tonic-gate 		(void) fprintf(trace, "adding in terminal type '%s'.\n",
6387c478bd9Sstevel@tonic-gate 		    nterm);
639*bc54f855SJohn Levon 	}
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	(void) setupterm(nterm, devnull, (int *) 0);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	if (printing != pr_none) {
6447c478bd9Sstevel@tonic-gate 		pr_heading(nterm, ttytype);
6457c478bd9Sstevel@tonic-gate 		pr_bheading();
6467c478bd9Sstevel@tonic-gate 	}
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	if (diff || common || neither) {
6497c478bd9Sstevel@tonic-gate 		if (Aflag && Bflag)
6507c478bd9Sstevel@tonic-gate 			(void) printf("comparing %s (TERMINFO=%s) to %s "
6517c478bd9Sstevel@tonic-gate 			    "(TERMINFO=%s).\n",
6527c478bd9Sstevel@tonic-gate 			firstterm, term1info, nterm, term2info);
6537c478bd9Sstevel@tonic-gate 		else if (Aflag)
6547c478bd9Sstevel@tonic-gate 			(void) printf("comparing %s (TERMINFO=%s) to %s.\n",
6557c478bd9Sstevel@tonic-gate 			    firstterm, term1info, nterm);
6567c478bd9Sstevel@tonic-gate 		else if (Bflag)
6577c478bd9Sstevel@tonic-gate 			(void) printf("comparing %s to %s (TERMINFO=%s).\n",
6587c478bd9Sstevel@tonic-gate 			    firstterm, nterm, term2info);
6597c478bd9Sstevel@tonic-gate 		else
6607c478bd9Sstevel@tonic-gate 			(void) printf("comparing %s to %s.\n",
6617c478bd9Sstevel@tonic-gate 			    firstterm, nterm);
6627c478bd9Sstevel@tonic-gate 		(void) printf("    comparing booleans.\n");
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	/* save away the values for the nth terminal */
6667c478bd9Sstevel@tonic-gate 	for (i = 0; i < numbools; i++) {
6677c478bd9Sstevel@tonic-gate 		boolval = tgetflag(ibool[i].capname);
6687c478bd9Sstevel@tonic-gate 		if (use) {
6697c478bd9Sstevel@tonic-gate 			if (ibool[i].seenagain) {
6707c478bd9Sstevel@tonic-gate 			/*
6717c478bd9Sstevel@tonic-gate 			** We do not have to worry about this impossible case
6727c478bd9Sstevel@tonic-gate 			** since booleans can have only two values: true and
6737c478bd9Sstevel@tonic-gate 			** false.
6747c478bd9Sstevel@tonic-gate 			** if (boolval && (boolval != ibool[i].secondval))
6757c478bd9Sstevel@tonic-gate 			**  {
6767c478bd9Sstevel@tonic-gate 			**  (void) fprintf(trace, "use= order dependency"
6777c478bd9Sstevel@tonic-gate 			**  "found:\n");
6787c478bd9Sstevel@tonic-gate 			**  (void) fprintf(trace, "    %s: %s has %d, %s has"
6797c478bd9Sstevel@tonic-gate 			**   " %d.\n",
6807c478bd9Sstevel@tonic-gate 			**	ibool[i].capname, ibool[i].secondname,
6817c478bd9Sstevel@tonic-gate 			**	ibool[i].secondval, nterm, boolval);
6827c478bd9Sstevel@tonic-gate 			**  }
6837c478bd9Sstevel@tonic-gate 			*/
6847c478bd9Sstevel@tonic-gate 			} else {
6857c478bd9Sstevel@tonic-gate 				if (boolval == TRUE) {
6867c478bd9Sstevel@tonic-gate 					ibool[i].seenagain = TRUE;
6877c478bd9Sstevel@tonic-gate 					ibool[i].secondval = boolval;
6887c478bd9Sstevel@tonic-gate 					ibool[i].secondname = nterm;
6897c478bd9Sstevel@tonic-gate 					if (ibool[i].val != boolval)
6907c478bd9Sstevel@tonic-gate 						ibool[i].changed = TRUE;
6917c478bd9Sstevel@tonic-gate 					else
6927c478bd9Sstevel@tonic-gate 						used[n] = TRUE;
6937c478bd9Sstevel@tonic-gate 				}
6947c478bd9Sstevel@tonic-gate 			}
6957c478bd9Sstevel@tonic-gate 		}
6967c478bd9Sstevel@tonic-gate 		if (boolval) {
697*bc54f855SJohn Levon 			if (printing != pr_none) {
6987c478bd9Sstevel@tonic-gate 				pr_boolean(ibool[i].infoname, ibool[i].capname,
6997c478bd9Sstevel@tonic-gate 				    ibool[i].fullname, 1);
700*bc54f855SJohn Levon 			}
701*bc54f855SJohn Levon 
7027c478bd9Sstevel@tonic-gate 			if (common && (ibool[i].val == boolval))
7037c478bd9Sstevel@tonic-gate 				(void) printf("\t%s= T.\n", ibool[i].infoname);
704*bc54f855SJohn Levon 		} else if (neither && !ibool[i].val) {
7057c478bd9Sstevel@tonic-gate 			(void) printf("\t!%s.\n", ibool[i].infoname);
706*bc54f855SJohn Levon 		}
7077c478bd9Sstevel@tonic-gate 		if (diff && (ibool[i].val != boolval))
7087c478bd9Sstevel@tonic-gate 			(void) printf("\t%s: %c:%c.\n", ibool[i].infoname,
7097c478bd9Sstevel@tonic-gate 			    ibool[i].val?'T':'F', boolval?'T':'F');
710*bc54f855SJohn Levon 		if (verbose) {
7117c478bd9Sstevel@tonic-gate 			(void) fprintf(trace, "%s: %d:%d, changed=%d, "
7127c478bd9Sstevel@tonic-gate 			    "seen=%d.\n", ibool[i].infoname, ibool[i].val,
7137c478bd9Sstevel@tonic-gate 			    boolval, ibool[i].changed, ibool[i].seenagain);
714*bc54f855SJohn Levon 		}
7157c478bd9Sstevel@tonic-gate 	}
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	if (printing != pr_none) {
7187c478bd9Sstevel@tonic-gate 		if (printing == pr_cap)
7197c478bd9Sstevel@tonic-gate 			pr_bcaps();
7207c478bd9Sstevel@tonic-gate 		pr_bfooting();
7217c478bd9Sstevel@tonic-gate 		pr_nheading();
7227c478bd9Sstevel@tonic-gate 	}
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	if (diff || common || neither)
7257c478bd9Sstevel@tonic-gate 		(void) printf("    comparing numbers.\n");
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	for (i = 0; i < numnums; i++) {
7287c478bd9Sstevel@tonic-gate 		numval = tgetnum(num[i].capname);
7297c478bd9Sstevel@tonic-gate 		if (use) {
7307c478bd9Sstevel@tonic-gate 			if (num[i].seenagain) {
7317c478bd9Sstevel@tonic-gate 				if ((numval > -1) &&
7327c478bd9Sstevel@tonic-gate 				    (numval != num[i].secondval)) {
7337c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
7347c478bd9Sstevel@tonic-gate 					    "%s: use = order dependency "
7357c478bd9Sstevel@tonic-gate 					    "found:\n", progname);
7367c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, "    %s: %s "
7377c478bd9Sstevel@tonic-gate 					    "has %d, %s has %d.\n",
7387c478bd9Sstevel@tonic-gate 					    num[i].capname, num[i].secondname,
7397c478bd9Sstevel@tonic-gate 					    num[i].secondval, nterm, numval);
7407c478bd9Sstevel@tonic-gate 				}
7417c478bd9Sstevel@tonic-gate 			} else {
7427c478bd9Sstevel@tonic-gate 				if (numval > -1) {
7437c478bd9Sstevel@tonic-gate 					num[i].seenagain = TRUE;
7447c478bd9Sstevel@tonic-gate 					num[i].secondval = numval;
7457c478bd9Sstevel@tonic-gate 					num[i].secondname = nterm;
7467c478bd9Sstevel@tonic-gate 					if ((numval > -1) &&
7477c478bd9Sstevel@tonic-gate 					    (num[i].val != numval))
7487c478bd9Sstevel@tonic-gate 						num[i].changed = TRUE;
7497c478bd9Sstevel@tonic-gate 					else
7507c478bd9Sstevel@tonic-gate 						used[n] = TRUE;
7517c478bd9Sstevel@tonic-gate 				}
7527c478bd9Sstevel@tonic-gate 			}
7537c478bd9Sstevel@tonic-gate 		}
7547c478bd9Sstevel@tonic-gate 		if (numval > -1) {
755*bc54f855SJohn Levon 			if (printing != pr_none) {
7567c478bd9Sstevel@tonic-gate 				pr_number(num[i].infoname, num[i].capname,
7577c478bd9Sstevel@tonic-gate 				    num[i].fullname, numval);
758*bc54f855SJohn Levon 			}
759*bc54f855SJohn Levon 
760*bc54f855SJohn Levon 			if (common && (num[i].val == numval)) {
7617c478bd9Sstevel@tonic-gate 				(void) printf("\t%s= %d.\n", num[i].infoname,
7627c478bd9Sstevel@tonic-gate 				    numval);
763*bc54f855SJohn Levon 			}
764*bc54f855SJohn Levon 
765*bc54f855SJohn Levon 		} else if (neither && (num[i].val == -1)) {
766*bc54f855SJohn Levon 			(void) printf("\t!%s.\n", num[i].infoname);
767*bc54f855SJohn Levon 		}
768*bc54f855SJohn Levon 
769*bc54f855SJohn Levon 		if (diff && (num[i].val != numval)) {
770*bc54f855SJohn Levon 			(void) printf("\t%s: %d:%d.\n",
771*bc54f855SJohn Levon 			    num[i].infoname, num[i].val, numval);
772*bc54f855SJohn Levon 		}
773*bc54f855SJohn Levon 
774*bc54f855SJohn Levon 		if (verbose) {
775*bc54f855SJohn Levon 			(void) fprintf(trace, "%s: %d:%d, "
776*bc54f855SJohn Levon 			    "changed = %d, seen = %d.\n",
777*bc54f855SJohn Levon 			    num[i].infoname, num[i].val, numval,
778*bc54f855SJohn Levon 			    num[i].changed, num[i].seenagain);
779*bc54f855SJohn Levon 		}
7807c478bd9Sstevel@tonic-gate 	}
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	if (printing != pr_none) {
7837c478bd9Sstevel@tonic-gate 		if (printing == pr_cap)
7847c478bd9Sstevel@tonic-gate 			pr_ncaps();
7857c478bd9Sstevel@tonic-gate 		pr_nfooting();
7867c478bd9Sstevel@tonic-gate 		pr_sheading();
7877c478bd9Sstevel@tonic-gate 	}
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	if (diff || common || neither)
7907c478bd9Sstevel@tonic-gate 		(void) printf("    comparing strings.\n");
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	for (i = 0; i < numstrs; i++) {
7937c478bd9Sstevel@tonic-gate 		strval = tgetstr(str[i].capname, (char **)0);
7947c478bd9Sstevel@tonic-gate 		if (use) {
7957c478bd9Sstevel@tonic-gate 			if (str[i].seenagain && (strval != NULL)) {
7967c478bd9Sstevel@tonic-gate 				if (!EQUAL(strval, str[i].secondval)) {
7977c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
7987c478bd9Sstevel@tonic-gate 					    "use= order dependency"
7997c478bd9Sstevel@tonic-gate 					    "  found:\n");
8007c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
8017c478bd9Sstevel@tonic-gate 					    "    %s: %s has '",
8027c478bd9Sstevel@tonic-gate 					    str[i].capname, str[i].secondname);
8037c478bd9Sstevel@tonic-gate 					PR(stderr, str[i].secondval);
8047c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
8057c478bd9Sstevel@tonic-gate 					    "', %s has '", nterm);
8067c478bd9Sstevel@tonic-gate 					PR(stderr, strval);
8077c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, "'.\n");
8087c478bd9Sstevel@tonic-gate 				}
8097c478bd9Sstevel@tonic-gate 			} else {
8107c478bd9Sstevel@tonic-gate 				if (strval != NULL) {
8117c478bd9Sstevel@tonic-gate 					str[i].seenagain = TRUE;
8127c478bd9Sstevel@tonic-gate 					str[i].secondval = strval;
8137c478bd9Sstevel@tonic-gate 					str[i].secondname = nterm;
8147c478bd9Sstevel@tonic-gate 					if (!EQUAL(str[i].val, strval))
8157c478bd9Sstevel@tonic-gate 						str[i].changed = TRUE;
8167c478bd9Sstevel@tonic-gate 					else
8177c478bd9Sstevel@tonic-gate 						used[n] = TRUE;
8187c478bd9Sstevel@tonic-gate 				}
8197c478bd9Sstevel@tonic-gate 			}
8207c478bd9Sstevel@tonic-gate 		}
8217c478bd9Sstevel@tonic-gate 		if (strval != NULL) {
822*bc54f855SJohn Levon 			if (printing != pr_none) {
8237c478bd9Sstevel@tonic-gate 				pr_string(str[i].infoname, str[i].capname,
8247c478bd9Sstevel@tonic-gate 				    str[i].fullname, strval);
825*bc54f855SJohn Levon 			}
826*bc54f855SJohn Levon 
8277c478bd9Sstevel@tonic-gate 			if (common && EQUAL(str[i].val, strval)) {
8287c478bd9Sstevel@tonic-gate 				(void) printf("\t%s= '", str[i].infoname);
8297c478bd9Sstevel@tonic-gate 				PR(stdout, strval);
8307c478bd9Sstevel@tonic-gate 				(void) printf("'.\n");
8317c478bd9Sstevel@tonic-gate 			}
8327c478bd9Sstevel@tonic-gate 		} else if (neither && (str[i].val == NULL))
8337c478bd9Sstevel@tonic-gate 			(void) printf("\t!%s.\n", str[i].infoname);
8347c478bd9Sstevel@tonic-gate 		if (diff && !EQUAL(str[i].val, strval)) {
8357c478bd9Sstevel@tonic-gate 			(void) printf("\t%s: '", str[i].infoname);
8367c478bd9Sstevel@tonic-gate 			PR(stdout, str[i].val);
8377c478bd9Sstevel@tonic-gate 			(void) printf("','");
8387c478bd9Sstevel@tonic-gate 			PR(stdout, strval);
8397c478bd9Sstevel@tonic-gate 			(void) printf("'.\n");
8407c478bd9Sstevel@tonic-gate 		}
8417c478bd9Sstevel@tonic-gate 		if (verbose) {
8427c478bd9Sstevel@tonic-gate 			(void) fprintf(trace, "%s: '", str[i].infoname);
8437c478bd9Sstevel@tonic-gate 			PR(trace, str[i].val);
8447c478bd9Sstevel@tonic-gate 			(void) fprintf(trace, "':'");
8457c478bd9Sstevel@tonic-gate 			PR(trace, strval);
8467c478bd9Sstevel@tonic-gate 			(void) fprintf(trace, "',changed=%d,seen=%d.\n",
8477c478bd9Sstevel@tonic-gate 			    str[i].changed, str[i].seenagain);
8487c478bd9Sstevel@tonic-gate 		}
8497c478bd9Sstevel@tonic-gate 	}
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	if (printing == pr_cap)
8527c478bd9Sstevel@tonic-gate 		pr_scaps();
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	if (printing != pr_none)
8557c478bd9Sstevel@tonic-gate 		pr_sfooting();
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	return;
8587c478bd9Sstevel@tonic-gate }
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate /*
8617c478bd9Sstevel@tonic-gate 	A capability gets an at-sign if it no longer exists, but
8627c478bd9Sstevel@tonic-gate 	one of the relative entries contains a value for it.
8637c478bd9Sstevel@tonic-gate 	It gets printed if the original value is not seen in ANY
8647c478bd9Sstevel@tonic-gate 	of the relative entries, or if the FIRST relative entry that has
8657c478bd9Sstevel@tonic-gate 	the capability gives a DIFFERENT value for the capability.
8667c478bd9Sstevel@tonic-gate */
8677c478bd9Sstevel@tonic-gate void
dorelative(int firstoptind,int argc,char ** argv)8687c478bd9Sstevel@tonic-gate dorelative(int firstoptind, int argc, char **argv)
8697c478bd9Sstevel@tonic-gate {
8707c478bd9Sstevel@tonic-gate 	register int i;
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	/* turn off printing of termcap and long names */
8737c478bd9Sstevel@tonic-gate 	pr_init(pr_terminfo);
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	/* print out the entry name */
8767c478bd9Sstevel@tonic-gate 	pr_heading((char *)0, savettytype);
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	pr_bheading();
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	/* Print out all bools that are different. */
881*bc54f855SJohn Levon 	for (i = 0; i < numbools; i++) {
882*bc54f855SJohn Levon 		if (!ibool[i].val && ibool[i].changed) {
8837c478bd9Sstevel@tonic-gate 			pr_boolean(ibool[i].infoname, (char *)0,
8847c478bd9Sstevel@tonic-gate 			    (char *)0, -1);
885*bc54f855SJohn Levon 		} else if (ibool[i].val && (ibool[i].changed ||
886*bc54f855SJohn Levon 		    !ibool[i].seenagain)) {
8877c478bd9Sstevel@tonic-gate 			pr_boolean(ibool[i].infoname, (char *)0, (char *)0, 1);
888*bc54f855SJohn Levon 		}
889*bc54f855SJohn Levon 	}
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	pr_bfooting();
8927c478bd9Sstevel@tonic-gate 	pr_nheading();
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	/* Print out all nums that are different. */
895*bc54f855SJohn Levon 	for (i = 0; i < numnums; i++) {
896*bc54f855SJohn Levon 		if (num[i].val < 0 && num[i].changed) {
8977c478bd9Sstevel@tonic-gate 			pr_number(num[i].infoname, (char *)0, (char *)0, -1);
898*bc54f855SJohn Levon 		} else if (num[i].val >= 0 && (num[i].changed ||
899*bc54f855SJohn Levon 		    !num[i].seenagain)) {
9007c478bd9Sstevel@tonic-gate 			pr_number(num[i].infoname, (char *)0,
9017c478bd9Sstevel@tonic-gate 			    (char *)0, num[i].val);
902*bc54f855SJohn Levon 		}
903*bc54f855SJohn Levon 	}
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	pr_nfooting();
9067c478bd9Sstevel@tonic-gate 	pr_sheading();
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	/* Print out all strs that are different. */
909*bc54f855SJohn Levon 	for (i = 0; i < numstrs; i++) {
910*bc54f855SJohn Levon 		if (str[i].val == NULL && str[i].changed) {
9117c478bd9Sstevel@tonic-gate 			pr_string(str[i].infoname, (char *)0, (char *)0,
9127c478bd9Sstevel@tonic-gate 			    (char *)0);
913*bc54f855SJohn Levon 		} else if ((str[i].val != NULL) &&
914*bc54f855SJohn Levon 		    (str[i].changed || !str[i].seenagain)) {
915*bc54f855SJohn Levon 			pr_string(str[i].infoname,
916*bc54f855SJohn Levon 			    (char *)0, (char *)0, str[i].val);
917*bc54f855SJohn Levon 		}
918*bc54f855SJohn Levon 	}
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	pr_sfooting();
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	/* Finish it up. */
923*bc54f855SJohn Levon 	for (i = firstoptind; i < argc; i++) {
924*bc54f855SJohn Levon 		if (used[i - firstoptind]) {
9257c478bd9Sstevel@tonic-gate 			(void) printf("\tuse=%s,\n", argv[i]);
926*bc54f855SJohn Levon 		} else {
9277c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9287c478bd9Sstevel@tonic-gate 			    "%s: 'use=%s' did not add anything to the "
9297c478bd9Sstevel@tonic-gate 			    "description.\n", progname, argv[i]);
930*bc54f855SJohn Levon 		}
931*bc54f855SJohn Levon 	}
9327c478bd9Sstevel@tonic-gate }
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate void
local_setenv(char * termNinfo)9357c478bd9Sstevel@tonic-gate local_setenv(char *termNinfo)
9367c478bd9Sstevel@tonic-gate {
9377c478bd9Sstevel@tonic-gate 	extern char **environ;
9387c478bd9Sstevel@tonic-gate 	static char *newenviron[2] = { 0, 0 };
9397c478bd9Sstevel@tonic-gate 	static unsigned int termsize = BUFSIZ;
9407c478bd9Sstevel@tonic-gate 	static char _terminfo[BUFSIZ];
9417c478bd9Sstevel@tonic-gate 	static char *terminfo = &_terminfo[0];
9427c478bd9Sstevel@tonic-gate 	register int termlen;
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	if (termNinfo && *termNinfo) {
945*bc54f855SJohn Levon 		if (verbose) {
9467c478bd9Sstevel@tonic-gate 			(void) fprintf(trace, "setting TERMINFO=%s.\n",
9477c478bd9Sstevel@tonic-gate 			    termNinfo);
948*bc54f855SJohn Levon 		}
949*bc54f855SJohn Levon 
9507c478bd9Sstevel@tonic-gate 		termlen = strlen(termNinfo);
9517c478bd9Sstevel@tonic-gate 		if (termlen + 10 > termsize) {
9527c478bd9Sstevel@tonic-gate 			termsize = termlen + 20;
9537c478bd9Sstevel@tonic-gate 			terminfo = (char *) malloc(termsize * sizeof (char));
9547c478bd9Sstevel@tonic-gate 		}
9557c478bd9Sstevel@tonic-gate 		if (terminfo == (char *) NULL)
9567c478bd9Sstevel@tonic-gate 			badmalloc();
9577c478bd9Sstevel@tonic-gate 		(void) sprintf(terminfo, "TERMINFO=%s", termNinfo);
9587c478bd9Sstevel@tonic-gate 		newenviron[0] = terminfo;
9597c478bd9Sstevel@tonic-gate 	} else
9607c478bd9Sstevel@tonic-gate 		newenviron[0] = (char *) 0;
9617c478bd9Sstevel@tonic-gate 	environ = newenviron;
9627c478bd9Sstevel@tonic-gate }
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)9657c478bd9Sstevel@tonic-gate main(int argc, char **argv)
9667c478bd9Sstevel@tonic-gate {
9677c478bd9Sstevel@tonic-gate 	int i, c, firstoptind;
9687c478bd9Sstevel@tonic-gate 	char *tempargv[2];
9697c478bd9Sstevel@tonic-gate 	char *term = getenv("TERM");
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	term1info = term2info = getenv("TERMINFO");
9727c478bd9Sstevel@tonic-gate 	progname = argv[0];
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	/* parse options */
9757c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "ducnILCvV1rw:s:A:B:")) != EOF)
9767c478bd9Sstevel@tonic-gate 		switch (c) {
9777c478bd9Sstevel@tonic-gate 			case 'v':	verbose++;
9787c478bd9Sstevel@tonic-gate 					break;
9797c478bd9Sstevel@tonic-gate 			case '1':	pr_onecolumn(1);
9807c478bd9Sstevel@tonic-gate 					break;
9817c478bd9Sstevel@tonic-gate 			case 'w':	pr_width(atoi(optarg));
9827c478bd9Sstevel@tonic-gate 					break;
9837c478bd9Sstevel@tonic-gate 			case 'd':	diff++;
9847c478bd9Sstevel@tonic-gate 					break;
9857c478bd9Sstevel@tonic-gate 			case 'c':	common++;
9867c478bd9Sstevel@tonic-gate 					break;
9877c478bd9Sstevel@tonic-gate 			case 'n':	neither++;
9887c478bd9Sstevel@tonic-gate 					break;
9897c478bd9Sstevel@tonic-gate 			case 'u':	use++;
9907c478bd9Sstevel@tonic-gate 					break;
9917c478bd9Sstevel@tonic-gate 			case 'L':	pr_init(printing = pr_longnames);
9927c478bd9Sstevel@tonic-gate 					break;
9937c478bd9Sstevel@tonic-gate 			case 'I':	pr_init(printing = pr_terminfo);
9947c478bd9Sstevel@tonic-gate 					break;
9957c478bd9Sstevel@tonic-gate 			case 'C':	pr_init(printing = pr_cap);
9967c478bd9Sstevel@tonic-gate 					break;
9977c478bd9Sstevel@tonic-gate 			case 'A':	term1info = optarg; Aflag++;
9987c478bd9Sstevel@tonic-gate 					break;
9997c478bd9Sstevel@tonic-gate 			case 'B':	term2info = optarg; Bflag++;
10007c478bd9Sstevel@tonic-gate 					break;
10017c478bd9Sstevel@tonic-gate 			case 'r':	pr_caprestrict(0);
10027c478bd9Sstevel@tonic-gate 					break;
10037c478bd9Sstevel@tonic-gate 			case 's':
10047c478bd9Sstevel@tonic-gate 				if (strcmp(optarg, "d") == 0)
10057c478bd9Sstevel@tonic-gate 					sortorder = by_database;
10067c478bd9Sstevel@tonic-gate 				else if (strcmp(optarg, "i") == 0)
10077c478bd9Sstevel@tonic-gate 					sortorder = by_terminfo;
10087c478bd9Sstevel@tonic-gate 				else if (strcmp(optarg, "l") == 0)
10097c478bd9Sstevel@tonic-gate 					sortorder = by_longnames;
10107c478bd9Sstevel@tonic-gate 				else if (strcmp(optarg, "c") == 0)
10117c478bd9Sstevel@tonic-gate 					sortorder = by_cap;
10127c478bd9Sstevel@tonic-gate 				else
10137c478bd9Sstevel@tonic-gate 					goto usage;
10147c478bd9Sstevel@tonic-gate 				break;
10157c478bd9Sstevel@tonic-gate 			case 'V':
10167c478bd9Sstevel@tonic-gate 				(void) printf("%s: version %s\n", progname,
10177c478bd9Sstevel@tonic-gate 				    "@(#)curses:screen/infocmp.c	1.13");
10187c478bd9Sstevel@tonic-gate 				exit(0);
10197c478bd9Sstevel@tonic-gate 			case '?':
10207c478bd9Sstevel@tonic-gate 				usage:
10217c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10227c478bd9Sstevel@tonic-gate 				    "usage: %s [-ducn] [-ILC] [-1Vv] "
10237c478bd9Sstevel@tonic-gate 				    "[-s d|i|l|c] [-A directory] "
10247c478bd9Sstevel@tonic-gate 				    "[-B directory] term-names ...\n",
10257c478bd9Sstevel@tonic-gate 				    progname);
10267c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-d\tprint "
10277c478bd9Sstevel@tonic-gate 				    "differences (the default for >1 "
10287c478bd9Sstevel@tonic-gate 				    "term-name)\n");
10297c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-u\tproduce "
10307c478bd9Sstevel@tonic-gate 				    "relative description\n");
10317c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-c\tprint common "
10327c478bd9Sstevel@tonic-gate 				    "entries\n");
10337c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-n\tprint entries "
10347c478bd9Sstevel@tonic-gate 				    "in neither\n");
10357c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-I\tprint terminfo "
10367c478bd9Sstevel@tonic-gate 				    "entries (the default for 1 term-name)\n");
10377c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-C\tprint termcap "
10387c478bd9Sstevel@tonic-gate 				    "entries\n");
10397c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-L\tprint long C "
10407c478bd9Sstevel@tonic-gate 				    "variable names\n");
10417c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-1\tsingle column "
10427c478bd9Sstevel@tonic-gate 				    "output\n");
10437c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-V\tprint program "
10447c478bd9Sstevel@tonic-gate 				    "version\n");
10457c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-v\tverbose "
10467c478bd9Sstevel@tonic-gate 				    "debugging output\n");
10477c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-s\tchange sort "
10487c478bd9Sstevel@tonic-gate 				    "order\n");
10497c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-A\tset $TERMINFO "
10507c478bd9Sstevel@tonic-gate 				    "for first term-name\n");
10517c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\t-B\tset $TERMINFO "
10527c478bd9Sstevel@tonic-gate 				    "for other term-names\n");
10537c478bd9Sstevel@tonic-gate 				exit(-1);
10547c478bd9Sstevel@tonic-gate 		}
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	argc -= optind;
10577c478bd9Sstevel@tonic-gate 	argv += optind;
10587c478bd9Sstevel@tonic-gate 	optind = 0;
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	/* Default to $TERM for -n, -I, -C and -L options. */
10617c478bd9Sstevel@tonic-gate 	/* This is done by faking argv[][], argc and optind. */
10627c478bd9Sstevel@tonic-gate 	if (neither && (argc == 0 || argc == 1)) {
10637c478bd9Sstevel@tonic-gate 		if (argc == 0)
10647c478bd9Sstevel@tonic-gate 			tempargv[0] = term;
10657c478bd9Sstevel@tonic-gate 		else
10667c478bd9Sstevel@tonic-gate 			tempargv[0] = argv[optind];
10677c478bd9Sstevel@tonic-gate 		tempargv[1] = term;
10687c478bd9Sstevel@tonic-gate 		argc = 2;
10697c478bd9Sstevel@tonic-gate 		argv = tempargv;
10707c478bd9Sstevel@tonic-gate 		optind = 0;
10717c478bd9Sstevel@tonic-gate 	} else if ((printing != pr_none) && (argc == 0)) {
10727c478bd9Sstevel@tonic-gate 		tempargv[0] = term;
10737c478bd9Sstevel@tonic-gate 		argc = 1;
10747c478bd9Sstevel@tonic-gate 		argv = tempargv;
10757c478bd9Sstevel@tonic-gate 		optind = 0;
10767c478bd9Sstevel@tonic-gate 	}
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	/* Check for enough names. */
10797c478bd9Sstevel@tonic-gate 	if ((use || diff || common) && (argc <= 1)) {
10807c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
10817c478bd9Sstevel@tonic-gate 		    "%s: must have at least two terminal names for a "
10827c478bd9Sstevel@tonic-gate 		    "comparison to be done.\n", progname);
10837c478bd9Sstevel@tonic-gate 		goto usage;
10847c478bd9Sstevel@tonic-gate 	}
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	/* Set the default of diff -d or print -I */
10877c478bd9Sstevel@tonic-gate 	if (!use && (printing == pr_none) && !common && !neither) {
10887c478bd9Sstevel@tonic-gate 		if (argc == 0 || argc == 1) {
10897c478bd9Sstevel@tonic-gate 			if (argc == 0) {
10907c478bd9Sstevel@tonic-gate 				tempargv[0] = term;
10917c478bd9Sstevel@tonic-gate 				argc = 1;
10927c478bd9Sstevel@tonic-gate 				argv = tempargv;
10937c478bd9Sstevel@tonic-gate 				optind = 0;
10947c478bd9Sstevel@tonic-gate 			}
10957c478bd9Sstevel@tonic-gate 			pr_init(printing = pr_terminfo);
1096*bc54f855SJohn Levon 		} else {
10977c478bd9Sstevel@tonic-gate 			diff++;
1098*bc54f855SJohn Levon 		}
10997c478bd9Sstevel@tonic-gate 	}
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	/* Set the default sorting order. */
1102*bc54f855SJohn Levon 	if (sortorder == none) {
11037c478bd9Sstevel@tonic-gate 		switch ((int) printing) {
11047c478bd9Sstevel@tonic-gate 			case (int) pr_cap:
11057c478bd9Sstevel@tonic-gate 				sortorder = by_cap; break;
11067c478bd9Sstevel@tonic-gate 			case (int) pr_longnames:
11077c478bd9Sstevel@tonic-gate 				sortorder = by_longnames; break;
11087c478bd9Sstevel@tonic-gate 			case (int) pr_terminfo:
11097c478bd9Sstevel@tonic-gate 			case (int) pr_none:
11107c478bd9Sstevel@tonic-gate 				sortorder = by_terminfo; break;
11117c478bd9Sstevel@tonic-gate 		}
1112*bc54f855SJohn Levon 	}
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	firstterm = argv[optind++];
11157c478bd9Sstevel@tonic-gate 	firstoptind = optind;
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	allocvariables(argc, firstoptind);
11187c478bd9Sstevel@tonic-gate 	sortnames();
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	devnull = open("/dev/null", O_RDWR);
11217c478bd9Sstevel@tonic-gate 	local_setenv(term1info);
11227c478bd9Sstevel@tonic-gate 	initfirstterm(firstterm);
11237c478bd9Sstevel@tonic-gate 	local_setenv(term2info);
11247c478bd9Sstevel@tonic-gate 	for (i = 0; optind < argc; optind++, i++)
11257c478bd9Sstevel@tonic-gate 		check_nth_terminal(argv[optind], i);
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	if (use)
11287c478bd9Sstevel@tonic-gate 		dorelative(firstoptind, argc, argv);
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	return (0);
11317c478bd9Sstevel@tonic-gate }
1132