summaryrefslogtreecommitdiff
path: root/src/commands/ed.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands/ed.c')
-rw-r--r--src/commands/ed.c64
1 files changed, 32 insertions, 32 deletions
diff --git a/src/commands/ed.c b/src/commands/ed.c
index 44f1a2c..379da3c 100644
--- a/src/commands/ed.c
+++ b/src/commands/ed.c
@@ -50,7 +50,7 @@ static bool read_regex(char** end, re_t* regex, enum RegexDirection dir) {
while(true) {
c = *(index++);
if (c == '\0') {
- fprintf(stderr, "error: missing regex after %c\n", dir == BEFORE ? '?' : '/');
+ error_s("missing regex after %c\n", dir == BEFORE ? '?' : '/');
return false;
}
if (c == (dir == BEFORE ? '?' : '/')) {
@@ -170,7 +170,7 @@ static bool read_address(char** command, bool whitespace, struct LineAddress* a)
n_pre = -1;
} else {
if (n_pre < 0) {
- fprintf(stderr, "error: input cannot be negative\n");
+ error_s("input cannot be negative\n");
return false;
}
index = end_pre;
@@ -192,7 +192,7 @@ static bool read_address(char** command, bool whitespace, struct LineAddress* a)
char* end;
long int n = strtol(index, &end, 10) - 1;
if (n < 0) {
- fprintf(stderr, "error: input cannot be negative\n");
+ error_s("input cannot be negative\n");
return false;
}
if (index == end) {
@@ -201,7 +201,7 @@ static bool read_address(char** command, bool whitespace, struct LineAddress* a)
address.data.index.i = line_current - n;
}
if (address.data.index.i < 0) {
- fprintf(stderr, "error: line number %ld does not exist\n", address.data.index.i + 1);
+ error_s("line number %ld does not exist\n", address.data.index.i + 1);
return false;
}
break;
@@ -211,7 +211,7 @@ static bool read_address(char** command, bool whitespace, struct LineAddress* a)
char* end;
long int n = strtol(index, &end, 10) - 1;
if (n < 0) {
- fprintf(stderr, "error: input cannot be negative\n");
+ error_s("input cannot be negative\n");
return false;
}
if (index == end) {
@@ -220,7 +220,7 @@ static bool read_address(char** command, bool whitespace, struct LineAddress* a)
address.data.index.i = line_current + n;
}
if (address.data.index.i >= (long int) line_count) {
- fprintf(stderr, "error: line number %ld does not exist\n", address.data.index.i + 1);
+ error_s("line number %ld does not exist\n", address.data.index.i + 1);
return false;
}
break;
@@ -256,7 +256,7 @@ static bool read_address(char** command, bool whitespace, struct LineAddress* a)
address.data.index.i = n_pre;
}
if (address.data.index.i < 0 || address.data.index.i >= (long int) line_count) {
- fprintf(stderr, "error: line number %ld does not exist\n", address.data.index.i + 1);
+ error_s("line number %ld does not exist\n", address.data.index.i + 1);
return false;
}
}
@@ -404,7 +404,7 @@ static void delete_lines(unsigned long a, unsigned long b) {
static bool handle_append(struct LineAddress* address) {
if (address->type != INDEX) {
- fprintf(stderr, "error: append command requires index addressing\n");
+ error_s("append command requires index addressing\n");
return false;
}
if (line_count == 0) {
@@ -424,20 +424,20 @@ static bool handle_append(struct LineAddress* address) {
static bool handle_delete(struct LineAddress* address) {
if (address->empty && address->data.index.i >= (long int) line_count) {
- fprintf(stderr, "error: line number %ld does not exist\n", address->data.index.i + 1);
+ error_s("line number %ld does not exist\n", address->data.index.i + 1);
return false;
}
if (address->type == INDEX) {
delete_lines(address->data.index.i, address->data.index.i);
- printf("ed: deleted line %ld\n", address->data.index.i+1);
+ output("deleted line %ld\n", address->data.index.i+1);
} else if (address->type == RANGE) {
delete_lines(address->data.range.a, address->data.range.b);
- printf("ed: deleted lines %ld-%ld\n", address->data.range.a+1, address->data.range.b+1);
+ output("deleted lines %ld-%ld\n", address->data.range.a+1, address->data.range.b+1);
} else if (address->type == SET) {
for (unsigned long i = 0; i < address->data.set.s; i++) {
delete_lines(address->data.set.b[i], address->data.set.b[i]);
}
- printf("ed: deleted %lu lines\n", address->data.set.s);
+ output("deleted %lu lines\n", address->data.set.s);
}
return true;
}
@@ -446,7 +446,7 @@ static bool get_file_name(char** filename) {
size_t len = strlen(*filename);
if (len < 1 || (len == 1 && **filename == '\n')) {
if (default_filename == NULL) {
- fprintf(stderr, "error: no default filename specified\n");
+ error_s("no default filename specified\n");
return false;
}
*filename = default_filename;
@@ -468,7 +468,7 @@ static bool get_file_name(char** filename) {
static void write_file(char* filename, struct LineAddress* address, char* type) {
if (line_count < 1) {
- fprintf(stderr, "error: cannot write empty file\n");
+ error_s("cannot write empty file\n");
return;
}
if (!get_file_name(&filename)) return;
@@ -497,7 +497,7 @@ static void write_file(char* filename, struct LineAddress* address, char* type)
}
pending_writes = false;
fclose(file);
- printf("ed: wrote %d lines from %s\n", wrote, filename);
+ output("wrote %d lines from %s\n", wrote, filename);
}
static void read_file(char* filename) {
@@ -512,7 +512,7 @@ static void read_file(char* filename) {
if (size < 1) {
free(buf);
- fprintf(stderr, "error: attempted to read a empty file\n");
+ error_s("attempted to read a empty file\n");
return;
}
@@ -522,7 +522,7 @@ static void read_file(char* filename) {
}
append_lines(line, buf, size);
free(buf);
- printf("ed: read and appended %lu lines from %s\n", size, filename);
+ output("read and appended %lu lines from %s\n", size, filename);
}
static void expand_string(char** buf, int* capacity, int* size, char* text, int len) {
@@ -582,7 +582,7 @@ static void prompt(void) {
if (cmd == ',') {
if (address.type != INDEX) {
- fprintf(stderr, "error: comma range addressing requires two index addresses\n");
+ error_s("comma range addressing requires two index addresses\n");
free_address(address);
return;
}
@@ -593,7 +593,7 @@ static void prompt(void) {
return;
}
if (address2.type != INDEX) {
- fprintf(stderr, "error: comma range addressing requires two index addresses\n");
+ error_s("comma range addressing requires two index addresses\n");
free_address(address);
free_address(address2);
return;
@@ -606,7 +606,7 @@ static void prompt(void) {
}
if (address.type == RANGE && address.data.range.a > address.data.range.b) {
- fprintf(stderr, "error: range addressing must be in ascending order\n");
+ error_s("range addressing must be in ascending order\n");
free_address(address);
return;
}
@@ -618,10 +618,10 @@ test:
case '\n':
if (address.empty) {
if (line_current == line_count) {
- fprintf(stderr, "error: line number %ld does not exist\n", line_current + 1);
+ error_s("line number %ld does not exist\n", line_current + 1);
break;
} else if (line_current + 1 == line_count) {
- fprintf(stderr, "error: line number %ld does not exist\n", line_current + 2);
+ error_s("line number %ld does not exist\n", line_current + 2);
break;
} else {
line_current++;
@@ -634,7 +634,7 @@ test:
} else if (address.type == RANGE) {
line_current = address.data.range.b;
} else if (address.type == SET) {
- fprintf(stderr, "error: unexpected range addressing\n");
+ error_s("unexpected range addressing\n");
break;
}
printf("%s", lines[line_current]);
@@ -656,7 +656,7 @@ test:
__attribute__((fallthrough));
case 'p':
if (address.empty && address.data.index.i >= (long int) line_count) {
- fprintf(stderr, "error: line number %ld does not exist\n", address.data.index.i + 1);
+ error_s("line number %ld does not exist\n", address.data.index.i + 1);
break;
}
if (address.type == INDEX) {
@@ -686,7 +686,7 @@ test:
skip_whitespace(&index);
free_address(address);
if (*(index++) != '/') {
- fprintf(stderr, "error: unexpected character at start of regex\n");
+ error_s("unexpected character at start of regex\n");
break;
}
if (!parse_regex(&index, &address, ALL)) { return; }
@@ -701,18 +701,18 @@ test:
case 's':
skip_whitespace(&index);
if (*(index++) != '/') {
- fprintf(stderr, "error: unexpected character at start of regex\n");
+ error_s("unexpected character at start of regex\n");
break;
}
if (!parse_regex_lines(&index, &address)) { return; }
char* replace = index;
while(*index != '\0' && *index != '/') index++;
if (*index != '/') {
- fprintf(stderr, "error: / missing after %c\n", *index);
+ error_s("/ missing after %c\n", *index);
break;
}
if (address.data.set.s < 1) {
- printf("ed: no matches found\n");
+ error_s("no matches found\n");
break;
}
*(index++) = '\0';
@@ -725,11 +725,11 @@ test:
char* end;
matches = strtol(index, &end, 10);
if (end == index) {
- fprintf(stderr, "error: invalid number: %s\n", index);
+ error_s("invalid number: %s\n", index);
break;
}
if (matches < 1) {
- fprintf(stderr, "error: matches cannot be less than 1\n");
+ error_s("matches cannot be less than 1\n");
break;
}
}
@@ -739,7 +739,7 @@ test:
for (unsigned long i = 0; i < address.data.set.s; i++) {
matches_found += substute_string(address.data.set.b[i], matches, last_regex, replace, sub_len);
}
- printf("ed: replaced %ld matches over %ld lines\n", matches_found, address.data.set.s);
+ output("replaced %ld matches over %ld lines\n", matches_found, address.data.set.s);
pending_writes = true;
break;
case 'w': {
@@ -782,7 +782,7 @@ test:
printf("%ld\n", line_current + 1);
break;
default:
- fprintf(stderr, "error: unimplemented command\n");
+ error_s("unimplemented command\n");
break;
}