|
root / base / usr / src / tools / sgs
sgs Plain Text 2153 lines 54.0 KB
   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
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
#
# 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 2019, Richard Lowe.

# Hammerhead: Enforce sequential builds - sgsmsg must complete before
# liblddbg can compile (generates msg.h header)
.NOTPARALLEL:

SUBDIRS =		\
	include		\
	ld		\
	libconv		\
	libelf		\
	libld		\
	liblddbg	\
	sgsmsg

all :		TARGET= install
install :	TARGET= install
clean :	TARGET= clean
clobber :	TARGET= clobber

ld: sgsmsg include libconv libelf libld
libconv: sgsmsg include
libelf: sgsmsg include
libld: sgsmsg libelf libconv liblddbg include
liblddbg: sgsmsg libconv include

all install clean clobber: $(SUBDIRS)

$(SUBDIRS): FRC
	@cd $@; pwd; $(MAKE) $(TARGET)

FRC:
#
# 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 2019, Richard Lowe.

# Hammerhead: Override ONBLD_TOOLS to use just-built tools from proto area.
# This must come AFTER Makefile.master is included (via other includes) because
# Makefile.master sets ONBLD_TOOLS = $(BUILD_TOOLS)/onbld = /opt/onbld.
# This allows tools like sgsmsg to be used to build libconv.
ONBLD_TOOLS =	$(TOOLS_PROTO)/opt/onbld

CPPFLAGS =	-I. -I$(SRCDIR) -I$(SRCDIR)/common \
	-I$(SGSHOME)/include -I$(SGSHOME)/include/$(MACH) \
	-I../include $(CPPFLAGS.native) -I$(ELFCAP) -DNATIVE_BUILD
#
# 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 2019, Richard Lowe.

include ../../../Makefile.master

#
# To build a proper 'tools' version of the link-editor is not possible, as
# we'll always depend on the ELF-related headers from the workspace, not the
# system.
#
# We pull in the minimum amount of headers using an explicit list of "ELF-y"
# headers to populate this directory, which we then search while building.
#
# This may not be enough on all occasions, judgement must be used to decide
# between providing extra headers, and wrapping problematic code in conditions
# on NATIVE_BUILD for a period of time.
#

ROOTHDRS=	dlfcn.h \
		gelf.h \
		libelf.h \
		proc_service.h \
		rtld_db.h \
		link.h

SYSHDRS=	sys/elf.h \
		sys/elf_386.h \
		sys/elf_SPARC.h \
		sys/elf_notes.h \
		sys/elf_amd64.h \
		sys/elftypes.h \
		sys/auxv.h \
		sys/auxv_SPARC.h \
		sys/auxv_386.h \
		sys/avl.h \
		sys/link.h \
		sys/machelf.h \
		sys/note.h \
		sys/systeminfo.h

sys:
	$(MKDIR) -p sys

# Hammerhead: Use static pattern rules for GNU Make compatibility
# (match-anything pattern rules have special restrictions in GNU Make)
$(ROOTHDRS): %: $(SRC)/head/%
	$(INS.file)

# Hammerhead: Use order-only prerequisite for sys directory so $< is the source file
$(SYSHDRS): sys/%: $(SRC)/uts/common/sys/% | sys
	$(INS.file)

all install: $(SYSHDRS) $(ROOTHDRS)

clean:

clobber: clean
	$(RM) $(SYSHDRS) $(ROOTHDRS)



#
# 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 (c) 1996 by Sun Microsystems, Inc.
# All rights reserved.

include		../../Makefile.tools
include		$(SRC)/cmd/sgs/ld/Makefile.com
include		../Makefile.com

LDLIBS =	-lumem -L../libconv -L$(ROOTONBLDLIBMACH64) -lld -lelf -lconv
NATIVE_LIBS +=	libc.so libumem.so
MAPFILES =	$(SRCDIR)/common/mapfile-intf
RPATH =		'-R$$ORIGIN/../../lib/$(MACH)/64'

include		$(SRC)/Makefile.master.64
include		$(SRC)/Makefile.native

install: $(ROOTONBLDMACH64PROG)

.KEEP_STATE:

include		$(SGSHOME)/ld/Makefile.targ
#
# 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 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#

include		../../Makefile.tools
include		$(SRC)/cmd/sgs/libconv/Makefile.com
include		../Makefile.com

CPPFLAGS +=	-I$(SRC)/lib/libc/inc -I$(ELFCAP) \
	-I$(SRC)/common/sgsrtcid \
	-I$(SRC)/lib/libdemangle/common

include		$(SRC)/Makefile.native
include		$(SRC)/Makefile.master.64

.PARALLEL:	$(PICS)
.KEEP_STATE:

install all:	$(LIBRARY)

include		$(SGSHOME)/libconv/Makefile.targ
include		$(SRC)/Makefile.master.64
#
# 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) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
#

include		../../Makefile.tools
include		$(SRC)/cmd/sgs/libelf/Makefile.com
include		../Makefile.com

CONVLIBDIR =	-L../libconv
SGSMSGTARG +=	$(SGSMSG32)
NATIVE_LIBS +=	libc.so

CPPFLAGS +=	-I$(SRC)/cmd/sgs/libelf/common
LDLIBS +=	$(CONVLIBDIR) -lconv

include		$(SRC)/Makefile.native
include		$(SRC)/Makefile.master.64

# As a bootstrapping issue, we can't use the real mapfile because we build
# early in tools and thus don't have support for assertions.
MAPFILES=

.KEEP_STATE:

install:	all $(ROOTONBLDLIBMACH64)/$(DYNLIB) \
	$(ROOTONBLDLIBMACH64)/$(LIBLINKS)

$(ROOTONBLDLIBMACH64)/$(DYNLIB): $(PICS) $(ROOTONBLDLIBMACH64)
		$(BUILD.SO)
		$(POST_PROCESS_SO)

$(ROOTONBLDLIBMACH64)/$(LIBLINKS): $(ROOTONBLDLIBMACH64)/$(DYNLIB)
		@$(RM) $(ROOTONBLDLIBMACH64)/$(LIBLINKS)
		$(SYMLINK) $(DYNLIB) $(ROOTONBLDLIBMACH64)/$(LIBLINKS)

include		$(SGSHOME)/libelf/Makefile.targ
include		$(SRC)/Makefile.master.64
#
# 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 2008 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#

include		../../Makefile.tools
include		$(SRC)/cmd/sgs/libld/Makefile.com
include		../Makefile.com

.KEEP_STATE:

#
# By using libld's Makefile.com LDLIBS ends up pointing to the proto
# area and many of the directories of cmd/sgs. As such, we need to reset
# that and just make sure to find everything relative to the tools proto
# area.
#
LDLIBS =	-L../libconv -lconv -L$(ROOTONBLDLIBMACH64) -llddbg -lelf -lc
NATIVE_LIBS +=	libc.so

CPPFLAGS += -DUSE_LIBLD_MALLOC -I$(SRC)/lib/libc/inc \
	    -I$(SRC)/uts/common/krtld -I$(SRC)/uts/sparc \
	    -I$(SRC)/uts/common

DYNFLAGS +=	$(VERSREF) '-Wl,-R$$ORIGIN'

# Necessary because we're a non-debug build in a debug environment, because
# of being in src/tools
CERRWARN += -Wno-unused-but-set-variable

include		$(SRC)/Makefile.native
include		$(SRC)/Makefile.master.64

all:		$(DYNLIB) $(LIBLINKS)

install:	all $(ROOTONBLDLIBMACH64)/$(DYNLIB) $(ROOTONBLDLIBMACH64)/$(LIBLINKS)

$(ROOTONBLDLIBMACH64)/$(DYNLIB): $(PICS) $(ROOTONBLDLIBMACH64)
		$(BUILD.SO)
		$(POST_PROCESS_SO)

$(ROOTONBLDLIBMACH64)/$(LIBLINKS): $(ROOTONBLDLIBMACH64)/$(DYNLIB)
		@$(RM) $(ROOTONBLDLIBMACH64)/$(LIBLINKS)
		$(SYMLINK) $(DYNLIB) $(ROOTONBLDLIBMACH64)/$(LIBLINKS)

include		$(SGSHOME)/libld/Makefile.targ
#
# 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 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

include		../../Makefile.tools
include		$(SRC)/cmd/sgs/liblddbg/Makefile.com
include		../Makefile.com

.KEEP_STATE:

CONVLIBDIR=	-L../libconv
CPPFLAGS +=	-I$(SRC)/lib/libc/inc
SGSMSGTARG +=	$(SGSMSG32)
NATIVE_LIBS +=	libc.so
# Hammerhead: Add -ldl for dlopen/dlsym used by libconv (demangle.c)
LDLIBS +=	$(CONVLIBDIR) -lconv -ldl

# A bootstrapping problem means we can't use the regular mapfile just yet
MAPFILES =

include		$(SRC)/Makefile.native
include		$(SRC)/Makefile.master.64

all:		$(DYNLIB) $(LIBLINKS)

install:	all $(ROOTONBLDLIBMACH64)/$(DYNLIB) $(ROOTONBLDLIBMACH64)/$(LIBLINKS)

$(ROOTONBLDLIBMACH64)/$(DYNLIB): $(PICS) $(ROOTONBLDLIBMACH64)
		$(BUILD.SO)
		$(POST_PROCESS_SO)

$(ROOTONBLDLIBMACH64)/$(LIBLINKS): $(ROOTONBLDLIBMACH64)/$(DYNLIB)
		@$(RM) $(ROOTONBLDLIBMACH64)/$(LIBLINKS)
		$(SYMLINK) $(DYNLIB) $(ROOTONBLDLIBMACH64)/$(LIBLINKS)

include		$(SGSHOME)/liblddbg/Makefile.targ
#
# 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) 2018, Joyent, Inc.
# Copyright 2019 OmniOS Community Edition (OmniOSce) Association.

#
# Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# Makefile to support tools used for linker development:
#
#  o	sgsmsg creates message headers/arrays/catalogs (a native tool).
#
# Note, these tools are not part of the product.
#

include		../../Makefile.tools
include		$(SRC)/cmd/sgs/Makefile.com
include		$(SRC)/cmd/Makefile.cmd
include		../Makefile.com

PROG=	sgsmsg

SRCDIR=	$(SRC)/tools/sgs/sgsmsg

MAN1ONBLDFILES =	sgsmsg.1onbld
$(ROOTONBLDMAN1ONBLDFILES) : FILEMODE=	644

OBJS=	assfail.o	\
	avl.o		\
	findprime.o	\
	sgsmsg.o	\
	string_table.o

FILEMODE=	0755

NATIVE_LIBS +=	libc.so
CSTD = $(CSTD_GNU99)

# not linted
SMATCH=		off

include		$(SRC)/Makefile.native

.KEEP_STATE:

all: $(PROG)

install: $(ROOTONBLDMACHPROG) $(ROOTONBLDMAN1ONBLDFILES)

$(PROG): $(OBJS)
	$(LINK.c) $(OBJS) -o $@ $(LDLIBS)

%.o:	$(SGSCOMMON)/%.c
	$(COMPILE.c) -c $<

avl.o:	$(SRC)/common/avl/avl.c
	$(COMPILE.c) -c $(SRC)/common/avl/avl.c -o $@

clean:
	$(RM) $(OBJS)

include $(SRC)/cmd/Makefile.targ
.\" Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
.\" Use is subject to license terms.
.\"
.\" 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
.\"
.if n .tr \--
.TH sgsmsg 1ONBLD "Jun 2, 1999"
.SH NAME
sgsmsg \- generate message strings for SGS subsystem.
.SH SYNOPSIS
.B sgsmsg
[
.B \-cl
] [
.BI \-d\0 "data"
] [
.BI \-h\0 "defs"
] [
.BI \-i\0 "ident"
]
.if n .ti +5n
[
.BI \-m\0 "messages"
] [
.BI \-n\0 "name"
]
.I file \.\.\.
.SH AVAILABILITY
SUNWonld
.SH DESCRIPTION
\f3sgsmsg\f1 generates several message files from an input string definition
\f2file\f1.  \f3sgsmsg\f1 provides a flexible, centralized, mechanism
of collecting character strings within a code group such as an executable or
shared object.  All character strings are captured into a single data array
within the \f2data\f1 file.
The data array is similar to that produced by
.BR xstr (1)),
and helps reduce the relocation overhead incurred by string pointers.
.LP
Indexes into the data array are generated as
definitions within the \f2defs\f1 file. The code group can reference each
character string via predefined macros.
.LP
The character strings may also be translated into an internationalized
format and captured in the
\f2messages\f1 file.  By default these message strings are suitable for
.BR gettext (3I)
manipulation.  The \f3\-c\f1 option provides for these message strings to be
translated into a form suitable for
.BR catgets (3C)
manipulation.
.SH OPERANDS
One of more input \f2file\f1s contains a definition for each character
string used by a particular code group.  The interpretation of a
definition is determined by the first character of each line within
the input \f2file\f1:
.LP
.PD 0
.RS +4
.IP \(bu 3
Entries that begin with a \f3#\fI, \f3$\f1 or a newline are treated as
comments and are copied (as is) to the \f2messages\f1 file.
.IP \(bu 3
Entries that begin with a \f3@\f1 are translated and will be written to
one or more of the output files. Two translations are possible dependent upon
whether one or more tokens follow the \f3@\f1 character.
.RE
.PD
.sp
.LP
An \f4@\f1 character followed by a single token is interpreted as one of
two reserved message output
\f2indicators\f1, or a message \f2identifier\f1.  The reserved output
indicator \f4_START_\f1 enables output to the \f2messages\f1 file (note that
the occurrence of any \f4@\f1 token will also enable message output).
The reserved output indicator \f4_END_\f1 disables output to the
\f2messages\f1 file.  These two indicators provides a means of isolating
only those character strings that require translation into the \f2messages\f1
file.
.LP
Besides the reserved output indicators, an \f4@\f1 character followed by a
single token is taken to be a
message \f2identifier\f1. This identifier will be translated into a
\f2domain\f1 name for
.BR gettext (3I)
output, or a \f2setid\f1 for
.BR catgets (3C)
output.  This translated value is determine by substituting the message
\f2identifier\f1 token
for the associated definition from in the \f2ident\f1 file. Note that
a message \f2identifier\f1 is required for
.BR catgets (3C)
use but is optional for
.BR gettext (3I).
.LP
An \f4@\f1 character followed by multiple tokens is taken to be a
string \f2definition\f1 followed by a quoted character string. Character
strings can be continued over multiple lines by ending the preceding
line with a backslash - all initial whitespace on the continuation line will
is ignored. Character strings can contain the escape sequences
.B \en
for newline,
.B \et
for tab,
.B \ev
for vertical tab,
.B \eb
for backspace,
.B \er
for carriage return,
.B \ef
for formfeed,
.B \e\e
for backslash, and
\e"
for double quote.
.LP
The character string is copied to the \f2data\f1 array
and an index into this array is generated as the \f2definition\f1 within
the string
\f2defs\f1 file. The character string is also translated to the appropriate
message format and written to the \f2messages\f1 file.
.SH OPTIONS
.TP 12
.B \-c
By default, strings generated in the \f2messages\f1 file are suitable for
.BR msgfmt (1)
processing, which provides for message extraction via
.BR gettext (3I).
This option causes the formatting of the message strings to be suitable for
.BR gencat (1)
processing, which provides for message extraction via
.BR catgets (3C).
.TP
.BI \-d\0 data
Specify a \f2data\f1 file is to be created.
This file contains a single data array, by default named (\f2__name\f1[]),
containing all the strings
defined in the string definition \f2file\f1.
.TP
.BI \-h\0 defs
Specify a \f2defs\f1 file is to be created.
This file contains definitions for each character string contained in
the data array within the \f2data\f1 file.  These definitions represent
offsets in the data array for each string.  Reference to individual strings
should use one of the two defined macros \f4MSG_INTL\f1 (which specifies
a user defined message extraction function), or \f4MSG_ORIG\f1
(which specifies a direct access to the \f2__name\f1[] array).
.TP
.BI \-i\0 ident
Specify an \f2ident\f1 file from which to interpret a message identifier
token.
.TP
.B \-l
Indicate that the \f2data\f1 array be defined local (\f2static\f1).  This
is useful for establishing individual string arrays on a per-object basis.
.TP
.BI \-m\0 messages
Specify a \f2messages\f1 file is to be created. This
contain message strings suitable for delivery to a localization group.
.TP
.BI \-n\0 name
Specify an alternative interface \f2name\f1. This name is used to label
the message data array (\f2__name\f1[]) and the user defined message
extraction function (const char * \f2_name\f1(int)) which will interface
with this array.
.SH EXAMPLES
The following examples provide a simplified guide to using the \f3sgsmsg\fP
command, including sample input files and generated output files.
.LP
The following \f2ident\f1 file provides message \f2identifiers\f1 for
the link-editor utilities
.BR ld (1),
.BR libld.so.2 ,
and
.BR liblddbg.so.3 .
These identifiers are referenced from the input string definition files
of the respective code groups:
.if n .ta 1.8i 2.2i
.if t .ta 1.6i 2.2i
.RS
.nf
.ft 3

% cat sgs.ident
.ft 1
.if t .sp 0.35
.if n .sp
# mesgid	setid	domain
.if t .sp 0.35
.if n .sp
MSG_ID_LD	1	SUNW_OST_SGS
MSG_ID_LIBLD	2	SUNW_OST_SGS
MSG_ID_LIBLDDBG	3	SUNW_OST_SGS

.fi
.RE
.if t .bp
.LP
The following string definition \f2file\f1 defines a small number of
strings used by
.BR libld.so.2 :
.ta 2.2i
.RS
.nf
.ft 3

% cat libld.msg
.ft 1
.if t .sp 0.35
.if n .sp
@ _START_
.if t .sp 0.35
.if n .sp
# Message file for cmd/sgs/libld.
.if t .sp 0.35
.if n .sp
@ MSG_ID_LIBLD

# System call messages
.if t .sp 0.35
.if n .sp
@ MSG_SYS_OPEN	"file %s: cannot open file: %s"
@ MSG_SYS_MMAP	"file %s: cannot mmap file: %s"

# Symbol processing errors
.if t .sp 0.35
.if n .sp
@ MSG_SYM_DIFFTYPE	"symbol `%s' has differing types:"
@ MSG_SYM_DIFFATTR	"symbol `%s' has differing %s:\\n\\
		\ \\t(file %s value=0x%x; file %s value=0x%x);"
.if t .sp 0.35
.if n .sp
@ _END_

# The following strings represent reserved names.  Reference to
# these strings is via the MSG_ORIG() macro, and thus no
# translations are required.
.if t .sp 0.35
.if n .sp
@ MSG_STR_EMPTY	""
@ MSG_PTH_DEVZERO	"/dev/zero"
@ MSG_SUNW_OST_SGS	"SUNW_OST_SGS"

.fi
.RE
.LP
Using the above input files, the following string and message data files can be
generated:
.if t .ta 0.5i 2.2i
.if n .ta 0.5i 0.8i 3.0i
.RS
.nf
.ft 3

% sgsmsg\ \ \-i sgs.ident\ \ \-m\ messages\ \ \-d\ msg.c\ \ \-h\ msg.h \\
\ \ \ \ \-n\ libld_msg\ \ libld.msg
% cat msg.c
.ft 1
.if t .sp 0.35
.if n .sp
const char __libld_msg[] = { 0x00,
	0x66,  0x69,  0x6c,  0x65,  0x20,  0x25,  0x73,  0x3a,  \.\.\.\.
	0x61,  0x6e,  0x6e,  0x6f,  0x74,  0x20,  0x6f,  0x70,  \.\.\.\.
	\.\.\.\.
	0x00
};

.if t .bp
.ft 3
% cat msg.h
.ft 1
.if t .sp 0.35
.if n .sp
extern	const char	__libld_msg[];
.if t .sp 0.35
.if n .sp
#define	MSG_ORIG(x)	&__libld_msg[x]
.if t .sp 0.35
.if n .sp
extern	const char *	_libld_msg(int);
.if t .sp 0.35
.if n .sp
#define	MSG_INTL(x)	_libld_msg(x)
.if t .sp 0.35
.if n .sp
#define	MSG_SYS_OPEN	1
#define	MSG_SYS_MMAP	31
#define	MSG_SYM_DIFFTYPE	61
#define	MSG_SYM_DIFFATTR	94
#define	MSG_STR_EMPTY	167
#define	MSG_PTH_DEVZERO	168
#define	MSG_SUNW_OST_SGS	178

.ft 3
% cat messages
.ft 1
.if t .sp 0.35
.if n .sp
# Message file for cmd/sgs/libld.
.if t .sp 0.35
.if n .sp
domain  "SUNW_OST_SGS"
.if t .sp 0.35
.if n .sp
# System call messages
.if t .sp 0.35
.if n .sp
msgid	"file %s: cannot open file: %s"
msgstr	""
msgid	"file %s: cannot mmap file: %s"
msgstr	""
.if t .sp 0.35
.if n .sp
# Symbol processing errors
.if t .sp 0.35
.if n .sp
msgid	"symbol `%s' has differing types:"
msgstr	""
msgid	"symbol `%s' has differing %s:\\n\\t(file %s value=0x%x; file %s value=0x%x);"
msgstr  ""

.fi
.RE
.LP
References to the string data from the code group
should use one of the two defined macros
depending upon whether an original or localized string is required.
For example, the simple
.BR open (2)
of a file would use the original string, however its associated
error message should be localized:
.if n .ta 0.75i
.if t .ta 0.5i 2.2i
.RS
.nf
.ft 3

const char * file = MSG_ORIG(MSG_PTH_DEVZERO);
.if t .sp 0.35
.if n .sp
if ((fd = open(file, O_RDWR, 0)) == -1) {
	int err = errno;
	(void) fprintf(stderr, MSG_INTL(MSG_SYS_OPEN), file,
	    strerror(err));
	return (1);
}

.fi
.RE
.if t .bp
.LP
The \f3MSG_INTL\f1 definition provides for a user defined message
extraction function
that allows the greatest flexibility in providing an objects localization.
Normally this interface is quite simple.  For a code group that resides
in a shared object the following interface can be provided by the user:
.RS
.nf
.ft 3

extern char * _dgettext(const char *, const char *);
.if t .sp 0.35
.if n .sp
const char *
_libld_msg(int mid)
{
	return (_dgettext(MSG_ORIG(MSG_SUNW_OST_SGS),
	    MSG_ORIG(mid)));
}

.fi
.RE
.LP
For a code group that resides in an executable the following interface,
and initialization can be provided by the user:
.RS
.nf
.ft 3

#include <locale.h>

int
main(int argc, char ** argv)
{
	\&\.\.\.\.\.\.
	(void) setlocale(LC_MESSAGES, MSG_ORIG(MSG_STR_EMPTY));
	(void) textdomain(MSG_ORIG(MSG_SUNW_OST_SGS));
	\&\.\.\.\.\.\.
}

const char *
_ld_msg(int mid)
{
	return (gettext(MSG_ORIG(mid)));
}
.RE
.sp
.SH "EXIT STATUS"
A non-zero error return indicates a processing error.
.PD
.SH "SEE ALSO"
.BR gencat (1),
.BR ld (1),
.BR msgfmt (1),
.BR catgets (3C),
.BR gettext (3C).
.if n .tr \-\-
/*
 * 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) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
 *
 * sgsmsg generates several message files from an input template file.  Messages
 * are constructed for use with gettext(3C) - the default - or catgets(3C).  The
 * files generate are:
 *
 * msg.h	a header file containing definitions for each message.  The -h
 *		option triggers the creation of these definitions and specifies
 *		the name to use.
 *
 * msg.c	a data array of message strings.  The msg.h definitions are
 *		offsets into this array.  The -d option triggers the creation of
 *		these definitions and specifies the name to use.
 *
 * messages	a message file suitable for catgets(3C) or gettext(3C) use.  The
 *		-m option triggers this output and specifies the filename to be
 *		used.
 *
 * The template file is processed based on the first character of each line:
 *
 * # or $	entries are copied (as is) to the message file (messages).
 *
 * @ token(s)	entries are translated.  Two translations are possible dependent
 *		on whether one or more tokens are supplied:
 *
 *		A single token is interpreted as one of two reserved message
 *		output indicators, or a message identifier.  The reserved output
 *		indicator _START_ enables output to the message file - Note that
 *		the occurance of any other @ token will also enable message
 *		output.  The reserved output indicator _END_ disables output to
 *		the message file.  The use of these two indicators provides for
 *		only those message strings that require translation to be output
 *		to the message file.
 *
 *		Besides the reserved output indicators, a single token is taken
 *		to be a message identifier which will be subsituted for a
 *		`setid' for catgets(3c) output, or a `domain' name for
 *		gettext(3C) output.  This value is determine by substituting the
 *		token for the associated definition found in the message
 *		identifier file (specified with the -i option).
 *
 *		Multiple tokens are taken to be a message definition followed by
 *		the associated message string.  The message string is copied to
 *		the data array being built in msg.c.  The index into this array
 *		becomes the `message' identifier created in the msg.h file.
 */

#include	<fcntl.h>
#include	<stdlib.h>
#include	<stdio.h>
#include	<unistd.h>
#include	<limits.h>
#include	<string.h>
#include	<ctype.h>
#include	<errno.h>
#include	<sys/param.h>

#include	<sgs.h>
#include	<_string_table.h>

/*
 * Define any error message strings.
 */
static const char
	* Errmsg_malt =	"sgsmsg: file %s: line %d: malformed input "
			"at line\n",
	* Errmsg_nmem =	"sgsmsg: memory allocation failed: %s\n",
	* Errmsg_opne =	"sgsmsg: file %s: open failed: %s\n",
	* Errmsg_wrte =	"sgsmsg: file %s: write failed: %s\n",
	* Errmsg_read =	"sgsmsg: file %s: read failed %s\n",
	* Errmsg_stnw =	"sgsmsg: st_new(): failed: %s\n",
	* Errmsg_stin =	"sgsmsg: Str_tbl insert failed: %s\n",
	* Errmsg_mnfn =	"sgsmsg: message not found in Str_tbl: %s\n",
	* Errmsg_use  =	"usage: sgsmsg [-clv] [-d mesgdata] [-h mesgdefs] "
			"[-m messages] [-n name] [-i mesgident] file ...\n";

/*
 * Define all output filenames and associated descriptors.
 */
static FILE	*fddefs, *fddata, *fdmsgs, *fdmids, *fddesc;
static char	*fldefs, *fldata, *flmsgs, *flmids, *fldesc;
static FILE	*fdlint;
static char	fllint[MAXPATHLEN];

static uint_t		vflag;	/* verbose flag */
static Str_tbl		*stp;	/* string table */

/*
 * Define any default strings.
 */
static const char
	*nmlint =	"/tmp/sgsmsg.lint",
	*interface =	"sgs_msg",
	*start =	"_START_",
	*end =		"_END_";

/*
 * Define any default flags and data items.
 */
static int	cflag = 0, lflag = 0, prtmsgs = 0, line, ptr = 1, msgid = 0;
static char	*mesgid = 0, *setid = 0, *domain = 0;

typedef struct msg_string {
	char			*ms_defn;
	char			*ms_message;
	struct msg_string	*ms_next;
} msg_string;

static msg_string	*msg_head;
static msg_string	*msg_tail;

/*
 * message_append() is responsible for both inserting strings into
 * the master Str_tbl as well as maintaining a list of the
 * DEFINITIONS associated with each string.
 *
 * The list of strings is traversed at the end once the full
 * Str_tbl has been constructed - and string offsets can be
 * assigned.
 */
static void
message_append(const char *defn, const char *message)
{
	msg_string	*msg;
	if ((msg = calloc(sizeof (msg_string), 1)) == 0) {
		(void) fprintf(stderr, Errmsg_nmem, strerror(errno));
		exit(1);
	}

	/*
	 * Initialize the string table.
	 */
	if ((stp == 0) && ((stp = st_new(FLG_STNEW_COMPRESS)) == NULL)) {
		(void) fprintf(stderr, Errmsg_stnw, strerror(errno));
		exit(1);
	}


	if ((msg->ms_defn = strdup(defn)) == 0) {
		(void) fprintf(stderr, Errmsg_nmem, strerror(errno));
		exit(1);
	}
	if ((msg->ms_message = strdup(message)) == 0) {
		(void) fprintf(stderr, Errmsg_nmem, strerror(errno));
		exit(1);
	}

	if (st_insert(stp, msg->ms_message) == -1) {
		(void) fprintf(stderr, Errmsg_stin,
		    message);
		exit(1);
	}

	if (msg_head == 0) {
		msg_head = msg_tail = msg;
		return;
	}
	msg_tail->ms_next = msg;
	msg_tail = msg;
}

/*
 * Initialize a setid value.  Given a setid definition determine its numeric
 * value from the specified message identifier file (specified with the -i
 * option).  Return a pointer to the numeric string.
 */
static int
getmesgid(char *id)
{
	char	*buffer, *token, *_mesgid = 0, *_setid = 0, *_domain = 0;

	/*
	 * If we're being asked to interpret a message id but the user didn't
	 * provide the required message identifier file (-i option) we're in
	 * trouble.
	 */
	if (flmids == 0) {
		(void) fprintf(stderr, "sgsmsg: file %s: line %d: mesgid %s: "
		    "unable to process mesgid\n\t"
		    "no message identifier file specified "
		    "(see -i option)\n", fldesc, line, id);
		return (1);
	}

	if ((buffer = malloc(LINE_MAX)) == 0) {
		(void) fprintf(stderr, Errmsg_nmem, strerror(errno));
		return (1);
	}

	/*
	 * Read the message identifier file and locate the required mesgid.
	 */
	rewind(fdmids);
	while (fgets(buffer, LINE_MAX, fdmids) != NULL) {
		if ((token = strstr(buffer, id)) == NULL)
			continue;

		/*
		 * Establish individual strings for the mesgid, setid and domain
		 * values.
		 */
		_mesgid = token;
		while (!(isspace(*token)))
			token++;
		*token++ = 0;

		while (isspace(*token))
			token++;
		_setid = token;
		while (!(isspace(*token)))
			token++;
		*token++ = 0;

		while (isspace(*token))
			token++;
		_domain = token;
		while (!(isspace(*token)))
			token++;
		*token = 0;
		break;
	}

	/*
	 * Did we find a match?
	 */
	if ((_mesgid == 0) || (_setid == 0) || (_domain == 0)) {
		(void) fprintf(stderr, "sgsmsg: file %s: line %d: mesgid %s: "
		    "unable to process mesgid\n\t"
		    "identifier does not exist in file %s\n",
		    fldesc, line, id, flmids);
		return (1);
	}

	/*
	 * Have we been here before?
	 */
	if (mesgid) {
		if (cflag == 1) {
			/*
			 * If we're being asked to process more than one mesgid
			 * warn the user that only one mesgid can be used for
			 * the catgets(3c) call.
			 */
			(void) fprintf(stderr, "sgsmsg: file %s: line %d: "
			    "setid %s: warning: multiple mesgids "
			    "encountered\n\t"
			    "last setting used in messaging code\n",
			    fldesc, line, id);
		}
	}

	mesgid = _mesgid;
	setid = _setid;
	domain = _domain;

	/*
	 * Generate the message file output (insure output flag is enabled).
	 */
	if (prtmsgs != -1)
		prtmsgs = 1;
	if (fdmsgs && (prtmsgs == 1)) {
		if (cflag == 1) {
			if (fprintf(fdmsgs, "$quote \"\n$set %s\n",
			    setid) < 0) {
				(void) fprintf(stderr, Errmsg_wrte, flmsgs,
				    strerror(errno));
				return (1);
			}
		} else {
			if (fprintf(fdmsgs, "domain\t\"%s\"\n", domain) < 0) {
				(void) fprintf(stderr, Errmsg_wrte, flmsgs,
				    strerror(errno));
				return (1);
			}
		}
	}

	/*
	 * For catgets(3c) output generate a setid definition in the message
	 * definition file.
	 */
	if (fddefs && (cflag == 1) &&
	    (fprintf(fddefs, "#define\t%s\t%s\n\n", mesgid, setid) < 0)) {
		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
		return (1);
	}

	return (0);
}

/*
 * Dump contents of String Table to standard out
 */
static void
dump_stringtab(Str_tbl *stp)
{
	uint_t	cnt;

	if ((stp->st_flags & FLG_STTAB_COMPRESS) == 0) {
		(void) printf("string table full size: %ld: uncompressed\n",
		    stp->st_fullstrsize);
		return;
	}

	(void) printf("string table full size: %ld compressed down to: %ld\n\n",
	    stp->st_fullstrsize, stp->st_strsize);
	(void) printf("string table compression information [%d buckets]:\n",
	    stp->st_hbckcnt);

	for (cnt = 0; cnt < stp->st_hbckcnt; cnt++) {
		Str_hash	*sthash = stp->st_hashbcks[cnt];

		if (sthash == 0)
			continue;

		(void) printf(" bucket: [%d]\n", cnt);

		while (sthash) {
			size_t	stroff = sthash->hi_mstr->sm_strlen -
			    sthash->hi_strlen;

			if (stroff == 0) {
				(void) printf("  [%ld]: '%s'  <master>\n",
				    sthash->hi_refcnt, sthash->hi_mstr->sm_str);
			} else {
				(void) printf("  [%ld]: '%s'  <suffix of: "
				    "'%s'>\n", sthash->hi_refcnt,
				    &sthash->hi_mstr->sm_str[stroff],
				    sthash->hi_mstr->sm_str);
			}
			sthash = sthash->hi_next;
		}
	}
}

/*
 * Initialize the message definition header file stream.
 */
static int
init_defs(void)
{
	static char	guard[FILENAME_MAX + 6];
	char		*optr;
	const char	*iptr, *_ptr;

	/*
	 * Establish a header guard name using the files basename.
	 */
	for (iptr = 0, _ptr = fldefs; _ptr && (*_ptr != '\0'); _ptr++) {
		if (*_ptr == '/')
			iptr = _ptr + 1;
	}
	if (iptr == 0)
		iptr = fldefs;

	optr = guard;
	for (*optr++ = '_'; iptr && (*iptr != '\0'); iptr++, optr++) {
		if (*iptr == '.') {
			*optr++ = '_';
			*optr++ = 'D';
			*optr++ = 'O';
			*optr++ = 'T';
			*optr = '_';
		} else
			*optr = toupper(*iptr);
	}

	if (fprintf(fddefs, "#ifndef\t%s\n#define\t%s\n\n", guard, guard) < 0) {
		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
		return (1);
	}

	if (fprintf(fddefs, "#include <sgsmsg.h>\t/* Msg typedef */\n\n") < 0) {
		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
		return (1);
	}

	if (fprintf(fddefs, "#ifndef\t__lint\n\n") < 0) {
		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
		return (1);
	}

	/*
	 * The MSG_SGS_ARRAY_NAME macro supplies a generic way to
	 * reference the string table regardless of its name.
	 */
	if (fprintf(fddefs, "#define\tMSG_SGS_LOCAL_ARRAY\t__%s\n\n",
	    interface) < 0) {
		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
		return (1);
	}

	/*
	 * If the associated data array is global define a prototype.
	 * Define a macro to access the array elements.
	 */
	if (lflag == 0) {
		if (fprintf(fddefs, "extern\tconst char\t__%s[];\n\n",
		    interface) < 0) {
			(void) fprintf(stderr, Errmsg_wrte, fldefs,
			    strerror(errno));
			return (1);
		}
	}
	if (fprintf(fddefs,
	    "#define\tMSG_ORIG_STRTAB(_x, _s)\t&_s[_x]\n\n") < 0) {
		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
		return (1);
	}
	if (fprintf(fddefs,
	    "#define\tMSG_ORIG(x)\tMSG_ORIG_STRTAB(x, __%s)\n\n",
	    interface) < 0) {
		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
		return (1);
	}

	/*
	 * Generate a prototype to access the associated data array.
	 */
	if (fprintf(fddefs, "extern\tconst char *\t_%s(Msg);\n\n",
	    interface) < 0) {
		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
		return (1);
	}
	if (fprintf(fddefs, "#define\tMSG_INTL(x)\t_%s(x)\n\n",
	    interface) < 0) {
		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
		return (1);
	}

	return (0);
}


/*
 * Finish the message definition header file.
 */
static int
fini_defs(void)
{
	if (fprintf(fddefs, "\n#else\t/* __lint */\n\n") < 0) {
		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
		return (1);
	}

	if (fprintf(fddefs, "extern\tconst char *\t_%s(Msg);\n\n",
	    interface) < 0) {
		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
		return (1);
	}

	if (fprintf(fddefs, "#ifndef MSG_SGS_LOCAL_ARRAY\n"
	    "#define\tMSG_SGS_LOCAL_ARRAY\t\"\"\n#endif\n\n") < 0) {
		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
		return (1);
	}

	if (lflag == 0) {
		if (fprintf(fddefs, "extern\tconst char\t__%s[];\n\n",
		    interface) < 0) {
			(void) fprintf(stderr, Errmsg_wrte, fldefs,
			    strerror(errno));
			return (1);
		}
	}

	if (fprintf(fddefs,
	    "#define MSG_ORIG_STRTAB(_x, _s)\t_x\n"
	    "#define MSG_ORIG(x)\tx\n#define MSG_INTL(x)\tx\n") < 0) {
		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
		return (1);
	}

	/*
	 * Provide a way to get the array and function declarations above
	 * without also getting the actual messages. This is useful in
	 * our lintsup.c files that include more than one message header.
	 * lintsup doesn't need the actual messages, and this prevents
	 * macro name collisions.
	 */
	if (fprintf(fddefs, "\n#ifndef LINTSUP_SUPPRESS_STRINGS\n") < 0) {
		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
		return (1);
	}

	/*
	 * Copy the temporary lint defs file into the new header.
	 */
	if (fdlint) {
		long	size;
		char	*buf;

		size = ftell(fdlint);
		(void) rewind(fdlint);

		if ((buf = malloc(size)) == 0) {
			(void) fprintf(stderr, Errmsg_nmem, strerror(errno));
			return (1);
		}
		if (fread(buf, size, 1, fdlint) == 0) {
			(void) fprintf(stderr, Errmsg_read, fllint,
			    strerror(errno));
			return (1);
		}
		if (fwrite(buf, size, 1, fddefs) == 0) {
			(void) fprintf(stderr, Errmsg_wrte, fldefs,
			    strerror(errno));
			return (1);
		}
		(void) free(buf);
	}

	if (fprintf(fddefs, "\n#endif\t/* LINTSUP_SUPPRESS_STRINGS */\n") < 0) {
		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
		return (1);
	}

	if (fprintf(fddefs, "\n#endif\t/* __lint */\n") < 0) {
		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
		return (1);
	}

	if (fprintf(fddefs, "\n#endif\n") < 0) {
		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
		return (1);
	}

	return (0);
}

/*
 * The entire messaging file has been scanned - and all strings have been
 * inserted into the string_table.  We can now walk the message queue
 * and create the '#define <DEFN>' for each string - with the strings
 * assigned offset into the string_table.
 */
static int
output_defs(void)
{
	msg_string	*msg;
	size_t		stbufsize;
	char		*stbuf;

	stbufsize = st_getstrtab_sz(stp);
	if ((stbuf = malloc(stbufsize)) == 0) {
		(void) fprintf(stderr, Errmsg_nmem, strerror(errno));
		exit(1);
	}
	(void) st_setstrbuf(stp, stbuf, stbufsize);
	for (msg = msg_head; msg; msg = msg->ms_next) {
		size_t	stoff;
		if ((st_setstring(stp, msg->ms_message, &stoff)) == -1) {
			(void) fprintf(stderr, Errmsg_mnfn, msg->ms_message);
			return (1);
		}
		if (fprintf(fddefs, "\n#define\t%s\t%ld\n",
		    msg->ms_defn, stoff) < 0) {
			(void) fprintf(stderr, Errmsg_wrte,
			    fldefs, strerror(errno));
			return (1);
		}
		if (fddefs && fprintf(fddefs, "#define\t%s_SIZE\t%d\n",
		    msg->ms_defn, strlen(msg->ms_message)) < 0) {
			(void) fprintf(stderr, Errmsg_wrte,
			    fldefs, strerror(errno));
			return (1);
		}
	}
	return (0);
}


/*
 * Finish off the data structure definition.
 */
static int
output_data(void)
{
	size_t		stbufsize;
	size_t		ndx;
	size_t		column = 1;
	const char	*stbuf;
	const char	*fmtstr;

	stbufsize = st_getstrtab_sz(stp);
	stbuf = st_getstrbuf(stp);

	assert(stbuf);

	/*
	 * Determine from the local flag whether the data declaration should
	 * be static.
	 */
	if (lflag)
		fmtstr = (const char *)"static const";
	else
		fmtstr = (const char *)"const";

	if (fprintf(fddata, "\n%s char __%s[%ld] __attribute__((unused)) = { ",
	    fmtstr, interface, stbufsize) < 0) {
		(void) fprintf(stderr, Errmsg_wrte, fldata, strerror(errno));
		return (1);
	}

	for (ndx = 0; ndx < (stbufsize - 1); ndx++) {
		if (column == 1) {
			if (fddata && fprintf(fddata,
			    "\n/* %4ld */ 0x%.2x,", ndx,
			    (unsigned char)stbuf[ndx]) < 0) {
				(void) fprintf(stderr, Errmsg_wrte,
				    fldata, strerror(errno));
				return (1);
			}
		} else {
			if (fddata && fprintf(fddata, "  0x%.2x,",
			    (unsigned char)stbuf[ndx]) < 0) {
				(void) fprintf(stderr, Errmsg_wrte,
				    fldata, strerror(errno));
				return (1);
			}
		}

		if (column++ == 10)
			column = 1;
	}

	if (column == 1)
		fmtstr = "\n\t0x%.2x };\n";
	else
		fmtstr = "  0x%.2x };\n";

	if (fprintf(fddata, fmtstr, (unsigned char)stbuf[stbufsize - 1]) < 0) {
		(void) fprintf(stderr, Errmsg_wrte, fldata, strerror(errno));
		return (1);
	}

	return (0);
}

static int
file()
{
	char	buffer[LINE_MAX], * token;
	uint_t	bufsize;
	char	*token_buffer;
	int	escape = 0;
	int	len = 0;

	if ((token_buffer = malloc(LINE_MAX)) == 0) {
		(void) fprintf(stderr, Errmsg_nmem, strerror(errno));
		return (1);
	}
	bufsize = LINE_MAX;

	line = 1;

	while ((token = fgets(buffer, LINE_MAX, fddesc)) != NULL) {
		char	defn[PATH_MAX], * _defn, * str;

		switch (*token) {
		case '#':
		case '$':
			if (escape) {
				(void) fprintf(stderr, Errmsg_malt, fldesc,
				    line);
				return (1);
			}

			/*
			 * If a msgid has been output a msgstr must follow
			 * before we digest the new token.  A msgid is only set
			 * if fdmsgs is in use.
			 */
			if (msgid) {
				msgid = 0;
				if (fprintf(fdmsgs, "msgstr\t\"\"\n") < 0) {
					(void) fprintf(stderr, Errmsg_wrte,
					    flmsgs, strerror(errno));
					return (1);
				}
			}

			/*
			 * Pass lines directly through to the output message
			 * file.
			 */
			if (fdmsgs && (prtmsgs == 1)) {
				char	comment;

				if (cflag == 0)
					comment = '#';
				else
					comment = '$';

				if (fprintf(fdmsgs, "%c%s", comment,
				    ++token) < 0) {
					(void) fprintf(stderr, Errmsg_wrte,
					    flmsgs, strerror(errno));
					return (1);
				}
			}
			break;

		case '@':
			if (escape) {
				(void) fprintf(stderr, Errmsg_malt, fldesc,
				    line);
				return (1);
			}

			/*
			 * If a msgid has been output a msgstr must follow
			 * before we digest the new token.
			 */
			if (msgid) {
				msgid = 0;
				if (fprintf(fdmsgs, "msgstr\t\"\"\n") < 0) {
					(void) fprintf(stderr, Errmsg_wrte,
					    flmsgs, strerror(errno));
					return (1);
				}
			}

			/*
			 * Determine whether we have one or more tokens.
			 */
			token++;
			while (isspace(*token))		/* rid any whitespace */
				token++;
			_defn = token;			/* definition start */
			while (!(isspace(*token)))
				token++;
			*token++ = 0;

			while (isspace(*token))		/* rid any whitespace */
				token++;

			/*
			 * Determine whether the single token is one of the
			 * reserved message output delimiters otherwise
			 * translate it as a message identifier.
			 */
			if (*token == 0) {
				if (strcmp(_defn, start) == 0)
					prtmsgs = 1;
				else if (strcmp(_defn, end) == 0)
					prtmsgs = -1;
				else if (getmesgid(_defn) == 1)
					return (1);
				break;
			}

			/*
			 * Multiple tokens are translated by taking the first
			 * token as the message definition, and the rest of the
			 * line as the message itself.  A message line ending
			 * with an escape ('\') is expected to be continued on
			 * the next line.
			 */
			if (prtmsgs != -1)
				prtmsgs = 1;
			if (fdmsgs && (prtmsgs == 1)) {
				/*
				 * For catgets(3c) make sure a message
				 * identifier has been established (this is
				 * normally a domain for gettext(3C), but for
				 * sgsmsg use this could be argued as being
				 * redundent).  Also make sure that the message
				 * definitions haven't exceeeded the maximum
				 * value allowed by gencat(1) before generating
				 * any message file entries.
				 */
				if (cflag == 1) {
					if (setid == 0) {
						(void) fprintf(stderr, "file "
						    "%s: no message identifier "
						    "has been established\n",
						    fldesc);
						return (1);
					}
					if (ptr	> NL_MSGMAX) {
						(void) fprintf(stderr, "file "
						    "%s: message definition "
						    "(%d) exceeds allowable "
						    "limit (NL_MSGMAX)\n",
						    fldesc, ptr);
						return (1);
					}
				}

				/*
				 * For catgets(3c) write the definition and the
				 * message string to the message file.  For
				 * gettext(3C) write the message string as a
				 * mesgid - indicate a mesgid has been output
				 * so that a msgstr can follow.
				 */
				if (cflag == 1) {
					if (fprintf(fdmsgs, "%d\t%s", ptr,
					    token) < 0) {
						(void) fprintf(stderr,
						    Errmsg_wrte, flmsgs,
						    strerror(errno));
						return (1);
					}
				} else {
					if (fprintf(fdmsgs, "msgid\t\"") < 0) {
						(void) fprintf(stderr,
						    Errmsg_wrte, flmsgs,
						    strerror(errno));
						return (1);
					}
					msgid = 1;
				}
			}

			/*
			 * The message itself is a quoted string as this makes
			 * embedding spaces at the start (or the end) of the
			 * string very easy.
			 */
			if (*token != '"') {
				(void) fprintf(stderr, Errmsg_malt, fldesc,
				    line);
				return (1);
			}

			(void) strcpy(defn, _defn);

			/*
			 * Write the tag to the lint definitions.
			 */
			if (fdlint) {
				if (fprintf(fdlint, "\n#define\t%s\t",
				    _defn) < 0) {
					(void) fprintf(stderr, Errmsg_wrte,
					    fllint, strerror(errno));
					return (1);
				}
			}

			len = 0;

			/*
			 * Write each character of the message string to the
			 * data array.  Translate any escaped characters - use
			 * the same specially recognized characters as defined
			 * by gencat(1).
			 */
message:
			if (*token == '"') {
				if (fdlint &&
				    (fprintf(fdlint, "%c", *token) < 0)) {
					(void) fprintf(stderr, Errmsg_wrte,
					    fllint, strerror(errno));
					return (1);
				}
				token++;
			}
			while (*token) {
				char	_token;

				if ((*token == '\\') && (escape == 0)) {
					escape = 1;
					if (fdlint && (*(token + 1) != '\n') &&
					    fprintf(fdlint, "%c", *token) < 0) {
						(void) fprintf(stderr,
						    Errmsg_wrte, fllint,
						    strerror(errno));
						return (1);
					}
					token++;
					continue;
				}
				if (escape) {
					if (*token == 'n')
						_token = '\n';
					else if (*token == 't')
						_token = '\t';
					else if (*token == 'v')
						_token = '\v';
					else if (*token == 'b')
						_token = '\b';
					else if (*token == 'f')
						_token = '\f';
					else if (*token == '\\')
						_token = '\\';
					else if (*token == '"')
						_token = '"';
					else if (*token == '\n')
						break;
					else
						_token = *token;

					if (fdmsgs && (prtmsgs == 1) &&
					    (fprintf(fdmsgs, "\\") < 0)) {
						(void) fprintf(stderr,
						    Errmsg_wrte, flmsgs,
						    strerror(errno));
						return (1);
					}
				} else {
					/*
					 * If this is the trailing quote then
					 * thats the last of the message string.
					 * Eat up any remaining white space and
					 * unless an escape character is found
					 * terminate the data string with a 0.
					 */
					/* BEGIN CSTYLED */
					if (*token == '"') {
					    if (fdlint && (fprintf(fdlint,
						"%c", *token) < 0)) {
						(void) fprintf(stderr,
						    Errmsg_wrte, fllint,
						    strerror(errno));
						return (1);
					    }

					    if (fdmsgs && (prtmsgs == 1) &&
						(fprintf(fdmsgs, "%c",
						*token) < 0)) {
						(void) fprintf(stderr,
						    Errmsg_wrte, flmsgs,
						    strerror(errno));
						return (1);
					    }

					    while (*++token) {
						if (*token == '\n')
							break;
					    }
					    _token = '\0';
					} else
					    _token = *token;
					/* END CSTYLED */
				}

				if (fdmsgs && (prtmsgs == 1) &&
				    (fprintf(fdmsgs, "%c", *token) < 0)) {
					(void) fprintf(stderr, Errmsg_wrte,
					    flmsgs, strerror(errno));
					return (1);
				}

				if (fdlint && fprintf(fdlint,
				    "%c", *token) < 0) {
					(void) fprintf(stderr, Errmsg_wrte,
					    fllint, strerror(errno));
					return (1);
				}

				if (len >= bufsize) {
					bufsize += LINE_MAX;
					if ((token_buffer = realloc(
					    token_buffer, bufsize)) == 0) {
						(void) fprintf(stderr,
						    Errmsg_nmem,
						    strerror(errno));
						return (1);
					}
				}
				token_buffer[len] = _token;
				ptr++, token++, len++;
				escape = 0;

				if (_token == '\0')
					break;
			}

			/*
			 * After the complete message string has been processed
			 * (including its continuation beyond one line), create
			 * a string size definition.
			 */
			if (escape == 0) {
				const char *form = "#define\t%s_SIZE\t%d\n";

				token_buffer[len] = '\0';

				message_append(defn, token_buffer);

				if (fdlint && fprintf(fdlint, form, defn,
				    (len - 1)) < 0) {
					(void) fprintf(stderr, Errmsg_wrte,
					    fllint, strerror(errno));
					return (1);
				}
			}
			break;

		default:
			/*
			 * Empty lines are passed through to the message file.
			 */
			while (isspace(*token))
				token++;

			if (*token == 0) {
				if (msgid || (fdmsgs && (prtmsgs == 1))) {
					/*
					 * If a msgid has been output a msgstr
					 * must follow before we digest the new
					 * token.
					 */
					if (msgid) {
						msgid = 0;
						str = "msgstr\t\"\"\n\n";
					} else
						str = "\n";

					if (fprintf(fdmsgs, str) < 0) {
						(void) fprintf(stderr,
						    Errmsg_wrte, flmsgs,
						    strerror(errno));
						return (1);
					}
				}
				break;
			}

			/*
			 * If an escape is in effect then any tokens are taken
			 * to be message continuations.
			 */
			if (escape) {
				escape = 0;
				goto message;
			}

			(void) fprintf(stderr, "file %s: line %d: invalid "
			    "input does not start with #, $ or @\n", fldesc,
			    line);
			return (1);
		}
		line++;
	}

	free(token_buffer);

	return (0);
}

int
main(int argc, char ** argv)
{
	opterr = 0;
	while ((line = getopt(argc, argv, "cd:h:lm:n:i:v")) != EOF) {
		switch (line) {
		case 'c':			/* catgets instead of gettext */
			cflag = 1;
			break;
		case 'd':			/* new message data filename */
			fldata = optarg;	/*	(msg.c is default) */
			break;
		case 'h':			/* new message defs filename */
			fldefs = optarg;	/*	(msg.h is default) */
			break;
		case 'i':			/* input message ids from */
			flmids = optarg;	/*	from this file */
			break;
		case 'l':			/* define message data arrays */
			lflag = 1;		/*	to be local (static) */
			break;
		case 'm':			/* generate message database */
			flmsgs = optarg;	/*	to this file */
			break;
		case 'n':			/* new data array and func */
			interface = optarg;	/*	name (msg is default) */
			break;
		case 'v':
			vflag = 1;		/* set verbose flag */
			break;
		case '?':
			(void) fprintf(stderr, Errmsg_use, argv[0]);
			exit(1);
		default:
			break;
		}
	}

	/*
	 * Validate the we have been given at least one input file.
	 */
	if ((argc - optind) < 1) {
		(void) fprintf(stderr, Errmsg_use);
		exit(1);
	}

	/*
	 * Open all the required output files.
	 */
	if (fldefs) {
		if ((fddefs = fopen(fldefs, "w+")) == NULL) {
			(void) fprintf(stderr, Errmsg_opne, fldefs,
			    strerror(errno));
			return (1);
		}
	}
	if (fldata) {
		if (fldefs && (strcmp(fldefs, fldata) == 0))
			fddata = fddefs;
		else if ((fddata = fopen(fldata, "w+")) == NULL) {
			(void) fprintf(stderr, Errmsg_opne, fldata,
			    strerror(errno));
			return (1);
		}
	}
	if (fddefs && fddata) {
		(void) sprintf(fllint, "%s.%d.XXXXXX", nmlint, (int)getpid());
		if ((mkstemp(fllint) == -1) ||
		    ((fdlint = fopen(fllint, "w+")) == NULL)) {
			(void) fprintf(stderr, Errmsg_opne, fllint,
			    strerror(errno));
			return (1);
		}
	}
	if (flmsgs) {
		if ((fdmsgs = fopen(flmsgs, "w+")) == NULL) {
			(void) fprintf(stderr, Errmsg_opne, flmsgs,
			    strerror(errno));
			return (1);
		}
	}
	if (flmids) {
		if ((fdmids = fopen(flmids, "r")) == NULL) {
			(void) fprintf(stderr, Errmsg_opne, flmids,
			    strerror(errno));
			return (1);
		}
	}


	/*
	 * Initialize the message definition and message data streams.
	 */
	if (fddefs) {
		if (init_defs())
			return (1);
	}

	/*
	 * Read the input message file, and for each line process accordingly.
	 */
	for (; optind < argc; optind++) {
		int	err;

		fldesc = argv[optind];

		if ((fddesc = fopen(fldesc, "r")) == NULL) {
			(void) fprintf(stderr, Errmsg_opne, fldesc,
			    strerror(errno));
			return (1);
		}
		err = file();
		(void) fclose(fddesc);

		if (err != 0)
			return (1);
	}

	/*
	 * If a msgid has been output a msgstr must follow before we end the
	 * file.
	 */
	if (msgid) {
		msgid = 0;
		if (fprintf(fdmsgs, "msgstr\t\"\"\n") < 0) {
			(void) fprintf(stderr, Errmsg_wrte, flmsgs,
			    strerror(errno));
			return (1);
		}
	}

	if (fdmids)
		(void) fclose(fdmids);
	if (fdmsgs)
		(void) fclose(fdmsgs);

	if (fddefs) {
		if (output_defs())
			return (1);
	}

	/*
	 * Finish off any generated data and header file.
	 */
	if (fldata) {
		if (output_data())
			return (1);
	}
	if (fddefs) {
		if (fini_defs())
			return (1);
	}

	if (vflag)
		dump_stringtab(stp);

	/*
	 * Close up everything and go home.
	 */
	if (fddata)
		(void) fclose(fddata);
	if (fddefs && (fddefs != fddata))
		(void) fclose(fddefs);
	if (fddefs && fddata) {
		(void) fclose(fdlint);
		(void) unlink(fllint);
	}

	if (stp)
		st_destroy(stp);

	return (0);
}