ed.c 22 KB

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