ed.c 19 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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 ATTRIBUTE_UNUSED, 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 = malloc(sizeof(LINE) + lp->len + deltaLen);
  395. if (nlp == NULL) {
  396. bb_error_msg("cannot get memory for line");
  397. return;
  398. }
  399. nlp->len = lp->len + deltaLen;
  400. memcpy(nlp->data, lp->data, offset);
  401. memcpy(&nlp->data[offset], newStr, newLen);
  402. memcpy(&nlp->data[offset + newLen],
  403. &lp->data[offset + oldLen],
  404. lp->len - offset - oldLen);
  405. nlp->next = lp->next;
  406. nlp->prev = lp->prev;
  407. nlp->prev->next = nlp;
  408. nlp->next->prev = nlp;
  409. if (curLine == lp)
  410. curLine = nlp;
  411. free(lp);
  412. lp = nlp;
  413. offset += newLen;
  414. if (globalFlag)
  415. continue;
  416. if (needPrint) {
  417. printLines(num1, num1, FALSE);
  418. needPrint = FALSE;
  419. }
  420. lp = lp->next;
  421. num1++;
  422. }
  423. if (!didSub)
  424. bb_error_msg("no substitutions found for \"%s\"", oldStr);
  425. }
  426. /*
  427. * Search a line for the specified string starting at the specified
  428. * offset in the line. Returns the offset of the found string, or -1.
  429. */
  430. static int findString(const LINE *lp, const char *str, int len, int offset)
  431. {
  432. int left;
  433. const char *cp, *ncp;
  434. cp = &lp->data[offset];
  435. left = lp->len - offset;
  436. while (left >= len) {
  437. ncp = memchr(cp, *str, left);
  438. if (ncp == NULL)
  439. return -1;
  440. left -= (ncp - cp);
  441. if (left < len)
  442. return -1;
  443. cp = ncp;
  444. if (memcmp(cp, str, len) == 0)
  445. return (cp - lp->data);
  446. cp++;
  447. left--;
  448. }
  449. return -1;
  450. }
  451. /*
  452. * Add lines which are typed in by the user.
  453. * The lines are inserted just before the specified line number.
  454. * The lines are terminated by a line containing a single dot (ugly!),
  455. * or by an end of file.
  456. */
  457. static void addLines(int num)
  458. {
  459. int len;
  460. char buf[USERSIZE + 1];
  461. while (1) {
  462. /* Returns:
  463. * -1 on read errors or EOF, or on bare Ctrl-D.
  464. * 0 on ctrl-C,
  465. * >0 length of input string, including terminating '\n'
  466. */
  467. len = read_line_input("", buf, sizeof(buf), NULL);
  468. if (len <= 0) {
  469. /* Previously, ctrl-C was exiting to shell.
  470. * Now we exit to ed prompt. Is in important? */
  471. return;
  472. }
  473. if ((buf[0] == '.') && (buf[1] == '\n') && (buf[2] == '\0'))
  474. return;
  475. if (!insertLine(num++, buf, len))
  476. return;
  477. }
  478. }
  479. /*
  480. * Parse a line number argument if it is present. This is a sum
  481. * or difference of numbers, '.', '$', 'x, or a search string.
  482. * Returns TRUE if successful (whether or not there was a number).
  483. * Returns FALSE if there was a parsing error, with a message output.
  484. * Whether there was a number is returned indirectly, as is the number.
  485. * The character pointer which stopped the scan is also returned.
  486. */
  487. static int getNum(const char **retcp, smallint *retHaveNum, int *retNum)
  488. {
  489. const char *cp;
  490. char *endStr, str[USERSIZE];
  491. int value, num;
  492. smallint haveNum, minus;
  493. cp = *retcp;
  494. value = 0;
  495. haveNum = FALSE;
  496. minus = 0;
  497. while (TRUE) {
  498. cp = skip_blank(cp);
  499. switch (*cp) {
  500. case '.':
  501. haveNum = TRUE;
  502. num = curNum;
  503. cp++;
  504. break;
  505. case '$':
  506. haveNum = TRUE;
  507. num = lastNum;
  508. cp++;
  509. break;
  510. case '\'':
  511. cp++;
  512. if ((*cp < 'a') || (*cp > 'z')) {
  513. bb_error_msg("bad mark name");
  514. return FALSE;
  515. }
  516. haveNum = TRUE;
  517. num = marks[*cp++ - 'a'];
  518. break;
  519. case '/':
  520. strcpy(str, ++cp);
  521. endStr = strchr(str, '/');
  522. if (endStr) {
  523. *endStr++ = '\0';
  524. cp += (endStr - str);
  525. } else
  526. cp = "";
  527. num = searchLines(str, curNum, lastNum);
  528. if (num == 0)
  529. return FALSE;
  530. haveNum = TRUE;
  531. break;
  532. default:
  533. if (!isdigit(*cp)) {
  534. *retcp = cp;
  535. *retHaveNum = haveNum;
  536. *retNum = value;
  537. return TRUE;
  538. }
  539. num = 0;
  540. while (isdigit(*cp))
  541. num = num * 10 + *cp++ - '0';
  542. haveNum = TRUE;
  543. break;
  544. }
  545. value += (minus ? -num : num);
  546. cp = skip_blank(cp);
  547. switch (*cp) {
  548. case '-':
  549. minus = 1;
  550. cp++;
  551. break;
  552. case '+':
  553. minus = 0;
  554. cp++;
  555. break;
  556. default:
  557. *retcp = cp;
  558. *retHaveNum = haveNum;
  559. *retNum = value;
  560. return TRUE;
  561. }
  562. }
  563. }
  564. /*
  565. * Read lines from a file at the specified line number.
  566. * Returns TRUE if the file was successfully read.
  567. */
  568. static int readLines(const char *file, int num)
  569. {
  570. int fd, cc;
  571. int len, lineCount, charCount;
  572. char *cp;
  573. if ((num < 1) || (num > lastNum + 1)) {
  574. bb_error_msg("bad line for read");
  575. return FALSE;
  576. }
  577. fd = open(file, 0);
  578. if (fd < 0) {
  579. perror(file);
  580. return FALSE;
  581. }
  582. bufPtr = bufBase;
  583. bufUsed = 0;
  584. lineCount = 0;
  585. charCount = 0;
  586. cc = 0;
  587. printf("\"%s\", ", file);
  588. fflush(stdout);
  589. do {
  590. cp = memchr(bufPtr, '\n', bufUsed);
  591. if (cp) {
  592. len = (cp - bufPtr) + 1;
  593. if (!insertLine(num, bufPtr, len)) {
  594. close(fd);
  595. return FALSE;
  596. }
  597. bufPtr += len;
  598. bufUsed -= len;
  599. charCount += len;
  600. lineCount++;
  601. num++;
  602. continue;
  603. }
  604. if (bufPtr != bufBase) {
  605. memcpy(bufBase, bufPtr, bufUsed);
  606. bufPtr = bufBase + bufUsed;
  607. }
  608. if (bufUsed >= bufSize) {
  609. len = (bufSize * 3) / 2;
  610. cp = realloc(bufBase, len);
  611. if (cp == NULL) {
  612. bb_error_msg("no memory for buffer");
  613. close(fd);
  614. return FALSE;
  615. }
  616. bufBase = cp;
  617. bufPtr = bufBase + bufUsed;
  618. bufSize = len;
  619. }
  620. cc = safe_read(fd, bufPtr, bufSize - bufUsed);
  621. bufUsed += cc;
  622. bufPtr = bufBase;
  623. } while (cc > 0);
  624. if (cc < 0) {
  625. perror(file);
  626. close(fd);
  627. return FALSE;
  628. }
  629. if (bufUsed) {
  630. if (!insertLine(num, bufPtr, bufUsed)) {
  631. close(fd);
  632. return -1;
  633. }
  634. lineCount++;
  635. charCount += bufUsed;
  636. }
  637. close(fd);
  638. printf("%d lines%s, %d chars\n", lineCount,
  639. (bufUsed ? " (incomplete)" : ""), charCount);
  640. return TRUE;
  641. }
  642. /*
  643. * Write the specified lines out to the specified file.
  644. * Returns TRUE if successful, or FALSE on an error with a message output.
  645. */
  646. static int writeLines(const char *file, int num1, int num2)
  647. {
  648. LINE *lp;
  649. int fd, lineCount, charCount;
  650. if (bad_nums(num1, num2, "write"))
  651. return FALSE;
  652. lineCount = 0;
  653. charCount = 0;
  654. fd = creat(file, 0666);
  655. if (fd < 0) {
  656. perror(file);
  657. return FALSE;
  658. }
  659. printf("\"%s\", ", file);
  660. fflush(stdout);
  661. lp = findLine(num1);
  662. if (lp == NULL) {
  663. close(fd);
  664. return FALSE;
  665. }
  666. while (num1++ <= num2) {
  667. if (full_write(fd, lp->data, lp->len) != lp->len) {
  668. perror(file);
  669. close(fd);
  670. return FALSE;
  671. }
  672. charCount += lp->len;
  673. lineCount++;
  674. lp = lp->next;
  675. }
  676. if (close(fd) < 0) {
  677. perror(file);
  678. return FALSE;
  679. }
  680. printf("%d lines, %d chars\n", lineCount, charCount);
  681. return TRUE;
  682. }
  683. /*
  684. * Print lines in a specified range.
  685. * The last line printed becomes the current line.
  686. * If expandFlag is TRUE, then the line is printed specially to
  687. * show magic characters.
  688. */
  689. static int printLines(int num1, int num2, int expandFlag)
  690. {
  691. const LINE *lp;
  692. const char *cp;
  693. int ch, count;
  694. if (bad_nums(num1, num2, "print"))
  695. return FALSE;
  696. lp = findLine(num1);
  697. if (lp == NULL)
  698. return FALSE;
  699. while (num1 <= num2) {
  700. if (!expandFlag) {
  701. write(STDOUT_FILENO, lp->data, lp->len);
  702. setCurNum(num1++);
  703. lp = lp->next;
  704. continue;
  705. }
  706. /*
  707. * Show control characters and characters with the
  708. * high bit set specially.
  709. */
  710. cp = lp->data;
  711. count = lp->len;
  712. if ((count > 0) && (cp[count - 1] == '\n'))
  713. count--;
  714. while (count-- > 0) {
  715. ch = (unsigned char) *cp++;
  716. fputc_printable(ch | PRINTABLE_META, stdout);
  717. }
  718. fputs("$\n", stdout);
  719. setCurNum(num1++);
  720. lp = lp->next;
  721. }
  722. return TRUE;
  723. }
  724. /*
  725. * Insert a new line with the specified text.
  726. * The line is inserted so as to become the specified line,
  727. * thus pushing any existing and further lines down one.
  728. * The inserted line is also set to become the current line.
  729. * Returns TRUE if successful.
  730. */
  731. static int insertLine(int num, const char *data, int len)
  732. {
  733. LINE *newLp, *lp;
  734. if ((num < 1) || (num > lastNum + 1)) {
  735. bb_error_msg("inserting at bad line number");
  736. return FALSE;
  737. }
  738. newLp = malloc(sizeof(LINE) + len - 1);
  739. if (newLp == NULL) {
  740. bb_error_msg("failed to allocate memory for line");
  741. return FALSE;
  742. }
  743. memcpy(newLp->data, data, len);
  744. newLp->len = len;
  745. if (num > lastNum)
  746. lp = &lines;
  747. else {
  748. lp = findLine(num);
  749. if (lp == NULL) {
  750. free((char *) newLp);
  751. return FALSE;
  752. }
  753. }
  754. newLp->next = lp;
  755. newLp->prev = lp->prev;
  756. lp->prev->next = newLp;
  757. lp->prev = newLp;
  758. lastNum++;
  759. dirty = TRUE;
  760. return setCurNum(num);
  761. }
  762. /*
  763. * Delete lines from the given range.
  764. */
  765. static void deleteLines(int num1, int num2)
  766. {
  767. LINE *lp, *nlp, *plp;
  768. int count;
  769. if (bad_nums(num1, num2, "delete"))
  770. return;
  771. lp = findLine(num1);
  772. if (lp == NULL)
  773. return;
  774. if ((curNum >= num1) && (curNum <= num2)) {
  775. if (num2 < lastNum)
  776. setCurNum(num2 + 1);
  777. else if (num1 > 1)
  778. setCurNum(num1 - 1);
  779. else
  780. curNum = 0;
  781. }
  782. count = num2 - num1 + 1;
  783. if (curNum > num2)
  784. curNum -= count;
  785. lastNum -= count;
  786. while (count-- > 0) {
  787. nlp = lp->next;
  788. plp = lp->prev;
  789. plp->next = nlp;
  790. nlp->prev = plp;
  791. free(lp);
  792. lp = nlp;
  793. }
  794. dirty = TRUE;
  795. }
  796. /*
  797. * Search for a line which contains the specified string.
  798. * If the string is "", then the previously searched for string
  799. * is used. The currently searched for string is saved for future use.
  800. * Returns the line number which matches, or 0 if there was no match
  801. * with an error printed.
  802. */
  803. static int searchLines(const char *str, int num1, int num2)
  804. {
  805. const LINE *lp;
  806. int len;
  807. if (bad_nums(num1, num2, "search"))
  808. return 0;
  809. if (*str == '\0') {
  810. if (searchString[0] == '\0') {
  811. bb_error_msg("no previous search string");
  812. return 0;
  813. }
  814. str = searchString;
  815. }
  816. if (str != searchString)
  817. strcpy(searchString, str);
  818. len = strlen(str);
  819. lp = findLine(num1);
  820. if (lp == NULL)
  821. return 0;
  822. while (num1 <= num2) {
  823. if (findString(lp, str, len, 0) >= 0)
  824. return num1;
  825. num1++;
  826. lp = lp->next;
  827. }
  828. bb_error_msg("cannot find string \"%s\"", str);
  829. return 0;
  830. }
  831. /*
  832. * Return a pointer to the specified line number.
  833. */
  834. static LINE *findLine(int num)
  835. {
  836. LINE *lp;
  837. int lnum;
  838. if ((num < 1) || (num > lastNum)) {
  839. bb_error_msg("line number %d does not exist", num);
  840. return NULL;
  841. }
  842. if (curNum <= 0) {
  843. curNum = 1;
  844. curLine = lines.next;
  845. }
  846. if (num == curNum)
  847. return curLine;
  848. lp = curLine;
  849. lnum = curNum;
  850. if (num < (curNum / 2)) {
  851. lp = lines.next;
  852. lnum = 1;
  853. } else if (num > ((curNum + lastNum) / 2)) {
  854. lp = lines.prev;
  855. lnum = lastNum;
  856. }
  857. while (lnum < num) {
  858. lp = lp->next;
  859. lnum++;
  860. }
  861. while (lnum > num) {
  862. lp = lp->prev;
  863. lnum--;
  864. }
  865. return lp;
  866. }
  867. /*
  868. * Set the current line number.
  869. * Returns TRUE if successful.
  870. */
  871. static int setCurNum(int num)
  872. {
  873. LINE *lp;
  874. lp = findLine(num);
  875. if (lp == NULL)
  876. return FALSE;
  877. curNum = num;
  878. curLine = lp;
  879. return TRUE;
  880. }