Home | History | Annotate | Line # | Download | only in infocmp
infocmp.c revision 1.1
      1  1.1  roy /* $NetBSD: infocmp.c,v 1.1 2010/02/03 15:16:33 roy Exp $ */
      2  1.1  roy 
      3  1.1  roy /*
      4  1.1  roy  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5  1.1  roy  *
      6  1.1  roy  * This code is derived from software contributed to The NetBSD Foundation
      7  1.1  roy  * by Roy Marples.
      8  1.1  roy  *
      9  1.1  roy  * Redistribution and use in source and binary forms, with or without
     10  1.1  roy  * modification, are permitted provided that the following conditions
     11  1.1  roy  * are met:
     12  1.1  roy  * 1. Redistributions of source code must retain the above copyright
     13  1.1  roy  *    notice, this list of conditions and the following disclaimer.
     14  1.1  roy  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  roy  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  roy  *    documentation and/or other materials provided with the distribution.
     17  1.1  roy  *
     18  1.1  roy  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.1  roy  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.1  roy  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.1  roy  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.1  roy  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  1.1  roy  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  1.1  roy  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  1.1  roy  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  1.1  roy  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  1.1  roy  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1  roy  */
     29  1.1  roy 
     30  1.1  roy #include <sys/cdefs.h>
     31  1.1  roy __RCSID("$NetBSD: infocmp.c,v 1.1 2010/02/03 15:16:33 roy Exp $");
     32  1.1  roy 
     33  1.1  roy #include <sys/ioctl.h>
     34  1.1  roy 
     35  1.1  roy #include <ctype.h>
     36  1.1  roy #include <err.h>
     37  1.1  roy #include <stdio.h>
     38  1.1  roy #include <stdlib.h>
     39  1.1  roy #include <string.h>
     40  1.1  roy #include <term_private.h>
     41  1.1  roy #include <term.h>
     42  1.1  roy #include <unistd.h>
     43  1.1  roy 
     44  1.1  roy #define SW 8
     45  1.1  roy 
     46  1.1  roy typedef struct tient {
     47  1.1  roy 	char type;
     48  1.1  roy 	const char *id;
     49  1.1  roy 	char flag;
     50  1.1  roy 	short num;
     51  1.1  roy 	const char *str;
     52  1.1  roy } TIENT;
     53  1.1  roy 
     54  1.1  roy static size_t cols;
     55  1.1  roy static int aflag, cflag, nflag, qflag, xflag, raw;
     56  1.1  roy 
     57  1.1  roy static size_t
     58  1.1  roy outstr(FILE *f, const char *str)
     59  1.1  roy {
     60  1.1  roy 	unsigned char ch;
     61  1.1  roy 	size_t r, l;
     62  1.1  roy 
     63  1.1  roy 	r = 0;
     64  1.1  roy 	l = strlen(str);
     65  1.1  roy 	while ((ch = (unsigned char)(*str++)) != '\0') {
     66  1.1  roy 		switch (ch) {
     67  1.1  roy 		case 128:
     68  1.1  roy 			ch = '0';
     69  1.1  roy 			break;
     70  1.1  roy 		case '\033':
     71  1.1  roy 			ch = 'E';
     72  1.1  roy 			break;
     73  1.1  roy 		case '\014':
     74  1.1  roy 			ch = 'f';
     75  1.1  roy 			break;
     76  1.1  roy 		case '^': /* FALLTHROUGH */
     77  1.1  roy 		case ',': /* escape these */
     78  1.1  roy 			break;
     79  1.1  roy 		case ' ':
     80  1.1  roy 			ch = 's';
     81  1.1  roy 			break;
     82  1.1  roy 		default:
     83  1.1  roy 			if (ch == '\177') {
     84  1.1  roy 				if (f != NULL)
     85  1.1  roy 					fputc('^', f);
     86  1.1  roy 				ch = '?';
     87  1.1  roy 				r++;
     88  1.1  roy 			} else if (iscntrl(ch) &&
     89  1.1  roy 			    ch < 128 &&
     90  1.1  roy 			    ch != '\\' &&
     91  1.1  roy 			    (l < 4 || isdigit((unsigned char)*str)))
     92  1.1  roy 			{
     93  1.1  roy 				if (f != NULL)
     94  1.1  roy 					fputc('^', f);
     95  1.1  roy 				ch += '@';
     96  1.1  roy 				r++;
     97  1.1  roy 			} else if (!isprint(ch)) {
     98  1.1  roy 				if (f != NULL)
     99  1.1  roy 					fprintf(f, "\\%03o", ch);
    100  1.1  roy 				r += 4;
    101  1.1  roy 				continue;
    102  1.1  roy 			}
    103  1.1  roy 			goto prnt;
    104  1.1  roy 		}
    105  1.1  roy 
    106  1.1  roy 		if (f != NULL)
    107  1.1  roy 			fputc('\\', f);
    108  1.1  roy 		r++;
    109  1.1  roy prnt:
    110  1.1  roy 		if (f != NULL)
    111  1.1  roy 			fputc(ch, f);
    112  1.1  roy 		r++;
    113  1.1  roy 	}
    114  1.1  roy 	return r;
    115  1.1  roy }
    116  1.1  roy 
    117  1.1  roy static int
    118  1.1  roy ent_compare(const void *a, const void *b)
    119  1.1  roy {
    120  1.1  roy 	const TIENT *ta, *tb;
    121  1.1  roy 
    122  1.1  roy 	ta = (const TIENT *)a;
    123  1.1  roy 	tb = (const TIENT *)b;
    124  1.1  roy 	return strcmp(ta->id, tb->id);
    125  1.1  roy }
    126  1.1  roy 
    127  1.1  roy static void
    128  1.1  roy setdb(char *db)
    129  1.1  roy {
    130  1.1  roy 	size_t len;
    131  1.1  roy 
    132  1.1  roy 	len = strlen(db);
    133  1.1  roy 	if (len > 3 &&
    134  1.1  roy 	    db[len - 3] == '.' &&
    135  1.1  roy 	    db[len - 2] == 'd' &&
    136  1.1  roy 	    db[len - 1] == 'b')
    137  1.1  roy 		db[len - 3] = '\0';
    138  1.1  roy 	setenv("TERMINFO", db, 1);
    139  1.1  roy }
    140  1.1  roy 
    141  1.1  roy static void
    142  1.1  roy print_ent(const TIENT *ents, size_t nents)
    143  1.1  roy {
    144  1.1  roy 	size_t col, i, l;
    145  1.1  roy 	char nbuf[64];
    146  1.1  roy 
    147  1.1  roy 	if (nents == 0)
    148  1.1  roy 		return;
    149  1.1  roy 
    150  1.1  roy 	col = SW;
    151  1.1  roy 	printf("\t");
    152  1.1  roy 	for (i = 0; i < nents; i++) {
    153  1.1  roy 		if (*ents[i].id == '.' && aflag == 0)
    154  1.1  roy 			continue;
    155  1.1  roy 		switch (ents[i].type) {
    156  1.1  roy 		case 'f':
    157  1.1  roy 			if (ents[i].flag == ABSENT_BOOLEAN)
    158  1.1  roy 				continue;
    159  1.1  roy 			l = strlen(ents[i].id) + 2;
    160  1.1  roy 			if (ents[i].flag == CANCELLED_BOOLEAN)
    161  1.1  roy 				l++;
    162  1.1  roy 			break;
    163  1.1  roy 		case 'n':
    164  1.1  roy 			if (ents[i].num == ABSENT_NUMERIC)
    165  1.1  roy 				continue;
    166  1.1  roy 			if (VALID_NUMERIC(ents[i].num))
    167  1.1  roy 				l = snprintf(nbuf, sizeof(nbuf), "%s#%d,",
    168  1.1  roy 				    ents[i].id, ents[i].num);
    169  1.1  roy 			else
    170  1.1  roy 				l = snprintf(nbuf, sizeof(nbuf), "%s@,",
    171  1.1  roy 				    ents[i].id);
    172  1.1  roy 			break;
    173  1.1  roy 		case 's':
    174  1.1  roy 			if (ents[i].str == ABSENT_STRING)
    175  1.1  roy 				continue;
    176  1.1  roy 			if (VALID_STRING(ents[i].str))
    177  1.1  roy 				l = strlen(ents[i].id) +
    178  1.1  roy 				    outstr(NULL, ents[i].str) + 7;
    179  1.1  roy 			else
    180  1.1  roy 				l = strlen(ents[i].id) + 3;
    181  1.1  roy 			break;
    182  1.1  roy 		default:
    183  1.1  roy 			errx(1, "invalid type");
    184  1.1  roy 		}
    185  1.1  roy 		if (col != SW) {
    186  1.1  roy 			if (col + l > cols) {
    187  1.1  roy 				printf("\n\t");
    188  1.1  roy 				col = SW;
    189  1.1  roy 			} else
    190  1.1  roy 				col += printf(" ");
    191  1.1  roy 		}
    192  1.1  roy 		switch (ents[i].type) {
    193  1.1  roy 		case 'f':
    194  1.1  roy 			col += printf("%s", ents[i].id);
    195  1.1  roy 			if (ents[i].flag == ABSENT_BOOLEAN ||
    196  1.1  roy 			    ents[i].flag == CANCELLED_BOOLEAN)
    197  1.1  roy 				col += printf("@");
    198  1.1  roy 			col += printf(",");
    199  1.1  roy 			break;
    200  1.1  roy 		case 'n':
    201  1.1  roy 			col += printf("%s", nbuf);
    202  1.1  roy 			break;
    203  1.1  roy 		case 's':
    204  1.1  roy 			col += printf("%s", ents[i].id);
    205  1.1  roy 			if (VALID_STRING(ents[i].str)) {
    206  1.1  roy 				col += printf("=");
    207  1.1  roy 				col += outstr(stdout, ents[i].str);
    208  1.1  roy 			} else
    209  1.1  roy 				col += printf("@");
    210  1.1  roy 			col += printf(",");
    211  1.1  roy 			break;
    212  1.1  roy 		}
    213  1.1  roy 	}
    214  1.1  roy 	printf("\n");
    215  1.1  roy }
    216  1.1  roy 
    217  1.1  roy static size_t
    218  1.1  roy load_ents(TIENT *ents, TERMINAL *t, char type)
    219  1.1  roy {
    220  1.1  roy 	size_t i, n, max;
    221  1.1  roy 	TERMUSERDEF *ud;
    222  1.1  roy 
    223  1.1  roy 	switch (type) {
    224  1.1  roy 	case 'f':
    225  1.1  roy 		max = TIFLAGMAX;
    226  1.1  roy 		break;
    227  1.1  roy 	case 'n':
    228  1.1  roy 		max = TINUMMAX;
    229  1.1  roy 		break;
    230  1.1  roy 	default:
    231  1.1  roy 		max = TISTRMAX;
    232  1.1  roy 	}
    233  1.1  roy 
    234  1.1  roy 	n = 0;
    235  1.1  roy 	if (xflag < 2) {
    236  1.1  roy 		for (i = 0; i <= max; i++) {
    237  1.1  roy 			switch (type) {
    238  1.1  roy 			case 'f':
    239  1.1  roy 				if (t->flags[i] == 1 ||
    240  1.1  roy 				    (raw != 0 &&
    241  1.1  roy 					t->flags[i] == CANCELLED_BOOLEAN))
    242  1.1  roy 				{
    243  1.1  roy 					ents[n].id = _ti_flagid(i);
    244  1.1  roy 					ents[n].type = 'f';
    245  1.1  roy 					ents[n++].flag = t->flags[i];
    246  1.1  roy 				}
    247  1.1  roy 				break;
    248  1.1  roy 			case 'n':
    249  1.1  roy 				if (VALID_NUMERIC(t->nums[i]) ||
    250  1.1  roy 				    (raw != 0 &&
    251  1.1  roy 					t->nums[i] == CANCELLED_NUMERIC))
    252  1.1  roy 				{
    253  1.1  roy 					ents[n].id = _ti_numid(i);
    254  1.1  roy 					ents[n].type = 'n';
    255  1.1  roy 					ents[n++].num = t->nums[i];
    256  1.1  roy 				}
    257  1.1  roy 				break;
    258  1.1  roy 			default:
    259  1.1  roy 				if (VALID_STRING(t->strs[i]) ||
    260  1.1  roy 				    (raw != 0 &&
    261  1.1  roy 					t->strs[i] == CANCELLED_STRING))
    262  1.1  roy 				{
    263  1.1  roy 					ents[n].id = _ti_strid(i);
    264  1.1  roy 					ents[n].type = 's';
    265  1.1  roy 					ents[n++].str = t->strs[i];
    266  1.1  roy 				}
    267  1.1  roy 				break;
    268  1.1  roy 			}
    269  1.1  roy 		}
    270  1.1  roy 	}
    271  1.1  roy 
    272  1.1  roy 	if (xflag != 0 && t->_nuserdefs != 0) {
    273  1.1  roy 		for (i = 0; i < t->_nuserdefs; i++) {
    274  1.1  roy 			ud = &t->_userdefs[i];
    275  1.1  roy 			if (ud->type == type) {
    276  1.1  roy 				switch (type) {
    277  1.1  roy 				case 'f':
    278  1.1  roy 					if (raw == 0 &&
    279  1.1  roy 					    !VALID_BOOLEAN(ud->flag))
    280  1.1  roy 						continue;
    281  1.1  roy 					break;
    282  1.1  roy 				case 'n':
    283  1.1  roy 					if (raw == 0 &&
    284  1.1  roy 					    !VALID_NUMERIC(ud->num))
    285  1.1  roy 						continue;
    286  1.1  roy 					break;
    287  1.1  roy 				case 's':
    288  1.1  roy 					if (raw == 0 &&
    289  1.1  roy 					    !VALID_STRING(ud->str))
    290  1.1  roy 						continue;
    291  1.1  roy 					break;
    292  1.1  roy 				}
    293  1.1  roy 				ents[n].id = ud->id;
    294  1.1  roy 				ents[n].type = ud->type;
    295  1.1  roy 				ents[n].flag = ud->flag;
    296  1.1  roy 				ents[n].num = ud->num;
    297  1.1  roy 				ents[n++].str = ud->str;
    298  1.1  roy 			}
    299  1.1  roy 		}
    300  1.1  roy 	}
    301  1.1  roy 
    302  1.1  roy 	qsort(ents, n, sizeof(TIENT), ent_compare);
    303  1.1  roy 	return n;
    304  1.1  roy }
    305  1.1  roy 
    306  1.1  roy static void
    307  1.1  roy cprint_ent(TIENT *ent)
    308  1.1  roy {
    309  1.1  roy 
    310  1.1  roy 	if (ent == NULL) {
    311  1.1  roy 		if (qflag == 0)
    312  1.1  roy 			printf("NULL");
    313  1.1  roy 		else
    314  1.1  roy 			printf("-");
    315  1.1  roy 	}
    316  1.1  roy 
    317  1.1  roy 	switch (ent->type) {
    318  1.1  roy 	case 'f':
    319  1.1  roy 		if (VALID_BOOLEAN(ent->flag))
    320  1.1  roy 			printf(ent->flag == 1 ? "T" : "F");
    321  1.1  roy 		else if (qflag == 0)
    322  1.1  roy 			printf("F");
    323  1.1  roy 		else if (ent->flag == CANCELLED_BOOLEAN)
    324  1.1  roy 			printf("@");
    325  1.1  roy 		else
    326  1.1  roy 			printf("-");
    327  1.1  roy 		break;
    328  1.1  roy 	case 'n':
    329  1.1  roy 		if (VALID_NUMERIC(ent->num))
    330  1.1  roy 			printf("%d", ent->num);
    331  1.1  roy 		else if (qflag == 0)
    332  1.1  roy 			printf("NULL");
    333  1.1  roy 		else if (ent->num == CANCELLED_NUMERIC)
    334  1.1  roy 			printf("@");
    335  1.1  roy 		else
    336  1.1  roy 			printf("-");
    337  1.1  roy 		break;
    338  1.1  roy 	case 's':
    339  1.1  roy 		if (VALID_STRING(ent->str)) {
    340  1.1  roy 			printf("'");
    341  1.1  roy 			outstr(stdout, ent->str);
    342  1.1  roy 			printf("'");
    343  1.1  roy 		} else if (qflag == 0)
    344  1.1  roy 			printf("NULL");
    345  1.1  roy 		else if (ent->str == CANCELLED_STRING)
    346  1.1  roy 			printf("@");
    347  1.1  roy 		else
    348  1.1  roy 			printf("-");
    349  1.1  roy 		break;
    350  1.1  roy 	}
    351  1.1  roy }
    352  1.1  roy 
    353  1.1  roy static void
    354  1.1  roy compare_ents(TIENT *ents1, size_t n1, TIENT *ents2, size_t n2)
    355  1.1  roy {
    356  1.1  roy 	size_t i1, i2;
    357  1.1  roy 	TIENT *e1, *e2, ee;
    358  1.1  roy 	int c;
    359  1.1  roy 
    360  1.1  roy 	i1 = i2 = 0;
    361  1.1  roy 	ee.type = 'f';
    362  1.1  roy 	ee.flag = ABSENT_BOOLEAN;
    363  1.1  roy 	ee.num = ABSENT_NUMERIC;
    364  1.1  roy 	ee.str = ABSENT_STRING;
    365  1.1  roy 	while (i1 != n1 || i2 != n2) {
    366  1.1  roy 		if (i1 == n1)
    367  1.1  roy 			c = 1;
    368  1.1  roy 		else if (i2 == n2)
    369  1.1  roy 			c = -1;
    370  1.1  roy 		else
    371  1.1  roy 			c = strcmp(ents1[i1].id, ents2[i2].id);
    372  1.1  roy 		if (c == 0) {
    373  1.1  roy 			e1 = &ents1[i1++];
    374  1.1  roy 			e2 = &ents2[i2++];
    375  1.1  roy 		} else if (c < 0) {
    376  1.1  roy 			e1 = &ents1[i1++];
    377  1.1  roy 			e2 = &ee;
    378  1.1  roy 			ee.id = e1->id;
    379  1.1  roy 			ee.type = e1->type;
    380  1.1  roy 		} else {
    381  1.1  roy 			e1 = &ee;
    382  1.1  roy 			e2 = &ents2[i2++];
    383  1.1  roy 			ee.id = e2->id;
    384  1.1  roy 			ee.type = e2->type;
    385  1.1  roy 		}
    386  1.1  roy 		switch (e1->type) {
    387  1.1  roy 		case 'f':
    388  1.1  roy 			if (cflag != 0) {
    389  1.1  roy 				if (e1->flag == e2->flag)
    390  1.1  roy 					printf("\t%s\n", ents1[i1].id);
    391  1.1  roy 				continue;
    392  1.1  roy 			}
    393  1.1  roy 			if (e1->flag == e2->flag)
    394  1.1  roy 				continue;
    395  1.1  roy 			break;
    396  1.1  roy 		case 'n':
    397  1.1  roy 			if (cflag != 0) {
    398  1.1  roy 				if (e1->num == e2->num)
    399  1.1  roy 					printf("\t%s#%d\n",
    400  1.1  roy 					    ents1[i1].id, ents1[i1].num);
    401  1.1  roy 				continue;
    402  1.1  roy 			}
    403  1.1  roy 			if (e1->num == e2->num)
    404  1.1  roy 				continue;
    405  1.1  roy 			break;
    406  1.1  roy 		case 's':
    407  1.1  roy 			if (cflag != 0) {
    408  1.1  roy 				if (VALID_STRING(e1->str) &&
    409  1.1  roy 				    VALID_STRING(e2->str) &&
    410  1.1  roy 				    strcmp(e1->str, e2->str) == 0) {
    411  1.1  roy 					printf("\t%s=", ents1[i1].id);
    412  1.1  roy 					outstr(stdout, ents1[i1].str);
    413  1.1  roy 					printf("\n");
    414  1.1  roy 				}
    415  1.1  roy 				continue;
    416  1.1  roy 			}
    417  1.1  roy 			if (VALID_STRING(e1->str) &&
    418  1.1  roy 			    VALID_STRING(e2->str) &&
    419  1.1  roy 			    strcmp(e1->str, e2->str) == 0)
    420  1.1  roy 				continue;
    421  1.1  roy 			break;
    422  1.1  roy 		}
    423  1.1  roy 		printf("\t%s: ", e1->id);
    424  1.1  roy 		cprint_ent(e1);
    425  1.1  roy 		if (e1->type == 'f')
    426  1.1  roy 			printf(":");
    427  1.1  roy 		else
    428  1.1  roy 			printf(", ");
    429  1.1  roy 		cprint_ent(e2);
    430  1.1  roy 		printf(".\n");
    431  1.1  roy 	}
    432  1.1  roy }
    433  1.1  roy 
    434  1.1  roy static TERMINAL *
    435  1.1  roy load_term(const char *name)
    436  1.1  roy {
    437  1.1  roy 	TERMINAL *t;
    438  1.1  roy 
    439  1.1  roy 	t = calloc(1, sizeof(*t));
    440  1.1  roy 	if (t == NULL)
    441  1.1  roy 		err(1, "calloc");
    442  1.1  roy 	if (name == NULL)
    443  1.1  roy 		name = getenv("TERM");
    444  1.1  roy 	if (name == NULL)
    445  1.1  roy 		name = "dumb";
    446  1.1  roy 	if (_ti_getterm(t, name, 1) != 1) /* load the raw data */
    447  1.1  roy 		errx(1, "no terminal definition found in %s.db", _ti_database);
    448  1.1  roy 	return t;
    449  1.1  roy }
    450  1.1  roy 
    451  1.1  roy static void
    452  1.1  roy show_missing(TERMINAL *t1, TERMINAL *t2, char type)
    453  1.1  roy {
    454  1.1  roy 	ssize_t i, max;
    455  1.1  roy 	const char *id;
    456  1.1  roy 
    457  1.1  roy 	switch (type) {
    458  1.1  roy 	case 'f':
    459  1.1  roy 		max = TIFLAGMAX;
    460  1.1  roy 		break;
    461  1.1  roy 	case 'n':
    462  1.1  roy 		max = TINUMMAX;
    463  1.1  roy 		break;
    464  1.1  roy 	default:
    465  1.1  roy 		max = TISTRMAX;
    466  1.1  roy 	}
    467  1.1  roy 
    468  1.1  roy 	for (i = 0; i <= max; i++) {
    469  1.1  roy 		switch (type) {
    470  1.1  roy 		case 'f':
    471  1.1  roy 			if (t1->flags[i] != ABSENT_BOOLEAN ||
    472  1.1  roy 			    t2->flags[i] != ABSENT_BOOLEAN)
    473  1.1  roy 				continue;
    474  1.1  roy 			id = _ti_flagid(i);
    475  1.1  roy 			break;
    476  1.1  roy 		case 'n':
    477  1.1  roy 			if (t1->nums[i] != ABSENT_NUMERIC ||
    478  1.1  roy 			    t2->nums[i] != ABSENT_NUMERIC)
    479  1.1  roy 				continue;
    480  1.1  roy 			id = _ti_numid(i);
    481  1.1  roy 			break;
    482  1.1  roy 		default:
    483  1.1  roy 			if (t1->strs[i] != ABSENT_STRING ||
    484  1.1  roy 			    t2->strs[i] != ABSENT_STRING)
    485  1.1  roy 				continue;
    486  1.1  roy 			id = _ti_strid(i);
    487  1.1  roy 			break;
    488  1.1  roy 		}
    489  1.1  roy 		printf("\t!%s.\n", id);
    490  1.1  roy 	}
    491  1.1  roy }
    492  1.1  roy 
    493  1.1  roy static TERMUSERDEF *
    494  1.1  roy find_userdef(TERMINAL *term, const char *id)
    495  1.1  roy {
    496  1.1  roy 	size_t i;
    497  1.1  roy 
    498  1.1  roy 	for (i = 0; i < term->_nuserdefs; i++)
    499  1.1  roy 		if (strcmp(term->_userdefs[i].id, id) == 0)
    500  1.1  roy 			return &term->_userdefs[i];
    501  1.1  roy 	return NULL;
    502  1.1  roy }
    503  1.1  roy 
    504  1.1  roy static void
    505  1.1  roy use_terms(TERMINAL *term, size_t nuse, char **uterms)
    506  1.1  roy {
    507  1.1  roy 	TERMINAL **terms;
    508  1.1  roy 	TERMUSERDEF *ud, *tud;
    509  1.1  roy 	size_t i, j, agree, absent, data;
    510  1.1  roy 
    511  1.1  roy 	terms = malloc(sizeof(**terms) * nuse);
    512  1.1  roy 	if (terms == NULL)
    513  1.1  roy 		err(1, "malloc");
    514  1.1  roy 	for (i = 0; i < nuse; i++) {
    515  1.1  roy 		if (strcmp(term->name, *uterms) == 0)
    516  1.1  roy 			errx(1, "cannot use same terminal");
    517  1.1  roy 		for (j = 0; j < i; j++)
    518  1.1  roy 			if (strcmp(terms[j]->name, *uterms) == 0)
    519  1.1  roy 				errx(1, "cannot use same terminal");
    520  1.1  roy 		terms[i] = load_term(*uterms++);
    521  1.1  roy 	}
    522  1.1  roy 
    523  1.1  roy 	for (i = 0; i < TIFLAGMAX + 1; i++) {
    524  1.1  roy 		agree = absent = data = 0;
    525  1.1  roy 		for (j = 0; j < nuse; j++) {
    526  1.1  roy 			if (terms[j]->flags[i] == ABSENT_BOOLEAN ||
    527  1.1  roy 			    terms[j]->flags[i] == CANCELLED_BOOLEAN)
    528  1.1  roy 				absent++;
    529  1.1  roy 			else {
    530  1.1  roy 				data++;
    531  1.1  roy 				if (term->flags[i] == terms[j]->flags[i])
    532  1.1  roy 					agree++;
    533  1.1  roy 			}
    534  1.1  roy 		}
    535  1.1  roy 		if (data == 0)
    536  1.1  roy 			continue;
    537  1.1  roy 		if (agree > 0 && agree + absent == nuse)
    538  1.1  roy 			term->flags[i] = ABSENT_BOOLEAN;
    539  1.1  roy 		else if (term->flags[i] == ABSENT_BOOLEAN)
    540  1.1  roy 			term->flags[i] = CANCELLED_BOOLEAN;
    541  1.1  roy 	}
    542  1.1  roy 
    543  1.1  roy 	for (i = 0; i < TINUMMAX + 1; i++) {
    544  1.1  roy 		agree = absent = data = 0;
    545  1.1  roy 		for (j = 0; j < nuse; j++) {
    546  1.1  roy 			if (terms[j]->nums[i] == ABSENT_NUMERIC ||
    547  1.1  roy 			    terms[j]->nums[i] == CANCELLED_NUMERIC)
    548  1.1  roy 				absent++;
    549  1.1  roy 			else {
    550  1.1  roy 				data++;
    551  1.1  roy 				if (term->nums[i] == terms[j]->nums[i])
    552  1.1  roy 					agree++;
    553  1.1  roy 			}
    554  1.1  roy 		}
    555  1.1  roy 		if (data == 0)
    556  1.1  roy 			continue;
    557  1.1  roy 		if (agree > 0 && agree + absent == nuse)
    558  1.1  roy 			term->nums[i] = ABSENT_NUMERIC;
    559  1.1  roy 		else if (term->nums[i] == ABSENT_NUMERIC)
    560  1.1  roy 			term->nums[i] = CANCELLED_NUMERIC;
    561  1.1  roy 	}
    562  1.1  roy 
    563  1.1  roy 	for (i = 0; i < TISTRMAX + 1; i++) {
    564  1.1  roy 		agree = absent = data = 0;
    565  1.1  roy 		for (j = 0; j < nuse; j++) {
    566  1.1  roy 			if (terms[j]->strs[i] == ABSENT_STRING ||
    567  1.1  roy 			    terms[j]->strs[i] == CANCELLED_STRING)
    568  1.1  roy 				absent++;
    569  1.1  roy 			else {
    570  1.1  roy 				data++;
    571  1.1  roy 				if (VALID_STRING(term->strs[i]) &&
    572  1.1  roy 				    strcmp(term->strs[i],
    573  1.1  roy 					terms[j]->strs[i]) == 0)
    574  1.1  roy 					agree++;
    575  1.1  roy 			}
    576  1.1  roy 		}
    577  1.1  roy 		if (data == 0)
    578  1.1  roy 			continue;
    579  1.1  roy 		if (agree > 0 && agree + absent == nuse)
    580  1.1  roy 			term->strs[i] = ABSENT_STRING;
    581  1.1  roy 		else if (term->strs[i] == ABSENT_STRING)
    582  1.1  roy 			term->strs[i] = CANCELLED_STRING;
    583  1.1  roy 	}
    584  1.1  roy 
    585  1.1  roy 	/* User defined caps are more tricky.
    586  1.1  roy 	   First we set any to absent that agree. */
    587  1.1  roy 	for (i = 0; i < term->_nuserdefs; i++) {
    588  1.1  roy 		agree = absent = data = 0;
    589  1.1  roy 		ud = &term->_userdefs[i];
    590  1.1  roy 		for (j = 0; j < nuse; j++) {
    591  1.1  roy 			tud = find_userdef(terms[j], ud->id);
    592  1.1  roy 			if (tud == NULL)
    593  1.1  roy 				absent++;
    594  1.1  roy 			else {
    595  1.1  roy 				data++;
    596  1.1  roy 				switch (ud->type) {
    597  1.1  roy 				case 'f':
    598  1.1  roy 					if (tud->type == 'f' &&
    599  1.1  roy 					    tud->flag == ud->flag)
    600  1.1  roy 						agree++;
    601  1.1  roy 					break;
    602  1.1  roy 				case 'n':
    603  1.1  roy 					if (tud->type == 'n' &&
    604  1.1  roy 					    tud->num == ud->num)
    605  1.1  roy 						agree++;
    606  1.1  roy 					break;
    607  1.1  roy 				case 's':
    608  1.1  roy 					if (tud->type == 's' &&
    609  1.1  roy 					    VALID_STRING(tud->str) &&
    610  1.1  roy 					    VALID_STRING(ud->str) &&
    611  1.1  roy 					    strcmp(ud->str, tud->str) == 0)
    612  1.1  roy 						agree++;
    613  1.1  roy 					break;
    614  1.1  roy 				}
    615  1.1  roy 			}
    616  1.1  roy 		}
    617  1.1  roy 		if (data == 0)
    618  1.1  roy 			continue;
    619  1.1  roy 		if (agree > 0 && agree + absent == nuse) {
    620  1.1  roy 			ud->flag = ABSENT_BOOLEAN;
    621  1.1  roy 			ud->num = ABSENT_NUMERIC;
    622  1.1  roy 			ud->str = ABSENT_STRING;
    623  1.1  roy 		}
    624  1.1  roy 	}
    625  1.1  roy 
    626  1.1  roy 	/* Now add any that we don't have as cancelled */
    627  1.1  roy 	for (i = 0; i < nuse; i++) {
    628  1.1  roy 		for (j = 0; j < terms[i]->_nuserdefs; j++) {
    629  1.1  roy 			ud = find_userdef(term, terms[i]->_userdefs[j].id);
    630  1.1  roy 			if (ud != NULL)
    631  1.1  roy 				continue; /* We have handled this */
    632  1.1  roy 			term->_userdefs = realloc(term->_userdefs,
    633  1.1  roy 			    sizeof(*term->_userdefs) * (term->_nuserdefs + 1));
    634  1.1  roy 			if (term->_userdefs == NULL)
    635  1.1  roy 				err(1, "malloc");
    636  1.1  roy 			tud = &term->_userdefs[term->_nuserdefs++];
    637  1.1  roy 			tud->id = terms[i]->_userdefs[j].id;
    638  1.1  roy 			tud->type = terms[i]->_userdefs[j].flag;
    639  1.1  roy 			tud->flag = CANCELLED_BOOLEAN;
    640  1.1  roy 			tud->num = CANCELLED_NUMERIC;
    641  1.1  roy 			tud->str = CANCELLED_STRING;
    642  1.1  roy 		}
    643  1.1  roy 	}
    644  1.1  roy }
    645  1.1  roy 
    646  1.1  roy int
    647  1.1  roy main(int argc, char **argv)
    648  1.1  roy {
    649  1.1  roy 	char *term, *Barg;
    650  1.1  roy 	int ch, uflag;
    651  1.1  roy 	TERMINAL *t, *t2;
    652  1.1  roy 	size_t n, n2;
    653  1.1  roy 	struct winsize ws;
    654  1.1  roy 	TIENT ents[TISTRMAX + 1], ents2[TISTRMAX + 1];
    655  1.1  roy 
    656  1.1  roy 	cols = 80; /* default */
    657  1.1  roy 	term = getenv("COLUMNS");
    658  1.1  roy 	if (term != NULL)
    659  1.1  roy 		cols = strtoul(term, NULL, 10);
    660  1.1  roy 	else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0)
    661  1.1  roy 		cols = ws.ws_col;
    662  1.1  roy 
    663  1.1  roy 	uflag = xflag = 0;
    664  1.1  roy 	Barg = NULL;
    665  1.1  roy 	while ((ch = getopt(argc, argv, "1A:B:acnquw:x")) != -1)
    666  1.1  roy 		switch (ch) {
    667  1.1  roy 		case '1':
    668  1.1  roy 			cols = 1;
    669  1.1  roy 			break;
    670  1.1  roy 		case 'A':
    671  1.1  roy 			setdb(optarg);
    672  1.1  roy 			break;
    673  1.1  roy 		case 'B':
    674  1.1  roy 			Barg = optarg;
    675  1.1  roy 			break;
    676  1.1  roy 		case 'a':
    677  1.1  roy 			aflag++;
    678  1.1  roy 			xflag++;
    679  1.1  roy 			break;
    680  1.1  roy 		case 'c':
    681  1.1  roy 			cflag++;
    682  1.1  roy 			break;
    683  1.1  roy 		case 'n':
    684  1.1  roy 			nflag++;
    685  1.1  roy 			break;
    686  1.1  roy 		case 'q':
    687  1.1  roy 			qflag++;
    688  1.1  roy 			break;
    689  1.1  roy 		case 'u':
    690  1.1  roy 			uflag++;
    691  1.1  roy 			break;
    692  1.1  roy 		case 'w':
    693  1.1  roy 			cols = strtoul(optarg, NULL, 10);
    694  1.1  roy 			break;
    695  1.1  roy 		case 'x':
    696  1.1  roy 			xflag++;
    697  1.1  roy 			break;
    698  1.1  roy 		case '?':
    699  1.1  roy 		default:
    700  1.1  roy 			fprintf(stderr,
    701  1.1  roy 			    "usage: %s [-1acnqux] [-A database] [-B database] "
    702  1.1  roy 			    "[-w cols] [term]\n",
    703  1.1  roy 			    getprogname());
    704  1.1  roy 			return EXIT_FAILURE;
    705  1.1  roy 		}
    706  1.1  roy 	cols--;
    707  1.1  roy 
    708  1.1  roy 	if (optind + 1 < argc)
    709  1.1  roy 		raw = 1;
    710  1.1  roy 
    711  1.1  roy 	if (optind < argc)
    712  1.1  roy 		term = argv[optind++];
    713  1.1  roy 	else
    714  1.1  roy 		term = NULL;
    715  1.1  roy 	t = load_term(term);
    716  1.1  roy 
    717  1.1  roy 	if (uflag != 0)
    718  1.1  roy 		use_terms(t, argc - optind, argv + optind);
    719  1.1  roy 
    720  1.1  roy 	if ((optind + 1 != argc && nflag == 0) || uflag != 0) {
    721  1.1  roy 		if (uflag == 0)
    722  1.1  roy 			printf("# Reconstructed from %s.db\n", _ti_database);
    723  1.1  roy 		printf("%s", t->name);
    724  1.1  roy 		if (t->desc != NULL && *t->desc != '\0')
    725  1.1  roy 			printf("|%s", t->desc);
    726  1.1  roy 		printf(",\n");
    727  1.1  roy 
    728  1.1  roy 		n = load_ents(ents, t, 'f');
    729  1.1  roy 		print_ent(ents, n);
    730  1.1  roy 		n = load_ents(ents, t, 'n');
    731  1.1  roy 		print_ent(ents, n);
    732  1.1  roy 		n = load_ents(ents, t, 's');
    733  1.1  roy 		print_ent(ents, n);
    734  1.1  roy 
    735  1.1  roy 		if (uflag != 0) {
    736  1.1  roy 			printf("\t");
    737  1.1  roy 			n = SW;
    738  1.1  roy 			for (; optind < argc; optind++) {
    739  1.1  roy 				n2 = 5 + strlen(argv[optind]);
    740  1.1  roy 				if (n != SW) {
    741  1.1  roy 					if (n + n2 > cols) {
    742  1.1  roy 						printf("\n\t");
    743  1.1  roy 						n = SW;
    744  1.1  roy 					} else
    745  1.1  roy 						n += printf(" ");
    746  1.1  roy 				}
    747  1.1  roy 				n += printf("use=%s,", argv[optind]);
    748  1.1  roy 			}
    749  1.1  roy 			printf("\n");
    750  1.1  roy 		}
    751  1.1  roy 		return EXIT_SUCCESS;
    752  1.1  roy 	}
    753  1.1  roy 
    754  1.1  roy 	if (Barg == NULL)
    755  1.1  roy 		unsetenv("TERMINFO");
    756  1.1  roy 	else
    757  1.1  roy 		setdb(Barg);
    758  1.1  roy 	t2 = load_term(argv[optind++]);
    759  1.1  roy 	printf("comparing %s to %s.\n", t->name, t2->name);
    760  1.1  roy 	if (qflag == 0)
    761  1.1  roy 		printf("    comparing booleans.\n");
    762  1.1  roy 	if (nflag == 0) {
    763  1.1  roy 		n = load_ents(ents, t, 'f');
    764  1.1  roy 		n2 = load_ents(ents2, t2, 'f');
    765  1.1  roy 		compare_ents(ents, n, ents2, n2);
    766  1.1  roy 	} else
    767  1.1  roy 		show_missing(t, t2, 'f');
    768  1.1  roy 	if (qflag == 0)
    769  1.1  roy 		printf("    comparing numbers.\n");
    770  1.1  roy 	if (nflag == 0) {
    771  1.1  roy 		n = load_ents(ents, t, 'n');
    772  1.1  roy 		n2 = load_ents(ents2, t2, 'n');
    773  1.1  roy 		compare_ents(ents, n, ents2, n2);
    774  1.1  roy 	} else
    775  1.1  roy 		show_missing(t, t2, 'n');
    776  1.1  roy 	if (qflag == 0)
    777  1.1  roy 		printf("    comparing strings.\n");
    778  1.1  roy 	if (nflag == 0) {
    779  1.1  roy 		n = load_ents(ents, t, 's');
    780  1.1  roy 		n2 = load_ents(ents2, t2, 's');
    781  1.1  roy 		compare_ents(ents, n, ents2, n2);
    782  1.1  roy 	} else
    783  1.1  roy 		show_missing(t, t2, 's');
    784  1.1  roy 	return EXIT_SUCCESS;
    785  1.1  roy }
    786