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
|
/*-
* Copyright (c) 1992, 1993, 1994
* The Regents of the University of California. All rights reserved.
* Copyright (c) 1992, 1993, 1994, 1995, 1996
* Keith Bostic. All rights reserved.
*
* See the LICENSE file for redistribution information.
*/
#include "config.h"
#ifndef lint
static const char sccsid[] = "@(#)ex.c 10.50 (Berkeley) 6/30/96";
#endif /* not lint */
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <bitstring.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../common/common.h"
#include "../vi/vi.h"
#if defined(DEBUG) && defined(COMLOG)
static void ex_comlog __P((SCR *, EXCMD *));
#endif
static EXCMDLIST const *
ex_comm_search __P((char *, size_t));
static int ex_discard __P((SCR *));
static int ex_line __P((SCR *, EXCMD *, MARK *, int *, int *));
static int ex_load __P((SCR *));
static void ex_unknown __P((SCR *, char *, size_t));
/*
* ex --
* Main ex loop.
*
* PUBLIC: int ex __P((SCR **));
*/
int
ex(spp)
SCR **spp;
{
EX_PRIVATE *exp;
GS *gp;
MSGS *mp;
SCR *sp;
TEXT *tp;
u_int32_t flags;
sp = *spp;
gp = sp->gp;
exp = EXP(sp);
/* Start the ex screen. */
if (ex_init(sp))
return (1);
/* Flush any saved messages. */
while ((mp = gp->msgq.lh_first) != NULL) {
gp->scr_msg(sp, mp->mtype, mp->buf, mp->len);
LIST_REMOVE(mp, q);
free(mp->buf);
free(mp);
}
/* If reading from a file, errors should have name and line info. */
if (!F_ISSET(gp, G_STDIN_TTY)) {
gp->excmd.if_lno = 1;
gp->excmd.if_name = "script";
}
/*
* !!!
* Initialize the text flags. The beautify edit option historically
* applied to ex command input read from a file. In addition, the
* first time a ^H was discarded from the input, there was a message,
* "^H discarded", that was displayed. We don't bother.
*/
LF_INIT(TXT_BACKSLASH | TXT_CNTRLD | TXT_CR);
for (;; ++gp->excmd.if_lno) {
/* Display status line and flush. */
if (F_ISSET(sp, SC_STATUS)) {
if (!F_ISSET(sp, SC_EX_SILENT))
msgq_status(sp, sp->lno, 0);
F_CLR(sp, SC_STATUS);
}
(void)ex_fflush(sp);
/* Set the flags the user can reset. */
if (O_ISSET(sp, O_BEAUTIFY))
LF_SET(TXT_BEAUTIFY);
if (O_ISSET(sp, O_PROMPT))
LF_SET(TXT_PROMPT);
/* Clear any current interrupts, and get a command. */
CLR_INTERRUPT(sp);
if (ex_txt(sp, &sp->tiq, ':', flags))
return (1);
if (INTERRUPTED(sp)) {
(void)ex_puts(sp, "\n");
(void)ex_fflush(sp);
continue;
}
/* Initialize the command structure. */
CLEAR_EX_PARSER(&gp->excmd);
/*
* If the user entered a single carriage return, send
* ex_cmd() a separator -- it discards single newlines.
*/
tp = sp->tiq.cqh_first;
if (tp->len == 0) {
gp->excmd.cp = " "; /* __TK__ why not |? */
gp->excmd.clen = 1;
} else {
gp->excmd.cp = tp->lb;
gp->excmd.clen = tp->len;
}
F_INIT(&gp->excmd, E_NRSEP);
if (ex_cmd(sp) && !F_ISSET(gp, G_STDIN_TTY))
return (1);
if (INTERRUPTED(sp)) {
CLR_INTERRUPT(sp);
msgq(sp, M_ERR, "170|Interrupted");
}
/*
* If the last command caused a restart, or switched screens
* or into vi, return.
*/
if (F_ISSET(gp, G_SRESTART) || F_ISSET(sp, SC_SSWITCH | SC_VI)) {
*spp = sp;
break;
}
/* If the last command switched files, we don't care. */
F_CLR(sp, SC_FSWITCH);
/*
* If we're exiting this screen, move to the next one. By
* definition, this means returning into vi, so return to the
* main editor loop. The ordering is careful, don't discard
* the contents of sp until the end.
*/
if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
if (file_end(sp, NULL, F_ISSET(sp, SC_EXIT_FORCE)))
return (1);
*spp = screen_next(sp);
return (screen_end(sp));
}
}
return (0);
}
/*
* ex_cmd --
* The guts of the ex parser: parse and execute a string containing
* ex commands.
*
* !!!
* This code MODIFIES the string that gets passed in, to delete quoting
* characters, etc. The string cannot be readonly/text space, nor should
* you expect to use it again after ex_cmd() returns.
*
* !!!
* For the fun of it, if you want to see if a vi clone got the ex argument
* parsing right, try:
*
* echo 'foo|bar' > file1; echo 'foo/bar' > file2;
* vi
* :edit +1|s/|/PIPE/|w file1| e file2|1 | s/\//SLASH/|wq
*
* or: vi
* :set|file|append|set|file
*
* For extra credit, try them in a startup .exrc file.
*
* PUBLIC: int ex_cmd __P((SCR *));
*/
int
ex_cmd(sp)
SCR *sp;
{
enum nresult nret;
EX_PRIVATE *exp;
EXCMD *ecp;
GS *gp;
MARK cur;
recno_t lno;
size_t arg1_len, len;
u_int32_t flags;
long ltmp;
int at_found, gv_found;
int ch, cnt, delim, isaddr, namelen;
int newscreen, notempty, tmp, vi_address;
char *arg1, *p, *s, *t;
gp = sp->gp;
exp = EXP(sp);
/*
* We always start running the command on the top of the stack.
* This means that *everything* must be resolved when we leave
* this function for any reason.
*/
loop: ecp = gp->ecq.lh_first;
/* If we're reading a command from a file, set up error information. */
if (ecp->if_name != NULL) {
gp->if_lno = ecp->if_lno;
gp->if_name = ecp->if_name;
}
/*
* If a move to the end of the file is scheduled for this command,
* do it now.
*/
if (F_ISSET(ecp, E_MOVETOEND)) {
if (db_last(sp, &sp->lno))
goto rfail;
sp->cno = 0;
F_CLR(ecp, E_MOVETOEND);
}
/* If we found a newline, increment the count now. */
if (F_ISSET(ecp, E_NEWLINE)) {
++gp->if_lno;
++ecp->if_lno;
F_CLR(ecp, E_NEWLINE);
}
/* (Re)initialize the EXCMD structure, preserving some flags. */
CLEAR_EX_CMD(ecp);
/* Initialize the argument structures. */
if (argv_init(sp, ecp))
goto err;
/* Initialize +cmd, saved command information. */
arg1 = NULL;
ecp->save_cmdlen = 0;
/* Skip <blank>s, empty lines. */
for (notempty = 0; ecp->clen > 0; ++ecp->cp, --ecp->clen)
if ((ch = *ecp->cp) == '\n') {
++gp->if_lno;
++ecp->if_lno;
} else if (isblank(ch))
notempty = 1;
else
break;
/*
* !!!
* Permit extra colons at the start of the line. Historically,
* ex/vi allowed a single extra one. It's simpler not to count.
* The stripping is done here because, historically, any command
* could have preceding colons, e.g. ":g/pattern/:p" worked.
*/
if (ecp->clen != 0 && ch == ':') {
notempty = 1;
while (--ecp->clen > 0 && (ch = *++ecp->cp) == ':');
}
/*
* Command lines that start with a double-quote are comments.
*
* !!!
* Historically, there was no escape or delimiter for a comment, e.g.
* :"foo|set was a single comment and nothing was output. Since nvi
* permits users to escape <newline> characters into command lines, we
* have to check for that case.
*/
if (ecp->clen != 0 && ch == '"') {
while (--ecp->clen > 0 && *++ecp->cp != '\n');
if (*ecp->cp == '\n') {
F_SET(ecp, E_NEWLINE);
++ecp->cp;
--ecp->clen;
}
goto loop;
}
/* Skip whitespace. */
for (; ecp->clen > 0; ++ecp->cp, --ecp->clen) {
ch = *ecp->cp;
if (!isblank(ch))
break;
}
/*
* The last point at which an empty line can mean do nothing.
*
* !!!
* Historically, in ex mode, lines containing only <blank> characters
* were the same as a single <carriage-return>, i.e. a default command.
* In vi mode, they were ignored. In .exrc files this was a serious
* annoyance, as vi kept trying to treat them as print commands. We
* ignore backward compatibility in this case, discarding lines that
* contain only <blank> characters from .exrc files.
*
* !!!
* This is where you end up when you're done a command, i.e. clen has
* gone to zero. Continue if there are more commands to run.
*/
if (ecp->clen == 0 &&
(!notempty || F_ISSET(sp, SC_VI) || F_ISSET(ecp, E_BLIGNORE))) {
if (ex_load(sp))
goto rfail;
ecp = gp->ecq.lh_first;
if (ecp->clen == 0)
goto rsuccess;
goto loop;
}
/*
* Check to see if this is a command for which we may want to move
* the cursor back up to the previous line. (The command :1<CR>
* wants a <newline> separator, but the command :<CR> wants to erase
* the command line.) If the line is empty except for <blank>s,
* <carriage-return> or <eof>, we'll probably want to move up. I
* don't think there's any way to get <blank> characters *after* the
* command character, but this is the ex parser, and I've been wrong
* before.
*/
if (F_ISSET(ecp, E_NRSEP) &&
ecp->clen != 0 && (ecp->clen != 1 || ecp->cp[0] != '\004'))
F_CLR(ecp, E_NRSEP);
/* Parse command addresses. */
if (ex_range(sp, ecp, &tmp))
goto rfail;
if (tmp)
goto err;
/*
* Skip <blank>s and any more colons (the command :3,5:print
* worked, historically).
*/
for (; ecp->clen > 0; ++ecp->cp, --ecp->clen) {
ch = *ecp->cp;
if (!isblank(ch) && ch != ':')
break;
}
/*
* If no command, ex does the last specified of p, l, or #, and vi
* moves to the line. Otherwise, determine the length of the command
* name by looking for the first non-alphabetic character. (There
* are a few non-alphabetic characters in command names, but they're
* all single character commands.) This isn't a great test, because
* it means that, for the command ":e +cut.c file", we'll report that
* the command "cut" wasn't known. However, it makes ":e+35 file" work
* correctly.
*
* !!!
* Historically, lines with multiple adjacent (or <blank> separated)
* command separators were very strange. For example, the command
* |||<carriage-return>, when the cursor was on line 1, displayed
* lines 2, 3 and 5 of the file. In addition, the command " | "
* would only display the line after the next line, instead of the
* next two lines. No ideas why. It worked reasonably when executed
* from vi mode, and displayed lines 2, 3, and 4, so we do a default
* command for each separator.
*/
#define SINGLE_CHAR_COMMANDS "\004!#&*<=>@~"
newscreen = 0;
if (ecp->clen != 0 && ecp->cp[0] != '|' && ecp->cp[0] != '\n') {
if (strchr(SINGLE_CHAR_COMMANDS, *ecp->cp)) {
p = ecp->cp;
++ecp->cp;
--ecp->clen;
namelen = 1;
} else {
for (p = ecp->cp;
ecp->clen > 0; --ecp->clen, ++ecp->cp)
if (!isalpha(*ecp->cp))
break;
if ((namelen = ecp->cp - p) == 0) {
msgq(sp, M_ERR, "080|Unknown command name");
goto err;
}
}
/*
* !!!
* Historic vi permitted flags to immediately follow any
* subset of the 'delete' command, but then did not permit
* further arguments (flag, buffer, count). Make it work.
* Permit further arguments for the few shreds of dignity
* it offers.
*
* Adding commands that start with 'd', and match "delete"
* up to a l, p, +, - or # character can break this code.
*
* !!!
* Capital letters beginning the command names ex, edit,
* next, previous, tag and visual (in vi mode) indicate the
* command should happen in a new screen.
*/
switch (p[0]) {
case 'd':
for (s = p,
t = cmds[C_DELETE].name; *s == *t; ++s, ++t);
if (s[0] == 'l' || s[0] == 'p' || s[0] == '+' ||
s[0] == '-' || s[0] == '^' || s[0] == '#') {
len = (ecp->cp - p) - (s - p);
ecp->cp -= len;
ecp->clen += len;
ecp->rcmd = cmds[C_DELETE];
ecp->rcmd.syntax = "1bca1";
ecp->cmd = &ecp->rcmd;
goto skip_srch;
}
break;
case 'E': case 'F': case 'N': case 'P': case 'T': case 'V':
newscreen = 1;
p[0] = tolower(p[0]);
break;
}
/*
* Search the table for the command.
*
* !!!
* Historic vi permitted the mark to immediately follow the
* 'k' in the 'k' command. Make it work.
*
* !!!
* Historic vi permitted any flag to follow the s command, e.g.
* "s/e/E/|s|sgc3p" was legal. Make the command "sgc" work.
* Since the following characters all have to be flags, i.e.
* alphabetics, we can let the s command routine return errors
* if it was some illegal command string. This code will break
* if an "sg" or similar command is ever added. The substitute
* code doesn't care if it's a "cgr" flag or a "#lp" flag that
* follows the 's', but we limit the choices here to "cgr" so
* that we get unknown command messages for wrong combinations.
*/
if ((ecp->cmd = ex_comm_search(p, namelen)) == NULL)
switch (p[0]) {
case 'k':
if (p[1] && !p[2]) {
ecp->cp -= namelen - 1;
ecp->clen += namelen - 1;
ecp->cmd = &cmds[C_K];
break;
}
goto unknown;
case 's':
for (s = p + 1, cnt = namelen; --cnt; ++s)
if (s[0] != 'c' &&
s[0] != 'g' && s[0] != 'r')
break;
if (cnt == 0) {
ecp->cp -= namelen - 1;
ecp->clen += namelen - 1;
ecp->rcmd = cmds[C_SUBSTITUTE];
ecp->rcmd.fn = ex_subagain;
ecp->cmd = &ecp->rcmd;
break;
}
/* FALLTHROUGH */
default:
unknown: if (newscreen)
p[0] = toupper(p[0]);
ex_unknown(sp, p, namelen);
goto err;
}
/*
* The visual command has a different syntax when called
* from ex than when called from a vi colon command. FMH.
* Make the change now, before we test for the newscreen
* semantic, so that we're testing the right one.
*/
skip_srch: if (ecp->cmd == &cmds[C_VISUAL_EX] && F_ISSET(sp, SC_VI))
ecp->cmd = &cmds[C_VISUAL_VI];
/*
* !!!
* Historic vi permitted a capital 'P' at the beginning of
* any command that started with 'p'. Probably wanted the
* P[rint] command for backward compatibility, and the code
* just made Preserve and Put work by accident. Nvi uses
* Previous to mean previous-in-a-new-screen, so be careful.
*/
if (newscreen && !F_ISSET(ecp->cmd, E_NEWSCREEN) &&
(ecp->cmd == &cmds[C_PRINT] ||
ecp->cmd == &cmds[C_PRESERVE]))
newscreen = 0;
/* Test for a newscreen associated with this command. */
if (newscreen && !F_ISSET(ecp->cmd, E_NEWSCREEN))
goto unknown;
/* Secure means no shell access. */
if (F_ISSET(ecp->cmd, E_SECURE) && O_ISSET(sp, O_SECURE)) {
ex_emsg(sp, ecp->cmd->name, EXM_SECURE);
goto err;
}
/*
* Multiple < and > characters; another "feature". Note,
* The string passed to the underlying function may not be
* nul terminated in this case.
*/
if ((ecp->cmd == &cmds[C_SHIFTL] && *p == '<') ||
(ecp->cmd == &cmds[C_SHIFTR] && *p == '>')) {
for (ch = *p;
ecp->clen > 0; --ecp->clen, ++ecp->cp)
if (*ecp->cp != ch)
break;
if (argv_exp0(sp, ecp, p, ecp->cp - p))
goto err;
}
/* Set the format style flags for the next command. */
if (ecp->cmd == &cmds[C_HASH])
exp->fdef = E_C_HASH;
else if (ecp->cmd == &cmds[C_LIST])
exp->fdef = E_C_LIST;
else if (ecp->cmd == &cmds[C_PRINT])
exp->fdef = E_C_PRINT;
F_CLR(ecp, E_USELASTCMD);
} else {
/* Print is the default command. */
ecp->cmd = &cmds[C_PRINT];
/* Set the saved format flags. */
F_SET(ecp, exp->fdef);
/*
* !!!
* If no address was specified, and it's not a global command,
* we up the address by one. (I have no idea why globals are
* exempted, but it's (ahem) historic practice.)
*/
if (ecp->addrcnt == 0 && !F_ISSET(sp, SC_EX_GLOBAL)) {
ecp->addrcnt = 1;
ecp->addr1.lno = sp->lno + 1;
ecp->addr1.cno = sp->cno;
}
F_SET(ecp, E_USELASTCMD);
}
/*
* !!!
* Historically, the number option applied to both ex and vi. One
* strangeness was that ex didn't switch display formats until a
* command was entered, e.g. <CR>'s after the set didn't change to
* the new format, but :1p would.
*/
if (O_ISSET(sp, O_NUMBER)) {
F_SET(ecp, E_OPTNUM);
FL_SET(ecp->iflags, E_C_HASH);
} else
F_CLR(ecp, E_OPTNUM);
/* Check for ex mode legality. */
if (F_ISSET(sp, SC_EX) && (F_ISSET(ecp->cmd, E_VIONLY) || newscreen)) {
msgq(sp, M_ERR,
"082|%s: command not available in ex mode", ecp->cmd->name);
goto err;
}
/* Add standard command flags. */
F_SET(ecp, ecp->cmd->flags);
if (!newscreen)
F_CLR(ecp, E_NEWSCREEN);
/*
* There are three normal termination cases for an ex command. They
* are the end of the string (ecp->clen), or unescaped (by <literal
* next> characters) <newline> or '|' characters. As we're now past
* possible addresses, we can determine how long the command is, so we
* don't have to look for all the possible terminations. Naturally,
* there are some exciting special cases:
*
* 1: The bang, global, v and the filter versions of the read and
* write commands are delimited by <newline>s (they can contain
* shell pipes).
* 2: The ex, edit, next and visual in vi mode commands all take ex
* commands as their first arguments.
* 3: The s command takes an RE as its first argument, and wants it
* to be specially delimited.
*
* Historically, '|' characters in the first argument of the ex, edit,
* next, vi visual, and s commands didn't delimit the command. And,
* in the filter cases for read and write, and the bang, global and v
* commands, they did not delimit the command at all.
*
* For example, the following commands were legal:
*
* :edit +25|s/abc/ABC/ file.c
* :s/|/PIPE/
* :read !spell % | columnate
* :global/pattern/p|l
*
* It's not quite as simple as it sounds, however. The command:
*
* :s/a/b/|s/c/d|set
*
* was also legal, i.e. the historic ex parser (using the word loosely,
* since "parser" implies some regularity) delimited the RE's based on
* its delimiter and not anything so irretrievably vulgar as a command
* syntax.
*
* One thing that makes this easier is that we can ignore most of the
* command termination conditions for the commands that want to take
* the command up to the next newline. None of them are legal in .exrc
* files, so if we're here, we only dealing with a single line, and we
* can just eat it.
*
* Anyhow, the following code makes this all work. First, for the
* special cases we move past their special argument(s). Then, we
* do normal command processing on whatever is left. Barf-O-Rama.
*/
arg1_len = 0;
ecp->save_cmd = ecp->cp;
if (ecp->cmd == &cmds[C_EDIT] || ecp->cmd == &cmds[C_EX] ||
ecp->cmd == &cmds[C_NEXT] || ecp->cmd == &cmds[C_VISUAL_VI]) {
/*
* Move to the next non-whitespace character. A '!'
* immediately following the command is eaten as a
* force flag.
*/
if (ecp->clen > 0 && *ecp->cp == '!') {
++ecp->cp;
--ecp->clen;
FL_SET(ecp->iflags, E_C_FORCE);
/* Reset, don't reparse. */
ecp->save_cmd = ecp->cp;
}
for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
if (!isblank(*ecp->cp))
break;
/*
* QUOTING NOTE:
*
* The historic implementation ignored all escape characters
* so there was no way to put a space or newline into the +cmd
* field. We do a simplistic job of fixing it by moving to the
* first whitespace character that isn't escaped by a literal
* next character. The literal next characters are stripped
* as they're no longer useful.
*/
if (ecp->clen > 0 && *ecp->cp == '+') {
++ecp->cp;
--ecp->clen;
for (arg1 = p = ecp->cp;
ecp->clen > 0; --ecp->clen, ++ecp->cp) {
ch = *ecp->cp;
if (IS_ESCAPE(sp,
ecp, ch) && ecp->clen > 1) {
--ecp->clen;
ch = *++ecp->cp;
} else if (isblank(ch))
break;
*p++ = ch;
}
arg1_len = ecp->cp - arg1;
/* Reset, so the first argument isn't reparsed. */
ecp->save_cmd = ecp->cp;
}
} else if (ecp->cmd == &cmds[C_BANG] ||
ecp->cmd == &cmds[C_GLOBAL] || ecp->cmd == &cmds[C_V]) {
ecp->cp += ecp->clen;
ecp->clen = 0;
} else if (ecp->cmd == &cmds[C_READ] || ecp->cmd == &cmds[C_WRITE]) {
/*
* Move to the next character. If it's a '!', it's a filter
* command and we want to eat it all, otherwise, we're done.
*/
for (; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
ch = *ecp->cp;
if (!isblank(ch))
break;
}
if (ecp->clen > 0 && ch == '!') {
ecp->cp += ecp->clen;
ecp->clen = 0;
}
} else if (ecp->cmd == &cmds[C_SUBSTITUTE]) {
/*
* Move to the next non-whitespace character, we'll use it as
* the delimiter. If the character isn't an alphanumeric or
* a '|', it's the delimiter, so parse it. Otherwise, we're
* into something like ":s g", so use the special s command.
*/
for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
if (!isblank(ecp->cp[0]))
break;
if (isalnum(ecp->cp[0]) || ecp->cp[0] == '|') {
ecp->rcmd = cmds[C_SUBSTITUTE];
ecp->rcmd.fn = ex_subagain;
ecp->cmd = &ecp->rcmd;
} else if (ecp->clen > 0) {
/*
* QUOTING NOTE:
*
* Backslashes quote delimiter characters for RE's.
* The backslashes are NOT removed since they'll be
* used by the RE code. Move to the third delimiter
* that's not escaped (or the end of the command).
*/
delim = *ecp->cp;
++ecp->cp;
--ecp->clen;
for (cnt = 2; ecp->clen > 0 &&
cnt != 0; --ecp->clen, ++ecp->cp)
if (ecp->cp[0] == '\\' &&
ecp->clen > 1) {
++ecp->cp;
--ecp->clen;
} else if (ecp->cp[0] == delim)
--cnt;
}
}
/*
* Use normal quoting and termination rules to find the end of this
* command.
*
* QUOTING NOTE:
*
* Historically, vi permitted ^V's to escape <newline>'s in the .exrc
* file. It was almost certainly a bug, but that's what bug-for-bug
* compatibility means, Grasshopper. Also, ^V's escape the command
* delimiters. Literal next quote characters in front of the newlines,
* '|' characters or literal next characters are stripped as they're
* no longer useful.
*/
vi_address = ecp->clen != 0 && ecp->cp[0] != '\n';
for (cnt = 0, p = ecp->cp; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
ch = ecp->cp[0];
if (IS_ESCAPE(sp, ecp, ch) && ecp->clen > 1) {
tmp = ecp->cp[1];
if (tmp == '\n' || tmp == '|') {
if (tmp == '\n') {
++gp->if_lno;
++ecp->if_lno;
}
--ecp->clen;
++ecp->cp;
++cnt;
ch = tmp;
}
} else if (ch == '\n' || ch == '|') {
if (ch == '\n')
F_SET(ecp, E_NEWLINE);
--ecp->clen;
break;
}
*p++ = ch;
}
/*
* Save off the next command information, go back to the
* original start of the command.
*/
p = ecp->cp + 1;
ecp->cp = ecp->save_cmd;
ecp->save_cmd = p;
ecp->save_cmdlen = ecp->clen;
ecp->clen = ((ecp->save_cmd - ecp->cp) - 1) - cnt;
/*
* QUOTING NOTE:
*
* The "set tags" command historically used a backslash, not the
* user's literal next character, to escape whitespace. Handle
* it here instead of complicating the argv_exp3() code. Note,
* this isn't a particularly complex trap, and if backslashes were
* legal in set commands, this would have to be much more complicated.
*/
if (ecp->cmd == &cmds[C_SET])
for (p = ecp->cp, len = ecp->clen; len > 0; --len, ++p)
if (*p == '\\')
*p = CH_LITERAL;
/*
* Set the default addresses. It's an error to specify an address for
* a command that doesn't take them. If two addresses are specified
* for a command that only takes one, lose the first one. Two special
* cases here, some commands take 0 or 2 addresses. For most of them
* (the E_ADDR2_ALL flag), 0 defaults to the entire file. For one
* (the `!' command, the E_ADDR2_NONE flag), 0 defaults to no lines.
*
* Also, if the file is empty, some commands want to use an address of
* 0, i.e. the entire file is 0 to 0, and the default first address is
* 0. Otherwise, an entire file is 1 to N and the default line is 1.
* Note, we also add the E_ADDR_ZERO flag to the command flags, for the
* case where the 0 address is only valid if it's a default address.
*
* Also, set a flag if we set the default addresses. Some commands
* (ex: z) care if the user specified an address of if we just used
* the current cursor.
*/
switch (F_ISSET(ecp, E_ADDR1 | E_ADDR2 | E_ADDR2_ALL | E_ADDR2_NONE)) {
case E_ADDR1: /* One address: */
switch (ecp->addrcnt) {
case 0: /* Default cursor/empty file. */
ecp->addrcnt = 1;
F_SET(ecp, E_ADDR_DEF);
if (F_ISSET(ecp, E_ADDR_ZERODEF)) {
if (db_last(sp, &lno))
goto err;
if (lno == 0) {
ecp->addr1.lno = 0;
F_SET(ecp, E_ADDR_ZERO);
} else
ecp->addr1.lno = sp->lno;
} else
ecp->addr1.lno = sp->lno;
ecp->addr1.cno = sp->cno;
break;
case 1:
break;
case 2: /* Lose the first address. */
ecp->addrcnt = 1;
ecp->addr1 = ecp->addr2;
}
break;
case E_ADDR2_NONE: /* Zero/two addresses: */
if (ecp->addrcnt == 0) /* Default to nothing. */
break;
goto two_addr;
case E_ADDR2_ALL: /* Zero/two addresses: */
if (ecp->addrcnt == 0) { /* Default entire/empty file. */
F_SET(ecp, E_ADDR_DEF);
ecp->addrcnt = 2;
if (sp->ep == NULL)
ecp->addr2.lno = 0;
else if (db_last(sp, &ecp->addr2.lno))
goto err;
if (F_ISSET(ecp, E_ADDR_ZERODEF) &&
ecp->addr2.lno == 0) {
ecp->addr1.lno = 0;
F_SET(ecp, E_ADDR_ZERO);
} else
ecp->addr1.lno = 1;
ecp->addr1.cno = ecp->addr2.cno = 0;
F_SET(ecp, E_ADDR2_ALL);
break;
}
/* FALLTHROUGH */
case E_ADDR2: /* Two addresses: */
two_addr: switch (ecp->addrcnt) {
case 0: /* Default cursor/empty file. */
ecp->addrcnt = 2;
F_SET(ecp, E_ADDR_DEF);
if (sp->lno == 1 &&
F_ISSET(ecp, E_ADDR_ZERODEF)) {
if (db_last(sp, &lno))
goto err;
if (lno == 0) {
ecp->addr1.lno = ecp->addr2.lno = 0;
F_SET(ecp, E_ADDR_ZERO);
} else
ecp->addr1.lno =
ecp->addr2.lno = sp->lno;
} else
ecp->addr1.lno = ecp->addr2.lno = sp->lno;
ecp->addr1.cno = ecp->addr2.cno = sp->cno;
break;
case 1: /* Default to first address. */
ecp->addrcnt = 2;
ecp->addr2 = ecp->addr1;
break;
case 2:
break;
}
break;
default:
if (ecp->addrcnt) /* Error. */
goto usage;
}
/*
* !!!
* The ^D scroll command historically scrolled the value of the scroll
* option or to EOF. It was an error if the cursor was already at EOF.
* (Leading addresses were permitted, but were then ignored.)
*/
if (ecp->cmd == &cmds[C_SCROLL]) {
ecp->addrcnt = 2;
ecp->addr1.lno = sp->lno + 1;
ecp->addr2.lno = sp->lno + O_VAL(sp, O_SCROLL);
ecp->addr1.cno = ecp->addr2.cno = sp->cno;
if (db_last(sp, &lno))
goto err;
if (lno != 0 && lno > sp->lno && ecp->addr2.lno > lno)
ecp->addr2.lno = lno;
}
ecp->flagoff = 0;
for (p = ecp->cmd->syntax; *p != '\0'; ++p) {
/*
* The force flag is sensitive to leading whitespace, i.e.
* "next !" is different from "next!". Handle it before
* skipping leading <blank>s.
*/
if (*p == '!') {
if (ecp->clen > 0 && *ecp->cp == '!') {
++ecp->cp;
--ecp->clen;
FL_SET(ecp->iflags, E_C_FORCE);
}
continue;
}
/* Skip leading <blank>s. */
for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
if (!isblank(*ecp->cp))
break;
if (ecp->clen == 0)
break;
switch (*p) {
case '1': /* +, -, #, l, p */
/*
* !!!
* Historically, some flags were ignored depending
* on where they occurred in the command line. For
* example, in the command, ":3+++p--#", historic vi
* acted on the '#' flag, but ignored the '-' flags.
* It's unambiguous what the flags mean, so we just
* handle them regardless of the stupidity of their
* location.
*/
for (; ecp->clen; --ecp->clen, ++ecp->cp)
switch (*ecp->cp) {
case '+':
++ecp->flagoff;
break;
case '-':
case '^':
--ecp->flagoff;
break;
case '#':
F_CLR(ecp, E_OPTNUM);
FL_SET(ecp->iflags, E_C_HASH);
exp->fdef |= E_C_HASH;
break;
case 'l':
FL_SET(ecp->iflags, E_C_LIST);
exp->fdef |= E_C_LIST;
break;
case 'p':
FL_SET(ecp->iflags, E_C_PRINT);
exp->fdef |= E_C_PRINT;
break;
default:
goto end_case1;
}
end_case1: break;
case '2': /* -, ., +, ^ */
case '3': /* -, ., +, ^, = */
for (; ecp->clen; --ecp->clen, ++ecp->cp)
switch (*ecp->cp) {
case '-':
FL_SET(ecp->iflags, E_C_DASH);
break;
case '.':
FL_SET(ecp->iflags, E_C_DOT);
break;
case '+':
FL_SET(ecp->iflags, E_C_PLUS);
break;
case '^':
FL_SET(ecp->iflags, E_C_CARAT);
break;
case '=':
if (*p == '3') {
FL_SET(ecp->iflags, E_C_EQUAL);
break;
}
/* FALLTHROUGH */
default:
goto end_case23;
}
end_case23: break;
case 'b': /* buffer */
/*
* !!!
* Historically, "d #" was a delete with a flag, not a
* delete into the '#' buffer. If the current command
* permits a flag, don't use one as a buffer. However,
* the 'l' and 'p' flags were legal buffer names in the
* historic ex, and were used as buffers, not flags.
*/
if ((ecp->cp[0] == '+' || ecp->cp[0] == '-' ||
ecp->cp[0] == '^' || ecp->cp[0] == '#') &&
strchr(p, '1') != NULL)
break;
/*
* !!!
* Digits can't be buffer names in ex commands, or the
* command "d2" would be a delete into buffer '2', and
* not a two-line deletion.
*/
if (!isdigit(ecp->cp[0])) {
ecp->buffer = *ecp->cp;
++ecp->cp;
--ecp->clen;
FL_SET(ecp->iflags, E_C_BUFFER);
}
break;
case 'c': /* count [01+a] */
++p;
/* Validate any signed value. */
if (!isdigit(*ecp->cp) && (*p != '+' ||
(*ecp->cp != '+' && *ecp->cp != '-')))
break;
/* If a signed value, set appropriate flags. */
if (*ecp->cp == '-')
FL_SET(ecp->iflags, E_C_COUNT_NEG);
else if (*ecp->cp == '+')
FL_SET(ecp->iflags, E_C_COUNT_POS);
if ((nret =
nget_slong(<mp, ecp->cp, &t, 10)) != NUM_OK) {
ex_badaddr(sp, NULL, A_NOTSET, nret);
goto err;
}
if (ltmp == 0 && *p != '0') {
msgq(sp, M_ERR, "083|Count may not be zero");
goto err;
}
ecp->clen -= (t - ecp->cp);
ecp->cp = t;
/*
* Counts as address offsets occur in commands taking
* two addresses. Historic vi practice was to use
* the count as an offset from the *second* address.
*
* Set a count flag; some underlying commands (see
* join) do different things with counts than with
* line addresses.
*/
if (*p == 'a') {
ecp->addr1 = ecp->addr2;
ecp->addr2.lno = ecp->addr1.lno + ltmp - 1;
} else
ecp->count = ltmp;
FL_SET(ecp->iflags, E_C_COUNT);
break;
case 'f': /* file */
if (argv_exp2(sp, ecp, ecp->cp, ecp->clen))
goto err;
goto arg_cnt_chk;
case 'l': /* line */
/*
* Get a line specification.
*
* If the line was a search expression, we may have
* changed state during the call, and we're now
* searching the file. Push ourselves onto the state
* stack.
*/
if (ex_line(sp, ecp, &cur, &isaddr, &tmp))
goto rfail;
if (tmp)
goto err;
/* Line specifications are always required. */
if (!isaddr) {
msgq_str(sp, M_ERR, ecp->cp,
"084|%s: bad line specification");
goto err;
}
/*
* The target line should exist for these commands,
* but 0 is legal for them as well.
*/
if (cur.lno != 0 && !db_exist(sp, cur.lno)) {
ex_badaddr(sp, NULL, A_EOF, NUM_OK);
goto err;
}
ecp->lineno = cur.lno;
break;
case 'S': /* string, file exp. */
if (ecp->clen != 0) {
if (argv_exp1(sp, ecp, ecp->cp,
ecp->clen, ecp->cmd == &cmds[C_BANG]))
goto err;
goto addr_verify;
}
/* FALLTHROUGH */
case 's': /* string */
if (argv_exp0(sp, ecp, ecp->cp, ecp->clen))
goto err;
goto addr_verify;
case 'W': /* word string */
/*
* QUOTING NOTE:
*
* Literal next characters escape the following
* character. Quoting characters are stripped here
* since they are no longer useful.
*
* First there was the word.
*/
for (p = t = ecp->cp;
ecp->clen > 0; --ecp->clen, ++ecp->cp) {
ch = *ecp->cp;
if (IS_ESCAPE(sp,
ecp, ch) && ecp->clen > 1) {
--ecp->clen;
*p++ = *++ecp->cp;
} else if (isblank(ch)) {
++ecp->cp;
--ecp->clen;
break;
} else
*p++ = ch;
}
if (argv_exp0(sp, ecp, t, p - t))
goto err;
/* Delete intervening whitespace. */
for (; ecp->clen > 0;
--ecp->clen, ++ecp->cp) {
ch = *ecp->cp;
if (!isblank(ch))
break;
}
if (ecp->clen == 0)
goto usage;
/* Followed by the string. */
for (p = t = ecp->cp; ecp->clen > 0;
--ecp->clen, ++ecp->cp, ++p) {
ch = *ecp->cp;
if (IS_ESCAPE(sp,
ecp, ch) && ecp->clen > 1) {
--ecp->clen;
*p = *++ecp->cp;
} else
*p = ch;
}
if (argv_exp0(sp, ecp, t, p - t))
goto err;
goto addr_verify;
case 'w': /* word */
if (argv_exp3(sp, ecp, ecp->cp, ecp->clen))
goto err;
arg_cnt_chk: if (*++p != 'N') { /* N */
/*
* If a number is specified, must either be
* 0 or that number, if optional, and that
* number, if required.
*/
tmp = *p - '0';
if ((*++p != 'o' || exp->argsoff != 0) &&
exp->argsoff != tmp)
goto usage;
}
goto addr_verify;
default:
msgq(sp, M_ERR,
"085|Internal syntax table error (%s: %s)",
ecp->cmd->name, KEY_NAME(sp, *p));
}
}
/* Skip trailing whitespace. */
for (; ecp->clen > 0; --ecp->clen) {
ch = *ecp->cp++;
if (!isblank(ch))
break;
}
/*
* There shouldn't be anything left, and no more required fields,
* i.e neither 'l' or 'r' in the syntax string.
*/
if (ecp->clen != 0 || strpbrk(p, "lr")) {
usage: msgq(sp, M_ERR, "086|Usage: %s", ecp->cmd->usage);
goto err;
}
/*
* Verify that the addresses are legal. Check the addresses here,
* because this is a place where all ex addresses pass through.
* (They don't all pass through ep_line(), for instance.) We're
* assuming that any non-existent line doesn't exist because it's
* past the end-of-file. That's a pretty good guess.
*
* If it's a "default vi command", an address of zero is okay.
*/
addr_verify:
switch (ecp->addrcnt) {
case 2:
/*
* Historic ex/vi permitted commands with counts to go past
* EOF. So, for example, if the file only had 5 lines, the
* ex command "1,6>" would fail, but the command ">300"
* would succeed. Since we don't want to have to make all
* of the underlying commands handle random line numbers,
* fix it here.
*/
if (ecp->addr2.lno == 0) {
if (!F_ISSET(ecp, E_ADDR_ZERO) &&
(F_ISSET(sp, SC_EX) ||
!F_ISSET(ecp, E_USELASTCMD))) {
ex_badaddr(sp, ecp->cmd, A_ZERO, NUM_OK);
goto err;
}
} else if (!db_exist(sp, ecp->addr2.lno))
if (FL_ISSET(ecp->iflags, E_C_COUNT)) {
if (db_last(sp, &lno))
goto err;
ecp->addr2.lno = lno;
} else {
ex_badaddr(sp, NULL, A_EOF, NUM_OK);
goto err;
}
/* FALLTHROUGH */
case 1:
if (ecp->addr1.lno == 0) {
if (!F_ISSET(ecp, E_ADDR_ZERO) &&
(F_ISSET(sp, SC_EX) ||
!F_ISSET(ecp, E_USELASTCMD))) {
ex_badaddr(sp, ecp->cmd, A_ZERO, NUM_OK);
goto err;
}
} else if (!db_exist(sp, ecp->addr1.lno)) {
ex_badaddr(sp, NULL, A_EOF, NUM_OK);
goto err;
}
break;
}
/*
* If doing a default command and there's nothing left on the line,
* vi just moves to the line. For example, ":3" and ":'a,'b" just
* move to line 3 and line 'b, respectively, but ":3|" prints line 3.
*
* !!!
* In addition, IF THE LINE CHANGES, move to the first nonblank of
* the line.
*
* !!!
* This is done before the absolute mark gets set; historically,
* "/a/,/b/" did NOT set vi's absolute mark, but "/a/,/b/d" did.
*/
if ((F_ISSET(sp, SC_VI) || F_ISSET(ecp, E_NOPRDEF)) &&
F_ISSET(ecp, E_USELASTCMD) && vi_address == 0) {
switch (ecp->addrcnt) {
case 2:
if (sp->lno !=
(ecp->addr2.lno ? ecp->addr2.lno : 1)) {
sp->lno =
ecp->addr2.lno ? ecp->addr2.lno : 1;
sp->cno = 0;
(void)nonblank(sp, sp->lno, &sp->cno);
}
break;
case 1:
if (sp->lno !=
(ecp->addr1.lno ? ecp->addr1.lno : 1)) {
sp->lno =
ecp->addr1.lno ? ecp->addr1.lno : 1;
sp->cno = 0;
(void)nonblank(sp, sp->lno, &sp->cno);
}
break;
}
ecp->cp = ecp->save_cmd;
ecp->clen = ecp->save_cmdlen;
goto loop;
}
/*
* Set the absolute mark -- we have to set it for vi here, in case
* it's a compound command, e.g. ":5p|6" should set the absolute
* mark for vi.
*/
if (F_ISSET(ecp, E_ABSMARK)) {
cur.lno = sp->lno;
cur.cno = sp->cno;
F_CLR(ecp, E_ABSMARK);
if (mark_set(sp, ABSMARK1, &cur, 1))
goto err;
}
#if defined(DEBUG) && defined(COMLOG)
ex_comlog(sp, ecp);
#endif
/* Increment the command count if not called from vi. */
if (F_ISSET(sp, SC_EX))
++sp->ccnt;
/*
* If file state available, and not doing a global command,
* log the start of an action.
*/
if (sp->ep != NULL && !F_ISSET(sp, SC_EX_GLOBAL))
(void)log_cursor(sp);
/*
* !!!
* There are two special commands for the purposes of this code: the
* default command (<carriage-return>) or the scrolling commands (^D
* and <EOF>) as the first non-<blank> characters in the line.
*
* If this is the first command in the command line, we received the
* command from the ex command loop and we're talking to a tty, and
* and there's nothing else on the command line, and it's one of the
* special commands, we move back up to the previous line, and erase
* the prompt character with the output. Since ex runs in canonical
* mode, we don't have to do anything else, a <newline> has already
* been echoed by the tty driver. It's OK if vi calls us -- we won't
* be in ex mode so we'll do nothing.
*/
if (F_ISSET(ecp, E_NRSEP)) {
if (sp->ep != NULL &&
F_ISSET(sp, SC_EX) && F_ISSET(gp, G_STDIN_TTY) &&
(F_ISSET(ecp, E_USELASTCMD) || ecp->cmd == &cmds[C_SCROLL]))
gp->scr_ex_adjust(sp, EX_TERM_SCROLL);
F_CLR(ecp, E_NRSEP);
}
/*
* Call the underlying function for the ex command.
*
* XXX
* Interrupts behave like errors, for now.
*/
if (ecp->cmd->fn(sp, ecp) || INTERRUPTED(sp)) {
if (!F_ISSET(gp, G_STDIN_TTY))
F_SET(sp, SC_EXIT_FORCE);
goto err;
}
#ifdef DEBUG
/* Make sure no function left global temporary space locked. */
if (F_ISSET(gp, G_TMP_INUSE)) {
F_CLR(gp, G_TMP_INUSE);
msgq(sp, M_ERR, "087|%s: temporary buffer not released",
ecp->cmd->name);
}
#endif
/*
* Ex displayed the number of lines modified immediately after each
* command, so the command "1,10d|1,10d" would display:
*
* 10 lines deleted
* 10 lines deleted
* <autoprint line>
*
* Executing ex commands from vi only reported the final modified
* lines message -- that's wrong enough that we don't match it.
*/
if (F_ISSET(sp, SC_EX))
mod_rpt(sp);
/*
* Integrate any offset parsed by the underlying command, and make
* sure the referenced line exists.
*
* XXX
* May not match historic practice (which I've never been able to
* completely figure out.) For example, the '=' command from vi
* mode often got the offset wrong, and complained it was too large,
* but didn't seem to have a problem with the cursor. If anyone
* complains, ask them how it's supposed to work, they might know.
*/
if (sp->ep != NULL && ecp->flagoff) {
if (ecp->flagoff < 0) {
if (sp->lno <= -ecp->flagoff) {
msgq(sp, M_ERR,
"088|Flag offset to before line 1");
goto err;
}
} else {
if (!NPFITS(MAX_REC_NUMBER, sp->lno, ecp->flagoff)) {
ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
goto err;
}
if (!db_exist(sp, sp->lno + ecp->flagoff)) {
msgq(sp, M_ERR,
"089|Flag offset past end-of-file");
goto err;
}
}
sp->lno += ecp->flagoff;
}
/*
* If the command was successful may want to display a line based on
* the autoprint option or an explicit print flag. (Make sure that
* there's a line to display.) Also, the autoprint edit option is
* turned off for the duration of global commands.
*/
if (F_ISSET(sp, SC_EX) && sp->ep != NULL && sp->lno != 0) {
/*
* The print commands have already handled the `print' flags.
* If so, clear them.
*/
if (FL_ISSET(ecp->iflags, E_CLRFLAG))
FL_CLR(ecp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT);
/* If hash set only because of the number option, discard it. */
if (F_ISSET(ecp, E_OPTNUM))
FL_CLR(ecp->iflags, E_C_HASH);
/*
* If there was an explicit flag to display the new cursor line,
* or autoprint is set and a change was made, display the line.
* If any print flags were set use them, else default to print.
*/
LF_INIT(FL_ISSET(ecp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT));
if (!LF_ISSET(E_C_HASH | E_C_LIST | E_C_PRINT | E_NOAUTO) &&
!F_ISSET(sp, SC_EX_GLOBAL) &&
O_ISSET(sp, O_AUTOPRINT) && F_ISSET(ecp, E_AUTOPRINT))
LF_INIT(E_C_PRINT);
if (LF_ISSET(E_C_HASH | E_C_LIST | E_C_PRINT)) {
cur.lno = sp->lno;
cur.cno = 0;
(void)ex_print(sp, ecp, &cur, &cur, flags);
}
}
/*
* If the command had an associated "+cmd", it has to be executed
* before we finish executing any more of this ex command. For
* example, consider a .exrc file that contains the following lines:
*
* :set all
* :edit +25 file.c|s/abc/ABC/|1
* :3,5 print
*
* This can happen more than once -- the historic vi simply hung or
* dropped core, of course. Prepend the + command back into the
* current command and continue. We may have to add an additional
* <literal next> character. We know that it will fit because we
* discarded at least one space and the + character.
*/
if (arg1_len != 0) {
/*
* If the last character of the + command was a <literal next>
* character, it would be treated differently because of the
* append. Quote it, if necessary.
*/
if (IS_ESCAPE(sp, ecp, arg1[arg1_len - 1])) {
*--ecp->save_cmd = CH_LITERAL;
++ecp->save_cmdlen;
}
ecp->save_cmd -= arg1_len;
ecp->save_cmdlen += arg1_len;
memmove(ecp->save_cmd, arg1, arg1_len);
/*
* Any commands executed from a +cmd are executed starting at
* the first column of the last line of the file -- NOT the
* first nonblank.) The main file startup code doesn't know
* that a +cmd was set, however, so it may have put us at the
* top of the file. (Note, this is safe because we must have
* switched files to get here.)
*/
F_SET(ecp, E_MOVETOEND);
}
/* Update the current command. */
ecp->cp = ecp->save_cmd;
ecp->clen = ecp->save_cmdlen;
/*
* !!!
* If we've changed screens or underlying files, any pending global or
* v command, or @ buffer that has associated addresses, has to be
* discarded. This is historic practice for globals, and necessary for
* @ buffers that had associated addresses.
*
* Otherwise, if we've changed underlying files, it's not a problem,
* we continue with the rest of the ex command(s), operating on the
* new file. However, if we switch screens (either by exiting or by
* an explicit command), we have no way of knowing where to put output
* messages, and, since we don't control screens here, we could screw
* up the upper layers, (e.g. we could exit/reenter a screen multiple
* times). So, return and continue after we've got a new screen.
*/
if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE | SC_FSWITCH | SC_SSWITCH)) {
at_found = gv_found = 0;
for (ecp = sp->gp->ecq.lh_first;
ecp != NULL; ecp = ecp->q.le_next)
switch (ecp->agv_flags) {
case 0:
case AGV_AT_NORANGE:
break;
case AGV_AT:
if (!at_found) {
at_found = 1;
msgq(sp, M_ERR,
"090|@ with range running when the file/screen changed");
}
break;
case AGV_GLOBAL:
case AGV_V:
if (!gv_found) {
gv_found = 1;
msgq(sp, M_ERR,
"091|Global/v command running when the file/screen changed");
}
break;
default:
abort();
}
if (at_found || gv_found)
goto discard;
if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE | SC_SSWITCH))
goto rsuccess;
}
goto loop;
/* NOTREACHED */
err: /*
* On command failure, we discard keys and pending commands remaining,
* as well as any keys that were mapped and waiting. The save_cmdlen
* test is not necessarily correct. If we fail early enough we don't
* know if the entire string was a single command or not. Guess, as
* it's useful to know if commands other than the current one are being
* discarded.
*/
if (ecp->save_cmdlen == 0)
for (; ecp->clen; --ecp->clen) {
ch = *ecp->cp++;
if (IS_ESCAPE(sp, ecp, ch) && ecp->clen > 1) {
--ecp->clen;
++ecp->cp;
} else if (ch == '\n' || ch == '|') {
if (ecp->clen > 1)
ecp->save_cmdlen = 1;
break;
}
}
if (ecp->save_cmdlen != 0 || gp->ecq.lh_first != &gp->excmd) {
discard: msgq(sp, M_BERR,
"092|Ex command failed: pending commands discarded");
ex_discard(sp);
}
if (v_event_flush(sp, CH_MAPPED))
msgq(sp, M_BERR,
"093|Ex command failed: mapped keys discarded");
rfail: tmp = 1;
if (0)
rsuccess: tmp = 0;
/* Turn off any file name error information. */
gp->if_name = NULL;
/* Turn off the global bit. */
F_CLR(sp, SC_EX_GLOBAL);
return (tmp);
}
/*
* ex_range --
* Get a line range for ex commands, or perform a vi ex address search.
*
* PUBLIC: int ex_range __P((SCR *, EXCMD *, int *));
*/
int
ex_range(sp, ecp, errp)
SCR *sp;
EXCMD *ecp;
int *errp;
{
enum { ADDR_FOUND, ADDR_NEED, ADDR_NONE } addr;
GS *gp;
EX_PRIVATE *exp;
MARK m;
int isaddr;
*errp = 0;
/*
* Parse comma or semi-colon delimited line specs.
*
* Semi-colon delimiters update the current address to be the last
* address. For example, the command
*
* :3;/pattern/ecp->cp
*
* will search for pattern from line 3. In addition, if ecp->cp
* is not a valid command, the current line will be left at 3, not
* at the original address.
*
* Extra addresses are discarded, starting with the first.
*
* !!!
* If any addresses are missing, they default to the current line.
* This was historically true for both leading and trailing comma
* delimited addresses as well as for trailing semicolon delimited
* addresses. For consistency, we make it true for leading semicolon
* addresses as well.
*/
gp = sp->gp;
exp = EXP(sp);
for (addr = ADDR_NONE, ecp->addrcnt = 0; ecp->clen > 0;)
switch (*ecp->cp) {
case '%': /* Entire file. */
/* Vi ex address searches didn't permit % signs. */
if (F_ISSET(ecp, E_VISEARCH))
goto ret;
/* It's an error if the file is empty. */
if (sp->ep == NULL) {
ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
*errp = 1;
return (0);
}
/*
* !!!
* A percent character addresses all of the lines in
* the file. Historically, it couldn't be followed by
* any other address. We do it as a text substitution
* for simplicity. POSIX 1003.2 is expected to follow
* this practice.
*
* If it's an empty file, the first line is 0, not 1.
*/
if (addr == ADDR_FOUND) {
ex_badaddr(sp, NULL, A_COMBO, NUM_OK);
*errp = 1;
return (0);
}
if (db_last(sp, &ecp->addr2.lno))
return (1);
ecp->addr1.lno = ecp->addr2.lno == 0 ? 0 : 1;
ecp->addr1.cno = ecp->addr2.cno = 0;
ecp->addrcnt = 2;
addr = ADDR_FOUND;
++ecp->cp;
--ecp->clen;
break;
case ',': /* Comma delimiter. */
/* Vi ex address searches didn't permit commas. */
if (F_ISSET(ecp, E_VISEARCH))
goto ret;
/* FALLTHROUGH */
case ';': /* Semi-colon delimiter. */
if (sp->ep == NULL) {
ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
*errp = 1;
return (0);
}
if (addr != ADDR_FOUND)
switch (ecp->addrcnt) {
case 0:
ecp->addr1.lno = sp->lno;
ecp->addr1.cno = sp->cno;
ecp->addrcnt = 1;
break;
case 2:
ecp->addr1 = ecp->addr2;
/* FALLTHROUGH */
case 1:
ecp->addr2.lno = sp->lno;
ecp->addr2.cno = sp->cno;
ecp->addrcnt = 2;
break;
}
if (*ecp->cp == ';')
switch (ecp->addrcnt) {
case 0:
abort();
/* NOTREACHED */
case 1:
sp->lno = ecp->addr1.lno;
sp->cno = ecp->addr1.cno;
break;
case 2:
sp->lno = ecp->addr2.lno;
sp->cno = ecp->addr2.cno;
break;
}
addr = ADDR_NEED;
/* FALLTHROUGH */
case ' ': /* Whitespace. */
case '\t': /* Whitespace. */
++ecp->cp;
--ecp->clen;
break;
default:
/* Get a line specification. */
if (ex_line(sp, ecp, &m, &isaddr, errp))
return (1);
if (*errp)
return (0);
if (!isaddr)
goto ret;
if (addr == ADDR_FOUND) {
ex_badaddr(sp, NULL, A_COMBO, NUM_OK);
*errp = 1;
return (0);
}
switch (ecp->addrcnt) {
case 0:
ecp->addr1 = m;
ecp->addrcnt = 1;
break;
case 1:
ecp->addr2 = m;
ecp->addrcnt = 2;
break;
case 2:
ecp->addr1 = ecp->addr2;
ecp->addr2 = m;
break;
}
addr = ADDR_FOUND;
break;
}
/*
* !!!
* Vi ex address searches are indifferent to order or trailing
* semi-colons.
*/
ret: if (F_ISSET(ecp, E_VISEARCH))
return (0);
if (addr == ADDR_NEED)
switch (ecp->addrcnt) {
case 0:
ecp->addr1.lno = sp->lno;
ecp->addr1.cno = sp->cno;
ecp->addrcnt = 1;
break;
case 2:
ecp->addr1 = ecp->addr2;
/* FALLTHROUGH */
case 1:
ecp->addr2.lno = sp->lno;
ecp->addr2.cno = sp->cno;
ecp->addrcnt = 2;
break;
}
if (ecp->addrcnt == 2 && ecp->addr2.lno < ecp->addr1.lno) {
msgq(sp, M_ERR,
"094|The second address is smaller than the first");
*errp = 1;
}
return (0);
}
/*
* ex_line --
* Get a single line address specifier.
*
* The way the "previous context" mark worked was that any "non-relative"
* motion set it. While ex/vi wasn't totally consistent about this, ANY
* numeric address, search pattern, '$', or mark reference in an address
* was considered non-relative, and set the value. Which should explain
* why we're hacking marks down here. The problem was that the mark was
* only set if the command was called, i.e. we have to set a flag and test
* it later.
*
* XXX
* This is probably still not exactly historic practice, although I think
* it's fairly close.
*/
static int
ex_line(sp, ecp, mp, isaddrp, errp)
SCR *sp;
EXCMD *ecp;
MARK *mp;
int *isaddrp, *errp;
{
enum nresult nret;
EX_PRIVATE *exp;
GS *gp;
long total, val;
int isneg;
int (*sf) __P((SCR *, MARK *, MARK *, char *, size_t, char **, u_int));
char *endp;
gp = sp->gp;
exp = EXP(sp);
*isaddrp = *errp = 0;
F_CLR(ecp, E_DELTA);
/* No addresses permitted until a file has been read in. */
if (sp->ep == NULL && strchr("$0123456789'\\/?.+-^", *ecp->cp)) {
ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
*errp = 1;
return (0);
}
switch (*ecp->cp) {
case '$': /* Last line in the file. */
*isaddrp = 1;
F_SET(ecp, E_ABSMARK);
mp->cno = 0;
if (db_last(sp, &mp->lno))
return (1);
++ecp->cp;
--ecp->clen;
break; /* Absolute line number. */
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
*isaddrp = 1;
F_SET(ecp, E_ABSMARK);
if ((nret = nget_slong(&val, ecp->cp, &endp, 10)) != NUM_OK) {
ex_badaddr(sp, NULL, A_NOTSET, nret);
*errp = 1;
return (0);
}
if (!NPFITS(MAX_REC_NUMBER, 0, val)) {
ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
*errp = 1;
return (0);
}
mp->lno = val;
mp->cno = 0;
ecp->clen -= (endp - ecp->cp);
ecp->cp = endp;
break;
case '\'': /* Use a mark. */
*isaddrp = 1;
F_SET(ecp, E_ABSMARK);
if (ecp->clen == 1) {
msgq(sp, M_ERR, "095|No mark name supplied");
*errp = 1;
return (0);
}
if (mark_get(sp, ecp->cp[1], mp, M_ERR)) {
*errp = 1;
return (0);
}
ecp->cp += 2;
ecp->clen -= 2;
break;
case '\\': /* Search: forward/backward. */
/*
* !!!
* I can't find any difference between // and \/ or between
* ?? and \?. Mark Horton doesn't remember there being any
* difference. C'est la vie.
*/
if (ecp->clen < 2 ||
ecp->cp[1] != '/' && ecp->cp[1] != '?') {
msgq(sp, M_ERR, "096|\\ not followed by / or ?");
*errp = 1;
return (0);
}
++ecp->cp;
--ecp->clen;
sf = ecp->cp[0] == '/' ? f_search : b_search;
goto search;
case '/': /* Search forward. */
sf = f_search;
goto search;
case '?': /* Search backward. */
sf = b_search;
search: mp->lno = sp->lno;
mp->cno = sp->cno;
if (sf(sp, mp, mp, ecp->cp, ecp->clen, &endp,
SEARCH_MSG | SEARCH_PARSE | SEARCH_SET |
(F_ISSET(ecp, E_SEARCH_WMSG) ? SEARCH_WMSG : 0))) {
*errp = 1;
return (0);
}
/* Fix up the command pointers. */
ecp->clen -= (endp - ecp->cp);
ecp->cp = endp;
*isaddrp = 1;
F_SET(ecp, E_ABSMARK);
break;
case '.': /* Current position. */
*isaddrp = 1;
mp->cno = sp->cno;
/* If an empty file, then '.' is 0, not 1. */
if (sp->lno == 1) {
if (db_last(sp, &mp->lno))
return (1);
if (mp->lno != 0)
mp->lno = 1;
} else
mp->lno = sp->lno;
/*
* !!!
* Historically, .<number> was the same as .+<number>, i.e.
* the '+' could be omitted. (This feature is found in ed
* as well.)
*/
if (ecp->clen > 1 && isdigit(ecp->cp[1]))
*ecp->cp = '+';
else {
++ecp->cp;
--ecp->clen;
}
break;
}
/* Skip trailing <blank>s. */
for (; ecp->clen > 0 &&
isblank(ecp->cp[0]); ++ecp->cp, --ecp->clen);
/*
* Evaluate any offset. If no address yet found, the offset
* is relative to ".".
*/
total = 0;
if (ecp->clen != 0 && (isdigit(ecp->cp[0]) ||
ecp->cp[0] == '+' || ecp->cp[0] == '-' ||
ecp->cp[0] == '^')) {
if (!*isaddrp) {
*isaddrp = 1;
mp->lno = sp->lno;
mp->cno = sp->cno;
}
/*
* Evaluate an offset, defined as:
*
* [+-^<blank>]*[<blank>]*[0-9]*
*
* The rough translation is any number of signs, optionally
* followed by numbers, or a number by itself, all <blank>
* separated.
*
* !!!
* All address offsets were additive, e.g. "2 2 3p" was the
* same as "7p", or, "/ZZZ/ 2" was the same as "/ZZZ/+2".
* Note, however, "2 /ZZZ/" was an error. It was also legal
* to insert signs without numbers, so "3 - 2" was legal, and
* equal to 4.
*
* !!!
* Offsets were historically permitted for any line address,
* e.g. the command "1,2 copy 2 2 2 2" copied lines 1,2 after
* line 8.
*
* !!!
* Offsets were historically permitted for search commands,
* and handled as addresses: "/pattern/2 2 2" was legal, and
* referenced the 6th line after pattern.
*/
F_SET(ecp, E_DELTA);
for (;;) {
for (; ecp->clen > 0 && isblank(ecp->cp[0]);
++ecp->cp, --ecp->clen);
if (ecp->clen == 0 || !isdigit(ecp->cp[0]) &&
ecp->cp[0] != '+' && ecp->cp[0] != '-' &&
ecp->cp[0] != '^')
break;
if (!isdigit(ecp->cp[0]) &&
!isdigit(ecp->cp[1])) {
total += ecp->cp[0] == '+' ? 1 : -1;
--ecp->clen;
++ecp->cp;
} else {
if (ecp->cp[0] == '-' ||
ecp->cp[0] == '^') {
++ecp->cp;
--ecp->clen;
isneg = 1;
} else
isneg = 0;
/* Get a signed long, add it to the total. */
if ((nret = nget_slong(&val,
ecp->cp, &endp, 10)) != NUM_OK ||
(nret = NADD_SLONG(sp,
total, val)) != NUM_OK) {
ex_badaddr(sp, NULL, A_NOTSET, nret);
*errp = 1;
return (0);
}
total += isneg ? -val : val;
ecp->clen -= (endp - ecp->cp);
ecp->cp = endp;
}
}
}
/*
* Any value less than 0 is an error. Make sure that the new value
* will fit into a recno_t.
*/
if (*isaddrp && total != 0) {
if (total < 0) {
if (-total > mp->lno) {
msgq(sp, M_ERR,
"097|Reference to a line number less than 0");
*errp = 1;
return (0);
}
} else
if (!NPFITS(MAX_REC_NUMBER, mp->lno, total)) {
ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
*errp = 1;
return (0);
}
mp->lno += total;
}
return (0);
}
/*
* ex_load --
* Load up the next command, which may be an @ buffer or global command.
*/
static int
ex_load(sp)
SCR *sp;
{
GS *gp;
EXCMD *ecp;
RANGE *rp;
F_CLR(sp, SC_EX_GLOBAL);
/*
* Lose any exhausted commands. We know that the first command
* can't be an AGV command, which makes things a bit easier.
*/
for (gp = sp->gp;;) {
/*
* If we're back to the original structure, leave it around,
* but discard any allocated source name, we've returned to
* the beginning of the command stack.
*/
if ((ecp = gp->ecq.lh_first) == &gp->excmd) {
if (F_ISSET(ecp, E_NAMEDISCARD)) {
free(ecp->if_name);
ecp->if_name = NULL;
}
return (0);
}
/*
* ecp->clen will be 0 for the first discarded command, but
* may not be 0 for subsequent ones, e.g. if the original
* command was ":g/xx/@a|s/b/c/", then when we discard the
* command pushed on the stack by the @a, we have to resume
* the global command which included the substitute command.
*/
if (ecp->clen != 0)
return (0);
/*
* If it's an @, global or v command, we may need to continue
* the command on a different line.
*/
if (FL_ISSET(ecp->agv_flags, AGV_ALL)) {
/* Discard any exhausted ranges. */
while ((rp = ecp->rq.cqh_first) != (void *)&ecp->rq)
if (rp->start > rp->stop) {
CIRCLEQ_REMOVE(&ecp->rq, rp, q);
free(rp);
} else
break;
/* If there's another range, continue with it. */
if (rp != (void *)&ecp->rq)
break;
/* If it's a global/v command, fix up the last line. */
if (FL_ISSET(ecp->agv_flags,
AGV_GLOBAL | AGV_V) && ecp->range_lno != OOBLNO)
if (db_exist(sp, ecp->range_lno))
sp->lno = ecp->range_lno;
else {
if (db_last(sp, &sp->lno))
return (1);
if (sp->lno == 0)
sp->lno = 1;
}
free(ecp->o_cp);
}
/* Discard the EXCMD. */
LIST_REMOVE(ecp, q);
free(ecp);
}
/*
* We only get here if it's an active @, global or v command. Set
* the current line number, and get a new copy of the command for
* the parser. Note, the original pointer almost certainly moved,
* so we have play games.
*/
ecp->cp = ecp->o_cp;
memmove(ecp->cp, ecp->cp + ecp->o_clen, ecp->o_clen);
ecp->clen = ecp->o_clen;
ecp->range_lno = sp->lno = rp->start++;
if (FL_ISSET(ecp->agv_flags, AGV_GLOBAL | AGV_V))
F_SET(sp, SC_EX_GLOBAL);
return (0);
}
/*
* ex_discard --
* Discard any pending ex commands.
*/
static int
ex_discard(sp)
SCR *sp;
{
GS *gp;
EXCMD *ecp;
RANGE *rp;
/*
* We know the first command can't be an AGV command, so we don't
* process it specially. We do, however, nail the command itself.
*/
for (gp = sp->gp; (ecp = gp->ecq.lh_first) != &gp->excmd;) {
if (FL_ISSET(ecp->agv_flags, AGV_ALL)) {
while ((rp = ecp->rq.cqh_first) != (void *)&ecp->rq) {
CIRCLEQ_REMOVE(&ecp->rq, rp, q);
free(rp);
}
free(ecp->o_cp);
}
LIST_REMOVE(ecp, q);
free(ecp);
}
gp->ecq.lh_first->clen = 0;
return (0);
}
/*
* ex_unknown --
* Display an unknown command name.
*/
static void
ex_unknown(sp, cmd, len)
SCR *sp;
char *cmd;
size_t len;
{
size_t blen;
char *bp;
GET_SPACE_GOTO(sp, bp, blen, len + 1);
bp[len] = '\0';
memmove(bp, cmd, len);
msgq_str(sp, M_ERR, bp, "098|The %s command is unknown");
FREE_SPACE(sp, bp, blen);
alloc_err:
return;
}
/*
* ex_is_abbrev -
* The vi text input routine needs to know if ex thinks this is an
* [un]abbreviate command, so it can turn off abbreviations. See
* the usual ranting in the vi/v_txt_ev.c:txt_abbrev() routine.
*
* PUBLIC: int ex_is_abbrev __P((char *, size_t));
*/
int
ex_is_abbrev(name, len)
char *name;
size_t len;
{
EXCMDLIST const *cp;
return ((cp = ex_comm_search(name, len)) != NULL &&
(cp == &cmds[C_ABBR] || cp == &cmds[C_UNABBREVIATE]));
}
/*
* ex_is_unmap -
* The vi text input routine needs to know if ex thinks this is an
* unmap command, so it can turn off input mapping. See the usual
* ranting in the vi/v_txt_ev.c:txt_unmap() routine.
*
* PUBLIC: int ex_is_unmap __P((char *, size_t));
*/
int
ex_is_unmap(name, len)
char *name;
size_t len;
{
EXCMDLIST const *cp;
/*
* The command the vi input routines are really interested in
* is "unmap!", not just unmap.
*/
if (name[len - 1] != '!')
return (0);
--len;
return ((cp = ex_comm_search(name, len)) != NULL &&
cp == &cmds[C_UNMAP]);
}
/*
* ex_comm_search --
* Search for a command name.
*/
static EXCMDLIST const *
ex_comm_search(name, len)
char *name;
size_t len;
{
EXCMDLIST const *cp;
for (cp = cmds; cp->name != NULL; ++cp) {
if (cp->name[0] > name[0])
return (NULL);
if (cp->name[0] != name[0])
continue;
if (!memcmp(name, cp->name, len))
return (cp);
}
return (NULL);
}
/*
* ex_badaddr --
* Display a bad address message.
*
* PUBLIC: void ex_badaddr
* PUBLIC: __P((SCR *, EXCMDLIST const *, enum badaddr, enum nresult));
*/
void
ex_badaddr(sp, cp, ba, nret)
SCR *sp;
EXCMDLIST const *cp;
enum badaddr ba;
enum nresult nret;
{
recno_t lno;
switch (nret) {
case NUM_OK:
break;
case NUM_ERR:
msgq(sp, M_SYSERR, NULL);
return;
case NUM_OVER:
msgq(sp, M_ERR, "099|Address value overflow");
return;
case NUM_UNDER:
msgq(sp, M_ERR, "100|Address value underflow");
return;
}
/*
* When encountering an address error, tell the user if there's no
* underlying file, that's the real problem.
*/
if (sp->ep == NULL) {
ex_emsg(sp, cp->name, EXM_NOFILEYET);
return;
}
switch (ba) {
case A_COMBO:
msgq(sp, M_ERR, "101|Illegal address combination");
break;
case A_EOF:
if (db_last(sp, &lno))
return;
if (lno != 0) {
msgq(sp, M_ERR,
"102|Illegal address: only %lu lines in the file",
lno);
break;
}
/* FALLTHROUGH */
case A_EMPTY:
msgq(sp, M_ERR, "103|Illegal address: the file is empty");
break;
case A_NOTSET:
abort();
/* NOTREACHED */
case A_ZERO:
msgq(sp, M_ERR,
"104|The %s command doesn't permit an address of 0",
cp->name);
break;
}
return;
}
#if defined(DEBUG) && defined(COMLOG)
/*
* ex_comlog --
* Log ex commands.
*/
static void
ex_comlog(sp, ecp)
SCR *sp;
EXCMD *ecp;
{
TRACE(sp, "ecmd: %s", ecp->cmd->name);
if (ecp->addrcnt > 0) {
TRACE(sp, " a1 %d", ecp->addr1.lno);
if (ecp->addrcnt > 1)
TRACE(sp, " a2: %d", ecp->addr2.lno);
}
if (ecp->lineno)
TRACE(sp, " line %d", ecp->lineno);
if (ecp->flags)
TRACE(sp, " flags 0x%x", ecp->flags);
if (F_ISSET(&exc, E_BUFFER))
TRACE(sp, " buffer %c", ecp->buffer);
if (ecp->argc)
for (cnt = 0; cnt < ecp->argc; ++cnt)
TRACE(sp, " arg %d: {%s}", cnt, ecp->argv[cnt]->bp);
TRACE(sp, "\n");
}
#endif
|