summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatolaZ <[email protected]>2018-04-20 18:23:24 +0100
committerKatolaZ <[email protected]>2018-04-20 18:23:24 +0100
commitb10f9522863c937c08d41e2d6da48174cdb3906a (patch)
tree96afc00c5ac6918767302b67829584fcf84bde2c
parent99c9a9e20f80f5d04dfddaf19e18d1179c65c91e (diff)
Implemented 'c' command
-rw-r--r--Makefile8
-rw-r--r--TODO.org22
-rw-r--r--buff.c147
-rw-r--r--buff.h4
-rw-r--r--main.c42
-rw-r--r--obf.ed33
6 files changed, 223 insertions, 33 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..435b4a3
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,8 @@
+CC=gcc
+CFLAGS=-Os
+
+all: myed
+
+myed: main.c buff.c buff.h
+ $(CC) $(CFLAGS) -o myed main.c buff.c
+ strip myed
diff --git a/TODO.org b/TODO.org
new file mode 100644
index 0000000..b4c0c48
--- /dev/null
+++ b/TODO.org
@@ -0,0 +1,22 @@
+#+TODO: TODO IN-PROGRESS | DONE
+
+
+* TODO Commands
+** DONE c
+** TODO w
+** TODO W
+** TODO e
+** TODO E
+** TODO f
+** TODO h
+** TODO H
+** TODO j
+** TODO Q
+** IN-PROGRESS q -- check for changes
+** TODO r
+** TODO s
+** TODO t
+** TODO m
+** TODO !
+** TODO k
+** TODO g,G,v,V
diff --git a/buff.c b/buff.c
index f0ef72d..96cb12f 100644
--- a/buff.c
+++ b/buff.c
@@ -14,11 +14,13 @@ int read_lines(FILE *fin){
char buff[4096];
line_t *line;
- size_t s;
+ size_t s, tot;
+ tot = 0;
while(feof(fin) == 0){
if (!fgets(buff, 4095, fin)) break;
s = strlen(buff) + 1;
+ tot += s-1;
line = malloc(sizeof(line_t));
line->s = s;
line->c = malloc(s * sizeof(char));
@@ -38,12 +40,17 @@ int read_lines(FILE *fin){
}
cur = b_end;
pos = num;
+ printf("%d\n", tot);
}
+
+
+
+
void print_cur_line(char lineno){
if (!b_start){
- printf("?\n");
+ E;
return;
}
@@ -95,10 +102,10 @@ void print_lineno(){
int move_forward(int n){
if (pos + n > num){
- printf("?\n");
+ E;
return -1;
}
- while(n-- >0){
+ while(n-- > 0){
cur = cur->next;
}
pos += n;
@@ -108,7 +115,7 @@ int move_forward(int n){
int move_backward(int n){
if (pos - n < 1){
- printf("?\n");
+ E;
return -1;
}
while(n-- >0){
@@ -118,17 +125,14 @@ int move_backward(int n){
return 0;
}
-
-
-/* add lines after pos */
-void append_lines(){
+int __get_lines(line_t **first, line_t **last){
char buff[4096];
int n,s;
- line_t *first, *l, *last;
+ line_t *l;
n = 0;
- first = last = NULL;
+ *first = *last = NULL;
while(feof(stdin) == 0){
if (!fgets(buff, 4095, stdin)) break;
if (buff[0] == '.')
@@ -138,29 +142,134 @@ void append_lines(){
l->c = malloc(s * sizeof(char));
memcpy(l->c, buff, s);
l->s = s;
- if (first == NULL)
- first = last = l;
+ if (*first == NULL)
+ *first = *last = l;
else{
- last -> next = l;
- l->prev = last;
- last = l;
+ (*last) -> next = l;
+ l->prev = *last;
+ *last = l;
}
n += 1;
}
+ return n;
+}
+
+
+/* add lines after pos */
+void append_lines(){
+
+ int n;
+ line_t *first, *last;
+
+ first = last = NULL;
+ n = __get_lines(&first, &last);
+
if (addr1 == 0){
+ fprintf(stderr, " >>> append (zero): pos: %d n:%d\n", pos, n);
last->next = b_start;
b_start -> prev = last;
b_start = first;
+ num += n;
move_to_line(n,0);
}
else{
+ fprintf(stderr, " >>> append (non-zero): pos: %d n:%d\n", pos, n);
last -> next = cur -> next;
cur->next = first;
first->prev = cur;
- move_forward(n);
+ num += n;
+ //move_forward(n);
+ pos += n;
+ cur = last;
+ }
+}
+
+void insert_lines(){
+
+ int n;
+ line_t *first, *last;
+
+ first = last = NULL;
+ n = __get_lines(&first, &last);
+
+ if (addr1 == 0){
+ fprintf(stderr, " >>> insert (zero): pos: %d n:%d\n", pos, n);
+ last->next = b_start;
+ b_start -> prev = last;
+ b_start = first;
+ num += n;
+ move_to_line(n,0);
+ }
+ else{
+ fprintf(stderr, " >>> insert (non-zero): pos: %d n:%d\n", pos, n);
+
+ cur -> prev -> next = first;
+ first -> prev = cur -> prev;
+ last -> next = cur;
+ num += n;
+ pos -=1 ;
+ cur = last;
}
}
-void insert_lines(){}
-void delete_lines(){}
+void delete_lines(){
+
+ line_t *handle;
+
+ if (addr1 == -1){
+ addr1 = pos;
+ }
+ if (addr2 == -1){
+ addr2 = addr1;
+ }
+
+ fprintf(stderr, " >>> delete: addr1: %d addr2: %d\n", addr1, addr2);
+
+ move_to_line(addr2,0);
+
+ if (addr2 <= 1){ /* we are deleting the first line */
+ while(pos <= addr1){
+ free(cur->c);
+ cur = cur -> next;
+ if (cur)
+ free(cur -> prev);
+ pos ++;
+ }
+ b_start = cur;
+ if (cur){
+ pos = 1;
+ cur = b_start;
+ }
+ else
+ pos = 0;
+ }
+ else{
+ handle = cur->prev;
+ while(pos < addr1){
+ free(cur->c);
+ cur = cur -> next;
+ free(cur->prev);
+ pos ++;
+ }
+ handle->next = cur -> next;
+ free(cur->c);
+ free(cur);
+ if (handle -> next){
+ pos = addr2 ;
+ cur = handle->next;
+ }
+ else{
+ pos = addr2 - 1;
+ cur = handle;
+ }
+ }
+ num -= addr1 - addr2 + 1;
+}
+
+
+void change_lines(){
+ delete_lines();
+ addr2 = addr1 =pos;
+ append_lines();
+}
diff --git a/buff.h b/buff.h
index 2d55c04..740006d 100644
--- a/buff.h
+++ b/buff.h
@@ -5,6 +5,10 @@
#include <stdlib.h>
#include <string.h>
+#define E printf("?\n")
+#define CHECKADDR if (addr1 < addr2){ E; break;}
+
+
typedef struct line_t {
char *c;
unsigned int s;
diff --git a/main.c b/main.c
index aeeb680..e0b60ff 100644
--- a/main.c
+++ b/main.c
@@ -4,12 +4,14 @@
#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 */
+/* addr1 is the last address, while addr2 is the previous one */
int addr1, addr2;
@@ -117,28 +119,40 @@ void main_loop(){
}
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 'c':
+ if (addr1 > -1)
+ move_to_line(addr1,0);
+ delete_lines();
+ addr1 = addr2 = pos;
+ append_lines();
case 'd':
+ if (ret == -1){
+ E;
+ break;
+ }
+ if (ret == 2){
+ CHECKADDR;
+ }
+ if (ret == 1)
+ addr2 = addr1;
delete_lines();
break;
+ case 'i':
+ if (addr1 > -1)
+ move_to_line(addr1, 0);
+ insert_lines();
+ break;
case 'n': p = 1;
case 'p':
- if (ret == 2)
- if (addr1 < addr2){
- printf("?\n");
- break;
- }
- else
- print_lines(p);
+ if (ret == 2){
+ CHECKADDR;
+ print_lines(p);
+ }
else if (ret == 1)
move_to_line(addr2, 1);
else if (ret == 0)
@@ -166,7 +180,7 @@ void main_loop(){
break;
default:
fprintf(stderr, ">>> **** unknown command: '%c'****\n", cmd[0]);
- printf("?\n");
+ E;
}
}
}
diff --git a/obf.ed b/obf.ed
new file mode 100644
index 0000000..1d964a6
--- /dev/null
+++ b/obf.ed
@@ -0,0 +1,33 @@
+H
+1,$s/b_start/bs/g
+1,$s/buff/b/g
+1,$s/line_t/_/g
+1,$s/next/n/g
+1,$s/addr/a/g
+1,$s/prev/p/g
+1,$s/first/f/g
+1,$s/last/ll/g
+1,$s/move_to_line/mtl/g
+1,$s/skip_blank/sb/g
+1,$s/get_addr/ga/g
+1,$s/main_loop/mp/g
+1,$s/append_lines/al/g
+1,$s/delete_lines/dl/g
+1,$s/read_lines/rl/g
+1,$s/insert_lines/il/g
+1,$s/print_lines/pl/g
+1,$s/print_cur_line/pcl/g
+1,$s/print_lineno/pn/g
+1,$s/search_pos/sp/g
+1,$s/get_lines/gl/g
+1,$s/line/l/g
+1,$s/pos/p/g
+1,$s/cur/c/g
+1,$s/fprintf.*//g
+1,$s/ -> /->/g
+1,$s/; */;/g
+1,$s/ = /=/g
+1,$s/ == /==/g
+1,$s/sizeof/S/g
+,p
+Q