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)
49 fileName = strdup(argv[1]);
51 if (fileName == NULL) {
52 bb_error_msg("no memory");
57 if (!readLines(fileName, 1)) {
75 * Read commands until we are told to stop.
77 static void doCommands(void)
80 char *endbuf, *newname, buf[USERSIZE];
81 int len, num1, num2, have1, have2;
87 if (fgets(buf, sizeof(buf), stdin) == NULL)
95 endbuf = &buf[len - 1];
97 if (*endbuf != '\n') {
98 bb_error_msg("command line too long");
102 } while ((len != EOF) && (len != '\n'));
107 while ((endbuf > buf) && isblank(endbuf[-1]))
120 if ((curNum == 0) && (lastNum > 0)) {
122 curLine = lines.next;
125 if (!getNum(&cp, &have1, &num1))
134 if (!getNum(&cp, &have2, &num2))
159 deleteLines(num1, num2);
164 deleteLines(num1, num2);
168 if (*cp && !isblank(*cp)) {
169 bb_error_msg("bad file command");
178 printf("\"%s\"\n", fileName);
180 printf("No file name\n");
184 newname = strdup(cp);
186 if (newname == NULL) {
187 bb_error_msg("no memory for file name");
205 if ((*cp < 'a') || (*cp > 'a') || cp[1]) {
206 bb_error_msg("bad mark name");
210 marks[*cp - 'a'] = num2;
214 printLines(num1, num2, TRUE);
218 printLines(num1, num2, FALSE);
226 bb_error_msg("bad quit command");
233 printf("Really quit? ");
237 fgets(buf, sizeof(buf), stdin);
243 if ((*cp == 'y') || (*cp == 'Y'))
249 if (*cp && !isblank(*cp)) {
250 bb_error_msg("bad read command");
258 bb_error_msg("no file name");
265 if (readLines(cp, num1 + 1))
268 if (fileName == NULL)
269 fileName = strdup(cp);
274 subCommand(cp, num1, num2);
278 if (*cp && !isblank(*cp)) {
279 bb_error_msg("bad write command");
295 bb_error_msg("no file name specified");
299 writeLines(cp, num1, num2);
305 printLines(curNum-21, curNum, FALSE);
308 printLines(curNum-11, curNum+10, FALSE);
311 printLines(curNum, curNum+21, FALSE);
318 bb_error_msg("no arguments allowed");
322 printLines(curNum, curNum, FALSE);
326 if (setCurNum(curNum - 1))
327 printLines(curNum, curNum, FALSE);
332 printf("%d\n", num1);
337 printLines(num2, num2, FALSE);
341 if (setCurNum(curNum + 1))
342 printLines(curNum, curNum, FALSE);
347 bb_error_msg("unimplemented command");
355 * Do the substitute command.
356 * The current line is set to the last substitution done.
358 static void subCommand(const char * cmd, int num1, int num2)
360 char *cp, *oldStr, *newStr, buf[USERSIZE];
361 int delim, oldLen, newLen, deltaLen, offset;
363 int globalFlag, printFlag, didSub, needPrint;
365 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
366 bb_error_msg("bad line range for substitute");
376 * Copy the command so we can modify it.
381 if (isblank(*cp) || (*cp == '\0')) {
382 bb_error_msg("bad delimiter for substitute");
389 cp = strchr(cp, delim);
392 bb_error_msg("missing 2nd delimiter for substitute");
399 cp = strchr(cp, delim);
406 while (*cp) switch (*cp++) {
416 bb_error_msg("unknown option for substitute");
420 if (*oldStr == '\0') {
421 if (searchString[0] == '\0') {
422 bb_error_msg("no previous search string");
426 oldStr = searchString;
429 if (oldStr != searchString)
430 strcpy(searchString, oldStr);
437 oldLen = strlen(oldStr);
438 newLen = strlen(newStr);
439 deltaLen = newLen - oldLen;
443 while (num1 <= num2) {
444 offset = findString(lp, oldStr, oldLen, offset);
448 printLines(num1, num1, FALSE);
459 needPrint = printFlag;
464 * If the replacement string is the same size or shorter
465 * than the old string, then the substitution is easy.
468 memcpy(&lp->data[offset], newStr, newLen);
471 memcpy(&lp->data[offset + newLen],
472 &lp->data[offset + oldLen],
473 lp->len - offset - oldLen);
484 printLines(num1, num1, FALSE);
495 * The new string is larger, so allocate a new line
496 * structure and use that. Link it in in place of
497 * the old line structure.
499 nlp = (LINE *) malloc(sizeof(LINE) + lp->len + deltaLen);
502 bb_error_msg("cannot get memory for line");
506 nlp->len = lp->len + deltaLen;
508 memcpy(nlp->data, lp->data, offset);
510 memcpy(&nlp->data[offset], newStr, newLen);
512 memcpy(&nlp->data[offset + newLen],
513 &lp->data[offset + oldLen],
514 lp->len - offset - oldLen);
516 nlp->next = lp->next;
517 nlp->prev = lp->prev;
518 nlp->prev->next = nlp;
519 nlp->next->prev = nlp;
533 printLines(num1, num1, FALSE);
542 bb_error_msg("no substitutions found for \"%s\"", oldStr);
547 * Search a line for the specified string starting at the specified
548 * offset in the line. Returns the offset of the found string, or -1.
550 static int findString( const LINE * lp, const char * str, int len, int offset)
553 const char *cp, *ncp;
555 cp = &lp->data[offset];
556 left = lp->len - offset;
558 while (left >= len) {
559 ncp = memchr(cp, *str, left);
571 if (memcmp(cp, str, len) == 0)
572 return (cp - lp->data);
583 * Add lines which are typed in by the user.
584 * The lines are inserted just before the specified line number.
585 * The lines are terminated by a line containing a single dot (ugly!),
586 * or by an end of file.
588 static void addLines(int num)
591 char buf[USERSIZE + 1];
593 while (fgets(buf, sizeof(buf), stdin)) {
594 if ((buf[0] == '.') && (buf[1] == '\n') && (buf[2] == '\0'))
602 if (buf[len - 1] != '\n') {
603 bb_error_msg("line too long");
606 } while ((len != EOF) && (len != '\n'));
610 if (!insertLine(num++, buf, len))
617 * Parse a line number argument if it is present. This is a sum
618 * or difference of numbers, '.', '$', 'x, or a search string.
619 * Returns TRUE if successful (whether or not there was a number).
620 * Returns FALSE if there was a parsing error, with a message output.
621 * Whether there was a number is returned indirectly, as is the number.
622 * The character pointer which stopped the scan is also returned.
624 static int getNum(const char **retcp, int *retHaveNum, int *retNum)
627 char *endStr, str[USERSIZE];
628 int haveNum, value, num, sign;
655 if ((*cp < 'a') || (*cp > 'z')) {
656 bb_error_msg("bad mark name");
661 num = marks[*cp++ - 'a'];
666 endStr = strchr(str, '/');
670 cp += (endStr - str);
675 num = searchLines(str, curNum, lastNum);
686 *retHaveNum = haveNum;
694 num = num * 10 + *cp++ - '0';
718 *retHaveNum = haveNum;
727 * Initialize everything for editing.
729 static int initEdit(void)
733 bufSize = INITBUF_SIZE;
734 bufBase = malloc(bufSize);
736 if (bufBase == NULL) {
737 bb_error_msg("no memory for buffer");
752 searchString[0] = '\0';
754 for (i = 0; i < 26; i++)
764 static void termEdit(void)
779 searchString[0] = '\0';
782 deleteLines(1, lastNum);
791 * Read lines from a file at the specified line number.
792 * Returns TRUE if the file was successfully read.
794 static int readLines(const char * file, int num)
797 int len, lineCount, charCount;
800 if ((num < 1) || (num > lastNum + 1)) {
801 bb_error_msg("bad line for read");
818 printf("\"%s\", ", file);
822 cp = memchr(bufPtr, '\n', bufUsed);
825 len = (cp - bufPtr) + 1;
827 if (!insertLine(num, bufPtr, len)) {
841 if (bufPtr != bufBase) {
842 memcpy(bufBase, bufPtr, bufUsed);
843 bufPtr = bufBase + bufUsed;
846 if (bufUsed >= bufSize) {
847 len = (bufSize * 3) / 2;
848 cp = realloc(bufBase, len);
851 bb_error_msg("no memory for buffer");
857 bufPtr = bufBase + bufUsed;
861 cc = read(fd, bufPtr, bufSize - bufUsed);
874 if (!insertLine(num, bufPtr, bufUsed)) {
880 charCount += bufUsed;
885 printf("%d lines%s, %d chars\n", lineCount,
886 (bufUsed ? " (incomplete)" : ""), charCount);
893 * Write the specified lines out to the specified file.
894 * Returns TRUE if successful, or FALSE on an error with a message output.
896 static int writeLines(const char * file, int num1, int num2)
899 int fd, lineCount, charCount;
901 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
902 bb_error_msg("bad line range for write");
909 fd = creat(file, 0666);
916 printf("\"%s\", ", file);
926 while (num1++ <= num2) {
927 if (write(fd, lp->data, lp->len) != lp->len) {
933 charCount += lp->len;
943 printf("%d lines, %d chars\n", lineCount, charCount);
949 * Print lines in a specified range.
950 * The last line printed becomes the current line.
951 * If expandFlag is TRUE, then the line is printed specially to
952 * show magic characters.
954 static int printLines(int num1, int num2, int expandFlag)
960 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
961 bb_error_msg("bad line range for print");
970 while (num1 <= num2) {
972 write(1, lp->data, lp->len);
980 * Show control characters and characters with the
981 * high bit set specially.
986 if ((count > 0) && (cp[count - 1] == '\n'))
989 while (count-- > 0) {
1010 fputs("$\n", stdout);
1021 * Insert a new line with the specified text.
1022 * The line is inserted so as to become the specified line,
1023 * thus pushing any existing and further lines down one.
1024 * The inserted line is also set to become the current line.
1025 * Returns TRUE if successful.
1027 static int insertLine(int num, const char * data, int len)
1031 if ((num < 1) || (num > lastNum + 1)) {
1032 bb_error_msg("inserting at bad line number");
1036 newLp = malloc(sizeof(LINE) + len - 1);
1038 if (newLp == NULL) {
1039 bb_error_msg("failed to allocate memory for line");
1043 memcpy(newLp->data, data, len);
1052 free((char *) newLp);
1058 newLp->prev = lp->prev;
1059 lp->prev->next = newLp;
1064 return setCurNum(num);
1069 * Delete lines from the given range.
1071 static int deleteLines(int num1, int num2)
1073 LINE *lp, *nlp, *plp;
1076 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
1077 bb_error_msg("bad line numbers for delete");
1081 lp = findLine(num1);
1086 if ((curNum >= num1) && (curNum <= num2)) {
1088 setCurNum(num2 + 1);
1090 setCurNum(num1 - 1);
1095 count = num2 - num1 + 1;
1102 while (count-- > 0) {
1121 * Search for a line which contains the specified string.
1122 * If the string is NULL, then the previously searched for string
1123 * is used. The currently searched for string is saved for future use.
1124 * Returns the line number which matches, or 0 if there was no match
1125 * with an error printed.
1127 static int searchLines(const char *str, int num1, int num2)
1132 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
1133 bb_error_msg("bad line numbers for search");
1138 if (searchString[0] == '\0') {
1139 bb_error_msg("no previous search string");
1146 if (str != searchString)
1147 strcpy(searchString, str);
1151 lp = findLine(num1);
1156 while (num1 <= num2) {
1157 if (findString(lp, str, len, 0) >= 0)
1164 bb_error_msg("cannot find string \"%s\"", str);
1170 * Return a pointer to the specified line number.
1172 static LINE *findLine(int num)
1177 if ((num < 1) || (num > lastNum)) {
1178 bb_error_msg("line number %d does not exist", num);
1184 curLine = lines.next;
1193 if (num < (curNum / 2)) {
1197 else if (num > ((curNum + lastNum) / 2)) {
1202 while (lnum < num) {
1207 while (lnum > num) {
1216 * Set the current line number.
1217 * Returns TRUE if successful.
1219 static int setCurNum(int num)