2006-05-19 12:38:30 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-05-04 20:56:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2002 by David I. Bell
|
|
|
|
* Permission is granted to use, distribute, or modify this source,
|
|
|
|
* provided that this copyright notice remains intact.
|
|
|
|
*
|
|
|
|
* The "ed" built-in command (much simplified)
|
|
|
|
*/
|
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2006-05-04 20:56:43 +00:00
|
|
|
|
2007-09-25 11:55:57 +00:00
|
|
|
typedef struct LINE {
|
|
|
|
struct LINE *next;
|
|
|
|
struct LINE *prev;
|
|
|
|
int len;
|
|
|
|
char data[1];
|
|
|
|
} LINE;
|
|
|
|
|
|
|
|
|
2007-06-04 10:16:52 +00:00
|
|
|
#define searchString bb_common_bufsiz1
|
|
|
|
|
2007-03-24 22:42:29 +00:00
|
|
|
enum {
|
2007-06-04 10:16:52 +00:00
|
|
|
USERSIZE = sizeof(searchString) > 1024 ? 1024
|
|
|
|
: sizeof(searchString) - 1, /* max line length typed in by user */
|
2007-03-24 22:42:29 +00:00
|
|
|
INITBUF_SIZE = 1024, /* initial buffer size */
|
|
|
|
};
|
|
|
|
|
2007-09-25 11:55:57 +00:00
|
|
|
struct globals {
|
|
|
|
int curNum;
|
|
|
|
int lastNum;
|
|
|
|
int bufUsed;
|
|
|
|
int bufSize;
|
|
|
|
LINE *curLine;
|
|
|
|
char *bufBase;
|
|
|
|
char *bufPtr;
|
|
|
|
char *fileName;
|
|
|
|
LINE lines;
|
|
|
|
smallint dirty;
|
|
|
|
int marks[26];
|
|
|
|
};
|
|
|
|
#define G (*ptr_to_globals)
|
|
|
|
#define curLine (G.curLine )
|
|
|
|
#define bufBase (G.bufBase )
|
|
|
|
#define bufPtr (G.bufPtr )
|
|
|
|
#define fileName (G.fileName )
|
|
|
|
#define curNum (G.curNum )
|
|
|
|
#define lastNum (G.lastNum )
|
|
|
|
#define bufUsed (G.bufUsed )
|
|
|
|
#define bufSize (G.bufSize )
|
|
|
|
#define dirty (G.dirty )
|
|
|
|
#define lines (G.lines )
|
|
|
|
#define marks (G.marks )
|
|
|
|
#define INIT_G() do { \
|
2008-02-27 18:41:59 +00:00
|
|
|
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
|
2007-09-25 11:55:57 +00:00
|
|
|
} while (0)
|
2006-05-04 20:56:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void doCommands(void);
|
|
|
|
static void subCommand(const char *cmd, int num1, int num2);
|
2007-09-25 11:55:57 +00:00
|
|
|
static int getNum(const char **retcp, smallint *retHaveNum, int *retNum);
|
2006-05-04 20:56:43 +00:00
|
|
|
static int setCurNum(int num);
|
|
|
|
static void addLines(int num);
|
|
|
|
static int insertLine(int num, const char *data, int len);
|
2007-09-25 11:55:57 +00:00
|
|
|
static void deleteLines(int num1, int num2);
|
2006-05-04 20:56:43 +00:00
|
|
|
static int printLines(int num1, int num2, int expandFlag);
|
|
|
|
static int writeLines(const char *file, int num1, int num2);
|
|
|
|
static int readLines(const char *file, int num);
|
|
|
|
static int searchLines(const char *str, int num1, int num2);
|
|
|
|
static LINE *findLine(int num);
|
|
|
|
static int findString(const LINE *lp, const char * str, int len, int offset);
|
|
|
|
|
2007-09-25 11:55:57 +00:00
|
|
|
|
|
|
|
static int bad_nums(int num1, int num2, const char *for_what)
|
|
|
|
{
|
|
|
|
if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
|
|
|
|
bb_error_msg("bad line range for %s", for_what);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *skip_blank(const char *cp)
|
|
|
|
{
|
|
|
|
while (isblank(*cp))
|
|
|
|
cp++;
|
|
|
|
return (char *)cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int ed_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-03-17 09:09:09 +00:00
|
|
|
int ed_main(int argc ATTRIBUTE_UNUSED, char **argv)
|
2006-05-04 20:56:43 +00:00
|
|
|
{
|
2007-09-25 11:55:57 +00:00
|
|
|
INIT_G();
|
2006-05-04 20:56:43 +00:00
|
|
|
|
2007-09-25 11:55:57 +00:00
|
|
|
bufSize = INITBUF_SIZE;
|
|
|
|
bufBase = xmalloc(bufSize);
|
|
|
|
bufPtr = bufBase;
|
|
|
|
lines.next = &lines;
|
|
|
|
lines.prev = &lines;
|
2006-05-04 20:56:43 +00:00
|
|
|
|
2008-03-17 09:09:09 +00:00
|
|
|
if (argv[1]) {
|
2007-09-25 11:55:57 +00:00
|
|
|
fileName = xstrdup(argv[1]);
|
2006-05-04 20:56:43 +00:00
|
|
|
if (!readLines(fileName, 1)) {
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
if (lastNum)
|
|
|
|
setCurNum(1);
|
|
|
|
dirty = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
doCommands();
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read commands until we are told to stop.
|
|
|
|
*/
|
|
|
|
static void doCommands(void)
|
|
|
|
{
|
|
|
|
const char *cp;
|
|
|
|
char *endbuf, *newname, buf[USERSIZE];
|
2007-09-25 11:55:57 +00:00
|
|
|
int len, num1, num2;
|
|
|
|
smallint have1, have2;
|
2006-05-04 20:56:43 +00:00
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
while (TRUE) {
|
2007-09-25 11:55:57 +00:00
|
|
|
/* Returns:
|
|
|
|
* -1 on read errors or EOF, or on bare Ctrl-D.
|
|
|
|
* 0 on ctrl-C,
|
|
|
|
* >0 length of input string, including terminating '\n'
|
|
|
|
*/
|
|
|
|
len = read_line_input(": ", buf, sizeof(buf), NULL);
|
|
|
|
if (len <= 0)
|
2006-05-04 20:56:43 +00:00
|
|
|
return;
|
|
|
|
endbuf = &buf[len - 1];
|
|
|
|
while ((endbuf > buf) && isblank(endbuf[-1]))
|
|
|
|
endbuf--;
|
|
|
|
*endbuf = '\0';
|
|
|
|
|
2007-09-25 11:55:57 +00:00
|
|
|
cp = skip_blank(buf);
|
2006-05-04 20:56:43 +00:00
|
|
|
have1 = FALSE;
|
|
|
|
have2 = FALSE;
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
if ((curNum == 0) && (lastNum > 0)) {
|
2006-05-04 20:56:43 +00:00
|
|
|
curNum = 1;
|
|
|
|
curLine = lines.next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!getNum(&cp, &have1, &num1))
|
|
|
|
continue;
|
|
|
|
|
2007-09-25 11:55:57 +00:00
|
|
|
cp = skip_blank(cp);
|
2006-05-04 20:56:43 +00:00
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
if (*cp == ',') {
|
2006-05-04 20:56:43 +00:00
|
|
|
cp++;
|
|
|
|
if (!getNum(&cp, &have2, &num2))
|
|
|
|
continue;
|
|
|
|
if (!have1)
|
|
|
|
num1 = 1;
|
|
|
|
if (!have2)
|
|
|
|
num2 = lastNum;
|
|
|
|
have1 = TRUE;
|
|
|
|
have2 = TRUE;
|
|
|
|
}
|
|
|
|
if (!have1)
|
|
|
|
num1 = curNum;
|
|
|
|
if (!have2)
|
|
|
|
num2 = num1;
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
switch (*cp++) {
|
2006-05-04 20:56:43 +00:00
|
|
|
case 'a':
|
|
|
|
addLines(num1 + 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
deleteLines(num1, num2);
|
|
|
|
addLines(num1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
deleteLines(num1, num2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
2006-11-30 20:57:50 +00:00
|
|
|
if (*cp && !isblank(*cp)) {
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("bad file command");
|
2006-05-04 20:56:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-09-25 11:55:57 +00:00
|
|
|
cp = skip_blank(cp);
|
2006-11-30 20:57:50 +00:00
|
|
|
if (*cp == '\0') {
|
2006-05-04 20:56:43 +00:00
|
|
|
if (fileName)
|
|
|
|
printf("\"%s\"\n", fileName);
|
|
|
|
else
|
|
|
|
printf("No file name\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
newname = strdup(cp);
|
2006-11-30 20:57:50 +00:00
|
|
|
if (newname == NULL) {
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("no memory for file name");
|
2006-05-04 20:56:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-09-25 11:55:57 +00:00
|
|
|
free(fileName);
|
2006-05-04 20:56:43 +00:00
|
|
|
fileName = newname;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i':
|
|
|
|
addLines(num1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'k':
|
2007-09-25 11:55:57 +00:00
|
|
|
cp = skip_blank(cp);
|
|
|
|
if ((*cp < 'a') || (*cp > 'z') || cp[1]) {
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("bad mark name");
|
2006-05-04 20:56:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
marks[*cp - 'a'] = num2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
printLines(num1, num2, TRUE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
printLines(num1, num2, FALSE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'q':
|
2007-09-25 11:55:57 +00:00
|
|
|
cp = skip_blank(cp);
|
2006-11-30 20:57:50 +00:00
|
|
|
if (have1 || *cp) {
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("bad quit command");
|
2006-05-04 20:56:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!dirty)
|
|
|
|
return;
|
2007-09-25 11:55:57 +00:00
|
|
|
len = read_line_input("Really quit? ", buf, 16, NULL);
|
|
|
|
/* read error/EOF - no way to continue */
|
|
|
|
if (len < 0)
|
|
|
|
return;
|
|
|
|
cp = skip_blank(buf);
|
|
|
|
if ((*cp | 0x20) == 'y') /* Y or y */
|
2006-05-04 20:56:43 +00:00
|
|
|
return;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
2006-11-30 20:57:50 +00:00
|
|
|
if (*cp && !isblank(*cp)) {
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("bad read command");
|
2006-05-04 20:56:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-09-25 11:55:57 +00:00
|
|
|
cp = skip_blank(cp);
|
2006-11-30 20:57:50 +00:00
|
|
|
if (*cp == '\0') {
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("no file name");
|
2006-05-04 20:56:43 +00:00
|
|
|
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':
|
2006-11-30 20:57:50 +00:00
|
|
|
if (*cp && !isblank(*cp)) {
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("bad write command");
|
2006-05-04 20:56:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-09-25 11:55:57 +00:00
|
|
|
cp = skip_blank(cp);
|
2006-05-04 20:56:43 +00:00
|
|
|
if (!have1) {
|
|
|
|
num1 = 1;
|
|
|
|
num2 = lastNum;
|
|
|
|
}
|
|
|
|
if (*cp == '\0')
|
|
|
|
cp = fileName;
|
2006-11-30 20:57:50 +00:00
|
|
|
if (cp == NULL) {
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("no file name specified");
|
2006-05-04 20:56:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
writeLines(cp, num1, num2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'z':
|
2006-11-30 20:57:50 +00:00
|
|
|
switch (*cp) {
|
2006-05-04 20:56:43 +00:00
|
|
|
case '-':
|
2007-09-25 11:55:57 +00:00
|
|
|
printLines(curNum - 21, curNum, FALSE);
|
2006-05-04 20:56:43 +00:00
|
|
|
break;
|
|
|
|
case '.':
|
2007-09-25 11:55:57 +00:00
|
|
|
printLines(curNum - 11, curNum + 10, FALSE);
|
2006-05-04 20:56:43 +00:00
|
|
|
break;
|
|
|
|
default:
|
2007-09-25 11:55:57 +00:00
|
|
|
printLines(curNum, curNum + 21, FALSE);
|
2006-05-04 20:56:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '.':
|
2006-11-30 20:57:50 +00:00
|
|
|
if (have1) {
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("no arguments allowed");
|
2006-05-04 20:56:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
printLines(curNum, curNum, FALSE);
|
|
|
|
break;
|
2006-05-19 12:38:30 +00:00
|
|
|
|
2006-05-04 20:56:43 +00:00
|
|
|
case '-':
|
|
|
|
if (setCurNum(curNum - 1))
|
|
|
|
printLines(curNum, curNum, FALSE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '=':
|
|
|
|
printf("%d\n", num1);
|
|
|
|
break;
|
|
|
|
case '\0':
|
2006-11-30 20:57:50 +00:00
|
|
|
if (have1) {
|
2006-05-04 20:56:43 +00:00
|
|
|
printLines(num2, num2, FALSE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (setCurNum(curNum + 1))
|
|
|
|
printLines(curNum, curNum, FALSE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("unimplemented command");
|
2006-05-04 20:56:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do the substitute command.
|
|
|
|
* The current line is set to the last substitution done.
|
|
|
|
*/
|
2007-09-25 11:55:57 +00:00
|
|
|
static void subCommand(const char *cmd, int num1, int num2)
|
2006-05-04 20:56:43 +00:00
|
|
|
{
|
|
|
|
char *cp, *oldStr, *newStr, buf[USERSIZE];
|
2006-11-30 20:57:50 +00:00
|
|
|
int delim, oldLen, newLen, deltaLen, offset;
|
2006-05-04 20:56:43 +00:00
|
|
|
LINE *lp, *nlp;
|
|
|
|
int globalFlag, printFlag, didSub, needPrint;
|
|
|
|
|
2007-09-25 11:55:57 +00:00
|
|
|
if (bad_nums(num1, num2, "substitute"))
|
2006-05-04 20:56:43 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
globalFlag = FALSE;
|
|
|
|
printFlag = FALSE;
|
|
|
|
didSub = FALSE;
|
|
|
|
needPrint = FALSE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy the command so we can modify it.
|
|
|
|
*/
|
|
|
|
strcpy(buf, cmd);
|
|
|
|
cp = buf;
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
if (isblank(*cp) || (*cp == '\0')) {
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("bad delimiter for substitute");
|
2006-05-04 20:56:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
delim = *cp++;
|
|
|
|
oldStr = cp;
|
|
|
|
|
|
|
|
cp = strchr(cp, delim);
|
2006-11-30 20:57:50 +00:00
|
|
|
if (cp == NULL) {
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("missing 2nd delimiter for substitute");
|
2006-05-04 20:56:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*cp++ = '\0';
|
|
|
|
|
|
|
|
newStr = cp;
|
|
|
|
cp = strchr(cp, delim);
|
|
|
|
|
|
|
|
if (cp)
|
|
|
|
*cp++ = '\0';
|
|
|
|
else
|
2007-01-29 22:51:00 +00:00
|
|
|
cp = (char*)"";
|
2006-05-04 20:56:43 +00:00
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
while (*cp) switch (*cp++) {
|
2006-05-04 20:56:43 +00:00
|
|
|
case 'g':
|
|
|
|
globalFlag = TRUE;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
printFlag = TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("unknown option for substitute");
|
2006-05-04 20:56:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
if (*oldStr == '\0') {
|
|
|
|
if (searchString[0] == '\0') {
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("no previous search string");
|
2006-05-04 20:56:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
oldStr = searchString;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldStr != searchString)
|
|
|
|
strcpy(searchString, oldStr);
|
|
|
|
|
|
|
|
lp = findLine(num1);
|
|
|
|
if (lp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
oldLen = strlen(oldStr);
|
|
|
|
newLen = strlen(newStr);
|
|
|
|
deltaLen = newLen - oldLen;
|
|
|
|
offset = 0;
|
|
|
|
nlp = NULL;
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
while (num1 <= num2) {
|
2006-05-04 20:56:43 +00:00
|
|
|
offset = findString(lp, oldStr, oldLen, offset);
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
if (offset < 0) {
|
|
|
|
if (needPrint) {
|
2006-05-04 20:56:43 +00:00
|
|
|
printLines(num1, num1, FALSE);
|
|
|
|
needPrint = FALSE;
|
|
|
|
}
|
|
|
|
offset = 0;
|
|
|
|
lp = lp->next;
|
|
|
|
num1++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
needPrint = printFlag;
|
|
|
|
didSub = TRUE;
|
|
|
|
dirty = TRUE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the replacement string is the same size or shorter
|
|
|
|
* than the old string, then the substitution is easy.
|
|
|
|
*/
|
2006-11-30 20:57:50 +00:00
|
|
|
if (deltaLen <= 0) {
|
2006-05-04 20:56:43 +00:00
|
|
|
memcpy(&lp->data[offset], newStr, newLen);
|
2006-11-30 20:57:50 +00:00
|
|
|
if (deltaLen) {
|
2006-05-04 20:56:43 +00:00
|
|
|
memcpy(&lp->data[offset + newLen],
|
|
|
|
&lp->data[offset + oldLen],
|
|
|
|
lp->len - offset - oldLen);
|
|
|
|
|
|
|
|
lp->len += deltaLen;
|
|
|
|
}
|
|
|
|
offset += newLen;
|
|
|
|
if (globalFlag)
|
|
|
|
continue;
|
2006-11-30 20:57:50 +00:00
|
|
|
if (needPrint) {
|
2006-05-04 20:56:43 +00:00
|
|
|
printLines(num1, num1, FALSE);
|
|
|
|
needPrint = FALSE;
|
|
|
|
}
|
|
|
|
lp = lp->next;
|
|
|
|
num1++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The new string is larger, so allocate a new line
|
|
|
|
* structure and use that. Link it in in place of
|
|
|
|
* the old line structure.
|
|
|
|
*/
|
2007-09-25 11:55:57 +00:00
|
|
|
nlp = malloc(sizeof(LINE) + lp->len + deltaLen);
|
2006-11-30 20:57:50 +00:00
|
|
|
if (nlp == NULL) {
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("cannot get memory for line");
|
2006-05-04 20:56:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nlp->len = lp->len + deltaLen;
|
|
|
|
|
|
|
|
memcpy(nlp->data, lp->data, offset);
|
|
|
|
memcpy(&nlp->data[offset], newStr, newLen);
|
|
|
|
memcpy(&nlp->data[offset + newLen],
|
|
|
|
&lp->data[offset + oldLen],
|
|
|
|
lp->len - offset - oldLen);
|
|
|
|
|
|
|
|
nlp->next = lp->next;
|
|
|
|
nlp->prev = lp->prev;
|
|
|
|
nlp->prev->next = nlp;
|
|
|
|
nlp->next->prev = nlp;
|
|
|
|
|
|
|
|
if (curLine == lp)
|
|
|
|
curLine = nlp;
|
|
|
|
|
|
|
|
free(lp);
|
|
|
|
lp = nlp;
|
|
|
|
|
|
|
|
offset += newLen;
|
|
|
|
|
|
|
|
if (globalFlag)
|
|
|
|
continue;
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
if (needPrint) {
|
2006-05-04 20:56:43 +00:00
|
|
|
printLines(num1, num1, FALSE);
|
|
|
|
needPrint = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
lp = lp->next;
|
|
|
|
num1++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!didSub)
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("no substitutions found for \"%s\"", oldStr);
|
2006-05-04 20:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search a line for the specified string starting at the specified
|
|
|
|
* offset in the line. Returns the offset of the found string, or -1.
|
|
|
|
*/
|
2007-09-25 11:55:57 +00:00
|
|
|
static int findString(const LINE *lp, const char *str, int len, int offset)
|
2006-05-04 20:56:43 +00:00
|
|
|
{
|
|
|
|
int left;
|
|
|
|
const char *cp, *ncp;
|
|
|
|
|
|
|
|
cp = &lp->data[offset];
|
|
|
|
left = lp->len - offset;
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
while (left >= len) {
|
2006-05-04 20:56:43 +00:00
|
|
|
ncp = memchr(cp, *str, left);
|
|
|
|
if (ncp == NULL)
|
|
|
|
return -1;
|
|
|
|
left -= (ncp - cp);
|
|
|
|
if (left < len)
|
|
|
|
return -1;
|
|
|
|
cp = ncp;
|
|
|
|
if (memcmp(cp, str, len) == 0)
|
|
|
|
return (cp - lp->data);
|
|
|
|
cp++;
|
|
|
|
left--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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(int num)
|
|
|
|
{
|
2006-11-30 20:57:50 +00:00
|
|
|
int len;
|
|
|
|
char buf[USERSIZE + 1];
|
2006-05-04 20:56:43 +00:00
|
|
|
|
2007-09-25 11:55:57 +00:00
|
|
|
while (1) {
|
|
|
|
/* Returns:
|
|
|
|
* -1 on read errors or EOF, or on bare Ctrl-D.
|
|
|
|
* 0 on ctrl-C,
|
|
|
|
* >0 length of input string, including terminating '\n'
|
|
|
|
*/
|
|
|
|
len = read_line_input("", buf, sizeof(buf), NULL);
|
|
|
|
if (len <= 0) {
|
|
|
|
/* Previously, ctrl-C was exiting to shell.
|
|
|
|
* Now we exit to ed prompt. Is in important? */
|
2006-05-04 20:56:43 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-09-25 11:55:57 +00:00
|
|
|
if ((buf[0] == '.') && (buf[1] == '\n') && (buf[2] == '\0'))
|
|
|
|
return;
|
2006-05-04 20:56:43 +00:00
|
|
|
if (!insertLine(num++, buf, len))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse a line number argument if it is present. This is a sum
|
|
|
|
* or difference of numbers, '.', '$', 'x, or a search string.
|
2006-05-19 12:38:30 +00:00
|
|
|
* Returns TRUE if successful (whether or not there was a number).
|
2006-05-04 20:56:43 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
2007-09-25 11:55:57 +00:00
|
|
|
static int getNum(const char **retcp, smallint *retHaveNum, int *retNum)
|
2006-05-04 20:56:43 +00:00
|
|
|
{
|
|
|
|
const char *cp;
|
|
|
|
char *endStr, str[USERSIZE];
|
2007-09-25 11:55:57 +00:00
|
|
|
int value, num;
|
|
|
|
smallint haveNum, minus;
|
2006-05-04 20:56:43 +00:00
|
|
|
|
|
|
|
cp = *retcp;
|
|
|
|
value = 0;
|
2007-09-25 11:55:57 +00:00
|
|
|
haveNum = FALSE;
|
|
|
|
minus = 0;
|
2006-05-04 20:56:43 +00:00
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
while (TRUE) {
|
2007-09-25 11:55:57 +00:00
|
|
|
cp = skip_blank(cp);
|
2006-05-04 20:56:43 +00:00
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
switch (*cp) {
|
2006-05-04 20:56:43 +00:00
|
|
|
case '.':
|
|
|
|
haveNum = TRUE;
|
|
|
|
num = curNum;
|
|
|
|
cp++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '$':
|
|
|
|
haveNum = TRUE;
|
|
|
|
num = lastNum;
|
|
|
|
cp++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\'':
|
|
|
|
cp++;
|
2006-11-30 20:57:50 +00:00
|
|
|
if ((*cp < 'a') || (*cp > 'z')) {
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("bad mark name");
|
2006-05-04 20:56:43 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
haveNum = TRUE;
|
|
|
|
num = marks[*cp++ - 'a'];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '/':
|
|
|
|
strcpy(str, ++cp);
|
|
|
|
endStr = strchr(str, '/');
|
2006-11-30 20:57:50 +00:00
|
|
|
if (endStr) {
|
2006-05-04 20:56:43 +00:00
|
|
|
*endStr++ = '\0';
|
|
|
|
cp += (endStr - str);
|
2007-09-25 11:55:57 +00:00
|
|
|
} else
|
2006-05-04 20:56:43 +00:00
|
|
|
cp = "";
|
|
|
|
num = searchLines(str, curNum, lastNum);
|
|
|
|
if (num == 0)
|
|
|
|
return FALSE;
|
|
|
|
haveNum = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2006-11-30 20:57:50 +00:00
|
|
|
if (!isdigit(*cp)) {
|
2006-05-04 20:56:43 +00:00
|
|
|
*retcp = cp;
|
|
|
|
*retHaveNum = haveNum;
|
|
|
|
*retNum = value;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
num = 0;
|
|
|
|
while (isdigit(*cp))
|
|
|
|
num = num * 10 + *cp++ - '0';
|
|
|
|
haveNum = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-09-25 11:55:57 +00:00
|
|
|
value += (minus ? -num : num);
|
2006-05-04 20:56:43 +00:00
|
|
|
|
2007-09-25 11:55:57 +00:00
|
|
|
cp = skip_blank(cp);
|
2006-05-04 20:56:43 +00:00
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
switch (*cp) {
|
2006-05-04 20:56:43 +00:00
|
|
|
case '-':
|
2007-09-25 11:55:57 +00:00
|
|
|
minus = 1;
|
2006-05-04 20:56:43 +00:00
|
|
|
cp++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '+':
|
2007-09-25 11:55:57 +00:00
|
|
|
minus = 0;
|
2006-05-04 20:56:43 +00:00
|
|
|
cp++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
*retcp = cp;
|
|
|
|
*retHaveNum = haveNum;
|
|
|
|
*retNum = value;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read lines from a file at the specified line number.
|
|
|
|
* Returns TRUE if the file was successfully read.
|
|
|
|
*/
|
2007-09-25 11:55:57 +00:00
|
|
|
static int readLines(const char *file, int num)
|
2006-05-04 20:56:43 +00:00
|
|
|
{
|
2006-11-30 20:57:50 +00:00
|
|
|
int fd, cc;
|
2006-05-04 20:56:43 +00:00
|
|
|
int len, lineCount, charCount;
|
|
|
|
char *cp;
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
if ((num < 1) || (num > lastNum + 1)) {
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("bad line for read");
|
2006-05-04 20:56:43 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(file, 0);
|
2006-11-30 20:57:50 +00:00
|
|
|
if (fd < 0) {
|
2006-05-04 20:56:43 +00:00
|
|
|
perror(file);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bufPtr = bufBase;
|
|
|
|
bufUsed = 0;
|
|
|
|
lineCount = 0;
|
|
|
|
charCount = 0;
|
|
|
|
cc = 0;
|
|
|
|
|
|
|
|
printf("\"%s\", ", file);
|
|
|
|
fflush(stdout);
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
do {
|
2006-05-04 20:56:43 +00:00
|
|
|
cp = memchr(bufPtr, '\n', bufUsed);
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
if (cp) {
|
2006-05-04 20:56:43 +00:00
|
|
|
len = (cp - bufPtr) + 1;
|
2006-11-30 20:57:50 +00:00
|
|
|
if (!insertLine(num, bufPtr, len)) {
|
2006-05-04 20:56:43 +00:00
|
|
|
close(fd);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
bufPtr += len;
|
|
|
|
bufUsed -= len;
|
|
|
|
charCount += len;
|
|
|
|
lineCount++;
|
|
|
|
num++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
if (bufPtr != bufBase) {
|
2006-05-04 20:56:43 +00:00
|
|
|
memcpy(bufBase, bufPtr, bufUsed);
|
|
|
|
bufPtr = bufBase + bufUsed;
|
|
|
|
}
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
if (bufUsed >= bufSize) {
|
2006-05-04 20:56:43 +00:00
|
|
|
len = (bufSize * 3) / 2;
|
|
|
|
cp = realloc(bufBase, len);
|
2006-11-30 20:57:50 +00:00
|
|
|
if (cp == NULL) {
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("no memory for buffer");
|
2006-05-04 20:56:43 +00:00
|
|
|
close(fd);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
bufBase = cp;
|
|
|
|
bufPtr = bufBase + bufUsed;
|
|
|
|
bufSize = len;
|
|
|
|
}
|
|
|
|
|
2007-09-25 11:55:57 +00:00
|
|
|
cc = safe_read(fd, bufPtr, bufSize - bufUsed);
|
2006-05-04 20:56:43 +00:00
|
|
|
bufUsed += cc;
|
|
|
|
bufPtr = bufBase;
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
} while (cc > 0);
|
2006-05-04 20:56:43 +00:00
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
if (cc < 0) {
|
2006-05-04 20:56:43 +00:00
|
|
|
perror(file);
|
|
|
|
close(fd);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
if (bufUsed) {
|
|
|
|
if (!insertLine(num, bufPtr, bufUsed)) {
|
2006-05-04 20:56:43 +00:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
lineCount++;
|
|
|
|
charCount += bufUsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
printf("%d lines%s, %d chars\n", lineCount,
|
|
|
|
(bufUsed ? " (incomplete)" : ""), charCount);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write the specified lines out to the specified file.
|
|
|
|
* Returns TRUE if successful, or FALSE on an error with a message output.
|
|
|
|
*/
|
2007-09-25 11:55:57 +00:00
|
|
|
static int writeLines(const char *file, int num1, int num2)
|
2006-05-04 20:56:43 +00:00
|
|
|
{
|
|
|
|
LINE *lp;
|
2006-11-30 20:57:50 +00:00
|
|
|
int fd, lineCount, charCount;
|
2006-05-04 20:56:43 +00:00
|
|
|
|
2007-09-25 11:55:57 +00:00
|
|
|
if (bad_nums(num1, num2, "write"))
|
2006-05-04 20:56:43 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
lineCount = 0;
|
|
|
|
charCount = 0;
|
|
|
|
|
|
|
|
fd = creat(file, 0666);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror(file);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\"%s\", ", file);
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
lp = findLine(num1);
|
2006-11-30 20:57:50 +00:00
|
|
|
if (lp == NULL) {
|
2006-05-04 20:56:43 +00:00
|
|
|
close(fd);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
while (num1++ <= num2) {
|
2007-09-25 11:55:57 +00:00
|
|
|
if (full_write(fd, lp->data, lp->len) != lp->len) {
|
2006-05-04 20:56:43 +00:00
|
|
|
perror(file);
|
|
|
|
close(fd);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
charCount += lp->len;
|
|
|
|
lineCount++;
|
|
|
|
lp = lp->next;
|
|
|
|
}
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
if (close(fd) < 0) {
|
2006-05-04 20:56:43 +00:00
|
|
|
perror(file);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%d lines, %d chars\n", lineCount, charCount);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 int printLines(int num1, int num2, int expandFlag)
|
|
|
|
{
|
|
|
|
const LINE *lp;
|
|
|
|
const char *cp;
|
|
|
|
int ch, count;
|
|
|
|
|
2007-09-25 11:55:57 +00:00
|
|
|
if (bad_nums(num1, num2, "print"))
|
2006-05-04 20:56:43 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
lp = findLine(num1);
|
|
|
|
if (lp == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
while (num1 <= num2) {
|
|
|
|
if (!expandFlag) {
|
2006-05-04 20:56:43 +00:00
|
|
|
write(1, lp->data, lp->len);
|
|
|
|
setCurNum(num1++);
|
|
|
|
lp = lp->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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--;
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
while (count-- > 0) {
|
2007-12-30 01:59:53 +00:00
|
|
|
ch = (unsigned char) *cp++;
|
|
|
|
fputc_printable(ch | PRINTABLE_META, stdout);
|
2006-05-04 20:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fputs("$\n", stdout);
|
|
|
|
|
|
|
|
setCurNum(num1++);
|
|
|
|
lp = lp->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2007-09-25 11:55:57 +00:00
|
|
|
static int insertLine(int num, const char *data, int len)
|
2006-05-04 20:56:43 +00:00
|
|
|
{
|
|
|
|
LINE *newLp, *lp;
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
if ((num < 1) || (num > lastNum + 1)) {
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("inserting at bad line number");
|
2006-05-04 20:56:43 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
newLp = malloc(sizeof(LINE) + len - 1);
|
|
|
|
if (newLp == NULL) {
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("failed to allocate memory for line");
|
2006-05-04 20:56:43 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(newLp->data, data, len);
|
|
|
|
newLp->len = len;
|
|
|
|
|
|
|
|
if (num > lastNum)
|
|
|
|
lp = &lines;
|
2006-11-30 20:57:50 +00:00
|
|
|
else {
|
2006-05-04 20:56:43 +00:00
|
|
|
lp = findLine(num);
|
2006-11-30 20:57:50 +00:00
|
|
|
if (lp == NULL) {
|
2006-05-04 20:56:43 +00:00
|
|
|
free((char *) newLp);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newLp->next = lp;
|
|
|
|
newLp->prev = lp->prev;
|
|
|
|
lp->prev->next = newLp;
|
|
|
|
lp->prev = newLp;
|
|
|
|
|
|
|
|
lastNum++;
|
|
|
|
dirty = TRUE;
|
|
|
|
return setCurNum(num);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete lines from the given range.
|
|
|
|
*/
|
2007-09-25 11:55:57 +00:00
|
|
|
static void deleteLines(int num1, int num2)
|
2006-05-04 20:56:43 +00:00
|
|
|
{
|
|
|
|
LINE *lp, *nlp, *plp;
|
|
|
|
int count;
|
|
|
|
|
2007-09-25 11:55:57 +00:00
|
|
|
if (bad_nums(num1, num2, "delete"))
|
|
|
|
return;
|
2006-05-04 20:56:43 +00:00
|
|
|
|
|
|
|
lp = findLine(num1);
|
|
|
|
if (lp == NULL)
|
2007-09-25 11:55:57 +00:00
|
|
|
return;
|
2006-05-04 20:56:43 +00:00
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
if ((curNum >= num1) && (curNum <= num2)) {
|
2006-05-04 20:56:43 +00:00
|
|
|
if (num2 < lastNum)
|
|
|
|
setCurNum(num2 + 1);
|
|
|
|
else if (num1 > 1)
|
|
|
|
setCurNum(num1 - 1);
|
|
|
|
else
|
|
|
|
curNum = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
count = num2 - num1 + 1;
|
|
|
|
if (curNum > num2)
|
|
|
|
curNum -= count;
|
|
|
|
lastNum -= count;
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
while (count-- > 0) {
|
2006-05-04 20:56:43 +00:00
|
|
|
nlp = lp->next;
|
|
|
|
plp = lp->prev;
|
|
|
|
plp->next = nlp;
|
|
|
|
nlp->prev = plp;
|
|
|
|
free(lp);
|
|
|
|
lp = nlp;
|
|
|
|
}
|
|
|
|
|
|
|
|
dirty = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search for a line which contains the specified string.
|
2007-09-25 11:55:57 +00:00
|
|
|
* If the string is "", then the previously searched for string
|
2006-05-04 20:56:43 +00:00
|
|
|
* 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 int searchLines(const char *str, int num1, int num2)
|
|
|
|
{
|
|
|
|
const LINE *lp;
|
|
|
|
int len;
|
|
|
|
|
2007-09-25 11:55:57 +00:00
|
|
|
if (bad_nums(num1, num2, "search"))
|
2006-05-04 20:56:43 +00:00
|
|
|
return 0;
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
if (*str == '\0') {
|
|
|
|
if (searchString[0] == '\0') {
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("no previous search string");
|
2006-05-04 20:56:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
str = searchString;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (str != searchString)
|
|
|
|
strcpy(searchString, str);
|
|
|
|
|
|
|
|
len = strlen(str);
|
|
|
|
|
|
|
|
lp = findLine(num1);
|
|
|
|
if (lp == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
while (num1 <= num2) {
|
2006-05-04 20:56:43 +00:00
|
|
|
if (findString(lp, str, len, 0) >= 0)
|
|
|
|
return num1;
|
|
|
|
num1++;
|
|
|
|
lp = lp->next;
|
|
|
|
}
|
|
|
|
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("cannot find string \"%s\"", str);
|
2006-05-04 20:56:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a pointer to the specified line number.
|
|
|
|
*/
|
|
|
|
static LINE *findLine(int num)
|
|
|
|
{
|
|
|
|
LINE *lp;
|
|
|
|
int lnum;
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
if ((num < 1) || (num > lastNum)) {
|
2006-10-27 09:02:31 +00:00
|
|
|
bb_error_msg("line number %d does not exist", num);
|
2006-05-04 20:56:43 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
if (curNum <= 0) {
|
2006-05-04 20:56:43 +00:00
|
|
|
curNum = 1;
|
|
|
|
curLine = lines.next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num == curNum)
|
|
|
|
return curLine;
|
|
|
|
|
|
|
|
lp = curLine;
|
|
|
|
lnum = curNum;
|
2006-11-30 20:57:50 +00:00
|
|
|
if (num < (curNum / 2)) {
|
2006-05-04 20:56:43 +00:00
|
|
|
lp = lines.next;
|
|
|
|
lnum = 1;
|
2007-09-25 11:55:57 +00:00
|
|
|
} else if (num > ((curNum + lastNum) / 2)) {
|
2006-05-04 20:56:43 +00:00
|
|
|
lp = lines.prev;
|
|
|
|
lnum = lastNum;
|
|
|
|
}
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
while (lnum < num) {
|
2006-05-04 20:56:43 +00:00
|
|
|
lp = lp->next;
|
|
|
|
lnum++;
|
|
|
|
}
|
|
|
|
|
2006-11-30 20:57:50 +00:00
|
|
|
while (lnum > num) {
|
2006-05-04 20:56:43 +00:00
|
|
|
lp = lp->prev;
|
|
|
|
lnum--;
|
|
|
|
}
|
|
|
|
return lp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the current line number.
|
|
|
|
* Returns TRUE if successful.
|
|
|
|
*/
|
|
|
|
static int setCurNum(int num)
|
|
|
|
{
|
|
|
|
LINE *lp;
|
|
|
|
|
|
|
|
lp = findLine(num);
|
|
|
|
if (lp == NULL)
|
|
|
|
return FALSE;
|
|
|
|
curNum = num;
|
|
|
|
curLine = lp;
|
|
|
|
return TRUE;
|
|
|
|
}
|