2006-05-19 18:08:30 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-05-05 02:26:43 +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.
|
|
|
|
*
|
|
|
|
* The "ed" built-in command (much simplified)
|
|
|
|
*/
|
2013-11-13 17:15:33 +05:30
|
|
|
//config:config ED
|
2018-12-28 07:50:17 +05:30
|
|
|
//config: bool "ed (21 kb)"
|
2013-11-13 17:15:33 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: The original 1970's Unix text editor, from the days of teletypes.
|
|
|
|
//config: Small, simple, evil. Part of SUSv3. If you're not already using
|
|
|
|
//config: this, you don't need it.
|
2013-11-13 17:15:33 +05:30
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_ED) += ed.o
|
|
|
|
|
|
|
|
//applet:IF_ED(APPLET(ed, BB_DIR_BIN, BB_SUID_DROP))
|
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
//usage:#define ed_trivial_usage "[FILE]"
|
2011-04-02 02:26:30 +05:30
|
|
|
//usage:#define ed_full_usage ""
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2016-04-21 19:56:30 +05:30
|
|
|
#include "common_bufsiz.h"
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2007-09-25 17:25:57 +05:30
|
|
|
typedef struct LINE {
|
|
|
|
struct LINE *next;
|
|
|
|
struct LINE *prev;
|
|
|
|
int len;
|
|
|
|
char data[1];
|
|
|
|
} LINE;
|
|
|
|
|
2016-04-21 22:08:51 +05:30
|
|
|
#define searchString bb_common_bufsiz1
|
2007-06-04 15:46:52 +05:30
|
|
|
|
2007-03-25 04:12:29 +05:30
|
|
|
enum {
|
2016-04-21 22:08:51 +05:30
|
|
|
USERSIZE = COMMON_BUFSIZE > 1024 ? 1024
|
|
|
|
: COMMON_BUFSIZE - 1, /* max line length typed in by user */
|
2007-03-25 04:12:29 +05:30
|
|
|
INITBUF_SIZE = 1024, /* initial buffer size */
|
|
|
|
};
|
|
|
|
|
2007-09-25 17:25:57 +05:30
|
|
|
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 { \
|
2016-04-21 22:08:51 +05:30
|
|
|
setup_common_bufsiz(); \
|
2008-02-28 00:11:59 +05:30
|
|
|
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
|
2007-09-25 17:25:57 +05:30
|
|
|
} while (0)
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2007-09-25 17:25:57 +05:30
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
/*
|
|
|
|
* Return a pointer to the specified line number.
|
|
|
|
*/
|
|
|
|
static LINE *findLine(int num)
|
2006-05-05 02:26:43 +05:30
|
|
|
{
|
2017-07-27 14:47:15 +05:30
|
|
|
LINE *lp;
|
|
|
|
int lnum;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if ((num < 1) || (num > lastNum)) {
|
|
|
|
bb_error_msg("line number %d does not exist", num);
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if (curNum <= 0) {
|
|
|
|
curNum = 1;
|
|
|
|
curLine = lines.next;
|
2006-05-05 02:26:43 +05:30
|
|
|
}
|
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if (num == curNum)
|
|
|
|
return curLine;
|
|
|
|
|
|
|
|
lp = curLine;
|
|
|
|
lnum = curNum;
|
|
|
|
if (num < (curNum / 2)) {
|
|
|
|
lp = lines.next;
|
|
|
|
lnum = 1;
|
|
|
|
} else if (num > ((curNum + lastNum) / 2)) {
|
|
|
|
lp = lines.prev;
|
|
|
|
lnum = lastNum;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (lnum < num) {
|
|
|
|
lp = lp->next;
|
|
|
|
lnum++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (lnum > num) {
|
|
|
|
lp = lp->prev;
|
|
|
|
lnum--;
|
|
|
|
}
|
|
|
|
return lp;
|
2006-05-05 02:26:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-07-27 14:47:15 +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.
|
2006-05-05 02:26:43 +05:30
|
|
|
*/
|
2017-07-27 14:47:15 +05:30
|
|
|
static int findString(const LINE *lp, const char *str, int len, int offset)
|
2006-05-05 02:26:43 +05:30
|
|
|
{
|
2017-07-27 14:47:15 +05:30
|
|
|
int left;
|
|
|
|
const char *cp, *ncp;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
cp = &lp->data[offset];
|
2017-07-27 15:28:25 +05:30
|
|
|
left = lp->len - offset - len;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 15:28:25 +05:30
|
|
|
while (left >= 0) {
|
|
|
|
ncp = memchr(cp, str[0], left + 1);
|
2017-07-27 14:47:15 +05:30
|
|
|
if (ncp == NULL)
|
|
|
|
return -1;
|
|
|
|
left -= (ncp - cp);
|
|
|
|
cp = ncp;
|
|
|
|
if (memcmp(cp, str, len) == 0)
|
|
|
|
return (cp - lp->data);
|
|
|
|
cp++;
|
|
|
|
left--;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
/*
|
|
|
|
* Search for a line which contains the specified string.
|
|
|
|
* If the string is "", 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 NOINLINE int searchLines(const char *str, int num1, int num2)
|
|
|
|
{
|
|
|
|
const LINE *lp;
|
|
|
|
int len;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if (bad_nums(num1, num2, "search"))
|
|
|
|
return 0;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if (*str == '\0') {
|
|
|
|
if (searchString[0] == '\0') {
|
|
|
|
bb_error_msg("no previous search string");
|
|
|
|
return 0;
|
2006-05-05 02:26:43 +05:30
|
|
|
}
|
2017-07-27 14:47:15 +05:30
|
|
|
str = searchString;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if (str != searchString)
|
|
|
|
strcpy(searchString, str);
|
2008-03-28 07:54:59 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
len = strlen(str);
|
2008-03-28 07:54:59 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
lp = findLine(num1);
|
|
|
|
if (lp == NULL)
|
|
|
|
return 0;
|
2008-03-28 07:54:59 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
while (num1 <= num2) {
|
|
|
|
if (findString(lp, str, len, 0) >= 0)
|
|
|
|
return num1;
|
|
|
|
num1++;
|
|
|
|
lp = lp->next;
|
|
|
|
}
|
2008-03-28 07:54:59 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
bb_error_msg("can't find string \"%s\"", str);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-03-28 07:54:59 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
/*
|
|
|
|
* Parse a line number argument if it is present. This is a sum
|
2017-07-27 16:04:56 +05:30
|
|
|
* or difference of numbers, ".", "$", "'c", or a search string.
|
|
|
|
* Returns pointer which stopped the scan if successful
|
|
|
|
* (whether or not there was a number).
|
2017-07-27 15:28:25 +05:30
|
|
|
* Returns NULL if there was a parsing error, with a message output.
|
2017-07-27 14:47:15 +05:30
|
|
|
* Whether there was a number is returned indirectly, as is the number.
|
|
|
|
*/
|
2017-07-27 15:28:25 +05:30
|
|
|
static const char* getNum(const char *cp, smallint *retHaveNum, int *retNum)
|
2017-07-27 14:47:15 +05:30
|
|
|
{
|
|
|
|
char *endStr, str[USERSIZE];
|
|
|
|
int value, num;
|
|
|
|
smallint haveNum, minus;
|
2008-03-28 07:54:59 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
value = 0;
|
|
|
|
haveNum = FALSE;
|
|
|
|
minus = 0;
|
2008-03-28 07:54:59 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
while (TRUE) {
|
2017-07-27 15:28:25 +05:30
|
|
|
cp = skip_whitespace(cp);
|
2008-03-28 07:54:59 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
switch (*cp) {
|
|
|
|
case '.':
|
|
|
|
haveNum = TRUE;
|
|
|
|
num = curNum;
|
|
|
|
cp++;
|
2006-05-05 02:26:43 +05:30
|
|
|
break;
|
2008-03-28 07:54:59 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
case '$':
|
|
|
|
haveNum = TRUE;
|
|
|
|
num = lastNum;
|
|
|
|
cp++;
|
2006-05-05 02:26:43 +05:30
|
|
|
break;
|
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
case '\'':
|
|
|
|
cp++;
|
2017-07-27 16:04:56 +05:30
|
|
|
if ((unsigned)(*cp - 'a') >= 26) {
|
2017-07-27 14:47:15 +05:30
|
|
|
bb_error_msg("bad mark name");
|
2017-07-27 15:28:25 +05:30
|
|
|
return NULL;
|
2017-07-27 14:47:15 +05:30
|
|
|
}
|
|
|
|
haveNum = TRUE;
|
2017-07-27 16:04:56 +05:30
|
|
|
num = marks[(unsigned)(*cp - 'a')];
|
|
|
|
cp++;
|
2006-05-05 02:26:43 +05:30
|
|
|
break;
|
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
case '/':
|
|
|
|
strcpy(str, ++cp);
|
|
|
|
endStr = strchr(str, '/');
|
|
|
|
if (endStr) {
|
|
|
|
*endStr++ = '\0';
|
|
|
|
cp += (endStr - str);
|
|
|
|
} else
|
|
|
|
cp = "";
|
|
|
|
num = searchLines(str, curNum, lastNum);
|
|
|
|
if (num == 0)
|
2017-07-27 15:28:25 +05:30
|
|
|
return NULL;
|
2017-07-27 14:47:15 +05:30
|
|
|
haveNum = TRUE;
|
2006-05-05 02:26:43 +05:30
|
|
|
break;
|
2017-07-27 14:47:15 +05:30
|
|
|
|
2008-03-28 07:54:59 +05:30
|
|
|
default:
|
2017-07-27 14:47:15 +05:30
|
|
|
if (!isdigit(*cp)) {
|
|
|
|
*retHaveNum = haveNum;
|
|
|
|
*retNum = value;
|
2017-07-27 15:28:25 +05:30
|
|
|
return cp;
|
2017-07-27 14:47:15 +05:30
|
|
|
}
|
|
|
|
num = 0;
|
|
|
|
while (isdigit(*cp))
|
|
|
|
num = num * 10 + *cp++ - '0';
|
|
|
|
haveNum = TRUE;
|
2006-05-05 02:26:43 +05:30
|
|
|
break;
|
2017-07-27 14:47:15 +05:30
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
value += (minus ? -num : num);
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 15:28:25 +05:30
|
|
|
cp = skip_whitespace(cp);
|
2008-03-28 07:54:59 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
switch (*cp) {
|
|
|
|
case '-':
|
|
|
|
minus = 1;
|
|
|
|
cp++;
|
2006-05-05 02:26:43 +05:30
|
|
|
break;
|
2008-03-28 07:54:59 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
case '+':
|
|
|
|
minus = 0;
|
|
|
|
cp++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
*retHaveNum = haveNum;
|
|
|
|
*retNum = value;
|
2017-07-27 15:28:25 +05:30
|
|
|
return cp;
|
2006-05-05 02:26:43 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-07-27 14:47:15 +05:30
|
|
|
* Set the current line number.
|
|
|
|
* Returns TRUE if successful.
|
2006-05-05 02:26:43 +05:30
|
|
|
*/
|
2017-07-27 14:47:15 +05:30
|
|
|
static int setCurNum(int num)
|
2006-05-05 02:26:43 +05:30
|
|
|
{
|
2017-07-27 14:47:15 +05:30
|
|
|
LINE *lp;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
lp = findLine(num);
|
|
|
|
if (lp == NULL)
|
|
|
|
return FALSE;
|
|
|
|
curNum = num;
|
|
|
|
curLine = lp;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +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 int insertLine(int num, const char *data, int len)
|
|
|
|
{
|
|
|
|
LINE *newLp, *lp;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if ((num < 1) || (num > lastNum + 1)) {
|
|
|
|
bb_error_msg("inserting at bad line number");
|
|
|
|
return FALSE;
|
2006-05-05 02:26:43 +05:30
|
|
|
}
|
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
newLp = xmalloc(sizeof(LINE) + len - 1);
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
memcpy(newLp->data, data, len);
|
|
|
|
newLp->len = len;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if (num > lastNum)
|
|
|
|
lp = &lines;
|
|
|
|
else {
|
|
|
|
lp = findLine(num);
|
|
|
|
if (lp == NULL) {
|
|
|
|
free((char *) newLp);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
newLp->next = lp;
|
|
|
|
newLp->prev = lp->prev;
|
|
|
|
lp->prev->next = newLp;
|
|
|
|
lp->prev = newLp;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
lastNum++;
|
|
|
|
dirty = TRUE;
|
|
|
|
return setCurNum(num);
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +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(int num)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
char buf[USERSIZE + 1];
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
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'
|
|
|
|
*/
|
2017-08-02 20:57:28 +05:30
|
|
|
len = read_line_input(NULL, "", buf, sizeof(buf));
|
2017-07-27 14:47:15 +05:30
|
|
|
if (len <= 0) {
|
|
|
|
/* Previously, ctrl-C was exiting to shell.
|
|
|
|
* Now we exit to ed prompt. Is in important? */
|
2006-05-05 02:26:43 +05:30
|
|
|
return;
|
|
|
|
}
|
2017-07-27 16:04:56 +05:30
|
|
|
if (buf[0] == '.' && buf[1] == '\n' && buf[2] == '\0')
|
2017-07-27 14:47:15 +05:30
|
|
|
return;
|
|
|
|
if (!insertLine(num++, buf, len))
|
|
|
|
return;
|
2006-05-05 02:26:43 +05:30
|
|
|
}
|
2017-07-27 14:47:15 +05:30
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
/*
|
|
|
|
* Read lines from a file at the specified line number.
|
|
|
|
* Returns TRUE if the file was successfully read.
|
|
|
|
*/
|
|
|
|
static int readLines(const char *file, int num)
|
|
|
|
{
|
|
|
|
int fd, cc;
|
|
|
|
int len, lineCount, charCount;
|
|
|
|
char *cp;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if ((num < 1) || (num > lastNum + 1)) {
|
|
|
|
bb_error_msg("bad line for read");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
fd = open(file, 0);
|
|
|
|
if (fd < 0) {
|
|
|
|
bb_simple_perror_msg(file);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
bufPtr = bufBase;
|
|
|
|
bufUsed = 0;
|
|
|
|
lineCount = 0;
|
|
|
|
charCount = 0;
|
|
|
|
cc = 0;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
printf("\"%s\", ", file);
|
|
|
|
fflush_all();
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
do {
|
|
|
|
cp = memchr(bufPtr, '\n', bufUsed);
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if (cp) {
|
|
|
|
len = (cp - bufPtr) + 1;
|
|
|
|
if (!insertLine(num, bufPtr, len)) {
|
|
|
|
close(fd);
|
|
|
|
return FALSE;
|
2006-05-05 02:26:43 +05:30
|
|
|
}
|
2017-07-27 14:47:15 +05:30
|
|
|
bufPtr += len;
|
|
|
|
bufUsed -= len;
|
|
|
|
charCount += len;
|
|
|
|
lineCount++;
|
|
|
|
num++;
|
2006-05-05 02:26:43 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if (bufPtr != bufBase) {
|
|
|
|
memcpy(bufBase, bufPtr, bufUsed);
|
|
|
|
bufPtr = bufBase + bufUsed;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if (bufUsed >= bufSize) {
|
|
|
|
len = (bufSize * 3) / 2;
|
|
|
|
cp = xrealloc(bufBase, len);
|
|
|
|
bufBase = cp;
|
|
|
|
bufPtr = bufBase + bufUsed;
|
|
|
|
bufSize = len;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
cc = safe_read(fd, bufPtr, bufSize - bufUsed);
|
|
|
|
bufUsed += cc;
|
|
|
|
bufPtr = bufBase;
|
|
|
|
} while (cc > 0);
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if (cc < 0) {
|
|
|
|
bb_simple_perror_msg(file);
|
|
|
|
close(fd);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if (bufUsed) {
|
|
|
|
if (!insertLine(num, bufPtr, bufUsed)) {
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
2006-05-05 02:26:43 +05:30
|
|
|
}
|
2017-07-27 14:47:15 +05:30
|
|
|
lineCount++;
|
|
|
|
charCount += bufUsed;
|
2006-05-05 02:26:43 +05:30
|
|
|
}
|
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
close(fd);
|
|
|
|
|
|
|
|
printf("%d lines%s, %d chars\n", lineCount,
|
|
|
|
(bufUsed ? " (incomplete)" : ""), charCount);
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
return TRUE;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
|
|
|
/*
|
2017-07-27 14:47:15 +05:30
|
|
|
* Write the specified lines out to the specified file.
|
|
|
|
* Returns TRUE if successful, or FALSE on an error with a message output.
|
2006-05-05 02:26:43 +05:30
|
|
|
*/
|
2017-07-27 14:47:15 +05:30
|
|
|
static int writeLines(const char *file, int num1, int num2)
|
2006-05-05 02:26:43 +05:30
|
|
|
{
|
2017-07-27 14:47:15 +05:30
|
|
|
LINE *lp;
|
|
|
|
int fd, lineCount, charCount;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if (bad_nums(num1, num2, "write"))
|
|
|
|
return FALSE;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
lineCount = 0;
|
|
|
|
charCount = 0;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
fd = creat(file, 0666);
|
|
|
|
if (fd < 0) {
|
|
|
|
bb_simple_perror_msg(file);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
printf("\"%s\", ", file);
|
|
|
|
fflush_all();
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
lp = findLine(num1);
|
|
|
|
if (lp == NULL) {
|
|
|
|
close(fd);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
while (num1++ <= num2) {
|
|
|
|
if (full_write(fd, lp->data, lp->len) != lp->len) {
|
|
|
|
bb_simple_perror_msg(file);
|
|
|
|
close(fd);
|
|
|
|
return FALSE;
|
2006-05-05 02:26:43 +05:30
|
|
|
}
|
2017-07-27 14:47:15 +05:30
|
|
|
charCount += lp->len;
|
|
|
|
lineCount++;
|
|
|
|
lp = lp->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (close(fd) < 0) {
|
|
|
|
bb_simple_perror_msg(file);
|
|
|
|
return FALSE;
|
2006-05-05 02:26:43 +05:30
|
|
|
}
|
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
printf("%d lines, %d chars\n", lineCount, charCount);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
|
|
|
/*
|
2017-07-27 14:47:15 +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.
|
2006-05-05 02:26:43 +05:30
|
|
|
*/
|
2017-07-27 14:47:15 +05:30
|
|
|
static int printLines(int num1, int num2, int expandFlag)
|
2006-05-05 02:26:43 +05:30
|
|
|
{
|
2017-07-27 14:47:15 +05:30
|
|
|
const LINE *lp;
|
2006-05-05 02:26:43 +05:30
|
|
|
const char *cp;
|
2017-07-27 14:47:15 +05:30
|
|
|
int ch, count;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if (bad_nums(num1, num2, "print"))
|
|
|
|
return FALSE;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
lp = findLine(num1);
|
|
|
|
if (lp == NULL)
|
|
|
|
return FALSE;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
while (num1 <= num2) {
|
|
|
|
if (!expandFlag) {
|
|
|
|
write(STDOUT_FILENO, lp->data, lp->len);
|
|
|
|
setCurNum(num1++);
|
|
|
|
lp = lp->next;
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
/*
|
|
|
|
* Show control characters and characters with the
|
|
|
|
* high bit set specially.
|
|
|
|
*/
|
|
|
|
cp = lp->data;
|
|
|
|
count = lp->len;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if ((count > 0) && (cp[count - 1] == '\n'))
|
|
|
|
count--;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
while (count-- > 0) {
|
|
|
|
ch = (unsigned char) *cp++;
|
|
|
|
fputc_printable(ch | PRINTABLE_META, stdout);
|
2006-05-05 02:26:43 +05:30
|
|
|
}
|
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
fputs("$\n", stdout);
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
setCurNum(num1++);
|
|
|
|
lp = lp->next;
|
2006-05-05 02:26:43 +05:30
|
|
|
}
|
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
return TRUE;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
|
|
|
/*
|
2017-07-27 14:47:15 +05:30
|
|
|
* Delete lines from the given range.
|
2006-05-05 02:26:43 +05:30
|
|
|
*/
|
2017-07-27 14:47:15 +05:30
|
|
|
static void deleteLines(int num1, int num2)
|
2006-05-05 02:26:43 +05:30
|
|
|
{
|
2017-07-27 14:47:15 +05:30
|
|
|
LINE *lp, *nlp, *plp;
|
|
|
|
int count;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if (bad_nums(num1, num2, "delete"))
|
|
|
|
return;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
lp = findLine(num1);
|
|
|
|
if (lp == NULL)
|
|
|
|
return;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if ((curNum >= num1) && (curNum <= num2)) {
|
|
|
|
if (num2 < lastNum)
|
|
|
|
setCurNum(num2 + 1);
|
|
|
|
else if (num1 > 1)
|
|
|
|
setCurNum(num1 - 1);
|
|
|
|
else
|
|
|
|
curNum = 0;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
count = num2 - num1 + 1;
|
|
|
|
if (curNum > num2)
|
|
|
|
curNum -= count;
|
|
|
|
lastNum -= count;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
while (count-- > 0) {
|
|
|
|
nlp = lp->next;
|
|
|
|
plp = lp->prev;
|
|
|
|
plp->next = nlp;
|
|
|
|
nlp->prev = plp;
|
|
|
|
free(lp);
|
|
|
|
lp = nlp;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
dirty = TRUE;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
/*
|
|
|
|
* Do the substitute command.
|
|
|
|
* The current line is set to the last substitution done.
|
|
|
|
*/
|
|
|
|
static void subCommand(const char *cmd, int num1, int num2)
|
|
|
|
{
|
|
|
|
char *cp, *oldStr, *newStr, buf[USERSIZE];
|
|
|
|
int delim, oldLen, newLen, deltaLen, offset;
|
|
|
|
LINE *lp, *nlp;
|
|
|
|
int globalFlag, printFlag, didSub, needPrint;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if (bad_nums(num1, num2, "substitute"))
|
|
|
|
return;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
globalFlag = FALSE;
|
|
|
|
printFlag = FALSE;
|
|
|
|
didSub = FALSE;
|
|
|
|
needPrint = FALSE;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
/*
|
|
|
|
* Copy the command so we can modify it.
|
|
|
|
*/
|
|
|
|
strcpy(buf, cmd);
|
|
|
|
cp = buf;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if (isblank(*cp) || (*cp == '\0')) {
|
|
|
|
bb_error_msg("bad delimiter for substitute");
|
|
|
|
return;
|
2006-05-05 02:26:43 +05:30
|
|
|
}
|
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
delim = *cp++;
|
|
|
|
oldStr = cp;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
cp = strchr(cp, delim);
|
|
|
|
if (cp == NULL) {
|
|
|
|
bb_error_msg("missing 2nd delimiter for substitute");
|
|
|
|
return;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
*cp++ = '\0';
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
newStr = cp;
|
|
|
|
cp = strchr(cp, delim);
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if (cp)
|
|
|
|
*cp++ = '\0';
|
|
|
|
else
|
|
|
|
cp = (char*)"";
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
while (*cp) switch (*cp++) {
|
|
|
|
case 'g':
|
|
|
|
globalFlag = TRUE;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
printFlag = TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
bb_error_msg("unknown option for substitute");
|
|
|
|
return;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if (*oldStr == '\0') {
|
|
|
|
if (searchString[0] == '\0') {
|
|
|
|
bb_error_msg("no previous search string");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
oldStr = searchString;
|
2006-05-05 02:26:43 +05:30
|
|
|
}
|
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if (oldStr != searchString)
|
|
|
|
strcpy(searchString, oldStr);
|
2006-05-05 02:26:43 +05:30
|
|
|
|
|
|
|
lp = findLine(num1);
|
2017-07-27 14:47:15 +05:30
|
|
|
if (lp == NULL)
|
|
|
|
return;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
oldLen = strlen(oldStr);
|
|
|
|
newLen = strlen(newStr);
|
|
|
|
deltaLen = newLen - oldLen;
|
|
|
|
offset = 0;
|
|
|
|
nlp = NULL;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2006-12-01 02:27:50 +05:30
|
|
|
while (num1 <= num2) {
|
2017-07-27 14:47:15 +05:30
|
|
|
offset = findString(lp, oldStr, oldLen, offset);
|
|
|
|
|
|
|
|
if (offset < 0) {
|
|
|
|
if (needPrint) {
|
|
|
|
printLines(num1, num1, FALSE);
|
|
|
|
needPrint = FALSE;
|
|
|
|
}
|
|
|
|
offset = 0;
|
2006-05-05 02:26:43 +05:30
|
|
|
lp = lp->next;
|
2017-07-27 14:47:15 +05:30
|
|
|
num1++;
|
2006-05-05 02:26:43 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
needPrint = printFlag;
|
|
|
|
didSub = TRUE;
|
|
|
|
dirty = TRUE;
|
|
|
|
|
2006-05-05 02:26:43 +05:30
|
|
|
/*
|
2017-07-27 14:47:15 +05:30
|
|
|
* If the replacement string is the same size or shorter
|
|
|
|
* than the old string, then the substitution is easy.
|
2006-05-05 02:26:43 +05:30
|
|
|
*/
|
2017-07-27 14:47:15 +05:30
|
|
|
if (deltaLen <= 0) {
|
|
|
|
memcpy(&lp->data[offset], newStr, newLen);
|
|
|
|
if (deltaLen) {
|
|
|
|
memcpy(&lp->data[offset + newLen],
|
|
|
|
&lp->data[offset + oldLen],
|
|
|
|
lp->len - offset - oldLen);
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
lp->len += deltaLen;
|
|
|
|
}
|
|
|
|
offset += newLen;
|
|
|
|
if (globalFlag)
|
|
|
|
continue;
|
|
|
|
if (needPrint) {
|
|
|
|
printLines(num1, num1, FALSE);
|
|
|
|
needPrint = FALSE;
|
|
|
|
}
|
|
|
|
lp = lp->next;
|
|
|
|
num1++;
|
|
|
|
continue;
|
2006-05-05 02:26:43 +05:30
|
|
|
}
|
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
/*
|
|
|
|
* The new string is larger, so allocate a new line
|
|
|
|
* structure and use that. Link it in place of
|
|
|
|
* the old line structure.
|
|
|
|
*/
|
|
|
|
nlp = xmalloc(sizeof(LINE) + lp->len + deltaLen);
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
nlp->len = lp->len + deltaLen;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
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);
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
nlp->next = lp->next;
|
|
|
|
nlp->prev = lp->prev;
|
|
|
|
nlp->prev->next = nlp;
|
|
|
|
nlp->next->prev = nlp;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if (curLine == lp)
|
|
|
|
curLine = nlp;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
free(lp);
|
|
|
|
lp = nlp;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
offset += newLen;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if (globalFlag)
|
|
|
|
continue;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if (needPrint) {
|
|
|
|
printLines(num1, num1, FALSE);
|
|
|
|
needPrint = FALSE;
|
2006-05-05 02:26:43 +05:30
|
|
|
}
|
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
lp = lp->next;
|
|
|
|
num1++;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if (!didSub)
|
|
|
|
bb_error_msg("no substitutions found for \"%s\"", oldStr);
|
2006-05-05 02:26:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-07-27 14:47:15 +05:30
|
|
|
* Read commands until we are told to stop.
|
2006-05-05 02:26:43 +05:30
|
|
|
*/
|
2017-07-27 14:47:15 +05:30
|
|
|
static void doCommands(void)
|
2006-05-05 02:26:43 +05:30
|
|
|
{
|
2017-07-27 14:47:15 +05:30
|
|
|
while (TRUE) {
|
2017-07-27 15:28:25 +05:30
|
|
|
char buf[USERSIZE];
|
|
|
|
const char *cp;
|
|
|
|
int len;
|
|
|
|
int n, num1, num2;
|
|
|
|
smallint h, have1, have2;
|
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
/* Returns:
|
|
|
|
* -1 on read errors or EOF, or on bare Ctrl-D.
|
|
|
|
* 0 on ctrl-C,
|
|
|
|
* >0 length of input string, including terminating '\n'
|
|
|
|
*/
|
2017-08-02 20:57:28 +05:30
|
|
|
len = read_line_input(NULL, ": ", buf, sizeof(buf));
|
2017-07-27 14:47:15 +05:30
|
|
|
if (len <= 0)
|
|
|
|
return;
|
2017-07-27 16:04:56 +05:30
|
|
|
while (len && isspace(buf[--len]))
|
2017-07-27 15:28:25 +05:30
|
|
|
buf[len] = '\0';
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
if ((curNum == 0) && (lastNum > 0)) {
|
|
|
|
curNum = 1;
|
|
|
|
curLine = lines.next;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 15:28:25 +05:30
|
|
|
have1 = FALSE;
|
|
|
|
have2 = FALSE;
|
2017-07-27 16:04:56 +05:30
|
|
|
/* Don't pass &haveN, &numN to getNum() since this forces
|
2017-07-27 15:28:25 +05:30
|
|
|
* compiler to keep them on stack, not in registers,
|
|
|
|
* which is usually quite suboptimal.
|
|
|
|
* Using intermediate variables shrinks code by ~150 bytes.
|
|
|
|
*/
|
|
|
|
cp = getNum(skip_whitespace(buf), &h, &n);
|
|
|
|
if (!cp)
|
2017-07-27 14:47:15 +05:30
|
|
|
continue;
|
2017-07-27 15:28:25 +05:30
|
|
|
have1 = h;
|
|
|
|
num1 = n;
|
|
|
|
cp = skip_whitespace(cp);
|
2017-07-27 14:47:15 +05:30
|
|
|
if (*cp == ',') {
|
2017-07-27 15:28:25 +05:30
|
|
|
cp = getNum(cp + 1, &h, &n);
|
|
|
|
if (!cp)
|
2017-07-27 14:47:15 +05:30
|
|
|
continue;
|
2017-07-27 15:28:25 +05:30
|
|
|
num2 = n;
|
2017-07-27 14:47:15 +05:30
|
|
|
if (!have1)
|
|
|
|
num1 = 1;
|
2017-07-27 15:28:25 +05:30
|
|
|
if (!h)
|
2017-07-27 14:47:15 +05:30
|
|
|
num2 = lastNum;
|
|
|
|
have1 = TRUE;
|
|
|
|
have2 = TRUE;
|
|
|
|
}
|
|
|
|
if (!have1)
|
|
|
|
num1 = curNum;
|
|
|
|
if (!have2)
|
|
|
|
num2 = num1;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
switch (*cp++) {
|
|
|
|
case 'a':
|
|
|
|
addLines(num1 + 1);
|
|
|
|
break;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
case 'c':
|
|
|
|
deleteLines(num1, num2);
|
|
|
|
addLines(num1);
|
|
|
|
break;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
case 'd':
|
|
|
|
deleteLines(num1, num2);
|
|
|
|
break;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
case 'f':
|
2017-07-27 16:04:56 +05:30
|
|
|
if (*cp != '\0' && *cp != ' ') {
|
2017-07-27 14:47:15 +05:30
|
|
|
bb_error_msg("bad file command");
|
|
|
|
break;
|
|
|
|
}
|
2017-07-27 15:28:25 +05:30
|
|
|
cp = skip_whitespace(cp);
|
2017-07-27 14:47:15 +05:30
|
|
|
if (*cp == '\0') {
|
|
|
|
if (fileName)
|
|
|
|
printf("\"%s\"\n", fileName);
|
|
|
|
else
|
|
|
|
puts("No file name");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(fileName);
|
|
|
|
fileName = xstrdup(cp);
|
|
|
|
break;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
case 'i':
|
2017-07-27 16:04:56 +05:30
|
|
|
if (!have1 && lastNum == 0)
|
|
|
|
num1 = 1;
|
2017-07-27 14:47:15 +05:30
|
|
|
addLines(num1);
|
|
|
|
break;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
case 'k':
|
2017-07-27 15:28:25 +05:30
|
|
|
cp = skip_whitespace(cp);
|
2017-07-27 16:04:56 +05:30
|
|
|
if ((unsigned)(*cp - 'a') >= 26 || cp[1]) {
|
2017-07-27 14:47:15 +05:30
|
|
|
bb_error_msg("bad mark name");
|
|
|
|
break;
|
|
|
|
}
|
2017-07-27 16:04:56 +05:30
|
|
|
marks[(unsigned)(*cp - 'a')] = num2;
|
2017-07-27 14:47:15 +05:30
|
|
|
break;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
case 'l':
|
|
|
|
printLines(num1, num2, TRUE);
|
|
|
|
break;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
case 'p':
|
|
|
|
printLines(num1, num2, FALSE);
|
|
|
|
break;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
case 'q':
|
2017-07-27 15:28:25 +05:30
|
|
|
cp = skip_whitespace(cp);
|
2017-07-27 14:47:15 +05:30
|
|
|
if (have1 || *cp) {
|
|
|
|
bb_error_msg("bad quit command");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!dirty)
|
|
|
|
return;
|
2017-08-02 20:57:28 +05:30
|
|
|
len = read_line_input(NULL, "Really quit? ", buf, 16);
|
2017-07-27 14:47:15 +05:30
|
|
|
/* read error/EOF - no way to continue */
|
|
|
|
if (len < 0)
|
|
|
|
return;
|
2017-07-27 15:28:25 +05:30
|
|
|
cp = skip_whitespace(buf);
|
2017-07-27 14:47:15 +05:30
|
|
|
if ((*cp | 0x20) == 'y') /* Y or y */
|
|
|
|
return;
|
|
|
|
break;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
case 'r':
|
2017-07-27 16:04:56 +05:30
|
|
|
if (*cp != '\0' && *cp != ' ') {
|
2017-07-27 14:47:15 +05:30
|
|
|
bb_error_msg("bad read command");
|
|
|
|
break;
|
|
|
|
}
|
2017-07-27 15:28:25 +05:30
|
|
|
cp = skip_whitespace(cp);
|
2017-07-27 14:47:15 +05:30
|
|
|
if (*cp == '\0') {
|
|
|
|
bb_error_msg("no file name");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!have1)
|
|
|
|
num1 = lastNum;
|
|
|
|
if (readLines(cp, num1 + 1))
|
|
|
|
break;
|
|
|
|
if (fileName == NULL)
|
|
|
|
fileName = xstrdup(cp);
|
|
|
|
break;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
case 's':
|
|
|
|
subCommand(cp, num1, num2);
|
|
|
|
break;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
case 'w':
|
2017-07-27 16:04:56 +05:30
|
|
|
if (*cp != '\0' && *cp != ' ') {
|
2017-07-27 14:47:15 +05:30
|
|
|
bb_error_msg("bad write command");
|
|
|
|
break;
|
|
|
|
}
|
2017-07-27 15:28:25 +05:30
|
|
|
cp = skip_whitespace(cp);
|
2017-07-27 16:04:56 +05:30
|
|
|
if (*cp == '\0') {
|
|
|
|
cp = fileName;
|
|
|
|
if (!cp) {
|
|
|
|
bb_error_msg("no file name specified");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-07-27 14:47:15 +05:30
|
|
|
if (!have1) {
|
|
|
|
num1 = 1;
|
|
|
|
num2 = lastNum;
|
2017-07-27 16:04:56 +05:30
|
|
|
dirty = FALSE;
|
2017-07-27 14:47:15 +05:30
|
|
|
}
|
|
|
|
writeLines(cp, num1, num2);
|
|
|
|
break;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
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;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
case '.':
|
|
|
|
if (have1) {
|
|
|
|
bb_error_msg("no arguments allowed");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
printLines(curNum, curNum, FALSE);
|
|
|
|
break;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
case '-':
|
|
|
|
if (setCurNum(curNum - 1))
|
|
|
|
printLines(curNum, curNum, FALSE);
|
|
|
|
break;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
case '=':
|
|
|
|
printf("%d\n", num1);
|
|
|
|
break;
|
|
|
|
case '\0':
|
|
|
|
if (have1) {
|
|
|
|
printLines(num2, num2, FALSE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (setCurNum(curNum + 1))
|
|
|
|
printLines(curNum, curNum, FALSE);
|
|
|
|
break;
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
default:
|
|
|
|
bb_error_msg("unimplemented command");
|
|
|
|
break;
|
|
|
|
}
|
2006-05-05 02:26:43 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
int ed_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int ed_main(int argc UNUSED_PARAM, char **argv)
|
2006-05-05 02:26:43 +05:30
|
|
|
{
|
2017-07-27 14:47:15 +05:30
|
|
|
INIT_G();
|
2006-05-05 02:26:43 +05:30
|
|
|
|
2017-07-27 14:47:15 +05:30
|
|
|
bufSize = INITBUF_SIZE;
|
|
|
|
bufBase = xmalloc(bufSize);
|
|
|
|
bufPtr = bufBase;
|
|
|
|
lines.next = &lines;
|
|
|
|
lines.prev = &lines;
|
|
|
|
|
|
|
|
if (argv[1]) {
|
|
|
|
fileName = xstrdup(argv[1]);
|
|
|
|
if (!readLines(fileName, 1)) {
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
if (lastNum)
|
|
|
|
setCurNum(1);
|
|
|
|
dirty = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
doCommands();
|
|
|
|
return EXIT_SUCCESS;
|
2006-05-05 02:26:43 +05:30
|
|
|
}
|