ed.c 21 KB

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