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)
17 #include <sys/param.h>
20 #define USERSIZE 1024 /* max line length typed in by user */
21 #define INITBUF_SIZE 1024 /* initial buffer size */
29 static LINE lines, *curLine;
30 static int curNum, lastNum, marks[26], dirty;
31 static char *bufBase, *bufPtr, *fileName, searchString[USERSIZE];
32 static int bufUsed, bufSize;
34 static void doCommands(void);
35 static void subCommand(const char *cmd, int num1, int num2);
36 static int getNum(const char **retcp, int *retHaveNum, int *retNum);
37 static int setCurNum(int num);
38 static int initEdit(void);
39 static void termEdit(void);
40 static void addLines(int num);
41 static int insertLine(int num, const char *data, int len);
42 static int deleteLines(int num1, int num2);
43 static int printLines(int num1, int num2, int expandFlag);
44 static int writeLines(const char *file, int num1, int num2);
45 static int readLines(const char *file, int num);
46 static int searchLines(const char *str, int num1, int num2);
47 static LINE *findLine(int num);
49 static int findString(const LINE *lp, const char * str, int len, int offset);
51 int ed_main(int argc, char **argv)
57 fileName = strdup(argv[1]);
59 if (fileName == NULL) {
60 bb_error_msg("No memory");
65 if (!readLines(fileName, 1)) {
83 * Read commands until we are told to stop.
85 static void doCommands(void)
88 char *endbuf, *newname, buf[USERSIZE];
89 int len, num1, num2, have1, have2;
96 if (fgets(buf, sizeof(buf), stdin) == NULL)
104 endbuf = &buf[len - 1];
108 bb_error_msg("Command line too long");
114 while ((len != EOF) && (len != '\n'));
119 while ((endbuf > buf) && isblank(endbuf[-1]))
132 if ((curNum == 0) && (lastNum > 0))
135 curLine = lines.next;
138 if (!getNum(&cp, &have1, &num1))
148 if (!getNum(&cp, &have2, &num2))
174 deleteLines(num1, num2);
179 deleteLines(num1, num2);
183 if (*cp && !isblank(*cp))
185 bb_error_msg("Bad file command");
195 printf("\"%s\"\n", fileName);
197 printf("No file name\n");
202 newname = strdup(cp);
206 bb_error_msg("No memory for file name");
224 if ((*cp < 'a') || (*cp > 'a') || cp[1])
226 bb_error_msg("Bad mark name");
230 marks[*cp - 'a'] = num2;
234 printLines(num1, num2, TRUE);
238 printLines(num1, num2, FALSE);
247 bb_error_msg("Bad quit command");
254 printf("Really quit? ");
258 fgets(buf, sizeof(buf), stdin);
264 if ((*cp == 'y') || (*cp == 'Y'))
270 if (*cp && !isblank(*cp))
272 bb_error_msg("Bad read command");
281 bb_error_msg("No file name");
288 if (readLines(cp, num1 + 1))
291 if (fileName == NULL)
292 fileName = strdup(cp);
297 subCommand(cp, num1, num2);
301 if (*cp && !isblank(*cp))
303 bb_error_msg("Bad write command");
320 bb_error_msg("No file name specified");
324 writeLines(cp, num1, num2);
331 printLines(curNum-21, curNum, FALSE);
334 printLines(curNum-11, curNum+10, FALSE);
337 printLines(curNum, curNum+21, FALSE);
345 bb_error_msg("No arguments allowed");
349 printLines(curNum, curNum, FALSE);
353 if (setCurNum(curNum - 1))
354 printLines(curNum, curNum, FALSE);
359 printf("%d\n", num1);
365 printLines(num2, num2, FALSE);
369 if (setCurNum(curNum + 1))
370 printLines(curNum, curNum, FALSE);
375 bb_error_msg("Unimplemented command");
383 * Do the substitute command.
384 * The current line is set to the last substitution done.
386 static void subCommand(const char * cmd, int num1, int num2)
388 char *cp, *oldStr, *newStr, buf[USERSIZE];
389 int delim, oldLen, newLen, deltaLen, offset;
391 int globalFlag, printFlag, didSub, needPrint;
393 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2))
395 bb_error_msg("Bad line range for substitute");
406 * Copy the command so we can modify it.
411 if (isblank(*cp) || (*cp == '\0'))
413 bb_error_msg("Bad delimiter for substitute");
421 cp = strchr(cp, delim);
425 bb_error_msg("Missing 2nd delimiter for substitute");
433 cp = strchr(cp, delim);
440 while (*cp) switch (*cp++)
451 bb_error_msg("Unknown option for substitute");
458 if (searchString[0] == '\0')
460 bb_error_msg("No previous search string");
465 oldStr = searchString;
468 if (oldStr != searchString)
469 strcpy(searchString, oldStr);
476 oldLen = strlen(oldStr);
477 newLen = strlen(newStr);
478 deltaLen = newLen - oldLen;
484 offset = findString(lp, oldStr, oldLen, offset);
490 printLines(num1, num1, FALSE);
501 needPrint = printFlag;
506 * If the replacement string is the same size or shorter
507 * than the old string, then the substitution is easy.
511 memcpy(&lp->data[offset], newStr, newLen);
515 memcpy(&lp->data[offset + newLen],
516 &lp->data[offset + oldLen],
517 lp->len - offset - oldLen);
529 printLines(num1, num1, FALSE);
540 * The new string is larger, so allocate a new line
541 * structure and use that. Link it in in place of
542 * the old line structure.
544 nlp = (LINE *) malloc(sizeof(LINE) + lp->len + deltaLen);
548 bb_error_msg("Cannot get memory for line");
553 nlp->len = lp->len + deltaLen;
555 memcpy(nlp->data, lp->data, offset);
557 memcpy(&nlp->data[offset], newStr, newLen);
559 memcpy(&nlp->data[offset + newLen],
560 &lp->data[offset + oldLen],
561 lp->len - offset - oldLen);
563 nlp->next = lp->next;
564 nlp->prev = lp->prev;
565 nlp->prev->next = nlp;
566 nlp->next->prev = nlp;
581 printLines(num1, num1, FALSE);
590 bb_error_msg("No substitutions found for \"%s\"", oldStr);
595 * Search a line for the specified string starting at the specified
596 * offset in the line. Returns the offset of the found string, or -1.
598 static int findString( const LINE * lp, const char * str, int len, int offset)
601 const char *cp, *ncp;
603 cp = &lp->data[offset];
604 left = lp->len - offset;
608 ncp = memchr(cp, *str, left);
620 if (memcmp(cp, str, len) == 0)
621 return (cp - lp->data);
632 * Add lines which are typed in by the user.
633 * The lines are inserted just before the specified line number.
634 * The lines are terminated by a line containing a single dot (ugly!),
635 * or by an end of file.
637 static void addLines(int num)
640 char buf[USERSIZE + 1];
642 while (fgets(buf, sizeof(buf), stdin))
644 if ((buf[0] == '.') && (buf[1] == '\n') && (buf[2] == '\0'))
652 if (buf[len - 1] != '\n')
654 bb_error_msg("Line too long");
660 while ((len != EOF) && (len != '\n'));
665 if (!insertLine(num++, buf, len))
672 * Parse a line number argument if it is present. This is a sum
673 * or difference of numbers, '.', '$', 'x, or a search string.
674 * Returns TRUE if successful (whether or not there was a number).
675 * Returns FALSE if there was a parsing error, with a message output.
676 * Whether there was a number is returned indirectly, as is the number.
677 * The character pointer which stopped the scan is also returned.
679 static int getNum(const char **retcp, int *retHaveNum, int *retNum)
682 char *endStr, str[USERSIZE];
683 int haveNum, value, num, sign;
712 if ((*cp < 'a') || (*cp > 'z'))
714 bb_error_msg("Bad mark name");
720 num = marks[*cp++ - 'a'];
725 endStr = strchr(str, '/');
730 cp += (endStr - str);
735 num = searchLines(str, curNum, lastNum);
747 *retHaveNum = haveNum;
756 num = num * 10 + *cp++ - '0';
781 *retHaveNum = haveNum;
791 * Initialize everything for editing.
793 static int initEdit(void)
797 bufSize = INITBUF_SIZE;
798 bufBase = malloc(bufSize);
802 bb_error_msg("No memory for buffer");
818 searchString[0] = '\0';
820 for (i = 0; i < 26; i++)
830 static void termEdit(void)
845 searchString[0] = '\0';
848 deleteLines(1, lastNum);
857 * Read lines from a file at the specified line number.
858 * Returns TRUE if the file was successfully read.
860 static int readLines(const char * file, int num)
863 int len, lineCount, charCount;
866 if ((num < 1) || (num > lastNum + 1))
868 bb_error_msg("Bad line for read");
888 printf("\"%s\", ", file);
893 cp = memchr(bufPtr, '\n', bufUsed);
897 len = (cp - bufPtr) + 1;
899 if (!insertLine(num, bufPtr, len))
915 if (bufPtr != bufBase)
917 memcpy(bufBase, bufPtr, bufUsed);
918 bufPtr = bufBase + bufUsed;
921 if (bufUsed >= bufSize)
923 len = (bufSize * 3) / 2;
924 cp = realloc(bufBase, len);
928 bb_error_msg("No memory for buffer");
935 bufPtr = bufBase + bufUsed;
939 cc = read(fd, bufPtr, bufSize - bufUsed);
956 if (!insertLine(num, bufPtr, bufUsed))
964 charCount += bufUsed;
969 printf("%d lines%s, %d chars\n", lineCount,
970 (bufUsed ? " (incomplete)" : ""), charCount);
977 * Write the specified lines out to the specified file.
978 * Returns TRUE if successful, or FALSE on an error with a message output.
980 static int writeLines(const char * file, int num1, int num2)
983 int fd, lineCount, charCount;
985 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2))
987 bb_error_msg("Bad line range for write");
995 fd = creat(file, 0666);
1003 printf("\"%s\", ", file);
1006 lp = findLine(num1);
1015 while (num1++ <= num2)
1017 if (write(fd, lp->data, lp->len) != lp->len)
1025 charCount += lp->len;
1037 printf("%d lines, %d chars\n", lineCount, charCount);
1044 * Print lines in a specified range.
1045 * The last line printed becomes the current line.
1046 * If expandFlag is TRUE, then the line is printed specially to
1047 * show magic characters.
1049 static int printLines(int num1, int num2, int expandFlag)
1055 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2))
1057 bb_error_msg("Bad line range for print");
1062 lp = findLine(num1);
1067 while (num1 <= num2)
1071 write(1, lp->data, lp->len);
1079 * Show control characters and characters with the
1080 * high bit set specially.
1085 if ((count > 0) && (cp[count - 1] == '\n'))
1094 fputs("M-", stdout);
1113 fputs("$\n", stdout);
1124 * Insert a new line with the specified text.
1125 * The line is inserted so as to become the specified line,
1126 * thus pushing any existing and further lines down one.
1127 * The inserted line is also set to become the current line.
1128 * Returns TRUE if successful.
1130 static int insertLine(int num, const char * data, int len)
1134 if ((num < 1) || (num > lastNum + 1))
1136 bb_error_msg("Inserting at bad line number");
1141 newLp = (LINE *) malloc(sizeof(LINE) + len - 1);
1145 bb_error_msg("Failed to allocate memory for line");
1150 memcpy(newLp->data, data, len);
1161 free((char *) newLp);
1168 newLp->prev = lp->prev;
1169 lp->prev->next = newLp;
1175 return setCurNum(num);
1180 * Delete lines from the given range.
1182 static int deleteLines(int num1, int num2)
1184 LINE *lp, *nlp, *plp;
1187 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2))
1189 bb_error_msg("Bad line numbers for delete");
1194 lp = findLine(num1);
1199 if ((curNum >= num1) && (curNum <= num2))
1202 setCurNum(num2 + 1);
1204 setCurNum(num1 - 1);
1209 count = num2 - num1 + 1;
1236 * Search for a line which contains the specified string.
1237 * If the string is NULL, then the previously searched for string
1238 * is used. The currently searched for string is saved for future use.
1239 * Returns the line number which matches, or 0 if there was no match
1240 * with an error printed.
1242 static int searchLines(const char *str, int num1, int num2)
1247 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2))
1249 bb_error_msg("Bad line numbers for search");
1256 if (searchString[0] == '\0')
1258 bb_error_msg("No previous search string");
1266 if (str != searchString)
1267 strcpy(searchString, str);
1271 lp = findLine(num1);
1276 while (num1 <= num2)
1278 if (findString(lp, str, len, 0) >= 0)
1285 bb_error_msg("Cannot find string \"%s\"", str);
1292 * Return a pointer to the specified line number.
1294 static LINE *findLine(int num)
1299 if ((num < 1) || (num > lastNum))
1301 bb_error_msg("Line number %d does not exist", num);
1309 curLine = lines.next;
1318 if (num < (curNum / 2))
1323 else if (num > ((curNum + lastNum) / 2))
1346 * Set the current line number.
1347 * Returns TRUE if successful.
1349 static int setCurNum(int num)