|
root / base / usr / src / cmd / msgfmt / xgettext.c
xgettext.c C 2346 lines 50.6 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
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
/*
 * 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 1991, 1999, 2001-2002 Sun Microsystems, Inc.
 * All rights reserved.
 * Use is subject to license terms.
 */

#include	<ctype.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>

#define	TRUE	1
#define	FALSE	0
#define	MAX_PATH_LEN	1024
#define	MAX_DOMAIN_LEN	1024
#define	MAX_STRING_LEN  2048

#define	USAGE	"Usage:	xgettext [-a [-x exclude-file]] [-jns]\
[-c comment-tag]\n	[-d default-domain] [-m prefix] \
[-M suffix] [-p pathname] files ...\n\
	xgettext -h\n"

#define	DEFAULT_DOMAIN	"messages"

extern char	yytext[];
extern int	yylex(void);

/*
 * Contains a list of strings to be used to store ANSI-C style string.
 * Each quoted string is stored in one node.
 */
struct strlist_st {
	char			*str;
	struct strlist_st	*next;
};

/*
 * istextdomain	: Boolean telling if this node contains textdomain call.
 * isduplicate	: Boolean telling if this node duplicate of any other msgid.
 * msgid	: contains msgid or textdomain if istextdomain is true.
 * msgstr	: contains msgstr.
 * comment	: comment extracted in case of -c option.
 * fname	: tells which file contains msgid.
 * linenum	: line number in the file.
 * next		: Next node.
 */
struct element_st {
	char			istextdomain;
	char			isduplicate;
	struct strlist_st	*msgid;
	struct strlist_st	*msgstr;
	struct strlist_st	*comment;
	char			*fname;
	int			linenum;
	struct element_st	*next;
};

/*
 * dname	   : domain name. NULL if default domain.
 * gettext_head    : Head of linked list containing [d]gettext().
 * gettext_tail    : Tail of linked list containing [d]gettext().
 * textdomain_head : Head of linked list containing textdomain().
 * textdomain_tail : Tail of linked list containing textdomain().
 * next		   : Next node.
 *
 * Each domain contains two linked list.
 *	(gettext_head,  textdomain_head)
 * If -s option is used, then textdomain_head contains all
 * textdomain() calls and no textdomain() calls are stored in gettext_head.
 * If -s option is not used, textdomain_head is empty list and
 * gettext_head contains all gettext() dgettext(), and textdomain() calls.
 */
struct domain_st {
	char			*dname;
	struct element_st	*gettext_head;
	struct element_st	*gettext_tail;
	struct element_st	*textdomain_head;
	struct element_st	*textdomain_tail;
	struct domain_st	*next;
};

/*
 * There are two domain linked lists.
 * def_dom contains default domain linked list and
 * dom_head contains all other deomain linked lists to be created by
 * dgettext() calls.
 */
static struct domain_st	*def_dom = NULL;
static struct domain_st	*dom_head = NULL;
static struct domain_st	*dom_tail = NULL;

/*
 * This linked list contains a list of strings to be excluded when
 * -x option is used.
 */
static struct exclude_st {
	struct strlist_st	*exstr;
	struct exclude_st	*next;
} *excl_head;

/*
 * All option flags and values for each option if any.
 */
static int	aflg = FALSE;
static int	cflg = FALSE;
static char	*comment_tag = NULL;
static char	*default_domain = NULL;
static int	hflg = FALSE;
static int	jflg = FALSE;
static int	mflg = FALSE;
static int	Mflg = FALSE;
static char	*suffix = NULL;
static char	*prefix = NULL;
static int	nflg = FALSE;
static int	pflg = FALSE;
static char	*pathname = NULL;
static int	sflg = FALSE;
static int	tflg = FALSE;	/* Undocumented option to extract dcgettext */
static int	xflg = FALSE;
static char	*exclude_file = NULL;

/*
 * Each variable shows the current state of parsing input file.
 *
 * in_comment    : Means inside comment block (C or C++).
 * in_cplus_comment    : Means inside C++ comment block.
 * in_gettext    : Means inside gettext call.
 * in_dgettext   : Means inside dgettext call.
 * in_dcgettext  : Means inside dcgettext call.
 * in_textdomain : Means inside textdomain call.
 * in_str	 : Means currently processing ANSI style string.
 * in_quote	 : Means currently processing double quoted string.
 * in_skippable_string	: Means currently processing double quoted string,
 *                        that occurs outside a call to gettext, dgettext,
 *                        dcgettext, textdomain, with -a not specified.
 * is_last_comment_line : Means the current line is the last line
 *			  of the comment block. This is necessary because
 *			  in_comment becomes FALSE when '* /' is encountered.
 * is_first_comma_found : This is used only for dcgettext because dcgettext()
 *			  requires 2 commas. So need to do different action
 *			  depending on which commas encountered.
 * num_nested_open_paren : This keeps track of the number of open parens to
 *			   handle dcgettext ((const char *)0,"msg",LC_TIME);
 */
static int	in_comment		= FALSE;
static int	in_cplus_comment	= FALSE;
static int	in_gettext		= FALSE;
static int	in_dgettext		= FALSE;
static int	in_dcgettext		= FALSE;
static int	in_textdomain		= FALSE;
static int	in_str			= FALSE;
static int	in_quote		= FALSE;
static int	is_last_comment_line	= FALSE;
static int	is_first_comma_found	= FALSE;
static int	in_skippable_string	= FALSE;
static int	num_nested_open_paren	= 0;

/*
 * This variable contains the first line of gettext(), dgettext(), or
 * textdomain() calls.
 * This is necessary for multiple lines of a single call to store
 * the starting line.
 */
static int	linenum_saved = 0;

int	stdin_only = FALSE;	/* Read input from stdin */

/*
 * curr_file    : Contains current file name processed.
 * curr_domain  : Contains the current domain for each dgettext().
 *		  This is NULL for gettext().
 * curr_line    : Contains the current line processed.
 * qstring_buf  : Contains the double quoted string processed.
 * curr_linenum : Line number being processed in the current input file.
 * warn_linenum : Line number of current warning message.
 */
char	curr_file[MAX_PATH_LEN];
static char	curr_domain[MAX_DOMAIN_LEN];
static char	curr_line[MAX_STRING_LEN];
static char	qstring_buf[MAX_STRING_LEN];
int	curr_linenum = 1;
int	warn_linenum = 0;

/*
 * strhead  : This list contains ANSI style string.
 *		Each node contains double quoted string.
 * strtail  : This is the tail of strhead.
 * commhead : This list contains comments string.
 *		Each node contains one line of comment.
 * commtail : This is the tail of commhead.
 */
static struct strlist_st	*strhead = NULL;
static struct strlist_st	*strtail = NULL;
static struct strlist_st	*commhead = NULL;
static struct strlist_st	*commtail = NULL;

/*
 * gargc : Same as argc. Used to pass argc to lex routine.
 * gargv : Same as argv. Used to pass argc to lex routine.
 */
int	gargc;
char	**gargv;

static void add_line_to_comment(void);
static void add_qstring_to_str(void);
static void add_str_to_element_list(int, char *);
static void copy_strlist_to_str(char *, struct strlist_st *);
static void end_ansi_string(void);
static void free_strlist(struct strlist_st *);
void handle_newline(void);
static void initialize_globals(void);
static void output_comment(FILE *, struct strlist_st *);
static void output_msgid(FILE *, struct strlist_st *, int);
static void output_textdomain(FILE *, struct element_st *);
static void print_help(void);
static void read_exclude_file(void);
static void trim_line(char *);
static void write_all_files(void);
static void write_one_file(struct domain_st *);

static void lstrcat(char *, const char *);

/*
 * Utility functions to malloc a node and initialize fields.
 */
static struct domain_st  *new_domain(void);
static struct strlist_st *new_strlist(void);
static struct element_st *new_element(void);
static struct exclude_st *new_exclude(void);

/*
 * Main program of xgettext.
 */
int
main(int argc, char **argv)
{
	int		opterr = FALSE;
	int		c;

	initialize_globals();

	while ((c = getopt(argc, argv, "jhax:nsc:d:m:M:p:t")) != EOF) {
		switch (c) {
		case 'a':
			aflg = TRUE;
			break;
		case 'c':
			cflg = TRUE;
			comment_tag = optarg;
			break;
		case 'd':
			default_domain = optarg;
			break;
		case 'h':
			hflg = TRUE;
			break;
		case 'j':
			jflg = TRUE;
			break;
		case 'M':
			Mflg = TRUE;
			suffix = optarg;
			break;
		case 'm':
			mflg = TRUE;
			prefix = optarg;
			break;
		case 'n':
			nflg = TRUE;
			break;
		case 'p':
			pflg = TRUE;
			pathname = optarg;
			break;
		case 's':
			sflg = TRUE;
			break;
		case 't':
			tflg = TRUE;
			break;
		case 'x':
			xflg = TRUE;
			exclude_file = optarg;
			break;
		case '?':
			opterr = TRUE;
			break;
		}
	}

	/* if -h is used, ignore all other options. */
	if (hflg == TRUE) {
		(void) fprintf(stderr, USAGE);
		print_help();
		exit(0);
	}

	/* -x can be used only with -a */
	if ((xflg == TRUE) && (aflg == FALSE))
		opterr = TRUE;

	/* -j cannot be used with -a */
	if ((jflg == TRUE) && (aflg == TRUE)) {
		(void) fprintf(stderr,
		"-a and -j options cannot be used together.\n");
		opterr = TRUE;
	}

	/* -j cannot be used with -s */
	if ((jflg == TRUE) && (sflg == TRUE)) {
		(void) fprintf(stderr,
		"-j and -s options cannot be used together.\n");
		opterr = TRUE;
	}

	if (opterr == TRUE) {
		(void) fprintf(stderr, USAGE);
		exit(2);
	}

	/* error, if no files are specified. */
	if (optind == argc) {
		(void) fprintf(stderr, USAGE);
		exit(2);
	}

	if (xflg == TRUE) {
		read_exclude_file();
	}

	/* If files are -, then read from stdin */
	if (argv[optind][0] == '-') {
		stdin_only = TRUE;
		optind++;
	} else {
		stdin_only = FALSE;
	}

	/* Store argc and argv to pass to yylex() */
	gargc = argc;
	gargv = argv;

#ifdef DEBUG
	(void) printf("optind=%d\n", optind);
	{
	int i = optind;
	for (; i < argc; i++) {
		(void) printf("   %d, <%s>\n", i, argv[i]);
	}
	}
#endif

	if (stdin_only == FALSE) {
		if (freopen(argv[optind], "r", stdin) == NULL) {
			(void) fprintf(stderr,
			"ERROR, can't open input file: %s\n", argv[optind]);
			exit(2);
		}
		(void) strcpy(curr_file, gargv[optind]);
		optind++;
	}

	/*
	 * Process input.
	 */
	(void) yylex();

#ifdef DEBUG
	printf("\n======= default_domain ========\n");
	print_one_domain(def_dom);
	printf("======= domain list ========\n");
	print_all_domain(dom_head);
#endif

	/*
	 * Write out all .po files.
	 */
	write_all_files();

	return (0);
} /* main */

/*
 * Prints help information for each option.
 */
static void
print_help(void)
{
	(void) fprintf(stderr, "\n");
	(void) fprintf(stderr, "-a\t\t\tfind ALL strings\n");
	(void) fprintf(stderr,
	    "-c <comment-tag>\tget comments containing <flag>\n");
	(void) fprintf(stderr,
	    "-d <default-domain>\tuse <default-domain> for default domain\n");
	(void) fprintf(stderr, "-h\t\t\tHelp\n");
	(void) fprintf(stderr,
	    "-j\t\t\tupdate existing file with the current result\n");
	(void) fprintf(stderr,
	    "-M <suffix>\t\tfill in msgstr with msgid<suffix>\n");
	(void) fprintf(stderr,
	    "-m <prefix>\t\tfill in msgstr with <prefix>msgid\n");
	(void) fprintf(stderr,
	    "-n\t\t\tline# file name and line number info in output\n");
	(void) fprintf(stderr,
	    "-p <pathname>\t\tuse <pathname> for output file directory\n");
	(void) fprintf(stderr,
	    "-s\t\t\tgenerate sorted output files\n");
	(void) fprintf(stderr, "-x <exclude-file>\texclude strings in file "
	    "<exclude-file> from output\n");
	(void) fprintf(stderr,
	    "-\t\t\tread stdin, use as a filter (input only)\n");
} /* print_help */

/*
 * Extract file name and line number information from macro line
 * and set the global variable accordingly.
 * The valid line format is
 *   1) # nnn
 *    or
 *   2) # nnn "xxxxx"
 *   where nnn is line number and xxxxx is file name.
 */
static void
extract_filename_linenumber(char *mline)
{
	int	num;
	char	*p, *q, *r;

	/*
	 * mline can contain multi newline.
	 * line number should be increased by the number of newlines.
	 */
	p = mline;
	while ((p = strchr(p, '\n')) != NULL) {
		p++;
		curr_linenum++;
	}
	p = strchr(mline, ' ');
	if (p == NULL)
		return;
	q = strchr(++p, ' ');
	if (q == NULL) {
		/* case 1 */
		if ((num = atoi(p)) > 0) {
			curr_linenum = num;
			return;
		}
	} else {
		/* case 2 */
		*q++ = 0;
		if (*q == '"') {
			q++;
			r = strchr(q, '"');
			if (r == NULL) {
				return;
			}
			*r = 0;
			if ((num = atoi(p)) > 0) {
				curr_linenum = num;
				(void) strcpy(curr_file, q);
			}
		}
	}
} /* extract_filename_linenumber */

/*
 * Handler for MACRO line which starts with #.
 */
void
handle_macro_line(void)
{
#ifdef DEBUG
	(void) printf("Macro line=<%s>\n", yytext);
#endif
	if (cflg == TRUE)
		lstrcat(curr_line, yytext);

	if (in_quote == TRUE) {
		lstrcat(qstring_buf, yytext);
	} else if (in_comment == FALSE) {
		extract_filename_linenumber(yytext);
	}

	curr_linenum--;
	handle_newline();
} /* handle_macro_line */

/*
 * Handler for C++ comments which starts with //.
 */
void
handle_cplus_comment_line(void)
{
	if (cflg == TRUE)
		lstrcat(curr_line, yytext);

	if (in_quote == TRUE) {
		lstrcat(qstring_buf, yytext);
	} else if ((in_comment == FALSE) &&
	    (in_skippable_string == FALSE)) {

		/*
		 * If already in c comments, don't do anything.
		 * Set both flags to TRUE here.
		 * Both flags will be set to FALSE when newline
		 * encounters.
		 */
		in_cplus_comment = TRUE;
		in_comment = TRUE;
	}
} /* handle_cplus_comment_line */

/*
 * Handler for the comment start (slash asterisk) in input file.
 */
void
handle_open_comment(void)
{
	if (cflg == TRUE)
		lstrcat(curr_line, yytext);

	if (in_quote == TRUE) {
		lstrcat(qstring_buf, yytext);
	} else if ((in_comment == FALSE) &&
	    (in_skippable_string == FALSE)) {

		in_comment = TRUE;
		is_last_comment_line = FALSE;
		/*
		 * If there is any comment extracted before accidently,
		 * clean it up and start the new comment again.
		 */
		free_strlist(commhead);
		commhead = commtail = NULL;
	}
}

/*
 * Handler for the comment end (asterisk slash) in input file.
 */
void
handle_close_comment(void)
{
	if (cflg == TRUE)
		lstrcat(curr_line, yytext);

	if (in_quote == TRUE) {
		lstrcat(qstring_buf, yytext);
	} else if (in_skippable_string == FALSE) {
		in_comment = FALSE;
		is_last_comment_line = TRUE;
	}
}

/*
 * Handler for "gettext" in input file.
 */
void
handle_gettext(void)
{
	/*
	 * If -t option is specified to extrct dcgettext,
	 * don't do anything for gettext().
	 */
	if (tflg == TRUE) {
		return;
	}

	num_nested_open_paren = 0;

	if (cflg == TRUE)
		lstrcat(curr_line, yytext);

	if (in_quote == TRUE) {
		lstrcat(qstring_buf, yytext);
	} else if (in_comment == FALSE) {
		in_gettext = TRUE;
		linenum_saved = curr_linenum;
		/*
		 * gettext will be put into default domain .po file
		 * curr_domain does not change for gettext.
		 */
		curr_domain[0] = '\0';
	}
} /* handle_gettext */

/*
 * Handler for "dgettext" in input file.
 */
void
handle_dgettext(void)
{
	/*
	 * If -t option is specified to extrct dcgettext,
	 * don't do anything for dgettext().
	 */
	if (tflg == TRUE) {
		return;
	}

	num_nested_open_paren = 0;

	if (cflg == TRUE)
		lstrcat(curr_line, yytext);

	if (in_quote == TRUE) {
		lstrcat(qstring_buf, yytext);
	} else if (in_comment == FALSE) {
		in_dgettext = TRUE;
		linenum_saved = curr_linenum;
		/*
		 * dgettext will be put into domain file specified.
		 * curr_domain will follow.
		 */
		curr_domain[0] = '\0';
	}
} /* handle_dgettext */

/*
 * Handler for "dcgettext" in input file.
 */
void
handle_dcgettext(void)
{
	/*
	 * dcgettext will be extracted only when -t flag is specified.
	 */
	if (tflg == FALSE) {
		return;
	}

	num_nested_open_paren = 0;

	is_first_comma_found = FALSE;

	if (cflg == TRUE)
		lstrcat(curr_line, yytext);

	if (in_quote == TRUE) {
		lstrcat(qstring_buf, yytext);
	} else if (in_comment == FALSE) {
		in_dcgettext = TRUE;
		linenum_saved = curr_linenum;
		/*
		 * dcgettext will be put into domain file specified.
		 * curr_domain will follow.
		 */
		curr_domain[0] = '\0';
	}
} /* handle_dcgettext */

/*
 * Handler for "textdomain" in input file.
 */
void
handle_textdomain(void)
{
	if (cflg == TRUE)
		lstrcat(curr_line, yytext);

	if (in_quote == TRUE) {
		lstrcat(qstring_buf, yytext);
	} else if (in_comment == FALSE) {
		in_textdomain = TRUE;
		linenum_saved = curr_linenum;
		curr_domain[0] = '\0';
	}
} /* handle_textdomain */

/*
 * Handler for '(' in input file.
 */
void
handle_open_paren(void)
{
	if (cflg == TRUE)
		lstrcat(curr_line, yytext);

	if (in_quote == TRUE) {
		lstrcat(qstring_buf, yytext);
	} else if (in_comment == FALSE) {
		if ((in_gettext == TRUE) ||
		    (in_dgettext == TRUE) ||
		    (in_dcgettext == TRUE) ||
		    (in_textdomain == TRUE)) {
			in_str = TRUE;
			num_nested_open_paren++;
		}
	}
} /* handle_open_paren */

/*
 * Handler for ')' in input file.
 */
void
handle_close_paren(void)
{
	if (cflg == TRUE)
		lstrcat(curr_line, yytext);

	if (in_quote == TRUE) {
		lstrcat(qstring_buf, yytext);
	} else if (in_comment == FALSE) {
		if ((in_gettext == TRUE) ||
		    (in_dgettext == TRUE) ||
		    (in_dcgettext == TRUE) ||
		    (in_textdomain == TRUE)) {
			/*
			 * If this is not the matching close paren with
			 * the first open paren, no action is necessary.
			 */
			if (--num_nested_open_paren > 0)
				return;
			add_str_to_element_list(in_textdomain, curr_domain);
			in_str = FALSE;
			in_gettext = FALSE;
			in_dgettext = FALSE;
			in_dcgettext = FALSE;
			in_textdomain = FALSE;
		} else if (aflg == TRUE) {
			end_ansi_string();
		}
	}
} /* handle_close_paren */

/*
 * Handler for '\\n' in input file.
 *
 * This is a '\' followed by new line.
 * This can be treated like a new line except when this is a continuation
 * of a ANSI-C string.
 * If this is a part of ANSI string, treat the current line as a double
 * quoted string and the next line is the start of the double quoted
 * string.
 */
void
handle_esc_newline(void)
{
	if (cflg == TRUE)
		lstrcat(curr_line, "\\");

	curr_linenum++;

	if (in_quote == TRUE) {
		add_qstring_to_str();
	} else if ((in_comment == TRUE) ||
	    (is_last_comment_line == TRUE)) {
		if (in_cplus_comment == FALSE) {
			add_line_to_comment();
		}
	}

	curr_line[0] = '\0';
} /* handle_esc_newline */

/*
 * Handler for '"' in input file.
 */
void
handle_quote(void)
{
	if (cflg == TRUE)
		lstrcat(curr_line, yytext);

	if (in_comment == TRUE) {
		/*EMPTY*/
	} else if ((in_gettext == TRUE) ||
	    (in_dgettext == TRUE) ||
	    (in_dcgettext == TRUE) ||
	    (in_textdomain == TRUE)) {
		if (in_str == TRUE) {
			if (in_quote == FALSE) {
				in_quote = TRUE;
			} else {
				add_qstring_to_str();
				in_quote = FALSE;
			}
		}
	} else if (aflg == TRUE) {
		/*
		 * The quote is found outside of gettext, dgetext, and
		 * textdomain. Everytime a quoted string is found,
		 * add it to the string list.
		 * in_str stays TRUE until ANSI string ends.
		 */
		if (in_str == TRUE) {
			if (in_quote == TRUE) {
				in_quote = FALSE;
				add_qstring_to_str();
			} else {
				in_quote = TRUE;
			}
		} else {
			in_str = TRUE;
			in_quote = TRUE;
			linenum_saved = curr_linenum;
		}
	} else {
		in_skippable_string = (in_skippable_string == TRUE) ?
		    FALSE : TRUE;
	}
} /* handle_quote */

/*
 * Handler for ' ' or TAB in input file.
 */
void
handle_spaces(void)
{
	if (cflg == TRUE)
		lstrcat(curr_line, yytext);

	if (in_quote == TRUE) {
		lstrcat(qstring_buf, yytext);
	}
} /* handle_spaces */

/*
 * Flattens a linked list containing ANSI string to the one string.
 */
static void
copy_strlist_to_str(char *str, struct strlist_st *strlist)
{
	struct strlist_st	*p;

	str[0] = '\0';

	if (strlist != NULL) {
		p = strlist;
		while (p != NULL) {
			if (p->str != NULL) {
				lstrcat(str, p->str);
			}
			p = p->next;
		}
	}
} /* copy_strlist_to_str */

/*
 * Handler for ',' in input file.
 */
void
handle_comma(void)
{
	if (cflg == TRUE)
		lstrcat(curr_line, yytext);

	if (in_quote == TRUE) {
		lstrcat(qstring_buf, yytext);
	} else if (in_comment == FALSE) {
		if (in_str == TRUE) {
			if (in_dgettext == TRUE) {
				copy_strlist_to_str(curr_domain, strhead);
				free_strlist(strhead);
				strhead = strtail = NULL;
			} else if (in_dcgettext == TRUE) {
				/*
				 * Ignore the second comma.
				 */
				if (is_first_comma_found == FALSE) {
					copy_strlist_to_str(curr_domain,
					    strhead);
					free_strlist(strhead);
					strhead = strtail = NULL;
					is_first_comma_found = TRUE;
				}
			} else if (aflg == TRUE) {
				end_ansi_string();
			}
		}
	}
} /* handle_comma */

/*
 * Handler for any other character that does not have special handler.
 */
void
handle_character(void)
{
	if (cflg == TRUE)
		lstrcat(curr_line, yytext);

	if (in_quote == TRUE) {
		lstrcat(qstring_buf, yytext);
	} else if (in_comment == FALSE) {
		if (in_str == TRUE) {
			if (aflg == TRUE) {
				end_ansi_string();
			}
		}
	}
} /* handle_character */

/*
 * Handler for new line in input file.
 */
void
handle_newline(void)
{
	curr_linenum++;

	/*
	 * in_quote is always FALSE here for ANSI-C code.
	 */
	if ((in_comment == TRUE) ||
	    (is_last_comment_line == TRUE)) {
		if (in_cplus_comment == TRUE) {
			in_cplus_comment = FALSE;
			in_comment = FALSE;
		} else {
			add_line_to_comment();
		}
	}

	curr_line[0] = '\0';
	/*
	 * C++ comment always ends with new line.
	 */
} /* handle_newline */

/*
 * Process ANSI string.
 */
static void
end_ansi_string(void)
{
	if ((aflg == TRUE) &&
	    (in_str == TRUE) &&
	    (in_gettext == FALSE) &&
	    (in_dgettext == FALSE) &&
	    (in_dcgettext == FALSE) &&
	    (in_textdomain == FALSE)) {
		add_str_to_element_list(FALSE, curr_domain);
		in_str = FALSE;
	}
} /* end_ansi_string */

/*
 * Initialize global variables if necessary.
 */
static void
initialize_globals(void)
{
	default_domain = strdup(DEFAULT_DOMAIN);
	curr_domain[0] = '\0';
	curr_file[0] = '\0';
	qstring_buf[0] = '\0';
} /* initialize_globals() */

/*
 * Extract only string part when read a exclude file by removing
 * keywords (e.g. msgid, msgstr, # ) and heading and trailing blanks and
 * double quotes.
 */
static void
trim_line(char *line)
{
	int	i, p, len;
	int	first = 0;
	int	last = 0;
	char	c;

	len = strlen(line);

	/*
	 * Find the position of the last non-whitespace character.
	 */
	i = len - 1;
	/*CONSTCOND*/
	while (1) {
		c = line[i--];
		if ((c != ' ') && (c != '\n') && (c != '\t')) {
			last = ++i;
			break;
		}
	}

	/*
	 * Find the position of the first non-whitespace character
	 * by skipping "msgid" initially.
	 */
	if (strncmp("msgid ", line, 6) == 0) {
		i = 5;
	} else if (strncmp("msgstr ", line, 7) == 0) {
		i = 6;
	} else if (strncmp("# ", line, 2) == 0) {
		i = 2;
	} else {
		i = 0;
	}

	/*CONSTCOND*/
	while (1) {
		c = line[i++];
		if ((c != ' ') && (c != '\n') && (c != '\t')) {
			first = --i;
			break;
		}
	}

	/*
	 * For Backward compatibility, we consider both double quoted
	 * string and non-quoted string.
	 * The double quote is removed before being stored if exists.
	 */
	if (line[first] == '"') {
		first++;
	}
	if (line[last] == '"') {
		last--;
	}

	/*
	 * Now copy the valid part of the string.
	 */
	p = first;
	for (i = 0; i <= (last-first); i++) {
		line[i] = line[p++];
	}
	line [i] = '\0';
} /* trim_line */

/*
 * Read exclude file and stores it in the global linked list.
 */
static void
read_exclude_file(void)
{
	FILE	*fp;
	struct exclude_st	*tmp_excl;
	struct strlist_st	*tail;
	int			ignore_line;
	char			line [MAX_STRING_LEN];

	if ((fp = fopen(exclude_file, "r")) == NULL) {
		(void) fprintf(stderr, "ERROR, can't open exclude file: %s\n",
		    exclude_file);
		exit(2);
	}

	ignore_line = TRUE;
	while (fgets(line, MAX_STRING_LEN, fp) != NULL) {
		/*
		 * Line starting with # is a comment line and ignored.
		 * Blank line is ignored, too.
		 */
		if ((line[0] == '\n') || (line[0] == '#')) {
			continue;
		} else if (strncmp(line, "msgstr", 6) == 0) {
			ignore_line = TRUE;
		} else if (strncmp(line, "domain", 6) == 0) {
			ignore_line = TRUE;
		} else if (strncmp(line, "msgid", 5) == 0) {
			ignore_line = FALSE;
			tmp_excl = new_exclude();
			tmp_excl->exstr = new_strlist();
			trim_line(line);
			tmp_excl->exstr->str = strdup(line);
			tail = tmp_excl->exstr;
			/*
			 * Prepend new exclude string node to the list.
			 */
			tmp_excl->next = excl_head;
			excl_head = tmp_excl;
		} else {
			/*
			 * If more than one line of string forms msgid,
			 * append it to the string linked list.
			 */
			if (ignore_line == FALSE) {
				trim_line(line);
				tail->next = new_strlist();
				tail->next->str = strdup(line);
				tail = tail->next;
			}
		}
	} /* while */

#ifdef DEBUG
	tmp_excl = excl_head;
	while (tmp_excl != NULL) {
		printf("============================\n");
		tail = tmp_excl->exstr;
		while (tail != NULL) {
			printf("%s###\n", tail->str);
			tail = tail->next;
		}
		tmp_excl = tmp_excl->next;
	}
#endif
} /* read_exclude_file */

/*
 * Get next character from the string list containing ANSI style string.
 * This function returns three valus. (p, *m, *c).
 * p is returned by return value and, *m and *c are returned by changing
 * values in the location pointed.
 *
 *  p : points node in the linked list for ANSI string.
 *	Each node contains double quoted string.
 *  m : The location of the next characters in the double quoted string
 *	as integer index in the string.
 *	When it gets to end of quoted string, the next node will be
 *	read and m starts as zero for every new node.
 *  c : Stores the value of the characterto be returned.
 */
static struct strlist_st *
get_next_ch(struct strlist_st *p, int *m, char *c)
{
	char	ch, oct, hex;
	int	value, i;

	/*
	 * From the string list, find non-null string first.
	 */

	/*CONSTCOND*/
	while (1) {
		if (p == NULL) {
			break;
		} else if (p->str == NULL)  {
			p = p->next;
		} else if (p->str[*m] == '\0') {
			p = p->next;
			*m = 0;
		} else {
			break;
		}
	}

	/*
	 * No more character is available.
	 */
	if (p == NULL) {
		*c = 0;
		return (NULL);
	}

	/*
	 * Check if the character back slash.
	 * If yes, ANSI defined escape sequence rule is used.
	 */
	if (p->str[*m] != '\\') {
		*c = p->str[*m];
		*m = *m + 1;
		return (p);
	} else {
		/*
		 * Get next character after '\'.
		 */
		*m = *m + 1;
		ch = p->str[*m];
		switch (ch) {
		case 'a':
			*c = '\a';
			break;
		case 'b':
			*c = '\b';
			break;
		case 'f':
			*c = '\f';
			break;
		case 'n':
			*c = '\n';
			break;
		case 'r':
			*c = '\r';
			break;
		case 't':
			*c = '\t';
			break;
		case 'v':
			*c = '\v';
			break;
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
			/*
			 * Get maximum of three octal digits.
			 */
			value = ch;
			for (i = 0; i < 2; i++) {
				*m = *m + 1;
				oct = p->str[*m];
				if ((oct >= '0') && (oct <= '7')) {
					value = value * 8 + (oct - '0');
				} else {
					*m = *m - 1;
					break;
				}
			}
			*c = value;
#ifdef DEBUG
			/* (void) fprintf(stderr, "octal=%d\n", value); */
#endif
			break;
		case 'x':
			value = 0;
			/*
			 * Remove all heading zeros first and
			 * get one or two valuid hexadecimal charaters.
			 */
			*m = *m + 1;
			while (p->str[*m] == '0') {
				*m = *m + 1;
			}
			value = 0;
			for (i = 0; i < 2; i++) {
				hex = p->str[*m];
				*m = *m + 1;
				if (isdigit(hex)) {
					value = value * 16 + (hex - '0');
				} else if (isxdigit(hex)) {
					hex = tolower(hex);
					value = value * 16 + (hex - 'a' + 10);
				} else {
					*m = *m - 1;
					break;
				}
			}
			*c = value;
#ifdef DEBUG
			(void) fprintf(stderr, "hex=%d\n", value);
#endif
			*m = *m - 1;
			break;
		default :
			/*
			 * Undefined by ANSI.
			 * Just ignore "\".
			 */
			*c = p->str[*m];
			break;
		}
		/*
		 * Advance pointer to point the next character to be parsed.
		 */
		*m = *m + 1;
		return (p);
	}
} /* get_next_ch */

/*
 * Compares two msgids.
 * Comparison is done by values, not by characters represented.
 * For example, '\t', '\011' and '0x9' are identical values.
 * Return values are same as in strcmp.
 *   1   if  msgid1 > msgid2
 *   0   if  msgid1 = msgid2
 *  -1   if  msgid1 < msgid2
 */
static int
msgidcmp(struct strlist_st *id1, struct strlist_st *id2)
{
	char	c1, c2;
	int	m1, m2;

	m1 = 0;
	m2 = 0;

	/*CONSTCOND*/
	while (1) {
		id1 = get_next_ch(id1, &m1, &c1);
		id2 = get_next_ch(id2, &m2, &c2);

		if ((c1 == 0) && (c2 == 0)) {
			return (0);
		}

		if (c1 > c2) {
			return (1);
		} else if (c1 < c2) {
			return (-1);
		}
	}
	/*NOTREACHED*/
} /* msgidcmp */

/*
 * Check if a ANSI string (which is a linked list itself) is a duplicate
 * of any string in the list of ANSI string.
 */
static int
isduplicate(struct element_st *list, struct strlist_st *str)
{
	struct element_st	*p;

	if (list == NULL) {
		return (FALSE);
	}

	p = list;
	while (p != NULL) {
		if (p->msgid != NULL) {
			if (msgidcmp(p->msgid, str) == 0) {
				return (TRUE);
			}
		}
		p = p->next;
	}

	return (FALSE);
} /* isduplicate */

/*
 * Extract a comment line and add to the linked list containing
 * comment block.
 * Each comment line is stored in the node.
 */
static void
add_line_to_comment(void)
{
	struct strlist_st	*tmp_str;

	tmp_str = new_strlist();
	tmp_str->str = strdup(curr_line);
	tmp_str->next = NULL;

	if (commhead == NULL) {
		/* Empty comment list */
		commhead = tmp_str;
		commtail = tmp_str;
	} else {
		/* append it to the list */
		commtail->next = tmp_str;
		commtail = commtail->next;
	}

	is_last_comment_line = FALSE;
} /* add_line_to_comment */

/*
 * Add a double quoted string to the linked list containing ANSI string.
 */
static void
add_qstring_to_str(void)
{
	struct strlist_st	*tmp_str;

	tmp_str = new_strlist();
	tmp_str->str = strdup(qstring_buf);
	tmp_str->next = NULL;

	if (strhead == NULL) {
		/* Null ANSI string */
		strhead = tmp_str;
		strtail = tmp_str;
	} else {
		/* Append it to the ANSI string linked list */
		strtail->next = tmp_str;
		strtail = strtail->next;
	}

	qstring_buf[0] = '\0';
} /* add_qstring_to_str */

/*
 * Finds the head of domain nodes given domain name.
 */
static struct domain_st *
find_domain_node(char *dname)
{
	struct domain_st	*tmp_dom, *p;

	/*
	 * If -a option is specified everything will be written to the
	 * default domain file.
	 */
	if (aflg == TRUE) {
		if (def_dom == NULL) {
			def_dom = new_domain();
		}
		return (def_dom);
	}

	if ((dname == NULL) ||
	    (dname[0] == '\0') ||
	    (strcmp(dname, default_domain) == 0)) {
		if (def_dom == NULL) {
			def_dom = new_domain();
		}
		if (strcmp(dname, default_domain) == 0) {
			(void) fprintf(stderr, "%s \"%s\" is used in dgettext "
			    "of file:%s line:%d.\n",
			    "Warning: default domain name",
			    default_domain, curr_file, curr_linenum);
		}
		return (def_dom);
	} else {
		p = dom_head;
		while (p != NULL) {
			if (strcmp(p->dname, dname) == 0) {
				return (p);
			}
			p = p->next;
		}

		tmp_dom = new_domain();
		tmp_dom->dname = strdup(dname);

		if (dom_head == NULL) {
			dom_head = tmp_dom;
			dom_tail = tmp_dom;
		} else {
			dom_tail->next = tmp_dom;
			dom_tail = dom_tail->next;
		}
		return (tmp_dom);
	}
} /* find_domain_node */

/*
 * Frees the ANSI string linked list.
 */
static void
free_strlist(struct strlist_st *ptr)
{
	struct strlist_st	*p;

	p = ptr;
	ptr = NULL;
	while (p != NULL) {
		ptr = p->next;
		free(p->str);
		free(p);
		p = ptr;
	}
} /* free_strlist */

/*
 * Finds if a ANSI string is contained in the exclude file.
 */
static int
isexcluded(struct strlist_st *strlist)
{
	struct exclude_st	*p;

	p = excl_head;
	while (p != NULL) {
		if (msgidcmp(p->exstr, strlist) == 0) {
			return (TRUE);
		}
		p = p->next;
	}
	return (FALSE);
} /* isexcluded */

/*
 * Finds if a comment block is to be extracted.
 *
 * When -c option is specified, find out if comment block contains
 * comment-tag as a token separated by blanks. If it does, this
 * comment block is associated with the next msgid encountered.
 * Comment block is a linked list where each node contains one line
 * of comments.
 */
static int
isextracted(struct strlist_st *strlist)
{
	struct strlist_st	*p;
	char			*first, *pc;


	p = strlist;
	while (p != NULL) {
		first = strdup(p->str);
		while ((first != NULL) && (first[0] != '\0')) {
			pc = first;

			/*CONSTCOND*/
			while (1) {
				if (*pc == '\0') {
					break;
				} else if ((*pc == ' ') || (*pc == '\t')) {
					*pc++ = '\0';
					break;
				}
				pc++;
			}
			if (strcmp(first, comment_tag) == 0) {
				return (TRUE);
			}
			first = pc;
		}
		p = p->next;
	} /* while */

	/*
	 * Not found.
	 */
	return (FALSE);
} /* isextracted */

/*
 * Adds ANSI string to the domain element list.
 */
static void
add_str_to_element_list(int istextdomain, char *domain_list)
{
	struct element_st	*tmp_elem;
	struct element_st	*p, *q;
	struct domain_st	*tmp_dom;
	int			result;

	/*
	 * This can happen if something like gettext(USAGE) is used
	 * and it is impossible to get msgid for this gettext.
	 * Since -x option should be used in this kind of cases,
	 * it is OK not to catch msgid.
	 */
	if (strhead == NULL) {
		return;
	}

	/*
	 * The global variable curr_domain contains either NULL
	 * for default_domain or domain name for dgettext().
	 */
	tmp_dom = find_domain_node(domain_list);

	/*
	 * If this msgid is in the exclude file,
	 * then free the linked list and return.
	 */
	if ((istextdomain == FALSE) &&
	    (isexcluded(strhead) == TRUE)) {
		free_strlist(strhead);
		strhead = strtail = NULL;
		return;
	}

	tmp_elem = new_element();
	tmp_elem->msgid = strhead;
	tmp_elem->istextdomain = istextdomain;
	/*
	 * If -c option is specified and TAG matches,
	 * then associate the comment to the next [d]gettext() calls
	 * encountered in the source code.
	 * textdomain() calls will not have any effect.
	 */
	if (istextdomain == FALSE) {
		if ((cflg == TRUE) && (commhead != NULL)) {
			if (isextracted(commhead) == TRUE) {
				tmp_elem->comment = commhead;
			} else {
				free_strlist(commhead);
			}
			commhead = commtail = NULL;
		}
	}

	tmp_elem->linenum = linenum_saved;
	tmp_elem->fname = strdup(curr_file);


	if (sflg == TRUE) {
		/*
		 * If this is textdomain() call and -s option is specified,
		 * append this node to the textdomain linked list.
		 */
		if (istextdomain == TRUE) {
			if (tmp_dom->textdomain_head == NULL) {
				tmp_dom->textdomain_head = tmp_elem;
				tmp_dom->textdomain_tail = tmp_elem;
			} else {
				tmp_dom->textdomain_tail->next = tmp_elem;
				tmp_dom->textdomain_tail = tmp_elem;
			}
			strhead = strtail = NULL;
			return;
		}

		/*
		 * Insert the node to the properly sorted position.
		 */
		q = NULL;
		p = tmp_dom->gettext_head;
		while (p != NULL) {
			result = msgidcmp(strhead, p->msgid);
			if (result == 0) {
				/*
				 * Duplicate id. Do not store.
				 */
				free_strlist(strhead);
				strhead = strtail = NULL;
				return;
			} else if (result > 0) {
				/* move to the next node */
				q = p;
				p = p->next;
			} else {
				tmp_elem->next = p;
				if (q != NULL) {
					q->next = tmp_elem;
				} else {
					tmp_dom->gettext_head = tmp_elem;
				}
				strhead = strtail = NULL;
				return;
			}
		} /* while */

		/*
		 * New msgid is the largest or empty list.
		 */
		if (q != NULL) {
			/* largest case */
			q->next = tmp_elem;
		} else {
			/* empty list */
			tmp_dom->gettext_head = tmp_elem;
		}
	} else {
		/*
		 * Check if this msgid is already in the same domain.
		 */
		if (tmp_dom != NULL) {
			if (isduplicate(tmp_dom->gettext_head,
			    tmp_elem->msgid) == TRUE) {
				tmp_elem->isduplicate = TRUE;
			}
		}
		/*
		 * If -s option is not specified, then everything
		 * is stored in gettext linked list.
		 */
		if (tmp_dom->gettext_head == NULL) {
			tmp_dom->gettext_head = tmp_elem;
			tmp_dom->gettext_tail = tmp_elem;
		} else {
			tmp_dom->gettext_tail->next = tmp_elem;
			tmp_dom->gettext_tail = tmp_elem;
		}
	}

	strhead = strtail = NULL;
} /* add_str_to_element_list */

/*
 * Write all domain linked list to the files.
 */
static void
write_all_files(void)
{
	struct domain_st	*tmp;

	/*
	 * Write out default domain file.
	 */
	write_one_file(def_dom);

	/*
	 * If dgettext() exists and -a option is not used,
	 * then there are non-empty linked list.
	 */
	tmp = dom_head;
	while (tmp != NULL) {
		write_one_file(tmp);
		tmp = tmp->next;
	}
} /* write_all_files */

/*
 * add an element_st list to the linked list.
 */
static void
add_node_to_polist(struct element_st **pohead,
    struct element_st **potail, struct element_st *elem)
{
	if (elem == NULL) {
		return;
	}

	if (*pohead == NULL) {
		*pohead = *potail = elem;
	} else {
		(*potail)->next = elem;
		*potail = (*potail)->next;
	}
} /* add_node_to_polist */

#define	INIT_STATE	0
#define	IN_MSGID	1
#define	IN_MSGSTR	2
#define	IN_COMMENT	3
/*
 * Reads existing po file into the linked list and returns the head
 * of the linked list.
 */
static struct element_st *
read_po(char *fname)
{
	struct element_st	*tmp_elem = NULL;
	struct element_st	*ehead = NULL, *etail = NULL;
	struct strlist_st	*comment_tail = NULL;
	struct strlist_st	*msgid_tail = NULL;
	struct strlist_st	*msgstr_tail = NULL;
	int			state = INIT_STATE;
	char			line [MAX_STRING_LEN];
	FILE			*fp;

	if ((fp = fopen(fname, "r")) == NULL) {
		return (NULL);
	}

	while (fgets(line, MAX_STRING_LEN, fp) != NULL) {
		/*
		 * Line starting with # is a comment line and ignored.
		 * Blank line is ignored, too.
		 */
		if (line[0] == '\n') {
			continue;
		} else if (line[0] == '#') {
			/*
			 * If tmp_elem is not NULL, there is msgid pair
			 * stored. Therefore, add it.
			 */
			if ((tmp_elem != NULL) && (state == IN_MSGSTR)) {
				add_node_to_polist(&ehead, &etail, tmp_elem);
			}

			if ((state == INIT_STATE) || (state == IN_MSGSTR)) {
				state = IN_COMMENT;
				tmp_elem = new_element();
				tmp_elem->comment = comment_tail =
				    new_strlist();
				/*
				 * remove new line and skip "# "
				 * in the beginning of the existing
				 * comment line.
				 */
				line[strlen(line)-1] = 0;
				comment_tail->str = strdup(line+2);
			} else if (state == IN_COMMENT) {
				comment_tail->next = new_strlist();
				comment_tail = comment_tail->next;
				/*
				 * remove new line and skip "# "
				 * in the beginning of the existing
				 * comment line.
				 */
				line[strlen(line)-1] = 0;
				comment_tail->str = strdup(line+2);
			}

		} else if (strncmp(line, "domain", 6) == 0) {
			/* ignore domain line */
			continue;
		} else if (strncmp(line, "msgid", 5) == 0) {
			if (state == IN_MSGSTR) {
				add_node_to_polist(&ehead, &etail, tmp_elem);
				tmp_elem = new_element();
			} else if (state == INIT_STATE) {
				tmp_elem = new_element();
			}

			state = IN_MSGID;
			trim_line(line);
			tmp_elem->msgid = msgid_tail = new_strlist();
			msgid_tail->str = strdup(line);

		} else if (strncmp(line, "msgstr", 6) == 0) {
			state = IN_MSGSTR;
			trim_line(line);
			tmp_elem->msgstr = msgstr_tail = new_strlist();
			msgstr_tail->str = strdup(line);
		} else {
			/*
			 * If more than one line of string forms msgid,
			 * append it to the string linked list.
			 */
			if (state == IN_MSGID) {
				trim_line(line);
				msgid_tail->next = new_strlist();
				msgid_tail = msgid_tail->next;
				msgid_tail->str = strdup(line);
			} else if (state == IN_MSGSTR) {
				trim_line(line);
				msgstr_tail->next = new_strlist();
				msgstr_tail = msgstr_tail->next;
				msgstr_tail->str = strdup(line);
			}
		}
	} /* while */

	/*
	 * To insert the last msgid pair.
	 */
	if (tmp_elem != NULL) {
		add_node_to_polist(&ehead, &etail, tmp_elem);
	}

#ifdef DEBUG
	{
		struct domain_st *tmp_domain = new_domain();
		char	tmpstr[256];

		sprintf(tmpstr, "existing_po file : <%s>", fname);
		tmp_domain->dname = strdup(tmpstr);
		tmp_domain->gettext_head = ehead;
		printf("======= existing po file <%s>  ========\n", fname);
		print_one_domain(tmp_domain);
	}
#endif /* DEBUG */

	(void) fclose(fp);
	return (ehead);
} /* read_po */

/*
 * This function will append the second list to the first list.
 * If the msgid in the second list contains msgid in the first list,
 * it will be marked as duplicate.
 */
static struct element_st *
append_list(struct element_st *l1, struct element_st *l2)
{
	struct element_st	*p = NULL, *q = NULL, *l1_tail = NULL;

	if (l1 == NULL)
		return (l2);
	if (l2 == NULL)
		return (l1);

	/*
	 * in this while loop, just mark isduplicate field of node in the
	 * l2 list if the same msgid exists in l1 list.
	 */
	p = l2;
	while (p != NULL) {
		q = l1;
		while (q != NULL) {
			if (msgidcmp(p->msgid, q->msgid) == 0) {
				p->isduplicate = TRUE;
				break;
			}
			q = q->next;
		}
		p = p->next;
	}

	/* Now connect two linked lists. */
	l1_tail = l1;
	while (l1_tail->next != NULL) {
		if (l1->next == NULL)
			break;
		l1_tail = l1_tail-> next;
	}
	l1_tail->next = l2;

	return (l1);
} /* append_list */

/*
 * Writes one domain list to the file.
 */
static void
write_one_file(struct domain_st *head)
{
	FILE			*fp;
	char			fname [MAX_PATH_LEN];
	char			dname [MAX_DOMAIN_LEN];
	struct element_st	*p;
	struct element_st	*existing_po_list;

	/*
	 * If head is NULL, then it still has to create .po file
	 * so that it will guarantee that the previous .po file was
	 * alwasys deleted.
	 * This is why checking NULL pointer has been moved to after
	 * creating  .po file.
	 */

	/*
	 * If domain name is NULL, it is the default domain list.
	 * The domain name is either "messages" or specified by option -d.
	 * The default domain name is contained in default_domain variable.
	 */
	dname[0] = '\0';
	if ((head != NULL) &&
	    (head->dname != NULL)) {
		(void) strcpy(dname, head->dname);
	} else {
		(void) strcpy(dname, default_domain);
	}

	/*
	 * path is the current directory if not specified by option -p.
	 */
	fname[0] = 0;
	if (pflg == TRUE) {
		(void) strcat(fname, pathname);
		(void) strcat(fname, "/");
	}
	(void) strcat(fname, dname);
	(void) strcat(fname, ".po");

	/*
	 * If -j flag is specified, read exsiting .po file and
	 * append the current list to the end of the list read from
	 * the existing .po file.
	 */
	if (jflg == TRUE) {
		/*
		 * If head is NULL, we don't have to change existing file.
		 * Therefore, just return it.
		 */
		if (head == NULL) {
			return;
		}
		existing_po_list = read_po(fname);
		head->gettext_head = append_list(existing_po_list,
		    head->gettext_head);
#ifdef DEBUG
		if (head->dname != NULL) {
			printf("===after merge (-j option): <%s>===\n",
			    head->dname);
		} else {
			printf("===after merge (-j option): <NULL>===\n");
		}
		print_one_domain(head);
#endif

	} /* if jflg */

	if ((fp = fopen(fname, "w")) == NULL) {
		(void) fprintf(stderr,
		    "ERROR, can't open output file: %s\n", fname);
		exit(2);
	}

	(void) fprintf(fp, "domain \"%s\"\n", dname);

	/* See comments above in the beginning of this function */
	if (head == NULL)
		return;

	/*
	 * There are separate storage for textdomain() calls if
	 * -s option is used (textdomain_head linked list).
	 * Otherwise, textdomain() is mixed with gettext(0 and dgettext().
	 * If mixed, the boolean varaible istextdomain is used to see
	 * if the current node contains textdomain() or [d]gettext().
	 */
	if (sflg == TRUE) {
		p = head->textdomain_head;
		while (p != NULL) {
			/*
			 * textdomain output line already contains
			 * FIle name and line number information.
			 * Therefore, does not have to check for nflg.
			 */
			output_textdomain(fp, p);
			p = p->next;
		}
	}

	p = head->gettext_head;
	while (p != NULL) {

		/*
		 * Comment is printed only if -c is used and
		 * associated with gettext or dgettext.
		 * textdomain is not associated with comments.
		 * Changes:
		 *    comments should be extracted in case of -j option
		 *    because there are read from exising file.
		 */
		if (((cflg == TRUE) || (jflg == TRUE)) &&
		    (p->istextdomain != TRUE)) {
			output_comment(fp, p->comment);
		}

		/*
		 * If -n is used, then file number and line number
		 * information is printed.
		 * In case of textdomain(), this information is redundant
		 * and is not printed.
		 * If linenum is 0, it means this information has been
		 * read from existing po file and it already contains
		 * file and line number info as a comment line. So, it
		 * should not printed in such case.
		 */
		if ((nflg == TRUE) && (p->istextdomain == FALSE) &&
		    (p->linenum > 0)) {
			(void) fprintf(fp, "# File:%s, line:%d\n",
			    p->fname, p->linenum);
		}

		/*
		 * Depending on the type of node, output textdomain comment
		 * or msgid.
		 */
		if ((sflg == FALSE) &&
		    (p->istextdomain == TRUE)) {
			output_textdomain(fp, p);
		} else {
			output_msgid(fp, p->msgid, p->isduplicate);
		}
		p = p->next;

	} /* while */

	(void) fclose(fp);
} /* write_one_file */

/*
 * Prints out textdomain call as a comment line with file name and
 * the line number information.
 */
static void
output_textdomain(FILE *fp, struct element_st *p)
{

	if (p == NULL)
		return;

	/*
	 * Write textdomain() line as a comment.
	 */
	(void) fprintf(fp, "# File:%s, line:%d, textdomain(\"%s\");\n",
	    p->fname, p->linenum,  p->msgid->str);
} /* output_textdomain */

/*
 * Prints out comments from linked list.
 */
static void
output_comment(FILE *fp, struct strlist_st *p)
{
	if (p == NULL)
		return;

	/*
	 * Write comment section.
	 */
	while (p != NULL) {
		(void) fprintf(fp, "# %s\n", p->str);
		p = p->next;
	}
} /* output_comment */

/*
 * Prints out msgid along with msgstr.
 */
static void
output_msgid(FILE *fp, struct strlist_st *p, int duplicate)
{
	struct strlist_st	*q;

	if (p == NULL)
		return;

	/*
	 * Write msgid section.
	 * If duplciate flag is ON, prepend "# " in front of every line
	 * so that they are considered as comment lines in .po file.
	 */
	if (duplicate == TRUE) {
		(void) fprintf(fp, "# ");
	}
	(void) fprintf(fp, "msgid  \"%s\"\n", p->str);
	q = p->next;
	while (q != NULL) {
		if (duplicate == TRUE) {
			(void) fprintf(fp, "# ");
		}
		(void) fprintf(fp, "       \"%s\"\n", q->str);
		q = q->next;
	}

	/*
	 * Write msgstr section.
	 * if -M option is specified, append <suffix> to msgid.
	 * if -m option is specified, prepend <prefix> to msgid.
	 */
	if (duplicate == TRUE) {
		(void) fprintf(fp, "# ");
	}
	if ((mflg == TRUE) || (Mflg == TRUE)) {
		if (mflg == TRUE) {
			/*
			 * If single line msgid, add suffix to the same line
			 */
			if ((Mflg == TRUE) && (p->next == NULL)) {
				/* -M and -m and single line case */
				(void) fprintf(fp, "msgstr \"%s%s%s\"\n",
				    prefix, p->str, suffix);
			} else {
				/* -M and -m and multi line case */
				(void) fprintf(fp, "msgstr \"%s%s\"\n",
				    prefix, p->str);
			}
		} else {
			if ((Mflg == TRUE) && (p->next == NULL)) {
				/* -M only with single line case */
				(void) fprintf(fp, "msgstr \"%s%s\"\n",
				    p->str, suffix);
			} else {
				/* -M only with multi line case */
				(void) fprintf(fp, "msgstr \"%s\"\n", p->str);
			}
		}
		q = p->next;
		while (q != NULL) {
			if (duplicate == TRUE) {
				(void) fprintf(fp, "# ");
			}
			(void) fprintf(fp, "       \"%s\"\n", q->str);
			q = q->next;
		}
		/*
		 * If multi line msgid, add suffix after the last line.
		 */
		if ((Mflg == TRUE) && (p->next != NULL) &&
		    (suffix[0] != '\0')) {
			(void) fprintf(fp, "       \"%s\"\n", suffix);
		}
	} else {
		(void) fprintf(fp, "msgstr\n");
	}
} /* output_msgid */

/*
 * Malloc a new element node and initialize fields.
 */
static struct element_st *
new_element(void)
{
	struct element_st *tmp;

	tmp = (struct element_st *)malloc(sizeof (struct element_st));
	tmp->istextdomain = FALSE;
	tmp->isduplicate = FALSE;
	tmp->msgid = NULL;
	tmp->msgstr = NULL;
	tmp->comment = NULL;
	tmp->fname = NULL;
	tmp->linenum = 0;
	tmp->next = NULL;

	return (tmp);
} /* new_element */

/*
 * Malloc a new domain node and initialize fields.
 */
static struct domain_st *
new_domain(void)
{
	struct domain_st *tmp;

	tmp = (struct domain_st *)malloc(sizeof (struct domain_st));
	tmp->dname = NULL;
	tmp->gettext_head = NULL;
	tmp->gettext_tail = NULL;
	tmp->textdomain_head = NULL;
	tmp->textdomain_tail = NULL;
	tmp->next = NULL;

	return (tmp);
} /* new_domain */

/*
 * Malloc a new string list node and initialize fields.
 */
static struct strlist_st *
new_strlist(void)
{
	struct strlist_st *tmp;

	tmp = (struct strlist_st *)malloc(sizeof (struct strlist_st));
	tmp->str = NULL;
	tmp->next = NULL;

	return (tmp);
} /* new_strlist */

/*
 * Malloc a new exclude string list node and initialize fields.
 */
static struct exclude_st *
new_exclude(void)
{
	struct exclude_st *tmp;

	tmp = (struct exclude_st *)malloc(sizeof (struct exclude_st));
	tmp->exstr = NULL;
	tmp->next = NULL;

	return (tmp);
} /* new_exclude */

/*
 * Local version of strcat to keep within maximum string size.
 */
static void
lstrcat(char *s1, const char *s2)
{
	char	*es1 = &s1[MAX_STRING_LEN];
	char	*ss1 = s1;

	while (*s1++)
		;
	--s1;
	while (*s1++ = *s2++)
		if (s1 >= es1) {
			s1[-1] = '\0';
			if ((in_comment == TRUE || in_quote == TRUE) &&
			    (warn_linenum != curr_linenum)) {
				if (stdin_only == FALSE) {
					(void) fprintf(stderr,
					    "WARNING: file %s line %d exceeds "\
					    "%d characters:  \"%15.15s\"\n",
					    curr_file, curr_linenum,
					    MAX_STRING_LEN, ss1);
				} else {
					(void) fprintf(stderr,
					    "WARNING: line %d exceeds "\
					    "%d characters:  \"%15.15s\"\n",
					    curr_linenum, MAX_STRING_LEN, ss1);
				}
				warn_linenum = curr_linenum;
			}
			break;
		}
} /* lstrcat */

#ifdef DEBUG
/*
 * Debug print routine. Compiled only with DEBUG on.
 */
void
print_element_list(struct element_st *q)
{
	struct strlist_st	*r;

	while (q != NULL) {
		printf("   istextdomain = %d\n", q->istextdomain);
		printf("   isduplicate  = %d\n", q->isduplicate);
		if ((q->msgid != NULL) && (q->msgid->str != NULL)) {
			printf("   msgid = <%s>\n", q->msgid->str);
			r = q->msgid->next;
			while (r != NULL) {
				printf("           <%s>\n", r->str);
				r = r->next;
			}
		} else {
			printf("   msgid = <NULL>\n");
		}
		if ((q->msgstr != NULL) && (q->msgstr->str != NULL)) {
			printf("   msgstr= <%s>\n", q->msgstr->str);
			r = q->msgstr->next;
			while (r != NULL) {
				printf("           <%s>\n", r->str);
				r = r->next;
			}
		} else {
			printf("   msgstr= <NULL>\n");
		}

		if (q->comment == NULL) {
			printf("   comment = <NULL>\n");
		} else {
			printf("   comment = <%s>\n", q->comment->str);
			r = q->comment->next;
			while (r != NULL) {
				printf("             <%s>\n", r->str);
				r = r->next;
			}
		}

		if (q->fname == NULL) {
			printf("   fname = <NULL>\n");
		} else {
			printf("   fname = <%s>\n", q->fname);
		}
		printf("   linenum = %d\n", q->linenum);
		printf("\n");
		q = q->next;
	}
}

/*
 * Debug print routine. Compiled only with DEBUG on.
 */
void
print_one_domain(struct domain_st *p)
{
	struct element_st	*q;

	if (p == NULL) {
		printf("domain pointer = <NULL>\n");
		return;
	} else if (p->dname == NULL) {
		printf("domain_name = <%s>\n", "<NULL>");
	} else {
		printf("domain_name = <%s>\n", p->dname);
	}
	q = p->gettext_head;
	print_element_list(q);

	q = p->textdomain_head;
	print_element_list(q);
} /* print_one_domain */

void
print_all_domain(struct domain_st *dom_list)
{
	struct domain_st	*p;
	struct element_st	*q;

	p = dom_list;
	while (p != NULL) {
		print_one_domain(p);
		p = p->next;
	} /* while */
} /* print_all_domain */
#endif