ed.c 19 KB

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