summaryrefslogtreecommitdiff
path: root/imake.c
blob: a85a166034023d2e56a3d4019899ad199bf1a706 (plain)
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

/***************************************************************************
 *                                                                         *
 *                                Porting Note                             *
 *                                                                         *
 * Add the value of BOOTSTRAPCFLAGS to the cpp_argv table so that it will  *
 * be passed to the template file.                                         *
 *                                                                         *
 ***************************************************************************/
/* $XFree86: xc/config/imake/imake.c,v 3.63 2002/12/16 22:48:27 herrb Exp $ */

/*
 *
Copyright (c) 1985, 1986, 1987, 1998 The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.
 *
 * Original Author:
 *	Todd Brunhoff
 *	Tektronix, inc.
 *	While a guest engineer at Project Athena, MIT
 *
 * imake: the include-make program.
 *
 * Usage: imake [-Idir] [-Ddefine] [-T template] [-f imakefile ] [-C Imakefile.c ] [-s] [-e] [-v] [make flags]
 *
 * Imake takes a template file (Imake.tmpl) and a prototype (Imakefile)
 * and runs cpp on them producing a Makefile.  It then optionally runs make
 * on the Makefile.
 * Options:
 *		-D	define.  Same as cpp -D argument.
 *		-U	undefine.  Same as cpp -U argument.
 *		-W	warning.  Same as cpp -W argument.
 *		-I	Include directory.  Same as cpp -I argument.
 *		-T	template.  Designate a template other
 *			than Imake.tmpl
 *		-f	specify the Imakefile file
 *		-C	specify the name to use instead of Imakefile.c
 *		-s[F]	show.  Show the produced makefile on the standard
 *			output.  Make is not run is this case.  If a file
 *			argument is provided, the output is placed there.
 *		-e[F]	execute instead of show; optionally name Makefile F
 *		-v	verbose.  Show the make command line executed.
 *
 * Environment variables:
 *
 *		IMAKEINCLUDE	Include directory to use in addition to "."
 *		IMAKECPP	Cpp to use instead of /lib/cpp
 *		IMAKEMAKE	make program to use other than what is
 *				found by searching the $PATH variable.
 * Other features:
 *	imake reads the entire cpp output into memory and then scans it
 *	for occurences of "@@".  If it encounters them, it replaces it with
 *	a newline.  It also trims any trailing white space on output lines
 *	(because make gets upset at them).  This helps when cpp expands
 *	multi-line macros but you want them to appear on multiple lines.
 *	It also changes occurences of "XCOMM" to "#", to avoid problems
 *	with treating commands as invalid preprocessor commands.
 *
 *	The macros MAKEFILE and MAKE are provided as macros
 *	to make.  MAKEFILE is set to imake's makefile (not the constructed,
 *	preprocessed one) and MAKE is set to argv[0], i.e. the name of
 *	the imake program.
 *
 * Theory of operation:
 *   1. Determine the name of the imakefile from the command line (-f)
 *	or from the content of the current directory (Imakefile or imakefile).
 *	Call this <imakefile>.  This gets added to the arguments for
 *	make as MAKEFILE=<imakefile>.
 *   2. Determine the name of the template from the command line (-T)
 *	or the default, Imake.tmpl.  Call this <template>
 *   3. Determine the name of the imakeCfile from the command line (-C)
 *	or the default, Imakefile.c.  Call this <imakeCfile>
 *   4. Store lines of input into <imakeCfile>:
 *	- A c-style comment header (see ImakefileCHeader below), used
 *	  to recognize temporary files generated by imake.
 *	- If DEFAULT_OS_NAME is defined, format the utsname struct and
 *	  call the result <defaultOsName>.  Add:
 *		#define DefaultOSName <defaultOsName>
 *	- If DEFAULT_OS_MAJOR_REV is defined, format the utsname struct
 *	  and call the result <defaultOsMajorVersion>.  Add:
 *		#define DefaultOSMajorVersion <defaultOsMajorVersion>
 *	- If DEFAULT_OS_MINOR_REV is defined, format the utsname struct
 *	  and call the result <defaultOsMinorVersion>.  Add:
 *		#define DefaultOSMinorVersion <defaultOsMinorVersion>
 *	- If DEFAULT_OS_TEENY_REV is defined, format the utsname struct
 *	  and call the result <defaultOsTeenyVersion>.  Add:
 *		#define DefaultOSTeenyVersion <defaultOsTeenyVersion>
 *	- If DEFAULT_MACHINE_ARCITECTURE is defined, format the utsname struct
 *	  and define the corresponding macro.  (For example on the amiga,
 *	  this will define amiga in addition to m68k).
 *	- If the file "localdefines" is readable in the current
 *	  directory, print a warning message to stderr and add:
 *		#define IMAKE_LOCAL_DEFINES	"localdefines"
 *		#include IMAKE_LOCAL_DEFINES
 *	- If the file "admindefines" is readable in the current
 *	  directory, print a warning message to stderr and add:
 *		#define IMAKE_ADMIN_DEFINES	"admindefines"
 *		#include IMAKE_ADMIN_DEFINES
 *	- The following lines:
 *		#define INCLUDE_IMAKEFILE	< <imakefile> >
 *		#define IMAKE_TEMPLATE		" <template> "
 *		#include IMAKE_TEMPLATE
 *	- If the file "adminmacros" is readable in the current
 *	  directory, print a warning message to stderr and add:
 *		#define IMAKE_ADMIN_MACROS	"adminmacros"
 *		#include IMAKE_ADMIN_MACROS
 *	- If the file "localmacros" is readable in the current
 *	  directory, print a warning message to stderr and add:
 *		#define IMAKE_LOCAL_MACROS	"localmacros"
 *		#include IMAKE_LOCAL_MACROS
 *   5. Start up cpp and provide it with this file.
 *	Note that the define for INCLUDE_IMAKEFILE is intended for
 *	use in the template file.  This implies that the imake is
 *	useless unless the template file contains at least the line
 *		#include INCLUDE_IMAKEFILE
 *   6. Gather the output from cpp, and clean it up, expanding @@ to
 *	newlines, stripping trailing white space, cpp control lines,
 *	and extra blank lines, and changing XCOMM to #.  This cleaned
 *	output is placed in a new file, default "Makefile", but can
 *	be specified with -s or -e options.
 *   7. Optionally start up make on the resulting file.
 *
 * The design of the template makefile should therefore be:
 *	<set global macros like CFLAGS, etc.>
 *	<include machine dependent additions>
 *	#include INCLUDE_IMAKEFILE
 *	<add any global targets like 'clean' and long dependencies>
 */
#if defined(__FreeBSD__) || defined(__NetBSD__)
/* This needs to be before _POSIX_SOURCE gets defined */
# include <sys/param.h>
# include <sys/types.h>
# include <sys/sysctl.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include "Xosdefs.h"
#include <string.h>
#include <ctype.h>
#ifdef WIN32
# include "Xw32defs.h"
#endif
#if 0
#ifndef X_NOT_POSIX
# ifndef _POSIX_SOURCE
#  define _POSIX_SOURCE
# endif
#endif
#endif
#include <sys/types.h>
#include <fcntl.h>
#ifdef X_NOT_POSIX
# ifndef WIN32
#  include <sys/file.h>
# endif
#else
# include <unistd.h>
#endif
#ifdef ISC
# include <unistd.h>
#endif
#if defined(X_NOT_POSIX) || defined(_POSIX_SOURCE)
# include <signal.h>
#else
# define _POSIX_SOURCE
# include <signal.h>
# undef _POSIX_SOURCE
#endif
#if !defined(SIGCHLD) && defined(SIGCLD)
# define SIGCHLD		SIGCLD
#endif
#include <sys/stat.h>
#ifndef X_NOT_POSIX
# ifdef _POSIX_SOURCE
#  ifdef SCO325
#   include <sys/procset.h>
#   include <sys/siginfo.h>
#  endif
#  include <sys/wait.h>
# else
#  define _POSIX_SOURCE
#  include <sys/wait.h>
#  undef _POSIX_SOURCE
# endif
# define waitCode(w)	WEXITSTATUS(w)
# define waitSig(w)	WTERMSIG(w)
typedef int		waitType;
#else /* X_NOT_POSIX */
# ifdef SYSV
#  define waitCode(w)	(((w) >> 8) & 0x7f)
#  define waitSig(w)	((w) & 0xff)
typedef int		waitType;
# else /* SYSV */
#  ifdef WIN32
#   include <process.h>
typedef int		waitType;
#  else
#   include <sys/wait.h>
#   define waitCode(w)	((w).w_T.w_Retcode)
#   define waitSig(w)	((w).w_T.w_Termsig)
typedef union wait	waitType;
#  endif
# endif
# ifndef WIFSIGNALED
#  define WIFSIGNALED(w) waitSig(w)
# endif
# ifndef WIFEXITED
#  define WIFEXITED(w) waitCode(w)
# endif
#endif /* X_NOT_POSIX */
# include <stdlib.h>
#if defined(macII) && !defined(__STDC__)  /* stdlib.h fails to define these */
char *malloc(), *realloc();
#endif /* macII */
#include <errno.h>
#ifdef __minix_vmd
#define USE_FREOPEN		1
#endif

#if !((defined(sun) && !defined(SVR4)) || defined(macII))
#define USE_STRERROR		1
#endif
#ifndef WIN32
#include <sys/utsname.h>
#else
#include <windows.h>
#endif
#ifndef SYS_NMLN
# ifdef _SYS_NMLN
#  define SYS_NMLN _SYS_NMLN
# else
#  define SYS_NMLN 257
# endif
#endif
#if defined(linux) || defined(__GNU__)
#include <limits.h>
#include <stdio.h>
#endif
#ifdef __QNX__
#include <unix.h>
#endif

/*
 * This define of strerror is copied from (and should be identical to)
 * Xos.h, which we don't want to include here for bootstrapping reasons.
 */
#ifndef USE_STRERROR
# ifndef strerror
extern char *sys_errlist[];
extern int sys_nerr;
#  define strerror(n) \
    (((n) >= 0 && (n) < sys_nerr) ? sys_errlist[n] : "unknown error")
# endif
#endif

#if defined(__NetBSD__)		/* see code clock in init() below */
#include <sys/utsname.h>
#endif

#if !(defined(Lynx) || defined(__Lynx__) || (defined(SVR4) && !defined(sun))) && !defined (__CYGWIN__)
#define HAS_MKSTEMP
#endif

typedef unsigned char boolean;
#define TRUE		1
#define FALSE		0

# include "imakemdep.h"
#ifdef CROSSCOMPILE
# include "imakemdep_cpp.h"
#endif

#if defined CROSSCOMPILE || defined FIXUP_CPP_WHITESPACE
int InRule = FALSE;
#endif
#if defined CROSSCOMPILE || defined INLINE_SYNTAX
int InInline = 0;
#endif
#if defined CROSSCOMPILE || defined MAGIC_MAKE_VARS
int xvariable = 0;
int xvariables[10];
#endif

#ifndef PATH_MAX
#define PATH_MAX 1024
#endif

/*
 * Some versions of cpp reduce all tabs in macro expansion to a single
 * space.  In addition, the escaped newline may be replaced with a
 * space instead of being deleted.  Blech.
 */
void KludgeOutputLine(char **), KludgeResetRule(void);

#ifndef CROSSCOMPILE
# ifdef USE_CC_E
#  ifndef DEFAULT_CC
#   define DEFAULT_CC "cc"
#  endif
# else
#  ifndef DEFAULT_CPP
#   ifdef CPP_PROGRAM
#    define DEFAULT_CPP CPP_PROGRAM
#   else
#    define DEFAULT_CPP "/lib/cpp"
#   endif
#  endif
# endif
#endif

char *cpp = NULL;

char	*tmpMakefile = "/tmp/Imf.XXXXXX";
char	*tmpImakefile = "/tmp/IIf.XXXXXX";
char	*make_argv[ ARGUMENTS ] = {
#ifdef WIN32
    "nmake"
#else
    "make"
#endif
};

int	make_argindex;
int	cpp_argindex;
char	*Imakefile = NULL;
char	*Makefile = "Makefile";
char	*Template = "Imake.tmpl";
char	*ImakefileC = "Imakefile.c";
boolean haveImakefileC = FALSE;
char	*cleanedImakefile = NULL;
char	*program;
char	*FindImakefile(char *Imakefile);
char	*ReadLine(FILE *tmpfd, char *tmpfname);
char	*CleanCppInput(char *imakefile);
char	*Strdup(char *cp);
char	*Emalloc(int size);
void	LogFatalI(char *s, int i), LogFatal(char *x0, char *x1),
	LogMsg(char *x0, char *x1);

void	showit(FILE *fd);
void	wrapup(void);
void	init(void);
void	AddMakeArg(char *arg);
void	AddCppArg(char *arg);
#ifdef CROSSCOMPILE
char	*CrossCompileCPP(void);
#endif
void	SetOpts(int argc, char **argv);
void	CheckImakefileC(char *masterc);
void	cppit(char *imakefile, char *template, char *masterc,
	      FILE *outfd, char *outfname);
void	makeit(void);
void	CleanCppOutput(FILE *tmpfd, char *tmpfname);
boolean isempty(char *line);
void	writetmpfile(FILE *fd, char *buf, int cnt, char *fname);
#ifdef SIGNALRETURNSINT
int	catch(int sig);
#else
void	catch(int sig);
#endif
void	showargs(char **argv);
boolean optional_include(FILE *inFile, char *defsym, char *fname);
void	  doit(FILE *outfd, char *cmd, char **argv);
boolean define_os_defaults(FILE *inFile);
#ifdef CROSSCOMPILE
static void get_cross_compile_dir(FILE *inFile);
#endif
#ifdef CROSSCOMPILEDIR
char *CrossCompileDir = CROSSCOMPILEDIR;
#else
char *CrossCompileDir = "";
#endif
boolean CrossCompiling = FALSE;



boolean verbose = FALSE;
boolean show = TRUE;

int
main(int argc, char *argv[])
{
	FILE	*tmpfd = NULL;
	char	makeMacro[ BUFSIZ ];
	char	makefileMacro[ BUFSIZ ];
	int	lenCrossCompileDir = 0;

	program = argv[0];
	init();

	lenCrossCompileDir = strlen(CrossCompileDir);
	if (lenCrossCompileDir) {
	    if (lenCrossCompileDir > (PATH_MAX - 20))
	      LogFatal("Cross compile directory path too long %s\n",
		       CrossCompileDir);
	    else
		CrossCompiling = TRUE;
	}

	SetOpts(argc, argv);
	Imakefile = FindImakefile(Imakefile);
	CheckImakefileC(ImakefileC);
	if (Makefile) {
		tmpMakefile = Makefile;
		if ((tmpfd = fopen(tmpMakefile, "w+")) == NULL)
		   LogFatal("Cannot create temporary file %s.", tmpMakefile);
	} else {
#ifdef HAS_MKSTEMP
		int fd;
#endif
		tmpMakefile = Strdup(tmpMakefile);
#ifndef HAS_MKSTEMP
		if (mktemp(tmpMakefile) == NULL ||
		    (tmpfd = fopen(tmpMakefile, "w+")) == NULL) {
		   LogFatal("Cannot create temporary file %s.", tmpMakefile);
		}
#else
		fd = mkstemp(tmpMakefile);
		if (fd == -1 || (tmpfd = fdopen(fd, "w+")) == NULL) {
		   if (fd != -1) {
		      unlink(tmpMakefile); close(fd);
		   }
		   LogFatal("Cannot create temporary file %s.", tmpMakefile);
		}
#endif
	}
	AddMakeArg("-f");
	AddMakeArg( tmpMakefile );
	sprintf(makeMacro, "MAKE=%s", program);
	AddMakeArg( makeMacro );
	sprintf(makefileMacro, "MAKEFILE=%s", Imakefile);
	AddMakeArg( makefileMacro );

	cleanedImakefile = CleanCppInput(Imakefile);
	cppit(cleanedImakefile, Template, ImakefileC, tmpfd, tmpMakefile);

	if (show) {
		if (Makefile == NULL)
			showit(tmpfd);
	} else
		makeit();
	wrapup();
	exit(0);
}

void
showit(FILE *fd)
{
	char	buf[ BUFSIZ ];
	int	red;

	fseek(fd, 0, 0);
	while ((red = fread(buf, 1, BUFSIZ, fd)) > 0)
		writetmpfile(stdout, buf, red, "stdout");
	if (red < 0)
	    LogFatal("Cannot read %s.", tmpMakefile);
}

void
wrapup(void)
{
	if (tmpMakefile != Makefile)
		unlink(tmpMakefile);
	if (cleanedImakefile && cleanedImakefile != Imakefile)
		unlink(cleanedImakefile);
	if (haveImakefileC)
		unlink(ImakefileC);
}

#ifdef SIGNALRETURNSINT
int
#else
void
#endif
catch(int sig)
{
	errno = 0;
	LogFatalI("Signal %d.", sig);
}

/*
 * Initialize some variables.
 */
void
init(void)
{
	register char	*p;

	make_argindex=0;
	while (make_argv[ make_argindex ] != NULL)
		make_argindex++;
	cpp_argindex = 0;
	while (cpp_argv[ cpp_argindex ] != NULL)
		cpp_argindex++;

#if defined CROSSCOMPILE
	if (sys == netBSD)
	  if (CrossCompiling) {
	    LogFatal("fix imake to do crosscompiling for NetBSD\n","");
	  } else
#endif
#if defined(__NetBSD__) || defined CROSSCOMPILE
	{
		struct utsname uts;
		static char argument[512];

		/*
		 * Sharable imake configurations require a
		 * machine identifier.
		 */
		if (uname(&uts) != 0)
			LogFatal("uname(3) failed; can't tell what %s",
			    "kind of machine you have.");

		memset(argument, 0, sizeof(argument));
		(void)snprintf(argument, sizeof(argument) - 1,
		    "-D__%s__", uts.machine);

		AddCppArg(argument);
	}
#endif /* __NetBSD__ */

	/*
	 * See if the standard include directory is different than
	 * the default.  Or if cpp is not the default.  Or if the make
	 * found by the PATH variable is not the default.
	 */
	if ((p = getenv("IMAKEINCLUDE"))) {
		if (*p != '-' || *(p+1) != 'I')
			LogFatal("Environment var IMAKEINCLUDE %s",
				"must begin with -I");
		AddCppArg(p);
		for (; *p; p++)
			if (*p == ' ') {
				*p++ = '\0';
				AddCppArg(p);
			}
	}
	if ((p = getenv("IMAKECPP")))
		cpp = p;
	if ((p = getenv("IMAKEMAKE")))
		make_argv[0] = p;

	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
		signal(SIGINT, catch);
#ifdef SIGCHLD
	signal(SIGCHLD, SIG_DFL);
#endif
}

void
AddMakeArg(char *arg)
{
	errno = 0;
	if (make_argindex >= ARGUMENTS-1)
		LogFatal("Out of internal storage.", "");
	make_argv[ make_argindex++ ] = arg;
	make_argv[ make_argindex ] = NULL;
}

void
AddCppArg(char *arg)
{
	errno = 0;
	if (cpp_argindex >= ARGUMENTS-1)
		LogFatal("Out of internal storage.", "");
	cpp_argv[ cpp_argindex++ ] = arg;
	cpp_argv[ cpp_argindex ] = NULL;
}

void
SetOpts(int argc, char **argv)
{

	errno = 0;
	/*
	 * Now gather the arguments for make
	 */
	for(argc--, argv++; argc; argc--, argv++) {
	    /*
	     * We intercept these flags.
	     */
	    if (argv[0][0] == '-') {
		if (argv[0][1] == 'D') {
		    AddCppArg(argv[0]);
		} else if (argv[0][1] == 'I') {
		    AddCppArg(argv[0]);
		} else if (argv[0][1] == 'U') {
		    AddCppArg(argv[0]);
		} else if (argv[0][1] == 'W') {
		    AddCppArg(argv[0]);
		} else if (argv[0][1] == 'f') {
		    if (argv[0][2])
			Imakefile = argv[0]+2;
		    else {
			argc--, argv++;
			if (! argc)
			    LogFatal("No description arg after -f flag", "");
			Imakefile = argv[0];
		    }
		} else if (argv[0][1] == 's') {
		    if (argv[0][2])
			Makefile = ((argv[0][2] == '-') && !argv[0][3]) ?
			    NULL : argv[0]+2;
		    else {
			argc--, argv++;
			if (!argc)
			    LogFatal("No description arg after -s flag", "");
			Makefile = ((argv[0][0] == '-') && !argv[0][1]) ?
			    NULL : argv[0];
		    }
		    show = TRUE;
		} else if (argv[0][1] == 'e') {
		   Makefile = (argv[0][2] ? argv[0]+2 : NULL);
		   show = FALSE;
		} else if (argv[0][1] == 'T') {
		    if (argv[0][2])
			Template = argv[0]+2;
		    else {
			argc--, argv++;
			if (! argc)
			    LogFatal("No description arg after -T flag", "");
			Template = argv[0];
		    }
		} else if (argv[0][1] == 'C') {
		    if (argv[0][2])
			ImakefileC = argv[0]+2;
		    else {
			argc--, argv++;
			if (! argc)
			    LogFatal("No imakeCfile arg after -C flag", "");
			ImakefileC = argv[0];
		    }
		} else if (argv[0][1] == 'v') {
		    verbose = TRUE;
		} else
		    AddMakeArg(argv[0]);
	    } else
		AddMakeArg(argv[0]);
	}

#ifndef CROSSCOMPILE
# ifdef USE_CC_E
	    if (!cpp)
	    {
		AddCppArg("-E");
#ifdef __GNUC__
		if (verbose)
		    AddCppArg("-v");
#endif
		cpp = DEFAULT_CC;
	    }
# else
	    if (!cpp)
		cpp = DEFAULT_CPP;
# endif
#else
	    if (!cpp)
		cpp = CrossCompileCPP();
#endif

	cpp_argv[0] = cpp;
	AddCppArg(ImakefileC);
}

char *
FindImakefile(char *Imakefile)
{
	if (Imakefile) {
		if (access(Imakefile, R_OK) < 0)
			LogFatal("Cannot find %s.", Imakefile);
	} else {
		if (access("Imakefile", R_OK) < 0) {
			if (access("imakefile", R_OK) < 0)
				LogFatal("No description file.", "");
			else
				Imakefile = "imakefile";
		} else
			Imakefile = "Imakefile";
	}
	return(Imakefile);
}

void
LogFatalI(char *s, int i)
{
	/*NOSTRICT*/
	LogFatal(s, (char *)(long)i);
}

void
LogFatal(char *x0, char *x1)
{
	static boolean entered = FALSE;

	if (entered)
		return;
	entered = TRUE;

	LogMsg(x0, x1);
	fprintf(stderr, "  Stop.\n");
	wrapup();
	exit(1);
}

void
LogMsg(char *x0, char *x1)
{
	int error_number = errno;

	if (error_number) {
		fprintf(stderr, "%s: ", program);
		fprintf(stderr, "%s\n", strerror(error_number));
	}
	fprintf(stderr, "%s: ", program);
	fprintf(stderr, x0, x1);
	fprintf(stderr, "\n");
}

void
showargs(char **argv)
{
	for (; *argv; argv++)
		fprintf(stderr, "%s ", *argv);
	fprintf(stderr, "\n");
}

#define ImakefileCHeader "/* imake - temporary file */"

void
CheckImakefileC(char *masterc)
{
	char mkcbuf[1024];
	FILE *inFile;

	if (access(masterc, F_OK) == 0) {
		inFile = fopen(masterc, "r");
		if (inFile == NULL)
			LogFatal("Refuse to overwrite: %s", masterc);
		if ((fgets(mkcbuf, sizeof(mkcbuf), inFile) &&
		     strncmp(mkcbuf, ImakefileCHeader,
			     sizeof(ImakefileCHeader)-1)))
		{
			fclose(inFile);
			LogFatal("Refuse to overwrite: %s", masterc);
		}
		fclose(inFile);
	}
}

#define LocalDefineFmt	"#define %s \"%s\"\n"
#define IncludeFmt	"#include %s\n"
#define ImakeDefSym	"INCLUDE_IMAKEFILE"
#define ImakeTmplSym	"IMAKE_TEMPLATE"
#define OverrideWarning "Warning: local file \"%s\" overrides global macros."

boolean
optional_include(FILE *inFile, char *defsym, char *fname)
{
	errno = 0;
	if (access(fname, R_OK) == 0) {
		LogMsg(OverrideWarning, fname);
		return (fprintf(inFile, LocalDefineFmt, defsym, fname) < 0 ||
			fprintf(inFile, IncludeFmt, defsym) < 0);
	}
	return FALSE;
}

void
doit(FILE *outfd, char *cmd, char **argv)
{
	int		pid;
	waitType	status;

	/*
	 * Fork and exec the command.
	 */
#ifdef WIN32
	if (outfd)
		dup2(fileno(outfd), 1);
	status = _spawnvp(_P_WAIT, cmd, argv);
	if (status < 0)
		LogFatal("Cannot spawn %s.", cmd);
	if (status > 0)
		LogFatalI("Exit code %d.", status);
#else
	pid = fork();
	if (pid < 0)
		LogFatal("Cannot fork.", "");
	if (pid) {	/* parent... simply wait */
		while (wait(&status) > 0) {
			errno = 0;
			if (WIFSIGNALED(status))
				LogFatalI("Signal %d.", waitSig(status));
			if (WIFEXITED(status) && waitCode(status))
				LogFatalI("Exit code %d.", waitCode(status));
		}
	}
	else {	/* child... dup and exec cmd */
		if (verbose)
			showargs(argv);
		if (outfd)
			dup2(fileno(outfd), 1);
		execvp(cmd, argv);
		LogFatal("Cannot exec %s.", cmd);
	}
#endif
}

#if !defined WIN32
static void
parse_utsname(struct utsname *name, char *fmt, char *result, char *msg)
{
  char buf[SYS_NMLN * 5 + 1];
  char *ptr = buf;
  int arg;

  if (!name)
      LogFatal(msg,fmt);

  /* Assemble all the pieces into a buffer. */
  for (arg = 0; fmt[arg] != ' '; arg++)
    {
      /* Our buffer is only guaranteed to hold 5 arguments. */
      if (arg >= 5)
	LogFatal(msg, fmt);

      switch (fmt[arg])
	{
	case 's':
	  if (arg > 0)
	    *ptr++ = ' ';
	  strcpy(ptr, name->sysname);
	  ptr += strlen(ptr);
	  break;

	case 'n':
	  if (arg > 0)
	    *ptr++ = ' ';
	  strcpy(ptr, name->nodename);
	  ptr += strlen(ptr);
	  break;

	case 'r':
	  if (arg > 0)
	    *ptr++ = ' ';
	  strcpy(ptr, name->release);
	  ptr += strlen(ptr);
	  break;

	case 'v':
	  if (arg > 0)
	    *ptr++ = ' ';
	  strcpy(ptr, name->version);
	  ptr += strlen(ptr);
	  break;

	case 'm':
	  if (arg > 0)
	    *ptr++ = ' ';
	  strcpy(ptr, name->machine);
	  ptr += strlen(ptr);
	  break;

	default:
	  LogFatal(msg, fmt);
	}
    }

  /* Just in case... */
  if (strlen(buf) >= sizeof(buf))
    LogFatal("Buffer overflow parsing uname.", "");

  /* Parse the buffer.  The sscanf() return value is rarely correct. */
  *result = '\0';
  (void) sscanf(buf, fmt + arg + 1, result);
}

/* Trim leading 0's and periods from version names.  The 0's cause
   the number to be interpreted as octal numbers.  Some version strings
   have the potential for different numbers of .'s in them.
 */

static char *
trim_version(char *p)
{

	if (p != 0 && *p != '\0')
	{
		while ((*p == '0' || *p == '.') && *(p + 1) != '\0')
			++p;
	}
	return (p);
}
#endif

#if defined linux
const char *libc_c=
"#include <stdio.h>\n"
"#include <ctype.h>\n"
"\n"
"#if 1\n"
"#pragma weak gnu_get_libc_version\n"
"#pragma weak __libc_version\n"
"#pragma weak __linux_C_lib_version\n"
"#endif\n"
"\n"
"extern const char * gnu_get_libc_version (void);\n"
"extern const char * __linux_C_lib_version;\n"
"extern const char __libc_version [];\n"
"\n"
"int\n"
"main ()\n"
"{\n"
"  int libcmajor = 0, libcminor = 0, libcteeny = 0;\n"
"  const char * ptr = NULL;\n"
"  int glibcmajor = 0;\n"
"\n"
"  if (gnu_get_libc_version != 0)\n"
"  {\n"
"    ptr = gnu_get_libc_version ();\n"
"    glibcmajor = 4;\n"
"  }\n"
"  else if (&__libc_version != 0)\n"
"  {\n"
"    ptr = __libc_version;\n"
"    glibcmajor = 4;\n"
"  }\n"
"  else if (&__linux_C_lib_version != 0)\n"
"  {\n"
"    ptr = __linux_C_lib_version;\n"
"  }\n"
"  else\n"
"  {\n"
"    libcmajor = 0; libcminor = 0; libcteeny = 0;\n"
"  }\n"
"\n"
"  if (ptr)\n"
"  {\n"
"    while (!isdigit (*ptr))\n"
"      ptr++;\n"
"\n"
"    sscanf (ptr, \"%d.%d.%d\", &libcmajor, &libcminor, &libcteeny);\n"
"    libcmajor += glibcmajor;\n"
"  }\n"
"\n"
"  printf(\"#define DefaultLinuxCLibMajorVersion %d\\n\", libcmajor);\n"
"  printf(\"#define DefaultLinuxCLibMinorVersion %d\\n\", libcminor);\n"
"  printf(\"#define DefaultLinuxCLibTeenyVersion %d\\n\", libcteeny);\n"
"\n"
"  return 0;\n"
"}\n"
;

static void
get_libc_version(FILE *inFile)
{
  char aout[] = "/tmp/imakeXXXXXX";
  FILE *fp;
  const char *format = "%s -o %s -x c -";
  char *cc;
  int len;
  char *command;

  /* Pre-create temp file safely */
  {
    /* Linux + ELF has mkstemp() */
    int tmpfd;
    if ((tmpfd = mkstemp(aout)) == -1) {
      perror("mkstemp");
      abort();
    }
    close(tmpfd);
  }
  cc = getenv ("CC");
  if (cc == NULL)
    cc = "gcc";
  len = strlen (aout) + strlen (format) + strlen (cc);
  if (len < 128) len = 128;
  command = alloca (len);

  if (snprintf (command , len, format, cc, aout) == len)
    abort ();

  fp = popen (command, "w");
  if (fp == NULL || fprintf (fp, "%s\n", libc_c) < 0
      || pclose (fp) != 0)
    abort ();

  fp = popen (aout, "r");
  if (fp == NULL)
    abort ();

  while (fgets (command, len, fp))
    fprintf (inFile, command);

  len = pclose (fp);
  remove (aout);
  if (len)
    abort ();
}
#endif

#if defined(__OpenBSD__) 
static void
get_stackprotector(FILE *inFile)
{
  FILE *fp;
  char *cc;
  char command[1024], buf[1024];
  
  cc = getenv("CC");
  if (cc == NULL) {
    cc = "cc";
  }
  snprintf(command, sizeof(command), "%s -v 2>&1", cc);
  fp = popen(command, "r");
  if (fp == NULL) 
    abort();
  while (fgets(buf, sizeof(buf), fp)) {
    if (strstr(buf, "propolice") != NULL) {
      fprintf(inFile, "#define HasGccStackProtector YES\n");
      break;
    }
  }
  if (pclose(fp)) 
    abort();
}
#endif
	

#if defined CROSSCOMPILE || defined linux
static void
get_distrib(FILE *inFile)
{
  struct stat sb;

  static char* suse = "/etc/SuSE-release";
  static char* redhat = "/etc/redhat-release";
  static char* debian = "/etc/debian_version";

  fprintf (inFile, "%s\n", "#define LinuxUnknown    0");
  fprintf (inFile, "%s\n", "#define LinuxSuSE       1");
  fprintf (inFile, "%s\n", "#define LinuxCaldera    2");
  fprintf (inFile, "%s\n", "#define LinuxCraftworks 3");
  fprintf (inFile, "%s\n", "#define LinuxDebian     4");
  fprintf (inFile, "%s\n", "#define LinuxInfoMagic  5");
  fprintf (inFile, "%s\n", "#define LinuxKheops     6");
  fprintf (inFile, "%s\n", "#define LinuxPro        7");
  fprintf (inFile, "%s\n", "#define LinuxRedHat     8");
  fprintf (inFile, "%s\n", "#define LinuxSlackware  9");
  fprintf (inFile, "%s\n", "#define LinuxTurbo      10");
  fprintf (inFile, "%s\n", "#define LinuxWare       11");
  fprintf (inFile, "%s\n", "#define LinuxYggdrasil  12");

#ifdef CROSSCOMPILE
  if (CrossCompiling) {
      fprintf (inFile, "%s\n",
	       "#define DefaultLinuxDistribution LinuxUnknown");
      fprintf (inFile, "%s\n", "#define DefaultLinuxDistName Unknown");
      return;
  }
#endif
  if (lstat (suse, &sb) == 0) {
    fprintf (inFile, "%s\n", "#define DefaultLinuxDistribution LinuxSuSE");
    fprintf (inFile, "%s\n", "#define DefaultLinuxDistName SuSE");
    return;
  }
  if (lstat (redhat, &sb) == 0) {
    fprintf (inFile, "%s\n", "#define DefaultLinuxDistribution LinuxRedHat");
    fprintf (inFile, "%s\n", "#define DefaultLinuxDistName RedHat");
    return;
  }
  if (lstat (debian, &sb) == 0) {
    fprintf (inFile, "%s\n", "#define DefaultLinuxDistribution LinuxDebian");
    fprintf (inFile, "%s\n", "#define DefaultLinuxDistName Debian");
    /* You could also try to get the version of the Debian distrib by looking
     * at the content of /etc/debian_version */
    return;
  }
  /* what's the definitive way to tell what any particular distribution is? */

  fprintf (inFile, "%s\n", "#define DefaultLinuxDistribution LinuxUnknown");
  fprintf (inFile, "%s\n", "#define DefaultLinuxDistName Unknown");
  /* would like to know what version of the distribution it is */
}

static void
get_ld_version(FILE *inFile)
{
  FILE* ldprog;
  signed char c;
  int ldmajor, ldminor;
  const char *ld = "ld -v";

#ifdef CROSSCOMPILE
  if (CrossCompiling) {
      char cmd[PATH_MAX];
      strcpy (cmd, CrossCompileDir);
      strcat (cmd,"/");
      strcat (cmd,ld);
      ldprog = popen (cmd, "r");
  } else
#endif
      ldprog = popen (ld, "r");

  if (ldprog) {
    do {
      c = fgetc (ldprog);
    } while (c != EOF && !isdigit (c));
    ungetc (c, ldprog);
    (void) fscanf (ldprog, "%d.%d", &ldmajor, &ldminor);
    /* Start conversion to a more rational number */
    if ((ldmajor > 2) || ((ldmajor == 2) && (ldminor > 9)))
	ldmajor *= 100;
    else
	ldmajor *= 10;

    fprintf(inFile, "#define DefaultLinuxBinUtilsMajorVersion %d\n",
	    ldmajor + ldminor);
    pclose (ldprog);
  }
}
#endif

#if defined __FreeBSD__
static void
get_binary_format(FILE *inFile)
{
  int mib[2];
  size_t len;
  int osrel = 0;
  FILE *objprog = NULL;
  int iself = 0;
  char buf[10];
  char cmd[PATH_MAX];

  mib[0] = CTL_KERN;
  mib[1] = KERN_OSRELDATE;
  len = sizeof(osrel);
  sysctl(mib, 2, &osrel, &len, NULL, 0);
  if (CrossCompiling) {
      strcpy (cmd, CrossCompileDir);
      strcat (cmd, "/");
      strcat (cmd,"objformat");
  } else
      strcpy (cmd, "objformat");

  if (osrel >= 300004 &&
      (objprog = popen(cmd, "r")) != NULL &&
      fgets(buf, sizeof(buf), objprog) != NULL &&
      strncmp(buf, "elf", 3) == 0)
    iself = 1;
  if (objprog)
    pclose(objprog);

  fprintf(inFile, "#define DefaultToElfFormat %s\n", iself ? "YES" : "NO");
}
#endif

#if defined(sun) && defined(__SVR4)
static void
get_sun_compiler_versions (FILE *inFile)
{
  char buf[PATH_MAX];
  char cmd[PATH_MAX];
  static char* sunpro_cc = "/opt/SUNWspro/bin/cc";
  static char* sunpro_CC = "/opt/SUNWspro/bin/CC";
  int cmajor, cminor;
  char* vptr;
  struct stat sb;
  FILE* ccproc;

#if defined CROSSCOMPILE
  if (CrossCompiling) {
      int len = strlen(CrossCompileDir);
      len += 3;
      sunpro_cc = (char *) malloc(len);
      sunpro_CC = (char *) malloc(len);
      strcpy(sunpro_cc,CrossCompileDir);
      strcpy(sunpro_CC,CrossCompileDir);
      strcat(sunpro_cc,"/cc");
      strcat(sunpro_CC,"/CC");
  }
#endif
  if (lstat (sunpro_cc, &sb) == 0) {
    strcpy (cmd, sunpro_cc);
    strcat (cmd, " -V 2>&1");
    if ((ccproc = popen (cmd, "r")) != NULL) {
      if (fgets (buf, PATH_MAX, ccproc) != NULL) {
	vptr = strrchr (buf, 'C');
	for (; !isdigit(*vptr); vptr++);
	(void) sscanf (vptr, "%d.%d", &cmajor, &cminor);
	fprintf (inFile,
		 "#define DefaultSunProCCompilerMajorVersion %d\n",
		 cmajor);
	fprintf (inFile,
		 "#define DefaultSunProCCompilerMinorVersion %d\n",
		 cminor);
      }
      while (fgets (buf, PATH_MAX, ccproc) != NULL) {};
      pclose (ccproc);
    }
  }
  if (lstat (sunpro_CC, &sb) == 0) {
    strcpy (cmd, sunpro_CC);
    strcat (cmd, " -V 2>&1");
    if ((ccproc = popen (cmd, "r")) != NULL) {
      if (fgets (buf, PATH_MAX, ccproc) != NULL) {
	vptr = strrchr (buf, 'C');
	for (; !isdigit(*vptr); vptr++);
	(void) sscanf (vptr, "%d.%d", &cmajor, &cminor);
	fprintf (inFile,
		 "#define DefaultSunProCplusplusCompilerMajorVersion %d\n",
		 cmajor);
	fprintf (inFile,
		 "#define DefaultSunProCplusplusCompilerMinorVersion %d\n",
		 cminor);
      }
      while (fgets (buf, PATH_MAX, ccproc) != NULL) {};
      pclose (ccproc);
    }
  }
}
#endif

#if defined CROSSCOMPILE || defined  __GNUC__
static void
get_gcc_version(FILE *inFile, char *name)
{
    fprintf (inFile, "#define HasGcc 1\n");
#ifdef CROSSCOMPILE
    if (CrossCompiling)
    {
	if (gnu_c > 1) {
	    fprintf (inFile, "#define HasGcc2 1\n");
	    if (gnu_c > 2)
		fprintf (inFile, "#define HasGcc3 1\n");
	}
	fprintf (inFile, "#define GccMajorVersion %d\n", gnu_c);
	fprintf (inFile, "#define GccMinorVersion %d\n", gnu_c_minor);
    } else
#endif
    {
#if __GNUC__ > 1
	fprintf (inFile, "#define HasGcc2 1\n");
# if __GNUC__ > 2
	fprintf (inFile, "#define HasGcc3 1\n");
# endif
#endif
	fprintf (inFile, "#define GccMajorVersion %d\n", __GNUC__);
	fprintf (inFile, "#define GccMinorVersion %d\n", __GNUC_MINOR__);
    }
#if defined(HAS_MERGE_CONSTANTS)
    fprintf (inFile, "#define HasGccMergeConstants %d\n", HAS_MERGE_CONSTANTS);
#endif
}
#endif

static boolean
get_gcc(char *cmd)
{
  struct stat sb;
    static char* gcc_path[] = {
# if defined(linux) || \
     defined(__NetBSD__) || \
     defined(__OpenBSD__) || \
     defined(__FreeBSD__) || \
     defined(__APPLE__) || \
     defined(__GNU__)
	"/usr/bin/cc",	/* for Linux PostIncDir */
# endif
	"/usr/local/bin/gcc",
	"/opt/gnu/bin/gcc",
	"/usr/pkg/bin/gcc"
    };

#ifdef CROSSCOMPILE
    static char* cross_cc_name[] = {
	"cc",
	"gcc"
    };

    if (CrossCompiling) {
	int i;
	for (i = 0; i < sizeof (cross_cc_name) / sizeof cross_cc_name[0]; i++){
	    strcpy (cmd, CrossCompileDir);
	    strcat (cmd, "/");
	    strcat (cmd, cross_cc_name[i]);
	    if (lstat (cmd, &sb) == 0) {
		return TRUE;
		break;
	    }
	}
    } else
#endif
      {
	int i;
	for (i = 0; i < sizeof (gcc_path) / sizeof gcc_path[0]; i++) {
	    if (lstat (gcc_path[i], &sb) == 0) {
		strcpy (cmd, gcc_path[i]);
		return TRUE;
	    }
	}
      }
    return FALSE;
}

#if defined CROSSCOMPILE || !defined __UNIXOS2__
static void
get_gcc_incdir(FILE *inFile, char* name)
{
  FILE* gccproc;
  char buf[PATH_MAX];
  char cmd[PATH_MAX];
  char* ptr;

  strcpy(cmd,name);

  buf[0] = '\0';
  strcat (cmd, " --print-libgcc-file-name");
  if ((gccproc = popen (cmd, "r")) != NULL) {
      if (fgets (buf, PATH_MAX, gccproc) != NULL) {
	  ptr = strstr (buf, "libgcc.a");
	  if (ptr) strcpy (ptr, "include");
      }
      (void) pclose (gccproc);
  }

  if (buf[0])
      fprintf (inFile, "#define DefaultGccIncludeDir \"%s\"\n", buf);
}
#endif

boolean
define_os_defaults(FILE *inFile)
{
#if defined CROSSCOMPILE || ( !defined(WIN32) && !defined(__UNIXOS2__) )
#ifdef CROSSCOMPILE
  if ((sys != win32) && (sys != emx))
#endif
    {
# if (defined(DEFAULT_OS_NAME) || defined(DEFAULT_OS_MAJOR_REV) || \
     defined(DEFAULT_OS_MINOR_REV) || defined(DEFAULT_OS_TEENY_REV))
	struct utsname *name = NULL;
	struct utsname uts_name;
	char buf[SYS_NMLN * 5 + 1];

	/* Obtain the system information. */
#ifdef CROSSCOMPILE
      if (!CrossCompiling)
#endif
      {
	  if (uname(&uts_name) < 0)
	      LogFatal("Cannot invoke uname", "");
	  else
	      name = &uts_name;
      }
#if defined CROSSCOMPILE && defined linux
      else {
	  strncpy(uts_name.sysname,cross_uts_sysname,SYS_NMLN);
	  strncpy(uts_name.release,cross_uts_release,SYS_NMLN);
	  strncpy(uts_name.version,cross_uts_version,SYS_NMLN);
	  strncpy(uts_name.machine,cross_uts_machine,SYS_NMLN);
	  name = &uts_name;
      }
#endif

#  if defined DEFAULT_OS_NAME
#   if defined CROSSCOMPILE
      if (!CrossCompiling)
#   endif
	{
	  parse_utsname(name, DEFAULT_OS_NAME, buf,
			"Bad DEFAULT_OS_NAME syntax %s");
#   ifdef DEFAULT_OS_NAME_FROB
	  DEFAULT_OS_NAME_FROB(buf, sizeof buf);
#   endif
	  if (buf[0] != '\0')
	    fprintf(inFile, "#define DefaultOSName %s\n", buf);
	}
#  endif

#  if defined CROSSCOMPILE
	if (CrossCompiling && defaultOsName) {
	  parse_utsname(name, defaultOsName, buf,
			"Bad DEFAULT_OS_NAME syntax %s");
	  if (defaultOsNameFrob)
	    defaultOsNameFrob(buf, sizeof buf);
	  if (buf[0] != '\0')
	    fprintf(inFile, "#define DefaultOSName %s\n", buf);
	}
#  endif

#  ifdef DEFAULT_OS_MAJOR_REV
#   if defined CROSSCOMPILE
	if (!CrossCompiling)
#   endif
	  {
	    parse_utsname(name, DEFAULT_OS_MAJOR_REV, buf,
			  "Bad DEFAULT_OS_MAJOR_REV syntax %s");
#   ifdef DEFAULT_OS_MAJOR_REV_FROB
	    DEFAULT_OS_MAJOR_REV_FROB(buf, sizeof buf);
#   endif
	    fprintf(inFile, "#define DefaultOSMajorVersion %s\n",
		    *buf ? trim_version(buf) : "0");
	  }
#  endif

#  if defined CROSSCOMPILE
	if (CrossCompiling && defaultOsMajorRev) {
	  parse_utsname(name, defaultOsMajorRev, buf,
			"Bad defaultOsMajorRev syntax %s");
	  if (defaultOsMajorRevFrob)
	    defaultOsMajorRevFrob(buf, sizeof buf);
	  fprintf(inFile, "#define DefaultOSMajorVersion %s\n",
		  *buf ? trim_version(buf) : "0");
	}
#  endif

#  ifdef DEFAULT_OS_MINOR_REV
#   if defined CROSSCOMPILE
	if (!CrossCompiling)
#   endif
	  {
	    parse_utsname(name, DEFAULT_OS_MINOR_REV, buf,
			  "Bad DEFAULT_OS_MINOR_REV syntax %s");
#   ifdef DEFAULT_OS_MINOR_REV_FROB
	    DEFAULT_OS_MINOR_REV_FROB(buf, sizeof buf);
#   endif
	    fprintf(inFile, "#define DefaultOSMinorVersion %s\n",
		    *buf ? trim_version(buf) : "0");
	  }
#  endif

#  if defined CROSSCOMPILE
	if (CrossCompiling && defaultOsMinorRev) {
	  parse_utsname(name, defaultOsMinorRev, buf,
			"Bad defaultOsMinorRev syntax %s");
	  if (defaultOsMinorRevFrob)
	    defaultOsMinorRevFrob(buf, sizeof buf);
	  fprintf(inFile, "#define DefaultOSMinorVersion %s\n",
		  *buf ? trim_version(buf) : "0");
	}
#  endif

#  ifdef DEFAULT_OS_TEENY_REV
#   if defined CROSSCOMPILE
	if (!CrossCompiling)
#   endif
	  {
	    parse_utsname(name, DEFAULT_OS_TEENY_REV, buf,
			  "Bad DEFAULT_OS_TEENY_REV syntax %s");
#   ifdef DEFAULT_OS_TEENY_REV_FROB
	    DEFAULT_OS_TEENY_REV_FROB(buf, sizeof buf);
#   endif
	    fprintf(inFile, "#define DefaultOSTeenyVersion %s\n",
		    *buf ? trim_version(buf) : "0");
	  }
#  endif

#  if defined CROSSCOMPILE
	if (CrossCompiling && defaultOsTeenyRev) {
	  parse_utsname(name, defaultOsTeenyRev, buf,
			"Bad defaultOsTeenyRev syntax %s");
	  if (defaultOsTeenyRevFrob)
	    defaultOsTeenyRevFrob(buf, sizeof buf);
	  fprintf(inFile, "#define DefaultOSTeenyVersion %s\n",
		  *buf ? trim_version(buf) : "0");
	}
#  endif

#  ifdef DEFAULT_MACHINE_ARCHITECTURE
#   if defined CROSSCOMPILE
	if (!CrossCompiling)
#   endif
	  {
	    parse_utsname(name, DEFAULT_MACHINE_ARCHITECTURE, buf,
			  "Bad DEFAULT_MACHINE_ARCHITECTURE %s");
	    fprintf(inFile, "#ifndef %s\n# define %s\n#endif\n", buf, buf);
	  }
#  endif

#  if defined CROSSCOMPILE
	if (CrossCompiling && defaultMachineArchitecture) {
	  parse_utsname(name, defaultMachineArchitecture, buf,
			"Bad defaultMachineArchitecture syntax %s");
	  fprintf(inFile, "#ifndef %s\n# define %s\n#endif\n", buf, buf);
	}
#  endif
# endif
# if defined CROSSCOMPILE
      if (CrossCompiling)
	get_cross_compile_dir(inFile);
      else
# endif
	  fprintf(inFile, "#define CrossCompiling NO\n");
# if defined CROSSCOMPILE
      if (CrossCompiling && sys == LinuX)
# endif
# if defined CROSSCOMPILE || defined linux
#  ifdef CROSSCOMPILE
	if (sys == LinuX)
#  endif
	  get_distrib (inFile);
# endif
# if defined linux
#  if defined CROSSCOMPILE
      if (!CrossCompiling)
#  endif
	  get_libc_version (inFile);
#  if defined CROSSCOMPILE
      else {
	  fprintf(inFile,"#define DefaultLinuxCLibMajorVersion %d\n",
		  glibc_major);
	  fprintf(inFile,"#define DefaultLinuxCLibMinorVersion %d\n",
		  glibc_minor);
	  fprintf(inFile,"#define DefaultLinuxCLibTeenyVersion 0\n");
      }
#  endif
# endif /* linux */
# if defined CROSSCOMPILE || defined linux
#  if defined CROSSCOMPILE
      if (sys == LinuX)
#  endif
	  get_ld_version(inFile);
# endif
# if defined (sun) && defined(SVR4)
      get_sun_compiler_versions (inFile);
# endif
# if defined CROSSCOMPILE || defined __GNUC__
#  if defined CROSSCOMPILE
      if (gnu_c)
#  endif
	{
	  char name[PATH_MAX];
	  if (get_gcc(name)) {
	      get_gcc_version (inFile,name);
#  if defined CROSSCOMPILE || !defined __UNIXOS2__
#   if defined CROSSCOMPILE
	      if (sys != emx)
#   endif
		  get_gcc_incdir(inFile,name);
#  endif
	  }
	}
# endif
# if defined __FreeBSD__
#  if defined CROSSCOMPILE
      if (sys == freeBSD)
#  endif
	  get_binary_format(inFile);
# endif
    }
#endif /* !WIN32 && !__UNIXOS2__*/
#if defined WIN32
# ifdef CROSSCOMPILE
  else if (sys == win32 && !CrossCompiling)
# endif
    {
      OSVERSIONINFO osvi;
      static char* os_names[] = { "Win32s", "Windows 95", "Windows NT" };

      memset(&osvi, 0, sizeof(OSVERSIONINFO));
      osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
      GetVersionEx (&osvi);

      fprintf (inFile, "#define DefaultOSName Microsoft %s\n",
	       os_names[osvi.dwPlatformId]);

      fprintf(inFile, "#define DefaultOSMajorVersion %d\n", osvi.dwMajorVersion);
      fprintf(inFile, "#define DefaultOSMinorVersion %d\n", osvi.dwMinorVersion);
      fprintf(inFile, "#define DefaultOSTeenyVersion %d\n",
	      osvi.dwBuildNumber & 0xFFFF);
    }
#endif /* WIN32 */
#ifdef CROSSCOMPILE
  else if (sys == emx)
#endif
#if defined CROSSCOMPILE || defined __UNIXOS2__
    {
      fprintf(inFile, "#define DefaultOSMajorVersion 4\n");
      fprintf(inFile, "#define DefaultOSMinorVersion 0\n");
      fprintf(inFile, "#define DefaultOSTeenyVersion 0\n");
    }
#endif /* EMX */
#if defined(__OpenBSD__)
  get_stackprotector(inFile);
#endif
  return FALSE;
}

void
cppit(char *imakefile, char *template, char *masterc,
      FILE *outfd, char *outfname)
{
	FILE	*inFile;

	haveImakefileC = TRUE;
	inFile = fopen(masterc, "w");
	if (inFile == NULL)
		LogFatal("Cannot open %s for output.", masterc);
	if (fprintf(inFile, "%s\n", ImakefileCHeader) < 0 ||
	    define_os_defaults(inFile) ||
	    optional_include(inFile, "IMAKE_LOCAL_DEFINES", "localdefines") ||
	    optional_include(inFile, "IMAKE_ADMIN_DEFINES", "admindefines") ||
	    fprintf(inFile, "#define %s <%s>\n", ImakeDefSym, imakefile) < 0 ||
	    fprintf(inFile, LocalDefineFmt, ImakeTmplSym, template) < 0 ||
	    fprintf(inFile, IncludeFmt, ImakeTmplSym) < 0 ||
	    optional_include(inFile, "IMAKE_ADMIN_MACROS", "adminmacros") ||
	    optional_include(inFile, "IMAKE_LOCAL_MACROS", "localmacros") ||
	    fflush(inFile) ||
	    fclose(inFile))
		LogFatal("Cannot write to %s.", masterc);
	/*
	 * Fork and exec cpp
	 */
	doit(outfd, cpp, cpp_argv);
	CleanCppOutput(outfd, outfname);
}

void
makeit(void)
{
	doit(NULL, make_argv[0], make_argv);
}

char *
CleanCppInput(char *imakefile)
{
	FILE	*outFile = NULL;
	FILE	*inFile;
	char	*buf,		/* buffer for file content */
		*pbuf,		/* walking pointer to buf */
		*punwritten,	/* pointer to unwritten portion of buf */
		*ptoken,	/* pointer to # token */
		*pend,		/* pointer to end of # token */
		savec;		/* temporary character holder */
	int	count;
	struct stat	st;

	/*
	 * grab the entire file.
	 */
	if (!(inFile = fopen(imakefile, "r")))
		LogFatal("Cannot open %s for input.", imakefile);
	if (fstat(fileno(inFile), &st) < 0)
		LogFatal("Cannot stat %s for size.", imakefile);
	buf = Emalloc((int)st.st_size+3);
	count = fread(buf + 2, 1, st.st_size, inFile);
	if (count == 0 && st.st_size != 0)
		LogFatal("Cannot read %s:", imakefile);
	fclose(inFile);
	buf[0] = '\n';
	buf[1] = '\n';
	buf[count + 2] = '\0';

	punwritten = pbuf = buf + 2;
	while (*pbuf) {
	    /* for compatibility, replace make comments for cpp */
	    if (*pbuf == '#' && pbuf[-1] == '\n' && pbuf[-2] != '\\') {
		ptoken = pbuf+1;
		while (*ptoken == ' ' || *ptoken == '\t')
			ptoken++;
		pend = ptoken;
		while (*pend && *pend != ' ' && *pend != '\t' && *pend != '\n' && *pend != '\r')
			pend++;
		savec = *pend;
		*pend = '\0';
		if (strcmp(ptoken, "define") &&
		    strcmp(ptoken, "if") &&
		    strcmp(ptoken, "ifdef") &&
		    strcmp(ptoken, "ifndef") &&
		    strcmp(ptoken, "include") &&
		    strcmp(ptoken, "line") &&
		    strcmp(ptoken, "else") &&
		    strcmp(ptoken, "elif") &&
		    strcmp(ptoken, "endif") &&
		    strcmp(ptoken, "error") &&
		    strcmp(ptoken, "pragma") &&
		    strcmp(ptoken, "undef")) {
		    if (outFile == NULL) {
#ifdef HAS_MKSTEMP
			int fd;
#endif
			tmpImakefile = Strdup(tmpImakefile);
#ifndef HAS_MKSTEMP
			if (mktemp(tmpImakefile) == NULL ||
			    (outFile = fopen(tmpImakefile, "w+")) == NULL) {
			    LogFatal("Cannot open %s for write.",
				tmpImakefile);
			}
#else
			fd=mkstemp(tmpImakefile);
			if (fd != -1)
			    outFile = fdopen(fd, "w");
			if (outFile == NULL) {
			    if (fd != -1) {
			       unlink(tmpImakefile); close(fd);
			    }
			    LogFatal("Cannot open %s for write.",
				tmpImakefile);
			}
#endif
		    }
		    writetmpfile(outFile, punwritten, pbuf-punwritten,
				 tmpImakefile);
		    if (ptoken > pbuf + 1)
			writetmpfile(outFile, "XCOMM", 5, tmpImakefile);
		    else
			writetmpfile(outFile, "XCOMM ", 6, tmpImakefile);
		    punwritten = pbuf + 1;
		}
		*pend = savec;
	    }
	    pbuf++;
	}
	if (outFile) {
	    writetmpfile(outFile, punwritten, pbuf-punwritten, tmpImakefile);
	    fclose(outFile);

	    return tmpImakefile;
	}

	return(imakefile);
}

void
CleanCppOutput(FILE *tmpfd, char *tmpfname)
{
	char	*input;
	int	blankline = 0;

	while((input = ReadLine(tmpfd, tmpfname))) {
		if (isempty(input)) {
			if (blankline++)
				continue;
#ifdef CROSSCOMPILE
			if (fixup_whitespace)
#endif
#if defined CROSSCOMPILE || defined FIXUP_CPP_WHITESPACE
			    KludgeResetRule();
#endif
		} else {
			blankline = 0;
#ifdef CROSSCOMPILE
			if (fixup_whitespace)
#endif
#if defined CROSSCOMPILE || defined FIXUP_CPP_WHITESPACE
			    KludgeOutputLine(&input);
#endif
			writetmpfile(tmpfd, input, strlen(input), tmpfname);
		}
		writetmpfile(tmpfd, "\n", 1, tmpfname);
	}
	fflush(tmpfd);
#ifdef NFS_STDOUT_BUG
	/*
	 * On some systems, NFS seems to leave a large number of nulls at
	 * the end of the file.  Ralph Swick says that this kludge makes the
	 * problem go away.
	 */
	ftruncate (fileno(tmpfd), (off_t)ftell(tmpfd));
#endif
}

/*
 * Determine if a line has nothing in it.  As a side effect, we trim white
 * space from the end of the line.  Cpp magic cookies are also thrown away.
 * "XCOMM" token is transformed to "#".
 */
boolean
isempty(char *line)
{
	char	*pend;

	/*
	 * Check for lines of the form
	 *	# n "...
	 * or
	 *	# line n "...
	 */
	if (*line == '#') {
		pend = line+1;
		if (*pend == ' ')
			pend++;
		if (*pend == 'l' && pend[1] == 'i' && pend[2] == 'n' &&
		    pend[3] == 'e' && pend[4] == ' ')
			pend += 5;
		if (isdigit(*pend)) {
			do {
			    pend++;
			} while (isdigit(*pend));
			if (*pend == '\n' || *pend == '\0')
				return(TRUE);
			if (*pend++ == ' ' && *pend == '"')
				return(TRUE);
		}
		while (*pend)
		    pend++;
	} else {
	    for (pend = line; *pend; pend++) {
		if (*pend == 'X' && pend[1] == 'C' && pend[2] == 'O' &&
		    pend[3] == 'M' && pend[4] == 'M' &&
		    (pend == line || pend[-1] == ' ' || pend[-1] == '\t' || pend[-1] == '\r') &&
		    (pend[5] == ' ' || pend[5] == '\t' || pend[5] == '\r' || pend[5] == '\0'))
		{
		    *pend = '#';
		    strcpy(pend+1, pend+5);
		}
#ifdef CROSSCOMPILE
		if (magic_make_vars)
#endif
		  {
#if defined CROSSCOMPILE || defined MAGIC_MAKE_VARS
		    if (*pend == 'X' && pend[1] == 'V' && pend[2] == 'A' &&
			pend[3] == 'R')
		    {
			char varbuf[5];
			int i;

			if (pend[4] == 'd' && pend[5] == 'e' && pend[6] == 'f' &&
			    pend[7] >= '0' && pend[7] <= '9')
			{
			    i = pend[7] - '0';
			    sprintf(varbuf, "%0.4d", xvariable);
			    strncpy(pend+4, varbuf, 4);
			    xvariables[i] = xvariable;
			    xvariable = (xvariable + 1) % 10000;
			}
			else if (pend[4] == 'u' && pend[5] == 's' &&
				 pend[6] == 'e' && pend[7] >= '0' &&
				 pend[7] <= '9')
			{
			    i = pend[7] - '0';
			    sprintf(varbuf, "%0.4d", xvariables[i]);
			    strncpy(pend+4, varbuf, 4);
			}
		    }
#endif
		}
	    }
	}
	while (--pend >= line && (*pend == ' ' || *pend == '\t' || *pend == '\r')) ;
	pend[1] = '\0';
	return (*line == '\0');
}

/*ARGSUSED*/
char *
ReadLine(FILE *tmpfd, char *tmpfname)
{
	static boolean	initialized = FALSE;
	static char	*buf, *pline, *end;
	register char	*p1, *p2;

	if (! initialized) {
#ifdef WIN32
		FILE *fp = tmpfd;
#endif
		int total_red;
		struct stat st;

		/*
		 * Slurp it all up.
		 */
		fseek(tmpfd, 0, 0);
		if (fstat(fileno(tmpfd), &st) < 0)
			LogFatal("cannot stat %s for size", tmpMakefile);
		pline = buf = Emalloc((int)st.st_size+1);
		total_red = fread(buf, 1, st.st_size, tmpfd);
		if (total_red == 0 && st.st_size != 0)
			LogFatal("cannot read %s", tmpMakefile);
		end = buf + total_red;
		*end = '\0';
		fseek(tmpfd, 0, 0);
#if defined(SYSV) || defined(WIN32) || defined(USE_FREOPEN)
		tmpfd = freopen(tmpfname, "w+", tmpfd);
#ifdef WIN32
		if (! tmpfd) /* if failed try again */
			tmpfd = freopen(tmpfname, "w+", fp);
#endif
		if (! tmpfd)
			LogFatal("cannot reopen %s\n", tmpfname);
#else	/* !SYSV */
		ftruncate(fileno(tmpfd), (off_t) 0);
#endif	/* !SYSV */
		initialized = TRUE;
	    fprintf (tmpfd, "# Makefile generated by imake - do not edit!\n");
	    fprintf (tmpfd, "# %s\n",
		"$Xorg: imake.c,v 1.6 2001/02/09 02:03:15 xorgcvs Exp $");
	}

	for (p1 = pline; p1 < end; p1++) {
		if (*p1 == '@' && *(p1+1) == '@'
		    /* ignore ClearCase version-extended pathnames */
		    && !(p1 != pline && !isspace(*(p1-1)) && *(p1+2) == '/'))
		{ /* soft EOL */
			*p1++ = '\0';
			p1++; /* skip over second @ */
			break;
		}
		else if (*p1 == '\n') { /* real EOL */
#if defined CROSSCOMPILE || defined WIN32
# if defined CROSSCOMPILE
		  if (sys == win32)
# endif
		    {
			if (p1 > pline && p1[-1] == '\r')
				p1[-1] = '\0';
		    }
#endif
			*p1++ = '\0';
			break;
		}
	}

	/*
	 * return NULL at the end of the file.
	 */
	p2 = (pline == p1 ? NULL : pline);
	pline = p1;
	return(p2);
}

void
writetmpfile(FILE *fd, char *buf, int cnt, char *fname)
{
	if (fwrite(buf, sizeof(char), cnt, fd) == -1)
		LogFatal("Cannot write to %s.", fname);
}

char *
Emalloc(int size)
{
	char	*p;

	if ((p = malloc(size)) == NULL)
		LogFatalI("Cannot allocate %d bytes", size);
	return(p);
}

#if defined CROSSCOMPILE || defined FIXUP_CPP_WHITESPACE
void
KludgeOutputLine(char **pline)
{
	char	*p = *pline;
	char	quotechar = '\0';

	switch (*p) {
	    case '#':	/*Comment - ignore*/
		break;
	    case '\t':	/*Already tabbed - ignore it*/
		break;
	    case ' ':	/*May need a tab*/
	    default:
#ifdef CROSSCOMPILE
		if (inline_syntax)
#endif
#if defined CROSSCOMPILE || defined INLINE_SYNTAX
		{
		    if (*p == '<' && p[1] == '<') { /* inline file close */
			InInline--;
			InRule = TRUE;
			break;
		    }
		}
#endif
		/*
		 * The following cases should not be treated as beginning of
		 * rules:
		 * variable := name (GNU make)
		 * variable = .*:.* (':' should be allowed as value)
		 * sed 's:/a:/b:'   (: used in quoted values)
		 */
		for (; *p; p++) {
		    if (quotechar) {
			if (quotechar == '\\' ||
			    (*p == quotechar &&
# if defined CROSSCOMPILE || defined WIN32
			     (
#  if defined CROSSCOMPILE
			      (sys == win32) &&
#  endif
			      quotechar != ')') &&
# endif
			     p[-1] != '\\'))
			    quotechar = '\0';
			continue;
		    }
		    switch (*p) {
		    case '\\':
		    case '"':
		    case '\'':
			quotechar = *p;
			break;
		    case '(':
			quotechar = ')';
			break;
		    case '{':
			quotechar = '}';
			break;
		    case '[':
			quotechar = ']';
			break;
		    case '=':
#ifdef CROSSCOMPILE
			if (remove_cpp_leadspace)
#endif
#if defined CROSSCOMPILE || defined REMOVE_CPP_LEADSPACE
			{
			    if (!InRule && **pline == ' ') {
				while (**pline == ' ')
				    (*pline)++;
			    }
			}
#endif
			goto breakfor;
#if defined CROSSCOMPILE || defined INLINE_SYNTAX
		    case '<':
			if (inline_syntax) {
			    if (p[1] == '<') /* inline file start */
				InInline++;
			}
			break;
#endif
		    case ':':
			if (p[1] == '=')
			    goto breakfor;
			while (**pline == ' ')
			    (*pline)++;
			InRule = TRUE;
			return;
		    }
		}
breakfor:
		if (InRule && **pline == ' ')
		    **pline = '\t';
		break;
	}
}

void
KludgeResetRule(void)
{
	InRule = FALSE;
}
#endif
char *
Strdup(char *cp)
{
	char *new = Emalloc(strlen(cp) + 1);

	strcpy(new, cp);
	return new;
}

#ifdef CROSSCOMPILE
char*
CrossCompileCPP(void)
{
    char *cpp, *c;
    int len ;
    if (crosscompile_use_cc_e)
	AddCppArg("-E");

    cpp = strrchr(crosscompile_cpp,'/');
    if (!cpp)
	cpp = crosscompile_cpp;
    else
	cpp++;

    len = strlen(cpp) + strlen(CrossCompileDir) + 2;
    c = Emalloc(len);

    (void)snprintf(c, len,"%s/%s",CrossCompileDir,cpp);

    return c;
}

#endif

#ifdef CROSSCOMPILE
static void
get_cross_compile_dir(FILE *inFile)
{
	fprintf(inFile, "#define CrossCompileDir %s\n",
		CrossCompileDir);
	fprintf(inFile, "#define CrossCompiling YES\n");
}
#endif