summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorKatolaZ <[email protected]>2018-04-20 00:40:47 +0100
committerKatolaZ <[email protected]>2018-04-20 00:40:47 +0100
commit8f936bc6c89163e3984ac619612c0ce76a51a74e (patch)
tree8d936adb262b2098a984e183e47ea9cc2a8d5d03 /main.c
first commit
Diffstat (limited to 'main.c')
-rw-r--r--main.c186
1 files changed, 186 insertions, 0 deletions
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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "buff.h"
+#include <ctype.h>
+
+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 <filein>\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();
+}
+