ed.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * Editor
  11. */
  12. #include <u.h>
  13. #include <libc.h>
  14. #include <bio.h>
  15. #include <regexp.h>
  16. enum
  17. {
  18. FNSIZE = 128, /* file name */
  19. LBSIZE = 4096, /* max line size */
  20. BLKSIZE = 4096, /* block size in temp file */
  21. NBLK = 8191, /* max size of temp file */
  22. ESIZE = 256, /* max size of reg exp */
  23. GBSIZE = 256, /* max size of global command */
  24. MAXSUB = 9, /* max number of sub reg exp */
  25. ESCFLG = Runemax, /* escape Rune - user defined code */
  26. EOF = -1,
  27. };
  28. void (*oldhup)(int);
  29. void (*oldquit)(int);
  30. int* addr1;
  31. int* addr2;
  32. int anymarks;
  33. Biobuf bcons;
  34. int col;
  35. int32_t count;
  36. int* dol;
  37. int* dot;
  38. int fchange;
  39. char file[FNSIZE];
  40. Rune genbuf[LBSIZE];
  41. int given;
  42. Rune* globp;
  43. int iblock;
  44. int ichanged;
  45. int io;
  46. Biobuf iobuf;
  47. int lastc;
  48. char line[70];
  49. Rune* linebp;
  50. Rune linebuf[LBSIZE];
  51. int listf;
  52. int listn;
  53. Rune* loc1;
  54. Rune* loc2;
  55. int names[26];
  56. int nleft;
  57. int oblock;
  58. int oflag;
  59. Reprog *pattern;
  60. int peekc;
  61. int pflag;
  62. int rescuing;
  63. Rune rhsbuf[LBSIZE/sizeof(Rune)];
  64. char savedfile[FNSIZE];
  65. jmp_buf savej;
  66. int subnewa;
  67. int subolda;
  68. Resub subexp[MAXSUB];
  69. char* tfname;
  70. int tline;
  71. int waiting;
  72. int wrapp;
  73. int* zero;
  74. char Q[] = "";
  75. char T[] = "TMP";
  76. char WRERR[] = "WRITE ERROR";
  77. int bpagesize = 20;
  78. char hex[] = "0123456789abcdef";
  79. char* linp = line;
  80. uint32_t nlall = 128;
  81. int tfile = -1;
  82. int vflag = 1;
  83. void add(int);
  84. int* address(void);
  85. int append(int(*)(void), int*);
  86. void browse(void);
  87. void callunix(void);
  88. void commands(void);
  89. void compile(int);
  90. int compsub(void);
  91. void dosub(void);
  92. void error(char*);
  93. int match(int*);
  94. void exfile(int);
  95. void filename(int);
  96. Rune* getblock(int, int);
  97. int getchr(void);
  98. int getcopy(void);
  99. int getfile(void);
  100. Rune* getline(int);
  101. int getnum(void);
  102. int getsub(void);
  103. int gettty(void);
  104. void global(int);
  105. void init(void);
  106. void join(void);
  107. void move(int);
  108. void newline(void);
  109. void nonzero(void);
  110. void notifyf(void*, char*);
  111. Rune* place(Rune*, Rune*, Rune*);
  112. void printcom(void);
  113. void putchr(int);
  114. void putd(void);
  115. void putfile(void);
  116. int putline(void);
  117. void putshst(Rune*);
  118. void putst(char*);
  119. void quit(void);
  120. void rdelete(int*, int*);
  121. void regerror(char *);
  122. void reverse(int*, int*);
  123. void setnoaddr(void);
  124. void setwide(void);
  125. void squeeze(int);
  126. void substitute(int);
  127. void
  128. main(int argc, char *argv[])
  129. {
  130. char *p1, *p2;
  131. Binit(&bcons, 0, OREAD);
  132. notify(notifyf);
  133. ARGBEGIN {
  134. case 'o':
  135. oflag = 1;
  136. vflag = 0;
  137. break;
  138. } ARGEND
  139. USED(argc);
  140. if(*argv && (strcmp(*argv, "-") == 0)) {
  141. argv++;
  142. vflag = 0;
  143. }
  144. if(oflag) {
  145. p1 = "/fd/1";
  146. p2 = savedfile;
  147. while(*p2++ = *p1++)
  148. ;
  149. globp = L"a";
  150. } else
  151. if(*argv) {
  152. p1 = *argv;
  153. p2 = savedfile;
  154. while(*p2++ = *p1++)
  155. if(p2 >= &savedfile[sizeof(savedfile)])
  156. p2--;
  157. globp = L"r";
  158. }
  159. zero = malloc((nlall+5)*sizeof(int*));
  160. tfname = mktemp("/tmp/eXXXXX");
  161. init();
  162. setjmp(savej);
  163. commands();
  164. quit();
  165. }
  166. void
  167. commands(void)
  168. {
  169. int *a1, c, temp;
  170. char lastsep;
  171. Dir *d;
  172. for(;;) {
  173. if(pflag) {
  174. pflag = 0;
  175. addr1 = addr2 = dot;
  176. printcom();
  177. }
  178. c = '\n';
  179. for(addr1 = 0;;) {
  180. lastsep = c;
  181. a1 = address();
  182. c = getchr();
  183. if(c != ',' && c != ';')
  184. break;
  185. if(lastsep == ',')
  186. error(Q);
  187. if(a1 == 0) {
  188. a1 = zero+1;
  189. if(a1 > dol)
  190. a1--;
  191. }
  192. addr1 = a1;
  193. if(c == ';')
  194. dot = a1;
  195. }
  196. if(lastsep != '\n' && a1 == 0)
  197. a1 = dol;
  198. if((addr2=a1) == 0) {
  199. given = 0;
  200. addr2 = dot;
  201. } else
  202. given = 1;
  203. if(addr1 == 0)
  204. addr1 = addr2;
  205. switch(c) {
  206. case 'a':
  207. add(0);
  208. continue;
  209. case 'b':
  210. nonzero();
  211. browse();
  212. continue;
  213. case 'c':
  214. nonzero();
  215. newline();
  216. rdelete(addr1, addr2);
  217. append(gettty, addr1-1);
  218. continue;
  219. case 'd':
  220. nonzero();
  221. newline();
  222. rdelete(addr1, addr2);
  223. continue;
  224. case 'E':
  225. fchange = 0;
  226. c = 'e';
  227. case 'e':
  228. setnoaddr();
  229. if(vflag && fchange) {
  230. fchange = 0;
  231. error(Q);
  232. }
  233. filename(c);
  234. init();
  235. addr2 = zero;
  236. goto caseread;
  237. case 'f':
  238. setnoaddr();
  239. filename(c);
  240. putst(savedfile);
  241. continue;
  242. case 'g':
  243. global(1);
  244. continue;
  245. case 'i':
  246. add(-1);
  247. continue;
  248. case 'j':
  249. if(!given)
  250. addr2++;
  251. newline();
  252. join();
  253. continue;
  254. case 'k':
  255. nonzero();
  256. c = getchr();
  257. if(c < 'a' || c > 'z')
  258. error(Q);
  259. newline();
  260. names[c-'a'] = *addr2 & ~01;
  261. anymarks |= 01;
  262. continue;
  263. case 'm':
  264. move(0);
  265. continue;
  266. case 'n':
  267. listn++;
  268. newline();
  269. printcom();
  270. continue;
  271. case '\n':
  272. if(a1==0) {
  273. a1 = dot+1;
  274. addr2 = a1;
  275. addr1 = a1;
  276. }
  277. if(lastsep==';')
  278. addr1 = a1;
  279. printcom();
  280. continue;
  281. case 'l':
  282. listf++;
  283. case 'p':
  284. case 'P':
  285. newline();
  286. printcom();
  287. continue;
  288. case 'Q':
  289. fchange = 0;
  290. case 'q':
  291. setnoaddr();
  292. newline();
  293. quit();
  294. case 'r':
  295. filename(c);
  296. caseread:
  297. if((io=open(file, OREAD)) < 0) {
  298. lastc = '\n';
  299. error(file);
  300. }
  301. if((d = dirfstat(io)) != nil){
  302. if(d->mode & DMAPPEND)
  303. print("warning: %s is append only\n", file);
  304. free(d);
  305. }
  306. Binit(&iobuf, io, OREAD);
  307. setwide();
  308. squeeze(0);
  309. c = zero != dol;
  310. append(getfile, addr2);
  311. exfile(OREAD);
  312. fchange = c;
  313. continue;
  314. case 's':
  315. nonzero();
  316. substitute(globp != 0);
  317. continue;
  318. case 't':
  319. move(1);
  320. continue;
  321. case 'u':
  322. nonzero();
  323. newline();
  324. if((*addr2&~01) != subnewa)
  325. error(Q);
  326. *addr2 = subolda;
  327. dot = addr2;
  328. continue;
  329. case 'v':
  330. global(0);
  331. continue;
  332. case 'W':
  333. wrapp++;
  334. case 'w':
  335. setwide();
  336. squeeze(dol>zero);
  337. temp = getchr();
  338. if(temp != 'q' && temp != 'Q') {
  339. peekc = temp;
  340. temp = 0;
  341. }
  342. filename(c);
  343. if(!wrapp ||
  344. ((io = open(file, OWRITE)) == -1) ||
  345. ((seek(io, 0L, 2)) == -1))
  346. if((io = create(file, OWRITE, 0666)) < 0)
  347. error(file);
  348. Binit(&iobuf, io, OWRITE);
  349. wrapp = 0;
  350. if(dol > zero)
  351. putfile();
  352. exfile(OWRITE);
  353. if(addr1<=zero+1 && addr2==dol)
  354. fchange = 0;
  355. if(temp == 'Q')
  356. fchange = 0;
  357. if(temp)
  358. quit();
  359. continue;
  360. case '=':
  361. setwide();
  362. squeeze(0);
  363. newline();
  364. count = addr2 - zero;
  365. putd();
  366. putchr(L'\n');
  367. continue;
  368. case '!':
  369. callunix();
  370. continue;
  371. case EOF:
  372. return;
  373. }
  374. error(Q);
  375. }
  376. }
  377. void
  378. printcom(void)
  379. {
  380. int *a1;
  381. nonzero();
  382. a1 = addr1;
  383. do {
  384. if(listn) {
  385. count = a1-zero;
  386. putd();
  387. putchr(L'\t');
  388. }
  389. putshst(getline(*a1++));
  390. } while(a1 <= addr2);
  391. dot = addr2;
  392. listf = 0;
  393. listn = 0;
  394. pflag = 0;
  395. }
  396. int*
  397. address(void)
  398. {
  399. int sign, *a, opcnt, nextopand, *b, c;
  400. nextopand = -1;
  401. sign = 1;
  402. opcnt = 0;
  403. a = dot;
  404. do {
  405. do {
  406. c = getchr();
  407. } while(c == ' ' || c == '\t');
  408. if(c >= '0' && c <= '9') {
  409. peekc = c;
  410. if(!opcnt)
  411. a = zero;
  412. a += sign*getnum();
  413. } else
  414. switch(c) {
  415. case '$':
  416. a = dol;
  417. case '.':
  418. if(opcnt)
  419. error(Q);
  420. break;
  421. case '\'':
  422. c = getchr();
  423. if(opcnt || c < 'a' || c > 'z')
  424. error(Q);
  425. a = zero;
  426. do {
  427. a++;
  428. } while(a <= dol && names[c-'a'] != (*a & ~01));
  429. break;
  430. case '?':
  431. sign = -sign;
  432. case '/':
  433. compile(c);
  434. b = a;
  435. for(;;) {
  436. a += sign;
  437. if(a <= zero)
  438. a = dol;
  439. if(a > dol)
  440. a = zero;
  441. if(match(a))
  442. break;
  443. if(a == b)
  444. error(Q);
  445. }
  446. break;
  447. default:
  448. if(nextopand == opcnt) {
  449. a += sign;
  450. if(a < zero || dol < a)
  451. continue; /* error(Q); */
  452. }
  453. if(c != '+' && c != '-' && c != '^') {
  454. peekc = c;
  455. if(opcnt == 0)
  456. a = 0;
  457. return a;
  458. }
  459. sign = 1;
  460. if(c != '+')
  461. sign = -sign;
  462. nextopand = ++opcnt;
  463. continue;
  464. }
  465. sign = 1;
  466. opcnt++;
  467. } while(zero <= a && a <= dol);
  468. error(Q);
  469. return 0;
  470. }
  471. int
  472. getnum(void)
  473. {
  474. int r, c;
  475. r = 0;
  476. for(;;) {
  477. c = getchr();
  478. if(c < '0' || c > '9')
  479. break;
  480. r = r*10 + (c-'0');
  481. }
  482. peekc = c;
  483. return r;
  484. }
  485. void
  486. setwide(void)
  487. {
  488. if(!given) {
  489. addr1 = zero + (dol>zero);
  490. addr2 = dol;
  491. }
  492. }
  493. void
  494. setnoaddr(void)
  495. {
  496. if(given)
  497. error(Q);
  498. }
  499. void
  500. nonzero(void)
  501. {
  502. squeeze(1);
  503. }
  504. void
  505. squeeze(int i)
  506. {
  507. if(addr1 < zero+i || addr2 > dol || addr1 > addr2)
  508. error(Q);
  509. }
  510. void
  511. newline(void)
  512. {
  513. int c;
  514. c = getchr();
  515. if(c == '\n' || c == EOF)
  516. return;
  517. if(c == 'p' || c == 'l' || c == 'n') {
  518. pflag++;
  519. if(c == 'l')
  520. listf++;
  521. else
  522. if(c == 'n')
  523. listn++;
  524. c = getchr();
  525. if(c == '\n')
  526. return;
  527. }
  528. error(Q);
  529. }
  530. void
  531. filename(int comm)
  532. {
  533. char *p1, *p2;
  534. Rune rune;
  535. int c;
  536. count = 0;
  537. c = getchr();
  538. if(c == '\n' || c == EOF) {
  539. p1 = savedfile;
  540. if(*p1 == 0 && comm != 'f')
  541. error(Q);
  542. p2 = file;
  543. while(*p2++ = *p1++)
  544. ;
  545. return;
  546. }
  547. if(c != ' ')
  548. error(Q);
  549. while((c=getchr()) == ' ')
  550. ;
  551. if(c == '\n')
  552. error(Q);
  553. p1 = file;
  554. do {
  555. if(p1 >= &file[sizeof(file)-6] || c == ' ' || c == EOF)
  556. error(Q);
  557. rune = c;
  558. p1 += runetochar(p1, &rune);
  559. } while((c=getchr()) != '\n');
  560. *p1 = 0;
  561. if(savedfile[0] == 0 || comm == 'e' || comm == 'f') {
  562. p1 = savedfile;
  563. p2 = file;
  564. while(*p1++ = *p2++)
  565. ;
  566. }
  567. }
  568. void
  569. exfile(int om)
  570. {
  571. if(om == OWRITE)
  572. if(Bflush(&iobuf) < 0)
  573. error(Q);
  574. close(io);
  575. io = -1;
  576. if(vflag) {
  577. putd();
  578. putchr(L'\n');
  579. }
  580. }
  581. void
  582. error1(char *s)
  583. {
  584. int c;
  585. wrapp = 0;
  586. listf = 0;
  587. listn = 0;
  588. count = 0;
  589. seek(0, 0, 2);
  590. pflag = 0;
  591. if(globp)
  592. lastc = '\n';
  593. globp = 0;
  594. peekc = lastc;
  595. if(lastc)
  596. for(;;) {
  597. c = getchr();
  598. if(c == '\n' || c == EOF)
  599. break;
  600. }
  601. if(io > 0) {
  602. close(io);
  603. io = -1;
  604. }
  605. putchr(L'?');
  606. putst(s);
  607. }
  608. void
  609. error(char *s)
  610. {
  611. error1(s);
  612. longjmp(savej, 1);
  613. }
  614. void
  615. rescue(void)
  616. {
  617. rescuing = 1;
  618. if(dol > zero) {
  619. addr1 = zero+1;
  620. addr2 = dol;
  621. io = create("ed.hup", OWRITE, 0666);
  622. if(io > 0){
  623. Binit(&iobuf, io, OWRITE);
  624. putfile();
  625. }
  626. }
  627. fchange = 0;
  628. quit();
  629. }
  630. void
  631. notifyf(void *a, char *s)
  632. {
  633. if(strcmp(s, "interrupt") == 0){
  634. if(rescuing || waiting)
  635. noted(NCONT);
  636. putchr(L'\n');
  637. lastc = '\n';
  638. error1(Q);
  639. notejmp(a, savej, 0);
  640. }
  641. if(strcmp(s, "hangup") == 0){
  642. if(rescuing)
  643. noted(NDFLT);
  644. rescue();
  645. }
  646. fprint(2, "ed: note: %s\n", s);
  647. abort();
  648. }
  649. int
  650. getchr(void)
  651. {
  652. if(lastc = peekc) {
  653. peekc = 0;
  654. return lastc;
  655. }
  656. if(globp) {
  657. if((lastc=*globp++) != 0)
  658. return lastc;
  659. globp = 0;
  660. return EOF;
  661. }
  662. lastc = Bgetrune(&bcons);
  663. return lastc;
  664. }
  665. int
  666. gety(void)
  667. {
  668. int c;
  669. Rune *gf, *p;
  670. p = linebuf;
  671. gf = globp;
  672. for(;;) {
  673. c = getchr();
  674. if(c == '\n') {
  675. *p = 0;
  676. return 0;
  677. }
  678. if(c == EOF) {
  679. if(gf)
  680. peekc = c;
  681. return c;
  682. }
  683. if(c == 0)
  684. continue;
  685. *p++ = c;
  686. if(p >= &linebuf[LBSIZE-sizeof(Rune)])
  687. error(Q);
  688. }
  689. }
  690. int
  691. gettty(void)
  692. {
  693. int rc;
  694. rc = gety();
  695. if(rc)
  696. return rc;
  697. if(linebuf[0] == '.' && linebuf[1] == 0)
  698. return EOF;
  699. return 0;
  700. }
  701. int
  702. getfile(void)
  703. {
  704. int c;
  705. Rune *lp;
  706. lp = linebuf;
  707. do {
  708. c = Bgetrune(&iobuf);
  709. if(c < 0) {
  710. if(lp > linebuf) {
  711. putst("'\\n' appended");
  712. c = '\n';
  713. } else
  714. return EOF;
  715. }
  716. if(lp >= &linebuf[LBSIZE]) {
  717. lastc = '\n';
  718. error(Q);
  719. }
  720. *lp++ = c;
  721. count++;
  722. } while(c != '\n');
  723. lp[-1] = 0;
  724. return 0;
  725. }
  726. void
  727. putfile(void)
  728. {
  729. int *a1;
  730. Rune *lp;
  731. int32_t c;
  732. a1 = addr1;
  733. do {
  734. lp = getline(*a1++);
  735. for(;;) {
  736. count++;
  737. c = *lp++;
  738. if(c == 0) {
  739. if(Bputrune(&iobuf, '\n') < 0)
  740. error(Q);
  741. break;
  742. }
  743. if(Bputrune(&iobuf, c) < 0)
  744. error(Q);
  745. }
  746. } while(a1 <= addr2);
  747. if(Bflush(&iobuf) < 0)
  748. error(Q);
  749. }
  750. int
  751. append(int (*f)(void), int *a)
  752. {
  753. int *a1, *a2, *rdot, nline, tl;
  754. nline = 0;
  755. dot = a;
  756. while((*f)() == 0) {
  757. if((dol-zero) >= nlall) {
  758. nlall += 512;
  759. a1 = realloc(zero, (nlall+5)*sizeof(int*));
  760. if(a1 == 0) {
  761. error("MEM?");
  762. rescue();
  763. }
  764. tl = a1 - zero; /* relocate pointers */
  765. zero += tl;
  766. addr1 += tl;
  767. addr2 += tl;
  768. dol += tl;
  769. dot += tl;
  770. }
  771. tl = putline();
  772. nline++;
  773. a1 = ++dol;
  774. a2 = a1+1;
  775. rdot = ++dot;
  776. while(a1 > rdot)
  777. *--a2 = *--a1;
  778. *rdot = tl;
  779. }
  780. return nline;
  781. }
  782. void
  783. add(int i)
  784. {
  785. if(i && (given || dol > zero)) {
  786. addr1--;
  787. addr2--;
  788. }
  789. squeeze(0);
  790. newline();
  791. append(gettty, addr2);
  792. }
  793. void
  794. browse(void)
  795. {
  796. int forward, n;
  797. static int bformat, bnum; /* 0 */
  798. forward = 1;
  799. peekc = getchr();
  800. if(peekc != '\n'){
  801. if(peekc == '-' || peekc == '+') {
  802. if(peekc == '-')
  803. forward = 0;
  804. getchr();
  805. }
  806. n = getnum();
  807. if(n > 0)
  808. bpagesize = n;
  809. }
  810. newline();
  811. if(pflag) {
  812. bformat = listf;
  813. bnum = listn;
  814. } else {
  815. listf = bformat;
  816. listn = bnum;
  817. }
  818. if(forward) {
  819. addr1 = addr2;
  820. addr2 += bpagesize;
  821. if(addr2 > dol)
  822. addr2 = dol;
  823. } else {
  824. addr1 = addr2-bpagesize;
  825. if(addr1 <= zero)
  826. addr1 = zero+1;
  827. }
  828. printcom();
  829. }
  830. void
  831. callunix(void)
  832. {
  833. int c, pid;
  834. Rune rune;
  835. char buf[512];
  836. char *p;
  837. setnoaddr();
  838. p = buf;
  839. while((c=getchr()) != EOF && c != '\n')
  840. if(p < &buf[sizeof(buf) - 6]) {
  841. rune = c;
  842. p += runetochar(p, &rune);
  843. }
  844. *p = 0;
  845. pid = fork();
  846. if(pid == 0) {
  847. execl("/bin/rc", "rc", "-c", buf, nil);
  848. exits("execl failed");
  849. }
  850. waiting = 1;
  851. while(waitpid() != pid)
  852. ;
  853. waiting = 0;
  854. if(vflag)
  855. putst("!");
  856. }
  857. void
  858. quit(void)
  859. {
  860. if(vflag && fchange && dol!=zero) {
  861. fchange = 0;
  862. error(Q);
  863. }
  864. remove(tfname);
  865. exits(0);
  866. }
  867. void
  868. onquit(int sig)
  869. {
  870. USED(sig);
  871. quit();
  872. }
  873. void
  874. rdelete(int *ad1, int *ad2)
  875. {
  876. int *a1, *a2, *a3;
  877. a1 = ad1;
  878. a2 = ad2+1;
  879. a3 = dol;
  880. dol -= a2 - a1;
  881. do {
  882. *a1++ = *a2++;
  883. } while(a2 <= a3);
  884. a1 = ad1;
  885. if(a1 > dol)
  886. a1 = dol;
  887. dot = a1;
  888. fchange = 1;
  889. }
  890. void
  891. gdelete(void)
  892. {
  893. int *a1, *a2, *a3;
  894. a3 = dol;
  895. for(a1=zero; (*a1&01)==0; a1++)
  896. if(a1>=a3)
  897. return;
  898. for(a2=a1+1; a2<=a3;) {
  899. if(*a2 & 01) {
  900. a2++;
  901. dot = a1;
  902. } else
  903. *a1++ = *a2++;
  904. }
  905. dol = a1-1;
  906. if(dot > dol)
  907. dot = dol;
  908. fchange = 1;
  909. }
  910. Rune*
  911. getline(int tl)
  912. {
  913. Rune *lp, *bp;
  914. int nl;
  915. lp = linebuf;
  916. bp = getblock(tl, OREAD);
  917. nl = nleft;
  918. tl &= ~((BLKSIZE/sizeof(Rune)) - 1);
  919. while(*lp++ = *bp++) {
  920. nl -= sizeof(Rune);
  921. if(nl == 0) {
  922. tl += BLKSIZE/sizeof(Rune);
  923. bp = getblock(tl, OREAD);
  924. nl = nleft;
  925. }
  926. }
  927. return linebuf;
  928. }
  929. int
  930. putline(void)
  931. {
  932. Rune *lp, *bp;
  933. int nl, tl;
  934. fchange = 1;
  935. lp = linebuf;
  936. tl = tline;
  937. bp = getblock(tl, OWRITE);
  938. nl = nleft;
  939. tl &= ~((BLKSIZE/sizeof(Rune))-1);
  940. while(*bp = *lp++) {
  941. if(*bp++ == '\n') {
  942. bp[-1] = 0;
  943. linebp = lp;
  944. break;
  945. }
  946. nl -= sizeof(Rune);
  947. if(nl == 0) {
  948. tl += BLKSIZE/sizeof(Rune);
  949. bp = getblock(tl, OWRITE);
  950. nl = nleft;
  951. }
  952. }
  953. nl = tline;
  954. tline += ((lp-linebuf) + 03) & 077776;
  955. return nl;
  956. }
  957. void
  958. blkio(int b, uint8_t *buf, int32_t (*iofcn)(int, void *, int32_t))
  959. {
  960. seek(tfile, b*BLKSIZE, 0);
  961. if((*iofcn)(tfile, buf, BLKSIZE) != BLKSIZE) {
  962. error(T);
  963. }
  964. }
  965. Rune*
  966. getblock(int atl, int iof)
  967. {
  968. int bno, off;
  969. static uint8_t ibuff[BLKSIZE];
  970. static uint8_t obuff[BLKSIZE];
  971. bno = atl / (BLKSIZE/sizeof(Rune));
  972. /* &~3 so the ptr is aligned to 4 (?) */
  973. off = (atl*sizeof(Rune)) & (BLKSIZE-1) & ~3;
  974. if(bno >= NBLK) {
  975. lastc = '\n';
  976. error(T);
  977. }
  978. nleft = BLKSIZE - off;
  979. if(bno == iblock) {
  980. ichanged |= iof;
  981. return (Rune*)(ibuff+off);
  982. }
  983. if(bno == oblock)
  984. return (Rune*)(obuff+off);
  985. if(iof == OREAD) {
  986. if(ichanged)
  987. blkio(iblock, ibuff, write);
  988. ichanged = 0;
  989. iblock = bno;
  990. blkio(bno, ibuff, read);
  991. return (Rune*)(ibuff+off);
  992. }
  993. if(oblock >= 0)
  994. blkio(oblock, obuff, write);
  995. oblock = bno;
  996. return (Rune*)(obuff+off);
  997. }
  998. void
  999. init(void)
  1000. {
  1001. int *markp;
  1002. close(tfile);
  1003. tline = 2;
  1004. for(markp = names; markp < &names[26]; )
  1005. *markp++ = 0;
  1006. subnewa = 0;
  1007. anymarks = 0;
  1008. iblock = -1;
  1009. oblock = -1;
  1010. ichanged = 0;
  1011. if((tfile = create(tfname, ORDWR, 0600)) < 0){
  1012. error1(T);
  1013. exits(0);
  1014. }
  1015. dot = dol = zero;
  1016. }
  1017. void
  1018. global(int k)
  1019. {
  1020. Rune *gp, globuf[GBSIZE];
  1021. int c, *a1;
  1022. if(globp)
  1023. error(Q);
  1024. setwide();
  1025. squeeze(dol > zero);
  1026. c = getchr();
  1027. if(c == '\n')
  1028. error(Q);
  1029. compile(c);
  1030. gp = globuf;
  1031. while((c=getchr()) != '\n') {
  1032. if(c == EOF)
  1033. error(Q);
  1034. if(c == '\\') {
  1035. c = getchr();
  1036. if(c != '\n')
  1037. *gp++ = '\\';
  1038. }
  1039. *gp++ = c;
  1040. if(gp >= &globuf[GBSIZE-2])
  1041. error(Q);
  1042. }
  1043. if(gp == globuf)
  1044. *gp++ = 'p';
  1045. *gp++ = '\n';
  1046. *gp = 0;
  1047. for(a1=zero; a1<=dol; a1++) {
  1048. *a1 &= ~01;
  1049. if(a1 >= addr1 && a1 <= addr2 && match(a1) == k)
  1050. *a1 |= 01;
  1051. }
  1052. /*
  1053. * Special case: g/.../d (avoid n^2 algorithm)
  1054. */
  1055. if(globuf[0] == 'd' && globuf[1] == '\n' && globuf[2] == 0) {
  1056. gdelete();
  1057. return;
  1058. }
  1059. for(a1=zero; a1<=dol; a1++) {
  1060. if(*a1 & 01) {
  1061. *a1 &= ~01;
  1062. dot = a1;
  1063. globp = globuf;
  1064. commands();
  1065. a1 = zero;
  1066. }
  1067. }
  1068. }
  1069. void
  1070. join(void)
  1071. {
  1072. Rune *gp, *lp;
  1073. int *a1;
  1074. nonzero();
  1075. gp = genbuf;
  1076. for(a1=addr1; a1<=addr2; a1++) {
  1077. lp = getline(*a1);
  1078. while(*gp = *lp++)
  1079. if(gp++ >= &genbuf[LBSIZE-sizeof(Rune)])
  1080. error(Q);
  1081. }
  1082. lp = linebuf;
  1083. gp = genbuf;
  1084. while(*lp++ = *gp++)
  1085. ;
  1086. *addr1 = putline();
  1087. if(addr1 < addr2)
  1088. rdelete(addr1+1, addr2);
  1089. dot = addr1;
  1090. }
  1091. void
  1092. substitute(int inglob)
  1093. {
  1094. int *mp, *a1, nl, gsubf, n;
  1095. n = getnum(); /* OK even if n==0 */
  1096. gsubf = compsub();
  1097. for(a1 = addr1; a1 <= addr2; a1++) {
  1098. if(match(a1)){
  1099. int *ozero;
  1100. int m = n;
  1101. do {
  1102. int span = loc2-loc1;
  1103. if(--m <= 0) {
  1104. dosub();
  1105. if(!gsubf)
  1106. break;
  1107. if(span == 0) { /* null RE match */
  1108. if(*loc2 == 0)
  1109. break;
  1110. loc2++;
  1111. }
  1112. }
  1113. } while(match(0));
  1114. if(m <= 0) {
  1115. inglob |= 01;
  1116. subnewa = putline();
  1117. *a1 &= ~01;
  1118. if(anymarks) {
  1119. for(mp=names; mp<&names[26]; mp++)
  1120. if(*mp == *a1)
  1121. *mp = subnewa;
  1122. }
  1123. subolda = *a1;
  1124. *a1 = subnewa;
  1125. ozero = zero;
  1126. nl = append(getsub, a1);
  1127. addr2 += nl;
  1128. nl += zero-ozero;
  1129. a1 += nl;
  1130. }
  1131. }
  1132. }
  1133. if(inglob == 0)
  1134. error(Q);
  1135. }
  1136. int
  1137. compsub(void)
  1138. {
  1139. int seof, c;
  1140. Rune *p;
  1141. seof = getchr();
  1142. if(seof == '\n' || seof == ' ')
  1143. error(Q);
  1144. compile(seof);
  1145. p = rhsbuf;
  1146. for(;;) {
  1147. c = getchr();
  1148. if(c == '\\') {
  1149. c = getchr();
  1150. *p++ = ESCFLG;
  1151. if(p >= &rhsbuf[LBSIZE/sizeof(Rune)])
  1152. error(Q);
  1153. } else
  1154. if(c == '\n' && (!globp || !globp[0])) {
  1155. peekc = c;
  1156. pflag++;
  1157. break;
  1158. } else
  1159. if(c == seof)
  1160. break;
  1161. *p++ = c;
  1162. if(p >= &rhsbuf[LBSIZE/sizeof(Rune)])
  1163. error(Q);
  1164. }
  1165. *p = 0;
  1166. peekc = getchr();
  1167. if(peekc == 'g') {
  1168. peekc = 0;
  1169. newline();
  1170. return 1;
  1171. }
  1172. newline();
  1173. return 0;
  1174. }
  1175. int
  1176. getsub(void)
  1177. {
  1178. Rune *p1, *p2;
  1179. p1 = linebuf;
  1180. if((p2 = linebp) == 0)
  1181. return EOF;
  1182. while(*p1++ = *p2++)
  1183. ;
  1184. linebp = 0;
  1185. return 0;
  1186. }
  1187. void
  1188. dosub(void)
  1189. {
  1190. Rune *lp, *sp, *rp;
  1191. int c, n;
  1192. lp = linebuf;
  1193. sp = genbuf;
  1194. rp = rhsbuf;
  1195. while(lp < loc1)
  1196. *sp++ = *lp++;
  1197. while(c = *rp++) {
  1198. if(c == '&'){
  1199. sp = place(sp, loc1, loc2);
  1200. continue;
  1201. }
  1202. if(c == ESCFLG && (c = *rp++) >= '1' && c < MAXSUB+'0') {
  1203. n = c-'0';
  1204. if(subexp[n].rsp && subexp[n].rep) {
  1205. sp = place(sp, subexp[n].rsp, subexp[n].rep);
  1206. continue;
  1207. }
  1208. error(Q);
  1209. }
  1210. *sp++ = c;
  1211. if(sp >= &genbuf[LBSIZE])
  1212. error(Q);
  1213. }
  1214. lp = loc2;
  1215. loc2 = sp - genbuf + linebuf;
  1216. while(*sp++ = *lp++)
  1217. if(sp >= &genbuf[LBSIZE])
  1218. error(Q);
  1219. lp = linebuf;
  1220. sp = genbuf;
  1221. while(*lp++ = *sp++)
  1222. ;
  1223. }
  1224. Rune*
  1225. place(Rune *sp, Rune *l1, Rune *l2)
  1226. {
  1227. while(l1 < l2) {
  1228. *sp++ = *l1++;
  1229. if(sp >= &genbuf[LBSIZE])
  1230. error(Q);
  1231. }
  1232. return sp;
  1233. }
  1234. void
  1235. move(int cflag)
  1236. {
  1237. int *adt, *ad1, *ad2;
  1238. nonzero();
  1239. if((adt = address())==0) /* address() guarantees addr is in range */
  1240. error(Q);
  1241. newline();
  1242. if(cflag) {
  1243. int *ozero, delta;
  1244. ad1 = dol;
  1245. ozero = zero;
  1246. append(getcopy, ad1++);
  1247. ad2 = dol;
  1248. delta = zero - ozero;
  1249. ad1 += delta;
  1250. adt += delta;
  1251. } else {
  1252. ad2 = addr2;
  1253. for(ad1 = addr1; ad1 <= ad2;)
  1254. *ad1++ &= ~01;
  1255. ad1 = addr1;
  1256. }
  1257. ad2++;
  1258. if(adt<ad1) {
  1259. dot = adt + (ad2-ad1);
  1260. if((++adt)==ad1)
  1261. return;
  1262. reverse(adt, ad1);
  1263. reverse(ad1, ad2);
  1264. reverse(adt, ad2);
  1265. } else
  1266. if(adt >= ad2) {
  1267. dot = adt++;
  1268. reverse(ad1, ad2);
  1269. reverse(ad2, adt);
  1270. reverse(ad1, adt);
  1271. } else
  1272. error(Q);
  1273. fchange = 1;
  1274. }
  1275. void
  1276. reverse(int *a1, int *a2)
  1277. {
  1278. int t;
  1279. for(;;) {
  1280. t = *--a2;
  1281. if(a2 <= a1)
  1282. return;
  1283. *a2 = *a1;
  1284. *a1++ = t;
  1285. }
  1286. }
  1287. int
  1288. getcopy(void)
  1289. {
  1290. if(addr1 > addr2)
  1291. return EOF;
  1292. getline(*addr1++);
  1293. return 0;
  1294. }
  1295. void
  1296. compile(int eof)
  1297. {
  1298. Rune c;
  1299. char *ep;
  1300. char expbuf[ESIZE];
  1301. if((c = getchr()) == '\n') {
  1302. peekc = c;
  1303. c = eof;
  1304. }
  1305. if(c == eof) {
  1306. if(!pattern)
  1307. error(Q);
  1308. return;
  1309. }
  1310. if(pattern) {
  1311. free(pattern);
  1312. pattern = 0;
  1313. }
  1314. ep = expbuf;
  1315. do {
  1316. if(c == '\\') {
  1317. if(ep >= expbuf+sizeof(expbuf)) {
  1318. error(Q);
  1319. return;
  1320. }
  1321. ep += runetochar(ep, &c);
  1322. if((c = getchr()) == '\n') {
  1323. error(Q);
  1324. return;
  1325. }
  1326. }
  1327. if(ep >= expbuf+sizeof(expbuf)) {
  1328. error(Q);
  1329. return;
  1330. }
  1331. ep += runetochar(ep, &c);
  1332. } while((c = getchr()) != eof && c != '\n');
  1333. if(c == '\n')
  1334. peekc = c;
  1335. *ep = 0;
  1336. pattern = regcomp(expbuf);
  1337. }
  1338. int
  1339. match(int *addr)
  1340. {
  1341. if(!pattern)
  1342. return 0;
  1343. if(addr){
  1344. if(addr == zero)
  1345. return 0;
  1346. subexp[0].rsp = getline(*addr);
  1347. } else
  1348. subexp[0].rsp = loc2;
  1349. subexp[0].rep = 0;
  1350. if(rregexec(pattern, linebuf, subexp, MAXSUB)) {
  1351. loc1 = subexp[0].rsp;
  1352. loc2 = subexp[0].rep;
  1353. return 1;
  1354. }
  1355. loc1 = loc2 = 0;
  1356. return 0;
  1357. }
  1358. void
  1359. putd(void)
  1360. {
  1361. int r;
  1362. r = count%10;
  1363. count /= 10;
  1364. if(count)
  1365. putd();
  1366. putchr(r + L'0');
  1367. }
  1368. void
  1369. putst(char *sp)
  1370. {
  1371. Rune r;
  1372. col = 0;
  1373. for(;;) {
  1374. sp += chartorune(&r, sp);
  1375. if(r == 0)
  1376. break;
  1377. putchr(r);
  1378. }
  1379. putchr(L'\n');
  1380. }
  1381. void
  1382. putshst(Rune *sp)
  1383. {
  1384. col = 0;
  1385. while(*sp)
  1386. putchr(*sp++);
  1387. putchr(L'\n');
  1388. }
  1389. void
  1390. putchr(int ac)
  1391. {
  1392. char *lp;
  1393. int c;
  1394. Rune rune;
  1395. lp = linp;
  1396. c = ac;
  1397. if(listf) {
  1398. if(c == '\n') {
  1399. if(linp != line && linp[-1] == ' ') {
  1400. *lp++ = '\\';
  1401. *lp++ = 'n';
  1402. }
  1403. } else {
  1404. if(col > (72-6-2)) {
  1405. col = 8;
  1406. *lp++ = '\\';
  1407. *lp++ = '\n';
  1408. *lp++ = '\t';
  1409. }
  1410. col++;
  1411. if(c=='\b' || c=='\t' || c=='\\') {
  1412. *lp++ = '\\';
  1413. if(c == '\b')
  1414. c = 'b';
  1415. else
  1416. if(c == '\t')
  1417. c = 't';
  1418. col++;
  1419. } else
  1420. if(c<' ' || c>='\177') {
  1421. *lp++ = '\\';
  1422. *lp++ = 'x';
  1423. *lp++ = hex[c>>12];
  1424. *lp++ = hex[c>>8&0xF];
  1425. *lp++ = hex[c>>4&0xF];
  1426. c = hex[c&0xF];
  1427. col += 5;
  1428. }
  1429. }
  1430. }
  1431. rune = c;
  1432. lp += runetochar(lp, &rune);
  1433. if(c == '\n' || lp >= &line[sizeof(line)-5]) {
  1434. linp = line;
  1435. write(oflag? 2: 1, line, lp-line);
  1436. return;
  1437. }
  1438. linp = lp;
  1439. }
  1440. char*
  1441. mktemp(char *as)
  1442. {
  1443. char *s;
  1444. unsigned pid;
  1445. int i;
  1446. pid = getpid();
  1447. s = as;
  1448. while(*s++)
  1449. ;
  1450. s--;
  1451. while(*--s == 'X') {
  1452. *s = pid % 10 + '0';
  1453. pid /= 10;
  1454. }
  1455. s++;
  1456. i = 'a';
  1457. while(access(as, 0) != -1) {
  1458. if(i == 'z')
  1459. return "/";
  1460. *s = i++;
  1461. }
  1462. return as;
  1463. }
  1464. void
  1465. regerror(char *s)
  1466. {
  1467. USED(s);
  1468. error(Q);
  1469. }