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
|
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
#
# Copyright 2011 Nexenta Systems, Inc. All rights reserved.
# Copyright (c) 2018, Joyent, Inc.
#
PROG=iconv
include ../Makefile.cmd
include ../Makefile.ctf
OBJS = iconv_main.o iconv_list.o charmap.o parser.tab.o scanner.o
CSTD= $(CSTD_GNU99)
LDLIBS += -lcmdutils -lavl
YFLAGS = -d -b parser
CPPFLAGS += -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE
# Hammerhead: converted from dmake conditional
ifneq ($(RELEASE_BUILD),$(POUND_SIGN))
CPPFLAGS += -DNDEBUG
endif
# because of labels from yacc
parser.tab.o : CERRWARN += -Wno-unused-label
# not linted
SMATCH=off
CLEANFILES = $(OBJS) parser.tab.c parser.tab.h
CLOBBERFILES = $(PROG) $(POFILE)
PIFILES = $(OBJS:%.o=%.i)
POFILE = iconv_cmd.po
all: $(PROG)
install: all $(ROOTPROG)
$(PROG): $(OBJS)
$(LINK.c) $(OBJS) -o $@ $(LDLIBS)
$(POST_PROCESS)
$(OBJS): parser.tab.h
$(PIFILES): parser.tab.h
parser.tab.c parser.tab.h: parser.y
$(YACC) $(YFLAGS) parser.y
clean:
$(RM) $(CLEANFILES)
$(POFILE): $(PIFILES)
$(RM) $@
$(RM) messages.po
$(XGETTEXT) -s $(PIFILES)
$(SED) -e '/domain/d' messages.po > $@
$(RM) $(PIFILES) messages.po
.KEEP_STATE:
include ../Makefile.targ
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2011 Nexenta Systems, Inc. All rights reserved.
*/
/*
* CHARMAP file handling for iconv.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <alloca.h>
#include <sys/avl.h>
#include <stddef.h>
#include <unistd.h>
#include "charmap.h"
#include "parser.tab.h"
#include <assert.h>
enum cmap_pass cmap_pass;
static avl_tree_t cmap_sym;
static avl_tree_t cmap_mbs;
typedef struct charmap {
const char *cm_name;
struct charmap *cm_alias_of;
avl_node_t cm_avl_sym;
avl_node_t cm_avl_mbs;
int cm_warned;
int cm_frmbs_len;
int cm_tombs_len;
char cm_frmbs[MB_LEN_MAX + 1]; /* input */
char cm_tombs[MB_LEN_MAX + 1]; /* output */
} charmap_t;
static void add_charmap_impl_fr(char *sym, char *mbs, int mbs_len, int nodups);
static void add_charmap_impl_to(char *sym, char *mbs, int mbs_len, int nodups);
/*
* Array of POSIX specific portable characters.
*/
static const struct {
char *name;
int ch;
} portable_chars[] = {
{ "NUL", '\0' },
{ "alert", '\a' },
{ "backspace", '\b' },
{ "tab", '\t' },
{ "carriage-return", '\r' },
{ "newline", '\n' },
{ "vertical-tab", '\v' },
{ "form-feed", '\f' },
{ "space", ' ' },
{ "exclamation-mark", '!' },
{ "quotation-mark", '"' },
{ "number-sign", '#' },
{ "dollar-sign", '$' },
{ "percent-sign", '%' },
{ "ampersand", '&' },
{ "apostrophe", '\'' },
{ "left-parenthesis", '(' },
{ "right-parenthesis", '(' },
{ "asterisk", '*' },
{ "plus-sign", '+' },
{ "comma", ','},
{ "hyphen-minus", '-' },
{ "hyphen", '-' },
{ "full-stop", '.' },
{ "period", '.' },
{ "slash", '/' },
{ "solidus", '/' },
{ "zero", '0' },
{ "one", '1' },
{ "two", '2' },
{ "three", '3' },
{ "four", '4' },
{ "five", '5' },
{ "six", '6' },
{ "seven", '7' },
{ "eight", '8' },
{ "nine", '9' },
{ "colon", ':' },
{ "semicolon", ';' },
{ "less-than-sign", '<' },
{ "equals-sign", '=' },
{ "greater-than-sign", '>' },
{ "question-mark", '?' },
{ "commercial-at", '@' },
{ "left-square-bracket", '[' },
{ "backslash", '\\' },
{ "reverse-solidus", '\\' },
{ "right-square-bracket", ']' },
{ "circumflex", '^' },
{ "circumflex-accent", '^' },
{ "low-line", '_' },
{ "underscore", '_' },
{ "grave-accent", '`' },
{ "left-brace", '{' },
{ "left-curly-bracket", '{' },
{ "vertical-line", '|' },
{ "right-brace", '}' },
{ "right-curly-bracket", '}' },
{ "tilde", '~' },
{ "A", 'A' },
{ "B", 'B' },
{ "C", 'C' },
{ "D", 'D' },
{ "E", 'E' },
{ "F", 'F' },
{ "G", 'G' },
{ "H", 'H' },
{ "I", 'I' },
{ "J", 'J' },
{ "K", 'K' },
{ "L", 'L' },
{ "M", 'M' },
{ "N", 'N' },
{ "O", 'O' },
{ "P", 'P' },
{ "Q", 'Q' },
{ "R", 'R' },
{ "S", 'S' },
{ "T", 'T' },
{ "U", 'U' },
{ "V", 'V' },
{ "W", 'W' },
{ "X", 'X' },
{ "Y", 'Y' },
{ "Z", 'Z' },
{ "a", 'a' },
{ "b", 'b' },
{ "c", 'c' },
{ "d", 'd' },
{ "e", 'e' },
{ "f", 'f' },
{ "g", 'g' },
{ "h", 'h' },
{ "i", 'i' },
{ "j", 'j' },
{ "k", 'k' },
{ "l", 'l' },
{ "m", 'm' },
{ "n", 'n' },
{ "o", 'o' },
{ "p", 'p' },
{ "q", 'q' },
{ "r", 'r' },
{ "s", 's' },
{ "t", 't' },
{ "u", 'u' },
{ "v", 'v' },
{ "w", 'w' },
{ "x", 'x' },
{ "y", 'y' },
{ "z", 'z' },
{ NULL, 0 }
};
static int
cmap_compare_sym(const void *n1, const void *n2)
{
const charmap_t *c1 = n1;
const charmap_t *c2 = n2;
int rv;
rv = strcmp(c1->cm_name, c2->cm_name);
return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
}
/*
* In order for partial match searches to work,
* we need these sorted by mbs contents.
*/
static int
cmap_compare_mbs(const void *n1, const void *n2)
{
const charmap_t *c1 = n1;
const charmap_t *c2 = n2;
int len, rv;
len = c1->cm_frmbs_len;
if (len < c2->cm_frmbs_len)
len = c2->cm_frmbs_len;
rv = memcmp(c1->cm_frmbs, c2->cm_frmbs, len);
if (rv < 0)
return (-1);
if (rv > 0)
return (1);
/* they match through length */
if (c1->cm_frmbs_len < c2->cm_frmbs_len)
return (-1);
if (c2->cm_frmbs_len < c1->cm_frmbs_len)
return (1);
return (0);
}
void
charmap_init(char *to_map, char *from_map)
{
avl_create(&cmap_sym, cmap_compare_sym, sizeof (charmap_t),
offsetof(charmap_t, cm_avl_sym));
avl_create(&cmap_mbs, cmap_compare_mbs, sizeof (charmap_t),
offsetof(charmap_t, cm_avl_mbs));
cmap_pass = CMAP_PASS_FROM;
reset_scanner(from_map);
(void) yyparse();
add_charmap_posix();
cmap_pass = CMAP_PASS_TO;
reset_scanner(to_map);
(void) yyparse();
}
void
charmap_dump()
{
charmap_t *cm;
int i;
cm = avl_first(&cmap_mbs);
while (cm != NULL) {
(void) printf("name=\"%s\"\n", cm->cm_name);
(void) printf("\timbs=\"");
for (i = 0; i < cm->cm_frmbs_len; i++)
(void) printf("\\x%02x", cm->cm_frmbs[i] & 0xFF);
(void) printf("\"\n");
(void) printf("\tombs=\"");
for (i = 0; i < cm->cm_tombs_len; i++)
(void) printf("\\x%02x", cm->cm_tombs[i] & 0xFF);
(void) printf("\"\n");
cm = AVL_NEXT(&cmap_mbs, cm);
}
}
/*
* We parse two charmap files: First the "from" map, where we build
* cmap_mbs and cmap_sym which we'll later use to translate the input
* stream (mbs encodings) to symbols. Second, we parse the "to" map,
* where we fill in the tombs members of entries in cmap_sym, (which
* must alread exist) used later to write the output encoding.
*/
static void
add_charmap_impl(char *sym, char *mbs, int mbs_len, int nodups)
{
/*
* While parsing both the "from" and "to" cmaps,
* require both the symbol and encoding.
*/
if (sym == NULL || mbs == NULL) {
errf(_("invalid charmap entry"));
return;
}
switch (cmap_pass) {
case CMAP_PASS_FROM:
add_charmap_impl_fr(sym, mbs, mbs_len, nodups);
break;
case CMAP_PASS_TO:
add_charmap_impl_to(sym, mbs, mbs_len, nodups);
break;
default:
abort();
break;
}
}
static void
add_charmap_impl_fr(char *sym, char *mbs, int mbs_len, int nodups)
{
charmap_t *m, *n, *s;
avl_index_t where_sym, where_mbs;
if ((n = calloc(1, sizeof (*n))) == NULL) {
errf(_("out of memory"));
return;
}
n->cm_name = sym;
assert(0 < mbs_len && mbs_len <= MB_LEN_MAX);
(void) memcpy(n->cm_frmbs, mbs, mbs_len);
n->cm_frmbs_len = mbs_len;
m = avl_find(&cmap_mbs, n, &where_mbs);
s = avl_find(&cmap_sym, n, &where_sym);
/*
* If we found the symbol, this is a dup.
*/
if (s != NULL) {
if (nodups) {
warn(_("%s: duplicate character symbol"), sym);
}
free(n);
return;
}
/*
* If we found the mbs, the new one is an alias,
* which we'll add _only_ to the symbol AVL.
*/
if (m != NULL) {
/* The new one is an alias of the original. */
n->cm_alias_of = m;
avl_insert(&cmap_sym, n, where_sym);
return;
}
avl_insert(&cmap_sym, n, where_sym);
avl_insert(&cmap_mbs, n, where_mbs);
}
static void
add_charmap_impl_to(char *sym, char *mbs, int mbs_len, int nodups)
{
charmap_t srch = {0};
charmap_t *m;
assert(0 < mbs_len && mbs_len <= MB_LEN_MAX);
srch.cm_name = sym;
m = avl_find(&cmap_sym, &srch, NULL);
if (m == NULL) {
if (sflag == 0)
warn(_("%s: symbol not found"), sym);
return;
}
if (m->cm_alias_of != NULL) {
m = m->cm_alias_of;
/* don't warn for dups with aliases */
if (m->cm_tombs_len != 0)
return;
}
if (m->cm_tombs_len != 0) {
if (nodups) {
warn(_("%s: duplicate encoding for"), sym);
}
return;
}
(void) memcpy(m->cm_tombs, mbs, mbs_len);
m->cm_tombs_len = mbs_len;
}
void
add_charmap(char *sym, char *mbs)
{
/* mbs[0] is the length */
int mbs_len = *mbs++;
assert(0 < mbs_len && mbs_len <= MB_LEN_MAX);
add_charmap_impl(sym, mbs, mbs_len, 1);
}
/*
* This is called by the parser with start/end symbol strings (ssym, esym),
* which are allocated in the scanner (T_SYMBOL) and free'd here.
*/
void
add_charmap_range(char *ssym, char *esym, char *mbs)
{
int ls, le;
int si;
int sn, en;
int i;
int mbs_len;
char tmbs[MB_LEN_MAX+1];
char *mb_last;
static const char *digits = "0123456789";
/* mbs[0] is the length */
mbs_len = *mbs++;
assert(0 < mbs_len && mbs_len <= MB_LEN_MAX);
(void) memcpy(tmbs, mbs, mbs_len);
mb_last = tmbs + mbs_len - 1;
ls = strlen(ssym);
le = strlen(esym);
if (((si = strcspn(ssym, digits)) == 0) || (si == ls) ||
(strncmp(ssym, esym, si) != 0) ||
(strspn(ssym + si, digits) != (ls - si)) ||
(strspn(esym + si, digits) != (le - si)) ||
((sn = atoi(ssym + si)) > ((en = atoi(esym + si))))) {
errf(_("malformed charmap range"));
return;
}
ssym[si] = 0;
for (i = sn; i <= en; i++) {
char *nn;
(void) asprintf(&nn, "%s%0*u", ssym, ls - si, i);
if (nn == NULL) {
errf(_("out of memory"));
return;
}
add_charmap_impl(nn, tmbs, mbs_len, 1);
(*mb_last)++;
}
free(ssym);
free(esym);
}
void
add_charmap_char(char *name, int c)
{
char mbs[MB_LEN_MAX+1];
mbs[0] = c;
mbs[1] = '\0';
add_charmap_impl(name, mbs, 1, 0);
}
/*
* POSIX insists that certain entries be present, even when not in the
* orginal charmap file.
*/
void
add_charmap_posix(void)
{
int i;
for (i = 0; portable_chars[i].name; i++) {
add_charmap_char(portable_chars[i].name, portable_chars[i].ch);
}
}
/*
* This is called with a buffer of (typically) MB_LEN_MAX bytes,
* which is potentially a multi-byte symbol, but often contains
* extra bytes. Find and return the longest match in the charmap.
*/
static charmap_t *
find_mbs(const char *mbs, int len)
{
charmap_t srch = {0};
charmap_t *cm = NULL;
while (len > 0) {
(void) memcpy(srch.cm_frmbs, mbs, len);
srch.cm_frmbs_len = len;
cm = avl_find(&cmap_mbs, &srch, NULL);
if (cm != NULL)
break;
len--;
}
return (cm);
}
/*
* Return true if this sequence matches the initial part
* of any sequence known in this charmap.
*/
static boolean_t
find_mbs_partial(const char *mbs, int len)
{
charmap_t srch = {0};
charmap_t *cm;
avl_index_t where;
(void) memcpy(srch.cm_frmbs, mbs, len);
srch.cm_frmbs_len = len;
cm = avl_find(&cmap_mbs, &srch, &where);
if (cm != NULL) {
/* full match - not expected, but OK */
return (B_TRUE);
}
cm = avl_nearest(&cmap_mbs, where, AVL_AFTER);
if (cm != NULL && 0 == memcmp(cm->cm_frmbs, mbs, len))
return (B_TRUE);
return (B_FALSE);
}
/*
* Do like iconv(3), but with charmaps.
*/
size_t
cm_iconv(const char **iptr, size_t *ileft, char **optr, size_t *oleft)
{
charmap_t *cm;
int mbs_len;
/* Ignore state reset requests. */
if (iptr == NULL || *iptr == NULL)
return (0);
if (*oleft < MB_LEN_MAX) {
errno = E2BIG;
return ((size_t)-1);
}
while (*ileft > 0 && *oleft >= MB_LEN_MAX) {
mbs_len = MB_LEN_MAX;
if (mbs_len > *ileft)
mbs_len = *ileft;
cm = find_mbs(*iptr, mbs_len);
if (cm == NULL) {
if (mbs_len < MB_LEN_MAX &&
find_mbs_partial(*iptr, mbs_len)) {
/* incomplete sequence */
errno = EINVAL;
} else {
errno = EILSEQ;
}
return ((size_t)-1);
}
assert(cm->cm_frmbs_len > 0);
if (cm->cm_tombs_len == 0) {
if (sflag == 0 && cm->cm_warned == 0) {
cm->cm_warned = 1;
warn(_("To-map does not encode <%s>\n"),
cm->cm_name);
}
if (cflag == 0) {
errno = EILSEQ;
return ((size_t)-1);
}
/* just skip this input seq. */
*iptr += cm->cm_frmbs_len;
*ileft -= cm->cm_frmbs_len;
continue;
}
*iptr += cm->cm_frmbs_len;
*ileft -= cm->cm_frmbs_len;
(void) memcpy(*optr, cm->cm_tombs, cm->cm_tombs_len);
*optr += cm->cm_tombs_len;
*oleft -= cm->cm_tombs_len;
}
return (0);
}
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy is of the CDDL is also available via the Internet
* at http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2011 Nexenta Systems, Inc. All rights reserved.
*/
#ifndef _CHARMAP_H
#define _CHARMAP_H
/*
* CHARMAP file handling for iconv.
*/
/* Common header files. */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <libintl.h>
enum cmap_pass {
CMAP_PASS_FROM,
CMAP_PASS_TO
};
extern int com_char;
extern int esc_char;
extern int mb_cur_max;
extern int mb_cur_min;
extern int last_kw;
extern int verbose;
extern int yydebug;
extern int lineno;
extern int debug;
extern int warnings;
extern int cflag;
extern int sflag;
int yyparse(void);
int yyerror(const char *) __NORETURN;
void errf(const char *, ...);
void warn(const char *, ...);
void reset_scanner(const char *);
void scan_to_eol(void);
/* charmap.c - CHARMAP handling */
void init_charmap(void);
void add_charmap(char *, char *);
void add_charmap_posix(void);
void add_charmap_range(char *, char *, char *);
void charmap_init(char *to, char *fr);
size_t cm_iconv(const char **iptr, size_t *ileft, char **optr, size_t *oleft);
void charmap_dump(void);
#define _(x) gettext(x)
#endif /* _CHARMAP_H */
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2016 Nexenta Systems, Inc. All rights reserved.
*/
/*
* implement "iconv -l"
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <alloca.h>
#include <sys/avl.h>
#include <sys/list.h>
#include <sys/param.h>
#include <stddef.h>
#include <dirent.h>
#include <unistd.h>
#define PATH_LIBICONV "/usr/lib/iconv"
#define PATH_BTABLES "/usr/lib/iconv/geniconvtbl/binarytables"
#define PATH_ALIASES "/usr/lib/iconv/alias"
typedef struct codeset {
avl_node_t cs_node;
char *cs_name;
list_t cs_aliases;
} codeset_t;
typedef struct csalias {
list_node_t a_node;
char *a_name;
} csalias_t;
static avl_tree_t cs_avl;
static void alias_destroy(csalias_t *);
/*
* codesets
*/
static int
cs_compare(const void *n1, const void *n2)
{
const codeset_t *c1 = n1;
const codeset_t *c2 = n2;
int rv;
rv = strcmp(c1->cs_name, c2->cs_name);
return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
}
static void
cs_insert(char *key)
{
codeset_t tmp, *cs;
avl_index_t where;
(void) memset(&tmp, 0, sizeof (tmp));
tmp.cs_name = key;
cs = avl_find(&cs_avl, &tmp, &where);
if (cs != NULL)
return; /* already there */
cs = calloc(1, sizeof (*cs));
if (cs == NULL) {
perror("cs_insert:calloc");
exit(1);
}
cs->cs_name = strdup(key);
if (cs->cs_name == NULL) {
perror("cs_insert:strdup");
exit(1);
}
list_create(&cs->cs_aliases, sizeof (csalias_t),
offsetof(csalias_t, a_node));
avl_insert(&cs_avl, cs, where);
}
const char topmatter[] =
"The following are all supported code set names. All combinations\n"
"of those names are not necessarily available for the pair of the\n"
"fromcode-tocode. Some of those code set names have aliases, which\n"
"are case-insensitive and described in parentheses following the\n"
"canonical name:\n";
static void
cs_dump(void)
{
codeset_t *cs;
csalias_t *a;
(void) puts(topmatter);
for (cs = avl_first(&cs_avl); cs != NULL;
cs = AVL_NEXT(&cs_avl, cs)) {
(void) printf(" %s", cs->cs_name);
if (!list_is_empty(&cs->cs_aliases)) {
a = list_head(&cs->cs_aliases);
(void) printf(" (%s", a->a_name);
while ((a = list_next(&cs->cs_aliases, a)) != NULL) {
(void) printf(", %s", a->a_name);
}
(void) printf(")");
}
(void) printf(",\n");
}
}
static void
cs_destroy(void)
{
void *cookie = NULL;
codeset_t *cs;
csalias_t *a;
while ((cs = avl_destroy_nodes(&cs_avl, &cookie)) != NULL) {
while ((a = list_remove_head(&cs->cs_aliases)) != NULL) {
alias_destroy(a);
}
free(cs->cs_name);
free(cs);
}
avl_destroy(&cs_avl);
}
/*
* aliases
*/
static void
alias_insert(char *codeset, char *alias)
{
codeset_t tcs, *cs;
csalias_t *a;
/*
* Find the codeset. If non-existent,
* ignore aliases of this codeset.
*/
(void) memset(&tcs, 0, sizeof (tcs));
tcs.cs_name = codeset;
cs = avl_find(&cs_avl, &tcs, NULL);
if (cs == NULL)
return;
/*
* Add this alias
*/
a = calloc(1, sizeof (*a));
if (a == NULL) {
perror("alias_insert:calloc");
exit(1);
}
a->a_name = strdup(alias);
if (a->a_name == NULL) {
perror("alias_insert:strdup");
exit(1);
}
list_insert_tail(&cs->cs_aliases, a);
}
static void
alias_destroy(csalias_t *a)
{
free(a->a_name);
free(a);
}
static void
scan_dir(DIR *dh, char sep, char *suffix)
{
char namebuf[MAXNAMELEN];
struct dirent *de;
while ((de = readdir(dh)) != NULL) {
char *p2, *p1;
/*
* We'll modify, so let's copy. If the dirent name is
* longer than MAXNAMELEN, then it can't possibly be a
* valid pair of codeset names, so just skip it.
*/
if (strlcpy(namebuf, de->d_name, sizeof (namebuf)) >=
sizeof (namebuf))
continue;
/* Find suffix */
p2 = strrchr(namebuf, *suffix);
if (p2 == NULL)
continue;
if (strcmp(p2, suffix) != 0)
continue;
*p2 = '\0';
p1 = strchr(namebuf, sep);
if (p1 == NULL)
continue;
*p1++ = '\0';
/* More than one sep? */
if (strchr(p1, sep) != NULL)
continue;
/* Empty strings? */
if (*namebuf == '\0' || *p1 == '\0')
continue;
/* OK, add both to the map. */
cs_insert(namebuf);
cs_insert(p1);
}
}
static void
scan_aliases(FILE *fh)
{
char linebuf[256];
char *p1, *p2;
while (fgets(linebuf, sizeof (linebuf), fh) != NULL) {
if (linebuf[0] == '#')
continue;
p1 = strchr(linebuf, ' ');
if (p1 == NULL)
continue;
*p1++ = '\0';
p2 = strchr(p1, '\n');
if (p2 == NULL)
continue;
*p2 = '\0';
alias_insert(p1, linebuf);
}
}
int
list_codesets(void)
{
DIR *dh;
FILE *fh;
avl_create(&cs_avl, cs_compare, sizeof (codeset_t),
offsetof(codeset_t, cs_node));
dh = opendir(PATH_LIBICONV);
if (dh == NULL) {
perror(PATH_LIBICONV);
return (1);
}
scan_dir(dh, '%', ".so");
(void) closedir(dh);
dh = opendir(PATH_BTABLES);
if (dh == NULL) {
perror(PATH_BTABLES);
return (1);
}
scan_dir(dh, '%', ".bt");
(void) closedir(dh);
fh = fopen(PATH_ALIASES, "r");
if (fh == NULL) {
perror(PATH_ALIASES);
/* let's continue */
} else {
scan_aliases(fh);
(void) fclose(fh);
}
cs_dump();
cs_destroy();
return (0);
}
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2011 Nexenta Systems, Inc. All rights reserved.
*/
/*
* iconv(1) command.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include <iconv.h>
#include <libintl.h>
#include <langinfo.h>
#include <locale.h>
#include "charmap.h"
#include <assert.h>
const char *progname;
char *from_cs;
char *to_cs;
int debug;
int cflag; /* skip invalid characters */
int sflag; /* silent */
int lflag; /* list conversions */
void iconv_file(FILE *, const char *);
extern int list_codesets(void);
iconv_t ich; /* iconv(3c) lib handle */
size_t (*pconv)(const char **iptr, size_t *ileft,
char **optr, size_t *oleft);
size_t
lib_iconv(const char **iptr, size_t *ileft, char **optr, size_t *oleft)
{
return (iconv(ich, iptr, ileft, optr, oleft));
}
void
usage(void)
{
(void) fprintf(stderr, gettext(
"usage: %s [-cs] [-f from-codeset] [-t to-codeset] "
"[file ...]\n"), progname);
(void) fprintf(stderr, gettext("\t%s -l\n"), progname);
exit(1);
}
int
main(int argc, char **argv)
{
FILE *fp;
char *fslash, *tslash;
int c;
yydebug = 0;
progname = getprogname();
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
(void) textdomain(TEXT_DOMAIN);
while ((c = getopt(argc, argv, "cdlsf:t:")) != EOF) {
switch (c) {
case 'c':
cflag++;
break;
case 'd':
debug++;
break;
case 'l':
lflag++;
break;
case 's':
sflag++;
break;
case 'f':
from_cs = optarg;
break;
case 't':
to_cs = optarg;
break;
case '?':
usage();
}
}
if (lflag) {
if (from_cs != NULL || to_cs != NULL || optind != argc)
usage();
exit(list_codesets());
}
if (from_cs == NULL)
from_cs = nl_langinfo(CODESET);
if (to_cs == NULL)
to_cs = nl_langinfo(CODESET);
/*
* If either "from" or "to" contains a slash,
* then we're using charmaps.
*/
fslash = strchr(from_cs, '/');
tslash = strchr(to_cs, '/');
if (fslash != NULL || tslash != NULL) {
charmap_init(to_cs, from_cs);
pconv = cm_iconv;
if (debug)
charmap_dump();
} else {
ich = iconv_open(to_cs, from_cs);
if (ich == ((iconv_t)-1)) {
switch (errno) {
case EINVAL:
(void) fprintf(stderr,
_("Not supported %s to %s\n"),
from_cs, to_cs);
break;
default:
(void) fprintf(stderr,
_("iconv_open failed: %s\n"),
strerror(errno));
break;
}
exit(1);
}
pconv = lib_iconv;
}
if (optind == argc ||
(optind == argc - 1 && 0 == strcmp(argv[optind], "-"))) {
iconv_file(stdin, "stdin");
exit(warnings ? 1 : 0);
}
for (; optind < argc; optind++) {
fp = fopen(argv[optind], "r");
if (fp == NULL) {
perror(argv[optind]);
exit(1);
}
iconv_file(fp, argv[optind]);
(void) fclose(fp);
}
exit(warnings ? 1 : 0);
}
/*
* Conversion buffer sizes:
*
* The input buffer has room to prepend one mbs character if needed for
* handling a left-over at the end of a previous conversion buffer.
*
* Conversions may grow or shrink data, so using a larger output buffer
* to reduce the likelihood of leftover input buffer data in each pass.
*/
#define IBUFSIZ (MB_LEN_MAX + BUFSIZ)
#define OBUFSIZ (2 * BUFSIZ)
void
iconv_file(FILE *fp, const char *fname)
{
static char ibuf[IBUFSIZ];
static char obuf[OBUFSIZ];
const char *iptr;
char *optr;
off64_t offset;
size_t ileft, oleft, ocnt;
int iconv_errno;
int nr, nw, rc;
offset = 0;
ileft = 0;
iptr = ibuf + MB_LEN_MAX;
while ((nr = fread(ibuf+MB_LEN_MAX, 1, BUFSIZ, fp)) > 0) {
assert(iptr <= ibuf+MB_LEN_MAX);
assert(ileft <= MB_LEN_MAX);
ileft += nr;
offset += nr;
optr = obuf;
oleft = OBUFSIZ;
/*
* Note: the *pconv function is either iconv(3c) or our
* private equivalent when using charmaps. Both update
* ileft, oleft etc. even when conversion stops due to
* an illegal sequence or whatever, so we need to copy
* the partially converted buffer even on error.
*/
iconv_again:
rc = (*pconv)(&iptr, &ileft, &optr, &oleft);
iconv_errno = errno;
ocnt = OBUFSIZ - oleft;
if (ocnt > 0) {
nw = fwrite(obuf, 1, ocnt, stdout);
if (nw != ocnt) {
perror("fwrite");
exit(1);
}
}
optr = obuf;
oleft = OBUFSIZ;
if (rc == (size_t)-1) {
switch (iconv_errno) {
case E2BIG: /* no room in output buffer */
goto iconv_again;
case EINVAL: /* incomplete sequence on input */
if (debug) {
(void) fprintf(stderr,
_("Incomplete sequence in %s at offset %lld\n"),
fname, offset - ileft);
}
/*
* Copy the remainder to the space reserved
* at the start of the input buffer.
*/
assert(ileft > 0);
if (ileft <= MB_LEN_MAX) {
char *p = ibuf+MB_LEN_MAX-ileft;
(void) memmove(p, iptr, ileft);
iptr = p;
continue; /* read again */
}
/*
* Should not see ileft > MB_LEN_MAX,
* but if we do, handle as EILSEQ.
*/
/* FALLTHROUGH */
case EILSEQ: /* invalid sequence on input */
if (!sflag) {
(void) fprintf(stderr,
_("Illegal sequence in %s at offset %lld\n"),
fname, offset - ileft);
(void) fprintf(stderr,
_("bad seq: \\x%02x\\x%02x\\x%02x\n"),
iptr[0] & 0xff,
iptr[1] & 0xff,
iptr[2] & 0xff);
}
assert(ileft > 0);
/* skip one */
iptr++;
ileft--;
assert(oleft > 0);
if (!cflag) {
*optr++ = '?';
oleft--;
}
goto iconv_again;
default:
(void) fprintf(stderr,
_("iconv error (%s) in file $s at offset %lld\n"),
strerror(iconv_errno), fname,
offset - ileft);
break;
}
}
/* normal iconv return */
ileft = 0;
iptr = ibuf + MB_LEN_MAX;
}
/*
* End of file
* Flush any shift encodings.
*/
iptr = NULL;
ileft = 0;
optr = obuf;
oleft = OBUFSIZ;
(*pconv)(&iptr, &ileft, &optr, &oleft);
ocnt = OBUFSIZ - oleft;
if (ocnt > 0) {
nw = fwrite(obuf, 1, ocnt, stdout);
if (nw != ocnt) {
perror("fwrite");
exit(1);
}
}
}
%{
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2011 Nexenta Systems, Inc. All rights reserved.
*/
/*
* POSIX iconv charmap grammar.
*/
#include <wchar.h>
#include <stdio.h>
#include <limits.h>
#include "charmap.h"
extern int yylex(void);
%}
%union {
char *token;
int num;
char mbs[MB_LEN_MAX + 2]; /* NB: [0] is length! */
}
%token T_CODE_SET
%token T_MB_CUR_MAX
%token T_MB_CUR_MIN
%token T_COM_CHAR
%token T_ESC_CHAR
%token T_LT
%token T_GT
%token T_NL
%token T_SEMI
%token T_COMMA
%token T_ELLIPSIS
%token T_RPAREN
%token T_LPAREN
%token T_QUOTE
%token T_NULL
%token T_END
%token T_CHARMAP
%token T_WIDTH
%token T_WIDTH_DEFAULT
%token <mbs> T_CHAR
%token <token> T_NAME
%token <num> T_NUMBER
%token <token> T_SYMBOL
%%
goal : setting_list charmap
| charmap
;
string : T_QUOTE charlist T_QUOTE
| T_QUOTE T_QUOTE
;
charlist : charlist T_CHAR
| T_CHAR
;
setting_list : setting_list setting
| setting
;
setting : T_COM_CHAR T_CHAR T_NL
{
com_char = $2[1];
}
| T_ESC_CHAR T_CHAR T_NL
{
esc_char = $2[1];
}
| T_MB_CUR_MAX T_NUMBER T_NL
{
mb_cur_max = $2;
}
| T_MB_CUR_MIN T_NUMBER T_NL
{
mb_cur_min = $2;
}
| T_CODE_SET T_NAME T_NL
{
/* ignore */
}
| T_CODE_SET string T_NL
{
/* ignore */
}
;
charmap : T_CHARMAP T_NL charmap_list T_END T_CHARMAP T_NL
charmap_list : charmap_list charmap_entry
| charmap_entry
;
charmap_entry : T_SYMBOL T_CHAR
{
add_charmap($1, $2);
scan_to_eol();
}
| T_SYMBOL T_ELLIPSIS T_SYMBOL T_CHAR
{
add_charmap_range($1, $3, $4);
scan_to_eol();
}
| T_NL
;
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2011 Nexenta Systems, Inc. All rights reserved.
*/
/*
* This file contains the "scanner", which tokenizes charmap files
* for iconv for processing by the higher level grammar processor.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <widec.h>
#include <sys/types.h>
#include <assert.h>
#include "charmap.h"
#include "parser.tab.h"
int com_char = '#';
int esc_char = '\\';
int mb_cur_min = 1;
int mb_cur_max = MB_LEN_MAX;
int lineno = 1;
int warnings = 0;
static int nextline;
static FILE *input = stdin;
static const char *filename = "<stdin>";
static int instring = 0;
static int escaped = 0;
/*
* Token space ... grows on demand.
*/
static char *token = NULL;
static int tokidx;
static int toksz = 0;
static int hadtok = 0;
/*
* The last keyword seen. This is useful to trigger the special lexer rules
* for "copy" and also collating symbols and elements.
*/
int last_kw = 0;
static int category = T_END;
static struct token {
int id;
const char *name;
} keywords[] = {
{ T_COM_CHAR, "comment_char" },
{ T_ESC_CHAR, "escape_char" },
{ T_END, "END" },
/*
* These are keywords used in the charmap file. Note that
* Solaris orginally used angle brackets to wrap some of them,
* but we removed that to simplify our parser. The first of these
* items are "global items."
*/
{ T_CHARMAP, "CHARMAP" },
{ T_WIDTH, "WIDTH" },
{ T_WIDTH_DEFAULT, "WIDTH_DEFAULT" },
{ -1, NULL },
};
/*
* These special words are only used in a charmap file, enclosed in <>.
*/
static struct token symwords[] = {
{ T_COM_CHAR, "comment_char" },
{ T_ESC_CHAR, "escape_char" },
{ T_CODE_SET, "code_set_name" },
{ T_MB_CUR_MAX, "mb_cur_max" },
{ T_MB_CUR_MIN, "mb_cur_min" },
{ -1, NULL },
};
static int categories[] = {
T_CHARMAP,
0
};
void
reset_scanner(const char *fname)
{
if (fname == NULL) {
filename = "<stdin>";
input = stdin;
} else {
if (input != stdin)
(void) fclose(input);
if ((input = fopen(fname, "r")) == NULL) {
perror(fname);
exit(1);
}
filename = fname;
}
com_char = '#';
esc_char = '\\';
instring = 0;
escaped = 0;
lineno = 1;
nextline = 1;
tokidx = 0;
last_kw = 0;
category = T_END;
}
#define hex(x) \
(isdigit(x) ? (x - '0') : ((islower(x) ? (x - 'a') : (x - 'A')) + 10))
#define isodigit(x) ((x >= '0') && (x <= '7'))
static int
scanc(void)
{
int c;
c = getc(input);
lineno = nextline;
if (c == '\n') {
nextline++;
}
return (c);
}
static void
unscanc(int c)
{
if (c == '\n') {
nextline--;
}
if (ungetc(c, input) < 0) {
(void) yyerror(_("ungetc failed"));
}
}
static int
scan_hex_byte(void)
{
int c1, c2;
int v;
c1 = scanc();
if (!isxdigit(c1)) {
(void) yyerror(_("malformed hex digit"));
return (0);
}
c2 = scanc();
if (!isxdigit(c2)) {
(void) yyerror(_("malformed hex digit"));
return (0);
}
v = ((hex(c1) << 4) | hex(c2));
return (v);
}
static int
scan_dec_byte(void)
{
int c1, c2, c3;
int b;
c1 = scanc();
if (!isdigit(c1)) {
(void) yyerror(_("malformed decimal digit"));
return (0);
}
b = c1 - '0';
c2 = scanc();
if (!isdigit(c2)) {
(void) yyerror(_("malformed decimal digit"));
return (0);
}
b *= 10;
b += (c2 - '0');
c3 = scanc();
if (!isdigit(c3)) {
unscanc(c3);
} else {
b *= 10;
b += (c3 - '0');
}
return (b);
}
static int
scan_oct_byte(void)
{
int c1, c2, c3;
int b;
b = 0;
c1 = scanc();
if (!isodigit(c1)) {
(void) yyerror(_("malformed octal digit"));
return (0);
}
b = c1 - '0';
c2 = scanc();
if (!isodigit(c2)) {
(void) yyerror(_("malformed octal digit"));
return (0);
}
b *= 8;
b += (c2 - '0');
c3 = scanc();
if (!isodigit(c3)) {
unscanc(c3);
} else {
b *= 8;
b += (c3 - '0');
}
return (b);
}
void
add_tok(int c)
{
if ((tokidx + 1) >= toksz) {
toksz += 64;
if ((token = realloc(token, toksz)) == NULL) {
(void) yyerror(_("out of memory"));
tokidx = 0;
toksz = 0;
return;
}
}
token[tokidx++] = (char)c;
token[tokidx] = 0;
}
static int
get_byte(void)
{
int c;
if ((c = scanc()) != esc_char) {
unscanc(c);
return (EOF);
}
c = scanc();
switch (c) {
case 'd':
case 'D':
return (scan_dec_byte());
case 'x':
case 'X':
return (scan_hex_byte());
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
/* put the character back so we can get it */
unscanc(c);
return (scan_oct_byte());
default:
unscanc(c);
unscanc(esc_char);
return (EOF);
}
}
int
get_escaped(int c)
{
switch (c) {
case 'n':
return ('\n');
case 'r':
return ('\r');
case 't':
return ('\t');
case 'f':
return ('\f');
case 'v':
return ('\v');
case 'b':
return ('\b');
case 'a':
return ('\a');
default:
return (c);
}
}
int
get_wide(void)
{
/* NB: yylval.mbs[0] is the length */
char *mbs = &yylval.mbs[1];
int mbi = 0;
int c;
mbs[mbi] = 0;
if (mb_cur_max > MB_LEN_MAX) {
(void) yyerror(_("max multibyte character size too big"));
return (T_NULL);
}
for (;;) {
if ((c = get_byte()) == EOF)
break;
if (mbi == mb_cur_max) {
unscanc(c);
(void) yyerror(_("length > mb_cur_max"));
return (T_NULL);
}
mbs[mbi++] = c;
mbs[mbi] = 0;
}
/* result in yylval.mbs */
mbs[-1] = mbi;
return (T_CHAR);
}
int
get_symbol(void)
{
int c;
while ((c = scanc()) != EOF) {
if (escaped) {
escaped = 0;
if (c == '\n')
continue;
add_tok(get_escaped(c));
continue;
}
if (c == esc_char) {
escaped = 1;
continue;
}
if (c == '\n') { /* well that's strange! */
(void) yyerror(_("unterminated symbolic name"));
continue;
}
if (c == '>') { /* end of symbol */
/*
* This restarts the token from the beginning
* the next time we scan a character. (This
* token is complete.)
*/
if (token == NULL) {
(void) yyerror(_("missing symbolic name"));
return (T_NULL);
}
tokidx = 0;
/*
* A few symbols are handled as keywords outside
* of the normal categories.
*/
if (category == T_END) {
int i;
for (i = 0; symwords[i].name != 0; i++) {
if (strcmp(token, symwords[i].name) ==
0) {
last_kw = symwords[i].id;
return (last_kw);
}
}
}
/* its an undefined symbol */
yylval.token = strdup(token);
if (yylval.token == NULL) {
perror("malloc");
exit(1);
}
token = NULL;
toksz = 0;
tokidx = 0;
return (T_SYMBOL);
}
add_tok(c);
}
(void) yyerror(_("unterminated symbolic name"));
return (EOF);
}
static int
consume_token(void)
{
int len = tokidx;
int i;
tokidx = 0;
if (token == NULL)
return (T_NULL);
/*
* this one is special, because we don't want it to alter the
* last_kw field.
*/
if (strcmp(token, "...") == 0) {
return (T_ELLIPSIS);
}
/* search for reserved words first */
for (i = 0; keywords[i].name; i++) {
int j;
if (strcmp(keywords[i].name, token) != 0) {
continue;
}
last_kw = keywords[i].id;
/* clear the top level category if we're done with it */
if (last_kw == T_END) {
category = T_END;
}
/* set the top level category if we're changing */
for (j = 0; categories[j]; j++) {
if (categories[j] != last_kw)
continue;
category = last_kw;
}
return (keywords[i].id);
}
/* maybe its a numeric constant? */
if (isdigit(*token) || (*token == '-' && isdigit(token[1]))) {
char *eptr;
yylval.num = strtol(token, &eptr, 10);
if (*eptr != 0)
(void) yyerror(_("malformed number"));
return (T_NUMBER);
}
/*
* A single lone character is treated as a character literal.
* To avoid duplication of effort, we stick in the charmap.
*/
if (len == 1) {
yylval.mbs[0] = 1; /* length */
yylval.mbs[1] = token[0];
yylval.mbs[2] = '\0';
return (T_CHAR);
}
/* anything else is treated as a symbolic name */
yylval.token = strdup(token);
token = NULL;
toksz = 0;
tokidx = 0;
return (T_NAME);
}
void
scan_to_eol(void)
{
int c;
while ((c = scanc()) != '\n') {
if (c == EOF) {
/* end of file without newline! */
errf(_("missing newline"));
return;
}
}
assert(c == '\n');
}
int
yylex(void)
{
int c;
while ((c = scanc()) != EOF) {
/* special handling for quoted string */
if (instring) {
if (escaped) {
escaped = 0;
/* if newline, just eat and forget it */
if (c == '\n')
continue;
if (strchr("xXd01234567", c)) {
unscanc(c);
unscanc(esc_char);
return (get_wide());
}
yylval.mbs[0] = 1; /* length */
yylval.mbs[1] = get_escaped(c);
yylval.mbs[2] = '\0';
return (T_CHAR);
}
if (c == esc_char) {
escaped = 1;
continue;
}
switch (c) {
case '<':
return (get_symbol());
case '>':
/* oops! should generate syntax error */
return (T_GT);
case '"':
instring = 0;
return (T_QUOTE);
default:
yylval.mbs[0] = 1; /* length */
yylval.mbs[1] = c;
yylval.mbs[2] = '\0';
return (T_CHAR);
}
}
/* escaped characters first */
if (escaped) {
escaped = 0;
if (c == '\n') {
/* eat the newline */
continue;
}
hadtok = 1;
if (tokidx) {
/* an escape mid-token is nonsense */
return (T_NULL);
}
/* numeric escapes are treated as wide characters */
if (strchr("xXd01234567", c)) {
unscanc(c);
unscanc(esc_char);
return (get_wide());
}
add_tok(get_escaped(c));
continue;
}
/* if it is the escape charter itself note it */
if (c == esc_char) {
escaped = 1;
continue;
}
/* remove from the comment char to end of line */
if (c == com_char) {
while (c != '\n') {
if ((c = scanc()) == EOF) {
/* end of file without newline! */
return (EOF);
}
}
assert(c == '\n');
if (!hadtok) {
/*
* If there were no tokens on this line,
* then just pretend it didn't exist at all.
*/
continue;
}
hadtok = 0;
return (T_NL);
}
if (strchr(" \t\n;()<>,\"", c) && (tokidx != 0)) {
/*
* These are all token delimiters. If there
* is a token already in progress, we need to
* process it.
*/
unscanc(c);
return (consume_token());
}
switch (c) {
case '\n':
if (!hadtok) {
/*
* If the line was completely devoid of tokens,
* then just ignore it.
*/
continue;
}
/* we're starting a new line, reset the token state */
hadtok = 0;
return (T_NL);
case ',':
hadtok = 1;
return (T_COMMA);
case ';':
hadtok = 1;
return (T_SEMI);
case '(':
hadtok = 1;
return (T_LPAREN);
case ')':
hadtok = 1;
return (T_RPAREN);
case '>':
hadtok = 1;
return (T_GT);
case '<':
/* symbol start! */
hadtok = 1;
return (get_symbol());
case ' ':
case '\t':
/* whitespace, just ignore it */
continue;
case '"':
hadtok = 1;
instring = 1;
return (T_QUOTE);
default:
hadtok = 1;
add_tok(c);
continue;
}
}
return (EOF);
}
int
yyerror(const char *msg)
{
(void) fprintf(stderr, _("%s: %d: error: %s\n"),
filename, lineno, msg);
exit(1);
}
void
errf(const char *fmt, ...)
{
char *msg;
va_list va;
va_start(va, fmt);
(void) vasprintf(&msg, fmt, va);
va_end(va);
(void) fprintf(stderr, _("%s: %d: error: %s\n"),
filename, lineno, msg);
free(msg);
exit(1);
}
void
warn(const char *fmt, ...)
{
char *msg;
va_list va;
va_start(va, fmt);
(void) vasprintf(&msg, fmt, va);
va_end(va);
(void) fprintf(stderr, _("%s: %d: warning: %s\n"),
filename, lineno, msg);
free(msg);
warnings++;
}
|