ed.c 19 KB

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