* The "ed" built-in command (much simplified)
*/
+//config:config ED
+//config: bool "ed"
+//config: default y
+//config: help
+//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.
+
+//kbuild:lib-$(CONFIG_ED) += ed.o
+
+//applet:IF_ED(APPLET(ed, BB_DIR_BIN, BB_SUID_DROP))
+
+//usage:#define ed_trivial_usage ""
+//usage:#define ed_full_usage ""
+
#include "libbb.h"
typedef struct LINE {
* 0 on ctrl-C,
* >0 length of input string, including terminating '\n'
*/
- len = read_line_input(": ", buf, sizeof(buf), NULL);
+ len = read_line_input(NULL, ": ", buf, sizeof(buf), /*timeout*/ -1);
if (len <= 0)
return;
endbuf = &buf[len - 1];
}
if (!dirty)
return;
- len = read_line_input("Really quit? ", buf, 16, NULL);
+ len = read_line_input(NULL, "Really quit? ", buf, 16, /*timeout*/ -1);
/* read error/EOF - no way to continue */
if (len < 0)
return;
/*
* The new string is larger, so allocate a new line
- * structure and use that. Link it in in place of
+ * structure and use that. Link it in place of
* the old line structure.
*/
- nlp = malloc(sizeof(LINE) + lp->len + deltaLen);
- if (nlp == NULL) {
- bb_error_msg("cannot get memory for line");
- return;
- }
+ nlp = xmalloc(sizeof(LINE) + lp->len + deltaLen);
nlp->len = lp->len + deltaLen;
* 0 on ctrl-C,
* >0 length of input string, including terminating '\n'
*/
- len = read_line_input("", buf, sizeof(buf), NULL);
+ len = read_line_input(NULL, "", buf, sizeof(buf), /*timeout*/ -1);
if (len <= 0) {
/* Previously, ctrl-C was exiting to shell.
* Now we exit to ed prompt. Is in important? */
fd = open(file, 0);
if (fd < 0) {
- perror(file);
+ bb_simple_perror_msg(file);
return FALSE;
}
cc = 0;
printf("\"%s\", ", file);
- fflush(stdout);
+ fflush_all();
do {
cp = memchr(bufPtr, '\n', bufUsed);
if (bufUsed >= bufSize) {
len = (bufSize * 3) / 2;
- cp = realloc(bufBase, len);
- if (cp == NULL) {
- bb_error_msg("no memory for buffer");
- close(fd);
- return FALSE;
- }
+ cp = xrealloc(bufBase, len);
bufBase = cp;
bufPtr = bufBase + bufUsed;
bufSize = len;
} while (cc > 0);
if (cc < 0) {
- perror(file);
+ bb_simple_perror_msg(file);
close(fd);
return FALSE;
}
fd = creat(file, 0666);
if (fd < 0) {
- perror(file);
+ bb_simple_perror_msg(file);
return FALSE;
}
printf("\"%s\", ", file);
- fflush(stdout);
+ fflush_all();
lp = findLine(num1);
if (lp == NULL) {
while (num1++ <= num2) {
if (full_write(fd, lp->data, lp->len) != lp->len) {
- perror(file);
+ bb_simple_perror_msg(file);
close(fd);
return FALSE;
}
}
if (close(fd) < 0) {
- perror(file);
+ bb_simple_perror_msg(file);
return FALSE;
}
return FALSE;
}
- newLp = malloc(sizeof(LINE) + len - 1);
- if (newLp == NULL) {
- bb_error_msg("failed to allocate memory for line");
- return FALSE;
- }
+ newLp = xmalloc(sizeof(LINE) + len - 1);
memcpy(newLp->data, data, len);
newLp->len = len;
lp = lp->next;
}
- bb_error_msg("cannot find string \"%s\"", str);
+ bb_error_msg("can't find string \"%s\"", str);
return 0;
}