1 /* vi: set sw=4 ts=4: */
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.
7 * The "ed" built-in command (much simplified)
12 #define USERSIZE 1024 /* max line length typed in by user */
13 #define INITBUF_SIZE 1024 /* initial buffer size */
21 static LINE lines, *curLine;
22 static int curNum, lastNum, marks[26], dirty;
23 static char *bufBase, *bufPtr, *fileName, searchString[USERSIZE];
24 static int bufUsed, bufSize;
26 static void doCommands(void);
27 static void subCommand(const char *cmd, int num1, int num2);
28 static int getNum(const char **retcp, int *retHaveNum, int *retNum);
29 static int setCurNum(int num);
30 static int initEdit(void);
31 static void termEdit(void);
32 static void addLines(int num);
33 static int insertLine(int num, const char *data, int len);
34 static int deleteLines(int num1, int num2);
35 static int printLines(int num1, int num2, int expandFlag);
36 static int writeLines(const char *file, int num1, int num2);
37 static int readLines(const char *file, int num);
38 static int searchLines(const char *str, int num1, int num2);
39 static LINE *findLine(int num);
41 static int findString(const LINE *lp, const char * str, int len, int offset);
43 int ed_main(int argc, char **argv);
44 int ed_main(int argc, char **argv)
50 fileName = strdup(argv[1]);
52 if (fileName == NULL) {
53 bb_error_msg("no memory");
58 if (!readLines(fileName, 1)) {
76 * Read commands until we are told to stop.
78 static void doCommands(void)
81 char *endbuf, *newname, buf[USERSIZE];
82 int len, num1, num2, have1, have2;
88 if (fgets(buf, sizeof(buf), stdin) == NULL)
96 endbuf = &buf[len - 1];
98 if (*endbuf != '\n') {
99 bb_error_msg("command line too long");
103 } while ((len != EOF) && (len != '\n'));
108 while ((endbuf > buf) && isblank(endbuf[-1]))
121 if ((curNum == 0) && (lastNum > 0)) {
123 curLine = lines.next;
126 if (!getNum(&cp, &have1, &num1))
135 if (!getNum(&cp, &have2, &num2))
160 deleteLines(num1, num2);
165 deleteLines(num1, num2);
169 if (*cp && !isblank(*cp)) {
170 bb_error_msg("bad file command");
179 printf("\"%s\"\n", fileName);
181 printf("No file name\n");
185 newname = strdup(cp);
187 if (newname == NULL) {
188 bb_error_msg("no memory for file name");
206 if ((*cp < 'a') || (*cp > 'a') || cp[1]) {
207 bb_error_msg("bad mark name");
211 marks[*cp - 'a'] = num2;
215 printLines(num1, num2, TRUE);
219 printLines(num1, num2, FALSE);
227 bb_error_msg("bad quit command");
234 printf("Really quit? ");
238 fgets(buf, sizeof(buf), stdin);
244 if ((*cp == 'y') || (*cp == 'Y'))
250 if (*cp && !isblank(*cp)) {
251 bb_error_msg("bad read command");
259 bb_error_msg("no file name");
266 if (readLines(cp, num1 + 1))
269 if (fileName == NULL)
270 fileName = strdup(cp);
275 subCommand(cp, num1, num2);
279 if (*cp && !isblank(*cp)) {
280 bb_error_msg("bad write command");
296 bb_error_msg("no file name specified");
300 writeLines(cp, num1, num2);
306 printLines(curNum-21, curNum, FALSE);
309 printLines(curNum-11, curNum+10, FALSE);
312 printLines(curNum, curNum+21, FALSE);
319 bb_error_msg("no arguments allowed");
323 printLines(curNum, curNum, FALSE);
327 if (setCurNum(curNum - 1))
328 printLines(curNum, curNum, FALSE);
333 printf("%d\n", num1);
338 printLines(num2, num2, FALSE);
342 if (setCurNum(curNum + 1))
343 printLines(curNum, curNum, FALSE);
348 bb_error_msg("unimplemented command");
356 * Do the substitute command.
357 * The current line is set to the last substitution done.
359 static void subCommand(const char * cmd, int num1, int num2)
361 char *cp, *oldStr, *newStr, buf[USERSIZE];
362 int delim, oldLen, newLen, deltaLen, offset;
364 int globalFlag, printFlag, didSub, needPrint;
366 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
367 bb_error_msg("bad line range for substitute");
377 * Copy the command so we can modify it.
382 if (isblank(*cp) || (*cp == '\0')) {
383 bb_error_msg("bad delimiter for substitute");
390 cp = strchr(cp, delim);
393 bb_error_msg("missing 2nd delimiter for substitute");
400 cp = strchr(cp, delim);
407 while (*cp) switch (*cp++) {
417 bb_error_msg("unknown option for substitute");
421 if (*oldStr == '\0') {
422 if (searchString[0] == '\0') {
423 bb_error_msg("no previous search string");
427 oldStr = searchString;
430 if (oldStr != searchString)
431 strcpy(searchString, oldStr);
438 oldLen = strlen(oldStr);
439 newLen = strlen(newStr);
440 deltaLen = newLen - oldLen;
444 while (num1 <= num2) {
445 offset = findString(lp, oldStr, oldLen, offset);
449 printLines(num1, num1, FALSE);
460 needPrint = printFlag;
465 * If the replacement string is the same size or shorter
466 * than the old string, then the substitution is easy.
469 memcpy(&lp->data[offset], newStr, newLen);
472 memcpy(&lp->data[offset + newLen],
473 &lp->data[offset + oldLen],
474 lp->len - offset - oldLen);
485 printLines(num1, num1, FALSE);
496 * The new string is larger, so allocate a new line
497 * structure and use that. Link it in in place of
498 * the old line structure.
500 nlp = (LINE *) malloc(sizeof(LINE) + lp->len + deltaLen);
503 bb_error_msg("cannot get memory for line");
507 nlp->len = lp->len + deltaLen;
509 memcpy(nlp->data, lp->data, offset);
511 memcpy(&nlp->data[offset], newStr, newLen);
513 memcpy(&nlp->data[offset + newLen],
514 &lp->data[offset + oldLen],
515 lp->len - offset - oldLen);
517 nlp->next = lp->next;
518 nlp->prev = lp->prev;
519 nlp->prev->next = nlp;
520 nlp->next->prev = nlp;
534 printLines(num1, num1, FALSE);
543 bb_error_msg("no substitutions found for \"%s\"", oldStr);
548 * Search a line for the specified string starting at the specified
549 * offset in the line. Returns the offset of the found string, or -1.
551 static int findString( const LINE * lp, const char * str, int len, int offset)
554 const char *cp, *ncp;
556 cp = &lp->data[offset];
557 left = lp->len - offset;
559 while (left >= len) {
560 ncp = memchr(cp, *str, left);
572 if (memcmp(cp, str, len) == 0)
573 return (cp - lp->data);
584 * Add lines which are typed in by the user.
585 * The lines are inserted just before the specified line number.
586 * The lines are terminated by a line containing a single dot (ugly!),
587 * or by an end of file.
589 static void addLines(int num)
592 char buf[USERSIZE + 1];
594 while (fgets(buf, sizeof(buf), stdin)) {
595 if ((buf[0] == '.') && (buf[1] == '\n') && (buf[2] == '\0'))
603 if (buf[len - 1] != '\n') {
604 bb_error_msg("line too long");
607 } while ((len != EOF) && (len != '\n'));
611 if (!insertLine(num++, buf, len))
618 * Parse a line number argument if it is present. This is a sum
619 * or difference of numbers, '.', '$', 'x, or a search string.
620 * Returns TRUE if successful (whether or not there was a number).
621 * Returns FALSE if there was a parsing error, with a message output.
622 * Whether there was a number is returned indirectly, as is the number.
623 * The character pointer which stopped the scan is also returned.
625 static int getNum(const char **retcp, int *retHaveNum, int *retNum)
628 char *endStr, str[USERSIZE];
629 int haveNum, value, num, sign;
656 if ((*cp < 'a') || (*cp > 'z')) {
657 bb_error_msg("bad mark name");
662 num = marks[*cp++ - 'a'];
667 endStr = strchr(str, '/');
671 cp += (endStr - str);
676 num = searchLines(str, curNum, lastNum);
687 *retHaveNum = haveNum;
695 num = num * 10 + *cp++ - '0';
719 *retHaveNum = haveNum;
728 * Initialize everything for editing.
730 static int initEdit(void)
734 bufSize = INITBUF_SIZE;
735 bufBase = malloc(bufSize);
737 if (bufBase == NULL) {
738 bb_error_msg("no memory for buffer");
753 searchString[0] = '\0';
755 for (i = 0; i < 26; i++)
765 static void termEdit(void)
780 searchString[0] = '\0';
783 deleteLines(1, lastNum);
792 * Read lines from a file at the specified line number.
793 * Returns TRUE if the file was successfully read.
795 static int readLines(const char * file, int num)
798 int len, lineCount, charCount;
801 if ((num < 1) || (num > lastNum + 1)) {
802 bb_error_msg("bad line for read");
819 printf("\"%s\", ", file);
823 cp = memchr(bufPtr, '\n', bufUsed);
826 len = (cp - bufPtr) + 1;
828 if (!insertLine(num, bufPtr, len)) {
842 if (bufPtr != bufBase) {
843 memcpy(bufBase, bufPtr, bufUsed);
844 bufPtr = bufBase + bufUsed;
847 if (bufUsed >= bufSize) {
848 len = (bufSize * 3) / 2;
849 cp = realloc(bufBase, len);
852 bb_error_msg("no memory for buffer");
858 bufPtr = bufBase + bufUsed;
862 cc = read(fd, bufPtr, bufSize - bufUsed);
875 if (!insertLine(num, bufPtr, bufUsed)) {
881 charCount += bufUsed;
886 printf("%d lines%s, %d chars\n", lineCount,
887 (bufUsed ? " (incomplete)" : ""), charCount);
894 * Write the specified lines out to the specified file.
895 * Returns TRUE if successful, or FALSE on an error with a message output.
897 static int writeLines(const char * file, int num1, int num2)
900 int fd, lineCount, charCount;
902 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
903 bb_error_msg("bad line range for write");
910 fd = creat(file, 0666);
917 printf("\"%s\", ", file);
927 while (num1++ <= num2) {
928 if (write(fd, lp->data, lp->len) != lp->len) {
934 charCount += lp->len;
944 printf("%d lines, %d chars\n", lineCount, charCount);
950 * Print lines in a specified range.
951 * The last line printed becomes the current line.
952 * If expandFlag is TRUE, then the line is printed specially to
953 * show magic characters.
955 static int printLines(int num1, int num2, int expandFlag)
961 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
962 bb_error_msg("bad line range for print");
971 while (num1 <= num2) {
973 write(1, lp->data, lp->len);
981 * Show control characters and characters with the
982 * high bit set specially.
987 if ((count > 0) && (cp[count - 1] == '\n'))
990 while (count-- > 0) {
1011 fputs("$\n", stdout);
1022 * Insert a new line with the specified text.
1023 * The line is inserted so as to become the specified line,
1024 * thus pushing any existing and further lines down one.
1025 * The inserted line is also set to become the current line.
1026 * Returns TRUE if successful.
1028 static int insertLine(int num, const char * data, int len)
1032 if ((num < 1) || (num > lastNum + 1)) {
1033 bb_error_msg("inserting at bad line number");
1037 newLp = malloc(sizeof(LINE) + len - 1);
1039 if (newLp == NULL) {
1040 bb_error_msg("failed to allocate memory for line");
1044 memcpy(newLp->data, data, len);
1053 free((char *) newLp);
1059 newLp->prev = lp->prev;
1060 lp->prev->next = newLp;
1065 return setCurNum(num);
1070 * Delete lines from the given range.
1072 static int deleteLines(int num1, int num2)
1074 LINE *lp, *nlp, *plp;
1077 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
1078 bb_error_msg("bad line numbers for delete");
1082 lp = findLine(num1);
1087 if ((curNum >= num1) && (curNum <= num2)) {
1089 setCurNum(num2 + 1);
1091 setCurNum(num1 - 1);
1096 count = num2 - num1 + 1;
1103 while (count-- > 0) {
1122 * Search for a line which contains the specified string.
1123 * If the string is NULL, then the previously searched for string
1124 * is used. The currently searched for string is saved for future use.
1125 * Returns the line number which matches, or 0 if there was no match
1126 * with an error printed.
1128 static int searchLines(const char *str, int num1, int num2)
1133 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
1134 bb_error_msg("bad line numbers for search");
1139 if (searchString[0] == '\0') {
1140 bb_error_msg("no previous search string");
1147 if (str != searchString)
1148 strcpy(searchString, str);
1152 lp = findLine(num1);
1157 while (num1 <= num2) {
1158 if (findString(lp, str, len, 0) >= 0)
1165 bb_error_msg("cannot find string \"%s\"", str);
1171 * Return a pointer to the specified line number.
1173 static LINE *findLine(int num)
1178 if ((num < 1) || (num > lastNum)) {
1179 bb_error_msg("line number %d does not exist", num);
1185 curLine = lines.next;
1194 if (num < (curNum / 2)) {
1198 else if (num > ((curNum + lastNum) / 2)) {
1203 while (lnum < num) {
1208 while (lnum > num) {
1217 * Set the current line number.
1218 * Returns TRUE if successful.
1220 static int setCurNum(int num)