ed.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  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++) != 0)
  152. ;
  153. globp = &la;
  154. } else
  155. if(*argv) {
  156. p1 = *argv;
  157. p2 = savedfile;
  158. while((*p2++ = *p1++) != 0)
  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++) != 0)
  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++) != 0)
  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) != 0) {
  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++) != 0){
  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++) != 0){
  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. typedef int32_t Rdwrfn(int, void *, int32_t);
  962. void
  963. blkio(int b, uint8_t *buf, Rdwrfn *iofcn)
  964. {
  965. seek(tfile, b*BLKSIZE, 0);
  966. if((*iofcn)(tfile, buf, BLKSIZE) != BLKSIZE) {
  967. error(T);
  968. }
  969. }
  970. Rune*
  971. getblock(int atl, int iof)
  972. {
  973. int bno, off;
  974. static uint8_t ibuff[BLKSIZE];
  975. static uint8_t obuff[BLKSIZE];
  976. bno = atl / (BLKSIZE/sizeof(Rune));
  977. /* &~3 so the ptr is aligned to 4 (?) */
  978. off = (atl*sizeof(Rune)) & (BLKSIZE-1) & ~3;
  979. if(bno >= NBLK) {
  980. lastc = '\n';
  981. error(T);
  982. }
  983. nleft = BLKSIZE - off;
  984. if(bno == iblock) {
  985. ichanged |= iof;
  986. return (Rune*)(ibuff+off);
  987. }
  988. if(bno == oblock)
  989. return (Rune*)(obuff+off);
  990. if(iof == OREAD) {
  991. if(ichanged)
  992. blkio(iblock, ibuff, (Rdwrfn *)write);
  993. ichanged = 0;
  994. iblock = bno;
  995. blkio(bno, ibuff, read);
  996. return (Rune*)(ibuff+off);
  997. }
  998. if(oblock >= 0)
  999. blkio(oblock, obuff, (Rdwrfn *)write);
  1000. oblock = bno;
  1001. return (Rune*)(obuff+off);
  1002. }
  1003. void
  1004. init(void)
  1005. {
  1006. int *markp;
  1007. close(tfile);
  1008. tline = 2;
  1009. for(markp = names; markp < &names[26]; )
  1010. *markp++ = 0;
  1011. subnewa = 0;
  1012. anymarks = 0;
  1013. iblock = -1;
  1014. oblock = -1;
  1015. ichanged = 0;
  1016. if((tfile = create(tfname, ORDWR, 0600)) < 0){
  1017. error1(T);
  1018. exits(0);
  1019. }
  1020. dot = dol = zero;
  1021. }
  1022. void
  1023. global(int k)
  1024. {
  1025. Rune *gp, globuf[GBSIZE];
  1026. int c, *a1;
  1027. if(globp)
  1028. error(Q);
  1029. setwide();
  1030. squeeze(dol > zero);
  1031. c = getchr();
  1032. if(c == '\n')
  1033. error(Q);
  1034. compile(c);
  1035. gp = globuf;
  1036. while((c=getchr()) != '\n') {
  1037. if(c == EOF)
  1038. error(Q);
  1039. if(c == '\\') {
  1040. c = getchr();
  1041. if(c != '\n')
  1042. *gp++ = '\\';
  1043. }
  1044. *gp++ = c;
  1045. if(gp >= &globuf[GBSIZE-2])
  1046. error(Q);
  1047. }
  1048. if(gp == globuf)
  1049. *gp++ = 'p';
  1050. *gp++ = '\n';
  1051. *gp = 0;
  1052. for(a1=zero; a1<=dol; a1++) {
  1053. *a1 &= ~01;
  1054. if(a1 >= addr1 && a1 <= addr2 && match(a1) == k)
  1055. *a1 |= 01;
  1056. }
  1057. /*
  1058. * Special case: g/.../d (avoid n^2 algorithm)
  1059. */
  1060. if(globuf[0] == 'd' && globuf[1] == '\n' && globuf[2] == 0) {
  1061. gdelete();
  1062. return;
  1063. }
  1064. for(a1=zero; a1<=dol; a1++) {
  1065. if(*a1 & 01) {
  1066. *a1 &= ~01;
  1067. dot = a1;
  1068. globp = globuf;
  1069. commands();
  1070. a1 = zero;
  1071. }
  1072. }
  1073. }
  1074. void
  1075. join(void)
  1076. {
  1077. Rune *gp, *lp;
  1078. int *a1;
  1079. nonzero();
  1080. gp = genbuf;
  1081. for(a1=addr1; a1<=addr2; a1++) {
  1082. lp = getline(*a1);
  1083. while((*gp = *lp++) != 0)
  1084. if(gp++ >= &genbuf[LBSIZE-sizeof(Rune)])
  1085. error(Q);
  1086. }
  1087. lp = linebuf;
  1088. gp = genbuf;
  1089. while((*lp++ = *gp++) != 0)
  1090. ;
  1091. *addr1 = putline();
  1092. if(addr1 < addr2)
  1093. rdelete(addr1+1, addr2);
  1094. dot = addr1;
  1095. }
  1096. void
  1097. substitute(int inglob)
  1098. {
  1099. int *mp, *a1, nl, gsubf, n;
  1100. n = getnum(); /* OK even if n==0 */
  1101. gsubf = compsub();
  1102. for(a1 = addr1; a1 <= addr2; a1++) {
  1103. if(match(a1)){
  1104. int *ozero;
  1105. int m = n;
  1106. do {
  1107. int span = loc2-loc1;
  1108. if(--m <= 0) {
  1109. dosub();
  1110. if(!gsubf)
  1111. break;
  1112. if(span == 0) { /* null RE match */
  1113. if(*loc2 == 0)
  1114. break;
  1115. loc2++;
  1116. }
  1117. }
  1118. } while(match(0));
  1119. if(m <= 0) {
  1120. inglob |= 01;
  1121. subnewa = putline();
  1122. *a1 &= ~01;
  1123. if(anymarks) {
  1124. for(mp=names; mp<&names[26]; mp++)
  1125. if(*mp == *a1)
  1126. *mp = subnewa;
  1127. }
  1128. subolda = *a1;
  1129. *a1 = subnewa;
  1130. ozero = zero;
  1131. nl = append(getsub, a1);
  1132. addr2 += nl;
  1133. nl += zero-ozero;
  1134. a1 += nl;
  1135. }
  1136. }
  1137. }
  1138. if(inglob == 0)
  1139. error(Q);
  1140. }
  1141. int
  1142. compsub(void)
  1143. {
  1144. int seof, c;
  1145. Rune *p;
  1146. seof = getchr();
  1147. if(seof == '\n' || seof == ' ')
  1148. error(Q);
  1149. compile(seof);
  1150. p = rhsbuf;
  1151. for(;;) {
  1152. c = getchr();
  1153. if(c == '\\') {
  1154. c = getchr();
  1155. *p++ = ESCFLG;
  1156. if(p >= &rhsbuf[LBSIZE/sizeof(Rune)])
  1157. error(Q);
  1158. } else
  1159. if(c == '\n' && (!globp || !globp[0])) {
  1160. peekc = c;
  1161. pflag++;
  1162. break;
  1163. } else
  1164. if(c == seof)
  1165. break;
  1166. *p++ = c;
  1167. if(p >= &rhsbuf[LBSIZE/sizeof(Rune)])
  1168. error(Q);
  1169. }
  1170. *p = 0;
  1171. peekc = getchr();
  1172. if(peekc == 'g') {
  1173. peekc = 0;
  1174. newline();
  1175. return 1;
  1176. }
  1177. newline();
  1178. return 0;
  1179. }
  1180. int
  1181. getsub(void)
  1182. {
  1183. Rune *p1, *p2;
  1184. p1 = linebuf;
  1185. if((p2 = linebp) == 0)
  1186. return EOF;
  1187. while((*p1++ = *p2++) != 0)
  1188. ;
  1189. linebp = 0;
  1190. return 0;
  1191. }
  1192. void
  1193. dosub(void)
  1194. {
  1195. Rune *lp, *sp, *rp;
  1196. int c, n;
  1197. lp = linebuf;
  1198. sp = genbuf;
  1199. rp = rhsbuf;
  1200. while(lp < loc1)
  1201. *sp++ = *lp++;
  1202. while((c = *rp++) != 0){
  1203. if(c == '&'){
  1204. sp = place(sp, loc1, loc2);
  1205. continue;
  1206. }
  1207. if(c == ESCFLG && (c = *rp++) >= '1' && c < MAXSUB+'0') {
  1208. n = c-'0';
  1209. if(subexp[n].rsp && subexp[n].rep) {
  1210. sp = place(sp, subexp[n].rsp, subexp[n].rep);
  1211. continue;
  1212. }
  1213. error(Q);
  1214. }
  1215. *sp++ = c;
  1216. if(sp >= &genbuf[LBSIZE])
  1217. error(Q);
  1218. }
  1219. lp = loc2;
  1220. loc2 = sp - genbuf + linebuf;
  1221. while((*sp++ = *lp++) != 0)
  1222. if(sp >= &genbuf[LBSIZE])
  1223. error(Q);
  1224. lp = linebuf;
  1225. sp = genbuf;
  1226. while((*lp++ = *sp++) != 0)
  1227. ;
  1228. }
  1229. Rune*
  1230. place(Rune *sp, Rune *l1, Rune *l2)
  1231. {
  1232. while(l1 < l2) {
  1233. *sp++ = *l1++;
  1234. if(sp >= &genbuf[LBSIZE])
  1235. error(Q);
  1236. }
  1237. return sp;
  1238. }
  1239. void
  1240. move(int cflag)
  1241. {
  1242. int *adt, *ad1, *ad2;
  1243. nonzero();
  1244. if((adt = address())==0) /* address() guarantees addr is in range */
  1245. error(Q);
  1246. newline();
  1247. if(cflag) {
  1248. int *ozero, delta;
  1249. ad1 = dol;
  1250. ozero = zero;
  1251. append(getcopy, ad1++);
  1252. ad2 = dol;
  1253. delta = zero - ozero;
  1254. ad1 += delta;
  1255. adt += delta;
  1256. } else {
  1257. ad2 = addr2;
  1258. for(ad1 = addr1; ad1 <= ad2;)
  1259. *ad1++ &= ~01;
  1260. ad1 = addr1;
  1261. }
  1262. ad2++;
  1263. if(adt<ad1) {
  1264. dot = adt + (ad2-ad1);
  1265. if((++adt)==ad1)
  1266. return;
  1267. reverse(adt, ad1);
  1268. reverse(ad1, ad2);
  1269. reverse(adt, ad2);
  1270. } else
  1271. if(adt >= ad2) {
  1272. dot = adt++;
  1273. reverse(ad1, ad2);
  1274. reverse(ad2, adt);
  1275. reverse(ad1, adt);
  1276. } else
  1277. error(Q);
  1278. fchange = 1;
  1279. }
  1280. void
  1281. reverse(int *a1, int *a2)
  1282. {
  1283. int t;
  1284. for(;;) {
  1285. t = *--a2;
  1286. if(a2 <= a1)
  1287. return;
  1288. *a2 = *a1;
  1289. *a1++ = t;
  1290. }
  1291. }
  1292. int
  1293. getcopy(void)
  1294. {
  1295. if(addr1 > addr2)
  1296. return EOF;
  1297. getline(*addr1++);
  1298. return 0;
  1299. }
  1300. void
  1301. compile(int eof)
  1302. {
  1303. Rune c;
  1304. char *ep;
  1305. char expbuf[ESIZE];
  1306. if((c = getchr()) == '\n') {
  1307. peekc = c;
  1308. c = eof;
  1309. }
  1310. if(c == eof) {
  1311. if(!pattern)
  1312. error(Q);
  1313. return;
  1314. }
  1315. if(pattern) {
  1316. free(pattern);
  1317. pattern = 0;
  1318. }
  1319. ep = expbuf;
  1320. do {
  1321. if(c == '\\') {
  1322. if(ep >= expbuf+sizeof(expbuf)) {
  1323. error(Q);
  1324. return;
  1325. }
  1326. ep += runetochar(ep, &c);
  1327. if((c = getchr()) == '\n') {
  1328. error(Q);
  1329. return;
  1330. }
  1331. }
  1332. if(ep >= expbuf+sizeof(expbuf)) {
  1333. error(Q);
  1334. return;
  1335. }
  1336. ep += runetochar(ep, &c);
  1337. } while((c = getchr()) != eof && c != '\n');
  1338. if(c == '\n')
  1339. peekc = c;
  1340. *ep = 0;
  1341. pattern = regcomp(expbuf);
  1342. }
  1343. int
  1344. match(int *addr)
  1345. {
  1346. if(!pattern)
  1347. return 0;
  1348. if(addr){
  1349. if(addr == zero)
  1350. return 0;
  1351. subexp[0].rsp = getline(*addr);
  1352. } else
  1353. subexp[0].rsp = loc2;
  1354. subexp[0].rep = 0;
  1355. if(rregexec(pattern, linebuf, subexp, MAXSUB)) {
  1356. loc1 = subexp[0].rsp;
  1357. loc2 = subexp[0].rep;
  1358. return 1;
  1359. }
  1360. loc1 = loc2 = 0;
  1361. return 0;
  1362. }
  1363. void
  1364. putd(void)
  1365. {
  1366. int r;
  1367. r = count%10;
  1368. count /= 10;
  1369. if(count)
  1370. putd();
  1371. putchr(r + L'0');
  1372. }
  1373. void
  1374. putst(char *sp)
  1375. {
  1376. Rune r;
  1377. col = 0;
  1378. for(;;) {
  1379. sp += chartorune(&r, sp);
  1380. if(r == 0)
  1381. break;
  1382. putchr(r);
  1383. }
  1384. putchr(L'\n');
  1385. }
  1386. void
  1387. putshst(Rune *sp)
  1388. {
  1389. col = 0;
  1390. while(*sp)
  1391. putchr(*sp++);
  1392. putchr(L'\n');
  1393. }
  1394. void
  1395. putchr(int ac)
  1396. {
  1397. char *lp;
  1398. int c;
  1399. Rune rune;
  1400. lp = linp;
  1401. c = ac;
  1402. if(listf) {
  1403. if(c == '\n') {
  1404. if(linp != line && linp[-1] == ' ') {
  1405. *lp++ = '\\';
  1406. *lp++ = 'n';
  1407. }
  1408. } else {
  1409. if(col > (72-6-2)) {
  1410. col = 8;
  1411. *lp++ = '\\';
  1412. *lp++ = '\n';
  1413. *lp++ = '\t';
  1414. }
  1415. col++;
  1416. if(c=='\b' || c=='\t' || c=='\\') {
  1417. *lp++ = '\\';
  1418. if(c == '\b')
  1419. c = 'b';
  1420. else
  1421. if(c == '\t')
  1422. c = 't';
  1423. col++;
  1424. } else
  1425. if(c<' ' || c>='\177') {
  1426. *lp++ = '\\';
  1427. *lp++ = 'x';
  1428. *lp++ = hex[c>>12];
  1429. *lp++ = hex[c>>8&0xF];
  1430. *lp++ = hex[c>>4&0xF];
  1431. c = hex[c&0xF];
  1432. col += 5;
  1433. }
  1434. }
  1435. }
  1436. rune = c;
  1437. lp += runetochar(lp, &rune);
  1438. if(c == '\n' || lp >= &line[sizeof(line)-5]) {
  1439. linp = line;
  1440. write(oflag? 2: 1, line, lp-line);
  1441. return;
  1442. }
  1443. linp = lp;
  1444. }
  1445. char*
  1446. mktemp(char *as)
  1447. {
  1448. char *s;
  1449. unsigned pid;
  1450. int i;
  1451. pid = getpid();
  1452. s = as = strdup(as);
  1453. while(*s++)
  1454. ;
  1455. s--;
  1456. while(*--s == 'X') {
  1457. *s = pid % 10 + '0';
  1458. pid /= 10;
  1459. }
  1460. s++;
  1461. i = 'a';
  1462. while(access(as, 0) != -1) {
  1463. if(i == 'z')
  1464. return "/";
  1465. *s = i++;
  1466. }
  1467. return as;
  1468. }
  1469. void
  1470. regerror(char *s)
  1471. {
  1472. USED(s);
  1473. error(Q);
  1474. }