lazysphere/src/commands/ed.c

529 lines
15 KiB
C
Raw Normal View History

2023-04-30 06:12:02 +00:00
#include "../command.h"
#include "../util//regex.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
enum LineAddressType {
INDEX,
RANGE,
SET,
};
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 parse_regex(char** end, struct LineAddress* address, enum RegexDirection dir) {
char c;
char* index = *end;
char* regex_str = index;
while(true) {
c = *(index++);
if (c == '\0') {
fprintf(stderr, "error: missing regex after %c\n", dir == BEFORE ? '?' : '/');
return false;
}
if (c == (dir == BEFORE ? '?' : '/')) {
*(index - 1) = '\0';
break;
}
}
unsigned long cap = 8;
unsigned long siz = 0;
long int* buf = malloc(cap * sizeof(unsigned long));
re_t regex = re_compile(regex_str);
unsigned long i = (dir == ALL ? 0 : line_current);
unsigned long until = (dir == BEFORE ? 0 : line_count - 1);
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;
buf = realloc(buf, cap * sizeof(unsigned long));
}
buf[siz] = i;
siz++;
if (dir == BEFORE && i == 0) break;
}
address->type = SET;
address->data.set.s = siz;
address->data.set.c = cap;
address->data.set.b = buf;
*end = index;
return true;
}
static bool read_address(char** command, bool whitespace, struct LineAddress* a) {
char* index = *command;
struct LineAddress address;
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;
}
char* end_pre;
long int n_pre = strtol(index, &end_pre, 10) - 1;
if (end_pre == index) {
n_pre = -1;
} else {
if (n_pre < 0) {
fprintf(stderr, "error: input cannot be negative\n");
return false;
}
index = end_pre;
}
char pre = *(index++);
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 '^': {
address.type = INDEX;
char* end;
long int n = strtol(index, &end, 10) - 1;
if (n < 0) {
fprintf(stderr, "error: input cannot be negative\n");
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 < 0) {
fprintf(stderr, "error: line number %ld does not exist\n", address.data.index.i + 1);
return false;
}
break;
}
case '+': {
address.type = INDEX;
char* end;
long int n = strtol(index, &end, 10) - 1;
if (n < 0) {
fprintf(stderr, "error: input cannot be negative\n");
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) {
fprintf(stderr, "error: line number %ld does not exist\n", address.data.index.i + 1);
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) {
fprintf(stderr, "error: line number %ld does not exist\n", address.data.index.i + 1);
return false;
}
}
}
*command = index;
*a = address;
return true;
}
static void free_address (struct LineAddress address) {
if (address.type != SET) return;
free(address.data.set.b);
}
static void free_data() {
if (lines == NULL) return;
for (unsigned long i = 0; i < line_count; i++)
free(lines[i]);
free(lines);
lines = NULL;
}
static void load_empty() {
free_data();
line_capacity = 8;
lines = malloc(sizeof(char*) * line_capacity);
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;
char** buf = malloc(sizeof(char*) * cap);
char* line = NULL;
size_t offset = 0;
clearerr(stdin);
while (getline(&line, &offset, file) != -1) {
if (cap == siz) {
cap *= 2;
buf = realloc(buf, sizeof(char*) * cap);
}
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);
while ((ch = getchar()) != EOF) { // Read until EOF ...
if (i + 1 < size) {
buf[i++] = ch;
}
if (ch == '\n') { // ... or end of line
break;
}
}
buf[i] = '\0';
if (i == 0) {
return EOF;
}
return i;
}
static void load_file(FILE* file) {
free_data();
line_current = 0;
get_input(file, &lines, &line_capacity, &line_count);
if (file != stdin)
fclose(file);
}
static bool check_if_sure() {
if (!pending_writes) {
return true;
}
printf("Do you really want to quit? ");
fflush(stdout);
char buf[INPUT_LEN];
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;
lines = realloc(lines, line_capacity * sizeof(char*));
}
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) {
if (b < a) return;
pending_writes = true;
for (unsigned long i = a; i <= b; i++) {
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) {
if (address->type != INDEX) {
fprintf(stderr, "error: append command requires index addressing\n");
return false;
}
if (line_count == 0) {
address->data.index.i = -1;
}
char** buf;
unsigned long cap, size;
get_input(stdin, &buf, &cap, &size);
if (size > 0) {
append_lines(address->data.index.i + 1, buf, size);
}
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) {
fprintf(stderr, "error: 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);
} else if (address->type == RANGE) {
delete_lines(address->data.range.a, address->data.range.b);
} 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]);
}
}
return true;
}
static void prompt() {
printf("%ld: ", line_current + 1);
fflush(stdout);
char buf[INPUT_LEN];
if (ed_getline(buf, INPUT_LEN) == EOF) { putchar('\n'); return; }
if (buf[0] == '\0') { putchar('\n'); return; }
char* index = &buf[0];
bool whitespace = skip_whitespace(&index);
struct LineAddress address;
if (!read_address(&index, whitespace, &address)) return;
char cmd = *(index++);
if (cmd == ',') {
if (address.type != INDEX) {
fprintf(stderr, "error: comma range addressing requires two index addresses\n");
free_address(address);
return;
}
struct LineAddress address2;
whitespace = skip_whitespace(&index);
if (!read_address(&index, whitespace, &address2)) {
free_address(address);
return;
}
if (address2.type != INDEX) {
fprintf(stderr, "error: comma range addressing requires two index addresses\n");
free_address(address);
free_address(address2);
return;
}
address.type = RANGE;
address.data.range.a = address.data.index.i; // cursed
address.data.range.b = address2.data.index.i;
cmd = *(index++);
}
if (address.type == RANGE && address.data.range.a > address.data.range.b) {
fprintf(stderr, "error: range addressing must be in ascending order\n");
free_address(address);
return;
}
switch (cmd) {
case '\n':
case '\0':
if (address.empty) {
if (line_current == line_count) {
fprintf(stderr, "error: 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);
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) {
fprintf(stderr, "error: unexpected range addressing\n");
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;
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);
break;
}
if (address.type == INDEX) {
printf("%s", lines[address.data.index.i]);
} else if (address.type == RANGE) {
for (long int i = address.data.range.a; i <= address.data.range.b; i++) {
printf("%s", lines[i]);
}
} else if (address.type == SET) {
for (unsigned long i = 0; i < address.data.set.s; i++) {
printf("%s", lines[address.data.set.b[i]]);
}
}
break;
case 'q':
if(check_if_sure()) {
free_address(address);
free_data();
exit(EXIT_SUCCESS);
};
break;
case 'Q':
free_address(address);
free_data();
exit(EXIT_SUCCESS);
case 'g':
skip_whitespace(&index);
free_address(address);
if (*(index++) != '/') {
fprintf(stderr, "error: unexpected character at start or regex\n");
break;
}
if (!parse_regex(&index, &address, ALL)) { return; }
for (unsigned long i = 0; i < address.data.set.s; i++) {
printf("%s", lines[address.data.set.b[i]]);
}
break;
default:
fprintf(stderr, "error: unimplemented command\n");
break;
}
free_address(address);
}
static void prompt_loop() {
while (true) {
prompt();
}
}
COMMAND(ed) {
if (argc < 1) {
load_empty();
prompt_loop();
} else {
FILE* file = get_file(argv[0], "r");
load_file(file);
prompt_loop();
}
return EXIT_SUCCESS;
}