ed.c 19 KB

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