3
0

ed.c 19 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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. printf("No file name\n");
  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. }