From d46fc59c8713fdc2805ed613132f5cfabbcca1c2 Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Tue, 24 Apr 2018 17:09:16 +0100 Subject: Removed read_lines -- several fixes --- main.c | 49 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 11 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 74bd93c..2d2a1eb 100644 --- a/main.c +++ b/main.c @@ -9,8 +9,8 @@ line_t *b_start = NULL; line_t *b_end = NULL; line_t *cur = NULL; -unsigned int num = 0; -unsigned int pos = 0; +int num = 0; +int pos = 0; /* addr1 is the last address, while addr2 is the previous one */ int addr1, addr2; char *fname; @@ -87,6 +87,9 @@ int get_addr(const char **cmd){ *cmd = c; fprintf(stderr, " >>> get_addr: addr1: %d addr2: %d\n", addr1, addr2); + + if (addr1 > num || addr2 > num) + return -2; if (addr1 == -1){ if (comma){ @@ -135,11 +138,12 @@ int get_fname(char **cmd){ void main_loop(){ - char *cmd; + char *cmd, *c; int ret; char p; cmd = malloc(256 * sizeof(char)); + c = cmd; while(1){ p = 0; @@ -147,7 +151,7 @@ void main_loop(){ ret = get_addr((const char **) &cmd); - fprintf(stderr,">>> *** pos: %d address: %d ***\n", pos, ret); + fprintf(stderr,">>> *** pos: %d ret: %d ***\n", pos, ret); if (ret) { fprintf(stderr, ">>> *** addr1: %d, addr2= %d***\n", addr1, addr2); } @@ -176,6 +180,20 @@ void main_loop(){ addr2 = addr1; delete_lines(); break; + case 'e': + cmd += 1; + skip_blank((const char**)&cmd); + if (cmd[0] == '!') + //read_command(); + ; + else + if(get_fname(&cmd) >=0) + read_file(); + else{ + perror(fname); + E; + } + break; case 'f': if (ret > 0) E; @@ -210,15 +228,19 @@ void main_loop(){ move_to_line(addr2,0); print_lines(p); } + else + E; break; case '\n': - if (addr1 > -1) + if (addr1 > -1 && addr1 <=pos) move_to_line(addr1, 1); - else + else if (addr2 <=pos) print_cur_line(p); + else + E; break; case 'q': - exit(0); + goto cleanup; break; case 'w': cmd +=1; @@ -238,10 +260,14 @@ void main_loop(){ print_lineno(); break; default: - fprintf(stderr, ">>> **** unknown command: '%c'****\n", cmd[0]); + if (cmd[0]) + fprintf(stderr, ">>> **** unknown command: '%c'****\n", cmd[0]); E; } } + cleanup: + free(c); + return; } @@ -254,14 +280,15 @@ int main(int argc, char *argv[]){ if (argc > 1){ fname = malloc((strlen(argv[1])+1) * sizeof(char)); memcpy(fname, argv[1], strlen(argv[1]) + 1); - if (fin = fopen(fname, "r+")){ - read_lines(fin); + if (!read_file()) fclose(fin); - } else{ perror(fname); } } main_loop(); + addr1 = pos, addr2 = 1; + delete_lines(); + free(fname); } -- cgit v1.2.3