1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
|
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2007 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# Copyright (c) 2018, Joyent, Inc.
PROG= captoinfo
OBJS= captoinfo.o otermcap.o
SRCS= $(OBJS:%.o=%.c)
include ../Makefile.cmd
LDFLAGS += $(MAPFILE.INT:%=-Wl,-M%)
LDLIBS += -lcurses
CPPFLAGS += -I../../lib/libcurses/screen
CERRWARN += -Wno-parentheses
# code is a mess right now
SMATCH=off
.KEEP_STATE:
all: $(PROG)
$(PROG): $(OBJS) $(MAPFILE.INT)
$(LINK.c) $(OBJS) -o $@ $(LDLIBS)
$(POST_PROCESS)
install: all $(ROOTPROG)
clean:
$(RM) $(OBJS)
include ../Makefile.targ
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1988 AT&T */
/* All Rights Reserved */
/*
* NAME
* captoinfo - convert a termcap description to a terminfo description
*
* SYNOPSIS
* captoinfo [-1vV] [-w width] [ filename ... ]
*
* AUTHOR
* Tony Hansen, January 22, 1984.
*/
#include "curses.h"
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "otermcap.h"
#include "print.h"
#define trace stderr /* send trace messages to stderr */
/* extra termcap variables no longer in terminfo */
char *oboolcodes[] =
{
"bs", /* Terminal can backspace with "^H" */
"nc", /* No correctly working carriage return (DM2500,H2000) */
"ns", /* Terminal is a CRT but does not scroll. */
"pt", /* Has hardware tabs (may need to be set with "is") */
"MT", /* Has meta key, alternate code. */
"xr", /* Return acts like ce \r \n (Delta Data) */
0
};
int cap_bs = 0, cap_nc = 1, cap_ns = 2, cap_pt = 3, cap_MT = 4, cap_xr = 5;
char *onumcodes[] =
{
"dB", /* Number of millisec of bs delay needed */
"dC", /* Number of millisec of cr delay needed */
"dF", /* Number of millisec of ff delay needed */
"dN", /* Number of millisec of nl delay needed */
"dT", /* Number of millisec of tab delay needed */
"ug", /* Number of blank chars left by us or ue */
/* Ignore the 'kn' number. It was ill-defined and never used. */
"kn", /* Number of "other" keys */
0
};
int cap_dB = 0, cap_dC = 1, cap_dF = 2, cap_dN = 3, cap_dT = 4, cap_ug = 5;
char *ostrcodes[] =
{
"bc", /* Backspace if not "^H" */
"ko", /* Termcap entries for other non-function keys */
"ma", /* Arrow key map, used by vi version 2 only */
"nl", /* Newline character (default "\n") */
"rs", /* undocumented reset string, like is (info is2) */
/* Ignore the 'ml' and 'mu' strings. */
"ml", /* Memory lock on above cursor. */
"mu", /* Memory unlock (turn off memory lock). */
0
};
int cap_bc = 0, cap_ko = 1, cap_ma = 2, cap_nl = 3, cap_rs = 4;
#define numelements(x) (sizeof (x)/sizeof (x[0]))
char oboolval[2][numelements(oboolcodes)];
short onumval[2][numelements(onumcodes)];
char *ostrval[2][numelements(ostrcodes)];
/* externs from libcurses.a */
extern char *boolnames[], *boolcodes[];
extern char *numnames[], *numcodes[];
extern char *strnames[], *strcodes[];
/* globals for this file */
char *progname; /* argv [0], the name of the program */
static char *term_name; /* the name of the terminal being worked on */
static int uselevel; /* whether we're dealing with use= info */
static int boolcount, /* the maximum numbers of each name array */
numcount,
strcount;
/* globals dealing with the environment */
extern char **environ;
static char TERM[100];
#if defined(SYSV) || defined(USG) /* handle both Sys Vr2 and Vr3 curses */
static char dirname[BUFSIZ];
#else
#include <sys/param.h>
static char dirname[MAXPATHLEN];
#endif /* SYSV || USG */
static char TERMCAP[BUFSIZ+15];
static char *newenviron[] = { &TERM[0], &TERMCAP[0], 0 };
/* dynamic arrays */
static char *boolval[2]; /* dynamic array of boolean values */
static short *numval[2]; /* dynamic array of numeric values */
static char **strval[2]; /* dynamic array of string pointers */
/* data buffers */
static char *capbuffer; /* string table, pointed at by strval */
static char *nextstring; /* pointer into string table */
static char *bp; /* termcap raw string table */
static char *buflongname; /* place to copy the long names */
/* flags */
static int verbose = 0; /* debugging printing level */
static int copycomments = 0; /* copy comments from tercap source */
#define ispadchar(c) (isdigit(c) || (c) == '.' || (c) == '*')
static void getlongname(void);
static void handleko(void);
static void handlema(void);
static void print_no_use_entry(void);
static void print_use_entry(char *);
static void captoinfo(void);
static void use_etc_termcap(void);
static void initdirname(void);
static void setfilename(char *);
static void setterm_name(void);
static void use_file(char *);
static void sorttable(char *[], char *[]);
static void inittables(void);
/*
* Verify that the names given in the termcap entry are all valid.
*/
int
capsearch(char *codes[], char *ocodes[], char *cap)
{
for (; *codes; codes++)
if (((*codes)[0] == cap[0]) && ((*codes)[1] == cap[1]))
return (1);
for (; *ocodes; ocodes++)
if (((*ocodes)[0] == cap[0]) && ((*ocodes)[1] == cap[1]))
return (1);
return (0);
}
void
checktermcap()
{
char *tbuf = bp;
enum { tbool, tnum, tstr, tcancel, tunknown } type;
for (;;) {
tbuf = tskip(tbuf);
while (*tbuf == '\t' || *tbuf == ' ' || *tbuf == ':')
tbuf++;
if (*tbuf == 0)
return;
/* commented out entry? */
if (*tbuf == '.') {
if (verbose)
(void) fprintf(trace, "termcap string '%c%c' "
"commented out.\n", tbuf[1], tbuf[2]);
if (!capsearch(boolcodes, oboolcodes, tbuf + 1) &&
!capsearch(numcodes, onumcodes, tbuf + 1) &&
!capsearch(strcodes, ostrcodes, tbuf + 1))
(void) fprintf(stderr,
"%s: TERM=%s: commented out code '%.2s' "
"is unknown.\n", progname, term_name,
tbuf+1);
continue;
}
if (verbose)
(void) fprintf(trace, "looking at termcap string "
"'%.2s'.\n", tbuf);
switch (tbuf[2]) {
case ':': case '\0': type = tbool; break;
case '#': type = tnum; break;
case '=': type = tstr; break;
case '@': type = tcancel; break;
default:
(void) fprintf(stderr,
"%s: TERM=%s: unknown type given for the "
"termcap code '%.2s'.\n", progname,
term_name, tbuf);
type = tunknown;
}
if (verbose > 1)
(void) fprintf(trace, "type of '%.2s' is %s.\n", tbuf,
(type == tbool) ? "boolean" :
(type == tnum) ? "numeric" :
(type = tstr) ? "string" :
(type = tcancel) ? "canceled" : "unknown");
/* look for the name in bools */
if (capsearch(boolcodes, oboolcodes, tbuf)) {
if (type != tbool && type != tcancel)
(void) fprintf(stderr,
"%s: TERM=%s: wrong type given for the "
"boolean termcap code '%.2s'.\n", progname,
term_name, tbuf);
continue;
}
/* look for the name in nums */
if (capsearch(numcodes, onumcodes, tbuf)) {
if (type != tnum && type != tcancel)
(void) fprintf(stderr,
"%s: TERM=%s: wrong type given for the "
"numeric termcap code '%.2s'.\n", progname,
term_name, tbuf);
continue;
}
/* look for the name in strs */
if (capsearch(strcodes, ostrcodes, tbuf)) {
if (type != tstr && type != tcancel)
(void) fprintf(stderr,
"%s: TERM=%s: wrong type given for the "
"string termcap code '%.2s'.\n", progname,
term_name, tbuf);
continue;
}
(void) fprintf(stderr,
"%s: TERM=%s: the %s termcap code '%.2s' is not a valid "
"name.\n", progname, term_name,
(type == tbool) ? "boolean" :
(type == tnum) ? "numeric" :
(type = tstr) ? "string" :
(type = tcancel) ? "canceled" : "(unknown type)", tbuf);
}
}
/*
* Fill up the termcap tables.
*/
int
filltables(void)
{
int i, tret;
/* Retrieve the termcap entry. */
if ((tret = otgetent(bp, term_name)) != 1) {
(void) fprintf(stderr,
"%s: TERM=%s: tgetent failed with return code %d (%s).\n",
progname, term_name, tret,
(tret == 0) ? "non-existent or invalid entry" :
(tret == -1) ? "cannot open $TERMCAP" : "unknown reason");
return (0);
}
if (verbose) {
(void) fprintf(trace, "bp=");
(void) cpr(trace, bp);
(void) fprintf(trace, ".\n");
}
if (uselevel == 0)
checktermcap();
/* Retrieve the values that are in terminfo. */
/* booleans */
for (i = 0; boolcodes[i]; i++) {
boolval[uselevel][i] = otgetflag(boolcodes[i]);
if (verbose > 1) {
(void) fprintf(trace, "boolcodes=%s, ", boolcodes[i]);
(void) fprintf(trace, "boolnames=%s, ", boolnames[i]);
(void) fprintf(trace,
"flag=%d.\n", boolval[uselevel][i]);
}
}
/* numbers */
for (i = 0; numcodes[i]; i++) {
numval[uselevel][i] = otgetnum(numcodes[i]);
if (verbose > 1) {
(void) fprintf(trace, "numcodes=%s, ", numcodes[i]);
(void) fprintf(trace, "numnames=%s, ", numnames[i]);
(void) fprintf(trace, "num=%d.\n", numval[uselevel][i]);
}
}
if (uselevel == 0)
nextstring = capbuffer;
/* strings */
for (i = 0; strcodes[i]; i++) {
strval[uselevel][i] = otgetstr(strcodes[i], &nextstring);
if (verbose > 1) {
(void) fprintf(trace, "strcodes=%s, ", strcodes [i]);
(void) fprintf(trace, "strnames=%s, ", strnames [i]);
if (strval[uselevel][i]) {
(void) fprintf(trace, "str=");
tpr(trace, strval[uselevel][i]);
(void) fprintf(trace, ".\n");
}
else
(void) fprintf(trace, "str=NULL.\n");
}
/* remove zero length strings */
if (strval[uselevel][i] && (strval[uselevel][i][0] == '\0')) {
(void) fprintf(stderr,
"%s: TERM=%s: cap %s (info %s) is NULL: REMOVED\n",
progname, term_name, strcodes[i], strnames[i]);
strval[uselevel][i] = NULL;
}
}
/* Retrieve the values not found in terminfo anymore. */
/* booleans */
for (i = 0; oboolcodes[i]; i++) {
oboolval[uselevel][i] = otgetflag(oboolcodes[i]);
if (verbose > 1) {
(void) fprintf(trace, "oboolcodes=%s, ",
oboolcodes[i]);
(void) fprintf(trace, "flag=%d.\n",
oboolval[uselevel][i]);
}
}
/* numbers */
for (i = 0; onumcodes[i]; i++) {
onumval[uselevel][i] = otgetnum(onumcodes[i]);
if (verbose > 1) {
(void) fprintf(trace, "onumcodes=%s, ", onumcodes[i]);
(void) fprintf(trace, "num=%d.\n",
onumval[uselevel][i]);
}
}
/* strings */
for (i = 0; ostrcodes[i]; i++) {
ostrval[uselevel][i] = otgetstr(ostrcodes[i], &nextstring);
if (verbose > 1) {
(void) fprintf(trace, "ostrcodes=%s, ", ostrcodes[i]);
if (ostrval[uselevel][i]) {
(void) fprintf(trace, "ostr=");
tpr(trace, ostrval[uselevel][i]);
(void) fprintf(trace, ".\n");
}
else
(void) fprintf(trace, "ostr=NULL.\n");
}
/* remove zero length strings */
if (ostrval[uselevel][i] && (ostrval[uselevel][i][0] == '\0')) {
(void) fprintf(stderr,
"%s: TERM=%s: cap %s (no terminfo name) is NULL: "
"REMOVED\n", progname, term_name, ostrcodes[i]);
ostrval[uselevel][i] = NULL;
}
}
return (1);
}
/*
* This routine copies the set of names from the termcap entry into
* a separate buffer, getting rid of the old obsolete two character
* names.
*/
static void
getlongname(void)
{
char *b = &bp[0], *l = buflongname;
/* Skip the two character name */
if (bp[2] == '|')
b = &bp[3];
/* Copy the rest of the names */
while (*b && *b != ':')
*l++ = *b++;
*l = '\0';
if (b != &bp[0]) {
(void) fprintf(stderr, "%s: obsolete 2 character name "
"'%2.2s' removed.\n", progname, bp);
(void) fprintf(stderr, "\tsynonyms are: '%s'\n", buflongname);
}
}
/*
* Return the value of the termcap string 'capname' as stored in our list.
*/
char *
getcapstr(char *capname)
{
int i;
if (verbose > 1)
(void) fprintf(trace, "looking for termcap value of %s.\n",
capname);
/* Check the old termcap list. */
for (i = 0; ostrcodes[i]; i++)
if (strcmp(ostrcodes[i], capname) == 0) {
if (verbose > 1) {
(void) fprintf(trace, "\tvalue is:");
tpr(trace, ostrval[uselevel][i]);
(void) fprintf(trace, ".\n");
}
return (ostrval[uselevel][i]);
}
if (verbose > 1)
(void) fprintf(trace, "termcap name '%s' not found in "
"ostrcodes.\n", capname);
/* Check the terminfo list. */
for (i = 0; strcodes[i]; i++)
if (strcmp(strcodes[i], capname) == 0) {
if (verbose > 1) {
(void) fprintf(trace, "\tvalue is:");
tpr(trace, strval[uselevel][i]);
(void) fprintf(trace, ".\n");
}
return (strval[uselevel][i]);
}
(void) fprintf(stderr, "%s: TERM=%s: termcap name '%s' not found.\n",
progname, term_name, capname);
return ((char *)NULL);
}
/*
* Search for a name in the given table and
* return the index.
* Someday I'll redo this to use bsearch().
*/
/* ARGSUSED */
int
search(char *names[], int max, char *infoname)
{
#ifndef BSEARCH
int i;
for (i = 0; names [i] != NULL; i++)
if (strcmp(names [i], infoname) == 0)
return (i);
return (-1);
#else /* this doesn't work for some reason */
char **bret;
bret = (char **)bsearch(infoname, (char *)names, max,
sizeof (char *), strcmp);
(void) fprintf(trace, "search looking for %s.\n", infoname);
(void) fprintf(trace, "base=%#x, bret=%#x, nel=%d.\n", names,
bret, max);
(void) fprintf(trace, "returning %d.\n", bret == NULL ? -1 :
bret - names);
if (bret == NULL)
return (-1);
else
return (bret - names);
#endif /* OLD */
}
/*
* return the value of the terminfo string 'infoname'
*/
char *
getinfostr(char *infoname)
{
int i;
if (verbose > 1)
(void) fprintf(trace, "looking for terminfo value of %s.\n",
infoname);
i = search(strnames, strcount, infoname);
if (i != -1) {
if (verbose > 1) {
(void) fprintf(trace, "\tvalue is:");
tpr(trace, strval[uselevel][i]);
(void) fprintf(trace, ".\n");
}
return (strval[uselevel][i]);
}
if (verbose > 1)
(void) fprintf(trace, "terminfo name '%s' not found.\n",
infoname);
return ((char *)NULL);
}
/*
* Replace the value stored for the terminfo boolean
* capability 'infoname' with the newvalue.
*/
void
putbool(char *infoname, int newvalue)
{
int i;
if (verbose > 1)
(void) fprintf(trace, "changing value for %s to %d.\n",
infoname, newvalue);
i = search(boolnames, boolcount, infoname);
if (i != -1) {
if (verbose > 1)
(void) fprintf(trace, "value was: %d.\n",
boolval[uselevel][i]);
boolval[uselevel][i] = newvalue;
return;
}
(void) fprintf(stderr, "%s: TERM=%s: the boolean name '%s' was not "
"found!\n", progname, term_name, infoname);
}
/*
* Replace the value stored for the terminfo number
* capability 'infoname' with the newvalue.
*/
void
putnum(char *infoname, int newvalue)
{
int i;
if (verbose > 1)
(void) fprintf(trace, "changing value for %s to %d.\n",
infoname, newvalue);
i = search(numnames, numcount, infoname);
if (i != -1) {
if (verbose > 1)
(void) fprintf(trace, "value was: %d.\n",
numval[uselevel][i]);
numval[uselevel][i] = newvalue;
return;
}
(void) fprintf(stderr, "%s: TERM=%s: the numeric name '%s' was not "
"found!\n",
progname, term_name, infoname);
}
/*
* replace the value stored for the terminfo string capability 'infoname'
* with the newvalue.
*/
void
putstr(char *infoname, char *newvalue)
{
int i;
if (verbose > 1) {
(void) fprintf(trace, "changing value for %s to ", infoname);
tpr(trace, newvalue);
(void) fprintf(trace, ".\n");
}
i = search(strnames, strcount, infoname);
if (i != -1) {
if (verbose > 1) {
(void) fprintf(trace, "value was:");
tpr(trace, strval[uselevel][i]);
(void) fprintf(trace, ".\n");
}
strval[uselevel][i] = nextstring;
while (*newvalue)
*nextstring++ = *newvalue++;
*nextstring++ = '\0';
return;
}
(void) fprintf(stderr, "%s: TERM=%s: the string name '%s' was not "
"found!\n",
progname, term_name, infoname);
}
/*
* Add in extra delays if they are not recorded already.
* This is done before the padding information has been modified by
* changecalculations() below, so the padding information, if there
* already, is still at the beginning of the string in termcap format.
*/
void
addpadding(int cappadding, char *infostr)
{
char *cap;
char tempbuffer [100];
/* Is there padding to add? */
if (cappadding > 0)
/* Is there a string to add it to? */
if (cap = getinfostr(infostr))
/* Is there any padding info already? */
if (ispadchar(*cap)) {
/* EMPTY */;
/* Assume that the padding info that is there is correct. */
} else {
/* Add the padding at the end of the present string. */
(void) snprintf(tempbuffer, sizeof (tempbuffer),
"%s$<%d>", cap, cappadding);
putstr(infostr, tempbuffer);
} else {
/* Create a new string that only has the padding. */
(void) sprintf(tempbuffer, "$<%d>", cappadding);
putstr(infostr, tempbuffer);
}
}
struct
{
char *capname;
char *keyedinfoname;
} ko_map[] = {
"al", "kil1",
"bs", "kbs", /* special addition */
"bt", "kcbt",
"cd", "ked",
"ce", "kel",
"cl", "kclr",
"ct", "ktbc",
"dc", "kdch1",
"dl", "kdl1",
"do", "kcud1",
"ei", "krmir",
"ho", "khome",
"ic", "kich1",
"im", "kich1", /* special addition */
"le", "kcub1",
"ll", "kll",
"nd", "kcuf1",
"sf", "kind",
"sr", "kri",
"st", "khts",
"up", "kcuu1",
/* "", "kctab", */
/* "", "knp", */
/* "", "kpp", */
0, 0
};
/*
* Work with the ko string. It is a comma separated list of keys for which
* the keyboard has a key by the same name that emits the same sequence.
* For example, ko = dc, im, ei means that there are keys called
* delete-character, enter-insert-mode and exit-insert-mode on the keyboard,
* and they emit the same sequences as specified in the dc, im and ei
* capabilities.
*/
static void
handleko(void)
{
char capname[3];
char *capstr;
int i, j, found;
char *infostr;
if (verbose > 1)
(void) fprintf(trace, "working on termcap ko string.\n");
if (ostrval[uselevel][cap_ko] == NULL)
return;
capname[2] = '\0';
for (i = 0; ostrval[uselevel][cap_ko][i] != '\0'; ) {
/* isolate the termcap name */
capname[0] = ostrval[uselevel][cap_ko][i++];
if (ostrval[uselevel][cap_ko][i] == '\0')
break;
capname[1] = ostrval[uselevel][cap_ko][i++];
if (ostrval[uselevel][cap_ko][i] == ',')
i++;
if (verbose > 1) {
(void) fprintf(trace, "key termcap name is '");
tpr(trace, capname);
(void) fprintf(trace, "'.\n");
}
/* match it up into our list */
found = 0;
for (j = 0; !found && ko_map[j].keyedinfoname != NULL; j++) {
if (verbose > 1)
(void) fprintf(trace, "looking at termcap name %s.\n",
ko_map[j].capname);
if (capname[0] == ko_map[j].capname[0] &&
capname[1] == ko_map[j].capname[1]) {
/* add the value to our database */
if ((capstr = getcapstr(capname)) != NULL) {
infostr = getinfostr
(ko_map[j].keyedinfoname);
if (infostr == NULL) {
/* skip any possible padding */
/* information */
while (ispadchar(*capstr))
capstr++;
putstr(ko_map[j].keyedinfoname, capstr);
} else
if (strcmp(capstr, infostr) != 0) {
(void) fprintf(stderr,
"%s: TERM=%s: a function "
"key for '%s' was "
"specified with the "
"value ", progname,
term_name, capname);
tpr(stderr, capstr);
(void) fprintf(stderr,
", but it already has the "
"value '");
tpr(stderr, infostr);
(void) fprintf(stderr, "'.\n");
}
}
found = 1;
}
}
if (!found) {
(void) fprintf(stderr, "%s: TERM=%s: the unknown "
"termcap name '%s' was\n", progname, term_name,
capname);
(void) fprintf(stderr, "specified in the 'ko' "
"termcap capability.\n");
}
}
}
#define CONTROL(x) ((x) & 037)
struct
{
char vichar;
char *keyedinfoname;
} ma_map[] = {
CONTROL('J'), "kcud1", /* down */
CONTROL('N'), "kcud1",
'j', "kcud1",
CONTROL('P'), "kcuu1", /* up */
'k', "kcuu1",
'h', "kcub1", /* left */
CONTROL('H'), "kcub1",
' ', "kcuf1", /* right */
'l', "kcuf1",
'H', "khome", /* home */
CONTROL('L'), "kclr", /* clear */
0, 0
};
/*
* Work with the ma string. This is a list of pairs of characters.
* The first character is the what a function key sends. The second
* character is the equivalent vi function that should be done when
* it receives that character. Note that only function keys that send
* a single character could be defined by this list.
*/
void
prchar(FILE *stream, int c)
{
char xbuf[2];
xbuf[0] = c;
xbuf[1] = '\0';
(void) fprintf(stream, "%s", iexpand(xbuf));
}
static void
handlema(void)
{
char vichar;
char cap[2];
int i, j, found;
char *infostr;
if (verbose > 1)
(void) fprintf(trace, "working on termcap ma string.\n");
if (ostrval[uselevel][cap_ma] == NULL)
return;
cap[1] = '\0';
for (i = 0; ostrval[uselevel][cap_ma][i] != '\0'; ) {
/* isolate the key's value */
cap[0] = ostrval[uselevel][cap_ma][i++];
if (verbose > 1) {
(void) fprintf(trace, "key value is '");
tpr(trace, cap);
(void) fprintf(trace, "'.\n");
}
if (ostrval[uselevel][cap_ma][i] == '\0')
break;
/* isolate the vi key name */
vichar = ostrval[uselevel][cap_ma][i++];
if (verbose > 1) {
(void) fprintf(trace, "the vi key is '");
prchar(trace, vichar);
(void) fprintf(trace, "'.\n");
}
/* match up the vi name in our list */
found = 0;
for (j = 0; !found && ma_map[j].keyedinfoname != NULL; j++) {
if (verbose > 1) {
(void) fprintf(trace, "looking at vi "
"character '");
prchar(trace, ma_map[j].vichar);
(void) fprintf(trace, "'\n");
}
if (vichar == ma_map[j].vichar) {
infostr = getinfostr(ma_map[j].keyedinfoname);
if (infostr == NULL)
putstr(ma_map[j].keyedinfoname, cap);
else if (strcmp(cap, infostr) != 0) {
(void) fprintf(stderr, "%s: TERM=%s: "
"the vi character '", progname,
term_name);
prchar(stderr, vichar);
(void) fprintf(stderr,
"' (info '%s') has the value '",
ma_map[j].keyedinfoname);
tpr(stderr, infostr);
(void) fprintf(stderr, "', but 'ma' "
"gives '");
prchar(stderr, cap[0]);
(void) fprintf(stderr, "'.\n");
}
found = 1;
}
}
if (!found) {
(void) fprintf(stderr, "%s: the unknown vi key '",
progname);
prchar(stderr, vichar);
(void) fprintf(stderr, "' was\n");
(void) fprintf(stderr, "specified in the 'ma' termcap "
"capability.\n");
}
}
}
/*
* Many capabilities were defaulted in termcap which must now be explicitly
* given. We'll assume that the defaults are in effect for this terminal.
*/
void
adddefaults(void)
{
char *cap;
int sg;
if (verbose > 1)
(void) fprintf(trace, "assigning defaults.\n");
/* cr was assumed to be ^M, unless nc was given, */
/* which meant it could not be done. */
/* Also, xr meant that ^M acted strangely. */
if ((getinfostr("cr") == NULL) && !oboolval[uselevel][cap_nc] &&
!oboolval[uselevel][cap_xr])
if ((cap = getcapstr("cr")) == NULL)
putstr("cr", "\r");
else
putstr("cr", cap);
/* cursor down was assumed to be ^J if not specified by nl */
if (getinfostr("cud1") == NULL)
if (ostrval[uselevel][cap_nl] != NULL)
putstr("cud1", ostrval[uselevel][cap_nl]);
else
putstr("cud1", "\n");
/* ind was assumed to be ^J, unless ns was given, */
/* which meant it could not be done. */
if ((getinfostr("ind") == NULL) && !oboolval[uselevel][cap_ns])
if (ostrval[uselevel][cap_nl] == NULL)
putstr("ind", "\n");
else
putstr("ind", ostrval[uselevel][cap_nl]);
/* bel was assumed to be ^G */
if (getinfostr("bel") == NULL)
putstr("bel", "\07");
/* if bs, then could do backspacing, */
/* with value of bc, default of ^H */
if ((getinfostr("cub1") == NULL) && oboolval[uselevel][cap_bs])
if (ostrval[uselevel][cap_bc] != NULL)
putstr("cub1", ostrval[uselevel][cap_bc]);
else
putstr("cub1", "\b");
/* default xon to true */
if (!otgetflag("xo"))
putbool("xon", 1);
/* if pt, then hardware tabs are allowed, */
/* with value of ta, default of ^I */
if ((getinfostr("ht") == NULL) && oboolval[uselevel][cap_pt])
if ((cap = getcapstr("ta")) == NULL)
putstr("ht", "\t");
else
putstr("ht", cap);
/* The dX numbers are now stored as padding */
/* in the appropriate terminfo string. */
addpadding(onumval[uselevel][cap_dB], "cub1");
addpadding(onumval[uselevel][cap_dC], "cr");
addpadding(onumval[uselevel][cap_dF], "ff");
addpadding(onumval[uselevel][cap_dN], "cud1");
addpadding(onumval[uselevel][cap_dT], "ht");
/* The ug and sg caps were essentially identical, */
/* so ug almost never got used. We set sg from ug */
/* if it hasn't already been set. */
if (onumval[uselevel][cap_ug] >= 0 && (sg = otgetnum("sg")) < 0)
putnum("xmc", onumval[uselevel][cap_ug]);
else if ((onumval[uselevel][cap_ug] >= 0) &&
(sg >= 0) && (onumval[uselevel][cap_ug] != sg))
(void) fprintf(stderr,
"%s: TERM=%s: Warning: termcap sg and ug had different "
"values (%d<->%d).\n", progname, term_name, sg,
onumval[uselevel][cap_ug]);
/* The MT boolean was never really part of termcap, */
/* but we can check for it anyways. */
if (oboolval[uselevel][cap_MT] && !otgetflag("km"))
putbool("km", 1);
/* the rs string was renamed r2 (info rs2) */
if ((ostrval[uselevel][cap_rs] != NULL) &&
(ostrval[uselevel][cap_rs][0] != '\0'))
putstr("rs2", ostrval[uselevel][cap_rs]);
handleko();
handlema();
}
#define caddch(x) *to++ = (x)
/*
* add the string to the string table
*/
char *
caddstr(char *to, char *str)
{
while (*str)
*to++ = *str++;
return (to);
}
/* If there is no padding info or parmed strings, */
/* then we do not need to copy the string. */
int
needscopying(char *string)
{
/* any string at all? */
if (string == NULL)
return (0);
/* any padding info? */
if (ispadchar(*string))
return (1);
/* any parmed info? */
while (*string)
if (*string++ == '%')
return (1);
return (0);
}
/*
* Certain manipulations of the stack require strange manipulations of the
* values that are on the stack. To handle these, we save the values of the
* parameters in registers at the very beginning and make the changes in
* the registers. We don't want to do this in the general case because of the
* potential performance loss.
*/
int
fancycap(char *string)
{
int parmset = 0;
while (*string)
if (*string++ == '%') {
switch (*string) {
/* These manipulate just the top value on */
/* the stack, so we only have to do */
/* something strange if a %r follows. */
case '>': case 'B': case 'D':
parmset = 1;
break;
/* If the parm has already been been */
/* pushed onto the stack by %>, then we */
/* can not reverse the parms and must get */
/* them from the registers. */
case 'r':
if (parmset)
return (1);
break;
/* This manipulates both parameters, so we */
/* cannot just do one and leave the value */
/* on the stack like we can with %>, */
/* %B or %D. */
case 'n':
return (1);
}
string++;
}
return (0);
}
/*
* Change old style of doing calculations to the new stack style.
* Note that this will not necessarily produce the most efficient string,
* but it will work.
*/
void
changecalculations()
{
int i, currentparm;
char *from, *to = nextstring;
int ch;
int parmset, parmsaved;
char padding[100], *saveto;
for (i = 0; strnames[i]; i++)
if (needscopying(strval[uselevel][i])) {
if (verbose) {
(void) fprintf(trace, "%s needs copying, "
"was:", strnames [i]);
tpr(trace, strval[uselevel][i]);
(void) fprintf(trace, ".\n");
}
from = strval[uselevel][i];
strval[uselevel][i] = to;
currentparm = 1;
parmset = 0;
/* Handle padding information. Save it so that it can be */
/* placed at the end of the string where it should */
/* have been in the first place. */
if (ispadchar(*from)) {
saveto = to;
to = padding;
to = caddstr(to, "$<");
while (isdigit(*from) || *from == '.')
caddch(*from++);
if (*from == '*')
caddch(*from++);
caddch('>');
caddch('\0');
to = saveto;
} else
padding[0] = '\0';
if (fancycap(from)) {
to = caddstr(to, "%p1%Pa%p2%Pb");
parmsaved = 1;
(void) fprintf(stderr,
"%s: TERM=%s: Warning: the string "
"produced for '%s' may be inefficient.\n",
progname, term_name, strnames[i]);
(void) fprintf(stderr, "It should be "
"looked at by hand.\n");
} else
parmsaved = 0;
while ((ch = *from++) != '\0')
if (ch != '%')
caddch(ch);
else
switch (ch = *from++) {
case '.': /* %. -> %p1%c */
case 'd': /* %d -> %p1%d */
case '2': /* %2 -> %p1%2.2d */
case '3': /* %3 -> %p1%3.3d */
case '+':
/* %+x -> %p1%'x'%+%c */
case '>':
/* %>xy -> %p1%Pc%?%'x'%> */
/* %t%gc%'y'%+ */
/* if current value > x, then add y. */
/* No output. */
case 'B':
/* %B: BCD */
/* (16*(x/10))+(x%10) */
/* No output. */
/* (Adds Regent 100) */
case 'D':
/* %D: Reverse coding */
/* (x-2*(x%16)) */
/* No output. */
/* (Delta Data) */
if (!parmset)
if (parmsaved) {
to = caddstr(to, "%g");
if (currentparm == 1)
caddch('a');
else
caddch('b');
} else {
to = caddstr(to, "%p");
if (currentparm == 1)
caddch('1');
else
caddch('2');
}
currentparm = 3 - currentparm;
parmset = 0;
switch (ch) {
case '.':
to = caddstr(to, "%c");
break;
case 'd':
to = caddstr(to, "%d");
break;
case '2': case '3':
#ifdef USG /* Vr2==USG, Vr3==SYSV. Use %02d for Vr2, %2.2d for Vr3 */
caddch('%');
caddch('0');
#else
caddch('%');
caddch(ch);
caddch('.');
#endif /* USG vs. SYSV */
caddch(ch);
caddch('d');
break;
case '+':
to = caddstr(to, "%'");
caddch(*from++);
to = caddstr(to,
"'%+%c");
break;
case '>':
to = caddstr(to,
"%Pc%?%'");
caddch(*from++);
to = caddstr(to,
"'%>%t%gc%'");
caddch(*from++);
to = caddstr(to,
"'%+");
parmset = 1;
break;
case 'B':
to = caddstr(to,
"%Pc%gc%{10}%/%{16}%*%gc%{10}%m%+");
parmset = 1;
break;
case 'D':
to = caddstr(to,
"%Pc%gc%gc%{16}%m%{2}%*%-");
parmset = 1;
break;
}
break;
/* %r reverses current parameter */
case 'r':
currentparm = 3 - currentparm;
break;
/* %n: exclusive-or row AND column */
/* with 0140, 96 decimal, no output */
/* (Datamedia 2500, Exidy Sorceror) */
case 'n':
to = caddstr(to,
"%ga%'`'%^%Pa");
to = caddstr(to,
"%gb%'`'%^%Pb");
break;
/* assume %x means %x */
/* this includes %i and %% */
default:
caddch('%');
caddch(ch);
}
to = caddstr(to, padding);
caddch('\0');
if (verbose) {
(void) fprintf(trace, "and has become:");
tpr(trace, strval[uselevel][i]);
(void) fprintf(trace, ".\n");
}
}
nextstring = to;
}
static void
print_no_use_entry(void)
{
int i;
pr_heading("", buflongname);
pr_bheading();
for (i = 0; boolcodes[i]; i++)
if (boolval[0][i])
pr_boolean(boolnames[i], (char *)0, (char *)0, 1);
pr_bfooting();
pr_sheading();
for (i = 0; numcodes[i]; i++)
if (numval[0][i] > -1)
pr_number(numnames[i], (char *)0, (char *)0,
numval[0][i]);
pr_nfooting();
pr_sheading();
for (i = 0; strcodes[i]; i++)
if (strval[0][i])
pr_string(strnames[i], (char *)0, (char *)0,
strval[0][i]);
pr_sfooting();
}
static void
print_use_entry(char *usename)
{
int i;
pr_heading("", buflongname);
pr_bheading();
for (i = 0; boolcodes[i]; i++)
if (boolval[0][i] && !boolval[1][i])
pr_boolean(boolnames[i], (char *)0, (char *)0, 1);
else if (!boolval[0][i] && boolval[1][i])
pr_boolean(boolnames[i], (char *)0, (char *)0, -1);
pr_bfooting();
pr_nheading();
for (i = 0; numcodes[i]; i++)
if ((numval[0][i] > -1) && (numval[0][i] != numval[1][i]))
pr_number(numnames[i], (char *)0, (char *)0,
numval[0][i]);
else if ((numval [0] [i] == -1) && (numval [1] [i] > -1))
pr_number(numnames[i], (char *)0, (char *)0, -1);
pr_nfooting();
pr_sheading();
for (i = 0; strcodes[i]; i++)
/* print out str[0] if: */
/* str[0] != NULL and str[1] == NULL, or str[0] != str[1] */
if (strval[0][i] && ((strval[1][i] == NULL) ||
(strcmp(strval[0][i], strval[1][i]) != 0)))
pr_string(strnames[i], (char *)0, (char *)0,
strval[0][i]);
/* print out @ if str[0] == NULL and str[1] != NULL */
else if (strval[0][i] == NULL && strval[1][i] != NULL)
pr_string(strnames[i], (char *)0, (char *)0,
(char *)0);
pr_sfooting();
(void) printf("\tuse=%s,\n", usename);
}
static void
captoinfo(void)
{
char usename[512];
char *sterm_name;
if (term_name == NULL) {
(void) fprintf(stderr, "%s: Null term_name given.\n",
progname);
return;
}
if (verbose)
(void) fprintf(trace, "changing cap to info, TERM=%s.\n",
term_name);
uselevel = 0;
if (filltables() == 0)
return;
getlongname();
adddefaults();
changecalculations();
if (TLHtcfound != 0) {
uselevel = 1;
if (verbose)
(void) fprintf(trace, "use= found, %s uses %s.\n",
term_name, TLHtcname);
(void) strcpy(usename, TLHtcname);
sterm_name = term_name;
term_name = usename;
if (filltables() == 0)
return;
adddefaults();
changecalculations();
term_name = sterm_name;
print_use_entry(usename);
} else
print_no_use_entry();
}
#include <signal.h> /* use this file to determine if this is SVR4.0 system */
static void
use_etc_termcap(void)
{
if (verbose)
#ifdef SIGSTOP
(void) fprintf(trace, "reading from /usr/share/lib/termcap\n");
#else /* SIGSTOP */
(void) fprintf(trace, "reading from /etc/termcap\n");
#endif /* SIGSTOP */
term_name = getenv("TERM");
captoinfo();
}
static void
initdirname(void)
{
#if defined(SYSV) || defined(USG) /* handle both Sys Vr2 and Vr3 curses */
(void) getcwd(dirname, BUFSIZ-2);
#else
(void) getwd(dirname);
#endif /* SYSV || USG */
if (verbose)
(void) fprintf(trace, "current directory name=%s.\n", dirname);
environ = newenviron;
}
static void
setfilename(char *capfile)
{
if (capfile [0] == '/')
(void) snprintf(TERMCAP, sizeof (TERMCAP),
"TERMCAP=%s", capfile);
else
(void) snprintf(TERMCAP, sizeof (TERMCAP),
"TERMCAP=%s/%s", dirname, capfile);
if (verbose)
(void) fprintf(trace, "setting the environment for %s.\n",
TERMCAP);
}
static void
setterm_name(void)
{
if (verbose)
(void) fprintf(trace, "setting the environment "
"for TERM=%s.\n", term_name);
(void) snprintf(TERM, sizeof (TERM), "TERM=%s", term_name);
}
/* Look at the current line to see if it is a list of names. */
/* If it is, return the first name in the list, else NULL. */
/* As a side-effect, comment lines and blank lines */
/* are copied to standard output. */
char *
getterm_name(char *line)
{
char *lineptr = line;
if (verbose)
(void) fprintf(trace, "extracting name from '%s'.\n", line);
/* Copy comment lines out. */
if (*line == '#') {
if (copycomments)
(void) printf("%s", line);
}
/* Blank lines get copied too. */
else if (isspace (*line)) {
if (copycomments) {
for (; *lineptr; lineptr++)
if (!isspace(*lineptr))
break;
if (*lineptr == '\0')
(void) printf("\n");
}
}
else
for (; *lineptr; lineptr++)
if (*lineptr == '|' || *lineptr == ':') {
*lineptr = '\0';
if (verbose)
(void) fprintf(trace,
"returning %s.\n", line);
return (line);
}
if (verbose)
(void) fprintf(trace, "returning NULL.\n");
return (NULL);
}
static void
use_file(char *filename)
{
FILE *termfile;
char buffer[BUFSIZ];
if (verbose)
(void) fprintf(trace, "reading from %s.\n", filename);
if ((termfile = fopen(filename, "r")) == NULL) {
(void) fprintf(stderr, "%s: cannot open %s for reading.\n",
progname, filename);
return;
}
copycomments++;
setfilename(filename);
while (fgets(buffer, BUFSIZ, termfile) != NULL) {
if ((term_name = getterm_name(buffer)) != NULL) {
setterm_name();
captoinfo();
}
}
}
/*
* Sort a name and code table pair according to the name table.
* Use a simple bubble sort for now. Too bad I can't call qsort(3).
* At least I only have to do it once for each table.
*/
static void
sorttable(char *nametable[], char *codetable[])
{
int i, j;
char *c;
for (i = 0; nametable[i]; i++)
for (j = 0; j < i; j++)
if (strcmp(nametable[i], nametable[j]) < 0) {
c = nametable[i];
nametable[i] = nametable[j];
nametable[j] = c;
c = codetable[i];
codetable[i] = codetable[j];
codetable[j] = c;
}
}
/*
* Initialize and sort the name and code tables. Allocate space for the
* value tables.
*/
static void
inittables(void)
{
unsigned int i;
for (i = 0; boolnames [i]; i++)
;
boolval[0] = (char *)malloc(i * sizeof (char));
boolval[1] = (char *)malloc(i * sizeof (char));
boolcount = i;
sorttable(boolnames, boolcodes);
for (i = 0; numcodes [i]; i++)
;
numval[0] = (short *)malloc(i * sizeof (short));
numval[1] = (short *)malloc(i * sizeof (short));
numcount = i;
sorttable(numnames, numcodes);
for (i = 0; strcodes [i]; i++)
;
strval[0] = (char **)malloc(i * sizeof (char *));
strval[1] = (char **)malloc(i * sizeof (char *));
strcount = i;
sorttable(strnames, strcodes);
}
int
main(int argc, char **argv)
{
int c;
char _capbuffer [8192];
char _bp [TBUFSIZE];
char _buflongname [128];
capbuffer = &_capbuffer[0];
bp = &_bp[0];
buflongname = &_buflongname[0];
progname = argv[0];
while ((c = getopt(argc, argv, "1vVw:")) != EOF)
switch (c) {
case '1':
pr_onecolumn(1);
break;
case 'w':
pr_width(atoi(optarg));
break;
case 'v':
verbose++;
break;
case 'V':
(void) printf("%s: version %s\n", progname,
"@(#)curses:screen/captoinfo.c 1.12");
(void) fflush(stdout);
exit(0);
/* FALLTHROUGH (not really) */
case '?':
(void) fprintf(stderr,
"usage: %s [-1Vv] [-w width] "
"[filename ...]\n", progname);
(void) fprintf(stderr, "\t-1\tsingle column "
"output\n");
(void) fprintf(stderr,
"\t-v\tverbose debugging output\n");
(void) fprintf(stderr,
"\t-V\tprint program version\n");
exit(-1);
}
/* initialize */
pr_init(pr_terminfo);
inittables();
if (optind >= argc)
use_etc_termcap();
else {
initdirname();
for (; optind < argc; optind++)
use_file(argv [optind]);
}
return (0);
}
/* fake out the modules in print.c so we don't have to load in */
/* cexpand.c and infotocap.c */
/* ARGSUSED */
int
cpr(FILE *stream, char *string)
{
return (0);
}
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
#
#
# MAPFILE HEADER START
#
# WARNING: STOP NOW. DO NOT MODIFY THIS FILE.
# Object versioning must comply with the rules detailed in
#
# usr/src/lib/README.mapfiles
#
# You should not be making modifications here until you've read the most current
# copy of that file. If you need help, contact a gatekeeper for guidance.
#
# MAPFILE HEADER END
#
$mapfile_version 2
# captoinfo interposes on cpr() and progname[].
SYMBOL_SCOPE {
global:
cpr { FLAGS = INTERPOSE };
progname { FLAGS = INTERPOSE };
};
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1988 AT&T */
/* All Rights Reserved */
/* Copyright (c) 1979 Regents of the University of California */
/* Modified to: */
/* 1) remember the name of the first tc= parameter */
/* encountered during parsing. */
/* 2) handle multiple invocations of tgetent(). */
/* 3) tskip() is now available outside of the library. */
/* 4) remember $TERM name for error messages. */
/* 5) have a larger buffer. */
/* 6) really fix the bug that 5) got around. This fix by */
/* Marion Hakanson, orstcs!hakanson */
#include "otermcap.h"
#define MAXHOP 32 /* max number of tc= indirections */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/uio.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <signal.h> /* use this file to determine if this is SVR4.0 system */
#ifdef SIGSTOP /* SVR4.0 and beyond */
#define E_TERMCAP "/usr/share/lib/termcap"
#else
#define E_TERMCAP "/etc/termcap"
#endif
/*
* termcap - routines for dealing with the terminal capability data base
*
* BUG: Should use a "last" pointer in tbuf, so that searching
* for capabilities alphabetically would not be a n**2/2
* process when large numbers of capabilities are given.
* Note: If we add a last pointer now we will screw up the
* tc capability. We really should compile termcap.
*
* Essentially all the work here is scanning and decoding escapes
* in string capabilities. We don't use stdio because the editor
* doesn't, and because living w/o it is not hard.
*/
static char *tbuf;
static int hopcount; /* detect infinite loops in termcap, init 0 */
char *tskip(char *);
char *otgetstr(char *, char **);
/* Tony Hansen */
int TLHtcfound = 0;
char TLHtcname[16];
static char *termname;
static int _tgetent(char *, char *);
static int otnchktc(void);
static char *tdecode(char *, char **);
static int otnamatch(char *);
/*
* Get an entry for terminal name in buffer bp,
* from the termcap file. Parse is very rudimentary;
* we just notice escaped newlines.
*/
int
otgetent(char *bp, char *name)
{
/* Tony Hansen */
int ret;
TLHtcfound = 0;
hopcount = 0;
termname = name;
ret = _tgetent(bp, name);
/*
* There is some sort of bug in the check way down below to prevent
* buffer overflow. I really don't want to track it down, so I
* upped the standard buffer size and check here to see if the created
* buffer is larger than the old buffer size.
*/
if (strlen(bp) >= 1024)
(void) fprintf(stderr,
"tgetent(): TERM=%s: Termcap entry is too long.\n",
termname);
return (ret);
}
static int
_tgetent(char *bp, char *name)
{
char *cp;
int c;
int i = 0, cnt = 0;
char ibuf[TBUFSIZE];
char *cp2;
int tf;
tbuf = bp;
tf = 0;
#ifndef V6
cp = getenv("TERMCAP");
/*
* TERMCAP can have one of two things in it. It can be the
* name of a file to use instead of /etc/termcap. In this
* case it better start with a "/". Or it can be an entry to
* use so we don't have to read the file. In this case it
* has to already have the newlines crunched out.
*/
if (cp && *cp) {
if (*cp != '/') {
cp2 = getenv("TERM");
if (cp2 == (char *)0 || strcmp(name, cp2) == 0) {
(void) strcpy(bp, cp);
return (otnchktc());
} else {
tf = open(E_TERMCAP, 0);
}
} else
tf = open(cp, 0);
}
if (tf == 0)
tf = open(E_TERMCAP, 0);
#else
tf = open(E_TERMCAP, 0);
#endif
if (tf < 0)
return (-1);
for (; ; ) {
cp = bp;
for (; ; ) {
if (i == cnt) {
cnt = read(tf, ibuf, TBUFSIZE);
if (cnt <= 0) {
(void) close(tf);
return (0);
}
i = 0;
}
c = ibuf[i++];
if (c == '\n') {
if (cp > bp && cp[-1] == '\\') {
cp--;
continue;
}
break;
}
if (cp >= bp + TBUFSIZE) {
(void) fprintf(stderr, "tgetent(): TERM=%s: "
"Termcap entry too long\n", termname);
break;
} else
*cp++ = c;
}
*cp = 0;
/*
* The real work for the match.
*/
if (otnamatch(name)) {
(void) close(tf);
return (otnchktc());
}
}
}
/*
* otnchktc: check the last entry, see if it's tc=xxx. If so,
* recursively find xxx and append that entry (minus the names)
* to take the place of the tc=xxx entry. This allows termcap
* entries to say "like an HP2621 but doesn't turn on the labels".
* Note that this works because of the left to right scan.
*/
static int
otnchktc(void)
{
char *p, *q;
#define TERMNAMESIZE 16
char tcname[TERMNAMESIZE]; /* name of similar terminal */
char tcbuf[TBUFSIZE];
char *holdtbuf = tbuf;
int l;
p = tbuf + strlen(tbuf) - 2; /* before the last colon */
while (*--p != ':')
if (p < tbuf) {
(void) fprintf(stderr, "tnchktc(): TERM=%s: Bad "
"termcap entry\n", termname);
return (0);
}
p++;
/* p now points to beginning of last field */
if (p[0] != 't' || p[1] != 'c')
return (1);
(void) strncpy(tcname, p + 3, TERMNAMESIZE); /* TLH */
q = tcname;
while (*q && *q != ':')
q++;
*q = 0;
if (++hopcount > MAXHOP) {
(void) fprintf(stderr, "tnchktc(): TERM=%s: Infinite tc= "
"loop\n", termname);
return (0);
}
if (_tgetent(tcbuf, tcname) != 1)
return (0);
/* Tony Hansen */
TLHtcfound++;
(void) strcpy(TLHtcname, tcname);
for (q = tcbuf; *q != ':'; q++)
;
l = p - holdtbuf + strlen(q);
if (l > TBUFSIZE) {
(void) fprintf(stderr, "tnchktc(): TERM=%s: Termcap entry "
"too long\n", termname);
q[TBUFSIZE - (p - holdtbuf)] = 0;
}
(void) strcpy(p, q + 1);
tbuf = holdtbuf;
return (1);
}
/*
* Tnamatch deals with name matching. The first field of the termcap
* entry is a sequence of names separated by |'s, so we compare
* against each such name. The normal : terminator after the last
* name (before the first field) stops us.
*/
static int
otnamatch(char *np)
{
char *Np, *Bp;
Bp = tbuf;
if (*Bp == '#')
return (0);
for (;;) {
for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
continue;
if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
return (1);
while (*Bp && *Bp != ':' && *Bp != '|')
Bp++;
if (*Bp == 0 || *Bp == ':')
return (0);
Bp++;
}
}
/*
* Skip to the next field. Notice that this is very dumb, not
* knowing about \: escapes or any such. If necessary, :'s can be put
* into the termcap file in octal.
*/
char *
tskip(char *bp)
{
while (*bp && *bp != ':')
bp++;
if (*bp == ':')
bp++;
return (bp);
}
/*
* Return the (numeric) option id.
* Numeric options look like
* li#80
* i.e. the option string is separated from the numeric value by
* a # character. If the option is not found we return -1.
* Note that we handle octal numbers beginning with 0.
*/
int
otgetnum(char *id)
{
int i, base;
char *bp = tbuf;
for (;;) {
bp = tskip(bp);
if (*bp == 0)
return (-1);
if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
continue;
if (*bp == '@')
return (-1);
if (*bp != '#')
continue;
bp++;
base = 10;
if (*bp == '0')
base = 8;
i = 0;
while (isdigit(*bp))
i *= base, i += *bp++ - '0';
return (i);
}
}
/*
* Handle a flag option.
* Flag options are given "naked", i.e. followed by a : or the end
* of the buffer. Return 1 if we find the option, or 0 if it is
* not given.
*/
int
otgetflag(char *id)
{
char *bp = tbuf;
for (;;) {
bp = tskip(bp);
if (!*bp)
return (0);
if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
if (!*bp || *bp == ':')
return (1);
else if (*bp == '@')
return (0);
}
}
}
/*
* Get a string valued option.
* These are given as
* cl=^Z
* Much decoding is done on the strings, and the strings are
* placed in area, which is a ref parameter which is updated.
* No checking on area overflow.
*/
char *
otgetstr(char *id, char **area)
{
char *bp = tbuf;
for (; ; ) {
bp = tskip(bp);
if (!*bp)
return (0);
if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
continue;
if (*bp == '@')
return (0);
if (*bp != '=')
continue;
bp++;
return (tdecode(bp, area));
}
}
/*
* Tdecode does the grung work to decode the
* string capability escapes.
*/
static char *
tdecode(char *str, char **area)
{
char *cp;
int c;
char *dp;
int i;
cp = *area;
while ((c = *str++) != '\0' && c != ':') {
switch (c) {
case '^':
c = *str++ & 037;
break;
case '\\':
dp = "E\033^^\\\\::n\nr\rt\tb\bf\f";
c = *str++;
nextc:
if (*dp++ == c) {
c = *dp++;
break;
}
dp++;
if (*dp)
goto nextc;
if (isdigit(c)) {
c -= '0', i = 2;
do
c <<= 3, c |= *str++ - '0';
while (--i && isdigit(*str))
;
}
break;
}
*cp++ = c;
}
*cp++ = 0;
str = *area;
*area = cp;
return (str);
}
|