ed.c 19 KB

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