ed.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  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. return 0;
  682. }
  683. int
  684. gettty(void)
  685. {
  686. int rc;
  687. rc = gety();
  688. if(rc)
  689. return rc;
  690. if(linebuf[0] == '.' && linebuf[1] == 0)
  691. return EOF;
  692. return 0;
  693. }
  694. int
  695. getfile(void)
  696. {
  697. int c;
  698. Rune *lp;
  699. lp = linebuf;
  700. do {
  701. c = Bgetrune(&iobuf);
  702. if(c < 0) {
  703. if(lp > linebuf) {
  704. putst("'\\n' appended");
  705. c = '\n';
  706. } else
  707. return EOF;
  708. }
  709. if(lp >= &linebuf[LBSIZE]) {
  710. lastc = '\n';
  711. error(Q);
  712. }
  713. *lp++ = c;
  714. count++;
  715. } while(c != '\n');
  716. lp[-1] = 0;
  717. return 0;
  718. }
  719. void
  720. putfile(void)
  721. {
  722. int *a1;
  723. Rune *lp;
  724. long c;
  725. a1 = addr1;
  726. do {
  727. lp = getline(*a1++);
  728. for(;;) {
  729. count++;
  730. c = *lp++;
  731. if(c == 0) {
  732. if(Bputrune(&iobuf, '\n') < 0)
  733. error(Q);
  734. break;
  735. }
  736. if(Bputrune(&iobuf, c) < 0)
  737. error(Q);
  738. }
  739. } while(a1 <= addr2);
  740. if(Bflush(&iobuf) < 0)
  741. error(Q);
  742. }
  743. int
  744. append(int (*f)(void), int *a)
  745. {
  746. int *a1, *a2, *rdot, nline, tl;
  747. nline = 0;
  748. dot = a;
  749. while((*f)() == 0) {
  750. if((dol-zero) >= nlall) {
  751. nlall += 512;
  752. a1 = realloc(zero, (nlall+5)*sizeof(int*));
  753. if(a1 == 0) {
  754. error("MEM?");
  755. rescue();
  756. }
  757. tl = a1 - zero; /* relocate pointers */
  758. zero += tl;
  759. addr1 += tl;
  760. addr2 += tl;
  761. dol += tl;
  762. dot += tl;
  763. }
  764. tl = putline();
  765. nline++;
  766. a1 = ++dol;
  767. a2 = a1+1;
  768. rdot = ++dot;
  769. while(a1 > rdot)
  770. *--a2 = *--a1;
  771. *rdot = tl;
  772. }
  773. return nline;
  774. }
  775. void
  776. add(int i)
  777. {
  778. if(i && (given || dol > zero)) {
  779. addr1--;
  780. addr2--;
  781. }
  782. squeeze(0);
  783. newline();
  784. append(gettty, addr2);
  785. }
  786. void
  787. browse(void)
  788. {
  789. int forward, n;
  790. static bformat, bnum; /* 0 */
  791. forward = 1;
  792. peekc = getchr();
  793. if(peekc != '\n'){
  794. if(peekc == '-' || peekc == '+') {
  795. if(peekc == '-')
  796. forward = 0;
  797. getchr();
  798. }
  799. n = getnum();
  800. if(n > 0)
  801. bpagesize = n;
  802. }
  803. newline();
  804. if(pflag) {
  805. bformat = listf;
  806. bnum = listn;
  807. } else {
  808. listf = bformat;
  809. listn = bnum;
  810. }
  811. if(forward) {
  812. addr1 = addr2;
  813. addr2 += bpagesize;
  814. if(addr2 > dol)
  815. addr2 = dol;
  816. } else {
  817. addr1 = addr2-bpagesize;
  818. if(addr1 <= zero)
  819. addr1 = zero+1;
  820. }
  821. printcom();
  822. }
  823. void
  824. callunix(void)
  825. {
  826. int c, pid;
  827. Rune rune;
  828. char buf[512];
  829. char *p;
  830. setnoaddr();
  831. p = buf;
  832. while((c=getchr()) != EOF && c != '\n')
  833. if(p < &buf[sizeof(buf) - 6]) {
  834. rune = c;
  835. p += runetochar(p, &rune);
  836. }
  837. *p = 0;
  838. pid = fork();
  839. if(pid == 0) {
  840. execl("/bin/rc", "rc", "-c", buf, 0);
  841. exits("execl failed");
  842. }
  843. waiting = 1;
  844. while(waitpid() != pid)
  845. ;
  846. waiting = 0;
  847. if(vflag)
  848. putst("!");
  849. }
  850. void
  851. quit(void)
  852. {
  853. if(vflag && fchange && dol!=zero) {
  854. fchange = 0;
  855. error(Q);
  856. }
  857. remove(tfname);
  858. exits(0);
  859. }
  860. void
  861. onquit(int sig)
  862. {
  863. USED(sig);
  864. quit();
  865. }
  866. void
  867. rdelete(int *ad1, int *ad2)
  868. {
  869. int *a1, *a2, *a3;
  870. a1 = ad1;
  871. a2 = ad2+1;
  872. a3 = dol;
  873. dol -= a2 - a1;
  874. do {
  875. *a1++ = *a2++;
  876. } while(a2 <= a3);
  877. a1 = ad1;
  878. if(a1 > dol)
  879. a1 = dol;
  880. dot = a1;
  881. fchange = 1;
  882. }
  883. void
  884. gdelete(void)
  885. {
  886. int *a1, *a2, *a3;
  887. a3 = dol;
  888. for(a1=zero; (*a1&01)==0; a1++)
  889. if(a1>=a3)
  890. return;
  891. for(a2=a1+1; a2<=a3;) {
  892. if(*a2 & 01) {
  893. a2++;
  894. dot = a1;
  895. } else
  896. *a1++ = *a2++;
  897. }
  898. dol = a1-1;
  899. if(dot > dol)
  900. dot = dol;
  901. fchange = 1;
  902. }
  903. Rune*
  904. getline(int tl)
  905. {
  906. Rune *lp, *bp;
  907. int nl;
  908. lp = linebuf;
  909. bp = getblock(tl, OREAD);
  910. nl = nleft;
  911. tl &= ~((BLKSIZE/2) - 1);
  912. while(*lp++ = *bp++) {
  913. nl -= sizeof(Rune);
  914. if(nl == 0) {
  915. bp = getblock(tl += BLKSIZE/2, OREAD);
  916. nl = nleft;
  917. }
  918. }
  919. return linebuf;
  920. }
  921. int
  922. putline(void)
  923. {
  924. Rune *lp, *bp;
  925. int nl, tl;
  926. fchange = 1;
  927. lp = linebuf;
  928. tl = tline;
  929. bp = getblock(tl, OWRITE);
  930. nl = nleft;
  931. tl &= ~((BLKSIZE/2)-1);
  932. while(*bp = *lp++) {
  933. if(*bp++ == '\n') {
  934. bp[-1] = 0;
  935. linebp = lp;
  936. break;
  937. }
  938. nl -= sizeof(Rune);
  939. if(nl == 0) {
  940. tl += BLKSIZE/2;
  941. bp = getblock(tl, OWRITE);
  942. nl = nleft;
  943. }
  944. }
  945. nl = tline;
  946. tline += ((lp-linebuf) + 03) & 077776;
  947. return nl;
  948. }
  949. void
  950. blkio(int b, uchar *buf, long (*iofcn)(int, void *, long))
  951. {
  952. seek(tfile, b*BLKSIZE, 0);
  953. if((*iofcn)(tfile, buf, BLKSIZE) != BLKSIZE) {
  954. error(T);
  955. }
  956. }
  957. Rune*
  958. getblock(int atl, int iof)
  959. {
  960. int bno, off;
  961. static uchar ibuff[BLKSIZE];
  962. static uchar obuff[BLKSIZE];
  963. bno = atl / (BLKSIZE/2);
  964. off = (atl<<1) & (BLKSIZE-1) & ~03;
  965. if(bno >= NBLK) {
  966. lastc = '\n';
  967. error(T);
  968. }
  969. nleft = BLKSIZE - off;
  970. if(bno == iblock) {
  971. ichanged |= iof;
  972. return (Rune*)(ibuff+off);
  973. }
  974. if(bno == oblock)
  975. return (Rune*)(obuff+off);
  976. if(iof == OREAD) {
  977. if(ichanged)
  978. blkio(iblock, ibuff, write);
  979. ichanged = 0;
  980. iblock = bno;
  981. blkio(bno, ibuff, read);
  982. return (Rune*)(ibuff+off);
  983. }
  984. if(oblock >= 0)
  985. blkio(oblock, obuff, write);
  986. oblock = bno;
  987. return (Rune*)(obuff+off);
  988. }
  989. void
  990. init(void)
  991. {
  992. int *markp;
  993. close(tfile);
  994. tline = 2;
  995. for(markp = names; markp < &names[26]; )
  996. *markp++ = 0;
  997. subnewa = 0;
  998. anymarks = 0;
  999. iblock = -1;
  1000. oblock = -1;
  1001. ichanged = 0;
  1002. if((tfile = create(tfname, ORDWR, 0600)) < 0){
  1003. error1(T);
  1004. exits(0);
  1005. }
  1006. dot = dol = zero;
  1007. }
  1008. void
  1009. global(int k)
  1010. {
  1011. Rune *gp, globuf[GBSIZE];
  1012. int c, *a1;
  1013. if(globp)
  1014. error(Q);
  1015. setwide();
  1016. squeeze(dol > zero);
  1017. c = getchr();
  1018. if(c == '\n')
  1019. error(Q);
  1020. compile(c);
  1021. gp = globuf;
  1022. while((c=getchr()) != '\n') {
  1023. if(c == EOF)
  1024. error(Q);
  1025. if(c == '\\') {
  1026. c = getchr();
  1027. if(c != '\n')
  1028. *gp++ = '\\';
  1029. }
  1030. *gp++ = c;
  1031. if(gp >= &globuf[GBSIZE-2])
  1032. error(Q);
  1033. }
  1034. if(gp == globuf)
  1035. *gp++ = 'p';
  1036. *gp++ = '\n';
  1037. *gp = 0;
  1038. for(a1=zero; a1<=dol; a1++) {
  1039. *a1 &= ~01;
  1040. if(a1 >= addr1 && a1 <= addr2 && match(a1) == k)
  1041. *a1 |= 01;
  1042. }
  1043. /*
  1044. * Special case: g/.../d (avoid n^2 algorithm)
  1045. */
  1046. if(globuf[0] == 'd' && globuf[1] == '\n' && globuf[2] == 0) {
  1047. gdelete();
  1048. return;
  1049. }
  1050. for(a1=zero; a1<=dol; a1++) {
  1051. if(*a1 & 01) {
  1052. *a1 &= ~01;
  1053. dot = a1;
  1054. globp = globuf;
  1055. commands();
  1056. a1 = zero;
  1057. }
  1058. }
  1059. }
  1060. void
  1061. join(void)
  1062. {
  1063. Rune *gp, *lp;
  1064. int *a1;
  1065. nonzero();
  1066. gp = genbuf;
  1067. for(a1=addr1; a1<=addr2; a1++) {
  1068. lp = getline(*a1);
  1069. while(*gp = *lp++)
  1070. if(gp++ >= &genbuf[LBSIZE-2])
  1071. error(Q);
  1072. }
  1073. lp = linebuf;
  1074. gp = genbuf;
  1075. while(*lp++ = *gp++)
  1076. ;
  1077. *addr1 = putline();
  1078. if(addr1 < addr2)
  1079. rdelete(addr1+1, addr2);
  1080. dot = addr1;
  1081. }
  1082. void
  1083. substitute(int inglob)
  1084. {
  1085. int *mp, *a1, nl, gsubf, n;
  1086. n = getnum(); /* OK even if n==0 */
  1087. gsubf = compsub();
  1088. for(a1 = addr1; a1 <= addr2; a1++) {
  1089. if(match(a1)){
  1090. int *ozero;
  1091. int m = n;
  1092. do {
  1093. int span = loc2-loc1;
  1094. if(--m <= 0) {
  1095. dosub();
  1096. if(!gsubf)
  1097. break;
  1098. if(span == 0) { /* null RE match */
  1099. if(*loc2 == 0)
  1100. break;
  1101. loc2++;
  1102. }
  1103. }
  1104. } while(match(0));
  1105. if(m <= 0) {
  1106. inglob |= 01;
  1107. subnewa = putline();
  1108. *a1 &= ~01;
  1109. if(anymarks) {
  1110. for(mp=names; mp<&names[26]; mp++)
  1111. if(*mp == *a1)
  1112. *mp = subnewa;
  1113. }
  1114. subolda = *a1;
  1115. *a1 = subnewa;
  1116. ozero = zero;
  1117. nl = append(getsub, a1);
  1118. addr2 += nl;
  1119. nl += zero-ozero;
  1120. a1 += nl;
  1121. }
  1122. }
  1123. }
  1124. if(inglob == 0)
  1125. error(Q);
  1126. }
  1127. int
  1128. compsub(void)
  1129. {
  1130. int seof, c;
  1131. Rune *p;
  1132. seof = getchr();
  1133. if(seof == '\n' || seof == ' ')
  1134. error(Q);
  1135. compile(seof);
  1136. p = rhsbuf;
  1137. for(;;) {
  1138. c = getchr();
  1139. if(c == '\\') {
  1140. c = getchr();
  1141. *p++ = ESCFLG;
  1142. if(p >= &rhsbuf[LBSIZE/2])
  1143. error(Q);
  1144. } else
  1145. if(c == '\n' && (!globp || !globp[0])) {
  1146. peekc = c;
  1147. pflag++;
  1148. break;
  1149. } else
  1150. if(c == seof)
  1151. break;
  1152. *p++ = c;
  1153. if(p >= &rhsbuf[LBSIZE/2])
  1154. error(Q);
  1155. }
  1156. *p = 0;
  1157. peekc = getchr();
  1158. if(peekc == 'g') {
  1159. peekc = 0;
  1160. newline();
  1161. return 1;
  1162. }
  1163. newline();
  1164. return 0;
  1165. }
  1166. int
  1167. getsub(void)
  1168. {
  1169. Rune *p1, *p2;
  1170. p1 = linebuf;
  1171. if((p2 = linebp) == 0)
  1172. return EOF;
  1173. while(*p1++ = *p2++)
  1174. ;
  1175. linebp = 0;
  1176. return 0;
  1177. }
  1178. void
  1179. dosub(void)
  1180. {
  1181. Rune *lp, *sp, *rp;
  1182. int c, n;
  1183. lp = linebuf;
  1184. sp = genbuf;
  1185. rp = rhsbuf;
  1186. while(lp < loc1)
  1187. *sp++ = *lp++;
  1188. while(c = *rp++) {
  1189. if(c == '&'){
  1190. sp = place(sp, loc1, loc2);
  1191. continue;
  1192. }
  1193. if(c == ESCFLG && (c = *rp++) >= '1' && c < MAXSUB+'0') {
  1194. n = c-'0';
  1195. if(subexp[n].rsp && subexp[n].rep) {
  1196. sp = place(sp, subexp[n].rsp, subexp[n].rep);
  1197. continue;
  1198. }
  1199. error(Q);
  1200. }
  1201. *sp++ = c;
  1202. if(sp >= &genbuf[LBSIZE])
  1203. error(Q);
  1204. }
  1205. lp = loc2;
  1206. loc2 = sp - genbuf + linebuf;
  1207. while(*sp++ = *lp++)
  1208. if(sp >= &genbuf[LBSIZE])
  1209. error(Q);
  1210. lp = linebuf;
  1211. sp = genbuf;
  1212. while(*lp++ = *sp++)
  1213. ;
  1214. }
  1215. Rune*
  1216. place(Rune *sp, Rune *l1, Rune *l2)
  1217. {
  1218. while(l1 < l2) {
  1219. *sp++ = *l1++;
  1220. if(sp >= &genbuf[LBSIZE])
  1221. error(Q);
  1222. }
  1223. return sp;
  1224. }
  1225. void
  1226. move(int cflag)
  1227. {
  1228. int *adt, *ad1, *ad2;
  1229. nonzero();
  1230. if((adt = address())==0) /* address() guarantees addr is in range */
  1231. error(Q);
  1232. newline();
  1233. if(cflag) {
  1234. int *ozero, delta;
  1235. ad1 = dol;
  1236. ozero = zero;
  1237. append(getcopy, ad1++);
  1238. ad2 = dol;
  1239. delta = zero - ozero;
  1240. ad1 += delta;
  1241. adt += delta;
  1242. } else {
  1243. ad2 = addr2;
  1244. for(ad1 = addr1; ad1 <= ad2;)
  1245. *ad1++ &= ~01;
  1246. ad1 = addr1;
  1247. }
  1248. ad2++;
  1249. if(adt<ad1) {
  1250. dot = adt + (ad2-ad1);
  1251. if((++adt)==ad1)
  1252. return;
  1253. reverse(adt, ad1);
  1254. reverse(ad1, ad2);
  1255. reverse(adt, ad2);
  1256. } else
  1257. if(adt >= ad2) {
  1258. dot = adt++;
  1259. reverse(ad1, ad2);
  1260. reverse(ad2, adt);
  1261. reverse(ad1, adt);
  1262. } else
  1263. error(Q);
  1264. fchange = 1;
  1265. }
  1266. void
  1267. reverse(int *a1, int *a2)
  1268. {
  1269. int t;
  1270. for(;;) {
  1271. t = *--a2;
  1272. if(a2 <= a1)
  1273. return;
  1274. *a2 = *a1;
  1275. *a1++ = t;
  1276. }
  1277. }
  1278. int
  1279. getcopy(void)
  1280. {
  1281. if(addr1 > addr2)
  1282. return EOF;
  1283. getline(*addr1++);
  1284. return 0;
  1285. }
  1286. void
  1287. compile(int eof)
  1288. {
  1289. Rune c;
  1290. char *ep;
  1291. char expbuf[ESIZE];
  1292. if((c = getchr()) == '\n') {
  1293. peekc = c;
  1294. c = eof;
  1295. }
  1296. if(c == eof) {
  1297. if(!pattern)
  1298. error(Q);
  1299. return;
  1300. }
  1301. if(pattern) {
  1302. free(pattern);
  1303. pattern = 0;
  1304. }
  1305. ep = expbuf;
  1306. do {
  1307. if(c == '\\') {
  1308. if(ep >= expbuf+sizeof(expbuf)) {
  1309. error(Q);
  1310. return;
  1311. }
  1312. ep += runetochar(ep, &c);
  1313. if((c = getchr()) == '\n') {
  1314. error(Q);
  1315. return;
  1316. }
  1317. }
  1318. if(ep >= expbuf+sizeof(expbuf)) {
  1319. error(Q);
  1320. return;
  1321. }
  1322. ep += runetochar(ep, &c);
  1323. } while((c = getchr()) != eof && c != '\n');
  1324. if(c == '\n')
  1325. peekc = c;
  1326. *ep = 0;
  1327. pattern = regcomp(expbuf);
  1328. }
  1329. int
  1330. match(int *addr)
  1331. {
  1332. if(!pattern)
  1333. return 0;
  1334. if(addr){
  1335. if(addr == zero)
  1336. return 0;
  1337. subexp[0].rsp = getline(*addr);
  1338. } else
  1339. subexp[0].rsp = loc2;
  1340. subexp[0].rep = 0;
  1341. if(rregexec(pattern, linebuf, subexp, MAXSUB)) {
  1342. loc1 = subexp[0].rsp;
  1343. loc2 = subexp[0].rep;
  1344. return 1;
  1345. }
  1346. loc1 = loc2 = 0;
  1347. return 0;
  1348. }
  1349. void
  1350. putd(void)
  1351. {
  1352. int r;
  1353. r = count%10;
  1354. count /= 10;
  1355. if(count)
  1356. putd();
  1357. putchr(r + L'0');
  1358. }
  1359. void
  1360. putst(char *sp)
  1361. {
  1362. Rune r;
  1363. col = 0;
  1364. for(;;) {
  1365. sp += chartorune(&r, sp);
  1366. if(r == 0)
  1367. break;
  1368. putchr(r);
  1369. }
  1370. putchr(L'\n');
  1371. }
  1372. void
  1373. putshst(Rune *sp)
  1374. {
  1375. col = 0;
  1376. while(*sp)
  1377. putchr(*sp++);
  1378. putchr(L'\n');
  1379. }
  1380. void
  1381. putchr(int ac)
  1382. {
  1383. char *lp;
  1384. int c;
  1385. Rune rune;
  1386. lp = linp;
  1387. c = ac;
  1388. if(listf) {
  1389. if(c == '\n') {
  1390. if(linp != line && linp[-1] == ' ') {
  1391. *lp++ = '\\';
  1392. *lp++ = 'n';
  1393. }
  1394. } else {
  1395. if(col > (72-6-2)) {
  1396. col = 8;
  1397. *lp++ = '\\';
  1398. *lp++ = '\n';
  1399. *lp++ = '\t';
  1400. }
  1401. col++;
  1402. if(c=='\b' || c=='\t' || c=='\\') {
  1403. *lp++ = '\\';
  1404. if(c == '\b')
  1405. c = 'b';
  1406. else
  1407. if(c == '\t')
  1408. c = 't';
  1409. col++;
  1410. } else
  1411. if(c<' ' || c>='\177') {
  1412. *lp++ = '\\';
  1413. *lp++ = 'x';
  1414. *lp++ = hex[c>>12];
  1415. *lp++ = hex[c>>8&0xF];
  1416. *lp++ = hex[c>>4&0xF];
  1417. c = hex[c&0xF];
  1418. col += 5;
  1419. }
  1420. }
  1421. }
  1422. rune = c;
  1423. lp += runetochar(lp, &rune);
  1424. if(c == '\n' || lp >= &line[sizeof(line)-5]) {
  1425. linp = line;
  1426. write(oflag? 2: 1, line, lp-line);
  1427. return;
  1428. }
  1429. linp = lp;
  1430. }
  1431. char*
  1432. mktemp(char *as)
  1433. {
  1434. char *s;
  1435. unsigned pid;
  1436. int i;
  1437. pid = getpid();
  1438. s = as;
  1439. while(*s++)
  1440. ;
  1441. s--;
  1442. while(*--s == 'X') {
  1443. *s = pid % 10 + '0';
  1444. pid /= 10;
  1445. }
  1446. s++;
  1447. i = 'a';
  1448. while(access(as, 0) != -1) {
  1449. if(i == 'z')
  1450. return "/";
  1451. *s = i++;
  1452. }
  1453. return as;
  1454. }
  1455. void
  1456. regerror(char *s)
  1457. {
  1458. USED(s);
  1459. error(Q);
  1460. }