ed.c 19 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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 (16 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. if (num <= curNum)
  298. curLine = curLine->prev;
  299. lastNum++;
  300. dirty = TRUE;
  301. return setCurNum(num);
  302. }
  303. /*
  304. * Add lines which are typed in by the user.
  305. * The lines are inserted just before the specified line number.
  306. * The lines are terminated by a line containing a single dot (ugly!),
  307. * or by an end of file.
  308. */
  309. static void addLines(int num)
  310. {
  311. int len;
  312. char buf[USERSIZE + 1];
  313. while (1) {
  314. /* Returns:
  315. * -1 on read errors or EOF, or on bare Ctrl-D.
  316. * 0 on ctrl-C,
  317. * >0 length of input string, including terminating '\n'
  318. */
  319. len = read_line_input(NULL, "", buf, sizeof(buf));
  320. if (len <= 0) {
  321. /* Previously, ctrl-C was exiting to shell.
  322. * Now we exit to ed prompt. Is in important? */
  323. return;
  324. }
  325. if (buf[0] == '.' && buf[1] == '\n' && buf[2] == '\0')
  326. return;
  327. if (!insertLine(num++, buf, len))
  328. return;
  329. }
  330. }
  331. /*
  332. * Read lines from a file at the specified line number.
  333. * Returns TRUE if the file was successfully read.
  334. */
  335. static int readLines(const char *file, int num)
  336. {
  337. int fd, cc;
  338. int len;
  339. unsigned charCount;
  340. char *cp;
  341. if ((num < 1) || (num > lastNum + 1)) {
  342. bb_simple_error_msg("bad line for read");
  343. return FALSE;
  344. }
  345. fd = open(file, 0);
  346. if (fd < 0) {
  347. bb_simple_perror_msg(file);
  348. return FALSE;
  349. }
  350. bufPtr = bufBase;
  351. bufUsed = 0;
  352. charCount = 0;
  353. cc = 0;
  354. do {
  355. cp = memchr(bufPtr, '\n', bufUsed);
  356. if (cp) {
  357. len = (cp - bufPtr) + 1;
  358. if (!insertLine(num, bufPtr, len)) {
  359. close(fd);
  360. return FALSE;
  361. }
  362. bufPtr += len;
  363. bufUsed -= len;
  364. charCount += len;
  365. num++;
  366. continue;
  367. }
  368. if (bufPtr != bufBase) {
  369. memcpy(bufBase, bufPtr, bufUsed);
  370. bufPtr = bufBase + bufUsed;
  371. }
  372. if (bufUsed >= bufSize) {
  373. len = (bufSize * 3) / 2;
  374. cp = xrealloc(bufBase, len);
  375. bufBase = cp;
  376. bufPtr = bufBase + bufUsed;
  377. bufSize = len;
  378. }
  379. cc = safe_read(fd, bufPtr, bufSize - bufUsed);
  380. bufUsed += cc;
  381. bufPtr = bufBase;
  382. } while (cc > 0);
  383. if (cc < 0) {
  384. bb_simple_perror_msg(file);
  385. close(fd);
  386. return FALSE;
  387. }
  388. if (bufUsed) {
  389. if (!insertLine(num, bufPtr, bufUsed)) {
  390. close(fd);
  391. return -1;
  392. }
  393. charCount += bufUsed;
  394. }
  395. close(fd);
  396. /* https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ed.html
  397. * "Read Command"
  398. * "...the number of bytes read shall be written to standard output
  399. * in the following format:
  400. * "%d\n", <number of bytes read>
  401. */
  402. if (!(option_mask32 & OPT_s))
  403. printf("%u\n", charCount);
  404. return TRUE;
  405. }
  406. /*
  407. * Write the specified lines out to the specified file.
  408. * Returns TRUE if successful, or FALSE on an error with a message output.
  409. */
  410. static int writeLines(const char *file, int num1, int num2)
  411. {
  412. LINE *lp;
  413. int fd;
  414. unsigned charCount;
  415. if (bad_nums(num1, num2, "write"))
  416. return FALSE;
  417. charCount = 0;
  418. fd = creat(file, 0666);
  419. if (fd < 0) {
  420. bb_simple_perror_msg(file);
  421. return FALSE;
  422. }
  423. lp = findLine(num1);
  424. if (lp == NULL) {
  425. close(fd);
  426. return FALSE;
  427. }
  428. while (num1++ <= num2) {
  429. if (full_write(fd, lp->data, lp->len) != lp->len) {
  430. bb_simple_perror_msg(file);
  431. close(fd);
  432. return FALSE;
  433. }
  434. charCount += lp->len;
  435. lp = lp->next;
  436. }
  437. if (close(fd) < 0) {
  438. bb_simple_perror_msg(file);
  439. return FALSE;
  440. }
  441. /* https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ed.html
  442. * "Write Command"
  443. * "...the number of bytes written shall be written to standard output,
  444. * unless the -s option was specified, in the following format:
  445. * "%d\n", <number of bytes written>
  446. */
  447. if (!(option_mask32 & OPT_s))
  448. printf("%u\n", charCount);
  449. return TRUE;
  450. }
  451. /*
  452. * Print lines in a specified range.
  453. * The last line printed becomes the current line.
  454. * If expandFlag is TRUE, then the line is printed specially to
  455. * show magic characters.
  456. */
  457. static int printLines(int num1, int num2, int expandFlag)
  458. {
  459. const LINE *lp;
  460. const char *cp;
  461. int ch, count;
  462. if (bad_nums(num1, num2, "print"))
  463. return FALSE;
  464. lp = findLine(num1);
  465. if (lp == NULL)
  466. return FALSE;
  467. while (num1 <= num2) {
  468. if (!expandFlag) {
  469. write(STDOUT_FILENO, lp->data, lp->len);
  470. setCurNum(num1++);
  471. lp = lp->next;
  472. continue;
  473. }
  474. /*
  475. * Show control characters and characters with the
  476. * high bit set specially.
  477. */
  478. cp = lp->data;
  479. count = lp->len;
  480. if ((count > 0) && (cp[count - 1] == '\n'))
  481. count--;
  482. while (count-- > 0) {
  483. ch = (unsigned char) *cp++;
  484. fputc_printable(ch | PRINTABLE_META, stdout);
  485. }
  486. fputs_stdout("$\n");
  487. setCurNum(num1++);
  488. lp = lp->next;
  489. }
  490. return TRUE;
  491. }
  492. /*
  493. * Delete lines from the given range.
  494. */
  495. static void deleteLines(int num1, int num2)
  496. {
  497. LINE *lp, *nlp, *plp;
  498. int count;
  499. if (bad_nums(num1, num2, "delete"))
  500. return;
  501. lp = findLine(num1);
  502. if (lp == NULL)
  503. return;
  504. if ((curNum >= num1) && (curNum <= num2)) {
  505. if (num2 < lastNum)
  506. setCurNum(num2 + 1);
  507. else if (num1 > 1)
  508. setCurNum(num1 - 1);
  509. else
  510. curNum = 0;
  511. }
  512. count = num2 - num1 + 1;
  513. if (curNum > num2)
  514. curNum -= count;
  515. lastNum -= count;
  516. while (count-- > 0) {
  517. nlp = lp->next;
  518. plp = lp->prev;
  519. plp->next = nlp;
  520. nlp->prev = plp;
  521. free(lp);
  522. lp = nlp;
  523. }
  524. dirty = TRUE;
  525. }
  526. /*
  527. * Do the substitute command.
  528. * The current line is set to the last substitution done.
  529. */
  530. static void subCommand(const char *cmd, int num1, int num2)
  531. {
  532. char *cp, *oldStr, *newStr, buf[USERSIZE];
  533. int delim, oldLen, newLen, deltaLen, offset;
  534. LINE *lp, *nlp;
  535. int globalFlag, printFlag, didSub, needPrint;
  536. if (bad_nums(num1, num2, "substitute"))
  537. return;
  538. globalFlag = FALSE;
  539. printFlag = FALSE;
  540. didSub = FALSE;
  541. needPrint = FALSE;
  542. /*
  543. * Copy the command so we can modify it.
  544. */
  545. strcpy(buf, cmd);
  546. cp = buf;
  547. if (isblank(*cp) || (*cp == '\0')) {
  548. bb_simple_error_msg("bad delimiter for substitute");
  549. return;
  550. }
  551. delim = *cp++;
  552. oldStr = cp;
  553. cp = strchr(cp, delim);
  554. if (cp == NULL) {
  555. bb_simple_error_msg("missing 2nd delimiter for substitute");
  556. return;
  557. }
  558. *cp++ = '\0';
  559. newStr = cp;
  560. cp = strchr(cp, delim);
  561. if (cp)
  562. *cp++ = '\0';
  563. else
  564. cp = (char*)"";
  565. while (*cp) switch (*cp++) {
  566. case 'g':
  567. globalFlag = TRUE;
  568. break;
  569. case 'p':
  570. printFlag = TRUE;
  571. break;
  572. default:
  573. bb_simple_error_msg("unknown option for substitute");
  574. return;
  575. }
  576. if (*oldStr == '\0') {
  577. if (searchString[0] == '\0') {
  578. bb_simple_error_msg("no previous search string");
  579. return;
  580. }
  581. oldStr = searchString;
  582. }
  583. if (oldStr != searchString)
  584. strcpy(searchString, oldStr);
  585. lp = findLine(num1);
  586. if (lp == NULL)
  587. return;
  588. oldLen = strlen(oldStr);
  589. newLen = strlen(newStr);
  590. deltaLen = newLen - oldLen;
  591. offset = 0;
  592. nlp = NULL;
  593. while (num1 <= num2) {
  594. offset = findString(lp, oldStr, oldLen, offset);
  595. if (offset < 0) {
  596. if (needPrint) {
  597. printLines(num1, num1, FALSE);
  598. needPrint = FALSE;
  599. }
  600. offset = 0;
  601. lp = lp->next;
  602. num1++;
  603. continue;
  604. }
  605. needPrint = printFlag;
  606. didSub = TRUE;
  607. dirty = TRUE;
  608. /*
  609. * If the replacement string is the same size or shorter
  610. * than the old string, then the substitution is easy.
  611. */
  612. if (deltaLen <= 0) {
  613. memcpy(&lp->data[offset], newStr, newLen);
  614. if (deltaLen) {
  615. memmove(&lp->data[offset + newLen],
  616. &lp->data[offset + oldLen],
  617. lp->len - offset - oldLen);
  618. lp->len += deltaLen;
  619. }
  620. offset += newLen;
  621. if (globalFlag)
  622. continue;
  623. if (needPrint) {
  624. printLines(num1, num1, FALSE);
  625. needPrint = FALSE;
  626. }
  627. lp = lp->next;
  628. num1++;
  629. continue;
  630. }
  631. /*
  632. * The new string is larger, so allocate a new line
  633. * structure and use that. Link it in place of
  634. * the old line structure.
  635. */
  636. nlp = xmalloc(sizeof(LINE) + lp->len + deltaLen);
  637. nlp->len = lp->len + deltaLen;
  638. memcpy(nlp->data, lp->data, offset);
  639. memcpy(&nlp->data[offset], newStr, newLen);
  640. memcpy(&nlp->data[offset + newLen],
  641. &lp->data[offset + oldLen],
  642. lp->len - offset - oldLen);
  643. nlp->next = lp->next;
  644. nlp->prev = lp->prev;
  645. nlp->prev->next = nlp;
  646. nlp->next->prev = nlp;
  647. if (curLine == lp)
  648. curLine = nlp;
  649. free(lp);
  650. lp = nlp;
  651. offset += newLen;
  652. if (globalFlag)
  653. continue;
  654. if (needPrint) {
  655. printLines(num1, num1, FALSE);
  656. needPrint = FALSE;
  657. }
  658. lp = lp->next;
  659. num1++;
  660. }
  661. if (!didSub)
  662. bb_error_msg("no substitutions found for \"%s\"", oldStr);
  663. }
  664. /*
  665. * Read commands until we are told to stop.
  666. */
  667. static void doCommands(void)
  668. {
  669. while (TRUE) {
  670. char buf[USERSIZE];
  671. const char *cp;
  672. int len;
  673. int n, num1, num2;
  674. smallint h, have1, have2;
  675. /* Returns:
  676. * -1 on read errors or EOF, or on bare Ctrl-D.
  677. * 0 on ctrl-C,
  678. * >0 length of input string, including terminating '\n'
  679. */
  680. len = read_line_input(NULL, prompt, buf, sizeof(buf));
  681. if (len <= 0)
  682. return;
  683. while (len && isspace(buf[--len]))
  684. buf[len] = '\0';
  685. if ((curNum == 0) && (lastNum > 0)) {
  686. curNum = 1;
  687. curLine = lines.next;
  688. }
  689. have1 = FALSE;
  690. have2 = FALSE;
  691. /* Don't pass &haveN, &numN to getNum() since this forces
  692. * compiler to keep them on stack, not in registers,
  693. * which is usually quite suboptimal.
  694. * Using intermediate variables shrinks code by ~150 bytes.
  695. */
  696. cp = getNum(skip_whitespace(buf), &h, &n);
  697. if (!cp)
  698. continue;
  699. have1 = h;
  700. num1 = n;
  701. cp = skip_whitespace(cp);
  702. if (*cp == ',') {
  703. cp = getNum(cp + 1, &h, &n);
  704. if (!cp)
  705. continue;
  706. num2 = n;
  707. if (!have1)
  708. num1 = 1;
  709. if (!h)
  710. num2 = lastNum;
  711. have1 = TRUE;
  712. have2 = TRUE;
  713. }
  714. if (!have1)
  715. num1 = curNum;
  716. if (!have2)
  717. num2 = num1;
  718. switch (*cp++) {
  719. case 'a':
  720. addLines(num1 + 1);
  721. break;
  722. case 'c':
  723. deleteLines(num1, num2);
  724. addLines(num1);
  725. break;
  726. case 'd':
  727. deleteLines(num1, num2);
  728. break;
  729. case 'f':
  730. if (*cp != '\0' && *cp != ' ') {
  731. bb_simple_error_msg("bad file command");
  732. break;
  733. }
  734. cp = skip_whitespace(cp);
  735. if (*cp == '\0') {
  736. if (fileName)
  737. printf("\"%s\"\n", fileName);
  738. else
  739. puts("No file name");
  740. break;
  741. }
  742. free(fileName);
  743. fileName = xstrdup(cp);
  744. break;
  745. case 'i':
  746. if (!have1 && lastNum == 0)
  747. num1 = 1;
  748. addLines(num1);
  749. break;
  750. case 'k':
  751. cp = skip_whitespace(cp);
  752. if ((unsigned)(*cp - 'a') >= 26 || cp[1]) {
  753. bb_simple_error_msg("bad mark name");
  754. break;
  755. }
  756. marks[(unsigned)(*cp - 'a')] = num2;
  757. break;
  758. case 'l':
  759. printLines(num1, num2, TRUE);
  760. break;
  761. case 'p':
  762. printLines(num1, num2, FALSE);
  763. break;
  764. case 'q':
  765. cp = skip_whitespace(cp);
  766. if (have1 || *cp) {
  767. bb_simple_error_msg("bad quit command");
  768. break;
  769. }
  770. if (!dirty)
  771. return;
  772. len = read_line_input(NULL, "Really quit? ", buf, 16);
  773. /* read error/EOF - no way to continue */
  774. if (len < 0)
  775. return;
  776. cp = skip_whitespace(buf);
  777. if ((*cp | 0x20) == 'y') /* Y or y */
  778. return;
  779. break;
  780. case 'r':
  781. if (*cp != '\0' && *cp != ' ') {
  782. bb_simple_error_msg("bad read command");
  783. break;
  784. }
  785. cp = skip_whitespace(cp);
  786. if (*cp == '\0') {
  787. bb_simple_error_msg("no file name");
  788. break;
  789. }
  790. if (!have1)
  791. num1 = lastNum;
  792. if (readLines(cp, num1 + 1))
  793. break;
  794. if (fileName == NULL)
  795. fileName = xstrdup(cp);
  796. break;
  797. case 's':
  798. subCommand(cp, num1, num2);
  799. break;
  800. case 'w':
  801. if (*cp != '\0' && *cp != ' ') {
  802. bb_simple_error_msg("bad write command");
  803. break;
  804. }
  805. cp = skip_whitespace(cp);
  806. if (*cp == '\0') {
  807. cp = fileName;
  808. if (!cp) {
  809. bb_simple_error_msg("no file name specified");
  810. break;
  811. }
  812. }
  813. if (!have1) {
  814. num1 = 1;
  815. num2 = lastNum;
  816. dirty = FALSE;
  817. }
  818. writeLines(cp, num1, num2);
  819. break;
  820. case 'z':
  821. switch (*cp) {
  822. case '-':
  823. printLines(curNum - 21, curNum, FALSE);
  824. break;
  825. case '.':
  826. printLines(curNum - 11, curNum + 10, FALSE);
  827. break;
  828. default:
  829. printLines(curNum, curNum + 21, FALSE);
  830. break;
  831. }
  832. break;
  833. case '.':
  834. if (have1) {
  835. bb_simple_error_msg("no arguments allowed");
  836. break;
  837. }
  838. printLines(curNum, curNum, FALSE);
  839. break;
  840. case '-':
  841. if (setCurNum(curNum - 1))
  842. printLines(curNum, curNum, FALSE);
  843. break;
  844. case '=':
  845. printf("%d\n", num1);
  846. break;
  847. case '\0':
  848. if (have1) {
  849. printLines(num2, num2, FALSE);
  850. break;
  851. }
  852. if (setCurNum(curNum + 1))
  853. printLines(curNum, curNum, FALSE);
  854. break;
  855. default:
  856. bb_simple_error_msg("unimplemented command");
  857. break;
  858. }
  859. }
  860. }
  861. int ed_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  862. int ed_main(int argc UNUSED_PARAM, char **argv)
  863. {
  864. INIT_G();
  865. bufSize = INITBUF_SIZE;
  866. bufBase = xmalloc(bufSize);
  867. bufPtr = bufBase;
  868. lines.next = &lines;
  869. lines.prev = &lines;
  870. prompt = ""; /* no prompt by default */
  871. getopt32(argv, OPTION_STR, &prompt);
  872. argv += optind;
  873. if (argv[0]) {
  874. fileName = xstrdup(argv[0]);
  875. if (!readLines(fileName, 1)) {
  876. return EXIT_SUCCESS;
  877. }
  878. dirty = FALSE;
  879. }
  880. doCommands();
  881. return EXIT_SUCCESS;
  882. }