ed.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (c) 2002 by David I. Bell
  4. * Permission is granted to use, distribute, or modify this source,
  5. * provided that this copyright notice remains intact.
  6. *
  7. * The "ed" built-in command (much simplified)
  8. */
  9. //config:config ED
  10. //config: bool "ed"
  11. //config: default y
  12. //config: help
  13. //config: The original 1970's Unix text editor, from the days of teletypes.
  14. //config: Small, simple, evil. Part of SUSv3. If you're not already using
  15. //config: this, you don't need it.
  16. //kbuild:lib-$(CONFIG_ED) += ed.o
  17. //applet:IF_ED(APPLET(ed, BB_DIR_BIN, BB_SUID_DROP))
  18. //usage:#define ed_trivial_usage ""
  19. //usage:#define ed_full_usage ""
  20. #include "libbb.h"
  21. typedef struct LINE {
  22. struct LINE *next;
  23. struct LINE *prev;
  24. int len;
  25. char data[1];
  26. } LINE;
  27. #define searchString bb_common_bufsiz1
  28. enum {
  29. USERSIZE = sizeof(searchString) > 1024 ? 1024
  30. : sizeof(searchString) - 1, /* max line length typed in by user */
  31. INITBUF_SIZE = 1024, /* initial buffer size */
  32. };
  33. struct globals {
  34. int curNum;
  35. int lastNum;
  36. int bufUsed;
  37. int bufSize;
  38. LINE *curLine;
  39. char *bufBase;
  40. char *bufPtr;
  41. char *fileName;
  42. LINE lines;
  43. smallint dirty;
  44. int marks[26];
  45. };
  46. #define G (*ptr_to_globals)
  47. #define curLine (G.curLine )
  48. #define bufBase (G.bufBase )
  49. #define bufPtr (G.bufPtr )
  50. #define fileName (G.fileName )
  51. #define curNum (G.curNum )
  52. #define lastNum (G.lastNum )
  53. #define bufUsed (G.bufUsed )
  54. #define bufSize (G.bufSize )
  55. #define dirty (G.dirty )
  56. #define lines (G.lines )
  57. #define marks (G.marks )
  58. #define INIT_G() do { \
  59. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  60. } while (0)
  61. static void doCommands(void);
  62. static void subCommand(const char *cmd, int num1, int num2);
  63. static int getNum(const char **retcp, smallint *retHaveNum, int *retNum);
  64. static int setCurNum(int num);
  65. static void addLines(int num);
  66. static int insertLine(int num, const char *data, int len);
  67. static void deleteLines(int num1, int num2);
  68. static int printLines(int num1, int num2, int expandFlag);
  69. static int writeLines(const char *file, int num1, int num2);
  70. static int readLines(const char *file, int num);
  71. static int searchLines(const char *str, int num1, int num2);
  72. static LINE *findLine(int num);
  73. static int findString(const LINE *lp, const char * str, int len, int offset);
  74. static int bad_nums(int num1, int num2, const char *for_what)
  75. {
  76. if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
  77. bb_error_msg("bad line range for %s", for_what);
  78. return 1;
  79. }
  80. return 0;
  81. }
  82. static char *skip_blank(const char *cp)
  83. {
  84. while (isblank(*cp))
  85. cp++;
  86. return (char *)cp;
  87. }
  88. int ed_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  89. int ed_main(int argc UNUSED_PARAM, char **argv)
  90. {
  91. INIT_G();
  92. bufSize = INITBUF_SIZE;
  93. bufBase = xmalloc(bufSize);
  94. bufPtr = bufBase;
  95. lines.next = &lines;
  96. lines.prev = &lines;
  97. if (argv[1]) {
  98. fileName = xstrdup(argv[1]);
  99. if (!readLines(fileName, 1)) {
  100. return EXIT_SUCCESS;
  101. }
  102. if (lastNum)
  103. setCurNum(1);
  104. dirty = FALSE;
  105. }
  106. doCommands();
  107. return EXIT_SUCCESS;
  108. }
  109. /*
  110. * Read commands until we are told to stop.
  111. */
  112. static void doCommands(void)
  113. {
  114. const char *cp;
  115. char *endbuf, buf[USERSIZE];
  116. int len, num1, num2;
  117. smallint have1, have2;
  118. while (TRUE) {
  119. /* Returns:
  120. * -1 on read errors or EOF, or on bare Ctrl-D.
  121. * 0 on ctrl-C,
  122. * >0 length of input string, including terminating '\n'
  123. */
  124. len = read_line_input(NULL, ": ", buf, sizeof(buf), /*timeout*/ -1);
  125. if (len <= 0)
  126. return;
  127. endbuf = &buf[len - 1];
  128. while ((endbuf > buf) && isblank(endbuf[-1]))
  129. endbuf--;
  130. *endbuf = '\0';
  131. cp = skip_blank(buf);
  132. have1 = FALSE;
  133. have2 = FALSE;
  134. if ((curNum == 0) && (lastNum > 0)) {
  135. curNum = 1;
  136. curLine = lines.next;
  137. }
  138. if (!getNum(&cp, &have1, &num1))
  139. continue;
  140. cp = skip_blank(cp);
  141. if (*cp == ',') {
  142. cp++;
  143. if (!getNum(&cp, &have2, &num2))
  144. continue;
  145. if (!have1)
  146. num1 = 1;
  147. if (!have2)
  148. num2 = lastNum;
  149. have1 = TRUE;
  150. have2 = TRUE;
  151. }
  152. if (!have1)
  153. num1 = curNum;
  154. if (!have2)
  155. num2 = num1;
  156. switch (*cp++) {
  157. case 'a':
  158. addLines(num1 + 1);
  159. break;
  160. case 'c':
  161. deleteLines(num1, num2);
  162. addLines(num1);
  163. break;
  164. case 'd':
  165. deleteLines(num1, num2);
  166. break;
  167. case 'f':
  168. if (*cp && !isblank(*cp)) {
  169. bb_error_msg("bad file command");
  170. break;
  171. }
  172. cp = skip_blank(cp);
  173. if (*cp == '\0') {
  174. if (fileName)
  175. printf("\"%s\"\n", fileName);
  176. else
  177. puts("No file name");
  178. break;
  179. }
  180. free(fileName);
  181. fileName = xstrdup(cp);
  182. break;
  183. case 'i':
  184. addLines(num1);
  185. break;
  186. case 'k':
  187. cp = skip_blank(cp);
  188. if ((*cp < 'a') || (*cp > 'z') || cp[1]) {
  189. bb_error_msg("bad mark name");
  190. break;
  191. }
  192. marks[*cp - 'a'] = num2;
  193. break;
  194. case 'l':
  195. printLines(num1, num2, TRUE);
  196. break;
  197. case 'p':
  198. printLines(num1, num2, FALSE);
  199. break;
  200. case 'q':
  201. cp = skip_blank(cp);
  202. if (have1 || *cp) {
  203. bb_error_msg("bad quit command");
  204. break;
  205. }
  206. if (!dirty)
  207. return;
  208. len = read_line_input(NULL, "Really quit? ", buf, 16, /*timeout*/ -1);
  209. /* read error/EOF - no way to continue */
  210. if (len < 0)
  211. return;
  212. cp = skip_blank(buf);
  213. if ((*cp | 0x20) == 'y') /* Y or y */
  214. return;
  215. break;
  216. case 'r':
  217. if (*cp && !isblank(*cp)) {
  218. bb_error_msg("bad read command");
  219. break;
  220. }
  221. cp = skip_blank(cp);
  222. if (*cp == '\0') {
  223. bb_error_msg("no file name");
  224. break;
  225. }
  226. if (!have1)
  227. num1 = lastNum;
  228. if (readLines(cp, num1 + 1))
  229. break;
  230. if (fileName == NULL)
  231. fileName = xstrdup(cp);
  232. break;
  233. case 's':
  234. subCommand(cp, num1, num2);
  235. break;
  236. case 'w':
  237. if (*cp && !isblank(*cp)) {
  238. bb_error_msg("bad write command");
  239. break;
  240. }
  241. cp = skip_blank(cp);
  242. if (!have1) {
  243. num1 = 1;
  244. num2 = lastNum;
  245. }
  246. if (*cp == '\0')
  247. cp = fileName;
  248. if (cp == NULL) {
  249. bb_error_msg("no file name specified");
  250. break;
  251. }
  252. writeLines(cp, num1, num2);
  253. break;
  254. case 'z':
  255. switch (*cp) {
  256. case '-':
  257. printLines(curNum - 21, curNum, FALSE);
  258. break;
  259. case '.':
  260. printLines(curNum - 11, curNum + 10, FALSE);
  261. break;
  262. default:
  263. printLines(curNum, curNum + 21, FALSE);
  264. break;
  265. }
  266. break;
  267. case '.':
  268. if (have1) {
  269. bb_error_msg("no arguments allowed");
  270. break;
  271. }
  272. printLines(curNum, curNum, FALSE);
  273. break;
  274. case '-':
  275. if (setCurNum(curNum - 1))
  276. printLines(curNum, curNum, FALSE);
  277. break;
  278. case '=':
  279. printf("%d\n", num1);
  280. break;
  281. case '\0':
  282. if (have1) {
  283. printLines(num2, num2, FALSE);
  284. break;
  285. }
  286. if (setCurNum(curNum + 1))
  287. printLines(curNum, curNum, FALSE);
  288. break;
  289. default:
  290. bb_error_msg("unimplemented command");
  291. break;
  292. }
  293. }
  294. }
  295. /*
  296. * Do the substitute command.
  297. * The current line is set to the last substitution done.
  298. */
  299. static void subCommand(const char *cmd, int num1, int num2)
  300. {
  301. char *cp, *oldStr, *newStr, buf[USERSIZE];
  302. int delim, oldLen, newLen, deltaLen, offset;
  303. LINE *lp, *nlp;
  304. int globalFlag, printFlag, didSub, needPrint;
  305. if (bad_nums(num1, num2, "substitute"))
  306. return;
  307. globalFlag = FALSE;
  308. printFlag = FALSE;
  309. didSub = FALSE;
  310. needPrint = FALSE;
  311. /*
  312. * Copy the command so we can modify it.
  313. */
  314. strcpy(buf, cmd);
  315. cp = buf;
  316. if (isblank(*cp) || (*cp == '\0')) {
  317. bb_error_msg("bad delimiter for substitute");
  318. return;
  319. }
  320. delim = *cp++;
  321. oldStr = cp;
  322. cp = strchr(cp, delim);
  323. if (cp == NULL) {
  324. bb_error_msg("missing 2nd delimiter for substitute");
  325. return;
  326. }
  327. *cp++ = '\0';
  328. newStr = cp;
  329. cp = strchr(cp, delim);
  330. if (cp)
  331. *cp++ = '\0';
  332. else
  333. cp = (char*)"";
  334. while (*cp) switch (*cp++) {
  335. case 'g':
  336. globalFlag = TRUE;
  337. break;
  338. case 'p':
  339. printFlag = TRUE;
  340. break;
  341. default:
  342. bb_error_msg("unknown option for substitute");
  343. return;
  344. }
  345. if (*oldStr == '\0') {
  346. if (searchString[0] == '\0') {
  347. bb_error_msg("no previous search string");
  348. return;
  349. }
  350. oldStr = searchString;
  351. }
  352. if (oldStr != searchString)
  353. strcpy(searchString, oldStr);
  354. lp = findLine(num1);
  355. if (lp == NULL)
  356. return;
  357. oldLen = strlen(oldStr);
  358. newLen = strlen(newStr);
  359. deltaLen = newLen - oldLen;
  360. offset = 0;
  361. nlp = NULL;
  362. while (num1 <= num2) {
  363. offset = findString(lp, oldStr, oldLen, offset);
  364. if (offset < 0) {
  365. if (needPrint) {
  366. printLines(num1, num1, FALSE);
  367. needPrint = FALSE;
  368. }
  369. offset = 0;
  370. lp = lp->next;
  371. num1++;
  372. continue;
  373. }
  374. needPrint = printFlag;
  375. didSub = TRUE;
  376. dirty = TRUE;
  377. /*
  378. * If the replacement string is the same size or shorter
  379. * than the old string, then the substitution is easy.
  380. */
  381. if (deltaLen <= 0) {
  382. memcpy(&lp->data[offset], newStr, newLen);
  383. if (deltaLen) {
  384. memcpy(&lp->data[offset + newLen],
  385. &lp->data[offset + oldLen],
  386. lp->len - offset - oldLen);
  387. lp->len += deltaLen;
  388. }
  389. offset += newLen;
  390. if (globalFlag)
  391. continue;
  392. if (needPrint) {
  393. printLines(num1, num1, FALSE);
  394. needPrint = FALSE;
  395. }
  396. lp = lp->next;
  397. num1++;
  398. continue;
  399. }
  400. /*
  401. * The new string is larger, so allocate a new line
  402. * structure and use that. Link it in place of
  403. * the old line structure.
  404. */
  405. nlp = xmalloc(sizeof(LINE) + lp->len + deltaLen);
  406. nlp->len = lp->len + deltaLen;
  407. memcpy(nlp->data, lp->data, offset);
  408. memcpy(&nlp->data[offset], newStr, newLen);
  409. memcpy(&nlp->data[offset + newLen],
  410. &lp->data[offset + oldLen],
  411. lp->len - offset - oldLen);
  412. nlp->next = lp->next;
  413. nlp->prev = lp->prev;
  414. nlp->prev->next = nlp;
  415. nlp->next->prev = nlp;
  416. if (curLine == lp)
  417. curLine = nlp;
  418. free(lp);
  419. lp = nlp;
  420. offset += newLen;
  421. if (globalFlag)
  422. continue;
  423. if (needPrint) {
  424. printLines(num1, num1, FALSE);
  425. needPrint = FALSE;
  426. }
  427. lp = lp->next;
  428. num1++;
  429. }
  430. if (!didSub)
  431. bb_error_msg("no substitutions found for \"%s\"", oldStr);
  432. }
  433. /*
  434. * Search a line for the specified string starting at the specified
  435. * offset in the line. Returns the offset of the found string, or -1.
  436. */
  437. static int findString(const LINE *lp, const char *str, int len, int offset)
  438. {
  439. int left;
  440. const char *cp, *ncp;
  441. cp = &lp->data[offset];
  442. left = lp->len - offset;
  443. while (left >= len) {
  444. ncp = memchr(cp, *str, left);
  445. if (ncp == NULL)
  446. return -1;
  447. left -= (ncp - cp);
  448. if (left < len)
  449. return -1;
  450. cp = ncp;
  451. if (memcmp(cp, str, len) == 0)
  452. return (cp - lp->data);
  453. cp++;
  454. left--;
  455. }
  456. return -1;
  457. }
  458. /*
  459. * Add lines which are typed in by the user.
  460. * The lines are inserted just before the specified line number.
  461. * The lines are terminated by a line containing a single dot (ugly!),
  462. * or by an end of file.
  463. */
  464. static void addLines(int num)
  465. {
  466. int len;
  467. char buf[USERSIZE + 1];
  468. while (1) {
  469. /* Returns:
  470. * -1 on read errors or EOF, or on bare Ctrl-D.
  471. * 0 on ctrl-C,
  472. * >0 length of input string, including terminating '\n'
  473. */
  474. len = read_line_input(NULL, "", buf, sizeof(buf), /*timeout*/ -1);
  475. if (len <= 0) {
  476. /* Previously, ctrl-C was exiting to shell.
  477. * Now we exit to ed prompt. Is in important? */
  478. return;
  479. }
  480. if ((buf[0] == '.') && (buf[1] == '\n') && (buf[2] == '\0'))
  481. return;
  482. if (!insertLine(num++, buf, len))
  483. return;
  484. }
  485. }
  486. /*
  487. * Parse a line number argument if it is present. This is a sum
  488. * or difference of numbers, '.', '$', 'x, or a search string.
  489. * Returns TRUE if successful (whether or not there was a number).
  490. * Returns FALSE if there was a parsing error, with a message output.
  491. * Whether there was a number is returned indirectly, as is the number.
  492. * The character pointer which stopped the scan is also returned.
  493. */
  494. static int getNum(const char **retcp, smallint *retHaveNum, int *retNum)
  495. {
  496. const char *cp;
  497. char *endStr, str[USERSIZE];
  498. int value, num;
  499. smallint haveNum, minus;
  500. cp = *retcp;
  501. value = 0;
  502. haveNum = FALSE;
  503. minus = 0;
  504. while (TRUE) {
  505. cp = skip_blank(cp);
  506. switch (*cp) {
  507. case '.':
  508. haveNum = TRUE;
  509. num = curNum;
  510. cp++;
  511. break;
  512. case '$':
  513. haveNum = TRUE;
  514. num = lastNum;
  515. cp++;
  516. break;
  517. case '\'':
  518. cp++;
  519. if ((*cp < 'a') || (*cp > 'z')) {
  520. bb_error_msg("bad mark name");
  521. return FALSE;
  522. }
  523. haveNum = TRUE;
  524. num = marks[*cp++ - 'a'];
  525. break;
  526. case '/':
  527. strcpy(str, ++cp);
  528. endStr = strchr(str, '/');
  529. if (endStr) {
  530. *endStr++ = '\0';
  531. cp += (endStr - str);
  532. } else
  533. cp = "";
  534. num = searchLines(str, curNum, lastNum);
  535. if (num == 0)
  536. return FALSE;
  537. haveNum = TRUE;
  538. break;
  539. default:
  540. if (!isdigit(*cp)) {
  541. *retcp = cp;
  542. *retHaveNum = haveNum;
  543. *retNum = value;
  544. return TRUE;
  545. }
  546. num = 0;
  547. while (isdigit(*cp))
  548. num = num * 10 + *cp++ - '0';
  549. haveNum = TRUE;
  550. break;
  551. }
  552. value += (minus ? -num : num);
  553. cp = skip_blank(cp);
  554. switch (*cp) {
  555. case '-':
  556. minus = 1;
  557. cp++;
  558. break;
  559. case '+':
  560. minus = 0;
  561. cp++;
  562. break;
  563. default:
  564. *retcp = cp;
  565. *retHaveNum = haveNum;
  566. *retNum = value;
  567. return TRUE;
  568. }
  569. }
  570. }
  571. /*
  572. * Read lines from a file at the specified line number.
  573. * Returns TRUE if the file was successfully read.
  574. */
  575. static int readLines(const char *file, int num)
  576. {
  577. int fd, cc;
  578. int len, lineCount, charCount;
  579. char *cp;
  580. if ((num < 1) || (num > lastNum + 1)) {
  581. bb_error_msg("bad line for read");
  582. return FALSE;
  583. }
  584. fd = open(file, 0);
  585. if (fd < 0) {
  586. bb_simple_perror_msg(file);
  587. return FALSE;
  588. }
  589. bufPtr = bufBase;
  590. bufUsed = 0;
  591. lineCount = 0;
  592. charCount = 0;
  593. cc = 0;
  594. printf("\"%s\", ", file);
  595. fflush_all();
  596. do {
  597. cp = memchr(bufPtr, '\n', bufUsed);
  598. if (cp) {
  599. len = (cp - bufPtr) + 1;
  600. if (!insertLine(num, bufPtr, len)) {
  601. close(fd);
  602. return FALSE;
  603. }
  604. bufPtr += len;
  605. bufUsed -= len;
  606. charCount += len;
  607. lineCount++;
  608. num++;
  609. continue;
  610. }
  611. if (bufPtr != bufBase) {
  612. memcpy(bufBase, bufPtr, bufUsed);
  613. bufPtr = bufBase + bufUsed;
  614. }
  615. if (bufUsed >= bufSize) {
  616. len = (bufSize * 3) / 2;
  617. cp = xrealloc(bufBase, len);
  618. bufBase = cp;
  619. bufPtr = bufBase + bufUsed;
  620. bufSize = len;
  621. }
  622. cc = safe_read(fd, bufPtr, bufSize - bufUsed);
  623. bufUsed += cc;
  624. bufPtr = bufBase;
  625. } while (cc > 0);
  626. if (cc < 0) {
  627. bb_simple_perror_msg(file);
  628. close(fd);
  629. return FALSE;
  630. }
  631. if (bufUsed) {
  632. if (!insertLine(num, bufPtr, bufUsed)) {
  633. close(fd);
  634. return -1;
  635. }
  636. lineCount++;
  637. charCount += bufUsed;
  638. }
  639. close(fd);
  640. printf("%d lines%s, %d chars\n", lineCount,
  641. (bufUsed ? " (incomplete)" : ""), charCount);
  642. return TRUE;
  643. }
  644. /*
  645. * Write the specified lines out to the specified file.
  646. * Returns TRUE if successful, or FALSE on an error with a message output.
  647. */
  648. static int writeLines(const char *file, int num1, int num2)
  649. {
  650. LINE *lp;
  651. int fd, lineCount, charCount;
  652. if (bad_nums(num1, num2, "write"))
  653. return FALSE;
  654. lineCount = 0;
  655. charCount = 0;
  656. fd = creat(file, 0666);
  657. if (fd < 0) {
  658. bb_simple_perror_msg(file);
  659. return FALSE;
  660. }
  661. printf("\"%s\", ", file);
  662. fflush_all();
  663. lp = findLine(num1);
  664. if (lp == NULL) {
  665. close(fd);
  666. return FALSE;
  667. }
  668. while (num1++ <= num2) {
  669. if (full_write(fd, lp->data, lp->len) != lp->len) {
  670. bb_simple_perror_msg(file);
  671. close(fd);
  672. return FALSE;
  673. }
  674. charCount += lp->len;
  675. lineCount++;
  676. lp = lp->next;
  677. }
  678. if (close(fd) < 0) {
  679. bb_simple_perror_msg(file);
  680. return FALSE;
  681. }
  682. printf("%d lines, %d chars\n", lineCount, charCount);
  683. return TRUE;
  684. }
  685. /*
  686. * Print lines in a specified range.
  687. * The last line printed becomes the current line.
  688. * If expandFlag is TRUE, then the line is printed specially to
  689. * show magic characters.
  690. */
  691. static int printLines(int num1, int num2, int expandFlag)
  692. {
  693. const LINE *lp;
  694. const char *cp;
  695. int ch, count;
  696. if (bad_nums(num1, num2, "print"))
  697. return FALSE;
  698. lp = findLine(num1);
  699. if (lp == NULL)
  700. return FALSE;
  701. while (num1 <= num2) {
  702. if (!expandFlag) {
  703. write(STDOUT_FILENO, lp->data, lp->len);
  704. setCurNum(num1++);
  705. lp = lp->next;
  706. continue;
  707. }
  708. /*
  709. * Show control characters and characters with the
  710. * high bit set specially.
  711. */
  712. cp = lp->data;
  713. count = lp->len;
  714. if ((count > 0) && (cp[count - 1] == '\n'))
  715. count--;
  716. while (count-- > 0) {
  717. ch = (unsigned char) *cp++;
  718. fputc_printable(ch | PRINTABLE_META, stdout);
  719. }
  720. fputs("$\n", stdout);
  721. setCurNum(num1++);
  722. lp = lp->next;
  723. }
  724. return TRUE;
  725. }
  726. /*
  727. * Insert a new line with the specified text.
  728. * The line is inserted so as to become the specified line,
  729. * thus pushing any existing and further lines down one.
  730. * The inserted line is also set to become the current line.
  731. * Returns TRUE if successful.
  732. */
  733. static int insertLine(int num, const char *data, int len)
  734. {
  735. LINE *newLp, *lp;
  736. if ((num < 1) || (num > lastNum + 1)) {
  737. bb_error_msg("inserting at bad line number");
  738. return FALSE;
  739. }
  740. newLp = xmalloc(sizeof(LINE) + len - 1);
  741. memcpy(newLp->data, data, len);
  742. newLp->len = len;
  743. if (num > lastNum)
  744. lp = &lines;
  745. else {
  746. lp = findLine(num);
  747. if (lp == NULL) {
  748. free((char *) newLp);
  749. return FALSE;
  750. }
  751. }
  752. newLp->next = lp;
  753. newLp->prev = lp->prev;
  754. lp->prev->next = newLp;
  755. lp->prev = newLp;
  756. lastNum++;
  757. dirty = TRUE;
  758. return setCurNum(num);
  759. }
  760. /*
  761. * Delete lines from the given range.
  762. */
  763. static void deleteLines(int num1, int num2)
  764. {
  765. LINE *lp, *nlp, *plp;
  766. int count;
  767. if (bad_nums(num1, num2, "delete"))
  768. return;
  769. lp = findLine(num1);
  770. if (lp == NULL)
  771. return;
  772. if ((curNum >= num1) && (curNum <= num2)) {
  773. if (num2 < lastNum)
  774. setCurNum(num2 + 1);
  775. else if (num1 > 1)
  776. setCurNum(num1 - 1);
  777. else
  778. curNum = 0;
  779. }
  780. count = num2 - num1 + 1;
  781. if (curNum > num2)
  782. curNum -= count;
  783. lastNum -= count;
  784. while (count-- > 0) {
  785. nlp = lp->next;
  786. plp = lp->prev;
  787. plp->next = nlp;
  788. nlp->prev = plp;
  789. free(lp);
  790. lp = nlp;
  791. }
  792. dirty = TRUE;
  793. }
  794. /*
  795. * Search for a line which contains the specified string.
  796. * If the string is "", then the previously searched for string
  797. * is used. The currently searched for string is saved for future use.
  798. * Returns the line number which matches, or 0 if there was no match
  799. * with an error printed.
  800. */
  801. static NOINLINE int searchLines(const char *str, int num1, int num2)
  802. {
  803. const LINE *lp;
  804. int len;
  805. if (bad_nums(num1, num2, "search"))
  806. return 0;
  807. if (*str == '\0') {
  808. if (searchString[0] == '\0') {
  809. bb_error_msg("no previous search string");
  810. return 0;
  811. }
  812. str = searchString;
  813. }
  814. if (str != searchString)
  815. strcpy(searchString, str);
  816. len = strlen(str);
  817. lp = findLine(num1);
  818. if (lp == NULL)
  819. return 0;
  820. while (num1 <= num2) {
  821. if (findString(lp, str, len, 0) >= 0)
  822. return num1;
  823. num1++;
  824. lp = lp->next;
  825. }
  826. bb_error_msg("can't find string \"%s\"", str);
  827. return 0;
  828. }
  829. /*
  830. * Return a pointer to the specified line number.
  831. */
  832. static LINE *findLine(int num)
  833. {
  834. LINE *lp;
  835. int lnum;
  836. if ((num < 1) || (num > lastNum)) {
  837. bb_error_msg("line number %d does not exist", num);
  838. return NULL;
  839. }
  840. if (curNum <= 0) {
  841. curNum = 1;
  842. curLine = lines.next;
  843. }
  844. if (num == curNum)
  845. return curLine;
  846. lp = curLine;
  847. lnum = curNum;
  848. if (num < (curNum / 2)) {
  849. lp = lines.next;
  850. lnum = 1;
  851. } else if (num > ((curNum + lastNum) / 2)) {
  852. lp = lines.prev;
  853. lnum = lastNum;
  854. }
  855. while (lnum < num) {
  856. lp = lp->next;
  857. lnum++;
  858. }
  859. while (lnum > num) {
  860. lp = lp->prev;
  861. lnum--;
  862. }
  863. return lp;
  864. }
  865. /*
  866. * Set the current line number.
  867. * Returns TRUE if successful.
  868. */
  869. static int setCurNum(int num)
  870. {
  871. LINE *lp;
  872. lp = findLine(num);
  873. if (lp == NULL)
  874. return FALSE;
  875. curNum = num;
  876. curLine = lp;
  877. return TRUE;
  878. }