2005-04-23 07:20:55 +05:30
|
|
|
Index: editors/Makefile.in
|
|
|
|
===================================================================
|
|
|
|
--- editors/Makefile.in (revision 10144)
|
|
|
|
+++ editors/Makefile.in (working copy)
|
|
|
|
@@ -24,8 +24,9 @@
|
|
|
|
srcdir=$(top_srcdir)/editors
|
|
|
|
|
|
|
|
EDITOR-y:=
|
|
|
|
-EDITOR-$(CONFIG_AWK) += awk.o
|
|
|
|
-EDITOR-$(CONFIG_PATCH) += patch.o
|
|
|
|
+EDITOR-$(CONFIG_AWK) += awk.o
|
|
|
|
+EDITOR-$(CONFIG_ED) += ed.o
|
|
|
|
+EDITOR-$(CONFIG_PATCH) += patch.o
|
|
|
|
EDITOR-$(CONFIG_SED) += sed.o
|
|
|
|
EDITOR-$(CONFIG_VI) += vi.o
|
|
|
|
EDITOR_SRC:= $(EDITOR-y)
|
|
|
|
Index: editors/Config.in
|
|
|
|
===================================================================
|
|
|
|
--- editors/Config.in (revision 10144)
|
|
|
|
+++ editors/Config.in (working copy)
|
|
|
|
@@ -20,6 +20,12 @@
|
|
|
|
Enable math functions of the Awk programming language.
|
|
|
|
NOTE: This will require libm to be present for linking.
|
|
|
|
|
|
|
|
+config CONFIG_ED
|
|
|
|
+ bool "ed"
|
|
|
|
+ default n
|
|
|
|
+ help
|
|
|
|
+ ed
|
|
|
|
+
|
|
|
|
config CONFIG_PATCH
|
|
|
|
bool "patch"
|
|
|
|
default n
|
|
|
|
Index: include/usage.h
|
|
|
|
===================================================================
|
|
|
|
--- include/usage.h (revision 10151)
|
|
|
|
+++ include/usage.h (working copy)
|
|
|
|
@@ -556,6 +561,9 @@
|
|
|
|
"$ echo \"Erik\\nis\\ncool\"\n" \
|
|
|
|
"Erik\\nis\\ncool\n")
|
|
|
|
|
|
|
|
+#define ed_trivial_usage ""
|
|
|
|
+#define ed_full_usage ""
|
|
|
|
+
|
|
|
|
#define env_trivial_usage \
|
|
|
|
"[-iu] [-] [name=value]... [command]"
|
|
|
|
#define env_full_usage \
|
|
|
|
Index: include/applets.h
|
|
|
|
===================================================================
|
|
|
|
--- include/applets.h (revision 10151)
|
|
|
|
+++ include/applets.h (working copy)
|
|
|
|
@@ -179,6 +179,9 @@
|
|
|
|
#ifdef CONFIG_ECHO
|
|
|
|
APPLET(echo, echo_main, _BB_DIR_BIN, _BB_SUID_NEVER)
|
|
|
|
#endif
|
|
|
|
+#ifdef CONFIG_ED
|
|
|
|
+ APPLET(ed, ed_main, _BB_DIR_BIN, _BB_SUID_NEVER)
|
|
|
|
+#endif
|
|
|
|
#if defined(CONFIG_FEATURE_GREP_EGREP_ALIAS)
|
|
|
|
APPLET_NOUSAGE("egrep", grep_main, _BB_DIR_BIN, _BB_SUID_NEVER)
|
|
|
|
#endif
|
2005-04-24 10:48:00 +05:30
|
|
|
--- /dev/null 2005-04-24 01:00:01.350003056 -0400
|
2005-04-24 11:09:52 +05:30
|
|
|
+++ ed.c 2005-04-24 01:38:51.000000000 -0400
|
|
|
|
@@ -0,0 +1,1425 @@
|
2005-04-23 07:20:55 +05:30
|
|
|
+/*
|
2005-04-24 10:48:00 +05:30
|
|
|
+ * Copyright (c) 2002 by David I. Bell
|
|
|
|
+ * Permission is granted to use, distribute, or modify this source,
|
|
|
|
+ * provided that this copyright notice remains intact.
|
2005-04-23 07:20:55 +05:30
|
|
|
+ *
|
2005-04-24 10:48:00 +05:30
|
|
|
+ * The "ed" built-in command (much simplified)
|
2005-04-23 07:20:55 +05:30
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+#include <stdio.h>
|
2005-04-24 10:48:00 +05:30
|
|
|
+#include <stdlib.h>
|
|
|
|
+#include <unistd.h>
|
|
|
|
+#include <fcntl.h>
|
2005-04-23 07:20:55 +05:30
|
|
|
+#include <string.h>
|
2005-04-24 10:48:00 +05:30
|
|
|
+#include <memory.h>
|
|
|
|
+#include <time.h>
|
2005-04-23 07:20:55 +05:30
|
|
|
+#include <ctype.h>
|
2005-04-24 10:48:00 +05:30
|
|
|
+#include <sys/param.h>
|
|
|
|
+#include <malloc.h>
|
2005-04-24 11:09:52 +05:30
|
|
|
+#include "busybox.h"
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+#define USERSIZE 1024 /* max line length typed in by user */
|
|
|
|
+#define INITBUF_SIZE 1024 /* initial buffer size */
|
|
|
|
+
|
|
|
|
+typedef int BOOL;
|
|
|
|
+typedef int NUM;
|
|
|
|
+typedef int LEN;
|
|
|
|
+
|
2005-04-24 11:09:52 +05:30
|
|
|
+typedef struct LINE LINE;
|
|
|
|
+struct LINE {
|
|
|
|
+ LINE *next;
|
|
|
|
+ LINE *prev;
|
2005-04-24 10:48:00 +05:30
|
|
|
+ LEN len;
|
2005-04-24 11:09:52 +05:30
|
|
|
+ char data[1];
|
2005-04-23 07:20:55 +05:30
|
|
|
+};
|
|
|
|
+
|
2005-04-24 11:09:52 +05:30
|
|
|
+static LINE lines;
|
|
|
|
+static LINE *curLine;
|
|
|
|
+static NUM curNum;
|
|
|
|
+static NUM lastNum;
|
|
|
|
+static NUM marks[26];
|
|
|
|
+static BOOL dirty;
|
|
|
|
+static char *fileName;
|
|
|
|
+static char searchString[USERSIZE];
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 11:09:52 +05:30
|
|
|
+static char *bufBase;
|
|
|
|
+static char *bufPtr;
|
|
|
|
+static LEN bufUsed;
|
|
|
|
+static LEN bufSize;
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+static void doCommands(void);
|
|
|
|
+static void subCommand(const char * cmd, NUM num1, NUM num2);
|
|
|
|
+static BOOL getNum(const char ** retcp, BOOL * retHaveNum, NUM * retNum);
|
|
|
|
+static BOOL setCurNum(NUM num);
|
|
|
|
+static BOOL initEdit(void);
|
|
|
|
+static void termEdit(void);
|
|
|
|
+static void addLines(NUM num);
|
|
|
|
+static BOOL insertLine(NUM num, const char * data, LEN len);
|
|
|
|
+static BOOL deleteLines(NUM num1, NUM num2);
|
|
|
|
+static BOOL printLines(NUM num1, NUM num2, BOOL expandFlag);
|
|
|
|
+static BOOL writeLines(const char * file, NUM num1, NUM num2);
|
|
|
|
+static BOOL readLines(const char * file, NUM num);
|
|
|
|
+static NUM searchLines(const char * str, NUM num1, NUM num2);
|
|
|
|
+static LINE * findLine(NUM num);
|
|
|
|
+
|
2005-04-24 11:09:52 +05:30
|
|
|
+static LEN findString(const LINE * lp, const char * str, LEN len, LEN offset);
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
2005-04-24 11:09:52 +05:30
|
|
|
+int ed_main(int argc, char **argv)
|
2005-04-23 07:20:55 +05:30
|
|
|
+{
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (!initEdit())
|
2005-04-24 11:09:52 +05:30
|
|
|
+ return EXIT_FAILURE;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 11:09:52 +05:30
|
|
|
+ if (argc > 1) {
|
2005-04-24 10:48:00 +05:30
|
|
|
+ fileName = strdup(argv[1]);
|
|
|
|
+
|
2005-04-24 11:09:52 +05:30
|
|
|
+ if (fileName == NULL) {
|
|
|
|
+ bb_error_msg("No memory");
|
2005-04-24 10:48:00 +05:30
|
|
|
+ termEdit();
|
2005-04-24 11:09:52 +05:30
|
|
|
+ return EXIT_SUCCESS;
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
|
|
|
+
|
2005-04-24 11:09:52 +05:30
|
|
|
+ if (!readLines(fileName, 1)) {
|
2005-04-24 10:48:00 +05:30
|
|
|
+ termEdit();
|
2005-04-24 11:09:52 +05:30
|
|
|
+ return EXIT_SUCCESS;
|
2005-04-24 10:48:00 +05:30
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (lastNum)
|
|
|
|
+ setCurNum(1);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ dirty = FALSE;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ doCommands();
|
|
|
|
+
|
|
|
|
+ termEdit();
|
2005-04-24 11:09:52 +05:30
|
|
|
+ return EXIT_SUCCESS;
|
2005-04-23 07:20:55 +05:30
|
|
|
+}
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+/*
|
|
|
|
+ * Read commands until we are told to stop.
|
|
|
|
+ */
|
2005-04-24 11:09:52 +05:30
|
|
|
+static void doCommands(void)
|
2005-04-23 07:20:55 +05:30
|
|
|
+{
|
2005-04-24 10:48:00 +05:30
|
|
|
+ const char * cp;
|
|
|
|
+ char * endbuf;
|
|
|
|
+ char * newname;
|
|
|
|
+ int len;
|
|
|
|
+ NUM num1;
|
|
|
|
+ NUM num2;
|
|
|
|
+ BOOL have1;
|
|
|
|
+ BOOL have2;
|
|
|
|
+ char buf[USERSIZE];
|
|
|
|
+
|
|
|
|
+ while (TRUE)
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 10:48:00 +05:30
|
|
|
+ printf(": ");
|
|
|
|
+ fflush(stdout);
|
|
|
|
+
|
|
|
|
+ if (fgets(buf, sizeof(buf), stdin) == NULL)
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ len = strlen(buf);
|
|
|
|
+
|
|
|
|
+ if (len == 0)
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ endbuf = &buf[len - 1];
|
|
|
|
+
|
|
|
|
+ if (*endbuf != '\n')
|
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("Command line too long");
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+ do
|
|
|
|
+ {
|
|
|
|
+ len = fgetc(stdin);
|
|
|
|
+ }
|
|
|
|
+ while ((len != EOF) && (len != '\n'));
|
|
|
|
+
|
|
|
|
+ continue;
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
2005-04-24 11:09:52 +05:30
|
|
|
+ while ((endbuf > buf) && isblank(endbuf[-1]))
|
2005-04-24 10:48:00 +05:30
|
|
|
+ endbuf--;
|
|
|
|
+
|
|
|
|
+ *endbuf = '\0';
|
|
|
|
+
|
|
|
|
+ cp = buf;
|
|
|
|
+
|
2005-04-24 11:09:52 +05:30
|
|
|
+ while (isblank(*cp))
|
2005-04-24 10:48:00 +05:30
|
|
|
+ cp++;
|
|
|
|
+
|
|
|
|
+ have1 = FALSE;
|
|
|
|
+ have2 = FALSE;
|
|
|
|
+
|
|
|
|
+ if ((curNum == 0) && (lastNum > 0))
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 10:48:00 +05:30
|
|
|
+ curNum = 1;
|
|
|
|
+ curLine = lines.next;
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (!getNum(&cp, &have1, &num1))
|
|
|
|
+ continue;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 11:09:52 +05:30
|
|
|
+ while (isblank(*cp))
|
2005-04-24 10:48:00 +05:30
|
|
|
+ cp++;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (*cp == ',')
|
|
|
|
+ {
|
|
|
|
+ cp++;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (!getNum(&cp, &have2, &num2))
|
|
|
|
+ continue;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (!have1)
|
|
|
|
+ num1 = 1;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (!have2)
|
|
|
|
+ num2 = lastNum;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ have1 = TRUE;
|
|
|
|
+ have2 = TRUE;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (!have1)
|
|
|
|
+ num1 = curNum;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (!have2)
|
|
|
|
+ num2 = num1;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ switch (*cp++)
|
|
|
|
+ {
|
|
|
|
+ case 'a':
|
|
|
|
+ addLines(num1 + 1);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case 'c':
|
|
|
|
+ deleteLines(num1, num2);
|
|
|
|
+ addLines(num1);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case 'd':
|
|
|
|
+ deleteLines(num1, num2);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case 'f':
|
2005-04-24 11:09:52 +05:30
|
|
|
+ if (*cp && !isblank(*cp))
|
2005-04-24 10:48:00 +05:30
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("Bad file command");
|
2005-04-24 10:48:00 +05:30
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
2005-04-24 11:09:52 +05:30
|
|
|
+ while (isblank(*cp))
|
2005-04-24 10:48:00 +05:30
|
|
|
+ cp++;
|
|
|
|
+
|
|
|
|
+ if (*cp == '\0')
|
|
|
|
+ {
|
|
|
|
+ if (fileName)
|
|
|
|
+ printf("\"%s\"\n", fileName);
|
|
|
|
+ else
|
|
|
|
+ printf("No file name\n");
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ newname = strdup(cp);
|
|
|
|
+
|
|
|
|
+ if (newname == NULL)
|
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("No memory for file name");
|
2005-04-24 10:48:00 +05:30
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (fileName)
|
|
|
|
+ free(fileName);
|
|
|
|
+
|
|
|
|
+ fileName = newname;
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case 'i':
|
|
|
|
+ addLines(num1);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case 'k':
|
2005-04-24 11:09:52 +05:30
|
|
|
+ while (isblank(*cp))
|
2005-04-24 10:48:00 +05:30
|
|
|
+ cp++;
|
|
|
|
+
|
|
|
|
+ if ((*cp < 'a') || (*cp > 'a') || cp[1])
|
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("Bad mark name");
|
2005-04-24 10:48:00 +05:30
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ marks[*cp - 'a'] = num2;
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case 'l':
|
|
|
|
+ printLines(num1, num2, TRUE);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case 'p':
|
|
|
|
+ printLines(num1, num2, FALSE);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case 'q':
|
2005-04-24 11:09:52 +05:30
|
|
|
+ while (isblank(*cp))
|
2005-04-24 10:48:00 +05:30
|
|
|
+ cp++;
|
|
|
|
+
|
|
|
|
+ if (have1 || *cp)
|
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("Bad quit command");
|
2005-04-24 10:48:00 +05:30
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!dirty)
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ printf("Really quit? ");
|
|
|
|
+ fflush(stdout);
|
|
|
|
+
|
|
|
|
+ buf[0] = '\0';
|
|
|
|
+ fgets(buf, sizeof(buf), stdin);
|
|
|
|
+ cp = buf;
|
|
|
|
+
|
2005-04-24 11:09:52 +05:30
|
|
|
+ while (isblank(*cp))
|
2005-04-24 10:48:00 +05:30
|
|
|
+ cp++;
|
|
|
|
+
|
|
|
|
+ if ((*cp == 'y') || (*cp == 'Y'))
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case 'r':
|
2005-04-24 11:09:52 +05:30
|
|
|
+ if (*cp && !isblank(*cp))
|
2005-04-24 10:48:00 +05:30
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("Bad read command");
|
2005-04-24 10:48:00 +05:30
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
2005-04-24 11:09:52 +05:30
|
|
|
+ while (isblank(*cp))
|
2005-04-24 10:48:00 +05:30
|
|
|
+ cp++;
|
|
|
|
+
|
|
|
|
+ if (*cp == '\0')
|
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("No file name");
|
2005-04-24 10:48:00 +05:30
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!have1)
|
|
|
|
+ num1 = lastNum;
|
|
|
|
+
|
|
|
|
+ if (readLines(cp, num1 + 1))
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ if (fileName == NULL)
|
|
|
|
+ fileName = strdup(cp);
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case 's':
|
|
|
|
+ subCommand(cp, num1, num2);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case 'w':
|
2005-04-24 11:09:52 +05:30
|
|
|
+ if (*cp && !isblank(*cp))
|
2005-04-24 10:48:00 +05:30
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("Bad write command");
|
2005-04-24 10:48:00 +05:30
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
2005-04-24 11:09:52 +05:30
|
|
|
+ while (isblank(*cp))
|
2005-04-24 10:48:00 +05:30
|
|
|
+ cp++;
|
|
|
|
+
|
|
|
|
+ if (!have1) {
|
|
|
|
+ num1 = 1;
|
|
|
|
+ num2 = lastNum;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (*cp == '\0')
|
|
|
|
+ cp = fileName;
|
|
|
|
+
|
|
|
|
+ if (cp == NULL)
|
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("No file name specified");
|
2005-04-24 10:48:00 +05:30
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ writeLines(cp, num1, num2);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case 'z':
|
|
|
|
+ switch (*cp)
|
|
|
|
+ {
|
|
|
|
+ case '-':
|
|
|
|
+ printLines(curNum-21, curNum, FALSE);
|
|
|
|
+ break;
|
|
|
|
+ case '.':
|
|
|
|
+ printLines(curNum-11, curNum+10, FALSE);
|
|
|
|
+ break;
|
|
|
|
+ default:
|
|
|
|
+ printLines(curNum, curNum+21, FALSE);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case '.':
|
|
|
|
+ if (have1)
|
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("No arguments allowed");
|
2005-04-24 10:48:00 +05:30
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ printLines(curNum, curNum, FALSE);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case '-':
|
|
|
|
+ if (setCurNum(curNum - 1))
|
|
|
|
+ printLines(curNum, curNum, FALSE);
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case '=':
|
|
|
|
+ printf("%d\n", num1);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case '\0':
|
|
|
|
+ if (have1)
|
|
|
|
+ {
|
|
|
|
+ printLines(num2, num2, FALSE);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (setCurNum(curNum + 1))
|
|
|
|
+ printLines(curNum, curNum, FALSE);
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ default:
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("Unimplemented command");
|
2005-04-24 10:48:00 +05:30
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+/*
|
|
|
|
+ * Do the substitute command.
|
|
|
|
+ * The current line is set to the last substitution done.
|
|
|
|
+ */
|
|
|
|
+static void
|
|
|
|
+subCommand(const char * cmd, NUM num1, NUM num2)
|
2005-04-23 07:20:55 +05:30
|
|
|
+{
|
2005-04-24 10:48:00 +05:30
|
|
|
+ int delim;
|
|
|
|
+ char * cp;
|
|
|
|
+ char * oldStr;
|
|
|
|
+ char * newStr;
|
|
|
|
+ LEN oldLen;
|
|
|
|
+ LEN newLen;
|
|
|
|
+ LEN deltaLen;
|
|
|
|
+ LEN offset;
|
|
|
|
+ LINE * lp;
|
|
|
|
+ LINE * nlp;
|
|
|
|
+ BOOL globalFlag;
|
|
|
|
+ BOOL printFlag;
|
|
|
|
+ BOOL didSub;
|
|
|
|
+ BOOL needPrint;
|
|
|
|
+ char buf[USERSIZE];
|
|
|
|
+
|
|
|
|
+ if ((num1 < 1) || (num2 > lastNum) || (num1 > num2))
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("Bad line range for substitute");
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+ return;
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+ globalFlag = FALSE;
|
|
|
|
+ printFlag = FALSE;
|
|
|
|
+ didSub = FALSE;
|
|
|
|
+ needPrint = FALSE;
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Copy the command so we can modify it.
|
|
|
|
+ */
|
|
|
|
+ strcpy(buf, cmd);
|
|
|
|
+ cp = buf;
|
|
|
|
+
|
2005-04-24 11:09:52 +05:30
|
|
|
+ if (isblank(*cp) || (*cp == '\0'))
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("Bad delimiter for substitute");
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+ return;
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+ delim = *cp++;
|
|
|
|
+ oldStr = cp;
|
|
|
|
+
|
|
|
|
+ cp = strchr(cp, delim);
|
|
|
|
+
|
|
|
|
+ if (cp == NULL)
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("Missing 2nd delimiter for substitute");
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+ return;
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ *cp++ = '\0';
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ newStr = cp;
|
|
|
|
+ cp = strchr(cp, delim);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (cp)
|
|
|
|
+ *cp++ = '\0';
|
|
|
|
+ else
|
|
|
|
+ cp = "";
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ while (*cp) switch (*cp++)
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 10:48:00 +05:30
|
|
|
+ case 'g':
|
|
|
|
+ globalFlag = TRUE;
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case 'p':
|
|
|
|
+ printFlag = TRUE;
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ default:
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("Unknown option for substitute");
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+ return;
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+ if (*oldStr == '\0')
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (searchString[0] == '\0')
|
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("No previous search string");
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ return;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ oldStr = searchString;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (oldStr != searchString)
|
|
|
|
+ strcpy(searchString, oldStr);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ lp = findLine(num1);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (lp == NULL)
|
|
|
|
+ return;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ oldLen = strlen(oldStr);
|
|
|
|
+ newLen = strlen(newStr);
|
|
|
|
+ deltaLen = newLen - oldLen;
|
|
|
|
+ offset = 0;
|
|
|
|
+ nlp = NULL;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ while (num1 <= num2)
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 10:48:00 +05:30
|
|
|
+ offset = findString(lp, oldStr, oldLen, offset);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (offset < 0)
|
|
|
|
+ {
|
|
|
|
+ if (needPrint)
|
|
|
|
+ {
|
|
|
|
+ printLines(num1, num1, FALSE);
|
|
|
|
+ needPrint = FALSE;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ offset = 0;
|
|
|
|
+ lp = lp->next;
|
|
|
|
+ num1++;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ continue;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ needPrint = printFlag;
|
|
|
|
+ didSub = TRUE;
|
|
|
|
+ dirty = TRUE;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ /*
|
|
|
|
+ * If the replacement string is the same size or shorter
|
|
|
|
+ * than the old string, then the substitution is easy.
|
|
|
|
+ */
|
|
|
|
+ if (deltaLen <= 0)
|
|
|
|
+ {
|
|
|
|
+ memcpy(&lp->data[offset], newStr, newLen);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (deltaLen)
|
|
|
|
+ {
|
|
|
|
+ memcpy(&lp->data[offset + newLen],
|
|
|
|
+ &lp->data[offset + oldLen],
|
|
|
|
+ lp->len - offset - oldLen);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ lp->len += deltaLen;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ offset += newLen;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (globalFlag)
|
|
|
|
+ continue;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (needPrint)
|
|
|
|
+ {
|
|
|
|
+ printLines(num1, num1, FALSE);
|
|
|
|
+ needPrint = FALSE;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ lp = lp->next;
|
|
|
|
+ num1++;
|
|
|
|
+
|
|
|
|
+ continue;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ /*
|
|
|
|
+ * The new string is larger, so allocate a new line
|
|
|
|
+ * structure and use that. Link it in in place of
|
|
|
|
+ * the old line structure.
|
|
|
|
+ */
|
|
|
|
+ nlp = (LINE *) malloc(sizeof(LINE) + lp->len + deltaLen);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (nlp == NULL)
|
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("Cannot get memory for line");
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ return;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ nlp->len = lp->len + deltaLen;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ memcpy(nlp->data, lp->data, offset);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ memcpy(&nlp->data[offset], newStr, newLen);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ memcpy(&nlp->data[offset + newLen],
|
|
|
|
+ &lp->data[offset + oldLen],
|
|
|
|
+ lp->len - offset - oldLen);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ nlp->next = lp->next;
|
|
|
|
+ nlp->prev = lp->prev;
|
|
|
|
+ nlp->prev->next = nlp;
|
|
|
|
+ nlp->next->prev = nlp;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (curLine == lp)
|
|
|
|
+ curLine = nlp;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ free(lp);
|
|
|
|
+ lp = nlp;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ offset += newLen;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (globalFlag)
|
|
|
|
+ continue;
|
|
|
|
+
|
|
|
|
+ if (needPrint)
|
|
|
|
+ {
|
|
|
|
+ printLines(num1, num1, FALSE);
|
|
|
|
+ needPrint = FALSE;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ lp = lp->next;
|
|
|
|
+ num1++;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!didSub)
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("No substitutions found for \"%s\"", oldStr);
|
2005-04-23 07:20:55 +05:30
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+/*
|
|
|
|
+ * Search a line for the specified string starting at the specified
|
|
|
|
+ * offset in the line. Returns the offset of the found string, or -1.
|
|
|
|
+ */
|
|
|
|
+static LEN
|
|
|
|
+findString( const LINE * lp, const char * str, LEN len, LEN offset)
|
2005-04-23 07:20:55 +05:30
|
|
|
+{
|
2005-04-24 10:48:00 +05:30
|
|
|
+ LEN left;
|
|
|
|
+ const char * cp;
|
|
|
|
+ const char * ncp;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ cp = &lp->data[offset];
|
|
|
|
+ left = lp->len - offset;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ while (left >= len)
|
|
|
|
+ {
|
|
|
|
+ ncp = memchr(cp, *str, left);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (ncp == NULL)
|
|
|
|
+ return -1;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ left -= (ncp - cp);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (left < len)
|
|
|
|
+ return -1;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ cp = ncp;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (memcmp(cp, str, len) == 0)
|
|
|
|
+ return (cp - lp->data);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ cp++;
|
|
|
|
+ left--;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return -1;
|
2005-04-23 07:20:55 +05:30
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+/*
|
|
|
|
+ * Add lines which are typed in by the user.
|
|
|
|
+ * The lines are inserted just before the specified line number.
|
|
|
|
+ * The lines are terminated by a line containing a single dot (ugly!),
|
|
|
|
+ * or by an end of file.
|
|
|
|
+ */
|
|
|
|
+static void
|
|
|
|
+addLines(NUM num)
|
2005-04-23 07:20:55 +05:30
|
|
|
+{
|
2005-04-24 10:48:00 +05:30
|
|
|
+ int len;
|
|
|
|
+ char buf[USERSIZE + 1];
|
|
|
|
+
|
|
|
|
+ while (fgets(buf, sizeof(buf), stdin))
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if ((buf[0] == '.') && (buf[1] == '\n') && (buf[2] == '\0'))
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ len = strlen(buf);
|
|
|
|
+
|
|
|
|
+ if (len == 0)
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ if (buf[len - 1] != '\n')
|
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("Line too long");
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+ do
|
|
|
|
+ {
|
|
|
|
+ len = fgetc(stdin);
|
|
|
|
+ }
|
|
|
|
+ while ((len != EOF) && (len != '\n'));
|
|
|
|
+
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!insertLine(num++, buf, len))
|
|
|
|
+ return;
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+/*
|
|
|
|
+ * Parse a line number argument if it is present. This is a sum
|
|
|
|
+ * or difference of numbers, '.', '$', 'x, or a search string.
|
|
|
|
+ * Returns TRUE if successful (whether or not there was a number).
|
|
|
|
+ * Returns FALSE if there was a parsing error, with a message output.
|
|
|
|
+ * Whether there was a number is returned indirectly, as is the number.
|
|
|
|
+ * The character pointer which stopped the scan is also returned.
|
|
|
|
+ */
|
|
|
|
+static BOOL
|
|
|
|
+getNum(const char ** retcp, BOOL * retHaveNum, NUM * retNum)
|
2005-04-23 07:20:55 +05:30
|
|
|
+{
|
2005-04-24 10:48:00 +05:30
|
|
|
+ const char * cp;
|
|
|
|
+ char * endStr;
|
|
|
|
+ char str[USERSIZE];
|
|
|
|
+ BOOL haveNum;
|
|
|
|
+ NUM value;
|
|
|
|
+ NUM num;
|
|
|
|
+ NUM sign;
|
|
|
|
+
|
|
|
|
+ cp = *retcp;
|
|
|
|
+ haveNum = FALSE;
|
|
|
|
+ value = 0;
|
|
|
|
+ sign = 1;
|
|
|
|
+
|
|
|
|
+ while (TRUE)
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ while (isblank(*cp))
|
2005-04-24 10:48:00 +05:30
|
|
|
+ cp++;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ switch (*cp)
|
|
|
|
+ {
|
|
|
|
+ case '.':
|
|
|
|
+ haveNum = TRUE;
|
|
|
|
+ num = curNum;
|
|
|
|
+ cp++;
|
|
|
|
+ break;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ case '$':
|
|
|
|
+ haveNum = TRUE;
|
|
|
|
+ num = lastNum;
|
|
|
|
+ cp++;
|
|
|
|
+ break;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ case '\'':
|
|
|
|
+ cp++;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if ((*cp < 'a') || (*cp > 'z'))
|
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("Bad mark name");
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ return FALSE;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ haveNum = TRUE;
|
|
|
|
+ num = marks[*cp++ - 'a'];
|
|
|
|
+ break;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ case '/':
|
|
|
|
+ strcpy(str, ++cp);
|
|
|
|
+ endStr = strchr(str, '/');
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (endStr)
|
|
|
|
+ {
|
|
|
|
+ *endStr++ = '\0';
|
|
|
|
+ cp += (endStr - str);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ cp = "";
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ num = searchLines(str, curNum, lastNum);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (num == 0)
|
|
|
|
+ return FALSE;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ haveNum = TRUE;
|
|
|
|
+ break;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ default:
|
2005-04-24 11:09:52 +05:30
|
|
|
+ if (!isdigit(*cp))
|
2005-04-24 10:48:00 +05:30
|
|
|
+ {
|
|
|
|
+ *retcp = cp;
|
|
|
|
+ *retHaveNum = haveNum;
|
|
|
|
+ *retNum = value;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ return TRUE;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ num = 0;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 11:09:52 +05:30
|
|
|
+ while (isdigit(*cp))
|
2005-04-24 10:48:00 +05:30
|
|
|
+ num = num * 10 + *cp++ - '0';
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ haveNum = TRUE;
|
|
|
|
+ break;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ value += num * sign;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 11:09:52 +05:30
|
|
|
+ while (isblank(*cp))
|
2005-04-24 10:48:00 +05:30
|
|
|
+ cp++;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ switch (*cp)
|
|
|
|
+ {
|
|
|
|
+ case '-':
|
|
|
|
+ sign = -1;
|
|
|
|
+ cp++;
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case '+':
|
|
|
|
+ sign = 1;
|
|
|
|
+ cp++;
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ default:
|
|
|
|
+ *retcp = cp;
|
|
|
|
+ *retHaveNum = haveNum;
|
|
|
|
+ *retNum = value;
|
|
|
|
+
|
|
|
|
+ return TRUE;
|
|
|
|
+ }
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+/*
|
|
|
|
+ * Initialize everything for editing.
|
|
|
|
+ */
|
|
|
|
+static BOOL
|
|
|
|
+initEdit(void)
|
2005-04-23 07:20:55 +05:30
|
|
|
+{
|
2005-04-24 10:48:00 +05:30
|
|
|
+ int i;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ bufSize = INITBUF_SIZE;
|
|
|
|
+ bufBase = malloc(bufSize);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (bufBase == NULL)
|
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("No memory for buffer");
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ return FALSE;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ bufPtr = bufBase;
|
|
|
|
+ bufUsed = 0;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ lines.next = &lines;
|
|
|
|
+ lines.prev = &lines;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ curLine = NULL;
|
|
|
|
+ curNum = 0;
|
|
|
|
+ lastNum = 0;
|
|
|
|
+ dirty = FALSE;
|
|
|
|
+ fileName = NULL;
|
|
|
|
+ searchString[0] = '\0';
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ for (i = 0; i < 26; i++)
|
|
|
|
+ marks[i] = 0;
|
|
|
|
+
|
|
|
|
+ return TRUE;
|
2005-04-23 07:20:55 +05:30
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+/*
|
|
|
|
+ * Finish editing.
|
|
|
|
+ */
|
|
|
|
+static void
|
|
|
|
+termEdit(void)
|
2005-04-23 07:20:55 +05:30
|
|
|
+{
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (bufBase)
|
|
|
|
+ free(bufBase);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ bufBase = NULL;
|
|
|
|
+ bufPtr = NULL;
|
|
|
|
+ bufSize = 0;
|
|
|
|
+ bufUsed = 0;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (fileName)
|
|
|
|
+ free(fileName);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ fileName = NULL;
|
|
|
|
+
|
|
|
|
+ searchString[0] = '\0';
|
|
|
|
+
|
|
|
|
+ if (lastNum)
|
|
|
|
+ deleteLines(1, lastNum);
|
|
|
|
+
|
|
|
|
+ lastNum = 0;
|
|
|
|
+ curNum = 0;
|
|
|
|
+ curLine = NULL;
|
2005-04-23 07:20:55 +05:30
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+/*
|
|
|
|
+ * Read lines from a file at the specified line number.
|
|
|
|
+ * Returns TRUE if the file was successfully read.
|
|
|
|
+ */
|
|
|
|
+static BOOL
|
|
|
|
+readLines(const char * file, NUM num)
|
2005-04-23 07:20:55 +05:30
|
|
|
+{
|
2005-04-24 10:48:00 +05:30
|
|
|
+ int fd;
|
|
|
|
+ int cc;
|
|
|
|
+ LEN len;
|
|
|
|
+ LEN lineCount;
|
|
|
|
+ LEN charCount;
|
|
|
|
+ char * cp;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if ((num < 1) || (num > lastNum + 1))
|
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("Bad line for read");
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ return FALSE;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ fd = open(file, 0);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (fd < 0)
|
|
|
|
+ {
|
|
|
|
+ perror(file);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ return FALSE;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ bufPtr = bufBase;
|
|
|
|
+ bufUsed = 0;
|
|
|
|
+ lineCount = 0;
|
|
|
|
+ charCount = 0;
|
|
|
|
+ cc = 0;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ printf("\"%s\", ", file);
|
|
|
|
+ fflush(stdout);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ do
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 10:48:00 +05:30
|
|
|
+ cp = memchr(bufPtr, '\n', bufUsed);
|
|
|
|
+
|
|
|
|
+ if (cp)
|
|
|
|
+ {
|
|
|
|
+ len = (cp - bufPtr) + 1;
|
|
|
|
+
|
|
|
|
+ if (!insertLine(num, bufPtr, len))
|
|
|
|
+ {
|
|
|
|
+ close(fd);
|
|
|
|
+
|
|
|
|
+ return FALSE;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ bufPtr += len;
|
|
|
|
+ bufUsed -= len;
|
|
|
|
+ charCount += len;
|
|
|
|
+ lineCount++;
|
|
|
|
+ num++;
|
|
|
|
+
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (bufPtr != bufBase)
|
|
|
|
+ {
|
|
|
|
+ memcpy(bufBase, bufPtr, bufUsed);
|
|
|
|
+ bufPtr = bufBase + bufUsed;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (bufUsed >= bufSize)
|
|
|
|
+ {
|
|
|
|
+ len = (bufSize * 3) / 2;
|
|
|
|
+ cp = realloc(bufBase, len);
|
|
|
|
+
|
|
|
|
+ if (cp == NULL)
|
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("No memory for buffer");
|
2005-04-24 10:48:00 +05:30
|
|
|
+ close(fd);
|
|
|
|
+
|
|
|
|
+ return FALSE;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ bufBase = cp;
|
|
|
|
+ bufPtr = bufBase + bufUsed;
|
|
|
|
+ bufSize = len;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ cc = read(fd, bufPtr, bufSize - bufUsed);
|
|
|
|
+ bufUsed += cc;
|
|
|
|
+ bufPtr = bufBase;
|
|
|
|
+
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
2005-04-24 10:48:00 +05:30
|
|
|
+ while (cc > 0);
|
|
|
|
+
|
|
|
|
+ if (cc < 0)
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 10:48:00 +05:30
|
|
|
+ perror(file);
|
|
|
|
+ close(fd);
|
|
|
|
+
|
|
|
|
+ return FALSE;
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (bufUsed)
|
|
|
|
+ {
|
|
|
|
+ if (!insertLine(num, bufPtr, bufUsed))
|
|
|
|
+ {
|
|
|
|
+ close(fd);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ return -1;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ lineCount++;
|
|
|
|
+ charCount += bufUsed;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ close(fd);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ printf("%d lines%s, %d chars\n", lineCount,
|
|
|
|
+ (bufUsed ? " (incomplete)" : ""), charCount);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ return TRUE;
|
2005-04-23 07:20:55 +05:30
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+/*
|
|
|
|
+ * Write the specified lines out to the specified file.
|
|
|
|
+ * Returns TRUE if successful, or FALSE on an error with a message output.
|
|
|
|
+ */
|
|
|
|
+static BOOL
|
|
|
|
+writeLines(const char * file, NUM num1, NUM num2)
|
2005-04-23 07:20:55 +05:30
|
|
|
+{
|
2005-04-24 10:48:00 +05:30
|
|
|
+ int fd;
|
|
|
|
+ LINE * lp;
|
|
|
|
+ LEN lineCount;
|
|
|
|
+ LEN charCount;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if ((num1 < 1) || (num2 > lastNum) || (num1 > num2))
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("Bad line range for write");
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+ return FALSE;
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ lineCount = 0;
|
|
|
|
+ charCount = 0;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ fd = creat(file, 0666);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (fd < 0) {
|
|
|
|
+ perror(file);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ return FALSE;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ printf("\"%s\", ", file);
|
|
|
|
+ fflush(stdout);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ lp = findLine(num1);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (lp == NULL)
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 10:48:00 +05:30
|
|
|
+ close(fd);
|
|
|
|
+
|
|
|
|
+ return FALSE;
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+ while (num1++ <= num2)
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (write(fd, lp->data, lp->len) != lp->len)
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 10:48:00 +05:30
|
|
|
+ perror(file);
|
|
|
|
+ close(fd);
|
|
|
|
+
|
|
|
|
+ return FALSE;
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ charCount += lp->len;
|
|
|
|
+ lineCount++;
|
|
|
|
+ lp = lp->next;
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (close(fd) < 0)
|
|
|
|
+ {
|
|
|
|
+ perror(file);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ return FALSE;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ printf("%d lines, %d chars\n", lineCount, charCount);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ return TRUE;
|
2005-04-23 07:20:55 +05:30
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+/*
|
|
|
|
+ * Print lines in a specified range.
|
|
|
|
+ * The last line printed becomes the current line.
|
|
|
|
+ * If expandFlag is TRUE, then the line is printed specially to
|
|
|
|
+ * show magic characters.
|
|
|
|
+ */
|
|
|
|
+static BOOL
|
|
|
|
+printLines(NUM num1, NUM num2, BOOL expandFlag)
|
2005-04-23 07:20:55 +05:30
|
|
|
+{
|
2005-04-24 10:48:00 +05:30
|
|
|
+ const LINE * lp;
|
|
|
|
+ const unsigned char * cp;
|
|
|
|
+ int ch;
|
|
|
|
+ LEN count;
|
|
|
|
+
|
|
|
|
+ if ((num1 < 1) || (num2 > lastNum) || (num1 > num2))
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("Bad line range for print");
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+ return FALSE;
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ lp = findLine(num1);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (lp == NULL)
|
|
|
|
+ return FALSE;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ while (num1 <= num2)
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (!expandFlag)
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ write(1, lp->data, lp->len);
|
2005-04-24 10:48:00 +05:30
|
|
|
+ setCurNum(num1++);
|
|
|
|
+ lp = lp->next;
|
|
|
|
+
|
|
|
|
+ continue;
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Show control characters and characters with the
|
|
|
|
+ * high bit set specially.
|
|
|
|
+ */
|
|
|
|
+ cp = lp->data;
|
|
|
|
+ count = lp->len;
|
|
|
|
+
|
|
|
|
+ if ((count > 0) && (cp[count - 1] == '\n'))
|
|
|
|
+ count--;
|
|
|
|
+
|
|
|
|
+ while (count-- > 0)
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 10:48:00 +05:30
|
|
|
+ ch = *cp++;
|
|
|
|
+
|
|
|
|
+ if (ch & 0x80)
|
|
|
|
+ {
|
|
|
|
+ fputs("M-", stdout);
|
|
|
|
+ ch &= 0x7f;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (ch < ' ')
|
|
|
|
+ {
|
|
|
|
+ fputc('^', stdout);
|
|
|
|
+ ch += '@';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (ch == 0x7f)
|
|
|
|
+ {
|
|
|
|
+ fputc('^', stdout);
|
|
|
|
+ ch = '?';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ fputc(ch, stdout);
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+ fputs("$\n", stdout);
|
|
|
|
+
|
|
|
|
+ setCurNum(num1++);
|
|
|
|
+ lp = lp->next;
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+ return TRUE;
|
2005-04-23 07:20:55 +05:30
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+/*
|
|
|
|
+ * Insert a new line with the specified text.
|
|
|
|
+ * The line is inserted so as to become the specified line,
|
|
|
|
+ * thus pushing any existing and further lines down one.
|
|
|
|
+ * The inserted line is also set to become the current line.
|
|
|
|
+ * Returns TRUE if successful.
|
|
|
|
+ */
|
|
|
|
+static BOOL
|
|
|
|
+insertLine(NUM num, const char * data, LEN len)
|
2005-04-23 07:20:55 +05:30
|
|
|
+{
|
2005-04-24 10:48:00 +05:30
|
|
|
+ LINE * newLp;
|
|
|
|
+ LINE * lp;
|
|
|
|
+
|
|
|
|
+ if ((num < 1) || (num > lastNum + 1))
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("Inserting at bad line number");
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+ return FALSE;
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+ newLp = (LINE *) malloc(sizeof(LINE) + len - 1);
|
|
|
|
+
|
|
|
|
+ if (newLp == NULL)
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("Failed to allocate memory for line");
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+ return FALSE;
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ memcpy(newLp->data, data, len);
|
|
|
|
+ newLp->len = len;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (num > lastNum)
|
|
|
|
+ lp = &lines;
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ lp = findLine(num);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (lp == NULL)
|
|
|
|
+ {
|
|
|
|
+ free((char *) newLp);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ return FALSE;
|
|
|
|
+ }
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ newLp->next = lp;
|
|
|
|
+ newLp->prev = lp->prev;
|
|
|
|
+ lp->prev->next = newLp;
|
|
|
|
+ lp->prev = newLp;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ lastNum++;
|
|
|
|
+ dirty = TRUE;
|
|
|
|
+
|
|
|
|
+ return setCurNum(num);
|
2005-04-23 07:20:55 +05:30
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+/*
|
|
|
|
+ * Delete lines from the given range.
|
|
|
|
+ */
|
|
|
|
+static BOOL
|
|
|
|
+deleteLines(NUM num1, NUM num2)
|
|
|
|
+{
|
|
|
|
+ LINE * lp;
|
|
|
|
+ LINE * nlp;
|
|
|
|
+ LINE * plp;
|
|
|
|
+ NUM count;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if ((num1 < 1) || (num2 > lastNum) || (num1 > num2))
|
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("Bad line numbers for delete");
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ return FALSE;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ lp = findLine(num1);
|
|
|
|
+
|
|
|
|
+ if (lp == NULL)
|
|
|
|
+ return FALSE;
|
|
|
|
+
|
|
|
|
+ if ((curNum >= num1) && (curNum <= num2))
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (num2 < lastNum)
|
|
|
|
+ setCurNum(num2 + 1);
|
|
|
|
+ else if (num1 > 1)
|
|
|
|
+ setCurNum(num1 - 1);
|
|
|
|
+ else
|
|
|
|
+ curNum = 0;
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ count = num2 - num1 + 1;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (curNum > num2)
|
|
|
|
+ curNum -= count;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ lastNum -= count;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ while (count-- > 0)
|
|
|
|
+ {
|
|
|
|
+ nlp = lp->next;
|
|
|
|
+ plp = lp->prev;
|
|
|
|
+ plp->next = nlp;
|
|
|
|
+ nlp->prev = plp;
|
|
|
|
+ lp->next = NULL;
|
|
|
|
+ lp->prev = NULL;
|
|
|
|
+ lp->len = 0;
|
|
|
|
+ free(lp);
|
|
|
|
+ lp = nlp;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ dirty = TRUE;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ return TRUE;
|
2005-04-23 07:20:55 +05:30
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+/*
|
|
|
|
+ * Search for a line which contains the specified string.
|
|
|
|
+ * If the string is NULL, then the previously searched for string
|
|
|
|
+ * is used. The currently searched for string is saved for future use.
|
|
|
|
+ * Returns the line number which matches, or 0 if there was no match
|
|
|
|
+ * with an error printed.
|
|
|
|
+ */
|
|
|
|
+static NUM
|
|
|
|
+searchLines(const char * str, NUM num1, NUM num2)
|
2005-04-23 07:20:55 +05:30
|
|
|
+{
|
2005-04-24 10:48:00 +05:30
|
|
|
+ const LINE * lp;
|
|
|
|
+ int len;
|
|
|
|
+
|
|
|
|
+ if ((num1 < 1) || (num2 > lastNum) || (num1 > num2))
|
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("Bad line numbers for search");
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (*str == '\0')
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (searchString[0] == '\0')
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("No previous search string");
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+ return 0;
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ str = searchString;
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (str != searchString)
|
|
|
|
+ strcpy(searchString, str);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ len = strlen(str);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ lp = findLine(num1);
|
|
|
|
+
|
|
|
|
+ if (lp == NULL)
|
|
|
|
+ return 0;
|
|
|
|
+
|
|
|
|
+ while (num1 <= num2)
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (findString(lp, str, len, 0) >= 0)
|
|
|
|
+ return num1;
|
|
|
|
+
|
|
|
|
+ num1++;
|
|
|
|
+ lp = lp->next;
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("Cannot find string \"%s\"", str);
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+ return 0;
|
2005-04-23 07:20:55 +05:30
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+/*
|
|
|
|
+ * Return a pointer to the specified line number.
|
|
|
|
+ */
|
|
|
|
+static LINE *
|
|
|
|
+findLine(NUM num)
|
2005-04-23 07:20:55 +05:30
|
|
|
+{
|
2005-04-24 10:48:00 +05:30
|
|
|
+ LINE * lp;
|
|
|
|
+ NUM lnum;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if ((num < 1) || (num > lastNum))
|
2005-04-23 07:20:55 +05:30
|
|
|
+ {
|
2005-04-24 11:09:52 +05:30
|
|
|
+ bb_error_msg("Line number %d does not exist", num);
|
2005-04-24 10:48:00 +05:30
|
|
|
+
|
|
|
|
+ return NULL;
|
2005-04-23 07:20:55 +05:30
|
|
|
+ }
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (curNum <= 0)
|
|
|
|
+ {
|
|
|
|
+ curNum = 1;
|
|
|
|
+ curLine = lines.next;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (num == curNum)
|
|
|
|
+ return curLine;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ lp = curLine;
|
|
|
|
+ lnum = curNum;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (num < (curNum / 2))
|
|
|
|
+ {
|
|
|
|
+ lp = lines.next;
|
|
|
|
+ lnum = 1;
|
|
|
|
+ }
|
|
|
|
+ else if (num > ((curNum + lastNum) / 2))
|
|
|
|
+ {
|
|
|
|
+ lp = lines.prev;
|
|
|
|
+ lnum = lastNum;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ while (lnum < num)
|
|
|
|
+ {
|
|
|
|
+ lp = lp->next;
|
|
|
|
+ lnum++;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ while (lnum > num)
|
|
|
|
+ {
|
|
|
|
+ lp = lp->prev;
|
|
|
|
+ lnum--;
|
|
|
|
+ }
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ return lp;
|
2005-04-23 07:20:55 +05:30
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+/*
|
|
|
|
+ * Set the current line number.
|
|
|
|
+ * Returns TRUE if successful.
|
|
|
|
+ */
|
|
|
|
+static BOOL
|
|
|
|
+setCurNum(NUM num)
|
2005-04-23 07:20:55 +05:30
|
|
|
+{
|
2005-04-24 10:48:00 +05:30
|
|
|
+ LINE * lp;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ lp = findLine(num);
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ if (lp == NULL)
|
|
|
|
+ return FALSE;
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+ curNum = num;
|
|
|
|
+ curLine = lp;
|
|
|
|
+
|
|
|
|
+ return TRUE;
|
|
|
|
+}
|
2005-04-23 07:20:55 +05:30
|
|
|
+
|
2005-04-24 10:48:00 +05:30
|
|
|
+/* END CODE */
|