aboutsummaryrefslogtreecommitdiff
path: root/teccmd.c
blob: 22c121978c68cd5c4a17cf0948965f4f4b2e26dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
char *teccmd_c_version = "teccmd.c: $Revision: 1.3 $";

/*
 * $Date: 2007/12/26 13:28:30 $
 * $Source: /cvsroot/videoteco/videoteco/teccmd.c,v $
 * $Revision: 1.3 $
 * $Locker:  $
 */

/**
 * \file teccmd.c
 * \brief Subroutines which implement (usually large) TECO commands
 */

/*
 *                     Copyright (C) 1985-2007 BY Paul Cantrell
 *
 *    This program 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, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    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, see <http://www.gnu.org/licenses/>.
 */

#include "teco.h"
#include "tecparse.h"

/*
 * Define constants for search table code
 */
#define SEARCH_M_MATCHREPEAT	0x80
#define SEARCH_M_NOT		0x40

#define SEARCH_M_TYPE		0x0F
#define SEARCH_C_BYMASK		1
#define SEARCH_C_ATLEASTONE	2
#define SEARCH_C_QREGNUM	3
#define SEARCH_C_QREGCHARS	4

/*
 * Globals
 */
    unsigned long last_search_pos1;
    unsigned long last_search_pos2;
    int last_search_status;
    struct tags *current_tags;

#ifdef CHECKPOINT
    static char *checkpoint_filename = NULL;
#endif /* CHECKPOINT */

/*
 * External Routines, etc.
 */
    extern int forced_height,forced_width;
    extern char eight_bit_output_flag,hide_cr_flag;

#ifdef CHECKPOINT
    void cmd_checkpoint(void);
#endif /* CHECKPOINT */

    struct tags *tag_load_file( char *string );

    void srch_setbits(int *bit_table,char *string);
    void srch_setbit(int *bit_table,unsigned char value);
    void srch_setallbits(int *bit_table);
    void srch_invert_sense(int *bit_table);
    int cmd_writebak(int,char *,char *,char *,int);

    extern struct buff_header *curbuf;
    extern struct buff_header *buffer_headers;
    extern char waiting_for_input_flag;
    extern char alternate_escape_character;
    extern char alternate_delete_character;
    extern char intr_flag;
    extern int term_lines;
    extern char checkpoint_flag;
    extern char checkpoint_enabled;
    extern int checkpoint_interval;
    extern char checkpoint_modified;
    extern char resize_flag;
    extern unsigned int IntBits[BITS_PER_INT];
    extern struct search_buff search_string;
    extern char suspend_is_okay_flag;

/**
 * \brief Execute a search command
 *
 * This is the runtime execution of the search command. \a arg1 and \a arg2 may
 * specify a buffer range <tt>a,bS\<mumble\></tt> in which case the search is
 * constrained within the range. In this case, the direction of the search
 * is determined by whether \a arg1 is greater than or less than \a arg2.
 * If \a arg2 == -1, then it is an ignored argument and \a arg1 specifies the
 * direction of search and the number of times to search.
 */
int
cmd_search(
			long arg1,
			long arg2,
			struct search_buff *search_tbl )
{
int count;
char forwards;
int status;
unsigned long pos1;
unsigned long pos2;
unsigned long original_dot;

    PREAMBLE();

/*
 * Compile the search Q-register into a search table
 */
    if(compile_search_string(search_tbl) == FAIL) return(FAIL);

/*
 * Before looking at the arguments, set up the defaults
 */
    original_dot = curbuf->dot;
    count = 1;
    forwards = 1;
    pos1 = 0;
    pos2 = curbuf->zee;

/*
 * If ARG2 is -1, then he did not specify a range, but rather a count
 */
    if(arg2 == -1){
	if(arg1 < 0){
	    forwards = 0;
	    count = -arg1;
	}/* End IF */

	else count = arg1;
    }/* End IF */
/*
 * Else, if ARG2 != -1, then he specified a range for the search
 */
    else {
	if(arg1 < arg2){
	    curbuf->dot = pos1 = arg1;
	    pos2 = arg2;
	}/* End IF */

	else {
	    forwards = 0;
	    pos1 = arg2;
	    curbuf->dot = pos2 = arg1;
	}/* End Else */
    }/* End Else */

/*
 * Now implement the search
 */
    while(count--){
	if(forwards){
	    status = cmd_forward_search(pos1,pos2,search_tbl);
	}/* End IF */

	else {
	    status = cmd_reverse_search(pos1,pos2,search_tbl);
	}/* End IF */

	if(status == FAIL){
	    curbuf->dot = original_dot;
	    last_search_pos1 = last_search_pos2 = -1;
	    last_search_status = 0;
	    return(FAIL);
	}/* End IF */
    }/* End While */

    last_search_status = -1;

    return(SUCCESS);

}/* End Routine */



/**
 * \brief Search in a forward direction
 *
 * This routine is used when the search progresses forward
 */
int
cmd_forward_search(
					unsigned long pos1,
					unsigned long pos2,
					struct search_buff *search_tbl )
{
register struct search_element *ep;
register unsigned char buffer_char;
register int table_position;
int dotpos;
char token_match;
char tmp_message[LINE_BUFFER_SIZE];
struct position_cache base_position;
struct position_cache running_position;

    PREAMBLE();

    dotpos = curbuf->dot;
/*
 * Insure the search string is non-null
 */
    if(!search_tbl->length){
	error_message("?Null Search String");
	search_tbl->error_message_given = YES;
	return(FAIL);
    }/* End IF */

    if(dotpos < pos1) return(FAIL);

    table_position = 0;
    ep = &search_tbl->data[table_position];
    buffer_char = buff_cached_contents(curbuf,dotpos,&base_position);
    running_position = base_position;

    while(dotpos < pos2){

	if(table_position == 0){
	    last_search_pos1 = dotpos;
	    base_position = running_position;
	}/* End IF */

	buffer_char = BUFF_CACHE_CONTENTS(&running_position);
	BUFF_INCREMENT_CACHE_POSITION(&running_position);
	dotpos++;
	token_match = 1;

	switch(ep->type & SEARCH_M_TYPE){
	    case SEARCH_C_BYMASK:
		if((ep->bitmask.intarray[buffer_char / BITS_PER_INT] & 
		    IntBits[buffer_char % BITS_PER_INT]) == 0){
			token_match = 0;
			break;
		}/* End IF */
		if(ep->type & SEARCH_M_MATCHREPEAT){
		    while(dotpos < pos2){
			buffer_char = BUFF_CACHE_CONTENTS(&running_position);
			if((ep->bitmask.intarray[buffer_char / BITS_PER_INT] &
			  IntBits[buffer_char % BITS_PER_INT]) == 0) break;
			BUFF_INCREMENT_CACHE_POSITION(&running_position);
			dotpos++;
		    }/* End While */
		}/* End IF */
		break;
	    case SEARCH_C_QREGCHARS:
		{ struct buff_header *qbp;
		register int i;
		qbp = buff_qfind(ep->value,0);
		token_match = 0;
		if(qbp == NULL) break;
		for(i = 0; i < qbp->zee; i++){
		    if(buffer_char == buff_contents(qbp,i)) token_match = 1;
		}/* End FOR */
		if(ep->type & SEARCH_M_NOT){
		    token_match = token_match ? 0 : 1;
		}
		}/* End Block */
		break;
	    case SEARCH_C_ATLEASTONE:
		error_message("?^EM not implemented...");
		search_tbl->error_message_given = YES;
		return(FAIL);
	    case SEARCH_C_QREGNUM:
		{ struct buff_header *qbp;
		qbp = buff_qfind(ep->value,0);
		token_match = 0;
		if(qbp == NULL) break;
		if(buffer_char == qbp->ivalue) token_match = 1;
		if(ep->type & SEARCH_M_NOT){
		    token_match = token_match ? 0 : 1;
		}/* End IF */
		}/* End Block */
		break;
	    default:
		sprintf(tmp_message,
		    "SEARCH: Illegal token type %d - internal error",
		    ep->type);
		tec_panic(tmp_message);
		return(FAIL);
	}/* End Switch */

	if(token_match){
	    table_position += 1;
	    ep++;
	    if(table_position == search_tbl->length){
		curbuf->dot = last_search_pos2 = dotpos;
		{/* Local Block */
		    register struct buff_header *hbp;
		    hbp = buff_qfind('_',1);
		    if(hbp == NULL) return(FAIL);
		    hbp->ivalue = last_search_pos1;
		}/* End Block */
		return(SUCCESS);
	    }/* End IF */
	}/* End IF */

	else {
	    if(intr_flag){
		error_message("?Search aborted");
		search_tbl->error_message_given = YES;
		return(FAIL);
	    }/* End IF */
	    table_position = 0;
	    ep = &search_tbl->data[table_position];
	    dotpos = last_search_pos1 + 1;
	    running_position = base_position;
	    BUFF_INCREMENT_CACHE_POSITION(&running_position);
	}/* End IF */

    }/* End While */

    return(FAIL);
	    
}/* End Routine */



/**
 * \brief Search in a reverse direction
 *
 * This routine is used when the search progresses backwards
 */
int
cmd_reverse_search(
					int pos1,
					int pos2,
					struct search_buff *search_tbl )
{
register struct search_element *ep;
register unsigned char buffer_char;
register int table_position;
int dotpos;
char token_match;
char tmp_message[LINE_BUFFER_SIZE];
struct position_cache base_position;
struct position_cache running_position;

    PREAMBLE();

    dotpos = curbuf->dot;
/*
 * Insure the search string is non-null
 */
    if(!search_tbl->length){
	error_message("?Null Search String");
	search_tbl->error_message_given = YES;
	return(FAIL);
    }/* End IF */

    if(dotpos > pos2) return(FAIL);

    table_position = search_tbl->length - 1;
    ep = &search_tbl->data[table_position];
    buffer_char = buff_cached_contents(curbuf,dotpos,&base_position);
    running_position = base_position;

    while(dotpos > pos1){

	if(table_position == (search_tbl->length - 1)){
	    last_search_pos2 = dotpos;
	    base_position = running_position;
	}/* End IF */

	dotpos--;
	BUFF_DECREMENT_CACHE_POSITION(&running_position);
	buffer_char = BUFF_CACHE_CONTENTS(&running_position);
	token_match = 1;

	switch(ep->type & SEARCH_M_TYPE){
	    case SEARCH_C_BYMASK:
		if((ep->bitmask.intarray[buffer_char / BITS_PER_INT] & 
		    IntBits[buffer_char % BITS_PER_INT]) == 0){
			token_match = 0;
			break;
		}/* End IF */
		if(ep->type & SEARCH_M_MATCHREPEAT){
		    while(dotpos > pos1){
			BUFF_DECREMENT_CACHE_POSITION(&running_position);
			dotpos--;
			buffer_char = BUFF_CACHE_CONTENTS(&running_position);
			if(ep->bitmask.intarray[buffer_char / BITS_PER_INT] &
			  IntBits[buffer_char % BITS_PER_INT]) continue;
			dotpos++;
			BUFF_INCREMENT_CACHE_POSITION(&running_position);
			break;
		    }/* End While */
		}/* End IF */
		break;
	    case SEARCH_C_QREGCHARS:
		{ struct buff_header *qbp;
		register int i;
		qbp = buff_qfind(ep->value,0);
		token_match = 0;
		if(qbp == NULL) break;
		for(i = 0; i < qbp->zee; i++){
		    if(buffer_char == buff_contents(qbp,i)) token_match = 1;
		}/* End FOR */
		}/* End Block */
		break;
	    case SEARCH_C_ATLEASTONE:
		error_message("?^EM not implemented...");
		search_tbl->error_message_given = YES;
		return(FAIL);
	    case SEARCH_C_QREGNUM:
		{ struct buff_header *qbp;
		qbp = buff_qfind(ep->value,0);
		token_match = 0;
		if(qbp == NULL) break;
		if(buffer_char == qbp->ivalue) token_match = 1;
		}/* End Block */
		break;
	    default:
		sprintf(tmp_message,
		    "SEARCH: Illegal token type %d - internal error",
		    ep->type);
		tec_panic(tmp_message);
		return(FAIL);
	}/* End Switch */

	if(token_match){
	    table_position -= 1;
	    ep--;
	    if(table_position < 0){
		curbuf->dot = last_search_pos1 = dotpos;
		{/* Local Block */
		    register struct buff_header *hbp;
		    hbp = buff_qfind('_',1);
		    if(hbp == NULL) return(FAIL);
		    hbp->ivalue = last_search_pos2;
		}/* End Block */
		return(SUCCESS);
	    }/* End IF */
	}/* End IF */

	else {
	    if(intr_flag){
		error_message("?Search aborted\n");
		search_tbl->error_message_given = YES;
		return(FAIL);
	    }/* End IF */
	    table_position = search_tbl->length - 1;
	    ep = &search_tbl->data[table_position];
	    dotpos = last_search_pos2 - 1;
	    running_position = base_position;
	    BUFF_DECREMENT_CACHE_POSITION(&running_position);
	}/* End IF */

    }/* End While */

    return(FAIL);
	    
}/* End Routine */



/**
 * \brief Sets Q-register '_' to the new search string
 *
 * This routine is called from the exec functions when a search string
 * is to be set. It takes care of loading Q-register '_' with the new
 * search string, as well as setting up all the undo tokens to insure
 * that this is all reversable.
 */
int
set_search_string_with_undo(
								char *string,
								struct cmd_token *uct )
{
register struct buff_header *qbp;
register struct undo_token *ut;
register int i,c;
register char *cp;
int new_length;

    PREAMBLE();

/*
 * Get a pointer to the special Q-register which holds the search string. If
 * it does not exist, have it be created automatically.
 */
    qbp = buff_qfind('_',1);
    if(qbp == NULL){
	return(FAIL);
    }/* End IF */

/*
 * This code allows us to load the numeric half of the search string
 * Q-register with the length of the string we just found. In order
 * to be able to undo it, we have to protect the previous value, and
 * it just happens that by doing it here we probably catch all the
 * places where the search could have taken place. Then again, maybe
 * not (search really should not be calling this routine repeatedly
 * in an iteration or macro, for instance). If this gets cleaned up,
 * then this code might not be appropriate... XXX
 */
    ut = allocate_undo_token(uct);
    if(ut == NULL) return(FAIL);
    ut->opcode = UNDO_C_UQREGISTER;
    ut->iarg1 = '_';
    ut->iarg2 = qbp->ivalue;

    if(string == NULL) return(SUCCESS);
    if((new_length = strlen(string)) == 0) return(SUCCESS);

/*
 * Set up undo tokens for the search_state and search_position information
 * which typically gets used to pass search information between halves of
 * a search / replace type command.
 */
    ut = allocate_undo_token(uct);
    if(ut == NULL) return(FAIL);
    ut->opcode = UNDO_C_SET_SEARCH_GLOBALS;
    ut->iarg1 = last_search_pos1;
    ut->iarg2 = last_search_pos2;
    ut->carg1 = (char *)(uintptr_t)last_search_status;

/*
 * If the new length is the same as the old length, there is a chance we are
 * just setting the same search string over again. To find out, we actually
 * have to loop through comparing bytes in the Q-register to bytes in the
 * supplied string. If none of them are different, we can just return without
 * changing the search string. This happens often in iterations...
 */
    if(new_length == qbp->zee){
	cp = string;
	for(i = 0; i < new_length; i++){
	    c = buff_contents(qbp,i);
	    if(*cp++ != c) break;
	}/* End FOR */

	if(i == new_length) return(SUCCESS);

    }/* End IF */
/*
 * At this point we set an undo token so that if the following stuff gets
 * undone, we will re-compile the search string.
 */
    ut = allocate_undo_token(uct);
    if(ut == NULL) return(FAIL);
    ut->opcode = UNDO_C_SET_SEARCH_STRING;
/*
 * Delete the current contents of the search string Q-register to make room
 * for the new contents.
 */
    if(qbp->zee > 0) buff_delete_with_undo(uct,qbp,0,qbp->zee);
/*
 * Set up the undo token to delete the text we are going to insert into the
 * search string Q-register.
 */
    buff_insert_with_undo(uct,qbp,0,string,new_length);

    return(SUCCESS);

}/* End Routine */



/**
 * \brief Creates the search table for a given input string
 *
 * This routine parses the input search string and creates the search
 * table which will describe to the search routines how to match the
 * buffer characters. Note that the search table must be constructed
 * in such a way that it works equally well backward or forward.
 */
int
compile_search_string( struct search_buff *search_tbl )
{
register unsigned long position;
register char c;
register char *cp;
register struct buff_header *qbp;
register struct search_element *ep;
int radix;
int bracket_bits[256/BITS_PER_INT];
char tmp_message[LINE_BUFFER_SIZE];
char either_case_flag = YES;
char bracket_wildcard = NO;
char bracket_not_flag = NO;
char not_flag = NO;
char matchrepeat_flag = NO;

    PREAMBLE();

#define QBUF_CHAR() \
    (position < qbp->zee ? *cp++ = buff_contents(qbp,position++) : 0)

    search_tbl->error_message_given = NO;

/*
 * Get a pointer to the special Q-register which holds the search string. If
 * it does not exist, have it be created automatically.
 */
    qbp = buff_qfind('_',1);
    if(qbp == NULL){
	error_message("Internal error - qfind returned null");
	search_tbl->error_message_given = YES;
	return(FAIL);
    }/* End IF */

    if(qbp->zee >= SEARCH_STRING_MAX){
	error_message("Search Q-register too long");
	search_tbl->error_message_given = YES;
	return(FAIL);
    }/* End IF */

    search_string.length = 0;
    position = 0;
    cp = search_string.input;
    ep = search_string.data;
    bzero(&ep->bitmask,sizeof(ep->bitmask));

    while((c = QBUF_CHAR())){
/*
 * Check for CNTRL_E which is the standard lead-in for wildcard search
 * strings.
 */
	if(c == CNTRL_E){
	    c = QBUF_CHAR();
	    switch(c){
/*
 * [ specifies a list of possible matches, ANY of which satisfy the
 * position.
 */
		case '[':
		    bzero(bracket_bits,sizeof(bracket_bits));
		    bracket_wildcard = YES;
		    if(not_flag){
			not_flag = NO;
			bracket_not_flag = YES;
		    }/* End IF */
		    continue;
/*
 * A specifies any ALPHABETIC character as a match. Thus any upper or lower
 * case character in the range a-z will match.
 */
		case 'A': case 'a':
		    ep->type = SEARCH_C_BYMASK;
		    srch_setbits((int *)&ep->bitmask,
						"abcdefghijklmnopqrstuvwxyz");
		    srch_setbits((int *)&ep->bitmask,
						"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
		    break;
/*
 * B specifies any separator character, when separator is defined as any
 * non-alphanumeric character.
 */
		case 'B': case 'b':
		{
		    register int i;
		    ep->type = SEARCH_C_BYMASK;
		    for(i = 0; i < 256; i++){
			if(!isalnum(i)) srch_setbit((int *)&ep->bitmask,i);
		    }/* End FOR */
		    break;
		}
/*
 * C specifies any symbol constituent. This is any alpha-numeric character,
 * or dot (.), dollar sign ($), or underscore (_).
 */
		case 'C': case 'c':
		    ep->type = SEARCH_C_BYMASK;
		    srch_setbits((int *)&ep->bitmask,
						"abcdefghijklmnopqrstuvwxyz");
		    srch_setbits((int *)&ep->bitmask,
						"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
		    srch_setbits((int *)&ep->bitmask,"0123456789.$_");
		    break;
/*
 * D specifies that any digit (0 to 9) is acceptable.
 */
		case 'D': case 'd':
		    ep->type = SEARCH_C_BYMASK | SEARCH_M_MATCHREPEAT;
		    srch_setbits((int *)&ep->bitmask,"0123456789");
		    break;
/* 
 * E or ^E is a Video TECO only definition which means that whether searches
 * are case sensitive or not should be toggled.
 */
		case 'E': case 'e': case CNTRL_E:
		    either_case_flag ^= 1;
		    continue;
/*
 * G followed by a Q register name matches any character which is in the
 * text portion of the Q register.
 */
		case 'G': case 'g':
		    if(bracket_wildcard){
			error_message("^EG inside ^E[,,,] illegal");
			search_tbl->error_message_given = YES;
			return(FAIL);
		    }/* End IF */
		    ep->type = SEARCH_C_QREGCHARS;
		    ep->value = QBUF_CHAR();
		    break;
/*
 * L matches any line terminator character.
 */
		case 'L': case 'l':
		    ep->type = SEARCH_C_BYMASK;
		    srch_setbits((int *)&ep->bitmask,"\n\r\f");
		    break;
/* 
 * M is a flag that says any number of the following character (but at least
 * one) are acceptable. Thus ^EMA would match A or AA or AAA, etc.
 */
		case 'M': case 'm':
		    matchrepeat_flag = YES;
		    continue;
/*
 * R matches any alphanumeric
 */
		case 'R': case 'r':
		    ep->type = SEARCH_C_BYMASK;
		    srch_setbits((int *)&ep->bitmask,
						"abcdefghijklmnopqrstuvwxyz");
		    srch_setbits((int *)&ep->bitmask,
						"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
		    srch_setbits((int *)&ep->bitmask,"0123456789");
		    break;
/*
 * S matches any number of tabs and spaces, in any mixture (but there must
 * be at least one).
 */
		case 'S': case 's':
		    ep->type = SEARCH_C_BYMASK | SEARCH_M_MATCHREPEAT;
		    srch_setbits((int *)&ep->bitmask," \t");
		    break;
/*
 * U matches against the ASCII code which is contained in the Q register's
 * numeric storage.
 */
		case 'U': case 'u':
		    if(bracket_wildcard){
			error_message("^EU inside ^E[,,,] illegal");
			search_tbl->error_message_given = YES;
			return(FAIL);
		    }/* End IF */
		    ep->type = SEARCH_C_QREGNUM;
		    ep->value = QBUF_CHAR();
		    break;
/*
 * V matches any lowercase letter.
 */
		case 'V': case 'v':
		    ep->type = SEARCH_C_BYMASK;
		    srch_setbits((int *)&ep->bitmask,
						"abcdefghijklmnopqrstuvwxyz");
		    break;
/*
 * W matches any uppercase letter
 */
		case 'W': case 'w':
		    ep->type = SEARCH_C_BYMASK;
		    srch_setbits((int *)&ep->bitmask,
						"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
		    break;
/*
 * X is similar to ^X in that it matches any character
 */
		case 'X': case 'x':
		    ep->type = SEARCH_C_BYMASK;
		    srch_setallbits((int *)&ep->bitmask);
		    break;
/*
 * The < character opens the sequence for ^E<nnn> where nnn is a numeric
 * (ASCII) code to search for. Classic TECO says that this code is octal,
 * but Video TECO defaults to decimal, octal if the first digit is zero,
 * or hex if the first two digits are 0x.
 */
		case '<':
		    ep->value = 0;
		    radix = 10;
		    c = QBUF_CHAR();
		    if(c == '0'){
			radix = 8;
			c = QBUF_CHAR();
			if(c == 'x' || c == 'X'){
			    radix = 16;
			    c = QBUF_CHAR();
			}/* End IF */
		    }/* End IF */
		    while(c && c != '>'){
			if(c >= '0' && c <= '7'){
			    ep->value *= radix;
			    ep->value += c - '0';
			    c = QBUF_CHAR();
			    continue;
			}/* End IF */
			if(c >= '8' && c <= '9' && radix > 8){
			    ep->value *= radix;
			    ep->value += c - '0';
			    c = QBUF_CHAR();
			    continue;
			}/* End IF */
			if(c >= 'a' && c <= 'f' && radix == 16){
			    c = TOUPPER(c);
			}/* End IF */
			if(c >= 'A' && c <= 'F' && radix == 16){
			    ep->value *= radix;
			    ep->value += c - 'A' + 10;
			    c = QBUF_CHAR();
			    continue;
			}/* End IF */
			sprintf(tmp_message,
			    "?Illegal ^E code '%c' in search string",c);
			search_tbl->error_message_given = YES;
			error_message(tmp_message);
			return(FAIL);
		    }/* End While */
		    if(c == '>') c = QBUF_CHAR();
		    else {
			error_message("?Missing '>' in ^E<nnn> search string");
			search_tbl->error_message_given = YES;
			return(FAIL);
		    }/* End Else */
		    ep->type = SEARCH_C_BYMASK;
		    srch_setbit((int *)&ep->bitmask,ep->value);
		    break;
/*
 * This case is really just to catch the user typing ^E followed by the
 * end of the search string. We just make it search for ^E in this case.
 */
		case '\0':
		    ep->type = SEARCH_C_BYMASK;
		    srch_setbit((int *)&ep->bitmask,CNTRL_E);
		    break;
/*
 * Any unrecognized ^E code just searches for the exact character which
 * follows the ^E. This is probably not very good, we should issue an
 * error message instead.
 */
		default:
		    ep->type = SEARCH_C_BYMASK;
		    srch_setbit((int *)&ep->bitmask,c);
		    break;
	    }/* End Switch */
	}/* End IF */
/*
 * The ^X code embedded in a search string means match any character at
 * this position.
 */
	else if(c == CNTRL_X){
	    ep->type = SEARCH_C_BYMASK;
	    srch_setallbits((int *)&ep->bitmask);
	}/* End Else */
/*
 * The ^N code embedded in a search string means match any character EXCEPT
 * the one that follows.
 */
	else if(c == CNTRL_N){
	    not_flag = YES;
	    continue;
	}/* End Else */
/*
 * Else, here if it is just a normal character which should be searched for
 */
	else {
	    ep->type = SEARCH_C_BYMASK;
	    srch_setbit((int *)&ep->bitmask,c);
	    if(either_case_flag){
		if(islower((int)c)) srch_setbit((int *)&ep->bitmask,TOUPPER((int)c));
		if(isupper((int)c)) srch_setbit((int *)&ep->bitmask,TOLOWER((int)c));
	    }/* End IF */
	}/* End Else */

/*
 * If sense has been inverted with ^N, invert the bits
 */
	if(not_flag){
	    if(ep->type & SEARCH_C_BYMASK){
		srch_invert_sense((int *)&ep->bitmask);
	    }/* End IF */
	    ep->type &= ~SEARCH_M_MATCHREPEAT;
	    ep->type |= SEARCH_M_NOT;
	    not_flag = NO;
	}/* End IF */
/*
 * If already inside a ^E[a,b,c] type wildcard, check for comma or close
 * bracket.
 */
	if(bracket_wildcard){
	    c = QBUF_CHAR();
	    switch(c){
		case ',':
		{
		    register int i;
		    for(i = 0; i < 256 / BITS_PER_INT; i++){
			bracket_bits[i] |= ep->bitmask.intarray[i];
		    }/* End FOR */
		    bzero(&ep->bitmask,sizeof(ep->bitmask));
		    continue;
		}
		case ']':
		{
		    register int i;
		    for(i = 0; i < 256 / BITS_PER_INT; i++){
			ep->bitmask.intarray[i] |= bracket_bits[i];
		    }/* End FOR */
		    if(bracket_not_flag){
			srch_invert_sense((int *)&ep->bitmask);
			ep->type &= ~SEARCH_M_MATCHREPEAT;
			bracket_not_flag = NO;
		    }/* End IF */
		    bracket_wildcard = NO;
		    break;
		}
		default:
		    error_message("Illegal syntax in [,,,] construct");
		    search_tbl->error_message_given = YES;
		    return(FAIL);
	    }/* End Switch */
	}/* End IF */
/*
 * Test for match repeating characters flag
 */
	if(matchrepeat_flag){
	    ep->type |= SEARCH_M_MATCHREPEAT;
	    matchrepeat_flag = NO;
	}/* End IF */
/*
 * Point to the next entry in the search string table
 */
	ep++;
	bzero(&ep->bitmask,sizeof(ep->bitmask));
	search_string.length += 1;

    }/* End While */

    *cp = '\0';

    return(SUCCESS);

}/* End Routine */



/**
 * \brief Set ALL the bits on for a search table entry
 *
 * This routine makes ANY character match the search string table
 * entry.
 */
void
srch_setallbits( int *bit_table )
{
register int i;

    PREAMBLE();

    for(i = 0; i < 256 / BITS_PER_INT; i++){
	bit_table[i] = ~0;
    }/* End FOR */

}/* End Routine */

/**
 * \brief Invert the sense of a search table entry
 *
 * This routine is called when a 'NOT' modifier has been used,
 * meaning that just the opposite matching should occur. We
 * simply invert the setting of the bits in the search table entry.
 */
void
srch_invert_sense( int *bit_table )
{
register int i;

    PREAMBLE();

    for(i = 0; i < 256 / BITS_PER_INT; i++){
	bit_table[i] ^= ~0;
    }/* End FOR */

}/* End Routine */

/**
 * \brief Set the corresponding search bits to 'on'
 *
 * This routine is called with a zero terminated string of characters.
 * It sets each corresponding bit in the search table to a 1.
 */
void
srch_setbits( int *bit_table, char *string )
{
register int c;

    PREAMBLE();

    while((c = *string++)){
	bit_table[c/BITS_PER_INT] |= IntBits[c%BITS_PER_INT];
    }/* End While */

}/* End Routine */

/**
 * \brief Set the corresponding search bit to 'on'
 *
 * This routine is called with a char whose corresponding bit should
 * be set in the search table.
 */
void
srch_setbit( int *bit_table, unsigned char value )
{

    PREAMBLE();

    bit_table[value/BITS_PER_INT] |= IntBits[value%BITS_PER_INT];

}/* End Routine */



/**
 * \brief Implements the EW command by writing out a buffer
 *
 * This routine will write out the contents of the buffer after creating
 * the appropriate backup files. If there is not already a file with the
 * a .OLD extension, this is created. Otherwise, a .BAK file is created.
 * If the name is so long that we can't append a of suffix, we have to
 * hack it up a bit. This is not guaranteed to work, but...
 */
int
cmd_write( struct buff_header *hbp, char *filename )
{
char base_filename[TECO_FILENAME_COMPONENT_LENGTH + 1];
char path_name[TECO_FILENAME_TOTAL_LENGTH + 1];
char tmp_filename[TECO_FILENAME_TOTAL_LENGTH + 1];
char tmp_message[LINE_BUFFER_SIZE];
register char *cp1,*cp2;
register int i;
int path_len;
int file_len;
int combined_len;
int fi;
int status;

    PREAMBLE();

    /*
     * FIXME: This could be supported on MS-DOS if we
     * respected DOS directory separators and
     * used short filename extensions.
     */
#ifdef UNIX
/*
 * First step is to separate the path elements from the filename itself.
 * To do this, we search for the directory path separator.
 */
    cp1 = cp2 = filename;
    while(*cp1){
	if(*cp1++ == '/') cp2 = cp1;
    }/* End While */

    combined_len = strlen(filename);
    file_len = strlen(cp2);
    path_len = combined_len - file_len;

/*
 * The filename really should not be longer than this.
 */
    if(file_len > TECO_FILENAME_COMPONENT_LENGTH){
	sprintf(tmp_message,"?Filename is too long <%s>",cp2);
	error_message(tmp_message);
	return(FAIL);
    }/* End IF */
/*
 * Make a copy of the filename portion for safe keeping.
 */
    (void) strcpy(base_filename,cp2);
/*
 * And do the same for the path portion.
 */
    cp1 = path_name;
    cp2 = filename;
    for(i = 0; i < path_len; i++){
	*cp1++ = *cp2++;
    }/* End FOR */
    *cp1 = '\0';

/*
 * Now the first thing to do is see if the file exists, because this
 * will impact us on whether we want to attempt .BAK files
 */
    if(hbp->isbackedup == NO){

	fi = open(filename,O_RDONLY);
	if(fi >= 0){

#ifdef CREATE_OLD_FILES
#ifdef HAVE_LONG_FILE_NAMES
	    (void) strcpy(tmp_filename,base_filename);
	    (void) strcat(tmp_filename,".OLD");
	    status = cmd_writebak(fi,path_name,filename,tmp_filename,O_EXCL);
#else
	    (void) strcpy(tmp_filename,path_name);
	    (void) strcat(tmp_filename,".TECOLD");
	    status = cmd_writebak(fi,filename,tmp_filename,base_filename,O_EXCL);
#endif
	    if(status == SUCCESS) hbp->isbackedup = YES;

	    if(status == FAIL && errno != EEXIST){
	    	/* NOTE: cmd_writebak has already written the error_message */
		close(fi);
		return(FAIL);
	    }/* End IF */
#endif

	    if(hbp->isbackedup == NO){
#ifdef HAVE_LONG_FILE_NAMES
		(void) strcpy(tmp_filename,base_filename);
		(void) strcat(tmp_filename,".BAK");
		status = cmd_writebak(fi,path_name,filename,tmp_filename,0);
#else
		(void) strcpy(tmp_filename,path_name);
		(void) strcat(tmp_filename,".TECBAK");
		status = cmd_writebak(fi,filename,tmp_filename,base_filename,0);
#endif
		if(status == SUCCESS) hbp->isbackedup = YES;
		if(status == FAIL){
		    /* NOTE: cmd_writebak has already written the error_message */
		    close(fi);
		    return(FAIL);
	        }/* End IF */
	    }/* End IF */
	}/* End IF */

    close(fi);

    }/* End IF */

/* END OF UNIX CONDITIONAL CODE */

#endif

#ifdef VMS
    fi = creat(filename,0,"mrs = 0");
#else
    fi = open(filename,O_WRONLY|O_CREAT|O_TRUNC,0666);
#endif

    if(fi < 0){
	sprintf(tmp_message,"?Error opening <%s>: %s",
	    filename,error_text(errno));
	error_message(tmp_message);
	return(FAIL);
    }/* End IF */

    status = buff_write(hbp,fi,0,hbp->zee);

    if(status == FAIL){
	sprintf(tmp_message,"?Error writing <%s>: %s",
	    filename,error_text(errno));
	error_message(tmp_message);
	close(fi);
	return(FAIL);
    }/* End IF */

    close(fi);

    hbp->ismodified = NO;
    if(hbp == curbuf){
	screen_label_line(hbp,"",LABEL_C_MODIFIED);
    }/* End IF */

    return(SUCCESS);

}/* End Routine */



/**
 * \brief Routine to open the BAK or OLD file
 *
 * This routine opens a channel to the backup file and creates any
 * subdirectories that may be necessary.
 */
int
cmd_writebak(
				int fi,
				char *pathname,
				char *input_filename,
				char *output_filename,
				int open_flags )
{
char tmp_filename[TECO_FILENAME_TOTAL_LENGTH + 1];
char tmp_message[LINE_BUFFER_SIZE];
char iobuf[IO_BUFFER_SIZE];
int fo;
register int i;
register int status;

    PREAMBLE();

#ifndef HAVE_LONG_FILE_NAMES
#ifdef MSDOS
    i = mkdir(pathname);
#else
    i = mkdir(pathname,0777);
#endif
    if(i){
	if(errno != EEXIST){
	    sprintf(tmp_message,"?Error creating subdirectory <%s>: %s",
		pathname,error_text(errno));
	    error_message(tmp_message);
	    errno = 0;
	    return(FAIL);
	}/* End IF */
    }/* End IF */
#endif

    (void) strcpy(tmp_filename,pathname);
    if(strlen(tmp_filename)){
	(void) strcat(tmp_filename,"/");
    }/* End IF */

    (void) strcat(tmp_filename,output_filename);
    chmod(tmp_filename,0666);
    fo = open(tmp_filename,O_WRONLY|O_CREAT|O_TRUNC|open_flags,0666);
    if(fo < 0){
	if(errno == EEXIST){
	    chmod(tmp_filename,0444);
	    errno = EEXIST;
	    return(FAIL);
	}/* End IF */
	snprintf(tmp_message,sizeof(tmp_message),"?Error opening <%s>: %s",
	    tmp_filename,error_text(errno));
	error_message(tmp_message);
	errno = 0;
	return(FAIL);
    }/* End IF */

    while(1){
	i = read(fi,iobuf,IO_BUFFER_SIZE);
	if(i < 0){
	    snprintf(tmp_message,sizeof(tmp_message),"?Error reading <%s>: %s",
		input_filename,error_text(errno));
	    error_message(tmp_message);
	    close(fo);
	    errno = 0;
	    return(FAIL);
	}/* End IF */

	status = write(fo,iobuf,(unsigned)i);
	if(status < 0){
	    snprintf(tmp_message,sizeof(tmp_message),"?Error writing <%s>: %s",
		tmp_filename,error_text(errno));
	    error_message(tmp_message);
	    close(fo);
	    errno = 0;
	    return(FAIL);
	}/* End IF */

	if(i < IO_BUFFER_SIZE) break;

    }/* End While */

    close(fo);
    chmod(tmp_filename,0444);

    return(SUCCESS);

}/* End Routine */



/**
 * \brief Here to move within the edit buffer by words
 *
 * Here in response to one of the 'word' commands. We move the
 * current edit position forward or backward by the specified
 * number of words.
 */
int
cmd_wordmove( long count )
{
    register int c;

    PREAMBLE();

    while(count > 0){
	while(1){
	    if(curbuf->dot == curbuf->zee) break;
	    c = buff_contents(curbuf,curbuf->dot);
	    if(isspace(c)) break;
	    curbuf->dot++;
	}/* End While */

	while(1){
	    if(curbuf->dot == curbuf->zee) break;
	    c = buff_contents(curbuf,curbuf->dot);
	    if(!isspace(c)) break;
	    curbuf->dot++;
	}/* End While */

	count -= 1;

    }/* End While */

    while(count < 0){
	while(1){
	    if(curbuf->dot == 0) break;
	    c = buff_contents(curbuf,curbuf->dot-1);
	    if(isspace(c)) break;
	    curbuf->dot--;
	}/* End While */

	while(1){
	    if(curbuf->dot == 0) break;
	    c = buff_contents(curbuf,curbuf->dot-1);
	    if(!isspace(c)) break;
	    curbuf->dot--;
	}/* End While */

	count += 1;

    }/* End While */

    return(SUCCESS);

}/* End Routine */



/**
 * \brief We get here on a suspend signal from the tty
 *
 * Here when the user has typed ^Z. We want to suspend back to the
 * original shell.
 */
void
cmd_suspend()
{
#if defined(UNIX) || defined(VMS)
extern char susp_flag;
#endif

    PREAMBLE();

#ifdef UNIX
    if(waiting_for_input_flag == YES){
	pause_while_in_input_wait();
	return;
    }/* End IF */

#ifdef JOB_CONTROL
    if(susp_flag++ >= 4){
	signal(SIGTSTP,(void (*)(int))SIG_DFL);
    }/* End IF */
#endif

#endif

#ifdef VMS
    if(suspend_is_okay_flag == YES){
	susp_flag++;
    }/* End IF */
#endif

}/* End Routine */

/**
 * \brief Here when we are ready to suspend back to the shell
 *
 * Here when the parser has noticed that the suspend flag is set, and
 * is ready for us to suspend the process.
 */
void
cmd_pause()
{
extern char susp_flag;

#ifdef VMS
long owner_pid;
long status;

struct itmlst {
    short buffer_length;
    short item_code;
    char *buffer_address;
    long *length_address;
    long terminating_zero;
}list;
#endif

    PREAMBLE();

    restore_tty();

#if defined(JOB_CONTROL) || defined(VMS)

#ifdef CHECKPOINT
    alarm(0);
#endif /* CHECKPOINT */

#ifdef UNIX
    if(suspend_is_okay_flag == YES){
	kill(getpid(),SIGSTOP);
	signal(SIGTSTP,(void (*)(int))cmd_suspend);
    }
#endif /* UNIX */

#ifdef VMS
    owner_pid = 0;
    list.buffer_length = sizeof(owner_pid);
    list.item_code = JPI$_OWNER;
    list.buffer_address = (char *)&owner_pid;
    list.length_address = 0;
    list.terminating_zero = 0;
    sys$getjpiw(0,0,0,&list,0,0,0);

    if(owner_pid){
	status = lib$attach(&owner_pid);
	if(status != SS$_NORMAL){
	    error_message("?LIB$ATTACH failed");
	    susp_flag = 0;
	    return;
	}/* End IF */
    }/* End IF */

    else {
	error_message("?Cannot suspend. No parent process for LIB$ATTACH");
	susp_flag = 0;
	return;
    }/* End IF */
#endif

#ifdef CHECKPOINT
    alarm(checkpoint_interval);
#endif /* CHECKPOINT */

#endif /* JOB_CONTROL */

    initialize_tty();
    screen_redraw();
    susp_flag = 0;

}/* End Routine */



/**
 * \brief We get here when the OS says the window changed size
 *
 * Here when the OS says our window changed size. We just wanna
 * call the screen package's resize entry point so it can build
 * a new screen for us.
 */
void
cmd_winch()
{
    PREAMBLE();

#ifdef ATT_UNIX
    signal(SIGWINCH,(void (*)())cmd_winch);
#endif

#ifdef SEQUOIA
    screen_message("WINCH!\g\g");
    screen_refresh();
    return;
#endif /* SEQUOIA */

    resize_flag = YES;

    if(waiting_for_input_flag){
	screen_resize();
	screen_refresh();
    }/* End IF */

}/* End Routine */



/**
 * \brief We get here on an interrupt signal from the user
 *
 * Here when the user has typed ^C. We want to cause any current commands
 * to abort. This gives the user a way to break out of infinite iterations
 * and macros.
 */
void
cmd_interrupt()
{
    PREAMBLE();

#ifdef ATT_UNIX
    signal(SIGINT,(void (*)())cmd_interrupt);
#endif

    if(intr_flag++ >= 4){
	if(waiting_for_input_flag == NO){
	    restore_tty();
	    fprintf(stderr,"TECO aborted...\n");
#ifdef MSDOS
	    abort();
#else
	    signal(SIGINT,(void (*)(int))SIG_DFL);
	    kill(getpid(),SIGINT);
#endif
	}/* End IF */
    }/* End IF */

    screen_message("interrupt!");

}/* End Routine */



#ifdef CHECKPOINT

/**
 * \brief Here to when the interval timer expires
 *
 * This function is called periodically when the interval timer says
 * it is time for us to checkpoint all our buffers.
 */
cmd_alarm()
{

    PREAMBLE();

#if defined(ATT_UNIX) || defined(VMS)
    signal(SIGALRM,(void (*)())cmd_alarm);
#endif

    if(waiting_for_input_flag == 0){
	checkpoint_flag = YES;
    }/* End IF */

    else {
	if(checkpoint_enabled == YES){
	    cmd_checkpoint();
	}
	checkpoint_flag = NO;
    }/* End Else */

#ifdef VMS
    alarm(0);
#endif /* VMS */
    alarm(checkpoint_interval);

}/* End Routine */

#endif /* CHECKPOINT */



#ifdef CHECKPOINT

/**
 * \brief Here to write all our buffers to a checkpoint file
 *
 * Here we check out all the buffers and write out any which have
 * ever been modified.
 */
void
cmd_checkpoint()
{
register struct buff_header *bp;
int fd = 0;
char temp_buffer[TECO_FILENAME_TOTAL_LENGTH+20];
char message_save_buff[SCREEN_NOMINAL_LINE_WIDTH];
char filename_buffer[64];
register char *cp;
int i,status;

    PREAMBLE();

    if(checkpoint_modified == NO) return;
    checkpoint_modified = NO;

    if(checkpoint_filename == NULL){
	temp_buffer[0] = '\0';
	if(cp = (char *)getenv("HOME")){
	    (void) strcpy(temp_buffer,cp);
#ifndef VMS
	    (void) strcat(temp_buffer,"/");
#endif /* VMS */
	}/* End IF */

#ifndef VMS
	strcpy(filename_buffer,".tecXXXXXX");
#else
	strcpy(filename_buffer,"tecXXXXXX.CKP");
#endif /* VMS */
	cp = mktemp(filename_buffer);

	strcat(temp_buffer,cp);
	checkpoint_filename = tec_alloc(TYPE_C_CPERM,strlen(temp_buffer)+1);
	if(checkpoint_filename == NULL) return;
	strcpy(checkpoint_filename,temp_buffer);
    }/* End IF */

    status = 0;
    for(bp = buffer_headers; bp != NULL; bp = bp->next_header){
	if(bp->ismodified){
	    status = 1;
	}/* End IF */
    }/* End FOR */

    if(status == 0) return;

    screen_save_current_message(message_save_buff,sizeof(message_save_buff));
    screen_message("checkpointing...");
    screen_refresh();

    fd = open(checkpoint_filename,O_WRONLY|O_CREAT|O_TRUNC,0666);
    if(fd == 0 || fd == -1){
	sprintf(temp_buffer,"?Checkpoint file %s open failed %s",
	    checkpoint_filename,error_text(errno));
	error_message(temp_buffer);
	screen_refresh();
	sleep(2);
	return;
    }/* End IF */


    for(bp = buffer_headers; bp != NULL; bp = bp->next_header){
	if(bp->ismodified == NO && bp->buffer_number <= 0) continue;
	if(bp->buffer_number == 0)continue;
	i = (78 - strlen(bp->name) - 4) / 2;
	cp = temp_buffer;
	*cp++ = '\n';
	while(i-- > 0)*cp++ = '*';
	*cp++ = '(';
	strcpy(cp,bp->name);
	while(*cp)cp++;
	*cp++ = ')';
	if(bp->ismodified) *cp++ = '+';
	while(cp < &temp_buffer[78]) *cp++ = '*';
	*cp++ = '\n';
	*cp++ = '\0';
	i = strlen(temp_buffer);

	if(write(fd,temp_buffer,i) != i){
	    sprintf(temp_buffer,"?checkpoint failed %s",error_text(errno));
	    error_message(temp_buffer);
	    screen_refresh();
	    sleep(2);
	    close(fd);
	    return;
	}/* End IF */


	if(bp->ismodified){
	    status = buff_write(bp,fd,0,bp->zee);
	}/* End IF */

    }/* End FOR */

    close(fd);

    screen_message("checkpointing... done");
    screen_refresh();
    screen_message(message_save_buff);

}/* End Routine */

#endif /* CHECKPOINT */



#ifdef CHECKPOINT

/**
 * \brief On exit we clean up the checkpoint file
 *
 * This routine is called when a clean exit is assured. We remove
 * the checkpoint file so they don't clutter up the disk.
 */
void
remove_checkpoint_file()
{

    PREAMBLE();

    if(checkpoint_filename == NULL) return;
#ifndef VMS
    unlink(checkpoint_filename);
#endif

}/* End Routine */

#endif /* CHECKPOINT */



#if defined(VMS) || defined(STARDENT) || defined(STARDENT_860) || defined(SEQUOIA) || defined(LOCUS_SYSV) || defined(SCO_386)
/**
 * \brief Zero an array of bytes
 *
 * This routine zeros an array of bytes. For some reason the VMS
 * library doesn't seem to know about it.
 */
bzero(array,length)
register char *array;
register int length;
{
    PREAMBLE();

    while(length-- > 0) *array++ = '\0';

}/* End Routine */
#endif /*  VMS || STARDENT et al */



/**
 * \brief Issue a command to the operating system
 *
 * This routine is called in response to the \c EC command which allows the
 * user to execute operating system commands from within the editor.
 * Buffer content can either be piped into the process and replaced with its
 * output (filtering) or the command's output can be inserted at the current
 * buffer pointer position.
 *
 * \param uct		Command Token
 * \param arg_count	Number of arguments passed to \c EC. 0 results in
 *			unidirectional piping of the command's output into
 *			the edit buffer (at \e DOT). 1 or 2 indicates that
 *			\a arg1 and \a arg2 contain a buffer range that should
 *			be filtered through the command (single-arguments to
 *			\c EC indicating a relative buffer range in lines are
 *			already translated).
 * \param arg1		Start of buffer range. It is ensured to be less than
 *			\a arg2.
 * \param arg2		End of buffer range
 * \param cp		Command string that will be executed by \c /bin/sh
 *
 * \return		Status Code
 * \retval SUCCESS	Command succeeded
 * \retval FAIL		Command failed. Error message has been displayed.
 */
#ifdef UNIX

int
cmd_oscmd(struct cmd_token *uct, int arg_count, unsigned long arg1, unsigned long arg2, char *cp)
{
int last_intr_flag;
int line_cnt,w;
char tmpbuf[LINE_BUFFER_SIZE];
char pipebuff[IO_BUFFER_SIZE];
extern char susp_flag;
struct undo_token *ut;
int pid;
int bidir_flag = arg_count > 0;
int input_pipe[2], output_pipe[2];
int _errno;
int status;

    PREAMBLE();

/*
 * In bidirectional piping mode, the process will read stdin from input_pipe
 */
    if(bidir_flag){
	if (pipe(input_pipe)) {
		sprintf(tmpbuf,"?Error creating PIPE: %s",error_text(errno));
		error_message(tmpbuf);
		return(FAIL);
	}/* End IF */
    }/* End IF */

/*
 * otherwise the process will not get any input and only write stdout to
 * output_pipe
 */
    if(pipe(output_pipe)){
	sprintf(tmpbuf,"?Error creating PIPE: %s",error_text(errno));
	error_message(tmpbuf);
	return(FAIL);
    }/* End IF */

/*
 * Fork to get a process to run the shell
 */
    pid = fork();
    _errno = errno;
    if(pid == 0){
        if(bidir_flag){
            close(0); dup(input_pipe[0]);
            close(1); dup(output_pipe[1]);
            close(input_pipe[0]);
            close(input_pipe[1]);
	}else{
	    close(1); dup(output_pipe[1]);
	    close(0);
	}
	close(2); /* don't use stderr */
	close(output_pipe[0]);
	close(output_pipe[1]);

	/* Clean up */
	for (w = 3; w < 256; w++)
	    close(w);

	execl("/bin/sh","sh","-c",cp,NULL);
	exit(EXIT_FAILURE);
    }/* End IF */

/*
 * Close file descriptors which are no longer needed in the parent process
 */
    if(bidir_flag) close(input_pipe[0]);
    close(output_pipe[1]);

    if(pid == -1){
    	if(bidir_flag) close(input_pipe[1]);
	close(output_pipe[0]);

	sprintf(tmpbuf,"?Error forking child process: %s",error_text(_errno));
	error_message(tmpbuf);
	return(FAIL);
    }/* End IF */

/*
 * In bidirectional mode, write buffer range to input_pipe, delete buffer range
 * and care about the Undo structures
 */
    if(bidir_flag){
    	status = buff_write(curbuf,input_pipe[1],arg1,arg2);
    	close(input_pipe[1]);
    	if(status == FAIL){
    	    close(output_pipe[0]);
    	    goto failreap;
    	}/* End If */

	ut = allocate_undo_token(uct);
	if(ut == NULL){
	    close(output_pipe[0]);
	    goto failreap;
	}/* End If */
	ut->opcode = UNDO_C_CHANGEDOT;
	ut->iarg1 = curbuf->dot;

    	status = buff_delete_with_undo(uct,curbuf,arg1,arg2-arg1);
    	if(status == FAIL){
    	    close(output_pipe[0]);
    	    goto failreap;
    	}/* End If */
    }/* End If */

/*
 * Loop reading stuff coming back from the pipe until we get an
 * EOF which means the process has finished. Update the screen
 * on newlines so the user can see what is going on.
 *
 * In unidirectional mode, process output is written to DOT, otherwise to
 * the selected buffer range (it effectively gets replaced by the process output).
 */
    ut = allocate_undo_token(uct);
    if(ut == NULL){
    	close(output_pipe[0]);
    	goto failreap;
    }/* End If */
    ut->opcode = UNDO_C_DELETE;
    ut->carg1 = (char *)curbuf;
    ut->iarg1 = bidir_flag ? arg1 : curbuf->dot;
    ut->iarg2 = 0;

    last_intr_flag = intr_flag;
    line_cnt = 0;

    while((w = read(output_pipe[0],pipebuff,sizeof(pipebuff))) > 0){
    	char *p = pipebuff;

	status = buff_insert(curbuf,ut->iarg1 + ut->iarg2,pipebuff,w);
	if(status == FAIL){
	    close(output_pipe[0]);
    	    goto failreap;
        }/* End If */

	if(intr_flag != last_intr_flag){
	    kill(pid,SIGINT);
	    last_intr_flag = intr_flag;
	}/* End IF */

	if(susp_flag){
	    cmd_pause();
	}/* End IF */

	ut->iarg2 += w;

	while(w--){
	    if(intr_flag != last_intr_flag) break;
	    if(*p++ == '\n' && line_cnt++ > (term_lines / 4)){
		line_cnt = 0;
		if(tty_input_pending()) break;
		screen_format_windows();
		screen_refresh();
	    }/* End IF */
	}/* End While */
    }/* End While */
    _errno = errno;
    close(output_pipe[0]);
    if(w < 0){
    	sprintf(tmpbuf,"?Error reading from child process <%d>: %s",
    		pid,error_text(_errno));
	error_message(tmpbuf);
	goto failreap;
    }/* End If */
 
/*
 * The wait here is required so that the process we forked doesn't
 * stay around as a zombie. Also the command will only be Successful
 * if the process indicated Success.
 */
    if(waitpid(pid,&status,0) == -1){
    	sprintf(tmpbuf,"?Error reaping child process <%d>: %s",
    		pid,error_text(errno));
	error_message(tmpbuf);
	kill(pid,SIGKILL);
	return(FAIL);
    }/* End If */
    if (!WIFEXITED(status)){
    	sprintf(tmpbuf,"?Child process <%d> terminated abnormally",
    		pid);
	error_message(tmpbuf);
	return(FAIL);
    }/* End If */
    if(WEXITSTATUS(status) != EXIT_SUCCESS){
    	sprintf(tmpbuf,"?Child process <%d> terminated with status %d",
    		pid,WEXITSTATUS(status));
	error_message(tmpbuf);
	return(FAIL);
    }/* End If */

    return(SUCCESS);

/*
 * In case of errors, the process gets killed and is reaped.
 * NOTE: Error message has already been displayed
 */

failreap:
    kill(pid,SIGKILL);
    waitpid(pid,NULL,0);
    return(FAIL);

}/* End Routine */

/* END OF UNIX CONDITIONAL CODE */

#else /* UNIX */

/*
 * This version will work on any/most systems with a standard C library
 * as it relies on temporary files.
 *
 * FIXME: We cannot redirect stderr in MS-DOS.
 */
int
cmd_oscmd(struct cmd_token *uct, int arg_count, unsigned long arg1, unsigned long arg2, char *cp)
{
char tmpbuf[LINE_BUFFER_SIZE];
char pipebuff[IO_BUFFER_SIZE];
struct undo_token *ut;
int bidir_flag = arg_count > 0;
int status,w;
char cmdline[TECO_FILENAME_TOTAL_LENGTH];
int input_fd = -1, output_fd = -1;
char input_filename[L_tmpnam] = "", output_filename[L_tmpnam] = "";
int rc = FAIL;

    PREAMBLE();

    snprintf(cmdline,sizeof(cmdline),"%s >%s <",
             cp,tmpnam(output_filename));

    if(bidir_flag){
	strcat(cmdline,tmpnam(input_filename));
	input_fd = open(input_filename,O_WRONLY|O_CREAT|O_TRUNC);
	if(input_fd < 0){
	    sprintf(tmpbuf,"?Error opening input temporary file %s: %s",
	            input_filename,error_text(errno));
	    error_message(tmpbuf);
	    goto cleanup;
	}/* End IF */

/*
 * In bidirectional mode, write buffer range to input_pipe, delete buffer range
 * and care about the Undo structures
 */
	status = buff_write(curbuf,input_fd,arg1,arg2);
	if(status == FAIL) goto cleanup;

	ut = allocate_undo_token(uct);
	if(ut == NULL) goto cleanup;
	ut->opcode = UNDO_C_CHANGEDOT;
	ut->iarg1 = curbuf->dot;

	status = buff_delete_with_undo(uct,curbuf,arg1,arg2-arg1);
	if(status == FAIL) goto cleanup;

	close(input_fd);
	input_fd = -1;
    }/* End IF */
#ifdef MSDOS
    else{
	strcat(cmdline,"NUL");
    }/*End IF*/

    _heapshrink();
#endif
    errno = 0;
    status = system(cmdline);
    if(errno){
    	sprintf(tmpbuf,"?Error spawning external process: %s",
    		error_text(errno));
	error_message(tmpbuf);
	goto cleanup;
    }/* End If */
    if(status){
    	sprintf(tmpbuf,"?External process failed: %d",status);
	error_message(tmpbuf);
	goto cleanup;
    }/* End If */

    output_fd = open(output_filename,O_RDONLY);
    if(output_fd < 0){
	sprintf(tmpbuf,"?Error opening output temporary file %s: %s",
	        output_filename,error_text(errno));
	error_message(tmpbuf);
	goto cleanup;
    }/* End IF */

/*
 * Loop reading stuff coming back from the pipe until we get an
 * EOF which means the process has finished. Update the screen
 * on newlines so the user can see what is going on.
 *
 * In unidirectional mode, process output is written to DOT, otherwise to
 * the selected buffer range (it effectively gets replaced by the process output).
 */
    ut = allocate_undo_token(uct);
    if(ut == NULL) goto cleanup;
    ut->opcode = UNDO_C_DELETE;
    ut->carg1 = (char *)curbuf;
    ut->iarg1 = bidir_flag ? arg1 : curbuf->dot;
    ut->iarg2 = 0;

    while((w = read(output_fd,pipebuff,sizeof(pipebuff))) > 0){
	status = buff_insert(curbuf,ut->iarg1 + ut->iarg2,pipebuff,w);
	if(status == FAIL) goto cleanup;

	ut->iarg2 += w;
    }/* End While */
    if(w < 0){
    	sprintf(tmpbuf,"?Error reading from output file %s: %s",
    		output_filename,error_text(errno));
	error_message(tmpbuf);
	goto cleanup;
    }/* End If */

    rc = SUCCESS;

cleanup:
    if(input_fd < 0) close(input_fd);
    if(*input_filename) unlink(input_filename);
    if(output_fd < 0) close(output_fd);
    if(*output_filename) unlink(output_filename);
    return(rc);
}/* End Routine */

#endif /* !UNIX */

/* END OF UNIX CONDITIONAL CODE */



/**
 * \brief Loads buffer name into q-register *
 *
 * This routine is called by a command operating on the text
 * portion of a q-register if the specified q-register is *.
 * In this case, we load the name of the current edit buffer
 * into it so the user can easilly access it.
 */
void
load_qname_register()
{
register struct buff_header *hbp;
register struct buff_header *qbp;

    PREAMBLE();

    hbp = curbuf;
    qbp = buff_qfind('*',1);

    if(qbp->zee > 0){
	buff_delete(qbp,0,qbp->zee);
    }/* End IF */

    buff_insert(qbp,qbp->dot,hbp->name,strlen(hbp->name));

}/* End Routine */

/**
 * \brief Change the name of the specified edit buffer
 *
 * This routine is called by parser exec routines which have
 * loaded q-register *. Since the text portion of this q-register
 * is the buffer name, it has the effect of changing the name
 * of the buffer.
 */
int
rename_edit_buffer(
					struct buff_header *hbp,
					char *new_name,
					struct cmd_token *uct )
{
register int i;
register struct undo_token *ut;
int length;

    PREAMBLE();

    ut = allocate_undo_token(uct);
    if(ut == NULL) return(FAIL);
    ut->opcode = UNDO_C_RENAME_BUFFER;
    ut->carg1 = hbp->name;

    length = strlen(new_name);
    hbp->name = tec_alloc(TYPE_C_CBUFF,length+1);
    if(hbp->name == NULL) return(FAIL);
    for(i = 0; i < length; i++){
	hbp->name[i] = new_name[i];
    }/* End FOR */
    hbp->name[length] = '\0';

    buff_switch(hbp,0);
    return(SUCCESS);

}/* End Routine */



/**
 * \brief Set runtime options via the EJ command
 *
 * This routine is called when the user sets runtime variables using
 * the EJ command. Although the command is a classic TECO command, the
 * actual values are specific to Video TECO
 */
int
cmd_setoptions(
				long arg1,
				long arg2,
				struct undo_token *uct )
{
struct undo_token fake_token;
extern int tab_width;

    PREAMBLE();

    if(arg1 < SETOPTION_MIN_OPTION || arg1 > SETOPTION_MAX_OPTION){
	error_message("EJ option selector out of range");
	return(FAIL);
    }/* End IF */

    if(!uct) uct = &fake_token;

    switch(arg1){
	case SETOPTION_ALTERNATE_ESCAPE_CHAR:
	    uct->iarg1 = arg1;
	    uct->iarg2 = alternate_escape_character;
	    alternate_escape_character = arg2;
	    break;
	case SETOPTION_SCREEN_WIDTH:
	    uct->iarg1 = arg1;
	    uct->iarg2 = forced_width;
	    forced_width = arg2;
	    screen_resize();
	    break;
	case SETOPTION_SCREEN_HEIGHT:
	    if(arg2 != 0 && arg2 < 3){
		error_message("TECO requires a larger window");
		return(FAIL);
	    }/* End IF */
	    uct->iarg1 = arg1;
	    uct->iarg2 = forced_height;
	    forced_height = arg2;
	    screen_resize();
	    break;
	case SETOPTION_ALTERNATE_DELETE_CHAR:
	    uct->iarg1 = arg1;
	    uct->iarg2 = alternate_delete_character;
	    alternate_delete_character = arg2;
	    break;
	case SETOPTION_FORCE_8_BIT_CHARS:
	    uct->iarg1 = arg1;
	    uct->iarg2 = eight_bit_output_flag;
	    eight_bit_output_flag = arg2 ? YES : NO;
	    screen_reformat_windows();
	    break;
	case SETOPTION_TAB_WIDTH:
	    uct->iarg1 = arg1;
	    uct->iarg2 = tab_width;
	    tab_width = arg2 >= 1 && arg2 <= 16 ? arg2 : NORMAL_TAB_WIDTH;
	    screen_reformat_windows();
	    break;
	case SETOPTION_HIDE_CR_CHARS:
	    uct->iarg1 = arg1;
	    uct->iarg2 = hide_cr_flag;
	    hide_cr_flag = arg2 ? YES : NO;
	    screen_reformat_windows();
	    break;

    }/* End Switch */

    return(SUCCESS);

}/* End Routine */



/**
 * \brief Perform UNIX tags function
 *
 * This routine performs various UNIX tags functions. The following
 * functions are supported:
 *
 * <table>
 *	<tr>
 *		<td>\b arg1</td><td>\b arg2</td><td>\b string</td>
 *		<td>\b Description</td>
 *	</tr><tr>
 *		<td>0 \e (optional)</td><td>-</td><td>Tags file name</td>
 *		<td>Load tags file given in \a string.
 *		    It can be either in \e etags or \e ctags format.</td>
 *	</tr><tr>
 *		<td>1</td><td>\e optional (default: 0)</td><td>Symbol name</td>
 *		<td>Find tag entry whose symbol matches \a string,
 *		    skipping \a arg2 matching entries</td>
 *	</tr><tr>
 *		<td>2</td><td>-</td><td>-</td>
 *		<td>Check whether a tags file has been loaded</td>
 *	</tr><tr>
 *		<td>3</td><td>-</td><td>-</td>
 *		<td>Insert current tag entry's target file name into the edit buffer</td>
 *	</tr><tr>
 *		<td>4</td><td>-</td><td>-</td>
 *		<td>Insert current tag entry's search string into the edit buffer</td>
 *	</tr><tr>
 *		<td>5</td><td>-</td><td>-</td>
 *		<td>Insert current tag entry's line number into the edit buffer</td>
 *	</tr><tr>
 *		<td>6</td><td>-</td><td>-</td>
 *		<td>Dump entire tag database into the edit buffer</td>
 *	</tr>
 * </table>
 *
 * \param uct		Command Token
 * \param arg_count	Number of arguments: 0 \e none, 1 \a arg1 given,
 *			2 \a arg1 and \a arg2 given
 * \param arg1		Operation identifier
 * \param arg2		Operation parameter if required, depending on \a arg1
 * \param string	Tags file name or symbol depending on \a arg1
 *
 * \return		Status code
 * \retval SUCCESS	Operation was successful
 * \retval FAIL		Operation failed, an error message has been displayed
 */
int
cmd_tags(
			struct cmd_token *uct,
			int arg_count,
			int arg1,
			int arg2,
			char *string )
{
register struct undo_token *ut;
register struct tags *old_tags;
register struct tags *new_tags;
register struct tagent *tep = NULL;
int hashval,skip_cnt;

    PREAMBLE();

    if(arg_count == 0) arg1 = 0;
    if(arg_count < 2) arg2 = 0;

    if(arg1 < TAGS_MIN_OPTION || arg1 > TAGS_MAX_OPTION){
	error_message("FT option selector out of range");
	return(FAIL);
    }/* End IF */

    switch(arg1){
	case TAGS_LOAD_TAGS_FILE:
	    old_tags = current_tags;
	    new_tags = tag_load_file(string);
	    if(new_tags == NULL) return(FAIL);
	    current_tags = new_tags;
	    ut = allocate_undo_token(uct);
	    if(ut == NULL) return(FAIL);
	    ut->opcode = UNDO_C_LOAD_TAGS;
	    ut->carg1 = (char *)old_tags;
	    break;
	case TAGS_SEARCH_TAGS_FILE:
	    if(current_tags == NULL) return(FAIL);
	    hashval = tag_calc_hash(string);
	    tep = current_tags->tagents[hashval];
	    skip_cnt = arg2;
	    while(tep){
		if(strcmp(string,tep->te_symbol) == 0){
		    if(skip_cnt-- <= 0){
			if(current_tags->current_entry != tep){
		    	    ut = allocate_undo_token(uct);
		    	    if(ut == NULL) return(FAIL);
		    	    ut->opcode = UNDO_C_SELECT_TAGS;
		    	    ut->carg1 = (char *)current_tags->current_entry;
			}/* End IF */
			current_tags->current_entry = tep;
			return(SUCCESS);
		    }/* End IF */
		}/* End IF */
		tep = tep->te_next;
	    }/* End While */
	    return(FAIL);
	case TAGS_TEST_FOR_LOADED_TAGS:
	    if(current_tags){
		return(SUCCESS);
	    }/* End IF */
	    return(FAIL);
	case TAGS_INSERT_TARGET_FILE:
	    if(current_tags->current_entry){
		if(current_tags->current_entry->te_filename){
		    buff_insert_with_undo(
			uct,
			curbuf,
			curbuf->dot,
			current_tags->current_entry->te_filename,
			strlen(current_tags->current_entry->te_filename)
		    );
		    return(SUCCESS);
		}/* End IF */
	    }/* End IF */
	    return(FAIL);
	case TAGS_INSERT_SEARCH_STRING:
	    if(current_tags->current_entry){
		if(current_tags->current_entry->te_search_string){
		    buff_insert_with_undo(
			uct,
			curbuf,
			curbuf->dot,
			current_tags->current_entry->te_search_string,
			strlen(current_tags->current_entry->te_search_string)
		    );
		    return(SUCCESS);
		}/* End IF */
	    }/* End IF */
	    return(FAIL);
	case TAGS_INSERT_LINENO:
	    if(current_tags->current_entry){
		if(current_tags->current_entry->te_lineno){
		    buff_insert_with_undo(
			uct,
			curbuf,
			curbuf->dot,
			current_tags->current_entry->te_lineno,
			strlen(current_tags->current_entry->te_lineno)
		    );
		    return(SUCCESS);
		}/* End IF */
	    }/* End IF */
	    return(FAIL);
	case TAGS_INSERT_ALL:
	    if(current_tags){
		tag_dump_database(current_tags,uct);
		return(SUCCESS);
	    }/* End IF */
	    return(FAIL);
    }/* End Switch */

    return(SUCCESS);

}/* End Routine */

/**
 * \brief Read in a tags file
 *
 * This is a huge monolithic nasty routine which reads either VI (\e ctags) or
 * EMACS (\e etags) tags files, and builds an internal representation.
 *
 * \param string	File name of tags file to load
 *
 * \return		Internal representation of tags database
 * \retval NULL		An error occurred, an error message has been displayed
 */
struct tags *
tag_load_file( char *string )
{
FILE *fd = NULL;
register struct tags *tp = NULL;
register struct tagent *tep = NULL;
register char *cp;
char *outcp = NULL;
char *filename_cp = NULL;
int state = 0;
int hashval;
int c;
char emacs_flag = NO;
char comma_seen = 0;
char tmp_buffer[LINE_BUFFER_SIZE];
char tmp_search_string[LINE_BUFFER_SIZE];
char tmp_filename[LINE_BUFFER_SIZE];
char tmp_symbol[LINE_BUFFER_SIZE];
char tmp_number[LINE_BUFFER_SIZE];
char tmp_message[LINE_BUFFER_SIZE];
int line = 0;

    fd = fopen(string,"r");
    if(fd == NULL){
	sprintf(
	    tmp_message,
	    "Could not open TAGS file <%s>: %s",
	    string,
	    error_text(errno)
	);
	error_message(tmp_message);
	return(NULL);
    }/* End IF */

    tp = (struct tags *)tec_alloc(TYPE_C_TAGS,sizeof(struct tags));
    if(tp == NULL) return(NULL);
    bzero((char *)tp,sizeof(struct tags));

/*
 * Loop reading tag entries
 */
    while(1){
	cp = tmp_buffer;
	bzero(cp,sizeof(tmp_buffer));
	if(fgets(cp,sizeof(tmp_buffer),fd) == NULL) break;
	line++;
/*
 * Ok, we have the start of an entry. Check for continuation.
 */
	while(cp[0]){
	    if(cp[0] == '\n'){
		cp[1] = '\0';
		break;
	    }/* End IF */
	    if(cp[0] == '\\' && cp[1] == '\n'){
		if(fgets(cp,sizeof(tmp_buffer)-(cp-tmp_buffer),fd) == NULL){
		    cp[0] = '\n'; cp[1] = '\0';
		}/* End IF */
		line++;
	    }/* End IF */
	    cp++;
	}/* End While */

/*
 * First step is to get the name of the symbol so that we can obtain the
 * hash value
 */
	cp = tmp_buffer;

	if(emacs_flag == NO) state = 0;

	while(*cp){
	    if(state < 0) break;
	    switch(state){
/*
 * Skip over symbol name until we hit whitespace. Then calculate the hash
 * value for that symbol, allocate the tag entry structure, and copy the
 * symbol name into it.
 */
		case 0:
		    if(cp[0] == '\f' && cp[1] == '\n'){
			emacs_flag = YES;
			state = 100;
			continue;
		    }/* End IF */

		    if(isspace((int)*cp)){
			c = *cp;
			*cp = '\0';
			hashval = tag_calc_hash(tmp_buffer);
			tep = (struct tagent *)tec_alloc(
			    TYPE_C_TAGENT,
			    sizeof(struct tagent)
			);
			if(tep == NULL) goto err;
			bzero((char *)tep,sizeof(*tep));

			tep->te_next = tp->tagents[hashval];
			tp->tagents[hashval] = tep;

			tep->te_symbol = (char *)tec_alloc(
			    TYPE_C_TAGSTR,
			    strlen(tmp_buffer) + 1
			);
			if(tep->te_symbol == NULL) goto err;
			strcpy(tep->te_symbol,tmp_buffer);

			*cp = c;
			state++;
		    }/* End IF */
		    cp++;
		    continue;
/*
 * Skip any amount of whitespace which seperates the symbol from the
 * filename.
 */
		case 1:
		    if(!isspace((int)*cp)){
			filename_cp = cp;
			state++;
			continue;
		    }/* End IF */
		    cp++;
		    continue;
/*
 * Find the length of the filename portion and copy it to the tagent
 */
		case 2:
		    if(isspace((int)*cp)){
			c = *cp;
			*cp = '\0';

			tep->te_filename = (char *)tec_alloc(
			    TYPE_C_TAGSTR,
			    strlen(filename_cp) + 1
			);
			if(tep->te_filename == NULL) goto err;
			strcpy(tep->te_filename,filename_cp);

			*cp = c;
			state++;
		    }/* End IF */
		    cp++;
		    continue;
/*
 * Skip any amount of whitespace which seperates the filename from the
 * search string
 */
		case 3:
		    if(!isspace((int)*cp)){
			state++;
			continue;
		    }/* End IF */
		    cp++;
		    continue;

/*
 * The next character should be a slash to begin the search string
 */
		case 4:
		    if(*cp == '/'){
			outcp = tmp_search_string;
			*outcp = '\0';
			state++;
		    }
		    cp++;
		    continue;
/*
 * Find the length of the search string portion and copy it to the tagent
 */
		case 5:
		    if(*cp == '/'){
			*outcp = '\0';

			tep->te_search_string = (char *)tec_alloc(
			    TYPE_C_TAGSTR,
			    strlen(tmp_search_string) + 1
			);
			if(tep->te_search_string == NULL) goto err;
			strcpy(tep->te_search_string,tmp_search_string);

			state = -1;
			continue;
		    }/* End IF */
		    if(*cp == '\\'){
			state = state + 1;
			cp++;
			continue;
		    }/* End IF */
		    *outcp++ = *cp++;
		    continue;

/*
 * Here on a backquote character. Just skip it and pass through the quoted
 * character without testing it for termination of the search string.
 */
		case 6:
		    *outcp++ = *cp++;
		    state = state - 1;
		    continue;

/*
 * Here if we encounter a form-feed. This is a giveaway that we have an
 * emacs style tags file, so we have to parse it differently.
 */
		case 100:
		    if(cp[0] != '\f' || cp[1] != '\n'){
			cp++;
			continue;
		    }/* End IF */
		    cp += 2;
		    state = 101;
		    outcp = tmp_filename;
		    comma_seen = 0;
		    continue;
/*
 * Here we get the name of the source file
 */
		case 101:
		    if(*cp == '\n'){
			if(comma_seen == 0){
			    state = 100;
			    continue;
			}/* End IF */
			outcp--;
			while(*outcp != ',') outcp--;
			*outcp = '\0';
			cp++;
			state = 102;
			outcp = tmp_symbol;
			*outcp++ = 127;
			continue;
		    }/* End IF */

		    if(*cp == ',') comma_seen = 1;
		    *outcp++ = *cp++;
		    continue;
/*
 * Now we get symbol names
 */
		case 102:
		    if(*cp == '\f'){
			state = 100;
			continue;
		    }/* End IF */

		    if(*cp != 127){
			*outcp++ = *cp++;
			continue;
		    }/* End IF */
/*
 * Here on a 127 code which terminates the symbol name. First eat back to
 * the first symbol character.
 */
		    while((c = outcp[-1])){
			if(c == 127) break;
			if(
			    isalnum(c) ||
			    isdigit(c) ||
			    c == '_' ||
			    c == '$' ||
			    c == '.'
			){
			    *outcp = '\0';
			    break;
			}/* End IF */
			outcp--;
		    }/* End While */
/*
 * Now go back until we find the first non-symbol character.
 */
		    while((c = outcp[-1])){
			if(c == 127) break;
			if(
			    isalnum(c) ||
			    isdigit(c) ||
			    c == '_' ||
			    c == '$' ||
			    c == '.'
			){
			    outcp--;
			    continue;
			}/* End IF */
			break;
		    }/* End While */

		    hashval = tag_calc_hash(outcp);
		    tep = (struct tagent *)tec_alloc(
			TYPE_C_TAGENT,
			sizeof(struct tagent)
		    );
		    if(tep == NULL) goto err;
		    bzero((char *)tep,sizeof(*tep));

		    tep->te_next = tp->tagents[hashval];
		    tp->tagents[hashval] = tep;

		    tep->te_symbol = (char *)tec_alloc(
			TYPE_C_TAGSTR,
			strlen(outcp) + 1
		    );
		    if(tep->te_symbol == NULL) goto err;
		    strcpy(tep->te_symbol,outcp);
		    tep->te_filename = (char *)tec_alloc(
			TYPE_C_TAGSTR,
			strlen(tmp_filename) + 1
		    );
		    if(tep->te_filename == NULL) goto err;
		    strcpy(tep->te_filename,tmp_filename);

		    state = 103;
		    continue;

/*
 * Eat until we reach a number, which is the line number
 */
		case 103:
		    if(isdigit((int)*cp)){
			state = 104;
			outcp = tmp_number;
			continue;
		    }/* End IF */

		    if(*cp == '\f'){
			state = 100;
			continue;
		    }/* End IF */

		    cp++;
		    continue;
/*
 * Read in the line number
 */
		case 104:
		    if(isdigit((int)*cp)){
			*outcp++ = *cp++;
			continue;
		    }/* End IF */

		    *outcp++ = '\0';
		    tep->te_lineno = (char *)tec_alloc(
			TYPE_C_TAGSTR,
			strlen(tmp_number) + 1
		    );
		    if(tep->te_lineno == NULL) goto err;
		    strcpy(tep->te_lineno,tmp_number);

		    state = 105;
		    continue;
/*
 * Eat until end of line
 */
		case 105:
		    if(*cp == '\n'){
			cp++;
			state = 102;
			outcp = tmp_symbol;
			continue;
		    }/* End IF */
		    if(*cp == '\f'){
			state = 100;
			continue;
		    }/* End IF */
		    cp++;
		    continue;
	    }/* End Switch */
	}/* End While */

    }/* End While */

    fclose(fd);
    return(tp);

err:

    tag_free_struct(tp);
    if(fd) fclose(fd);
    return(NULL);

}/* End Routine */

/**
 * \brief Release memory associated with a tags database
 *
 * \param tp Tags database
 */
void
tag_free_struct( struct tags *tp )
{
register struct tagent **tepp;
register struct tagent *tep;
register int i;

    if(tp == NULL) return;

    for(i = 0, tepp = &tp->tagents[0]; (unsigned)i < ELEMENTS(tp->tagents); i++,tepp++){
	while((tep = *tepp)){
	    *tepp = tep->te_next;

	    if(tep->te_symbol){
		tec_release(TYPE_C_TAGSTR,tep->te_symbol);
	    }/* End IF */

	    if(tep->te_filename){
		tec_release(TYPE_C_TAGSTR,tep->te_filename);
	    }/* End IF */

	    if(tep->te_search_string){
		tec_release(TYPE_C_TAGSTR,tep->te_search_string);
	    }/* End IF */

	    tec_release(TYPE_C_TAGENT,(char *)tep);

	}/* End While */

    }/* End FOR */

    tec_release(TYPE_C_TAGS,(char *)tp);

}/* End Routine */

/**
 * \brief Hash function for tag entries of a tags database
 *
 * \param string	Tag entry symbol name
 *
 * \return		Hash value
 */
int
tag_calc_hash( char *string )
{
register int hash = 0;
register int c;
register int shift = 0;

    while((c = *string++)){
	shift = shift == 8 ? 0 : shift + 1;
	hash += c << shift;
    }/* End While */

    return(hash % TAG_HASH_ENTRIES);

}/* End Routine */

/**
 * \brief Dump tags database into edit buffer
 *
 * \param tp	Tags database
 * \param uct	Command Token
 */
void
tag_dump_database( struct tags *tp, struct cmd_token *uct )
{
register struct tagent **tepp;
register struct tagent *tep;
register int i;
char tmp_output[LINE_BUFFER_SIZE];

    if(tp == NULL) return;

    for(i = 0, tepp = &tp->tagents[0]; (unsigned)i < ELEMENTS(tp->tagents); i++,tepp++){
	tep = *tepp;
	while(tep){

	    sprintf(
		tmp_output,
		"sym <%s> hash %d file <%s> line <%s> search <%s>\n",
		tep->te_symbol ? tep->te_symbol : "",
		tep->te_symbol ? tag_calc_hash(tep->te_symbol) : 0,
		tep->te_filename ? tep->te_filename : "",
		tep->te_lineno ? tep->te_lineno : "",
		tep->te_search_string ? tep->te_search_string : ""
	    );

	    buff_insert_with_undo(
		uct,
		curbuf,
		curbuf->dot,
		tmp_output,
		strlen(tmp_output)
	    );

	    tep = tep->te_next;

	}/* End While */
    }/* End FOR */

    return;

}/* End Routine */