ed.c 19 KB

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