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
|
/*
* tclUtil.c --
*
* This file contains utility procedures that are used by many Tcl
* commands.
*
* Copyright (c) 1987-1993 The Regents of the University of California.
* Copyright (c) 1994-1997 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* SCCS: %Z% $Id: tclUtil.c,v 1.2 1998/06/12 16:49:08 suresh Exp $
*/
#include "tclInt.h"
#include "tclPort.h"
/*
* The following variable holds the full path name of the binary
* from which this application was executed, or NULL if it isn't
* know. The value of the variable is set by the procedure
* Tcl_FindExecutable. The storage space is dynamically allocated.
*/
char *tclExecutableName = NULL;
/*
* The following values are used in the flags returned by Tcl_ScanElement
* and used by Tcl_ConvertElement. The value TCL_DONT_USE_BRACES is also
* defined in tcl.h; make sure its value doesn't overlap with any of the
* values below.
*
* TCL_DONT_USE_BRACES - 1 means the string mustn't be enclosed in
* braces (e.g. it contains unmatched braces,
* or ends in a backslash character, or user
* just doesn't want braces); handle all
* special characters by adding backslashes.
* USE_BRACES - 1 means the string contains a special
* character that can be handled simply by
* enclosing the entire argument in braces.
* BRACES_UNMATCHED - 1 means that braces aren't properly matched
* in the argument.
*/
#define USE_BRACES 2
#define BRACES_UNMATCHED 4
/*
* The following values determine the precision used when converting
* floating-point values to strings. This information is linked to all
* of the tcl_precision variables in all interpreters via the procedure
* TclPrecTraceProc.
*
* NOTE: these variables are not thread-safe.
*/
static char precisionString[10] = "12";
/* The string value of all the tcl_precision
* variables. */
static char precisionFormat[10] = "%.12g";
/* The format string actually used in calls
* to sprintf. */
/*
* Function prototypes for local procedures in this file:
*/
static void SetupAppendBuffer _ANSI_ARGS_((Interp *iPtr,
int newSpace));
/*
*----------------------------------------------------------------------
*
* TclFindElement --
*
* Given a pointer into a Tcl list, locate the first (or next)
* element in the list.
*
* Results:
* The return value is normally TCL_OK, which means that the
* element was successfully located. If TCL_ERROR is returned
* it means that list didn't have proper list structure;
* interp->result contains a more detailed error message.
*
* If TCL_OK is returned, then *elementPtr will be set to point to the
* first element of list, and *nextPtr will be set to point to the
* character just after any white space following the last character
* that's part of the element. If this is the last argument in the
* list, then *nextPtr will point just after the last character in the
* list (i.e., at the character at list+listLength). If sizePtr is
* non-NULL, *sizePtr is filled in with the number of characters in the
* element. If the element is in braces, then *elementPtr will point
* to the character after the opening brace and *sizePtr will not
* include either of the braces. If there isn't an element in the list,
* *sizePtr will be zero, and both *elementPtr and *termPtr will point
* just after the last character in the list. Note: this procedure does
* NOT collapse backslash sequences.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
TclFindElement(interp, list, listLength, elementPtr, nextPtr, sizePtr,
bracePtr)
Tcl_Interp *interp; /* Interpreter to use for error reporting.
* If NULL, then no error message is left
* after errors. */
char *list; /* Points to the first byte of a string
* containing a Tcl list with zero or more
* elements (possibly in braces). */
int listLength; /* Number of bytes in the list's string. */
char **elementPtr; /* Where to put address of first significant
* character in first element of list. */
char **nextPtr; /* Fill in with location of character just
* after all white space following end of
* argument (next arg or end of list). */
int *sizePtr; /* If non-zero, fill in with size of
* element. */
int *bracePtr; /* If non-zero, fill in with non-zero/zero
* to indicate that arg was/wasn't
* in braces. */
{
char *p = list;
char *elemStart; /* Points to first byte of first element. */
char *limit; /* Points just after list's last byte. */
int openBraces = 0; /* Brace nesting level during parse. */
int inQuotes = 0;
int size = 0; /* Init. avoids compiler warning. */
int numChars;
char *p2;
/*
* Skim off leading white space and check for an opening brace or
* quote. We treat embedded NULLs in the list as bytes belonging to
* a list element. Note: use of "isascii" below and elsewhere in this
* procedure is a temporary hack (7/27/90) because Mx uses characters
* with the high-order bit set for some things. This should probably
* be changed back eventually, or all of Tcl should call isascii.
*/
limit = (list + listLength);
while ((p < limit) && (isspace(UCHAR(*p)))) {
p++;
}
if (p == limit) { /* no element found */
elemStart = limit;
goto done;
}
if (*p == '{') {
openBraces = 1;
p++;
} else if (*p == '"') {
inQuotes = 1;
p++;
}
elemStart = p;
if (bracePtr != 0) {
*bracePtr = openBraces;
}
/*
* Find element's end (a space, close brace, or the end of the string).
*/
while (p < limit) {
switch (*p) {
/*
* Open brace: don't treat specially unless the element is in
* braces. In this case, keep a nesting count.
*/
case '{':
if (openBraces != 0) {
openBraces++;
}
break;
/*
* Close brace: if element is in braces, keep nesting count and
* quit when the last close brace is seen.
*/
case '}':
if (openBraces > 1) {
openBraces--;
} else if (openBraces == 1) {
size = (p - elemStart);
p++;
if ((p >= limit) || isspace(UCHAR(*p))) {
goto done;
}
/*
* Garbage after the closing brace; return an error.
*/
if (interp != NULL) {
char buf[100];
p2 = p;
while ((p2 < limit) && (!isspace(UCHAR(*p2)))
&& (p2 < p+20)) {
p2++;
}
sprintf(buf,
"list element in braces followed by \"%.*s\" instead of space",
(int) (p2-p), p);
Tcl_SetResult(interp, buf, TCL_VOLATILE);
}
return TCL_ERROR;
}
break;
/*
* Backslash: skip over everything up to the end of the
* backslash sequence.
*/
case '\\': {
(void) Tcl_Backslash(p, &numChars);
p += (numChars - 1);
break;
}
/*
* Space: ignore if element is in braces or quotes; otherwise
* terminate element.
*/
case ' ':
case '\f':
case '\n':
case '\r':
case '\t':
case '\v':
if ((openBraces == 0) && !inQuotes) {
size = (p - elemStart);
goto done;
}
break;
/*
* Double-quote: if element is in quotes then terminate it.
*/
case '"':
if (inQuotes) {
size = (p - elemStart);
p++;
if ((p >= limit) || isspace(UCHAR(*p))) {
goto done;
}
/*
* Garbage after the closing quote; return an error.
*/
if (interp != NULL) {
char buf[100];
p2 = p;
while ((p2 < limit) && (!isspace(UCHAR(*p2)))
&& (p2 < p+20)) {
p2++;
}
sprintf(buf,
"list element in quotes followed by \"%.*s\" %s",
(int) (p2-p), p, "instead of space");
Tcl_SetResult(interp, buf, TCL_VOLATILE);
}
return TCL_ERROR;
}
break;
}
p++;
}
/*
* End of list: terminate element.
*/
if (p == limit) {
if (openBraces != 0) {
if (interp != NULL) {
Tcl_SetResult(interp, "unmatched open brace in list",
TCL_STATIC);
}
return TCL_ERROR;
} else if (inQuotes) {
if (interp != NULL) {
Tcl_SetResult(interp, "unmatched open quote in list",
TCL_STATIC);
}
return TCL_ERROR;
}
size = (p - elemStart);
}
done:
while ((p < limit) && (isspace(UCHAR(*p)))) {
p++;
}
*elementPtr = elemStart;
*nextPtr = p;
if (sizePtr != 0) {
*sizePtr = size;
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* TclCopyAndCollapse --
*
* Copy a string and eliminate any backslashes that aren't in braces.
*
* Results:
* There is no return value. Count characters get copied from src to
* dst. Along the way, if backslash sequences are found outside braces,
* the backslashes are eliminated in the copy. After scanning count
* chars from source, a null character is placed at the end of dst.
* Returns the number of characters that got copied.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
TclCopyAndCollapse(count, src, dst)
int count; /* Number of characters to copy from src. */
char *src; /* Copy from here... */
char *dst; /* ... to here. */
{
char c;
int numRead;
int newCount = 0;
for (c = *src; count > 0; src++, c = *src, count--) {
if (c == '\\') {
*dst = Tcl_Backslash(src, &numRead);
dst++;
src += numRead-1;
count -= numRead-1;
newCount++;
} else {
*dst = c;
dst++;
newCount++;
}
}
*dst = 0;
return newCount;
}
/*
*----------------------------------------------------------------------
*
* Tcl_SplitList --
*
* Splits a list up into its constituent fields.
*
* Results
* The return value is normally TCL_OK, which means that
* the list was successfully split up. If TCL_ERROR is
* returned, it means that "list" didn't have proper list
* structure; interp->result will contain a more detailed
* error message.
*
* *argvPtr will be filled in with the address of an array
* whose elements point to the elements of list, in order.
* *argcPtr will get filled in with the number of valid elements
* in the array. A single block of memory is dynamically allocated
* to hold both the argv array and a copy of the list (with
* backslashes and braces removed in the standard way).
* The caller must eventually free this memory by calling free()
* on *argvPtr. Note: *argvPtr and *argcPtr are only modified
* if the procedure returns normally.
*
* Side effects:
* Memory is allocated.
*
*----------------------------------------------------------------------
*/
int
Tcl_SplitList(interp, list, argcPtr, argvPtr)
Tcl_Interp *interp; /* Interpreter to use for error reporting.
* If NULL, no error message is left. */
char *list; /* Pointer to string with list structure. */
int *argcPtr; /* Pointer to location to fill in with
* the number of elements in the list. */
char ***argvPtr; /* Pointer to place to store pointer to
* array of pointers to list elements. */
{
char **argv;
char *p;
int length, size, i, result, elSize, brace;
char *element;
/*
* Figure out how much space to allocate. There must be enough
* space for both the array of pointers and also for a copy of
* the list. To estimate the number of pointers needed, count
* the number of space characters in the list.
*/
for (size = 1, p = list; *p != 0; p++) {
if (isspace(UCHAR(*p))) {
size++;
}
}
size++; /* Leave space for final NULL pointer. */
argv = (char **) ckalloc((unsigned)
((size * sizeof(char *)) + (p - list) + 1));
length = strlen(list);
for (i = 0, p = ((char *) argv) + size*sizeof(char *);
*list != 0; i++) {
char *prevList = list;
result = TclFindElement(interp, list, length, &element,
&list, &elSize, &brace);
length -= (list - prevList);
if (result != TCL_OK) {
ckfree((char *) argv);
return result;
}
if (*element == 0) {
break;
}
if (i >= size) {
ckfree((char *) argv);
if (interp != NULL) {
Tcl_SetResult(interp, "internal error in Tcl_SplitList",
TCL_STATIC);
}
return TCL_ERROR;
}
argv[i] = p;
if (brace) {
memcpy((VOID *) p, (VOID *) element, (size_t) elSize);
p += elSize;
*p = 0;
p++;
} else {
TclCopyAndCollapse(elSize, element, p);
p += elSize+1;
}
}
argv[i] = NULL;
*argvPtr = argv;
*argcPtr = i;
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* Tcl_ScanElement --
*
* This procedure is a companion procedure to Tcl_ConvertElement.
* It scans a string to see what needs to be done to it (e.g. add
* backslashes or enclosing braces) to make the string into a
* valid Tcl list element.
*
* Results:
* The return value is an overestimate of the number of characters
* that will be needed by Tcl_ConvertElement to produce a valid
* list element from string. The word at *flagPtr is filled in
* with a value needed by Tcl_ConvertElement when doing the actual
* conversion.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
Tcl_ScanElement(string, flagPtr)
CONST char *string; /* String to convert to Tcl list element. */
int *flagPtr; /* Where to store information to guide
* Tcl_ConvertCountedElement. */
{
return Tcl_ScanCountedElement(string, -1, flagPtr);
}
/*
*----------------------------------------------------------------------
*
* Tcl_ScanCountedElement --
*
* This procedure is a companion procedure to
* Tcl_ConvertCountedElement. It scans a string to see what
* needs to be done to it (e.g. add backslashes or enclosing
* braces) to make the string into a valid Tcl list element.
* If length is -1, then the string is scanned up to the first
* null byte.
*
* Results:
* The return value is an overestimate of the number of characters
* that will be needed by Tcl_ConvertCountedElement to produce a
* valid list element from string. The word at *flagPtr is
* filled in with a value needed by Tcl_ConvertCountedElement
* when doing the actual conversion.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
Tcl_ScanCountedElement(string, length, flagPtr)
CONST char *string; /* String to convert to Tcl list element. */
int length; /* Number of bytes in string, or -1. */
int *flagPtr; /* Where to store information to guide
* Tcl_ConvertElement. */
{
int flags, nestingLevel;
CONST char *p, *lastChar;
/*
* This procedure and Tcl_ConvertElement together do two things:
*
* 1. They produce a proper list, one that will yield back the
* argument strings when evaluated or when disassembled with
* Tcl_SplitList. This is the most important thing.
*
* 2. They try to produce legible output, which means minimizing the
* use of backslashes (using braces instead). However, there are
* some situations where backslashes must be used (e.g. an element
* like "{abc": the leading brace will have to be backslashed.
* For each element, one of three things must be done:
*
* (a) Use the element as-is (it doesn't contain any special
* characters). This is the most desirable option.
*
* (b) Enclose the element in braces, but leave the contents alone.
* This happens if the element contains embedded space, or if it
* contains characters with special interpretation ($, [, ;, or \),
* or if it starts with a brace or double-quote, or if there are
* no characters in the element.
*
* (c) Don't enclose the element in braces, but add backslashes to
* prevent special interpretation of special characters. This is a
* last resort used when the argument would normally fall under case
* (b) but contains unmatched braces. It also occurs if the last
* character of the argument is a backslash or if the element contains
* a backslash followed by newline.
*
* The procedure figures out how many bytes will be needed to store
* the result (actually, it overestimates). It also collects information
* about the element in the form of a flags word.
*
* Note: list elements produced by this procedure and
* Tcl_ConvertCountedElement must have the property that they can be
* enclosing in curly braces to make sub-lists. This means, for
* example, that we must not leave unmatched curly braces in the
* resulting list element. This property is necessary in order for
* procedures like Tcl_DStringStartSublist to work.
*/
nestingLevel = 0;
flags = 0;
if (string == NULL) {
string = "";
}
if (length == -1) {
length = strlen(string);
}
lastChar = string + length;
p = string;
if ((p == lastChar) || (*p == '{') || (*p == '"')) {
flags |= USE_BRACES;
}
for ( ; p != lastChar; p++) {
switch (*p) {
case '{':
nestingLevel++;
break;
case '}':
nestingLevel--;
if (nestingLevel < 0) {
flags |= TCL_DONT_USE_BRACES|BRACES_UNMATCHED;
}
break;
case '[':
case '$':
case ';':
case ' ':
case '\f':
case '\n':
case '\r':
case '\t':
case '\v':
flags |= USE_BRACES;
break;
case '\\':
if ((p+1 == lastChar) || (p[1] == '\n')) {
flags = TCL_DONT_USE_BRACES | BRACES_UNMATCHED;
} else {
int size;
(void) Tcl_Backslash(p, &size);
p += size-1;
flags |= USE_BRACES;
}
break;
}
}
if (nestingLevel != 0) {
flags = TCL_DONT_USE_BRACES | BRACES_UNMATCHED;
}
*flagPtr = flags;
/*
* Allow enough space to backslash every character plus leave
* two spaces for braces.
*/
return 2*(p-string) + 2;
}
/*
*----------------------------------------------------------------------
*
* Tcl_ConvertElement --
*
* This is a companion procedure to Tcl_ScanElement. Given
* the information produced by Tcl_ScanElement, this procedure
* converts a string to a list element equal to that string.
*
* Results:
* Information is copied to *dst in the form of a list element
* identical to src (i.e. if Tcl_SplitList is applied to dst it
* will produce a string identical to src). The return value is
* a count of the number of characters copied (not including the
* terminating NULL character).
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
Tcl_ConvertElement(src, dst, flags)
CONST char *src; /* Source information for list element. */
char *dst; /* Place to put list-ified element. */
int flags; /* Flags produced by Tcl_ScanElement. */
{
return Tcl_ConvertCountedElement(src, -1, dst, flags);
}
/*
*----------------------------------------------------------------------
*
* Tcl_ConvertCountedElement --
*
* This is a companion procedure to Tcl_ScanCountedElement. Given
* the information produced by Tcl_ScanCountedElement, this
* procedure converts a string to a list element equal to that
* string.
*
* Results:
* Information is copied to *dst in the form of a list element
* identical to src (i.e. if Tcl_SplitList is applied to dst it
* will produce a string identical to src). The return value is
* a count of the number of characters copied (not including the
* terminating NULL character).
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
Tcl_ConvertCountedElement(src, length, dst, flags)
CONST char *src; /* Source information for list element. */
int length; /* Number of bytes in src, or -1. */
char *dst; /* Place to put list-ified element. */
int flags; /* Flags produced by Tcl_ScanElement. */
{
char *p = dst;
CONST char *lastChar;
/*
* See the comment block at the beginning of the Tcl_ScanElement
* code for details of how this works.
*/
if (src && length == -1) {
length = strlen(src);
}
if ((src == NULL) || (length == 0)) {
p[0] = '{';
p[1] = '}';
p[2] = 0;
return 2;
}
lastChar = src + length;
if ((flags & USE_BRACES) && !(flags & TCL_DONT_USE_BRACES)) {
*p = '{';
p++;
for ( ; src != lastChar; src++, p++) {
*p = *src;
}
*p = '}';
p++;
} else {
if (*src == '{') {
/*
* Can't have a leading brace unless the whole element is
* enclosed in braces. Add a backslash before the brace.
* Furthermore, this may destroy the balance between open
* and close braces, so set BRACES_UNMATCHED.
*/
p[0] = '\\';
p[1] = '{';
p += 2;
src++;
flags |= BRACES_UNMATCHED;
}
for (; src != lastChar; src++) {
switch (*src) {
case ']':
case '[':
case '$':
case ';':
case ' ':
case '\\':
case '"':
*p = '\\';
p++;
break;
case '{':
case '}':
/*
* It may not seem necessary to backslash braces, but
* it is. The reason for this is that the resulting
* list element may actually be an element of a sub-list
* enclosed in braces (e.g. if Tcl_DStringStartSublist
* has been invoked), so there may be a brace mismatch
* if the braces aren't backslashed.
*/
if (flags & BRACES_UNMATCHED) {
*p = '\\';
p++;
}
break;
case '\f':
*p = '\\';
p++;
*p = 'f';
p++;
continue;
case '\n':
*p = '\\';
p++;
*p = 'n';
p++;
continue;
case '\r':
*p = '\\';
p++;
*p = 'r';
p++;
continue;
case '\t':
*p = '\\';
p++;
*p = 't';
p++;
continue;
case '\v':
*p = '\\';
p++;
*p = 'v';
p++;
continue;
}
*p = *src;
p++;
}
}
*p = '\0';
return p-dst;
}
/*
*----------------------------------------------------------------------
*
* Tcl_Merge --
*
* Given a collection of strings, merge them together into a
* single string that has proper Tcl list structured (i.e.
* Tcl_SplitList may be used to retrieve strings equal to the
* original elements, and Tcl_Eval will parse the string back
* into its original elements).
*
* Results:
* The return value is the address of a dynamically-allocated
* string containing the merged list.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
char *
Tcl_Merge(argc, argv)
int argc; /* How many strings to merge. */
char **argv; /* Array of string values. */
{
# define LOCAL_SIZE 20
int localFlags[LOCAL_SIZE], *flagPtr;
int numChars;
char *result;
char *dst;
int i;
/*
* Pass 1: estimate space, gather flags.
*/
if (argc <= LOCAL_SIZE) {
flagPtr = localFlags;
} else {
flagPtr = (int *) ckalloc((unsigned) argc*sizeof(int));
}
numChars = 1;
for (i = 0; i < argc; i++) {
numChars += Tcl_ScanElement(argv[i], &flagPtr[i]) + 1;
}
/*
* Pass two: copy into the result area.
*/
result = (char *) ckalloc((unsigned) numChars);
dst = result;
for (i = 0; i < argc; i++) {
numChars = Tcl_ConvertElement(argv[i], dst, flagPtr[i]);
dst += numChars;
*dst = ' ';
dst++;
}
if (dst == result) {
*dst = 0;
} else {
dst[-1] = 0;
}
if (flagPtr != localFlags) {
ckfree((char *) flagPtr);
}
return result;
}
/*
*----------------------------------------------------------------------
*
* Tcl_Concat --
*
* Concatenate a set of strings into a single large string.
*
* Results:
* The return value is dynamically-allocated string containing
* a concatenation of all the strings in argv, with spaces between
* the original argv elements.
*
* Side effects:
* Memory is allocated for the result; the caller is responsible
* for freeing the memory.
*
*----------------------------------------------------------------------
*/
char *
Tcl_Concat(argc, argv)
int argc; /* Number of strings to concatenate. */
char **argv; /* Array of strings to concatenate. */
{
int totalSize, i;
char *p;
char *result;
for (totalSize = 1, i = 0; i < argc; i++) {
totalSize += strlen(argv[i]) + 1;
}
result = (char *) ckalloc((unsigned) totalSize);
if (argc == 0) {
*result = '\0';
return result;
}
for (p = result, i = 0; i < argc; i++) {
char *element;
int length;
/*
* Clip white space off the front and back of the string
* to generate a neater result, and ignore any empty
* elements.
*/
element = argv[i];
while (isspace(UCHAR(*element))) {
element++;
}
for (length = strlen(element);
(length > 0) && (isspace(UCHAR(element[length-1])))
&& ((length < 2) || (element[length-2] != '\\'));
length--) {
/* Null loop body. */
}
if (length == 0) {
continue;
}
memcpy((VOID *) p, (VOID *) element, (size_t) length);
p += length;
*p = ' ';
p++;
}
if (p != result) {
p[-1] = 0;
} else {
*p = 0;
}
return result;
}
/*
*----------------------------------------------------------------------
*
* Tcl_ConcatObj --
*
* Concatenate the strings from a set of objects into a single string
* object with spaces between the original strings.
*
* Results:
* The return value is a new string object containing a concatenation
* of the strings in objv. Its ref count is zero.
*
* Side effects:
* A new object is created.
*
*----------------------------------------------------------------------
*/
Tcl_Obj *
Tcl_ConcatObj(objc, objv)
int objc; /* Number of objects to concatenate. */
Tcl_Obj *CONST objv[]; /* Array of objects to concatenate. */
{
int allocSize, finalSize, length, elemLength, i;
char *p;
char *element;
char *concatStr;
Tcl_Obj *objPtr;
allocSize = 0;
for (i = 0; i < objc; i++) {
objPtr = objv[i];
element = TclGetStringFromObj(objPtr, &length);
if ((element != NULL) && (length > 0)) {
allocSize += (length + 1);
}
}
if (allocSize == 0) {
allocSize = 1; /* enough for the NULL byte at end */
}
/*
* Allocate storage for the concatenated result. Note that allocSize
* is one more than the total number of characters, and so includes
* room for the terminating NULL byte.
*/
concatStr = (char *) ckalloc((unsigned) allocSize);
/*
* Now concatenate the elements. Clip white space off the front and back
* to generate a neater result, and ignore any empty elements. Also put
* a null byte at the end.
*/
finalSize = 0;
if (objc == 0) {
*concatStr = '\0';
} else {
p = concatStr;
for (i = 0; i < objc; i++) {
objPtr = objv[i];
element = TclGetStringFromObj(objPtr, &elemLength);
while ((elemLength > 0) && (isspace(UCHAR(*element)))) {
element++;
elemLength--;
}
/*
* Trim trailing white space. But, be careful not to trim
* a space character if it is preceded by a backslash: in
* this case it could be significant.
*/
while ((elemLength > 0)
&& isspace(UCHAR(element[elemLength-1]))
&& ((elemLength < 2) || (element[elemLength-2] != '\\'))) {
elemLength--;
}
if (elemLength == 0) {
continue; /* nothing left of this element */
}
memcpy((VOID *) p, (VOID *) element, (size_t) elemLength);
p += elemLength;
*p = ' ';
p++;
finalSize += (elemLength + 1);
}
if (p != concatStr) {
p[-1] = 0;
finalSize -= 1; /* we overwrote the final ' ' */
} else {
*p = 0;
}
}
TclNewObj(objPtr);
objPtr->bytes = concatStr;
objPtr->length = finalSize;
return objPtr;
}
/*
*----------------------------------------------------------------------
*
* Tcl_StringMatch --
*
* See if a particular string matches a particular pattern.
*
* Results:
* The return value is 1 if string matches pattern, and
* 0 otherwise. The matching operation permits the following
* special characters in the pattern: *?\[] (see the manual
* entry for details on what these mean).
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
Tcl_StringMatch(string, pattern)
char *string; /* String. */
char *pattern; /* Pattern, which may contain special
* characters. */
{
char c2;
while (1) {
/* See if we're at the end of both the pattern and the string.
* If so, we succeeded. If we're at the end of the pattern
* but not at the end of the string, we failed.
*/
if (*pattern == 0) {
if (*string == 0) {
return 1;
} else {
return 0;
}
}
if ((*string == 0) && (*pattern != '*')) {
return 0;
}
/* Check for a "*" as the next pattern character. It matches
* any substring. We handle this by calling ourselves
* recursively for each postfix of string, until either we
* match or we reach the end of the string.
*/
if (*pattern == '*') {
pattern += 1;
if (*pattern == 0) {
return 1;
}
while (1) {
if (Tcl_StringMatch(string, pattern)) {
return 1;
}
if (*string == 0) {
return 0;
}
string += 1;
}
}
/* Check for a "?" as the next pattern character. It matches
* any single character.
*/
if (*pattern == '?') {
goto thisCharOK;
}
/* Check for a "[" as the next pattern character. It is followed
* by a list of characters that are acceptable, or by a range
* (two characters separated by "-").
*/
if (*pattern == '[') {
pattern += 1;
while (1) {
if ((*pattern == ']') || (*pattern == 0)) {
return 0;
}
if (*pattern == *string) {
break;
}
if (pattern[1] == '-') {
c2 = pattern[2];
if (c2 == 0) {
return 0;
}
if ((*pattern <= *string) && (c2 >= *string)) {
break;
}
if ((*pattern >= *string) && (c2 <= *string)) {
break;
}
pattern += 2;
}
pattern += 1;
}
while (*pattern != ']') {
if (*pattern == 0) {
pattern--;
break;
}
pattern += 1;
}
goto thisCharOK;
}
/* If the next pattern character is '/', just strip off the '/'
* so we do exact matching on the character that follows.
*/
if (*pattern == '\\') {
pattern += 1;
if (*pattern == 0) {
return 0;
}
}
/* There's no special character. Just make sure that the next
* characters of each string match.
*/
if (*pattern != *string) {
return 0;
}
thisCharOK: pattern += 1;
string += 1;
}
}
/*
*----------------------------------------------------------------------
*
* Tcl_SetResult --
*
* Arrange for "string" to be the Tcl return value.
*
* Results:
* None.
*
* Side effects:
* interp->result is left pointing either to "string" (if "copy" is 0)
* or to a copy of string. Also, the object result is reset.
*
*----------------------------------------------------------------------
*/
void
Tcl_SetResult(interp, string, freeProc)
Tcl_Interp *interp; /* Interpreter with which to associate the
* return value. */
char *string; /* Value to be returned. If NULL, the
* result is set to an empty string. */
Tcl_FreeProc *freeProc; /* Gives information about the string:
* TCL_STATIC, TCL_VOLATILE, or the address
* of a Tcl_FreeProc such as free. */
{
Interp *iPtr = (Interp *) interp;
int length;
Tcl_FreeProc *oldFreeProc = iPtr->freeProc;
char *oldResult = iPtr->result;
if (string == NULL) {
iPtr->resultSpace[0] = 0;
iPtr->result = iPtr->resultSpace;
iPtr->freeProc = 0;
} else if (freeProc == TCL_VOLATILE) {
length = strlen(string);
if (length > TCL_RESULT_SIZE) {
iPtr->result = (char *) ckalloc((unsigned) length+1);
iPtr->freeProc = TCL_DYNAMIC;
} else {
iPtr->result = iPtr->resultSpace;
iPtr->freeProc = 0;
}
strcpy(iPtr->result, string);
} else {
iPtr->result = string;
iPtr->freeProc = freeProc;
}
/*
* If the old result was dynamically-allocated, free it up. Do it
* here, rather than at the beginning, in case the new result value
* was part of the old result value.
*/
if (oldFreeProc != 0) {
if ((oldFreeProc == TCL_DYNAMIC)
|| (oldFreeProc == (Tcl_FreeProc *) free)) {
ckfree(oldResult);
} else {
(*oldFreeProc)(oldResult);
}
}
/*
* Reset the object result since we just set the string result.
*/
TclResetObjResult(iPtr);
}
/*
*----------------------------------------------------------------------
*
* Tcl_GetStringResult --
*
* Returns an interpreter's result value as a string.
*
* Results:
* The interpreter's result as a string.
*
* Side effects:
* If the string result is empty, the object result is moved to the
* string result, then the object result is reset.
*
*----------------------------------------------------------------------
*/
char *
Tcl_GetStringResult(interp)
Tcl_Interp *interp; /* Interpreter whose result to return. */
{
/*
* If the string result is empty, move the object result to the
* string result, then reset the object result.
* FAILS IF OBJECT RESULT'S STRING REPRESENTATION CONTAINS NULLS.
*/
if (*(interp->result) == 0) {
Tcl_SetResult(interp,
TclGetStringFromObj(Tcl_GetObjResult(interp), (int *) NULL),
TCL_VOLATILE);
}
return interp->result;
}
/*
*----------------------------------------------------------------------
*
* Tcl_SetObjResult --
*
* Arrange for objPtr to be an interpreter's result value.
*
* Results:
* None.
*
* Side effects:
* interp->objResultPtr is left pointing to the object referenced
* by objPtr. The object's reference count is incremented since
* there is now a new reference to it. The reference count for any
* old objResultPtr value is decremented. Also, the string result
* is reset.
*
*----------------------------------------------------------------------
*/
void
Tcl_SetObjResult(interp, objPtr)
Tcl_Interp *interp; /* Interpreter with which to associate the
* return object value. */
Tcl_Obj *objPtr; /* Tcl object to be returned. If NULL, the
* obj result is made an empty string
* object. */
{
Interp *iPtr = (Interp *) interp;
Tcl_Obj *oldObjResult = iPtr->objResultPtr;
iPtr->objResultPtr = objPtr;
Tcl_IncrRefCount(objPtr); /* since interp result is a reference */
/*
* We wait until the end to release the old object result, in case
* we are setting the result to itself.
*/
TclDecrRefCount(oldObjResult);
/*
* Reset the string result since we just set the result object.
*/
if (iPtr->freeProc != NULL) {
if ((iPtr->freeProc == TCL_DYNAMIC)
|| (iPtr->freeProc == (Tcl_FreeProc *) free)) {
ckfree(iPtr->result);
} else {
(*iPtr->freeProc)(iPtr->result);
}
iPtr->freeProc = 0;
}
iPtr->result = iPtr->resultSpace;
iPtr->resultSpace[0] = 0;
}
/*
*----------------------------------------------------------------------
*
* Tcl_GetObjResult --
*
* Returns an interpreter's result value as a Tcl object. The object's
* reference count is not modified; the caller must do that if it
* needs to hold on to a long-term reference to it.
*
* Results:
* The interpreter's result as an object.
*
* Side effects:
* If the interpreter has a non-empty string result, the result object
* is either empty or stale because some procedure set interp->result
* directly. If so, the string result is moved to the result object
* then the string result is reset.
*
*----------------------------------------------------------------------
*/
Tcl_Obj *
Tcl_GetObjResult(interp)
Tcl_Interp *interp; /* Interpreter whose result to return. */
{
Interp *iPtr = (Interp *) interp;
Tcl_Obj *objResultPtr;
int length;
/*
* If the string result is non-empty, move the string result to the
* object result, then reset the string result.
*/
if (*(iPtr->result) != 0) {
TclResetObjResult(iPtr);
objResultPtr = iPtr->objResultPtr;
length = strlen(iPtr->result);
TclInitStringRep(objResultPtr, iPtr->result, length);
if (iPtr->freeProc != NULL) {
if ((iPtr->freeProc == TCL_DYNAMIC)
|| (iPtr->freeProc == (Tcl_FreeProc *) free)) {
ckfree(iPtr->result);
} else {
(*iPtr->freeProc)(iPtr->result);
}
iPtr->freeProc = 0;
}
iPtr->result = iPtr->resultSpace;
iPtr->resultSpace[0] = 0;
}
return iPtr->objResultPtr;
}
/*
*----------------------------------------------------------------------
*
* Tcl_AppendResult --
*
* Append a variable number of strings onto the interpreter's string
* result.
*
* Results:
* None.
*
* Side effects:
* The result of the interpreter given by the first argument is
* extended by the strings given by the second and following arguments
* (up to a terminating NULL argument).
*
* If the string result is empty, the object result is moved to the
* string result, then the object result is reset.
*
*----------------------------------------------------------------------
*/
void
Tcl_AppendResult TCL_VARARGS_DEF(Tcl_Interp *,arg1)
{
va_list argList;
Interp *iPtr;
char *string;
int newSpace;
/*
* If the string result is empty, move the object result to the
* string result, then reset the object result.
* FAILS IF OBJECT RESULT'S STRING REPRESENTATION CONTAINS NULLS.
*/
iPtr = (Interp *) TCL_VARARGS_START(Tcl_Interp *,arg1,argList);
if (*(iPtr->result) == 0) {
Tcl_SetResult((Tcl_Interp *) iPtr,
TclGetStringFromObj(Tcl_GetObjResult((Tcl_Interp *) iPtr),
(int *) NULL),
TCL_VOLATILE);
}
/*
* Scan through all the arguments to see how much space is needed.
*/
newSpace = 0;
while (1) {
string = va_arg(argList, char *);
if (string == NULL) {
break;
}
newSpace += strlen(string);
}
va_end(argList);
/*
* If the append buffer isn't already setup and large enough to hold
* the new data, set it up.
*/
if ((iPtr->result != iPtr->appendResult)
|| (iPtr->appendResult[iPtr->appendUsed] != 0)
|| ((newSpace + iPtr->appendUsed) >= iPtr->appendAvl)) {
SetupAppendBuffer(iPtr, newSpace);
}
/*
* Now go through all the argument strings again, copying them into the
* buffer.
*/
TCL_VARARGS_START(Tcl_Interp *,arg1,argList);
while (1) {
string = va_arg(argList, char *);
if (string == NULL) {
break;
}
strcpy(iPtr->appendResult + iPtr->appendUsed, string);
iPtr->appendUsed += strlen(string);
}
va_end(argList);
}
/*
*----------------------------------------------------------------------
*
* Tcl_AppendElement --
*
* Convert a string to a valid Tcl list element and append it to the
* result (which is ostensibly a list).
*
* Results:
* None.
*
* Side effects:
* The result in the interpreter given by the first argument is
* extended with a list element converted from string. A separator
* space is added before the converted list element unless the current
* result is empty, contains the single character "{", or ends in " {".
*
* If the string result is empty, the object result is moved to the
* string result, then the object result is reset.
*
*----------------------------------------------------------------------
*/
void
Tcl_AppendElement(interp, string)
Tcl_Interp *interp; /* Interpreter whose result is to be
* extended. */
char *string; /* String to convert to list element and
* add to result. */
{
Interp *iPtr = (Interp *) interp;
char *dst;
int size;
int flags;
/*
* If the string result is empty, move the object result to the
* string result, then reset the object result.
* FAILS IF OBJECT RESULT'S STRING REPRESENTATION CONTAINS NULLS.
*/
if (*(iPtr->result) == 0) {
Tcl_SetResult(interp,
TclGetStringFromObj(Tcl_GetObjResult(interp), (int *) NULL),
TCL_VOLATILE);
}
/*
* See how much space is needed, and grow the append buffer if
* needed to accommodate the list element.
*/
size = Tcl_ScanElement(string, &flags) + 1;
if ((iPtr->result != iPtr->appendResult)
|| (iPtr->appendResult[iPtr->appendUsed] != 0)
|| ((size + iPtr->appendUsed) >= iPtr->appendAvl)) {
SetupAppendBuffer(iPtr, size+iPtr->appendUsed);
}
/*
* Convert the string into a list element and copy it to the
* buffer that's forming, with a space separator if needed.
*/
dst = iPtr->appendResult + iPtr->appendUsed;
if (TclNeedSpace(iPtr->appendResult, dst)) {
iPtr->appendUsed++;
*dst = ' ';
dst++;
}
iPtr->appendUsed += Tcl_ConvertElement(string, dst, flags);
}
/*
*----------------------------------------------------------------------
*
* SetupAppendBuffer --
*
* This procedure makes sure that there is an append buffer properly
* initialized, if necessary, from the interpreter's result, and
* that it has at least enough room to accommodate newSpace new
* bytes of information.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
SetupAppendBuffer(iPtr, newSpace)
Interp *iPtr; /* Interpreter whose result is being set up. */
int newSpace; /* Make sure that at least this many bytes
* of new information may be added. */
{
int totalSpace;
/*
* Make the append buffer larger, if that's necessary, then copy the
* result into the append buffer and make the append buffer the official
* Tcl result.
*/
if (iPtr->result != iPtr->appendResult) {
/*
* If an oversized buffer was used recently, then free it up
* so we go back to a smaller buffer. This avoids tying up
* memory forever after a large operation.
*/
if (iPtr->appendAvl > 500) {
ckfree(iPtr->appendResult);
iPtr->appendResult = NULL;
iPtr->appendAvl = 0;
}
iPtr->appendUsed = strlen(iPtr->result);
} else if (iPtr->result[iPtr->appendUsed] != 0) {
/*
* Most likely someone has modified a result created by
* Tcl_AppendResult et al. so that it has a different size.
* Just recompute the size.
*/
iPtr->appendUsed = strlen(iPtr->result);
}
totalSpace = newSpace + iPtr->appendUsed;
if (totalSpace >= iPtr->appendAvl) {
char *new;
if (totalSpace < 100) {
totalSpace = 200;
} else {
totalSpace *= 2;
}
new = (char *) ckalloc((unsigned) totalSpace);
strcpy(new, iPtr->result);
if (iPtr->appendResult != NULL) {
ckfree(iPtr->appendResult);
}
iPtr->appendResult = new;
iPtr->appendAvl = totalSpace;
} else if (iPtr->result != iPtr->appendResult) {
strcpy(iPtr->appendResult, iPtr->result);
}
Tcl_FreeResult((Tcl_Interp *) iPtr);
iPtr->result = iPtr->appendResult;
}
/*
*----------------------------------------------------------------------
*
* Tcl_FreeResult --
*
* This procedure frees up the memory associated with an interpreter's
* string result. It also resets the interpreter's result object.
* Tcl_FreeResult is most commonly used when a procedure is about to
* replace one result value with another.
*
* Results:
* None.
*
* Side effects:
* Frees the memory associated with interp's string result and sets
* interp->freeProc to zero, but does not change interp->result or
* clear error state. Resets interp's result object to an unshared
* empty object.
*
*----------------------------------------------------------------------
*/
void
Tcl_FreeResult(interp)
Tcl_Interp *interp; /* Interpreter for which to free result. */
{
Interp *iPtr = (Interp *) interp;
if (iPtr->freeProc != NULL) {
if ((iPtr->freeProc == TCL_DYNAMIC)
|| (iPtr->freeProc == (Tcl_FreeProc *) free)) {
ckfree(iPtr->result);
} else {
(*iPtr->freeProc)(iPtr->result);
}
iPtr->freeProc = 0;
}
TclResetObjResult(iPtr);
}
/*
*----------------------------------------------------------------------
*
* Tcl_ResetResult --
*
* This procedure resets both the interpreter's string and object
* results.
*
* Results:
* None.
*
* Side effects:
* It resets the result object to an unshared empty object. It
* then restores the interpreter's string result area to its default
* initialized state, freeing up any memory that may have been
* allocated. It also clears any error information for the interpreter.
*
*----------------------------------------------------------------------
*/
void
Tcl_ResetResult(interp)
Tcl_Interp *interp; /* Interpreter for which to clear result. */
{
Interp *iPtr = (Interp *) interp;
TclResetObjResult(iPtr);
Tcl_FreeResult(interp);
iPtr->result = iPtr->resultSpace;
iPtr->resultSpace[0] = 0;
iPtr->flags &= ~(ERR_ALREADY_LOGGED | ERR_IN_PROGRESS | ERROR_CODE_SET);
}
/*
*----------------------------------------------------------------------
*
* Tcl_SetErrorCode --
*
* This procedure is called to record machine-readable information
* about an error that is about to be returned.
*
* Results:
* None.
*
* Side effects:
* The errorCode global variable is modified to hold all of the
* arguments to this procedure, in a list form with each argument
* becoming one element of the list. A flag is set internally
* to remember that errorCode has been set, so the variable doesn't
* get set automatically when the error is returned.
*
*----------------------------------------------------------------------
*/
/* VARARGS2 */
void
Tcl_SetErrorCode TCL_VARARGS_DEF(Tcl_Interp *,arg1)
{
va_list argList;
char *string;
int flags;
Interp *iPtr;
/*
* Scan through the arguments one at a time, appending them to
* $errorCode as list elements.
*/
iPtr = (Interp *) TCL_VARARGS_START(Tcl_Interp *,arg1,argList);
flags = TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT;
while (1) {
string = va_arg(argList, char *);
if (string == NULL) {
break;
}
(void) Tcl_SetVar2((Tcl_Interp *) iPtr, "errorCode",
(char *) NULL, string, flags);
flags |= TCL_APPEND_VALUE;
}
va_end(argList);
iPtr->flags |= ERROR_CODE_SET;
}
/*
*----------------------------------------------------------------------
*
* Tcl_SetObjErrorCode --
*
* This procedure is called to record machine-readable information
* about an error that is about to be returned. The caller should
* build a list object up and pass it to this routine.
*
* Results:
* None.
*
* Side effects:
* The errorCode global variable is modified to be the new value.
* A flag is set internally to remember that errorCode has been
* set, so the variable doesn't get set automatically when the
* error is returned.
*
*----------------------------------------------------------------------
*/
void
Tcl_SetObjErrorCode(interp, errorObjPtr)
Tcl_Interp *interp;
Tcl_Obj *errorObjPtr;
{
Tcl_Obj *namePtr;
Interp *iPtr;
namePtr = Tcl_NewStringObj("errorCode", -1);
iPtr = (Interp *) interp;
Tcl_ObjSetVar2(interp, namePtr, (Tcl_Obj *) NULL, errorObjPtr,
TCL_GLOBAL_ONLY);
iPtr->flags |= ERROR_CODE_SET;
Tcl_DecrRefCount(namePtr);
}
/*
*----------------------------------------------------------------------
*
* Tcl_RegExpCompile --
*
* Compile a regular expression into a form suitable for fast
* matching. This procedure retains a small cache of pre-compiled
* regular expressions in the interpreter, in order to avoid
* compilation costs as much as possible.
*
* Results:
* The return value is a pointer to the compiled form of string,
* suitable for passing to Tcl_RegExpExec. This compiled form
* is only valid up until the next call to this procedure, so
* don't keep these around for a long time! If an error occurred
* while compiling the pattern, then NULL is returned and an error
* message is left in interp->result.
*
* Side effects:
* The cache of compiled regexp's in interp will be modified to
* hold information for string, if such information isn't already
* present in the cache.
*
*----------------------------------------------------------------------
*/
Tcl_RegExp
Tcl_RegExpCompile(interp, string)
Tcl_Interp *interp; /* For use in error reporting. */
char *string; /* String for which to produce
* compiled regular expression. */
{
Interp *iPtr = (Interp *) interp;
int i, length;
regexp *result;
length = strlen(string);
for (i = 0; i < NUM_REGEXPS; i++) {
if ((length == iPtr->patLengths[i])
&& (strcmp(string, iPtr->patterns[i]) == 0)) {
/*
* Move the matched pattern to the first slot in the
* cache and shift the other patterns down one position.
*/
if (i != 0) {
int j;
char *cachedString;
cachedString = iPtr->patterns[i];
result = iPtr->regexps[i];
for (j = i-1; j >= 0; j--) {
iPtr->patterns[j+1] = iPtr->patterns[j];
iPtr->patLengths[j+1] = iPtr->patLengths[j];
iPtr->regexps[j+1] = iPtr->regexps[j];
}
iPtr->patterns[0] = cachedString;
iPtr->patLengths[0] = length;
iPtr->regexps[0] = result;
}
return (Tcl_RegExp) iPtr->regexps[0];
}
}
/*
* No match in the cache. Compile the string and add it to the
* cache.
*/
TclRegError((char *) NULL);
result = TclRegComp(string);
if (TclGetRegError() != NULL) {
Tcl_AppendResult(interp,
"couldn't compile regular expression pattern: ",
TclGetRegError(), (char *) NULL);
return NULL;
}
if (iPtr->patterns[NUM_REGEXPS-1] != NULL) {
ckfree(iPtr->patterns[NUM_REGEXPS-1]);
ckfree((char *) iPtr->regexps[NUM_REGEXPS-1]);
}
for (i = NUM_REGEXPS - 2; i >= 0; i--) {
iPtr->patterns[i+1] = iPtr->patterns[i];
iPtr->patLengths[i+1] = iPtr->patLengths[i];
iPtr->regexps[i+1] = iPtr->regexps[i];
}
iPtr->patterns[0] = (char *) ckalloc((unsigned) (length+1));
strcpy(iPtr->patterns[0], string);
iPtr->patLengths[0] = length;
iPtr->regexps[0] = result;
return (Tcl_RegExp) result;
}
/*
*----------------------------------------------------------------------
*
* Tcl_RegExpExec --
*
* Execute the regular expression matcher using a compiled form
* of a regular expression and save information about any match
* that is found.
*
* Results:
* If an error occurs during the matching operation then -1
* is returned and interp->result contains an error message.
* Otherwise the return value is 1 if a matching range is
* found and 0 if there is no matching range.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
Tcl_RegExpExec(interp, re, string, start)
Tcl_Interp *interp; /* Interpreter to use for error reporting. */
Tcl_RegExp re; /* Compiled regular expression; must have
* been returned by previous call to
* Tcl_RegExpCompile. */
char *string; /* String against which to match re. */
char *start; /* If string is part of a larger string,
* this identifies beginning of larger
* string, so that "^" won't match. */
{
int match;
regexp *regexpPtr = (regexp *) re;
TclRegError((char *) NULL);
match = TclRegExec(regexpPtr, string, start);
if (TclGetRegError() != NULL) {
Tcl_ResetResult(interp);
Tcl_AppendResult(interp, "error while matching regular expression: ",
TclGetRegError(), (char *) NULL);
return -1;
}
return match;
}
/*
*----------------------------------------------------------------------
*
* Tcl_RegExpRange --
*
* Returns pointers describing the range of a regular expression match,
* or one of the subranges within the match.
*
* Results:
* The variables at *startPtr and *endPtr are modified to hold the
* addresses of the endpoints of the range given by index. If the
* specified range doesn't exist then NULLs are returned.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
void
Tcl_RegExpRange(re, index, startPtr, endPtr)
Tcl_RegExp re; /* Compiled regular expression that has
* been passed to Tcl_RegExpExec. */
int index; /* 0 means give the range of the entire
* match, > 0 means give the range of
* a matching subrange. Must be no greater
* than NSUBEXP. */
char **startPtr; /* Store address of first character in
* (sub-) range here. */
char **endPtr; /* Store address of character just after last
* in (sub-) range here. */
{
regexp *regexpPtr = (regexp *) re;
if (index >= NSUBEXP) {
*startPtr = *endPtr = NULL;
} else {
*startPtr = regexpPtr->startp[index];
*endPtr = regexpPtr->endp[index];
}
}
/*
*----------------------------------------------------------------------
*
* Tcl_RegExpMatch --
*
* See if a string matches a regular expression.
*
* Results:
* If an error occurs during the matching operation then -1
* is returned and interp->result contains an error message.
* Otherwise the return value is 1 if "string" matches "pattern"
* and 0 otherwise.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
Tcl_RegExpMatch(interp, string, pattern)
Tcl_Interp *interp; /* Used for error reporting. */
char *string; /* String. */
char *pattern; /* Regular expression to match against
* string. */
{
Tcl_RegExp re;
re = Tcl_RegExpCompile(interp, pattern);
if (re == NULL) {
return -1;
}
return Tcl_RegExpExec(interp, re, string, string);
}
/*
*----------------------------------------------------------------------
*
* Tcl_DStringInit --
*
* Initializes a dynamic string, discarding any previous contents
* of the string (Tcl_DStringFree should have been called already
* if the dynamic string was previously in use).
*
* Results:
* None.
*
* Side effects:
* The dynamic string is initialized to be empty.
*
*----------------------------------------------------------------------
*/
void
Tcl_DStringInit(dsPtr)
Tcl_DString *dsPtr; /* Pointer to structure for dynamic string. */
{
dsPtr->string = dsPtr->staticSpace;
dsPtr->length = 0;
dsPtr->spaceAvl = TCL_DSTRING_STATIC_SIZE;
dsPtr->staticSpace[0] = 0;
}
/*
*----------------------------------------------------------------------
*
* Tcl_DStringAppend --
*
* Append more characters to the current value of a dynamic string.
*
* Results:
* The return value is a pointer to the dynamic string's new value.
*
* Side effects:
* Length bytes from string (or all of string if length is less
* than zero) are added to the current value of the string. Memory
* gets reallocated if needed to accomodate the string's new size.
*
*----------------------------------------------------------------------
*/
char *
Tcl_DStringAppend(dsPtr, string, length)
Tcl_DString *dsPtr; /* Structure describing dynamic string. */
CONST char *string; /* String to append. If length is -1 then
* this must be null-terminated. */
int length; /* Number of characters from string to
* append. If < 0, then append all of string,
* up to null at end. */
{
int newSize;
char *newString, *dst;
CONST char *end;
if (length < 0) {
length = strlen(string);
}
newSize = length + dsPtr->length;
/*
* Allocate a larger buffer for the string if the current one isn't
* large enough. Allocate extra space in the new buffer so that there
* will be room to grow before we have to allocate again.
*/
if (newSize >= dsPtr->spaceAvl) {
dsPtr->spaceAvl = newSize*2;
newString = (char *) ckalloc((unsigned) dsPtr->spaceAvl);
memcpy((VOID *) newString, (VOID *) dsPtr->string,
(size_t) dsPtr->length);
if (dsPtr->string != dsPtr->staticSpace) {
ckfree(dsPtr->string);
}
dsPtr->string = newString;
}
/*
* Copy the new string into the buffer at the end of the old
* one.
*/
for (dst = dsPtr->string + dsPtr->length, end = string+length;
string < end; string++, dst++) {
*dst = *string;
}
*dst = '\0';
dsPtr->length += length;
return dsPtr->string;
}
/*
*----------------------------------------------------------------------
*
* Tcl_DStringAppendElement --
*
* Append a list element to the current value of a dynamic string.
*
* Results:
* The return value is a pointer to the dynamic string's new value.
*
* Side effects:
* String is reformatted as a list element and added to the current
* value of the string. Memory gets reallocated if needed to
* accomodate the string's new size.
*
*----------------------------------------------------------------------
*/
char *
Tcl_DStringAppendElement(dsPtr, string)
Tcl_DString *dsPtr; /* Structure describing dynamic string. */
CONST char *string; /* String to append. Must be
* null-terminated. */
{
int newSize, flags;
char *dst, *newString;
newSize = Tcl_ScanElement(string, &flags) + dsPtr->length + 1;
/*
* Allocate a larger buffer for the string if the current one isn't
* large enough. Allocate extra space in the new buffer so that there
* will be room to grow before we have to allocate again.
* SPECIAL NOTE: must use memcpy, not strcpy, to copy the string
* to a larger buffer, since there may be embedded NULLs in the
* string in some cases.
*/
if (newSize >= dsPtr->spaceAvl) {
dsPtr->spaceAvl = newSize*2;
newString = (char *) ckalloc((unsigned) dsPtr->spaceAvl);
memcpy((VOID *) newString, (VOID *) dsPtr->string,
(size_t) dsPtr->length);
if (dsPtr->string != dsPtr->staticSpace) {
ckfree(dsPtr->string);
}
dsPtr->string = newString;
}
/*
* Convert the new string to a list element and copy it into the
* buffer at the end, with a space, if needed.
*/
dst = dsPtr->string + dsPtr->length;
if (TclNeedSpace(dsPtr->string, dst)) {
*dst = ' ';
dst++;
dsPtr->length++;
}
dsPtr->length += Tcl_ConvertElement(string, dst, flags);
return dsPtr->string;
}
/*
*----------------------------------------------------------------------
*
* Tcl_DStringSetLength --
*
* Change the length of a dynamic string. This can cause the
* string to either grow or shrink, depending on the value of
* length.
*
* Results:
* None.
*
* Side effects:
* The length of dsPtr is changed to length and a null byte is
* stored at that position in the string. If length is larger
* than the space allocated for dsPtr, then a panic occurs.
*
*----------------------------------------------------------------------
*/
void
Tcl_DStringSetLength(dsPtr, length)
Tcl_DString *dsPtr; /* Structure describing dynamic string. */
int length; /* New length for dynamic string. */
{
if (length < 0) {
length = 0;
}
if (length >= dsPtr->spaceAvl) {
char *newString;
dsPtr->spaceAvl = length+1;
newString = (char *) ckalloc((unsigned) dsPtr->spaceAvl);
/*
* SPECIAL NOTE: must use memcpy, not strcpy, to copy the string
* to a larger buffer, since there may be embedded NULLs in the
* string in some cases.
*/
memcpy((VOID *) newString, (VOID *) dsPtr->string,
(size_t) dsPtr->length);
if (dsPtr->string != dsPtr->staticSpace) {
ckfree(dsPtr->string);
}
dsPtr->string = newString;
}
dsPtr->length = length;
dsPtr->string[length] = 0;
}
/*
*----------------------------------------------------------------------
*
* Tcl_DStringFree --
*
* Frees up any memory allocated for the dynamic string and
* reinitializes the string to an empty state.
*
* Results:
* None.
*
* Side effects:
* The previous contents of the dynamic string are lost, and
* the new value is an empty string.
*
*----------------------------------------------------------------------
*/
void
Tcl_DStringFree(dsPtr)
Tcl_DString *dsPtr; /* Structure describing dynamic string. */
{
if (dsPtr->string != dsPtr->staticSpace) {
ckfree(dsPtr->string);
}
dsPtr->string = dsPtr->staticSpace;
dsPtr->length = 0;
dsPtr->spaceAvl = TCL_DSTRING_STATIC_SIZE;
dsPtr->staticSpace[0] = 0;
}
/*
*----------------------------------------------------------------------
*
* Tcl_DStringResult --
*
* This procedure moves the value of a dynamic string into an
* interpreter as its string result. Afterwards, the dynamic string
* is reset to an empty string.
*
* Results:
* None.
*
* Side effects:
* The string is "moved" to interp's result, and any existing
* string result for interp is freed. dsPtr is reinitialized to
* an empty string.
*
*----------------------------------------------------------------------
*/
void
Tcl_DStringResult(interp, dsPtr)
Tcl_Interp *interp; /* Interpreter whose result is to be reset. */
Tcl_DString *dsPtr; /* Dynamic string that is to become the
* result of interp. */
{
Tcl_ResetResult(interp);
if (dsPtr->string != dsPtr->staticSpace) {
interp->result = dsPtr->string;
interp->freeProc = TCL_DYNAMIC;
} else if (dsPtr->length < TCL_RESULT_SIZE) {
interp->result = ((Interp *) interp)->resultSpace;
strcpy(interp->result, dsPtr->string);
} else {
Tcl_SetResult(interp, dsPtr->string, TCL_VOLATILE);
}
dsPtr->string = dsPtr->staticSpace;
dsPtr->length = 0;
dsPtr->spaceAvl = TCL_DSTRING_STATIC_SIZE;
dsPtr->staticSpace[0] = 0;
}
/*
*----------------------------------------------------------------------
*
* Tcl_DStringGetResult --
*
* This procedure moves an interpreter's result into a dynamic string.
*
* Results:
* None.
*
* Side effects:
* The interpreter's string result is cleared, and the previous
* contents of dsPtr are freed.
*
* If the string result is empty, the object result is moved to the
* string result, then the object result is reset.
*
*----------------------------------------------------------------------
*/
void
Tcl_DStringGetResult(interp, dsPtr)
Tcl_Interp *interp; /* Interpreter whose result is to be reset. */
Tcl_DString *dsPtr; /* Dynamic string that is to become the
* result of interp. */
{
Interp *iPtr = (Interp *) interp;
if (dsPtr->string != dsPtr->staticSpace) {
ckfree(dsPtr->string);
}
/*
* If the string result is empty, move the object result to the
* string result, then reset the object result.
* FAILS IF OBJECT RESULT'S STRING REPRESENTATION CONTAINS NULLS.
*/
if (*(iPtr->result) == 0) {
Tcl_SetResult(interp,
TclGetStringFromObj(Tcl_GetObjResult(interp), (int *) NULL),
TCL_VOLATILE);
}
dsPtr->length = strlen(iPtr->result);
if (iPtr->freeProc != NULL) {
if ((iPtr->freeProc == TCL_DYNAMIC)
|| (iPtr->freeProc == (Tcl_FreeProc *) free)) {
dsPtr->string = iPtr->result;
dsPtr->spaceAvl = dsPtr->length+1;
} else {
dsPtr->string = (char *) ckalloc((unsigned) (dsPtr->length+1));
strcpy(dsPtr->string, iPtr->result);
(*iPtr->freeProc)(iPtr->result);
}
dsPtr->spaceAvl = dsPtr->length+1;
iPtr->freeProc = NULL;
} else {
if (dsPtr->length < TCL_DSTRING_STATIC_SIZE) {
dsPtr->string = dsPtr->staticSpace;
dsPtr->spaceAvl = TCL_DSTRING_STATIC_SIZE;
} else {
dsPtr->string = (char *) ckalloc((unsigned) (dsPtr->length + 1));
dsPtr->spaceAvl = dsPtr->length + 1;
}
strcpy(dsPtr->string, iPtr->result);
}
iPtr->result = iPtr->resultSpace;
iPtr->resultSpace[0] = 0;
}
/*
*----------------------------------------------------------------------
*
* Tcl_DStringStartSublist --
*
* This procedure adds the necessary information to a dynamic
* string (e.g. " {" to start a sublist. Future element
* appends will be in the sublist rather than the main list.
*
* Results:
* None.
*
* Side effects:
* Characters get added to the dynamic string.
*
*----------------------------------------------------------------------
*/
void
Tcl_DStringStartSublist(dsPtr)
Tcl_DString *dsPtr; /* Dynamic string. */
{
if (TclNeedSpace(dsPtr->string, dsPtr->string + dsPtr->length)) {
Tcl_DStringAppend(dsPtr, " {", -1);
} else {
Tcl_DStringAppend(dsPtr, "{", -1);
}
}
/*
*----------------------------------------------------------------------
*
* Tcl_DStringEndSublist --
*
* This procedure adds the necessary characters to a dynamic
* string to end a sublist (e.g. "}"). Future element appends
* will be in the enclosing (sub)list rather than the current
* sublist.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
void
Tcl_DStringEndSublist(dsPtr)
Tcl_DString *dsPtr; /* Dynamic string. */
{
Tcl_DStringAppend(dsPtr, "}", -1);
}
/*
*----------------------------------------------------------------------
*
* Tcl_PrintDouble --
*
* Given a floating-point value, this procedure converts it to
* an ASCII string using.
*
* Results:
* The ASCII equivalent of "value" is written at "dst". It is
* written using the current precision, and it is guaranteed to
* contain a decimal point or exponent, so that it looks like
* a floating-point value and not an integer.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
void
Tcl_PrintDouble(interp, value, dst)
Tcl_Interp *interp; /* Interpreter whose tcl_precision
* variable used to be used to control
* printing. It's ignored now. */
double value; /* Value to print as string. */
char *dst; /* Where to store converted value;
* must have at least TCL_DOUBLE_SPACE
* characters. */
{
char *p;
sprintf(dst, precisionFormat, value);
/*
* If the ASCII result looks like an integer, add ".0" so that it
* doesn't look like an integer anymore. This prevents floating-point
* values from being converted to integers unintentionally.
*/
for (p = dst; *p != 0; p++) {
if ((*p == '.') || (isalpha(UCHAR(*p)))) {
return;
}
}
p[0] = '.';
p[1] = '0';
p[2] = 0;
}
/*
*----------------------------------------------------------------------
*
* TclPrecTraceProc --
*
* This procedure is invoked whenever the variable "tcl_precision"
* is written.
*
* Results:
* Returns NULL if all went well, or an error message if the
* new value for the variable doesn't make sense.
*
* Side effects:
* If the new value doesn't make sense then this procedure
* undoes the effect of the variable modification. Otherwise
* it modifies the format string that's used by Tcl_PrintDouble.
*
*----------------------------------------------------------------------
*/
/* ARGSUSED */
char *
TclPrecTraceProc(clientData, interp, name1, name2, flags)
ClientData clientData; /* Not used. */
Tcl_Interp *interp; /* Interpreter containing variable. */
char *name1; /* Name of variable. */
char *name2; /* Second part of variable name. */
int flags; /* Information about what happened. */
{
char *value, *end;
int prec;
/*
* If the variable is unset, then recreate the trace.
*/
if (flags & TCL_TRACE_UNSETS) {
if ((flags & TCL_TRACE_DESTROYED) && !(flags & TCL_INTERP_DESTROYED)) {
Tcl_TraceVar2(interp, name1, name2,
TCL_GLOBAL_ONLY|TCL_TRACE_READS|TCL_TRACE_WRITES
|TCL_TRACE_UNSETS, TclPrecTraceProc, clientData);
}
return (char *) NULL;
}
/*
* When the variable is read, reset its value from our shared
* value. This is needed in case the variable was modified in
* some other interpreter so that this interpreter's value is
* out of date.
*/
if (flags & TCL_TRACE_READS) {
Tcl_SetVar2(interp, name1, name2, precisionString,
flags & TCL_GLOBAL_ONLY);
return (char *) NULL;
}
/*
* The variable is being written. Check the new value and disallow
* it if it isn't reasonable or if this is a safe interpreter (we
* don't want safe interpreters messing up the precision of other
* interpreters).
*/
if (Tcl_IsSafe(interp)) {
Tcl_SetVar2(interp, name1, name2, precisionString,
flags & TCL_GLOBAL_ONLY);
return "can't modify precision from a safe interpreter";
}
value = Tcl_GetVar2(interp, name1, name2, flags & TCL_GLOBAL_ONLY);
if (value == NULL) {
value = "";
}
prec = strtoul(value, &end, 10);
if ((prec <= 0) || (prec > TCL_MAX_PREC) || (prec > 100) ||
(end == value) || (*end != 0)) {
Tcl_SetVar2(interp, name1, name2, precisionString,
flags & TCL_GLOBAL_ONLY);
return "improper value for precision";
}
TclFormatInt(precisionString, prec);
sprintf(precisionFormat, "%%.%dg", prec);
return (char *) NULL;
}
/*
*----------------------------------------------------------------------
*
* TclNeedSpace --
*
* This procedure checks to see whether it is appropriate to
* add a space before appending a new list element to an
* existing string.
*
* Results:
* The return value is 1 if a space is appropriate, 0 otherwise.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
TclNeedSpace(start, end)
char *start; /* First character in string. */
char *end; /* End of string (place where space will
* be added, if appropriate). */
{
/*
* A space is needed unless either
* (a) we're at the start of the string, or
* (b) the trailing characters of the string consist of one or more
* open curly braces preceded by a space or extending back to
* the beginning of the string.
* (c) the trailing characters of the string consist of a space
* preceded by a character other than backslash.
*/
if (end == start) {
return 0;
}
end--;
if (*end != '{') {
if (isspace(UCHAR(*end)) && ((end == start) || (end[-1] != '\\'))) {
return 0;
}
return 1;
}
do {
if (end == start) {
return 0;
}
end--;
} while (*end == '{');
if (isspace(UCHAR(*end))) {
return 0;
}
return 1;
}
/*
*----------------------------------------------------------------------
*
* TclFormatInt --
*
* This procedure formats an integer into a sequence of decimal digit
* characters in a buffer. If the integer is negative, a minus sign is
* inserted at the start of the buffer. A null character is inserted at
* the end of the formatted characters. It is the caller's
* responsibility to ensure that enough storage is available. This
* procedure has the effect of sprintf(buffer, "%d", n) but is faster.
*
* Results:
* An integer representing the number of characters formatted, not
* including the terminating \0.
*
* Side effects:
* The formatted characters are written into the storage pointer to
* by the "buffer" argument.
*
*----------------------------------------------------------------------
*/
int
TclFormatInt(buffer, n)
char *buffer; /* Points to the storage into which the
* formatted characters are written. */
long n; /* The integer to format. */
{
long intVal;
int i;
int numFormatted, j;
char *digits = "0123456789";
/*
* Check first whether "n" is the maximum negative value. This is
* -2^(m-1) for an m-bit word, and has no positive equivalent;
* negating it produces the same value.
*/
if (n == -n) {
sprintf(buffer, "%ld", n);
return strlen(buffer);
}
/*
* Generate the characters of the result backwards in the buffer.
*/
intVal = (n < 0? -n : n);
i = 0;
buffer[0] = '\0';
do {
i++;
buffer[i] = digits[intVal % 10];
intVal = intVal/10;
} while (intVal > 0);
if (n < 0) {
i++;
buffer[i] = '-';
}
numFormatted = i;
/*
* Now reverse the characters.
*/
for (j = 0; j < i; j++, i--) {
char tmp = buffer[i];
buffer[i] = buffer[j];
buffer[j] = tmp;
}
return numFormatted;
}
/*
*----------------------------------------------------------------------
*
* TclLooksLikeInt --
*
* This procedure decides whether the leading characters of a
* string look like an integer or something else (such as a
* floating-point number or string).
*
* Results:
* The return value is 1 if the leading characters of p look
* like a valid Tcl integer. If they look like a floating-point
* number (e.g. "e01" or "2.4"), or if they don't look like a
* number at all, then 0 is returned.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
TclLooksLikeInt(p)
char *p; /* Pointer to string. */
{
while (isspace(UCHAR(*p))) {
p++;
}
if ((*p == '+') || (*p == '-')) {
p++;
}
if (!isdigit(UCHAR(*p))) {
return 0;
}
p++;
while (isdigit(UCHAR(*p))) {
p++;
}
if ((*p != '.') && (*p != 'e') && (*p != 'E')) {
return 1;
}
return 0;
}
/*
*----------------------------------------------------------------------
*
* TclGetIntForIndex --
*
* This procedure returns an integer corresponding to the list index
* held in a Tcl object. The Tcl object's value is expected to be
* either an integer or the string "end".
*
* Results:
* The return value is normally TCL_OK, which means that the index was
* successfully stored into the location referenced by "indexPtr". If
* the Tcl object referenced by "objPtr" has the value "end", the
* value stored is "endValue". If "objPtr"s values is not "end" and
* can not be converted to an integer, TCL_ERROR is returned and, if
* "interp" is non-NULL, an error message is left in the interpreter's
* result object.
*
* Side effects:
* The object referenced by "objPtr" might be converted to an
* integer object.
*
*----------------------------------------------------------------------
*/
int
TclGetIntForIndex(interp, objPtr, endValue, indexPtr)
Tcl_Interp *interp; /* Interpreter to use for error reporting.
* If NULL, then no error message is left
* after errors. */
Tcl_Obj *objPtr; /* Points to an object containing either
* "end" or an integer. */
int endValue; /* The value to be stored at "indexPtr" if
* "objPtr" holds "end". */
int *indexPtr; /* Location filled in with an integer
* representing an index. */
{
Interp *iPtr = (Interp *) interp;
char *bytes;
int index, length, result;
/*
* THIS FAILS IF THE INDEX OBJECT'S STRING REP CONTAINS NULLS.
*/
if (objPtr->typePtr == &tclIntType) {
*indexPtr = (int)objPtr->internalRep.longValue;
return TCL_OK;
}
bytes = TclGetStringFromObj(objPtr, &length);
if ((*bytes == 'e')
&& (strncmp(bytes, "end", (unsigned) length) == 0)) {
index = endValue;
} else {
result = Tcl_GetIntFromObj((Tcl_Interp *) NULL, objPtr, &index);
if (result != TCL_OK) {
if (iPtr != NULL) {
Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
"bad index \"", bytes,
"\": must be integer or \"end\"", (char *) NULL);
}
return result;
}
}
*indexPtr = index;
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* Tcl_GetNameOfExecutable --
*
* This procedure simply returns a pointer to the internal full
* path name of the executable file as computed by
* Tcl_FindExecutable. This procedure call is the C API
* equivalent to the "info nameofexecutable" command.
*
* Results:
* A pointer to the internal string or NULL if the internal full
* path name has not been computed or unknown.
*
* Side effects:
* The object referenced by "objPtr" might be converted to an
* integer object.
*
*----------------------------------------------------------------------
*/
CONST char *
Tcl_GetNameOfExecutable()
{
return (tclExecutableName);
}
|