From 8f936bc6c89163e3984ac619612c0ce76a51a74e Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Fri, 20 Apr 2018 00:40:47 +0100 Subject: first commit --- main.c | 186 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 main.c (limited to 'main.c') diff --git a/main.c b/main.c new file mode 100644 index 0000000..0599575 --- /dev/null +++ b/main.c @@ -0,0 +1,186 @@ +#include +#include +#include +#include "buff.h" +#include + +line_t *b_start = NULL; +line_t *b_end = NULL; +line_t *cur = NULL; +unsigned int num = 0; +unsigned int pos = 0; +/* addr1 is the lasr address, while addr2 is the previous one */ +int addr1, addr2; + + + +const char* skip_blank(const char **c){ + + while (isblank(**c)) (*c)++; + return *c; +} + + +int get_addr(const char **cmd){ + + int addr, n, got; + char sign = 0, comma = 0; + const char *c; + + addr1 = addr2 = -1; + c = *cmd; + while(*c){ + c = skip_blank(&c); + n = 0; + got = 0; + if (isdigit(*c)){ + while (isdigit(*c)){ + got = 1; + n = n * 10 + *c - '0', c++; + } + if (got){ + if (addr1 > -1 ) + addr2 = addr1; + addr1 = n; + } + } + else { + switch(*c){ + case '.': + if (addr1 > -1) + addr2 = addr1; + addr1 = pos; + break; + case '$': + if (addr1 > -1) + addr2 = addr1; + addr1 = num; + break; + case ',': + comma = 1; + if (addr1 < addr2){ + *cmd = c; + return -1; + } + break; + default: + goto check; + } + c++; + } + } + check: + *cmd = c; + + if (addr1 == -1){ + if (comma){ + addr1 = num; + addr2 = 1; + return 2; + } + addr1 = addr2 = pos; + return 0; + } + if (addr2 == -1){ + addr2 = pos; + return 1; + } + return 2; +} + + +void main_loop(){ + + char *cmd; + int ret; + char p; + + cmd = malloc(256 * sizeof(char)); + + while(1){ + p = 0; + fgets(cmd, 255, stdin); + + ret = get_addr((const char **) &cmd); + + printf(">>> *** pos: %d address: %d ***\n", pos, ret); + if (ret) { + printf(">>> *** addr1: %d, addr2= %d***\n", addr1, addr2); + } + + switch(cmd[0]){ + case 'i': + if (addr1 > -1) + move_to_line(addr1, 0); + insert_lines(); + break; + case 'a': + if (addr1 > -1) + move_to_line(addr1, 0); + append_lines(); + break; + case 'd': + delete_lines(); + break; + case 'n': p = 1; + case 'p': + if (ret == 2) + if (addr1 < addr2){ + printf("?\n"); + break; + } + else + print_lines(p); + else if (ret == 1) + move_to_line(addr2, 1); + else if (ret == 0) + print_cur_line(p); + else if (ret == -1){ + addr1 = num; + addr2 = 1; + move_to_line(addr2,0); + print_lines(p); + } + break; + case '\n': + if (addr1 > -1) + move_to_line(addr1, 1); + else + print_cur_line(p); + break; + case 'q': + exit(0); + break; + case '=': + if (addr1 == -1) + addr1 = num; + print_lineno(); + break; + default: + printf(">>> **** unknown command: '%c'****\n", cmd[0]) + ; + } + } +} + + + +int main(int argc, char *argv[]){ + + FILE *fin; + + if (argc < 2){ + printf("Usage: %s \n", argv[0]); + exit(1); + } + + if ((fin = fopen(argv[1], "r+"))){ + read_lines(fin); + } + else{ + printf("Error opening file %s\n", argv[1]); + } + + main_loop(); +} + -- cgit v1.2.3