infocmp.c revision 1.13 1 1.13 roy /* $NetBSD: infocmp.c,v 1.13 2020/03/13 15:19:25 roy Exp $ */
2 1.1 roy
3 1.1 roy /*
4 1.13 roy * Copyright (c) 2009, 2010, 2020 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.13 roy __RCSID("$NetBSD: infocmp.c,v 1.13 2020/03/13 15:19:25 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.10 christos #include <util.h>
44 1.1 roy
45 1.1 roy #define SW 8
46 1.1 roy
47 1.1 roy typedef struct tient {
48 1.1 roy char type;
49 1.1 roy const char *id;
50 1.2 roy signed char flag;
51 1.13 roy int num;
52 1.1 roy const char *str;
53 1.1 roy } TIENT;
54 1.1 roy
55 1.1 roy static size_t cols;
56 1.4 roy static int aflag, cflag, nflag, qflag, xflag;
57 1.1 roy
58 1.1 roy static size_t
59 1.1 roy outstr(FILE *f, const char *str)
60 1.1 roy {
61 1.1 roy unsigned char ch;
62 1.1 roy size_t r, l;
63 1.1 roy
64 1.1 roy r = 0;
65 1.1 roy l = strlen(str);
66 1.1 roy while ((ch = (unsigned char)(*str++)) != '\0') {
67 1.1 roy switch (ch) {
68 1.1 roy case 128:
69 1.1 roy ch = '0';
70 1.1 roy break;
71 1.1 roy case '\033':
72 1.1 roy ch = 'E';
73 1.1 roy break;
74 1.1 roy case '\014':
75 1.1 roy ch = 'f';
76 1.1 roy break;
77 1.1 roy case '^': /* FALLTHROUGH */
78 1.1 roy case ',': /* escape these */
79 1.1 roy break;
80 1.1 roy case ' ':
81 1.1 roy ch = 's';
82 1.1 roy break;
83 1.1 roy default:
84 1.1 roy if (ch == '\177') {
85 1.1 roy if (f != NULL)
86 1.1 roy fputc('^', f);
87 1.1 roy ch = '?';
88 1.1 roy r++;
89 1.1 roy } else if (iscntrl(ch) &&
90 1.1 roy ch < 128 &&
91 1.1 roy ch != '\\' &&
92 1.1 roy (l < 4 || isdigit((unsigned char)*str)))
93 1.1 roy {
94 1.1 roy if (f != NULL)
95 1.1 roy fputc('^', f);
96 1.1 roy ch += '@';
97 1.1 roy r++;
98 1.1 roy } else if (!isprint(ch)) {
99 1.1 roy if (f != NULL)
100 1.1 roy fprintf(f, "\\%03o", ch);
101 1.1 roy r += 4;
102 1.1 roy continue;
103 1.1 roy }
104 1.1 roy goto prnt;
105 1.1 roy }
106 1.9 roy
107 1.1 roy if (f != NULL)
108 1.1 roy fputc('\\', f);
109 1.1 roy r++;
110 1.1 roy prnt:
111 1.1 roy if (f != NULL)
112 1.1 roy fputc(ch, f);
113 1.1 roy r++;
114 1.1 roy }
115 1.1 roy return r;
116 1.1 roy }
117 1.1 roy
118 1.1 roy static int
119 1.1 roy ent_compare(const void *a, const void *b)
120 1.1 roy {
121 1.1 roy const TIENT *ta, *tb;
122 1.1 roy
123 1.1 roy ta = (const TIENT *)a;
124 1.1 roy tb = (const TIENT *)b;
125 1.1 roy return strcmp(ta->id, tb->id);
126 1.1 roy }
127 1.1 roy
128 1.1 roy static void
129 1.1 roy setdb(char *db)
130 1.1 roy {
131 1.1 roy size_t len;
132 1.1 roy
133 1.1 roy len = strlen(db);
134 1.1 roy if (len > 3 &&
135 1.1 roy db[len - 3] == '.' &&
136 1.1 roy db[len - 2] == 'd' &&
137 1.1 roy db[len - 1] == 'b')
138 1.1 roy db[len - 3] = '\0';
139 1.1 roy setenv("TERMINFO", db, 1);
140 1.1 roy }
141 1.1 roy
142 1.1 roy static void
143 1.1 roy print_ent(const TIENT *ents, size_t nents)
144 1.1 roy {
145 1.1 roy size_t col, i, l;
146 1.9 roy char nbuf[64];
147 1.1 roy
148 1.1 roy if (nents == 0)
149 1.1 roy return;
150 1.9 roy
151 1.1 roy col = SW;
152 1.1 roy printf("\t");
153 1.1 roy for (i = 0; i < nents; i++) {
154 1.1 roy if (*ents[i].id == '.' && aflag == 0)
155 1.1 roy continue;
156 1.1 roy switch (ents[i].type) {
157 1.1 roy case 'f':
158 1.1 roy if (ents[i].flag == ABSENT_BOOLEAN)
159 1.1 roy continue;
160 1.1 roy l = strlen(ents[i].id) + 2;
161 1.1 roy if (ents[i].flag == CANCELLED_BOOLEAN)
162 1.1 roy l++;
163 1.1 roy break;
164 1.1 roy case 'n':
165 1.1 roy if (ents[i].num == ABSENT_NUMERIC)
166 1.1 roy continue;
167 1.1 roy if (VALID_NUMERIC(ents[i].num))
168 1.1 roy l = snprintf(nbuf, sizeof(nbuf), "%s#%d,",
169 1.1 roy ents[i].id, ents[i].num);
170 1.1 roy else
171 1.1 roy l = snprintf(nbuf, sizeof(nbuf), "%s@,",
172 1.1 roy ents[i].id);
173 1.1 roy break;
174 1.1 roy case 's':
175 1.1 roy if (ents[i].str == ABSENT_STRING)
176 1.1 roy continue;
177 1.1 roy if (VALID_STRING(ents[i].str))
178 1.1 roy l = strlen(ents[i].id) +
179 1.1 roy outstr(NULL, ents[i].str) + 7;
180 1.1 roy else
181 1.1 roy l = strlen(ents[i].id) + 3;
182 1.1 roy break;
183 1.1 roy default:
184 1.10 christos errx(EXIT_FAILURE, "invalid type");
185 1.1 roy }
186 1.1 roy if (col != SW) {
187 1.1 roy if (col + l > cols) {
188 1.1 roy printf("\n\t");
189 1.1 roy col = SW;
190 1.1 roy } else
191 1.1 roy col += printf(" ");
192 1.1 roy }
193 1.1 roy switch (ents[i].type) {
194 1.1 roy case 'f':
195 1.1 roy col += printf("%s", ents[i].id);
196 1.1 roy if (ents[i].flag == ABSENT_BOOLEAN ||
197 1.1 roy ents[i].flag == CANCELLED_BOOLEAN)
198 1.1 roy col += printf("@");
199 1.1 roy col += printf(",");
200 1.1 roy break;
201 1.1 roy case 'n':
202 1.1 roy col += printf("%s", nbuf);
203 1.1 roy break;
204 1.1 roy case 's':
205 1.1 roy col += printf("%s", ents[i].id);
206 1.1 roy if (VALID_STRING(ents[i].str)) {
207 1.1 roy col += printf("=");
208 1.1 roy col += outstr(stdout, ents[i].str);
209 1.1 roy } else
210 1.1 roy col += printf("@");
211 1.1 roy col += printf(",");
212 1.1 roy break;
213 1.1 roy }
214 1.1 roy }
215 1.1 roy printf("\n");
216 1.1 roy }
217 1.1 roy
218 1.1 roy static size_t
219 1.1 roy load_ents(TIENT *ents, TERMINAL *t, char type)
220 1.1 roy {
221 1.1 roy size_t i, n, max;
222 1.1 roy TERMUSERDEF *ud;
223 1.1 roy
224 1.1 roy switch (type) {
225 1.1 roy case 'f':
226 1.1 roy max = TIFLAGMAX;
227 1.1 roy break;
228 1.1 roy case 'n':
229 1.1 roy max = TINUMMAX;
230 1.1 roy break;
231 1.1 roy default:
232 1.1 roy max = TISTRMAX;
233 1.1 roy }
234 1.9 roy
235 1.1 roy n = 0;
236 1.4 roy for (i = 0; i <= max; i++) {
237 1.4 roy switch (type) {
238 1.4 roy case 'f':
239 1.4 roy if (t->flags[i] == 1 ||
240 1.4 roy (aflag && t->flags[i] == CANCELLED_BOOLEAN))
241 1.4 roy {
242 1.4 roy ents[n].id = _ti_flagid(i);
243 1.4 roy ents[n].type = 'f';
244 1.4 roy ents[n++].flag = t->flags[i];
245 1.4 roy }
246 1.4 roy break;
247 1.4 roy case 'n':
248 1.4 roy if (VALID_NUMERIC(t->nums[i]) ||
249 1.4 roy (aflag && t->nums[i] == CANCELLED_NUMERIC))
250 1.4 roy {
251 1.4 roy ents[n].id = _ti_numid(i);
252 1.4 roy ents[n].type = 'n';
253 1.4 roy ents[n++].num = t->nums[i];
254 1.4 roy }
255 1.4 roy break;
256 1.4 roy default:
257 1.4 roy if (VALID_STRING(t->strs[i]) ||
258 1.4 roy (aflag && t->strs[i] == CANCELLED_STRING))
259 1.4 roy {
260 1.4 roy ents[n].id = _ti_strid(i);
261 1.4 roy ents[n].type = 's';
262 1.4 roy ents[n++].str = t->strs[i];
263 1.1 roy }
264 1.4 roy break;
265 1.1 roy }
266 1.1 roy }
267 1.9 roy
268 1.1 roy if (xflag != 0 && t->_nuserdefs != 0) {
269 1.1 roy for (i = 0; i < t->_nuserdefs; i++) {
270 1.1 roy ud = &t->_userdefs[i];
271 1.1 roy if (ud->type == type) {
272 1.1 roy switch (type) {
273 1.1 roy case 'f':
274 1.4 roy if (!aflag &&
275 1.1 roy !VALID_BOOLEAN(ud->flag))
276 1.1 roy continue;
277 1.1 roy break;
278 1.1 roy case 'n':
279 1.4 roy if (!aflag &&
280 1.1 roy !VALID_NUMERIC(ud->num))
281 1.1 roy continue;
282 1.1 roy break;
283 1.1 roy case 's':
284 1.4 roy if (!aflag &&
285 1.1 roy !VALID_STRING(ud->str))
286 1.1 roy continue;
287 1.1 roy break;
288 1.1 roy }
289 1.1 roy ents[n].id = ud->id;
290 1.1 roy ents[n].type = ud->type;
291 1.1 roy ents[n].flag = ud->flag;
292 1.1 roy ents[n].num = ud->num;
293 1.1 roy ents[n++].str = ud->str;
294 1.1 roy }
295 1.1 roy }
296 1.1 roy }
297 1.9 roy
298 1.1 roy qsort(ents, n, sizeof(TIENT), ent_compare);
299 1.1 roy return n;
300 1.1 roy }
301 1.1 roy
302 1.1 roy static void
303 1.1 roy cprint_ent(TIENT *ent)
304 1.1 roy {
305 1.1 roy
306 1.1 roy if (ent == NULL) {
307 1.1 roy if (qflag == 0)
308 1.1 roy printf("NULL");
309 1.1 roy else
310 1.1 roy printf("-");
311 1.1 roy }
312 1.9 roy
313 1.1 roy switch (ent->type) {
314 1.1 roy case 'f':
315 1.1 roy if (VALID_BOOLEAN(ent->flag))
316 1.1 roy printf(ent->flag == 1 ? "T" : "F");
317 1.1 roy else if (qflag == 0)
318 1.1 roy printf("F");
319 1.1 roy else if (ent->flag == CANCELLED_BOOLEAN)
320 1.1 roy printf("@");
321 1.1 roy else
322 1.1 roy printf("-");
323 1.1 roy break;
324 1.1 roy case 'n':
325 1.1 roy if (VALID_NUMERIC(ent->num))
326 1.1 roy printf("%d", ent->num);
327 1.1 roy else if (qflag == 0)
328 1.1 roy printf("NULL");
329 1.1 roy else if (ent->num == CANCELLED_NUMERIC)
330 1.1 roy printf("@");
331 1.1 roy else
332 1.1 roy printf("-");
333 1.1 roy break;
334 1.1 roy case 's':
335 1.1 roy if (VALID_STRING(ent->str)) {
336 1.1 roy printf("'");
337 1.1 roy outstr(stdout, ent->str);
338 1.1 roy printf("'");
339 1.1 roy } else if (qflag == 0)
340 1.1 roy printf("NULL");
341 1.1 roy else if (ent->str == CANCELLED_STRING)
342 1.1 roy printf("@");
343 1.1 roy else
344 1.1 roy printf("-");
345 1.1 roy break;
346 1.1 roy }
347 1.1 roy }
348 1.1 roy
349 1.1 roy static void
350 1.1 roy compare_ents(TIENT *ents1, size_t n1, TIENT *ents2, size_t n2)
351 1.1 roy {
352 1.1 roy size_t i1, i2;
353 1.1 roy TIENT *e1, *e2, ee;
354 1.1 roy int c;
355 1.9 roy
356 1.1 roy i1 = i2 = 0;
357 1.1 roy ee.type = 'f';
358 1.1 roy ee.flag = ABSENT_BOOLEAN;
359 1.1 roy ee.num = ABSENT_NUMERIC;
360 1.1 roy ee.str = ABSENT_STRING;
361 1.1 roy while (i1 != n1 || i2 != n2) {
362 1.1 roy if (i1 == n1)
363 1.1 roy c = 1;
364 1.1 roy else if (i2 == n2)
365 1.1 roy c = -1;
366 1.1 roy else
367 1.1 roy c = strcmp(ents1[i1].id, ents2[i2].id);
368 1.1 roy if (c == 0) {
369 1.1 roy e1 = &ents1[i1++];
370 1.1 roy e2 = &ents2[i2++];
371 1.1 roy } else if (c < 0) {
372 1.1 roy e1 = &ents1[i1++];
373 1.1 roy e2 = ⅇ
374 1.1 roy ee.id = e1->id;
375 1.1 roy ee.type = e1->type;
376 1.1 roy } else {
377 1.1 roy e1 = ⅇ
378 1.1 roy e2 = &ents2[i2++];
379 1.1 roy ee.id = e2->id;
380 1.1 roy ee.type = e2->type;
381 1.1 roy }
382 1.1 roy switch (e1->type) {
383 1.1 roy case 'f':
384 1.1 roy if (cflag != 0) {
385 1.1 roy if (e1->flag == e2->flag)
386 1.1 roy printf("\t%s\n", ents1[i1].id);
387 1.1 roy continue;
388 1.1 roy }
389 1.1 roy if (e1->flag == e2->flag)
390 1.1 roy continue;
391 1.1 roy break;
392 1.1 roy case 'n':
393 1.1 roy if (cflag != 0) {
394 1.1 roy if (e1->num == e2->num)
395 1.1 roy printf("\t%s#%d\n",
396 1.1 roy ents1[i1].id, ents1[i1].num);
397 1.1 roy continue;
398 1.1 roy }
399 1.1 roy if (e1->num == e2->num)
400 1.1 roy continue;
401 1.1 roy break;
402 1.1 roy case 's':
403 1.1 roy if (cflag != 0) {
404 1.1 roy if (VALID_STRING(e1->str) &&
405 1.1 roy VALID_STRING(e2->str) &&
406 1.1 roy strcmp(e1->str, e2->str) == 0) {
407 1.1 roy printf("\t%s=", ents1[i1].id);
408 1.1 roy outstr(stdout, ents1[i1].str);
409 1.1 roy printf("\n");
410 1.1 roy }
411 1.1 roy continue;
412 1.1 roy }
413 1.1 roy if (VALID_STRING(e1->str) &&
414 1.1 roy VALID_STRING(e2->str) &&
415 1.1 roy strcmp(e1->str, e2->str) == 0)
416 1.1 roy continue;
417 1.1 roy break;
418 1.1 roy }
419 1.1 roy printf("\t%s: ", e1->id);
420 1.1 roy cprint_ent(e1);
421 1.1 roy if (e1->type == 'f')
422 1.1 roy printf(":");
423 1.1 roy else
424 1.1 roy printf(", ");
425 1.1 roy cprint_ent(e2);
426 1.1 roy printf(".\n");
427 1.1 roy }
428 1.1 roy }
429 1.1 roy
430 1.1 roy static TERMINAL *
431 1.1 roy load_term(const char *name)
432 1.1 roy {
433 1.1 roy TERMINAL *t;
434 1.1 roy
435 1.10 christos t = ecalloc(1, sizeof(*t));
436 1.1 roy if (name == NULL)
437 1.1 roy name = getenv("TERM");
438 1.1 roy if (name == NULL)
439 1.1 roy name = "dumb";
440 1.6 roy if (_ti_getterm(t, name, 1) == 1)
441 1.6 roy return t;
442 1.6 roy
443 1.6 roy if (_ti_database == NULL)
444 1.13 roy errx(EXIT_FAILURE,
445 1.13 roy "no terminal definition found in internal database");
446 1.6 roy else
447 1.13 roy errx(EXIT_FAILURE,
448 1.13 roy "no terminal definition found in %s.db", _ti_database);
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.9 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.11 christos terms = ecalloc(nuse, sizeof(*terms));
512 1.1 roy for (i = 0; i < nuse; i++) {
513 1.1 roy if (strcmp(term->name, *uterms) == 0)
514 1.10 christos errx(EXIT_FAILURE, "cannot use same terminal");
515 1.1 roy for (j = 0; j < i; j++)
516 1.1 roy if (strcmp(terms[j]->name, *uterms) == 0)
517 1.10 christos errx(EXIT_FAILURE, "cannot use same terminal");
518 1.1 roy terms[i] = load_term(*uterms++);
519 1.1 roy }
520 1.9 roy
521 1.1 roy for (i = 0; i < TIFLAGMAX + 1; i++) {
522 1.1 roy agree = absent = data = 0;
523 1.1 roy for (j = 0; j < nuse; j++) {
524 1.1 roy if (terms[j]->flags[i] == ABSENT_BOOLEAN ||
525 1.1 roy terms[j]->flags[i] == CANCELLED_BOOLEAN)
526 1.1 roy absent++;
527 1.1 roy else {
528 1.1 roy data++;
529 1.1 roy if (term->flags[i] == terms[j]->flags[i])
530 1.1 roy agree++;
531 1.1 roy }
532 1.1 roy }
533 1.1 roy if (data == 0)
534 1.1 roy continue;
535 1.1 roy if (agree > 0 && agree + absent == nuse)
536 1.1 roy term->flags[i] = ABSENT_BOOLEAN;
537 1.1 roy else if (term->flags[i] == ABSENT_BOOLEAN)
538 1.1 roy term->flags[i] = CANCELLED_BOOLEAN;
539 1.1 roy }
540 1.9 roy
541 1.1 roy for (i = 0; i < TINUMMAX + 1; i++) {
542 1.1 roy agree = absent = data = 0;
543 1.1 roy for (j = 0; j < nuse; j++) {
544 1.1 roy if (terms[j]->nums[i] == ABSENT_NUMERIC ||
545 1.1 roy terms[j]->nums[i] == CANCELLED_NUMERIC)
546 1.1 roy absent++;
547 1.1 roy else {
548 1.1 roy data++;
549 1.1 roy if (term->nums[i] == terms[j]->nums[i])
550 1.1 roy agree++;
551 1.1 roy }
552 1.1 roy }
553 1.1 roy if (data == 0)
554 1.1 roy continue;
555 1.1 roy if (agree > 0 && agree + absent == nuse)
556 1.1 roy term->nums[i] = ABSENT_NUMERIC;
557 1.1 roy else if (term->nums[i] == ABSENT_NUMERIC)
558 1.1 roy term->nums[i] = CANCELLED_NUMERIC;
559 1.1 roy }
560 1.9 roy
561 1.1 roy for (i = 0; i < TISTRMAX + 1; i++) {
562 1.1 roy agree = absent = data = 0;
563 1.1 roy for (j = 0; j < nuse; j++) {
564 1.1 roy if (terms[j]->strs[i] == ABSENT_STRING ||
565 1.1 roy terms[j]->strs[i] == CANCELLED_STRING)
566 1.1 roy absent++;
567 1.1 roy else {
568 1.1 roy data++;
569 1.1 roy if (VALID_STRING(term->strs[i]) &&
570 1.1 roy strcmp(term->strs[i],
571 1.1 roy terms[j]->strs[i]) == 0)
572 1.1 roy agree++;
573 1.1 roy }
574 1.1 roy }
575 1.1 roy if (data == 0)
576 1.1 roy continue;
577 1.1 roy if (agree > 0 && agree + absent == nuse)
578 1.1 roy term->strs[i] = ABSENT_STRING;
579 1.1 roy else if (term->strs[i] == ABSENT_STRING)
580 1.1 roy term->strs[i] = CANCELLED_STRING;
581 1.1 roy }
582 1.1 roy
583 1.1 roy /* User defined caps are more tricky.
584 1.1 roy First we set any to absent that agree. */
585 1.1 roy for (i = 0; i < term->_nuserdefs; i++) {
586 1.1 roy agree = absent = data = 0;
587 1.1 roy ud = &term->_userdefs[i];
588 1.1 roy for (j = 0; j < nuse; j++) {
589 1.1 roy tud = find_userdef(terms[j], ud->id);
590 1.1 roy if (tud == NULL)
591 1.1 roy absent++;
592 1.1 roy else {
593 1.1 roy data++;
594 1.1 roy switch (ud->type) {
595 1.1 roy case 'f':
596 1.1 roy if (tud->type == 'f' &&
597 1.1 roy tud->flag == ud->flag)
598 1.1 roy agree++;
599 1.1 roy break;
600 1.1 roy case 'n':
601 1.1 roy if (tud->type == 'n' &&
602 1.1 roy tud->num == ud->num)
603 1.1 roy agree++;
604 1.1 roy break;
605 1.1 roy case 's':
606 1.1 roy if (tud->type == 's' &&
607 1.1 roy VALID_STRING(tud->str) &&
608 1.1 roy VALID_STRING(ud->str) &&
609 1.1 roy strcmp(ud->str, tud->str) == 0)
610 1.1 roy agree++;
611 1.1 roy break;
612 1.1 roy }
613 1.1 roy }
614 1.1 roy }
615 1.1 roy if (data == 0)
616 1.1 roy continue;
617 1.1 roy if (agree > 0 && agree + absent == nuse) {
618 1.1 roy ud->flag = ABSENT_BOOLEAN;
619 1.1 roy ud->num = ABSENT_NUMERIC;
620 1.1 roy ud->str = ABSENT_STRING;
621 1.1 roy }
622 1.1 roy }
623 1.1 roy
624 1.1 roy /* Now add any that we don't have as cancelled */
625 1.1 roy for (i = 0; i < nuse; i++) {
626 1.1 roy for (j = 0; j < terms[i]->_nuserdefs; j++) {
627 1.1 roy ud = find_userdef(term, terms[i]->_userdefs[j].id);
628 1.1 roy if (ud != NULL)
629 1.1 roy continue; /* We have handled this */
630 1.10 christos term->_userdefs = erealloc(term->_userdefs,
631 1.1 roy sizeof(*term->_userdefs) * (term->_nuserdefs + 1));
632 1.1 roy tud = &term->_userdefs[term->_nuserdefs++];
633 1.1 roy tud->id = terms[i]->_userdefs[j].id;
634 1.1 roy tud->type = terms[i]->_userdefs[j].flag;
635 1.1 roy tud->flag = CANCELLED_BOOLEAN;
636 1.1 roy tud->num = CANCELLED_NUMERIC;
637 1.1 roy tud->str = CANCELLED_STRING;
638 1.1 roy }
639 1.1 roy }
640 1.1 roy }
641 1.1 roy
642 1.1 roy int
643 1.1 roy main(int argc, char **argv)
644 1.1 roy {
645 1.1 roy char *term, *Barg;
646 1.1 roy int ch, uflag;
647 1.1 roy TERMINAL *t, *t2;
648 1.1 roy size_t n, n2;
649 1.1 roy struct winsize ws;
650 1.1 roy TIENT ents[TISTRMAX + 1], ents2[TISTRMAX + 1];
651 1.1 roy
652 1.1 roy cols = 80; /* default */
653 1.1 roy term = getenv("COLUMNS");
654 1.1 roy if (term != NULL)
655 1.1 roy cols = strtoul(term, NULL, 10);
656 1.1 roy else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0)
657 1.1 roy cols = ws.ws_col;
658 1.1 roy
659 1.1 roy uflag = xflag = 0;
660 1.1 roy Barg = NULL;
661 1.1 roy while ((ch = getopt(argc, argv, "1A:B:acnquw:x")) != -1)
662 1.1 roy switch (ch) {
663 1.1 roy case '1':
664 1.1 roy cols = 1;
665 1.1 roy break;
666 1.1 roy case 'A':
667 1.1 roy setdb(optarg);
668 1.1 roy break;
669 1.1 roy case 'B':
670 1.1 roy Barg = optarg;
671 1.1 roy break;
672 1.1 roy case 'a':
673 1.4 roy aflag = 1;
674 1.1 roy break;
675 1.1 roy case 'c':
676 1.4 roy cflag = 1;
677 1.1 roy break;
678 1.1 roy case 'n':
679 1.4 roy nflag = 1;
680 1.1 roy break;
681 1.1 roy case 'q':
682 1.4 roy qflag = 1;
683 1.1 roy break;
684 1.1 roy case 'u':
685 1.4 roy uflag = 1;
686 1.4 roy aflag = 1;
687 1.1 roy break;
688 1.1 roy case 'w':
689 1.1 roy cols = strtoul(optarg, NULL, 10);
690 1.1 roy break;
691 1.1 roy case 'x':
692 1.4 roy xflag = 1;
693 1.1 roy break;
694 1.1 roy case '?':
695 1.1 roy default:
696 1.1 roy fprintf(stderr,
697 1.1 roy "usage: %s [-1acnqux] [-A database] [-B database] "
698 1.1 roy "[-w cols] [term]\n",
699 1.1 roy getprogname());
700 1.1 roy return EXIT_FAILURE;
701 1.1 roy }
702 1.1 roy cols--;
703 1.1 roy
704 1.1 roy if (optind + 1 < argc)
705 1.4 roy aflag = 1;
706 1.1 roy
707 1.1 roy if (optind < argc)
708 1.1 roy term = argv[optind++];
709 1.1 roy else
710 1.1 roy term = NULL;
711 1.1 roy t = load_term(term);
712 1.1 roy
713 1.1 roy if (uflag != 0)
714 1.1 roy use_terms(t, argc - optind, argv + optind);
715 1.1 roy
716 1.1 roy if ((optind + 1 != argc && nflag == 0) || uflag != 0) {
717 1.12 roy if (uflag == 0)
718 1.12 roy printf("# Reconstructed from %s\n",
719 1.12 roy _ti_database == NULL ?
720 1.12 roy "internal database" : _ti_database);
721 1.1 roy printf("%s", t->name);
722 1.3 roy if (t->_alias != NULL && *t->_alias != '\0')
723 1.3 roy printf("|%s", t->_alias);
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.3 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