File: xpather.cc

package info (click to toggle)
eclipse-titan 6.1.0-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 79,084 kB
  • ctags: 29,092
  • sloc: cpp: 210,764; ansic: 44,862; yacc: 21,034; sh: 12,594; makefile: 12,225; lex: 8,972; xml: 5,348; java: 4,849; perl: 3,780; python: 2,834; php: 175
file content (3150 lines) | stat: -rw-r--r-- 129,445 bytes parent folder | download
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
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
/******************************************************************************
 * Copyright (c) 2000-2016 Ericsson Telecom AB
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   
 *   Baji, Laszlo
 *   Balasko, Jeno
 *   Baranyi, Botond
 *   Beres, Szabolcs
 *   Delic, Adam
 *   Kovacs, Ferenc
 *   Ormandi, Matyas
 *   Pandi, Krisztian
 *   Raduly, Csaba
 *   Szabados, Kristof
 *   Szabo, Bence Janos
 *   Pandi, Krisztian
 *
 ******************************************************************************/
#include "xpather.h"

#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string>

#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>

#define LIBXML_SCHEMAS_ENABLED
#include <libxml/xmlschemastypes.h>

#include "../common/memory.h"
#include "vector.hh"
// Do _NOT_ #include "string.hh", it drags in ustring.o, common/Quadruple.o,
// Int.o, ttcn3/PatternString.o, and then the entire AST :(
#include "map.hh"
#include "ProjectGenHelper.hh"
#include "../common/path.h"
#include "ttcn3/ttcn3_preparser.h"
#include "asn1/asn1_preparser.h"

// in makefile.c
void ERROR  (const char *fmt, ...);
void WARNING(const char *fmt, ...);
void NOTIFY (const char *fmt, ...);
void DEBUG  (const char *fmt, ...);

// for vector and map
void fatal_error(const char * filename, int lineno, const char * fmt, ...)
__attribute__ ((__format__ (__printf__, 3, 4), __noreturn__));

void fatal_error(const char * filename, int lineno, const char * fmt, ...)
{
  fputs(filename, stderr);
  fprintf(stderr, ":%d: ", lineno);
  va_list va;
  va_start(va, fmt);
  vfprintf(stderr, fmt, va);
  va_end(va);
  abort();
}

ProjectGenHelper& projGenHelper = ProjectGenHelper::Instance();

/// Run an XPath query and return an xmlXPathObjectPtr, which must be freed
xmlXPathObjectPtr run_xpath(xmlXPathContextPtr xpathCtx, const char *xpathExpr)
{
  xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(
    (const xmlChar *)xpathExpr, xpathCtx);
  if(xpathObj == NULL) {
    fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpathExpr);
    return 0;
  }

  return xpathObj;
}

// RAII classes

class XmlDoc {
public:
  explicit XmlDoc(xmlDocPtr p) : doc_(p) {}
  ~XmlDoc() {
    if (doc_ != NULL) xmlFreeDoc(doc_);
  }
  operator xmlDocPtr() const { return doc_; }
private:
  xmlDocPtr doc_;
};

class XPathContext {
public:
  explicit XPathContext(xmlXPathContextPtr c) : ctx_(c) {}
  ~XPathContext() {
    if (ctx_ != NULL) xmlXPathFreeContext(ctx_);
  }
  operator xmlXPathContextPtr() const { return ctx_; }
private:
  xmlXPathContextPtr ctx_;
};

class XPathObject {
public:
  explicit XPathObject(xmlXPathObjectPtr o) : xpo_(o) {}
  ~XPathObject() {
    if (xpo_ != NULL) xmlXPathFreeObject(xpo_);
  }
  operator   xmlXPathObjectPtr() const { return xpo_; }
  xmlXPathObjectPtr operator->() const { return xpo_; }
private:
  xmlXPathObjectPtr xpo_;
};

//------------------------------------------------------------------
/// compare-by-content wrapper of a plain C string
struct cstring {
  explicit cstring(const char *s) : str(s) {}
  void destroy() const;
  operator const char*() const { return str; }
protected:
  const char *str;
  friend boolean operator<(const cstring& l, const cstring& r);
  friend boolean operator==(const cstring& l, const cstring& r);
};

void cstring::destroy() const {
  Free(const_cast<char*>(str)); // assumes valid pointer or NULL
}

boolean operator<(const cstring& l, const cstring& r) {
  return strcmp(l.str, r.str) < 0;
}

boolean operator==(const cstring& l, const cstring& r) {
  return strcmp(l.str, r.str) == 0;
}

/// RAII for C string
struct autostring : public cstring {
  /// Constructor; takes over ownership
  explicit autostring(const char *s = 0) : cstring(s) {}
  ~autostring() {
    // He who can destroy a thing, controls that thing -- Paul Muad'Dib
    Free(const_cast<char*>(str)); // assumes valid pointer or NULL
  }
  /// %Assignment; takes over ownership
  const autostring& operator=(const char *s) {
    Free(const_cast<char*>(str)); // assumes valid pointer or NULL
    str = s;
    return *this;
  }
  /// Relinquish ownership
  const char *extract() {
    const char *retval = str;
    str = 0;
    return retval;
  }
private:
  autostring(const autostring&);
  autostring& operator=(const autostring&);
};


bool validate_tpd(const XmlDoc& xml_doc, const char* tpd_file_name, const char* xsd_file_name)
{
  xmlLineNumbersDefault(1);

  xmlSchemaParserCtxtPtr ctxt = xmlSchemaNewParserCtxt(xsd_file_name);
  if (ctxt==NULL) {
    ERROR("Unable to create xsd context for xsd file `%s'", xsd_file_name);
    return false;
  }
  xmlSchemaSetParserErrors(ctxt, (xmlSchemaValidityErrorFunc)fprintf, (xmlSchemaValidityWarningFunc)fprintf, stderr);

  xmlSchemaPtr schema = xmlSchemaParse(ctxt);
  if (schema==NULL) {
    ERROR("Unable to parse xsd file `%s'", xsd_file_name);
    xmlSchemaFreeParserCtxt(ctxt);
    return false;
  }

  xmlSchemaValidCtxtPtr xsd = xmlSchemaNewValidCtxt(schema);
  if (xsd==NULL) {
    ERROR("Schema validation error for xsd file `%s'", xsd_file_name);
    xmlSchemaFree(schema);
    xmlSchemaFreeParserCtxt(ctxt);
    return false;
  }
  xmlSchemaSetValidErrors(xsd, (xmlSchemaValidityErrorFunc) fprintf, (xmlSchemaValidityWarningFunc) fprintf, stderr);

  int ret = xmlSchemaValidateDoc(xsd, xml_doc);

  xmlSchemaFreeValidCtxt(xsd);
  xmlSchemaFree(schema);
  xmlSchemaFreeParserCtxt(ctxt);
  xmlSchemaCleanupTypes();

  if (ret==0) {
    return true; // successful validation
  } else if (ret>0) {
    ERROR("TPD file `%s' is invalid according to schema `%s'", tpd_file_name, xsd_file_name);
    return false;
  } else {
    ERROR("TPD validation of `%s' generated an internal error in libxml2", tpd_file_name);
    return false;
  }
}

/** Extract a boolean value from the XML, if it exists otherwise flag is unchanged
 *
 * @param xpathCtx XPath context object
 * @param actcfg name of the active configuration
 * @param option name of the value
 * @param flag pointer to the variable to receive the value
 */
void xsdbool2boolean(const XPathContext& xpathCtx, const char *actcfg,
  const char *option, boolean* flag)
{
  char *xpath = mprintf(
    "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
    "/ProjectProperties/MakefileSettings/%s[text()='true']",
    actcfg, option);
  XPathObject xpathObj(run_xpath(xpathCtx, xpath));
  Free(xpath);

  if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
    *flag = TRUE;
  }
}

extern "C" string_list* getExternalLibs(const char* projName)
{
  if (!projGenHelper.getZflag()) return NULL;
  ProjectDescriptor* proj = projGenHelper.getTargetOfProject(projName);
  if (!proj) return NULL;

  std::vector<const char*> externalLibs;
  projGenHelper.getExternalLibs(externalLibs);

  if (0 == externalLibs.size()) return NULL;

  struct string_list* head = (struct string_list*)Malloc(sizeof(struct string_list));
  struct string_list* last_elem = head;
  struct string_list* tail = head;

  for (size_t i = 0; i < externalLibs.size(); ++i) {
     tail = last_elem;
     last_elem->str = mcopystr(externalLibs[i]);
     last_elem->next = (struct string_list*)Malloc(sizeof(struct string_list));
     last_elem = last_elem->next;
  }
  Free(last_elem);
  tail->next = NULL;
  return head;
}

extern "C" string_list* getExternalLibPaths(const char* projName)
{
  if (!projGenHelper.getZflag()) return NULL;
  ProjectDescriptor* proj = projGenHelper.getTargetOfProject(projName);
  if (!proj) return NULL;

  std::vector<const char*> externalLibs;
  projGenHelper.getExternalLibSearchPaths(externalLibs);

  if (0 == externalLibs.size()) return NULL;

  struct string_list* head = (struct string_list*)Malloc(sizeof(struct string_list));
  struct string_list* last_elem = head;
  struct string_list* tail = head;

  for (size_t i = 0; i < externalLibs.size(); ++i) {
     tail = last_elem;
     last_elem->str = mcopystr(externalLibs[i]);
     last_elem->next = (struct string_list*)Malloc(sizeof(struct string_list));
     last_elem = last_elem->next;
  }
  Free(last_elem);
  tail->next = NULL;
  return head;
}

extern "C" string_list* getRefWorkingDirs(const char* projName)
{
  if (!projGenHelper.getZflag()) return NULL;
  ProjectDescriptor* proj = projGenHelper.getTargetOfProject(projName);
  if (!proj) FATAL_ERROR("Project \"%s\" was not found in the project list", projName);

  struct string_list* head = (struct string_list*)Malloc(sizeof(struct string_list));
  struct string_list* last_elem = head;
  struct string_list* tail = head;
  last_elem->str = NULL;
  last_elem->next = NULL;
  for (size_t i = 0; i < proj->numOfRefProjWorkingDirs(); ++i) {
    tail = last_elem;
    last_elem->str = mcopystr(proj->getRefProjWorkingDir(i).c_str());
    last_elem->next = (struct string_list*)Malloc(sizeof(struct string_list));
    last_elem = last_elem->next;
  }
  Free(last_elem);
  tail->next = NULL;
  return head;
}

extern "C" string2_list* getLinkerLibs(const char* projName)
{

  if (!projGenHelper.getZflag()) return NULL;
  if (1 == projGenHelper.numOfProjects() || 0 == projGenHelper.numOfLibs()){
    return NULL; //no library
  }
  ProjectDescriptor* projLib = projGenHelper.getTargetOfProject(projName);
  if (!projLib) FATAL_ERROR("Project \"%s\" was not found in the project list", projName);

  struct string2_list* head = (struct string2_list*)Malloc(sizeof(struct string2_list));
  struct string2_list* last_elem = head;
  struct string2_list* tail = head;
  last_elem->next = NULL;
  last_elem->str1 = NULL;
  last_elem->str2 = NULL;
  for (std::map<std::string, ProjectDescriptor>::const_iterator it = projGenHelper.getHead();
       it != projGenHelper.getEnd(); ++it) {
    if ((it->second).isLibrary()) {
      if (!(it->second).getLinkingStrategy() &&
          !projLib->hasLinkerLibTo((it->second).getProjectName())) { // static linked library
          continue;
      }
      std::string relPath = projLib->setRelativePathTo((it->second).getProjectAbsWorkingDir());
      if (relPath == std::string(".")) {
        continue; // the relpath shows to itself
      }
      tail = last_elem;
      last_elem->str1 = mcopystr(relPath.c_str());
      last_elem->str2 = mcopystr((it->second).getTargetExecName().c_str());
      last_elem->next = (struct string2_list*)Malloc(sizeof(struct string2_list));
      last_elem = last_elem->next;
    }
  }
  tail->next = NULL;
  Free(last_elem);

  if (head->str1 && head->str2)
    return head;
  else
    return NULL;
}

extern "C" const char* getLibFromProject(const char* projName)
{
  if (!projGenHelper.getZflag()) return NULL;
  ProjectDescriptor* lib = projGenHelper.getTargetOfProject(projName);
  if (lib) return lib->getTargetExecName().c_str();
  return NULL;
}

extern "C" void erase_libs() {
  projGenHelper.cleanUp();
}

extern "C" void print_libs() {
  projGenHelper.print();
}


extern "C" boolean hasSubProject(const char* projName) {
  if (!projGenHelper.getZflag()) return FALSE;
  if (projGenHelper.getHflag())
    return static_cast<boolean>(projGenHelper.hasReferencedProject());
  else if(std::string(projName) == projGenHelper.getToplevelProjectName())
    return static_cast<boolean>(projGenHelper.hasReferencedProject());
  else
    return FALSE;
}

extern "C" boolean hasExternalLibrary(const char* libName, const char* projName) {
  if (!projGenHelper.getZflag()) return FALSE;
  ProjectDescriptor* projLib = projGenHelper.getTargetOfProject(projName);
  if (projLib && projLib->hasLinkerLib(libName))
    return TRUE;
  else
    return FALSE;
}

extern "C" boolean isTopLevelExecutable(const char* projName) {
  if (!projGenHelper.getZflag()) return false;
  ProjectDescriptor* proj = projGenHelper.getTargetOfProject(projName);
  if (projGenHelper.getToplevelProjectName() != std::string(projName)) return FALSE;
  if (proj && proj->isLibrary())
    return FALSE;
  else
    return TRUE;
}

extern "C" boolean isDynamicLibrary(const char* key) {
  if (!projGenHelper.getZflag()) return false;
  ProjectDescriptor* proj = projGenHelper.getProjectDescriptor(key);
  if (proj) return proj->getLinkingStrategy();
  FATAL_ERROR("Library \"%s\" was not found", key);
  return false;
}

extern "C" const char* getTPDFileName(const char* projName) {
  if (!projGenHelper.getZflag()) return NULL;
  ProjectDescriptor* proj = projGenHelper.getTargetOfProject(projName);
  if (proj) return proj->getTPDFileName().c_str();
  FATAL_ERROR("TPD file name to project \"%s\" was not found", projName);
}

extern "C" const char* getPathToRootDir(const char* projName) {
  if (!projGenHelper.getZflag()) return NULL;
  ProjectDescriptor* proj = projGenHelper.getTargetOfProject(projName);
  const char* rootDir = projGenHelper.getRootDirOS(projName).c_str();
  if (proj && rootDir) {
    return rootDir;
  }
  FATAL_ERROR("Project \"%s\": no relative path was found to top directory at OS level.", projName);
}

extern "C" const char* findLibraryPath(const char* libraryName, const char* projName)
{
  if (!projGenHelper.getZflag()) return NULL;
  ProjectDescriptor* projLib = projGenHelper.getTargetOfProject(projName);
  if (!projLib) FATAL_ERROR("Project \"%s\" was not found in the project list", projName);
  ProjectDescriptor* libLib = projGenHelper.getProjectDescriptor(libraryName);
  if (!libLib) return NULL;
  std::string str = projLib->setRelativePathTo(libLib->getProjectAbsWorkingDir());
  size_t refIndex = projLib->getLibSearchPathIndex(libLib->getProjectName());
  if (refIndex > projLib->numOfLibSearchPaths()) return NULL;
  projLib->setLibSearchPath(refIndex, str);
  return projLib->getLibSearchPath(libLib->getProjectName());
}

extern "C" const char* findLibraryName(const char* libraryName, const char* projName)
{
  if (!projGenHelper.getZflag()) return NULL;
  ProjectDescriptor* projLib = projGenHelper.getTargetOfProject(projName);
  if (!projLib) FATAL_ERROR("Project \"%s\" was not found in the project list", projName);
  ProjectDescriptor* libLib = projGenHelper.getProjectDescriptor(libraryName);
  if (!libLib) return NULL;
  for (size_t i = 0; i < projLib->numOfReferencedProjects(); ++i) {
    const std:: string refProjName = projLib->getReferencedProject(i);
    ProjectDescriptor* refLib = projGenHelper.getTargetOfProject(refProjName.c_str());
    if (refLib->getTargetExecName() == std::string(libraryName))
      return libraryName;
  }
  return NULL;
}

extern "C" boolean isTtcn3ModuleInLibrary(const char* moduleName)
{
  if (!projGenHelper.getZflag()) return FALSE;
  return (boolean)projGenHelper.isTtcn3ModuleInLibrary(moduleName);
}

extern "C" boolean isAsn1ModuleInLibrary(const char* moduleName)
{
  if (!projGenHelper.getZflag()) return FALSE;
  return (boolean)projGenHelper.isAsn1ModuleInLibrary(moduleName);
}

extern "C" boolean isSourceFileInLibrary(const char* fileName)
{
  if (!projGenHelper.getZflag()) return FALSE;
  return (boolean)projGenHelper.isSourceFileInLibrary(fileName);
}

extern "C" boolean isHeaderFileInLibrary(const char* fileName)
{
  if (!projGenHelper.getZflag()) return FALSE;
  return (boolean)projGenHelper.isHeaderFileInLibrary(fileName);
}

extern "C" boolean isTtcnPPFileInLibrary(const char* fileName)
{
  if (!projGenHelper.getZflag()) return FALSE;
  return (boolean)projGenHelper.isTtcnPPFileInLibrary(fileName);
}

extern "C" boolean isXSDModuleInLibrary(const char* fileName)
{
  if (!projGenHelper.getZflag()) return FALSE;
  return (boolean)projGenHelper.isXSDModuleInLibrary(fileName);
}

extern "C" boolean buildObjects(const char* projName, boolean add_referenced)
{
  if (!projGenHelper.getZflag()) return FALSE;
  if (projGenHelper.getHflag()) return FALSE;
  if (add_referenced) return FALSE;
  ProjectDescriptor* desc =projGenHelper.getTargetOfProject(projName);
  if (desc && desc->isLibrary()) return FALSE;
  return TRUE;
}

void append_to_library_list (const char* prjName,
                             const XPathContext& xpathCtx,
                             const char *actcfg)
{
  if (!projGenHelper.getZflag()) return;

  char *exeXpath = mprintf(
    "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
    "/ProjectProperties/MakefileSettings/targetExecutable/text()",
    actcfg);
  XPathObject exeObj(run_xpath(xpathCtx, exeXpath));
  Free(exeXpath);
  std::string lib_name;
  if (exeObj->nodesetval && exeObj->nodesetval->nodeNr > 0) {
    const char* target_executable = (const char*)exeObj->nodesetval->nodeTab[0]->content;
    autostring target_exe_dir(get_dir_from_path(target_executable));
    autostring target_exe_file(get_file_from_path(target_executable));
    lib_name = target_exe_file;
    ProjectDescriptor* projDesc = projGenHelper.getTargetOfProject(prjName);
    if (projDesc) {
      projDesc->setTargetExecName(lib_name.c_str());
    }
  }
}

// data structures and functions to manage excluded folders/files

map<cstring, const char> excluded_files;

boolean is_excluded_file(const cstring& path, const char* project) {
  if (!excluded_files.has_key(path)) return false;
  const char* proj = excluded_files[path];
  if (0 == strcmp(project, proj)) return true;
  return false;
}

vector<const char> excluded_folders;

// Unfortunately, when "docs" is excluded, we need to drop
// files in "docs/", "docs/pdf/", "docs/txt/", "docs/txt/old/" etc;
// so it's not as simple as using a map :(

/** Checks whether a file is under an excluded folder
 *
 * @param path (relative) path of the file
 * @return true if file is excluded, false otherwise
 */
boolean is_excluded_folder(const char *path) {
  boolean answer = FALSE;
  size_t pathlen = strlen(path);

  for (size_t i = 0, end = excluded_folders.size(); i < end; ++i) {
    const char *xdir = excluded_folders[i];
    size_t xdlen = strlen(xdir);
    if (pathlen > xdlen && path[xdlen] == '/') {
      // we may have a winner
      if ((answer = !strncmp(path, xdir, xdlen))) break;
    }
  }
  return answer;
}

// How do you treat a raw info? You cook it, of course!
// Returns a newly allocated string.
char *cook(const char *raw, const map<cstring, const char>& path_vars)
{
  const char *slash = strchr(raw, '/');
  if (!slash) { // Pretend that the slash is at the end of the string.
    slash = raw + strlen(raw);
  }

  // Assume that a path variable reference is the first (or only) component
  // of the path: ROOT in "ROOT/etc/issue".
  autostring prefix(mcopystrn(raw, slash - raw));
  if (path_vars.has_key(prefix)) {
    char *cooked = mcopystr(path_vars[prefix]);
    bool ends_with_slash = cooked[strlen(cooked)-1] == '/';
    if (ends_with_slash && *slash == '/') {
      // Avoid paths with two slashes at the start; Cygwin thinks it's UNC
      ++slash;
    }
    // If there was no '/' (only the path variable reference e.g "ROOT")
    // then slash points to a null byte and the mputstr is a no-op.
    cooked = mputstr(cooked, slash);
    return cooked;
  }

  // If the path variable could not be substituted,
  // return (a copy of) the original.
  return mcopystr(raw);
}

void replacechar(char** content) {

  std::string s= *content;
  size_t found = 0;
  while ((found = s.find('['))!= std::string::npos){
    s.replace(found,1, "${");
  }
  while ((found = s.find(']')) != std::string::npos){
    s.replace(found,1, "}");
  }
  *content = mcopystr(s.c_str());
}

/** Determines the suffix (i.e. the character sequence following the last dot)
 * of file or path name \a file_name. NULL pointer is returned if \a file_name
 * does not contain any dot character or the last character of it is a dot.
 * The suffix is not copied, the returned pointer points to the tail of
 * \a file_name. */
const char *get_suffix(const char *file_name)
{
  size_t last_dot = (size_t)-1;
  size_t i;
  for (i = 0; file_name[i] != '\0'; i++)
    if (file_name[i] == '.') last_dot = i;
  if (last_dot == (size_t)-1 || file_name[last_dot + 1] == '\0') return NULL;
  else return file_name + last_dot + 1;
}

int is_xsd_module(const char *file_name, char **module_name) {
  const char * extension = get_suffix(file_name);
  if (extension == NULL) {
    return 0;
  }
  if (strcmp(extension, "xsd") != 0) {
    return 0;
  }
  if (module_name != NULL) *module_name = NULL;
  FILE *fp;
  char line[1024];
  char *command = NULL;
  command = mputprintf(command, "xsd2ttcn -q -n %s", file_name);
  fp = popen(command, "r");
  if (fp == NULL) {
    ERROR("Could not get the module names of the XSD modules");
    return 0;
  }
  while (fgets(line, sizeof(line)-1, fp) != NULL) {
    *module_name = mputstr(*module_name, line);
  }
  int rv= pclose(fp);
  if (rv > 0) {
    return 0;
  }
  return *module_name != NULL;
}

static void clear_seen_tpd_files(map<cstring, int>& seen_tpd_files) {
  for (size_t i = 0, num = seen_tpd_files.size(); i < num; ++i) {
    const cstring& key = seen_tpd_files.get_nth_key(i);
    int *elem = seen_tpd_files.get_nth_elem(i);
    key.destroy();
    delete elem;
  }
  seen_tpd_files.clear();
}

const char* get_act_config(struct string2_list* cfg, const char* project_name) {
  while (cfg && cfg->str1 && project_name) {
    if (!strcmp(cfg->str1, project_name)) return cfg->str2;
    cfg = cfg->next;
  }
  return NULL;
}

void free_string_list(struct string_list* act_elem)
{
  while (act_elem) {
    struct string_list* next_elem = act_elem->next;
    Free(act_elem->str);
    Free(act_elem);
    act_elem = next_elem;
  }
}

void free_string2_list(struct string2_list* act_elem)
{
  while (act_elem) {
    struct string2_list* next_elem = act_elem->next;
    Free(act_elem->str1);
    Free(act_elem->str2);
    Free(act_elem);
    act_elem = next_elem;
  }
}

void free_config_list(struct config_list* act_elem) {
  while (act_elem) {
    struct config_list* next_elem = act_elem->next;
    Free(act_elem->str1);
    Free(act_elem->str2);
    Free(act_elem);
    act_elem = next_elem;
  }
}

void free_config_struct(struct config_struct* act_elem) {
  while (act_elem) {
    struct config_struct* next_elem = act_elem->next;
    Free(act_elem->project_name);
    Free(act_elem->project_conf);
    free_string_list(act_elem->dependencies);
    free_string2_list(act_elem->requirements);
    free_string_list(act_elem->children);
    Free(act_elem);
    act_elem = next_elem;
  }
}

// Initialize a config_struct to NULL-s
static void config_struct_init(struct config_struct* const list) {
  list->project_name = list->project_conf = NULL;
  list->is_active = FALSE;
  list->dependencies = (struct string_list*)Malloc(sizeof(struct string_list));
  list->dependencies->str = NULL;
  list->dependencies->next = NULL;
  list->requirements = (struct string2_list*)Malloc(sizeof(struct string2_list));
  list->requirements->str1 = NULL;
  list->requirements->str2 = NULL;
  list->requirements->next = NULL;
  list->children = (struct string_list*)Malloc(sizeof(struct string_list));
  list->children->str = NULL;
  list->children->next = NULL;
  list->processed = FALSE;
  list->next = NULL;
}

// This function fills up the dependencies field of the config_structs
// using the children fields.
static void config_struct_fillup_deps(struct config_struct* const list) {
  struct config_struct* last = list;
  while (last && last->project_name != NULL) {  // Go through the projects
    struct config_struct* lastest = list;
    while (lastest && lastest->project_name != NULL) { // Go through the projects again n^2 complexity
      struct string_list* lastest_child = lastest->children;
      while (lastest_child && lastest_child->str != NULL) {
        if (strcmp(last->project_name, lastest_child->str) == 0) { // If a project is a child of another project
          // Add the other project to the project's dependencies
          // But first check if it is already in the dependencies
          boolean already_in = FALSE;
          struct string_list* last_dep = last->dependencies;
          while (last_dep && last_dep->str != NULL) {
            if (strcmp(last_dep->str, lastest->project_name) == 0) {
              already_in = TRUE;
              break;
            }
            last_dep = last_dep->next;
          }
          if (already_in == FALSE) {
            last_dep->str = mcopystr(lastest->project_name);
            struct string_list* last_dep_next = (struct string_list*)Malloc(sizeof(string_list));
            last_dep_next->str = NULL;
            last_dep_next->next = NULL;
            last_dep->next = last_dep_next;
            break;
          }
        }
        lastest_child = lastest_child->next;
      }
      lastest = lastest->next;
    }
    last = last->next;
  }
}

// This function inserts project_name project's project_config configuration
// into the required configurations. This function can detect errors.
// If an error is detected FALSE is returned, otherwise TRUE.
static boolean insert_to_required_config(const struct config_struct* all_configs, const char* project_name, const char* project_config, struct string2_list* const required_configs) {

  boolean found_project = FALSE;
  boolean found_config = FALSE;
  boolean result = TRUE;
  
  //Check that it is a valid configuration for a valid project
  const struct config_struct* last = all_configs;
  while(last && last->project_name != NULL && last->project_conf != NULL) {
    if (strcmp(last->project_name, project_name) == 0) {
      found_project = TRUE;
      if (strcmp(last->project_conf, project_config) == 0) {
        found_config = TRUE;
        break;
      }
    }
    last = last->next;
  }
  // If the project or the configuration is not found
  if (found_project == FALSE || found_config == FALSE) {
    result = FALSE;
  }
  
  // Check if the project is already in the required configurations
  // or if the project is present with a different configuration
  boolean already_in = FALSE;
  struct string2_list* last_required_config = required_configs;
  while (last_required_config && last_required_config->str1 != NULL && last_required_config->str2 != NULL) {
    // If we have a record of this project
    if (strcmp(last_required_config->str1, project_name) == 0) {
      if (strcmp(last_required_config->str2, project_config) == 0) {
        // This project configuration is already in the required_configs
        already_in = TRUE;
      } else {
        // This project configuration is different than it is in the required_configs
        result = FALSE;
      }
    }
    last_required_config = last_required_config->next;
  }
  // If the project's configuration is not already present in the required
  // configs then we insert it. We insert it even when the result is FALSE.
  if (last_required_config && already_in == FALSE) {
    // Make copies of strings
    last_required_config->str1 = mcopystr(project_name);
    last_required_config->str2 = mcopystr(project_config);
    // Init next required configuration
    struct string2_list* last_required_config_next = (struct string2_list*)Malloc(sizeof(struct string2_list));
    last_required_config_next->str1 = NULL;
    last_required_config_next->str2 = NULL;
    last_required_config_next->next = NULL;
    last_required_config->next = last_required_config_next;
  }
  return result;
}

// Inserts project_name project with project_config configuration to the tmp_configs
// Return TRUE if the configuration is inserted
// Returns FALSE if the configuration is not inserted
// Returns 2 if the configuration is already in the tmp_configs (still inserted)
static boolean insert_to_tmp_config(struct config_list* const tmp_configs, const char* project_name, const char* project_config, const boolean is_active) {
  //First we check that it is a valid configuration for a valid project
  boolean found_project = FALSE;
  boolean found_config = FALSE;
  boolean active = FALSE;
  boolean result = TRUE;
  
  //Check if we have the same project with same configuration in the tmp_configs
  struct config_list* last = tmp_configs;
  while(last && last->str1 != NULL && last->str2 != NULL) {
    if (strcmp(last->str1, project_name) == 0) {
      found_project = TRUE;
      active = last->is_active;
      if (strcmp(last->str2, project_config) == 0) {
        found_config = TRUE;
        break;
      }
    }
    last = last->next;
  }
  
  //The case of same project with same configuration
  if (found_project == TRUE && found_config == TRUE) {
    result = 2;
    
  // The case of same project different configuration and the configuration
  // is not default
  } else if(found_project == TRUE && active == FALSE) {
    return FALSE;
  }
  // Go to the end of list
  while (last->next) {
    last = last->next;
  }
  // Insert new config into the tmp_configs
  last->str1 = mcopystr(project_name); 
  last->str2 = mcopystr(project_config);
  last->is_active = is_active;
  last->next = (struct config_list*)Malloc(sizeof(config_list));
  last->next->str1 = NULL;
  last->next->str2 = NULL;
  last->next->is_active = FALSE;
  last->next->next = NULL;

  return result;
}

// Removes the last element from the tmp_configs
static void remove_from_tmp_config(struct config_list* const tmp_configs, const char* /*project_name*/, const char* /*project_config*/) {
  struct config_list* last = tmp_configs;
  while (last->next && last->next->next != NULL) {
    last = last->next;
  }

  Free(last->str1);
  Free(last->str2);
  last->str1 = NULL;
  last->str2 = NULL;
  last->is_active = FALSE;
  Free(last->next);
  last->next = NULL;
}

// This function detects a circle originating from start_project.
// act_project is the next project which might be in the circle
// needed_in is a project that is needed to be inside the circle
// list is a temporary list which contains the elements of circle
static boolean is_circular_dep(const struct config_struct* all_configs, const char* start_project, const char* act_project, const char* needed_in, struct string_list** list) {
  if (*list == NULL) {
    *list = (struct string_list*)Malloc(sizeof(string_list));
    (*list)->str = mcopystr(start_project);
    (*list)->next = (struct string_list*)Malloc(sizeof(string_list));
    (*list)->next->str = NULL;
    (*list)->next->next = NULL;
  }
  //Circle detection
  struct string_list* last_list = *list;
  while (last_list && last_list->str != NULL) {
    struct string_list* last_list2 = last_list;
    while (last_list2 && last_list2->str != NULL) {
      // If the pointers are different but the project name is the same
      if (last_list != last_list2 && strcmp(last_list->str, last_list2->str) == 0) {
        // We look for the circle which starts from the start_project
        if (strcmp(last_list->str, start_project) != 0) {
          return FALSE;
        } else {
          // Check if needed_in is inside the circle
          while (last_list != last_list2) {
            if (needed_in != NULL && strcmp(last_list->str, needed_in) == 0) {
              return TRUE;
            }
            last_list = last_list->next;
          }
          return FALSE;
        }
      }
      last_list2 = last_list2->next;
    }
    last_list = last_list->next;
  }
  // Insert next element and call recursively for all the referenced projects
  const struct config_struct* last = all_configs;
  while (last && last->project_name) {
    //Find an act_project configuration
    if (strcmp(last->project_name, act_project) == 0) {
      // Go through its children
      struct string_list* children = last->children;
      while (children && children->str != NULL) {
        // Insert child into list
        last_list->str = mcopystr(children->str);
        last_list->next = (struct string_list*)Malloc(sizeof(string_list));
        last_list->next->str = NULL;
        last_list->next->next = NULL;
        // Call recursively
        if (is_circular_dep(all_configs, start_project, children->str, needed_in, list) == TRUE) {
          return TRUE;
        }
        // Remove child
        last_list = *list;
        while(last_list && last_list->next != NULL && last_list->next->next != NULL) {
          last_list = last_list->next;
        }
        Free(last_list->str);
        Free(last_list->next);
        last_list->str = NULL;
        last_list->next = NULL;
        children = children->next;
      }
    }
    last = last->next;
  }
  return FALSE;
}

// check if project_name project does not exists todo
// Project config == NULL means that we need to analyse the children of the project, todo why
// This function analyses project_name project to get the required configuration
// of this project.
// project_config may be NULL. It is not null when we are not certain of the
// project_name project's configuration.
// tmp_configs acts like a stack. It contains the history calls of anal_child. It
// is used to detect circles therefore prevents infinite recursions.
static boolean analyse_child(struct config_struct* const all_configs, const char* project_name, const char* project_config, struct string2_list* const required_configs, struct config_list* const tmp_configs) {
  boolean result = TRUE;
  const char* act_config = get_act_config(required_configs, project_name);
  // If the required configuration is known of project_name project
  if (act_config != NULL) {
    struct config_struct* tmp = all_configs;
    // Get the project_name's act_config config_struct (tmp holds it)
    while (tmp && tmp->project_name != NULL && tmp->project_conf != NULL) {
      if (strcmp(tmp->project_name, project_name) == 0 && strcmp(tmp->project_conf, act_config) == 0) {
        break;
      }
      tmp = tmp->next;
    }
    if (tmp->processed == TRUE) {
      // We already processed this project there is nothing to be done
      return result;
    }
    // Set the project to processed
    tmp->processed = TRUE;
    // Get the last (empty) required configuration
    struct string2_list* last_required_config = required_configs;
    while (last_required_config->next != NULL) {
      last_required_config = last_required_config->next;
    }
    // Insert all required config of project_name project's act_config configuration
    struct string2_list* last_proj_config = tmp->requirements;
    while (last_proj_config && last_proj_config->str1 != NULL && last_proj_config->str2 != NULL) {
      insert_to_required_config(all_configs, last_proj_config->str1, last_proj_config->str2, required_configs);
      last_proj_config = last_proj_config->next;
    }
    // Analyse the children of this project too.
    insert_to_tmp_config(tmp_configs, project_name, act_config, TRUE);
    struct string_list* last_child = tmp->children;
    while (last_child && last_child->str != NULL) {
      result = analyse_child(all_configs, last_child->str, NULL, required_configs, tmp_configs);
      if (result == FALSE) return result;
      last_child = last_child->next;
    }
    remove_from_tmp_config(tmp_configs, project_name, act_config);
    
  } else {
    
    // The required configuration of the project_name project is unknown
    boolean found = FALSE; // True if someone requires a configuration about project_name project
    boolean is_active = FALSE;
    
    // Go through every required configuration to check if someone requires
    // something about project_name project.
    struct config_struct* last = all_configs;
    while (last && last->requirements != NULL) {
      struct string2_list* req_config = last->requirements;
      while (req_config && req_config->str1 != NULL && req_config->str2 != NULL) {
        // If someone requires something about project_name project
        if (strcmp(req_config->str1, project_name) == 0) {
          const struct config_struct* tmp = all_configs;
          if (project_config == NULL) {
            // Get the active configuration of project_name project
            while (tmp && tmp->project_name != NULL && tmp->project_conf != NULL) {
              if (strcmp(tmp->project_name, project_name) == 0 && tmp->is_active == TRUE) {
                act_config = tmp->project_conf;
                is_active = TRUE;
                break;
              }
              tmp = tmp->next;
            }
          } else {
            act_config = project_config;
          }
          found = TRUE;
          
          // Insert the project_name with its active configuration
          result = insert_to_tmp_config(tmp_configs, project_name, act_config, is_active);
          if (result == FALSE) {
            return FALSE;
          } else if (result == 2) { // result is 2 if a circle is detected (this will cause error later)
            result = insert_to_required_config(all_configs, project_name, act_config, required_configs);
            if (result == FALSE) return result;
          }
          
          // Analyse the project that requires something about project_name project
          result = analyse_child(all_configs, last->project_name, last->project_conf, required_configs, tmp_configs);
          remove_from_tmp_config(tmp_configs, project_name, act_config);
          // If some errors happened during the analysis of last->project_conf project
          if (result == FALSE) {
            req_config = req_config->next;
            continue;
          }

          // If we still don't know anything about the project_name project's configuration
          if (get_act_config(required_configs, project_name) == NULL) {
            // Go to the next required config
            req_config = req_config->next;
            continue;
          }
          
          // Get the project_name's act_config config_struct (tmp holds it)
          tmp = all_configs;
          while (tmp && tmp->project_name != NULL && tmp->project_conf != NULL) {
            if (strcmp(tmp->project_name, project_name) == 0 && strcmp(tmp->project_conf, act_config) == 0) {
              break;
            }
            tmp = tmp->next;
          }

          // We know for sure that the project_name project's configuration is
          // act_config, so we insert all the required configs of project_name 
          // project's act_config configuration
          struct string2_list* last_proj_config = tmp->requirements;
          while (last_proj_config && last_proj_config->str1 != NULL && last_proj_config->str2 != NULL) {
            result = insert_to_required_config(all_configs, last_proj_config->str1, last_proj_config->str2, required_configs);
            if (result == FALSE) return result;
            last_proj_config = last_proj_config->next;
          }
          insert_to_tmp_config(tmp_configs, project_name, act_config, is_active);
          
          // Analyse referenced project's of project_name project
          struct string_list* last_child = tmp->children;
          while (last_child && last_child->str != NULL) {
            result = analyse_child(all_configs, last_child->str, NULL, required_configs, tmp_configs);
            if (result == FALSE) return result;
            last_child = last_child->next;
          }
          remove_from_tmp_config(tmp_configs, tmp->project_name, tmp->project_conf);
        }
        req_config = req_config->next;
      }
      last = last->next;
    }
    
    if (found == FALSE) { // No one said anything about this project's configuration
      //Get the active configuration of this project
      last = all_configs;
      while (last && last->project_name != NULL && last->project_conf != NULL) {
        if (strcmp(last->project_name, project_name) == 0 && last->is_active) {
          act_config = last->project_conf;
          break;
        }
        last = last->next;
      }
      if (last->processed == TRUE) {
        // We already processed this project
        return TRUE;
      }
      
      last->processed = TRUE;
      // Insert the active configuration to the required_configs
      result = insert_to_required_config(all_configs, project_name, act_config, required_configs);
      if (result == FALSE) return result;
      
      //Insert the project requirements of the project_name project's active configuration
      struct string2_list* last_proj_config = last->requirements;
      while (last_proj_config && last_proj_config->str1 != NULL && last_proj_config->str2 != NULL) {
        struct config_list* tmp_tmp = tmp_configs;
        // Detect circle in all_configs, which is started from last_proj_config->str1 and
        // project_name is an element of the circle
        struct string_list* list = NULL;
        boolean circular = is_circular_dep(all_configs, last_proj_config->str1, last_proj_config->str1, project_name, &list);
        boolean need_circular_error = FALSE;
        // Find the last_proj_config->str1 project in the circle, and 
        // determine if it has parent projects other than that are in the circle
        if (circular) {
          // Find the config struct of last_proj_config->str1
          struct config_struct* tmp2 = all_configs;
          while (tmp2 && tmp2->project_name != NULL) {
            if (strcmp(tmp2->project_name, last_proj_config->str1) == 0) {
              break;
            }
            tmp2 = tmp2->next;
          }
          if (list && tmp2 && tmp2->dependencies != NULL) {
            struct string_list* deps = tmp2->dependencies;
            while (deps && deps->str != NULL) {
              struct string_list* tmp_list = list;
              boolean tmp_error = FALSE;
              while (tmp_list && tmp_list->str != NULL) {
                if (strcmp(tmp_list->str, deps->str) == 0) {
                  tmp_error = TRUE;
                  break;
                }
                tmp_list = tmp_list->next;
              }
              if (tmp_error == FALSE) {
                need_circular_error = TRUE;
                break;
              }
              deps = deps->next;
            }
          }
        }
        free_string_list(list);
        // Go through the tmp_configs to check inconsistency
        while (tmp_tmp && tmp_tmp->str1 != NULL && tmp_tmp->str2 != NULL) {
          if (strcmp(tmp_tmp->str1, last_proj_config->str1) == 0 && 
              strcmp(tmp_tmp->str2, last_proj_config->str2) != 0 && 
              circular && need_circular_error) {
            // Insert the configuration. This will cause an error later.
            insert_to_required_config(all_configs, tmp_tmp->str1, tmp_tmp->str2, required_configs);
            result = FALSE;
          }
          tmp_tmp = tmp_tmp->next;
        }
        // Important to && the result at the end. If the result is FALSE from the
        // while cycle above, then it must remain FALSE
        result = insert_to_required_config(all_configs, last_proj_config->str1, last_proj_config->str2, required_configs) && result;
        if (result == FALSE) return result;
        last_proj_config = last_proj_config->next;
      }
      insert_to_tmp_config(tmp_configs, project_name, act_config, last->is_active);
      // Analyse children of the project_name project
      struct string_list* last_child = last->children;
      while (last_child && last_child->str != NULL) {
        result = analyse_child(all_configs, last_child->str, NULL, required_configs, tmp_configs);
        if (result == FALSE) return result;
        last_child = last_child->next;
      }
      remove_from_tmp_config(tmp_configs, project_name, act_config);
    }
  }
  return result;
}

static tpd_result config_struct_get_required_configs(struct config_struct* const all_configs, struct string2_list* const required_configs, struct config_list** tmp_configs) {
  // Init tmp_configs, which will be used to detect circularity
  *tmp_configs = (struct config_list*)Malloc(sizeof(config_list));
  (*tmp_configs)->str1 = NULL;
  (*tmp_configs)->str2 = NULL;
  (*tmp_configs)->is_active = FALSE;
  (*tmp_configs)->next = NULL;
  
  // Fill up dependencies of the configurations
  config_struct_fillup_deps(all_configs);
  struct config_struct* last = all_configs;
  // The top level project is always the first project and we need the active configuration of the project
  while (last) {
    if (last->project_name != NULL && all_configs->project_name != NULL) {
      if (strcmp(all_configs->project_name, last->project_name) == 0 && last->is_active == TRUE && last->processed == FALSE) {
        boolean result = TRUE;
        
        // Insert the top level project's active configuration to the required configs.
        result = insert_to_required_config(all_configs, last->project_name, last->project_conf, required_configs);
        if (result == FALSE) return TPD_FAILED;
        insert_to_tmp_config(*tmp_configs, last->project_name, last->project_conf, TRUE);
        
        // last variable holds the top level project's active configuration which needed to be analysed
        // Insert every required config of the top level project active configuration
        struct string2_list* last_proj_config = last->requirements;
        while (last_proj_config && last_proj_config->str1 != NULL && last_proj_config->str2 != NULL) { // todo ezek a null cuccok mindenhova, every param should not be null
          struct string_list* children = last->children;
          // This if allows that a top level project can require an other project's configuration
          // without referencing it.
          if (children->str != NULL || strcmp(last_proj_config->str1, last->project_name) == 0) {
            result = insert_to_required_config(all_configs, last_proj_config->str1, last_proj_config->str2, required_configs);
            if (result == FALSE) return TPD_FAILED;
          }
          last_proj_config = last_proj_config->next;
        }
        last->processed = TRUE;
        
        //Analyse the referenced project of the top level project
        struct string_list* last_child = last->children;
        while (last_child && last_child->str != NULL) {
          result = analyse_child(all_configs, last_child->str, NULL, required_configs, *tmp_configs); // todo check if everywhere is handled
          if (result == FALSE) return TPD_FAILED;
          last_child = last_child->next;
        }
        remove_from_tmp_config(*tmp_configs, last->project_name, last->project_conf);
        break; // No more needed
      }
    }
    last = last->next;
  }
  return TPD_SUCCESS;
}

static tpd_result process_tpd_internal(const char *p_tpd_name, char* tpdName, const char *actcfg,
  const char *file_list_path, int *p_argc, char ***p_argv,
  int *p_optind, char **p_ets_name, char **p_project_name,
  boolean *p_gflag, boolean *p_sflag, boolean *p_cflag, boolean *p_aflag, boolean *preprocess,
  boolean *p_Rflag, boolean *p_lflag, boolean *p_mflag, boolean *p_Pflag,
  boolean *p_Lflag, boolean recursive, boolean force_overwrite, boolean gen_only_top_level,
  const char *output_file, char** abs_work_dir_p, struct string_list* sub_project_dirs,
  const char* program_name, FILE* prj_graph_fp, struct string2_list* create_symlink_list, struct string_list* ttcn3_prep_includes,
  struct string_list* ttcn3_prep_defines, struct string_list* ttcn3_prep_undefines, struct string_list* prep_includes,
  struct string_list* prep_defines, struct string_list* prep_undefines, boolean *p_csflag, boolean *p_quflag, boolean* p_dsflag,
  char** cxxcompiler, char** optlevel, char** optflags, boolean* p_dbflag, boolean* p_drflag, boolean* p_dtflag, boolean* p_dxflag,
  boolean* p_djflag, boolean* p_fxflag, boolean* p_doflag, boolean* p_gfflag, boolean* p_lnflag, boolean* p_isflag,
  boolean* p_asflag, boolean* p_swflag, boolean* p_Yflag, boolean* p_Mflag, boolean *p_Eflag, boolean* p_nflag,
  boolean* p_diflag, struct string_list* solspeclibs, struct string_list* sol8speclibs,
  struct string_list* linuxspeclibs, struct string_list* freebsdspeclibs, struct string_list* win32speclibs, char** ttcn3prep,
  struct string_list* linkerlibs, struct string_list* additionalObjects, struct string_list* linkerlibsearchp, boolean Vflag, boolean Dflag,
  boolean *p_Zflag, boolean *p_Hflag, char** generatorCommandOutput, struct string2_list* target_placement_list, boolean prefix_workdir, 
  struct string2_list* run_command_list, map<cstring, int>& seen_tpd_files, struct string2_list* required_configs, struct string_list** profiled_file_list,
  const char **search_paths, size_t n_search_paths, char** makefileScript, struct config_struct * const all_configs);

extern "C" tpd_result process_tpd(const char *p_tpd_name, const char *actcfg,
  const char *file_list_path, int *p_argc, char ***p_argv,
  int *p_optind, char **p_ets_name, char **p_project_name,
  boolean *p_gflag, boolean *p_sflag, boolean *p_cflag, boolean *p_aflag, boolean *preprocess,
  boolean *p_Rflag, boolean *p_lflag, boolean *p_mflag, boolean *p_Pflag,
  boolean *p_Lflag, boolean recursive, boolean force_overwrite, boolean gen_only_top_level,
  const char *output_file, char** abs_work_dir_p, struct string_list* sub_project_dirs,
  const char* program_name, FILE* prj_graph_fp, struct string2_list* create_symlink_list, struct string_list* ttcn3_prep_includes,
  struct string_list* ttcn3_prep_defines, struct string_list* ttcn3_prep_undefines, struct string_list* prep_includes, 
  struct string_list* prep_defines, struct string_list* prep_undefines, boolean *p_csflag, boolean *p_quflag, boolean* p_dsflag,
  char** cxxcompiler, char** optlevel, char** optflags, boolean* p_dbflag, boolean* p_drflag, boolean* p_dtflag, boolean* p_dxflag, 
  boolean* p_djflag, boolean* p_fxflag, boolean* p_doflag, boolean* p_gfflag, boolean* p_lnflag, boolean* p_isflag,
  boolean* p_asflag, boolean* p_swflag, boolean* p_Yflag, boolean* p_Mflag, boolean* p_Eflag, boolean* p_nflag,
  boolean* p_diflag, struct string_list* solspeclibs, struct string_list* sol8speclibs,
  struct string_list* linuxspeclibs, struct string_list* freebsdspeclibs, struct string_list* win32speclibs, char** ttcn3prep,
  string_list* linkerlibs, string_list* additionalObjects, string_list* linkerlibsearchp, boolean Vflag, boolean Dflag, boolean *p_Zflag,
  boolean *p_Hflag, char** generatorCommandOutput, struct string2_list* target_placement_list, boolean prefix_workdir,
  struct string2_list* run_command_list, struct string2_list* required_configs, struct string_list** profiled_file_list,
  const char **search_paths, size_t n_search_paths, char** makefileScript) {

  map<cstring, int> seen_tpd_files;
  char *tpdName = NULL;
  projGenHelper.setZflag(*p_Zflag);
  projGenHelper.setWflag(prefix_workdir);
  projGenHelper.setHflag(*p_Hflag);

  struct config_struct* all_configs = (struct config_struct*)Malloc(sizeof(struct config_struct));
  config_struct_init(all_configs);
  
  // The first round only collects the configurations about the tpd-s into the
  // all_configs variable. It does not do anything else.
  tpd_result success = process_tpd_internal(p_tpd_name, tpdName,
      actcfg, file_list_path, p_argc, p_argv, p_optind, p_ets_name, p_project_name,
      p_gflag, p_sflag, p_cflag, p_aflag, preprocess,
      p_Rflag, p_lflag, p_mflag, p_Pflag,
      p_Lflag, recursive, force_overwrite, gen_only_top_level,
      output_file, abs_work_dir_p, sub_project_dirs,
      program_name, prj_graph_fp, create_symlink_list, ttcn3_prep_includes,
      ttcn3_prep_defines, ttcn3_prep_undefines, prep_includes, prep_defines,
      prep_undefines, p_csflag, p_quflag, p_dsflag, cxxcompiler,
      optlevel, optflags, p_dbflag, p_drflag, p_dtflag, p_dxflag, p_djflag,
      p_fxflag, p_doflag, p_gfflag, p_lnflag, p_isflag,
      p_asflag, p_swflag, p_Yflag, p_Mflag, p_Eflag, p_nflag, p_diflag, solspeclibs, sol8speclibs,
      linuxspeclibs, freebsdspeclibs, win32speclibs, ttcn3prep,
      linkerlibs, additionalObjects, linkerlibsearchp, Vflag, Dflag, p_Zflag,
      p_Hflag, generatorCommandOutput, target_placement_list, prefix_workdir, 
      run_command_list, seen_tpd_files, required_configs, profiled_file_list,
      search_paths, n_search_paths, makefileScript, all_configs);
  
  if (success == TPD_SUCCESS) {
    struct config_list* tmp_configs = NULL;
    config_struct_get_required_configs(all_configs, required_configs, &tmp_configs);
    free_config_list(tmp_configs);
    free_config_struct(all_configs);
    all_configs = NULL;
    
    // In the second round every configuration is known for every project in the
    // optimal case. In the not optimal case errors are produced.
    // This round does get the information from the tpd to generate the makefile.
    success = process_tpd_internal(p_tpd_name, tpdName,
      actcfg, file_list_path, p_argc, p_argv, p_optind, p_ets_name, p_project_name,
      p_gflag, p_sflag, p_cflag, p_aflag, preprocess,
      p_Rflag, p_lflag, p_mflag, p_Pflag,
      p_Lflag, recursive, force_overwrite, gen_only_top_level,
      output_file, abs_work_dir_p, sub_project_dirs,
      program_name, prj_graph_fp, create_symlink_list, ttcn3_prep_includes,
      ttcn3_prep_defines, ttcn3_prep_undefines, prep_includes, prep_defines,
      prep_undefines, p_csflag, p_quflag, p_dsflag, cxxcompiler,
      optlevel, optflags, p_dbflag, p_drflag, p_dtflag, p_dxflag, p_djflag,
      p_fxflag, p_doflag, p_gfflag, p_lnflag, p_isflag,
      p_asflag, p_swflag, p_Yflag, p_Mflag, p_Eflag, p_nflag, p_diflag, solspeclibs, sol8speclibs,
      linuxspeclibs, freebsdspeclibs, win32speclibs, ttcn3prep,
      linkerlibs, additionalObjects, linkerlibsearchp, Vflag, Dflag, p_Zflag,
      p_Hflag, generatorCommandOutput, target_placement_list, prefix_workdir, 
      run_command_list, seen_tpd_files, required_configs, profiled_file_list,
      search_paths, n_search_paths, makefileScript, all_configs);
  }
  if (TPD_FAILED == success){
    exit(EXIT_FAILURE);
  }

  if (false == projGenHelper.sanityCheck()) {
    fprintf (stderr, "makefilegen exits\n");
    exit(EXIT_FAILURE);
  }

  projGenHelper.generateRefProjectWorkingDirsTo(*p_project_name);

  for (size_t i = 0, num = seen_tpd_files.size(); i < num; ++i) {
    const cstring& key = seen_tpd_files.get_nth_key(i);
    int *elem = seen_tpd_files.get_nth_elem(i);
    key.destroy();
    delete elem;
  }
  seen_tpd_files.clear();

  return success;
}

// optind is the index of the next element of argv to be processed.
// Return TPD_SUCESS if parsing successful, TPD_SKIPPED if the tpd was
// seen already, or TPD_FAILED.
//
// Note: if process_tpd() returns TPD_SUCCESS, it is expected that all strings
// (argv[], ets_name, other_files[], output_file) are allocated on the heap
// and need to be freed. On input, these strings point into argv.
// process_tpd() may alter these strings; new values will be on the heap.
// If process_tpd() preserves the content of such a string (e.g. ets_name),
// it must nevertheless make a copy on the heap via mcopystr().
static tpd_result process_tpd_internal(const char *p_tpd_name, char *tpdName, const char *actcfg,
  const char *file_list_path, int *p_argc, char ***p_argv,
  int *p_optind, char **p_ets_name, char **p_project_name,
  boolean *p_gflag, boolean *p_sflag, boolean *p_cflag, boolean *p_aflag, boolean *preprocess,
  boolean *p_Rflag, boolean *p_lflag, boolean *p_mflag, boolean *p_Pflag,
  boolean *p_Lflag, boolean recursive, boolean force_overwrite, boolean gen_only_top_level,
  const char *output_file, char** abs_work_dir_p, struct string_list* sub_project_dirs,
  const char* program_name, FILE* prj_graph_fp, struct string2_list* create_symlink_list, struct string_list* ttcn3_prep_includes,
  struct string_list* ttcn3_prep_defines, struct string_list* ttcn3_prep_undefines, struct string_list* prep_includes,
  struct string_list* prep_defines, struct string_list* prep_undefines, boolean *p_csflag, boolean *p_quflag, boolean* p_dsflag,
  char** cxxcompiler, char** optlevel, char** optflags, boolean* p_dbflag, boolean* p_drflag, boolean* p_dtflag, boolean* p_dxflag,
  boolean* p_djflag, boolean* p_fxflag, boolean* p_doflag, boolean* p_gfflag, boolean* p_lnflag, boolean* p_isflag,
  boolean* p_asflag, boolean* p_swflag, boolean* p_Yflag, boolean* p_Mflag, boolean* p_Eflag, boolean* p_nflag,
  boolean* p_diflag, struct string_list* solspeclibs, struct string_list* sol8speclibs,
  struct string_list* linuxspeclibs, struct string_list* freebsdspeclibs, struct string_list* win32speclibs, char** ttcn3prep,
  string_list* linkerlibs, string_list* additionalObjects, string_list* linkerlibsearchp, boolean Vflag, boolean Dflag, boolean *p_Zflag,
  boolean *p_Hflag, char** generatorCommandOutput, struct string2_list* target_placement_list, boolean prefix_workdir,
  struct string2_list* run_command_list, map<cstring, int>& seen_tpd_files, struct string2_list* required_configs, struct string_list** profiled_file_list,
  const char **search_paths, size_t n_search_paths, char** makefileScript, struct config_struct * const all_configs)
{
  tpd_result result = TPD_SUCCESS;
  // read-only non-pointer aliases
  //char** const& local_argv = *p_argv;
  int const& local_argc = *p_argc;
  int const& local_optind = *p_optind;
  *abs_work_dir_p = NULL;
  const boolean get_config_mode = all_configs != NULL;

  assert(local_optind >= 2 // at least '-ttpd_name' must be in the args
    || local_optind == 0); // if called for a referenced project

  assert(local_argc   >= local_optind);

  autostring tpd_dir(get_dir_from_path(p_tpd_name));
  autostring abs_tpd_dir(get_absolute_dir(tpd_dir, NULL, FALSE));
  boolean free_name = FALSE;
  struct stat buf;
  //Only referenced project, when first try is failed,              and when not absolute path
  if(0 == local_optind && p_tpd_name != NULL && stat(p_tpd_name, &buf) && tpdName != NULL) {
    //Find the first search_path that has the tpd
    for(size_t i = 0; i<n_search_paths; i++) {
      boolean need_slash = search_paths[i][strlen(search_paths[i]) - 1] != '/';
      NOTIFY("Cannot find %s, trying with %s%s%s\n", p_tpd_name, search_paths[i], need_slash ? "/" : "", tpdName);
      //Create path
      char * prefixed_file_path = (char*)Malloc((strlen(search_paths[i]) + strlen(tpdName) + 1 + need_slash) * sizeof(char));
      strcpy(prefixed_file_path, search_paths[i]);
      if(need_slash) {
        strcat(prefixed_file_path, "/");
      }
      strcat(prefixed_file_path, tpdName);

      tpd_dir = get_dir_from_path(prefixed_file_path);
      abs_tpd_dir = get_absolute_dir(tpd_dir, NULL, FALSE);

      if(!stat(prefixed_file_path, &buf)){
        //Ok, tpd found
        p_tpd_name = prefixed_file_path;
        free_name = TRUE;
        NOTIFY("TPD with name %s found at %s.", tpdName, search_paths[i]);
        break;
      }else {
        //tpd not found, continue search
        abs_tpd_dir = NULL;
        Free(prefixed_file_path);
      }
    }
    //Error if tpd is not found in either search paths
    if(NULL == (const char*)abs_tpd_dir) {
      //Only write out the name in the error message (without .tpd)
      tpdName[strlen(tpdName)-4] ='\0';
      ERROR("Unable to find ReferencedProject with name: %s", (const char*)tpdName);
      return TPD_FAILED;
    }
  }

  if (NULL == (const char*)abs_tpd_dir) {
    ERROR("absolute TPD directory could not be retrieved from %s", (const char*)tpd_dir);
    return TPD_FAILED;
  }
  autostring tpd_filename(get_file_from_path(p_tpd_name));
  autostring abs_tpd_name(compose_path_name(abs_tpd_dir, tpd_filename));

  if (seen_tpd_files.has_key(abs_tpd_name)) {
    ++*seen_tpd_files[abs_tpd_name];
    return TPD_SKIPPED; // nothing to do
  }
  else {
    if (recursive && !prefix_workdir) {
      // check that this tpd file is not inside a directory of another tpd file
      for (size_t i = 0; i < seen_tpd_files.size(); ++i) {
        const cstring& other_tpd_name = seen_tpd_files.get_nth_key(i);
        autostring other_tpd_dir(get_dir_from_path((const char*)other_tpd_name));
        if (strcmp((const char*)abs_tpd_dir,(const char*)other_tpd_dir)==0) {
          ERROR("TPD files `%s' and `%s' are in the same directory! Use the `-W' option.", (const char*)abs_tpd_name, (const char*)other_tpd_name);
          return TPD_FAILED;
        }
      }
    }
    // mcopystr makes another copy for the map
    seen_tpd_files.add(cstring(mcopystr(abs_tpd_name)), new int(1));
  }

  vector<char> base_files; // values Malloc'd but we pass them to the caller

  XmlDoc doc(xmlParseFile(p_tpd_name));
  if (doc == NULL) {
    fprintf(stderr, "Error: unable to parse file \"%s\"\n", p_tpd_name);
    return TPD_FAILED;
  }

  if (!Vflag && get_config_mode) {
    // try schema validation if tpd schema file was found
    bool tpd_is_valid = false;
    const char* ttcn3_dir = getenv("TTCN3_DIR");
    if (ttcn3_dir) {
      size_t ttcn3_dir_len = strlen(ttcn3_dir);
      bool ends_with_slash = (ttcn3_dir_len>0) && (ttcn3_dir[ttcn3_dir_len - 1]=='/');
      expstring_t xsd_file_name = mprintf("%s%setc/xsd/TPD.xsd", ttcn3_dir, ends_with_slash?"":"/");
      autostring xsd_file_name_as(xsd_file_name);
      if (get_path_status(xsd_file_name)==PS_FILE) {
        if (validate_tpd(doc, p_tpd_name, xsd_file_name)) {
          tpd_is_valid = true;
          NOTIFY("TPD file `%s' validated successfully with schema file `%s'", p_tpd_name, xsd_file_name);
        }
      } else {
        ERROR("Cannot find XSD for schema for validation of TPD on path `%s'", xsd_file_name);
      }
    } else {
      ERROR("Environment variable TTCN3_DIR not present, cannot find XSD for schema validation of TPD");
    }
    if (!tpd_is_valid) {
      return TPD_FAILED;
    }
  }

  // Source files.
  // Key is projectRelativePath, value is relativeURI or rawURI.
  map<cstring, const char> files;   // values Malloc'd
  map<cstring, const char> folders; // values Malloc'd
  // NOTE! files and folders must be local variables of process_tpd.
  // This is because the keys (not the values) are owned by the XmlDoc.

  map<cstring, const char> path_vars;
  
  autostring workdir;
  
  autostring proj_abs_workdir;

  autostring abs_workdir;

  XPathContext xpathCtx(xmlXPathNewContext(doc));
  if (xpathCtx == NULL) {
    fprintf(stderr,"Error: unable to create new XPath context\n");
    return TPD_FAILED;
  }
  
    /////////////////////////////////////////////////////////////////////////////
  {
    char *projectNameXpath = mprintf("/TITAN_Project_File_Information/ProjectName/text()");
    XPathObject projectNameObj(run_xpath(xpathCtx, projectNameXpath));
    Free(projectNameXpath);
    if (projectNameObj->nodesetval && projectNameObj->nodesetval->nodeNr > 0) {
      *p_project_name = mcopystr((const char*)projectNameObj->nodesetval->nodeTab[0]->content);
      projGenHelper.addTarget(*p_project_name);
      projGenHelper.setToplevelProjectName(*p_project_name);
      ProjectDescriptor* projDesc = projGenHelper.getTargetOfProject(*p_project_name);
      if (projDesc) projDesc->setProjectAbsTpdDir((const char*)abs_tpd_dir);
    }
  }
  
  /////////////////////////////////////////////////////////////////////////////
  
  if (!actcfg && !get_config_mode) {
    actcfg = get_act_config(required_configs, *p_project_name);
  }

  if (actcfg == NULL) {
    // Find out the active config
    XPathObject  activeConfig(run_xpath(xpathCtx,
      "/TITAN_Project_File_Information/ActiveConfiguration/text()"));
    if (activeConfig->nodesetval && activeConfig->nodesetval->nodeNr == 1) {
      // there is one node
      actcfg = (const char*)activeConfig->nodesetval->nodeTab[0]->content;
    }
  }
  
  if (actcfg == NULL) {
    ERROR("Can not find the active build configuration.");
    for (size_t i = 0; i < folders.size(); ++i) {
      Free(const_cast<char*>(folders.get_nth_elem(i)));
    }
    folders.clear();
    return TPD_FAILED;
  }

  { // check if the active configuration exists
    expstring_t xpathActCfg= mprintf(
      "/TITAN_Project_File_Information/Configurations/"
        "Configuration[@name='%s']/text()", actcfg);
    XPathObject theConfigEx(run_xpath(xpathCtx, xpathActCfg));
    Free(xpathActCfg);

    xmlNodeSetPtr nodes = theConfigEx->nodesetval;
    if (nodes == NULL) {
      ERROR("The active build configuration named '%s' of project '%s' does not exist",
        actcfg, *p_project_name);
      for (size_t i = 0; i < folders.size(); ++i) {
        Free(const_cast<char*>(folders.get_nth_elem(i)));
      }
      folders.clear();
      return TPD_FAILED;
    }
  }

  if (!get_config_mode) {
    struct string2_list* last_elem = required_configs;
    //                        To ensure that the first elem is checked too if last_elem->next is null
    while (last_elem && last_elem->str1 != NULL && last_elem->str2 != NULL) {
      if (!strcmp(last_elem->str1, *p_project_name) && strcmp(last_elem->str2, actcfg)) {
        { // check if the other configuration exists
          expstring_t xpathActCfg= mprintf(
            "/TITAN_Project_File_Information/Configurations/"
              "Configuration[@name='%s']/text()", last_elem->str2);
          XPathObject theConfigEx(run_xpath(xpathCtx, xpathActCfg));
          Free(xpathActCfg);

          xmlNodeSetPtr nodes = theConfigEx->nodesetval;
          if (nodes == NULL) {
            ERROR("The active build configuration named '%s' of project '%s' does not exist",
              last_elem->str2, *p_project_name);
            for (size_t i = 0; i < folders.size(); ++i) {
              Free(const_cast<char*>(folders.get_nth_elem(i)));
            }
            folders.clear();
            return TPD_FAILED;
          }
        }
        ERROR("Required configuration is inconsistent or circular : Project '%s' cannot have 2 "
              "different configuration '%s' and '%s'",
              last_elem->str1, actcfg, last_elem->str2);
        for (size_t i = 0; i < folders.size(); ++i) {
          Free(const_cast<char*>(folders.get_nth_elem(i)));
        }
        folders.clear();
        return TPD_FAILED;
      }
      last_elem = last_elem->next;
    }

  // Collect path variables
  {
    XPathObject pathsObj(run_xpath(xpathCtx,
      "/TITAN_Project_File_Information/PathVariables/PathVariable"));
    xmlNodeSetPtr nodes = pathsObj->nodesetval;

    const char *name = 0, *value = 0;
    if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
      // nodes->nodeTab[i]->name === "PathVariable"
      for (xmlAttrPtr attr = nodes->nodeTab[i]->properties; attr; attr = attr->next) {
        if (!strcmp((const char*)attr->name, "name")) {
          name = (const char*)attr->children->content;
        }
        else if (!strcmp((const char*)attr->name, "value")) {
          value = (const char*)attr->children->content;
        }
        else {
          WARNING("Unknown attribute %s", (const char*)nodes->nodeTab[i]->name);
        }
      } // next attribute

      if (name && value) path_vars.add(cstring(name), value);
      else ERROR("A PathVariable must have both name and value");
    } // next PathVariable
  }

  // Collect folders
  {
    XPathObject foldersObj(run_xpath(xpathCtx,
      "/TITAN_Project_File_Information/Folders/FolderResource"));

    xmlNodeSetPtr nodes = foldersObj->nodesetval;
    if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
      // nodes->nodeTab[i]->name === "FolderResource"
      const char *uri = 0, *path = 0, *raw = 0;

      // projectRelativePath is the path as it appears in Project Explorer (illusion)
      // relativeURI is the actual location, relative to the project root (reality)
      // rawURI is present if the relative path can not be calculated
      //
      // Theoretically these attributes could be in any order, loop over them
      for (xmlAttrPtr attr = nodes->nodeTab[i]->properties; attr; attr = attr->next) {
        if (!strcmp((const char*)attr->name, "projectRelativePath")) {
          path = (const char*)attr->children->content;
        }
        else if (!strcmp((const char*)attr->name, "relativeURI")) {
          uri = (const char*)attr->children->content;
        }
        else if (!strcmp((const char*)attr->name, "rawURI")) {
          raw = (const char*)attr->children->content;
        }
        else {
          WARNING("Unknown attribute %s", (const char*)nodes->nodeTab[i]->name);
        }
      } // next attribute

      if (path == NULL) {
        ERROR("A FolderResource must have a projectRelativePath");
        continue;
      }

      if (uri == NULL && raw == NULL) {
        ERROR("A FolderResource must have either relativeURI or rawURI");
        continue;
      }
      // relativeURI wins over rawURI
      folders.add(cstring(path), uri ? mcopystr(uri) : cook(raw, path_vars));
      // TODO uri: cut "file:", complain on anything else
    } // next FolderResource
  }

  // working directory stuff
  {
    const char* workdirFromTpd = "bin"; // default value
    char *workdirXpath = mprintf(
      "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
      "/ProjectProperties/LocalBuildSettings/workingDirectory/text()",
      actcfg);
    XPathObject workdirObj(run_xpath(xpathCtx, workdirXpath));
    Free(workdirXpath);
    if (workdirObj->nodesetval && workdirObj->nodesetval->nodeNr > 0) {
      workdirFromTpd = (const char*)workdirObj->nodesetval->nodeTab[0]->content;
    }
    if (prefix_workdir) { // the working directory is: prjNameStr + "_" + workdirFromTpd
      const char* prjNameStr = "unnamedproject";
      XPathObject prjName(run_xpath(xpathCtx, "/TITAN_Project_File_Information/ProjectName/text()"));
      if (prjName->nodesetval && prjName->nodesetval->nodeNr == 1) {
        prjNameStr = (const char*)prjName->nodesetval->nodeTab[0]->content;
      }
      workdir = mprintf("%s_%s", prjNameStr, workdirFromTpd);
    } else {
      workdir = mcopystr(workdirFromTpd);
    }
  }
  if (!folders.has_key(workdir)) {
    // Maybe the tpd was saved with the option "No info about work dir"
    folders.add(workdir, mcopystr(workdir)); // fake it
  }
  const char *real_workdir = folders[workdir]; // This is relative to the location of the tpd file
  excluded_folders.add(real_workdir); // excluded by convention

  // If -D flag was specified then we ignore the workdir
  // in the TPD (the current dir is considered the work dir).
  if (!Dflag) {
    bool hasWorkDir = false;
    // if the working directory does not exist create it
    autostring saved_work_dir(get_working_dir());
    if (set_working_dir(abs_tpd_dir)) {
      ERROR("Could not change to project directory `%s'", (const char*)abs_tpd_dir);
    } else {
      switch (get_path_status(real_workdir)) {
      case PS_FILE:
        ERROR("Cannot create working directory `%s' in project directory `%s' because a file with the same name exists", (const char*)abs_tpd_dir, real_workdir);
        break;
      case PS_DIRECTORY:
        // already exists
        hasWorkDir = true;
        break;
      default:
        if (recursive || local_argc != 0) { // we only want to create workdir if necessary
          fprintf(stderr, "Working directory `%s' in project `%s' does not exist, trying to create it...\n",
                  real_workdir, (const char*)abs_tpd_dir);
          int rv = mkdir(real_workdir, 0755);
          if (rv) ERROR("Could not create working directory, mkdir() failed: %s", strerror(errno));
          else printf("Working directory created\n");
          hasWorkDir = true;
        }
      }
    }

    if (local_argc==0) { // if not top level
      set_working_dir(saved_work_dir); // restore working directory
    } else { // if top level
      set_working_dir(real_workdir); // go into the working dir
    }
    if (hasWorkDir) { //we created working directory, or its already been created (from a parent makefilegen process maybe)
      *abs_work_dir_p = get_absolute_dir(real_workdir, abs_tpd_dir, TRUE);
      abs_workdir = (mcopystr(*abs_work_dir_p));
      proj_abs_workdir = mcopystr(*abs_work_dir_p);
    }
  }

  if (Dflag) { // the path to subproject working dir is needed to find the linkerlibsearchpath
    proj_abs_workdir = compose_path_name(abs_tpd_dir, real_workdir);
  }

  ProjectDescriptor* projDesc = projGenHelper.getTargetOfProject(*p_project_name);
  if (projDesc) {
    projDesc->setProjectAbsWorkingDir((const char*)proj_abs_workdir);
    projDesc->setProjectWorkingDir(real_workdir);
    projDesc->setTPDFileName(p_tpd_name);
  }

  /////////////////////////////////////////////////////////////////////////////

  // Gather the excluded folders in the active config
  {
    expstring_t xpathActCfgPaths = mprintf(
      "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
      "/FolderProperties/FolderResource/FolderProperties/ExcludeFromBuild[text()='true']"
      // This was the selection criterium, we need to go up and down for the actual information
      "/parent::*/parent::*/FolderPath/text()",
      actcfg);
    XPathObject theConfigEx(run_xpath(xpathCtx, xpathActCfgPaths));
    Free(xpathActCfgPaths);

    xmlNodeSetPtr nodes = theConfigEx->nodesetval;
    if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {

      excluded_folders.add((const char*)nodes->nodeTab[i]->content);
    }
  }

  // Gather individual excluded files in the active config
  {
    expstring_t xpathActCfgPaths = mprintf(
      "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
      "/FileProperties/FileResource/FileProperties/ExcludeFromBuild[text()='true']"
      "/parent::*/parent::*/FilePath/text()",
      actcfg);
    XPathObject theConfigEx(run_xpath(xpathCtx, xpathActCfgPaths));
    Free(xpathActCfgPaths);

    xmlNodeSetPtr nodes = theConfigEx->nodesetval;
    if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
      xmlNodePtr curnode = nodes->nodeTab[i];
      cstring aa((const char*)curnode->content);
      if (!excluded_files.has_key(aa)) {
        excluded_files.add(aa, *p_project_name);
      } else {
        WARNING("Multiple exclusion of file %s", (const char*)curnode->content);
      }
    }
  }

  // Collect files; filter out excluded ones
  {
    XPathObject  filesObj(run_xpath(xpathCtx,
      "TITAN_Project_File_Information/Files/FileResource"));

    xmlNodeSetPtr nodes = filesObj->nodesetval;
    if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
      // nodes->nodeTab[i]->name === "FileResource"
      const char *uri = 0, *path = 0, *raw = 0;

      // projectRelativePath is the path as it appears in Project Explorer (illusion)
      // relativeURI is the actual location, relative to the project root (reality)
      // rawURI is present if the relative path can not be calculated
      //
      // Theoretically these attributes could be in any order, loop over them
      for (xmlAttrPtr attr = nodes->nodeTab[i]->properties; attr; attr = attr->next) {
        if (!strcmp((const char*)attr->name, "projectRelativePath")) {
          path = (const char*)attr->children->content;
        }
        else if (!strcmp((const char*)attr->name, "relativeURI")) {
          uri = (const char*)attr->children->content;
        }
        else if (!strcmp((const char*)attr->name, "rawURI")) {
          raw = (const char*)attr->children->content;
        }
        else {
          WARNING("Unknown attribute %s", (const char*)nodes->nodeTab[i]->name);
        }
      } // next attribute

      if (path == NULL) {
        ERROR("A FileResource must have a projectRelativePath");
        continue;
      }

      if (uri == NULL && raw == NULL) {
        ERROR("A FileResource must have either relativeURI or rawURI");
        continue;
      }

      cstring cpath(path);
      if (!is_excluded_file(cpath, *p_project_name) && !is_excluded_folder(path)) {
        // relativeURI wins over rawURI
        char *ruri = uri ? mcopystr(uri) : cook(raw, path_vars);
        if (files.has_key(cpath)) {
          ERROR("A FileResource %s must be unique!", (const char*)cpath);
        }
        else {
          bool drop = false;
          const char* file_path = ruri;
          expstring_t rel_file_dir = get_dir_from_path(file_path);
          expstring_t file_name = get_file_from_path(file_path);
          expstring_t abs_dir_path = get_absolute_dir(rel_file_dir, abs_tpd_dir, TRUE);
          expstring_t abs_file_name = compose_path_name(abs_dir_path, file_name);
          if (abs_file_name != NULL) {
            if (get_path_status(abs_file_name) == PS_FILE) {
              FILE *fp = fopen(abs_file_name, "r");
              if (fp != NULL) {
                char* ttcn3_module_name;
                if (is_ttcn3_module(abs_file_name, fp, &ttcn3_module_name)) {
                  projGenHelper.addTtcn3ModuleToProject(*p_project_name, ttcn3_module_name);
                }
                Free(ttcn3_module_name);
                char* asn1_module_name;
                if (is_asn1_module(abs_file_name, fp, &asn1_module_name)) {
                  projGenHelper.addAsn1ModuleToProject(*p_project_name, asn1_module_name);
                }
                Free(asn1_module_name);
                char* xsd_module_name = NULL;
                if (is_xsd_module(abs_file_name, &xsd_module_name)) {
                  projGenHelper.addXSDModuleToProject(*p_project_name, xsd_module_name);
                }
                Free(xsd_module_name);
                if (projGenHelper.isCPPSourceFile(file_name)) {
                   projGenHelper.addUserSourceToProject(*p_project_name, file_name);
                }
                if (projGenHelper.isCPPHeaderFile(file_name)) {
                   projGenHelper.addUserHeaderToProject(*p_project_name, file_name);
                }
                if (projGenHelper.isTtcnPPFile(file_name)) {
                   projGenHelper.addTtcnPPToProject(*p_project_name, file_name);
                }
              }
              fclose(fp);
            }else {
              drop = true;
              ERROR("%s does not exist", abs_file_name);
            }
          }
          if(abs_dir_path != NULL && !drop){
            files.add(cpath, ruri); // relativeURI to the TPD location
          }else {
            result = TPD_FAILED;
          }
          { // set the *preprocess value if .ttcnpp file was found
            const size_t ttcnpp_extension_len = 7; // ".ttcnpp"
            const size_t ruri_len = strlen(ruri);
            if ( ruri_len>ttcnpp_extension_len && strcmp(ruri+(ruri_len-ttcnpp_extension_len),".ttcnpp")==0 ) {
              *preprocess = TRUE;
            }
          }
          Free(rel_file_dir);
          Free(file_name);
          Free(abs_dir_path);
          Free(abs_file_name);
        }
      }
    } // next FileResource
  }

  // Check options
  xsdbool2boolean(xpathCtx, actcfg, "useAbsolutePath", p_aflag);
  xsdbool2boolean(xpathCtx, actcfg, "GNUMake", p_gflag);
  if (*p_Zflag) *p_lflag = FALSE;
  xsdbool2boolean(xpathCtx, actcfg, "dynamicLinking", p_lflag);
  xsdbool2boolean(xpathCtx, actcfg, "functiontestRuntime", p_Rflag);
  xsdbool2boolean(xpathCtx, actcfg, "singleMode", p_sflag);
  xsdbool2boolean(xpathCtx, actcfg, "codeSplitting", p_csflag);
  xsdbool2boolean(xpathCtx, actcfg, "quietly", p_quflag);
  xsdbool2boolean(xpathCtx, actcfg, "disableSubtypeChecking", p_dsflag);
  xsdbool2boolean(xpathCtx, actcfg, "disableBER", p_dbflag);
  xsdbool2boolean(xpathCtx, actcfg, "disableRAW", p_drflag);
  xsdbool2boolean(xpathCtx, actcfg, "disableTEXT", p_dtflag);
  xsdbool2boolean(xpathCtx, actcfg, "disableXER", p_dxflag);
  xsdbool2boolean(xpathCtx, actcfg, "disableJSON", p_djflag);
  xsdbool2boolean(xpathCtx, actcfg, "forceXERinASN.1", p_fxflag);
  xsdbool2boolean(xpathCtx, actcfg, "defaultasOmit", p_doflag);
  xsdbool2boolean(xpathCtx, actcfg, "gccMessageFormat", p_gfflag);
  xsdbool2boolean(xpathCtx, actcfg, "lineNumbersOnlyInMessages", p_lnflag);
  xsdbool2boolean(xpathCtx, actcfg, "includeSourceInfo", p_isflag);
  xsdbool2boolean(xpathCtx, actcfg, "addSourceLineInfo", p_asflag);
  xsdbool2boolean(xpathCtx, actcfg, "suppressWarnings", p_swflag);
  xsdbool2boolean(xpathCtx, actcfg, "outParamBoundness", p_Yflag); //not documented, obsolete
  xsdbool2boolean(xpathCtx, actcfg, "forceOldFuncOutParHandling", p_Yflag);
  xsdbool2boolean(xpathCtx, actcfg, "omitInValueList", p_Mflag);
  xsdbool2boolean(xpathCtx, actcfg, "warningsForBadVariants", p_Eflag);
  xsdbool2boolean(xpathCtx, actcfg, "activateDebugger", p_nflag);
  xsdbool2boolean(xpathCtx, actcfg, "disablePredefinedExternalFolder", p_diflag);

  projDesc = projGenHelper.getTargetOfProject(*p_project_name);
  if (projDesc) projDesc->setLinkingStrategy(*p_lflag);

  // Extract the "incremental dependencies" option
  {
    boolean incremental_deps = TRUE;
    xsdbool2boolean(xpathCtx, actcfg, "incrementalDependencyRefresh", &incremental_deps);

    // For makefilegen, "Use GNU make" implies incremental deps by default,
    // unless explicitly disabled by "use makedepend" (a.k.a. mflag).
    // For Eclipse, incremental deps must be explicitly specified,
    // even if GNU make is being used.
    
    if (incremental_deps) {
      if( !(*p_gflag) ) {
        WARNING("Incremental dependency ordered but it requires gnu make");
      }
    }
    else {
      if (*p_gflag) {
        // GNU make but no incremental deps
        *p_mflag = true;
      }
    }
  }
  
  // Extract the makefileScript only for top level
  // In the recursive case the subsequent makefile calls will process the 
  // makefileScript
  if (local_argc != 0)
  {
    char *makefileScriptXpath = mprintf(
      "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
      "/ProjectProperties/LocalBuildSettings/MakefileScript/text()",
      actcfg);
    XPathObject makefileScriptObj(run_xpath(xpathCtx, makefileScriptXpath));
    Free(makefileScriptXpath);
    if (makefileScriptObj->nodesetval && makefileScriptObj->nodesetval->nodeNr > 0) {
      const char* file_path = (const char*)makefileScriptObj->nodesetval->nodeTab[0]->content;
      expstring_t rel_file_dir = get_dir_from_path(file_path);
      expstring_t file_name = get_file_from_path(file_path);
      expstring_t abs_dir_path = get_absolute_dir(rel_file_dir, abs_tpd_dir, TRUE);
      expstring_t abs_file_name = compose_path_name(abs_dir_path, file_name);
      *makefileScript = mcopystr(abs_file_name);
    }
  }

  // Extract the default target option
  // if it is not defined as a command line argument
  if (!(*p_Lflag)) {
    expstring_t defTargetXpath = mprintf(
      "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
      "/ProjectProperties/MakefileSettings/defaultTarget/text()",
      actcfg);
    XPathObject defTargetObj(run_xpath(xpathCtx, defTargetXpath));
    Free(defTargetXpath);
    if (defTargetObj->nodesetval && defTargetObj->nodesetval->nodeNr > 0) {
      const char* content = (const char*)defTargetObj->nodesetval->nodeTab[0]->content;
      if (!strcmp(content, "library")) {
        *p_Lflag = true;
      } else if (!strcmp(content, "executable")) {
        *p_Lflag = false;
      } else {
        ERROR("Unknown default target: '%s'."
            " The available targets are: 'executable', 'library'", content);
      }
    }
    ProjectDescriptor* projDesc = projGenHelper.getTargetOfProject(*p_project_name);
    if (projDesc) projDesc->setLibrary(*p_Lflag);
  }

  // Executable name (don't care unless top-level invocation)
  if (local_argc != 0)
  {
    char *exeXpath = mprintf(
      "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
      "/ProjectProperties/MakefileSettings/targetExecutable/text()",
      actcfg);
    XPathObject exeObj(run_xpath(xpathCtx, exeXpath));
    Free(exeXpath);
    if (exeObj->nodesetval && exeObj->nodesetval->nodeNr > 0) {
      const char* target_executable = (const char*)exeObj->nodesetval->nodeTab[0]->content;
      autostring target_exe_dir(get_dir_from_path(target_executable));
      autostring target_exe_file(get_file_from_path(target_executable));
      if (target_exe_dir!=NULL) { // if it's not only a file name
        if (get_path_status(target_exe_dir)==PS_NONEXISTENT) {
          if (strcmp(real_workdir,target_exe_dir)!=0) {
            WARNING("Provided targetExecutable directory `%s' does not exist, only file name `%s' will be used", (const char*)target_exe_dir, (const char*)target_exe_file);
          }
          target_executable = target_exe_file;
        }
      }
      if (!*p_ets_name) { // Command line will win
        *p_ets_name = mcopystr(target_executable);
      }
    }
  }

  // create an xml element for the currently processed project
  if (prj_graph_fp) {
    const char* prjNameStr = "???";
    XPathObject  prjName(run_xpath(xpathCtx, "/TITAN_Project_File_Information/ProjectName/text()"));
    if (prjName->nodesetval && prjName->nodesetval->nodeNr == 1) {
      prjNameStr = (const char*)prjName->nodesetval->nodeTab[0]->content;
    }
    autostring tpd_rel_dir(get_relative_dir(tpd_dir, NULL));
    autostring tpd_rel_path(compose_path_name(tpd_rel_dir, (const char*)tpd_filename));
    fprintf(prj_graph_fp, "<project name=\"%s\" uri=\"%s\">\n", prjNameStr, (const char*)tpd_rel_path);
    XPathObject subprojects(run_xpath(xpathCtx, "/TITAN_Project_File_Information/ReferencedProjects/ReferencedProject/attribute::name"));
    xmlNodeSetPtr nodes = subprojects->nodesetval;
    if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
      const char* refd_name = "???";
      if (!strcmp((const char*)nodes->nodeTab[i]->name, "name")) {
        refd_name = (const char*)nodes->nodeTab[i]->children->content;
      }
      fprintf(prj_graph_fp, "<reference name=\"%s\"/>\n", refd_name);
    }
    fprintf(prj_graph_fp, "</project>\n");
  }

  // Tpd part of the MakefileSettings
  {
    //TTCN3preprocessorIncludes
    char *preincludeXpath = mprintf(
        "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
        "/ProjectProperties/MakefileSettings/TTCN3preprocessorIncludes/listItem/text()",
        actcfg);
    XPathObject preincludeObj(run_xpath(xpathCtx, preincludeXpath));
    Free(preincludeXpath);

    xmlNodeSetPtr nodes = preincludeObj->nodesetval;

    if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
      char* content = (char*)preincludeObj->nodesetval->nodeTab[i]->content;

      // add includes to the end of list
      if (ttcn3_prep_includes) {
        // go to last element
        struct string_list* last_elem = ttcn3_prep_includes;
        while (last_elem->next) last_elem = last_elem->next;
        // add string to last element if empty or create new last element and add it to that
        if (last_elem->str) {
          last_elem->next = (struct string_list*)Malloc(sizeof(struct string_list));
          last_elem = last_elem->next;
          last_elem->next = NULL;
        }
        replacechar(&content);
        last_elem->str = content;
      }
    }
  }
  {
    //TTCN3preprocessorDefines
    char *ttcn3predefinesXpath = mprintf(
        "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
        "/ProjectProperties/MakefileSettings/TTCN3preprocessorDefines/listItem/text()",
        actcfg);
    XPathObject ttcn3predefinesObj(run_xpath(xpathCtx, ttcn3predefinesXpath));
    Free(ttcn3predefinesXpath);

    xmlNodeSetPtr nodes = ttcn3predefinesObj->nodesetval;

    if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
      const char* content = (const char*)ttcn3predefinesObj->nodesetval->nodeTab[i]->content;

      // add includes to the end of list
      if (ttcn3_prep_defines) {
        // go to last element
        struct string_list* last_elem = ttcn3_prep_defines;
        while (last_elem->next) last_elem = last_elem->next;
        // add string to last element if empty or create new last element and add it to that
        if (last_elem->str) {
          last_elem->next = (struct string_list*)Malloc(sizeof(struct string_list));
          last_elem = last_elem->next;
          last_elem->next = NULL;
        }
        last_elem->str = mcopystr(content);
      }
    }
  }
  {
    //TTCN3preprocessorUnDefines
    char *ttcn3preUndefinesXpath = mprintf(
        "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
        "/ProjectProperties/MakefileSettings/TTCN3preprocessorUndefines/listItem/text()",
        actcfg);
    XPathObject ttcn3preUndefinesObj(run_xpath(xpathCtx, ttcn3preUndefinesXpath));
    Free(ttcn3preUndefinesXpath);

    xmlNodeSetPtr nodes = ttcn3preUndefinesObj->nodesetval;

    if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
      const char* content = (const char*)ttcn3preUndefinesObj->nodesetval->nodeTab[i]->content;

      // add includes to the end of list
      if (ttcn3_prep_undefines) {
        // go to last element
        struct string_list* last_elem = ttcn3_prep_undefines;
        while (last_elem->next) last_elem = last_elem->next;
        // add string to last element if empty or create new last element and add it to that
        if (last_elem->str) {
          last_elem->next = (struct string_list*)Malloc(sizeof(struct string_list));
          last_elem = last_elem->next;
          last_elem->next = NULL;
        }
        last_elem->str = mcopystr(content);
      }
    }
  }

  {
    //preprocessorIncludes
    char *preincludesXpath = mprintf(
        "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
        "/ProjectProperties/MakefileSettings/preprocessorIncludes/listItem/text()",
        actcfg);
    XPathObject preincludesObj(run_xpath(xpathCtx, preincludesXpath));
    Free(preincludesXpath);

    xmlNodeSetPtr nodes = preincludesObj->nodesetval;

    if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
      char* content = (char*)preincludesObj->nodesetval->nodeTab[i]->content;

      // add includes to the end of list
      if (prep_includes) {
        // go to last element
        struct string_list* last_elem = prep_includes;
        while (last_elem->next) last_elem = last_elem->next;
        // add string to last element if empty or create new last element and add it to that
        if (last_elem->str) {
          last_elem->next = (struct string_list*)Malloc(sizeof(struct string_list));
          last_elem = last_elem->next;
          last_elem->next = NULL;
        }
        replacechar(&content);
        last_elem->str = content;
      }
    }
  }
  {
    //preprocessorDefines
    char *predefinesXpath = mprintf(
        "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
        "/ProjectProperties/MakefileSettings/preprocessorDefines/listItem/text()",
        actcfg);
    XPathObject predefinesObj(run_xpath(xpathCtx, predefinesXpath));
    Free(predefinesXpath);

    xmlNodeSetPtr nodes = predefinesObj->nodesetval;

    if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
      const char* content = (const char*)predefinesObj->nodesetval->nodeTab[i]->content;

      // add includes to the end of list
      if (prep_defines) {
        // go to last element
        struct string_list* last_elem = prep_defines;
        while (last_elem->next) last_elem = last_elem->next;
        // add string to last element if empty or create new last element and add it to that
        if (last_elem->str) {
          last_elem->next = (struct string_list*)Malloc(sizeof(struct string_list));
          last_elem = last_elem->next;
          last_elem->next = NULL;
        }
        last_elem->str = mcopystr(content);
      }
    }
  }
  {
    //preprocessorUnDefines
    char *preUndefinesXpath = mprintf(
        "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
        "/ProjectProperties/MakefileSettings/preprocessorUndefines/listItem/text()",
        actcfg);
    XPathObject preUndefinesObj(run_xpath(xpathCtx, preUndefinesXpath));
    Free(preUndefinesXpath);

    xmlNodeSetPtr nodes = preUndefinesObj->nodesetval;

    if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
      const char* content = (const char*)preUndefinesObj->nodesetval->nodeTab[i]->content;

      // add includes to the end of list
      if (prep_undefines) {
        // go to last element
        struct string_list* last_elem = prep_undefines;
        while (last_elem->next) last_elem = last_elem->next;
        // add string to last element if empty or create new last element and add it to that
        if (last_elem->str) {
          last_elem->next = (struct string_list*)Malloc(sizeof(struct string_list));
          last_elem = last_elem->next;
          last_elem->next = NULL;
        }
        last_elem->str = mcopystr(content);
      }
    }
  }
  {
    char *cxxCompilerXpath = mprintf(
            "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
            "/ProjectProperties/MakefileSettings/CxxCompiler/text()",
            actcfg);
    XPathObject cxxCompilerObj(run_xpath(xpathCtx, cxxCompilerXpath));
    Free(cxxCompilerXpath);
    xmlNodeSetPtr nodes = cxxCompilerObj->nodesetval;
    if (nodes) {
      *cxxcompiler = mcopystr((const char*)cxxCompilerObj->nodesetval->nodeTab[0]->content);
    }
  }
  {
    char *optLevelXpath = mprintf(
        "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
        "/ProjectProperties/MakefileSettings/optimizationLevel/text()",
        actcfg);
    XPathObject optLevelObj(run_xpath(xpathCtx, optLevelXpath));
    Free(optLevelXpath);
    xmlNodeSetPtr nodes = optLevelObj->nodesetval;
    if (nodes) {
      *optlevel = mcopystr((const char*)optLevelObj->nodesetval->nodeTab[0]->content);
    }
  }
  {
    char *optFlagsXpath = mprintf(
        "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
        "/ProjectProperties/MakefileSettings/otherOptimizationFlags/text()",
        actcfg);
    XPathObject optFlagsObj(run_xpath(xpathCtx, optFlagsXpath));
    Free(optFlagsXpath);
    xmlNodeSetPtr nodes = optFlagsObj->nodesetval;
    if (nodes) {
      *optflags = mcopystr((const char*)optFlagsObj->nodesetval->nodeTab[0]->content);
    }
  }
  {
    //SolarisSpecificLibraries
    char *solspeclibXpath = mprintf(
        "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
        "/ProjectProperties/MakefileSettings/SolarisSpecificLibraries/listItem/text()",
        actcfg);
    XPathObject solspeclibObj(run_xpath(xpathCtx, solspeclibXpath));
    Free(solspeclibXpath);

    xmlNodeSetPtr nodes = solspeclibObj->nodesetval;

    if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
      char* content = (char*)solspeclibObj->nodesetval->nodeTab[i]->content;

      // add includes to the end of list
      if (solspeclibs) {
        // go to last element
        struct string_list* last_elem =solspeclibs;
        while (last_elem->next) last_elem = last_elem->next;
        // add string to last element if empty or create new last element and add it to that
        if (last_elem->str) {
          last_elem->next = (struct string_list*)Malloc(sizeof(struct string_list));
          last_elem = last_elem->next;
          last_elem->next = NULL;
        }
        replacechar(&content);
        last_elem->str = content;
      }
    }
  }
  {
    //Solaris8SpecificLibraries
    char *sol8speclibXpath = mprintf(
        "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
        "/ProjectProperties/MakefileSettings/Solaris8SpecificLibraries/listItem/text()",
        actcfg);
    XPathObject sol8speclibObj(run_xpath(xpathCtx, sol8speclibXpath));
    Free(sol8speclibXpath);

    xmlNodeSetPtr nodes = sol8speclibObj->nodesetval;

    if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
      char* content = (char*)sol8speclibObj->nodesetval->nodeTab[i]->content;

      // add includes to the end of list
      if (sol8speclibs) {
        // go to last element
        struct string_list* last_elem = sol8speclibs;
        while (last_elem->next) last_elem = last_elem->next;
        // add string to last element if empty or create new last element and add it to that
        if (last_elem->str) {
          last_elem->next = (struct string_list*)Malloc(sizeof(struct string_list));
          last_elem = last_elem->next;
          last_elem->next = NULL;
        }
        replacechar(&content);
        last_elem->str = content;
      }
    }
  }
  {
    //LinuxSpecificLibraries
    char *linuxspeclibXpath = mprintf(
        "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
        "/ProjectProperties/MakefileSettings/LinuxSpecificLibraries/listItem/text()",
        actcfg);
    XPathObject linuxspeclibObj(run_xpath(xpathCtx, linuxspeclibXpath));
    Free(linuxspeclibXpath);

    xmlNodeSetPtr nodes = linuxspeclibObj->nodesetval;

    if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
      char* content = (char*)linuxspeclibObj->nodesetval->nodeTab[i]->content;

      // add includes to the end of list
      if (linuxspeclibs) {
        // go to last element
        struct string_list* last_elem = linuxspeclibs;
        while (last_elem->next) last_elem = last_elem->next;
        // add string to last element if empty or create new last element and add it to that
        if (last_elem->str) {
          last_elem->next = (struct string_list*)Malloc(sizeof(struct string_list));
          last_elem = last_elem->next;
          last_elem->next = NULL;
        }
        replacechar(&content);
        last_elem->str = content;
      }
    }
  }
  {
    //FreeBSDSpecificLibraries
    char *freebsdspeclibXpath = mprintf(
        "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
        "/ProjectProperties/MakefileSettings/FreeBSDSpecificLibraries/listItem/text()",
        actcfg);
    XPathObject freebsdspeclibObj(run_xpath(xpathCtx, freebsdspeclibXpath));
    Free(freebsdspeclibXpath);

    xmlNodeSetPtr nodes = freebsdspeclibObj->nodesetval;

    if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
      char* content = (char*)freebsdspeclibObj->nodesetval->nodeTab[i]->content;

      // add includes to the end of list
      if (freebsdspeclibs) {
        // go to last element
        struct string_list* last_elem = freebsdspeclibs;
        while (last_elem->next) last_elem = last_elem->next;
        // add string to last element if empty or create new last element and add it to that
        if (last_elem->str) {
          last_elem->next = (struct string_list*)Malloc(sizeof(struct string_list));
          last_elem = last_elem->next;
          last_elem->next = NULL;
        }
        replacechar(&content);
        last_elem->str = content;
      }
    }
  }
  {
    //Win32SpecificLibraries
    char *win32speclibXpath = mprintf(
        "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
        "/ProjectProperties/MakefileSettings/Win32SpecificLibraries/listItem/text()",
        actcfg);
    XPathObject win32speclibObj(run_xpath(xpathCtx, win32speclibXpath));
    Free(win32speclibXpath);

    xmlNodeSetPtr nodes = win32speclibObj->nodesetval;

    if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
      char* content = (char*)win32speclibObj->nodesetval->nodeTab[i]->content;

      // add includes to the end of list
      if (win32speclibs) {
        // go to last element
        struct string_list* last_elem = win32speclibs;
        while (last_elem->next) last_elem = last_elem->next;
        // add string to last element if empty or create new last element and add it to that
        if (last_elem->str) {
          last_elem->next = (struct string_list*)Malloc(sizeof(struct string_list));
          last_elem = last_elem->next;
          last_elem->next = NULL;
        }
        replacechar(&content);
        last_elem->str = content;
      }

    }
  }
  {
    //TTCN3preprocessor
    char *ttcn3preproc = mprintf(
        "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
        "/ProjectProperties/MakefileSettings/TTCN3preprocessor/text()",
        actcfg);
    XPathObject ttcn3preprocObj(run_xpath(xpathCtx, ttcn3preproc));
    Free(ttcn3preproc);
    xmlNodeSetPtr nodes = ttcn3preprocObj->nodesetval;
    if (nodes) {
      *ttcn3prep = mcopystr((const char*)ttcn3preprocObj->nodesetval->nodeTab[0]->content);
    }
  }
  {
    // additionalObjects
    char *additionalObjectsXpath = mprintf(
        "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
        "/ProjectProperties/MakefileSettings/additionalObjects/listItem/text()",
        actcfg);
    XPathObject additionalObjectsObj(run_xpath(xpathCtx, additionalObjectsXpath));
    Free(additionalObjectsXpath);

    xmlNodeSetPtr nodes = additionalObjectsObj->nodesetval;

    if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
      char* content = (char*)additionalObjectsObj->nodesetval->nodeTab[i]->content;

      // add to the end of list
      if (additionalObjects) {
        // go to last element
        struct string_list* last_elem = additionalObjects;
        while (last_elem->next) last_elem = last_elem->next;
        // add string to last element if empty or create new last element and add it to that
        if (last_elem->str) {
          last_elem->next = (struct string_list*)Malloc(sizeof(struct string_list));
          last_elem = last_elem->next;
          last_elem->next = NULL;
        }
        replacechar(&content);
        last_elem->str = content;
      }
    }
  }
  {
    //The project name needed the hierarchical projects
    char* prjNameStr = 0;
    char *prjNameStrXpath = mprintf("/TITAN_Project_File_Information/ProjectName/text()");
    XPathObject  prjName(run_xpath(xpathCtx, prjNameStrXpath));
    if (prjName->nodesetval && prjName->nodesetval->nodeNr == 1) {
      prjNameStr = (char*)prjName->nodesetval->nodeTab[0]->content;
    }
    Free(prjNameStrXpath);
    append_to_library_list (prjNameStr, xpathCtx, actcfg);

    //linkerLibraries
    char *linkerlibsXpath = mprintf(
        "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
        "/ProjectProperties/MakefileSettings/linkerLibraries/listItem/text()",
        actcfg);
    XPathObject linkerlibsObj(run_xpath(xpathCtx, linkerlibsXpath));
    Free(linkerlibsXpath);

    xmlNodeSetPtr nodes = linkerlibsObj->nodesetval;

    if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
      char* content = (char*)linkerlibsObj->nodesetval->nodeTab[i]->content;

      // add includes to the end of list
      if (linkerlibs) {
        // go to last element
        struct string_list* last_elem = linkerlibs;
        while (last_elem->next) last_elem = last_elem->next;
        // add string to last element if empty or create new last element and add it to that
        if (last_elem->str) {
          last_elem->next = (struct string_list*)Malloc(sizeof(struct string_list));
          last_elem = last_elem->next;
          last_elem->next = NULL;
        }
        replacechar(&content);
        last_elem->str = content;

        ProjectDescriptor* projDesc = projGenHelper.getTargetOfProject(*p_project_name);
        if (projDesc) projDesc->addToLinkerLibs(last_elem->str);
      }
    }
  }
  {
    //linkerLibrarySearchPath
    char *linkerlibsearchXpath = mprintf(
        "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
        "/ProjectProperties/MakefileSettings/linkerLibrarySearchPath/listItem/text()",
        actcfg);
    XPathObject linkerlibsearchObj(run_xpath(xpathCtx, linkerlibsearchXpath));
    Free(linkerlibsearchXpath);

    xmlNodeSetPtr nodes = linkerlibsearchObj->nodesetval;

    if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
      char* content = (char*)linkerlibsearchObj->nodesetval->nodeTab[i]->content;

      // add includes to the end of list
      if (linkerlibsearchp) {
        // go to last element
        struct string_list* last_elem = linkerlibsearchp;
        while (last_elem->next) last_elem = last_elem->next;
        // add string to last element if empty or create new last element and add it to that
        if (last_elem->str) {
          last_elem->next = (struct string_list*)Malloc(sizeof(struct string_list));
          last_elem = last_elem->next;
          last_elem->next = NULL;
        }
        replacechar(&content);
        last_elem->str = content;

        ProjectDescriptor* projDesc = projGenHelper.getTargetOfProject(*p_project_name);
        if (projDesc) projDesc->addToLibSearchPaths(last_elem->str);
      }
    }
  }

  if (generatorCommandOutput && local_argc != 0) { // only in case of top-level invocation
    char* generatorCommandXpath = mprintf(
      "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
      "/ProjectProperties/MakefileSettings/ProjectSpecificRulesGenerator/GeneratorCommand/text()",
      actcfg);
    XPathObject generatorCommandObj(run_xpath(xpathCtx, generatorCommandXpath));
    Free(generatorCommandXpath);
    autostring generatorCommand;
    if (generatorCommandObj->nodesetval && generatorCommandObj->nodesetval->nodeNr > 0) {
      generatorCommand = mcopystr((const char*)generatorCommandObj->nodesetval->nodeTab[0]->content);
      // run the command and capture the output
      printf("Executing generator command `%s' specified in `%s'...\n", (const char*)generatorCommand, (const char*)abs_tpd_name);
      FILE* gc_fp = popen(generatorCommand, "r");
      if (!gc_fp) {
        ERROR("Could not execute command `%s'", (const char*)generatorCommand);
      } else {
        char buff[1024];
        while (fgets(buff, sizeof(buff), gc_fp)!=NULL) {
          *generatorCommandOutput = mputstr(*generatorCommandOutput, buff);
        }
        pclose(gc_fp);
      }
    }
  }

  if (target_placement_list && local_argc != 0) { // only in case of top-level invocation
    char* targetPlacementXpath = mprintf(
      "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
      "/ProjectProperties/MakefileSettings/ProjectSpecificRulesGenerator/Targets/Target/attribute::*",
      actcfg);
    XPathObject targetPlacementObj(run_xpath(xpathCtx, targetPlacementXpath));
    Free(targetPlacementXpath);
    xmlNodeSetPtr nodes = targetPlacementObj->nodesetval;
    const char* targetName = NULL;
    const char* targetPlacement = NULL;
    if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
      if (!strcmp((const char*)nodes->nodeTab[i]->name, "name")) {
        targetName = (const char*)nodes->nodeTab[i]->children->content;
      }
      else if (!strcmp((const char*)nodes->nodeTab[i]->name,"placement")) {
        targetPlacement = (const char*)nodes->nodeTab[i]->children->content;
      }
      if (targetName && targetPlacement) { // collected both
        if (target_placement_list) {
          // go to last element
          struct string2_list* last_elem = target_placement_list;
          while (last_elem->next) last_elem = last_elem->next;
          // add strings to last element if empty or create new last element and add it to that
          if (last_elem->str1) {
            last_elem->next = (struct string2_list*)Malloc(sizeof(struct string2_list));
            last_elem = last_elem->next;
            last_elem->next = NULL;
          }
          last_elem->str1 = mcopystr(targetName);
          last_elem->str2 = mcopystr(targetPlacement);
        }
        targetName = targetPlacement = NULL; // forget both
      }
    }
  }
  
  {
    // profiler file name
    char* profilerXpath = mprintf(
      "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
      "/ProjectProperties/MakefileSettings/profiledFileList", actcfg);
    XPathObject profiledFilesNameObj(run_xpath(xpathCtx, profilerXpath));
    Free(profilerXpath);
    xmlNodeSetPtr nodes = profiledFilesNameObj->nodesetval;
    if (nodes && nodes->nodeNr > 0) {
      const char *uri = 0, *path = 0, *raw = 0;
      for (xmlAttrPtr attr = nodes->nodeTab[0]->properties; attr; attr = attr->next) {
        if (!strcmp((const char*)attr->name, "projectRelativePath")) {
          path = (const char*)attr->children->content;
        }
        else if (!strcmp((const char*)attr->name, "relativeURI")) {
          uri = (const char*)attr->children->content;
        }
        else if (!strcmp((const char*)attr->name, "rawURI")) {
          raw = (const char*)attr->children->content;
        }
        else {
          WARNING("Unknown attribute %s", (const char*)nodes->nodeTab[0]->name);
        }
      } // next attribute
      
      if (path == NULL) {
        ERROR("A profiledFileList must have a projectRelativePath");
      }
      else {
        if (uri == NULL && raw == NULL) {
          ERROR("A profiledFileList must have either relativeURI or rawURI");
        }
        else {
          cstring cpath(path);
          if (files.has_key(cpath)) {
            ERROR("profiledFileLists and FileResources must be unique!");
          }
          else {
            // add the file to the end of the list
            struct string_list* new_item = NULL;
            if (*profiled_file_list == NULL) {
              *profiled_file_list = (struct string_list*)Malloc(sizeof(struct string_list));
              new_item = *profiled_file_list;
            }
            else {
              new_item = *profiled_file_list;
              while(new_item->next != NULL) {
                new_item = new_item->next;
              }
              new_item->next = (struct string_list*)Malloc(sizeof(struct string_list));
              new_item = new_item->next;
            }
            new_item->str = mcopystr(path);
            new_item->next = NULL;
            
            // add the file to the map of files to be copied to the working directory
            char *ruri = uri ? mcopystr(uri) : cook(raw, path_vars);
            files.add(cpath, ruri);
          }
        }
      }
    }
  }
  } // If get_config_mode
// collect the required configurations
  
  struct config_struct* config_elem = all_configs;
  {
    if (required_configs && get_config_mode) {

      char* cfgConfigsXpath(mprintf(
        "/TITAN_Project_File_Information/Configurations/Configuration"));
      XPathObject cfgConfigsObjects(run_xpath(xpathCtx, cfgConfigsXpath));
      Free(cfgConfigsXpath);
      xmlNodeSetPtr configs = cfgConfigsObjects->nodesetval;
      if (configs) {
        // Go through configurations
        for (int i = 0; i < configs->nodeNr; ++i) {
          xmlAttrPtr currentNode = configs->nodeTab[i]->properties;
          const char* configName = NULL;
          
          for (xmlAttrPtr attr = currentNode; attr; attr = attr->next) {
            if (!strcmp((const char*)attr->name, "name")) {
              configName = (const char*)attr->children->content;
            }
          }
          char* cfgReqsXpath(mprintf(
            "/TITAN_Project_File_Information/Configurations/Configuration[@name='%s']"
            "/ProjectProperties/ConfigurationRequirements/configurationRequirement"
            , configName));

          while(config_elem->next != NULL) {
            config_elem = config_elem->next;
          }
          config_elem->project_name = mcopystr(*p_project_name);
          config_elem->project_conf = mcopystr(configName);
          config_elem->is_active = strcmp(configName, actcfg) == 0;
          struct config_struct* next_elem = (struct config_struct*)Malloc(sizeof(struct config_struct));
          config_struct_init(next_elem);
          config_elem->next = next_elem;

          XPathObject reqcfgObjects(run_xpath(xpathCtx, cfgReqsXpath));
          Free (cfgReqsXpath);
          xmlNodeSetPtr reqConfigs = reqcfgObjects->nodesetval;
          // Go through the required configurations of configurations
          if (reqConfigs) for (int j = 0; j < reqConfigs->nodeNr; ++j) {
            xmlNodePtr curNodePtr = reqConfigs->nodeTab[j]->children;
            const char* projectName = NULL;
            const char* reqConfig = NULL;
            while(curNodePtr) {
              if (!strcmp((const char*)curNodePtr->name, "projectName")) {
                projectName = (const char*)curNodePtr->children->content;
              }
              if (!strcmp((const char*)curNodePtr->name, "rerquiredConfiguration") || // backward compatibility
                  !strcmp((const char*)curNodePtr->name, "requiredConfiguration")) {
                  reqConfig = (const char*)curNodePtr->children->content;
              }
              curNodePtr = curNodePtr->next;
            }
            
            // Check if the required configuration is already inserted.
            struct string2_list* last_req = config_elem->requirements;
            boolean already_in = FALSE;
            while (last_req && last_req->str1 != NULL && last_req->str2 != NULL && already_in == FALSE) {
              if (strcmp(last_req->str1, projectName) == 0 && strcmp(last_req->str2, reqConfig) == 0) {
                already_in = TRUE;
                break;
              }
              last_req = last_req->next;
            }
            // If not, insert into the requirements of the configuration
            if (already_in == FALSE) {
              last_req->str1 = mcopystr(projectName);
              last_req->str2 = mcopystr(reqConfig);
              struct string2_list* next_req = (struct string2_list*)Malloc(sizeof(struct string2_list));
              next_req->str1 = NULL;
              next_req->str2 = NULL;
              next_req->next = NULL;
              last_req->next = next_req;
              last_req = next_req;
            }

          } // ReqConfigs
        } // Configs
      } // If configs
    }
  }
  // Referenced projects
  {
    XPathObject subprojects(run_xpath(xpathCtx,
      "/TITAN_Project_File_Information/ReferencedProjects/ReferencedProject"));
    xmlNodeSetPtr nodes = subprojects->nodesetval;
    //Go through ReferencedProjects
    if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
      const char *name = NULL, *projectLocationURI = NULL;
      char *tpdName_loc = NULL;
      // FIXME: this assumes every ReferencedProject has name and URI.
      // This is not necessarily so if the referenced project was closed
      // when the project was exported to TPD.
      // Luckily, the name from the closed project will be overwritten
      // by the name from the next ReferencedProject. However, if some pervert
      // changes the next ReferencedProject to have the projectLocationURI
      // as the first attribute, it will be joined to the name
      // of the previous, closed, ReferencedProject.
      
      //Go through attributes
      for (xmlAttrPtr attr = nodes->nodeTab[i]->properties; attr; attr = attr->next) {
        if (!strcmp((const char*)attr->name, "name")) {
          name = (const char*)attr->children->content;
        }
        else if (!strcmp((const char*)attr->name,"projectLocationURI")) {
          projectLocationURI = (const char*)attr->children->content;
        }
        else if (!strcmp((const char*)attr->name, "tpdName")) {
          //Allocate memory
          tpdName_loc = mcopystr((char*)attr->children->content);
        }
      }
      //We don't want to orerride an absolute location with -I, tpdName remains NULL
      boolean not_abs_path = projectLocationURI &&
#if defined WIN32 && defined MINGW
	/* On native Windows the absolute path name shall begin with
	 * a drive letter, colon and backslash */
	(((projectLocationURI[0] < 'A' || projectLocationURI[0] > 'Z') &&
	  (projectLocationURI[0] < 'a' || projectLocationURI[0] > 'z')) ||
	 projectLocationURI[1] != ':' || projectLocationURI[2] != '\\');
#else
	/* On UNIX-like systems the absolute path name shall begin with
	 * a slash */
	projectLocationURI[0] != '/';
#endif
      if (!tpdName_loc && not_abs_path) {
        //Allocate memory: +5 because .tpd + closing 0
        tpdName_loc = (char*)Malloc((strlen(name) + 5) * sizeof(char));
        //Default name: name + .tpd
        strcpy(tpdName_loc, name);
        strcat(tpdName_loc, ".tpd");
      }else if (!not_abs_path) {
        Free(tpdName_loc);
        tpdName_loc = NULL;
      }
      
      if (name && projectLocationURI) { // collected both
        // see if there is a specified configuration for the project

        ProjectDescriptor* projDesc = projGenHelper.getTargetOfProject(*p_project_name);
        if (projDesc) projDesc->addToReferencedProjects(name);
         
        const char *my_actcfg = NULL;
        int my_argc = 0;
        char *my_args[] = { NULL };
        char **my_argv = my_args + 0;
        int my_optind = 0;
        boolean my_gflag = *p_gflag, my_aflag = *p_aflag, my_cflag = *p_cflag, // pass down
          my_Rflag = *p_Rflag, my_Pflag = *p_Pflag, my_Zflag = *p_Zflag, my_Hflag = *p_Hflag,
          my_sflag =  0, my_Lflag =  0, my_lflag =  0, my_mflag =  0, my_csflag = 0,
          my_quflag = 0, my_dsflag = 0, my_dbflag = 0, my_drflag = 0,
          my_dtflag = 0, my_dxflag = 0, my_djflag = 0, my_fxflag = 0, my_doflag = 0, 
          my_gfflag = 0, my_lnflag = 0, my_isflag = 0, my_asflag = 0, 
          my_swflag = 0, my_Yflag = 0, my_Mflag = *p_Mflag, my_Eflag = 0, my_nflag = *p_nflag,
          my_diflag = *p_diflag;

        char *my_ets = NULL;
        char *my_proj_name = NULL;
        autostring abs_projectLocationURI;
        if (not_abs_path) {
          abs_projectLocationURI = compose_path_name(abs_tpd_dir, projectLocationURI);
        } else {
          //If absolute directory, then just copy the URI
          abs_projectLocationURI = mcopystr(projectLocationURI);
        }

        char* sub_proj_abs_work_dir = NULL;
        
        //Insert children of the project
        if (get_config_mode && config_elem->project_name != NULL) {
          struct config_struct* tmp = all_configs;
          boolean already_in = FALSE;
          while (tmp && tmp->project_name != NULL && already_in == FALSE) {
            if (strcmp(tmp->project_name, config_elem->project_name) == 0) {
              struct string_list* last_child = tmp->children;
              while(last_child && last_child->str != NULL) {
                if (strcmp(last_child->str, name) == 0) {
                  already_in = TRUE;
                  break;
                }
                last_child = last_child->next;
              }
              if (already_in == FALSE) {
                last_child->str = mcopystr(name);
                struct string_list* next_child = (struct string_list*)Malloc(sizeof(string_list));
                next_child->str = NULL;
                next_child->next = NULL;
                last_child->next = next_child;
                last_child = next_child;
              }
            }
            tmp = tmp->next;
          }
        }
      
        tpd_result success = process_tpd_internal((const char*)abs_projectLocationURI, tpdName_loc,
          my_actcfg, file_list_path, &my_argc, &my_argv, &my_optind, &my_ets, &my_proj_name,
          &my_gflag, &my_sflag, &my_cflag, &my_aflag, preprocess, &my_Rflag, &my_lflag,
          &my_mflag, &my_Pflag, &my_Lflag, recursive, force_overwrite, gen_only_top_level, NULL, &sub_proj_abs_work_dir,
          sub_project_dirs, program_name, prj_graph_fp, create_symlink_list, ttcn3_prep_includes, ttcn3_prep_defines, ttcn3_prep_undefines, 
          prep_includes, prep_defines, prep_undefines, &my_csflag,
          &my_quflag, &my_dsflag, cxxcompiler, optlevel, optflags, &my_dbflag, &my_drflag,
          &my_dtflag, &my_dxflag, &my_djflag, &my_fxflag, &my_doflag,
          &my_gfflag, &my_lnflag, &my_isflag, &my_asflag, &my_swflag, &my_Yflag, &my_Mflag, &my_Eflag, &my_nflag, &my_diflag,
          solspeclibs, sol8speclibs, linuxspeclibs, freebsdspeclibs, win32speclibs,
          ttcn3prep, linkerlibs, additionalObjects, linkerlibsearchp, Vflag, FALSE, &my_Zflag, 
          &my_Hflag, NULL, NULL, prefix_workdir, run_command_list, seen_tpd_files, required_configs, profiled_file_list,
          search_paths, n_search_paths, makefileScript, all_configs);
        
        autostring sub_proj_abs_work_dir_as(sub_proj_abs_work_dir); // ?!

        if (success == TPD_SUCCESS && !get_config_mode) {
          my_actcfg = get_act_config(required_configs, my_proj_name);
          if (recursive) { // call ttcn3_makefilegen on referenced project's tpd file
            // -r is not needed any more because top level process traverses all projects recursively
            expstring_t command = mprintf("%s -cVD", program_name);
            if (force_overwrite) command = mputc(command, 'f');
            if (prefix_workdir) command = mputc(command, 'W');
            if (*p_gflag) command = mputc(command, 'g');
            if (*p_sflag) command = mputc(command, 's');
            if (*p_aflag) command = mputc(command, 'a');
            if (*p_Rflag) command = mputc(command, 'R');
            if (*p_lflag) command = mputc(command, 'l');
            if (*p_mflag) command = mputc(command, 'm');
            if (*p_Zflag) command = mputc(command, 'Z');
            if (*p_Hflag) command = mputc(command, 'H');
            command = mputstr(command, " -t ");
            command = mputstr(command, (const char*)abs_projectLocationURI);
            if (my_actcfg) {
              command = mputstr(command, " -b ");
              command = mputstr(command, my_actcfg);
            }

            autostring sub_tpd_dir(get_dir_from_path((const char*)abs_projectLocationURI));
            const char * sub_proj_effective_work_dir = sub_proj_abs_work_dir ? sub_proj_abs_work_dir : (const char*)sub_tpd_dir;
            if (!gen_only_top_level) {
              if (run_command_list) {
                // go to last element
                struct string2_list* last_elem = run_command_list;
                while (last_elem->next) last_elem = last_elem->next;
                // add strings to last element if empty or create new last element and add it to that
                if (last_elem->str1 || last_elem->str2) {
                  last_elem->next = (struct string2_list*)Malloc(sizeof(struct string2_list));
                  last_elem = last_elem->next;
                  last_elem->next = NULL;
                }
                last_elem->str1 = mcopystr(sub_proj_effective_work_dir);
                last_elem->str2 = command;
              } else {
                ERROR("Internal error: cannot add command to list");
              }
            }
            // add working dir to the end of list
            if (sub_project_dirs) {
              // go to last element
              struct string_list* last_elem = sub_project_dirs;
              while (last_elem->next) last_elem = last_elem->next;
              // add string to last element if empty or create new last element and add it to that
              if (last_elem->str) {
                last_elem->next = (struct string_list*)Malloc(sizeof(struct string_list));
                last_elem = last_elem->next;
                last_elem->next = NULL;
              }
              autostring cwd_as(get_working_dir());
              last_elem->str = (*p_aflag) ? mcopystr(sub_proj_effective_work_dir) : get_relative_dir(sub_proj_effective_work_dir, (const char*)cwd_as);
            }
          }

          for (int z = 0; z < my_argc; ++z) {
            if (*p_cflag) {
              // central storage, keep in separate container
              base_files.add(my_argv[z]); // string was allocated with new
            }
            else {
              const cstring tmp(my_argv[z]);
              if (!files.has_key(tmp)){
                files.add(tmp, my_argv[z]);
              } else {
                Free(my_argv[z]);
              }
            }
          }

          Free(my_argv); // free the array; we keep the pointers
          Free(my_ets);
          Free(my_proj_name);
        }
        else if (success == TPD_FAILED) {
          ERROR("Failed to process %s", (const char*)abs_projectLocationURI);
          result = TPD_FAILED;
        }
        // else TPD_SKIPPED, keep quiet
        Free(tpdName_loc);
        name = projectLocationURI = tpdName_loc = NULL; // forget all
      }
    } // next referenced project
  }

  if (output_file) {
    if (get_path_status(output_file) == PS_DIRECTORY) {
      // points to existing dir; use as-is
    }
    else { // we assume it points to a file: not our problem
      output_file = NULL;
    }
  }

  // (argc - optind) is the number of non-option arguments (assumed to be files)
  // given on the command line.
  int new_argc = local_argc - local_optind + files.size() + base_files.size();
  char ** new_argv = (char**)Malloc(sizeof(char*) * new_argc);

  int n = 0;

  // First, copy the filenames gathered from the TPD
  //
  // We symlink the files into the working directory
  // and pass only the filename to the makefile generator
  for (int nf = files.size(); n < nf; ++n) {
    const char *fn = files.get_nth_elem(n); // relativeURI to the TPD location
    autostring  dir_n (get_dir_from_path (fn));
    autostring  file_n(get_file_from_path(fn));
    autostring  rel_n (get_absolute_dir(dir_n, abs_tpd_dir, TRUE));
    autostring  abs_n (compose_path_name(rel_n, file_n));

    if (local_argc == 0) {
      // We are being invoked recursively, for a referenced TPD.
      // Do not symlink; just return absolute paths to the files.
      if (*fn == '/') {
        if (*p_cflag) {
          // compose with workdir
          new_argv[n] = compose_path_name(abs_workdir, file_n);
        } else {
          // it's an absolute path, copy verbatim
          new_argv[n] = mcopystr(fn); // fn will be destroyed, pass a copy
        }
      }
      else { // relative path
        if (*p_cflag) {
          // compose with workdir
          new_argv[n] = compose_path_name(abs_workdir, file_n);
          // Do not call file_n.extract() : the composed path will be returned,
          // its component will need to be deallocated here.
        }
        else {
          // compose with tpd dir
          new_argv[n] = const_cast<char*>(abs_n.extract());
        }
      }
    }
    else { // we are processing the top-level TPD
#ifndef MINGW
      if (!*p_Pflag) {
        int fd = open(abs_n, O_RDONLY);
        if (fd >= 0) { // successfully opened
          close(fd);
          if (output_file) {
            file_n = compose_path_name(output_file, file_n);
          }
//TODO ! compose with output_file
          // save into list: add symlink data to the end of list
          if (create_symlink_list) {
            // go to last element
            struct string2_list* last_elem = create_symlink_list;
            while (last_elem->next) last_elem = last_elem->next;
            // add strings to last element if empty or create new last element and add it to that
            if (last_elem->str1) {
              last_elem->next = (struct string2_list*)Malloc(sizeof(struct string2_list));
              last_elem = last_elem->next;
              last_elem->next = NULL;
            }
            last_elem->str1 = mcopystr(abs_n);
            last_elem->str2 = mcopystr(file_n);
          } 
        }
        else {
          ERROR("%s does not exist", (const char*)abs_n);
        }
      }
#endif
      if (*p_Pflag) {
        if (*p_aflag) {
          puts((const char *)abs_n);
        } else {
          autostring dir_part(get_dir_from_path(abs_n));
          autostring file_part(get_file_from_path(abs_n));
          autostring rel_dir_part(get_relative_dir((const char *)dir_part, file_list_path ? file_list_path : (const char *)abs_tpd_dir));
          autostring rel_dir_file_part(compose_path_name((const char *)rel_dir_part, (const char *)file_part));
          puts((const char *)rel_dir_file_part);
        }
      }
      new_argv[n] = const_cast<char *>(file_n.extract());
    }
  }
  // Print the TPD too.
  if (*p_Pflag) {
    autostring dir_part(get_dir_from_path(p_tpd_name));
    autostring file_part(get_file_from_path(p_tpd_name));
    if (*p_aflag) {
      puts((const char *)abs_tpd_name);
    } else {
      autostring rel_dir_part(get_relative_dir(dir_part, file_list_path ? file_list_path : abs_tpd_dir));
      autostring rel_dir_file_part(compose_path_name(rel_dir_part, file_part));
      const char *rel_tpd_name = (const char *)rel_dir_file_part;
      puts(rel_tpd_name);
    }
  }

  // base_files from referenced projects
  for (size_t bf = 0, bs = base_files.size(); bf < bs; ++bf, ++n) {
    new_argv[n] = base_files[bf];
  }
  base_files.clear(); // string ownership transfered

  // Then, copy the filenames from the command line.
  for (int a = *p_optind; a < *p_argc; ++a, ++n) {
    // String may be from main's argv; copy to the heap.
    new_argv[n] = mcopystr((*p_argv)[a]);
  }

  if (local_argc > 0) { // it is the outermost call
    clear_seen_tpd_files(seen_tpd_files);
  }
  // replace argv only if not config mode
  if (!get_config_mode) {
    *p_argv = new_argv;
    *p_argc = new_argc;
    *p_optind = 0;
  } else {
    Free(*p_project_name);
  }

  // finally...
  for (size_t i = 0, e = files.size(); i < e; ++i) {
    Free(const_cast<char*>(files.get_nth_elem(i)));
  }
  files.clear();

  for (size_t i = 0, e = folders.size(); i < e; ++i) {
    Free(const_cast<char*>(folders.get_nth_elem(i)));
  }
  folders.clear();
  
  if(free_name) {
    Free((char*)p_tpd_name);
  }

  excluded_files.clear();
  excluded_folders.clear();
  path_vars.clear();

  xmlCleanupParser();
  // ifdef debug
    xmlMemoryDump();
  return result;
}