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