File: isql.c

package info (click to toggle)
virtuoso-opensource 7.2.12%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 299,780 kB
  • sloc: ansic: 655,047; sql: 508,209; xml: 269,573; java: 84,064; javascript: 79,847; cpp: 37,662; sh: 32,429; cs: 25,702; php: 12,690; yacc: 11,661; lex: 7,933; makefile: 7,309; jsp: 4,523; awk: 1,719; perl: 1,013; ruby: 1,003; python: 326
file content (11331 lines) | stat: -rw-r--r-- 335,437 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
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
/*
 *  $Id$
 *
 *  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 *  project.
 *
 *  Copyright (C) 1998-2024 OpenLink Software
 *
 *  This project is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; only version 2 of the License, dated June 1991.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#include <stdlib.h>
#undef MALLOC_DEBUG
#include "Dk.h"
#include "libutil.h"
#include "odbcinc.h"
#include "timeacct.h"
#include "Wi/sqlver.h"

#ifdef WIN32
#include <windows.h>
# include <winsock.h>		/* For struct timeval */
#include <process.h>
#include <conio.h>
#include <time.h>
#else
#include <netdb.h>
#include <netinet/in.h>
#endif

#include <locale.h>

#include "isql_tchar.h"

#if (defined(UNICODE) || defined (_UNICODE)) && !defined (WIN32) && !defined (HAVE_WPRINTF)
#error It appears that your system does not support unicode console input/output. If you happend to be using glibc 2.1, please upgrade to more recent version
#endif


#if defined (WITH_READLINE)
#include <readline/readline.h>
#include <readline/history.h>
#endif

#if defined (WITH_EDITLINE)
#include <editline/readline.h>
#endif

/* Changed from 0.9845b at 07-DEC-1997 */
/* Changed from 0.9846b at 26-JAN-1998 */
/* Changed from 0.9847b at 12-FEB-1998 */
/* Changed from 0.9848b at 18-MAR-1998 */
#define ISQL_VERSION _T("" DBMS_SRV_VER "")


TCHAR *isql_version = ISQL_VERSION;

/*

ISQL/ISQLODBC  --  Interactive SQL client shell for KUBL DBMS


VERSION HISTORY:

The original bare-boned version (isql.c): Orri Erling

From the version 0.972b (since January 1997) onward developed
by Antti Karttunen.

This program is also used for reading in SQL-files produced
by DBDUMP.

Changes, from January 1997 onward

Version 0.972b: implemented first versions of new features, like

* Printing correctly blob and date/timestamp columns.

* The first rudimental versions of foreach, set, show and load commands.

* Commands TABLES, COLUMNS, PRIMARYKEYS, STATISTICS, etc. calling
the corresponding SQL-API-functions.

* Counting of the bracelevel of procedure definitions.


02-MAR-1997 AK  Corrected print_error.
03-MAR-1997 AK  Added FOREACH TIMESTAMP for testing timestamp params.
Cleaned few help texts (foreach and set).

20-MAR-1997 AK  Version changed from 0.973b to 0.974b
25-MAR-1997 AK  Added gnu readline routine thanks to a
tip from Han Holl. (should be compiled with
HAVE_READLINE defined in Linux)
Added also FOREACH item BETWEEN form
mainly for testing purposes.

26-MAR-1997 AK  Added spawning-possibility, i.e. if the input line
ends with the ampersand (&) instead of the normal
semicolon (;) then an asynchronous subprocess of
this same executable is spawned that should run the
statement(s) specified on that line. The statements
to be executed are given to the new subprocess
with the EXEC= option, and if there are more than
one statement they are separated with semicolons.

ISQL-command WAIT_FOR_CHILDREN will suspend the
execution of the parent process until all spawned
children have finished.

Corrected parse_statement_for_special_parameters
and count_bracelevel to take heed of the new
possibility of escaping singlequotes also with
backslash. Added also semicolon marking to
count_bracelevel to serve the new possibility
of executing multiple statements with one EXEC= option.
(See the note about spawning above.)

28-MAR-1997 AK   Moved read-eval-print loop from the end of main function
to its own function, called rep_loop. Now load command
will call it by default, unless the option -r is specified
in which case the old load_raw routine is used which
can be applied only to procedure definitions.

Also, when reading with rep_loop (interactively from user
or with load command without -r option) and
CREATE PROCEDURE or TRIGGER definition is encountered
which doesn't end with semicolon it is now sent to
server anyway.

New ISQL-commands ECHO and NOP. An empty line followed
by semicolon is also now ignored (not sent to server).
Added get_next_token macros like $RETVAL, $RETVALLEN,
$ROWCNT, $EQU, $NEQ and $IF with which it is possible
to write conditional ISQL-commands, so helping us
to write test-scripts with isql itself.

E.g. a command like following is legal, and will print
"nautajauheliha" followed by newline:

echo $if $equ 3 3 $if $equ 4 5 "sika" "nauta" "mursu" "jauheliha\n";

Also following works:

$IF $NEQ $RETVAL "FOU" EXIT ECHO "Assertion passed RETVAL=" $RETVAL "\n";

But this DOESN'T work:

$IF $EQU $RETVAL "FOU" ECHO "Assertion passed RETVAL=" $RETVAL "\n" EXIT;

(Which means that we need later a real IF-STATEMENT
with THEN, ELSE and FI keywords, as well as few other
real shell features like GOTO, WHILE,
SET EXIT ON ERRORS LIKE "4*", whatever...)

Note that these haven't been documented yet, and their
usage might slightly change later, so use with caution.

15-APR-1997 AK   Corrected the eating of SET PASSWORD and SET USER GROUP
commands, which are now sent to server as they should.

18-APR-1997 AK   New $-macros $STATE and $MESSAGE for SQL error state
checking, and $ARGV[$I] for argument passing from
command line.

19-APR-1997 AK   Made error messages more informative in regard to
in which file, at which line they occurred, with the
new system of load expression and linecount stacks.
(See macro current_linecount() and functions
push_to_loadexpr_stack and drop_from_loadexpr_stack)

It's now possible to overwrite ARGV vector elements
with SET ARGV[n] 'newvalue'  command.
Together with a new $-macro $+ (plus) it can be used
for keeping counts of passes and failings of tests, etc.
(Yes, it's a crude attempt at user variables and
leaks memory a little with each new strdup which is
not currently freed anywhere.) Now (29-APR-97) it is,
see G_argv_touched.

21-APR-1997 AK   Renamed get_next_keyword to get_next_token

24-APR-1997 AK   Version changed to 0.976b because of the following
changes:

Changed isql_exit(-1) to isql_exit (2) when losing
connection, because IF ERRORLEVEL 1 doesn't detect
the exit code -1 in Windows NT MS-DOS shell.
SO! Always use only exit codes 0 (success) and
1 (generic failure), 2 (Lost line, 08S01) or
3 (could not connect to) when exiting from isql !!!
(should be #defined somewhere indeed...)

Added fclose(in_fp) to isql_foreach after statements
like: FOREACH item IN filename
has been executed.

Modified field_print so that it now avoids any
unnecessary padding with blanks on the right side
if the column to be printed is the rightmost one.
Because of that, added inx argument to field_print
and print_datetime_col

Made load_raw (actually do_statement that it calls)
to return more civilized error-message when something
is screwed: instead of showing whole procedure
definition twice it will now show procedure definition
once and then the procedure name if it was specified.
(Well, actually the first procedure definition of
error message is included in the error message
that is got from Kubl server.)

Modified EXIT statement so that EXIT NOT is NO-OP,
allowing statements like:

EXIT $IF $EQU $ARGV[0] 10 $+ 50 $ARGV[0] NOT;

which exits with exit code 60 if $ARGV[0] is ten
(presumably keeping a somekind of failure counter)
but otherwise does nothing special, and continues
from the next statement. (Real IF-statement is
needed again...)

Implemented deadlock retry counter tentatively
(new variables perm_deadlock_retries and
vol_deadlock_retries, ISQL-command SET DEADLOCK_RETRIES
modified print_error function and IF_SEVERE_ERR_GO).
Currently has any effect only with
FOREACH statements if SET DEADLOCK_RETRIES is
explicitly set to non-zero value.

Splicing of EXEC= option created by spawn_to_background
before the -i option (if it is present), so that it
will have any effect in the daughter process.


02-MAY-1997       Started doing a WEB-interface, taking pieces from
old W3SQL, whose Windows version is more or less
in deep freeze now.


03-MAY-1997       Version changed to 0.977b

Added n+1 casts between unsigned char * and char *
(using macros SCP and UCP) to make code C++ compilable.

Added the command SET ACCESSMODE RO/RW.

The $dollar_form substitution implemented in the
function parse_statement_for_special_parameters
which may now destructively modify its string argument.
See comment in the beginning of function exec_one.

Changed spawnv to spawnvp (Windows), and execv to execvp
(Unix), so that spawning commands to background
will find isql from anywhere in the path.

14-MAY-1997       Version changed to 0.978b
IMPORTANT CHANGE: Now the connection is done in the
delayed way, that is, only after a command is
encountered that really needs a database connection.
This allows specifying DSN, USERNAME and
PASSWORD, as well as HTTP content type used with
the commands SET DSN=, SET UID=,
SET PWD=, and SET CONTENT_TYPE= in the beginning
of the loaded ISQL file.
However, if the datasource (possibly together with
username and password) has been given from the
command line in traditional way (that is: not with
equal signs as DSN=Kubl, but just as Kubl)
then the connection is established almost immediately
in the beginning, as was the case before this version,
so this should not break any existing test suites.

Added also LOAD= option, for explicitly specifying
from the command line the file(s) to be loaded.

Wrote isql_exit as its own function (not just as macro)
that will do things like writing command history
(if compiled with HAVE_READLINE in Linux),
SQLShutdown (if connected to server) and
terminating HTML page clearly with proper tags
(if in Web mode, and content_type is "text/html")
before finally exiting.

Expressions like $LAST[$ARGV[$I]] work correctly now,
because of proper counting of nested brackets
with the new function find_closing_point.

Implemented associative array U for User and URL
variables. Additions to get_next_token and
is_set_subcommand.

get_next_token now cuts tokens to first
opening or closing brace/bracket/parenthesis/angle
bracket, as well as to comma, that is, all of the
{ }, [ ], ( ) and < >, will be
returned one token (of one character) per time.

Started major streamlining of get_next_token
by tabulating all more or less regular $ dollar-macros
into the table isql_variables with the macro
add_var_def. Implemented the rest of comparison
macros, $LT, $LTE, $GT and $GTE at the same time.

This streamlining needs to be extended into
is_set_subcommand also, e.g. so that it will
use the same table-scanning function as
get_next_token, both for showing and setting.
(mostly done now).

The return values of get_next_token should be cleansed
e.g. special token_null for NULL's, and token_error
for syntax errors, etc.

Needs to be done: Finishing deadlock retry counter.

Cleaning up the code for C++ compilation.
(Mostly done 3-MAY-1997, by adding SCP and UCP casts).

Dividing this source file to separate modules.

Checking for any minor strdup memory leaks.

Collecting all the global variables into somekind
of a structure (that would passed around in stack),
so as to make code more re-entrant, in anticipation
for the eventual multi-threaded implementation.

BULK-parameters for foreach statement.

And output parameters for procedures, e.g.
call FOU(?,?Oout_param1<2000>)
would allocate a buffer of 2000 plus one bytes,
assign a new (or old???) user variable named out_param1
to it, and bind that as the second, output parameter
of the procedure call to function FOU.
Then it would be possible to say echo $out_param1
Not that way, instead call FOU(?,?>2000U{newout})
or FOU(?,?2000>U{newout}) ? or FOU(?,?2000U{newout}) ?
Anyway, it would allocate a new 2000 bytes long
of user-variable called newout (if one didn't exist
already, in which it would be used instead), and
then bind it as output variable before continuing.

Also, dollar form $:name which were like $name
but would automatically wrap the result in single
quotes unless it evaluates to NULL.

More fancy options for FOREACH (see comments before
isql_foreach) allowing to build all kinds of loops
(e.g. non-?-parameterized version of FOREACH, calling
exec_one each time.)
and for reading in text files formatted in various ways
(e.g. CSV), maybe also something like
FOREACH name, value OF U call something(?,?); ???


Adding more $-macros, like $OR, $AND,
$LIKE and all the sundry others.

Implementing real IF THEN ELSE FI command, GOTO,
SET EXIT ON ERRORLEVEL '2*', and whatever else is
needed.

Substituting of $dollar_forms in SQL-statements
themselves (done now, see above), and/or implementing
?$ -parameters for foreach.
(The whole concept requires rethinking).

Ability to execute multiple statements (lines)
in one transaction. E.g. form like:
TRANSACT { statement1;
statement2;
statement3;
etc;
} -- Transaction ends here.
Or maybe just special commands:
TRX_BEGIN; and TRX_END; ?

Documentation.

Misgivings:       This program seems to become a yet another shell-like
interpreted programming language, of which there
are already more than enough in this world.
Building a really good ODBC-api into some existing
tool language (e.g. Perl, Python, or even gawk, csh
or bash) might be a better idea after all, provided
that language/shell/tool is easily available on all
platforms. But anyway, it's a handy tool (when finally
documented)!

11-JUN-1997  AK   Version now 0.980b
Added options TRAILING_NEWLINES=0 & BINARY_OUTPUT=ON
for the exact outputting of contents of blob columns.
Use them together with options VERBOSE=OFF BANNER=OFF
(possibly also PROMPT=OFF) and of course BLOBS=ON
to fetch a blob column just as it was originally
inserted into the database.

25-JUN-1997  AK   Added call to SQLRowCount in show_results and
exec_for_foreach.
SQLRowCount is now implemented with wiodbc.dll
version 1.05b, Kubl version 0.96b G14d3.
show_results needs a thorough cleaning anyway.
Version changed to 0.981b

09-JUL-1997  AK   Added second argument to push_to_loadexpr_stack
(which is the next current input stream used by load)
effecting that now FOREACH item
without the IN keyword reads the following items
(lines or blobs) always from the same source as
where the FOREACH statement itself was read from,
not stupidly from stdin as was case before.
This form of FOREACH doesn't yet work with
Webgate, when FOREACH statement is in EXEC-option.

21-AUG-1997  AK   Squashed an insidious bug in field_print which
caused numbers in the rightmost fields to be omitted
from the output. (Also, the TMPBUF_SIZE has been
grown to 560 to avoid crashings with certain test
procedures.)

11-SEP-1997  AK   Corrected the sleep command in the Windows version.
Hadn't realized that Sleep requires milliseconds,
not seconds, so now the argument is multiplied by
thousand. Version changed to 0.984b.

12-SEP-1997  AK   Added a spawning-possibility of shell-commands with !
(Added a new function spawn_shell_command).
Added also a Forced Load feature, which checks in
the beginning of main that if this program is started
with a name containing an underscore then the part
after an underscore is used as a filename (suffixed
with .sql) which is automatically loaded in as a
first file. By giving EXIT; as a last command in that
file the administrator can make sure that nothing else
is done with isql than what is specified in that file.
This is an useful feature for allowing certain
specific operations to be run by the public via the Web.

14-NOV-1997  AK   Version 0.9845b
Improved RFC1867 HTML file-upload receiving.
(Still to be extensively checked).
Now correctly escapes singlequotes and all other
problematic characters (e.g. newlines, backslashes)
when doing $-prefixed ISQL-variable substitutions
inside string literals (i.e. singlequotes) in SQL
statements that are to be sent to the server.
Improved function show_results so that it now
will dynamically increase the size of column's
result buffer in case the new precision of column
is greater than the previously allocated size.
Tried to compile the program with SQLCLI headers
(sqlcli.h, sqlicli2.h and odbc2cli.h) instead
of Microsoft's sql.h and sqlext.h. Made some
changes for that.

07-DEC-1997  AK   Version 0.9846b. Doesn't bind blob columns anymore,
unless BLOBS=OFF (print_blobs_flag is zero.)

26-JAN-1998  AK   Version 0.9847b
Added SQL_SCOPE_TRANSACTION and SQL_SCOPE_SESSION
to SpecialColumns commands
(used instead of default SQL_SCOPE_CURROW when
option /t or /s is present with /b).

*/

#ifndef ODBC_ONLY
# define ISQL_TYPE	_T("(Virtuoso)")
# define ISQL_TYPE_N	"(Virtuoso)"
#elif defined (UDBC)
# define ISQL_TYPE	_T("(UDBC)")
# define ISQL_TYPE_N	"(UDBC)"
#else
# define ISQL_TYPE	_T("(ODBC)")
# define ISQL_TYPE_N	"(ODBC)"
#endif

/* The default values for the three first command line arguments.
Have to be global as a few functions need these.
*/
#if defined(ODBC_ONLY)
TCHAR *datasource = _T("Local Virtuoso");
TCHAR *username = NULL;
TCHAR *password = NULL;

#else
TCHAR *datasource = _T("localhost:1111");
TCHAR *username = _T("dba");
TCHAR *password = NULL;
TCHAR *connect_port = NULL;
TCHAR *encryption = NULL;
TCHAR *ca_list = NULL;
int pwd_cleartext = 0;
#endif

#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif


#define NO  !
#define NOT !
#define empty_stringp(X) (!*(X))


#if defined (WITH_READLINE) || defined (WITH_EDITLINE)
/* The following declaration should be located before loading Dk.h */
static void readline_free(void *ptr) { free(ptr); }



TCHAR isqlhist[313] = {'\0'};	/* Path name for readline history file */
#endif

#if 1
/* Was: #define strncasecmp isqlt_tcsncmp */
#define strncasecmp isqlt_tcsnicmp	/* Changed by AK. */
#define strcasecmp  isqlt_tcsicmp
#endif


#ifndef UINT
#define UINT unsigned int
#endif

#if defined (UNICODE) || defined (_UNICODE)
#define UTCHAR TCHAR
#define tspawnvp _wspawnvp
#else
#define UTCHAR unsigned TCHAR
#define tspawnvp _spawnvp
#endif

#define UCP(X) ((UTCHAR *) ((X)))
#define SCP(X) ((TCHAR *) ((X)))

#ifndef SQL_NO_TOTAL
#define SQL_NO_TOTAL (-4)
#endif


#ifdef WIN32
# ifndef get_msec_count
#  define get_msec_count() GetTickCount ()
# endif
# define isql_sleep(S) Sleep(((DWORD)(S))*1000)	/* Wants milliseconds. */
#else
/* In Unix we have real sleep */
# define isql_sleep(S) sleep(S)
#endif


typedef int (*PFI) (TCHAR *);	/* Pointer to Function returning int (K&R C) */

typedef TCHAR *(*PFSTR) (TCHAR *);
/* Pointer to function returning a char pointer, taking one string as
its argument. */

#define NULLFP ((PFI)0)		/* Null function pointer. Don't call this one! */
#define IS_NULLFP(X) ((NULLFP) == ((PFI)(X)))

#define MAX_COL_WIDTH   15

#define MAXFILE_DEPTH 101

/* Stupid: should be in one stack, multiplied of an appropriate structure */
TCHAR *LOAD_loadexpr_stack[MAXFILE_DEPTH + 1] =
{_T("Top-Level"), NULL};
/* First one should be immediately initialized to stdin in the beginning
of main: */
FILE *LOAD_filepointer_stack[MAXFILE_DEPTH + 1] =
{NULL};
unsigned long int LOAD_linecount_stack[MAXFILE_DEPTH + 1] =
{0};
unsigned long int LOAD_lsline_stack[MAXFILE_DEPTH + 1] =
{0};
int loadfile_sp = 0;		/* "Stack Pointer" referencing arrays above */


#define current_loadexpr() LOAD_loadexpr_stack[loadfile_sp]
#define current_input_stream() LOAD_filepointer_stack[loadfile_sp]
#define current_linecount() LOAD_linecount_stack[loadfile_sp]
#define latest_statement_begins_at() LOAD_lsline_stack[loadfile_sp]

TCHAR tmp_SQL_error_state[15] =
{'O', 'K', '\0'};
TCHAR SQL_error_state[15] =
{'O', 'K', '\0'};
#ifdef WIN32
TCHAR tmp_SQL_error_message[1000] =
{'O', 'K', '\0'};
TCHAR SQL_error_message[1000] =
#else
TCHAR tmp_SQL_error_message[10 * SQL_MAX_MESSAGE_LENGTH - 1] =
{'O', 'K', '\0'};
TCHAR SQL_error_message[10 * SQL_MAX_MESSAGE_LENGTH - 1] =
#endif
{'O', 'K', '\0'};

#define clear_SQL_state_and_message()\
(isqlt_tcscpy(SQL_error_state,_T("OK")),isqlt_tcscpy(SQL_error_message,_T("OK")))

/* Set by connect_to_datasource, can referred with $DBMS and $DRIVER
dollar forms, in get_next_token. */
TCHAR info_dbms[100] =
{'\0'};
TCHAR info_driver[100] =
{'\0'};


HDBC hdbc = ((HDBC) 0);		/* Was: NULL; */
HENV henv = ((HENV) 0);		/* Was: NULL; */
HSTMT stmt = ((HSTMT) 0);	/* Was: NULL; */

TCHAR **G_argv;			/* Global versions of argv and argc, used by */
int G_argc, G_argc_i = 0;	/* spawn_to_background for example. */
/* The elements of this vector are switched to non-zeros when the
corresponding elements of G_argv vector are changed to strdupped
strings, so that if they are changed second time, the previous
version can be free'ed. Just to avoid a minor memory leak.
*/
TCHAR G_argv_touched[200] =
{0};				/* Should be all zeros and bytes. */


TCHAR *web_content_type = _T("text/plain");
TCHAR *web_query_string = NULL;	/* from environment variable QUERY_STRING */

#define in_HTML_mode() (!isqlt_tcscmp(web_content_type,_T("text/html")))

int web_mode = 0;		/* Is set to 1 in the beginning of main if used
			       as a cgi-script */
int kubl_mode = 1;		/* Currently affects only how MAXROWS are handled. */
int print_banner_flag = 1;
int print_types_also = 1;
int verbose_mode = 1;
int echo_mode = 0;
int explain_mode = 0;
int sparql_translate_mode = 0;
int vert_row_out_mode = 0;
int csv_mode = 0;
int csv_rfc4180_mode = 0;
int profile_mode = 0;
int json_mode = 0;
int flag_newlines_at_eor = 1;	/* By default print one nl at the end of row */
long int select_max_rows = 0;	/* By default show them all. */
long int perm_deadlock_retries = 0, vol_deadlock_retries = 0;
int flag_binary_output = 0;	/* Has effect on Windows platforms. */
int commit_mode = 0;		/* Zdravko: for manual autocommit */
char *isql_locale = NULL;	/* Current locale */

int virtuoso_shutdown = 0;
int virtuoso_debug = 0;

#ifdef ODBC_ONLY
int virtext=0;
#else
int virtext=1;
#endif


TCHAR *progname = _T("ISQL");	/* Might be set in main to *argv */
TCHAR *print_prompt = _T("SQL> ");

TCHAR *empty_string = _T("");
/* Overridden with EXEC command line option */
#define MAXEXECS_FROM_CMDLINE 101
int execs_from_cmdline = 0;
TCHAR *execute_these_only[MAXEXECS_FROM_CMDLINE + 1] =
{NULL};

#define MAX_ARGS_FOR_SHELL_PROGRAM MAXEXECS_FROM_CMDLINE

#define MAX_FILES_FROM_COMMAND_LINE 22
TCHAR *files_to_load[MAX_FILES_FROM_COMMAND_LINE + 2] =
{NULL};
int load_n_files = 0;		/* A zero-based index to previous. */

int fully_connected = 0;


#define SYSTEM_VARIABLE_URL_PREFIX _T("S_")

#define DEFAULT_DATASOURCE_IN_UNIX _T("localhost:1111")
#define DEFAULT_HTML_TEMPLATE_FILE _T("isql.htm")
#define DATASOURCE_TOKEN _T("INPUT_DATASOURCE")

TCHAR *form_action = _T("");
TCHAR *get_list_of_datasources (int for_html, TCHAR *dest_buf, int dest_size);
int output_html_file (TCHAR *templatename);
void print_csv_banner ();
void print_csv_rfc4180_banner ();
int print_csv_row();
int print_csv_rfc4180_row();
void print_json_banner (void);
void print_json_footer (void);
int print_json_row(int row_nr);

/* Only after fully_connected has been set to non-zero (i.e. after
full connection to data source and statement allocation)
are the settings saved to delayed_settings vector really
executed in function is_set_subcommand
*/
#define MAX_DELAYED_SETTINGS 50
TCHAR *delayed_settings[MAX_DELAYED_SETTINGS + 2] =
{NULL};
int n_delayed_settings = 0;	/* A zero-based index to previous. */

#define MIN_INPUT_SIZE (10*1024*1024)
TCHAR input1[MIN_INPUT_SIZE + 20];
int input_size = MIN_INPUT_SIZE;
TCHAR *input = input1;
#define INPUT_SIZE input_size

#define not_pointing_to_input_buf(PTR)\
(((PTR) < &input[0]) || ((PTR) >= &input[INPUT_SIZE]))


#ifndef TMPBUF_SIZE
#define TMPBUF_SIZE  1024
#endif

#define RETVALBUFSIZE 3003
TCHAR retvalbuf[RETVALBUFSIZE + 10] =	/* Referred with $RETVAL */
{'N', 'O', 'T', ' ', 'S', 'E', 'T', '\0'};
SQLLEN cbRetVal = 0;		/* Referred with $RETVALLEN */
SQLLEN sdtmp;
int N_rows = 0;			/* Referred with $ROWCNT */

/* These are now global so that we can refer to this with
get_next_token dollar-macro $LWE (LAST WORD ECHOED).
See functions isql_echo and get_next_token.
However, now that has been much obsoleted by another kludge: $LIF
Now isql_echo_lwe is used more as a flag. If non-null it tells
that echo command has been already used, and in web-mode it is
no use to print ContentType header anymore.
Giving ECHO $LWE as the first command is a stupid thing to do.
*/
#define ISQL_ECHOBUF_SIZE 2001
TCHAR isql_echo_word_buf[ISQL_ECHOBUF_SIZE + 20] =
{'\0'};
TCHAR *isql_echo_lwe = NULL;	/* Was: &isql_echo_word_buf[0]; */

/* Can be changed to stdout by user with ERRORS=STDOUT from commandline */
FILE *error_stream = NULL;	/* Should be immediately assigned to stderr. */

/* If this flag is toggled to non-zero with BLOBS ON command, the blobs
   are printed out in full. */
int print_blobs_flag = 0;
int foreach_err_break = 0;
int bind_blobs_flag = 0;	/* By default, don't do it. */
/* These are swapped automatically in the beginning if we use an
   Old Kubl driver, version less than 0.92: */
int times_conform_to_odbc_flag = 1, times_to_strings_flag = 0;
int clear_hidden_crs_flag = 1;	/* When reading multiline blobs. */
/* If set to 0 then no dollar-substitution is done inside SQL-statements: */
int flag_macro_substitution = 1;
int flag_ignore_params = 0;
int trim_string_result = 0;
int command_text_on_error = 1;

#ifndef ODBC_ONLY
int flag_bind_return_values = 1;
#else /* Doesn't work with the current version of MS Driver Manager. */
int flag_bind_return_values = 0;
#endif

TCHAR * current_file = NULL;

/* These are all for multipart/form-data file upload thing. */
TCHAR *form_filename = NULL;	/* filename from the first Content-Disposition encountered with filename parameter. */
TCHAR *form_filefieldname = NULL;	/* name from the first Content-Disposition encountered with filename parameter. */
TCHAR *form_last_content_type = NULL;	/* Latest Content-Type encountered. */
TCHAR *form_last_encoding = NULL;	/* Latest Content-Transfer-Encoding */
TCHAR *form_boundary = NULL;	/* The boundary string used, with the initial --, but not with the terminating -- */

/* ============================================================== */
/* Output Variables related to HTML output                        */

TCHAR *oo_sb = _T("<TABLE>");	/* Set Begin... */
TCHAR *oo_se = _T("</TABLE>");	/*  and Set End */
TCHAR *oo_rb = _T("<TR>");		/* Row Begin... */
TCHAR *oo_re = _T("</TR>");		/*  and Row End */
TCHAR *oo_hb = _T("<TH>");		/* Heading cell Begin... */
TCHAR *oo_he = _T("</TH>");		/*  and Heading cell End */
TCHAR *oo_db = _T("<TD>");		/* normal Cell Begin... */
TCHAR *oo_de = _T("</TD>");		/*  and normal Cell End */

TCHAR *oo_ob = _T("<PRE>");		/* Other text, Begin. */
TCHAR *oo_oe = _T("</PRE>");		/* Other text, End. */

/* CSV output mode */
TCHAR *csv_field_separator = _T(";");
TCHAR *csv_rfc4180_field_separator = _T(",");
TCHAR *csv_row_separator = _T("\n");
TCHAR *csv_rfc4180_row_separator = _T("\r\n");

int oo_esc = 1;			/* Normally escape <, > and & */


/* Necessary quite soon. */
TCHAR *get_next_token (TCHAR **str_ptr, TCHAR *result, int maxsize,
		      TCHAR *input_buffer, int input_size);


void html_print_head_title (FILE * fp, TCHAR *title1, TCHAR *title2, TCHAR *title3);

int flag_head_already_printed = 0;

int
print_http_headers_if_not_already_printed ()
{

  if ((0 == flag_head_already_printed) && web_mode)
    {				/* Print the HTML headers, as this is the last change to do it. */
      flag_head_already_printed = 1;
      {
/* Obsolete: if(NOT isql_echo_lwe)  No output done yet with ECHO command? */

	{
	  isqlt_tprintf (_T("Content-Type: %") PCT_S _T("\r\n\r\n"), web_content_type);
	  if (in_HTML_mode ())
	    {
	      /* Show the statement(s) to be executed in the title: */
	      html_print_head_title (stdout, _T("OpenLink Interactive SQL ") ISQL_TYPE, _T(": "),
				     execute_these_only[0]);
	      isqlt_fputts (oo_ob, stdout);	/* And the Otherness Begins, i.e. <PRE> */
	    }
	  fflush (stdout);
	  return (1);
	}
      }
    }
/* IvAn/IsqlSyncro/000805 If errors are sent into stderr, but
   isql is called like isql exec=... > logfile 2>&1, then error
   messages are printed asynchronously.
   Adding \c fflush() here fix this bug fully,
   but it slows down logging more than necessary: too many fflushes
   in output of every error.. */
  fflush (stdout);
  return (0);
}

#define isql_puts     print_http_headers_if_not_already_printed(), isqlt_putts
#define isql_fputs    print_http_headers_if_not_already_printed(), isqlt_fputts
#define isql_putc     print_http_headers_if_not_already_printed(), isqlt_puttc
#define isql_putchar  print_http_headers_if_not_already_printed(), isqlt_puttchar
#define isql_printf   print_http_headers_if_not_already_printed(), isqlt_tprintf
#define isql_fprintf  print_http_headers_if_not_already_printed(), isqlt_ftprintf

#if defined(__GNUC__) && __GNUC__ == 3 && (__GNUC_MINOR__ == 0 || __GNUC_MINOR__ == 1)
/* When compiling with optimization printf is defined as a macro and
   GNU C versions 3.0.x and 3.1.x don't support preprocessor directives
   inside macro arguments like those used in this file. */
# undef printf
#endif


/* Taken and modified from w3srv.c of w3sql: */

TCHAR *html_escapes[256] =
{NULL};


#define HTML_ESCAPE(c,s) html_escapes [c] = s;
#define html_escapes_has_been_initialized() html_escapes['<']

void
html_init_escapes ()
{
  HTML_ESCAPE ('\0', _T("&#0;"));	/* To catch strange NUL characters. */
  HTML_ESCAPE ('<', _T("&lt;"));
  HTML_ESCAPE ('>', _T("&gt;"));
  HTML_ESCAPE ('&', _T("&amp;"));
}


/* If maxcount is non-zero, then print that many characters from str,
   (Also the '\0' characters between).
   If maxcount is zero, then print the str to the first terminating zero.
 */
void
html_print_escaped_n (TCHAR *str, FILE * fp, int maxcount)
{
  int inx;
  UTCHAR c;

  if (NO html_escapes_has_been_initialized ())
    {
      html_init_escapes ();
    }

  for (inx = 0;
       (c = ((UTCHAR) (str[inx]))),
       (maxcount ? (inx < maxcount) : c);
       inx++)
    {
      TCHAR *esc = html_escapes[c];
      if (esc && oo_esc)
	{
	  isql_fputs (esc, fp);
	  continue;
	}
      else
	{
	  isql_putc (c, fp);
	}
    }
}


#define html_print_escaped(str,fp) html_print_escaped_n ((str),(fp),0)


void
html_print_head_title (FILE * fp, TCHAR *title1, TCHAR *title2, TCHAR *title3)
{
  isqlt_fputts (_T("<HTML><HEAD><TITLE>"), fp);
  html_print_escaped (title1, fp);
  html_print_escaped (title2, fp);
  html_print_escaped (title3, fp);
  isqlt_fputts (_T("</TITLE></HEAD><BODY>\n"), fp);
  fflush (fp);
}


/* ============================================================== */
/*  isql_exit function.                                           */
/* ============================================================== */

void
isql_exit (int status)
{

#if defined (WITH_READLINE) || defined (WITH_EDITLINE)
  if (isqlhist[0])
    {
      write_history (isqlhist);
    }
#endif

  if (fully_connected)
    {
      SQLDisconnect (hdbc);
    }

  if (in_HTML_mode ())		/* These tags not really needed, but it's clean */
    {
      isql_fputs (oo_oe, stdout);	/* Otherness Ends, i.e. </PRE> */
      isql_fputs (_T("</BODY></HTML>"), stdout);	/* Whole document Ends also. */
    }

  exit (status);
}


/* ============================================================== */

int print_error (HENV e1, HDBC e2, HSTMT e3, RETCODE rc);

#define IF_ERR_GO(stmt, tag, foo) \
  if (SQL_ERROR == foo)  \
    { \
      print_error (henv, hdbc, stmt, foo);  \
      goto tag; \
    } \
  else if (SQL_SUCCESS_WITH_INFO == foo) \
    { \
      print_error (henv, hdbc, stmt, foo);  \
    }

/* GK: the deadlock handling works only in manual commit */
#define IF_ERR_OR_DEADLOCK_GO(stmt, tag1, tag2, foo) \
  if (SQL_ERROR == foo) \
    { \
      if(2 == print_error (henv, hdbc, stmt, foo) && commit_mode == 0) \
	 goto tag2; \
      else \
	 goto tag1; \
    } \
  else if (SQL_SUCCESS_WITH_INFO == foo) \
    { \
      print_error (henv, hdbc, stmt, foo);  \
    }

/* It was like this: print_error (stmt, stmt, stmt);  \
   Produced spurious extra error messages now and then.
 */

#if 0 /* GK: this is dangerous : it can lead to endless loops */
/* This is like previous, but in case the error was just something like:
   *** Error 2001: Non unique primary key on torttu
   then don't go to the error tag.
   If the transaction was deadlocked and SET DEADLOCK_RETRIES has been
   set to non-zero (vol_deadlock_retries is still non-zero), then goto
   tag2 instead.
 */
#define IF_SEVERE_ERR_GO(stmt, tag1, tag2, foo) \
  if (SQL_ERROR == foo)  { \
    int tmp_stat = print_error (henv, hdbc, stmt, foo); \
    if(1 == tmp_stat) goto tag1; \
    if(2 == tmp_stat) goto tag2; \
 }
#else
#define IF_SEVERE_ERR_GO(stmt, tag1, tag2, foo) \
  IF_ERR_JUST_PRINT (stmt, foo)
#endif

#define IF_ERR_JUST_PRINT(stmt, foo) \
  if (SQL_ERROR == (foo) || SQL_SUCCESS_WITH_INFO == (foo))  { \
    print_error (henv, hdbc, stmt, foo);  \
 }



#define MAXCOLNAME 60

#define MAXBUFFER 2002

#define MAXCOLS 200

/* Don't try to allocate more than this. */
/* This is 2^24. 2147483647=(2^31)-1, was 150000; */
SQLULEN o_buffer_sanity_check = 16777216;

typedef struct _out
  {
    TCHAR o_title[MAXCOLNAME + 1];
    SQLULEN o_width;		/* The width as by SQLDescribeCol
				   (changed from int to SQLULEN by AK 4-JAN-97) */
    SQLULEN o_allocated_buflen;	/* Usually MAX(MAXBUFFER,o_width) */
    SWORD o_type;
    char *o_buffer;		/* Buffer for SQLBindCol */
    SQLLEN o_col_len;		/* the length of the col on current row */
    SWORD o_nullable;		/* Last parameter returned by SQLDescribeCol, is either
				   SQL_NO_NULLS, SQL_NULLABLE or SQL_NULLABLE_UNKNOWN Added by AK 3-JAN-97 */
  }
stmt_out_t;

int n_out_cols_long;
#define n_out_cols ((SWORD) n_out_cols_long)
stmt_out_t out_cols[MAXCOLS + 1];
TCHAR *coltypetitles[MAXCOLS + 1];



/* ==================================================================== */
/* Checked malloc and strdup, called chemalloc and chestrdup, in case   */
/* that our virtual memory is exhausted...                              */
/* ==================================================================== */


TCHAR *
my_strncat (TCHAR *string1, const TCHAR *string2, size_t count)
{
  int str1len = (int) isqlt_tcslen (string1);
  return (isqlt_tcsncat (string1, string2, (count - str1len)));
}



TCHAR *
chemalloc (size_t size, TCHAR *where)	/* where can be NULL or empty */
{
  TCHAR *result;

  if (NO (result = (TCHAR *) malloc (size * sizeof (TCHAR))))
    {
      TCHAR num[11];
      isqlt_stprintf (num, _T("%lu"), (unsigned long) size);
      isql_putc ('\n', error_stream);
      isql_fputs (progname, error_stream);
      if (where && *where)	/* Not null, not empty. */
	{
	  isql_fputs (_T(" ("), error_stream);
	  isql_fputs (where, error_stream);
	  isql_fputs (_T(")"), error_stream);
	}
      isql_fputs (_T(": chemalloc failed when tried to allocate "),
		  stderr);
      fflush (error_stream);
      isql_fputs (num, error_stream);
      isql_fputs (_T(" bytes\n"), stderr);
      fflush (error_stream);
      isql_exit (1);
      return NULL;
    }
  else
    {
      return (result);
    }
}


TCHAR *
chestrdup (TCHAR *str, TCHAR *where)	/* where can be NULL or empty */
{
  TCHAR *res;

  if (!str)
    return str;
  if (NOT (res = isqlt_tcsdup (str)))
    {
/* Don't use isql_fprintf if memory is on the red! */
      isql_putc ('\n', error_stream);
      isql_fputs (progname, error_stream);
      if (where && *where)	/* Not null, not empty. */
	{
	  isql_fputs (_T(" ("), error_stream);
	  isql_fputs (where, error_stream);
	  isql_fputs (_T(")"), error_stream);
	}
      isql_fputs (_T(": chestrdup failed when tried to make a copy of: "),
		  stderr);
      fflush (error_stream);
      isql_fputs (str, error_stream);
      isql_putc ('\n', error_stream);
      fflush (error_stream);
      isql_exit (1);
      return NULL;
    }
  else
    {
      return (res);
    }
}


/* ==================================================================== */
/* Stuff for creating and accessing generic non-zero-integer lists      */
/* by AK 26-MAR-1997                                                    */
/* ==================================================================== */

typedef struct _intlist
{
  struct _intlist *next;
  int integer;
}
integer_list;


/* Doesn't check whether there is already num in pidlist.
   The first element of pidlist is always unused, "anchoring" node.
   The latest pushed num is stored at the first node after it, at
   intlist->next->integer, and the rest of list is linked to the
   "next" link of that node.
 */
int
push_to_integer_list (int num, integer_list * intlist)
{
  integer_list *next_pt = intlist->next;

  intlist->next = (integer_list *)
    chemalloc (sizeof (integer_list), _T("push_to_integer_list"));

  intlist->next->next = next_pt;
  intlist->next->integer = num;

  return (num);
}

/* Returns the integer from the first node after the anchoring node,
   and frees that node, and makes anchoring node to point to the next
   node after that. If list is empty returns zero. */
int
pop_from_integer_list (integer_list * intlist)
{
  integer_list *next_pt = intlist->next;
  int num;

  if (NO next_pt)
    {
      return (0);
    }				/* An empty list. */
  num = next_pt->integer;
  intlist->next = next_pt->next;
  free (next_pt);
  return (num);
}

/* And similarly for simple associative string pair lists. */

typedef struct _stringpairlist
{
  struct _stringpairlist *next;
  TCHAR *s_name;
  TCHAR *s_value;
}
stringpair_list;


/* Doesn't check whether there is already name in the_list.
   The first element of the_list is always unused, _T("anchoring") node.
   The first set name is stored at the first node after it, at
   the_list->next->name and ->value, and the rest of list is linked
   to the "next" link of that node.
 */
int
push_to_stringpair_list (TCHAR *name, TCHAR *value,
			 stringpair_list * the_list)
{
  TCHAR *me = _T("push_to_stringpair_list");
  stringpair_list *next_pt = the_list->next;

  the_list->next = (stringpair_list *)
    chemalloc (sizeof (stringpair_list), me);

  the_list->next->next = next_pt;
  the_list->next->s_name = chestrdup (name, me);
  the_list->next->s_value = chestrdup (value, me);

  return (1);			/* Something more useful? */
}

/* Returns s_name from the first node after the anchoring node,
   (and sets value to p_value if p_value is NON-null pointer).
   and frees that node, and makes anchoring node to point to the next
   node after that. If list is empty returns NULL.
   Here is a small memory leak:
   Should actually free the strings returned???
   Anyway, this function is not currently used.
 */
TCHAR *
pop_from_stringpair_list (stringpair_list * the_list, TCHAR **p_value)
{
  stringpair_list *next_pt = the_list->next;
  TCHAR *name;

  if (NO next_pt)
    {
      return (NULL);
    }				/* An empty list. */
  name = next_pt->s_name;
  if (NULL != p_value)
    {
      *p_value = next_pt->s_value;
    }
  the_list->next = next_pt->next;
  free (next_pt);
  return (name);
}


stringpair_list *
get_ptr_to_stringpair_list (TCHAR *name,
			    stringpair_list * the_list)
{
  stringpair_list *next_pt = the_list->next;

  for (next_pt = the_list->next; (next_pt != NULL); next_pt = next_pt->next)
    {
      if (next_pt->s_name && !strcasecmp (next_pt->s_name, name))
	{
	  return (next_pt);
	}
    }

  return (NULL);		/* Not found, return NULL. */
}

TCHAR *
assoc_value_from_stringpair_list (TCHAR *name,
				  stringpair_list * the_list)
{
  stringpair_list *lp = get_ptr_to_stringpair_list (name, the_list);

  if (NULL == lp)
    {
      return (NULL);
    }
  else
    {
      return (lp->s_value);
    }
}



/*
   The first element of the_list is always unused, "anchoring" node.
   The first set name is stored at the first node after it, at
   the_list->next->s_name and ->s_value, and the rest of list is linked
   to the "next" link of that node.
   The name comparison is done in case insensitive way.
   E.g. NAKKI=makkara overwrites Nakki=Bratwurst definition.
   However, the s_name stays in its original form as Nakki
   name and value are strdupped by this function, and at the overwrite
   of variable value the previous value is free'ed.
 */
int
set_to_stringpair_list (TCHAR *name, TCHAR *value,
			stringpair_list * the_list)
{
  TCHAR *me = _T("set_to_stringpair_list");
  stringpair_list *next_pt = the_list->next;
  stringpair_list *prev_pt = the_list;

  for (next_pt = the_list->next; (next_pt != NULL);
       prev_pt = next_pt, next_pt = next_pt->next)
    {
      if (next_pt->s_name && !strcasecmp (next_pt->s_name, name))
	{			/* Free the previous value, if it's not NULL: */
	  if (next_pt->s_value)
	    {
	      free (next_pt->s_value);
	    }
	  /* Overwrite the value part. */
	  next_pt->s_value = chestrdup (value, me);
	  return (1);		/* Modified the old one. */
	}
    }

/* Otherwise, no previous value has been set for this name, let's
   allocate a new node to the end of list. */

  prev_pt = prev_pt->next = (stringpair_list *)
    chemalloc (sizeof (stringpair_list), me);

  prev_pt->next = NULL;
  prev_pt->s_name = chestrdup (name, me);
  prev_pt->s_value = chestrdup (value, me);

  return (0);			/* Added new one. */
}

int
dump_stringpair_list (TCHAR *title, stringpair_list * the_list)
{
  stringpair_list *next_pt = the_list->next;
  int count = 0;

  isql_fprintf (stdout, _T("%") PCT_S _T(":"), title);
  for (next_pt = the_list->next; (next_pt != NULL); next_pt = next_pt->next)
    {
      isql_fprintf (stdout, _T(" %") PCT_S _T("="), next_pt->s_name);
      if (NULL == next_pt->s_value)
	{
	  isql_fprintf (stdout, _T("NULL"));
	}
      else
	{
	  isql_fprintf (stdout, _T("\"%") PCT_S _T("\""), next_pt->s_value);
	}
      count++;
    }

  isql_fprintf (stdout, _T("\n"));
  return (count);
}


/* ================================================================== */
/* And here is an application of the previous functions.              */
/* We use them to implement $U{name} variables. $U is an associative  */
/* array containing all non S_ variables from URL line (in Web mode)  */
/* or any generic User-settable variables.                            */
/* If the amount of $U variables or demands for effectiveness grow,   */
/* then we can always implement this later as a hash table or         */
/* something equally fancy.                                           */
/* ================================================================== */


stringpair_list U_vars_space =
{NULL, NULL, NULL};
stringpair_list *U_vars = &U_vars_space;	/* The ->next is NULL. */



#define get_U_variable(name)\
 assoc_value_from_stringpair_list((name),U_vars)


#define set_U_variable(name,value)\
 set_to_stringpair_list((name),(value),U_vars)

#define dump_U_variables() dump_stringpair_list(_T("$U{}"),U_vars)


/* ================================================================== */
/*    And more specific stuff for handling loadexpression stack.      */
/* ================================================================== */


/* Might contain a small memory leak or unchecked strdup, but this
   is anyway used so rarely (for the command line implicit loads)
   that I don't care. Even better, I realize I don't need this one,
   so I comment it out!

   #define change_topmost_of_loadexpr_stack(loadexpr)\
   ((LOAD_loadexpr_stack[loadfile_sp] =\
   (loadfile_sp ? strdup(loadfile_sp) : loadfile_sp)),\
   (LOAD_linecount_stack[loadfile_sp] = 0),\
   (LOAD_lsline_stack[loadfile_sp] = 0))
 */


/*
   If loadexpr stack overflows exits with an error message,
   otherwise pushes the new load expression into stack
   and clears the corresponding line and latest statement begins at
   counts, and then returns the new stack pointer (which will always
   be above zero).
 */
int
push_to_loadexpr_stack (TCHAR *loadexpr, FILE * load_stream)
{
  if (loadfile_sp >= MAXFILE_DEPTH)
    {
      isql_fprintf (error_stream,
		    _T("\n***%") PCT_S _T(": Exceeded loading depth of %d, cannot do anymore: %") PCT_S _T("\nfrom %") PCT_S _T("\n"),
		    progname, MAXFILE_DEPTH, loadexpr, current_loadexpr ());
      isql_exit (1);		/* Could do return(0) also if caller handled this. */
      return 0;
    }
  else
    {
      loadfile_sp++;
      LOAD_loadexpr_stack[loadfile_sp]
	= chestrdup (loadexpr, _T("push_to_loadexpr_stack"));
      current_input_stream () = load_stream;

/* Was like this, but now chestrdup does the checking:
   if(NOT (LOAD_loadexpr_stack[loadfile_sp] = strdup(loadexpr)))
   {
   isql_fputs(
   "\n***Memory allocation failure in push_to_loadexpr_stack(",
   error_stream);
   isql_fputs(loadexpr,error_stream);
   isql_fputs("), exiting.\n",error_stream);
   fflush(error_stream);
   isql_exit(1);
   }
 */
      LOAD_linecount_stack[loadfile_sp] = 0;
      LOAD_lsline_stack[loadfile_sp] = 0;
      return (loadfile_sp);
    }
}


/* If not in error, drop the topmost things from the stack
   (and free the load expression string saved with strdup in
   push_to_loadexpr_stack), and return the new stack pointer.
 */
int
drop_from_loadexpr_stack ()
{
  TCHAR *loadexpr;

  if (loadfile_sp < 1)
    {
      isql_fprintf (error_stream,
	     _T("\n***%") PCT_S _T(": drop_from_loadexpr_stack: loadfile_sp = %d, below 1,")
		    _T(" you should not have dropped this much, good bye!\n"),
		    progname, loadfile_sp);
      isql_exit (1);
      return 0;
    }
  else
    {
      if (NULL != (loadexpr = LOAD_loadexpr_stack[loadfile_sp]))
	{
	  free (loadexpr);
	}
      return (--loadfile_sp);
    }
}



/* ==================================================================== */
/* Stuff for spawning and waiting daughter processes.                   */
/* by AK 26-MAR-1997                                                    */
/* ==================================================================== */

TCHAR *
last_of_argv (TCHAR **argv)
{
  if (!argv)
    {
      return (_T("NULL argv"));
    }
  while (*++argv);
  return (*(argv - 1));
}


#ifdef WIN32

/* I just guess that cwait of Windows NT works like this. At least
   when STATPTR==NULL and OPTIONS==0 it seems to work.
 */

#define waitpid(PID,STATPTR,OPTIONS) cwait((STATPTR),(PID),(OPTIONS))


/* Added 12-SEP-1997 a third argument, wait_for_him, which if non-zero
   forces the parent to wait for its child's exit. */

int
spawn_process (TCHAR *progname, TCHAR **argv, int wait_for_him)
{
  int pid;

  fflush (stdout);
  fflush (stderr);

  pid = (int) tspawnvp ((wait_for_him ? _P_WAIT : _P_NOWAIT), progname, argv);

/* When called with _P_WAIT spawnvp seems to return always 0.
   (But -1 on errors.) */

  if ((wait_for_him && (pid < 0)) || (pid <= 0))
    {
      TCHAR *arg1 = last_of_argv (argv);
      isql_fprintf (error_stream,
		  _T("\n%") PCT_S _T(": spawn_process: spawnvp failed for (pid=%d):\n%s\n"),
		    progname, pid, arg1);
    }

  return (pid);
}


#else /* Some kind of UNIX, we use fork and execl routines. */

#include <sys/wait.h>		/* For waitpid */

int
spawn_process (TCHAR *progname, TCHAR **argv, int wait_for_him)
{
  int pid, ret;

  fflush (stdout);
  fflush (stderr);
  switch ((pid = fork ()))
    {
    case -1:
      {
	TCHAR *arg1 = last_of_argv (argv);
	isql_fprintf (error_stream,
		      _T("\n%") PCT_S _T(": spawn_process: fork failed for:\n%s!\n"),
		      progname, arg1);
	return (pid);
      }
    case 0:			/* In child, do it. */
      {
	ret = isqlt_texecvp (progname, argv);	/* Was execv, but we need a path. */
/*        if(ret < 0) Should continue here only if execl failed. */
	{
	  TCHAR *arg1 = last_of_argv (argv);
	  isqlt_tperror (progname);
	  isql_fprintf (error_stream,
	  _T("\n%") PCT_S _T(": spawn_process in child: execvp (ret=%d) failed for:\n%s\n"),
			progname, ret, arg1);
	  isql_exit (-1);
	}
      }
    default:
      {
	if (wait_for_him)
	  {
	    waitpid (pid, NULL, 0);
	  }			/* Not tested! */
	return (pid);		/* In parent, return pid of child. */
      }
    }

}

#endif


UTCHAR *urlify (UTCHAR * dst, UTCHAR * src);
TCHAR *skip_blankos (TCHAR *str);

int
spawn_to_background (TCHAR *statements, integer_list * pidlist)
{
  TCHAR *exec_option = _T("EXEC=");
  TCHAR **new_argv;
  int exec_len = (int) isqlt_tcslen (exec_option);
  int pid, len, i, j;
  TCHAR *arg1;

  /* First allocate space for the new argument vector. */
  len = ((G_argc + 3) * sizeof (TCHAR *));	/* +3 should suffice. */
  new_argv = ((TCHAR **) chemalloc (len, _T("spawn_to_background")));
/*  if(NOT new_argv) { goto malloc_err; } chemalloc never returns NULL */

  /* When statements text is urlified the result can at max be three
     times longer than original, if every character is to be
     replaced by corresponding percent-hex-escape sequence,
     i.e. if every character is non alphanumeric. */
  len = (int) (3 * isqlt_tcslen (statements)) + exec_len + 1;

  arg1 = (TCHAR *) chemalloc (len, _T("spawn_to_background"));
/* chemalloc never returns NULL.
   if(NOT arg1)
   {
   malloc_err:
   isql_putc('\n',error_stream);
   isql_fputs(progname,error_stream);
   isql_fputs("spawn_to_background failed, cannot malloc ",error_stream);
   fflush(error_stream);
   isql_fprintf(error_stream,"%u",len);
   isql_fputs(" bytes for:\nEXEC=",error_stream);
   isql_fputs(statements,error_stream);
   isql_putc('\n',error_stream); fflush(error_stream);
   return(-1);
   }
   else
 */
  {
    isqlt_tcscpy (arg1, exec_option);
    urlify (UCP (arg1 + exec_len), UCP (statements));
  }


  /* Copy the initial arguments of this process to the new argument
     vector given to child process (until end or -u or -i option is found):
     If there is -i or -u option, then EXEC= has to be spliced before
     them as everything after -u and -i is ignored and has significance
     only as a means of passing information to dollar macros like
     $U{'var1'} or $ARGV[$I]
   */
  for (i = 0; ((NULL != G_argv[i]) && isqlt_tcscmp (G_argv[i], _T("-i"))
	       && isqlt_tcscmp (G_argv[i], _T("-u"))); i++)
    {
      new_argv[i] = G_argv[i];
    }
  j = i;			/* After this j will be one less than i (i.e. points one left) */
  new_argv[i++] = arg1;		/* Splice or add to end our new EXEC argument. */
  for (; (NULL != G_argv[j]); i++, j++)		/* Copy the rest from -i to end */
    {
      new_argv[i] = G_argv[j];
    }
  new_argv[i] = NULL;		/* And terminate the vector with NULL. */

/* The argument arg1 containing the statement itself has to be
   escaped by some means because at least Windows NT will go haywire
   if it sees that there are e.g. spaces. So it is urlified. */

  pid = spawn_process (progname, new_argv, 0);	/* Spawn and don't wait. */

  free (new_argv);
  free (arg1);

  if (pid > 0)
    {
      push_to_integer_list (pid, pidlist);
    }

  return (pid);
}


/* This should spawn some other command than ISQL(ODBC) itself
   to the background (the first token on statements line),
   with the following tokens as its arguments.
 */
int
spawn_shell_command (TCHAR *statements, integer_list * pidlist, int wait_for_it)
{
  TCHAR *new_argv[MAX_ARGS_FOR_SHELL_PROGRAM + 3];
  int pid, i;
  UTCHAR firstchar;	/* The original first char of token. Might be a singlequote. */
  TCHAR *token, *nextptr;
  TCHAR tmp2buf[TMPBUF_SIZE + 2];

  nextptr = skip_blankos (statements);

  /* If user gave nothing after !, then silently do nothing.
     Actually we should start an interactive command shell for
     the user, from the envvar COMSPEC in MS-DOS, and envvar SHELL
     in Unix, and as a default search for cmd.exe or /bin/sh */
  if (!*nextptr)
    {
      return (0);
    }

/* With ordinary ! commands (without trailing &) we use now system
   function which allows for us to give also internal MS-DOS commands
   like DIR, TYPE, SET, etc. */
  if (wait_for_it)
    {
      int stat;

      fflush (stdout);
      fflush (stderr);
      stat = isqlt_tsystem (statements);
      if (stat < 0)
	{
	  isql_fprintf (error_stream,
			_T("\n%") PCT_S _T(": **Call to system failed for shell command \"%") PCT_S _T("\" at line %lu of %") PCT_S _T(". stat=%d, errno=%d, errmsg=%") PCT_S _T("\n"),
			progname, statements,
			latest_statement_begins_at (), current_loadexpr (),
			stat, errno, strerror (errno));
	}
      return (stat);
    }

  i = 0;
  while ((nextptr = skip_blankos (nextptr))
	 && (firstchar = *((UTCHAR *) nextptr))
	 && (token = get_next_token (&nextptr, tmp2buf,
				     TMPBUF_SIZE,
				     NULL, 0)))
    {
      if (i >= MAX_ARGS_FOR_SHELL_PROGRAM)
	{
	  isql_fprintf (error_stream,
			_T("\n%") PCT_S _T(": **Too many (max. %d) arguments for shell command \"%") PCT_S _T("\" at line %lu of %") PCT_S _T(". The rest are ignored.\n"),
			progname, MAX_ARGS_FOR_SHELL_PROGRAM, new_argv[0],
			latest_statement_begins_at (), current_loadexpr ());
	  break;
	}
      new_argv[i++] = chestrdup (token, _T("spawn_shell_command"));
    }

  new_argv[i] = NULL;


  pid = spawn_process (new_argv[0], new_argv, wait_for_it);

  if (wait_for_it && (pid > 0))
    {
      push_to_integer_list (pid, pidlist);
    }

  return (pid);
}



/* Just pop all pids from pidlist and wait for each one with waitpid.
   If waitpid returns -1 or something, e.g. if the child has died already
   it doesn't matter, as the purpose is just to check here that
   no children are running anymore.
 */
int
wait_for_all_children (integer_list * pidlist)
{
  int pid, count = 0;

  while (0 != (pid = pop_from_integer_list (pidlist)))
    {
      waitpid (pid, NULL, 0);
      count++;
    }

  return (count);
}



/* ==================================================================== */
/* Stuff for parsing and manipulating character strings.                */
/* Should be really in a separate module.                               */
/* ==================================================================== */


/* The following function were taken from AK's conjugate software,
   module conjtest.cpp.
 */
UTCHAR *
parse_url_value (UTCHAR * url_piece)
{
  UTCHAR *s, *t;
  UTCHAR save_char;
  unsigned int tmp;

  s = t = url_piece;

  while (*s)
    {
      switch (*s)
	{
	case '+':
	  {
	    *t++ = ' ';
	    s++;
	    break;
	  }			/* Plus signs to spaces */
	case '%':
	  {			/* Double %% can be used for escaping a percent sign itself */
	    if (*(s + 1) == '%')
	      {
		*t++ = '%';
		s += 2;
	      }
	    else if (NOT isalnum (*(s + 1)))	/* Not followed by hex digits? */
	      {
		*t++ = *s++;
	      }
	    else if (isqlt_tcslen ((TCHAR *) s) >= 3)
	      {
		save_char = *(s + 3);
		*(s + 3) = '\0';
		/* Convert two hex digits to int */
		isqlt_stscanf (((const TCHAR *) s + 1), _T("%x"), &tmp);
		*(s + 3) = save_char;
		*t++ = ((UTCHAR) tmp);
		s += 3;
	      }
	    else
	      /* Copy the trailing percent signs literally. */
	      {
		*t++ = *s++;
	      }
	    break;
	  }
	default:
	  {
	    *t++ = *s++;
	    break;
	  }
	}
    }

  *t = '\0';
  return (url_piece);
}

/* This should in principle do the opposite as the function given above,
   i.e. to convert all spaces to plus signs, and all other strange
   characters (here: all non-alphanumeric) to corresponding three-char
   percent-hex-escape sequences, e.g. the plus-signs to %2B 's.
 */
UTCHAR *
urlify (UTCHAR * dst, UTCHAR * src)
{
  UTCHAR *ptr;
  UTCHAR *dstptr;

  for (ptr = src, dstptr = dst; *ptr; ptr++, dstptr++)
    {
      if (' ' == *ptr)
	{
	  *dstptr = '+';
	}
      else if (isalnum (*ptr))
	{
	  *dstptr = *ptr;
	}
      else
	/* Class strange, i.e. everything else, punctuations, etc. */
	{
	  *dstptr++ = '%';
	  isqlt_stprintf (SCP (dstptr), _T("%02x"), *ptr);
	  dstptr++;		/* Skip still one byte. */
	}
    }

  *dstptr = '\0';		/* Termination byte. */
  return (dst);
}



#define isoctdigit(C) (((C) & ~7) == 060)	/* 060 = 0x30 = 48. = '0' */
#define hexdigtoi(C) (isqlt_istdigit(C) ? ((C) - '0') : (toupper(C) - ('A' - 10)))

TCHAR *
skip_blankos (TCHAR *str)
{
  while (*str && isqlt_istspace (*str))
    {
      str++;
    }				/* Skip blankos. */
  return (str);
}

TCHAR *
get_rid_of_trailing_newline (TCHAR *str)
{
  TCHAR *ptr = str + isqlt_tcslen (str) - 1;	/* pointing to the last character */
  if (ptr < str)		/* If an empty string is given, it is given back intact */
    {
      return (str);
    }
  if ('\n' == *ptr)
    {
      *ptr = '\0';
      ptr--;
    }
  /* If there's also a trailing CR, then get rid of that also: */
  if ((ptr >= str) && ('\r' == *ptr))
    {
      *ptr = '\0';
    }
  return (str);
}

SQLLEN
get_rid_of_trailing_newline_and_CR_with_count (TCHAR *str, SQLLEN endpt)
{
  int removed = 0;

  TCHAR *ptr = str + endpt - 1;	/* pointing to the last character */
  if (ptr < str)		/* If an empty string is given, it is given back intact */
    {
      return (0);
    }
  if ('\n' == *ptr)
    {
      *ptr = '\0';
      ptr--;
      removed++;
    }
  /* If there's also a trailing CR, then get rid of that also: */
  if ((ptr >= str) && ('\r' == *ptr))
    {
      *ptr = '\0';
      removed++;
    }
  return (removed);
}

/* To clear any hidden trailing CRS of MS-DOS text files. */
int
get_rid_of_trailing_CRS_with_count (TCHAR *str, int endpt)
{
  int removed = 0;

  TCHAR *ptr = str + endpt - 1;	/* pointing to the last character */

  /* If an empty or one-byte string is given, zero is returned. */
  if (ptr < (str + 1))
    {
      return (0);
    }

/* If the last is newline and the second last is CR, then overwrite
   the CR with newline, and the newline with terminating zero, that is,
   remove the CR, and make a string one character shorter:
   Repeat as long as all hidden CR's before newline have been deleted.
 */
again:
  if ((ptr > str) && /* ('\n' == *ptr) && */ ('\r' == *(ptr - 1)))
    {
      *(ptr - 1) = *ptr;
      *ptr = '\0';
      ptr--;
      removed++;
      goto again;
    }

  return (removed);		/* return the count of removed CR's */
}

/* Not needed now:
   int get_rid_of_trailing_CRS(char *str)
   {
   return(get_rid_of_trailing_CRS_with_count(str,isqlt_tcslen(str)));
   }
 */


/* If there is in the end of string a semicolon (followed optionally
   by CR and/or NL), then CUTS the string at that point and returns 1,
   OTHERWISE returns 0, and DOESN'T MODIFY str */
int
get_rid_of_trailing_semicolon_if_there_is_one (TCHAR *str)
{
  UTCHAR *ptr = UCP (str + isqlt_tcslen (str) - 1);	/* To the last char */

/* Skip past all the white space characters and cr's in the end: */
  while ((ptr >= UCP (str)) && (isqlt_istspace (*ptr) || (*ptr == '\r')))
    {
      ptr--;
    }

/* If there's a trailing semicolon then get rid of that,
   and return non-zero: */
  if ((ptr >= UCP (str)) && (';' == *ptr))
    {
      *ptr-- = '\0';
      return (1);
    }
  return (0);
}


/* If there is in the end of string a semicolon or ampersand
   (followed optionally by CR and/or NL), then CUTS the string
   at that point and returns that character (i.e. ascii value of
   the semicolon or ampersand),
   OTHERWISE returns 0, and DOESN'T MODIFY str */
unsigned int
get_rid_of_trailing_semicolon_or_ampersand (TCHAR *str)
{
  UTCHAR ret;
  UTCHAR *ptr = UCP (str + isqlt_tcslen (str) - 1);	/* to the last char */

/* Skip past all the white space characters and cr's in the end: */
  while ((ptr >= UCP (str)) && (isqlt_istspace (*ptr) || (*ptr == '\r')))
    {
      ptr--;
    }

/* If there's a trailing semicolon or ampersand, then get rid of that,
   and return the character that was gotten rid of: */
  if ((ptr >= UCP (str)) && ((';' == *ptr) || ('&' == *ptr)))
    {
      ret = *ptr;
      *ptr-- = '\0';
      return (ret);
    }
  return (0);
}


unsigned int
hextoi (UTCHAR ** rest_ptr, UTCHAR * string)
{
  register UINT x;

  x = 0;
  while (*string && isxdigit (*string))
    {
      x = ((x << 4) + hexdigtoi (*string));
      string++;
    }
  *rest_ptr = string;
  return (x);
}


unsigned int
octoi (UTCHAR ** rest_ptr, UTCHAR * string)
{
  register UINT o;

  o = 0;
  while (isoctdigit (*string))
    {
      o = ((o << 3) + (*string++ - '0'));
    }
  *rest_ptr = string;
  return (o);
}


/* String should have an even number of hexadecimal digits, possibly
   followed by some whitespace or CR and/or NL, which are ignored.
   The hex-digit pairs are converted to corresponding byte values,
   and stored to the same string. So the real length, which is finally
   returned, will be about half of the original length.
   Note that the new string might contain one or more 0-bytes somewhere.
 */
int
unhex_string (TCHAR * _string)
{
  UTCHAR * string = (UTCHAR *) _string;
  UTCHAR *res_ptr, *next, *org_string = string;
  UTCHAR x;

  for (res_ptr = string; *string && *(next = (string + 1)) &&
       isxdigit (*string) && isxdigit (*next);
       string += 2)
    {
      x = ((UTCHAR) ((hexdigtoi (*string) << 4) + hexdigtoi (*next)));
      *res_ptr++ = x;
    }

  *res_ptr = '\0';		/* Terminating zero. Not actually needed. */
  return (int) (res_ptr - org_string);	/* Return the new length. */
}


/* Converts all backslash-escaped things in the string to their
   unescaped variants (modifying the same string), and returns
   the new length of the string.
   Added 8-FEB-1997 a new 'magical' escape-sequence \c
   which will just abruptly cut the string at that point.
   This fix is used for reading in with read_blob_from_input
   multiline blobs produced by DBDUMP whose last line DOESN'T
   have a trailing newline.

   Check the next function parse_string_literal how to really do it!

   At least now this doesn't interpret two consecutive single quotes
   as one.
 */
int
unescape_string (TCHAR * _string)
{
  UTCHAR *string = (UTCHAR *) _string;
  UTCHAR *res_ptr, *org_string = string;

  for (res_ptr = string; *string;)
    {
      if ('\\' != *string)	/* An ordinary character? */
	{
	  *res_ptr++ = *string++;
	}
      else
	/* Haa! It is an escape character! */
	{

/* New escapes added 23.AUG.1991 \a for bell, and \v for vertical tab
   as specified in ANSI C standard. Also now recognizes hexadecimal
   character constants beginning with \x Note that \e for escape
   doesn't belong to standard.
   Note that if case jumps out with break, then string is incremented
   one more step, but if it jumps out with continue then string is
   not incremented. (I.e. it's already positioned to the next character)
 */
	  switch (*++string)	/* Check the next character. */
	    {
/* If a string anomalously ends with a trailing (single) backslash, then
   leave it dangling there: */
	    case '\0':
	      {
		*res_ptr++ = *(string - 1);
		continue;
	      }
	    case 'a':
	      {
		*res_ptr++ = '\7';
		break;
	      }			/* BEL audible alert */
	    case 'b':
	      {
		*res_ptr++ = '\b';
		break;
	      }			/* BS  backspace */
	    case 'c':
	      {
		goto out;
	      }			/* Cut the crap. Este es la magia. */
	    case 'e':
	      {
		*res_ptr++ = '\033';
		break;
	      }			/* ESC escape */
	    case 'f':
	      {
		*res_ptr++ = '\f';
		break;
	      }			/* FF  form feed */
	    case 'n':
	      {
		*res_ptr++ = '\n';
		break;
	      }			/* NL (LF) newline */
	    case 'r':
	      {
		*res_ptr++ = '\r';
		break;
	      }			/* CR  carriage return */
	    case 't':
	      {
		*res_ptr++ = '\t';
		break;
	      }			/* HT  horizontal tab */
	    case 'v':
	      {
		*res_ptr++ = '\013';
		break;
	      }			/* VT  vertical tab */
	    case 'x':		/* There's a hexadecimal char constant \xhh */
	    case 'X':
	      {			/* Well, we should check that only max 2 digits are parsed */
		*res_ptr++ = ((UTCHAR) hextoi (&string, string+1));
		continue;
	      }
/* The following might conflict with some other usage. Commented out. */
#ifdef AK_EXTENSION_TO_UNESCAPE_STRING
	    case '^':		/* AK's special control-character escapes */
/* E.g. \^A or \^a is CTRL-A (= \1)  and \^? is 63-64 = -1 = 255 */
	      {
		UTCHAR veba;

		veba = *++string;
		if (!veba)	/* String anomalously ended, after \^ */
		  {
		    *res_ptr++ = *(string - 2);
		    *res_ptr++ = *(string - 1);
		    continue;	/* Keep \^ intact in the string. */
		  }
		veba = ((UTCHAR) toupper (veba));
		*res_ptr++ = ((UTCHAR) (veba - ((UTCHAR) 64)));
		break;
	      }
#endif
	    case '0':
	    case '1':
	    case '2':
	    case '3':
	    case '4':
	    case '5':
	    case '6':
	    case '7':
	      {			/* So it's an octal sequence like \033 : */
		unsigned int i = 3, z = 0;
		while (isoctdigit (*string) && (i--))
		  {
		    z = ((z << 3) + (*string++ - '0'));
		  }
		*res_ptr++ = ((UTCHAR) z);
		continue;	/* string already well positioned. */
	      }			/* octal digits */
	    default:		/* String like \END produces END: */
	      {
		*res_ptr++ = *string;
		break;
	      }			/* default */

	    }			/* switch */
	  string++;
	}			/* if there was a backslash */
    }				/* for loop */
out:;
  *res_ptr = '\0';		/* Terminating zero. */
  return (int) (res_ptr - org_string);	/* Return the new length. */
}


/*
   Before the call str_ptr should point to a string which points
   to the beginning quote (actually any character you wish to use
   as a quote), and then string is scanned until corresponding
   ending character is found. string referenced by str_ptr is set
   to point one character after that.
   If result is NULL, then just the length of string is returned,
   otherwise the parsed string literal is copied there.
   Note that str_ptr is modified anyway. See the next function
   get_next_token how to call this one.
 */

int
parse_string_literal (UTCHAR ** str_ptr, UTCHAR * result)
{
  unsigned int i = 0;
  unsigned int q, z;
  UTCHAR *str = *str_ptr;
  UTCHAR beg_quote = *str++;	/* And skip past it. */
  UTCHAR c;

  while (*str)
    {
      switch (*str)
	{
	case '\\':		/* An escaped character follows? */
	  {

/* New escapes added 23.AUG.1991 \a for bell, and \v for vertical tab
   as specified in ANSI C standard. Also now recognizes hexadecimal
   character constants beginning with \x Note that \e for escape
   doesn't belong to standard. (Commented out)
 */
	    switch (*++str)	/* Check the next character. */
	      {
/* If a string anomalously ends with a trailing (single) backslash, then
   leave it dangling there: */
	      case '\0':
		{
		  c = *(str - 1);
		  break;
		}
	      case 'a':
		{
		  c = '\7';
		  break;
		}		/* BEL audible alert */
	      case 'b':
		{
		  c = '\b';
		  break;
		}		/* BS  backspace */
		/*              case 'e': { c = '\033'; break; } *//* ESC escape */
	      case 'f':
		{
		  c = '\f';
		  break;
		}		/* FF  form feed */
	      case 'n':
		{
		  c = '\n';
		  break;
		}		/* NL (LF) newline */
	      case 'r':
		{
		  c = '\r';
		  break;
		}		/* CR  carriage return */
	      case 't':
		{
		  c = '\t';
		  break;
		}		/* HT  horizontal tab */
	      case 'v':
		{
		  c = '\013';
		  break;
		}		/* VT  vertical tab */
	      case 'x':	/* There's a hexadecimal char constant \xhh */
	      case 'X':
		{		/* We should check that only max 2 digits are parsed */
		  q = 2;
		  z = 0;
		  str++;
		  while (*str && isxdigit (*str) && (q--))
		    {
		      z = ((z << 4) + (*str++ - '0'));
		    }
		  c = ((UTCHAR) z);
		  str--;	/* str is incremented soon. */
		  break;
		}
	      case '0':
	      case '1':
	      case '2':
	      case '3':
	      case '4':
	      case '5':
	      case '6':
	      case '7':
		{		/* So it's an octal sequence like \033 : */
		  q = 3;
		  z = 0;
		  while (isoctdigit (*str) && (q--))
		    {
		      z = ((z << 3) + (*str++ - '0'));
		    }
		  c = ((UTCHAR) z);
		  str--;	/* str is incremented soon. */
		  break;
		}		/* octal digits */
	      default:		/* Every other character after backslash produces */
		{		/* that same character, i.e. \\ = \, \' = ', etc. */
		  c = *str;
		  break;
		}		/* default */

	      }			/* inner switch for character after a backslash */
	    if (result)
	      {
		result[i] = c;
	      }
	    i++;
	    str++;
	    break;
	  }			/* case for backslash. */
	default:
	  {
	    if (*str == beg_quote)
	      {
		/* If the next char is a quote also, then this is not yet
		   the terminating quote */
		if (*(str + 1) == beg_quote)
		  {
		    str++;	/* Skip that quote next time. */
		    goto copy_char;
		  }
		else
		  {		/* String is terminated. */
		    goto out;
		  }
	      }
	    else
	      /* Any other character. */
	      {
	      copy_char:
		if (result)
		  {
		    result[i] = *str;
		  }
		i++;
		str++;
		break;
	      }
	  }
	}			/* outer switch */
    }				/* for loop */
out:;
  if (result)
    {
      result[i] = '\0';
    }				/* Put a terminating zero. */
  if (*str)			/* The terminating quote is here. */
    {
      *str_ptr = str + 1;	/* Skip past it. */
    }
  else
    {
      /* The terminating quote is missing, we should produce an error here! */
      *str_ptr = str;		/* But in this version we are tolerant of that. */
    }
  return (i);			/* Return the length. */
}


int LIF_value = 0;		/* Contains the value of latest topmost $IF expression
				   encountered by get_next_token. */

#define INT_VAR     0
#define CHARBUF_VAR 1
#define CHARBUFLEN_VAR 2
#define CHARPTR_VAR 3
#define FILEPTR_VAR 4
#define ASSOC_ARR   5

#define INT_FLAG    6

#define SAM_MACRO   7		/* SQL API Method Macro. */
#define COMP_MACRO  8		/* $equ, $neq, $lt, $lte, $gt, or $gte */

#define macro_needs_connection(INFO) (((INFO)->type) == SAM_MACRO)

#define is_settable_macro_type(INFO) (((INFO)->type) < COMP_MACRO)


/* In this case var_addr is one of the following two,
   and more_info is either DVC_LESS, DVC_GREATER or DVC_MATCH. */
#define CM_SAME     0		/* Use ==    for comparing the result of isqlt_tcscmp */
#define CM_DIFF     1		/* or  !=    with more_info */

#ifndef DVC_LESS
/* Values returned by isqlt_tcscmp, strcasecmp functions. */
#define DVC_LESS -1
#define DVC_MATCH 0
#define DVC_GREATER 1

#endif

/* The elements of the following struct are used very haphazardously
   by the different types of macros. Later I will at least try to
   hide the ugliness behind the macro-accessors like SAM_macros below.
   I am too lazy now to write a proper union structure. Fortunately,
   the cast is powerful.
 */

struct name_var_pair
  {
    TCHAR *name;
    void *var_addr;
    int type;			/* 0 = integer, 1 = char [] buffer, 2 = char * pointer. */
    void *more_info;
    void *more2info;
  };


typedef signed short (SQL_API *SAM_GET) (void *, UWORD, PTR);
typedef signed short (SQL_API *SAM_SET) (void *, UWORD, SQLULEN);
/* For SQLGetInfo: */
typedef signed short (SQL_API *SAM2GET) (void *, UWORD, PTR, SWORD, SWORD *);


struct SAM_info			/* SAM stands for SQL API METHOD (pair) */
  {
    int sql_get_method_type;	/* Oh what a mess! 1 or 2 */
    SAM_GET get_method;
/* Should be:  union { SAM_GET get_method; SAM2GET get2method; }; */
    int sql_api_get_method_num;	/* 0 if not SQL API method. */
    SAM_SET set_method;
    int sql_api_set_method_num;	/* 0 if not SQL API method. */
    PTR *handle_ptr;		/* Usually either a connection or statement handle. */
  };

struct SAM_info SAM_CONNECT_OPTION
=
{1, ((SAM_GET) SQLGetConnectOption), SQL_API_SQLGETCONNECTOPTION,
 ((SAM_SET) SQLSetConnectOption), SQL_API_SQLSETCONNECTOPTION,
 ((PTR *) & hdbc)};

struct SAM_info SAM_STMT_OPTION
=
{1, ((SAM_GET) SQLGetStmtOption), SQL_API_SQLGETSTMTOPTION,
 ((SAM_SET) SQLSetStmtOption), SQL_API_SQLSETSTMTOPTION,
 ((PTR *) & stmt)};

struct SAM_info SAM_GET_INFO
=
{2, ((SAM_GET) ((SAM2GET) SQLGetInfo)), SQL_API_SQLGETINFO,
 ((SAM_SET) NULLFP), 0,
 ((PTR *) & hdbc)};


#define get_macro_SAM_struct(INFO)\
 ((struct SAM_info *)((INFO)->more_info))


#define SAM_get_method_type(INFO)\
 (get_macro_SAM_struct(INFO)->sql_get_method_type)

#define SAM_get_method(INFO) (get_macro_SAM_struct(INFO)->get_method)
#define SAM_get2method(INFO)\
 ((SAM2GET) (get_macro_SAM_struct(INFO)->get_method))
#define SAM_set_method(INFO) (get_macro_SAM_struct(INFO)->set_method)
#define SAM_handle(INFO)   (*(get_macro_SAM_struct(INFO)->handle_ptr))
#define SAM_option_value(INFO) ((UWORD)(int)(ptrlong)((INFO)->var_addr))


#define SAM_call_get_method(INFO, RESPTR)\
 ((SAM_get_method(INFO))\
    (SAM_handle(INFO), SAM_option_value(INFO), (RESPTR)) )

#define SAM_call_get2method(INFO, RESPTR, cbMAX, pcbINFO)\
 ((SAM_get2method(INFO))\
(SAM_handle(INFO), SAM_option_value(INFO), (RESPTR), (cbMAX), (pcbINFO)) )

/* Actually the third argument in prototype should be PTR
   (e.g. void *), not SQLULEN, and the cast should be other way. */
#define SAM_call_set_method(INFO, VALUE)\
 ((SAM_set_method(INFO))\
    (SAM_handle(INFO), SAM_option_value(INFO), ((SQLULEN)(VALUE))) )

#define SAM_choices_list(X) ((TCHAR **) (X)->more2info)	/* Stoopid... */

#define SAM_is_string_valued(X) (STRING_VALUED == SAM_choices_list(X))

#define int_flag_choices_list(X) ((TCHAR **) (X)->more_info)


#define INT_VALUED NULL

TCHAR *STRING_VALUED[] =
{NULL};
TCHAR *OFF_ON[3] =
{_T("OFF"), _T("ON"), NULL};		/* Also for AUTOCOMMIT */
TCHAR *PRESERVED_CLEARED[3] =
{_T("PRESERVED"), _T("CLEARED"), NULL};

TCHAR *PRE_AUTOCOMMIT[4] =
{_T("OFF"), _T("ON"), _T("MANUAL"), NULL};

TCHAR *RW_RO[] =			/* For SQL_ACCESS_MODE, "ACCESSMODE" */
{
  _T("RW"),				/* SQL_MODE_READ_WRITE  0UL */
  _T("RO"),				/* SQL_MODE_READ_ONLY   1UL */
  _T("RO_PERMANENTLY"),		/* SQL_MODE_READ_ONLY_PERMANENTLY 2UL (Kubl ext!) */
  NULL
};

TCHAR *LOCK_MODES[] =		/* For SQL_CONCURRENCY, "READMODE" */
{_T("UNKNOWN"),			/* 0  */
 _T("NORMAL"),			/* 1  SQL_CONCUR_READ_ONLY         old "SET RW" */
 _T("EXCLUSIVE"),			/* 2  SQL_CONCUR_LOCK                       */
 _T("SNAPSHOT"),			/* 3  SQL_CONCUR_ROWVER/_TIMESTAMP old "SET RO" */
 _T("VALUES"),			/* 4  SQL_CONCUR_VALUES                     */
 NULL
};


TCHAR *SET_TO_REST = _T("SET TO REST");

#define is_set_to_rest_macro(INFO) (((INFO)->more_info) == &SET_TO_REST)


/* This macro is useful so that we can define it later as function
   if we want to define isql_variables structures more flexibly,
   offering also possibility for run-time additions,
   e.g. if we implement user-defined associative arrays.
   (other than just default U).

   Really, we should have something more sophisticated here than
   linear search. E.g. a hash table.
 */

#define add_var_def(NAME,VAR_ADDR,TYPE,MORE_INFO)\
{ (NAME), ((void *) (VAR_ADDR)), (TYPE), ((void *)(MORE_INFO)), NULL }

#define add_buf_def(NAME,VAR_ADDR,TYPE)\
{ (NAME), ((void *) (VAR_ADDR)), (TYPE), ((void *)sizeof(VAR_ADDR)), NULL }

#define add_sam_def(NAME,OPTION,METHOD_PAIR,CHOICES)\
{ (NAME), ((void *) (OPTION)), (SAM_MACRO), ((void *)&(METHOD_PAIR)),\
  ((void *)(CHOICES)) }

#define add_empty_def(NAME,VAR_ADDR,TYPE)\
{ (NAME), ((void *) _T("")), (TYPE), ((void *)0), NULL }

/* With choices as NULL sets/shows integer values. */


struct name_var_pair isql_variables[] =
{

/* These are for HTML output. */
  add_var_def (_T("OO_SB"), (&oo_sb), CHARPTR_VAR, &SET_TO_REST),	/* <TABLE> */
  add_var_def (_T("OO_SE"), (&oo_se), CHARPTR_VAR, &SET_TO_REST),	/* </TABLE> */
  add_var_def (_T("OO_RB"), (&oo_rb), CHARPTR_VAR, &SET_TO_REST),	/* <TR> */
  add_var_def (_T("OO_RE"), (&oo_re), CHARPTR_VAR, &SET_TO_REST),	/* </TR> */
  add_var_def (_T("OO_HB"), (&oo_hb), CHARPTR_VAR, &SET_TO_REST),	/* <TH> */
  add_var_def (_T("OO_HE"), (&oo_he), CHARPTR_VAR, &SET_TO_REST),	/* </TH> */
  add_var_def (_T("OO_DB"), (&oo_db), CHARPTR_VAR, &SET_TO_REST),	/* <TD> */
  add_var_def (_T("OO_DE"), (&oo_de), CHARPTR_VAR, &SET_TO_REST),	/* </TD> */
  add_var_def (_T("OO_OB"), (&oo_ob), CHARPTR_VAR, &SET_TO_REST),	/* <PRE> */
  add_var_def (_T("OO_OE"), (&oo_oe), CHARPTR_VAR, &SET_TO_REST),	/* </PRE> */
  add_var_def (_T("OO_ESC"), (&oo_esc), INT_FLAG, OFF_ON),	/* HTML-escaping on? */

  add_var_def (_T("RETVAL"), (retvalbuf), CHARBUFLEN_VAR, (&cbRetVal)),

  add_var_def (_T("RETVALLEN"), (&cbRetVal), INT_VAR, INT_VALUED),
  add_var_def (_T("ROWCNT"), (&N_rows), INT_VAR, INT_VALUED),
  add_var_def (_T("COLCNT"), (&n_out_cols_long), INT_VAR, INT_VALUED),
  add_var_def (_T("ARGC"), (&G_argc), INT_VAR, INT_VALUED),
  add_var_def (_T("I"), (&G_argc_i), INT_VAR, INT_VALUED),
  add_var_def (_T("LIF"), (&LIF_value), INT_VAR, INT_VALUED),

  add_empty_def (_T("INPUTLINE"), (input), CHARBUF_VAR),
  add_buf_def (_T("STATE"), (SQL_error_state), CHARBUF_VAR),
  add_buf_def (_T("SQLSTATE"), (SQL_error_state), CHARBUF_VAR),	/* Synonym. */
  add_buf_def (_T("MESSAGE"), (SQL_error_message), CHARBUF_VAR),
  add_buf_def (_T("DBMSNAME"), (info_dbms), CHARBUF_VAR),	/* Available after */
  add_buf_def (_T("DRIVER"), (info_driver), CHARBUF_VAR),	/* the connection */

  add_var_def (_T("QUERY_STRING"), (&web_query_string), CHARPTR_VAR, NULL),

  add_var_def (_T("FORM_FILENAME"), (&form_filename), CHARPTR_VAR, NULL),
  add_var_def (_T("FORM_FILEFIELDNAME"), (&form_filefieldname), CHARPTR_VAR, NULL),
  add_var_def (_T("FORM_LAST_CONTENT_TYPE"), (&form_last_content_type), CHARPTR_VAR, NULL),
  add_var_def (_T("FORM_LAST_ENCODING"), (&form_last_encoding), CHARPTR_VAR, NULL),
  add_var_def (_T("FORM_BOUNDARY"), (&form_boundary), CHARPTR_VAR, NULL),

  add_var_def (_T("LWE"), (&isql_echo_lwe), CHARPTR_VAR, NULL),	/* Shhh... */

  add_var_def (_T("U"), (&U_vars_space), ASSOC_ARR, NULL),


  add_var_def (_T("CONTENT_TYPE"), (&web_content_type), CHARPTR_VAR, NULL),
  add_var_def (_T("DSN"), (&datasource), CHARPTR_VAR, NULL),
  add_var_def (_T("UID"), (&username), CHARPTR_VAR, NULL),
  add_var_def (_T("PWD"), (&password), CHARPTR_VAR, NULL),

  add_var_def (_T("ERRORS"), (&error_stream), FILEPTR_VAR, NULL),
  add_var_def (_T("PROMPT"), (&print_prompt), CHARPTR_VAR, NULL),
  add_var_def (_T("EMPTY"), (&empty_string), CHARPTR_VAR, NULL),
  add_var_def (_T("VERSION"), (&isql_version), CHARPTR_VAR, NULL),
  add_var_def (_T("FORM_ACTION"), (&form_action), CHARPTR_VAR, NULL),

  add_var_def (_T("BLOBS"), (&print_blobs_flag), INT_FLAG, OFF_ON),
  add_var_def (_T("FOREACH_ERR_BREAK"), (&foreach_err_break), INT_FLAG, OFF_ON),
  add_var_def (_T("ECHO"), (&echo_mode), INT_FLAG, OFF_ON),
  add_var_def (_T("EXPLAIN"), (&explain_mode), INT_FLAG, OFF_ON),
  add_var_def (_T("SPARQL_TRANSLATE"), (&sparql_translate_mode), INT_FLAG, OFF_ON),
  add_var_def (_T("VERT_ROW_OUTPUT"), (&vert_row_out_mode), INT_FLAG, OFF_ON),
  add_var_def (_T("CSV"), (&csv_mode), INT_FLAG, OFF_ON),
  add_var_def (_T("CSV_RFC4180"), (&csv_rfc4180_mode), INT_FLAG, OFF_ON),
  add_var_def (_T("PROFILE"), (&profile_mode), INT_FLAG, OFF_ON),
  add_var_def (_T("CSV_FIELD_SEPARATOR"), (&csv_field_separator), CHARPTR_VAR, NULL),
  add_var_def (_T("CSV_RFC4180_FIELD_SEPARATOR"), (&csv_rfc4180_field_separator), CHARPTR_VAR, NULL),
  add_var_def (_T("CSV_ROW_SEPARATOR"), (&csv_row_separator), CHARPTR_VAR, NULL),
  add_var_def (_T("CSV_RFC4180_ROW_SEPARATOR"), (&csv_rfc4180_row_separator), CHARPTR_VAR, NULL),
  add_var_def (_T("JSON"), (&json_mode), INT_FLAG, OFF_ON),
  add_var_def (_T("HIDDEN_CRS"), (&clear_hidden_crs_flag), INT_FLAG, PRESERVED_CLEARED),
  add_var_def (_T("BINARY_OUTPUT"), (&flag_binary_output), INT_FLAG, OFF_ON),
  add_var_def (_T("BANNER"), (&print_banner_flag), INT_FLAG, OFF_ON),
  add_var_def (_T("TYPES"), (&print_types_also), INT_FLAG, OFF_ON),
  add_var_def (_T("VERBOSE"), (&verbose_mode), INT_FLAG, OFF_ON),
  add_var_def (_T("TRAILING_NEWLINES"), (&flag_newlines_at_eor), INT_FLAG, INT_VALUED),
  add_var_def (_T("TIMES2STRINGS"), (&times_to_strings_flag), INT_FLAG, OFF_ON),
  add_var_def (_T("TS_ODBC"), (&times_conform_to_odbc_flag), INT_FLAG, OFF_ON),
  add_var_def (_T("DEADLOCK_RETRIES"), (&perm_deadlock_retries), INT_VAR, INT_VALUED),
  add_var_def (_T("MACRO_SUBSTITUTION"), (&flag_macro_substitution), INT_FLAG, OFF_ON),
  add_var_def (_T("IGNORE_PARAMS"), (&flag_ignore_params), INT_FLAG, OFF_ON),
  add_var_def (_T("BIND_RETURN_VALUES"), (&flag_bind_return_values), INT_FLAG, OFF_ON),
  add_var_def (_T("TRIM_STRING_RESULT"), (&trim_string_result), INT_FLAG, OFF_ON),
  add_var_def (_T("HEAD_ALREADY_PRINTED"), (&flag_head_already_printed), INT_FLAG, OFF_ON),
  add_var_def (_T("VIRTEXT"), (&virtext), INT_FLAG, OFF_ON),
  add_var_def (_T("LOCALE"), (&isql_locale), CHARPTR_VAR, NULL),

  add_var_def (_T("AUTOCOMMIT"), (&commit_mode), INT_FLAG, PRE_AUTOCOMMIT),	/* ON */
  add_sam_def (_T("ACCESSMODE"), (SQL_ACCESS_MODE), SAM_CONNECT_OPTION, RW_RO),
  add_sam_def (_T("READMODE"), (SQL_CONCURRENCY), SAM_STMT_OPTION, LOCK_MODES),
  add_sam_def (_T("TIMEOUT"), (SQL_QUERY_TIMEOUT), SAM_STMT_OPTION, INT_VALUED),
  add_sam_def (_T("MAXROWS"), (SQL_MAX_ROWS), SAM_STMT_OPTION, INT_VALUED),
/* The following are mainly for debugging purposes. */
  add_sam_def (_T("CURRENT_QUALIFIER"), (SQL_CURRENT_QUALIFIER), SAM_CONNECT_OPTION, STRING_VALUED),
  add_sam_def (_T("INFO_DATABASE_NAME"), (SQL_DATABASE_NAME), SAM_GET_INFO, STRING_VALUED),
  add_sam_def (_T("INFO_USER_NAME"), (SQL_USER_NAME), SAM_GET_INFO, STRING_VALUED),
  add_sam_def (_T("INFO_GETDATA_EXTENSIONS"), (SQL_GETDATA_EXTENSIONS), SAM_GET_INFO, INT_VALUED),

/* After this point only things that cannot be set. If you want to
   protect some variable from being set by user, move its definition
   after these ones. (Not really implemented yet!) */
  add_var_def (_T("LT"), (CM_SAME), COMP_MACRO, DVC_LESS),
  add_var_def (_T("GTE"), (CM_DIFF), COMP_MACRO, DVC_LESS),
  add_var_def (_T("GT"), (CM_SAME), COMP_MACRO, DVC_GREATER),
  add_var_def (_T("LTE"), (CM_DIFF), COMP_MACRO, DVC_GREATER),
  add_var_def (_T("EQU"), (CM_SAME), COMP_MACRO, DVC_MATCH),
  add_var_def (_T("NEQ"), (CM_DIFF), COMP_MACRO, DVC_MATCH),
  add_var_def (_T("COMMAND_TEXT_ON_ERROR"), (&command_text_on_error), INT_FLAG, OFF_ON),

  {NULL}			/* End marker. */
};


/* Case insensitive comparison! */
#define is_macro_name(name1,name2) (0 == strcasecmp((name1),(name2)))


struct name_var_pair *
find_var_info (TCHAR *varname)
{
  struct name_var_pair *structptr = isql_variables;

  while (structptr->name)
    {
      if (is_macro_name (varname, structptr->name))
	{
	  return (structptr);
	}
      structptr++;
    }

  return (NULL);		/* Not found. */
}



#define isopening(C) \
 (NULL != isqlt_tcschr (_T("{[<("), C))
/* ( ((C) == '{') || ((C) == '[') || ((C) == '<') || ((C) == '(') ) */


#define isclosing(C) \
 (NULL != isqlt_tcschr (_T("}]>)"), C))
/* ( ((C) == '}') || ((C) == ']') || ((C) == '>') || ((C) == ')') )*/

/*
   Now tokens are cut for any opening or closing bracket like
   character (see macros above) as well as for commas and equal signs.
   And dollars too!

   In any case, at least tokens should not be cut for a dot (.), for
   a dash (-), for a slash (/), and for a backslash (\),
   so that we don't need to enclose tablenames like qual.own.tablename,
   loaded filenames inside single- or doublequotes, nor options
   like -r for load command.

   10. March, added ampersand (&) and question mark (?) to cut token
   characters, so that we may have forms like VALUE="$YYYYMMDD&#46;log"
   where &#46; stands for a period (.), and we get "19980310&#46;log"
   and eventually "19980310.log"
   And stuff like <A HREF="$FORM_ACTION?S_EXEC=something">

 */

#define cut_token_char(C) \
 (NULL != isqlt_tcschr(_T("$&?,="), C) || isopening(C) || isclosing(C))
/*( ('$' == (C)) || ('&' == (C)) || ('?' == (C)) || (',' == (C)) || ('=' == (C)) || (isopening(C)) || (isclosing(C)) )*/


#define word_ends_at(PTR)\
 ( !(*(PTR)) ||\
 (!isalnum(*(PTR)) && ('_' != (*(PTR))) && ((*((UTCHAR *)PTR)) < 192)))

#define word_follows(TEXT,WORD)\
 (!strncasecmp((TEXT),(WORD),(sizeof(WORD)-1))\
  && word_ends_at((TEXT+sizeof(WORD)-1)))

UTCHAR *find_closing_point (UTCHAR * start_point, UTCHAR opening_char);

#define TOKEN_PREMATURE_END NULL
TCHAR *TOKEN_NULL = _T("NULL");


/* If ind is valid index to alt_ptr returns the corresponding element,
   otherwise returns "UNKNOWN:ind" string copied to result argument.
 */
TCHAR *
find_choice_for_int (TCHAR *name, int ind, TCHAR **alt_ptr,
		     TCHAR *result, int maxsize)
{
  int i;
  TCHAR tmp1buf[101];

  for (i = 0; alt_ptr[i] && (i < ind); i++)
    {
    }
  if (alt_ptr[i])
    {
      return (alt_ptr[i]);
    }
  else
    {
      isqlt_stprintf (tmp1buf, _T("UNKNOWN:%d"), ind);
      isqlt_tcsncpy (result, tmp1buf, maxsize);
      return (result);
    }
}


/* If arg found from alt_ptr returns 0-based index to it,
   otherwise gives a warning message and returns -1
 */
int
find_int_for_choice (TCHAR *name, TCHAR *arg, TCHAR **alt_ptr)
{
  int i;

  for (i = 0; alt_ptr[i]; i++)
    {
      if (!strcasecmp (arg, alt_ptr[i]))
	{
	  return (i);
	}
    }
  /* Else, we didn't find thing we were looking for. */
  {
    isql_fprintf (error_stream,
		  _T("%") PCT_S _T(": macro %") PCT_S _T(" requires one of the following options:"),
		  progname, name);
    for (i = 0; alt_ptr[i]; i++)
      {
	isql_fprintf (error_stream, _T(" %") PCT_S _T(""), alt_ptr[i]);
	if (NULL == alt_ptr[i + 1])
	  {
	    break;
	  }
	if (NULL == alt_ptr[i + 2])
	  {
	    isql_fprintf (error_stream, _T(" or"));
	  }
	else
	  {
	    isql_fprintf (error_stream, _T(","));
	  }
      }
    isql_fprintf (error_stream, _T(". NOT: \"%") PCT_S _T("\". at line %ld of %") PCT_S _T("\n"),
		  arg, latest_statement_begins_at (), current_loadexpr ());
    fflush (error_stream);
    return (-1);
  }
}

TCHAR *get_arg_after_braces (TCHAR *arg, TCHAR *tmp2buf, int bufsize, int get_all_raw);

int
set_value_to_macro (struct name_var_pair *macro_info,
		    TCHAR *arg, TCHAR *argptr, int ending_point)
{
  TCHAR *openptr, *closeptr;
  TCHAR *me = _T("set_value_to_macro");

  TCHAR *macro_name = macro_info->name;

  switch (macro_info->type)
    {
    case SAM_MACRO:
      {
	int i;
	int rc;
	SQLULEN j;
	TCHAR **alt_ptr;

	if (IS_NULLFP (SAM_set_method (macro_info)))
	  {
	    isql_fprintf (error_stream,
			  _T("%") PCT_S _T(": macro %") PCT_S _T(" is not settable!"),
			  progname, macro_name);
	    isql_fprintf (error_stream, _T(" At line %ld of %") PCT_S _T("\n"),
			latest_statement_begins_at (), current_loadexpr ());
	    fflush (error_stream);
	    return (0);
	  }

	if (NOT fully_connected)
	  {
	    isql_fprintf (error_stream,
	    _T("%") PCT_S _T(": macro %") PCT_S _T(" cannot be set before full connection to database."),
			  progname, macro_name);
	    isql_fprintf (error_stream, _T(" At line %ld of %") PCT_S _T("\n"),
			latest_statement_begins_at (), current_loadexpr ());
	    fflush (error_stream);
	    return (0);
	  }


	if (SAM_is_string_valued (macro_info))	/* Not really tested. */
	  {
	    rc = SAM_call_set_method (macro_info, arg);
	  }
	else
	  {
	    alt_ptr = SAM_choices_list (macro_info);

	    if (NULL != alt_ptr)
	      {
		i = find_int_for_choice (macro_name, arg, alt_ptr);
		if (i == -1)
		  {
		    return (0);
		  }
	      }
	    else
	      /* Else it's SAM_macro that wants an ordinary integer */
	      {			/* e.g. TIMEOUT or MAXROWS */
		if (NOT arg || empty_stringp (arg))
		  {
		    return (0);
		  }
		i = isqlt_tstoi (arg);
	      }
	    j = i;
	    rc = SAM_call_set_method (macro_info, j);
	  }
	if (SQL_SUCCESS != rc)
	  {
	    print_error (henv, hdbc, stmt, rc);
	    if (SQL_SUCCESS_WITH_INFO != rc)
	      return (0);
	  }
	return (1);
      }
    case INT_FLAG:
      {
	TCHAR **alt_ptr = int_flag_choices_list (macro_info);
	if (NULL != alt_ptr)
	  {
	    int i = find_int_for_choice (macro_name, arg, alt_ptr);
	    if (i != -1)
	      {
		(*((int *) macro_info->var_addr)) = i;
		if (!strcasecmp (macro_info->name, _T("AUTOCOMMIT")))
		  {
		    if (!strcasecmp (arg, _T("OFF")))
		      {
			commit_mode = 0;
			SQLSetConnectOption (hdbc, SQL_AUTOCOMMIT, 0);
		      }
		    else if (!strcasecmp (arg, _T("ON")))
		      {
			commit_mode = 1;
			SQLSetConnectOption (hdbc, SQL_AUTOCOMMIT, 1);
		      }
		    else if (!strcasecmp (arg, _T("MANUAL")))
		      {
			commit_mode = 2;
		      }
		  }
		return (1);
	      }
	    /* Else, we didn't find thing we were looking for. */
	      {
		return (0);
	      }
	  }
/* If more_info contains NULL, then we fall to the next... */
      }
    case INT_VAR:
      {
	if (arg && NOT empty_stringp (arg))
	  {
	    *((int *) macro_info->var_addr) = isqlt_tstoi (arg);
	    return (1);
	  }
	else
	  return (0);
      }
    case CHARBUF_VAR:
      {
	int buflen = ((int) (ptrlong) macro_info->more_info);
	isqlt_tcsncpy (((TCHAR *) macro_info->var_addr), arg, (buflen - 1));
	return (1);
      }
    case CHARBUFLEN_VAR:
      {				/* Very Ad Hoc: Just copy max. countbyte characters to
				   character buffer, on the assumption that at least that
				   many bytes fit there. (Of course we should have a separate
				   max_size count also saved in macro_info). */
	int buflen = *((int *) macro_info->more_info);
	if (buflen > 0)		/* E.g. not SQL_NULL_DATA */
	  {
	    isqlt_tcsncpy (((TCHAR *) macro_info->var_addr), arg, (buflen - 1));
	  }
	return (1);
      }
    case CHARPTR_VAR:		/* One indirection more than in CHARBUF_VAR. */
      {
	if (is_set_to_rest_macro (macro_info))
	  {
	    *((TCHAR **) macro_info->var_addr) = chestrdup (argptr, me);
	  }
	else if (arg && NOT empty_stringp (arg))
	  {
	    *((TCHAR **) macro_info->var_addr) = chestrdup (arg, me);
	  }
	return (1);
      }
    case FILEPTR_VAR:
      {
	FILE *ptr;
	if (!strcasecmp (arg, _T("STDIN")))
	  {
	    ptr = stdin;
	  }
	else if (!strcasecmp (arg, _T("STDOUT")))
	  {
	    ptr = stdout;
	  }
	else if (!strcasecmp (arg, _T("STDERR")))
	  {
	    ptr = stderr;
	  }
	else
	  {
	    isql_fprintf (error_stream,
			  _T("%") PCT_S _T(": %") PCT_S _T(": STDIN, STDOUT or STDERR required for ")
			  _T(" macro \"%") PCT_S _T("\" at line %ld of %") PCT_S _T("\n"),
			  progname, me, macro_name,
			latest_statement_begins_at (), current_loadexpr ());
	    fflush (error_stream);
	    return (0);		/* Really, we should signal this! */
	  }

	(*((FILE **) macro_info->var_addr)) = ptr;
	return (1);
      }
    case ASSOC_ARR:		/* Associative array ref, e.g. $U{something} */
      {
	if (('{' == *(openptr = skip_blankos (argptr))) &&
	    (closeptr = SCP (find_closing_point (UCP (openptr),
						 ((UTCHAR) * openptr))))
	  )
	  {
	    TCHAR tmp2buf[TMPBUF_SIZE + 2], tmp3buf[TMPBUF_SIZE + 2];
	    TCHAR *indstr;

	    closeptr++;		/* Skip past the closing brace. */

	    openptr++;		/* Past the opening brace. */
	    if (NO (indstr = get_next_token (&openptr, tmp2buf,
					     TMPBUF_SIZE,
					     NULL, 0)))
	      {
		return (0);
	      }			/* Shouldn't really happen! */


	    arg = get_arg_after_braces (closeptr, tmp3buf, TMPBUF_SIZE, ending_point);
	    set_to_stringpair_list (indstr, arg,
				((stringpair_list *) macro_info->var_addr));

	    return (1);
	  }
	else
	  /* No opening brace or no matching closing brace! */
	  {
	    isql_fprintf (error_stream,
		       _T("%") PCT_S _T(": %") PCT_S _T(": Missing or unbalanced braces/brackets after")
			  _T(" macro \"%") PCT_S _T("\" at line %ld of %") PCT_S _T("\n"),
			  progname, me, macro_name,
			latest_statement_begins_at (), current_loadexpr ());
	    fflush (error_stream);
	    return (0);		/* Really, we should signal this! */
	  }
      }				/* case ASSOC_ARR */
    default:			/* Not handled. */
      {
	return (0);
      }

    }				/* switch(macro_info->type) */
}

static int
all_digits (TCHAR *string)
{
  while (*string)
    {
      if ((unsigned)(*string) > 127 || !isqlt_istdigit (*string))
	return 0;
      else
	string++;
    }
  return 1;
}

TCHAR *
convert_macro_to_text (struct name_var_pair *macro_info,
		       TCHAR **str_ptr, TCHAR *result, int maxsize,
		       TCHAR *input_buffer, int input_size,
		       int used_for_show_command)
{
  TCHAR *openptr, *closeptr;
  TCHAR *me = _T("convert_macro_to_text");
  TCHAR *macro_name = macro_info->name;
  TCHAR tmpnumbuf[181];


  switch (macro_info->type)
    {
/* The following is a real mess, and requires cleaning! I'm sorry. */
    case SAM_MACRO:
      {
	int rc, get_method_type;
	SQLULEN intres;
	int is_strres = 0;
	TCHAR *not_set_string = _T("NOT SET");
	SWORD res_len;
	TCHAR **alt_ptr = SAM_choices_list (macro_info);

	if (IS_NULLFP (SAM_get_method (macro_info)))
	  {
	    isqlt_stprintf (tmpnumbuf, _T("macro %") PCT_S _T(" is not gettable!"),
		     macro_name);
	    isqlt_tcsncpy (result, tmpnumbuf, maxsize);
	    return (result);
	  }

	if (NOT fully_connected)
	  {
	    isqlt_stprintf (tmpnumbuf,
		     _T("NOT CONNECTED: %") PCT_S _T(" value unknown"), macro_name);
	    isqlt_tcsncpy (result, tmpnumbuf, maxsize);
	    return (result);
	  }

	get_method_type = SAM_get_method_type (macro_info);
	if (2 == get_method_type)	/* For SQLGetInfo */
	  {
	    if (SAM_is_string_valued (macro_info))
	      {			/* with a string result */
		rc = SAM_call_get2method (macro_info, result,
					  ((SWORD) maxsize),
		/* pcbInfoValue (the last arg) should be SWORD *   */ &res_len);
		is_strres = 1;
	      }
	    else
	      {			/* with an integer result */
		rc = SAM_call_get2method (macro_info, &intres, ((SWORD) 4),
		/* pcbInfoValue (the last arg) should be SWORD *   */ &res_len);
	      }
	  }
	else
	  /* method type is 1 for SQLGetStmt/ConnectOption */
	  {
	    if (SAM_is_string_valued (macro_info))
	      {			/* Last was: &strres */
		isqlt_tcsncpy (result, not_set_string, maxsize);
		rc = SAM_call_get_method (macro_info, result);
		is_strres = 1;
	      }
	    else
	      /* It's with an integer result */
	      {
		rc = SAM_call_get_method (macro_info, &intres);
	      }
	  }

	if (SQL_SUCCESS != rc)
	  {
	    print_error (henv, hdbc, stmt, rc);
	    if (SQL_SUCCESS_WITH_INFO != rc)
	      return (NULL);
	  }

/* Not needed:  if(2 == get_method_type) { return(result); } */
	if (is_strres)
	  {
	    return (result);	/* (strres); */
	  }

	if (NULL != alt_ptr)
	  {
	    return (find_choice_for_int (macro_name, ((int) intres),
					 alt_ptr, result, maxsize));
	  }
	else
	  /* The result is plain integer. */
	  {
	    isqlt_stprintf (tmpnumbuf, _T("%d"), (int) intres);
	    isqlt_tcsncpy (result, tmpnumbuf, maxsize);
	    return (result);
	  }
      }
    case INT_FLAG:
      {
	TCHAR **alt_ptr = int_flag_choices_list (macro_info);

	if (NULL != alt_ptr)
	  {
	    return (find_choice_for_int (macro_name,
				 (*((int *) macro_info->var_addr)), alt_ptr,
					 result, maxsize));
	  }
	/* If more_info contains NULL, then we fall to the next... */
      }
    case INT_VAR:
      {
	isqlt_stprintf (tmpnumbuf, _T("%d"), *((int *) macro_info->var_addr));
	isqlt_tcsncpy (result, tmpnumbuf, maxsize);
	return (result);
      }
    case CHARBUF_VAR:
      {
	return ((TCHAR *) macro_info->var_addr);
      }
    case CHARBUFLEN_VAR:	/* E.g. RETVAL */
      {				/* Like above, but more_info contains the address of
				   corresponding length byte, which should be checked first. */
	if (SQL_NULL_DATA == (*((int *) macro_info->more_info)))
	  {
	    result = NULL;
	  }
	else
	  {
	    result = ((TCHAR *) macro_info->var_addr);
	  }
	return (result);	/* (result ? result : "NULL"); */
      }

    case CHARPTR_VAR:		/* One indirection more than in CHARBUF_VAR. */
      {
	result = (*((TCHAR **) macro_info->var_addr));
	return (result);	/* (result ? result : "NULL"); */
      }
    case FILEPTR_VAR:
      {
	FILE *ptr = (*((FILE **) macro_info->var_addr));
	if (stdin == ptr)
	  {
	    return (_T("STDIN"));
	  }
	if (stdout == ptr)
	  {
	    return (_T("STDOUT"));
	  }
	if (stderr == ptr)
	  {
	    return (_T("STDERR"));
	  }
	else
	  {
	    TCHAR tmp1buf[TMPBUF_SIZE + 2];
	    isqlt_stprintf (tmp1buf, _T("FILEPTR:%08x"),
		     *((int *) macro_info->var_addr));
	    isqlt_tcsncpy (result, tmp1buf, maxsize);
	    return (result);
	  }
      }
    case ASSOC_ARR:		/* Associative array ref, e.g. $U{something} */
      {
	int openf = 0;
	if (('{' == *(openptr = skip_blankos (*str_ptr)))
	    && (openf = 1)
	    &&
	    (closeptr = SCP (find_closing_point (UCP (openptr),
						 ((UTCHAR) * openptr))))
	  )
	  {
	    TCHAR tmp3buf[TMPBUF_SIZE + 2];
	    TCHAR *indstr;

	    *str_ptr = closeptr + 1;	/* Skip past closing brace. */

	    openptr++;		/* Past the opening brace. */
	    if (NO (indstr = get_next_token (&openptr, tmp3buf,
					     TMPBUF_SIZE,
					     input_buffer, input_size)))
	      {
		return (TOKEN_PREMATURE_END);
	      }			/* Shouldn't really happen! */


	    result = assoc_value_from_stringpair_list (indstr,
				((stringpair_list *) macro_info->var_addr));

	    return (result);	/* (result ? result : _T("NULL")); */
	  }
	else
	  /* No opening brace or no matching closing brace! */
	  {			/* The following is kludgous, I admit.
				   If user has given command like show U
				   (without braces) then dump out all of its
				   contents. Good for debugging. */
	    if (used_for_show_command && (NOT openf))
	      {
		int len = dump_stringpair_list (macro_name,
				((stringpair_list *) macro_info->var_addr));
		fflush (stdout);
		isqlt_stprintf (tmpnumbuf, _T("%d"), len);
		isqlt_tcsncpy (result, tmpnumbuf, maxsize);
		return (result);	/* Returns the length of assoclist */
	      }
	    else
	      {
		isql_fprintf (error_stream,
		       _T("%") PCT_S _T(": %") PCT_S _T(": Missing or unbalanced braces/brackets after")
			      _T(" macro \"%") PCT_S _T("\" at line %ld of %") PCT_S _T("\n"),
			      progname, me, macro_name,
			latest_statement_begins_at (), current_loadexpr ());
		fflush (error_stream);
		return (TOKEN_PREMATURE_END);	/* Really, we should signal this! */
	      }
	  }
      }				/* case ASSOC_ARR */
    case COMP_MACRO:		/* $EQU, $NEW, $LT, $LTE, $GT or $GTE */
      {
	int op = ((int) (ptrlong) macro_info->var_addr);
	int compared_to = ((int) (ptrlong) macro_info->more_info);
	int code;		/* -1=DVC_LESS, 0=DVC_MATCH, 1=DVC_GREATER */
	TCHAR *left, *right;
	TCHAR hmp1buf[TMPBUF_SIZE + 2], hmp2buf[TMPBUF_SIZE + 2];

	/* First get the things to be compared. */
	if (NO (left = get_next_token (str_ptr, hmp1buf, TMPBUF_SIZE,
				       input_buffer, input_size)))
	  {
	    return (TOKEN_PREMATURE_END);
	  }
	if (NO (right = get_next_token (str_ptr, hmp2buf, TMPBUF_SIZE,
					input_buffer, input_size)))
	  {
	    return (TOKEN_PREMATURE_END);
	  }

 	if (all_digits (right) && isqlt_tcschr (left, '.'))
	  {
	    TCHAR *dot = isqlt_tcschr (left, '.'), *tmp;
	    int all_zero = 1;
	    tmp = dot;
	    while (tmp && *tmp)
	      {
		tmp++;
		if (*tmp && *tmp != '0')
		  {
		    all_zero = 0;
		    break;
		  }
	      }
	    if (all_zero)
	      *dot = 0;
	    code = (isqlt_tcscmp (left, right));
	    code = code < 0 ? -1 : (code > 0) ? 1 : 0;
	    *dot = '.';
	  }
	else if (trim_string_result)
	  code = (isqlt_tcsncmp (left, right, isqlt_tcslen (right)));
	else
	  code = (isqlt_tcscmp (left, right));
	code = code > 0 ? 1 : ((code < 0) ? -1 : 0);
	code = ((op == CM_SAME) ? (code == compared_to)
	/* else it's CM_DIFF */ : (code != compared_to));

	if (code)
	  {
	    isqlt_tcsncpy (result, _T("1"), maxsize);
	  }
	else
	  {
	    isqlt_tcsncpy (result, _T(""), maxsize);
	  }

	return (result);
      }				/* case COMP_MACRO */

    }				/* switch(macro_info->type) */
  return NULL;
}


int
dump_all_settable_variables (TCHAR *varname_to_match)
{
  struct name_var_pair *structptr = isql_variables;
  TCHAR *argptr, *value;
  TCHAR *dumpty_string = _T("");
  TCHAR tmp1buf[TMPBUF_SIZE + 2];

  while (structptr->name)
    {
      if (is_settable_macro_type (structptr))
	/* && some_matching_function (varname_to_match,structptr->name) */
	{
	  argptr = dumpty_string;
	  value = convert_macro_to_text (structptr, &argptr,
					 tmp1buf, TMPBUF_SIZE,
					 NULL, 0, 1);
	  if (NULL == value)
	      value = _T("NULL");
	  else if (!isqlt_tcscmp (value, _T("\r\n")))
	      value = _T("\\r\\n");
	  else if (!isqlt_tcscmp (value, _T("\r")))
	      value = _T("\\r");
	  else if (!isqlt_tcscmp (value, _T("\n")))
	      value = _T("\\n");
	  else if (!isqlt_tcscmp (value, _T("\t")))
	      value = _T("\\t");


	  if (in_HTML_mode () && oo_esc)
	    {
	      isql_fputs (structptr->name, stdout);
	      isql_fputs (_T(" is "), stdout);
	      html_print_escaped (value, stdout);
	      isql_fputs (_T("\n"), stdout);
	    }
	  else
	    {
	      isql_printf (_T("%-40") PCT_S _T(" is %") PCT_S _T("\n"), structptr->name, value);
	    }
	}
      structptr++;
    }

  fflush (stdout);
  return (1);
}


/* If fourth argument input_buffer is given as NULL, then doesn't
   try to read next line(s) in if the current line is finished.
   (That feature has been never used, so neither it has been tested,
   in current usage the input_buffer is always given as NULL.) */
TCHAR *
get_next_token (TCHAR **str_ptr, TCHAR *result, int maxsize,
		TCHAR *input_buffer, int input_size)
{
  TCHAR *text;
  TCHAR *resptr, *openptr, *closeptr;
  int len, indnum = -1;
  int is_argv = 0;		/* Flag for either LAST or ARGV [index] form */
  TCHAR tmpnumbuf[81];

  text = *str_ptr;
de_nuevo:
  text = skip_blankos (text);
  if (*text)
    {				/* Something following on this line? */
      if (('\'' == *text) || ('"' == *text))	/* Quoted String Literal? */
	{
	  *str_ptr = text;	/* Save the original starting location. */
	  /* Because text will be screwed in this first call: */
	  len = parse_string_literal ((UTCHAR **) & text, NULL);		/* First length */
	  if (len > maxsize)
	    {
	      return (NULL);
	    }
	  parse_string_literal ((UTCHAR **) str_ptr, (UTCHAR *) result);	/* Then the real stuff. */
	  return (result);
	}
      if ((*text == '-') && (*(text + 1) == '-'))	/* If a comment, skip it */
	{
	  while (*text && (*text != '\n'))
	    {
	      text++;
	    }
	  if (*text)
	    {
	      text++;
	    }			/* Skip past the newline. */
	  goto de_nuevo;
	}

      /* Is it one of the special groovy macro-words? */
      if (('$' == *text))	/* && (*(text+1)) && isalnum(*(text+1))) */
	{
	  struct name_var_pair *macro_info;
	  TCHAR *macro_name;
	  TCHAR tmp1buf[TMPBUF_SIZE + 2];

	  *str_ptr = (text + 1);	/* Skip past the first dollar. */
	  if ('$' == text[1])
	    {
	      str_ptr[0] = text + 2;
	      isqlt_tcscpy (result, _T("$"));
	      return result;
	    }

	  /* First get the macro word to be tested. */
	  if (NO (macro_name = get_next_token (str_ptr, tmp1buf, TMPBUF_SIZE,
					       input_buffer, input_size)))
	    {
	      macro_name = _T("NULL");
	    }

	  if (word_follows (text + 1, _T("LOADEXPR")))
	    {
	      *str_ptr = (text + sizeof (_T("LOADEXPR")));
	      return (current_loadexpr ());
	    }

	  else if (word_follows (text + 1, _T("LINENO")))
	    {
	      *str_ptr = (text + sizeof (_T("LINENO")));

	      isqlt_stprintf (tmpnumbuf, _T("%d"), (int) current_linecount ());
	      isqlt_tcsncpy (result, tmpnumbuf, maxsize);

	      return (result);
	    }

	  else if (word_follows (text + 1, _T("YYYYMMDD")))
	    {
	      struct tm *tm;
	      time_t time_now = time (NULL);
	      TCHAR temp[100];

	      *str_ptr = (text + sizeof (_T("YYYYMMDD")));

	      tm = localtime (&time_now);

	      isqlt_stprintf (temp, _T("%04d%02d%02d"),
		       tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);

	      isqlt_tcsncpy (result, temp, maxsize);
	      return (result);
	    }

	  else if (word_follows (text + 1, _T("HHMMSS")))
	    {
	      struct tm *tm;
	      time_t time_now = time (NULL);
	      TCHAR temp[100];

	      *str_ptr = (text + sizeof (_T("HHMMSS")));

	      tm = localtime (&time_now);

	      isqlt_stprintf (temp, _T("%02d%02d%02d"),
		       tm->tm_hour, tm->tm_min, tm->tm_sec);

	      isqlt_tcsncpy (result, temp, maxsize);
	      return (result);
	    }

	  else if (word_follows (text + 1, _T("DATASOURCES")))
	    {
	      *str_ptr = (text + sizeof (_T("DATASOURCES")));
	      return (get_list_of_datasources (0, result, maxsize));
	    }
	  else if (word_follows (text + 1, DATASOURCE_TOKEN))	/* INPUT_DATASOURCE */
	    {
	      *str_ptr = (text + sizeof (DATASOURCE_TOKEN));
	      return (get_list_of_datasources (1, result, maxsize));
	    }

	  else if (word_follows (text + 1, _T("+"))
	        || word_follows (text + 1, _T("-"))
	        || word_follows (text + 1, _T("*"))
	        || word_follows (text + 1, _T("/")))
	    {			/* Add/substract/multiply/divide the next two elements. */
	      long int leftval = 0, rightval = 0, res = 0;
	      TCHAR op = *(text + 1);
	      TCHAR *left, *right;
	      TCHAR tmp1buf[TMPBUF_SIZE + 2], tmp2buf[TMPBUF_SIZE + 2];

	      *str_ptr = (text + sizeof (_T("+")));

	      /* First get the things to be added. */
	      if (NO (left = get_next_token (str_ptr, tmp1buf, TMPBUF_SIZE,
					     input_buffer, input_size)))
		{
		  return (TOKEN_PREMATURE_END);
		}
	      if (NO (right = get_next_token (str_ptr, tmp2buf, TMPBUF_SIZE,
					      input_buffer, input_size)))
		{
		  return (TOKEN_PREMATURE_END);
		}

	      leftval = isqlt_tstol (left);
	      rightval = isqlt_tstol (right);

	      if (op == (TCHAR)('+'))
	        res = leftval + rightval;
	      else if (op == (TCHAR)('-'))
                res = leftval - rightval;
              else if (op == (TCHAR)('*'))
                res = leftval * rightval;
              else if (op == (TCHAR)('/') && rightval )
                res = leftval / rightval;

	      isqlt_stprintf (tmpnumbuf, _T("%ld"), res);
	      isqlt_tcsncpy (result, tmpnumbuf, maxsize);

	      return (result);
	    }

	  else if (word_follows (text + 1, _T("IF")))
	    {			/* Syntax: $IF $EQU STR1 STR2 THEN_RESULT ELSE_RESULT */
/* A command like following is legal, and will print "nauta-jauheliha"
   followed by NL:
   echo $if $equ 3 3 $if $equ 4 5 "sika" "nauta" "mursu" "-jauheliha\n";

   Also following works, by chance:

   $IF $NEQ $RETVAL "FOU" EXIT ECHO "Assertion passed RETVAL=" $RETVAL "\n";
 */
	      int is_true;
	      TCHAR *cond;
	      TCHAR *left, *right = empty_string;
	      TCHAR tmp1buf[TMPBUF_SIZE + 2];

	      *str_ptr = (text + sizeof (_T("IF")));

	      /* First get the thing to be tested. */
	      if (NO (cond = get_next_token (str_ptr, tmp1buf, TMPBUF_SIZE,
					     input_buffer, input_size)))
		{
		  return (TOKEN_PREMATURE_END);
		}

	      /* Length greater than zero? and different from "0" ? */
	      is_true = (*cond &&
			 ((*cond != '0') || (*(cond + 1) != '\0')));

/* Then get the then and else part. (We have to skip them anyway).
   The part we really return as result have to be copied directly
   into the result argument, and the one which is skipped is copied
   into tmp1buf and discarded.
   No dangling references should be returned!

   Important change at 10.March.1998:
   (the line **str_ptr && isspace(*((unsigned char *) *str_ptr)))
   If the then part ends with an immediately following closing
   character, that is, terminating zero '\0', any special character
   like ", ', >, or whatever, actually, anything else except white space,
   then it is understood that there's no else-part at all.
   So from this on, if there is an else-part, there should be always
   at least one blank between then and else parts.

   This is allows now constructs like
   <OPTION VALUE="fin" $IF $EQU $LANG fin SELECTED>Suomi
   or VALUE="$IF $EQU $JOKU $JOTAKIN stuff"
   without need for any cumbersome else-kludges.

 */
	      if (NO (left = get_next_token (str_ptr,
					     (is_true ? result : tmp1buf),
					  (is_true ? maxsize : TMPBUF_SIZE),
					     input_buffer, input_size)))
		{
		  return (TOKEN_PREMATURE_END);
		}

	      if (**str_ptr && isqlt_istspace (*((UTCHAR *) *str_ptr))
		  && NO (right = get_next_token (str_ptr,
					       (is_true ? tmp1buf : result),
					  (is_true ? TMPBUF_SIZE : maxsize),
						 input_buffer, input_size)))
		{		/* return(TOKEN_PREMATURE_END); */
		}		/* else-part is optional. */

/* We assign is_true to static LIF_value not until here, after we have
   returned back from recursion, i.e. gone through then and else parts
   which themselves might contain more $if-clauses. For all of them are
   executed too, even on those branches that actually are not valid
   (e.g. sub-ifs that are in else-part although the condition of this if
   indicated to choose then-part, or vice versa), so we decide that
   $LIF -macro refers always to the condition of the latest TOPMOST $IF
   as it is much clearer for a potential test-suite writer (i.e. me).
 */
	      LIF_value = is_true;
	      return (is_true ? left : right);	/* Choose then or else part */
	    }


/* The next one is for LAST or ARGV followed by index in brackets */
/* Should allow also expressions like $LAST[$COLCNT] or $ARGV[$I]
   $LAST[$ARGV[$I]] won't work yet, because of the nested brackets
   (Instead the unbalanced $LAST[$ARGV[I] might work temporarily,
   don't use...)
   14-MAY-1997 this has been fixed as by using the new function
   find_closing_point instead of old isqlt_tcschr(text+1,']'))
 */
	  else if ((word_follows (text + 1, _T("LAST")) ||
		    ((is_argv = 1), word_follows (text + 1, _T("ARGV")))) &&
	      ('[' == *(openptr = skip_blankos (text + sizeof (_T("LAST"))))) &&
		   (closeptr = SCP (find_closing_point (UCP (openptr),
						      ((UTCHAR) * openptr))))
	    )
	    {
	      TCHAR tmp1buf[TMPBUF_SIZE + 2];
	      TCHAR *indstr;

	      *str_ptr = closeptr + 1;	/* Skip past closing bracket. */

	      openptr++;	/* Past the opening bracket. */
	      if (NO (indstr = get_next_token (&openptr, tmp1buf, TMPBUF_SIZE,
					       input_buffer, input_size)))
		{
		  return (TOKEN_PREMATURE_END);
		}		/* Shouldn't really happen! (as there is ]) */

	      /* Old Way:  indnum = isqlt_tstoi(skip_blankos(openptr+1)); */

	      indnum = isqlt_tstoi (indstr);	/* Shouldn't care about closing ] */

	      if (is_argv)
		{
		  if ((indnum < 0) || (indnum > G_argc))
		    {
		      isqlt_stprintf (tmpnumbuf, _T("ARGV[%d]=Out_of_Bounds(%d)"),
			       indnum, n_out_cols);
		      isqlt_tcsncpy (result, tmpnumbuf, maxsize);
		      return (result);
		    }

		  if (NO G_argv[indnum])
		    {
		      return (TOKEN_NULL);
		    }
		  /* Was: { isqlt_tcsncpy(result,"NULL",maxsize); } */
		  else
		    {
		      return (G_argv[indnum]);
		    }
		}
	      else
		/* It's for LAST form. */
		{
		  if ((indnum < 1) || (indnum > n_out_cols))
		    {
		      isqlt_stprintf (tmpnumbuf, _T("LAST[%d]=Out_of_Bounds(%d)"),
			       indnum, n_out_cols);
		      isqlt_tcsncpy (result, tmpnumbuf, maxsize);
		      return (result);
		    }

		  indnum--;	/* One based indexing for columns */
		  if (SQL_NULL_DATA == out_cols[indnum].o_col_len)
		    {
		      return (TOKEN_NULL);
		    }
		  /* Was: { isqlt_tcsncpy(result,"NULL",maxsize); } */
		  else
		    /* Return a direct pointer to column buffer. */
		    {
		      return ((TCHAR *) out_cols[indnum].o_buffer);
		    }
		}

	    }			/* LAST or ARGV with [index] */

/* The rest should be in standard format and should be found with
   find_var_info function. */

	  else if (NO (macro_info = find_var_info (macro_name)))
	    {
	      isql_fprintf (error_stream,
			    _T("%") PCT_S _T(": get_next_token: Unknown macro word \"%") PCT_S _T("\" at line %ld of %") PCT_S _T("\n"),
			    progname, macro_name,
			latest_statement_begins_at (), current_loadexpr ());
	      fflush (error_stream);
	      return (TOKEN_PREMATURE_END);	/* Really, we should signal this! */
	    }

	  result = convert_macro_to_text (macro_info, str_ptr, result,
				      maxsize, input_buffer, input_size, 0);
	  return (result ? result : TOKEN_NULL);

	}			/* Begins with a dollar sign ? */

/* Else, an ordinary keyword. Also all other $-prefixed, unknown things */
/* The ending test was just: *text && !isqlt_istspace(*text)
   but test for singlequote and double quote was needed so that
   unknown $dollar_forms inside quoted strings (see the next function)
   don't screw up parse_statement_for_special_parameters.
   Also we now cut tokens to first opening or closing character, i.e.
   {, }, [, ], ( ) or < and >, as well as for COMMA (,), which is
   later necessary. (For implementing CSV reading, multiple foreach
   formal parameters, etc.)
   so that they will be returned one token (of one character) per time.
   (The last part of proceeding test of for).
   Now also the equal sign (=) so that set macro=value things are parsed
   all right.
   In any case, at least tokens should not be cut for a dot (.), for
   a dash (-), for a slash (/), and for a backslash (\),
   so that we don't need to enclose tablenames like qual.own.tablename,
   loaded filenames inside single- or doublequotes, nor options
   like -r for load command.
 */

      for (resptr = result;
	   *text && NOT isqlt_istspace (*((UTCHAR *) text))
	   && ('\'' != *text) && ('"' != *text)
	   && ((resptr == result) || (NOT cut_token_char (*text)));
	   maxsize--)
	{
	  if (maxsize > 0)
	    {
	      *resptr++ = *text++;
	    }
	}
      *resptr = '\0';
      *str_ptr = (TCHAR *) text;
      return (result);
    }
  else
    {
      if (input_buffer)		/* Have to read the next line in. */
	{
	  if (0 == isqlt_fgetts (input_buffer, input_size, stdin))
	    return (NULL);
	  current_linecount ()++;
	  text = *str_ptr = input;
	  goto de_nuevo;
	}
      else
	{
	  return (NULL);
	}
    }
}


#define not_escaped(C)\
 ( ((((UTCHAR)(C))) < 127) && (isalnum((C)) || ('_' == (C))) )


/* The next two functions, escape_string and count_escaped_length
   should use exactly the same criteria for determining what
   to escape and how.
   Doesn't add the terminating zero, instead, always keeps the
   original character following right after the last copied.
 */
UTCHAR *
escape_string (UTCHAR *dest, UTCHAR *src)
{
  UTCHAR *org_dest = dest;

  for (; *src; src++)
    {
      if (not_escaped (*src))
	{
	  *dest++ = *src;
	}
      else
	/* Needs backslash plus 3 octal digits. */
	{
	  UTCHAR last = *(dest + 4);
	  isqlt_stprintf (SCP (dest), _T("\\%03o"), *((UTCHAR *) src));
	  dest += 4;
	  *dest = last;		/* Restore the original character right after. */
	}
    }

  return (org_dest);
}


int
count_escaped_length (UTCHAR *s)
{
  int len;

  for (len = 0; *s; s++)
    {
      if (not_escaped (*s))
	{
	  len++;
	}
      else
	{
	  len += 4;
	}			/* Backslash plus 3 octal digits. */
    }
  return (len);
}



/* Used by exec_for_foreach to get the count of parameters in
   the statement, as well as to record all "special parameter extensions"
   like ?C, ?R or ?1 into pardescvec.

   Now, 3-MAY-1997 this is used also for substituting $dollar_forms
   into their respective results got with get_next_token.
   Note that because the whole_statement buffer is destructively
   modified in place, and because $dollar_forms may produce longer
   strings than the respective length of the $dollar_form, it is
   important that whole_statement points to a buffer that really
   has some extra space. For this reason, exec_one will check that
   its argument is pointing to input buffer, and if not, it will
   copy it there. (E.g. happens when statements are executed from
   the command line.)
   Note that this doesn't currently check for any kind of an overflow
   of that input buffer, but let's just now cross our fingers and hope
   that the input buffer is big enough (Well, it's almost 65Kb).

   Added possibility to give pardescvec as NULL, so also exec_one can
   call this one for doing just the $-substitutions, giving maxpars as
   zero.
 */

int
parse_statement_for_special_parameters (UTCHAR * whole_statement,
					UTCHAR * pardescvec,
					int maxpars)
{
  int pars_found = 0;
  int inside_string = 0;
  UTCHAR prevchar;
  UTCHAR *str = whole_statement, *p;
  int whole_len = (int) isqlt_tcslen (SCP (whole_statement));
/* whole_len should be updated accordingly wherever the length of
   whole_statement is changed! */
  TCHAR tmp1buf[TMPBUF_SIZE + 2];

  for (prevchar = 0; *str; prevchar = *str, str++)
    {
      switch (*str)
	{

/* A dollar is escaped inside strings with a backslash, e.g.
   'A Jackpot of 1000\$' is sent to server as it is, which in turn
   will convert \$ to just a $.

   but outside of strings it currently cannot be escaped
   except by setting macro_substitution off with the command
   SET MACRO_SUBSTITUTION OFF;
   (E.g. not by doubling the dollars (as before), because double
   dollar now makes sense as a syntactic construct inside isql).

   This makes it possible to write commands like

   foreach integer between 1 1 create table \$ARGV[$I];

   i.e. \ makes sure that the arbitrary tablename got from the command
   line works as a tablename, even although it would be a reserved
   SQL keyword.

 */
	case '$':
	  {
	    if (0 == flag_macro_substitution)
	      {
		break;
	      }
#ifdef FORGET_THIS
	    /* If the next one is a $ also, then it is a real dollar. */
	    if (('$' == *(str + 1)) && (NOT inside_string))
	      {			/* Shift the rest of string one left, overwriting the */
		for (p = str; *p; p++)
		  {
		    *p = *(p + 1);
		  }		/* first dollar */
		whole_len--;
	      }
#endif
	    else
	      /* It's a dollar expression to be substituted. */
	      {			/* Can be done also inside strings. */
		UTCHAR *p1, *p2, *cont;

		cont = str;
		p = UCP (get_next_token (((TCHAR **) &cont), tmp1buf, TMPBUF_SIZE,
					 NULL, 0));
		/* cont points now to the first char after $dollar_form */

		if (NULL == p)	/* If dollar expression produces a null */
		  {		/* We could just squash out the dollar and param name.
				   isqlt_tcscpy(SCP(str),SCP(cont));
				   whole_len -= (cont-str);
				   BUT WE DON'T DO THAT, INSTEAD WE USE STRING "NULL": */
		    p = UCP (_T("NULL"));
		  }
/*          No  else  i.e. fall through in any case. */
		{
		  int oldlen = (int) (cont - str);	/* Length of $dollar_form */
		  int newlen =	(int) /* Length of the new replacement */
		  (inside_string ? count_escaped_length (UCP (p))
		   : isqlt_tcslen (SCP (p)));
		  int delta = (newlen - oldlen);	/* Negative if shortens */

/* First copy the rest of statement to the proper new location, as either
   for making space for the string produced (if longer than $dollar_form)
   or to squash out any extra rubbish characters (if produced string is
   shorter than $dollar_form).
   Note that in the former case isqlt_tcscpy cannot be used.
   We leave the copying out if the produced string is of the same length
   than the $dollar_form which produced it.
 */
		  if (newlen > oldlen)
		    {		/* Copy from right to left making space. */
		      for (p1 = (str + whole_len), p2 = p1 + delta;
			   (p1 >= cont);
			   p1--, p2--)
			{
			  *p2 = *p1;
			}
		    }
		  else if (newlen < oldlen)
		    {		/* Do NOT use isqlt_tcscpy in order to squash out extra characters --- ranges overlap! */
		      for (p1 = cont, p2 = p1 + delta;
			   (p1 <= (str + whole_len));
			   p1++, p2++)
			{
			  *p2 = *p1;
			}
		    }
/* And then copy the produced string itself, starting from the same
   location where the $dollar_form was (but leave the terminating
   zero out this time, so that it does not overwrite the first char
   of "rest": */
		  if (inside_string)
		    {
		      escape_string (UCP (str), UCP (p));
		    }
		  else
		    {
		      memcpy (SCP (str), SCP (p), newlen * sizeof (TCHAR));
		    }
/* Update whole_len. Is equivalent to whole_len -= (oldlen-newlen); */
		  whole_len += delta;
		  str += (newlen - 1);	/* -1 is for str++ in the end of loop */
		}
	      }
	    break;
	  }

	case '-':
	  {			/* If the previous was a dash also, then it is a comment. */
	    if (!inside_string && ('-' == prevchar))
	      {			/* Then skip to the end of this line or whole statement: */
		for (; (*str && (*str != '\n')); str++);
		str--;		/* As there are str++ in the end of for loop. */
	      }
	    break;
	  }
/* Note: '' = "", ''' by itself is syntax error, but ''' fish' = "' fish"
   '''' = "'", '''''' = "''"  See notes below.
 */
	case '\\':		/* Backslash ? */
	  {
	    if (inside_string)	/* Inside string? */
	      {			/* Then skip that next character, whatever it is, also */
		if (*(str + 1))
		  {
		    str++;
		  }		/* another backslash or quote */
	      }			/* Or dollar!!! */
	    break;
	  }
	case '\'':		/* A single quote? */
	  {
	    if (!inside_string)
	      {
		inside_string = 1;
	      }
	    else
	      /* We are inside string already. Then if the next */
	      {			/* char is not a quote, then this is a terminating quote */
		if ('\'' != *(str + 1))
		  {
		    inside_string = 0;
		  }
		else
		  {
		    str++;
		  }		/* Skip that quote next time. */
	      }
	    break;
	  }
/*        case ':': Commented out until I know this for sure. */
	case '?':
	  {
	    if (1 == flag_ignore_params)
	      {
		break;
	      }
	    if (inside_string)
	      {
		break;
	      }			/* Not a true parameter. */
	    if (isalpha (str[1]) || (':' == str[1]))
	      break; /* ?name is SPARQL instruction. */
	    if ('?' == str[1])
	      break; /* This is the first of '??' in SPARQL, the second will be counted. */
	    if (pars_found == maxpars)
	      {
		isql_printf (
			      _T("-- More than %d parameters, ignoring all the rest of the statement %") PCT_S _T("\n"),
			      maxpars, whole_statement);
		if (pardescvec)
		  pardescvec[pars_found++] = '\0';	/* Terminating zero. */
	      }
	    else if (pars_found > maxpars)
	      {
		pars_found++;
	      }
	    else
	      /* pars_found < maxpars */
	      {			/* The older standard allows also colon as a parameter
				   marker? */
/*              if(':' == *str) { *str = '?'; } */

/* If the question mark is followed by an alphanumeric char, or by
   another question mark, then it is our special parameter extension
   like ??  ?C  ?R  or ?1 */
		if (*(str + 1) && (('?' == *(str + 1)) || isalnum (*(str + 1))))
		  {
		    str++;
/* Record the character found, and then copy the rest of statement string
   one left, overwriting just that character, so that DBMS will see just
   normal parameter marker, a question mark without any whistles: */
		    if (pardescvec)
		      {
			pardescvec[pars_found++] = *(str);
		      }
		    for (p = str; *p; p++)
		      {
			*p = *(p + 1);
		      }
		    whole_len--;
		    str--;	/* Because of str++ in the end of for loop */
		  }
		else
		  /* Otherwise, it was followed by space, punctuation */
		  {		/* or maybe a terminating zero. It is a normal par. */
		    /* Sapce is used for marking Nothing special. */
		    if (pardescvec)
		      {
			pardescvec[pars_found++] = ' ';
		      }
		  }
	      }
	    break;
	  }			/* case ? */
	}			/* switch */

    }				/* for loop. */

  if ((pars_found <= maxpars) && pardescvec)
    {
      pardescvec[pars_found] = '\0';
    }
  return (pars_found);
}



/* SQL standard allows also C-like multiline comments like this.
   Maybe we should check for those also?
   Note that this function, as well as the previous,
   parse_statement_for_special_parameters function should be modified
   accordingly, now that Kubl will soon allow
   also backslash escape sequences in strings.
   E.g. with the new system '\'' is "'" and '\\' is "\" is pain in the
   ass. So we have to know which system we are using.
   Not really, implemented 26-MAR-1997 easily by just adding a
   simple case clause for the backslash.
   Added also two more arguments, scivec (semicolon index vector)
   and maxsemicols which is its maximum size.
   scivec will hold the one-based indices to semicolons present in
   the str, or optionally if its is given as NULL pointer, then nothing
   is tried to store there. Indices are one-based so that the end of them
   can be marked with a terminating zero.

   Note the similarity with the previous function which collects
   parameter-markers and has to avoid strings and comments as well.

   Modification 14-MAY-1997: Added two parameters:
   chars_watched which should be a string two characters, e.g. "{}"
   if we are counting braces, or "[]" if brackets, or "()" if parentheses,
   or "<>" if angle brackets.
   and return_index_instead_of_level, which if non-zero, tells this
   function to return, instead of the final bracelevel, the one-based
   index to the point where the corresponding closing character
   of the same level was found (that is, where bracelevel plunged
   back to the zero.) or zero if no matching closing character were
   found.
   The old functionality is accessible with the following macro.
 */


#define count_bracelevel(s,l,vec,maxsemicols, set0atcomment, pinside_string)\
count_bracelevel_or_find_matching_one(UCP(s),(l),(vec),(maxsemicols),\
                                      UCP(_T("{}")),0, set0atcomment, pinside_string)


int count_bracelevel_or_find_matching_one
  (UTCHAR * str, int bracelevel, int *scivec, int maxsemicols,
   UTCHAR * chars_watched, int return_index_instead_of_level,
   int set0atcomment, UTCHAR *pinside_string)
{
  UTCHAR prevchar;
  int semicols_found = 0;
  UTCHAR *whole_line = str;

  if (scivec)
    {
      scivec[semicols_found] = 0;
    }				/* Empty it first. */

  for (prevchar = 0; *str; prevchar = *str, str++)
    {
      switch (*str)
	{
	case '-':
	  {			/* If the previous was a dash also, then it is a comment. */
	    if (!*pinside_string && ('-' == prevchar))
	      {
		if (set0atcomment && (bracelevel == 0) && (str - whole_line) > 0)
		  *(str - 1) = 0;
		if (return_index_instead_of_level)	/* Failed, because */
		  {
		    return (0);
		  }		/* no closing bracket was found. */
		else
		  {
		    return (bracelevel);
		  }
	      }
	    break;
	  }
/* Note: '' = "", ''' by itself is syntax error, but ''' fish' = "' fish"
   '''' = "'", '''''' = "''"
 */
	case '\\':		/* Backslash ? */
	  {
	    if (*pinside_string)	/* Inside string? */
	      {			/* Then skip that next character, whatever it is, also */
		if (*(str + 1))
		  {
		    str++;
		  }		/* another backslash or quote */
	      }
	    break;
	  }
#ifdef OLD_CODE_LEFT_FOR_CLARITYS_SAKE
	case '\'':		/* A single quote? */
	  {
	    if (!*pinside_string)
	      {
		*pinside_string = 1;
	      }
	    else
	      /* We are inside string already. Then if the next */
	      {			/* char is not a quote, then this is a terminating quote */
		if ('\'' != *(str + 1))
		  {
		    *pinside_string = 0;
		  }
		else
		  {
		    str++;
		  }		/* Skip that quote next time. */
	      }
	    break;
	  }
#endif /* Replaced by this: */
	case '\'':
	case '"':		/* A single quote or double quote? */
	  {
	    if (!*pinside_string)	/* Beginning a new string? */
	      {
		*pinside_string = (*str);
	      }			/* Mark the quote type to the flag. */
	    else if (*pinside_string == (*str))	/* Terminating quote? */
	      {			/* (Should be similar to opening quote) */
		/* If we are inside singlequote string then two */
		/* singlequotes in sequence stand for one. */
		if ('\'' == *pinside_string)
		  {
		    if ('\'' != *(str + 1))
		      {
			*pinside_string = 0;
		      }
		    else
		      {
			str++;
		      }		/* Skip that quote next time. */
		  }
		else
		  {
		    *pinside_string = 0;
		  }		/* With doublequotes always terminate. */
	      }
	    break;
	  }
	case ';':		/* A semicolon? */
	  {
	    if (*pinside_string)
	      {
		break;
	      }			/* Ignore semicols in strings */
/* Ignore semicols also inside braces or if scivec is NULL: */
	    if ((bracelevel > 0) || (NULL == scivec))
	      {
		break;
	      }
	    if (semicols_found == maxsemicols)
	      {
		isql_fprintf (error_stream,
			      _T("%") PCT_S _T(": Exiting because there are more than %d semicolons at the line: %") PCT_S _T("\n"),
			      progname, maxsemicols, whole_line);
		fflush (error_stream);
		isql_exit (1);
		scivec[semicols_found++] = 0;	/* Terminating zero. */
	      }
	    else if (semicols_found > maxsemicols)
	      {
		semicols_found++;
	      }
	    else
	      /* semicols_found < maxsemicols */
	      {			/* If it still fits store one-based index to scivec. */
		scivec[semicols_found++] = (int) ((str - whole_line) + 1);
		scivec[semicols_found] = 0;	/* Terminating zero. */
	      }
	    break;
	  }			/* case semicolon */
#ifdef OLD_CODE_COMMENTED_OUT
	case '{':		/* An opening brace? */
	  {
	    if (!*pinside_string)
	      {
		bracelevel++;
	      }
	    break;
	  }
	case '}':		/* A closing brace? */
	  {
	    if (!*pinside_string)
	      {
		--bracelevel;
	      }
	    break;
	  }
#endif /* AS IT IS REPLACED BY THIS: */
	default:
	  {			/* Check if it is an opening brace, bracket, parenthesis? */
	    if ((*str == *chars_watched) && (NOT *pinside_string))
	      {
		bracelevel++;
	      }
	    /* Or corresponding closing one? */
	    else if ((*str == *(chars_watched + 1)) && (NOT *pinside_string))
	      {
		--bracelevel;
		if (return_index_instead_of_level && (0 == bracelevel))
		  {
		    return (int) ((str - whole_line) + 1);
		  }
	      }

	    break;
	  }
	}			/* switch */

    }				/* for loop */

  if (return_index_instead_of_level)
    {
      return (0);
    }				/* Failed, didn't find the closing bracket of same level. */
  else
    {
      return (bracelevel);
    }

}


/* +2 formula works in ASCII for others than parentheses, i.e.
   for [ ], { } and < > */
#define get_closing_char(C) ( ( '(' == (C) ) ? ')' : ((C)+2) )

/* E.g. find_closing_point("$ARGV[$U[$ARGV[$I]]] the rest",'[')
   returns a pointer here ---------------------^
 */
UTCHAR *
find_closing_point (UTCHAR * start_point, UTCHAR opening_char)
{
  UTCHAR char_pair[3];
  int ind;
  UTCHAR inside_string = 0;

  char_pair[0] = opening_char;
  char_pair[1] = ((UTCHAR) get_closing_char (opening_char));
  char_pair[2] = '\0';

  ind = count_bracelevel_or_find_matching_one (UCP (start_point), (0),
					       NULL, 0, char_pair, 1, 0, &inside_string);

  if (0 == ind)
    {
      return (NULL);
    }
  else
    {
      return (start_point + (ind - 1));
    }
}



/* Returns the name of the procedure/function/trigger being declared
   if the line begins a declaration, otherwise returns a NULL.
 */
TCHAR *
is_declaration (TCHAR *line)
{
  int len;
  TCHAR savechar, *ptr, *ptr2, *result;
  ptr = skip_blankos (line);

  if (!strncasecmp (_T("CREATE"), ptr, 6) && *(ptr + 6) && isqlt_istspace (*(ptr + 6)))
    {
      ptr = skip_blankos (ptr + 6);
      if ((!strncasecmp (ptr, _T("PROCEDURE"), (len = 9))
	   ||
	   !strncasecmp (ptr, _T("FUNCTION"), (len = 8))
	   ||
	   !strncasecmp (ptr, _T("MODULE"), (len = 6))
	   ||
	   !strncasecmp (ptr, _T("METHOD"), (len = 6))
	   ||
	   !strncasecmp (ptr, _T("TRIGGER"), (len = 7)))
	  && *(ptr + len) && isqlt_istspace (*(ptr + len)))	/* Followed by space? */
	{
	  ptr = skip_blankos (ptr + len);
	  for (ptr2 = ptr; *ptr2 && !isqlt_istspace (*ptr2) && (*ptr2 != '('); ptr2++);
	  savechar = *ptr2;
	  *ptr2 = '\0';
	  result = chestrdup (ptr, _T("is_declaration"));	/* Make a copy. */
	  *ptr2 = savechar;
	  return (result);
	}
    }
  return (NULL);
}

/* =================================================================== */
/*  SQL - STUFF FOR PRINTING OUT THE COLUMN BANNERS AND CONTENTS, ETC. */
/* =================================================================== */

TCHAR *current_lines_range(void)
{
  long linefrom, lineto;
  static TCHAR lines_range[41];
  linefrom = latest_statement_begins_at ();
  lineto = current_linecount ();
  if (linefrom == lineto)
    isqlt_stprintf (lines_range, _T("at line %ld"), linefrom);
  else
    isqlt_stprintf (lines_range, _T("in lines %ld-%ld"), linefrom, lineto);
  return lines_range;
}

/* Now returns 0 if error is considered so easy that it is safe to
   continue, and 1 if the error is considered severe enough.
 */
int
print_error (HENV e1, HDBC e2, HSTMT e3, RETCODE rc)
/* Was:  (HSTMT e1, HSTMT e2, HSTMT e3) */
{
  int was_deadlock = 0;
  int message_end_is_cr = 0;
  short len;
  int ret = 1;
  TCHAR additional_message[81];


  while (SQL_SUCCESS == SQLError (e1, e2, e3, (UTCHAR *) tmp_SQL_error_state, NULL,
	    (UTCHAR *) tmp_SQL_error_message, sizeof (tmp_SQL_error_message),
	    (SWORD *) & len))
    {
      memcpy (SQL_error_state, tmp_SQL_error_state, sizeof (SQL_error_state));
      memcpy (SQL_error_message, tmp_SQL_error_message, sizeof (SQL_error_message));
      additional_message[0] = '\0';	/* By default, empty. */

      /* The transaction was deadlocked and vol_deadlock_retries is not 0 yet?
	 40001 is the correct status code for deadlock, 4001 is just a fluke
	 which will be done with (until that we just try to cope here).
	 The shortcoming here is that we are not sure that the caller actually
	 wants to retry, so the message (retrying ... times) might be
	 misleading.
       */
#ifndef ODBC_ONLY
      if (0 == isqlt_tcscmp (SQL_error_state, _T("VIRTS")) &&
	  isqlt_tcsstr (SQL_error_message, _T("SR312")))
	{
	  isql_fprintf (error_stream,
	      _T("\nThe server is shutting down\n"));
	  isql_exit (0);
	}
#endif

      if (vol_deadlock_retries &&
	  ((0 == isqlt_tcscmp (SQL_error_state, _T("40001"))) ||
	   (0 == isqlt_tcscmp (SQL_error_state, _T("4001")))))
	{
	  was_deadlock = 2;

	  if (1 == vol_deadlock_retries)	/* Be grammatically correct, gc... */
	    {
	      isqlt_tcscpy (additional_message, _T(" (retrying one more time)"));
	    }
	  else
	    {
	      isqlt_stprintf (additional_message, _T(" (retrying %ld times)"),
		  vol_deadlock_retries);
	    }
	}
      if (('\0' == additional_message[0]) &&
	  ('\n' == SQL_error_message [isqlt_tcslen (SQL_error_message) - 1]) )
	message_end_is_cr = 1;
      isql_fprintf (error_stream,
	  _T("\n%") PCT_S _T(" %") PCT_S _T(": %") PCT_S _T("%") PCT_S _T("%") PCT_S _T("%") PCT_S _T(" of %") PCT_S _T(":\n%") PCT_S _T("\n"),
	  rc == SQL_SUCCESS_WITH_INFO ? _T("Warning") : _T("*** Error"),
	  SQL_error_state, SQL_error_message,
	  additional_message,
	  (message_end_is_cr ? _T("") : _T("\n")),
	  current_lines_range(),
	  current_loadexpr (),
	  command_text_on_error ? input : _T("<not printed>"));

      if (was_deadlock)
	{
	  vol_deadlock_retries--;
	  /* Here we could have later something like:
	     isql_sleep(deadlock_retry_pause);
	   */
	  /* Don't do
	     SQLTransact (henv, hdbc, SQL_ROLLBACK);
	     here, although we could, as Kubl doesn't need it.
	     (Although some other DBMS might need it.) See also notes about
	     SQLTransact and preservation of cursors and prepared statements
	     over Rollback, just before function exec_for_foreach.
	   */
	  return (was_deadlock);
	}

      if (0 == isqlt_tcscmp (SQL_error_state, _T("08S01")))
	{
	  isql_exit (2);
	}


      /* E.g. 01000 General Warning, 01004 Data Truncated,
	 01006 Privilege not revoked, 01S03 No rows updated or deleted,
	 01S04 More than one row updated or deleted.
       */
      if (0 == isqlt_tcsncmp (SQL_error_state, _T("01"), 2))
	{
	  ret = 0;
	  /* this is because warnings will obscure the testsuite $IF $STATE OK tests */
	  SQL_error_state[0] = 'O'; SQL_error_state[1] = 'K'; SQL_error_state[2] = '\0';
	}
      /*
	 2001    Non unique primary key on torttu
	 22003   Numeric value out of range
	 22005   Error in assignment
	 22008   Datetime field overflow
	 22012   Division by zero
	 23000   Integrity constraint violation (e.g. NULL value on NON-NULL)
	 24000   Invalid cursor state (DM)
       */
      if ('2' == SQL_error_state[0])
	{
	  ret = 0;
	}
    }
  return (ret);
}




/* SQL data type codes.
   These were taken from ../sqlcli.h and ../sqlcli2.h header files.
   Added by AK 3-JAN-1997.
 */
TCHAR *
get_sql_type_title (int type)
{
  switch (type)
    {
#ifdef SQL_CHAR
    case SQL_CHAR:
      {
	return (_T("CHAR"));
      }				/*  1 */
#endif
#ifdef SQL_NUMERIC
    case SQL_NUMERIC:
      {
	return (_T("NUMERIC"));
      }				/*  2 */
#endif
#ifdef SQL_DECIMAL
    case SQL_DECIMAL:
      {
	return (_T("DECIMAL"));
      }				/*  3 */
#endif
#ifdef SQL_INTEGER
    case SQL_INTEGER:
      {
	return (_T("INTEGER"));
      }				/*  4 */
#endif
#ifdef SQL_SMALLINT
    case SQL_SMALLINT:
      {
	return (_T("SMALLINT"));
      }				/*  5 */
#endif
#ifdef SQL_FLOAT
    case SQL_FLOAT:
      {
	return (_T("FLOAT"));
      }				/*  6 */
#endif
#ifdef SQL_REAL
    case SQL_REAL:
      {
	return (_T("REAL"));
      }				/*  7 */
#endif
#ifdef SQL_DOUBLE
    case SQL_DOUBLE:
      {
	return (_T("DOUBLE PRECISION"));
      }				/*  8 */
#endif
#ifdef SQL_DATETIME
    case SQL_DATETIME:
      {
	return (_T("DATE"));
      }				/*  9 */
#endif
#if defined  (SQL_DATE) && NOT defined (SQL_DATETIME)
/* Was: #if defined  (SQL_DATE) && defined (ODBC_VER) */
    case SQL_DATE:
      {
	return (_T("DATE"));
      }				/* 9 in sqlext.h */
#endif
#ifdef SQL_TIME
    case SQL_TIME:
      {
	return (_T("TIME"));
      }				/* 10 in sqlext.h */
#endif
#if defined (SQL_INTERVAL) && ((NOT defined (SQL_TIME)) || (SQL_INTERVAL != SQL_TIME))
    case SQL_INTERVAL:
      {
	return (_T("TIME"));
      }				/* 10 */
#endif
#ifdef SQL_TIMESTAMP
    case SQL_TIMESTAMP:
      {
	return (_T("TIMESTAMP"));
      }				/* 11 */
#endif
#ifdef SQL_VARCHAR
    case SQL_VARCHAR:
      {
	return (_T("VARCHAR"));
      }				/* 12 */
#endif
#ifdef SQL_BIT
    case SQL_BIT:
      {
	return (_T("BIT"));
      }				/* 14 */
#endif
#ifdef SQL_BIT_VARYING
    case SQL_BIT_VARYING:
      {
	return (_T("BIT VARYING"));
      }				/* 15 */
#endif
#ifdef SQL_LONGVARCHAR
    case SQL_LONGVARCHAR:
      {
	return (_T("LONG VARCHAR"));
      }				/* (-1) */
#endif
#ifdef SQL_BINARY
    case SQL_BINARY:
      {
	return (_T("BINARY"));
      }				/* (-2) in sqlext.h */
#endif
#ifdef SQL_VARBINARY
    case SQL_VARBINARY:
      {
	return (_T("VARBINARY"));
      }				/* (-3) in sqlext.h */
#endif
#ifdef SQL_LONGVARBINARY
    case SQL_LONGVARBINARY:
      {
	return (_T("LONG VARBINARY"));
      }				/* (-4) */
#endif
#ifdef SQL_BIGINT
    case SQL_BIGINT:
      {
	return (_T("BIGINT"));
      }				/* (-5) in sqlext.h */
#endif
#ifdef SQL_TINYINT
    case SQL_TINYINT:
      {
	return (_T("TINYINT"));
      }				/* (-6) in sqlext.h */
#endif
    case -8:
      {
	return (_T("NCHAR"));
      }				/* (-8) in sqlext.h */
    case -9:
      {
	return (_T("NVARCHAR"));
      }				/* (-9) in sqlext.h */
    case -10:
      {
	return (_T("LONG NVARCHAR"));
      }				/* (-10) in sqlext.h */

    default:
      {
	TCHAR tmp[33];
	isqlt_stprintf (tmp, _T("UNK_TYPE:%d"), type);
	return (chestrdup (tmp, _T("get_sql_type_title")));
      }
    }				/* switch */
}

TCHAR *
get_sql_col_type_def (stmt_out_t * out)
{
/* Check whether we have to add something after a column type: */
  if ((out->o_nullable == SQL_NO_NULLS) || (out->o_type == SQL_CHAR))
    {
      TCHAR result[181];
      isqlt_tcscpy (result, get_sql_type_title (out->o_type));
      if (out->o_type == SQL_CHAR)
	{
	  isqlt_stprintf ((result + isqlt_tcslen (result)), _T("(%d)"), (int) out->o_width);
	}
      if (out->o_nullable == SQL_NO_NULLS)
	{
	  isqlt_tcscat (result, _T(" NOT NULL"));
	}

      return (chestrdup (result, _T("get_sql_col_type_def")));
    }
/* Otherwise, we can use directly the same type string returned by
   get_sql_type_title: */
  else
    {
      return (get_sql_type_title (out->o_type));
    }
}


#define FIELD_WIDTH_SANITY_CHECK 80

void
field_print_normal (TCHAR *str, SQLULEN w, int rightp, int inx)
{
  TCHAR temp[21];
  if ((w > FIELD_WIDTH_SANITY_CHECK) || (w < 1))
    {				/* Added by AK 4-JAN-1997 to catch strange bugs. */
/* Don't clutter our output any more...
   isql_fprintf(error_stream,
   "\n***field_print(\"%s\",%d,%d) called with an outrageous width: %u !\n",
   str,w,rightp,w);
 */
      w = FIELD_WIDTH_SANITY_CHECK;
    }

  if ((n_out_cols - 1) == inx)	/* The rightmost column? */
    {				/* Avoid any unnecessary padding on right side. */
      if (rightp)
	{
	  isql_printf (_T("%*") PCT_S _T(""), (int) w , str);
	}
      else
	{
	  isql_fputs (str, stdout);
	}
    }
  else
    {
      if (rightp)
	{
	  isql_printf (_T("%*") PCT_S _T("  "), (int) w , str);
	}
      else
	{
	  isql_printf (_T("%-*") PCT_S _T("  "), (int) w , str);
	}
    }
}

void
field_print_HTML (TCHAR *str, SQLULEN w, int rightp, int inx)
{
  if (0 == inx)
    {
      isql_fputs (oo_rb, stdout);
    }				/* Leftmost Column, Row begins */
  isql_fputs (oo_db, stdout);	/* Cell Begins. */
  if (oo_esc)
    {
      html_print_escaped (str, stdout);
    }
  else
    {
      isql_fputs (str, stdout);
    }
  isql_fputs (oo_de, stdout);	/* Cell Ends. */
  if ((n_out_cols - 1) == inx)
    {
      isql_fputs (oo_re, stdout);
    }				/* The Row End */
}

void
field_print_csv (TCHAR *str, SQLULEN w, int unused, int inx)
{
  TCHAR *ch;
  for (ch = str; *ch; ++ch)
    {
      if (!isprint(*ch) || *ch == (TCHAR)('%') || *ch == (TCHAR)('\n') || *ch == (TCHAR)('\r') || *ch == *csv_field_separator)
        isqlt_tprintf (_T("%%%2.2hX"), (short)*ch);
      else
        isqlt_puttchar (*ch);
    }

  if (inx < n_out_cols - 1)  /* not the rightmost column? */
    isqlt_fputts (csv_field_separator, stdout);
}

void
field_print_csv_rfc4180 (TCHAR *str, SQLULEN w, int unused, int inx)
{
  TCHAR *ch;
  isqlt_puttchar ('"');
  for (ch = str; *ch; ++ch)
    {
      if (!isprint(*ch))
        isqlt_tprintf (_T("%%%2.2hX"), (short)*ch);
      else if (*ch == (TCHAR)('"'))
	isqlt_tprintf (_T("\"\""));
      else
        isqlt_puttchar (*ch);
    }
  isqlt_puttchar ('"');
  if (inx < n_out_cols - 1)  /* not the rightmost column? */
    isqlt_fputts (csv_rfc4180_field_separator, stdout);
}

void
field_print (TCHAR *str, SQLULEN w, int rightp, int inx)
{
  if (in_HTML_mode ())
    {
      field_print_HTML (str, w, rightp, inx);
    }
  else
    {
      field_print_normal (str, w, rightp, inx);
    }
}


void
print_banner ()
{
  int inx;
  unsigned int len;
  TCHAR *save_oo_db = oo_db;
  TCHAR *save_oo_de = oo_de;

  oo_db = oo_hb;
  oo_de = oo_he;

  if (print_types_also)
    {
      for (inx = 0; inx < n_out_cols; inx++)
	{
	  coltypetitles[inx] = get_sql_col_type_def (&out_cols[inx]);
	  len = (int) isqlt_tcslen (coltypetitles[inx]);
	  if (len > out_cols[inx].o_width)
	    {
	      out_cols[inx].o_width = len;
	    }
	}
    }

  for (inx = 0; inx < n_out_cols; inx++)
    {
      field_print ((out_cols[inx].o_title ? out_cols[inx].o_title : (TCHAR *) _T("NULL")),
		   out_cols[inx].o_width, 0, inx);
    }

  if (print_types_also)
    {
      isql_printf (_T("\n"));
      for (inx = 0; inx < n_out_cols; inx++)
	{
	  field_print (coltypetitles[inx], out_cols[inx].o_width, 0, inx);
	}
    }

  if (NOT in_HTML_mode ())
    {
      isql_fputs (_T("\n_______________________________________________________________________________\n\n"),
		  stdout);
    }
  else
    {
      isql_fputs (_T("\n"), stdout);
    }
  fflush (stdout);

  oo_db = save_oo_db;
  oo_de = save_oo_de;

}

void
print_banner_vert ()
{
  int inx;
  unsigned int len;

  if (!print_types_also)
    return;

  isql_fputs (_T("\nresult set\n"), stdout);
  for (inx = 0; inx < n_out_cols; inx++)
    {
      coltypetitles[inx] = get_sql_col_type_def (&out_cols[inx]);
      isql_printf (_T("%i %") PCT_S _T(" : ") _T("%") PCT_S _T("\n"),
                   inx,
                   (out_cols[inx].o_title ? out_cols[inx].o_title : (TCHAR *) _T("??")),
                   coltypetitles[inx]);
    }

  isql_fputs (_T("\n================================================================\n\n"), stdout);
  fflush (stdout);
}

#define BLOB_BUFFER_SIZE 4001

/* Returns return code from the last SQLGetData done. */
int
print_blob_col (HSTMT stmt, UWORD n_col, SQLULEN width, SWORD sql_type)
{
  static TCHAR blob_buffer[BLOB_BUFFER_SIZE + 1];
  int got_n_bytes;
  SQLULEN total = 0;
  int rc;

  if (in_HTML_mode ())
    {
      if (1 == n_col)
	{
	  isql_fputs (oo_rb, stdout);
	}			/* Leftmost Column, Row begins */
      isql_fputs (oo_db, stdout);	/* Cell Begins. */
      isql_fputs (_T("<PRE>"), stdout);
    }

  for (;;)
    {
      SQLLEN n_recv;
      rc = SQLGetData (stmt, n_col,
		       SQL_C_TCHAR,
		       blob_buffer, BLOB_BUFFER_SIZE, &n_recv);
/* Here we got either SQL_NO_DATA or an error. */
      if (((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
	  || (n_recv == SQL_NULL_DATA))
	{			/* This ^ means really a blob of length 0, not real NULL ??? */
/* Tell calling function that everything is still all right, no panic: */
	  if (SQL_NO_DATA_FOUND == rc)
	    {
	      rc = SQL_SUCCESS;
	    }
	  if (NOT in_HTML_mode ())
	    {
	      field_print (_T(""), width, 0, (n_col - 1));
	    }
	  else
	    {
	      goto print_end_tags;
	    }			/* Oh yes, it's uglier than ever. */
	  return (rc);
	}

/* If we get SQL_NO_TOTAL, then we have to use isqlt_tcslen. This doesn't
   work with true binary data with null bytes in the last part. */
      got_n_bytes = (int)
	((rc == SQL_SUCCESS) ? ((n_recv != SQL_NO_TOTAL) ? n_recv
				: isqlt_tcslen (blob_buffer))
	 : BLOB_BUFFER_SIZE - 1);	/* Not the last part. */
      total += got_n_bytes;

      if (in_HTML_mode () && oo_esc)
	{
	  if (got_n_bytes)
	    {
	      html_print_escaped_n (blob_buffer, stdout, got_n_bytes);
	    }
	}
      else
	{
	  isqlt_fputts (blob_buffer, stdout);
	}

      if (rc == SQL_SUCCESS)	/* No more data after this one. */
	{
	  if (in_HTML_mode ())
	    {
	    print_end_tags:
	      isql_fputs (_T("</PRE>"), stdout);
	      isql_fputs (oo_de, stdout);	/* Cell Ends. */
	      if (n_out_cols == n_col)
		{
		  isql_fputs (oo_re, stdout);
		}		/* The Row End */
	    }
	  else
	    {
	      if (width > total)
		{
		  field_print (_T(""), (width - total), 0, (n_col - 1));
		}
	      else if (n_col < n_out_cols)
		{
		  isql_putchar (' ');
		}
	    }
	  return (rc);
	}
    }

  /* return(rc); *//* Never reached */

}

/* Returns return code from the last SQLGetData done. */
int
print_blob_col_csv (HSTMT stmt, UWORD n_col, SQLULEN width, SWORD sql_type)
{
  static TCHAR blob_buffer[BLOB_BUFFER_SIZE + 1];
  TCHAR *ch;
  int got_n_bytes;
  SQLULEN total = 0;
  int rc;

  for (;;)
    {
      SQLLEN n_recv;
      rc = SQLGetData (stmt, n_col,
                       SQL_C_TCHAR,
                       blob_buffer, BLOB_BUFFER_SIZE, &n_recv);
      /* Here we got either SQL_NO_DATA or an error. */
      if ((rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
          || n_recv == SQL_NULL_DATA)
        { /* This ^ means really a blob of length 0, not real NULL ??? */
          /* Tell calling function that everything is still all right, no panic: */
          if (SQL_NO_DATA_FOUND == rc)
            rc = SQL_SUCCESS;
          break;
        }

      /* If we get SQL_NO_TOTAL, then we have to use isqlt_tcslen. This doesn't
         work with true binary data with null bytes in the last part. */
      got_n_bytes = (int)
        ((rc == SQL_SUCCESS) ? ((n_recv != SQL_NO_TOTAL) ? n_recv
                                : isqlt_tcslen (blob_buffer))
         : BLOB_BUFFER_SIZE - 1);       /* Not the last part. */
      total += got_n_bytes;

      for (ch = blob_buffer; *ch; ++ch)
        {
          if (!isprint(*ch) || *ch == (TCHAR)('%') || *ch == (TCHAR)('\n') || *ch == (TCHAR)('\r') || *ch == *csv_field_separator)
            isqlt_tprintf (_T("%%%2.2hX"), (short)*ch);
          else
            isqlt_puttchar (*ch);
        }

      if (rc == SQL_SUCCESS)    /* No more data after this one. */
          break;
    }

  if (n_col < n_out_cols)  /* not the rightmost column? */
    isqlt_fputts (csv_field_separator, stdout);

  return rc;
}

int
print_blob_col_csv_rfc4180 (HSTMT stmt, UWORD n_col, SQLULEN width, SWORD sql_type)
{
  static TCHAR blob_buffer[BLOB_BUFFER_SIZE + 1];
  TCHAR *ch;
  int got_n_bytes;
  SQLULEN total = 0;
  int rc;

  for (;;)
    {
      SQLLEN n_recv;
      rc = SQLGetData (stmt, n_col,
                       SQL_C_TCHAR,
                       blob_buffer, BLOB_BUFFER_SIZE, &n_recv);
      /* Here we got either SQL_NO_DATA or an error. */
      if ((rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
          || n_recv == SQL_NULL_DATA)
        { /* This ^ means really a blob of length 0, not real NULL ??? */
          /* Tell calling function that everything is still all right, no panic: */
          if (SQL_NO_DATA_FOUND == rc)
            rc = SQL_SUCCESS;
          break;
        }

      /* If we get SQL_NO_TOTAL, then we have to use isqlt_tcslen. This doesn't
         work with true binary data with null bytes in the last part. */
      got_n_bytes = (int)
        ((rc == SQL_SUCCESS) ? ((n_recv != SQL_NO_TOTAL) ? n_recv
                                : isqlt_tcslen (blob_buffer))
         : BLOB_BUFFER_SIZE - 1);       /* Not the last part. */
      total += got_n_bytes;

      isqlt_puttchar ('"');
      for (ch = blob_buffer; *ch; ++ch)
        {
          if (!isprint(*ch))
            isqlt_tprintf (_T("%%%2.2hX"), (short)*ch);
	  else if (*ch == (TCHAR)('"'))
	    isqlt_tprintf (_T("\"\""));
          else
            isqlt_puttchar (*ch);
        }
      isqlt_puttchar ('"');

      if (rc == SQL_SUCCESS)    /* No more data after this one. */
          break;
    }

  if (n_col < n_out_cols)  /* not the rightmost column? */
    isqlt_fputts (csv_rfc4180_field_separator, stdout);

  return rc;
}

int
print_blob_col_json (HSTMT stmt, UWORD n_col, SQLULEN width, SWORD sql_type)
{
  static TCHAR blob_buffer[BLOB_BUFFER_SIZE + 1];
  TCHAR *ch;
  int got_n_bytes;
  SQLULEN total = 0;
  int rc;

  for (;;)
    {
      SQLLEN n_recv;
      rc = SQLGetData (stmt, n_col, SQL_C_TCHAR, blob_buffer, BLOB_BUFFER_SIZE, &n_recv);
      /* Here we got either SQL_NO_DATA or an error. */
      if ((rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) || n_recv == SQL_NULL_DATA)
	{			/* This ^ means really a blob of length 0, not real NULL ??? */
	  /* Tell calling function that everything is still all right, no panic: */
	  if (SQL_NO_DATA_FOUND == rc)
	    rc = SQL_SUCCESS;
	  break;
	}

      /* If we get SQL_NO_TOTAL, then we have to use isqlt_tcslen. This doesn't
         work with true binary data with null bytes in the last part. */
      got_n_bytes = (int) ((rc == SQL_SUCCESS) ? ((n_recv != SQL_NO_TOTAL) ? n_recv : isqlt_tcslen (blob_buffer)) : BLOB_BUFFER_SIZE - 1);	/* Not the last part. */
      total += got_n_bytes;

      isqlt_puttchar ('"');
      for (ch = blob_buffer; *ch; ++ch)
	{
	  if (!isprint (*ch))
	    isqlt_tprintf (_T ("%%%2.2hX"), (short) *ch);
	  else if (*ch == (TCHAR) ('"'))
	    isqlt_tprintf (_T ("\"\""));
	  else
	    isqlt_puttchar (*ch);
	}
      isqlt_puttchar ('"');

      if (rc == SQL_SUCCESS)	/* No more data after this one. */
	break;
    }

  return rc;
}

void
print_datetime_col (TCHAR *timebinstr, SQLULEN width, int rightp,
		    SQLLEN collen, int type, int inx)
{
  TCHAR temp[121];

  if (times_conform_to_odbc_flag)
    {
      if (type == SQL_DATE)
	{
	  /* Defined in /odbcsdk/include/sqlext.h */
	  DATE_STRUCT *ts = ((DATE_STRUCT *) timebinstr);
	  isqlt_stprintf (temp, _T("%d.%d.%d"), ts->year, ts->month, ts->day);
	}
      else if (type == SQL_TIMESTAMP)
	{
	  /* Defined in /odbcsdk/include/sqlext.h */
	  TIMESTAMP_STRUCT *ts = ((TIMESTAMP_STRUCT *) timebinstr);
	  isqlt_stprintf (temp, _T("%d.%d.%d %d:%d.%d %ld"),
		   ts->year, ts->month, ts->day,
		   ts->hour, ts->minute, ts->second,
		   (long) ts->fraction);
	}
      else if (type == SQL_TIME)
	{
	  /* Defined in /odbcsdk/include/sqlext.h */
	  TIME_STRUCT *ts = ((TIME_STRUCT *) timebinstr);

	  isqlt_stprintf (temp, _T("%d:%d.%d"), ts->hour, ts->minute, ts->second);
	}
      else
	/* Shouldn't happen! */
	{
	  isqlt_stprintf (temp,
		   _T("SHOULD NOT HAPPEN: print_date_col called(width=%lu,collen=%ld,type=%d)"),
		   (unsigned long) width, (long) collen, type);
	}
    }
  else
    /* Native KUBL mode. */
    {
      struct tm *tm;
      struct timeval tv;
      time_t tth;

      memcpy (&tv, timebinstr, sizeof (struct timeval));
      TV_TO_STRING (&tv);

      tth = (time_t) tv.tv_sec;
      tm = localtime (&tth);
      if (tm)
	{
	  isqlt_stprintf (temp, _T("%d.%d.%d %d:%d.%d %ld"),
		   tm->tm_year + 1900, tm->tm_mon + 1,
		   tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
		   (long) tv.tv_usec);
	}
      else
	/* localtime returned NULL pointer. */
	{
	  isqlt_stprintf (temp, _T("INV-TIME(%ld):%08lx:%08lx"),
		   collen, (long) tv.tv_sec, (long) tv.tv_usec);
	}
    }

  field_print (temp, width, rightp, inx);
}


/* Returns either SQL_SUCCESS or the last return code returned by
   print_blob_col (which calls SQLGetData in the loop.) */
int
print_row ()
{
  TCHAR temp[30];
  int inx, i;
  int rc = SQL_SUCCESS;

  for (inx = 0; inx < n_out_cols; inx++)
    {
      int rightp = 0;
      int tp = out_cols[inx].o_type;
      if (tp == SQL_NUMERIC || tp == SQL_FLOAT || tp == SQL_DOUBLE
	  || tp == SQL_REAL)
	rightp = 1;
      if (out_cols[inx].o_col_len == SQL_NULL_DATA)
	field_print (_T("NULL"), out_cols[inx].o_width, rightp, inx);
      else if ((out_cols[inx].o_type == SQL_TIMESTAMP) ||
	       (out_cols[inx].o_type == SQL_DATE) ||
	       (out_cols[inx].o_type == SQL_TIME))
	{
	  print_datetime_col ((TCHAR *) out_cols[inx].o_buffer, out_cols[inx].o_width, 0,
			out_cols[inx].o_col_len, out_cols[inx].o_type, inx);
	}
      else if (out_cols[inx].o_type == SQL_LONGVARCHAR ||
	       out_cols[inx].o_type == SQL_LONGVARBINARY ||
	       out_cols[inx].o_type == SQL_WLONGVARCHAR)
	{
	  if (print_blobs_flag)
	    {
	      /* Note zero-based indexing here. print_blob_col needs one+ */
	      if ((rc = print_blob_col (stmt, ((UWORD) (inx + 1)),
					out_cols[inx].o_width, out_cols[inx].o_type))
		  != SQL_SUCCESS)
		{
		  return (rc);
		}
	    }
	  else
	    {
	      if (out_cols[inx].o_col_len == SQL_NO_TOTAL)
		{
		  field_print (_T("BLOB SQL_NO_TOTAL"), (out_cols[inx].o_width), 0, inx);
		}
	      else if (out_cols[inx].o_col_len > 0 && out_cols[inx].o_col_len < 1024 &&
		  (out_cols[inx].o_type == SQL_LONGVARCHAR || out_cols[inx].o_type == SQL_WLONGVARCHAR))
		{
		  field_print ((TCHAR *) out_cols[inx].o_buffer, out_cols[inx].o_width,
		      rightp, inx);
		}
	      else
		{
		  if (out_cols[inx].o_type == SQL_WLONGVARCHAR)
		    isqlt_stprintf (temp, _T("NLOB %ld chars"), (long) (out_cols[inx].o_col_len / sizeof (TCHAR)));
		  else
		    isqlt_stprintf (temp, _T("BLOB %ld chars"), (long) (out_cols[inx].o_col_len / sizeof (TCHAR)));
		  field_print (temp, (out_cols[inx].o_width), 0, inx);
		}
	    }
	}
      else
	field_print ((TCHAR *) out_cols[inx].o_buffer, out_cols[inx].o_width, rightp, inx);
    }

  for (i = flag_newlines_at_eor; i; i--)
    {
      isql_printf (_T("\n"));
    }

  return (rc);
}

int
print_row_vert ()
{
  TCHAR temp[30];
  int inx, i;
  int rc = SQL_SUCCESS;
  unsigned max_col_name_width;

  max_col_name_width = 0;
  for (inx = 0; inx < n_out_cols; inx++)
    {
      max_col_name_width = MAX( max_col_name_width, (unsigned)isqlt_tcslen (out_cols[inx].o_title));
    }

  for (inx = 0; inx < n_out_cols; inx++)
    {
      int rightp = 0;
      int tp = out_cols[inx].o_type;
      if (tp == SQL_NUMERIC || tp == SQL_FLOAT || tp == SQL_DOUBLE || tp == SQL_REAL)
        rightp = 1;

      isql_printf (_T("%*") PCT_S _T(" : "), (int)max_col_name_width , (out_cols[inx].o_title ? out_cols[inx].o_title : (TCHAR *) _T("?")));

      //if (print_types_also)
      //  field_print (coltypetitles[inx], out_cols[inx].o_width, 0, inx);

      // ***************************************************************
      if (out_cols[inx].o_col_len == SQL_NULL_DATA)
        field_print (_T("NULL"), out_cols[inx].o_width, rightp, inx);
      else if ((out_cols[inx].o_type == SQL_TIMESTAMP) ||
               (out_cols[inx].o_type == SQL_DATE) ||
               (out_cols[inx].o_type == SQL_TIME))
        {
          print_datetime_col ((TCHAR *) out_cols[inx].o_buffer, out_cols[inx].o_width, 0,
                        out_cols[inx].o_col_len, out_cols[inx].o_type, inx);
        }
      else if (out_cols[inx].o_type == SQL_LONGVARCHAR ||
               out_cols[inx].o_type == SQL_LONGVARBINARY ||
               out_cols[inx].o_type == SQL_WLONGVARCHAR)
        {
          if (print_blobs_flag)
            {
              /* Note zero-based indexing here. print_blob_col needs one+ */
              rc = print_blob_col (stmt, ((UWORD) (inx + 1)), out_cols[inx].o_width, out_cols[inx].o_type);
              if (rc != SQL_SUCCESS)
                {
                  return (rc);
                }
            }
          else
            {
              if (out_cols[inx].o_col_len == SQL_NO_TOTAL)
                {
                  field_print (_T("BLOB SQL_NO_TOTAL"), (out_cols[inx].o_width), 0, inx);
                }
              else if (out_cols[inx].o_col_len > 0 && out_cols[inx].o_col_len < 1024 &&
                  (out_cols[inx].o_type == SQL_LONGVARCHAR || out_cols[inx].o_type == SQL_WLONGVARCHAR))
                {
                  field_print ((TCHAR *) out_cols[inx].o_buffer, out_cols[inx].o_width,
                      rightp, inx);
                }
              else
                {
                  if (out_cols[inx].o_type == SQL_WLONGVARCHAR)
                    isqlt_stprintf (temp, _T("NLOB %ld chars"), (long) (out_cols[inx].o_col_len / sizeof (TCHAR)));
                  else
                    isqlt_stprintf (temp, _T("BLOB %ld chars"), (long) (out_cols[inx].o_col_len / sizeof (TCHAR)));
                  field_print (temp, (out_cols[inx].o_width), 0, inx);
                }
            }
        }
      else
        field_print ((TCHAR *) out_cols[inx].o_buffer, out_cols[inx].o_width, rightp, inx);

      isql_printf (_T("\n"));
    }

  if (flag_newlines_at_eor)
    {
      isql_printf (_T("\n====\n"));
    }

  return (rc);
}

void
print_csv_banner ()
{
  int inx;
  for (inx = 0; inx < n_out_cols; inx++)
    {
      isqlt_tprintf (_T("%") PCT_S _T("%") PCT_S,
                   out_cols[inx].o_title ? out_cols[inx].o_title : (TCHAR *) _T(""),
                   (inx < n_out_cols -1) ? csv_field_separator : csv_row_separator );
    }
}

void
print_csv_rfc4180_banner ()
{
  int inx;
  for (inx = 0; inx < n_out_cols; inx++)
    {
      isqlt_tprintf (_T("\"%") PCT_S _T("\"%") PCT_S,
                   out_cols[inx].o_title ? out_cols[inx].o_title : (TCHAR *) _T(""),
                   (inx < n_out_cols -1) ? csv_rfc4180_field_separator : csv_rfc4180_row_separator );
    }
}


void
print_json_banner (void)
{
  isqlt_fputts (_T ("[ {\n"), stdout);
}


void
print_json_footer (void)
{
  isqlt_fputts (_T ("\n} ]\n"), stdout);
}


void
print_datetime_col_csv ( TCHAR *data, SQLULEN width, SQLLEN collen, int type, int inx, int n_cols)
{
  if (type == SQL_DATE)
    {
      /* Defined in /odbcsdk/include/sqlext.h */
      DATE_STRUCT *ts = (DATE_STRUCT *) data;
      isqlt_tprintf (_T("%d.%d.%d"), ts->year, ts->month, ts->day);
    }
  else if (type == SQL_TIME)
    {
      /* Defined in /odbcsdk/include/sqlext.h */
      TIME_STRUCT *ts = (TIME_STRUCT *) data;

      isqlt_tprintf (_T("%d:%d.%d"), ts->hour, ts->minute, ts->second);
    }
  else if (type == SQL_TIMESTAMP)
    {
      /* Defined in /odbcsdk/include/sqlext.h */
      TIMESTAMP_STRUCT *ts = (TIMESTAMP_STRUCT *) data;
      isqlt_tprintf (_T("%d.%d.%d %d:%d:%d.%ld"), ts->year, ts->month, ts->day, ts->hour, ts->minute, ts->second, (long) ts->fraction);
    }
  if (inx < n_cols - 1)  /* not the rightmost column? */
    isqlt_fputts (csv_field_separator, stdout);
}

void
print_datetime_col_csv_rfc4180 ( TCHAR *data, SQLULEN width, SQLLEN collen, int type, int inx, int n_cols)
{
  if (type == SQL_DATE)
    {
      /* Defined in /odbcsdk/include/sqlext.h */
      DATE_STRUCT *ts = (DATE_STRUCT *) data;
      isqlt_tprintf (_T("\"%d.%d.%d\""), ts->year, ts->month, ts->day);
    }
  else if (type == SQL_TIME)
    {
      /* Defined in /odbcsdk/include/sqlext.h */
      TIME_STRUCT *ts = (TIME_STRUCT *) data;

      isqlt_tprintf (_T("\"%d:%d.%d\""), ts->hour, ts->minute, ts->second);
    }
  else if (type == SQL_TIMESTAMP)
    {
      /* Defined in /odbcsdk/include/sqlext.h */
      TIMESTAMP_STRUCT *ts = (TIMESTAMP_STRUCT *) data;
      isqlt_tprintf (_T("\"%d.%d.%d %d:%d:%d.%ld\""), ts->year, ts->month, ts->day, ts->hour, ts->minute, ts->second, (long) ts->fraction);
    }
  if (inx < n_cols - 1)  /* not the rightmost column? */
    isqlt_fputts (csv_rfc4180_field_separator, stdout);
}


void
print_datetime_col_json (TCHAR * data, SQLULEN width, SQLLEN collen, int type, int inx, int n_cols)
{
  if (type == SQL_DATE)
    {
      /* Defined in /odbcsdk/include/sqlext.h */
      DATE_STRUCT *ts = (DATE_STRUCT *) data;
      isqlt_tprintf (_T ("\"%04d-%02d-%02d\""), ts->year, ts->month, ts->day);
    }
  else if (type == SQL_TIME)
    {
      /* Defined in /odbcsdk/include/sqlext.h */
      TIME_STRUCT *ts = (TIME_STRUCT *) data;

      isqlt_tprintf (_T ("\"%02d:%02d.%02d\""), ts->hour, ts->minute, ts->second);
    }
  else if (type == SQL_TIMESTAMP)
    {
      /* Defined in /odbcsdk/include/sqlext.h */
      TIMESTAMP_STRUCT *ts = (TIMESTAMP_STRUCT *) data;
      isqlt_tprintf (_T ("\"%04d-%02d-%02dT%02d:%02d:%02d.%ldZ\""), ts->year, ts->month, ts->day, ts->hour, ts->minute, ts->second,
	  (long) ts->fraction);
    }
}


/* Returns either SQL_SUCCESS or the last return code returned by
   print_blob_col (which calls SQLGetData in the loop.) */
int
print_csv_row()
{
  int inx;
  int rc = SQL_SUCCESS;

  for (inx = 0; inx < n_out_cols; inx++)
    {
      if (out_cols[inx].o_col_len == SQL_NULL_DATA)
        field_print_csv (_T(""), out_cols[inx].o_width, 0, inx);
      else if ((out_cols[inx].o_type == SQL_TIMESTAMP) ||
               (out_cols[inx].o_type == SQL_DATE) ||
               (out_cols[inx].o_type == SQL_TIME))
        {
          print_datetime_col_csv ((TCHAR *) out_cols[inx].o_buffer, out_cols[inx].o_width, out_cols[inx].o_col_len, out_cols[inx].o_type, inx, n_out_cols);
        }
      else if (out_cols[inx].o_type == SQL_LONGVARCHAR ||
               out_cols[inx].o_type == SQL_LONGVARBINARY ||
               out_cols[inx].o_type == SQL_WLONGVARCHAR)
        {
          /* Note zero-based indexing here. print_blob_col needs one+ */
          if ((rc = print_blob_col_csv (stmt, ((UWORD) (inx + 1)),
                                    out_cols[inx].o_width, out_cols[inx].o_type))
              != SQL_SUCCESS)
            {
              return (rc);
            }
        }
      else
        field_print_csv ((TCHAR *) out_cols[inx].o_buffer, out_cols[inx].o_width, 0, inx);
    }

  isqlt_fputts (csv_row_separator, stdout);

  return (rc);
}

int
print_csv_rfc4180_row()
{
  int inx;
  int rc = SQL_SUCCESS;

  for (inx = 0; inx < n_out_cols; inx++)
    {
      if (out_cols[inx].o_col_len == SQL_NULL_DATA)
        field_print_csv_rfc4180 (_T(""), out_cols[inx].o_width, 0, inx);
      else if ((out_cols[inx].o_type == SQL_TIMESTAMP) ||
               (out_cols[inx].o_type == SQL_DATE) ||
               (out_cols[inx].o_type == SQL_TIME))
        {
          print_datetime_col_csv_rfc4180 ((TCHAR *) out_cols[inx].o_buffer, out_cols[inx].o_width, out_cols[inx].o_col_len, out_cols[inx].o_type, inx, n_out_cols);
        }
      else if (out_cols[inx].o_type == SQL_LONGVARCHAR ||
               out_cols[inx].o_type == SQL_LONGVARBINARY ||
               out_cols[inx].o_type == SQL_WLONGVARCHAR)
        {
          /* Note zero-based indexing here. print_blob_col needs one+ */
          if ((rc = print_blob_col_csv_rfc4180 (stmt, ((UWORD) (inx + 1)),
                                    out_cols[inx].o_width, out_cols[inx].o_type))
              != SQL_SUCCESS)
            {
              return (rc);
            }
        }
      else
        field_print_csv_rfc4180 ((TCHAR *) out_cols[inx].o_buffer, out_cols[inx].o_width, 0, inx);
    }

  isqlt_fputts (csv_rfc4180_row_separator, stdout);

  return (rc);
}


/* Returns either SQL_SUCCESS or the last return code returned by
   print_blob_col (which calls SQLGetData in the loop.) */
int
print_json_row (int row_nr)
{
  int inx;
  int rc = SQL_SUCCESS;

  if (row_nr)
    isqlt_fputts (_T ("\n},{\n"), stdout);

  for (inx = 0; inx < n_out_cols; inx++)
    {
      TCHAR *o_buffer = (TCHAR *) out_cols[inx].o_buffer;
      SQLULEN o_width = out_cols[inx].o_width;
      SQLLEN o_col_len = out_cols[inx].o_col_len;
      int o_type = out_cols[inx].o_type;
      int use_quote = 1;

      isqlt_tprintf (_T ("    \"%") PCT_S _T ("\" : "), out_cols[inx].o_title ? out_cols[inx].o_title : (TCHAR *) _T (""));

      if (o_col_len == SQL_NULL_DATA)
	isqlt_fputts (_T ("null"), stdout);
      else
	switch (o_type)
	  {
	  case SQL_DATE:
	  case SQL_TIME:
	  case SQL_TIMESTAMP:
	    print_datetime_col_json (o_buffer, o_width, o_col_len, o_type, inx, n_out_cols);
	    break;

	  case SQL_LONGVARBINARY:
	  case SQL_LONGVARCHAR:
	  case SQL_WLONGVARCHAR:
	    /* Note zero-based indexing here. print_blob_col needs one+ */
	    if ((rc = print_blob_col_json (stmt, ((UWORD) (inx + 1)), o_width, o_type)) != SQL_SUCCESS)
	      return (rc);
	    break;

	  case SQL_DECIMAL:
	  case SQL_DOUBLE:
	  case SQL_FLOAT:
	  case SQL_INTEGER:
	  case SQL_NUMERIC:
	  case SQL_REAL:
	  case SQL_SMALLINT:
	    use_quote = 0;
	    /* FALLTRHOUGH */

	  default:
	    {
	      TCHAR *ch;

	      if (use_quote)
		isqlt_puttchar ('"');

	      for (ch = o_buffer; *ch; ++ch)
		{
		  if (!isprint (*ch))
		    isqlt_tprintf (_T ("%%%2.2hX"), (short) *ch);
		  else if (*ch == (TCHAR) ('"'))
		    isqlt_tprintf (_T ("\"\""));
		  else
		    isqlt_puttchar (*ch);
		}

	      if (use_quote)
		isqlt_puttchar ('"');
	    }
	    break;
	  }

      if (inx < n_out_cols - 1)
	isqlt_fputts (_T (",\n"), stdout);
    }

  return (rc);
}


/* If this is set to non-zero value, it will override the width values
   got with SQLDescribeCol. */
SQLULEN max_col_width = 0;

/* The following values MUST BE different from SQL_INVALID_HANDLE,
   SQL_ERROR, SQL_SUCCESS, SQL_SUCCESS_WITH_INFO, SQL_NO_DATA_FOUND,
   SQL_STILL_EXECUTING, SQL_NEED_DATA (All between -2 and 100)
 */
#define DO_SQL_API_COMMAND_IS_NOT_COMMAND   12345
#define DO_SQL_API_COMMAND_IS_NOT_AVAILABLE 12346

/* Pointer to SQL_API function returning RETCODE */
/* Was like this, but certain jerked header files don't recognize RETCODE
   typedef RETCODE (*SQL_API_FUNPTR)();
 */
/* So let's use this one: */
typedef signed short (*SQL_API_FUNPTR) (HSTMT, UTCHAR *, SWORD, UTCHAR *, SWORD,
					UTCHAR *, SWORD, UTCHAR *, SWORD);


struct sql_api_cmd
  {
    TCHAR *name;
    UWORD sql_api_num;
    SQL_API_FUNPTR fun;
    int str_args;		/* Number of string arguments. */
  };

struct sql_api_cmd some_sql_api_commands[] =
{
  {_T("Columns"), SQL_API_SQLCOLUMNS,
   ((SQL_API_FUNPTR) SQLColumns), 4},
  {_T("ColumnPrivileges"), SQL_API_SQLCOLUMNPRIVILEGES,
   ((SQL_API_FUNPTR) SQLColumnPrivileges), 4},
  {_T("PrimaryKeys"), SQL_API_SQLPRIMARYKEYS,
   ((SQL_API_FUNPTR) SQLPrimaryKeys), 3},
  {_T("ForeignKeys"), SQL_API_SQLFOREIGNKEYS,
   ((SQL_API_FUNPTR) SQLForeignKeys), 6},
  {_T("Procedures"), SQL_API_SQLPROCEDURES,
   ((SQL_API_FUNPTR) SQLProcedures), 3},
  {_T("ProcedureColumns"), SQL_API_SQLPROCEDURECOLUMNS,
   ((SQL_API_FUNPTR) SQLProcedureColumns), 4},
  {_T("TablePrivileges"), SQL_API_SQLTABLEPRIVILEGES,
   ((SQL_API_FUNPTR) SQLTablePrivileges), 3},
  {_T("Tables"), SQL_API_SQLTABLES,
   ((SQL_API_FUNPTR) SQLTables), 4},
  {NULL}			/* The end */
};
/*

   This should divide tablename (in case that tablequalifier and tableowner
   are NULL, i.e. not defined explicitly elsewhere) in the following manner:

   tablequalifier = NULL, tableowner = NULL, tablename = qual.own.name
   -> tablequalifier = qual, tableowner = own, tablename = name

   tablename = qual..name
   -> tablequalifier = qual, tableowner = NULL, tablename = name

   tablename = ..  or tablename = .
   -> tablequalifier = NULL, tableowner = NULL, tablename = NULL

   tablename = fool.baal.
   -> tablequalifier = fool, tableowner = baal, tablename = NULL

   tablename = fool.baal
   -> tablequalifier = NULL, tableowner = fool, tablename = baal

   tablename = qual..  -> tablequalifier = qual (others NULL)

   tablename = .own.  (or own.) -> tableowner = own (others NULL)

   tablename = ..name (or .name) -> tablename = name (others NULL)

   Returns the new tablename.
 */

UTCHAR *
divide_tablename (UTCHAR * tablename,
		  UTCHAR ** p_tablequalifier, UTCHAR ** p_tableowner)
{
  UTCHAR *ptr, *ptr2;

/* Has any effect only if tablequalifier and tableowner are not given
   elsewhere, i.e. they are still NULL. */
  if (tablename && NO * p_tablequalifier && NO * p_tableowner &&
      (ptr = (UTCHAR *) isqlt_tcschr (SCP (tablename), '.')))
    {
      if (ptr > (tablename))	/* Period not in the beginning of tablename? */
	{
	  *p_tablequalifier = tablename;
	  *ptr = '\0';
	}
      if (NULL != (ptr2 = (UTCHAR *) isqlt_tcschr ((TCHAR *) (ptr + 1), '.')))
	{
	  if (ptr2 > (ptr + 1))	/* Second period not right after first one? */
	    {
	      *p_tableowner = ptr + 1;
	      *ptr2 = '\0';
	    }
	  if (*(ptr2 + 1))
	    {
	      tablename = (ptr2 + 1);
	    }			/* Something following? */
	  else
	    {
	      tablename = NULL;
	    }			/* If nothing after second period. */
	}
      else
	/* If only one period found. */
	{
	  *p_tableowner = *p_tablequalifier;
	  *p_tablequalifier = NULL;
	  /* Check that it's not dangling in the end: */
	  if (*(ptr + 1))
	    {
	      tablename = ptr + 1;
	    }
	  else
	    {
	      tablename = NULL;
	    }
	}
    }

  return (tablename);
}



int
do_string_argument_sql_api_command (TCHAR *text,
				    TCHAR *command,
				    TCHAR *tablequalifier,
				    TCHAR *tableowner,
				    TCHAR *tablename,
				    TCHAR *usr2arg)
{
  UWORD ThisFunctionExists = TRUE;	/* If SQLGetFunctions does nothing. */
  int rc;
  struct sql_api_cmd *sacptr = some_sql_api_commands;

  while (sacptr->name)
    {
      if (0 == strcasecmp (command, sacptr->name))
	{
#if !(defined(WIN32) && defined(UDBC))
	  SQLGetFunctions (hdbc, sacptr->sql_api_num, &ThisFunctionExists);
#endif
	  if (ThisFunctionExists == FALSE)
	    {
	      isql_fprintf (error_stream,
			 _T("*** This ODBC driver doesn't implement SQL%") PCT_S _T(" !\n"),
			    sacptr->name);
	      return (DO_SQL_API_COMMAND_IS_NOT_AVAILABLE);
	    }

	  isql_printf (
			_T("Showing SQL%") PCT_S _T(" of tables like '%") PCT_S _T(".%") PCT_S _T(".%") PCT_S _T("', tabletype/colname like '%") PCT_S _T("'\n"),
			sacptr->name,
			(tablequalifier ? tablequalifier : _T("NULL")),
			(tableowner ? tableowner : _T("NULL")),
			(tablename ? tablename : _T("NULL")),
			(usr2arg ? usr2arg : _T("NULL")));

	  max_col_width = MAX_COL_WIDTH;

	  /* All qualifiers, all owners. */
	  switch (sacptr->sql_api_num)
	    {
	       case SQL_API_SQLTABLES:
		     {
		       rc = SQLTables (stmt, UCP (tablequalifier), SQL_NTS, UCP (tableowner),
			    SQL_NTS, UCP (tablename), SQL_NTS, UCP (usr2arg), SQL_NTS);
		       break;
		     }
	       case SQL_API_SQLCOLUMNS:
		     {
		       rc = SQLColumns (stmt, UCP (tablequalifier), SQL_NTS, UCP (tableowner),
			    SQL_NTS, UCP (tablename), SQL_NTS, UCP (usr2arg), SQL_NTS);
		       break;
		     }
	       case SQL_API_SQLCOLUMNPRIVILEGES:
		     {
		       rc = SQLColumnPrivileges (stmt, UCP (tablequalifier), SQL_NTS, UCP (tableowner),
			    SQL_NTS, UCP (tablename), SQL_NTS, UCP (usr2arg), SQL_NTS);
		       break;
		     }
	       case SQL_API_SQLPRIMARYKEYS:
		     {
		       rc = SQLPrimaryKeys (stmt, UCP (tablequalifier), SQL_NTS, UCP (tableowner),
			    SQL_NTS, UCP (tablename), SQL_NTS);
		       break;
		     }
	       case SQL_API_SQLPROCEDURES:
		     {
		       rc = SQLProcedures (stmt, UCP (tablequalifier), SQL_NTS, UCP (tableowner),
			    SQL_NTS, UCP (tablename), SQL_NTS);
		       break;
		     }
	       case SQL_API_SQLFOREIGNKEYS:
		     {
		       TCHAR *fk_table_qualifier = NULL, *fk_table_owner = NULL, *fk_table_name = NULL;

		       if (usr2arg)
			 fk_table_name = (TCHAR *) divide_tablename (((UTCHAR *) usr2arg),
			     ((UTCHAR **) & fk_table_qualifier),
			     ((UTCHAR **) & fk_table_owner));
		       rc = SQLForeignKeys (stmt,
			   UCP (tablequalifier), SQL_NTS, UCP (tableowner), SQL_NTS, UCP (tablename), SQL_NTS,
			   UCP (fk_table_qualifier), SQL_NTS, UCP (fk_table_owner), SQL_NTS, UCP (fk_table_name), SQL_NTS
			   );
		       break;
		     }
	       case SQL_API_SQLPROCEDURECOLUMNS:
		     {
		       rc = SQLProcedureColumns (stmt, UCP (tablequalifier), SQL_NTS, UCP (tableowner),
			    SQL_NTS, UCP (tablename), SQL_NTS, UCP (usr2arg), SQL_NTS);
		       break;
		     }
	       case SQL_API_SQLTABLEPRIVILEGES:
		     {
		       rc = SQLTablePrivileges (stmt, UCP (tablequalifier), SQL_NTS, UCP (tableowner),
			    SQL_NTS, UCP (tablename), SQL_NTS);
		       break;
		     }
	       default:
		  rc = (sacptr->fun) (stmt, UCP (tablequalifier), SQL_NTS, UCP (tableowner), SQL_NTS,
		                     UCP (tablename), SQL_NTS, UCP (usr2arg), SQL_NTS);
	    }

	  return (rc);
	}			/* if found the name. */
      sacptr++;			/* Otherwise check the next. */
    }				/* while(sacptr->name); */

  return (DO_SQL_API_COMMAND_IS_NOT_COMMAND);	/* Was not found from these. */
}


#define GETTYPEINFO_COMMAND	_T("GetTypeInfo")
#define STATS_COMMAND		_T("Statistics")
#define SPECCOL_COMMAND		_T("SpecialColumns")
#define QUALIFIERS_COMMAND	_T("TableQualifiers")
#define OWNERS_COMMAND		_T("TableOwners")
#define TYPES_COMMAND		_T("TableTypes")


int
do_sql_api_command (TCHAR *text)
{
  TCHAR *command;
  TCHAR *tablename;
  TCHAR *tablequalifier = NULL;
  TCHAR *tableowner = NULL;
  TCHAR *opt = NULL;
  TCHAR *ptr = text;
  TCHAR saveslash = 0, *savepoint = NULL;
  TCHAR tmp1buf[TMPBUF_SIZE + 2], tmp2buf[TMPBUF_SIZE + 2];
  TCHAR tmp3buf[TMPBUF_SIZE + 2];
  int rc;

  if (NULL != (opt = isqlt_tcsrchr (text, '/')))	/* Option or 2nd arg? Check the last slash */
    {
      saveslash = *opt;
      savepoint = opt;
      *opt++ = '\0';		/* Cut it from the slash and skip to next. */
/* Was: opt = skip_blankos(opt); */
/* Now use get_next_token instead so that $-macros will work also
   with options of SQL-API functions. But note that now we have to use
   TABLES/'SYSTEM TABLE' instead of just TABLES/SYSTEM TABLE
   if we are connected to non-Kubl database. (Kubl's driver allows
   just a SYSTEM as a tabletype).
   I hope the return value setting is really done after the screwing
   of opt pointer inside the function itself: */
      opt = get_next_token (&opt, tmp3buf, TMPBUF_SIZE, NULL, 0);
    }
  command = get_next_token (&ptr, tmp1buf, TMPBUF_SIZE, NULL, 0);

  if (NO command)
    {
      if (savepoint)
	{
	  *savepoint = saveslash;
	}
      return (DO_SQL_API_COMMAND_IS_NOT_COMMAND);
    }

  tablename = get_next_token (&ptr, tmp2buf, TMPBUF_SIZE, NULL, 0);
  tablename = SCP (divide_tablename (((UTCHAR *) tablename),
				     ((UTCHAR **) & tablequalifier),
				     ((UTCHAR **) & tableowner)));


  rc = do_string_argument_sql_api_command (text, command,
					   tablequalifier, tableowner,
					   tablename, opt);
  if (DO_SQL_API_COMMAND_IS_NOT_COMMAND != rc)
    {
      if (savepoint)
	{
	  *savepoint = saveslash;
	}
      return (rc);
    }

/* Else, check some special cases with strange arguments, if the text was
   not found from the standard list. */
  else if (!strcasecmp (command, GETTYPEINFO_COMMAND))
    {
      UWORD ThisFunctionExists = TRUE;

#if !(defined(WIN32) && defined(UDBC))
      SQLGetFunctions (hdbc, SQL_API_SQLGETTYPEINFO, &ThisFunctionExists);
#endif
      if (ThisFunctionExists == FALSE)
	{
	  if (savepoint)
	    {
	      *savepoint = saveslash;
	    }
	  isql_fprintf (error_stream,
	       _T("*** This ODBC driver doesn't implement SQLGetTypeInfo !\n"));
	  return (DO_SQL_API_COMMAND_IS_NOT_AVAILABLE);
	}

      rc = SQLGetTypeInfo (stmt, SQL_ALL_TYPES);
      max_col_width = MAX_COL_WIDTH;
    }

  else if (!strcasecmp (command, STATS_COMMAND))
    {
      UWORD ThisFunctionExists = TRUE;

#if !(defined(WIN32) && defined(UDBC))
      SQLGetFunctions (hdbc, SQL_API_SQLSTATISTICS, &ThisFunctionExists);
#endif
      if (ThisFunctionExists == FALSE)
	{
	  isql_fprintf (error_stream,
		_T("*** This ODBC driver doesn't implement SQLStatistics !\n"));
	  if (savepoint)
	    {
	      *savepoint = saveslash;
	    }
	  return (DO_SQL_API_COMMAND_IS_NOT_AVAILABLE);
	}


      isql_printf (_T("Showing SQLStatistics of table(s) '%") PCT_S _T(".%") PCT_S _T(".%") PCT_S _T("'\n"),
		   (tablequalifier ? tablequalifier : _T("NULL")),
		   (tableowner ? tableowner : _T("NULL")),
		   (tablename ? tablename : _T("NULL")));

      rc = SQLStatistics (stmt,
			  UCP (tablequalifier), ((SWORD) SQL_NTS),
			  UCP (tableowner), ((SWORD) SQL_NTS),
			  UCP (tablename), ((SWORD) SQL_NTS),
			  (UWORD) ((opt && isascii (*(opt)) &&
				    (toupper (*(opt)) == 'U'))
				   ? ((UWORD) SQL_INDEX_UNIQUE)
				   : ((UWORD) SQL_INDEX_ALL)),
			  ((UWORD) SQL_ENSURE));
      max_col_width = MAX_COL_WIDTH;
    }

  else if (!strcasecmp (command, SPECCOL_COMMAND))
    {
      UWORD ThisFunctionExists = TRUE;

#if !(defined(WIN32) && defined(UDBC))
      SQLGetFunctions (hdbc, SQL_API_SQLSPECIALCOLUMNS, &ThisFunctionExists);
#endif
      if (ThisFunctionExists == FALSE)
	{
	  isql_fprintf (error_stream,
	    _T("*** This ODBC driver doesn't implement SQLSpecialColumns !\n"));
	  if (savepoint)
	    {
	      *savepoint = saveslash;
	    }
	  return (DO_SQL_API_COMMAND_IS_NOT_AVAILABLE);
	}


      isql_printf (_T("Showing SQLSpecialColumns of table(s) '%") PCT_S _T(".%") PCT_S _T(".%") PCT_S _T("'\n"),
		   (tablequalifier ? tablequalifier : _T("NULL")),
		   (tableowner ? tableowner : _T("NULL")),
		   (tablename ? tablename : _T("NULL")));

      rc = SQLSpecialColumns (stmt,
			      ((UWORD) ((opt &&
				   (isqlt_tcschr (opt, 'B') || isqlt_tcschr (opt, 'b')))
					? ((UWORD) SQL_BEST_ROWID)
					: ((UWORD) SQL_ROWVER))),
			      UCP (tablequalifier), ((SWORD) SQL_NTS),
			      UCP (tableowner), ((SWORD) SQL_NTS),
			      UCP (tablename), ((SWORD) SQL_NTS),
			      ((UWORD)
			  ((opt && (isqlt_tcschr (opt, 'S') || isqlt_tcschr (opt, 's')))
			   ? ((UWORD) SQL_SCOPE_SESSION)
			: ((opt && (isqlt_tcschr (opt, 'T') || isqlt_tcschr (opt, 't')))
			   ? ((UWORD) SQL_SCOPE_TRANSACTION)
			   : ((UWORD) SQL_SCOPE_CURROW)))),
			      ((UWORD) ((opt &&
				   (isqlt_tcschr (opt, 'N') || isqlt_tcschr (opt, 'n')))
					? ((UWORD) SQL_NO_NULLS)
					: ((UWORD) SQL_NULLABLE))));
      max_col_width = MAX_COL_WIDTH;
    }

  else if (!strcasecmp (command, QUALIFIERS_COMMAND))
    {
      UWORD ThisFunctionExists = TRUE;

#if !(defined(WIN32) && defined(UDBC))
      SQLGetFunctions (hdbc, SQL_API_SQLTABLES, &ThisFunctionExists);
#endif
      if (ThisFunctionExists == FALSE)
	{
	  if (savepoint)
	    {
	      *savepoint = saveslash;
	    }
	  isql_fprintf (error_stream, _T ("*** This ODBC driver doesn't implement SQLTABLES !\n"));
	  return (DO_SQL_API_COMMAND_IS_NOT_AVAILABLE);
	}

      isql_printf (_T ("Showing SQLTables(QUALIFIERS)\n"));
      rc = SQLTables (stmt, UCP ("%"), SQL_NTS, NULL, 0, NULL, 0, NULL, 0);
      max_col_width = MAX_COL_WIDTH;
    }

  else if (!strcasecmp (command, OWNERS_COMMAND))
    {
      UWORD ThisFunctionExists = TRUE;

#if !(defined(WIN32) && defined(UDBC))
      SQLGetFunctions (hdbc, SQL_API_SQLTABLES, &ThisFunctionExists);
#endif
      if (ThisFunctionExists == FALSE)
	{
	  if (savepoint)
	    {
	      *savepoint = saveslash;
	    }
	  isql_fprintf (error_stream, _T ("*** This ODBC driver doesn't implement SQLTABLES !\n"));
	  return (DO_SQL_API_COMMAND_IS_NOT_AVAILABLE);
	}

      isql_printf (_T ("Showing SQLTables(OWNERS)\n"));
      rc = SQLTables (stmt, NULL, 0, UCP ("%"), SQL_NTS, NULL, 0, NULL, 0);
      max_col_width = MAX_COL_WIDTH;
    }

  else if (!strcasecmp (command, TYPES_COMMAND))
    {
      UWORD ThisFunctionExists = TRUE;

#if !(defined(WIN32) && defined(UDBC))
      SQLGetFunctions (hdbc, SQL_API_SQLTABLES, &ThisFunctionExists);
#endif
      if (ThisFunctionExists == FALSE)
	{
	  if (savepoint)
	    {
	      *savepoint = saveslash;
	    }
	  isql_fprintf (error_stream, _T ("*** This ODBC driver doesn't implement SQLTABLES !\n"));
	  return (DO_SQL_API_COMMAND_IS_NOT_AVAILABLE);
	}

      isql_printf (_T ("Showing SQLTables(TABLETYPES)\n"));
      rc = SQLTables (stmt, NULL, 0, NULL, 0, NULL, 0, UCP ("%"), SQL_NTS);
      max_col_width = MAX_COL_WIDTH;
    }

  else
    {
      if (savepoint)
	{
	  *savepoint = saveslash;
	}
      return (DO_SQL_API_COMMAND_IS_NOT_COMMAND);
    }

  /* Otherwise inserting string literals with slashes wouldn't work: */
  if (savepoint)
    {
      *savepoint = saveslash;
    }
  return (rc);
}


#ifdef OLD_AND_OBSOLETE_KEPT_HERE_JUST_FOR_REFERENCE
/* Now merged with show_results, a function nipped from old exec_one */

void
isql_procedure_results (int rc, long start)
{
  int nth_set = 1;
  UWORD inx;
  SWORD btype;
  start = start;		/* To avoid warning messages. */
  for (;;)
    {
      int n_rows = 0;
      if (rc == SQL_NO_DATA_FOUND)
	{
	  isql_printf (_T("Set %") PCT_S _T(" No rows.\n"), nth_set);
	}
      else
	{
	  SQLNumResultCols (stmt, &n_out_cols);

	  for (inx = 1; inx <= n_out_cols; inx++)
	    {
	      stmt_out_t *out = &out_cols[inx - 1];
	      SQLDescribeCol (stmt, inx, out->o_title, MAXCOLNAME, NULL,
			      &out->o_type, &out->o_width,
			      NULL, NULL);

	      out->o_width = 12;	/* ???? */
/* Was:
   if (out -> o_buffer) free (out -> o_buffer);
   out -> o_buffer = malloc (out -> o_width + 1);
 */
	      if (!out->o_buffer)
		{
		  out->o_buffer = chemalloc (MAXBUFFER + 2, _T("obsolete function"));
		}


	      switch (out->o_type)
		{
		case SQL_TIMESTAMP: case SQL_DATE:
		  btype = SQL_C_TIMESTAMP;
		  break;
		case SQL_WCHAR: case SQL_WVARCHAR: case SQL_WLONGVARCHAR:
		  btype = SQL_C_TCHAR;
		  break;
	        default:
		  btype = SQL_C_TCHAR;
		}

	      SQLBindCol (stmt, inx, btype, out->o_buffer,
			  MAXBUFFER, &out->o_col_len);

	    }

	  if (!print_banner_flag)
	    {
	      print_banner ();
	    }
	  for (;;)
	    {
	      rc = SQLFetch (stmt);
	      if (rc == SQL_NO_DATA_FOUND)
		break;
	      IF_ERR_GO (stmt, error, rc);
	      n_rows++;
	      rc = print_row ();
	      IF_ERR_GO (stmt, error, rc);
	    }
	  isql_printf (_T("\nSet %d, %d Rows,.\n"), nth_set, n_rows);
	}

      rc = SQLMoreResults (stmt);
      if (rc == SQL_NO_DATA_FOUND)
	break;
      nth_set++;
    }
  isql_printf (_T("%d sets, %ld msec.\n"), nth_set, get_msec_count () - start);

  if (commit_mode != 2)
    {
      RETCODE rc;
      SQLSetConnectOption (hdbc, SQL_AUTOCOMMIT, commit_mode);
      if (SQL_SUCCESS != SQLTransact (henv, hdbc, SQL_COMMIT))
	{
	  print_error (((HENV) 0), hdbc, ((HSTMT) 0));
	}
    }
  return;
error:
  SQLTransact (henv, hdbc, SQL_ROLLBACK);
}

#endif

static void * debug_session = NULL;

int is_cmd (TCHAR *text, TCHAR *cmd);

#ifdef ODBC_ONLY
#undef PLDBG
#endif

#ifdef PLDBG
void * pldbg_help (FILE * f);
TCHAR * pldbg_read_resp (void * ses1);
int pldbg_command (void * ses1, TCHAR * cmd1);
void * pldbg_connect (TCHAR * addr, TCHAR * usr, TCHAR * pwd1);
TCHAR pld_last_cmd[TMPBUF_SIZE + 2] = _T("");

int
debug_command (TCHAR *text)
{
  caddr_t resp;
  TCHAR *args, *cmd;
  TCHAR tmp0buf[TMPBUF_SIZE + 2];

  text = skip_blankos (text);
  args = text;

  cmd = get_next_token (&args, tmp0buf, TMPBUF_SIZE, NULL, 0);
  if (NULL == cmd)
    {
      if (pld_last_cmd[0] != 0)
	{
	  args = pld_last_cmd;
	  text = pld_last_cmd;
	  cmd = get_next_token (&args, tmp0buf, TMPBUF_SIZE, NULL, 0);
	}
      else
	return (1);
    }				/* Empty commands are NO-OPs */
  else
    isqlt_tcsncpy (pld_last_cmd, text, sizeof (pld_last_cmd));

  if (is_cmd (cmd, _T("QUIT")) || is_cmd (cmd, _T("EXIT")))
      isql_exit (0);
  else if (is_cmd (cmd, _T("HELP")))
    {
      pldbg_help (stdout);
      return (1);
    }

  if (!debug_session)
    {
      isql_fprintf (error_stream, _T("Can't estabilish debug session to the '%") PCT_S _T("'\n"), datasource);
      isql_exit (1);
    }

  if (!pldbg_command (debug_session, text))
    {
      isql_fprintf (error_stream, _T("Debug session to the '%") PCT_S _T("' is broken\n"), datasource);
      isql_exit (1);
    }
  resp = pldbg_read_resp (debug_session);
  if (resp)
    isql_printf (_T("%") PCT_S _T("\n"), resp);
  else
    {
      isql_fprintf (error_stream, _T("Debug session to the '%") PCT_S _T("' is broken\n"), datasource);
      isql_exit (1);
    }
  dk_free_tree (resp);
  return 1;
}
#endif

int show_results (long start, int rc, int nth_set, int loc_print_banner_flag, int loc_verbose_mode);
int is_isql_command (TCHAR *text, integer_list * pidlist);
int connect_to_datasource (TCHAR *datasource, TCHAR *username, TCHAR *password);

typedef int (*_func_t) (long start, int rc, int nth_set, int loc_print_banner_flag, int loc_verbose_mode);



void
exec_one (TCHAR *text, integer_list * pidlist)
{
  long start;
  int rc;
  int is_call_statement = 0;
  TCHAR *main_text;

  /* Currently this is only for making sure that there is enough space
     in statement text buffer for function
     parse_statement_for_special_parameters
     to do its destructive modifications (in case there are $dollar_forms
     to be substituted that produce a longer string than the dollar form
     itself). parse_statement_for_special_parameters is called by
     exec_for_foreach, called by isql_foreach, called by is_isql_command.
     Now it is called from here as well, immediately after we know
     that it's neither isql-command nor API-command, but a real
     SQL-command which is sent to the server. Of course we should do
     $-substitutions more consistently for all commands, but at the
     same time avoiding doing them twice.
     Added 3-MAY-1997. Yes, this is getting too ugly and kludgous again,
     to be redesigned and cleaned later.
   */
  if (not_pointing_to_input_buf (text))
    {
      isqlt_tcsncpy (input, text, INPUT_SIZE);
      text = input;
    }

  /* Set volatile deadlock retries to permanent deadlock retries
     which has been set with SET DEADLOCK_RETRIES command. */
  vol_deadlock_retries = perm_deadlock_retries;

#ifdef PLDBG
  if (virtuoso_debug)
    {
      debug_command (text);
      return;
    }
#endif

  main_text = text;
  while ('\0' != main_text[0])
    {
      if ('#' == main_text[0])
	{
	  while (('\0' != main_text[0]) &&
	    ('\r' != main_text[0]) &&
	    ('\n' != main_text[0]) )
	    main_text++;
	  continue;
	}
      while ((' ' == main_text[0]) || ('\t' == main_text[0]))
	main_text++;
      if (('\r' == main_text[0]) || ('\n' == main_text[0]))
	{
	  main_text++;
	  continue;
	}
      if (('-' == main_text[0]) && ('-' == main_text[1]))
	{
	  while (('\0' != main_text[0]) &&
	    ('\r' != main_text[0]) &&
	    ('\n' != main_text[0]) )
	    main_text++;
	  continue;
	}
      break;
    }

  if (is_isql_command (main_text, pidlist))
    {
      return;
    }

/* We should collect all these 'things to be cleared' before the execution
   of next real SQL-statement to one procedure. Also, retval and retvallen
 */
  text = skip_blankos (text);
again_exec:;
  clear_SQL_state_and_message ();
  N_rows = 0;
  n_out_cols_long = 0;

  start = get_msec_count ();


  if ((rc = do_sql_api_command (main_text)) == DO_SQL_API_COMMAND_IS_NOT_COMMAND)
    {

/*
   give pardescvec as NULL and maxpars as zero, so this is just for
   doing the $-substitutions. Also, the function will whine if there
   are any ?-parameter-markers present. (But it still returns and retains
   them there.)
 */
      parse_statement_for_special_parameters (UCP (text), NULL, 0);
      is_call_statement = !strncasecmp (main_text, _T("CALL"), 4);

      /* This thing doesn't work with Windows NT ODBC, because the damn
         MS ODBC Driver Manager whines both about ipar index 0 as well as about
         parameter SQL_RETURN_VALUE, as can be seen from the following:

         *** Error S1093: [Microsoft][ODBC Driver Manager] Invalid parameter number
         *** Error S1105: [Microsoft][ODBC Driver Manager] Invalid parameter type
         So when compiled for ODBC the flag_bind_return_values is by
         default off.
       */
      if (is_call_statement && flag_bind_return_values)
	{
	  rc = SQLBindParameter (	/* Bind the return value parameter. */
				  stmt,
				  ((UWORD) 0),	/* ipar, ignored, but DM wants > 0 */
				  ((SWORD) SQL_RETURN_VALUE),
				  ((SWORD) SQL_C_TCHAR),
				  ((SWORD) SQL_CHAR),
				  0,	/* The precision of the column. SQLULEN */
				  0,	/* The scale of the column. SWORD */
				  ((PTR) retvalbuf),
				  ((SQLLEN) RETVALBUFSIZE),	/* SQLLEN cbValueMax */
				  ((SQLLEN *) & cbRetVal)
	    );

	  IF_ERR_GO (stmt, error, rc);
          rc = SQLExecDirect (stmt, UCP (text), SQL_NTS);
	}
      else if (explain_mode)
        {
	  rc = SQLPrepare (stmt, _T("EXPLAIN(?)"), SQL_NTS);
	  IF_ERR_GO (stmt, error, rc);
	  rc = SQLBindParameter (stmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, isqlt_tcslen(text), 0, UCP(text), isqlt_tcslen(text), NULL);
	  IF_ERR_GO (stmt, error, rc);
	  rc = SQLExecute (stmt);
        }
      else if (sparql_translate_mode)
        {
          const TCHAR* q = text;
	  print_blobs_flag = 1;
          if (!strncasecmp (text, _T("SPARQL"), isqlt_tcslen(_T("SPARQL"))))
            q = text + isqlt_tcslen(_T("SPARQL"));

          rc = SQLPrepare (stmt, _T("SELECT SPARQL_TO_SQL_TEXT(?)"), SQL_NTS);
          IF_ERR_GO (stmt, error, rc);
          rc = SQLBindParameter (stmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, isqlt_tcslen(q), 0, UCP(q), isqlt_tcslen(q), NULL);
          IF_ERR_GO (stmt, error, rc);
          rc = SQLExecute (stmt);
        }
      else if (profile_mode)
        {
	  print_blobs_flag = 1;
	  rc = SQLPrepare (stmt, _T("PROFILE(?)"), SQL_NTS);
	  IF_ERR_GO (stmt, error, rc);
	  rc = SQLBindParameter (stmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, isqlt_tcslen(text), 0, UCP(text), isqlt_tcslen(text), NULL);
	  IF_ERR_GO (stmt, error, rc);
	  rc = SQLExecute (stmt);
        }
      else
        {
          rc = SQLExecDirect (stmt, UCP (text), SQL_NTS);
	}

    }
  else if (rc == DO_SQL_API_COMMAND_IS_NOT_AVAILABLE)
    {				/* Should we free something? */
      return;
    }

/* This should be here, almost right after SQLExecDirect, before any
   error checking, because it is for example possible to issue an
   update command that updates some of the rows and then fails because
   of let's say uniqueness violation, so we should check it with
   SQLRowCount now.
   But probably the whole transaction will bail out if there is an
   uniqueness violation, so it will be zero anyway in those cases.
   As is the case. And the totalitarian Microsoft driver manager
   won't allow SQLRowCount here in cases where SQLExecDirect produced
   an error. So commented out from here, transferred back to show_results
   *** Error S1010: [Microsoft][ODBC Driver Manager] Function sequence error
   SQLRowCount (stmt, ((SQLLEN *)&N_rows));
 */

  IF_ERR_OR_DEADLOCK_GO (stmt, error, deadlock_exec, rc);

  rc = show_results (start, rc, is_call_statement, print_banner_flag, verbose_mode);

  IF_ERR_OR_DEADLOCK_GO (stmt, error, deadlock_exec, rc);

/* It's important to call the following SQLFreeStmt's in all cases,
   otherwise we start getting very mysterious error messages like:
   *** Error S1010: Statement active at line X
   after we have done select even once, and then try to do something
   with foreach, for example.
 */

error:;
  SQLFreeStmt (stmt, SQL_UNBIND);
  SQLFreeStmt (stmt, SQL_CLOSE);

/* There was thing like this in the end of old isql_procedure_results
   (only statement after the error: label. What shall we do?)
   SQLTransact (henv, hdbc, SQL_ROLLBACK);
 */

  if (commit_mode != 2)
    {
      SQLSetConnectOption (hdbc, SQL_AUTOCOMMIT, commit_mode);
      if (SQL_SUCCESS != (rc = SQLTransact (henv, hdbc, SQL_COMMIT)))
	{
	  print_error (((HENV) 0), hdbc, ((HSTMT) 0), rc);
	}
    }
  return;

deadlock_exec:;
  if (commit_mode == 0)
    {
      SQLFreeStmt (stmt, SQL_UNBIND);
      SQLFreeStmt (stmt, SQL_CLOSE);
      if (SQL_SUCCESS != (rc = SQLTransact (henv, hdbc, SQL_ROLLBACK)))
	{
	  print_error (((HENV) 0), hdbc, ((HSTMT) 0), rc);
	}
      goto again_exec;
    }
  else
    goto error;
}

void
print_no_rows (int nth_set, long start)
{
  if (nth_set)
    {
      isql_printf (_T("Set %d "), nth_set);
    }
  isql_printf (_T("No rows"));
  if (!nth_set)
    {
      isql_printf (_T(" -- %ld msec."), get_msec_count () - start);
    }
  isql_printf (_T("\n"));
}


/*
   This is called from by the previous function (exec_one), with
   start argument being a starting time.
   If nth_set is given as non-zero in call, then we are reading
   data from the multiple result sets. (There was a call keyword)
   This returns the return code of the latest operation as its result.

   This really requires cleaning up!
 */

int
show_results (long start, int rc, int nth_set,
	      int loc_print_banner_flag, int loc_verbose_mode)
{
  int n_rows = 0;
  int HTML_sets_open = 0;	/* Just for helping us to balance tags. */
  SWORD btype;
  UWORD inx;


  /* Currently KUBL doesn't limit by itself the count of rows fetched,
     so we have to do it explicitly here. First take the value set to
     statement options (with the latest SET MAXROWS command, calling
     API-function SQLSetStmtOption). I hope this doesn't last long.
   */
  if (kubl_mode)
    {
      SQLLEN maxrows = 0;
      int rc2 = SQLGetStmtOption (stmt, SQL_MAX_ROWS, &maxrows);
      if (SQL_ERROR == rc2)
	{
	  rc = rc2;
	  IF_ERR_GO (stmt, error, rc2);
	}
      select_max_rows = (long) maxrows;
    }


next_set:

  if (nth_set > 1)
    {				/* We are coming second time here, after first set has been output. */
      rc = SQLMoreResults (stmt);
      IF_ERR_GO (stmt, error, rc);
      if (rc == SQL_NO_DATA_FOUND)	/* Finito, agotado! */
	{
	  if (loc_verbose_mode)
	    {
	      isql_printf (_T("%d sets, %ld msec.\n"), (nth_set - 1),
			   get_msec_count () - start);
	    }
	  goto end;
	}
      n_rows = 0;		/* Else... just zero the rows counter. */
    }

  if (nth_set && (SQL_NO_DATA_FOUND == rc))
    {
      if (loc_verbose_mode)
	{
	  print_no_rows (nth_set, start);
	}
      nth_set++;
      goto next_set;
    }
  else
    /* It's anything else except call of function with no data. */
    {
      SWORD temp;
      SQLNumResultCols (stmt, &temp);	/* Needs a pointer to short. */
      n_out_cols_long = (int) temp;	/* And is then converted to long. */
    }

  if (n_out_cols > MAXCOLS)
    {
      isql_fprintf (error_stream,_T("\n Too many columns (%d) in the resultset. Only the first %d will be displayed.\n\n"), n_out_cols, MAXCOLS);
      fflush (error_stream);
      n_out_cols_long = MAXCOLS;
    }

  if (n_out_cols > 0)		/* I.e. when doing select instead of something else */
    {				/* Could be a result set of called function also, (with some data) */
      if (rc == SQL_NO_DATA_FOUND)
	{
	  if (loc_verbose_mode)
	    {
	      print_no_rows (nth_set, start);
	    }
	}
      else
	{
	  for (inx = 1; inx <= n_out_cols; inx++)
	    {
	      SWORD cbColName;
	      SWORD dummy_digits;
	      stmt_out_t *out = &out_cols[inx - 1];
	      SQLDescribeCol (stmt, inx, UCP (out->o_title), MAXCOLNAME,
			      &cbColName,
			      &out->o_type, &out->o_width,
			      &dummy_digits, &out->o_nullable);

	      if (cbColName == SQL_NULL_DATA)
		{
		  isqlt_tcscpy (out->o_title, _T("NO NAME"));
		}

/* First time in this loop we set o_allocated_buflen element of
   each column to value MAXBUFFER (about 2500).
   Then, if width of column is greater than that (e.g. if explicitly
   specified as VARCHAR(10000)), we overwrite it with that value.

   If the corresponding o_buffer has been previously allocated,
   it is freed and set to NULL that the next if statement knows
   to allocate a bugger chunk of memory.

   Anyway, if o_buffer has not been allocated yet, it is allocated
   with the length of the current value in o_allocated_buflen.

   This buffer stays allocated between different queries,
   and is not freed until a bigger buffer is needed for that same
   column.
 */
	      if (0 == out->o_allocated_buflen)		/* 1st time for this col no. */
		{
		  out->o_allocated_buflen = MAXBUFFER;
		}

	      if (((out->o_width) > out->o_allocated_buflen)
		  && (out->o_width <= o_buffer_sanity_check)
		  && (out->o_type != SQL_LONGVARCHAR)
		  && (out->o_type != SQL_WLONGVARCHAR)
		  && (out->o_type != SQL_LONGVARBINARY))
		{
		  out->o_allocated_buflen = out->o_width + 1;
		  if (out->o_buffer)	/* If previously allocated buffer, */
		    {		/* we have to free it, because this */
		      free (out->o_buffer);	/* one would not fit into it. */
		      out->o_buffer = NULL;	/* Tell next case to malloc it again. */
		    }
		}

	      if (!out->o_buffer)	/* First time or needs a bigger buffer. */
		{
		  out->o_buffer =
		    (char *) chemalloc (out->o_allocated_buflen + 1, _T("show_results"));
		}

	      if (!times_to_strings_flag &&
		  (out->o_type == SQL_TIMESTAMP ||
		   out->o_type == SQL_DATE ||
		   out->o_type == SQL_TIME))
		{
		  btype = SQL_C_TIMESTAMP;
		  if (out->o_type == SQL_TIME)
		    btype = SQL_C_TIME;
		  else if (out->o_type == SQL_DATE)
		    btype = SQL_C_DATE;
		  else
		    btype = SQL_C_TIMESTAMP;
		}
	      else
		{
		  switch (out->o_type)
		    {
		    case SQL_WCHAR: case SQL_WVARCHAR: case SQL_WLONGVARCHAR:
		      btype = SQL_C_TCHAR;
		      break;
	            default:
		      btype = SQL_C_TCHAR;
	 	    }
		}

	      if (bind_blobs_flag || (0 == print_blobs_flag)
		  || ((SQL_LONGVARCHAR != out->o_type)
		      && (SQL_WLONGVARCHAR != out->o_type)
		      && (SQL_LONGVARBINARY != out->o_type)))
		{
		  SQLBindCol (stmt, inx, btype, out->o_buffer,
			      out->o_allocated_buflen,
			      &out->o_col_len);
		}
/* If max_col_width specified, then overwrite the out->o_width value
   we got from SQLDescribeCol with max_col_width if it's less than
   the actual width. */
	      if (max_col_width && (out->o_width > max_col_width))
		{
		  out->o_width = max_col_width;
		}
	    }			/* for loop over column names & types */

	  if (in_HTML_mode ())
	    {
	      HTML_sets_open++;
	      isql_fputs (oo_oe, stdout);	/* Otherness Ends. */
	      isql_fputs (oo_sb, stdout);	/* Set Begins. */
	    }
	  if (loc_print_banner_flag)
	    {
	      if (in_HTML_mode ())
	        print_banner ();
	      else if (vert_row_out_mode)
                print_banner_vert ();
	      else if (csv_rfc4180_mode)
		print_csv_rfc4180_banner ();
	      else if (csv_mode)
                print_csv_banner ();
	      else if (json_mode)
                print_json_banner ();
	      else
	        print_banner ();
	    }

	  for (;;)
	    {
	      rc = SQLFetch (stmt);
	      if (rc == SQL_NO_DATA_FOUND)
		break;
	      IF_ERR_GO (stmt, error, rc);
	      if (in_HTML_mode ())
	        rc = print_row();
	      else if(vert_row_out_mode)
	        rc = print_row_vert();
	      else if(csv_rfc4180_mode)
	        rc = print_csv_rfc4180_row();
	      else if(csv_mode)
	        rc = print_csv_row();
	      else if (json_mode)
	        rc = print_json_row(n_rows);
	      else
	        rc = print_row();
	      IF_ERR_GO (stmt, error, rc);
	      n_rows++;		/* Rows fetched and printed this far. */
	      if (select_max_rows && (n_rows >= select_max_rows))
		{		/* Are we limiting the amount ot rows? */
		  break;
		}
	    }			/* for fetch loop */

	  if (json_mode)
	    print_json_footer();

	  if (in_HTML_mode ())
	    {
	      HTML_sets_open--;
	      isql_fputs (oo_se, stdout);	/* Set Ends. */
	      isql_fputs (oo_ob, stdout);	/* Otherness Begins. */
	    }

	  if (loc_verbose_mode)
	    {
	      isql_printf (_T("\n"));
	      if (nth_set)
		{
		  isql_printf (_T("Set %d, "), nth_set);
		}
	      if (select_max_rows)
		{
		  isql_printf (_T("%d Rows of max %d allowed."),
			       n_rows, (int) select_max_rows);
		}
	      else
		{
		  isql_printf (_T("%d Rows."), n_rows);
		}
	      if (!nth_set)
		{
		  isql_printf (_T(" -- %ld msec."), get_msec_count () - start);
		}
	      isql_printf (_T("\n"));
	    }			/* if loc_verbose_mode */
	  fflush (stdout);
	  /* Save the latest row count also to the global var. */
	  N_rows = n_rows;
	}			/* else (data was found, one or more rows) */
    }				/* if(n_out_cols > 0) */
  else
    /* The statement was not a select. */
    {
      SQLRowCount (stmt, ((SQLLEN *) & sdtmp));
      N_rows = (int) sdtmp;

      if (loc_verbose_mode)
	{
	  isql_printf (_T("\n"));
	  if (nth_set)
	    {
	      isql_printf (_T("%d sets? "), nth_set);
	    }
	  isql_printf (_T("Done. -- %ld msec.\n"), get_msec_count () - start);
	  goto end;		/* Fix at 28-MAR-1997 to avoid jumittumista with
				   build in bif-functions like with  call ucase('jymy') */
	}
    }

  if (nth_set)
    {
      nth_set++;
      goto next_set;		/* Back to beginning. */
    }

end:;


  if (flag_bind_return_values)
    {
      if (nth_set)		/* Print out the return value parameter if it was call */
	{
	  if (loc_verbose_mode)
	    {
	      isql_printf (_T("RESULT="));
	    }
	  if (SQL_NULL_DATA == cbRetVal)
	    {
	      isql_fputs (_T("NULL"), stdout);
	    }
	  else
	    {
	      isql_fputs (retvalbuf, stdout);
	    }
	  isql_putchar ('\n');
	  fflush (stdout);
	}
    }

error:;
  if (HTML_sets_open > 0)
    {
      isql_fputs (oo_se, stdout);	/* Missing Set Ends. */
      isql_fputs (oo_ob, stdout);	/* Otherness Begins. */
      fflush (stdout);
    }

  return (rc);			/* It's the responsibility of the calling function
				   to print out the error message and free handles, etc. */
}


/* ================================================================ */
/*            STUFF FOR FOREACH PROCEDURE DEFINITIONS, ETC.         */
/*                                                                  */
/* ================================================================ */


TCHAR *
isql_fgets (TCHAR *inbuf, int maxbytes, FILE * fp, TCHAR *prompt)
{
#if defined (WITH_READLINE) || defined (WITH_EDITLINE)

  static TCHAR *previous_inbuf = NULL;

  if (isatty (fileno (fp)))	/* Never use readline with redirected input! */
    {
      /* For some reason there is SIGALARM signal waiting when we use
         readline. Let's ignore it with this construct.
         (Might it be a somekind of timeout which I should explicitly set
         to a greater value???)
       */
      if (SIG_ERR == signal (SIGALRM, SIG_IGN))
	{
	  isqlt_tperror (progname);
	  isql_exit (1);
	}

      if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
	{
	  isqlt_tperror (progname);
	  isql_exit (1);
	}

      if (NULL != previous_inbuf)	/* Return the previously allocate buffer. */
	{
	  readline_free (previous_inbuf);
	  previous_inbuf = NULL;
	}

      if (empty_stringp (isqlhist))	/* First time here? */
	{			/* Read the old history in */
	  TCHAR *homedir = getenv ("HOME");
	  if (!virtuoso_debug)
	    isqlt_stprintf (isqlhist, _T("%") PCT_S _T("/.isql_history"), (homedir ? homedir : "."));
	  else
	    isqlt_stprintf (isqlhist, _T("%") PCT_S _T("/.isql_pldebug_history"), (homedir ? homedir : "."));
	  read_history (isqlhist);
	}

      return (previous_inbuf = readline ((prompt ? prompt : "")));
    }

#endif

/* Else, either we have no readline routine or we are reading from an
   ordinary (not terminal) file with input redirection, so let's use an
   ordinary isqlt_fgetts.
 */

  if (prompt)
    {
      isql_printf (_T("%") PCT_S, prompt);
      fflush (stdout);
    }

  return (isqlt_fgetts (inbuf, maxbytes, fp));

}

/* When we are using readline it will return a pointer allocated from heap,
   but if using isqlt_fgetts, then it will return the same pointer it was given
   to as an argument, a pointer between start and end of global input
   buffer.
 */
#define using_readline(PTR) not_pointing_to_input_buf(PTR)
#define IFDEF_COND_MAX 200

typedef
struct ifdef_cond_s
{
  const TCHAR *ifc_expression;
  unsigned ifc_line;
  char ifc_effective_val;
  char ifc_reversed;
} ifdef_cond_t;

ifdef_cond_t ifdef_cond[IFDEF_COND_MAX] = { { NULL, 0, 1, 0 } };
unsigned ifdef_cond_current = 0;

void ifdef_push (const TCHAR *expr, unsigned line);
void ifdef_current_complement ();
char ifdef_current_is_true ();
void ifdef_pop();

void ifdef_push (const TCHAR *expr, unsigned line)
{
  TCHAR *token, *nextptr;
  TCHAR tmp2buf[TMPBUF_SIZE + 1];
  if (ifdef_cond_current < IFDEF_COND_MAX-1)
    {
      ++ifdef_cond_current;
      ifdef_cond[ifdef_cond_current].ifc_expression = expr;
      ifdef_cond[ifdef_cond_current].ifc_reversed = 0;
      ifdef_cond[ifdef_cond_current].ifc_line = line;
      nextptr = expr;
      token = get_next_token (&nextptr, tmp2buf, TMPBUF_SIZE, expr, 0);
      ifdef_cond[ifdef_cond_current].ifc_effective_val = *token=='1' ? 1 : 0;
    }
  else
    {
      isql_fprintf (error_stream, _T("ERROR: Too many nested #if instructions, maximum %d.\n"), IFDEF_COND_MAX);
      exit (1);
    }
}

void ifdef_current_complement ()
{
  // don't redefine 0-th bottom element, should be always TRUE
  if (ifdef_cond_current)
    {
      if (ifdef_cond[ifdef_cond_current].ifc_reversed == 0)
        ifdef_cond[ifdef_cond_current].ifc_reversed = 1;
      else
        isql_fprintf (error_stream, _T("ERROR: unexpected #else instruction. Ignored.\n"));
    }
  else
      isql_fprintf (error_stream, _T("ERROR: unexpected #else instruction. Ignored.\n"));
}

char ifdef_current_is_true ()
{
  return (char)( ifdef_cond[ifdef_cond_current].ifc_effective_val ^ ifdef_cond[ifdef_cond_current].ifc_reversed );
}

void ifdef_pop()
{
  if (ifdef_cond_current)
    {
      ifdef_cond[ifdef_cond_current].ifc_expression = NULL;
      ifdef_cond[ifdef_cond_current].ifc_reversed = 0;
      ifdef_cond[ifdef_cond_current].ifc_line = 0;
      ifdef_cond[ifdef_cond_current].ifc_effective_val = 1;
      --ifdef_cond_current;
    }
  else
    {
      isql_fprintf (error_stream, _T("ERROR: unexpected #endif instruction. Ignored.\n"));
    }
}


/* rep_loop: Read-Eval-Print loop. Called by main & load_file */
int
rep_loop (FILE * infp, TCHAR *new_prompt)
{				/* The Main Loop. */
  TCHAR *declared_name = NULL;
  TCHAR *save_org_prompt;
  int bracelevel = 0, old_bracelevel = 0;
  UTCHAR inside_string = 0;

  integer_list pidlist_space;
  integer_list *pidlist = &pidlist_space;
  pidlist->next = NULL;		/* Initialize the pidlist anchor. */

  save_org_prompt = print_prompt;
  print_prompt = ((new_prompt && strcasecmp (new_prompt, _T("OFF")))
		  ? (new_prompt) : NULL);

/* If prompt has been defined as "OFF", then turn it to NULL (no prompting
   at all). */

  latest_statement_begins_at () = current_linecount () + 1;

#define IN_BATCH 0x4000

  for (;;)
    {
      int nth_line_of_statement = 0;
      int inlen;
      unsigned int edel = 0;	/* End delimiter either semicolon or ampersand */
      TCHAR *tmp_pt;
      TCHAR *inpt = &input[0];
      /* When no readline is used inpiece and inpt should always be equal. */
      TCHAR *inpiece = inpt;
      TCHAR promptbuf[256], *promptptr = print_prompt;
      int empty_lines_so_far = 1;
      declared_name = NULL;
      input[0] = 0;		/* Clear the input buffer first. */

      /* if(print_prompt) isql_printf ("%s", print_prompt); fflush (stdout); */

      /* This inner loop collects one whole SQL-statement
         (upto the next semicolon terminated line). */
      for (nth_line_of_statement = 0;; nth_line_of_statement++)
	{
	  promptptr = print_prompt;

	  /* Except on certain conditions choose another kind of a prompt */
	  if (print_prompt)
	    {
	      if (declared_name)
		{
		  isqlt_stprintf (promptbuf, _T("%") PCT_S _T("(%d) "), declared_name, bracelevel);
		  promptptr = promptbuf;
		}
	      else if (nth_line_of_statement)
		{
		  if (virtuoso_debug)
		    break;
		  promptptr =
		    _T("Type the rest of statement, end with a semicolon (;)> ");
		}
	    }

	  if (NO (inpiece = isql_fgets (inpiece, IN_BATCH, infp, promptptr)))
	    {
	      goto over;
	    }

	  current_linecount ()++;

	  if (!declared_name && !virtuoso_debug && !inside_string)
	    {
	      TCHAR *inpiece_tail = inpiece;
	      while ((' ' == inpiece_tail[0]) || ('\t' == inpiece_tail[0]))
	        inpiece_tail++;
	      if (empty_lines_so_far)
		{
		  if (('\0' == inpiece_tail[0]) || ('\r' == inpiece_tail[0]) || ('\n' == inpiece_tail[0]))
		    {	/* If it's a blank line on the toplevel then skip it here. */
		      latest_statement_begins_at () = current_linecount () + 1;
		      nth_line_of_statement--;	/* Because of ++ in the for end */
		      continue;		/* The inpt pointer is NOT incremented */
		    }
		  else if (*inpiece_tail=='#')
		    break;
		}
	    }
	  if (!inside_string)
	    {
	      if (empty_lines_so_far)
		{
		  if (('-' == *inpiece) && ('-' == *(inpiece + 1)) && !declared_name)
		    {			/* If it's a comment on the toplevel then skip it here. */
		      latest_statement_begins_at () = current_linecount () + 1;
		      nth_line_of_statement--;	/* Because of ++ in the for end */
		      continue;		/* The inpt pointer is NOT incremented */
		    }
		}
	      empty_lines_so_far = 0;
	      if (!declared_name && (declared_name = is_declaration (inpiece)))
		{
		  old_bracelevel = bracelevel = 0;
		}
	    }
	  /* if(declared_name) */
	  {
	    old_bracelevel = bracelevel;
	    bracelevel = count_bracelevel (inpiece, bracelevel, NULL, 0, 1, &inside_string);
	  }

          /* Check statement for execution,
             IF the bracelevel is zero, (i.e. we are not inside procedure definition.)
             or less than zero (in which case it is syntax error),
             AND
             the last non-blank character of the line is semicolon (;) or ampersand (&)
             OR
             there has been a procedure definition (declared_name is not NULL)
             (and bracelevel has just now returned back to zero or less, i.e. there
             really is a closing brace on the line)
             This means that user doesn't have to finish the procedure definitions
             with semicolon, allowing us also to load with load (not just load -r)
             old-fashioned procedure definition files which have no separating
             semicolons.
           */

	  if ((bracelevel <= 0 && !inside_string) &&
	      ((edel = get_rid_of_trailing_semicolon_or_ampersand (inpiece))
	       ||
	       ((NULL != declared_name)		/* Ends procedure/trigger definition? */
		&& (isqlt_tcschr (inpiece, '}')))
	      )
	    )
	    {
	      if (using_readline (inpiece))
		{
		  if (inpt > &input[0])		/* Add blank between if there */
		    {
		      *inpt++ = ' ';
		    }		/* are previous lines */
		  isqlt_tcscpy (inpt, inpiece);	/* Copy the last piece got to input buf */
		}
	      bracelevel = 0;
	      break;
	    }
	  inlen = (int) isqlt_tcslen (inpiece);
	  if ((inpt + inlen) > (input + INPUT_SIZE - IN_BATCH))
	    {
	      isql_fprintf (error_stream,
			    _T("\n%") PCT_S _T(": *** The statement has grown too long on lines %ld - %ld.\n")
			    _T("Already %d bytes read without encountering a semicolon or ampersand\n")
			    _T("in the end of line! Goodbye!\n"),
	      progname, latest_statement_begins_at (), current_linecount (),
			    (int) ((inpt + inlen) - input));
	      isql_exit (1);
	    }

          /* With readline we have to copy inpiece to the point of input buffer
             (pointed by inpt) because inpiece is wholly separate buffer allocated
             by readline itself.
           */
	  if (using_readline (inpiece))
	    {
	      if (inpt > &input[0])	/* Add blank between if there */
		{
		  *inpt++ = ' ';
		}		/* are previous lines */
	      isqlt_tcscpy (inpt, inpiece);
	      inpt += inlen;
	    }
          /* Without readline inpiece and inpt should point to the same point in
             input buffer, where isqlt_fgetts will write its stuff. No need to add
             blanks between lines because isqlt_fgetts will keep the newline in the end
             of buffer, and get_rid_of_trailing_semicolon_if_there_is_one(inpiece)
             won't nuke it either if there is no semicolon (as we are here there
             is not one). This is getting UGLY, sorry.
           */
	  else
	    {
	      inpiece = inpt += inlen;
	    }
	}			/* End of the inner for loop, collected one statement */

      if (echo_mode)
	{
	  isql_fprintf (stdout, _T("\n-- Line %ld:%c%") PCT_S _T("\n"),
	    latest_statement_begins_at (),
	    ((latest_statement_begins_at () == current_linecount ()) ? ' ' : '\n'),
	    input );
	}

      fflush (stdout);

#if defined (WITH_READLINE) || defined (WITH_EDITLINE)
      if (using_readline (inpiece) && (input[0]))	/* Something read in really? */
	{			/* Then add it to history. */
	  int len = isqlt_tcslen (input);
	  input[len] = edel;	/* Restore the ending delimiter temporarily */
	  input[len + 1] = '\0';
	  add_history (input);	/* before adding input line to history */
	  input[len] = '\0';	/* and then wipe it out again. (kludgous) */
	}
#endif

      /* processing the input line */
      tmp_pt = skip_blankos (input);

      if (!isqlt_tcsncmp(tmp_pt,_T("#if "), 4)) /* begin of conditional expression. */
        {
          ifdef_push (tmp_pt + 4, current_linecount ());
        }
      else if (!isqlt_tcsncmp(tmp_pt,_T("#else"),5)) /* else brunch of conditional expression. */
        {
          ifdef_current_complement ();
        }
      else if (!isqlt_tcsncmp(tmp_pt,_T("#endif"),6)) /* end of conditional expression. */
        {
          ifdef_pop ();
        }
      else if (*tmp_pt == '!')
	{ /* Spawn a command to shell and wait for it if doesn't end with & */
          if (ifdef_current_is_true ())
	    spawn_shell_command ((tmp_pt + 1), pidlist, (edel != '&'));
	}
      else if (edel == '&')	/* Run in background a statement or group of stmts */
        {
          if (ifdef_current_is_true ())
            {
              if (load_n_files)
                {
                  isql_fprintf (error_stream,
                      _T("ERROR: Can't spawn a child process because there are ")
                      _T("files on the command line, which may cause infinite ")
                      _T("recursion %") PCT_S _T(" of %") PCT_S _T(":\n%") PCT_S _T("\n"),
                      current_lines_range(),
                      current_loadexpr (), input);
                  exit (1);
                }
              spawn_to_background (input, pidlist);
            }
        }
      else if (ifdef_current_is_true ())
        /* A normal semicolon terminated statement */
        {
          if (virtext && !sparql_translate_mode &&
              ( latest_statement_begins_at () != current_linecount() ||
                is_declaration (input) ) )
            {
              TCHAR pragma[1024];
              int pragma_len;
              isqlt_stprintf (pragma,
                _T("#line %ld \"%") PCT_S _T("\"\n"),
                latest_statement_begins_at (),
                current_file ? current_file : _T("(console)") );
              pragma_len = (int) isqlt_tcslen (pragma);
              memmove (input + pragma_len, input, (isqlt_tcslen(input) + 1) * sizeof (TCHAR));
              memcpy (input, pragma, pragma_len * sizeof (TCHAR));
            }
          exec_one (input, pidlist);
        }

      latest_statement_begins_at () = current_linecount () + 1;
    }	/* The outer for loop */

over:;
  if (bracelevel > 0)
    isql_fprintf (error_stream,
	_T("ERROR: Missing closing brace %") PCT_S _T(" of %") PCT_S _T(":\n%") PCT_S _T("\n"),
	current_lines_range(),
	current_loadexpr (), input);

  print_prompt = save_org_prompt;	/* Restore the prompt. */

  return (current_linecount ());
}


void
do_statement (TCHAR *in, TCHAR *procedure_name)
{
  IF_ERR_GO (stmt, err, SQLExecDirect (stmt, UCP (in), SQL_NTS));
  if (commit_mode != 2)
    {
      SQLSetConnectOption (hdbc, SQL_AUTOCOMMIT, commit_mode);
      SQLTransact (henv, hdbc, SQL_COMMIT);
    }
  return;
err:
  isql_fprintf (error_stream, _T("###While loading %") PCT_S _T("\n"),
		(procedure_name ? procedure_name : in));
  SQLTransact (henv, hdbc, SQL_ROLLBACK);
}


#define LINE_SIZE 3000

int
load_raw (FILE * f)
{
  TCHAR *procname = NULL;
  int any_in = 0;
  TCHAR line[LINE_SIZE + 3];
  input[0] = 0;

  while (isqlt_fgetts (line, LINE_SIZE, f))
    {
      if (NULL != (procname = is_declaration (line)))
	{
	  if (any_in)		/* Do the previous statement read. */
	    {
	      get_rid_of_trailing_semicolon_if_there_is_one (input);
	      do_statement (input, procname);
	    }
	  input[0] = 0;
	  any_in = 1;
	}
      isqlt_tcscat (input, line);
    }
  if (any_in && procname)
    {
      get_rid_of_trailing_semicolon_if_there_is_one (input);
      do_statement (input, procname);
    }
  return (1);
}


/* Now allows also the executing of the procedure definitions ending with
   a semicolon. Anyway, because this versions allows only the execution
   of the procedure definitions, we have to rewrite this later, like
   is now the brace-level counting main read-eval loop.
   Changed 29-MAR-1997: Added an integer argument raw_load.
   If it is non-zero, then works in old-fashioned way, allowing only
   procedure definitions in the file, but if it is zero, then uses
   rep_loop routine which is also used for interactive input.
 */
int
load_file (TCHAR *name, int raw_load, TCHAR *loadexpr)
{
  FILE *f = NULL;
  TCHAR *sql_path = NULL;
  size_t sql_path_size;
  TCHAR *sql_path_element = NULL;
  TCHAR filename_buf[4096];
  TCHAR *sql_path_delimiters =
#if defined (WIN32)
          _T(";");
#else
          _T(":");
#endif
  TCHAR *os_path_delimiter =
#if defined (WIN32)
          _T("\\");
#else
          _T("/");
#endif

  if (((NULL == name) && (name = _T("NULL")))
      || (!*name))
    {
      isqlt_tperror (progname);
      isql_fprintf (error_stream,
	     _T("%") PCT_S _T(": Cannot open file \"%") PCT_S _T("\" for loading, at line %ld of %") PCT_S _T("\n"),
		    progname, name, latest_statement_begins_at (),
		    current_loadexpr ());
      return (0);
    }

  f = isqlt_tfopen (name, _T("r"));

  if (!f)
    {
      sql_path = isqlt_tgetenv (_T("SQLPATH"));

      if (sql_path)
        {
	  TCHAR *tok_state;

          sql_path_size = (isqlt_tcslen (sql_path) + 1 ) * sizeof (TCHAR);
          sql_path = chemalloc (sql_path_size, _T("load_file"));
          isqlt_tcscpy (sql_path, isqlt_tgetenv (_T("SQLPATH")));

          sql_path_element = isqlt_tcstok (sql_path, sql_path_delimiters, &tok_state);

          while (sql_path_element)
            {
              isqlt_tcscpy (filename_buf, sql_path_element);
              isqlt_tcscat (filename_buf, os_path_delimiter);
              isqlt_tcscat (filename_buf, name);
              if ((f = isqlt_tfopen (filename_buf, _T("r"))))
                {
                  name = filename_buf;
                  break;
                }
              sql_path_element = isqlt_tcstok (NULL, sql_path_delimiters, &tok_state);
            }
          free (sql_path);
        }

      if (!f)
        {
          isqlt_tperror (progname);
          isql_fprintf (error_stream,
                 _T("%") PCT_S _T(": Cannot open file \"%") PCT_S _T("\" for loading, at line %ld of %") PCT_S _T("\n"),
                        progname, name, latest_statement_begins_at (),
                        current_loadexpr ());
          return (0);
        }
    }

  current_file = name;
  push_to_loadexpr_stack (loadexpr, f);
  if (raw_load)
    {
      load_raw (f);
    }
  else
    {
      rep_loop (f, NULL);
    }				/* Turn prompting off with NULL */
  fclose (f);
  current_file = NULL;
  drop_from_loadexpr_stack ();

  return (1);
}



/* ================================================================ */
/*    STUFF FOR FOREACH METASTATEMENT                               */
/*                                                                  */
/* ================================================================ */


#define PT_LINE 1
#define PT_INTEGER 2
#define PT_BLOB 3
#define PT_TIMESTAMP 4		/* A new one for debugging 3-3-1997 */
#define PT_DATE      5		/* As are these, 1-APR-1997 */
#define PT_TIME      6

/* With TIMESTAMP's we keep the C-type as SQL_C_CHAR, because the
   dates are strings input from the user or from the file, and they
   are converted to timestamps by the driver: */

#define get_proper_c_type(T,C)\
 (((C) || ((T)==PT_INTEGER)) ? SQL_C_SLONG : SQL_C_CHAR)

#define get_proper_sql_type(T,C) (((C) || ((T) == PT_INTEGER))\
 ? SQL_INTEGER : (((T) == PT_BLOB) ? SQL_LONGVARCHAR :\
 (((T) == PT_TIMESTAMP) ? SQL_TIMESTAMP :\
(((T) == PT_DATE) ? SQL_DATE : (((T) == PT_TIME) ? SQL_TIME : SQL_CHAR)))))

#define istermzero_or_space(C) ((!(C)) || isqlt_istspace((C)))

#define token_found(inbuf,token) (token &&\
 !isqlt_tcsncmp(inbuf,token,isqlt_tcslen(token))\
 && istermzero_or_space(inbuf[isqlt_tcslen(token)]))

#define user_input_p(FP) (stdin == (FP))

UTCHAR *
succstr (UTCHAR * str, UTCHAR * alphabet)
{
  signed int len = (int)isqlt_tcslen (SCP (str));
  UTCHAR first_of_alphabet = alphabet[0];
  UTCHAR last_of_alphabet = alphabet[isqlt_tcslen (SCP (alphabet)) - 1];

  /* Change all last_of_alphabets from the end of string
     to the first_of_alphabets */
  while (--len >= 0 && (str[len] == last_of_alphabet))
    {
      str[len] = first_of_alphabet;
    }

  /* If the whole string is composed of last_of_alphabets, then return NULL
     as we cannot find more successors with the same length.
     (Without this it would wrap over to the string composed only of
     first_of_alphabet). */
  if (len < 0)
    {
      return (NULL);
    }
  /* Otherwise increment the last non-end-alphabet char found by one. */
  else
    {
      (str[len])++;
      return (str);
    }
}


UTCHAR *
succelem (UTCHAR * inbuf, int inbuf_size, int type, ULONG intval,
	  UTCHAR * beg_elem, UTCHAR * end_elem)
{
  int is_signed = (('-' == *end_elem) || ('-' == *beg_elem));

  /* If inbuf is already same as end_elem return NULL to mark that
     we should break from the loop. */
  if (!isqlt_tcscmp (SCP (inbuf), SCP (end_elem)))
    {
      return (NULL);
    }

  if (PT_INTEGER == type)
    {
      isqlt_stprintf (SCP (inbuf), (is_signed ? _T("%ld") : _T("%lu")), (intval + 1));
      return (inbuf);
    }
  else
    {
      return (succstr (inbuf, UCP ("az")));
    }

}


int
skip_to_end_token (FILE * in_fp,
		   unsigned long int *ptr_to_linecount,
		   TCHAR *end_token,
		   TCHAR *inbuf, int inbuf_size)
{
  TCHAR *endtoken = (TCHAR *) (end_token ? end_token :
#ifdef WIN32
		    _T("CTRL-Z")
#else				/* It should be Unix. */
		    _T("CTRL-D")
#endif
  );

  for (((print_prompt && user_input_p (in_fp))
    ? isql_printf (_T("foreach waiting for end token \"%") PCT_S _T("\"> "), endtoken) : 0);
       isqlt_fgetts (inbuf, inbuf_size, in_fp);
       ((print_prompt && user_input_p (in_fp))
    ? isql_printf (_T("foreach waiting for end token \"%") PCT_S _T("\"> "), endtoken) : 0))
    {
      if (ptr_to_linecount)
	{
	  ++*ptr_to_linecount;
	}
      if (token_found (inbuf, end_token))
	{
	  return (1);
	}
    }
  return (0);			/* No more input. */
}

/* Returns the retcode of the last SQL-API-call. Arguments are the same
   as for exec_for_foreach (the next function), where this is called from,
   except additional integer pointer *count and *end_found
   reference arguments in the end.

   Here's the catch when reading in line_by_line mode:
   There has to be a possibility to present exactly, by DBDUMP,
   0) a NULL in blob column, i.e. value that is not really a blob at all,
   but NULL is NULL is NULL.
   1) an empty blob (of 0 bytes)
   2) as well as a blob whose last line doesn't end with newline.

   The NULL in the blob column is presented just as
   FOREACH BLOB INSERT INTO BLOBS(BLOB) VALUES(NULL);

   Without the END keyword!

   The empty blob is presented just as
   FOREACH BLOB INSERT INTO BLOBS(BLOB) VALUES(?);
   END

   The blob whose last line doesn't end with a newline is presented as:
   FOREACH BLOB INSERT INTO BLOBS(BLOB) VALUES(?);
   The first line
   \END the second line which just happens to begin with a string END
   The third line, that ends with a period, not a newline.\c
   END

   So we use here a new magical escape sequence \c that just cuts
   the string short at that point, leaving the newline dangling
   in the input buffer out.
   (See function unescape_string)

   In dbdump.c the function print_blob_col (not the one in this source file!)
   produces lines that can be read with this function in line_by_line mode.
 */

int bin_fgets (TCHAR *buffer, int max_size, FILE * infp);

/* Token is expected to be followed by '\n', '\r', ' ' or '\0' */

#define is_mime_boundary_token(ROW,ROWLEN,BTOKEN,BLEN,FINAL_FLAG)\
(((ROWLEN) >= (BLEN)) && !isqlt_tcsncmp((ROW),(BTOKEN),(BLEN))\
  && ((isqlt_istspace(*((ROW)+(BLEN))) || !*((ROW)+(BLEN)))\
  || (('-' == *((ROW)+(BLEN))) && ('-' == *((ROW)+(BLEN)+1))\
       && (isqlt_istspace(*((ROW)+(BLEN)+2)) || !*((ROW)+(BLEN)+2))\
       && (FINAL_FLAG = 1))))


/* C's && is not Lisp's AND, so this would not work as expected:
   #define is_mime_boundary_token(ROW,ROWLEN,BTOKEN,BLEN)\
   ( (((ROWLEN) >= (BLEN)) && !isqlt_tcsncmp((ROW),(BTOKEN),(BLEN))\
   && ((isqlt_istspace(*((ROW)+(BLEN))) || !*((ROW)+(BLEN)))\
   || (('-' == *((ROW)+(BLEN))) && ('-' == *((ROW)+(BLEN)+1))\
   && (isqlt_istspace(*((ROW)+(BLEN)+2)) || !*((ROW)+(BLEN)+2)))))\
   && ( ('-' == *((ROW)+(BLEN))) ? 2 : 1))
 */

#define only_lf_or_crlf(BUF) (  (('\n' == *(BUF)) && !*((BUF)+1) )\
 || (('\r' == *(BUF)) && ('\n' == *((BUF)+1)) && !*((BUF)+2))  )


/* Returns 1 if (BUF+LEN-1) (LEN should be the length of buffer)
   points to a linefeed (dec. 10.) and 2 if the preceding character
   is carriage return (dec. 13.). Otherwise returns 0.
 */
#define ends_with_lf_or_crlf(BUF,LEN)\
 ( ( ((LEN) >= 1) && ('\n' == *((BUF)+(LEN)-1)))\
  ? (1 + (((LEN) >= 2) && ('\r' == *((BUF)+(LEN)-2)))) : 0)


/* More kludgifications at 22.10.1997 by AK. Added an argument
   mime_boundary_token, which when given as non-NULL, overrides
   end_token and separating_token, and additionally changes the
   behaviour so that the last newline (e.g. consisting only of
   LF or CR+LF) before the boundary token itself is not flushed
   to the database, instead it is viewed as a part of the token
   itself.
   See rfc1521 for the details. Mime multipart messages, etc.
 */
int
read_blob_from_input (TCHAR *statement, int has_output, FILE * in_fp,
		      unsigned long int *ptr_to_linecount,	/* incr:ed if not NULL */
		      TCHAR *end_token, TCHAR *separating_token,
		      TCHAR *mime_boundary_token,
		      int *type, PFI unpack_function_ptr,
		      TCHAR *inbuf, int inbuf_size,
		      ULONG * count, int *end_found)
{
  SQLULEN param_num;		/* SQLULEN, not UWORD, to avoid stack corruption. */
  int dummy_tummy = 0;		/* In case param_num overflows. */
  int rc, rc2;
  int got_anything = 0, final_mime_token = 0;	/* Not much used. */
  SQLLEN inbuf_len;
  int blen = 0;
  int line_by_line_input =	/* A flag. */
  (user_input_p (in_fp) || end_token || separating_token
   || (NULLFP != unpack_function_ptr));
  int crlfs = 0, previous_crlfs = 0;	/* 1 or 2 if the last line ended with LF or CR+LF */
  int ind_to_crlfs = 0;

  if (mime_boundary_token)
    {
      blen = (int) isqlt_tcslen (mime_boundary_token);
    }

  dummy_tummy = dummy_tummy + 1;

/* Parameter is a blob? */
  do
    {
      rc = SQLParamData (stmt, ((PTR *) & param_num));
      if (SQL_NEED_DATA == rc)
	{
          /* rewinding input stream to the start, there can be many parameters */
          if (!user_input_p (in_fp))
            rewind (in_fp);

	  for (;;)
	    {
	      if (line_by_line_input)
		{
		  if (print_prompt)
		    {
		      isql_printf (_T("foreach %ld blob, param %ld> "),
				   *count, (long) param_num);
		    }
/*                 if(!(got_anything = !!isqlt_fgetts(inbuf, inbuf_size, in_fp))) */
		  if (!(got_anything = bin_fgets ((inbuf + previous_crlfs),
					      (inbuf_size - previous_crlfs),
						  in_fp)))
		    {
		      *end_found = 1;
		      break;
		    }
		  if (ptr_to_linecount)
		    {
		      ++*ptr_to_linecount;
		    }
		  if (mime_boundary_token)
		    {
		      if (is_mime_boundary_token ((inbuf + previous_crlfs),
						  got_anything,
			       mime_boundary_token, blen, final_mime_token))
			{
			  *end_found = 1;
			  break;
			}
		    }
		  else
		    {
		      if (token_found ((inbuf + previous_crlfs), end_token))
			{
			  *end_found = 1;
			  break;
			}
		      if (token_found ((inbuf + previous_crlfs),
				       separating_token))
			{
			  break;
			}
		    }

/* NOT TESTED: HOW DO WORK TOGETHER
   HIDDEN_CRS=CLEARED, FOREACH ESCAPED or HEXADECIMAL BLOB . MIME_BOUNDARY ?
 */
		  if (clear_hidden_crs_flag)
		    {
		      got_anything
			-= get_rid_of_trailing_CRS_with_count ((inbuf + previous_crlfs), got_anything);
		    }

		  if (mime_boundary_token)
		    {
		      if (0 != (crlfs = ends_with_lf_or_crlf (inbuf + previous_crlfs, got_anything)))
			{	/* Leave the trailing (CR+)LF for the next time. */
			  got_anything -= crlfs;
			  ind_to_crlfs = got_anything + previous_crlfs;
			}
		    }

/* Here is a bug when using MIME_BOUNDARY together with ESCAPED or HEX */
		  if (NULLFP != unpack_function_ptr)
		    {
		      UTCHAR save_it = 0;
		      if (crlfs)
			{
			  save_it = inbuf[ind_to_crlfs];
			  inbuf[ind_to_crlfs] = '\0';	/* Temp terminator */
			}
/* Assumed to always generate equal length or shorter string: */
		      got_anything =
			((unpack_function_ptr) ((TCHAR *) UCP (inbuf + previous_crlfs)));
		      if (crlfs)
			{
			  inbuf[ind_to_crlfs] = save_it;	/* Restore? */
			}
		    }

		  inbuf_len = got_anything + previous_crlfs;

		}
	      else /* Reading in binary format from the file specified */
		{
		  inbuf_len = got_anything = (int) fread (inbuf, 1, inbuf_size, in_fp);
		  /* isql_fprintf(error_stream,
		     "read_blob_from_input: fread got %d bytes, feof(in_fp)=%d, ferror(in_fp)=%d\n"
		     "inbuf[0-15]='%.16s'\n",
		     got_anything,feof(in_fp),ferror(in_fp), inbuf
		     ); fflush(error_stream);
		   */
		  if (!got_anything)
		    {
		      *end_found = 1;
		      break;
		    }
		}

              rc2 = SQLPutData (stmt, inbuf, ((SQLLEN) inbuf_len));
              if (SQL_ERROR == rc2)
                {
                  return (rc2);
                }
              if (0 != (previous_crlfs = crlfs))	/* Assignment on purpose */
                {		/* Copy the (CR+)LF in the end of this line to the
                                 beginning of next batch to be written to database,
                                 unless the next line happens to be the mime boundary. */
                  isqlt_tcsncpy (inbuf, (inbuf + ind_to_crlfs), crlfs);
                  /* inbuf[0] = inbuf[inbuf_len];
                     if(crlfs > 1) { inbuf[1] = inbuf[inbuf_len+1]; }
                     */
                }

              /* Was:   IF_SEVERE_ERR_GO(stmt, error, rc2); */
	    } /* for loop over blob chunks */
	}
      else
	{
	  if (SQL_ERROR == rc)
	    return (rc);
	}
    }
  while (SQL_NEED_DATA == rc);
/* Parameter was a blob? */

  return (rc);
}


/* An excerpt from ODBC API help file,
   function SQLTransact(henv, hdbc, fType)
   (Affects how we implement deadlock retrying...)

   If fType is SQL_COMMIT, SQLTransact issues a commit request for all
   active operations on any hstmt associated with an affected hdbc.
   If fType is SQL_ROLLBACK, SQLTransact issues a rollback request
   for all active operations on any hstmt associated with an affected hdbc.
   If no transactions are active, SQLTransact returns SQL_SUCCESS with no
   effect on any data sources.

   If the driver is in manual-commit mode (by calling SQLSetConnectOption
   with the SQL_AUTOCOMMIT option set to zero,
   i.e. in ISQL SET AUTOCOMMIT OFF, which is OFF by default), a new
   transaction is implicitly started when an SQL statement that can
   be contained within a transaction is executed against the current data
   source.

   To determine how transaction operations affect cursors, an application
   calls SQLGetInfo with the SQL_CURSOR_ROLLBACK_BEHAVIOR and
   SQL_CURSOR_COMMIT_BEHAVIOR options.

   If the SQL_CURSOR_ROLLBACK_BEHAVIOR or SQL_CURSOR_COMMIT_BEHAVIOR value
   equals SQL_CB_DELETE, SQLTransact closes and deletes all open cursors on
   all hstmts associated with the hdbc and discards all pending results.
   SQLTransact leaves any hstmt present in an allocated (unprepared) state;
   the application can reuse them for subsequent SQL requests or can call
   SQLFreeStmt to deallocate them.

   If the SQL_CURSOR_ROLLBACK_BEHAVIOR or SQL_CURSOR_COMMIT_BEHAVIOR value
   equals SQL_CB_CLOSE, SQLTransact closes all open cursors on all hstmts
   associated with the hdbc. SQLTransact leaves any hstmt present in a
   prepared state; the application can call SQLExecute for an hstmt
   associated with the hdbc without first calling SQLPrepare.

   If the SQL_CURSOR_ROLLBACK_BEHAVIOR or SQL_CURSOR_COMMIT_BEHAVIOR value
   equals SQL_CB_PRESERVE, SQLTransact does not affect open cursors
   associated with the hdbc. Cursors remain at the row they pointed to
   prior to the call to SQLTransact.

   For drivers and data sources that support transactions, calling
   SQLTransact with either SQL_COMMIT or SQL_ROLLBACK when no transaction
   is active will return SQL_SUCCESS (indicating that there is no
   work to be committed or rolled back) and have no effect on the data
   source.

   Drivers or data sources that do not support transactions (SQLGetInfo
   fOption SQL_TXN_CAPABLE is 0) are effectively always in autocommit mode.
   Therefore, calling SQLTransact with SQL_COMMIT will return SQL_SUCCESS.
   However, calling SQLTransact with SQL_ROLLBACK will result in SQLSTATE
   S1C00 (Driver not capable), indicating that a rollback can never be
   performed.

 */


#define MAXPARS (MAXCOLS+20)

/* If between_form is non-zero then separating_token will contain
   the starting point, end_token the ending point and no input is
   read from anywhere, instead inbuf will change from the starting point
   to the ending point (inclusive).
 */
int
exec_for_foreach (TCHAR *statement,	/* Just the first keyword. */
		  TCHAR *whole_statement,	/* Whole SQL-statement.
						   resides in input buffer so it's not very persistent. */
		  int has_output, FILE * in_fp,
		  TCHAR *loadexpr,	/* pushed to stack if not NULL */
		  TCHAR *end_token, TCHAR *separating_token,
		  TCHAR *mime_boundary_token,
		  int *type, PFI unpack_function_ptr,
		  TCHAR *inbuf, int inbuf_size,
		  int between_form)
{
  SQLULEN ipar = 1;		/* Changed from UWORD to SQLULEN to avoid whining. */
  SQLLEN cbInput, cbCount = 0;
  ULONG intpar;
  ULONG count = 1;		/* Count of successful operations (+1) */
  int end_found = 0;		/* Flag to tell that we should stop it! */
  int has_been_pushed = 0;
  int rc;
  int blen = (int) (mime_boundary_token ? isqlt_tcslen (mime_boundary_token) : 0);
  UTCHAR pardescvec[MAXPARS + 1];
  int pars_found = 0;


  if (between_form)
    {
      isqlt_tcsncpy (inbuf, separating_token, inbuf_size);
    }

  pars_found
    = parse_statement_for_special_parameters (UCP (whole_statement),
  /* Stores its stuff here: */ pardescvec,
					      MAXPARS);
/*
   isql_printf(
   "**exec_for_foreach: pardescvec=%s/%d, pars_found=%d, statement:\n%s\n",
   pardescvec,isqlt_tcslen(pardescvec),pars_found,whole_statement);
 */

  /* Do this after parse_statement_for_special_parameters,
     so that references to $STATE and $MESSAGE will be substituted
     with the SQLSTATE and MESSAGE of the last SQL-statement executed,
     instead of "OK" strings to which this macro will clear them: */
  clear_SQL_state_and_message ();

  rc = SQLPrepare (stmt, UCP (whole_statement), SQL_NTS);
  IF_ERR_GO (stmt, error, rc);

  /* No parameters, no stuff following, no need to find END keyword. */
  /* DBDUMP produces rows like
     FOREACH BLOB INSERT INTO taulu(jymy,blobi) VALUES(1,NULL);
     without the END keyword when there are NULL in the blob column.
     So we have to conform here.
     However, things like:
     FOREACH INTEGER BETWEEN 1 100 insert into taulu(txt) values('kala');
     will work as expected, attempting to insert string 'kala' hundred times
     to table taulu. (Presumably there is an auto TIMESTAMP primary key).
   */
  if ((NO pars_found) && (NOT between_form))
    {
      end_found = 1;
      goto execute_statement;	/* And jump right to the middle of loop. */
    }

/* Otherwise, bind the parameters in this primitive little loop: */
/* The Parameter ?R (Repeat previous) will be "implemented" here
   almost gratis. Same with ?1 ?2 refer to first, second, etc,
   because all parameters except ?C will actually point to one and
   same buffer. This needs a thorough revision later.
   For now it will work sufficiently well for our needs.
 */
/* NOTE THAT WE SHOULD IMPLEMENT ALSO BULK INSERTS using an array
   of parameter values and SQLParamOption API-calls !!!
   E.g. syntax like:
   FOREACH BULK OF 1000 LINES IN FILE1 INSERT INTO tab values(?);
   (If number is left out, then tries to insert all lines in the file
   with one call, or maybe there has to be some maximum value for
   length of a parameter vector?)
 */

  for (ipar = 1; ipar <= ((SQLULEN) pars_found); ipar++)
    {
      int iscountparam = 0;
      UTCHAR c;
      if (ipar <= MAXPARS)
	{
	  switch (c = (UTCHAR) toupper ((SCP (pardescvec))[ipar - 1]))
	    {
	    case 'C':
	      {
		iscountparam = 1;
		break;
	      }

#ifdef LEADING_TO_IDIOTIC_USAGE_COMMENTED_OUT
/* The parameter numbers ?1, ?2, etc should always refer to actual
   parameters given after foreach, separated by commas, not to other
   ?-parameters in the statement itself. This means that to the count
   parameter it is only possible to refer as ?C, not by any tricky
   indirect reference.
   But maybe I can later use this piece of code for something useful.
 */
	    case 'R':
	      {
		iscountparam = ((ipar > 1) &&	/* If the previous was? */
				('C' == toupper (pardescvec[ipar - 2])));
		break;
	      }
	    case '1':
	    case '2':
	    case '3':
	    case '4':
	    case '5':
	    case '6':
	    case '7':
	    case '8':
	    case '9':
	      {
		c = c - '0';	/* From digit to number. */
		iscountparam = ((c <= pars_found) &&
				('C' == toupper (pardescvec[c - 1])));
		break;
	      }
#endif
	    default:
	      {
		break;
	      }
	    }			/* switch */
	}			/* if */


      rc = SQLBindParameter (stmt,
			     ((UWORD) ipar),
			     ((SWORD) SQL_PARAM_INPUT),
			  ((SWORD) get_proper_c_type (*type, iscountparam)),
			((SWORD) get_proper_sql_type (*type, iscountparam)),
			     0,	/* The precision of the column. SQLULEN */
			     0,	/* The scale of the column. SWORD */
			     ((PTR) (iscountparam ? ((PTR) & count) :
				 ((*type == PT_INTEGER) ? ((PTR) & intpar) :
/* With blobs give parameter num. */ ((*type == PT_BLOB) ? ((PTR) ipar)
/* With lines give input buffer: */ : ((PTR) inbuf))))),
			     0,	/* SQLLEN cbValueMax */
			     (iscountparam ? &cbCount : &cbInput));

      IF_ERR_GO (stmt, error, rc);
    }

  switch (*type)		/* This should be later inside the loop, with type */
    {				/* scanning over the types given comma-separated */
    case PT_INTEGER:
      {
	cbInput = 0;
	break;
      }
    case PT_BLOB:
      {
	cbInput = SQL_DATA_AT_EXEC;	/* ODBC 1.0 defined as -2 in sql.h */
/* Doesn't work yet with SQL_LEN_DATA_AT_EXEC(0); (of ODBC 2.0)
   which returns -100. Have to fix \wi\cliuti.c, function parm_to_dv !
   (Now it has been fixed for a long time...) */
	break;
      }
    case PT_LINE:
    case PT_TIMESTAMP:
    case PT_TIME:
    case PT_DATE:
      {
	cbInput = SQL_NTS;	/* Just a default value. Overridden elsewhere. */
	break;
      }
    }

  /* Now that the statement has been prepared and parameters bound, the
     loop over parameters themselves (i.e. lines or blobs from file
     or stdin) can begin. */

  if (loadexpr)
    {
      has_been_pushed = push_to_loadexpr_stack (loadexpr, in_fp);
    }

  for (; NOT end_found;)
    {
      if (NOT between_form &&
	  print_prompt && user_input_p (in_fp) && (*type != PT_BLOB))
	{
	  isql_printf (_T("foreach %ld> "), (long) count);
	}

      if (PT_BLOB != *type)
	{
	  if (NOT between_form)
	    {
/* WAS: if(end_found = NOT isqlt_fgetts(inbuf, inbuf_size, in_fp)) { break; } */
	      cbInput = bin_fgets (inbuf, inbuf_size, in_fp);
	      if (0 != (end_found = (0 == cbInput)))
		{
		  break;
		}
	      current_linecount ()++;
	      if (mime_boundary_token)
		{
		  int final_mime_token = 0;	/* Almost unnecessary. */
		  if (is_mime_boundary_token (inbuf, cbInput,
			       mime_boundary_token, blen, final_mime_token))
		    {
		      end_found = 1;
		      break;
		    }
		}
	      else
		{
		  if (token_found (inbuf, end_token))
		    {
		      end_found = 1;
		      break;
		    }
		}
	    }
	  if (PT_INTEGER == *type)
	    {
	      TCHAR *numpt;
	      get_rid_of_trailing_newline (inbuf);
	      numpt = skip_blankos (inbuf);
/* If the user has given FOREACH HEXADECIMAL INTEGER ... */
	      if (unpack_function_ptr == unhex_string)
		{
		  if (!*numpt || (0 == isqlt_stscanf (numpt, _T("%lx"), &intpar)))
		    {
		      isql_printf (_T("Invalid hex number \"%") PCT_S _T("\" "), numpt);
		      continue;
		    }
		}
	      else
		/* Normal, decimal input. */
		{
		  if (!*numpt ||
		  ((*numpt != '+') && (*numpt != '-') && !isqlt_istdigit (*numpt)))
		    {
		      isql_printf (_T("Invalid dec number \"%") PCT_S _T("\" "), numpt);
		      continue;
		    }
		  intpar = isqlt_tstol (numpt);
		}
	    }
	  else if (PT_LINE == *type)
	    {
	      cbInput
		-= get_rid_of_trailing_newline_and_CR_with_count (inbuf, cbInput);

/* If escaped/hexed/uuencoded option specified, then convert a possibly
   escaped/hexed/uuencoded string back to original. It is possible that
   there are escapes like \000 for NULL bytes. So we use the new length
   returned by the function instead of SQL_NTS as the length value of
   cbInput. */
	      if (NULLFP != unpack_function_ptr)
		{
		  cbInput = ((unpack_function_ptr) ((TCHAR *) UCP (inbuf)));
		}
	    }
	}
      else /* GK: there can be only one blob in a file */
	end_found = 1;


    execute_statement:
      rc = SQLExecute (stmt);

      /* Don't break for a small whines, like a duplicate value on the */
      IF_SEVERE_ERR_GO (stmt, error, again, rc);	/* primary key. */
      if (SQL_NEED_DATA == rc)	/* Blob, blob, blob? */
	{
	  rc = read_blob_from_input (statement, has_output, in_fp,
				     &current_linecount (),
				     end_token, separating_token,
				     mime_boundary_token,
				     type, unpack_function_ptr,
				     inbuf, inbuf_size,
				     &count, &end_found);
	  IF_SEVERE_ERR_GO (stmt, error, again, rc);
	}

      if (foreach_err_break && rc != SQL_SUCCESS &&
	  rc != SQL_SUCCESS_WITH_INFO)
	goto error;


      count++;

      SQLRowCount (stmt,  & sdtmp);
      N_rows = (int) sdtmp;

      if (has_output && (rc == SQL_SUCCESS))	/* Is select or call fun */
	{			/* Print the selected rows out without any banners */
	  unsigned long start = get_msec_count ();
	  rc = show_results (start, rc, !strcasecmp (statement, _T("CALL")),
			     0,	/* print_banner_flag */
			     0);	/* verbose_mode */
	  IF_SEVERE_ERR_GO (stmt, error, again, rc);
	  SQLFreeStmt (stmt, SQL_CLOSE);
	}			/* if(rc == SQL_SUCCESS) */
      else
	/* insert, update, delete, function call without call keyword */
	{
	  /* Currently, do nothing. */
	  if (foreach_err_break && rc != SQL_SUCCESS &&
	      rc != SQL_SUCCESS_WITH_INFO)
	    goto error;
	}

      if (between_form)
	{
	  if (NULL == succelem (((UTCHAR *) inbuf), inbuf_size, *type, intpar,
				((UTCHAR *) separating_token),
				((UTCHAR *) end_token)))
	    {
	      end_found = 1;
	      break;
	    }
	}
    }				/* for loop */

error:;

  if (has_been_pushed)
    {
      drop_from_loadexpr_stack ();
    }

  if ((NOT between_form) && (NOT end_found)
  /* && end_token */  && user_input_p (in_fp))
    {				/* No need to scan a possibly megabytes long file to the end.
				   It's only necessary when we are reading from the user input. */
      skip_to_end_token (in_fp, &current_linecount (), end_token,
			 inbuf, inbuf_size);
    }

  SQLFreeStmt (stmt, SQL_RESET_PARAMS);
  SQLFreeStmt (stmt, SQL_CLOSE);	/* Maybe called unnecessarily, maybe not. */

  if (SQL_SUCCESS != (rc = SQLTransact (henv, hdbc, SQL_COMMIT)))
    {
      print_error (((HENV) 0), hdbc, ((HSTMT) 0), rc);
      if (rc != SQL_SUCCESS_WITH_INFO)
	isql_exit (1);
    }

  return (count - 1);		/* It was one-based. */
}


/*
   The syntax is:

   foreach [HEXADECIMAL/ESCAPED/NONESCAPED] TYPE [FOLLOWING/ IN filename]
   parameterized-sql-statement;
   -- And if following was used, then lines must end with END
   end

   Where TYPE is either a line, integer or blob.
   If the keywords FOLLOWING or IN filename is omitted after type, then
   the following is the default.
   If escaped/nonescaped is omitted, then escaped is the default.
   (except when reading from a file)

   In the future may read comma-separated values (CSV?)

   foreach INTEGER, STRING, FLOAT, INTEGER IN filename insert

   And something like this: (Needs good regexp parser? Or use string.c)

   foreach NONESCAPED BLOB IN mailfile STARTED_BY 'From:* '
   insert into mails(mail_id,from_line,mail) values(?C,?S,?);

   ?S will be a new special parameter referring to the starting line of a
   multiline blob, bound as a normal VARCHAR string. That first line is
   also included in the blob itself. (If using instead the SEPARATED_BY
   keyword, then the line which matched to it, is not included.)

   SEPARATED_BY 'END*' (* default *)


   foreach LINE NOT LIKE '#*' insert ...

   foreach WORD, REST ON LINE NOT LIKE '#*' insert ...

   Other kind of loops also:

   foreach TABLE LIKE 't%' DROP TABLE %s;
   foreach SYSTEM TABLE ...
   foreach VIEW LIKE ...

   foreach DIRECTORY LIKE '*'
   foreach FILE LIKE '%s/ *.blob' -- %s would refer here to directory names
   --                                generated by the first foreach part.
   -- And not until in this last part they would refer to filenames itself:
   foreach BLOB IN %s insert into blobtable(filename,blob) values(%s,?);

   What about, something like:

   foreach integer between $I $- $ARGC 1 create table \$ARGV[?](a integer, etc.);

   Probably we should use a more civilized syntax, something like ?D and ?F
   etc...

 */

#ifndef TMPBUF_SIZE
#define TMPBUF_SIZE 81
#endif

#define w_or_null(X) ((X) ? (X) : _T("NULL"))

int
isql_foreach (TCHAR *text)
{
  unsigned long start = get_msec_count ();
  TCHAR *ptr;
  TCHAR *optionword = NULL, *typeword = NULL, *nextword = NULL, *statement = NULL;
  TCHAR *filename = NULL;
  TCHAR *own_inbuf = input;	/* By default use common buffer. */
  int own_inbuf_size = INPUT_SIZE;	/* And the size of common buffer. */
  FILE *in_fp = current_input_stream ();	/* Was: stdin */
  PFI unpack_function_ptr = unescape_string;
  int following = 1, between_form = 0;
  int type = 0;
  int items_read = 0;
  TCHAR *end_token = _T("END"), *separating_token = _T("BLOB");
  TCHAR *mime_boundary_token = NULL;
  TCHAR tmpbuf[TMPBUF_SIZE + 2], tmp2buf[TMPBUF_SIZE + 2];
  TCHAR tmp3buf[TMPBUF_SIZE + 2], tmp4buf[TMPBUF_SIZE + 2];
  TCHAR tmp5buf[TMPBUF_SIZE + 2], tmp6buf[TMPBUF_SIZE + 2];


  ptr = text;

  /* First, skip the FOREACH command keyword itself. */
  optionword = get_next_token (&ptr, tmpbuf, TMPBUF_SIZE, NULL, 0);
  if (NULL == optionword)
    {
      goto premature_end;
    }

/* Was: ptr = text+(sizeof("FOREACH")-1); */

/*  isql_printf("isql_foreach(%s/%d): ptr=%s\n",text,isqlt_tcslen(text),ptr); */

  optionword = get_next_token (&ptr, tmpbuf, TMPBUF_SIZE, NULL, 0);
  if (NULL == optionword)
    {
      goto premature_end;
    }


  if (!strncasecmp (optionword, _T("ESC"), 3))
    {
      unpack_function_ptr = unescape_string;
    }
  else if (!strncasecmp (optionword, _T("HEX"), 3))
    {
      unpack_function_ptr = unhex_string;
    }
  else if (!strncasecmp (optionword, _T("NON"), 3))
    {
      unpack_function_ptr = NULLFP;
    }
  else
    {
      typeword = optionword;
    }

  if (!typeword)
    {
      typeword = get_next_token (&ptr, tmp2buf, TMPBUF_SIZE, NULL, 0);
      if (NULL == typeword)
	{
	  goto premature_end;
	}
    }

  if (!strcasecmp (typeword, _T("LINE")))
    {
      type = PT_LINE;
    }
  else if (!strcasecmp (typeword, _T("INTEGER")))
    {
      type = PT_INTEGER;
    }
  else if (!strcasecmp (typeword, _T("BLOB")))
    {
      type = PT_BLOB;
    }
  else if (!strcasecmp (typeword, _T("TIMESTAMP")))
    {
      type = PT_TIMESTAMP;
    }
  else if (!strcasecmp (typeword, _T("TIME")))
    {
      type = PT_TIME;
    }
  else if (!strcasecmp (typeword, _T("DATE")))
    {
      type = PT_DATE;
    }
  else
    {
      isql_fprintf (error_stream,
		    _T("\n*** Error: unrecognized type \"%") PCT_S _T("\" for foreach at line %ld of %") PCT_S _T(": %") PCT_S _T("\n"),
	       typeword, latest_statement_begins_at (), current_loadexpr (),
		    input);
      /* skip to the end ? */
      return (0);		/* isql_exit(1); */
    }

  nextword = get_next_token (&ptr, tmp3buf, TMPBUF_SIZE, NULL, 0);
  if (NULL == nextword)
    {
      goto premature_end;
    }

  else if (!strcasecmp (nextword, _T("FOLLOWING")))
    {
      following = 1;
    }
  else if (!strcasecmp (nextword, _T("IN")))
    {
      int might_be_stdin = 0;
      following = 0;
/* If no ESCAPED/NONESCAPED were specified when inputting stuff from
   file, then use NONESCAPED as a default: */
      if (typeword == optionword)
	{
	  unpack_function_ptr = NULLFP;
	}
      end_token = NULL;
      separating_token = NULL;

/* If IN is followed by - or -b (but not in quotes as '-')
   then read the stuff from stdin. */
      ptr = skip_blankos (ptr);
      if (*ptr == '-')
	{
	  might_be_stdin = 1;
	}

      filename = get_next_token (&ptr, tmp4buf, TMPBUF_SIZE, NULL, 0);
      if (NULL == filename)
	{
	  goto premature_end;
	}
      if (might_be_stdin &&
	  (!strcasecmp (filename, _T("-")) ||
	   (might_be_stdin++ && !strcasecmp (filename, _T("-B")))))
	{
	  in_fp = stdin;
#ifdef WIN32
	  /* Currently this is not undone anywhere...
	     (E.g. after END token is encountered). */
	  if (might_be_stdin > 1)	/* User used -b or -B */
	    {
	      setmode (fileno (stdin), _O_BINARY);	/* _O_TEXT */
	    }
#endif
	}

      else if (!(in_fp = isqlt_tfopen (filename,
#ifdef WIN32
				_T("rb")	/* Use binary mode in Windows. */
#else
				_T("r")	/* Unix is Unix is Unix is Good. */
#endif
		 )))
	{
	  isqlt_tperror (progname);
	  isql_fprintf (error_stream,
			_T("\n*** Error: can't open file \"%") PCT_S _T("\" for foreach at line %ld of %") PCT_S _T(": %") PCT_S _T("\n"),
	       filename, latest_statement_begins_at (), current_loadexpr (),
			input);
	  /* skip the statement itself ? */
	  goto error;
	}
    }
  else if (!strcasecmp (nextword, _T("BETWEEN")))
    {
      int len1, len2;
      following = 0;
      between_form = 1;
      separating_token = get_next_token (&ptr, tmp4buf, TMPBUF_SIZE, NULL, 0);
      if (NULL == separating_token)
	{
	  goto premature_end;
	}
      end_token = get_next_token (&ptr, tmp6buf, TMPBUF_SIZE, NULL, 0);
      if (NULL == end_token)
	{
	  goto premature_end;
	}
      len1 = (int) isqlt_tcslen (separating_token);
      len2 = (int) isqlt_tcslen (end_token);
      if (len2 > len1)
	{
	  len1 = len2;
	}			/* Get the max length of start, end */
      len1 += 20;		/* And little bit more to be sure. */
      own_inbuf_size = len1;
      own_inbuf = (TCHAR *) chemalloc (own_inbuf_size + 1, _T("isql_foreach"));
/* chemalloc never returns NULL.
   if(NULL == own_inbuf)
   {
   isql_fprintf (error_stream,
   "\n*** Error: foreach cannot allocate buffer of size %u at line %ld of %s: %s\n",
   (own_inbuf_size+1),latest_statement_begins_at(),
   current_loadexpr(), input);
   goto error;
   }
 */
      if (type != PT_INTEGER)
	{
	  isql_fprintf (error_stream,
	      _T("\n*** Error: foreach ... between does support integers only at line %ld of %") PCT_S _T(": %") PCT_S _T("\n"),
	      latest_statement_begins_at (), current_loadexpr (),
	      input);
	  /* skip to the end ? */
	  return (0);		/* isql_exit(1); */
	}
    }
  else
    {
      statement = nextword;
      goto do_it;
    }


  statement = get_next_token (&ptr, tmp5buf, TMPBUF_SIZE, NULL, 0);
  if (NULL == statement)
    {
      goto premature_end;
    }

do_it:;

/* If no BETWEEN is used, then statement may be preceded by the
   optional MIME_BOUNDARY 'TOKEN' pair. */
  if (!between_form && !strcasecmp (statement, _T("MIME_BOUNDARY")))
    {
      if (!(mime_boundary_token
	    = get_next_token (&ptr, tmp5buf, TMPBUF_SIZE, NULL, 0)))
	{
	  goto premature_end;
	}
      if (!(statement
	    = get_next_token (&ptr, tmp6buf, TMPBUF_SIZE, NULL, 0)))
	{
	  goto premature_end;
	}
    }

/* Then get the rest of the parameterized statement itself
   (upto the overwritten semicolon ; ). */
/* Just rewind ptr back to the beginning of statement: */
  ptr -= isqlt_tcslen (statement);

/*
   isql_printf(
   "optionword=%s,typeword=%s,nextword=%s,statement=%s,filename=%s,escaped=%d\n",
   w_or_null(optionword),w_or_null(typeword),w_or_null(nextword),
   w_or_null(statement),w_or_null(filename),escaped);
   fflush(stdout);
 */
  items_read =
    exec_for_foreach (statement,	/* 1st word: select, insert, etc. */
		      ptr,	/* The whole SQL-statement in input buffer */
		      (!strcasecmp (statement, _T("SELECT")) ||
		       !strcasecmp (statement, _T("CALL"))),	/* Has output? */
		      in_fp,	/* Is by default current_input_stream() */
		      (filename ? text : NULL),		/* As loadexpr */
		      end_token,	/* end token */
		      separating_token,		/* separating token */
		      mime_boundary_token,	/* Overrides above ones if given. */
		      &type,	/* pointer to type */
		      unpack_function_ptr,
		      own_inbuf,	/* Was: input, */
		      own_inbuf_size,	/* Was: INPUT_SIZE, */
		      between_form);

  if (verbose_mode)
    {
      isql_fprintf (stdout,
		    _T("-- foreach: %") PCT_S _T("%") PCT_S _T(" %d %") PCT_S _T("s %") PCT_S _T("%") PCT_S _T(" in %ld msec.\n"),
		    statement,
	      ((*(statement + isqlt_tcslen (statement) - 1) == 'e') ? _T("d") : _T("ed")),
		    items_read, typeword,
		    (filename ? _T("in ") : _T("")),
		    (filename ? filename : _T("")),
		    (((unsigned long int) get_msec_count ()) - start));
      fflush (stdout);
    }

  if (own_inbuf != input)	/* if(between_form) */
    {
      free (own_inbuf);
    }
  if (filename && (in_fp != stdin))
    {
      fclose (in_fp);
    }				/* Using IN */

  return (items_read);

premature_end:;

  isql_fprintf (error_stream,
   _T("\n*** Error: Input prematurely cut for foreach at line %ld of %") PCT_S _T(": %") PCT_S _T("\n"),
		latest_statement_begins_at (), current_loadexpr (), input);
error:
  if (following)		/* I.e. not reading from file */
    {
      skip_to_end_token (in_fp, &current_linecount (), end_token, input, INPUT_SIZE);
    }
  if (in_fp && (in_fp != stdin))
    {
      fclose (in_fp);
    }				/* Using IN */
  return (1);
}


int
isql_echo (TCHAR *text)
{
  int echoln = 0;		/* Is newline printed at the end? */
  int raw_mode = 0;		/* If 1, then never print it with HTTP-headers. (I.e. we are printing with it our own headers) */
  FILE *out_fp = stdout;	/* By default. */
  TCHAR *ptr, *word;
  TCHAR tmpbuf[TMPBUF_SIZE + 2];

  ptr = text;

  /* First, skip the ECHO command itself. */
  word = get_next_token (&ptr, tmpbuf, TMPBUF_SIZE, NULL, 0);
  if (NULL == word)
    {
      goto premature_end;
    }
  if (0 == strncasecmp (word, _T("ECHOLN"), (sizeof (_T("ECHOLN")) - 1)))
    {
      echoln = 1;
    }

  if (0 == strncasecmp ((word + isqlt_tcslen (word) - 3), _T("RAW"), 3))
    {
      raw_mode = 1;
    }

  /* If the next word begins with single or double quotes then
     output to stdout in any case.
   */

  ptr = skip_blankos (ptr);
  if (('"' == *ptr) || ('\'' == *ptr))
    {
      goto loop_start;
    }

  word = get_next_token (&ptr, tmpbuf, TMPBUF_SIZE, NULL, 0);
  if (NULL == word)
    {
      goto premature_end;
    }

  if (!strcasecmp (word, _T("stderr")))
    {
      out_fp = stderr;
    }
  else if (!strcasecmp (word, _T("stdout")))
    {
      out_fp = stdout;
    }
  else if (!strcasecmp (word, _T("error_stream")))
    {
      out_fp = error_stream;
    }
  else if (!strcasecmp (word, _T("both")))
    {
      out_fp = NULL;
    }
  else
    {
      isqlt_tcsncpy (isql_echo_word_buf, word, ISQL_ECHOBUF_SIZE);
      isql_echo_lwe = isql_echo_word_buf;
      goto right_in;
    }

loop_start:
  while (NULL != (word =
	 get_next_token (&ptr, isql_echo_word_buf, TMPBUF_SIZE, NULL, 0)))
    {
      isql_echo_lwe = word;
    right_in:
      if (NULL == out_fp)	/* I.e. echo to BOTH streams. */
	{
	  if (raw_mode)
	    {
	      isqlt_fputts (isql_echo_lwe, stdout);
	      isqlt_fputts (isql_echo_lwe, stderr);
	    }
	  else
	    {
	      isql_fputs (isql_echo_lwe, stdout);
	      isql_fputs (isql_echo_lwe, stderr);
	    }
	}
      else
	{
	  if (raw_mode)
	    {
	      isqlt_fputts (isql_echo_lwe, out_fp);
	    }
	  else
	    {
	      isql_fputs (isql_echo_lwe, out_fp);
	    }
	}
    }

  if (echoln)
    {
      TCHAR *newline = _T("\n");
      if (NULL == out_fp)	/* I.e. echo to BOTH streams. */
	{
	  if (raw_mode)
	    {
	      isqlt_fputts (newline, stdout);
	      isqlt_fputts (newline, stderr);
	    }
	  else
	    {
	      isql_fputs (newline, stdout);
	      isql_fputs (newline, stderr);
	    }
	}
      else
	{
	  if (raw_mode)
	    {
	      isqlt_fputts (newline, out_fp);
	    }
	  else
	    {
	      isql_fputs (newline, out_fp);
	    }
	}
    }

  if (NULL == out_fp)
    {
      fflush (stdout);
      fflush (stderr);
    }
  else
    {
      fflush (out_fp);
    }
premature_end:;
  return (1);
}


/* ================================================================ */
/*     STUFF FOR HELP AND PARSING ISQL COMMANDS, AND RELATED        */
/* ================================================================ */

/* Returns non-zero if text begins with cmd, and the next character
   is either a terminating zero (i.e. it also ends with cmd), or
   at least is something non-alpha-numeric, e.g. a white space.
   Note: old version uese !isalnum but load_xxx is the case
         tried to use isqlt_istspace instead of !isalnum
 */
int
is_cmd (TCHAR *text, TCHAR *cmd)
{
  int len = (int) isqlt_tcslen (cmd);
  return ((0 == strncasecmp (text, cmd, len))
	  &&
	  (empty_stringp (text + len) || isqlt_istspace (*(text + len)))
    );
}


#define is_arg is_cmd

void
isql_help (TCHAR *text)
{

  if (is_cmd (text, _T("SET")))
    {
      isql_printf (
_T("SET TIMEOUT sec       to set transaction timeout to sec seconds\n")
_T("    AUTOCOMMIT ON/OFF/MANUAL to set whether to autocommit or not.\n")
_T("                      Default is OFF\n")
_T("    READMODE SNAPSHOT or NORMAL to switch between non-locking and normal read\n")
/* Deprecated, replaced by READMODE above.
_T("    RO                to switch to non-locking historical (versioned) read\n")
_T("    RW                to switch to normal shared locking\n")
*/
_T("    ACCESSMODE RO or RW to switch between read-only and read-write connection\n")
_T("    MAXROWS num       to set the max. limit of rows printed with select.\n")
_T("                      Use the value zero to print always all rows.\n")
_T("    DEADLOCK_RETRIES num  to set the max. limit of deadlock retries.\n")
_T("    BLOBS ON or OFF   to control whether LONG VARCHAR columns are printed\n")
_T("    TIMES2STRINGS ON or OFF to control how timestamps are printed\n")
_T("    BANNER ON or OFF  to control whether the banner is shown with select\n")
_T("    TYPES ON or OFF   to control whether column types are shown in banner\n")
_T("    VERBOSE ON or OFF to control whether information about execution times,\n")
_T("                      etc, are printed.\n")
_T("    PROMPT New Prompt> or OFF to set the new prompt or turn off prompting\n")
_T("    HIDDEN_CRS CLEARED or PRESERVED to control whether trailing CRs of MS-DOS\n")
_T("                   files are cleared from foreach blobs. Note that all explicit\n")
_T("                   \\15 escape-sequences are always kept in the content.\n")
_T("    ERRORS STDOUT or STDERR to control where error messages are directed.\n")
_T("    ECHO ON or OFF    to control whether statements are echoed.\n")
_T("Use SHOW to show the current values of these parameters.\n"));

      if (print_prompt)
	{
	  isql_printf (_T("Help more?>"));
	  getchar ();
	}

      isql_printf (
_T("You can specify these options on the command line also, for example\n")
_T("%") PCT_S _T(" VERBOSE=OFF BANNER=OFF PROMPT=OFF BLOBS=ON ERRORS=STDOUT\n")
_T("You can also run one or more ISQL-commands directly from the command line with\n")
_T("EXEC=statement option. E.g. \"EXEC=select * from fyyl\" The statement(s)\n")
_T("are executed and no further input is asked, unless foreach statement is used.\n"),
progname
	);
    }

  else if (is_cmd (text, _T("FOREACH")))
    {
      isql_printf (
_T("FOREACH [HEXADECIMAL/ESCAPED/NONESCAPED] INTEGER/LINE/BLOB [IN <filename>]\n")
_T(" <sql statement with one parameter, and one or more special parameters>\n")
_T("\n")
_T("If  IN <filename>  part is omitted then data is read from standard input\n")
_T("until a line beginning with the keyword END is encountered.\n")
_T("To insert in this mode a line beginning with END into a string or a blob\n")
_T("you have to prefix it with the escape character backslash (e.g. \\END)\n")
_T("Also you can use in line or blob data all standard C-like escapes:\n")
_T("\\n for newline, \\ooo for any octal byte ooo, even \\0 for a null byte,\n")
_T("unless you explicitly turn the backslash parsing off with NONESCAPED option.\n")
_T("\n")
_T("If  IN <filename>  is present, then data is read from the file <filename>\n")
_T("and by default, unless ESCAPED option is explicitly specified, there is no\n")
_T("checking either for any escape characters or the END keyword, instead\n")
_T("the data is read in just as it occurs, upto the end of that file.\n")
_T("\n")
_T("Except the standard ? you can use the following 'special parameters' in the\n")
_T("statement: ?R for repeating the previous parameter, ?1, ?2 for using the first,\n")
_T("second, nth parameter. ?C is the special parameter bound to consecutive\n")
_T("integers from 1 onward, which is the count of successful operations so far,\n")
_T("e.g. you can use it in INSERT to generate unique values for the primary key.\n")
	);
      if (print_prompt)
	{
	  isql_printf (_T("Help more?>"));
	  getchar ();
	}

      isql_printf (
_T("Examples:\n\n")
_T("FOREACH LINE IN words.lst INSERT INTO wordtable(word,len) VALUES(?,length(?1));\n")
_T("  Inserts each line from the file words.lst (with the trailing newline removed)\n")
_T("  into the table wordtable, whose column named word should be of type VARCHAR,\n")
_T("  and defined as a primary key. At the same time, then length of the word is\n")
_T("  put into column len. If there are any duplicate lines in words.lst then a\n")
_T("  warning message is printed for them, but the execution still continues.\n")
_T("\n")
_T("FOREACH INTEGER SELECT humpty FROM pumpkin WHERE dumpty = ?;\n")
_T("  Inputs numbers from user, and selects from the table pumpkin those humpties\n")
_T("  whose dumpty is in the values given by the user. Doesn't output any banners.\n")
_T("\n")
_T("FOREACH HEXADECIMAL INTEGER dbg_printf('%%d times %%d is %%d',?,?C,?C*?1);\n")
_T("  Inputs hexadecimal numbers from user (until stopped with END) and shows on\n")
_T("  the console of server what their product is with a line count.\n")
_T("\n")
_T("FOREACH BLOB IN picture1.gif INSERT INTO pictures(pid,pictdata) VALUES(1,?);\n")
_T("  Insert the file picture1.gif as a blob into the table pictures, with\n")
_T("  a primary key pid set to one. Column pictdata should have been defined as of\n")
_T("  type LONG VARCHAR. When reading blobs from files this way, it is not\n")
_T("  possible to read more than one blob from one file. Instead, the whole file\n")
_T("  is inserted as one blob, in the exact binary format.\n")
	);
    }

  else
    {
      isql_printf (
_T("OpenLink Interactive SQL ") ISQL_TYPE _T(".\n")
_T("Type a SQL statement followed by a ';'.\n")
_T("\n")
_T("Database and Table information:\n")
_T("    TABLEQUALIFIERS                  Show all qualifiers\n")
_T("    TABLEOWNERS                      Show all owners\n")
_T("    TABLETYPES                       Show all table types\n")
_T("    TABLES [tablename_pattern] [/table_type]  Show tables in the database.\n")
_T("    e.g: TABLES a%%/TABLE; (Show all ordinary tables beginning with 'a')\n")
_T("    COLUMNS [tablename] [/colname]   COLUMNPRIVILEGES tablename [/colnamepat]\n")
_T("    PROCEDURES [procname_pattern]    TABLEPRIVILEGES [tablename_pattern]\n")
_T("    PRIMARYKEYS [tablename]          STATISTICS [tablename] (show indices)\n")
_T("    SPECIALCOLUMNS [tablename][/B]   GETTYPEINFO\n")
_T("    FOREIGNKEYS [tablename][/tablename]\n")
_T("\n")
#ifndef ODBC_ONLY
_T("Server Management:\n")
_T("    SHUTDOWN [log];    Shut server.\n")
_T("                       If [log] is given, preserve old log and make [log]\n")
_T("                       new transaction log file.\n")
_T("    CHECKPOINT [log];  Make a checkpoint.\n")
_T("                       If [log] is given, preserve old log and make [log]\n")
_T("                       new transaction log file.\n")
_T("    status();           Display database server status.\n")
#endif
_T("\n")
_T("Other commands:\n")
_T("    FOREACH   Execute parameterized statement multiple times. See HELP FOREACH\n")
_T("    CALL <procedure> (<argument>...)   Call the procedure and display\n")
_T("        result sets. Result sets are not shown if the CALL keyword is omitted.\n")
_T("    SET          Set various parameters. Type HELP SET to see them.\n")
_T("    SHOW         Show current values of various parameters.\n")
_T("    LOAD <file>  Load and execute statements from <file>.\n")
_T("    HELP         Show this help text. HELP topic  Show help on some topics.\n")
_T("    EXIT         Exit %") PCT_S _T("\n"), progname);
    }
  fflush (stdout);
}


int
add_file_to_be_loaded (TCHAR *filename)
{
  if (load_n_files < MAX_FILES_FROM_COMMAND_LINE)
    {
      files_to_load[load_n_files++]
	= chestrdup (filename, _T("add_file_to_be_loaded"));
      return (load_n_files);
    }
  else if (load_n_files == MAX_FILES_FROM_COMMAND_LINE)
    {
      isql_fprintf (error_stream,
		    _T("%") PCT_S _T(": Max. %d files can be specified at the command line, ignoring all\n")
		    _T("files from %") PCT_S _T(" onward.\n"),
		    progname, MAX_FILES_FROM_COMMAND_LINE, filename);
      load_n_files++;
    }
  else
    {				/* Do nothing, just skip the filenames. */
    }

  return (0);			/* Doesn't fit into vector anymore, return zero as status */
}


TCHAR *
get_arg_after_braces (TCHAR *arg, TCHAR *tmp2buf, int bufsize, int get_all_raw)
{				/* Arg should be pointing to the closing brace or bracket or paren. */

  /* Search for the first point after a closing bracket: */
  for (; *arg && !isqlt_istspace (*((UTCHAR *) arg)) && (*arg != '='); arg++)
    {
    }
  arg = skip_blankos (arg);	/* And now to = or value itself. */
  if ('=' == *arg)		/* A command line option or var=val syntax? */
    {
      arg++;			/* Skip past the equal sign to the value itself. */
    }

/* Then run arg through get_next_token so that $-macros are handled right */
  if (0 == get_all_raw)
    {
      arg = get_next_token (&arg, tmp2buf, bufsize, NULL, 0);
    }
  /* if(NULL == arg) { } *//* Nothing following, set it to NULL */

  return (arg);
}



/* This might later contain a matching function: */
#define show_this_one(A) (show_instead_of_set && !*(A))

#define set_or_show_this_one(A)\
 ((is_macro_name (macro_name,(A)) && (just_this_one=1)) ||\
  ( (just_this_one=0), show_this_one((macro_name)) ) )

/* Might be called from is_isql_command (with the text
   argument being a pointer to after SET or SHOW word)
   or from the main with text argument being something
   like BLOBS=ON
   (Now also for HTTP URL-variables if used in web_mode)

   Returns 0 if text is not recognizable ISQL command,
   e.g. if it is just a syntax error, or real SQL-command
   like SET PASSWORD or SET USER GROUP that needs to be sent
   to the server.
   Returns 1 if text is a valid ISQL command.
   Returns 2 if text is a valid ISQL command that would require
   a full connection to the datasource, but we have not
   connected yet. In that case the text is saved to delayed_settings
   vector and 2 is returned.

 */

#define is_url_option(command_line_option) (2 == command_line_option)

#define is_set_subcommand(T,S,C) is_set_subcommand_aux((T),(S),(C),0)

/* If ending_point is non-zero, then this is called from
   handle_multipart_form and no urldecoding should be used.
   Check and clear this later!
 */
int
is_set_subcommand_aux (TCHAR *text, int show_instead_of_set,
		       int command_line_option,
		       int ending_point)
{
  int just_this_one = 0;
  TCHAR *argptr, *arg;
  TCHAR *fun = _T("is_set_subcommand");
  struct name_var_pair *macro_info;
  TCHAR *macro_name;
  TCHAR tmp0buf[TMPBUF_SIZE + 2], tmp1buf[TMPBUF_SIZE + 8];

/*  char *str_ptr = text; */

  /* The following is probably unnecessary but let's be absolutely sure
     that text points to the beginning of macro/option name to be set/shown: */
  text = skip_blankos (text);
  argptr = text;

/*
   isql_fprintf(error_stream,"is_set_subcommand_aux: argptr=%s, ending_point=%d\n",argptr,ending_point);
   fflush(error_stream);
 */

  /* First get the macro word to be set/shown: */
  if (NO (macro_name = get_next_token (&argptr, tmp0buf, TMPBUF_SIZE,
				       NULL, 0)))
    {
/* There was nothing following the SET or SHOW command. Let's
   return just zero for SET, to tell the calling function to send the
   command to the server instead, which presumably will give a
   syntax error, and for empty SHOW we dump all the settable macros
   out. */
      if (show_instead_of_set)	/* For empty SHOW commands */
	{
	  dump_all_settable_variables (macro_name);	/* Arg not used now. */
	  return (1);
	}
      else
	{
	  return (0);
	}
    }

  if (is_url_option (command_line_option))
    {
      int len = (int) isqlt_tcslen (SYSTEM_VARIABLE_URL_PREFIX);
      if (0 == isqlt_tcsncmp (macro_name, SYSTEM_VARIABLE_URL_PREFIX, len))
	{
	  macro_name += len;	/* If there's a prefix, then skip it. */
	  text += len;		/* Also the text itself, if it is saved for later
				   consumption. */
	}
      else
	/* No prefix, wrap it inside U{' and '} */
	{			/* Where U stands for User/URL variables. */
	  /* This only happens when we are called for command line
	     or URL options, so input buffer should be free.
	     When called from handle_multipart_form input is used,
	     but in that case there is enough space reserved for
	     this operation...
	     (This is much too kludgous now...)
	   */
	  argptr = skip_blankos (argptr);
	  isqlt_stprintf (input, _T("{'%") PCT_S _T("'}"), macro_name);
	  my_strncat (input, argptr, INPUT_SIZE);
	  argptr = input;
	  macro_name = _T("U");
	}
    }

/*
   isql_fprintf(error_stream,"is_set_subcommand_aux: macro_name=%s, argptr=%s\n",
   macro_name,argptr);
   fflush(error_stream);
 */

  /* argptr now points to the first char after macro name. */
  argptr = skip_blankos (argptr);	/* And now to = or value itself. */
  if ('=' == *argptr)		/* A command line option or SET VAR=VAL syntax. */
    {
      argptr++;			/* Skip past the equal sign to the value itself. */
    }

/* Special: Set the SQL-command to be executed from the command line.
   Run this before taking arg from argptr with get_next_token,
   if this makes it any better...
   Because sometimes it would crash isql if it were run after the
   call to get_next_token and text were long enough and/or contained
   something peculiar...
   (happens e.g. with spawning in tconcur2.sql test).
   Probably this indicates a presence of a nasty bug in get_next_token
   that awaits to be corrected...
 */
  if (NO show_instead_of_set && is_macro_name (macro_name, _T("EXEC")))
    {
      if (execs_from_cmdline >= MAXEXECS_FROM_CMDLINE)
	{
	  isql_fprintf (error_stream,
			_T("\n%") PCT_S _T(": **Too many EXECs from command line. Max. %d allowed. (%") PCT_S _T(" is %dth)\n"),
	   progname, MAXEXECS_FROM_CMDLINE, text, (execs_from_cmdline + 1));
	  isql_exit (1);
	}
      execute_these_only[execs_from_cmdline++]
	= (ending_point ? chestrdup (argptr, fun) : ((TCHAR *) parse_url_value ((UTCHAR *) argptr)));
/* We always need apply parse_url_value here whether this is used
   via Web or not, as spawning syntax (lines ended with &,
   see function spawn_to_background) gives its EXEC=option to a new
   spawned subprocess of isql as URL-percent-encoded, done with
   the function urlify (to avoid all stupidities with single and
   doublequotes) */
      return 1;
    }

  /* In web-mode we always run args through unurlify process,
     unless the data comes from multi-part form.
     Note that parse_url_value does a conversion in place, producing
     possibly a shorter string than original (i.e. if there were any
     percent-escapes used).
   */
  if (web_mode && command_line_option && !ending_point)
    {
      parse_url_value (UCP (argptr));
    }

/* Then run arg through get_next_token so that $-macros are handled right,
   and leave argptr to point to the beginning of arg, in raw mode.
   (In case of $U{stuff} and $ARGV  [index] it will first point to the
   opening brace or bracket.)
 */
  arg = argptr;

/*
   Before the version "0.9849b" the condition was like this:
   (which is quite illogical, as it applies only to multipart-forms)

   if(!ending_point) ...

   Replaced by this:
 */
  if (!command_line_option	/* Given with explicit SET from ISQL-prompt? */
/*    ||  Or begins with a dollar, if from command-line, URL or multipart-form?
   ('$' == *arg)   but commented out also! See below. */
    )
    {
      arg = get_next_token (&arg, tmp1buf, TMPBUF_SIZE, NULL, 0);
    }
  if (NULL == arg)
    {
      arg = _T("");
    }				/* Nothing following. */


/* Problem:
   How the user can give text beginning with dollars via Web-forms?

   Why the dollar is not instead in the end of the variable to be set?

   So, instead of:
   <INPUT TYPE=TEXT NAME=BKFILE SIZE=20 VALUE="&#36;YYYYMMDD&#46;log">
   we would have:

   <INPUT TYPE=TEXT NAME="BKFILE&#36;" SIZE=20 VALUE="YYYYMMDD&#46;log">

   Balderash! Currently, we won't need that. If there's an input-field
   beginning with a dollar, coming from HTML-form or command-line arg,
   then it's handled literally, as a piece of text beginning with a
   dollar.
   User can still embed dollar-forms in statements given with S_EXEC
   from HTML-forms, and they will have their normal effect.
 */

/* Ignore all empty URL input variables like
   &AUTOCOMMIT=&READMODE=&TIMEOUT= etc... in web mode,
   as well as all empty settings from command line, e.g.
   like in case isql TIMEOUT=%1  where %1 is not given,
   that is, keep their default values.
 */
  if (NOT show_instead_of_set && command_line_option &&
      ((NOT arg) || empty_stringp (arg)))
    {
      return (1);
    }


  if ((macro_info = find_var_info (macro_name)) &&
      (show_instead_of_set || is_settable_macro_type (macro_info)))
    {				/* It's a standard macro found from the list isql_variables */
      int stat;
      TCHAR *value;

      if (show_instead_of_set)
	{
	  value = convert_macro_to_text (macro_info, &argptr,
					 tmp1buf, TMPBUF_SIZE,
					 NULL, 0, 1);
	  if (NULL == value)
	    {
	      value = _T("NULL");
	    }
	  isql_fputs (macro_info->name, stdout);
	  isql_fputs (_T("\tis "), stdout);
	  if (in_HTML_mode () && oo_esc)
	    {
	      html_print_escaped (value, stdout);
	    }
	  else
	    {
	      isql_fputs (value, stdout);
	    }
	  isql_fputs (_T("\n"), stdout);
	  fflush (stdout);
/* WAS: isql_printf("%s\tis %s\n",macro_info->name,(value ? value : "NULL")); */
	}
      else
	{
/* All SAM_macros (like AUTOCOMMIT, READMODE, TIMEOUT) need a
   connection to be established and a statement handle to be
   allocated, so if we are not yet fully connected, then we
   just save the setting text (e.g. AUTOCOMMIT=ON or TIMEOUT=86400)
   to the delayed_settings vector, which is then later, after the
   connection has been made, passed through, with this function
   (is_set_subcommand) called once again for each element. Note
   that this means that parse_url_value is called twice for these
   delayed settings, but currently it doesn't matter, as all the
   arguments of these should be simple strings like ON, OFF, RO,
   RW, numbers like 1000, etc.
   (Even "+1234" is corrupted only slightly, to " 1234", which is
   effectively the same number).
 */

	  if (command_line_option && (NOT fully_connected) &&
	      macro_needs_connection (macro_info))
	    {
	      if (n_delayed_settings >= MAX_DELAYED_SETTINGS)
		{
		  isql_fprintf (error_stream,
				_T("\n%") PCT_S _T(": **Too many delayed settings from command line. Max. %d allowed. (%") PCT_S _T(" is %dth)\n"),
				progname, MAX_DELAYED_SETTINGS, text, n_delayed_settings + 1);
		  isql_exit (1);
		}
	      else
		{
		  delayed_settings[n_delayed_settings++]
		    = (ending_point ? chestrdup (text, fun) : text);
		}
	      return (2);	/* Was 1. */
	    }
	  else
	    /* Normal usage. Just set it. */
	    {
#ifdef WIN32
	      int old_flag_binary_output = flag_binary_output;
#endif
	      stat = set_value_to_macro (macro_info, arg, argptr, ending_point);
#ifdef WIN32
/* BINARY_OUTPUT=ON is a Windows kludge for avoiding that
   NL's are converted to CR+LF's in output. Needed for the exact
   fetching of blobs (Use together with TRAILING_NEWLINE=0).
   On Unix platforms BINARY_OUTPUT is NO-OP.
 */
	      if (flag_binary_output != old_flag_binary_output)
		{
		  setmode (fileno (stdout),
			   (flag_binary_output ? _O_BINARY : _O_TEXT));
		}
#endif
	      if (is_macro_name (macro_name, _T("LOCALE")))
		{
		  isql_locale = setlocale (LC_ALL, arg);
		  if (!isql_locale)
		    {
		      isql_fprintf (error_stream, _T("%") PCT_S _T(": Warning: setlocale \"%") PCT_S _T("\" failed.\n"), progname, arg);
		      isql_locale = setlocale (LC_ALL, NULL);
		    }
		}
          return 1;
	    }
	}

      return (1);
    }				/* macro_info for this name was found. */


/* If not found from the standard macro list, then it might be one of
   the remaining special options, LOAD, ARGV */

/* Difference between LOAD=filename and plain filename (given as
   fourth or later non-option command line argument) is that
   in web_mode with LOAD= the filename is passed through parse_url_value.
 */
  if (NO show_instead_of_set && is_macro_name (macro_name, _T("LOAD")))
    {
      add_file_to_be_loaded (arg);
      return 1;
    }

  if (set_or_show_this_one (_T("ARGV")))	/* Mainly for debugging */
    {
      int i;
      if (show_instead_of_set)	/* !command_line_option */
	{
	  isql_printf (_T("ARGC=%d, ARGV="), G_argc);
	  fflush (stdout);
	  for (i = 0; i < G_argc /* G_argv[i] */ ; i++)
	    {
	      if (i > 0)
		{
		  isql_putchar (' ');
		}
	      isql_fputs ((G_argv[i] ? G_argv[i] : _T("NULL")), stdout);
	    }
	  isql_putchar ('\n');
	  fflush (stdout);
	}
/* Overriding of ARGV[ind] values added by AK 19-APR-1997 for the needs
   of test procedures. This allows a kludgous way of having at least few
   user-modifiable variables on the client side. I hope that argv vector
   is not defined strictly constant in any of our platforms. In that
   case we would have to make a copy of the original argv to G_argv.
 */
      if (NOT show_instead_of_set)
	{
	  TCHAR *openptr, *closeptr;
	  int indnum = -1;

/* if(('[' == *(openptr = skip_blankos(text+sizeof("ARGV")-1))) && */
	  if (('[' == *(openptr = skip_blankos (argptr))) &&
/* Was: (closeptr = isqlt_tcschr(text+1,']')) replaced by this: */
	      (closeptr = SCP (find_closing_point (UCP (openptr),
						   ((UTCHAR) * openptr))))
	    )
	    {
	      TCHAR tmp2buf[TMPBUF_SIZE + 2];
	      TCHAR *indstr;

	      closeptr++;	/* Skip past the closing bracket. */

	      openptr++;	/* Past the opening bracket. */
	      if (NO (indstr = get_next_token (&openptr, tmp2buf, TMPBUF_SIZE,
					       NULL, 0)))
		{
		  goto ertzu;
		}		/* Shouldn't really happen! (as there is ]) */

	      indnum = isqlt_tstoi (indstr);	/* Shouldn't care about closing ] */

	      if ((indnum < 0) || (indnum > G_argc))
		{
		  goto ertzu;
		}

	      arg = get_arg_after_braces (closeptr, tmp2buf, TMPBUF_SIZE, ending_point);

	      if ((indnum < sizeof (G_argv_touched)) &&
		  G_argv_touched[indnum] && G_argv[indnum])
		{
		  free (G_argv[indnum]);
		}		/* Not virginal, not NULL. */
	      G_argv[indnum] = (arg ? chestrdup (arg, text) : arg);
/* Mark changed argv vector elements, so that we can next time free
   those strdupped strings:
   (We should not free the original virgin elements!) */
	      if (indnum < sizeof (G_argv_touched))
		{
		  G_argv_touched[indnum] = 1;
		}
	      return (1);
	    }
	ertzu:
	  isql_fprintf (stdout,
			_T("Use set ARGV[ind] newelem   to override the old value of $ARGV[ind]\n"));
	  isql_fprintf (stdout,
		      _T("ind must be between 0 and %d, inclusive.\n"), G_argc);
	  return 1;
	}			/* if(NOT show_instead_of_set) */
      if (just_this_one)
	{
	  return 1;
	}
    }

/* These two are deprecated features, kept here just for the backward
   complicity's sake, please use the new READMODE=SNAPSHOT and
   READMODE=NORMAL options instead. */
  if (is_macro_name (macro_name, _T("RO")))
    {
      isql_fprintf (error_stream,
		    _T("%") PCT_S _T(": Warning: SET RO is deprecated feature, please use SET READMODE=SNAPSHOT instead!\n"),
		    progname);
      fflush (error_stream);
      if ((NOT fully_connected))	/* Requires connection. */
	{
	  connect_to_datasource (datasource, username, password);
	}

      /* Was: SQL_CONCUR_ROWVER but SQL_CONCUR_TIMESTAMP is same */
      SQLSetStmtOption (stmt, SQL_CONCURRENCY, SQL_CONCUR_TIMESTAMP);
      /* versioned read */
      return 1;
    }
  if (is_macro_name (macro_name, _T("RW")))
    {
      isql_fprintf (error_stream,
		    _T("%") PCT_S _T(": Warning: SET RW is deprecated feature, please use SET READMODE=NORMAL instead!\n"),
		    progname);
      fflush (error_stream);
      if ((NOT fully_connected))	/* Requires connection. */
	{
	  connect_to_datasource (datasource, username, password);
	}
      SQLSetStmtOption (stmt, SQL_CONCURRENCY, SQL_CONCUR_READ_ONLY);
      /* Normal shared locking */
      return 1;
    }

  else if (NOT show_instead_of_set)
/* If nothing (of the above ones) was after SET then ... */
/* Send the command to the server, as it might be SET PASSWORD, etc,
   unless this is called for command_line (or url) option, in which
   case print a warning message. */
    {
      if (command_line_option)
	{
	  isql_fprintf (error_stream,
			_T("%") PCT_S _T(": Warning: \"%") PCT_S _T("\" is unknown option, ignored!\n"),
			progname, macro_name);
	  fflush (error_stream);
	}
      return (0);
    }

  return (1);
}


int
is_isql_command (TCHAR *text, integer_list * pidlist)
{
  TCHAR *args, *arg1, *cmd, *next;
  int is_show = 0;
  TCHAR tmp0buf[TMPBUF_SIZE + 2], tmp1buf[TMPBUF_SIZE + 2];

/*  get_rid_of_trailing_newline(text);  Not necessary anymore? */

  text = skip_blankos (text);
  args = text;

  if ('!' == *text)
    {				/* Spawn a command to shell and wait for it if doesn't end with & */
      TCHAR edel = ';';
      spawn_shell_command ((text + 1), pidlist, (edel != '&'));
      return (1);
    }

  cmd = get_next_token (&args, tmp0buf, TMPBUF_SIZE, NULL, 0);
  if (NULL == cmd)
    {
      return (1);
    }				/* Empty commands are NO-OPs */


  args = skip_blankos (args);

/* SET and SHOW might or might not need a connection.
   If is_set_subcommand returns zero, it means there was a command
   it didn't recognize, e.g. SET PASSWORD or SET USER GROUP, which is a
   SQL command that should be sent to the server.
   If the return code is 1, then the command was ISQL set or show
   command executed all right, and requiring no further action.
   If the return code is 2, then the command was ISQL set command
   like SET AUTOCOMMIT=ON, that requires a full connection to be
   established, and we have not connected yet.

   Call is_set_subcommand here, before the following get_next_token
   to avoid double-call for the same macro-name (get_next_token
   is also called in is_set_subcommand).
 */

  if (((is_show = 0), is_cmd (cmd, _T("SET")))
      || ((is_show = 1), is_cmd (cmd, _T("SHOW"))))
    {
      int stat = is_set_subcommand (args, is_show, 0);
      if ((1 != stat) && (NOT fully_connected))
	{
	  /* This function also executes all the delayed settings: */
	  connect_to_datasource (datasource, username, password);
	}
      return (stat);
    }

  next = args;			/* because get_next_token will change its first argument. */

  /* arg1 can be set also to NULL, if there is nothing following the command */
  arg1 = get_next_token (&next, tmp1buf, TMPBUF_SIZE, NULL, 0);

  /* Now: text = points to beginning of command,
     args = pointer to beginning of arguments.
     cmd  = command word,
     arg1 = first argument
   */

  if (is_cmd (cmd, _T("NOP")))	/* Do nothing, just a NOP. */
    {
      return (1);
    }

  if (is_cmd (cmd, _T("ECHO")) || is_cmd (cmd, _T("ECHOLN"))
      || is_cmd (cmd, _T("ECHORAW")) || is_cmd (cmd, _T("ECHOLNRAW")))
    {
      isql_echo (text);
      return (1);
    }

  if (is_cmd (cmd, _T("WAIT_FOR_CHILDREN")))
    {
      wait_for_all_children (pidlist);
      return (1);
    }

  if (is_cmd (cmd, _T("LOAD")))
    {
      int rawload = 0;

      if (args && arg1 && ('"' != *args) && !strcasecmp (arg1, _T("-r")))
	{			/* If user gave the -r option (not in quotes!), then we use
				   raw_load, and have to get the filename following the option. */
	  rawload = 1;
	  arg1 = get_next_token (&next, tmp1buf, TMPBUF_SIZE, NULL, 0);
	}
      load_file (arg1, rawload, text);
      return (1);
    }

  if (is_cmd (cmd, _T("HELP")))
    {
      isql_help (args);
      return 1;
    }

  if (is_cmd (cmd, _T("EXIT")) || is_cmd (cmd, _T("QUIT")))
    {
      int status;
      /* EXIT NOT is NO-OP, allowing statements like:
         EXIT $IF $EQU $ARGV[0] 10 $ARGV[0] NOT;
         which exits with exit code 10 if $ARGV[0] is ten
         (presumably keeping a somekind of failure counter)
         but otherwise does nothing special, and continues
         from the next statement.
       */
      if (arg1 && (0 == strcasecmp (arg1, _T("NOT"))))
	{
	  return (1);
	}
      status = (arg1 ? isqlt_tstoi (arg1) : 0);
      isql_exit (status);
    }

  if (is_cmd (cmd, _T("SLEEP")))
    {
      int seconds = (arg1 ? isqlt_tstoi (arg1) : 0);
      isql_sleep (seconds);
      return (1);
    }


/* Without arguments forces the connection to be made with an ordinary
   way with SQLConnect. With an argument uses SQLDriverConnect, giving
   that argument as a connection string (e.g. "DSN=Kubl;UID=DBA,PWD=DBA")
   If we are already connected, then is just NO-OP which is silently
   ignored.
 */

  if (!debug_session && is_cmd (cmd, _T("CONNECT")))
    {
      if ((NOT fully_connected))
	{
	  if (arg1)
	    {
	      connect_to_datasource (arg1, NULL, NULL);
	    }
	  else
	    {
	      connect_to_datasource (datasource, username, password);
	    }
	}
      return (1);
    }


  if (!debug_session && is_cmd (cmd, _T("RECONNECT")))
    {
      if (fully_connected)
	{
	  SQLFreeStmt (stmt, SQL_DROP);
	  SQLDisconnect (hdbc);
	  SQLFreeConnect (hdbc); /* iodbc needs the realloc */
	  SQLAllocConnect (henv, &hdbc);
	  fully_connected = 0;
	}
      if ((NOT fully_connected))
	{
	  if (arg1)
	    {
	      connect_to_datasource (datasource, arg1, NULL);
	    }
	  else
	    {
	      connect_to_datasource (datasource, username, password);
	    }
	}
      return (1);
    }

/* All the rest need the connection to be established, also all
   the commands that are not matched here, e.g. all SQL CLI/ODBC
   Catalog API commands like TABLES, PRIMARYKEYS, etc, and everything
   else that by definition should be sent to the server. */

  if ((NOT fully_connected))
    {
      connect_to_datasource (datasource, username, password);
    }

  if (is_cmd (cmd, _T("FOREACH")))
    {
      isql_foreach (text);
      return (1);
    }

#ifndef ODBC_ONLY
  if (is_cmd (cmd, _T("SHUTDOWN")))
    {
      SQLExecDirect (stmt, UCP (cmd), SQL_NTS);
      isql_exit (0);
    }
#endif
  return 0;
}



int
version_less_than (TCHAR *s, int major, int minor)
{
  int tmp;
  while (*s && NOT isqlt_istdigit (*s))
    {
      s++;
    }				/* Skip non-digits. */
  if (empty_stringp (s))
    {
      return (0);
    }				/* Premature end? */
  tmp = isqlt_tstoi (s);
  if (tmp > major)
    {
      return (0);
    }
  else if (tmp < major)
    {
      return (1);
    }
  /* They are equivalent, we have to check the minor part also. */
  while (*s && (*s != '.'))
    {
      s++;
    }				/* Search a period. */
  if (empty_stringp (s) || empty_stringp (++s))
    {
      return (0);
    }				/* Not found or followed by nothing */
  tmp = isqlt_tstoi (s);
  return (tmp < minor);
}

#if defined (WIN32)
#define _PASSWORD_LEN 128
#define ENTER_CODE 0x0D
/* because the M$ doesn't have a getpass, so let's implement it */
TCHAR *
isqlt_tgetpass(const TCHAR *szPrompt)
{
  static TCHAR szPassword[_PASSWORD_LEN];
  TCHAR *szCurrent = szPassword;

  isqlt_tcprintf(_T("%") PCT_S, szPrompt);
  while (szCurrent - szPassword < _PASSWORD_LEN - 2 && ENTER_CODE != (*szCurrent = _getch()))
    szCurrent++;
  *szCurrent = 0;
  isqlt_tcprintf(_T("\n"));
  return (szPassword);
}
#elif defined (HPUX_10) || defined (HPUX_11)
#include <termios.h>
#define _PASSWORD_LEN 128
#define ENTER_CODE '\n'
/* because the HP/UXes getpass() truncates to 8 chars, so let's implement it */
TCHAR *
isqlt_tgetpass(const TCHAR *szPrompt)
{
  static TCHAR szPassword[_PASSWORD_LEN];
  TCHAR *szCurrent = szPassword;
  struct termios old, new;

  isql_printf(_T("%") PCT_S, szPrompt);
  tcgetattr (fileno (stdin), &old);
  new = old;
  new.c_lflag &= ~ECHO;
  tcsetattr (fileno (stdin), TCSAFLUSH, &new);

  while (szCurrent - szPassword < _PASSWORD_LEN - 2 && ENTER_CODE != (*szCurrent = getchar()))
    szCurrent++;

  tcsetattr (fileno (stdin), TCSAFLUSH, &old);
  *szCurrent = 0;
  isql_printf(_T("\n"));
  return (szPassword);
}
#endif

/* If username is given as NULL pointer, then calls SQLDriverConnect
   (with datasource argument as a whole connection string)
   instead of SQLConnect. No dialogues are prompted for, as no windows
   are used.
 */
int
connect_to_datasource (TCHAR *datasource, TCHAR *username, TCHAR *password)
{
  long ignore;
  int rc;
  int retries = 0;

#ifdef COMMENTED_OUT
/* (see function print_http_headers_if_not_already_printed) */
  if (web_mode)
    {				/* Print the HTML headers, as this is the last change to do it. */
      {
	if (NOT isql_echo_lwe)	/* No output done yet with ECHO command? */
	  {
	    isql_printf (_T("Content-Type: %") PCT_S _T("\r\n\r\n"), web_content_type);
	    if (in_HTML_mode ())
	      {
		/* Show the statement(s) to be executed in the title: */
		html_print_head_title (stdout, _T("Virtuoso ISQL"), _T(": "),
				       execute_these_only[0]);
		isql_fputs (oo_ob, stdout);	/* And the Otherness Begins, i.e. <PRE> */
	      }
	    fflush (stdout);
	  }
      }
    }
#endif

  if (NULL == username)
    {
#if defined (NO_DRIVER_CONNECT)
      isql_fprintf (error_stream,
		    "%") PCT_S _T(": SQLDriverConnect not available on this platform. Please use SET DSN, UID and PWD commands before CONNECT command without arguments to connect. Connection string was: \"%s\"\n",
		    progname, datasource);
      isql_exit (1);
#else
      SWORD conn_str_length;
      TCHAR completed_conn_string[1001];

#ifdef ODBC_ONLY
      TCHAR dsn2[512];
      if (isqlt_tcsncmp (datasource, _T("DSN="), 4))
	{
	  isqlt_stprintf (dsn2, _T("DSN=%") PCT_S, datasource);
	  datasource = dsn2;
	}
#endif

      isqlt_tcscpy (completed_conn_string, _T("NOT SET"));

      if (SQL_SUCCESS !=
	  (rc = SQLDriverConnect (hdbc, NULL,	/* No window handle */
				  (UTCHAR *) datasource, SQL_NTS,
				  ((UTCHAR *) completed_conn_string),
				  (sizeof (completed_conn_string) - 2),
				  &conn_str_length,
				  SQL_DRIVER_COMPLETE)))
	{
	  isql_fprintf (error_stream, _T("Connection: %") PCT_S _T("\n"),
		rc == SQL_SUCCESS_WITH_INFO ? _T("Establised (with info)") : _T("Failed"));
	  print_error (((HENV) 0), hdbc, ((HSTMT) 0), rc);
	  if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
	    isql_exit (3);
	}
      else if (verbose_mode)	/* A success */
	{
	  isql_fprintf (error_stream, _T("Connection: Established\n"));
	}
#endif
    }
  else
    /* if(username) Normal usage. */
    {
#ifndef ODBC_ONLY
      SQLSetConnectOption (hdbc, SQL_ENCRYPT_CONNECTION, (SQLULEN)encryption);
      SQLSetConnectOption (hdbc, SQL_SERVER_CERT, (SQLULEN)ca_list);
      SQLSetConnectOption (hdbc, SQL_PWD_CLEARTEXT, (SQLULEN)pwd_cleartext);
      SQLSetConnectOption (hdbc, SQL_SHUTDOWN_ON_CONNECT, (SQLULEN)virtuoso_shutdown);
#endif
      if (!password)
        {
	  password = username;
again:
	  rc = SQLConnect (hdbc, (UTCHAR *) datasource, SQL_NTS,
			    (UTCHAR *) username, SQL_NTS,
			    (UTCHAR *) password, SQL_NTS);
	  if (SQL_SUCCESS != rc && SQL_SUCCESS_WITH_INFO != rc)
	    {
	      TCHAR szPrompt[256];

	      print_error (SQL_NULL_HENV, hdbc, SQL_NULL_HSTMT, rc);

	      if (!isqlt_tcscmp (_T("28000"), SQL_error_state))
		{
		  if (++retries > 3)
		    {
		      isql_exit (3);
		    }
		  isqlt_stprintf(szPrompt, _T("\nEnter password for %") PCT_S _T(" :"), username);
#if defined (SOLARIS)
		  password = getpassphrase(szPrompt);
#else
		  password = isqlt_tgetpass(szPrompt);
#endif
#if 0 		 /* bug #4882 */
		  if (!*password)
		    isql_exit(3);
#endif
		  goto again;
		}
	      else
		isql_exit(3);
	    }
	  else
	    {
	      if (SQL_SUCCESS_WITH_INFO == rc)
		print_error (((HENV) 0), hdbc, ((HSTMT) 0), rc);
	      goto connected;
	    }
	}
      if (SQL_SUCCESS !=
	  (rc = SQLConnect (hdbc, (UTCHAR *) datasource, SQL_NTS,
			    (UTCHAR *) username, SQL_NTS,
			    (UTCHAR *) password, SQL_NTS)))
	{
	  print_error (((HENV) 0), hdbc, ((HSTMT) 0), rc);
	  if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
	    isql_exit (3);
	}
connected:
      ;
    }

  SQLSetConnectOption (hdbc, SQL_AUTOCOMMIT, 0);
/* Stupid ifdef #ifndef SQLCLI */
  SQLGetInfo (hdbc, SQL_DBMS_NAME, info_dbms,
	      sizeof (info_dbms), ((SWORD *) & ignore));
  SQLGetInfo (hdbc, SQL_DRIVER_VER, info_driver,
	      sizeof (info_driver), ((SWORD *) & ignore));

  if (verbose_mode)
    {
      isql_printf (_T("Connected to %") PCT_S _T("\nDriver: %") PCT_S _T("\n"), info_dbms, info_driver);
    }

  if (isqlt_tcsstr (info_dbms, _T("Virtuoso")) ||
      isqlt_tcsstr (info_dbms, _T("UBL")) || isqlt_tcsstr (info_dbms, _T("ubl")))
    {
      kubl_mode = 1;
      if (version_less_than (info_driver, 0, 92))
	{
/*
   isql_fprintf(stderr,
   "-- Sorry, you need KUBL ODBC driver version 0.92 or later to run this tool.\n");
   isql_fprintf(stderr,
   "-- It is available from http://www.kubl.com\n");
   isql_exit(1);
 */
	  times_conform_to_odbc_flag = 0;
	  times_to_strings_flag = 1;
	}
    }
  else
    /* Some other DBMS, e.g. Access. */
    {
      kubl_mode = 0;
    }


/* #endif */

  SQLAllocStmt (hdbc, &stmt);
  fully_connected = 1;

  if (n_delayed_settings)
    {
      int i;
      for (i = 0; delayed_settings[i]; i++)
	{
	  if (!is_set_subcommand (delayed_settings[i], 0, 1))
	    {			/* isql_exit(1); */
	    }
	}
    }

  return (1);

}


int
handle_query_string (TCHAR *query_string)
{
  TCHAR *ptr1, *ptr2;
  int i = 1;

/* Call is_set_subcommand for each ampersand-delimited piece:
   query_string is corrupted, but web_query_string is left to point
   to an intact copy of the percent-escaped original, so that
   it can be later referenced with $QUERY_STRING dollar form
   (see get_next_token).
   Call with the third argument as 2, tell is_set_subcommand
   to look for prefix S_ and if it is found, to skip it, and if
   not then to wrap set name inside U{' and '} so as to set
   all other stuff to User/URL associative array U.
 */

  for (ptr1 = query_string; NULL != (ptr2 = isqlt_tcschr (ptr1, '&'));
       ptr1 = (ptr2 + 1), i++)
    {
      *ptr2 = '\0';		/* Wipe the ampersand. */
      if (!is_set_subcommand (ptr1, 0, 2))
	{			/* isql_exit(1); */
	}
/*         *ptr2 = '&';   Don't restore the ampersand! */
    }

/* Still have to handle the last piece of web_query_string,
   (or the one and only one if there were no ampersands at all): */
  if (!is_set_subcommand (ptr1, 0, 2))
    {				/* isql_exit(1); */
    }

  return (i);
}

/* Like isqlt_fgetts, but doesn't get confused about null bytes '\0',
   just stores them into buffer as any other characters.
   Stops at the first '\n' (newline) which is also stored into buffer.
   Returns as a result the number of bytes read, 0 if EOF encountered.
 */
int
bin_fgets (TCHAR *buffer, int max_size, FILE * infp)
{
  int i, c = 0;

  if (ferror (infp) || feof (infp))
    {
      return (0);
    }
  for (i = 0; (c != '\n') && (i < (max_size - 1)) && ((c = getc (infp)) != EOF); i++)
    {
      ((UTCHAR *) buffer)[i] = ((UTCHAR) c);
    }
  ((UTCHAR *) buffer)[i] = '\0';		/* And add terminating zero. */

  return (i);
}

/* Note from rfc1521.html (MIME)

   It must be noted that Content-Type values, subtypes, and parameter
   names as defined in this document are case-insensitive.  However,
   parameter values are case-sensitive unless otherwise specified for
   the specific parameter.

   So this means that in many cases we really should use nc_strstr
   (defined in ../string.c) instead of isqlt_tcsstr !!!

   It is possible to completely screw up the input to CGI-script with
   HTML-constructs like this, which at least Netscape version 2.02E
   will send without any escaping...

   <INPUT TYPE=HIDDEN NAME="LUMIK��RME&#34;Lismake&LT;=&GT;kima&#59;ra&AMP;lahna" VALUE="Suikkaboksi ��li�m�isesti huikkaa!">
   <INPUT TYPE=CHECKBOX NAME="sikaniska-NAME=lumiukko filename=pena" VALUE=RASTITTU>

   like these:
   Content-Disposition: form-data; name="LUMIK��RME"Lismake<=>kima;ra&lahna"
   Content-Disposition: form-data; name="sikaniska-NAME=lumiukko filename=pena"

   A line with real filename= parameter should look like this:

   Content-Disposition: form-data; name="KUVA1"; filename="D:\InetPub\wwwroot\imagemap\soyombo.gif"

   So we assume that the users won't write forms with doublequotes
   in parameter names, and neither they should use spaces.

 */

#define starts_with(buf,str) (!strncasecmp((buf),(str),(sizeof(str)-1)))
#define is_contained_in(str1,str2) (isqlt_tcsstr((str1),(str2)))

/* Tries to be liberal, e.g. doublequotes are optional. */
TCHAR *
extract_from_doublequotes (TCHAR *src)
{
  size_t len;
  TCHAR *dest;
  TCHAR *pt1;
  if ('"' == *(src))
    {
      src++;
    }				/* Skip the beginning doublequote if there is one. */

  if ((pt1 = isqlt_tcschr (src, '"'))	/* Ending Doublequote? */
      || (pt1 = isqlt_tcschr (src, ';'))	/* Or semicolon ? */
      || (pt1 = isqlt_tcschr (src, ' '))	/* Space ? */
      || (pt1 = isqlt_tcschr (src, '\r'))	/* CR ? */
      || (pt1 = isqlt_tcschr (src, '\n'))	/* NL ? */
      || (pt1 = ((src) + isqlt_tcslen (src))))	/* The last always matches. */
    {
      len = (pt1 - src);
      dest = (TCHAR *) chemalloc ((len + 1), _T("extract_from_doublequotes"));
      isqlt_tcsncpy (dest, src, len);
      dest[len] = '\0';		/* Make sure it is terminated. */
/*
   isql_fprintf(error_stream,"extract_from_doublequotes: src=%s, dest=%s\n",src,dest);
   fflush(error_stream);
 */
      return (dest);
    }
  return NULL;
}

/* #define IN_BATCH 2000 */

/* Returns 1 if there is a file to read, 0 otherwise */
/* Call is_set_subcommand for each boundary delimited piece
   read from infp (e.g. stdin, stuff coming from the browser.)
   Call with the third argument as 2, tell is_set_subcommand
   to look for prefix S_ and if it is found, to skip it, and if
   not then to wrap set name inside U{' and '} so as to set
   all other stuff to User/URL associative array U.
   Also, don't try to do urldecoding for data after =
 */
int
handle_multipart_form (TCHAR *boundary, FILE * in_fp)
{
  TCHAR *me = _T("handle_multipart_form");
  TCHAR *tp1, *name = NULL;
  TCHAR *inpoint = &input[0];
  int blen = (int) isqlt_tcslen (boundary);
  int got_anything = 0, final_line = 0, state = 0;
  /* state is either:
     0 = waiting for the first boundary, 1 = reading headers,
     2 = reading stuff itself, 3 = reading headers of uploaded file, after
     they have been all read, return 1 to caller.
     4 = stuff reading truncated, scanning for the next boundary.
   */

/*
   isql_fprintf(error_stream,"handle_multipart_form(\"%s\") called\n",boundary);
   fflush(error_stream);
 */

  form_boundary = chestrdup (boundary, me);

  while (0 != (got_anything = bin_fgets (inpoint, IN_BATCH, in_fp)))
    {
/*
   isql_fprintf(error_stream,"handle_multipart_form: blen=%d, got_anything=%d, inpoint=%s, *(inpoint+blen)=%d.,%c\n",
   blen,got_anything,inpoint,
   *(((unsigned char *)inpoint+blen)),*(((unsigned char *)inpoint)+blen));
   fflush(error_stream);
 */
/* This slightly erroneous code
   if((got_anything >= blen) && !isqlt_tcsncmp(inpoint,boundary,blen)
   && ((isqlt_istspace(*(inpoint+blen)) || !*(inpoint+blen))
   || (('-' == inpoint[blen+1])
   && ('-' == inpoint[blen+2])
   && (isqlt_istspace(inpoint[blen+3]) || !inpoint[blen+3])
   && (final_line = 1))))
   Replaced by this outwardly neat macro:
 */
      if (is_mime_boundary_token (inpoint, got_anything, boundary, blen, final_line))
	{
	  if ((2 == state) || (4 == state))	/* Has been collecting stuff? */
	    {
	      /* Take the last CR+LF away from the end: */
	      if ((inpoint >= input) && ('\n' == *(inpoint - 1)))
		{
		  --inpoint;
		}
	      if ((inpoint >= input) && ('\r' == *(inpoint - 1)))
		{
		  --inpoint;
		}
	      *inpoint = '\0';
/* And terminate the thing as is_set_subcommand_aux still doesn't do
   much with its fourth argument. */
	      is_set_subcommand_aux (input, 0, 2, (int) (inpoint - input));
	    }
	  if (final_line)
	    {
	      return (0);
	    }
	  else
	    {
	      state = 1;
	    }
	  inpoint = &input[0];
	}
      else if ((1 == state) || (3 == state))	/* Reading headers ? */
	{
	  if (starts_with (input, _T("Content-Disposition:")))
	    {
	      if ((tp1 = is_contained_in (input, _T(" name="))))
		{
		  name = extract_from_doublequotes (tp1 + sizeof (_T(" name=")) - 1);
		}
	      if ((tp1 = is_contained_in (input, _T(" filename="))))
		{
		  form_filename = extract_from_doublequotes (tp1 + sizeof (_T(" filename=")) - 1);
		  form_filefieldname = name;
		  state = 3;
		}
	    }
	  else if (starts_with (input, _T("Content-Type:")))
	    {
	      form_last_content_type
		= chestrdup (get_rid_of_trailing_newline (skip_blankos (input + sizeof (_T("Content-Type:")) - 1)), me);
	    }
	  else if (starts_with (input, _T("Content-Transfer-Encoding:")))
	    {
	      form_last_encoding
		= chestrdup (get_rid_of_trailing_newline (skip_blankos (input + sizeof (_T("Content-Transfer-Encoding:")) - 1)), me);
	    }
	  else if (('\r' == *input) || ('\n' == *input))
	    {
	      if (3 == state)
		{
		  return (1);
		}		/* The contents of file start here. */
	      isqlt_tcscpy (input, name);
	      isqlt_tcscat (input, _T("             ="));		/* Space for {'thing'} */
	      inpoint = input + isqlt_tcslen (input);
	      state = 2;	/* Start reading the stuff itself. */
	    }
	}			/* if((1 == state) || (3 == state)) i.e. reading headers */
      else if ((2 == state))
	{
	  if ((inpoint + got_anything) > (input + INPUT_SIZE - IN_BATCH))
	    {
	      if (2 == state)
		{
		  isql_fprintf (error_stream,
				_T("\n%") PCT_S _T(": *** The multiline value in multipart form has grown too long on lines %ld - %ld.\n")
				_T("Already %d bytes read without encountering a boundary string: %") PCT_S _T("\n")
			     _T("in the end of line! Ignoring all the rest.\n"),
				progname, latest_statement_begins_at (), current_linecount (),
			(int)((inpoint + got_anything) - input), form_boundary);
		  state = 4;
		  /*           isql_exit(1); */
		}
	    }
	  else
	    {
	      inpoint += got_anything;
	    }
	}
    }				/* while loop. */

/* We should not come here if the input contains a valid
   terminating boundary separator. */
  return (0);

}


int isql_main (int argc, TCHAR **argv, PFSTR url_info_fun);

int __cdecl
_tmain (int argc, TCHAR **argv)
{
  isql_locale = setlocale (LC_ALL, "");
  if (!isql_locale)
    isql_locale = setlocale (LC_ALL, NULL);
#ifdef WIN32
  setmode (fileno (stdin), _O_BINARY);
#endif
#ifdef MALLOC_DEBUG
  dbg_malloc_enable();
#endif
  isql_exit (isql_main (argc, argv, (PFSTR) isqlt_tgetenv));
  return 0;
}


int
isql_main (int argc,
	   TCHAR **argv,
	   PFSTR url_info_fun)	/* Usually getenv */
{
  int i;
  int nth_non_option = 0;	/* If set to non-zero then we know that this
				   is used in the traditional way, and connection
				   should be done quite soon in the beginning. */
  int shortcuts_used = 0;
  int host_shortcut = 0;
/*  char *last_underscore = NULL, *last_slash_or_backslash = NULL, *last_something = NULL; */
  TCHAR *query_string = NULL, *req_method = NULL, *content_type = NULL;
  struct name_var_pair *buf_nfo;

  progname = *argv;
  error_stream = stderr;
  current_input_stream () = stdin;
  G_argv = argv;
  G_argc = argc;

  SQLAllocEnv (&henv);		/* This might be needed quite soon. */
  SQLAllocConnect (henv, &hdbc);

#if 0
  last_slash_or_backslash = isqlt_tcsrchr (progname, '/');
#ifdef _WINDOWS
  last_something = isqlt_tcsrchr (progname, '\\');
  if (last_something &&
    (!last_slash_or_backslash || (last_something > last_slash_or_backslash))
    )
    {
      last_slash_or_backslash = last_something;
    }
#endif

  form_action = (last_slash_or_backslash ? (last_slash_or_backslash + 1)
		 : progname);

  /* Actually not the last, but the first after slash/backslash or
     the very first in program name. */
  last_underscore =
    isqlt_tcschr ((last_slash_or_backslash ? last_slash_or_backslash : progname),
	    '_');

  if (last_underscore &&
  (!last_slash_or_backslash || (last_underscore > last_slash_or_backslash)))
    {				/* A forced load. */
      int len;
      char tmpbuf[TMPBUF_SIZE + 1];

      last_something = isqlt_tcsrchr (progname, '.');		/* This time a last period. */
      if (!last_something)	/* Or pointing to the end. */
	{
	  last_something = (progname + isqlt_tcslen (progname));
	}

      len = (last_something - last_underscore) - 1;	/* Excluding the terminating zero */

      if (len > TMPBUF_SIZE)
	{
	  len = TMPBUF_SIZE;
	}			/* Silent cut, not probable. */

      isqlt_tcsncpy (tmpbuf, (last_underscore + 1), len);
      tmpbuf[len] = '\0';
      my_strncat (tmpbuf, ".sql", TMPBUF_SIZE);
      add_file_to_be_loaded (tmpbuf);
    }
#endif
  /* Is this used as a CGI-script? */
  if ((query_string = (url_info_fun) (_T("QUERY_STRING"))) ||
      (req_method = (url_info_fun) (_T("REQUEST_METHOD"))) ||
      (url_info_fun) (_T("SERVER_PROTOCOL")))
    {				/* There is a query string in the environment variable QUERY_STRING */
      /* which means that this is used with the Web browser. */
      /* Note: If we ever refer to other environment variables after
         these ones we have to make a copy of the web_query_string,
         as (url_info_fun) (i.e. getenv) returns pointers to its own
         static buffer. (Well, it's done now.)
       */
      input = chemalloc (input_size+20, _T("isql_main"));
      input[0] = 0;
      buf_nfo = find_var_info (_T("INPUTLINE"));
      buf_nfo->var_addr = (void *) input;
      buf_nfo->more_info = (void *) (ptrlong) input_size;
      web_mode = 1;
      error_stream = stdout;
      print_prompt = NULL;	/* Disable prompting also in cases like HELP SET */

      if (query_string && *query_string)
	{
	  web_query_string
	    = chestrdup (query_string, _T("main (web_query_string)"));
	  handle_query_string (query_string);
	}			/* if(query_string) */

      if (!req_method)
	{
	  req_method = (url_info_fun) (_T("REQUEST_METHOD"));
	}

      if (req_method && !isqlt_tcscmp (req_method, _T("POST")))
	{
	  if (NULL != (content_type = (url_info_fun) (_T("CONTENT_TYPE"))))
	    {
/*
   multipart/form-data; boundary=---------------------------243953132224834
   application/x-www-form-urlencoded
 */
	      if (starts_with (content_type, _T("multipart/form-data")))
		{
		  TCHAR *boundary;
		  TCHAR tmp1buf[TMPBUF_SIZE + 2];

		  if (NULL != (boundary = is_contained_in (content_type, _T("boundary="))))
		    {
		      boundary += (sizeof (_T("boundary=")) - 1);
		      isqlt_tcscpy (tmp1buf, _T("--"));
		      isqlt_tcscat (tmp1buf, boundary);
		      boundary = tmp1buf;
		      handle_multipart_form (boundary, stdin);
		    }
		  else
		    /* No boundary= part in CONTENT_TYPE ? */
		    {
		      /* Give an ugly error message... */
		      /* And/Or EXIT: return(1); */
		    }
		}
	      else
		/* It is normally x-www-form-urlencoded, so read the first */
		{		/* line from stdin and handle it as a normal query string. */
		  if (NULL != (query_string = isqlt_fgetts (input, INPUT_SIZE, stdin)))
		    {
		      web_query_string
			= chestrdup (query_string, _T("main (web_query_string)"));
		      handle_query_string (query_string);
		    }
		  else
		    /* END of stdin encountered prematurely... */
		    {
		      /* Give an ugly error message... */
		      /* And/Or EXIT: return(1); */
		    }
		}
	    }
	}			/* REQUEST_METHOD=POST ? */
    }
  else
    /* Normal usage from shell. */
    /* Parse command line arguments in this little loop. */
    {				/* This part taken from dbdump.c. Web-interface implemented above */
      int u_encountered = 0;
      for (i = 1, nth_non_option = 0; i < argc; i++)
	{
	  if (!isqlt_tcsncmp (argv[i], _T("-?"), 2) || !isqlt_tcsncmp (argv[i], _T("/?"), 2) || !isqlt_tcsncmp (argv[i], _T("--help"), 6))
	    {
	      isqlt_ftprintf (stdout, _T("%") PCT_S _T(" Interactive SQL ") ISQL_TYPE _T("\n"), PRODUCT_NAME);
	      isqlt_ftprintf (stdout, _T("Version %")  PCT_S  _T(" as of %s\n"), ISQL_VERSION, __DATE__);
#ifndef ODBC_ONLY
	      isqlt_ftprintf (stdout, _T("Compiled for %")  PCT_S _T(" (%")  PCT_S _T(")\n"), build_opsys_id, build_host_id);
#endif
 	      isqlt_ftprintf (stdout, _T("%s\n"), PRODUCT_COPYRIGHT);
	      isqlt_ftprintf (stdout,
		_T("\n")
	        _T("Usage:\n")
#ifndef ODBC_ONLY
		_T("isql <HOST>[:<PORT>] <UID> <PWD> file1 file2 ...\n")
		_T("\n")
		_T("isql -H <server_IP> [-S <server_port>] [-U <UID>] [-P <PWD>]\n")
		_T("     [-E] [-X <pkcs12_file>] [-K] [-C <num>] [-b <num>]\n")
		_T("     [-u <name>=<val>]* [-i <param1> <param2>]\n")
#else
		_T("  isql <DSN> <UID> <PWD> file1 file2 ...\n")
		_T("\n")
		_T("isql -H datasource [-U <UID>] [-P <PWD>] [-u <name>=<val>]* [-i <param1> <param2>]\n")
#endif
		_T("\n")
		_T("  isql -?\n")
		_T("\n")
		_T("Connection options:\n")
		_T("  -?                  - This help message\n")
		_T("  -U username         - Specifies the login user ID\n")
		_T("  -P password         - Specifies the login password\n")
#ifndef ODBC_ONLY
		_T("  -H server_addr      - Specifies the Server address (IP)\n")
		_T("  -S server port      - Specifies the TCP port to connect to\n")
		_T("  -E                  - Specifies that encryption will be used\n")
                _T("  -C                  - Specifies that password will be sent in cleartext\n")
		_T("  -X pkcs12_file      - Specifies that encryption & X509 certificates will\n")
		_T("                        be used\n")
		_T("  -T server_cert      - Specifies that CA certificate file will be used\n")
		_T("  -W webid_delegate   - Specifies WebID delegate IRI\n")
                _T("  -b size             - Specifies that large command buffer will be used\n")
		_T("                        (in KBytes)\n")
		_T("  -K                  - Shuts down the virtuoso engine after connecting to it\n")
#else
		_T("  -H datasource       - Specifies the data source name (DSN)\n")
#endif
		_T("\n")
		_T("Parameter passing options:\n")
		_T("  -u name1=val1... - Everything after -u is stored to associative array U,\n")
		_T("                        until -i is encountered. If no equal sign then value\n")
		_T("                        is NULL\n")
		_T("  -i                  - Ignore everything after the -i option, after which \n")
		_T("                        comes arbitrary input parameter(s) for isql procedure,\n")
		_T("                        which can be referenced with $ARGV[$I] by the\n")
		_T("                        ISQL-commands.\n")
		_T("  <OPT>=<value>       - Sets the ISQL options\n")
		_T("\n")
		_T("Note: Arguments that do not match one of the above options will be assigned\n")
                _T("      in order to the following fields:\n")
#ifndef ODBC_ONLY
		_T("  <HOST>[:<PORT>] <UID> <PWD> file1 file2 ...\n\n")
#else
		_T("  <DSN> <UID> <PWD> file1 file2 ...\n\n")
#endif
		  );
	      exit (0);
	    }
	  if (!isqlt_tcsncmp (argv[i], _T("-U"), 2) && i+1 < argc)
	    {
	      if (isqlt_tcslen (argv[i]) > 2)
		username = argv[i] + 2;
	      else
		{
		  i++;
		  username = argv[i];
		}
	      shortcuts_used++;
	      continue;
	    }

	  if (!isqlt_tcsncmp (argv[i], _T("-P"), 2) && i+1 < argc)
	    {
	      if (isqlt_tcslen (argv[i]) > 2)
		password = argv[i] + 2;
	      else
		{
		  i++;
		  password = argv[i];
		}
	      shortcuts_used++;
	      continue;
	    }
	  if (!isqlt_tcsncmp (argv[i], _T("-H"), 2) && i+1 < argc)
	    {
	      if (isqlt_tcslen (argv[i]) > 2)
		datasource = argv[i] + 2;
	      else
		{
		  i++;
		  datasource = argv[i];
		}
	      shortcuts_used++;
	      host_shortcut++;
	      continue;
	    }
#ifndef ODBC_ONLY
	  if (!isqlt_tcsncmp (argv[i], _T("-E"), 2))
	    {
	      encryption = _T("1");
	      continue;
	    }
	  if (!isqlt_tcsncmp (argv[i], _T("-C"), 2) && i + 1 < argc)
	    {
	      if (isqlt_tcslen (argv[i]) > 2)
		pwd_cleartext = isqlt_tstoi (argv[i]) + 2;
              else
                {
		  i++;
		  pwd_cleartext = isqlt_tstoi (argv[i]);
		}
	      continue;
	    }
	  if (!isqlt_tcsncmp (argv[i], _T("-X"), 2) && i + 1 < argc)
	    {
	      if (isqlt_tcslen (argv[i]) > 2)
		encryption = argv[i] + 2;
	      else
		{
		  i++;
		  encryption = argv[i];
		}
	      continue;
	    }
	  if (!isqlt_tcsncmp (argv[i], _T("-T"), 2) && i + 1 < argc)
	    {
	      if (isqlt_tcslen (argv[i]) > 2)
		ca_list = argv[i] + 2;
	      else
		{
		  i++;
		  ca_list = argv[i];
		}
	      continue;
	    }

	  if (!isqlt_tcsncmp (argv[i], _T("-S"), 2) && i+1 < argc)
	    {
	      static TCHAR port[10];
	      struct servent *pserv;
	      if (isqlt_tcslen (argv[i]) > 2)
		connect_port = argv[i] + 2;
	      else
		{
		  i++;
		  connect_port = argv[i];
		}
	      pserv = getservbyname (connect_port, _T("tcp"));
	      if (pserv)
		{
		  isqlt_stprintf (port, _T("%d"), ntohs (pserv->s_port));
		  connect_port = port;
		}
	      shortcuts_used++;
	      continue;
	    }

	  if (!isqlt_tcsncmp (argv[i], _T("-b"), 2) && i + 1 < argc)
	    {
	      if (isqlt_tcslen (argv[i]) > 2)
		input_size = isqlt_tstoi (argv[i]+2) * 1024;
              else
                {
		  i++;
		  input_size = isqlt_tstoi (argv[i]) * 1024;
		}
	      if (input_size < MIN_INPUT_SIZE)
		input_size = MIN_INPUT_SIZE;
	      continue;
	    }

	  if (!isqlt_tcsncmp (argv[i], _T("-K"), 2))
	    {
	      virtuoso_shutdown = 1;
	      u_encountered = 1;
	    }
#ifdef PLDBG
	  if (!isqlt_tcsncmp (argv[i], _T("-D"), 2))
	    {
	      virtuoso_debug = 1;
	      continue;
	    }
#endif
#endif

	  if (!isqlt_tcscmp (argv[i], _T("-u")))	/* Everything after -u is stored to */
	    {			/* associative array U, until -i is */
	      u_encountered = 1;
	      continue;		/* encountered */
	    }

	  if (!isqlt_tcscmp (argv[i], _T("-i")))
	    {
/* Ignore everything after the -i option, after which comes arbitrary
   input parameter(s) for isql procedure, which can be referenced
   with $ARGV[$I] by the ISQL-commands. */
	      G_argc_i = i + 1;
	      break;
	    }

	  if (u_encountered)
	    {
	      if (!is_set_subcommand (argv[i], 0, 2))
		{		/* return(1); */
		}
	    }
	  else if (isqlt_tcschr (argv[i], '='))	/* There's an equal sign? */
	    {
	      if (!is_set_subcommand (argv[i], 0, 1))
		{		/* return(1); */
		}
	    }
	  else
	    {
	      switch (++nth_non_option)
		{
		case 1:
		  {
		    datasource = argv[i];
		    break;
		  }
		case 2:
		  {
		    username = argv[i];
		    break;
		  }
		case 3:
		  {
		    password = argv[i];
		    break;
		  }
		default:	/* 4th or later. */
		  {
		    add_file_to_be_loaded (argv[i]);
		    break;
		  }
		}
	    }
	}			/* for loop over arguments */
    }

  /* alloc the input buffer */
  if (input == input1)
    {
      input = chemalloc (input_size+20, _T("isql_main"));
      input[0] = 0;
      buf_nfo = find_var_info (_T("INPUTLINE"));
      buf_nfo->var_addr = (void *) input;
      buf_nfo->more_info = (void *) (ptrlong) input_size;
    }
/* If we are in web_mode, and there was neither statement(s) to be
   executed given, nor any files to be loaded, then we should
   dump the default form for the user.
 */
  if (web_mode)
    {
      if ((NOT execs_from_cmdline) && (NOT load_n_files))
	{

/* (see function print_http_headers_if_not_already_printed) */
	  flag_head_already_printed = 1;
	  isqlt_tprintf (_T("Content-Type: %") PCT_S _T("\r\n\r\n"), _T("text/html"));
	  fflush (stdout);

	  output_html_file (DEFAULT_HTML_TEMPLATE_FILE);
	  return (0);
	}
    }
#ifdef PLDBG
  if (virtuoso_debug)
    {
      if (!password)
	password = username;
      debug_session = pldbg_connect (datasource, username, password);

      if (!debug_session)
	{
	  isql_fprintf (error_stream, _T("Can't estabilish debug session to the '%") PCT_S _T("'\n"), datasource);
	  isql_exit (1);
	}
      if (verbose_mode)
	{
	  isql_printf (_T("OpenLink Interactive PL Debugger (Virtuoso).\nType EXIT to exit, HELP for help\n"));
	  isql_printf (_T("Debug session estabilished to %") PCT_S _T("\n"), datasource);
	}
      print_prompt = _T("DEBUG> ");
      rep_loop (stdin, print_prompt);
      return 0;
    }
#endif

  if (verbose_mode && (NOT web_mode))
    {
      isql_printf (_T("%") PCT_S _T(" Interactive SQL ") ISQL_TYPE _T("\n"), PRODUCT_NAME);
      isql_printf (_T("Version %")  PCT_S  _T(" as of %s\n"), ISQL_VERSION, __DATE__);
      isql_printf ("Type HELP; for help and EXIT; to exit.\n");
    }

  if (nth_non_option || shortcuts_used)		/* Used in the traditional way, with datasource,
				   and possibly username and password given from
				   the command line? */
    {				/* Connect now, do not wait. As this is done before the loading
				   of filenames saved to files_to_load vector, the datasource,
				   username and password specified at the command line override
				   the possible definitions for them given with SET DSN,
				   SET UID and SET PWD commands in the beginning of the
				   file to be loaded. Otherwise, if DSN, UID or PWD
				   were given from command line or URL with explicit
				   DSN=dsn, UID=user or PWD=pass options/URL input
				   variables, the settings in the loaded file will still win
				   (simply because they are executed later),
				   thus disabling the possibility of tweaking these from URL
				   in cases where customers are only allowed access to certain
				   things.
				 */
#ifndef ODBC_ONLY
      if (host_shortcut || connect_port)
	{
	  static TCHAR szData[2048];
	  isqlt_stprintf (szData, _T("%") PCT_S _T(":%") PCT_S,
	      host_shortcut ? datasource : _T("localhost"),
	      connect_port ? connect_port : _T("1111"));
	  datasource = szData;
	}
#endif
      connect_to_datasource (datasource, username, password);
    }

  if (load_n_files)
    {
#define MAX_LOAD_EXPR 1001
      TCHAR loadexpr[MAX_LOAD_EXPR + 3];
      for (i = 0; files_to_load[i]; i++)
	{
	  isqlt_tcscpy (loadexpr, _T("Command-Line-Load "));
	  my_strncat (loadexpr, files_to_load[i], MAX_LOAD_EXPR);
	  if (0 == load_file (files_to_load[i], 0, loadexpr))
	    {			/* 0 = normal load, 1 = raw load */
	      if (commit_mode == 2 && fully_connected)
		{
		  RETCODE rc;
		  if (SQL_SUCCESS != (rc = SQLTransact (henv, hdbc, SQL_ROLLBACK)))
		    {
		      print_error (((HENV) 0), hdbc, ((HSTMT) 0), rc);
		    }
		}
	      return (1);	/* Exit with an error code 1 if file not found. */
	    }
	}
/* Exit now if no statements given with EXEC=option(s) */
      if (0 == execs_from_cmdline)
	{
	  goto done;
	}
    }

/* Check if the user has given one or more statements to be executed
   from the command line with EXEC option: */
  if (execs_from_cmdline)
    {
#define MAXSEMICOLS_IN_LINE 50
      int scivec[MAXSEMICOLS_IN_LINE + 1];
      int *sciptr;
      TCHAR *exec_piece;
      integer_list pidlist_space;
      integer_list *pidlist = &pidlist_space;
      pidlist->next = NULL;	/* Initialize the pidlist anchor. */

      for (i = 0; i < execs_from_cmdline; i++)
	{
	  UTCHAR inside_string = 0;
/* The following has nothing to do with braces but semicolons. User can
   give more than one statement to be executed in ONE and SINGLE
   EXEC-option, just by separating them with semicolons.
   Note that count_bracelevel doesn't count as semicolons the semicolons
   found inside strings, after comments -- or inside block statements
   (i.e. if bracelevel > 0). We don't check here that bracelevel returned
   by count_bracelevel is zero, because the user has to have a right to
   give incorrect statements to Kubl server (and we can't be 100% sure
   here what is syntactically correct statement and what is not...)
 */
	  count_bracelevel (execute_these_only[i], 0,
			    scivec, MAXSEMICOLS_IN_LINE, 0, &inside_string);
	  for (exec_piece = execute_these_only[i], sciptr = scivec;
	       *sciptr;
	       sciptr++)
	    {			/* Wipe the next semicolon. Note the one-based indexing: */
	      *((execute_these_only[i]) + ((*sciptr) - 1)) = '\0';
	      exec_one (exec_piece, pidlist);	/* Execute the piece at the left side */
	      /* Restore the semicolon: (for cosmetic & debugging reasons) */
	      *((execute_these_only[i]) + ((*sciptr) - 1)) = ';';
	      exec_piece = (execute_these_only[i] + (*sciptr));
	    }			/* And then skip exec_piece past that semicolon. */

/* Still have to execute the last piece of execute_these_only[i],
   or one and the only one if there were no semicolons at all: */
	  exec_one (exec_piece, pidlist);
	}
      goto done;
    }

  rep_loop (stdin, print_prompt);

done:
  if (commit_mode == 2 && fully_connected)
    {
      RETCODE rc;
      if (SQL_SUCCESS != (rc = SQLTransact (henv, hdbc, SQL_ROLLBACK)))
	{
	  print_error (((HENV) 0), hdbc, ((HSTMT) 0), rc);
	}
    }
  return (0);
}


/* ================================================================== */
/*            FUNCTIONS FOR OUTPUTTING HTML PAGE                      */
/* ================================================================== */

TCHAR *
get_list_of_datasources (int for_html, TCHAR *dest_buf, int dest_size)
{
#if !defined(ODBC_ONLY)
  dest_buf[dest_size - 1] = '\0';
  if (for_html)
    {
      isqlt_tcsncpy (dest_buf, _T("<INPUT NAME=S_DATASOURCE TYPE=TEXT VALUE=\""), dest_size);
      my_strncat (dest_buf, DEFAULT_DATASOURCE_IN_UNIX, dest_size);
      my_strncat (dest_buf, "\">", dest_size);
    }
  else
    {
      isqlt_tcsncpy (dest_buf,
	       _T("Don't know how to list available data sources in Unix. Try "),
	       dest_size);
      my_strncat (dest_buf, DEFAULT_DATASOURCE_IN_UNIX, dest_size);
      my_strncat (dest_buf, _T(" or wherever your Virtuoso server is listening."), dest_size);
    }

#else

#define DRIVDESCMAX 256
#ifndef SQL_MAX_DSN_LENGTH
#define SQL_MAX_DSN_LENGTH 512
#endif

  TCHAR DSN[SQL_MAX_DSN_LENGTH + 1], DriverDescription[DRIVDESCMAX + 1];
  SWORD pcbDSN, pcbDescription;
  int rc, count;

  dest_buf[0] = '\0';
  dest_buf[dest_size] = '\0';

  isqlt_tcscpy (DSN, _T("VIRGEN"));
  isqlt_tcscpy (DriverDescription, _T("CERDO"));

  if (for_html)
    {
      isqlt_tcsncpy (dest_buf, _T("<SELECT NAME=S_DATASOURCE>"), dest_size);
    }

  for (count = 0; 1; count++)
    {
      rc = SQLDataSources (henv,
		       ((UWORD) (count ? SQL_FETCH_NEXT : SQL_FETCH_FIRST)),
			   UCP (DSN),
			   ((SWORD) SQL_MAX_DSN_LENGTH),
			   &pcbDSN,
			   UCP (DriverDescription),
			   ((SWORD) DRIVDESCMAX),
			   &pcbDescription);
      if (rc == SQL_NO_DATA_FOUND)
	{
	  break;
	}
      IF_ERR_GO (stmt, error, rc);

      if (for_html)
	{
	  my_strncat (dest_buf, _T("<OPTION VALUE=\""), dest_size);
	  my_strncat (dest_buf, DSN, dest_size);
	  my_strncat (dest_buf, _T("\">"), dest_size);
	  my_strncat (dest_buf, DSN, dest_size);
	  my_strncat (dest_buf, _T(",   "), dest_size);
	  my_strncat (dest_buf, DriverDescription, dest_size);
	  my_strncat (dest_buf, _T("\n"), dest_size);
	}
      else
	{
	  my_strncat (dest_buf, DSN, dest_size);
	  my_strncat (dest_buf, _T(",   "), dest_size);
	  my_strncat (dest_buf, DriverDescription, dest_size);
	  my_strncat (dest_buf, _T("\n"), dest_size);
	}
    }
error:
  if (for_html)
    {
      my_strncat (dest_buf, _T("</SELECT>"), dest_size);
      if (SQL_ERROR == rc)
	{
	  TCHAR numbuf[80];
	  isql_fprintf (stderr,
			_T("get_list_of_datasources: SQLDataSources returned status %d, count=%d\n"),
			rc, count);
	  fflush (stderr);
	  my_strncat (dest_buf, _T(" **Error: SQLDataSources returned status "), dest_size);
	  isqlt_stprintf (numbuf, _T("%d, count=%d"), rc, count);
	  my_strncat (dest_buf, numbuf, dest_size);
	}
    }

#endif

  return (dest_buf);

}




/*
   A dollar ($) can be escaped in a HTML-template as: &#36;
   except in URLs, where it can be escaped with %24
 */

int
output_line_of_template_with_parameters (UTCHAR * piece_of_template)
{
  UTCHAR *str = piece_of_template, *start_of_this_piece = str, *p;
/*  int whole_len = isqlt_tcslen(SCP(piece_of_template)); */
  int use_html_escapes = 0;
  TCHAR tmp1buf[5005];		/* Big enough. E.g. $DATASOURCES might get long */

  for (; *str;)
    {
      switch (*str)
	{
	case '$':
	  {
/*           if(0 == flag_macro_substitution) { str++; break; } */
/*           else */
	    {
	      UTCHAR *cont;

	      if (str != start_of_this_piece)
		{
		  UTCHAR saved = start_of_this_piece [str - start_of_this_piece];
		  start_of_this_piece [str - start_of_this_piece] = 0;
		  isql_fputs ((TCHAR *) start_of_this_piece, stdout);
		  start_of_this_piece [str - start_of_this_piece] = saved;
		}
	      cont = str;
	      p = UCP (get_next_token (((TCHAR **) &cont),
				       tmp1buf, (sizeof (tmp1buf) - 1),
				       NULL, 0));

	      /* cont points now to the first char after $dollar_form */
	      start_of_this_piece = str = cont;

	      if (NULL != p)	/* Print nothing for nulls */
		{
		  if (use_html_escapes)
		    {
		      html_print_escaped_n (SCP (p), stdout, 0);
		    }
		  else
		    {
		      isql_fputs (SCP (p), stdout);
		    }
		}
	    }
	    break;
	  }			/* case '$' */
	default:
	  {
	    str++;
	    break;
	  }
	}			/* switch */

    }				/* for loop. */

  if (str != start_of_this_piece)	/* print the last remaining piece */
    {
      UTCHAR saved = start_of_this_piece [str - start_of_this_piece];
      start_of_this_piece [str - start_of_this_piece] = 0;
      isql_fputs ((TCHAR *) start_of_this_piece, stdout);
      start_of_this_piece [str - start_of_this_piece] = saved;
    }

  return (1);			/* Something. */
}




FILE *html_infp = NULL;

TCHAR *
line_from_html_file (TCHAR *templatename)
{
  TCHAR tmp1[2002];

  return (isqlt_fgetts (tmp1, ((sizeof (tmp1) / sizeof (TCHAR)) - 1), html_infp));
}

/* Return the whole string in one piece. After that return NULL.
   Really Ad Hoc just for quick, quick work.
 */
TCHAR *
line_from_html_string (TCHAR *templatename)
{
  static TCHAR counts_visited = 0;
  static TCHAR *last_templatename = _T("");
  extern TCHAR *html_page_string;	/* In the end of this module. */

  if ((0 == counts_visited) || (templatename != last_templatename))
    {				/* First time here with this templatename? */
      counts_visited++;
      last_templatename = templatename;

      if (!isqlt_tcscmp (templatename, DEFAULT_HTML_TEMPLATE_FILE))
	{
	  return (html_page_string);
	}
    }
  else
    {
      return (NULL);
    }
  return NULL;
}


int
output_html_page (PFSTR sourcefun, TCHAR *templatename)
{
  TCHAR *line;


  while (NULL != (line = ((sourcefun) (templatename))))
    {
      output_line_of_template_with_parameters (UCP (line));
    }
  return (1);
}

int
output_html_file (TCHAR *templatename)
{
/* When using option -h, then take it always from the memory. */
  if (				/* dump_html_template_file_flag
				   || */
       NO (html_infp = isqlt_tfopen (templatename, _T("r"))))
    {
      output_html_page (line_from_html_string, templatename);
    }
  else
    {
      output_html_page (line_from_html_file, templatename);
    }
  return (1);
}


/*
   The layout is still very crude. See dbump.c for some improvements.
 */
TCHAR *html_page_string
=

_T("<HTML><HEAD><TITLE>Virtuoso ISQL (webface), Version $VERSION</TITLE>\n")
_T("</HEAD>\n")
_T("<BODY>\n")
_T("<H3>Virtuoso Isql Web-Interface Default Form</H3>\n")
_T("<FORM >\n")
_T("<TABLE>\n")
_T("<TR>\n")
_T("<TD><B>Data&nbsp;Source</B></TD>\n")
_T("<TD COLSPAN=2>$") DATASOURCE_TOKEN _T("</TD></TR>\n")
_T("<TR><TD VALIGN=TOP><B>Statements</B><BR>\n")
_T("<BR><CENTER><INPUT TYPE=SUBMIT VALUE=\"DO IT\"></CENTER>\n")
_T("</TD>\n")

_T("<TD COLSPAN=2>\n")
_T("One or more, separated with semicolons (;)<BR>\n")

_T("<TEXTAREA NAME=\"S_EXEC\" ROWS=3 COLS=60></TEXTAREA>\n")

_T("<TR><TD VALIGN=TOP><B>Options</B></TD>\n")

_T("<TD COLSPAN=2>\n")

_T("<TABLE>\n")

_T("<TR>\n")
_T("<TD><INPUT NAME=\"S_CONUSER\" TYPE=TEXT SIZE=8>\n")
_T("<!-- B -->Username<!-- /B --></TD>\n")

_T("<TD><INPUT NAME=\"S_CONPASS\" TYPE=PASSWORD SIZE=8>\n")
_T("<!-- B -->Password<!-- /B --></TD>\n")
_T("</TR>\n")

_T("<TR>\n")
_T("<TD><INPUT NAME=\"S_MAXROWS\" TYPE=TEXT SIZE=8 VALUE=\"1000\">\n")
_T("<!-- B -->Max Rows<!-- /B --></TD>\n")

_T("<TD><INPUT NAME=\"S_TIMEOUT\" TYPE=TEXT SIZE=8 VALUE=\"1000\">\n")
_T("<!-- B -->Timeout in Seconds<!-- /B --></TD>\n")
_T("</TR>\n")

_T("<TR>\n")
_T("<TD><INPUT TYPE=CHECKBOX NAME=\"S_READMODE\" VALUE=\"SNAPSHOT\">\n")
_T("<!-- B -->Snapshot Readmode<!-- /B --></TD>\n")

_T("<TD>\n")
_T("<INPUT TYPE=CHECKBOX NAME=\"S_AUTOCOMMIT\" VALUE=\"ON\"> <!-- B -->Autocommitted Execution<!-- /B -->\n")
_T("</TD>\n")
_T("</TR>\n")

_T("<TR>\n")
_T("<TD>\n")
_T("<INPUT TYPE=CHECKBOX NAME=\"S_BLOBS\" VALUE=\"ON\"> <!-- B -->Show Blobs<!-- /B -->\n")
_T("</TD>\n")
_T("<TD>\n")
_T("<INPUT TYPE=CHECKBOX NAME=\"S_BANNER\" VALUE=\"OFF\"> <!-- B -->No Banner<!-- /B -->\n")
_T("</TD>\n")
_T("</TR>\n")

_T("<TR>\n")
_T("<TD>\n")
_T("<INPUT TYPE=RADIO NAME=\"S_VERBOSE\" VALUE=\"OFF\"> <!-- B -->Brief<!-- /B -->\n")
_T("</TD>\n")
_T("<TD>\n")
_T("<INPUT TYPE=RADIO CHECKED NAME=\"S_VERBOSE\" VALUE=\"ON\"> <!-- B -->Verbose Output<!-- /B -->\n")
_T("</TD>\n")
_T("</TR>\n")


_T("<TR>\n")
_T("<TD>\n")
_T("<INPUT TYPE=RADIO NAME=\"S_CONTENT_TYPE\" VALUE=\"text/plain\">\n")
_T("<!-- B -->Dump in raw text format to screen<!-- /B -->\n")
_T("</TD>\n")

_T("<TD>\n")
_T("<INPUT TYPE=RADIO NAME=\"S_CONTENT_TYPE\" VALUE=\"x-isql/plain\">\n")

_T("<!-- B -->Dump in raw text format to file<!-- /B -->\n")

_T("</TD>\n")
_T("</TR>\n")

_T("<TR>\n")
_T("<TD COLSPAN=2>\n")
_T("<INPUT CHECKED TYPE=RADIO NAME=\"S_CONTENT_TYPE\" VALUE=\"text/html\">\n")

_T("<!-- B -->Dump in HTML format to screen<!-- /B -->\n")

_T("</TD>\n")
_T("</TR>\n")

_T("<TR>\n")

_T("<TD><INPUT CHECKED TYPE=CHECKBOX NAME=\"S_OO_SB\" VALUE=\"&LT;TABLE&#32;BORDER&GT;\">\n")

_T("<!-- B -->Use Table Borders<!-- /B --></TD>\n")

_T("<TD><INPUT TYPE=CHECKBOX NAME=\"S_OO_ESC\" VALUE=\"OFF\">\n")

_T("<!-- B -->No HTML-Escaping<!-- /B --></TD>\n")

_T("</TR>\n")


_T("<TR>\n")
_T("<TD><INPUT TYPE=SUBMIT VALUE=\"Do it\"></TD>\n")

_T("<TD><INPUT TYPE=RESET VALUE=\"Reset\"></TD>\n")
_T("</TR>\n")

_T("</TABLE>\n")

_T("</TD>\n")
_T("</TR>\n")

_T("</TR>\n")


_T("</TABLE>\n")

_T("</FORM>\n")

_T("<H3>Administration</H3>\n")

_T("<UL>\n")
_T("<LI><A HREF=\"$FORM_ACTION?S_EXEC=status\">Database Status</A>\n")
_T("<LI><A HREF=\"$FORM_ACTION?S_CONTENT_TYPE=text/plain&S_EXEC=help\">Help</A>\n")
_T("</UL>\n")

_T("<TABLE>\n")
_T("<TR>\n")
_T("<TD><FORM METHOD=GET $IF $FORM_ACTION 'ACTION=\"'$FORM_ACTION$IF $FORM_ACTION '\"'>\n")
_T("<INPUT TYPE=HIDDEN NAME=S_READMODE VALUE=SNAPSHOT>\n")
_T("<INPUT TYPE=HIDDEN NAME=S_EXEC VALUE=\"backup('&#36;U{BKFILE}')\">\n")
_T("Make a backup, to file: </TD>\n")
_T("<TD><INPUT TYPE=TEXT NAME=BKFILE SIZE=20 VALUE=\"$YYYYMMDD&#46;log\"></TD>\n")
_T("<TD><INPUT TYPE=SUBMIT VALUE=\"DO IT\"></FORM></TD>\n")
_T("</TR>\n")

_T("<TR>\n")
_T("<TD><FORM METHOD=GET $IF $FORM_ACTION 'ACTION=\"'$FORM_ACTION$IF $FORM_ACTION '\"'>\n")
_T("<INPUT TYPE=HIDDEN NAME=S_EXEC VALUE=\"checkpoint &#36;U{NEWLOG}\">\n")
_T("Make a checkpoint, new log: </TD>\n")
_T("<TD><INPUT TYPE=TEXT NAME=NEWLOG SIZE=20></TD>\n")
_T("<TD><INPUT TYPE=SUBMIT VALUE=\"DO IT\"></FORM></TD>\n")
_T("</TR>\n")
_T("<TR>\n")
_T("<TD><FORM METHOD=GET $IF $FORM_ACTION 'ACTION=\"'$FORM_ACTION$IF $FORM_ACTION '\"'>\n")
_T("<INPUT TYPE=HIDDEN NAME=S_EXEC VALUE=\"shutdown &#36;U{NEWLOG}\">\n")
_T("Shutdown server, new log: </TD>\n")
_T("<TD><INPUT TYPE=TEXT NAME=NEWLOG SIZE=20></TD>\n")
_T("<TD><INPUT TYPE=SUBMIT VALUE=\"DO IT\"></FORM></TD>\n")
_T("</TR>\n")
_T("</TABLE>\n")

_T("<HR>\n")

_T("<H3>Some Notes</H3>\n")
_T("<UL><LI>\n")

_T("Use <B>dump to file instead of screen</B> option to send the data\n")
_T("with the unknown content type instead of text/plain, thus forcing\n")
_T("your browser to ask for a filename where to save the output, instead\n")
_T("of showing it on the screen. Use this option if you have megabytes\n")
_T("to output and don't want to thrash your browser.\n")

_T("<P><LI>\n")

_T("<B>Warning:</B>\n")

_T("Leaving this utility unprotected without password in a public Web-server\n")
_T("constitutes a clear security risk to your machine.\n")
_T("Not only it can be used to gain access\n")
_T("to all <B>Unprotected System DSN's</B>, but it can be used for\n")
_T("gaining access to <B>the underlying operating system</B> as well, via\n")
_T("a shell-escape mechanism.\n")
_T("<BR>So you are strongly suggested either to set a password for this\n")
_T("cgi-bin script (by configuring your Web-server),\n")
/* Very leaky protection: that "secret" URL can be easily found from
   caches, etc.
   _T("or to hide it with such a name into your script-directory or subdirectory\n")
   _T("that it cannot be guessed by outsiders,\n")
 */
_T("or to totally deny any public access to your local Web server.\n")

_T("<P><LI>\n")

_T("</UL>\n")
_T("<HR>\n")
_T("</BODY></HTML>\n");