lazysphere/command/ed.c

921 lines
25 KiB
C
Raw Normal View History

2023-05-06 04:39:44 +00:00
#include "command.h"
#include "lslib.h"
#include <stdlib.h>
#include <string.h>
2023-04-30 06:12:02 +00:00
#define INPUT_LEN 1024
static char** lines = NULL;
static unsigned long line_capacity;
static unsigned long line_count;
static unsigned long line_current;
static bool pending_writes;
2023-04-30 18:50:00 +00:00
static char* default_filename = NULL;
static re_t last_regex = NULL;
2023-04-30 06:12:02 +00:00
enum LineAddressType {
INDEX,
RANGE,
SET,
2023-04-30 18:50:00 +00:00
FREE
2023-04-30 06:12:02 +00:00
};
struct LineAddress {
enum LineAddressType type;
union {
struct {
long int i;
} index;
struct {
long int a;
long int b;
} range;
struct {
long int* b;
unsigned long c;
unsigned long s;
} set;
} data;
bool empty;
};
enum RegexDirection {
BEFORE,
AFTER,
ALL
};
static bool read_regex(char** end, re_t* regex, enum RegexDirection dir) {
2023-04-30 06:12:02 +00:00
char c;
char* index = *end;
char* regex_str = index;
while(true) {
c = *(index++);
if (c == '\0') {
2023-05-03 16:17:56 +00:00
error_s("missing regex after %c\n", dir == BEFORE ? '?' : '/');
2023-04-30 06:12:02 +00:00
return false;
}
if (c == (dir == BEFORE ? '?' : '/')) {
*(index - 1) = '\0';
break;
}
}
*regex = re_compile(regex_str);
last_regex = *regex;
*end = index;
return true;
}
static bool parse_regex(char** end, struct LineAddress* address, enum RegexDirection dir) {
re_t regex;
2023-05-04 20:10:37 +00:00
unsigned long cap, siz, i, until;
long int* buf;
if (!read_regex(end, &regex, dir)) return false;
2023-05-04 20:10:37 +00:00
cap = 8;
siz = 0;
2023-05-15 01:43:02 +00:00
buf = xalloc(cap * sizeof(long int));
2023-04-30 18:50:00 +00:00
2023-05-04 20:10:37 +00:00
i = (dir == ALL ? 0 : line_current);
until = (dir == BEFORE ? 0 : line_count - 1);
2023-04-30 06:12:02 +00:00
for (; (dir == BEFORE ? i >= until : i < until); dir == BEFORE ? i-- : i++) {
int len;
if (re_matchp(regex, lines[i], &len) == -1) {
if (dir == BEFORE && i == 0) break;
continue;
}
if (cap == siz) {
cap *= 2;
2023-05-15 01:43:02 +00:00
buf = xrealloc(buf, cap * sizeof(long int));
2023-04-30 06:12:02 +00:00
}
buf[siz] = i;
siz++;
if (dir == BEFORE && i == 0) break;
}
2023-04-30 18:50:00 +00:00
address->empty = false;
2023-04-30 06:12:02 +00:00
address->type = SET;
address->data.set.s = siz;
address->data.set.c = cap;
address->data.set.b = buf;
return true;
}
static void free_address (struct LineAddress address) {
if (address.type != SET) return;
address.type = FREE;
free(address.data.set.b);
}
static void expand_buffer(long int** buf, unsigned long* cap, unsigned long* size) {
if (*cap == *size) {
*cap *= 2;
2023-05-15 01:43:02 +00:00
*buf = xrealloc(*buf, sizeof(long int) * *cap);
}
}
static bool parse_regex_lines(char** end, struct LineAddress* address) {
re_t regex;
2023-05-04 20:10:37 +00:00
unsigned long cap, siz;
long int* buf;
int len;
struct LineAddress addr;
if (!read_regex(end, &regex, ALL)) return false;
2023-05-04 20:10:37 +00:00
cap = 8;
siz = 0;
2023-05-15 01:43:02 +00:00
buf = xalloc(cap * sizeof(long int));
2023-05-04 20:10:37 +00:00
addr = *address;
if (addr.type == INDEX) {
if (re_matchp(regex, lines[addr.data.index.i], &len) != -1) {
buf[0] = addr.data.index.i;
siz = 1;
}
} else if (addr.type == RANGE) {
2023-05-04 20:10:37 +00:00
long int i;
for (i = addr.data.range.a; i < addr.data.range.b; i++) {
if (re_matchp(regex, lines[i], &len) == -1) continue;
expand_buffer(&buf, &cap, &siz);
buf[siz] = i;
siz++;
}
} else if (addr.type == SET) {
2023-05-04 20:10:37 +00:00
unsigned long i;
for (i = 0; i < addr.data.set.s; i++) {
if (re_matchp(regex, lines[addr.data.set.b[i]], &len) == -1) continue;
expand_buffer(&buf, &cap, &siz);
buf[siz] = addr.data.set.b[i];
siz++;
}
}
free_address(*address);
address->empty = false;
address->type = SET;
address->data.set.s = siz;
address->data.set.c = cap;
address->data.set.b = buf;
2023-04-30 06:12:02 +00:00
return true;
}
static bool read_address(char** command, bool whitespace, struct LineAddress* a) {
char* index = *command;
struct LineAddress address;
2023-05-04 20:10:37 +00:00
char* end_pre;
long int n_pre;
char pre;
2023-04-30 06:12:02 +00:00
memset(&address, 0, sizeof(struct LineAddress));
address.empty = false;
if (strlen(*command) < 1) {
address.type = INDEX;
address.data.index.i = line_current + 1;
if (line_current >= line_count) line_current = line_count - 1;
*a = address;
return true;
}
2023-05-04 20:10:37 +00:00
n_pre = strtol(index, &end_pre, 10) - 1;
2023-04-30 06:12:02 +00:00
if (end_pre == index) {
n_pre = -1;
} else {
if (n_pre < 0) {
2023-05-03 16:17:56 +00:00
error_s("input cannot be negative\n");
2023-04-30 06:12:02 +00:00
return false;
}
index = end_pre;
}
2023-05-04 20:10:37 +00:00
pre = *(index++);
2023-04-30 06:12:02 +00:00
switch (pre) {
case '.':
address.type = INDEX;
address.data.index.i = line_current;
break;
case '$':
address.type = INDEX;
address.data.index.i = line_count - 1;
break;
case '-':
case '^': {
char* end;
2023-05-04 20:10:37 +00:00
long int n;
address.type = INDEX;
n = strtol(index, &end, 10) - 1;
2023-04-30 06:12:02 +00:00
if (n < 0) {
2023-05-03 16:17:56 +00:00
error_s("input cannot be negative\n");
2023-04-30 06:12:02 +00:00
return false;
}
2023-05-04 20:10:37 +00:00
2023-04-30 06:12:02 +00:00
if (index == end) {
address.data.index.i = line_current - 1;
} else {
address.data.index.i = line_current - n;
}
2023-05-04 20:10:37 +00:00
2023-04-30 06:12:02 +00:00
if (address.data.index.i < 0) {
2023-05-03 16:17:56 +00:00
error_s("line number %ld does not exist\n", address.data.index.i + 1);
2023-04-30 06:12:02 +00:00
return false;
}
2023-05-04 20:10:37 +00:00
2023-04-30 06:12:02 +00:00
break;
}
case '+': {
char* end;
2023-05-04 20:10:37 +00:00
long int n;
address.type = INDEX;
n = strtol(index, &end, 10) - 1;
2023-04-30 06:12:02 +00:00
if (n < 0) {
2023-05-03 16:17:56 +00:00
error_s("input cannot be negative\n");
2023-04-30 06:12:02 +00:00
return false;
}
if (index == end) {
address.data.index.i = line_current + 1;
} else {
address.data.index.i = line_current + n;
}
if (address.data.index.i >= (long int) line_count) {
2023-05-03 16:17:56 +00:00
error_s("line number %ld does not exist\n", address.data.index.i + 1);
2023-04-30 06:12:02 +00:00
return false;
}
break;
}
case '%':
address.type = RANGE;
address.data.range.a = 0;
address.data.range.b = line_count - 1;
break;
case ';':
address.type = RANGE;
address.data.range.a = line_current;
address.data.range.b = line_count - 1;
break;
case '/':
if (!parse_regex(&index, &address, AFTER)) return false;
break;
case '?':
if (!parse_regex(&index, &address, BEFORE)) return false;
break;
default: {
index--;
if (n_pre == -1) {
address.type = INDEX;
address.data.index.i = line_current;
address.empty = true;
break;
} else if (whitespace) {
address.type = INDEX;
address.data.index.i = line_current + n_pre;
} else {
address.type = INDEX;
address.data.index.i = n_pre;
}
if (address.data.index.i < 0 || address.data.index.i >= (long int) line_count) {
2023-05-03 16:17:56 +00:00
error_s("line number %ld does not exist\n", address.data.index.i + 1);
2023-04-30 06:12:02 +00:00
return false;
}
}
}
*command = index;
*a = address;
return true;
}
2023-04-30 18:50:00 +00:00
static void free_data(bool all) {
if (lines != NULL) {
2023-05-04 20:10:37 +00:00
unsigned long i;
for (i = 0; i < line_count; i++) {
2023-04-30 18:50:00 +00:00
free(lines[i]);
}
free(lines);
lines = NULL;
}
if (all && default_filename != NULL) {
free(default_filename);
default_filename = NULL;
}
2023-04-30 06:12:02 +00:00
}
2023-05-01 22:43:32 +00:00
static void load_empty(void) {
2023-04-30 18:50:00 +00:00
free_data(false);
2023-04-30 06:12:02 +00:00
line_capacity = 8;
2023-05-15 01:43:02 +00:00
lines = xalloc(sizeof(char*) * line_capacity);
2023-04-30 06:12:02 +00:00
line_count = 0;
line_current = 0;
pending_writes = false;
}
static void get_input(FILE* file, char*** buffer, unsigned long* capacity, unsigned long* size) {
unsigned long cap = 8;
unsigned long siz = 0;
2023-05-15 01:43:02 +00:00
char** buf = xalloc(sizeof(char*) * cap);
2023-04-30 06:12:02 +00:00
char* line = NULL;
size_t offset = 0;
clearerr(stdin);
while (getline(&line, &offset, file) != -1) {
if (cap == siz) {
cap *= 2;
2023-05-15 01:43:02 +00:00
buf = xrealloc(buf, sizeof(char*) * cap);
2023-04-30 06:12:02 +00:00
}
buf[siz] = line;
siz++;
line = NULL;
}
free(line);
*buffer = buf;
*capacity = cap;
*size = siz;
}
int ed_getline(char *buf, size_t size) {
size_t i = 0;
int ch;
clearerr(stdin);
2023-05-04 20:10:37 +00:00
while ((ch = getchar()) != EOF) { /* Read until EOF ... */
2023-04-30 06:12:02 +00:00
if (i + 1 < size) {
buf[i++] = ch;
}
2023-05-04 20:10:37 +00:00
if (ch == '\n') { /* ... or end of line */
2023-04-30 06:12:02 +00:00
break;
}
}
buf[i] = '\0';
if (i == 0) {
return EOF;
}
return i;
}
static void load_file(FILE* file) {
2023-04-30 18:50:00 +00:00
free_data(false);
2023-04-30 06:12:02 +00:00
line_current = 0;
get_input(file, &lines, &line_capacity, &line_count);
if (file != stdin)
fclose(file);
2023-04-30 18:50:00 +00:00
2023-04-30 06:12:02 +00:00
}
2023-04-30 18:50:00 +00:00
static bool check_if_sure(char* prompt) {
2023-05-04 20:10:37 +00:00
char buf[INPUT_LEN];
2023-04-30 06:12:02 +00:00
if (!pending_writes) {
return true;
}
2023-04-30 18:50:00 +00:00
printf("%s", prompt);
2023-04-30 06:12:02 +00:00
fflush(stdout);
if (ed_getline(buf, INPUT_LEN) == EOF) {
putchar('\n');
return false;
}
return prefix("y", buf);
}
static bool skip_whitespace(char** index) {
char c;
bool w = false;
while (c = **index, c == ' ' || c == '\t') { (*index)++; w = true; }
return w;
}
static void expand(unsigned long count) {
if (count < line_capacity) return;
line_capacity *= 2;
if (count > line_capacity) line_capacity = count;
2023-05-15 01:43:02 +00:00
lines = xrealloc(lines, line_capacity * sizeof(char*));
2023-04-30 06:12:02 +00:00
}
static void append_lines(unsigned long index, char** new, unsigned long new_len) {
if (new_len < 1) return;
pending_writes = true;
expand(line_count + new_len);
if (index + 1 <= line_count)
memmove(&lines[index+new_len], &lines[index], sizeof(char*) * (line_count - index));
memcpy(&lines[index], new, sizeof(char*) * new_len);
line_count += new_len;
}
static void delete_lines(unsigned long a, unsigned long b) {
2023-05-04 20:10:37 +00:00
unsigned long i;
2023-04-30 06:12:02 +00:00
if (b < a) return;
pending_writes = true;
2023-05-04 20:10:37 +00:00
for (i = a; i <= b; i++) {
2023-04-30 06:12:02 +00:00
free(lines[i]);
}
if (b == line_count - 1) {
line_count = a;
return;
}
memmove(&lines[a], &lines[b+1], sizeof(char*) * (line_count - (b + 1)));
line_count -= (b - a) + 1;
line_current = a;
if (line_current >= line_count) line_current = line_count - 1;
}
static bool handle_append(struct LineAddress* address) {
2023-05-04 20:10:37 +00:00
char** buf;
unsigned long cap, size;
2023-04-30 06:12:02 +00:00
if (address->type != INDEX) {
2023-05-03 16:17:56 +00:00
error_s("append command requires index addressing\n");
2023-04-30 06:12:02 +00:00
return false;
}
2023-05-04 20:10:37 +00:00
2023-04-30 06:12:02 +00:00
if (line_count == 0) {
address->data.index.i = -1;
}
2023-05-04 20:10:37 +00:00
2023-04-30 06:12:02 +00:00
get_input(stdin, &buf, &cap, &size);
2023-05-04 20:10:37 +00:00
2023-04-30 06:12:02 +00:00
if (size > 0) {
append_lines(address->data.index.i + 1, buf, size);
2023-04-30 18:50:00 +00:00
printf("ed: appened %lu lines\n", size);
2023-04-30 06:12:02 +00:00
}
2023-05-04 20:10:37 +00:00
2023-04-30 06:12:02 +00:00
line_current += size;
free(buf);
return true;
}
static bool handle_delete(struct LineAddress* address) {
if (address->empty && address->data.index.i >= (long int) line_count) {
2023-05-03 16:17:56 +00:00
error_s("line number %ld does not exist\n", address->data.index.i + 1);
2023-04-30 06:12:02 +00:00
return false;
}
2023-05-04 20:10:37 +00:00
2023-04-30 06:12:02 +00:00
if (address->type == INDEX) {
delete_lines(address->data.index.i, address->data.index.i);
2023-05-03 16:17:56 +00:00
output("deleted line %ld\n", address->data.index.i+1);
2023-04-30 06:12:02 +00:00
} else if (address->type == RANGE) {
delete_lines(address->data.range.a, address->data.range.b);
2023-05-03 16:17:56 +00:00
output("deleted lines %ld-%ld\n", address->data.range.a+1, address->data.range.b+1);
2023-04-30 06:12:02 +00:00
} else if (address->type == SET) {
2023-05-04 20:10:37 +00:00
unsigned long i;
for (i = 0; i < address->data.set.s; i++) {
2023-04-30 06:12:02 +00:00
delete_lines(address->data.set.b[i], address->data.set.b[i]);
}
2023-05-03 16:17:56 +00:00
output("deleted %lu lines\n", address->data.set.s);
2023-04-30 18:50:00 +00:00
}
2023-05-04 20:10:37 +00:00
2023-04-30 18:50:00 +00:00
return true;
}
static bool get_file_name(char** filename) {
size_t len = strlen(*filename);
2023-05-04 20:10:37 +00:00
2023-04-30 18:50:00 +00:00
if (len < 1 || (len == 1 && **filename == '\n')) {
if (default_filename == NULL) {
2023-05-03 16:17:56 +00:00
error_s("no default filename specified\n");
2023-04-30 18:50:00 +00:00
return false;
}
*filename = default_filename;
} else {
if ((*filename)[len - 1] == '\n') {
(*filename)[len - 1] = '\0';
len--;
}
if (default_filename != NULL) {
2023-05-15 01:43:02 +00:00
default_filename = xrealloc(default_filename, len + 1);
2023-04-30 18:50:00 +00:00
} else {
2023-05-15 01:43:02 +00:00
default_filename = xalloc(len + 1);
2023-04-30 18:50:00 +00:00
}
memcpy(default_filename, *filename, len + 1);
*filename = default_filename;
2023-04-30 06:12:02 +00:00
}
return true;
}
2023-04-30 18:50:00 +00:00
static void write_file(char* filename, struct LineAddress* address, char* type) {
2023-05-04 20:10:37 +00:00
FILE* file;
int wrote;
2023-04-30 18:50:00 +00:00
if (line_count < 1) {
2023-05-03 16:17:56 +00:00
error_s("cannot write empty file\n");
2023-04-30 18:50:00 +00:00
return;
}
2023-05-04 20:10:37 +00:00
2023-04-30 18:50:00 +00:00
if (!get_file_name(&filename)) return;
2023-05-04 20:10:37 +00:00
file = get_file_s(filename, type);
2023-04-30 18:50:00 +00:00
if (file == NULL) return;
2023-05-04 20:10:37 +00:00
wrote = 0;
2023-04-30 18:50:00 +00:00
if (address->empty) {
2023-05-04 20:10:37 +00:00
unsigned long i;
for (i = 0; i < line_count; i++) {
2023-04-30 18:50:00 +00:00
fprintf(file, "%s", lines[i]);
wrote++;
}
} else if (address->type == INDEX) {
long int i = address->data.index.i;
fprintf(file, "%s", lines[i]);
wrote++;
} else if (address->type == RANGE) {
2023-05-04 20:10:37 +00:00
long int i;
for (i = address->data.range.a; i < address->data.range.b; i++) {
2023-04-30 18:50:00 +00:00
fprintf(file, "%s", lines[i]);
wrote++;
}
} else if (address->type == SET) {
2023-05-04 20:10:37 +00:00
unsigned long i;
for (i = 0; i < address->data.set.s; i++) {
2023-04-30 18:50:00 +00:00
fprintf(file, "%s", lines[address->data.set.b[i]]);
wrote++;
}
}
2023-05-04 20:10:37 +00:00
2023-04-30 18:50:00 +00:00
pending_writes = false;
fclose(file);
2023-05-03 16:17:56 +00:00
output("wrote %d lines from %s\n", wrote, filename);
2023-04-30 18:50:00 +00:00
}
static void read_file(char* filename) {
2023-05-04 20:10:37 +00:00
FILE* file;
char** buf;
long int line;
unsigned long capacity, size;
2023-04-30 18:50:00 +00:00
if (!get_file_name(&filename)) return;
2023-05-04 20:10:37 +00:00
file = get_file_s(filename, "r");
2023-04-30 18:50:00 +00:00
if (file == NULL) return;
2023-05-04 20:10:37 +00:00
capacity = 8;
size = 0;
2023-05-15 01:43:02 +00:00
buf = xalloc(capacity * sizeof(char*));
2023-05-04 20:10:37 +00:00
get_input(file, &buf, &capacity, &size);
2023-04-30 18:50:00 +00:00
if (size < 1) {
free(buf);
2023-05-03 16:17:56 +00:00
error_s("attempted to read a empty file\n");
2023-04-30 18:50:00 +00:00
return;
}
2023-05-04 20:10:37 +00:00
line = -1;
2023-04-30 18:50:00 +00:00
if (line_count > 0) {
line = line_count - 1;
}
2023-05-04 20:10:37 +00:00
2023-04-30 18:50:00 +00:00
append_lines(line, buf, size);
free(buf);
2023-05-03 16:17:56 +00:00
output("read and appended %lu lines from %s\n", size, filename);
2023-04-30 18:50:00 +00:00
}
static void expand_string(char** buf, int* capacity, int* size, char* text, int len) {
if (*size + len >= *capacity) {
*capacity *= 2;
if (*capacity < *size + len) *capacity = *size + len;
2023-05-15 01:43:02 +00:00
*buf = xrealloc(*buf, *capacity);
2023-04-30 18:50:00 +00:00
}
memcpy(*buf + *size, text, len);
*size += len;
}
static int substute_string(long int index, long int matches, re_t regex, char* sub, int sub_len) {
int capacity = 8;
int size = 0;
2023-05-15 01:43:02 +00:00
char* buf = xalloc(sizeof(char) * capacity);
2023-05-04 20:10:37 +00:00
long int left;
2023-04-30 18:50:00 +00:00
int offset = 0;
int matches_found = 0;
while(true) {
2023-05-04 20:10:37 +00:00
int distance, len;
2023-04-30 18:50:00 +00:00
if (lines[index][offset] == '\0') break;
2023-05-04 20:10:37 +00:00
2023-04-30 18:50:00 +00:00
if (matches_found >= matches && matches > 0) break;
2023-05-04 20:10:37 +00:00
2023-04-30 18:50:00 +00:00
if ((distance = re_matchp(regex, &lines[index][offset], &len)) == -1) {
break;
}
2023-05-04 20:10:37 +00:00
if (distance > 0) {
2023-04-30 18:50:00 +00:00
expand_string(&buf, &capacity, &size, &lines[index][offset], distance);
2023-05-04 20:10:37 +00:00
}
2023-04-30 18:50:00 +00:00
expand_string(&buf, &capacity, &size, sub, sub_len);
offset += len + distance;
matches_found++;
}
2023-05-04 20:10:37 +00:00
left = strlen(lines[index] + offset);
2023-04-30 18:50:00 +00:00
expand_string(&buf, &capacity, &size, &lines[index][offset], left + 1);
free(lines[index]);
lines[index] = buf;
return matches_found;
}
2023-05-01 22:43:32 +00:00
static void prompt(void) {
2023-05-04 20:10:37 +00:00
char buf[INPUT_LEN];
char* index;
char cmd;
bool whitespace, linenumbers;
struct LineAddress address;
2023-04-30 18:50:00 +00:00
printf(": ");
2023-04-30 06:12:02 +00:00
fflush(stdout);
if (ed_getline(buf, INPUT_LEN) == EOF) { putchar('\n'); return; }
if (buf[0] == '\0') { putchar('\n'); return; }
2023-05-04 20:10:37 +00:00
index = &buf[0];
whitespace = skip_whitespace(&index);
2023-04-30 06:12:02 +00:00
if (!read_address(&index, whitespace, &address)) return;
2023-05-04 20:10:37 +00:00
cmd = *(index++);
2023-04-30 06:12:02 +00:00
if (cmd == ',') {
2023-05-04 20:10:37 +00:00
struct LineAddress address2;
2023-04-30 06:12:02 +00:00
if (address.type != INDEX) {
2023-05-03 16:17:56 +00:00
error_s("comma range addressing requires two index addresses\n");
2023-04-30 06:12:02 +00:00
free_address(address);
return;
}
2023-05-04 20:10:37 +00:00
2023-04-30 06:12:02 +00:00
whitespace = skip_whitespace(&index);
2023-05-04 20:10:37 +00:00
2023-04-30 06:12:02 +00:00
if (!read_address(&index, whitespace, &address2)) {
free_address(address);
return;
}
2023-05-04 20:10:37 +00:00
2023-04-30 06:12:02 +00:00
if (address2.type != INDEX) {
2023-05-03 16:17:56 +00:00
error_s("comma range addressing requires two index addresses\n");
2023-04-30 06:12:02 +00:00
free_address(address);
free_address(address2);
return;
}
2023-05-04 20:10:37 +00:00
2023-04-30 06:12:02 +00:00
address.type = RANGE;
2023-05-04 20:10:37 +00:00
address.data.range.a = address.data.index.i; /* cursed */
2023-04-30 06:12:02 +00:00
address.data.range.b = address2.data.index.i;
cmd = *(index++);
}
if (address.type == RANGE && address.data.range.a > address.data.range.b) {
2023-05-03 16:17:56 +00:00
error_s("range addressing must be in ascending order\n");
2023-04-30 06:12:02 +00:00
free_address(address);
return;
}
2023-05-04 20:10:37 +00:00
linenumbers = false;
2023-04-30 18:50:00 +00:00
test:
2023-04-30 06:12:02 +00:00
switch (cmd) {
case '\0':
2023-04-30 18:50:00 +00:00
case '\n':
2023-04-30 06:12:02 +00:00
if (address.empty) {
if (line_current == line_count) {
2023-05-03 16:17:56 +00:00
error_s("line number %ld does not exist\n", line_current + 1);
2023-04-30 06:12:02 +00:00
break;
} else if (line_current + 1 == line_count) {
2023-05-03 16:17:56 +00:00
error_s("line number %ld does not exist\n", line_current + 2);
2023-04-30 06:12:02 +00:00
break;
} else {
line_current++;
}
printf("%s", lines[line_current]);
break;
}
if (address.type == INDEX) {
line_current = address.data.index.i;
} else if (address.type == RANGE) {
line_current = address.data.range.b;
} else if (address.type == SET) {
2023-05-03 16:17:56 +00:00
error_s("unexpected range addressing\n");
2023-04-30 06:12:02 +00:00
break;
}
printf("%s", lines[line_current]);
break;
case 'a':
handle_append(&address);
break;
case 'd':
handle_delete(&address);
break;
case 'c':
if (!handle_delete(&address)) { break; }
address.type = INDEX;
address.data.index.i = line_current - 1;
handle_append(&address);
break;
2023-04-30 18:50:00 +00:00
case 'n':
linenumbers = true;
__attribute__((fallthrough));
2023-04-30 06:12:02 +00:00
case 'p':
if (address.empty && address.data.index.i >= (long int) line_count) {
2023-05-03 16:17:56 +00:00
error_s("line number %ld does not exist\n", address.data.index.i + 1);
2023-04-30 06:12:02 +00:00
break;
}
if (address.type == INDEX) {
2023-04-30 18:50:00 +00:00
if (linenumbers) printf("%ld\t", address.data.index.i + 1);
2023-04-30 06:12:02 +00:00
printf("%s", lines[address.data.index.i]);
} else if (address.type == RANGE) {
2023-05-04 20:10:37 +00:00
long int i;
for (i = address.data.range.a; i <= address.data.range.b; i++) {
2023-04-30 18:50:00 +00:00
if (linenumbers) printf("%ld\t", i + 1);
2023-04-30 06:12:02 +00:00
printf("%s", lines[i]);
}
} else if (address.type == SET) {
2023-05-04 20:10:37 +00:00
unsigned long i;
for (i = 0; i < address.data.set.s; i++) {
if (linenumbers) printf("%ld\t", address.data.set.b[i] +1);
2023-04-30 06:12:02 +00:00
printf("%s", lines[address.data.set.b[i]]);
}
}
break;
case 'q':
2023-04-30 18:50:00 +00:00
if (!check_if_sure("Quit for sure? ")) break;
__attribute__((fallthrough));
2023-04-30 06:12:02 +00:00
case 'Q':
free_address(address);
2023-04-30 18:50:00 +00:00
free_data(true);
2023-04-30 06:12:02 +00:00
exit(EXIT_SUCCESS);
2023-04-30 18:50:00 +00:00
break;
2023-04-30 06:12:02 +00:00
case 'g':
skip_whitespace(&index);
free_address(address);
if (*(index++) != '/') {
2023-05-03 16:17:56 +00:00
error_s("unexpected character at start of regex\n");
2023-04-30 18:50:00 +00:00
break;
}
if (!parse_regex(&index, &address, ALL)) { return; }
skip_whitespace(&index);
if (*index == '\n' || *index == '\0') {
cmd = 'p';
} else {
cmd = *(index++);
}
goto test;
break;
2023-05-04 20:10:37 +00:00
case 's': {
char* replace = index;
long int matches, sub_len, matches_found;
unsigned long i;
2023-04-30 18:50:00 +00:00
skip_whitespace(&index);
if (*(index++) != '/') {
2023-05-03 16:17:56 +00:00
error_s("unexpected character at start of regex\n");
2023-04-30 06:12:02 +00:00
break;
}
if (!parse_regex_lines(&index, &address)) { return; }
2023-04-30 18:50:00 +00:00
while(*index != '\0' && *index != '/') index++;
if (*index != '/') {
2023-05-03 16:17:56 +00:00
error_s("/ missing after %c\n", *index);
2023-04-30 18:50:00 +00:00
break;
}
if (address.data.set.s < 1) {
2023-05-03 16:17:56 +00:00
error_s("no matches found\n");
2023-04-30 18:50:00 +00:00
break;
}
*(index++) = '\0';
if (*index == '\0' || *index == '\n') {
2023-04-30 18:50:00 +00:00
matches = 1;
} else if (*index == 'g') {
matches = -1;
} else {
char* end;
matches = strtol(index, &end, 10);
if (end == index) {
2023-05-03 16:17:56 +00:00
error_s("invalid number: %s\n", index);
2023-04-30 18:50:00 +00:00
break;
}
if (matches < 1) {
2023-05-03 16:17:56 +00:00
error_s("matches cannot be less than 1\n");
break;
}
2023-04-30 18:50:00 +00:00
}
2023-05-04 20:10:37 +00:00
sub_len = strlen(replace);
matches_found = 0;
2023-04-30 18:50:00 +00:00
2023-05-04 20:10:37 +00:00
for (i = 0; i < address.data.set.s; i++) {
2023-04-30 18:50:00 +00:00
matches_found += substute_string(address.data.set.b[i], matches, last_regex, replace, sub_len);
}
2023-05-03 16:17:56 +00:00
output("replaced %ld matches over %ld lines\n", matches_found, address.data.set.s);
2023-04-30 18:50:00 +00:00
pending_writes = true;
break;
2023-05-04 20:10:37 +00:00
}
2023-04-30 18:50:00 +00:00
case 'w': {
bool quit = false;
if (*index == 'q') {
index++;
quit = true;
}
skip_whitespace(&index);
write_file(index, &address, "w");
if (quit) {
free_address(address);
free_data(true);
exit(EXIT_SUCCESS);
2023-04-30 06:12:02 +00:00
}
break;
2023-04-30 18:50:00 +00:00
}
case 'r': {
skip_whitespace(&index);
read_file(index);
break;
}
case 'e':
if (!check_if_sure("Load new file for sure? ")) break;
__attribute__((fallthrough));
case 'E': {
2023-05-04 20:10:37 +00:00
char* filename;
FILE* file;
2023-04-30 18:50:00 +00:00
skip_whitespace(&index);
2023-05-04 20:10:37 +00:00
filename = index;
2023-04-30 18:50:00 +00:00
if(!get_file_name(&filename)) break;
2023-05-04 20:10:37 +00:00
file = get_file_s(filename, "r");
2023-04-30 18:50:00 +00:00
if (file == NULL) break;
2023-05-04 20:10:37 +00:00
2023-04-30 18:50:00 +00:00
load_file(file);
break;
}
case 'W':
skip_whitespace(&index);
write_file(index, &address, "a");
break;
case '=':
printf("%ld\n", line_current + 1);
break;
2023-04-30 06:12:02 +00:00
default:
2023-05-03 16:17:56 +00:00
error_s("unimplemented command\n");
2023-04-30 06:12:02 +00:00
break;
}
free_address(address);
}
2023-05-01 22:43:32 +00:00
static void prompt_loop(void) {
2023-04-30 06:12:02 +00:00
while (true) {
prompt();
}
}
2023-05-01 22:43:32 +00:00
static void help(void) {
printf("Usage: ed [FILE]\n\n");
printf("Edit a given [FILE] or create a new text file\n\n");
printf("\t(.,.)\tnewline\t\tgo to address line and print\n");
printf("\t(.)\ta\t\tappend new data after given line\n");
printf("\t(.,.)\tc\t\treplace given lines with new data\n");
printf("\t(.,.)\td\t\tdelete given lines\n");
printf("\t\te file\t\tdelete current buffer and edit file\n");
printf("\t\tE file\t\tdelete current buffer and edit file unconditionally\n");
printf("\t\tg/re/command\tgrep all lines with regex and run command on matches\n");
printf("\t(.,.)\tn\t\tprint given lines along with their line numbers\n");
printf("\t(.,.)\tp\t\tprint given lines\n");
printf("\t\tq\t\tquit file\n");
printf("\t\tQ\t\tquit file unconditionally\n");
printf("\t($)\tr file\t\tread file and append to end of buffer\n");
printf("\t(.,.)\ts/re/replace/\treplace the first match on each matching line\n");
printf("\t(.,.)\ts/re/replace/g\treplace all matches on each matching line\n");
printf("\t(.,.)\ts/re/replace/n\treplace n matches on each matching line\n");
printf("\t(.,.)\tw file\t\twrite contents of lines to file\n");
printf("\t(.,.)\twq file\t\twrite contents of lines to file then quit\n");
printf("\t(.,.)\tW file\t\tappend contents of line to file\n");
2023-05-01 22:43:32 +00:00
printf("\t\t=\t\tprint current line number\n");
}
2023-05-15 14:57:33 +00:00
COMMAND(ed_main) {
2023-05-01 22:43:32 +00:00
parse_help(argc, argv, help);
2023-04-30 06:12:02 +00:00
if (argc < 1) {
load_empty();
prompt_loop();
} else {
FILE* file = get_file(argv[0], "r");
load_file(file);
2023-04-30 18:50:00 +00:00
get_file_name(&argv[0]);
2023-04-30 06:12:02 +00:00
prompt_loop();
}
return EXIT_SUCCESS;
}