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
|
char *tecbuf_c_version = "tecbuf.c: $Revision: 1.4 $";
struct buff_header *buff_create( char *name, char internal_flag );
/*
* $Date: 2007/12/26 13:28:30 $
* $Source: /cvsroot/videoteco/videoteco/tecbuf.c,v $
* $Revision: 1.4 $
* $Locker: $
*/
/**
* \file tecbuf.c
* \brief Subroutines to handle the edit buffers
*/
/*
* 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"
static const char rcs_date[] = AUTO_DATE;
/*
* Global Storage is defined here for lack of a better place.
* First off are all the flags.
*/
/*
* Global Variables
*/
struct buff_header *buffer_headers;
struct buff_header *curbuf;
struct buff_header *qregister_push_down_list;
/*
* Global structures
*/
struct buff_line *line_buffer_lookaside_list = NULL;
/*
* Forward References
*/
struct buff_line *allocate_line_buffer(int);
void movc3(char *,char *,int);
void buff_buffer_map( void );
unsigned int stringHash( char *str );
extern FILE *teco_debug_log;
extern int term_columns;
extern struct window *curwin;
extern char checkpoint_modified;
/**
* \brief Find the named buffer
*
* This routine is called with the name of the buffer that we want to
* find. It searches all the buffer headers until it finds the name.
* It will either return the address of the buffer header, or null if
* it could not find one with the proper name.
*/
struct buff_header *
buff_find( char *name )
{
register struct buff_header *bp;
register char *cp1,*cp2;
unsigned int hash = stringHash( name );
PREAMBLE();
for(bp = buffer_headers; bp != NULL; bp = bp->next_header){
if( hash != bp->buf_hash )
{
// printf("skipping because hash 0x%08x != 0x%08x %s %s\n",
// hash, bp->buf_hash, name, bp->name);
continue;
}
// printf("hash match! %s %s\n",name,bp->name);
/*
* This loop implements the equivalent of strcmp, except that in the
* case of VMS and MS-DOS, it is case insensitive.
*/
for(cp1 = name, cp2 = bp->name;;cp1++,cp2++){
#if defined(VMS) || defined(MSDOS)
if(UPCASE(*cp1) != UPCASE(*cp2)) break;
#else
if(*cp1 != *cp2) break;
#endif
if(*cp1 == '\0') return(bp);
}/* End FOR */
}/* End FOR */
return(NULL);
}/* End Routine */
/**
* \brief Find the named Q register
*
* This routine is called with the name of the Q register that we
* want to find. It constructs the internal name of the Q register
* and then calls buff_find to find the buffer. If the buffer is not
* found and the create flag is set, we call buff_create to cause
* the Q register to be created.
*/
struct buff_header *
buff_qfind( char name, char create_flag )
{
register struct buff_header *hbp;
register int i;
char tmp_buffer[LINE_BUFFER_SIZE],tmp_message[LINE_BUFFER_SIZE];
//struct buff_header *buff_create();
PREAMBLE();
(void) strcpy(tmp_buffer,TECO_INTERNAL_BUFFER_NAME);
i = strlen(tmp_buffer);
name = UPCASE((int)name);
tmp_buffer[i] = name; tmp_buffer[i+1] = '\0';
hbp = buff_find(tmp_buffer);
if(hbp == NULL){
if(create_flag){
hbp = buff_create(tmp_buffer,1);
if(hbp == (struct buff_header *)NULL){
sprintf(tmp_message,"?Cannot create Q-register %c",name);
error_message(tmp_message);
return((struct buff_header *)NULL);
}/* End IF */
}/* End IF */
}/* End IF */
return(hbp);
}/* End Routine */
/**
* \brief Create a new buffer
*
* This routine is called when we need to create a new buffer. The caller
* should have already verified that this doesn't already exist. The
* routine returns the address of the new buffer, or NULL if there is a
* problem of some sort.
*/
struct buff_header *
buff_create( char *name, char internal_flag )
{
register struct buff_header *bp;
register struct buff_header *obp;
register struct buff_line *lbp;
register int i = 0;
PREAMBLE();
if(buff_find(name)) return(NULL);
/*
* Create the buffer header itself.
*/
bp = (struct buff_header *)tec_alloc(
TYPE_C_BHDR,
sizeof(struct buff_header)
);
if(bp == NULL) return(NULL);
memset(bp,0,sizeof(*bp));
MAGIC_UPDATE(bp, MAGIC_BUFFER);
bp->buf_hash = stringHash( name );
bp->name = tec_alloc(TYPE_C_CBUFF,strlen(name)+1);
if(bp->name == NULL){
MAGIC_UPDATE(bp, 0);
tec_release(TYPE_C_BHDR,(char *)bp);
return(NULL);
}/* End IF */
(void) strcpy(bp->name,name);
obp = buffer_headers;
if(obp) i = obp->buffer_number;
while(obp){
if(internal_flag){
if(obp->buffer_number < i) i = obp->buffer_number;
}
else {
if(obp->buffer_number > i) i = obp->buffer_number;
}
obp = obp->next_header;
}/* End While */
if(internal_flag) bp->buffer_number = i - 1;
else bp->buffer_number = i + 1;
/*
* Create the first line buffer structure
*/
lbp = allocate_line_buffer(1);
if(lbp == NULL){
MAGIC_UPDATE(bp, 0);
tec_release(TYPE_C_CBUFF,bp->name);
tec_release(TYPE_C_BHDR,(char *)bp);
return((struct buff_header *)NULL);
}/* End IF */
bp->first_line = lbp;
/*
* Link it into the list of buffer headers, in order by buffer number
*/
obp = buffer_headers;
while(obp){
if(obp->next_header == NULL) break;
if(obp->next_header->buffer_number > bp->buffer_number) break;
obp = obp->next_header;
}/* End While */
if(obp == NULL || buffer_headers->buffer_number > bp->buffer_number){
bp->next_header = buffer_headers;
buffer_headers = bp;
}/* End IF */
else {
bp->next_header = obp->next_header;
obp->next_header = bp;
}/* End Else */
return(bp);
}/* End Routine */
/**
* \brief Make a duplicate of a buffer
*
* This routine is called to duplicate the specified buffer and return
* a pointer to the duplicate. The current requirement for this is for
* the push Q register command ('[').
*/
struct buff_header *
buff_duplicate( struct buff_header *sbp )
{
register struct buff_header *dbp;
register struct buff_line *slp;
register struct buff_line *dlp;
PREAMBLE();
/*
* Create the buffer header itself.
*/
dbp = (struct buff_header *)
tec_alloc(TYPE_C_BHDR,sizeof(struct buff_header));
if(dbp == NULL){
return((struct buff_header *)NULL);
}/* End IF */
dbp->name = tec_alloc(TYPE_C_CBUFF,strlen(sbp->name)+1);
if(dbp->name == NULL){
tec_release(TYPE_C_BHDR,(char *)dbp);
return((struct buff_header *)NULL);
}/* End IF */
(void) strcpy(dbp->name,sbp->name);
MAGIC_UPDATE(dbp, MAGIC_BUFFER);
dbp->ismodified = sbp->ismodified;
dbp->isreadonly = sbp->isreadonly;
dbp->isbackedup = sbp->isbackedup;
dbp->dot = sbp->dot;
dbp->zee = sbp->zee;
dbp->ivalue = sbp->ivalue;
dbp->pos_cache.lbp = NULL;
dbp->pos_cache.base = 0;
dbp->first_line = NULL;
/*
* Copy the line buffer structures. If an allocation error occurs, we
* have to return all the allocated memory, and return an error.
*/
slp = sbp->first_line;
if(slp){
dbp->first_line = dlp = allocate_line_buffer(slp->byte_count);
if(dlp == NULL){
buff_destroy(dbp);
return((struct buff_header *)NULL);
}/* End IF */
movc3(slp->buffer,dlp->buffer,slp->byte_count);
dlp->byte_count = slp->byte_count;
while((slp = slp->next_line)){
dlp->next_line = allocate_line_buffer(slp->byte_count);
if(dlp->next_line == NULL){
buff_destroy(dbp);
return((struct buff_header *)NULL);
}/* End IF */
dlp->next_line->prev_line = dlp;
dlp = dlp->next_line;
movc3(slp->buffer,dlp->buffer,slp->byte_count);
dlp->byte_count = slp->byte_count;
}/* End While */
}/* End IF */
return(dbp);
}/* End Routine */
/**
* \brief Copy the specified number of bytes
*
* This routine copies 'n' bytes from the source to the
* destination.
*/
void
movc3( char *source, char *dest, int count )
{
PREAMBLE();
while(count-- > 0) *dest++ = *source++;
}/* End Routine */
/**
* \brief Delete an existing buffer
*
* This routine is called to delete an existing buffer. The routine is
* supplied the address of the buffer structure. We have to clean up
* all the lines in the buffer, all the display lines, etc.
*/
void
buff_destroy( struct buff_header *hbp )
{
register struct buff_header *bp;
register struct buff_line *lbp;
PREAMBLE();
/*
* There are certain buffers we don't allow to be destroyed
*/
if(strcmp(hbp->name,"TECO_INTERNAL-Main") == 0){
return;
}/* End IF */
/*
* Clean up all the data in the buffer first
*/
lbp = hbp->first_line;
hbp->pos_cache.lbp = NULL;
hbp->first_line = NULL;
buff_free_line_buffer_list(lbp);
/*
* Now unlink this buffer header from the header list.
* Check to see if it is at the head of the list.
*/
bp = buffer_headers;
if(bp == hbp){
buffer_headers = bp->next_header;
}/* End IF */
/*
* If not at the head of the list, we have to search the list till
* we find it's father. Then we make it's father's child be it's
* child.
*/
else {
while(bp){
if(bp->next_header == hbp){
bp->next_header = hbp->next_header;
break;
}/* End IF */
bp = bp->next_header;
}/* End While */
}/* End Else */
/*
* Now give back the storage for the name, and for the structure itself.
*/
if(hbp->name) tec_release(TYPE_C_CBUFF,(char *)hbp->name);
tec_release(TYPE_C_BHDR,(char *)hbp);
return;
}/* End Routine */
/**
* \brief Find the line structure that \a position is on
*
* This routine is called to find the \p line_buffer structure that
* the buffer position resides on.
*/
struct buff_line *
buff_find_line( struct buff_header *hbp, unsigned long position )
{
register struct buff_line *lbp;
register unsigned long i;
PREAMBLE();
if(hbp == NULL) return(NULL);
/*
* First step is to see if the current position cache in the buffer header will
* help us out. Our position must be on this line if it is to be of help to us.
*/
lbp = hbp->pos_cache.lbp;
if(lbp){
i = hbp->pos_cache.base + lbp->byte_count;
/*
* Test to see if it is a currently existing character position that is mapped
* by our position cache.
*/
if(position >= hbp->pos_cache.base && position < i){
return(lbp);
}/* End IF */
/*
* If we are inserting at the end of the buffer, the position doesn't really
* exist, but we can still resolve it if we happen to have the final line
* cached. The only case where we don't want to do this is when the line
* already has a trailing newline. Then we want to fall down into the code
* below which will recognize this and construct a new line buffer.
*/
if(position == hbp->zee && position == i){
if(lbp->byte_count == 0) return(lbp);
if(lbp->buffer[lbp->byte_count - 1] != '\n') return(lbp);
}/* End IF */
}/* End IF */
/*
* We get here if the current cached location does not help us out. In this
* case we need to hunt till we find the correct place in the buffer. We also
* update our cache to remember this.
*/
i = position;
lbp = hbp->first_line;
if(lbp == NULL){
return(NULL);
}/* End IF */
/*
* Now, even though the cache did not give us the correct line, it can still
* help us out in the search if it maps a position before the one we are
* looking for, since we can begin our search at it's base.
*/
if(hbp->pos_cache.lbp && hbp->pos_cache.base < position){
i -= hbp->pos_cache.base;
lbp = hbp->pos_cache.lbp;
}/* End IF */
while(1){
if(lbp->byte_count >= i) break;
i -= lbp->byte_count;
if(lbp->next_line == NULL){
char panic_string[LINE_BUFFER_SIZE];
sprintf(panic_string,
"buff_find_line: position %lu specified in buffer %s, z=%lu",
position,hbp->name,hbp->zee);
tec_panic(panic_string);
printf("NULL ptr %lu bytes still to go\n",i);
}/* End IF */
lbp = lbp->next_line;
}/* End While */
if(i == lbp->byte_count && i && lbp->buffer[i-1] == '\n'){
i = 0;
if(lbp->next_line == NULL){
lbp->next_line = allocate_line_buffer(1);
if(lbp->next_line == NULL) return((struct buff_line *)NULL);
lbp->next_line->prev_line = lbp;
}/* End IF */
lbp = lbp->next_line;
}/* End IF */
hbp->pos_cache.lbp = lbp;
hbp->pos_cache.base = position - i;
return(lbp);
}/* End Routine */
/**
* \brief Find offset onto line structure that \a position is on
*
* This routine is called to find the offset into the \p line_buffer of
* the specified position.
*/
int
buff_find_offset( struct buff_header *hbp,
struct buff_line *lbp, long position )
{
PREAMBLE();
if(hbp == NULL) return(-1);
/*
* First step is to see if the current position cache in the buffer header will
* help us out. Our position must be on this line if it is to be of help to us.
*/
if( lbp == NULL || hbp->pos_cache.lbp != lbp){
lbp = buff_find_line(hbp,position);
if(lbp == NULL) return(-1);
}/* End IF */
/*
* Now we simply calculate the position's offset from the line structure base.
*/
return( position - hbp->pos_cache.base );
}/* End Routine */
/**
* \brief Return the character at the specified position
*
* This routine is called to fetch the single character which is at
* the specified buffer position. This routine is generally called
* by routines which are low frequence or only handle a small number
* of characters at any one time.
*/
int
buff_contents( struct buff_header *hbp, long position )
{
register struct buff_line *lbp;
register int i;
PREAMBLE();
/*
* Insure that the specified position is legal
*/
if((unsigned long)position > hbp->zee || position < 0){
char panic_string[LINE_BUFFER_SIZE];
sprintf(panic_string,
"buff_contents: illegal position %ld specified in buffer %s",
position,hbp->name);
tec_panic(panic_string);
}/* End IF */
/*
* Call buff_find_line to find the line structure that the specified
* position resides on.
*/
lbp = buff_find_line(hbp,position);
i = buff_find_offset(hbp,lbp,position);
if(i == -1) return(-1);
return(lbp->buffer[i] & 0xFF);
}/* End Routine */
/**
* \brief Return the character and positional information
*
* This routine is called to fetch the single character which is at
* the specified buffer position. This routine is generally called
* by routines which are low frequence or only handle a small number
* of characters at any one time.
*/
int
buff_cached_contents( struct buff_header *hbp,
long position, struct position_cache *cache )
{
register struct buff_line *lbp;
register int i;
PREAMBLE();
/*
* Insure that the specified position is legal
*/
if(position < 0 || (unsigned long)position > hbp->zee){
char tmp_message[LINE_BUFFER_SIZE];
sprintf(tmp_message,"buff_contents: illegal position %lu %s Z = %lu\n",
position,hbp->name,hbp->zee);
tec_panic(tmp_message);
}/* End IF */
/*
* Call buff_find_line to find the line structure that the specified
* position resides on.
*/
lbp = buff_find_line(hbp,position);
if(lbp == NULL) return(-1);
i = buff_find_offset(hbp,lbp,position);
if(i == -1) return(-1);
/*
* If he has specified a local position cache, copy that information back to
* his local cache. It's up to him to invalidate it if the buffer gets changed.
*/
if(cache){
cache->lbp = lbp;
cache->base = i;
}/* End IF */
return(lbp->buffer[i] & 0xFF);
}/* End Routine */
/**
* \brief Routine to initialize the buffer routines
*
* This routine is called before the first buffer operations to
* initialize the buffer routines.
*/
int
buff_init()
{
register struct buff_header *hbp;
char tmp_buffer[LINE_BUFFER_SIZE];
PREAMBLE();
/*
* Create the buffer map buffer
*/
(void) strcpy(tmp_buffer,TECO_INTERNAL_BUFFER_NAME);
(void) strcat(tmp_buffer,"BUFFER-MAP");
hbp = buff_create(tmp_buffer,1);
if(hbp == NULL) return(FAIL);
hbp->buffer_number = 0;
/*
* Create a default edit buffer
*/
(void) strcpy(tmp_buffer,TECO_INTERNAL_BUFFER_NAME);
(void) strcat(tmp_buffer,"Main");
curbuf = hbp = buff_create(tmp_buffer,1);
if(hbp == NULL) return(FAIL);
return(SUCCESS);
}/* End Routine */
/**
* \brief Routine to read a file into a TECO edit buffer
*
* This routine is called to load a file into the named buffer. If the
* buffer already exists, we simply switch to it and don't modify it.
*/
int
buff_openbuffer( char *name, int buffer_number, int readonly_flag )
{
register struct buff_header *hbp = NULL;
PREAMBLE();
/*
* If no name is supplied then the buffer_number argument is significant,
* otherwise we ignore it.
*/
if(name == NULL){
hbp = buffer_headers;
while(hbp){
if(hbp->buffer_number == buffer_number){
name = hbp->name;
break;
}/* End IF */
hbp = hbp->next_header;
}/* End While */
}/* End IF */
/*
* If no name is supplied, the buffer_number argument must already exist
* or an error is declared.
*/
if(name == NULL){
error_message("?No such buffer number exists");
return(FAIL);
}/* End IF */
/*
* First determine if a buffer of that name already exists
*/
if(hbp == NULL){
hbp = buff_find(name);
}/* End IF */
if(hbp){
buff_switch(hbp,1);
return(SUCCESS);
}/* End IF */
/*
* If there is not such buffer in existence, we need to create one
*/
hbp = buff_create(name,0);
if(hbp == NULL) return(FAIL);
if(readonly_flag == YES) hbp->isreadonly = YES;
/*
* Ignore possible open error, since he may be creating a new file
*/
buff_read(hbp,name);
hbp->dot = 0;
hbp->ismodified = NO;
buff_switch(hbp,1);
return(SUCCESS);
}/* End Routine */
/**
* \brief Routine to open buffer number 'n'
*
* This routine is called with the number of a buffer which is to be
* made the 'current' edit buffer.
*/
int
buff_openbuffnum( int buffer_number, int map_flag )
{
register struct buff_header *hbp;
PREAMBLE();
/*
* Search the list of buffers for the specified buffer number
*/
hbp = buffer_headers;
while(hbp){
if(hbp->buffer_number == buffer_number){
buff_switch(hbp,map_flag);
return(SUCCESS);
}/* End IF */
hbp = hbp->next_header;
}/* End While */
return(FAIL);
}/* End Routine */
/**
* \brief Called by undo to re-create an edit buffer
*
* This routine is called to place a buffer back onto the buffer
* list.
*/
void
buff_reopenbuff( struct buff_header *bp )
{
register struct buff_header *obp;
PREAMBLE();
/*
* Make sure there isn't another buffer of this name already here. The
* way this can happen is automatically created Q-registers like the
* search string and rubout string Q-registers might be created. Since
* they don't get undone, they would stick around and then we might
* try to undo the removal of an earlier copy of one of them. This way,
* we make sure that any earlier copy replaces any more recent copy.
*/
obp = buffer_headers;
while(obp){
if(strcmp(obp->name,bp->name) == 0){
buff_destroy(obp);
break;
}/* End IF */
obp = obp->next_header;
}/* End While */
/*
* Insert this buffer in the proper place on the buffer list.
*/
obp = buffer_headers;
while(obp){
if(obp->next_header == NULL) break;
if(obp->next_header->buffer_number > bp->buffer_number) break;
obp = obp->next_header;
}/* End While */
if(obp == NULL || buffer_headers->buffer_number > bp->buffer_number){
bp->next_header = buffer_headers;
buffer_headers = bp;
}/* End IF */
else {
bp->next_header = obp->next_header;
obp->next_header = bp;
}/* End Else */
}/* End Routine */
/**
* \brief Routine to read a file into an existing TECO edit buffer
*
* This routine is called to read a file into the specified edit buffer.
* The edit buffer must already exist.
*/
int
buff_read( struct buff_header *hbp, char *name )
{
int iochan;
char tmp_message[LINE_BUFFER_SIZE];
int status;
PREAMBLE();
/*
* Attempt to open the file
*/
iochan = open(name,O_RDONLY);
if(iochan < 0){
sprintf(tmp_message,"?Cannot open <%s>: %s",name,error_text(errno));
error_message(tmp_message);
return(FAIL);
}/* End IF */
status = buff_readfd(hbp,name,iochan);
close(iochan);
return(status);
}/* End Routine */
/**
* \brief Reads the file descriptor into the specified buffer
*
* This routine is called with a buffer and a file descriptor. The
* entire contents of the file descriptor are read into the specified
* buffer.
*/
int
buff_readfd( struct buff_header *hbp, char *name, int iochan )
{
char iobuf[IO_BUFFER_SIZE];
char linebuf[IO_BUFFER_SIZE];
int linebuf_cnt;
char tmp_message[LINE_BUFFER_SIZE];
register int bcount;
register char *cp,*iop = 0;
register int i;
register struct buff_line *lbp;
struct buff_line *olbp;
unsigned long original_zee = hbp->zee;
int error_status = SUCCESS;
PREAMBLE();
/*
* Read the file and insert the characters into the buffer
*/
cp = linebuf;
linebuf_cnt = bcount = 0;
while(1){
if(bcount <= 0){
bcount = read(iochan,iobuf,sizeof(iobuf));
iop = iobuf;
if(bcount == 0) break;
if(bcount < 0){
sprintf(tmp_message,"?Error reading <%s>: %s",
name,error_text(errno));
error_message(tmp_message);
error_status = FAIL;
break;
}/* End IF */
}/* End IF */
linebuf_cnt += 1;
bcount -= 1;
if((*cp++ = *iop++) == '\n' || (unsigned)linebuf_cnt >= sizeof(linebuf)){
lbp = buff_find_line(hbp,hbp->dot);
if(lbp == NULL){
error_status = FAIL;
break;
}/* End IF */
i = buff_find_offset(hbp,lbp,hbp->dot);
if(i == -1){
error_status = FAIL;
break;
}/* End IF */
if(i == 0){
olbp = lbp;
lbp = allocate_line_buffer(linebuf_cnt);
if(lbp == NULL){
error_status = FAIL;
break;
}/* End IF */
lbp->next_line = olbp;
lbp->prev_line = olbp->prev_line;
olbp->prev_line = lbp;
if(lbp->prev_line) lbp->prev_line->next_line = lbp;
if(hbp->first_line == olbp) hbp->first_line = lbp;
movc3(linebuf,lbp->buffer,linebuf_cnt);
lbp->byte_count = linebuf_cnt;
hbp->pos_cache.lbp = lbp;
hbp->pos_cache.base = hbp->dot;
hbp->dot += linebuf_cnt;
hbp->zee += linebuf_cnt;
}/* End IF */
else {
if(buff_insert(hbp,hbp->dot,linebuf,linebuf_cnt) == FAIL){
error_status = FAIL;
break;
}/* End IF */
}/* End Else */
cp = linebuf;
linebuf_cnt = 0;
continue;
}/* End IF */
}/* End While */
if(error_status == SUCCESS && linebuf_cnt != 0){
if(buff_insert(hbp,hbp->dot,linebuf,linebuf_cnt) == FAIL){
error_status = FAIL;
}/* End IF */
}/* End IF */
/*
* If the buffer just became modified for the first time, we need to change the
* label line to reflect this.
*/
if(hbp->ismodified == NO && hbp->zee != original_zee){
if(hbp == curbuf){
if(screen_label_line(hbp,"(modified)",LABEL_C_MODIFIED) == FAIL){
error_status = FAIL;
}/* End IF */
}/* End IF */
hbp->ismodified = YES;
}/* End IF */
checkpoint_modified = YES;
return(error_status);
}/* End Routine */
/**
* \brief Write the specified buffer out to a file
*
* This routine is generally called on an EW command when the user
* wishes to write out the contents of the buffer.
*/
int
buff_write( struct buff_header *hbp, int chan, unsigned long start, unsigned long end )
{
register int bcount;
register struct buff_line *lbp;
register char *cp1,*cp2;
register int line_bcount;
char iobuf[IO_BUFFER_SIZE];
int status;
PREAMBLE();
bcount = 0;
cp2 = iobuf;
lbp = buff_find_line(hbp,start);
line_bcount = buff_find_offset(hbp,lbp,start);
cp1 = lbp->buffer + line_bcount;
line_bcount = lbp->byte_count - line_bcount;
while(start < end){
if(bcount == sizeof(iobuf)){
status = write(chan,iobuf,sizeof(iobuf));
if(status != sizeof(iobuf)){
/* NOTE: status *should* be -1, errno *should* be set */
return(FAIL);
}/* End IF */
bcount = 0;
cp2 = iobuf;
}/* End IF */
if(line_bcount == 0){
lbp = lbp->next_line;
cp1 = lbp->buffer;
line_bcount = lbp->byte_count;
}/* End IF */
*cp2++ = *cp1++;
bcount += 1;
line_bcount -= 1;
start += 1;
}/* End While */
if(bcount > 0){
status = write(chan,iobuf,(unsigned)bcount);
if(status != bcount){
/* NOTE: status *should* be -1, errno *should* be set */
return(FAIL);
}/* End IF */
}/* End IF */
return(SUCCESS);
}/* End Routine */
/**
* \brief Switch the current edit buffer
*
* This routine is called when we wish to switch the current buffer
* to some other edit buffer.
*/
int
buff_switch( struct buff_header *hbp, int map_flag )
{
char tmp_message[LINE_BUFFER_SIZE];
char *cp;
PREAMBLE();
curbuf = curwin->win_buffer = hbp;
sprintf(tmp_message,"<Buffer %d> %s",hbp->buffer_number,hbp->name);
screen_label_line(hbp,tmp_message,LABEL_C_FILENAME);
cp = "";
if(hbp->isreadonly) cp = "(READONLY)";
screen_label_line(hbp,cp,LABEL_C_READONLY);
cp = "";
if(hbp->ismodified) cp = "(modified)";
screen_label_line(hbp,cp,LABEL_C_MODIFIED);
if(hbp->buffer_number == 0 && map_flag){
buff_buffer_map();
}/* End IF */
return(SUCCESS);
}/* End Routine */
/**
* \brief Build a map of the existing buffers.
*
* This routine is called when the buffer map buffer is entered. It is
* used to fill this routine with the appropriate text.
*/
void
buff_buffer_map()
{
register struct buff_header *hbp;
char tmp_buffer[LINE_BUFFER_SIZE],padd_buffer[LINE_BUFFER_SIZE];
register int i;
int count;
register const char *cp;
int max_length;
PREAMBLE();
buff_delete(curbuf,0,curbuf->zee);
for(i = 0; (unsigned)i < sizeof(padd_buffer); i++){
padd_buffer[i] = ' ';
}/* End FOR */
padd_buffer[sizeof(padd_buffer)-1] = '\0';
max_length = 0;
for(hbp = buffer_headers; hbp != NULL ; hbp = hbp->next_header){
if(hbp->buffer_number == 0) continue;
i = strlen(hbp->name);
if(i > max_length) max_length = i;
}/* End FOR */
for(hbp = buffer_headers; hbp != NULL ; hbp = hbp->next_header){
if(hbp->buffer_number <= 0) continue;
i = max_length - strlen(hbp->name);
snprintf(tmp_buffer,sizeof(tmp_buffer),"<Buffer %-4d> %s%s %s %6lu bytes\n",
hbp->buffer_number,hbp->name,&padd_buffer[sizeof(padd_buffer)-1-i],
hbp->ismodified ? "(modified)" : " ",hbp->zee);
i = strlen(tmp_buffer);
buff_insert(curbuf,curbuf->dot,tmp_buffer,i);
}/* End FOR */
buff_insert(curbuf,curbuf->dot,"\n",1);
for(hbp = buffer_headers; hbp != NULL ; hbp = hbp->next_header){
if(hbp->buffer_number >= 0) continue;
i = max_length - strlen(hbp->name);
snprintf(tmp_buffer,sizeof(tmp_buffer),"<Buffer %-4d> %s%s %s %6lu bytes\n",
hbp->buffer_number,hbp->name,&padd_buffer[sizeof(padd_buffer)-1-i],
hbp->ismodified ? "(modified)" : " ",hbp->zee);
i = strlen(tmp_buffer);
buff_insert(curbuf,curbuf->dot,tmp_buffer,i);
}/* End FOR */
/*
* Here to tell him if any q registers are currently on the push down
* list.
*/
i = count = 0;
for(hbp = qregister_push_down_list; hbp != NULL ; hbp = hbp->next_header){
i += 1;
count += hbp->zee;
}/* End FOR */
if(i > 0){
sprintf(tmp_buffer,
"\nQ register push down list contains %d register%s, %d bytes\n",
i,(i > 1)?"s":"",count);
buff_insert(curbuf,curbuf->dot,tmp_buffer,strlen(tmp_buffer));
}/* End IF */
/*
* Insert our TITLE
*/
max_length = term_columns <= 80 ? term_columns : 80;
buff_insert(curbuf,curbuf->dot,"\n\n",2);
(void) strcpy(tmp_buffer,"Video TECO");
i = (max_length - strlen(tmp_buffer))/2;
if(i < 0) i = 0;
(void) strcat(tmp_buffer,"\n");
buff_insert(curbuf,curbuf->dot,padd_buffer,i);
buff_insert(curbuf,curbuf->dot,tmp_buffer,strlen(tmp_buffer));
(void) strcpy(tmp_buffer," by Paul Cantrell & Joyce Nishinaga");
i = (max_length - strlen(tmp_buffer))/2;
if(i < 0) i = 0;
(void) strcat(tmp_buffer,"\n");
buff_insert(curbuf,curbuf->dot,padd_buffer,i);
buff_insert(curbuf,curbuf->dot,tmp_buffer,strlen(tmp_buffer));
(void) strcpy(tmp_buffer,"Munged by Robin Haberkorn");
i = (max_length - strlen(tmp_buffer))/2;
if(i < 0) i = 0;
(void) strcat(tmp_buffer,"\n");
buff_insert(curbuf,curbuf->dot,padd_buffer,i);
buff_insert(curbuf,curbuf->dot,tmp_buffer,strlen(tmp_buffer));
#if 0
(void) strcpy(tmp_buffer,id_string);
if(tmp_buffer[0] == '%'){
tmp_buffer[0] = '\0';
cp = rcs_id;
i = 0;
if(*cp == '$'){
cp++;
while(cp[i] == "Revision: "[i]) i += 1;
cp += i;
}/* End IF */
i = 0;
while(*cp != '$' && *cp != '\0'){
tmp_buffer[i++] = *cp++;
}/* End While */
tmp_buffer[i] = '\0';
if(i <= 2) sprintf(tmp_buffer,"*%d.%d* ",VMAJOR,VMINOR);
cp = rcs_date;
i = 0;
if(*cp == '$'){
cp++;
while(cp[i] == "Date: "[i]) i += 1;
cp += i;
}/* End IF */
i = strlen(tmp_buffer);
while(*cp != '$' && *cp != '\0'){
tmp_buffer[i++] = *cp++;
}/* End While */
tmp_buffer[i] = '\0';
}/* End IF */
#endif
sprintf(tmp_buffer,"*%d.%d* ",VMAJOR,VMINOR);
cp = rcs_date;
i = 0;
if(*cp == '$'){
cp++;
while(cp[i] == "Date: "[i]) i += 1;
cp += i;
}/* End IF */
i = strlen(tmp_buffer);
while(*cp != '$' && *cp != '\0'){
tmp_buffer[i++] = *cp++;
}/* End While */
tmp_buffer[i] = '\0';
i = (max_length - strlen(tmp_buffer))/2;
if(i < 0) i = 0;
(void) strcat(tmp_buffer,"\n");
buff_insert(curbuf,curbuf->dot,padd_buffer,i);
buff_insert(curbuf,curbuf->dot,tmp_buffer,strlen(tmp_buffer));
tecmem_stats();
curbuf->dot = 0;
return;
}/* End Routine */
/**
* \brief Insert a string at current position
*
* This routine is called to insert a string into the specified buffer
* at the buffer's current location.
*/
int
buff_insert( struct buff_header *hbp, unsigned long position, char *buffer, unsigned long length )
{
struct buff_header fake_header;
struct buff_line *fake_line;
register char *cp;
PREAMBLE();
fake_line =
(struct buff_line *)tec_alloc(TYPE_C_LINE,sizeof(struct buff_line));
if(fake_line == NULL) return(FAIL);
memset(&fake_header,0,sizeof(fake_header));
fake_header.pos_cache.lbp = fake_line;
fake_header.first_line = fake_line;
memset(fake_line,0,sizeof(*fake_line));
fake_line->buffer_size = LINE_BUFFER_SIZE;
fake_line->buffer = buffer;
while(length){
cp = fake_line->buffer;
fake_line->byte_count = 0;
while(1){
fake_line->byte_count += 1;
length -= 1;
if(*cp == '\n' || length == 0){
buff_insert_from_buffer_with_undo(
NULL,
hbp,
position,
&fake_header,
0,
fake_line->byte_count
);
/*
* The following is a terrible kludge. The buff_find_line code ends up
* inserting a null line buffer after our fake line, in order to
* represent the location following the last character. If we don't
* free it, it will get lost and consume memory...
*/
if(fake_line->next_line){
buff_free_line_buffer(fake_line->next_line);
fake_line->next_line = NULL;
}/* End IF */
fake_line->buffer += fake_line->byte_count;
position += fake_line->byte_count;
break;
}/* End IF */
cp++;
}/* End While */
}/* End While */
tec_release(TYPE_C_LINE,(char *)fake_line);
return(SUCCESS);
}/* End Routine */
/**
* \brief Copy bytes from another buffer
*
* This routine copies bytes from one buffer to another, arranging
* undo capability so that we can reverse the process. Because it
* knows the lengths of each line, this should be a very efficient
* method of inserting data, and should certainly have preference
* over using the byte-at-a-time routines.
*/
int
buff_insert_from_buffer_with_undo( struct cmd_token *ct,
struct buff_header *dbp,
unsigned long dest_position,
struct buff_header *sbp,
unsigned long src_position,
size_t length )
{
register unsigned int i;
struct undo_token *ut = 0;
struct buff_line *dlbp,*sb_lbp,*se_lbp,*nlbp;
int doff,sb_off,se_off;
int newline_included_in_src_data;
size_t bytes_to_copy_from_source;
size_t bytes_required_for_line,bytes_to_allocate;
char *new_buffer;
register char *dcp,*scp;
size_t bytes_inserted_so_far = 0;
#ifdef DEBUG
char outbuf[1024];
term_goto(0,0);
term_clrtobot();
term_flush();
#endif /* DEBUG */
PREAMBLE();
/*
* Insure that the specified position is legal
*/
if(src_position > sbp->zee){
char tmp_message[LINE_BUFFER_SIZE];
sprintf(
tmp_message,
"buff_insert_from_buffer_with_undo: bad src pos %lu %s Z = %lu\n",
src_position,
sbp->name,sbp->zee
);
tec_panic(tmp_message);
}/* End IF */
if(dest_position > dbp->zee){
char tmp_message[LINE_BUFFER_SIZE];
sprintf(
tmp_message,
"buff_insert_from_buffer_with_undo: bad dest pos %lu %s Z = %lu\n",
dest_position,
dbp->name,dbp->zee
);
tec_panic(tmp_message);
}/* End IF */
if(ct){
ut = allocate_undo_token(ct);
if(ut == NULL) return(FAIL);
}/* End IF */
/*
* Determine which line the current insert point is on by calling the
* find routines.
*/
dlbp = buff_find_line(dbp,dest_position);
doff = buff_find_offset(dbp,dlbp,dest_position);
sb_lbp = buff_find_line(sbp,src_position);
sb_off = buff_find_offset(sbp,sb_lbp,src_position);
se_lbp = buff_find_line(sbp,src_position+length);
se_off = buff_find_offset(sbp,se_lbp,src_position+length);
#ifdef DEBUG
movc3(dlbp->buffer,outbuf,dlbp->byte_count);
outbuf[dlbp->byte_count] = '\0';
if(outbuf[dlbp->byte_count-1] == '\n') outbuf[dlbp->byte_count-1] = '\0';
fprintf(stderr,"'%s' dest begin doff %d\n",outbuf,doff);
for(i = 0; i < sizeof(outbuf); i++) outbuf[i] = ' ';
outbuf[doff+1] = '^'; outbuf[doff+2] = '\0';
fprintf(stderr,"%s\n",outbuf);
movc3(sb_lbp->buffer,outbuf,sb_lbp->byte_count);
outbuf[sb_lbp->byte_count] = '\0';
if(outbuf[sb_lbp->byte_count-1] == '\n'){
outbuf[sb_lbp->byte_count-1] = '\0';
}
fprintf(stderr,"'%s' source begin sb_off %d\n",outbuf,sb_off);
for(i = 0; i < sizeof(outbuf); i++) outbuf[i] = ' ';
outbuf[sb_off+1] = '^'; outbuf[sb_off+2] = '\0';
fprintf(stderr,"%s\n",outbuf);
movc3(se_lbp->buffer,outbuf,se_lbp->byte_count);
outbuf[se_lbp->byte_count] = '\0';
if(outbuf[se_lbp->byte_count-1] == '\n'){
outbuf[se_lbp->byte_count-1] = '\0';
}
fprintf(stderr,"'%s' source end se_off %d\n",outbuf,se_off);
for(i = 0; i < sizeof(outbuf); i++) outbuf[i] = ' ';
outbuf[se_off+1] = '^'; outbuf[se_off+2] = '\0';
fprintf(stderr,"%s\n",outbuf);
#endif /* DEBUG */
/*
* If the buffer just became modified for the first time, we need to change the
* label line to reflect this.
*/
if(dbp->ismodified == NO){
if(dbp == curbuf){
screen_label_line(dbp,"(modified)",LABEL_C_MODIFIED);
}/* End IF */
dbp->ismodified = YES;
}/* End IF */
checkpoint_modified = YES;
/*
* Since we are modifying the destination line buffer structure, we
* want to delete any format structures so that the screen display
* code will know to reformat it.
*/
if(dlbp->format_line){
screen_free_format_lines(dlbp->format_line);
dlbp->format_line = NULL;
}/* End IF */
/*
* The cases we have to handle are much more complex if there are newlines
* being inserted, so we determine now whether this is the case or not.
*/
newline_included_in_src_data = 1;
if(sb_lbp->buffer[sb_lbp->byte_count-1] != '\n'){
newline_included_in_src_data = 0;
}/* End IF */
#ifdef DEBUG
fprintf(
stderr,
"newline_included_in_src_data = %d\n",
newline_included_in_src_data
);
#endif /* DEBUG */
bytes_to_copy_from_source = sb_lbp->byte_count - sb_off;
if(bytes_to_copy_from_source > length){
#ifdef DEBUG
fprintf(
stderr,
"bytes_to_copy_from_source %d > length %d\n",
bytes_to_copy_from_source,
length
);
#endif /* DEBUG */
bytes_to_copy_from_source = length;
newline_included_in_src_data = 0;
}/* End IF */
#ifdef DEBUG
fprintf(
stderr,
"bytes_to_copy_from_source is now %d\n",
bytes_to_copy_from_source
);
#endif /* DEBUG */
/*
* Test for a simple case - one in which there are no newlines in the source
* data. In this case, we know that we simply have to insert it into the
* current line buffer. If the buffer area is not big enough, we may have
* to allocate a new buffer.
*/
if(newline_included_in_src_data == 0){
/*
* The total line length will be the current line length, plus the amount
* of data being inserted.
*/
bytes_required_for_line = dlbp->byte_count + bytes_to_copy_from_source;
#ifdef DEBUG
fprintf(
stderr,
"no newline, bytes_required_for_line %d \n",
bytes_required_for_line
);
#endif /* DEBUG */
/*
* If the current buffer is too small, we allocate a new buffer and copy
* all the data into it, and then free the old buffer.
*/
if(bytes_required_for_line > dlbp->buffer_size){
#ifdef DEBUG
fprintf(
stderr,
"new allocation required\n"
);
#endif /* DEBUG */
bytes_to_allocate =
bytes_required_for_line + INCREMENTAL_LINE_BUFFER_SIZE - 1;
bytes_to_allocate -=
bytes_to_allocate % INCREMENTAL_LINE_BUFFER_SIZE;
#if LARGEST_LOOKASIDE_BLOCK > 0
if(dlbp->buffer_size > LARGEST_LOOKASIDE_BLOCK){
bytes_to_allocate = dlbp->buffer_size * 2;
}/* End IF */
#endif
if(bytes_to_allocate < bytes_required_for_line){
char tmp_message[1024];
sprintf(
tmp_message,
"buff_insert_from_buffer_with_undo need %lu alloced %lu\n",
bytes_required_for_line,
bytes_to_allocate
);
tec_panic(tmp_message);
}/* End IF */
new_buffer =
tec_alloc(
TYPE_C_LINEBUF,
bytes_to_allocate
);
if(new_buffer == NULL){
return(FAIL);
}/* End IF */
dcp = new_buffer;
/*
* Copy up to the current insertion point in the new buffer, then the
* bytes coming from the source line, then if the source line didn't
* contribute a newline, the rest of the bytes coming from the original
* destination line.
*/
movc3(dlbp->buffer,dcp,doff);
dcp += doff;
movc3(sb_lbp->buffer+sb_off,dcp,bytes_to_copy_from_source);
dcp += bytes_to_copy_from_source;
movc3(&dlbp->buffer[doff],dcp,dlbp->byte_count-doff);
bytes_inserted_so_far += bytes_to_copy_from_source;
tec_release(TYPE_C_LINEBUF,dlbp->buffer);
dlbp->buffer = new_buffer;
dlbp->buffer_size = bytes_to_allocate;
dlbp->byte_count = bytes_required_for_line;
}/* End IF */
/*
* If the current buffer is large enough, make a hole to insert the new
* data into, and copy in the new data.
*/
else {
#ifdef DEBUG
fprintf(
stderr,
"it will fit into the current size of %d\n",
dlbp->buffer_size
);
#endif /* DEBUG */
dcp = &dlbp->buffer[bytes_required_for_line-1];
scp = &dlbp->buffer[dlbp->byte_count-1];
for(i = 0; i < dlbp->byte_count - doff; i++){
*dcp-- = *scp--;
}/* End FOR */
movc3(
&sb_lbp->buffer[sb_off],
&dlbp->buffer[doff],
bytes_to_copy_from_source
);
dlbp->byte_count = bytes_required_for_line;
bytes_inserted_so_far += bytes_to_copy_from_source;
}/* End Else */
#ifdef DEBUG
doff += bytes_to_copy_from_source;
movc3(dlbp->buffer,outbuf,dlbp->byte_count);
outbuf[dlbp->byte_count] = '\0';
if(outbuf[dlbp->byte_count-1] == '\n'){
outbuf[dlbp->byte_count-1] = '\0';
}
fprintf(stderr,"'%s' new dest doff %d\n",outbuf,doff);
for(i = 0; i < sizeof(outbuf); i++) outbuf[i] = ' ';
outbuf[doff+1] = '^'; outbuf[doff+2] = '\0';
fprintf(stderr,"%s\n",outbuf);
#endif /* DEBUG */
}/* End IF */
/*
* Else, if there is a newline involved, things get more complicated...
* The first thing we do is construct a line buffer which holds any
* trailing part of the source data (i.e. not terminated by a newline)
* along with the trailing part of the line we are inserting into.
*/
else {
bytes_required_for_line = dlbp->byte_count - doff + se_off;
if(bytes_required_for_line > 0){
nlbp = allocate_line_buffer(bytes_required_for_line);
if(nlbp == NULL) return(FAIL);
nlbp->next_line = dlbp->next_line;
dlbp->next_line = nlbp;
if(nlbp->next_line) nlbp->next_line->prev_line = nlbp;
nlbp->prev_line = dlbp;
movc3(se_lbp->buffer,nlbp->buffer,se_off);
movc3(
&dlbp->buffer[doff],
&nlbp->buffer[se_off],
dlbp->byte_count-doff
);
nlbp->byte_count = bytes_required_for_line;
dlbp->byte_count -= ( dlbp->byte_count - doff );
bytes_inserted_so_far += se_off;
}/* End IF */
/*
* Now we want to merge the data from the begining of the original
* destinatio line, and the data up to the first newline from the
* source data.
*/
bytes_required_for_line =
dlbp->byte_count + sb_lbp->byte_count - sb_off;
if(bytes_required_for_line > dlbp->buffer_size){
bytes_to_allocate =
bytes_required_for_line + INCREMENTAL_LINE_BUFFER_SIZE - 1;
bytes_to_allocate -=
bytes_to_allocate % INCREMENTAL_LINE_BUFFER_SIZE;
#if LARGEST_LOOKASIDE_BLOCK > 0
if(dlbp->buffer_size > LARGEST_LOOKASIDE_BLOCK){
bytes_to_allocate = dlbp->buffer_size * 2;
}/* End IF */
#endif
if(bytes_to_allocate < bytes_required_for_line){
char tmp_message[1024];
sprintf(
tmp_message,
"buff_insert_from_buffer_with_undo needs %lu alloced %lu\n",
bytes_required_for_line,
bytes_to_allocate
);
tec_panic(tmp_message);
}/* End IF */
new_buffer =
tec_alloc(
TYPE_C_LINEBUF,
bytes_to_allocate
);
if(new_buffer != NULL){
dcp = new_buffer;
movc3(dlbp->buffer,dcp,dlbp->byte_count);
dcp += dlbp->byte_count;
movc3(&sb_lbp->buffer[sb_off],dcp,sb_lbp->byte_count - sb_off);
tec_release(TYPE_C_LINEBUF,dlbp->buffer);
dlbp->buffer = new_buffer;
dlbp->buffer_size = bytes_to_allocate;
dlbp->byte_count = bytes_required_for_line;
bytes_inserted_so_far += (sb_lbp->byte_count - sb_off);
}/* End IF */
}/* End IF */
else {
movc3(
&sb_lbp->buffer[sb_off],
&dlbp->buffer[doff],
sb_lbp->byte_count - sb_off
);
dlbp->byte_count = bytes_required_for_line;
bytes_inserted_so_far += (sb_lbp->byte_count - sb_off);
}/* End Else */
/*
* Now we want to copy all the full-length lines in between the starting
* and ending line buffers.
*/
sb_lbp = sb_lbp->next_line;
while(sb_lbp){
if(sb_lbp == se_lbp) break;
bytes_required_for_line = sb_lbp->byte_count;
nlbp = allocate_line_buffer(bytes_required_for_line);
if(nlbp == NULL) break;
nlbp->next_line = dlbp->next_line;
dlbp->next_line = nlbp;
if(nlbp->next_line) nlbp->next_line->prev_line = nlbp;
nlbp->prev_line = dlbp;
movc3(sb_lbp->buffer,nlbp->buffer,bytes_required_for_line);
nlbp->byte_count = bytes_required_for_line;
bytes_inserted_so_far += bytes_required_for_line;
dlbp = nlbp;
sb_lbp = sb_lbp->next_line;
}/* End While */
#ifdef DEBUG
movc3(dlbp->buffer,outbuf,dlbp->byte_count);
outbuf[dlbp->byte_count] = '\0';
if(outbuf[dlbp->byte_count-1] == '\n'){
outbuf[dlbp->byte_count-1] = '\0';
}
fprintf(stderr,"'%s' new dest doff %d\n",outbuf,doff);
for(i = 0; i < sizeof(outbuf); i++) outbuf[i] = ' ';
outbuf[doff+1] = '^'; outbuf[doff+2] = '\0';
fprintf(stderr,"%s\n",outbuf);
movc3(nlbp->buffer,outbuf,nlbp->byte_count);
outbuf[nlbp->byte_count] = '\0';
if(outbuf[nlbp->byte_count-1] == '\n'){
outbuf[nlbp->byte_count-1] = '\0';
}
fprintf(stderr,"'%s' new line\n",outbuf);
#endif /* DEBUG */
}/* End Else */
if(ct){
ut->opcode = UNDO_C_DELETE;
ut->carg1 = (char *)dbp;
ut->iarg1 = dest_position;
ut->iarg2 = bytes_inserted_so_far;
}/* End IF */
/*
* Depending on whether or not the insert position precedes dot or not, we
* may have to increment dot.
*/
if(dest_position <= dbp->dot) dbp->dot += bytes_inserted_so_far;
dbp->zee += bytes_inserted_so_far;
/*
* If we are inserting characters before the position cache, it will
* shift our cached position down by the length, so we adjust the cache.
*/
if(dest_position < dbp->pos_cache.base){
dbp->pos_cache.base += bytes_inserted_so_far;
}/* End IF */
return(SUCCESS);
}/* End Routine */
/**
* \brief Insert a string and arrange for undo capability
*
* This routine is called when characters need to be inserted, and
* also need to be un-inserted if the command is undone.
*/
int
buff_insert_with_undo( struct cmd_token *ct,
struct buff_header *hbp,
unsigned long position,
char *buffer,
unsigned long length )
{
struct undo_token *ut;
PREAMBLE();
ut = allocate_undo_token(ct);
if(ut == NULL) return(FAIL);
ut->opcode = UNDO_C_DELETE;
ut->carg1 = (char *)hbp;
ut->iarg1 = position;
ut->iarg2 = length;
if(buff_insert(hbp,position,buffer,length) == FAIL){
ut->iarg2 = 0;
return(FAIL);
}/* End IF */
return(SUCCESS);
}/* End Routine */
/**
* \brief Insert the specified character into the current position
*
* This routine is called to insert the single character into the buffer
* at the current position (dot). It has to worry about such things as
* noting that the buffer has been modified, the screen display may be
* invalid, etc.
*/
int
buff_insert_char( struct buff_header *hbp,
unsigned long position,
char data )
{
register struct buff_line *lbp;
struct buff_line *nlbp;
register char *cp,*ocp;
register int i,j;
PREAMBLE();
/*
* Insure that the specified position is legal
*/
if(position > hbp->zee){
char tmp_message[LINE_BUFFER_SIZE];
sprintf(tmp_message,"buff_insert_char: bad position %lu %s Z = %lu\n",
position,hbp->name,hbp->zee);
tec_panic(tmp_message);
}/* End IF */
/*
* Determine which line the current insert point is on by calling the
* find routines.
*/
lbp = buff_find_line(hbp,position);
i = buff_find_offset(hbp,lbp,position);
/*
* Depending on whether or not the specified position precedes dot or not, we
* may have to decrement dot.
*/
if(position <= hbp->dot) hbp->dot += 1;
hbp->zee += 1;
/*
* If the buffer just became modified for the first time, we need to change the
* label line to reflect this.
*/
if(hbp->ismodified == NO){
if(hbp == curbuf){
screen_label_line(hbp,"(modified)",LABEL_C_MODIFIED);
}/* End IF */
hbp->ismodified = YES;
}/* End IF */
checkpoint_modified = YES;
/*
* If we are inserting a character before the position cache, it will
* shift our cached position down by one, so we adjust the cache.
*/
if(position < hbp->pos_cache.base){
hbp->pos_cache.base += 1;
}/* End IF */
/*
* If there is a format_line structure associated with this line, we make it
* go away so that the screen display routine will rebuild the display contents
* of this line.
*/
if(lbp->format_line){
screen_free_format_lines(lbp->format_line);
lbp->format_line = NULL;
}/* End IF */
/*
* If this is a newline, we do things a little differently because this will
* actually cause the line to be broken up. In this case we actually have to
* create a new line buffer structure.
*/
if(data == '\n'){
/*
* Calculate how many bytes are to move to the new line
*/
j = lbp->byte_count - i;
if(j > 0){
nlbp = allocate_line_buffer(j);
if(nlbp == NULL) return(FAIL);
nlbp->next_line = lbp->next_line;
lbp->next_line = nlbp;
if(nlbp->next_line) nlbp->next_line->prev_line = nlbp;
nlbp->prev_line = lbp;
/*
* Now copy the data into the new line buffer
*/
cp = lbp->buffer + i;
ocp = nlbp->buffer;
while(nlbp->byte_count < (unsigned int)j){
*ocp++ = *cp++;
nlbp->byte_count++;
lbp->byte_count--;
}/* End While */
}/* End IF */
}/* End IF */
/*
* If this line buffer does not have room for the data, we need to expand it
*/
if(lbp->byte_count == lbp->buffer_size){
ocp =
tec_alloc(
TYPE_C_LINEBUF,
lbp->buffer_size + INCREMENTAL_LINE_BUFFER_SIZE
);
if(ocp == NULL) return(FAIL);
movc3(lbp->buffer,ocp,lbp->buffer_size);
tec_release(TYPE_C_LINEBUF,lbp->buffer);
lbp->buffer = ocp;
lbp->buffer_size += INCREMENTAL_LINE_BUFFER_SIZE;
}/* End IF */
/*
* Make a hole in which to place the byte.
*/
if(lbp->byte_count){
ocp = lbp->buffer + lbp->byte_count;
cp = ocp - 1;
j = lbp->byte_count - i;
while(j--){
*ocp-- = *cp--;
}/* End While */
}/* End IF */
cp = lbp->buffer + i;
/*
* Ok, now insert the data into the hole
*/
*cp = data;
lbp->byte_count += 1;
return(SUCCESS);
}/* End Routine */
/**
* \brief Insert a single char with undo capability
*
* This is an envelope routine which guarentees that the character
* to be input can be undone if necessary.
*/
int
buff_insert_char_with_undo( struct cmd_token *ct,
struct buff_header *hbp,
unsigned long position,
char data )
{
struct undo_token *ut;
PREAMBLE();
ut = allocate_undo_token(ct);
if(ut == NULL) return(FAIL);
ut->opcode = UNDO_C_DELETE;
ut->carg1 = (char *)hbp;
ut->iarg1 = position;
ut->iarg2 = 1;
if(buff_insert_char(hbp,position,data) == FAIL){
ut->iarg2 = 0;
return(FAIL);
}/* End IF */
return(SUCCESS);
}/* End Routine */
/**
* \brief Delete specified number of characters
*
* This routine is called to delete the specified number of characters
* at the specified location in the buffer.
*/
void
buff_delete( struct buff_header *hbp,
unsigned long position,
unsigned long count )
{
PREAMBLE();
/*
* Insure that there are that many character in the buffer after the specified
* position.
*/
if((position + count) > hbp->zee){
char tmp_message[LINE_BUFFER_SIZE];
sprintf(tmp_message,"buff_delete: illegal range %lu-%lu %s Z = %lu\n",
position,position+count,hbp->name,hbp->zee);
tec_panic(tmp_message);
}/* End IF */
/*
* For now we are cheap and just repeatedly call the single character delete
* routine, because this is simple. It is also very expensive and will have to
* be fixed eventually.
*/
while(count){
buff_delete_char(hbp,position);
count -= 1;
}/* End While */
}/* End Routine */
/**
* \brief Delete the character at the current position
*
* This routine is called to delete the single character which is at
* the current buffer position.
*/
int
buff_delete_char( struct buff_header *hbp,
unsigned long position )
{
register struct buff_line *lbp;
struct buff_line *nlbp;
register char *cp,*ocp;
register int i;
register unsigned int j;
PREAMBLE();
/*
* Insure that the specified position is legal
*/
if(position >= hbp->zee){
char tmp_message[LINE_BUFFER_SIZE];
sprintf(tmp_message,"buff_delete_char: bad position %lu %s Z = %lu\n",
position,hbp->name,hbp->zee);
tec_panic(tmp_message);
}/* End IF */
/*
* Call buff_find_line to find the line structure that the specified
* position resides on.
*/
lbp = buff_find_line(hbp,position);
i = buff_find_offset(hbp,lbp,position);
/*
* If there is a format_line structure associated with this line, we make it
* go away so that the screen display routine will rebuild the display contents
* of this line.
*/
if(lbp->format_line){
screen_free_format_lines(lbp->format_line);
lbp->format_line = NULL;
}/* End IF */
/*
* Get a pointer to the character that is about to be deleted
*/
cp = lbp->buffer + i;
/*
* Depending on whether or not the specified position precedes dot or not, we
* may have to decrement dot.
*/
if(position < hbp->dot) hbp->dot -= 1;
hbp->zee -= 1;
/*
* If this has modified the buffer for the first time, it needs to be displayed
* in the label line.
*/
if(hbp->ismodified == NO){
hbp->ismodified = YES;
if(hbp == curbuf){
screen_label_line(hbp,"(modified)",LABEL_C_MODIFIED);
}/* End IF */
}/* End IF */
checkpoint_modified = YES;
/*
* If we are deleting a character before the position cache, it will
* shift our cached position up by one, so we adjust the cache.
*/
if(position < hbp->pos_cache.base){
hbp->pos_cache.base -= 1;
}/* End IF */
/*
* If this is not a newline, then things are pretty simple. Just
* make the character go away...
*/
if(*cp != '\n'){
movc3(cp+1,cp,lbp->byte_count-i-1);
lbp->byte_count -= 1;
return(SUCCESS);
}/* End IF */
/*
* If this is a newline, we do things a little differently because this
* actually causes the two lines to become concatenated (unless this is the
* final line in the buffer). If the buffer area is not big enough to hold
* both lines, we have to create a bigger area.
*/
nlbp = lbp->next_line;
if(nlbp == NULL){
lbp->byte_count -= 1;
return(SUCCESS);
}/* End IF */
/*
* If there is a format_line structure associated with the second line, we make
* it go away since this line is about to be trashed.
*/
if(nlbp->format_line){
screen_free_format_lines(nlbp->format_line);
nlbp->format_line = NULL;
}/* End IF */
/*
* Test whether or not the buffer is big enough to hold the data from both
* lines.
*/
j = lbp->byte_count + nlbp->byte_count - 1;
if(lbp->buffer_size < j){
i = j + INCREMENTAL_LINE_BUFFER_SIZE - 1;
i -= i % INCREMENTAL_LINE_BUFFER_SIZE;
cp = tec_alloc(TYPE_C_LINEBUF,i);
if(cp == NULL) return(FAIL);
movc3(lbp->buffer,cp,lbp->buffer_size);
tec_release(TYPE_C_LINEBUF,lbp->buffer);
lbp->buffer = cp;
lbp->buffer_size = i;
}/* End IF */
/*
* Ok, now concatenate the two line buffers by copying the second line
* into the first. Because we copy on top of the newline, it effectively
* goes away, so we have to decrement the line count by one.
*/
lbp->byte_count -= 1;
cp = lbp->buffer + lbp->byte_count;
ocp = nlbp->buffer;
i = nlbp->byte_count;
while(i--){
*cp++ = *ocp++;
lbp->byte_count++;
}/* End While */
/*
* Fixup the next and previous pointers so that they point around the
* line that we are about to remove.
*/
lbp->next_line = nlbp->next_line;
if(nlbp->next_line) nlbp->next_line->prev_line = lbp;
if(hbp->pos_cache.lbp == nlbp) hbp->pos_cache.lbp = NULL;
buff_free_line_buffer(nlbp);
return(SUCCESS);
}/* End Routine */
/**
* \brief Clobber buffer positions and build undo list
*
* This routine is called when we want to delete a range of bytes in the
* buffer, and build an undo list so that we could replace them if we need
* to. This happens to be pretty efficient for a bulk delete since we can
* just move the line buffers over to the undo list.
*/
int
buff_delete_with_undo( struct cmd_token *ct,
struct buff_header *hbp,
unsigned long position,
unsigned long count )
{
register char *cp;
struct undo_token *ut;
register struct buff_line *lbp;
struct buff_line *top_lbp;
unsigned long top_offset,bulk_position;
long local_count;
unsigned long bytes_deleted_so_far = 0;
PREAMBLE();
/* ???
* I think I may need this to insure that dot doesn't get changed. For
* instance, if we had <dot>abc and deleted 'a', we would have <dot>bc.
* Then if we undo, it's an insert of 'a' at position zero which causes
* dot to shift so we would now have a<dot>bc.
*/
if(hbp->dot == (position+count)){
ut = allocate_undo_token(ct);
if(ut == NULL) return(FAIL);
ut->opcode = UNDO_C_CHANGEDOT;
ut->iarg1 = curbuf->dot;
}/* End IF */
/*
* Because some of the undo functions only work in the 'current' buffer, if
* the user is deleting from a buffer other than the 'current' one, we have
* to set up some undo tokens to switch the current buffer back and forth
* during the undo process, should it occur.
*/
if(hbp != curbuf){
ut = allocate_undo_token(ct);
if(ut == NULL) return(FAIL);
ut->opcode = UNDO_C_CHANGEBUFF;
ut->iarg1 = curbuf->buffer_number;
}/* End IF */
top_lbp = buff_find_line(hbp,position);
top_offset = buff_find_offset(hbp,top_lbp,position);
/*
* First step will be to delete the (hopefully) large block of line buffer
* structures in the middle. This will leave a partial line at the top and
* also at the bottom. To start, if the begining is in the middle of a line,
* we need to skip over the bytes on that line.
*/
local_count = count;
lbp = top_lbp;
bulk_position = position;
if(top_offset){
local_count -= lbp->byte_count - top_offset;
bulk_position = position + lbp->byte_count - top_offset;
lbp = lbp->next_line;
}/* End IF */
if(lbp) top_lbp = lbp->prev_line;
/*
* Ok, now we want to chain over all the line buffer structures which can be
* removed in bulk. lbp gets left pointing at the final line which can be bulk
* removed.
*/
if(lbp && (ssize_t)lbp->byte_count <= local_count){
/*
* We will get to bulk delete at least one line, so allocate an undo buffer.
* Point the undo token at the head of the list of line buffers. Remember
* the buffer position of the first byte of bulk data in iarg1, and keep a
* running count of the number of bytes of bulk data in iarg2.
*/
ut = allocate_undo_token(ct);
if(ut == NULL) return(FAIL);
ut->opcode = UNDO_C_BULK_INSERT;
ut->carg1 = (char *)lbp;
ut->iarg1 = bulk_position;
ut->iarg2 = 0;
/*
* Now scan down until we reach the bottom of the bulk region. Keep track of
* how many characters this all represents, and clean up any associated format
* line structures.
*/
while(1){
ut->iarg2 += lbp->byte_count;
local_count -= lbp->byte_count;
if(lbp->format_line){
screen_free_format_lines(lbp->format_line);
lbp->format_line = NULL;
}/* End IF */
if(lbp->next_line == NULL) break;
if((ssize_t)lbp->next_line->byte_count > local_count) break;
lbp = lbp->next_line;
}/* End While */
/*
* Unchain the bulk area from the rest of the buffer. First, make the parent
* structure's forward pointer point to the next line buffer after the final
* one in the bulk deletion region.
*/
if(top_lbp) top_lbp->next_line = lbp->next_line;
else hbp->first_line = lbp->next_line;
if(hbp->first_line == NULL) hbp->first_line = allocate_line_buffer(1);
/*
* Now we have to fixup the backwards pointer of the trailing line buffer
* structure, if it exists.
*/
if(lbp->next_line){
lbp->next_line->prev_line = top_lbp;
}/* End IF */
/*
* Finally, clobber the 'next' pointer in lbp which is the end of the bulk
* area.
*/
lbp->next_line = NULL;
/*
* Update the remaining count of bytes to be deleted.
*/
bytes_deleted_so_far += ut->iarg2;
/*
* Depending on whether or not the bulk position precedes dot or not, we
* may have to decrement dot. In any case, we have to update zee.
*/
if(bulk_position < hbp->dot){
if(hbp->dot > bulk_position + ut->iarg2) hbp->dot -= ut->iarg2;
else hbp->dot = bulk_position;
}/* End IF */
hbp->zee -= ut->iarg2;
/*
* If this has modified the buffer for the first time, it needs to be displayed
* in the label line.
*/
if(hbp->ismodified == NO){
hbp->ismodified = YES;
if(hbp == curbuf){
screen_label_line(hbp,"(modified)",LABEL_C_MODIFIED);
}/* End IF */
}/* End IF */
checkpoint_modified = YES;
/*
* If we are deleting from before the position cache, it will invalidate the
* information there, so we have to set it invalid so it does not get used.
*/
if(hbp->pos_cache.base >= bulk_position){
hbp->pos_cache.lbp = NULL;
}/* End IF */
}/* End IF */
/*
* Now, just brute force clobber the remaining few bytes
*/
if(bytes_deleted_so_far < count){
ut = allocate_undo_token(ct);
if(ut == NULL) return(FAIL);
ut->opcode = UNDO_C_MEMFREE;
ut->carg1 = cp = tec_alloc(TYPE_C_CBUFF,count-bytes_deleted_so_far);
if(cp != NULL){
/*
* Set up the undo token to put the text back into the q-register, and copy
* it into our save buffer as we delete it from the q-register.
*/
ut = allocate_undo_token(ct);
if(ut == NULL) return(FAIL);
ut->opcode = UNDO_C_INSERT;
ut->iarg1 = position;
ut->iarg2 = 0;
ut->carg1 = cp;
while(count > bytes_deleted_so_far){
*cp++ = buff_contents(hbp,position);
if(buff_delete_char(hbp,position) == FAIL) return(FAIL);
bytes_deleted_so_far += 1;
ut->iarg2 += 1;
}/* End While */
}/* End IF */
}/* End IF */
/*
* Insure that the undo commands are working on the buffer the user specified
*/
if(hbp != curbuf){
ut = allocate_undo_token(ct);
if(ut == FAIL) return(FAIL);
ut->opcode = UNDO_C_CHANGEBUFF;
ut->iarg1 = hbp->buffer_number;
}/* End IF */
return(SUCCESS);
}/* End Routine */
/**
* \brief Insert a list of line buffer structures
*
* This routine is used to insert a list of line buffer structures at
* the specified position in the buffer.
*/
void
buff_bulk_insert( struct buff_header *hbp,
unsigned long position,
long count,
struct buff_line *lbp )
{
register struct buff_line *olbp;
int offset;
PREAMBLE();
/*
* If this will modify the buffer for the first time, it needs to be displayed
* in the label line.
*/
if(hbp->ismodified == NO){
hbp->ismodified = YES;
if(hbp == curbuf){
screen_label_line(hbp,"(modified)",LABEL_C_MODIFIED);
}/* End IF */
}/* End IF */
checkpoint_modified = YES;
/*
* We special case position zero because this is an easy case, and is common
* when HK is being used a lot
*/
if(position == 0){
if((olbp = hbp->first_line) != NULL){
if(olbp->byte_count == 0){
olbp = olbp->next_line;
buff_free_line_buffer(hbp->first_line);
}/* End IF */
}/* End IF */
hbp->first_line = lbp;
while(lbp->next_line) lbp = lbp->next_line;
lbp->next_line = olbp;
if(olbp) olbp->prev_line = lbp;
hbp->zee += count;
hbp->dot += count;
hbp->pos_cache.lbp = NULL;
return;
}/* End IF */
/*
* Also special check for position zee since this is also an easy case
*/
if(position == hbp->zee){
olbp = hbp->first_line;
if(hbp->pos_cache.lbp) olbp = hbp->pos_cache.lbp;
while(olbp->next_line) olbp = olbp->next_line;
if(olbp->byte_count == 0){
olbp = olbp->prev_line;
buff_free_line_buffer(olbp->next_line);
}/* End IF */
olbp->next_line = lbp;
lbp->prev_line = olbp;
if(hbp->dot == hbp->zee) hbp->dot += count;
hbp->zee += count;
hbp->pos_cache.lbp = NULL;
return;
}/* End IF */
/*
* Ok, if the insertion is not at the begining or the end of the buffer, then
* we just have to do it the hard way...
*/
olbp = buff_find_line(hbp,position);
offset = buff_find_offset(hbp,olbp,position);
/*
* Bulk inserts should always be guarenteed aligned.
*/
if(offset){
error_message("BULK_INSERT: internal error! non-aligned bulk insert");
return;
}/* End IF */
if(olbp->prev_line) olbp->prev_line->next_line = lbp;
lbp->prev_line = olbp->prev_line;
while(lbp->next_line) lbp = lbp->next_line;
lbp->next_line = olbp;
olbp->prev_line = lbp;
if(hbp->dot >= position) hbp->dot += count;
if(hbp->pos_cache.base >= position) hbp->pos_cache.base += count;
hbp->zee += count;
return;
}/* End Routine */
/**
* \brief Routine to allocate a line buffer structure
*
* This routine is used to allocate a line buffer structure with
* a data buffer large enough to accommodate the specified number
* of bytes.
*/
struct buff_line *
allocate_line_buffer( int size )
{
register struct buff_line *lbp;
PREAMBLE();
/*
* If he is asking for a size which will result in the minimum allocation, try
* to satisfy the request off of the lookaside list.
*/
if(size <= INITIAL_LINE_BUFFER_SIZE){
if((lbp = line_buffer_lookaside_list) != NULL){
line_buffer_lookaside_list = lbp->next_line;
lbp->next_line = NULL;
lbp->prev_line = NULL;
lbp->format_line = NULL;
lbp->byte_count = 0;
MAGIC_UPDATE(lbp, MAGIC_LINE);
return(lbp);
}/* End IF */
}/* End IF */
/*
* Lookaside list is empty, or he is asking for a non-standard size
*/
lbp = (struct buff_line *)tec_alloc(TYPE_C_LINE,sizeof(struct buff_line));
if(lbp == NULL) return(NULL);
memset(lbp,0,sizeof(*lbp));
if(size <= INITIAL_LINE_BUFFER_SIZE){
lbp->buffer_size = INITIAL_LINE_BUFFER_SIZE;
}/* End IF */
else if(size < INITIAL_LINE_BUFFER_SIZE + INCREMENTAL_LINE_BUFFER_SIZE){
lbp->buffer_size = INITIAL_LINE_BUFFER_SIZE +
INCREMENTAL_LINE_BUFFER_SIZE;
}
else {
lbp->buffer_size = size + INCREMENTAL_LINE_BUFFER_SIZE - 1;
lbp->buffer_size -= lbp->buffer_size % INCREMENTAL_LINE_BUFFER_SIZE;
}/* End IF */
lbp->buffer = tec_alloc(TYPE_C_LINEBUF,lbp->buffer_size);
if(lbp->buffer == NULL){
tec_release(TYPE_C_LINE,(char *)lbp);
return(NULL);
}/* End IF */
MAGIC_UPDATE(lbp, MAGIC_LINE);
return(lbp);
}/* End Routine */
/**
* \brief Free up the associated storage
*
* This routine is called when a line buffer is deleted. It cleans up any
* associated storage as well as the buffer itself.
*/
void
buff_free_line_buffer( struct buff_line *lbp )
{
PREAMBLE();
if(lbp->format_line){
screen_free_format_lines(lbp->format_line);
lbp->format_line = NULL;
}/* End IF */
/*
* If this is a standard size line buffer, place it back on the lookaside list
* rather than giving it back to tec_alloc.
*/
MAGIC_UPDATE(lbp, 0);
if(lbp->buffer_size == INITIAL_LINE_BUFFER_SIZE){
MAGIC_UPDATE(lbp, MAGIC_LINE_LOOKASIDE);
lbp->next_line = line_buffer_lookaside_list;
line_buffer_lookaside_list = lbp;
return;
}/* End IF */
tec_release(TYPE_C_LINEBUF,lbp->buffer);
tec_release(TYPE_C_LINE,(char *)lbp);
}/* End Routine */
/**
* \brief Release a whole list of line buffers
*
* This routine will delete an entire list of line buffers
*/
void
buff_free_line_buffer_list( struct buff_line *lbp )
{
register struct buff_line *olbp;
PREAMBLE();
while((olbp = lbp)){
lbp = lbp->next_line;
buff_free_line_buffer(olbp);
}/* End While */
}/* End Routine */
/**
* \brief Free up lookaside list
*
* This routine frees up any format_lines we've cached on our local
* lookaside list.
*/
void
buff_deallocate_line_buffer_lookaside_list()
{
register struct buff_line *lbp;
PREAMBLE();
while((lbp = line_buffer_lookaside_list) != NULL){
line_buffer_lookaside_list = lbp->next_line;
MAGIC_UPDATE(lbp, 0);
tec_release(TYPE_C_LINEBUF,lbp->buffer);
tec_release(TYPE_C_LINE,(char *)lbp);
}/* End While */
}/* End Routine */
#ifdef DEBUG_DUMPING
buff_dump_buffer(who_string,hbp,channel)
char *who_string;
register struct buff_header *hbp;
FILE *channel;
{
register struct buff_line *lbp;
register int i,j;
PREAMBLE();
fprintf(channel,"%s requested buffer dump:\n");
for(i = 0, lbp = hbp->first_line; lbp != NULL; i++,lbp = lbp->next_line){
fprintf(channel,"line %4d (count %4d)'",i,lbp->byte_count);
buff_dump_line(lbp,channel);
fprintf(channel,"'\n");
}/* End FOR */
fflush(debug_chan);
}/* End Routine */
buff_dump_line(lbp,channel)
register struct buff_line *lbp;
FILE *channel;
{
register struct buff_header *hbp = curbuf;
register int i,j;
PREAMBLE();
for(j = 0; j < lbp->byte_count; j++){
if(lbp->buffer[j] == '\n'){
fprintf(channel,"<newline>");
}/* End IF */
else fprintf(channel,"%c",lbp->buffer[j]);
}/* End FOR */
}/* End Routine */
#endif
unsigned int
stringHash( char *str )
{
unsigned long hash = 5381;
int c;
while ((c = *str++) & 0xFF )
{
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
}
return hash;
}
|