ed: large cleanup

function                                             old     new   delta
bad_nums                                               -      43     +43
skip_blank                                             -      24     +24
getNum                                               557     578     +21
insertLine                                           159     163      +4
setCurNum                                             31      33      +2
lastNum                                                4       -      -4
fileName                                               4       -      -4
dirty                                                  4       -      -4
curNum                                                 4       -      -4
curLine                                                4       -      -4
bufUsed                                                4       -      -4
bufSize                                                4       -      -4
bufPtr                                                 4       -      -4
bufBase                                                4       -      -4
printLines                                           357     345     -12
findLine                                             165     152     -13
lines                                                 16       -     -16
deleteLines                                          203     144     -59
readLines                                            538     473     -65
addLines                                             163      87     -76
marks                                                104       -    -104
termEdit                                             140       -    -140
ed_main                                             3125    2654    -471
------------------------------------------------------------------------------
(add/remove: 2/12 grow/shrink: 3/6 up/down: 94/-992)         Total: -898 bytes
   text    data     bss     dec     hex filename
 771142    1034   10564  782740   bf194 busybox_old
 770265    1034   10404  781703   bed87 busybox_unstripped

# size */ed.o
   text    data     bss     dec     hex filename
   6370       0     156    6526    197e editors.org/ed.o
   5505       0       0    5505    1581 editors/ed.o
This commit is contained in:
Denis Vlasenko 2007-09-25 11:55:57 +00:00
parent a545726d2b
commit d9391b15f1

View File

@ -9,6 +9,14 @@
#include "libbb.h" #include "libbb.h"
typedef struct LINE {
struct LINE *next;
struct LINE *prev;
int len;
char data[1];
} LINE;
#define searchString bb_common_bufsiz1 #define searchString bb_common_bufsiz1
enum { enum {
@ -17,64 +25,92 @@ enum {
INITBUF_SIZE = 1024, /* initial buffer size */ INITBUF_SIZE = 1024, /* initial buffer size */
}; };
typedef struct LINE { struct globals {
struct LINE *next; int curNum;
struct LINE *prev; int lastNum;
int len; int bufUsed;
char data[1]; int bufSize;
} LINE; 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 { \
PTR_TO_GLOBALS = xzalloc(sizeof(G)); \
} while (0)
static LINE lines, *curLine;
static int curNum, lastNum, marks[26], dirty;
static char *bufBase, *bufPtr, *fileName;
static int bufUsed, bufSize;
static void doCommands(void); static void doCommands(void);
static void subCommand(const char *cmd, int num1, int num2); static void subCommand(const char *cmd, int num1, int num2);
static int getNum(const char **retcp, int *retHaveNum, int *retNum); static int getNum(const char **retcp, smallint *retHaveNum, int *retNum);
static int setCurNum(int num); static int setCurNum(int num);
static int initEdit(void);
static void termEdit(void);
static void addLines(int num); static void addLines(int num);
static int insertLine(int num, const char *data, int len); static int insertLine(int num, const char *data, int len);
static int deleteLines(int num1, int num2); static void deleteLines(int num1, int num2);
static int printLines(int num1, int num2, int expandFlag); static int printLines(int num1, int num2, int expandFlag);
static int writeLines(const char *file, int num1, int num2); static int writeLines(const char *file, int num1, int num2);
static int readLines(const char *file, int num); static int readLines(const char *file, int num);
static int searchLines(const char *str, int num1, int num2); static int searchLines(const char *str, int num1, int num2);
static LINE *findLine(int num); static LINE *findLine(int num);
static int findString(const LINE *lp, const char * str, int len, int offset); static int findString(const LINE *lp, const char * str, int len, int offset);
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)
{
// NB: fix comment in skip_whitespace!
while (isblank(*cp))
cp++;
return (char *)cp;
}
int ed_main(int argc, char **argv); int ed_main(int argc, char **argv);
int ed_main(int argc, char **argv) int ed_main(int argc, char **argv)
{ {
if (!initEdit()) INIT_G();
return EXIT_FAILURE;
bufSize = INITBUF_SIZE;
bufBase = xmalloc(bufSize);
bufPtr = bufBase;
lines.next = &lines;
lines.prev = &lines;
if (argc > 1) { if (argc > 1) {
fileName = strdup(argv[1]); fileName = xstrdup(argv[1]);
if (fileName == NULL) {
bb_error_msg("no memory");
termEdit();
return EXIT_SUCCESS;
}
if (!readLines(fileName, 1)) { if (!readLines(fileName, 1)) {
termEdit();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
if (lastNum) if (lastNum)
setCurNum(1); setCurNum(1);
dirty = FALSE; dirty = FALSE;
} }
doCommands(); doCommands();
termEdit();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -85,42 +121,25 @@ static void doCommands(void)
{ {
const char *cp; const char *cp;
char *endbuf, *newname, buf[USERSIZE]; char *endbuf, *newname, buf[USERSIZE];
int len, num1, num2, have1, have2; int len, num1, num2;
smallint have1, have2;
while (TRUE) { while (TRUE) {
printf(": "); // NB: fix comment in lineedit.c!
fflush(stdout); /* Returns:
* -1 on read errors or EOF, or on bare Ctrl-D.
if (fgets(buf, sizeof(buf), stdin) == NULL) * 0 on ctrl-C,
* >0 length of input string, including terminating '\n'
*/
len = read_line_input(": ", buf, sizeof(buf), NULL);
if (len <= 0)
return; return;
len = strlen(buf);
if (len == 0)
return;
endbuf = &buf[len - 1]; endbuf = &buf[len - 1];
if (*endbuf != '\n') {
bb_error_msg("command line too long");
do {
len = fgetc(stdin);
} while ((len != EOF) && (len != '\n'));
continue;
}
while ((endbuf > buf) && isblank(endbuf[-1])) while ((endbuf > buf) && isblank(endbuf[-1]))
endbuf--; endbuf--;
*endbuf = '\0'; *endbuf = '\0';
cp = buf; cp = skip_blank(buf);
while (isblank(*cp))
cp++;
have1 = FALSE; have1 = FALSE;
have2 = FALSE; have2 = FALSE;
@ -132,28 +151,21 @@ static void doCommands(void)
if (!getNum(&cp, &have1, &num1)) if (!getNum(&cp, &have1, &num1))
continue; continue;
while (isblank(*cp)) cp = skip_blank(cp);
cp++;
if (*cp == ',') { if (*cp == ',') {
cp++; cp++;
if (!getNum(&cp, &have2, &num2)) if (!getNum(&cp, &have2, &num2))
continue; continue;
if (!have1) if (!have1)
num1 = 1; num1 = 1;
if (!have2) if (!have2)
num2 = lastNum; num2 = lastNum;
have1 = TRUE; have1 = TRUE;
have2 = TRUE; have2 = TRUE;
} }
if (!have1) if (!have1)
num1 = curNum; num1 = curNum;
if (!have2) if (!have2)
num2 = num1; num2 = num1;
@ -176,10 +188,7 @@ static void doCommands(void)
bb_error_msg("bad file command"); bb_error_msg("bad file command");
break; break;
} }
cp = skip_blank(cp);
while (isblank(*cp))
cp++;
if (*cp == '\0') { if (*cp == '\0') {
if (fileName) if (fileName)
printf("\"%s\"\n", fileName); printf("\"%s\"\n", fileName);
@ -187,17 +196,12 @@ static void doCommands(void)
printf("No file name\n"); printf("No file name\n");
break; break;
} }
newname = strdup(cp); newname = strdup(cp);
if (newname == NULL) { if (newname == NULL) {
bb_error_msg("no memory for file name"); bb_error_msg("no memory for file name");
break; break;
} }
if (fileName)
free(fileName); free(fileName);
fileName = newname; fileName = newname;
break; break;
@ -206,14 +210,11 @@ static void doCommands(void)
break; break;
case 'k': case 'k':
while (isblank(*cp)) cp = skip_blank(cp);
cp++; if ((*cp < 'a') || (*cp > 'z') || cp[1]) {
if ((*cp < 'a') || (*cp > 'a') || cp[1]) {
bb_error_msg("bad mark name"); bb_error_msg("bad mark name");
break; break;
} }
marks[*cp - 'a'] = num2; marks[*cp - 'a'] = num2;
break; break;
@ -226,30 +227,20 @@ static void doCommands(void)
break; break;
case 'q': case 'q':
while (isblank(*cp)) cp = skip_blank(cp);
cp++;
if (have1 || *cp) { if (have1 || *cp) {
bb_error_msg("bad quit command"); bb_error_msg("bad quit command");
break; break;
} }
if (!dirty) if (!dirty)
return; return;
len = read_line_input("Really quit? ", buf, 16, NULL);
printf("Really quit? "); /* read error/EOF - no way to continue */
fflush(stdout); if (len < 0)
return;
buf[0] = '\0'; cp = skip_blank(buf);
fgets(buf, sizeof(buf), stdin); if ((*cp | 0x20) == 'y') /* Y or y */
cp = buf;
while (isblank(*cp))
cp++;
if ((*cp == 'y') || (*cp == 'Y'))
return; return;
break; break;
case 'r': case 'r':
@ -257,24 +248,17 @@ static void doCommands(void)
bb_error_msg("bad read command"); bb_error_msg("bad read command");
break; break;
} }
cp = skip_blank(cp);
while (isblank(*cp))
cp++;
if (*cp == '\0') { if (*cp == '\0') {
bb_error_msg("no file name"); bb_error_msg("no file name");
break; break;
} }
if (!have1) if (!have1)
num1 = lastNum; num1 = lastNum;
if (readLines(cp, num1 + 1)) if (readLines(cp, num1 + 1))
break; break;
if (fileName == NULL) if (fileName == NULL)
fileName = strdup(cp); fileName = strdup(cp);
break; break;
case 's': case 's':
@ -286,23 +270,17 @@ static void doCommands(void)
bb_error_msg("bad write command"); bb_error_msg("bad write command");
break; break;
} }
cp = skip_blank(cp);
while (isblank(*cp))
cp++;
if (!have1) { if (!have1) {
num1 = 1; num1 = 1;
num2 = lastNum; num2 = lastNum;
} }
if (*cp == '\0') if (*cp == '\0')
cp = fileName; cp = fileName;
if (cp == NULL) { if (cp == NULL) {
bb_error_msg("no file name specified"); bb_error_msg("no file name specified");
break; break;
} }
writeLines(cp, num1, num2); writeLines(cp, num1, num2);
break; break;
@ -325,29 +303,24 @@ static void doCommands(void)
bb_error_msg("no arguments allowed"); bb_error_msg("no arguments allowed");
break; break;
} }
printLines(curNum, curNum, FALSE); printLines(curNum, curNum, FALSE);
break; break;
case '-': case '-':
if (setCurNum(curNum - 1)) if (setCurNum(curNum - 1))
printLines(curNum, curNum, FALSE); printLines(curNum, curNum, FALSE);
break; break;
case '=': case '=':
printf("%d\n", num1); printf("%d\n", num1);
break; break;
case '\0': case '\0':
if (have1) { if (have1) {
printLines(num2, num2, FALSE); printLines(num2, num2, FALSE);
break; break;
} }
if (setCurNum(curNum + 1)) if (setCurNum(curNum + 1))
printLines(curNum, curNum, FALSE); printLines(curNum, curNum, FALSE);
break; break;
default: default:
@ -369,10 +342,8 @@ static void subCommand(const char * cmd, int num1, int num2)
LINE *lp, *nlp; LINE *lp, *nlp;
int globalFlag, printFlag, didSub, needPrint; int globalFlag, printFlag, didSub, needPrint;
if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) { if (bad_nums(num1, num2, "substitute"))
bb_error_msg("bad line range for substitute");
return; return;
}
globalFlag = FALSE; globalFlag = FALSE;
printFlag = FALSE; printFlag = FALSE;
@ -394,7 +365,6 @@ static void subCommand(const char * cmd, int num1, int num2)
oldStr = cp; oldStr = cp;
cp = strchr(cp, delim); cp = strchr(cp, delim);
if (cp == NULL) { if (cp == NULL) {
bb_error_msg("missing 2nd delimiter for substitute"); bb_error_msg("missing 2nd delimiter for substitute");
return; return;
@ -414,11 +384,9 @@ static void subCommand(const char * cmd, int num1, int num2)
case 'g': case 'g':
globalFlag = TRUE; globalFlag = TRUE;
break; break;
case 'p': case 'p':
printFlag = TRUE; printFlag = TRUE;
break; break;
default: default:
bb_error_msg("unknown option for substitute"); bb_error_msg("unknown option for substitute");
return; return;
@ -429,7 +397,6 @@ static void subCommand(const char * cmd, int num1, int num2)
bb_error_msg("no previous search string"); bb_error_msg("no previous search string");
return; return;
} }
oldStr = searchString; oldStr = searchString;
} }
@ -437,7 +404,6 @@ static void subCommand(const char * cmd, int num1, int num2)
strcpy(searchString, oldStr); strcpy(searchString, oldStr);
lp = findLine(num1); lp = findLine(num1);
if (lp == NULL) if (lp == NULL)
return; return;
@ -455,11 +421,9 @@ static void subCommand(const char * cmd, int num1, int num2)
printLines(num1, num1, FALSE); printLines(num1, num1, FALSE);
needPrint = FALSE; needPrint = FALSE;
} }
offset = 0; offset = 0;
lp = lp->next; lp = lp->next;
num1++; num1++;
continue; continue;
} }
@ -473,7 +437,6 @@ static void subCommand(const char * cmd, int num1, int num2)
*/ */
if (deltaLen <= 0) { if (deltaLen <= 0) {
memcpy(&lp->data[offset], newStr, newLen); memcpy(&lp->data[offset], newStr, newLen);
if (deltaLen) { if (deltaLen) {
memcpy(&lp->data[offset + newLen], memcpy(&lp->data[offset + newLen],
&lp->data[offset + oldLen], &lp->data[offset + oldLen],
@ -481,20 +444,15 @@ static void subCommand(const char * cmd, int num1, int num2)
lp->len += deltaLen; lp->len += deltaLen;
} }
offset += newLen; offset += newLen;
if (globalFlag) if (globalFlag)
continue; continue;
if (needPrint) { if (needPrint) {
printLines(num1, num1, FALSE); printLines(num1, num1, FALSE);
needPrint = FALSE; needPrint = FALSE;
} }
lp = lp->next; lp = lp->next;
num1++; num1++;
continue; continue;
} }
@ -503,8 +461,7 @@ static void subCommand(const char * cmd, int num1, int num2)
* structure and use that. Link it in in place of * structure and use that. Link it in in place of
* the old line structure. * the old line structure.
*/ */
nlp = (LINE *) malloc(sizeof(LINE) + lp->len + deltaLen); nlp = malloc(sizeof(LINE) + lp->len + deltaLen);
if (nlp == NULL) { if (nlp == NULL) {
bb_error_msg("cannot get memory for line"); bb_error_msg("cannot get memory for line");
return; return;
@ -513,9 +470,7 @@ static void subCommand(const char * cmd, int num1, int num2)
nlp->len = lp->len + deltaLen; nlp->len = lp->len + deltaLen;
memcpy(nlp->data, lp->data, offset); memcpy(nlp->data, lp->data, offset);
memcpy(&nlp->data[offset], newStr, newLen); memcpy(&nlp->data[offset], newStr, newLen);
memcpy(&nlp->data[offset + newLen], memcpy(&nlp->data[offset + newLen],
&lp->data[offset + oldLen], &lp->data[offset + oldLen],
lp->len - offset - oldLen); lp->len - offset - oldLen);
@ -564,20 +519,14 @@ static int findString( const LINE * lp, const char * str, int len, int offset)
while (left >= len) { while (left >= len) {
ncp = memchr(cp, *str, left); ncp = memchr(cp, *str, left);
if (ncp == NULL) if (ncp == NULL)
return -1; return -1;
left -= (ncp - cp); left -= (ncp - cp);
if (left < len) if (left < len)
return -1; return -1;
cp = ncp; cp = ncp;
if (memcmp(cp, str, len) == 0) if (memcmp(cp, str, len) == 0)
return (cp - lp->data); return (cp - lp->data);
cp++; cp++;
left--; left--;
} }
@ -597,23 +546,20 @@ static void addLines(int num)
int len; int len;
char buf[USERSIZE + 1]; char buf[USERSIZE + 1];
while (fgets(buf, sizeof(buf), stdin)) { while (1) {
if ((buf[0] == '.') && (buf[1] == '\n') && (buf[2] == '\0')) /* Returns:
return; * -1 on read errors or EOF, or on bare Ctrl-D.
* 0 on ctrl-C,
len = strlen(buf); * >0 length of input string, including terminating '\n'
*/
if (len == 0) len = read_line_input("", buf, sizeof(buf), NULL);
return; if (len <= 0) {
/* Previously, ctrl-C was exiting to shell.
if (buf[len - 1] != '\n') { * Now we exit to ed prompt. Is in important? */
bb_error_msg("line too long");
do {
len = fgetc(stdin);
} while ((len != EOF) && (len != '\n'));
return; return;
} }
if ((buf[0] == '.') && (buf[1] == '\n') && (buf[2] == '\0'))
return;
if (!insertLine(num++, buf, len)) if (!insertLine(num++, buf, len))
return; return;
} }
@ -628,20 +574,20 @@ static void addLines(int num)
* Whether there was a number is returned indirectly, as is the number. * Whether there was a number is returned indirectly, as is the number.
* The character pointer which stopped the scan is also returned. * The character pointer which stopped the scan is also returned.
*/ */
static int getNum(const char **retcp, int *retHaveNum, int *retNum) static int getNum(const char **retcp, smallint *retHaveNum, int *retNum)
{ {
const char *cp; const char *cp;
char *endStr, str[USERSIZE]; char *endStr, str[USERSIZE];
int haveNum, value, num, sign; int value, num;
smallint haveNum, minus;
cp = *retcp; cp = *retcp;
haveNum = FALSE;
value = 0; value = 0;
sign = 1; haveNum = FALSE;
minus = 0;
while (TRUE) { while (TRUE) {
while (isblank(*cp)) cp = skip_blank(cp);
cp++;
switch (*cp) { switch (*cp) {
case '.': case '.':
@ -658,12 +604,10 @@ static int getNum(const char **retcp, int *retHaveNum, int *retNum)
case '\'': case '\'':
cp++; cp++;
if ((*cp < 'a') || (*cp > 'z')) { if ((*cp < 'a') || (*cp > 'z')) {
bb_error_msg("bad mark name"); bb_error_msg("bad mark name");
return FALSE; return FALSE;
} }
haveNum = TRUE; haveNum = TRUE;
num = marks[*cp++ - 'a']; num = marks[*cp++ - 'a'];
break; break;
@ -671,19 +615,14 @@ static int getNum(const char **retcp, int *retHaveNum, int *retNum)
case '/': case '/':
strcpy(str, ++cp); strcpy(str, ++cp);
endStr = strchr(str, '/'); endStr = strchr(str, '/');
if (endStr) { if (endStr) {
*endStr++ = '\0'; *endStr++ = '\0';
cp += (endStr - str); cp += (endStr - str);
} } else
else
cp = ""; cp = "";
num = searchLines(str, curNum, lastNum); num = searchLines(str, curNum, lastNum);
if (num == 0) if (num == 0)
return FALSE; return FALSE;
haveNum = TRUE; haveNum = TRUE;
break; break;
@ -694,29 +633,25 @@ static int getNum(const char **retcp, int *retHaveNum, int *retNum)
*retNum = value; *retNum = value;
return TRUE; return TRUE;
} }
num = 0; num = 0;
while (isdigit(*cp)) while (isdigit(*cp))
num = num * 10 + *cp++ - '0'; num = num * 10 + *cp++ - '0';
haveNum = TRUE; haveNum = TRUE;
break; break;
} }
value += num * sign; value += (minus ? -num : num);
while (isblank(*cp)) cp = skip_blank(cp);
cp++;
switch (*cp) { switch (*cp) {
case '-': case '-':
sign = -1; minus = 1;
cp++; cp++;
break; break;
case '+': case '+':
sign = 1; minus = 0;
cp++; cp++;
break; break;
@ -730,70 +665,6 @@ static int getNum(const char **retcp, int *retHaveNum, int *retNum)
} }
/*
* Initialize everything for editing.
*/
static int initEdit(void)
{
int i;
bufSize = INITBUF_SIZE;
bufBase = malloc(bufSize);
if (bufBase == NULL) {
bb_error_msg("no memory for buffer");
return FALSE;
}
bufPtr = bufBase;
bufUsed = 0;
lines.next = &lines;
lines.prev = &lines;
curLine = NULL;
curNum = 0;
lastNum = 0;
dirty = FALSE;
fileName = NULL;
searchString[0] = '\0';
for (i = 0; i < 26; i++)
marks[i] = 0;
return TRUE;
}
/*
* Finish editing.
*/
static void termEdit(void)
{
if (bufBase)
free(bufBase);
bufBase = NULL;
bufPtr = NULL;
bufSize = 0;
bufUsed = 0;
if (fileName)
free(fileName);
fileName = NULL;
searchString[0] = '\0';
if (lastNum)
deleteLines(1, lastNum);
lastNum = 0;
curNum = 0;
curLine = NULL;
}
/* /*
* Read lines from a file at the specified line number. * Read lines from a file at the specified line number.
* Returns TRUE if the file was successfully read. * Returns TRUE if the file was successfully read.
@ -810,7 +681,6 @@ static int readLines(const char * file, int num)
} }
fd = open(file, 0); fd = open(file, 0);
if (fd < 0) { if (fd < 0) {
perror(file); perror(file);
return FALSE; return FALSE;
@ -830,18 +700,15 @@ static int readLines(const char * file, int num)
if (cp) { if (cp) {
len = (cp - bufPtr) + 1; len = (cp - bufPtr) + 1;
if (!insertLine(num, bufPtr, len)) { if (!insertLine(num, bufPtr, len)) {
close(fd); close(fd);
return FALSE; return FALSE;
} }
bufPtr += len; bufPtr += len;
bufUsed -= len; bufUsed -= len;
charCount += len; charCount += len;
lineCount++; lineCount++;
num++; num++;
continue; continue;
} }
@ -853,19 +720,17 @@ static int readLines(const char * file, int num)
if (bufUsed >= bufSize) { if (bufUsed >= bufSize) {
len = (bufSize * 3) / 2; len = (bufSize * 3) / 2;
cp = realloc(bufBase, len); cp = realloc(bufBase, len);
if (cp == NULL) { if (cp == NULL) {
bb_error_msg("no memory for buffer"); bb_error_msg("no memory for buffer");
close(fd); close(fd);
return FALSE; return FALSE;
} }
bufBase = cp; bufBase = cp;
bufPtr = bufBase + bufUsed; bufPtr = bufBase + bufUsed;
bufSize = len; bufSize = len;
} }
cc = read(fd, bufPtr, bufSize - bufUsed); cc = safe_read(fd, bufPtr, bufSize - bufUsed);
bufUsed += cc; bufUsed += cc;
bufPtr = bufBase; bufPtr = bufBase;
@ -882,7 +747,6 @@ static int readLines(const char * file, int num)
close(fd); close(fd);
return -1; return -1;
} }
lineCount++; lineCount++;
charCount += bufUsed; charCount += bufUsed;
} }
@ -905,16 +769,13 @@ static int writeLines(const char * file, int num1, int num2)
LINE *lp; LINE *lp;
int fd, lineCount, charCount; int fd, lineCount, charCount;
if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) { if (bad_nums(num1, num2, "write"))
bb_error_msg("bad line range for write");
return FALSE; return FALSE;
}
lineCount = 0; lineCount = 0;
charCount = 0; charCount = 0;
fd = creat(file, 0666); fd = creat(file, 0666);
if (fd < 0) { if (fd < 0) {
perror(file); perror(file);
return FALSE; return FALSE;
@ -924,19 +785,17 @@ static int writeLines(const char * file, int num1, int num2)
fflush(stdout); fflush(stdout);
lp = findLine(num1); lp = findLine(num1);
if (lp == NULL) { if (lp == NULL) {
close(fd); close(fd);
return FALSE; return FALSE;
} }
while (num1++ <= num2) { while (num1++ <= num2) {
if (write(fd, lp->data, lp->len) != lp->len) { if (full_write(fd, lp->data, lp->len) != lp->len) {
perror(file); perror(file);
close(fd); close(fd);
return FALSE; return FALSE;
} }
charCount += lp->len; charCount += lp->len;
lineCount++; lineCount++;
lp = lp->next; lp = lp->next;
@ -964,13 +823,10 @@ static int printLines(int num1, int num2, int expandFlag)
const char *cp; const char *cp;
int ch, count; int ch, count;
if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) { if (bad_nums(num1, num2, "print"))
bb_error_msg("bad line range for print");
return FALSE; return FALSE;
}
lp = findLine(num1); lp = findLine(num1);
if (lp == NULL) if (lp == NULL)
return FALSE; return FALSE;
@ -979,7 +835,6 @@ static int printLines(int num1, int num2, int expandFlag)
write(1, lp->data, lp->len); write(1, lp->data, lp->len);
setCurNum(num1++); setCurNum(num1++);
lp = lp->next; lp = lp->next;
continue; continue;
} }
@ -995,22 +850,18 @@ static int printLines(int num1, int num2, int expandFlag)
while (count-- > 0) { while (count-- > 0) {
ch = *cp++; ch = *cp++;
if (ch & 0x80) { if (ch & 0x80) {
fputs("M-", stdout); fputs("M-", stdout);
ch &= 0x7f; ch &= 0x7f;
} }
if (ch < ' ') { if (ch < ' ') {
fputc('^', stdout); fputc('^', stdout);
ch += '@'; ch += '@';
} }
if (ch == 0x7f) { if (ch == 0x7f) {
fputc('^', stdout); fputc('^', stdout);
ch = '?'; ch = '?';
} }
fputc(ch, stdout); fputc(ch, stdout);
} }
@ -1041,7 +892,6 @@ static int insertLine(int num, const char * data, int len)
} }
newLp = malloc(sizeof(LINE) + len - 1); newLp = malloc(sizeof(LINE) + len - 1);
if (newLp == NULL) { if (newLp == NULL) {
bb_error_msg("failed to allocate memory for line"); bb_error_msg("failed to allocate memory for line");
return FALSE; return FALSE;
@ -1054,7 +904,6 @@ static int insertLine(int num, const char * data, int len)
lp = &lines; lp = &lines;
else { else {
lp = findLine(num); lp = findLine(num);
if (lp == NULL) { if (lp == NULL) {
free((char *) newLp); free((char *) newLp);
return FALSE; return FALSE;
@ -1075,20 +924,17 @@ static int insertLine(int num, const char * data, int len)
/* /*
* Delete lines from the given range. * Delete lines from the given range.
*/ */
static int deleteLines(int num1, int num2) static void deleteLines(int num1, int num2)
{ {
LINE *lp, *nlp, *plp; LINE *lp, *nlp, *plp;
int count; int count;
if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) { if (bad_nums(num1, num2, "delete"))
bb_error_msg("bad line numbers for delete"); return;
return FALSE;
}
lp = findLine(num1); lp = findLine(num1);
if (lp == NULL) if (lp == NULL)
return FALSE; return;
if ((curNum >= num1) && (curNum <= num2)) { if ((curNum >= num1) && (curNum <= num2)) {
if (num2 < lastNum) if (num2 < lastNum)
@ -1100,10 +946,8 @@ static int deleteLines(int num1, int num2)
} }
count = num2 - num1 + 1; count = num2 - num1 + 1;
if (curNum > num2) if (curNum > num2)
curNum -= count; curNum -= count;
lastNum -= count; lastNum -= count;
while (count-- > 0) { while (count-- > 0) {
@ -1111,22 +955,17 @@ static int deleteLines(int num1, int num2)
plp = lp->prev; plp = lp->prev;
plp->next = nlp; plp->next = nlp;
nlp->prev = plp; nlp->prev = plp;
lp->next = NULL;
lp->prev = NULL;
lp->len = 0;
free(lp); free(lp);
lp = nlp; lp = nlp;
} }
dirty = TRUE; dirty = TRUE;
return TRUE;
} }
/* /*
* Search for a line which contains the specified string. * Search for a line which contains the specified string.
* If the string is NULL, then the previously searched for string * If the string is "", then the previously searched for string
* is used. The currently searched for string is saved for future use. * 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 * Returns the line number which matches, or 0 if there was no match
* with an error printed. * with an error printed.
@ -1136,17 +975,14 @@ static int searchLines(const char *str, int num1, int num2)
const LINE *lp; const LINE *lp;
int len; int len;
if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) { if (bad_nums(num1, num2, "search"))
bb_error_msg("bad line numbers for search");
return 0; return 0;
}
if (*str == '\0') { if (*str == '\0') {
if (searchString[0] == '\0') { if (searchString[0] == '\0') {
bb_error_msg("no previous search string"); bb_error_msg("no previous search string");
return 0; return 0;
} }
str = searchString; str = searchString;
} }
@ -1156,14 +992,12 @@ static int searchLines(const char *str, int num1, int num2)
len = strlen(str); len = strlen(str);
lp = findLine(num1); lp = findLine(num1);
if (lp == NULL) if (lp == NULL)
return 0; return 0;
while (num1 <= num2) { while (num1 <= num2) {
if (findString(lp, str, len, 0) >= 0) if (findString(lp, str, len, 0) >= 0)
return num1; return num1;
num1++; num1++;
lp = lp->next; lp = lp->next;
} }
@ -1196,12 +1030,10 @@ static LINE *findLine(int num)
lp = curLine; lp = curLine;
lnum = curNum; lnum = curNum;
if (num < (curNum / 2)) { if (num < (curNum / 2)) {
lp = lines.next; lp = lines.next;
lnum = 1; lnum = 1;
} } else if (num > ((curNum + lastNum) / 2)) {
else if (num > ((curNum + lastNum) / 2)) {
lp = lines.prev; lp = lines.prev;
lnum = lastNum; lnum = lastNum;
} }
@ -1228,10 +1060,8 @@ static int setCurNum(int num)
LINE *lp; LINE *lp;
lp = findLine(num); lp = findLine(num);
if (lp == NULL) if (lp == NULL)
return FALSE; return FALSE;
curNum = num; curNum = num;
curLine = lp; curLine = lp;
return TRUE; return TRUE;