summaryrefslogtreecommitdiff
path: root/support/dktools/dk4appop.c
blob: a760d2c49d31a2026706492156c4a6e89b10f79f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
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
/*
	WARNING: This file was generated by dkct.
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk4appop.ctr
*/

/*
Copyright (C) 2015-2017, Dirk Krause

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above opyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used
  to endorse or promote products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#line 10 "dk4appop.ctr"

/**	@file
	Open and close dk4_app_t structure.
*/

#ifndef DK4APP_H_INCLUDED
#include "dk4app.h"
#endif

#ifndef DK4STT_H_INCLUDED
#include "dk4stt.h"
#endif

#ifndef DK4CONST_H_INCLUDED
#include "dk4const.h"
#endif

#ifndef DK4MEM_H_INCLUDED
#include "dk4mem.h"
#endif

#ifndef	DK4STR8_H_INCLUDED
#include "dk4str8.h"
#endif

#ifndef	DK4STRW_H_INCLUDED
#include "dk4strw.h"
#endif

#ifndef DK4STRD_H_INCLUDED
#include "dk4strd.h"
#endif

#ifndef DK4ENC_H_INCLUDED
#include "dk4enc.h"
#endif

#ifndef DK4LOC_H_INCLUDED
#include "dk4loc.h"
#endif

#ifndef DK4MPL_H_INCLUDED
#include "dk4mpl.h"
#endif

#ifndef DK4EXEPD_H_INCLUDED
#include "dk4exepd.h"
#endif

#ifndef DK4MAO8D_H_INCLUDED
#include "dk4mao8d.h"
#endif

#ifndef DK4INST_H_INCLUDED
#include "dk4inst.h"
#endif

#ifndef DK4PREF_H_INCLUDED
#include "dk4pref.h"
#endif

#ifndef DK4USER_H_INCLUDED
#include "dk4user.h"
#endif

#ifndef DK4FS_H_INCLUDED
#include "dk4fs.h"
#endif

#ifndef DK4STRMR_H_INCLUDED
#include "dk4strmr.h"
#endif

#ifndef DK4TSPDK_H_INCLUDED
#include "dk4tspdk.h"
#endif

#ifndef DK4FPUT_H_INCLUDED
#include "dk4fput.h"
#endif

#ifndef DK4HNAMED_H_INCLUDED
#include "dk4hnamed.h"
#endif

#ifndef DK4MAIDDDI_H_INCLUDED
#include "dk4maidddi.h"
#endif

#ifndef DK4MAIDDDU_H_INCLUDED
#include "dk4maidddu.h"
#endif

#ifndef DK4MAIDDSZ_H_INCLUDED
#include "dk4maiddsz.h"
#endif

#ifndef DK4MAIDDBL_H_INCLUDED
#include "dk4maiddbl.h"
#endif

#ifndef DK4MAODD_H_INCLUDED
#include "dk4maodd.h"
#endif

#ifndef DK4NUML_H_INCLUDED
#include "dk4numl.h"
#endif

#ifndef DK4STO_H_INCLUDED
#include "dk4sto.h"
#endif

#ifndef DK4APPI_C_INCLUDED
#include "dk4appi.c"
#endif

#ifndef DK4PATHD_H_INCLUDED
#include "dk4pathd.h"
#endif

#ifndef DK4STATD_H_INCLUDED
#include "dk4statd.h"
#endif

#ifndef DK4STAT_H_INCLUDED
#include "dk4stat.h"
#endif

#ifndef DK4REC21_H_INCLUDED
#include "dk4rec21.h"
#endif

#ifndef DK4TIME_H_INCLUDED
#include "dk4time.h"
#endif

#ifndef DK4TIMEDK_H_INCLUDED
#include "dk4timedk.h"
#endif

#ifndef DK4GETPID_H_INCLUDED
#include "dk4getpid.h"
#endif

#ifndef DK4APPMKDH_H_INCLUDED
#include "dk4appmkdh.h"
#endif

#ifndef DK4DELFILE_H_INCLUDED
#include "dk4delfile.h"
#endif


#ifdef __cplusplus
extern "C" {
#endif

/**	Log at application startup.
	@param	app	Application structure.
*/
void
dk4app_log_startup_debug(dk4_app_t *app);

#ifdef __cplusplus
}
#endif





#line 181 "dk4appop.ctr"



static const dkChar * const	dk4app_open_long_opt[] = {
/* 0 */
dkT("help"),

/* 1 */
dkT("version"),

/* 2 */
dkT("license"),

/* 3 */
dkT("quiet"),

/* 4 */
dkT("manual"),

NULL


#line 207 "dk4appop.ctr"
};



static const dkChar * const	dk4app_open_scope_names[] = {
/* 0 */
dkT("user"),

/* 1 */
dkT("application"),

/* 2 */
dkT("application-group"),

/* 3 */
dkT("host"),

/* 4 */
dkT("windows"),

/* 5 */
dkT("non-windows"),

/* 6 */
dkT("language"),

/* 7 */
dkT("region"),

NULL


#line 246 "dk4appop.ctr"
};



/**	Non localized 8 bit char keywords used by this module.
*/
static const char * const	dk4app_open_c8_kw[] = {
/* 0 */
"ERROR: Not enough memory!",

/* 1 */
"ERROR: Not enough memory, failed to allocate ",

/* 2 */
" bytes!",

NULL


#line 258 "dk4appop.ctr"
};



/**	Non localized dkChar keywords used by this module.
*/
static const dkChar * const	dk4app_open_kw[] = {
/* 0 */
dkT("--quiet"),

/* 1 */
dkT("bin"),

/* 2 */
dkT("sbin"),

/* 3 */
dkT("etc"),

/* 4 */
dkT("share"),

/* 5 */
dkT("lib"),

/* 6 */
dkT("var"),

/* 7 */
dkT("libexec"),

/* 8 */
dkT("dk4pref.conf"),

/* 9 */
dkT("/log"),

/* 10 */
dkT("/"),

/* 11 */
dkT("-"),

/* 12 */
dkT(".log"),

/* 13 */
dkT("/tmp"),

/* 14 */
dkT("dk3pref.conf"),

/* 15 */
dkT("*"),

NULL


#line 313 "dk4appop.ctr"
};



/**	Character used as separator between path name components.
*/
static const dkChar dk4app_filename_sep[] =
#if DK4_HAVE_BACKSLASH_AS_SEP
dkT("\\")
#else
dkT("/")
#endif
;



#if DK4_ON_WINDOWS
/**	File name suffixes for executable files on Windows systems.
*/
static const dkChar * const	dk4app_exe_file_suffixes[] = {
/* 0 */
dkT(".exe"),

/* 1 */
dkT(".com"),

/* 2 */
dkT(".dll"),

/* 3 */
dkT(".sys"),

/* 4 */
dkT(".cpl"),

NULL


#line 340 "dk4appop.ctr"
};
#endif



/**	Initialize an application structure after allocating the memory.
	@param	ptr	Structure to initialize.
*/
static
void
dk4app_open_initialize(dk4_app_t *ptr)
{
  

#line 353 "dk4appop.ctr"
  ptr->s_opt_all	=	NULL;
  ptr->i_opt_all	=	NULL;
  ptr->s_opt_short	=	NULL;
  ptr->i_opt_short	=	NULL;
  ptr->s_opt_long	=	NULL;
  ptr->i_opt_long	=	NULL;
  ptr->s_pref_self	=	NULL;
  ptr->s_pref_cmd	=	NULL;
  ptr->s_pref_user	=	NULL;
  ptr->s_pref_sys	=	NULL;
  ptr->s_stt		=	NULL;
  ptr->i_pref_self	=	NULL;
  ptr->i_pref_cmd	=	NULL;
  ptr->i_pref_user	=	NULL;
  ptr->i_pref_sys	=	NULL;
  ptr->i_stt		=	NULL;
  ptr->msg_base		=	NULL;
  ptr->msg_debug	=	NULL;
  ptr->help_text	=	NULL;
  ptr->lic_text		=	NULL;
  ptr->my_argv		=	NULL;
  ptr->vers_text	=	NULL;
  ptr->help_file_name	=	NULL;
  ptr->user_name	=	NULL;
  ptr->host_name	=	NULL;
  ptr->user_home	=	NULL;
  ptr->language		=	NULL;
  ptr->region		=	NULL;
  ptr->prog_name	=	NULL;
  ptr->group_name	=	NULL;
  ptr->dir_bin		=	NULL;
  ptr->dir_etc		=	NULL;
  ptr->dir_share	=	NULL;
  ptr->dir_lib		=	NULL;
  ptr->dir_var		=	NULL;
  ptr->dir_tmp		=	NULL;
  ptr->log_file_name	=	NULL;
  ptr->source_file_name	=	NULL;
  ptr->source_file_line	=	(dk4_um_t)0UL;
  ptr->timer_stderr	=	(dk4_time_t)0UL;
  ptr->timer_file	=	(dk4_time_t)0UL;
  ptr->sz_msg_base	=	0;
  ptr->sz_msg_debug	=	0;
  ptr->have_stderr_time	=	0;
  ptr->have_file_time	=	0;
  ptr->have_file_opened	=	0;
  ptr->have_stderr_init	=	0;
  ptr->my_argc		=	0;
  ptr->ll_stderr	=	DK4_LL_WARNING;
  ptr->ll_file		=	DK4_LL_NONE;
  ptr->ll_file_keep	=	DK4_LL_NONE;
  ptr->ll_found		=	DK4_LL_IGNORE;
  ptr->log_stderr_time	=	1;
  ptr->log_file_time	=	1;
  ptr->log_enabled	=	0;
  ptr->encoding		=
  ptr->enc_in_std	=
  ptr->enc_in_file	=
  ptr->enc_out_std	=
  ptr->enc_out_file	=	DK4_FILE_ENCODING_ASCII;
  ptr->hvl_cmd		=	0;
  ptr->have_self_pref	=	0;
  ptr->fast_close	=	0;
  ptr->tmp_dir_used	=	0;
  

#line 418 "dk4appop.ctr"
}



/**	Check command line arguments for --quiet option.
	@param	argc	Number of command line arguments.
	@param	argv	Command line arguments array.
	@return	1 if --quiet was found, 0 otherwise.
*/
static
int
dk4app_open_silence_check(int argc, dkChar **argv)
{
  size_t	 i;		/* Traverse arguments list */
  int		 back = 0;	/* Function result */
  

#line 434 "dk4appop.ctr"
  for (i = 1; i < (size_t)argc; i++) {
    if (0 == dk4str_cmp(argv[i], dk4app_open_kw[0])) {
      back = 1;
    }
  } 

#line 439 "dk4appop.ctr"
  return back;
}



/**	Find language, region, and encoding.
	Set app->error_found if any error occures.
	@param	app	Application structure to set up.
*/
static
void
dk4app_open_find_language_region(dk4_app_t *app, dk4_app_error_t *ae)
{
  dkChar	lbuf[16];
  dkChar	rbuf[16];
  int		enc		= DK4_ENCODING_ASCII;
  int		res;
  

#line 457 "dk4appop.ctr"
  lbuf[0] = dkT('\0'); rbuf[0] = dkT('\0');
  res = dk4loc_get_settings(
    lbuf, DK4_SIZEOF(lbuf,dkChar),
    rbuf, DK4_SIZEOF(rbuf,dkChar),
    &enc, NULL
  );
  if (0 != res) {	

#line 464 "dk4appop.ctr"
    if (0 < dk4str_len(lbuf)) {
      app->language = dk4str_dup(lbuf, NULL);
      if (NULL == app->language) {
        ae->error_found = E_MEMORY;
      }
    }
    if (0 < dk4str_len(rbuf)) {
      app->region = dk4str_dup(rbuf, NULL);
      if (NULL == app->region) {
        ae->error_found = E_MEMORY;
      }
    }
    app->encoding	=
    app->enc_in_std	=
    app->enc_in_file	=
    app->enc_out_std	=
    app->enc_out_file	=	enc;
#if DK4_CHAR_SIZE > 1
    /* Multi-byte input (UTF-16, C32) typically has a BOM at start */
    app->enc_in_std	=	DK4_FILE_ENCODING_ASCII;
    app->enc_in_file	=	DK4_FILE_ENCODING_ASCII;
#endif
  } else {
    ae->error_found = E_LOCALE;
  } 

#line 489 "dk4appop.ctr"
}



/**	Report errors occured in initialization.
	@param	app	Application structure to report errors for.
*/
static
void
dk4app_open_report_error(dk4_app_t *app, dk4_app_error_t *ae)
{
  /* !!!!! */
  switch (ae->error_found) {
    case E_LOCALE: {
    } break;
    case E_MEMORY: {
    } break;
    case E_EXEFILE: {
    } break;
    case E_EXECNAME_TOO_LONG: {
    } break;
    case E_NO_DIR_IN_EXENAME: {
    } break;
    case E_EMPTY_OPTION_SPEC: {
    } break;
    case E_OPTION_LONG_SPEC_MULT: {
    } break;
    case E_OPTION_SHORT_SPEC_MULT: {
    } break;
    case E_USER_NAME_NOT_FOUND: {
    } break;
    case E_USER_HOME_NOT_FOUND: {
    } break;
    case E_LOG_FILE_NAME: {
    } break;
  }
}



/**	Check whether a dynamically allocated string is not NULL.
	@param	app	Application to set up.
	@param	dirname	String to test.
*/
static
void
dk4app_open_check_directory_name(dk4_app_error_t *ae, dkChar *dirname)
{
  

#line 538 "dk4appop.ctr"
  if (NULL == dirname) {
    if (0 == ae->error_found) {	

#line 540 "dk4appop.ctr"
      ae->error_found = E_MEMORY;
    }
  } 

#line 543 "dk4appop.ctr"
}



/**	The bin directory is the one compiled into the dk4inst
	module, so retrieve etc, lib, share and var from the
	dk4inst module too.
	@param	app	Application structure to set up.
*/
static
void
dk4app_open_find_exec_from_inst(dk4_app_t *app, dk4_app_error_t *ae)
{
  

#line 557 "dk4appop.ctr"
  app->dir_etc		= dk4str_dup(dk4inst_get_directory(1), NULL);
  app->dir_share	= dk4str_dup(dk4inst_get_directory(2), NULL);
  app->dir_lib		= dk4str_dup(dk4inst_get_directory(6), NULL);
  app->dir_var		= dk4str_dup(dk4inst_get_directory(3), NULL);
  dk4app_open_check_directory_name(ae, app->dir_etc);
  dk4app_open_check_directory_name(ae, app->dir_share);
  dk4app_open_check_directory_name(ae, app->dir_lib);
  dk4app_open_check_directory_name(ae, app->dir_var);
  

#line 566 "dk4appop.ctr"
}



/**	Create one new directory name replacing the final path component.
	@param	app	Application structure to set up.
	@param	buf	Buffer containing old directory name,
			changed to the new directory name.
	@param	szbuf	Size of buffer.
	@param	p1	Pointer to first position to modify in buffer.
	@param	nn	New final component name.
	@return	String copy of new directory name in dynamically allocated
		memory.
*/
static
dkChar *
dk4app_open_find_exec_replace_one(
  dk4_app_t		*app,
  dk4_app_error_t	*ae,
  dkChar		*buf,
  size_t		 szbuf,
  dkChar		*p1,
  dkChar const		*nn
)
{
  dkChar	*back	=	NULL;
  

#line 593 "dk4appop.ctr"
  *p1 = dkT('\0');
  if (0 != dk4str_cat_s(buf, szbuf, nn, NULL)) {
    back = dk4str_dup(buf, NULL);
    if (NULL == back) {
      if (0 == ae->error_found) {
        ae->error_found = E_MEMORY;
      }
    }
  } else {
    if (0 == ae->error_found) {
      ae->error_found = E_EXECNAME_TOO_LONG;
    }
  } 

#line 606 "dk4appop.ctr"
  return back;
}



/**	Create directory names by replacing the final
	bin or sbin component by etc, lib, share and var.
	@param	app	Application structure to set up.
	@param	buf	Buffer for directory name.
	@param	szbuf	Size of buffer.
	@param	p1	Pointer to first character to modify.
*/
static
void
dk4app_open_find_exec_replace_final(
  dk4_app_t		*app,
  dk4_app_error_t	*ae,
  dkChar		*buf,
  size_t		 szbuf,
  dkChar		 *p1
)
{
  

#line 629 "dk4appop.ctr"
  app->dir_etc	 = dk4app_open_find_exec_replace_one(
    app, ae, buf, szbuf, p1, dk4app_open_kw[3]
  );
  app->dir_share = dk4app_open_find_exec_replace_one(
    app, ae, buf, szbuf, p1, dk4app_open_kw[4]
  );
  app->dir_lib	 = dk4app_open_find_exec_replace_one(
    app, ae, buf, szbuf, p1, dk4app_open_kw[5]
  );
  app->dir_var	 = dk4app_open_find_exec_replace_one(
    app, ae, buf, szbuf, p1, dk4app_open_kw[6]
  );
  

#line 642 "dk4appop.ctr"
}



/**	Check whether the path is below the libexec directory compiled
	into the dk4inst module.
	@param	buf	Buffer to check.
	@return	1 if the path is below the libexec directory, 0 otherwise.
*/
static
int
dk4app_open_is_dkinst_libexec(dkChar *buf)
{
  size_t	 sz;
  int		 back	=	0;
  

#line 658 "dk4appop.ctr"
  sz = dk4str_len(dk4inst_get_directory(10));
  if (0 < sz) {
    if (dk4str_len(buf) > sz) {
      if (buf[sz] == dk4app_filename_sep[0]) {
        buf[sz] = dkT('\0');
	if (0 == dk4str_pathcmp(buf, dk4inst_get_directory(10))) {
	  back = 1;
	} else {
	  buf[sz] = dk4app_filename_sep[0];
	}
      }
    }
  } 

#line 671 "dk4appop.ctr"
  return back;
}



/**	Check if there is any libexec component in the path.
	Cut path to part before libexec if found.
	@param	buf	Buffer containing the directory.
	@param	szbuf	Buffer size.
	@return	1 on success (libexec found), 0 otherwise.
*/
static
int
dk4app_open_is_any_libexec(dkChar *buf, size_t szbuf)
{
  dkChar	 mybuf[DK4_MAX_PATH];
  dkChar	*p1;
  dkChar	*p2;
  int		 back	= 0;
  

#line 691 "dk4appop.ctr"
  if (0 != dk4str_cpy_s(mybuf, DK4_SIZEOF(mybuf,dkChar), buf, NULL)) {
    p1 = dk4str_rchr(mybuf, dk4app_filename_sep[0]);
    while ((0 == back) && (NULL != p1)) {
      p2 = p1;
      p2++;
      if (0 == dk4str_pathcmp(p2, dk4app_open_kw[7])) {
        back = 1;
	*p1 = dkT('\0');
        dk4str_cpy_s(buf, szbuf, mybuf, NULL);
	p1 = NULL;
      } else {
        *p1 = dkT('\0');
	p1 = dk4str_rchr(mybuf, dk4app_filename_sep[0]);
      }
    }
  } 

#line 707 "dk4appop.ctr"
  return back;
}



static
void
dk4app_open_find_copy_bindir(dk4_app_t *app, dk4_app_error_t *ae)
{
  

#line 717 "dk4appop.ctr"
  if (NULL != app->dir_bin) {
    app->dir_etc   = dk4str_dup(app->dir_bin, NULL);
    app->dir_share = dk4str_dup(app->dir_bin, NULL);
    app->dir_lib   = dk4str_dup(app->dir_bin, NULL);
    app->dir_var   = dk4str_dup(app->dir_bin, NULL);
  }
  dk4app_open_check_directory_name(ae, app->dir_etc);
  dk4app_open_check_directory_name(ae, app->dir_share);
  dk4app_open_check_directory_name(ae, app->dir_lib);
  dk4app_open_check_directory_name(ae, app->dir_var);
  

#line 728 "dk4appop.ctr"
}



/**	Find executable file for process and derive
	program name and directories from it.
	@param	app	Application structure to set up.
	@param	arg0	First command line word.
*/
static
void
dk4app_open_find_executable(
  dk4_app_t		*app,
  dk4_app_error_t	*ae,
  const dkChar		*arg0
)
{
  dkChar	 buf[DK4_MAX_PATH];
  dkChar	*p1;
  size_t const	 szbuf = DK4_SIZEOF(buf,dkChar);
  

#line 749 "dk4appop.ctr"
  if (0 != dk4execpath(buf, szbuf, arg0, NULL)) {
    p1 = dk4str_rchr(buf, dk4app_filename_sep[0]);
    if (NULL != p1) {
      *(p1++) = dkT('\0');
      if (0 < dk4str_len(buf)) {
        /* Save program name */
	app->prog_name = dk4str_dup(p1, NULL);
	if (NULL == app->prog_name) {
	  ae->error_found = E_MEMORY;
	} else {
#if DK4_ON_WINDOWS
	  /* Remove .exe suffix */
	  p1 = dk4path_get_suffix(app->prog_name, NULL);
	  if (NULL != p1) {
	    if (-1 < dk4str_array_index(dk4app_exe_file_suffixes, p1, 0)) {
	      *p1 = dkT('\0');
	    }
	  }
#endif
	}
	

#line 770 "dk4appop.ctr"
	/* Save bindir name */
	app->dir_bin = dk4str_dup(buf, NULL);
	

#line 773 "dk4appop.ctr"
	if (NULL == app->dir_bin) { ae->error_found = E_MEMORY; }
	/* Find other directories */
	if (0 == dk4str_pathcmp(buf, dk4inst_get_directory(4))) {
	  

#line 777 "dk4appop.ctr"
	  /* In .../bin, use directories from dk4inst */
	  dk4app_open_find_exec_from_inst(app, ae);
	} else {
	  if (0 == dk4str_pathcmp(buf, dk4inst_get_directory(5))) {
	    

#line 782 "dk4appop.ctr"
	    /* In .../sbin, use directories from dk4inst */
	    dk4app_open_find_exec_from_inst(app, ae);
	  } else {
	    if (dk4app_open_is_dkinst_libexec(buf)) {
	      

#line 787 "dk4appop.ctr"
	      /* In .../libexec/..., use directories from dk4inst */
	      dk4app_open_find_exec_from_inst(app, ae);
	    } else {
	      p1 = dk4str_rchr(buf, dk4app_filename_sep[0]);
	      if (NULL != p1) {
	        p1++;
	        if (0 == dk4str_pathcmp(p1, dk4app_open_kw[1])) {
		  

#line 795 "dk4appop.ctr"
	          /* Replace final path component by etc, lib... */
		  dk4app_open_find_exec_replace_final(
		    app, ae, buf, szbuf, p1
		  );
	        } else {
	          if (0 == dk4str_pathcmp(p1, dk4app_open_kw[2])) {
		    

#line 802 "dk4appop.ctr"
		    /* Replace final path component by etc, lib... */
		    dk4app_open_find_exec_replace_final(
		      app, ae, buf, szbuf, p1
		    );
		  } else {
		    if (dk4app_open_is_any_libexec(buf,szbuf)) {
		      /*
		      		We can securely append the separator and the
		      		finalizer as the dk4app_open_is_any_libexec()
				function cutted a libexec string from the path.
		      */
		      

#line 814 "dk4appop.ctr"
		      p1 = buf;
		      while(dkT('\0') != *p1) { p1++; }
		      *(p1++) = dk4app_filename_sep[0];
		      *p1 = dkT('\0');
		      dk4app_open_find_exec_replace_final(
		        app, ae, buf, szbuf, p1
		      );
		    } else {	

#line 822 "dk4appop.ctr"
		      dk4app_open_find_copy_bindir(app, ae);
		    }
		  }
	        }
	      } else {		

#line 827 "dk4appop.ctr"
	        dk4app_open_find_copy_bindir(app, ae);
	      }
	    }
	  }
	}
	

#line 833 "dk4appop.ctr"
	

#line 834 "dk4appop.ctr"
	

#line 835 "dk4appop.ctr"
	

#line 836 "dk4appop.ctr"
      } else {			

#line 837 "dk4appop.ctr"
        ae->error_found = E_EXECNAME_TOO_LONG;
      }
    } else {			

#line 840 "dk4appop.ctr"
      ae->error_found = E_NO_DIR_IN_EXENAME;
    }
  } else {			

#line 843 "dk4appop.ctr"
    ae->error_found = E_EXEFILE;
  }
  

#line 846 "dk4appop.ctr"
}



static
void
dk4app_close_prefeferences(dk4_sto_t *s, dk4_sto_it_t *i)
{
  dk4_pref_t	*prefptr;
  if (NULL != i) {
    dk4sto_it_reset(i);
    do {
      prefptr = (dk4_pref_t *)dk4sto_it_next(i);
      if (NULL != prefptr) {
        dk4pref_close(prefptr);
      }
    } while(NULL != prefptr);
    dk4sto_it_close(i);
  }
  if (NULL != s) {
    dk4sto_close(s);
  }
}



int
dk4app_close(dk4_app_t *app)
{
  dk4_option_t		*optptr;
  dk4_string_table_t	*ptrstt;
  int			 back	=	0;
  

#line 879 "dk4appop.ctr"
  if (NULL != app) {
    /* Remove temporary files */
    if (0 == app->fast_close) {
      if ((NULL != app->dir_tmp) && (0 != app->tmp_dir_used)) {
        (void)dk4app_del_tree(app->dir_tmp, app);
      }
    }
#if 0
    /* Write preferences back to file */
    if ((0 != app->have_self_pref) && (0 == app->fast_close)) {
      /*
	Originally intended: Write self-set preferences back to file.
	2015-04-17: Design decision not to write back.
		    Preferences are read-only in version 4.
		    For GUI programs there are mechanisms in wxWidgets
		    to save configuration changes.
      */
    }
#endif
    /*	The base and debug messages are read from string tables, so the
	arrays are freed when freeing the string tables.
    */
    app->msg_base = NULL;
    app->msg_debug = NULL;
    app->source_file_name = NULL;
    back = 1;
    /* Disable logging */
    app->log_enabled = 0;

    /* Delete log file unless there is a reason to keep it */
    if (NULL != app->log_file_name) {
      if ((app->ll_found <= app->ll_file) && (0 != app->have_file_opened)) {
        if (app->ll_file_keep < app->ll_found) {
	  (void)dk4delete_file(app->log_file_name, NULL);
        }
      }
    }
    /* Release memory for log file */
    dk4mem_release(app->log_file_name);
    /* Release memory for directories */
    app->help_text = NULL;
    app->lic_text = NULL;
    dk4mem_release(app->dir_tmp);
    dk4mem_release(app->dir_var);
    dk4mem_release(app->dir_lib);
    dk4mem_release(app->dir_share);
    dk4mem_release(app->dir_etc);
    dk4mem_release(app->dir_bin);
    dk4mem_release(app->group_name);
    dk4mem_release(app->prog_name);
    /* Release memory for language and region */
    dk4mem_release(app->region);
    dk4mem_release(app->language);
    dk4mem_release(app->vers_text);
    dk4mem_release(app->help_file_name);
    dk4mem_release(app->host_name);
    dk4mem_release(app->user_home);
    dk4mem_release(app->user_name);
#if TRACE_DEBUG
    {
      int i;
      if ((0 < app->my_argc) && (NULL != app->my_argv)) {
        for (i = 0; i < app->my_argc; i++) {
          

#line 943 "dk4appop.ctr"
        }
      }
    }
#endif
    dk4mem_release(app->my_argv);
    /* Release string tables */
    if (NULL != app->s_stt) {
      if (NULL != app->i_stt) {
        dk4sto_it_reset(app->i_stt);
	do {
	  ptrstt = (dk4_string_table_t *)dk4sto_it_next(app->i_stt);
	  if (NULL != ptrstt) {
	    dk4stt_close(ptrstt);
	  }
	} while (NULL != ptrstt);
        dk4sto_it_close(app->i_stt);
      }
      dk4sto_close(app->s_stt);
    }
    app->s_stt = NULL;
    app->i_stt = NULL;
    /*
	Release options
	All options in s_opt_long and s_opt_short are also in s_opt_all.
	They are deleted later, so we can simply close the storages here.
    */
    if (NULL != app->i_opt_long) { dk4sto_it_close(app->i_opt_long); }
    app->i_opt_long = NULL;
    if (NULL != app->i_opt_short) { dk4sto_it_close(app->i_opt_short); }
    app->i_opt_short = NULL;
    if (NULL != app->s_opt_long) { dk4sto_close(app->s_opt_long); }
    app->s_opt_long = NULL;
    if (NULL != app->s_opt_short) { dk4sto_close(app->s_opt_short); }
    app->s_opt_short = NULL;
    dk4app_close_prefeferences(app->s_pref_sys, app->i_pref_sys);
    dk4app_close_prefeferences(app->s_pref_user, app->i_pref_user);
    dk4app_close_prefeferences(app->s_pref_cmd, app->i_pref_cmd);
    dk4app_close_prefeferences(app->s_pref_self, app->i_pref_self);
    app->s_pref_self	=	NULL;
    app->s_pref_cmd	=	NULL;
    app->s_pref_user	=	NULL;
    app->s_pref_sys	=	NULL;
    app->i_pref_self	=	NULL;
    app->i_pref_cmd	=	NULL;
    app->i_pref_user	=	NULL;
    app->i_pref_sys	=	NULL;
    if (NULL != app->i_opt_all) {
      dk4sto_it_reset(app->i_opt_all);
      do {
        optptr = (dk4_option_t *)dk4sto_it_next(app->i_opt_all);
	if (NULL != optptr) {
	  dk4opt_close(optptr);
	}
      } while(NULL != optptr) ;
      dk4sto_it_close(app->i_opt_all);
    }
    app->i_opt_all = NULL;
    if (NULL != app->s_opt_all) { dk4sto_close(app->s_opt_all); }
    app->s_opt_all = NULL;
    /* Release memory for app structure itself */
    dk4mem_free(app);
  } 

#line 1005 "dk4appop.ctr"
  return back;
}



/**	Process the options specification.
	@param	app		Application to set up.
	@param	ae		Application error structure.
	@param	optspec		The option specification.
	@param	szoptspec	Size of the optspec array.
*/
static
void
dk4app_open_process_optspec(
  dk4_app_t				*app,
  dk4_app_error_t			*ae,
  const dk4_option_specification_t	*optspec,
  size_t				 szoptspec
)
{
  dk4_option_t	*optptr;
  

#line 1027 "dk4appop.ctr"
  while(szoptspec--) {
    if ((NULL != optspec->longopt) || (dkT('\0') != optspec->c)) {
      optptr = dk4opt_open(optspec, NULL);
      if (NULL != optptr) {
        if (0 != dk4sto_add(app->s_opt_all, optptr, NULL)) {
	  if (NULL != optspec->longopt) {
	    if (NULL == dk4sto_it_find_like(app->i_opt_long,optspec->longopt,3))
	    {
	      if (0 == dk4sto_add(app->s_opt_long, optptr, NULL)) {
	        if (0 == ae->error_found) { ae->error_found = E_MEMORY; }
		szoptspec = 0;
	      }
	    }
	    else
	    {					

#line 1042 "dk4appop.ctr"
	      if (0 == ae->error_found) {
	        ae->error_found = E_OPTION_LONG_SPEC_MULT;
		ae->longopt = optspec->longopt;
	      }
	      szoptspec = 0;
	    }
	  }
	  if (dkT('\0') != optspec->c) {
	    if (NULL == dk4sto_it_find_like(app->i_opt_short,&(optspec->c),1))
	    {
	      if (0 == dk4sto_add(app->s_opt_short, optptr, NULL)) {
	        if (0 == ae->error_found) { ae->error_found = E_MEMORY; }
		szoptspec = 0;
	      }
	    }
	    else
	    {					

#line 1059 "dk4appop.ctr"
	      if (0 == ae->error_found) {
	        ae->error_found = E_OPTION_SHORT_SPEC_MULT;
		ae->longopt = optspec->longopt;
	      }
	      szoptspec = 0;
	    }
	  }
	} else {			

#line 1067 "dk4appop.ctr"
	  if (0 == ae->error_found) { ae->error_found = E_MEMORY; }
	  szoptspec = 0;
	  dk4opt_close(optptr); optptr = NULL;
	}
      } else {				

#line 1072 "dk4appop.ctr"
        if (0 == ae->error_found) { ae->error_found = E_MEMORY; }
	szoptspec = 0;
      }
    } else {				

#line 1076 "dk4appop.ctr"
      if (0 == ae->error_found) { ae->error_found = E_EMPTY_OPTION_SPEC; }
      szoptspec = 0;
    }
    optspec++;
  } 

#line 1081 "dk4appop.ctr"
}



/**	Create and prepare the storage containers for command line options.
	@param	app		Application to set up.
	@param	ae		Application error structure.
	@param	optspec		The option specification.
	@param	szoptspec	Size of the optspec array.
*/
static
void
dk4app_open_create_options_storages(
  dk4_app_t				*app,
  dk4_app_error_t			*ae,
  const dk4_option_specification_t	*optspec,
  size_t				 szoptspec
)
{
  

#line 1101 "dk4appop.ctr"
  if ((NULL != optspec) && (0 < szoptspec)) {
    app->s_opt_all = dk4sto_open(NULL);
    if (NULL != app->s_opt_all) {
      app->s_opt_long = dk4sto_open(NULL);
      if (NULL != app->s_opt_long) {
        dk4sto_set_comp(app->s_opt_long, dk4option_compare, 2);
        app->s_opt_short = dk4sto_open(NULL);
	if (NULL != app->s_opt_short) {
	  dk4sto_set_comp(app->s_opt_short, dk4option_compare, 0);
	  app->i_opt_all = dk4sto_it_open(app->s_opt_all, NULL);
	  if (NULL != app->i_opt_all) {
	    app->i_opt_long = dk4sto_it_open(app->s_opt_long, NULL);
	    if (NULL != app->i_opt_long) {
	      app->i_opt_short = dk4sto_it_open(app->s_opt_short, NULL);
	      if (NULL != app->i_opt_short) {
	        dk4app_open_process_optspec(app, ae, optspec, szoptspec);
	      } else {		

#line 1118 "dk4appop.ctr"
	        ae->error_found = E_MEMORY;
	      }
	    } else {		

#line 1121 "dk4appop.ctr"
	      ae->error_found = E_MEMORY;
	    }
	  } else {		

#line 1124 "dk4appop.ctr"
	    ae->error_found = E_MEMORY;
	  }
	} else {		

#line 1127 "dk4appop.ctr"
	  ae->error_found = E_MEMORY;
	}
      } else {			

#line 1130 "dk4appop.ctr"
        ae->error_found = E_MEMORY;
      }
    } else {			

#line 1133 "dk4appop.ctr"
      ae->error_found = E_MEMORY;
    }
  } 

#line 1136 "dk4appop.ctr"
}



/**	Create storage containers for preferences.
	@param	app	Application to set up.
	@param	ae	Error storage for setup.
*/
static
void
dk4app_open_create_pref_storages(
  dk4_app_t		*app,
  dk4_app_error_t	*ae
)
{
  app->s_pref_sys = dk4sto_open(NULL);
  if (NULL != app->s_pref_sys) {
    app->s_pref_user = dk4sto_open(NULL);
    if (NULL != app->s_pref_user) {
      app->s_pref_cmd = dk4sto_open(NULL);
      if (NULL != app->s_pref_cmd) {
        app->s_pref_self = dk4sto_open(NULL);
	if (NULL != app->s_pref_self) {
	  dk4sto_set_comp(app->s_pref_sys, dk4pref_compare, 0);
	  dk4sto_set_comp(app->s_pref_user, dk4pref_compare, 0);
	  dk4sto_set_comp(app->s_pref_cmd, dk4pref_compare, 0);
	  dk4sto_set_comp(app->s_pref_self, dk4pref_compare, 0);
	  app->i_pref_sys = dk4sto_it_open(app->s_pref_sys, NULL);
	  app->i_pref_user = dk4sto_it_open(app->s_pref_user, NULL);
	  app->i_pref_cmd = dk4sto_it_open(app->s_pref_cmd, NULL);
	  app->i_pref_self = dk4sto_it_open(app->s_pref_self, NULL);
	  if ((NULL == app->i_pref_sys) || (NULL == app->i_pref_user)) {
	    ae->error_found = E_MEMORY;
	  }
	  if ((NULL == app->i_pref_cmd) || (NULL == app->i_pref_self)) {
	    ae->error_found = E_MEMORY;
	  }
	} else {
	  ae->error_found = E_MEMORY;
	}
      } else {
        ae->error_found = E_MEMORY;
      }
    } else {
      ae->error_found = E_MEMORY;
    }
  } else {
    ae->error_found = E_MEMORY;
  }
}



/**	Create storage container for string tables.
	@param	app	Application to set up.
	@param	ae	Error storage for setup.
*/
static
void
dk4app_open_create_stt_storage(
  dk4_app_t		*app,
  dk4_app_error_t	*ae
)
{
  app->s_stt = dk4sto_open(NULL);
  if (NULL != app->s_stt) {
    dk4sto_set_comp(app->s_stt, dk4stt_compare, 0);
    app->i_stt = dk4sto_it_open(app->s_stt, NULL);
    if (NULL == app->i_stt) {
      ae->error_found = E_MEMORY;
    }
  } else {
    ae->error_found = E_MEMORY;
  }
}



/**	Attempt to find users login name and home directory.
	On Linux/Unix the effective user name and home directory is searched.
	@param	app	Application to set up.
	@param	ae	Application opening error structure.
*/
static
void
dk4app_open_find_user_name_and_home(dk4_app_t *app, dk4_app_error_t *ae)
{
  dkChar	buf[DK4_MAX_PATH];
  dk4_er_t	er;
  

#line 1226 "dk4appop.ctr"
  dk4error_init(&er);
  if (0 != dk4user_get_logname(buf, DK4_SIZEOF(buf,dkChar), 1, &er)) {
    app->user_name = dk4str_dup(buf, &er);
    if (NULL != app->user_name) {
      if (0 != dk4user_get_homedir(buf, DK4_SIZEOF(buf,dkChar), 1, &er)) {
        app->user_home = dk4str_dup(buf, &er);
	if (NULL == app->user_home) {
	  ae->error_found = E_MEMORY;
	}
      } else {
        ae->error_found = E_USER_HOME_NOT_FOUND;
      }
    } else {
      ae->error_found = E_MEMORY;
    }
  } else {
    ae->error_found = E_USER_NAME_NOT_FOUND;
  }
  

#line 1245 "dk4appop.ctr"
  

#line 1246 "dk4appop.ctr"
  

#line 1247 "dk4appop.ctr"
}



/**	Preference reader structure.
*/
typedef struct {
  dk4_app_t		*app;	/**< Application to set up. */
  dk4_app_error_t	*ae;	/**< Application error structure. */
  dk4_sto_t		*st;	/**< Storage for preferences. */
  dk4_sto_it_t		*it;	/**< Storage iterator. */
  int			 insc;	/**< Flag: In current scope. */
  int			 lasc;	/**< Flag: Previous line was scope line. */
  int			 co3;	/**< Flag: DK tools 3 compatibility. */
} dk4_app_pref_rd_t;



/**	Initialize preference reader structure for start of file.
	@param	ptr	Preference reader structure.
*/
static
void
dk4app_open_pref_reader_init_for_one_file(dk4_app_pref_rd_t *ptr)
{
  ptr->insc = 1;
  ptr->lasc = 0;
  ptr->co3  = 0;
}



/**	Check one scope line entry.
	@param	app	Application.
	@param	ae	Application open error structure.
	@param	pn	Pointer to entry name.
	@param	pv	Pointer to entry value.
	@return	1 if condition is true, 0 otherwise.
*/
static
int
dk4app_open_check_one_scope_entry(
  dk4_app_t		*app,
  dk4_app_error_t	*ae,
  const dkChar		*pn,
  const dkChar		*pv
)
{
  int		 back	= 0;
  

#line 1297 "dk4appop.ctr"
  switch (dk4str_array_index(dk4app_open_scope_names, pn, 0)) {
    case 0: {
      if ((NULL != pv) && (NULL != app->user_name)) {
        

#line 1301 "dk4appop.ctr"
#if DK4_HAVE_CASE_INSENSITIVE_USERNAMES
	if (0 == dk4str_casecmp(pv, app->user_name))
#else
	if (0 == dk4str_cmp(pv, app->user_name))
#endif
	{
	  back = 1;
	}
	else
	{
	  if (0 == dk4str_cmp(pv, dk4app_open_kw[15])) {
	    back = 1;
	  }
	}
      }
    } break;
    case 1: {
      if ((NULL != pv) && (NULL != app->prog_name)) {
        

#line 1320 "dk4appop.ctr"
#if DK4_HAVE_CASE_INSENSITIVE_PATHNAMES
	if (0 == dk4str_casecmp(pv, app->prog_name))
#else
	if (0 == dk4str_cmp(pv, app->prog_name))
#endif
	{
	  back = 1;
	}
	else
	{
	  if (0 == dk4str_cmp(pv, dk4app_open_kw[15])) {
	    back = 1;
	  }
	}
      }
    } break;
    case 2: {	

#line 1337 "dk4appop.ctr"
      if ((NULL != pv) && (NULL != app->group_name)) {
        

#line 1339 "dk4appop.ctr"
#if DK4_HAVE_CASE_INSENSITIVE_PATHNAMES
	if (0 == dk4str_casecmp(pv, app->group_name))
#else
	if (0 == dk4str_cmp(pv, app->group_name))
#endif
	{
	  back = 1;
	}
	else
	{
	  if (0 == dk4str_cmp(pv, dk4app_open_kw[15])) {
	    back = 1;
	  }
	}
      }
    } break;
    case 3: {	

#line 1356 "dk4appop.ctr"
      if ((NULL != pv) && (NULL != app->host_name)) {
        

#line 1358 "dk4appop.ctr"
        if (0 == dk4str_casecmp(pv, app->host_name)) {
          back = 1;
        } else {
	  if (0 == dk4str_cmp(pv, dk4app_open_kw[15])) {
	    back = 1;
	  }
	}
      }
    } break;
    case 4: {	

#line 1368 "dk4appop.ctr"
      if (NULL != pv) {
#if DK4_ON_WINDOWS
	if (0 != dk4str_is_on(pv)) {
	  back = 1;
	}
#else
	if (0 == dk4str_is_on(pv)) {
	  back = 1;
	}
#endif
      } else {
#if DK4_ON_WINDOWS
	back = 1;
#endif
      }
    } break;
    case 5: {	

#line 1385 "dk4appop.ctr"
      if (NULL != pv) {
#if DK4_ON_WINDOWS
	if (0 == dk4str_is_on(pv)) {
	  back = 1;
	}
#else
	if (0 != dk4str_is_on(pv)) {
	  back = 1;
	}
#endif
      } else {
#if DK4_ON_WINDOWS
	back = 0;
#else
	back = 1;
#endif
      }
    } break;
    case 6 : {				

#line 1404 "dk4appop.ctr"
      if ((NULL != pv) && (NULL != app->language)) {
	if (0 == dk4str_casecmp(pv, app->language)) {
	  back =1;
	} else {
	  if (0 == dk4str_cmp(pv, dk4app_open_kw[15])) {
	    back = 1;
	  }
	}
      }
    } break;
    case 7 : {				

#line 1415 "dk4appop.ctr"
      if ((NULL != pv) && (NULL != app->region)) {
	if (0 == dk4str_casecmp(pv, app->region)) {
	  back =1;
	} else {
	  if (0 == dk4str_cmp(pv, dk4app_open_kw[15])) {
	    back = 1;
	  }
	}
      }
    } break;
    default: {				

#line 1426 "dk4appop.ctr"
      if (0 == dk4str_len(pn)) {	

#line 1427 "dk4appop.ctr"
        back = 1;
      }
    } break;
  } 

#line 1431 "dk4appop.ctr"
  return back;
}



/**	Add one preference to storages.
	@param	app	Application to configure.
	@param	ae	Application open error structure.
	@param	st	Storage.
	@param	it	Iterator for storage.
	@param	pn	Preference name.
	@param	pv	Preference value.
*/
static
void
dk4app_open_add_preference(
  dk4_app_t		*app,
  dk4_app_error_t	*ae,
  dk4_sto_t		*st,
  dk4_sto_it_t		*it,
  const dkChar		*pn,
  const dkChar		*pv
)
{
  dk4_pref_t		*prefptr;
  dkChar		*npv;
  

#line 1458 "dk4appop.ctr"
  prefptr = dk4sto_it_find_like(it, pn, 1);
  if (NULL != prefptr) {	

#line 1460 "dk4appop.ctr"
    npv = dk4str_dup(pv, NULL);
    if (NULL != npv) {
#if 0
      dk4mem_release(prefptr->value);
#endif
      /*	2016-12-10	Sanitize memory before releasing it.
      */
      dk4str_release_sanitized(prefptr->value);
      prefptr->value = npv;
    } else {
      if (0 == ae->error_found) { ae->error_found = E_MEMORY; }
    }
  } else {			

#line 1473 "dk4appop.ctr"
    prefptr = dk4pref_open(pn, pv, NULL);
    if (NULL != prefptr) {
      if (0 == dk4sto_add(st, (void *)prefptr, NULL)) {
        dk4pref_close(prefptr);
	if (0 == ae->error_found) { ae->error_found = E_MEMORY; }
      }
    } else {
      if (0 == ae->error_found) { ae->error_found = E_MEMORY; }
    }
  }
  dk4app_pref_apply(app, ae, pn, pv);
  

#line 1485 "dk4appop.ctr"
}



/**	Modify a preference value from DK tools 3 to 4 syntax.
	Change "/print/ps/level" to "print.ps.level".
	@param	Preference name pointer.
*/
static
void
dk4app_open_modify_prefname_3_to_4(dkChar *pn)
{
  if (NULL != pn) {
    if (dkT('/') == *pn) {
      dk4str_cpy_to_left(pn, &(pn[1]));
      while (dkT('\0') != *pn) {
        if (dkT('/') == *pn) {
	  *pn = dkT('.');
	}
        pn++;
      }
    }
  }
}



/**	Process one input line.
	@param	obj		Preferences reader object.
	@param	line		Current line to process.
	@param	lineno		Line number.
	@param	erp		Error report, may be NULL.
	@return	
	DK4_TSP_RES_OK		if processing succeeded,
	DK4_TSP_RES_ERROR	on errors (can continue),
	DK4_TSP_RES_FATAL	on fatal errors (can not continue).

*/
static
int
dk4app_open_pref_line_handler(
  void				*obj,
  dkChar			*line,
  dk4_um_t			 lineno,
  dk4_er_t			*erp
)
{
  dkChar		*p1;			/* Name text */
  dkChar		*p2;			/* Next text */
  dkChar		*pv;			/* Value text */
  dk4_app_pref_rd_t	*ptr;			/* Reader structure */
  int		 	 back = DK4_TSP_RES_OK;	/* Function result */
  int		 	 isneg = 0;		/* Flag: Exclude scope */
  int			 ok;			/* Flag: No test failed yet */
  

#line 1540 "dk4appop.ctr"
  dk4str_delnl(line);
  ptr = (dk4_app_pref_rd_t *)obj;
  p1 = dk4str_start(line, NULL);
  if (NULL != p1) {
    if (dkT('#') != *p1) {		

#line 1545 "dk4appop.ctr"
      if (dkT('[' == *p1)) {		

#line 1546 "dk4appop.ctr"
        if (0 == ptr->lasc) {		

#line 1547 "dk4appop.ctr"
	  ptr->insc = 0;
	} else {			

#line 1549 "dk4appop.ctr"
	}
	p2 = dk4str_chr(p1, dkT(']'));
	if (NULL != p2) {		

#line 1552 "dk4appop.ctr"
	  *p2 = dkT('\0');
	}
	p1++;
	if (dkT('-') == *p1) {		

#line 1556 "dk4appop.ctr"
	  isneg = 1;
	  p1++;
	}
	ok = 1;
	while (NULL != p1) {
	  p2 = dk4str_chr(p1, dkT(','));
	  if (NULL != p2) { *(p2++) = dkT('\0'); p2 = dk4str_start(p2, NULL); }
	  pv = dk4str_chr(p1, dkT('='));
	  if (NULL != pv) { *(pv++) = dkT('\0'); pv = dk4str_start(pv, NULL); }
	  dk4str_normalize(p1, NULL);
	  if (NULL != pv) { dk4str_rtwh(pv, NULL); }
	  if (0 == dk4app_open_check_one_scope_entry(ptr->app,ptr->ae,p1,pv)) {
	    ok = 0;
	  }
	  p1 = p2;
	}
	if (1 == ok) {			

#line 1573 "dk4appop.ctr"
	  if (0 != isneg) {
	    ptr->insc = 0;		

#line 1575 "dk4appop.ctr"
	  } else {
	    ptr->insc = 1;		

#line 1577 "dk4appop.ctr"
	  }
	} else {			

#line 1579 "dk4appop.ctr"
	}
	ptr->lasc = 1;
      } else {				

#line 1582 "dk4appop.ctr"
        if (ptr->insc) {		

#line 1583 "dk4appop.ctr"
	  p2 = dk4str_chr(p1, dkT('='));
	  if (NULL != p2) { *(p2++) = dkT('\0'); p2 = dk4str_start(p2, NULL); }
	  if ((NULL != p1) && (NULL != p2)) {
	    dk4str_normalize(p1, NULL);
	    dk4str_rtwh(p2, NULL);
	    if (0 < ptr->co3) {			

#line 1589 "dk4appop.ctr"
	      dk4app_open_modify_prefname_3_to_4(p1);
	      

#line 1591 "dk4appop.ctr"
	    } else {				

#line 1592 "dk4appop.ctr"
	    }
	    dk4app_open_add_preference(ptr->app,ptr->ae,ptr->st,ptr->it,p1,p2);
	  }
	} else {			

#line 1596 "dk4appop.ctr"
	}
        ptr->lasc = 0;
      }
    } else {				

#line 1600 "dk4appop.ctr"
    }
  } else {				

#line 1602 "dk4appop.ctr"
  } 

#line 1603 "dk4appop.ctr"
  return back;
}



/**	Read all preferences files in increasing order.
	Cumulative operation, redefinitions possible.
	@param	app	Application to configure.
	@param	ae	Application opening error structure.
*/
static
void
dk4app_open_read_preference_files(dk4_app_t *app, dk4_app_error_t *ae)
{
  char			 ib[4096];		/* Input buffer */
  dkChar		 il[1024];		/* Input line */
  dkChar		 fnb[DK4_MAX_PATH];	/* File name buffer */
  dk4_tspdk_t		 tsp;			/* Text stream processor */
  dk4_er_t		 er;			/* Error report */
  dk4_app_pref_rd_t	 rdobj;			/* Reader object */
  dk4_stream_t		*istrm;			/* Input stream */
  size_t		 rb;			/* Bytes read from stream */
  int			 pn;			/* Pass number */
  int			 res;			/* Result from path construct */
  int			 cc;			/* Flag: Can continue */
  

#line 1629 "dk4appop.ctr"
  rdobj.app = app;
  rdobj.ae  = ae;
  for (pn = 0; ((0 == ae->error_found) && (pn <= DK4_FS_CONF_MAX)); pn++) {
    if (DK4_FS_CONF_SYS_MAX >= pn) {
      rdobj.st = app->s_pref_sys;
      rdobj.it = app->i_pref_sys;
    } else {
      rdobj.st = app->s_pref_user;
      rdobj.it = app->i_pref_user;
    }
#if DK4_HAVE_COMPATDKTOOLS3
    dk4error_init(&er);
    res = dk4fs_config_compat_one(
      fnb, DK4_SIZEOF(fnb,dkChar), dk4app_open_kw[14],
      app->dir_share, app->dir_etc, app->user_home,
      app->prog_name, app->group_name, pn, 1, 1, &er
    );
    if (0 != res) {
      

#line 1648 "dk4appop.ctr"
      istrm = dk4stream_open_file_reader(fnb, &er);
      if (NULL != istrm) {			

#line 1650 "dk4appop.ctr"
	dk4app_open_pref_reader_init_for_one_file(&rdobj);
	rdobj.co3 = 1;
	res = dk4tspdk_setup_line(
	  &tsp, &rdobj, dk4app_open_pref_line_handler,
	  il, DK4_SIZEOF(il,dkChar),
	  app->encoding, app->enc_in_file, &er
	);
	if (0 != res) {			

#line 1658 "dk4appop.ctr"
	  res = DK4_TSP_RES_OK;
	  do {
	    cc = 0;
	    rb = sizeof(ib);
	    if (0 != dk4stream_read(ib, &rb, istrm, &er)) {
	      if (0 < rb) {
	        cc  = 1;
	        res = dk4tspdk_add_bytes(&tsp, (const unsigned char *)ib, rb);
		if (DK4_TSP_RES_FATAL == res) {
		  cc = 0;
		}
	      }
	    }
	  } while(0 != cc);
	  if (DK4_TSP_RES_FATAL != res) {
#if VERSION_BEFORE_20150821
	    res = dk4tspdk_finish(&tsp);
#else
	    dk4tspdk_finish(&tsp);
#endif
	  }
	} else {			

#line 1680 "dk4appop.ctr"
	}
        dk4stream_close(istrm, NULL);
      } else {				

#line 1683 "dk4appop.ctr"
        if (0 == ae->error_found) { ae->error_found = E_PREF_FILE_OPEN; }
      }
    }
#endif
    dk4error_init(&er);
    res = dk4fs_config_one(
      fnb, DK4_SIZEOF(fnb,dkChar), dk4app_open_kw[8],
      app->dir_share, app->dir_etc, app->user_home,
      app->prog_name, app->group_name, pn, 1, &er
    );
    if (0 != res) {
      

#line 1695 "dk4appop.ctr"
      istrm = dk4stream_open_file_reader(fnb, &er);
      if (NULL != istrm) {			

#line 1697 "dk4appop.ctr"
	dk4app_open_pref_reader_init_for_one_file(&rdobj);
	res = dk4tspdk_setup_line(
	  &tsp, &rdobj, dk4app_open_pref_line_handler,
	  il, DK4_SIZEOF(il,dkChar),
	  app->encoding, app->enc_in_file, &er
	);
	if (0 != res) {			

#line 1704 "dk4appop.ctr"
	  res = DK4_TSP_RES_OK;
	  do {
	    cc = 0;
	    rb = sizeof(ib);
	    if (0 != dk4stream_read(ib, &rb, istrm, &er)) {
	      if (0 < rb) {
	        cc  = 1;
	        res = dk4tspdk_add_bytes(&tsp, (const unsigned char *)ib, rb);
		if (DK4_TSP_RES_FATAL == res) {
		  cc = 0;
		}
	      }
	    }
	  } while(0 != cc);
	  if (DK4_TSP_RES_FATAL != res) {
#if VERSION_BEFORE_20150821
	    res = dk4tspdk_finish(&tsp);
#else
	    dk4tspdk_finish(&tsp);
#endif
	  }
	} else {			

#line 1726 "dk4appop.ctr"
	}
        dk4stream_close(istrm, NULL);
      } else {				

#line 1729 "dk4appop.ctr"
        if (0 == ae->error_found) { ae->error_found = E_PREF_FILE_OPEN; }
      }
    }
  } 

#line 1733 "dk4appop.ctr"
}



/**	Find host name for computer.
	@param	app	Application to set up.
	@param	ae	Application opening error structure.
*/
static
void
dk4app_open_find_host_name(dk4_app_t *app, dk4_app_error_t *ae)
{
  dkChar	buf[256];
  if (0 != dk4hostname(buf, DK4_SIZEOF(buf,dkChar), NULL)) {
    app->host_name = dk4str_dup(buf, NULL);
    if (NULL == app->host_name) {
      if (0 == ae->error_found) { ae->error_found = E_MEMORY; }
    }
  } else {
    if (0 == ae->error_found) { ae->error_found = E_HOST_NAME_NOT_FOUND; }
  }
}



/**	Check whether an option specification was given and
	processed successfully.
	@param	app	Application to set up.
	@return	1 if option specification given, 0 otherwise.
*/
static
int
dk4app_open_options_check(dk4_app_t *app)
{
  int		 back = 0;
  if ((NULL != app->s_opt_all) && (NULL != app->i_opt_all)) {
  if ((NULL != app->s_opt_short) && (NULL != app->i_opt_short)) {
  if ((NULL != app->s_opt_long) && (NULL != app->i_opt_long)) {
    back = 1;
  }
  }
  }
  return back;
}



/**	Check for --help --version --license.
	@param	app	Application to set up.
	@param	ae	Application open error structure.
	@param	arg	Argument to process.
	@return	1 if the option needs no further processing, 0 otherwise.
*/
static
int
dk4app_open_check_long_opt(dk4_app_t *app, dk4_app_error_t *ae, dkChar *arg)
{
  int		 back = 0;
  switch (dk4str_array_index(dk4app_open_long_opt, arg, 1)) {
    case 0: {	/* help */
      app->hvl_cmd |= DK4_APP_CMD_HELP;
      back = 1;
    } break;
    case 1: {	/* version */
      app->hvl_cmd |= DK4_APP_CMD_VERSION;
      back = 1;
    } break;
    case 2: {	/* license */
      app->hvl_cmd = DK4_APP_CMD_LICENSE;
      back = 1;
    } break;
    case 3: {
      back = 1;
    } break;
    case 4: {
      app->hvl_cmd |= DK4_APP_CMD_MANUAL;
      back = 1;
    } break;
  }
  return back;
}



/**	Construct log file name, create log directory.
	@param	app	Application to set up.
	@param	ae	Application open error structure.
*/
static
void
dk4app_open_prepare_file_logging(dk4_app_t *app, dk4_app_error_t *ae)
{
  dkChar	 fnb[DK4_MAX_PATH];	/* File name buffer */
  dkChar	 dktb[32];		/* Date as dkChar text */
  dkChar	 pidb[32];		/* Buffer for PID */
  dkChar	 pi2b[32];		/* Buffer for padded PID */
  dkChar	 tb[32];		/* Date as text */
  dkChar	*ptr;
  dk4_time_t	 timer;			/* Current time */
  dk4_pid_t	 pid;			/* Process ID */
  size_t	 sz;			/* Size of fnb */
  size_t	 szdktb;		/* Size of dktb */
  size_t	 sl;			/* String length */
  size_t	 pl;			/* Padded length */
  int		 ok			= 0;
  

#line 1839 "dk4appop.ctr"
  sz = DK4_SIZEOF(fnb,dkChar);
  dk4time_get(&timer);
  if (dk4str_cpy_s(fnb, sz, app->user_home, NULL)) {
    if (dk4str_cat_s(fnb, sz, dk4app_open_kw[9], NULL)) {
      dk4path_correct_sep(fnb);
#if VERSION_BEFORE_20160127
      ok = dk4app_mkdir_hierarchy(fnb, 1, app);
#else
      ok = 1;
#endif
      if (0 < ok) {
        ok = 0;
        if (dk4str_cat_s(fnb, sz, dk4app_open_kw[10], NULL)) {
	  if (dk4str_cat_s(fnb, sz, app->prog_name, NULL)) {
	    if (dk4time_as_text(tb, DK4_SIZEOF(tb,dkChar), &timer, NULL)) {
	      szdktb = DK4_SIZEOF(dktb,dkChar);
	      if (dk4str_cpy_s(dktb, szdktb, tb, NULL)) {
		ptr = dktb;
		while (dkT('\0') != *ptr) {
		  if (dkT(':') == *ptr) { *ptr = dkT('-'); }
		  if (dkT(' ') == *ptr) { *ptr = dkT('-'); }
		  ptr++;
		}
		pid = dk4getpid();
		ok = dk4ma_write_decimal_unsigned(
		  pidb, DK4_SIZEOF(pidb,dkChar), (dk4_um_t)pid, 0,
		  NULL
		);
		if (0 != ok) {
		  for (sl = 0; sl < DK4_SIZEOF(pi2b,dkChar); sl++) {
		    pi2b[sl] = dkT('0');
		  }
		  pi2b[DK4_SIZEOF(pi2b,dkChar)-1] = dkT('\0');
		  sl = dk4str_len(pidb);
		  pl = dk4numlength(sizeof(dk4_pid_t), 0);
		  if (sl < pl) {
		    ok = dk4str_cpy_s(
		      &(pi2b[pl - sl]),
		      (DK4_SIZEOF(pi2b,dkChar) - (pl - sl)),
		      pidb, NULL
		    );
		  } else {
		    ok = dk4str_cpy_s(pi2b,DK4_SIZEOF(pi2b,dkChar),pidb,NULL);
		  }
		  if (0 != ok) {
		    ok = 0;
		    if (dk4str_cat_s(fnb, sz, dk4app_open_kw[11], NULL)) {
		      if (dk4str_cat_s(fnb, sz, dktb, NULL)) {
		        if (dk4str_cat_s(fnb, sz, dk4app_open_kw[11], NULL)) {
			  if (dk4str_cat_s(fnb, sz, pi2b, NULL)) {
			    if (dk4str_cat_s(fnb,sz,dk4app_open_kw[12],NULL)) {
			      app->log_file_name = dk4str_dup(fnb,NULL);
			      if (NULL != app->log_file_name) {
			        dk4path_correct_sep(app->log_file_name);
			        ok = 1;	

#line 1894 "dk4appop.ctr"
			      }
			    }
			  }
			}
		      }
		    }
		  }
		  if (0 != ok) {
		    ok = 0;
		    (void)dk4str_cpy_s(fnb, sz, app->user_home, NULL);
		    (void)dk4str_cat_s(fnb, sz, dk4app_open_kw[13], NULL);
		    (void)dk4str_cat_s(fnb, sz, dk4app_open_kw[10], NULL);
		    (void)dk4str_cat_s(fnb, sz, app->prog_name, NULL);
		    (void)dk4str_cat_s(fnb, sz, dk4app_open_kw[11], NULL);
		    (void)dk4str_cat_s(fnb, sz, dktb, NULL);
		    (void)dk4str_cat_s(fnb, sz, dk4app_open_kw[11], NULL);
		    (void)dk4str_cat_s(fnb, sz, pi2b, NULL);
		    dk4path_correct_sep(fnb);
		    app->dir_tmp = dk4str_dup(fnb, NULL);
		    if (NULL != app->dir_tmp) {
#if VERSION_BEFORE_20160127
		      ok = dk4app_mkdir_hierarchy(app->dir_tmp, 0, app);
#else
		      ok = 1;
#endif
		      

#line 1920 "dk4appop.ctr"
		    }
		  }
		}
	      }
	    }
	  }
	}
      }
    }
  }
  if (0 == ok) {
    /* ERROR: Failed to construct log file name */
    ae->error_found = E_LOG_FILE_NAME;
  }
  

#line 1935 "dk4appop.ctr"
}



/**	Process command line arguments.
	Save all non-option arguments in my_argc and my_argv if
	an option specification available, all arguments otherwise.
	@param	app	Application to set up.
	@param	ae	Application open error structure.
	@param	argc	Number of command line arguments.
	@param	argv	Command line arguments array.
*/
static
void
dk4app_open_process_argc_argv(
  dk4_app_t		*app,
  dk4_app_error_t	*ae,
  int			 argc,
  dkChar		**argv
)
{
  dkChar	 b[DK4_MAX_PATH + 128];	/* Private copy */
  dkChar	*argptr;		/* Current option to process. */
  dkChar	*pv;			/* Option value pointer */
  dkChar	*opv;			/* Original option value pointer */
  const dkChar	*endptr;		/* First char after value */
  dk4_option_t	*ospec;			/* Option specification */
  dk4_pref_t	*prefptr;		/* Preference */
  double	 dval;			/* Double value */
  dk4_um_t	 uval;			/* Unsigned integer value */
  dk4_im_t	 ival;			/* Integer value */
  size_t	 szval;			/* Size argument */
  int		 i;			/* Current option index to process */
  int		 argsused;		/* used elements in my_argv */
  int		 res;			/* Numeric conversion result */
  int		 must_add;		/* Flag: Must add to list */
  int		 oi;			/* Output index */
  

#line 1973 "dk4appop.ctr"
  if (0 < argc) {				

#line 1974 "dk4appop.ctr"
    app->my_argv = dk4mem_new(DK4_PDKCHAR,argc,NULL);
    if (NULL != app->my_argv) {			

#line 1976 "dk4appop.ctr"
      for (i = 0; i < argc; i++) { (app->my_argv)[i] = NULL; }
      if (dk4app_open_options_check(app)) {	

#line 1978 "dk4appop.ctr"
        argsused = 0;
        for (i = 1; i < argc; i++) {
	  argptr = argv[i];	

#line 1981 "dk4appop.ctr"
	  if (dkT('-') == *argptr) {	

#line 1982 "dk4appop.ctr"
	    if (dkT('-') == argptr[1]) {	

#line 1983 "dk4appop.ctr"
              if (0 == dk4app_open_check_long_opt(app, ae, &(argptr[2]))) {
                if (0 != dk4str_cpy_s(b,DK4_SIZEOF(b,dkChar),&(argptr[2]),NULL))
		{
                  

#line 1987 "dk4appop.ctr"
                  opv = NULL;
                  pv = dk4str_chr(b, dkT('='));
                  if (NULL != pv) {
                    *(pv++) = dkT('\0');
                    pv = dk4str_start(pv, NULL);
                  }
                  if (NULL != pv) {
                    opv = dk4str_chr(&(argptr[2]), dkT('='));
                    if (NULL != opv) { opv = dk4str_start(++opv, NULL); }
                  }
                  ospec = dk4sto_it_find_like(
                    app->i_opt_long, (void *)b, 3
                  ); 

#line 2000 "dk4appop.ctr"
                  if (NULL != ospec) {		

#line 2001 "dk4appop.ctr"
                    ospec->found = 1;
                    switch ((ospec->spec).argtype) {
                      case DK4_OPT_ARG_BOOL:
                      case DK4_OPT_ARG_SIZE:
                      case DK4_OPT_ARG_INT:
                      case DK4_OPT_ARG_UNSIGNED:
                      case DK4_OPT_ARG_DOUBLE:
                      case DK4_OPT_ARG_STRING: {	

#line 2009 "dk4appop.ctr"
                        if (NULL == opv) {	

#line 2010 "dk4appop.ctr"
                          if ((1 + i) < argc) {	

#line 2011 "dk4appop.ctr"
                            opv = argv[++i];
                          }
                        }
                        if (NULL != opv) {	

#line 2015 "dk4appop.ctr"
                          switch ((ospec->spec).argtype) {
                            case DK4_OPT_ARG_BOOL: {
                              (ospec->val).b = 0;
                              if (dk4str_is_bool(opv)) {	

#line 2019 "dk4appop.ctr"
                                (ospec->val).b = dk4str_is_on(opv);
                              } else {			

#line 2021 "dk4appop.ctr"
                                /* ERROR: Not a boolean argument */
				dk4app_log_base5(
				  app, DK4_LL_ERROR, 33, 14, 15,
				  b, opv
				);
                                if (0 == ae->error_found) {
                                  ae->error_found = E_INVALID_OPTION_ARG;
                                }
                              }
                            } break;
                            case DK4_OPT_ARG_SIZE: {
                              (ospec->val).s = 0;
                              szval = 0;
                              endptr = NULL;
                              res = dk4ma_input_dk_dec_size_t(
                                &szval, opv, &endptr, 1, NULL
                              );
                              if (0 != res) {		

#line 2039 "dk4appop.ctr"
                                (ospec->val).s = szval;
                              } else {			

#line 2041 "dk4appop.ctr"
                                /* ERROR: Not a size specification */
				dk4app_log_base7(
				  app, DK4_LL_ERROR, 32, 17, 154, 18,
				  b, endptr, opv
				);
                                if (0 == ae->error_found) {
                                  ae->error_found = E_INVALID_OPTION_ARG;
                                }
                              }
                            } break;
                            case DK4_OPT_ARG_INT: {
                              ival = (dk4_im_t)0L;
                              (ospec->val).i = (dk4_im_t)0L;
                              endptr = NULL;
                              res = dk4ma_input_dk_dec_dk4_im_t(
                                &ival, opv, &endptr, 1, NULL
                              );
                              if (0 != res) {		

#line 2059 "dk4appop.ctr"
                                (ospec->val).i = ival;
                              } else {			

#line 2061 "dk4appop.ctr"
                                /* ERROR: Not an integer value */
				dk4app_log_base7(
				  app, DK4_LL_ERROR, 31, 20, 154, 21,
				  b, endptr, opv
				);
                                if (0 == ae->error_found) {
                                  ae->error_found = E_INVALID_OPTION_ARG;
                                }
                              }
                            } break;
                            case DK4_OPT_ARG_UNSIGNED: {
                              uval = (dk4_um_t)0UL;
                              (ospec->val).u = (dk4_um_t)0UL;
                              endptr = NULL;
                              res = dk4ma_input_dk_dec_dk4_um_t(
                                &uval, opv, &endptr, 1, NULL
                              );
                              if (0 != res) {		

#line 2079 "dk4appop.ctr"
                                (ospec->val).u = uval;
                              } else {			

#line 2081 "dk4appop.ctr"
                                /* ERROR: Not an unsigned integer value */
				dk4app_log_base7(
				  app, DK4_LL_ERROR, 30, 23, 154, 24,
				  b, endptr, opv
				);
                                if (0 == ae->error_found) {
                                  ae->error_found = E_INVALID_OPTION_ARG;
                                }
                              }
                            } break;
                            case DK4_OPT_ARG_DOUBLE: {	

#line 2092 "dk4appop.ctr"
                              dval = 0.0;
                              (ospec->val).d = 0.0;
                              endptr = NULL;
                              res = dk4ma_input_dk_double(
                                &dval, opv, &endptr, 1, NULL
                              );
                              if (0 != res) {		

#line 2099 "dk4appop.ctr"
                                (ospec->val).d = dval;
                              } else {			

#line 2101 "dk4appop.ctr"
				dk4app_log_base7(
				  app, DK4_LL_ERROR, 29, 26, 154, 27,
				  b, endptr, opv
				);
                                if (0 == ae->error_found) {
                                  ae->error_found = E_INVALID_OPTION_ARG;
                                }
                              }
                            } break;
                            case DK4_OPT_ARG_STRING: {	

#line 2111 "dk4appop.ctr"
                              (ospec->val).t = opv;
                            } break;
                          }
                        } else {			

#line 2115 "dk4appop.ctr"
                          /* ERROR: Option requires argument */
			  dk4app_log_base3(
			    app, DK4_LL_ERROR, 28, 12, b
			  );
                          if (0 == ae->error_found) {
                            ae->error_found = E_OPT_REQUIRES_ARG;
                          }
                        }
                      } break;
                    }
                  } else {			

#line 2126 "dk4appop.ctr"
                    /* Handle preference */
                    if (NULL != pv) { 

#line 2128 "dk4appop.ctr"
                      prefptr = dk4sto_it_find_like(app->i_pref_cmd, b, 1);
                      if (NULL != prefptr) {	

#line 2130 "dk4appop.ctr"
                        opv = dk4str_dup(pv, NULL);
                        if (NULL != opv) {	

#line 2132 "dk4appop.ctr"
#if 0
                          dk4mem_release(prefptr->value);
#endif
			  /*	2016-12-10	Sanitize memory.
			  */
			  dk4str_release_sanitized(prefptr->value);
                          prefptr->value = opv;
                        } else {			

#line 2140 "dk4appop.ctr"
                          /* ERROR: Failed to save preference value */
			  dk4app_log_base5(
			    app, DK4_LL_ERROR, 34, 35, 36, b, pv
			  );
                          if (0 == ae->error_found) {
                            ae->error_found = E_MEMORY;
                          }
                        }
                      } else {			

#line 2149 "dk4appop.ctr"
                        prefptr = dk4pref_open(b, pv, NULL);
                        if (NULL != prefptr) {	

#line 2151 "dk4appop.ctr"
                          if (0 != dk4sto_add(app->s_pref_cmd, prefptr, NULL)) {
                            

#line 2153 "dk4appop.ctr"
                          } else {	

#line 2154 "dk4appop.ctr"
                            /* ERROR: Failed to save preference value */
			    dk4app_log_base5(
			      app, DK4_LL_ERROR, 34, 35, 36,
			      b, pv
			    );
                            dk4pref_close(prefptr);
                            if (0 == ae->error_found) {
                              ae->error_found = E_MEMORY;
                            }
                          }
                        } else {			

#line 2165 "dk4appop.ctr"
                          /* ERROR: Failed to save preference value */
			  dk4app_log_base5(
			    app, DK4_LL_ERROR, 34, 35, 36,
			    b, pv
			  );
                          if (0 == ae->error_found) {
                            ae->error_found = E_MEMORY; 
                          }
                        }
                      }
                      dk4app_pref_apply(app, ae, b, pv);
                    } else {			

#line 2177 "dk4appop.ctr"
                      /* ERROR: Missing preference value */
		      dk4app_log_base3(app, DK4_LL_ERROR, 37, 38, b);
                      if (0 == ae->error_found) {
                        ae->error_found = E_OPT_REQUIRES_ARG;
                      }
                    }
                  }
                } else {			

#line 2185 "dk4appop.ctr"
                  /* ERROR: Option too long! */
		  dk4app_log_base3(app, DK4_LL_ERROR, 39, 40, argptr);
                  if (0 == ae->error_found) {
                    ae->error_found = E_OPTION_TOO_LONG;
                  }
                }
	      }
	    } else {				

#line 2193 "dk4appop.ctr"
	      ospec = dk4sto_it_find_like(
	        app->i_opt_short, (void *)(&(argptr[1])), 1
	      );
	      if (NULL != ospec) {		

#line 2197 "dk4appop.ctr"
		ospec->found = 1;
		pv = NULL;
		switch ((ospec->spec).argtype) {
		  case DK4_OPT_ARG_BOOL:
		  case DK4_OPT_ARG_SIZE:
		  case DK4_OPT_ARG_INT:
		  case DK4_OPT_ARG_UNSIGNED:
		  case DK4_OPT_ARG_DOUBLE:
		  case DK4_OPT_ARG_STRING: {	

#line 2206 "dk4appop.ctr"
		    pv = dk4str_start(&(argptr[2]), NULL);
		    if (NULL == pv) {		

#line 2208 "dk4appop.ctr"
		      if ((1 + i) < argc) {	

#line 2209 "dk4appop.ctr"
		        pv = argv[++i];
		      }
		    }
		    if (NULL != pv) {
		      switch ((ospec->spec).argtype) {
		        case DK4_OPT_ARG_BOOL: {
			  (ospec->val).b = 0;
			  if (dk4str_is_bool(pv)) {	

#line 2217 "dk4appop.ctr"
			    (ospec->val).b = dk4str_is_on(pv);
			  } else {			

#line 2219 "dk4appop.ctr"
			    /* ERROR: Not a boolean argument */
			    dk4app_log_base5(
			      app, DK4_LL_ERROR, 13, 14, 15,
			      argptr, pv
			    );
			    if (0 == ae->error_found) {
			      ae->error_found = E_INVALID_OPTION_ARG;
			    }
			  }
			} break;
			case DK4_OPT_ARG_SIZE: {
			  (ospec->val).s = 0;
			  szval = 0;
			  endptr = NULL;
			  res = dk4ma_input_dk_dec_size_t(
			    &szval, pv, &endptr, 1, NULL
			  );
			  if (0 != res) {		

#line 2237 "dk4appop.ctr"
			    (ospec->val).s = szval;
			  } else {			

#line 2239 "dk4appop.ctr"
			    /* ERROR: Not a size specification */
			    dk4app_log_base7(
			      app, DK4_LL_ERROR, 16, 17, 154, 18,
			      argptr, endptr, pv
			    );
			    if (0 == ae->error_found) {
			      ae->error_found = E_INVALID_OPTION_ARG;
			    }
			  }
			} break;
			case DK4_OPT_ARG_INT: {
			  ival = (dk4_im_t)0L;
			  (ospec->val).i = (dk4_im_t)0L;
			  endptr = NULL;
			  res = dk4ma_input_dk_dec_dk4_im_t(
			    &ival, pv, &endptr, 1, NULL
			  );
			  if (0 != res) {		

#line 2257 "dk4appop.ctr"
			    (ospec->val).i = ival;
			  } else {			

#line 2259 "dk4appop.ctr"
			    /* ERROR: Not an integer value */
			    dk4app_log_base7(
			      app, DK4_LL_ERROR, 19, 20, 154, 21,
			      argptr, endptr, pv
			    );
			    if (0 == ae->error_found) {
			      ae->error_found = E_INVALID_OPTION_ARG;
			    }
			  }
			} break;
			case DK4_OPT_ARG_UNSIGNED: {
			  uval = (dk4_um_t)0UL;
			  (ospec->val).u = (dk4_um_t)0UL;
			  endptr = NULL;
			  res = dk4ma_input_dk_dec_dk4_um_t(
			    &uval, pv, &endptr, 1, NULL
			  );
			  if (0 != res) {		

#line 2277 "dk4appop.ctr"
			    (ospec->val).u = uval;
			  } else {			

#line 2279 "dk4appop.ctr"
			    /* ERROR: Not an unsigned integer value */
			    dk4app_log_base7(
			      app, DK4_LL_ERROR, 22, 23, 154, 24,
			      argptr, endptr, pv
			    );
			    if (0 == ae->error_found) {
			      ae->error_found = E_INVALID_OPTION_ARG;
			    }
			  }
			} break;
			case DK4_OPT_ARG_DOUBLE: {	

#line 2290 "dk4appop.ctr"
			  dval = 0.0;
			  (ospec->val).d = 0.0;
			  endptr = NULL;
			  res = dk4ma_input_dk_double(
			    &dval, pv, &endptr, 1, NULL
			  );
			  if (0 != res) {		

#line 2297 "dk4appop.ctr"
			    (ospec->val).d = dval;
			  } else {			

#line 2299 "dk4appop.ctr"
			    dk4app_log_base7(
			      app, DK4_LL_ERROR, 25, 26, 154, 27,
			      argptr, endptr, pv
			    );
			    if (0 == ae->error_found) {
			      ae->error_found = E_INVALID_OPTION_ARG;
			    }
			  }
			} break;
			case DK4_OPT_ARG_STRING: {	

#line 2309 "dk4appop.ctr"
			  (ospec->val).t = pv;
			} break;
		      }
		    } else {		

#line 2313 "dk4appop.ctr"
		      /* ERROR: Option requires argument */
		      dk4app_log_base3(
	  		app, DK4_LL_ERROR, 11, 12, argptr
		      );
		      if (0 == ae->error_found) {
		        ae->error_found = E_OPT_REQUIRES_ARG;
		      }
		    }
		  } break;
		}
	      } else {				

#line 2324 "dk4appop.ctr"
	        /* ERROR: Unexpected option */
		dk4app_log_base3(app, DK4_LL_ERROR, 9, 10, argptr);
		if (0 == ae->error_found) {
		  ae->error_found = E_UNEXPECTED_OPTION;
		}
	      }
	    }
	  } else {				

#line 2332 "dk4appop.ctr"
	    (app->my_argv)[argsused++] = argptr;
	  }
	}
	app->my_argc = argsused;
      } else {					

#line 2337 "dk4appop.ctr"
        oi = 0;
        for (i = 0; (i + 1) < argc; i++) {
	  must_add = 1;
	  if (dkT('-') == (argv[i + 1])[0]) {
	    if (dkT('-') == (argv[i + 1])[1]) {
	      if (0 == dk4app_open_check_long_opt(app, ae, &((argv[i+1])[2]))) {
	        /*	Treat all long options as preferences.
		*/
		must_add = dk4str_cpy_s(
		  b, DK4_SIZEOF(b,dkChar), &((argv[i + 1])[2]), NULL
		);
		if (0 != must_add) {
		  opv = NULL;
		  pv = dk4str_chr(b, dkT('='));
		  if (NULL != pv) {
		    *(pv++) = dkT('\0');
		    pv = dk4str_start(pv, NULL);
		  }
		  if (NULL != pv) {
		    opv = dk4str_chr(&((argv[i + 1])[2]), dkT('='));
		    if (NULL != opv) { opv = dk4str_start(++opv, NULL); }
#ifdef __clang_analyzer__
		      /*	2015-08-21 / 2016-01-20
				Static code analysis by clang complains
				opv is set but not used later. Probably true.
		      */
		      (void)opv;
#endif
		  }
		  if (NULL != pv) {
		    prefptr = dk4sto_it_find_like(app->i_pref_cmd, b, 1);
		    if (NULL != prefptr) {
		      opv = dk4str_dup(pv, NULL);
		      if (NULL != opv) {
#if 0
		        dk4mem_release(prefptr->value);
#endif
			/*	2016-12-10	Sanitize memory.
			*/
			dk4str_release_sanitized(prefptr->value);
			prefptr->value = opv;
		      } else {
		        dk4app_log_base5(
			  app, DK4_LL_ERROR, 34, 35, 36, b, pv
			);
			if (0 == ae->error_found) {
			  ae->error_found = E_MEMORY;
			}
		      }
		    } else {
		      prefptr = dk4pref_open(b, pv, NULL);
		      if (NULL != prefptr) {
		        if (0 == dk4sto_add(app->s_pref_cmd, prefptr, NULL)) {
			  dk4app_log_base5(
			    app, DK4_LL_ERROR, 34, 35, 36, b, pv
			  );
			  dk4pref_close(prefptr);
			  if (0 == ae->error_found) {
			    ae->error_found = E_MEMORY;
			  }
			}
		      } else {
		        dk4app_log_base5(
			  app, DK4_LL_ERROR, 34, 35, 36, b, pv
			);
			if (0 == ae->error_found) {
			  ae->error_found = E_MEMORY;
			}
		      }
		    }
		    dk4app_pref_apply(app, ae, b, pv);
		  } else {
		    dk4app_log_base3(app, DK4_LL_ERROR, 37, 38, b);
		    if (0 == ae->error_found) {
		      ae->error_found = E_OPT_REQUIRES_ARG;
		    }
		  }
		} else {
		  /* ERROR: Option too long! */
		  dk4app_log_base3(app, DK4_LL_ERROR, 39, 40, argv[i + 1]);
		  if (0 == ae->error_found) {
		    ae->error_found = E_OPTION_TOO_LONG;
		  }
		}
	      }
	      must_add = 0;
	    }
	  }
	  if (0 != must_add) {
	    (app->my_argv)[oi++] = argv[i + 1];
	  }
	}
	app->my_argc = oi;			

#line 2430 "dk4appop.ctr"
      }
    } else {					

#line 2432 "dk4appop.ctr"
      if (0 == ae->error_found) { ae->error_found = E_MEMORY; }
    }
  } else {					

#line 2435 "dk4appop.ctr"
    if (0 == ae->error_found) { ae->error_found = E_ARGC; }
  } 

#line 2437 "dk4appop.ctr"
}



/**	Keep private copies of version text, help file name, help text
	and license text.
	@param	app
	@param	ae
	@param	version
	@param	helpfile
	@param	helptext
	@param	lictext
*/
static
void
dk4app_open_save_names(
  dk4_app_t		*app,
  dk4_app_error_t	*ae,
  const dkChar		*version,
  const	dkChar		*helpfile,
  const	dkChar * const	*helptext,
  const	dkChar * const	*lictext
)
{
  int		ok = 0;
  if (NULL != version) {
    if (NULL != helpfile) {
      if (NULL != helptext) {
        if (NULL != lictext) {
	  ok = 1;
	  app->help_text = helptext;
	  app->lic_text = lictext;
	  app->vers_text = dk4str_dup(version, NULL);
	  app->help_file_name = dk4str_dup(helpfile, NULL);
	  if ((NULL == app->vers_text) || (NULL == app->help_file_name)) {
	    if (0 == ae->error_found) { ae->error_found = E_MEMORY; }
	  }
	}
      }
    }
  }
  if (0 == ok) {
    if (0 == ae->error_found) { ae->error_found = E_BUG_ARGUMENTS; }
  }
}



/**	Open an application structure for a command line program.
	@param	argc		Number of command line arguments.
	@param	argv		Command line arguments array.
	@param	optspec		Option specification, may be NULL.
	@param	szoptspec	Number of elements in optspec.
	@param	groupname	Program group name.
	@param	version		Version number string.
	@param	helpfile	Help file name.
	@param	helptext	Default help text.
	@param	lictext		License conditions text.
	@param	enablog		Flag: Enable logging.
	@return	Pointer to dk4_app_t structure on success, NULL on error.
*/
static
dk4_app_t *
dk4app_open_internally(
  int					  argc,
  dkChar 				**argv,
  const dk4_option_specification_t	 *optspec,
  size_t				  szoptspec,
  const dkChar				 *groupname,
  const	dkChar				 *version,
  const	dkChar				 *helpfile,
  const dkChar * const			 *helptext,
  const dkChar * const			 *lictext,
  int					  enablog
)
{
  dk4_app_error_t	 ae;			/* Error during startup */
  dk4_app_t		*back	=	NULL;	/* Function result */
  int			 quiet	=	0;	/* Flag: Run quiet */

  

#line 2518 "dk4appop.ctr"
  DK4_MEMRES(&ae, sizeof(dk4_app_error_t));
  ae.error_found = 0;
  if (NULL != groupname) {
    if ((0 < argc) && (NULL != argv)) {
      quiet = dk4app_open_silence_check(argc, argv);
      back  = dk4mem_new(dk4_app_t,1,NULL);
      if (NULL != back) {
        /* Set all members to reasonable default */
        dk4app_open_initialize(back);

	back->group_name = dk4str_dup(groupname, NULL);
	if (NULL == back->group_name) {
	  ae.error_found = E_MEMORY;
	}

	if (0 == ae.error_found) {
	  dk4app_open_save_names(
	    back, &ae,
	    version, helpfile, helptext, lictext
	  );
	}

	/* Find host name */
	if (0 == ae.error_found) {
	  dk4app_open_find_host_name(back, &ae);
	}

	/* Find user name and home directory */
	if (0 == ae.error_found) {
	  dk4app_open_find_user_name_and_home(back, &ae);
	}

	/* Find language and region */
	if (0 == ae.error_found) {
	  dk4app_open_find_language_region(back, &ae);
	}

	/* Find executable path name and derive directory names from it */
	if (0 == ae.error_found) {
	  dk4app_open_find_executable(back, &ae, argv[0]);
	}

	/* Create option storage from option specification */
	if (0 == ae.error_found) {
	  dk4app_open_create_options_storages(
	    back, &ae, optspec, szoptspec
	  );
	}

	/* Create preferences storage */
	if (0 == ae.error_found) {
	  dk4app_open_create_pref_storages(back, &ae);
	}

	/* Create string table storage */
	if (0 == ae.error_found) {
	  dk4app_open_create_stt_storage(back, &ae);
	}

	/* Read preference files */
	if (0 == ae.error_found) {
	  dk4app_open_read_preference_files(back, &ae);
	}

	/* Construct log file name, create log directory */
	if ((0 != enablog) && (0 == quiet) && (0 == ae.error_found)) {
	  dk4app_open_prepare_file_logging(back, &ae);
	}

        /* Enable logging if not switched to silent mode */
	if ((0 != enablog) && (0 == quiet) && (0 == ae.error_found)) {
	  back->log_enabled = 1;
	}

	/* We must be able to log, if there are errors */
	if (0 == ae.error_found) {
	  /* Process command line arguments, search for options */
	  dk4app_open_process_argc_argv(back, &ae, argc, argv);
	}

	/* If there were serious errors, destroy the structure */
	if (ae.error_found) {
	  dk4app_open_report_error(back, &ae);
	  dk4app_close(back);
	  back = NULL;
	}
      } else {				

#line 2605 "dk4appop.ctr"
        if (0 == quiet) {
	  char buf[64];
	  int  res;
	  res = dk4ma_write_c8_decimal_unsigned(
	    buf, sizeof(buf), (dk4_um_t)sizeof(dk4_app_t), 0, NULL
	  );
	  if (0 != res) {
	    fputs(dk4app_open_c8_kw[1], stderr);
	    fputs(buf, stderr);
	    fputs(dk4app_open_c8_kw[2], stderr);
	  } else {
	    fputs(dk4app_open_c8_kw[0], stderr);
	  }
	  fputc('\n', stderr);
	  fflush(stderr);
	}
      }
    } else {				

#line 2623 "dk4appop.ctr"
    }
  } else {				

#line 2625 "dk4appop.ctr"
  }
  if (NULL != back) {			

#line 2627 "dk4appop.ctr"
    if (0 != dk4app_log_do(back, DK4_LL_DEBUG)) {	

#line 2628 "dk4appop.ctr"
      (void)dk4app_log_debug_texts(back, NULL);
      dk4app_log_startup_debug(back);
    }
  } 

#line 2632 "dk4appop.ctr"
  return back;
}




dk4_app_t *
dk4app_open_cmd(
  int					  argc,
  dkChar 				**argv,
  const dk4_option_specification_t	 *optspec,
  size_t				  szoptspec,
  const dkChar				 *groupname,
  const	dkChar				 *version,
  const	dkChar				 *helpfile,
  const dkChar * const			 *helptext,
  const dkChar * const			 *lictext
)
{
  dk4_app_t *back;
  back = dk4app_open_internally(
    argc, argv, optspec, szoptspec, groupname, version,
    helpfile, helptext, lictext, 1
  );
  return back;
}




dk4_app_t *
dk4app_open_silent(
  int					  argc,
  dkChar 				**argv,
  const dk4_option_specification_t	 *optspec,
  size_t				  szoptspec,
  const dkChar				 *groupname,
  const	dkChar				 *version,
  const	dkChar				 *helpfile,
  const dkChar * const			 *helptext,
  const dkChar * const			 *lictext
)
{
  dk4_app_t *back;
  back = dk4app_open_internally(
    argc, argv, optspec, szoptspec, groupname, version,
    helpfile, helptext, lictext, 0
  );
  return back;
}



void
dk4app_set_fast_close(dk4_app_t *app)
{
  if (NULL != app) {
    app->fast_close = 1;
  }
}