summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatolaZ <katolaz@freaknet.org>2018-05-03 22:53:01 +0100
committerKatolaZ <katolaz@freaknet.org>2018-05-03 22:53:01 +0100
commite5fc4ed9fb277d92ee24cd8e1bf5e305eb8e18f4 (patch)
tree582130f556849a086e696bb9eec282daa8889d9f
parentf6d4aa13577c2f7d8c18458123f83ea6de9712d7 (diff)
implemented REGEXP match on addresses
-rw-r--r--TODO.org11
-rw-r--r--buff.c59
-rw-r--r--buff.h1
-rw-r--r--main.c40
4 files changed, 96 insertions, 15 deletions
diff --git a/TODO.org b/TODO.org
index f409a75..b4fcbbf 100644
--- a/TODO.org
+++ b/TODO.org
@@ -7,8 +7,6 @@
* IN-PROGRESS Commands [3/21]
-** DONE c
-** DONE w
** IN-PROGRESS e
** TODO W
** TODO E
@@ -27,14 +25,19 @@
** TODO !
** TODO r !
** TODO w !
+** DONE c
+** DONE w
** DONE f
-* IN-PROGRESS Addresses [4/7]
+* IN-PROGRESS Addresses [7/9]
** TODO Test , extensively
+** DONE ?RE?
+** DONE /RE/
+*** DONE FIXME: it does not work if the second separator is missing
** DONE numbers
** DONE ,
** DONE $
-** TODO .
+** DONE .
** DONE ;
** TODO Offsets (+-)
diff --git a/buff.c b/buff.c
index 89ca82f..aa66a67 100644
--- a/buff.c
+++ b/buff.c
@@ -1,4 +1,5 @@
#include "buff.h"
+#include <regex.h>
line_t* __search_pos(int addr){
@@ -108,6 +109,8 @@ void print_lines(char lineno){
int move_to_line(int addr, char print){
+ fprintf(stderr, " >>>> move_to_line: addr: %d pos: %d\n", addr, pos);
+
if (addr > num)
return -1;
else if (addr >= 0){
@@ -307,3 +310,59 @@ int write_lines(){
printf("%d\n", tot);
return 0;
}
+
+/* match either forward (bw=0) or backward (bw=1)*/
+int match(char **c, char bw){
+
+ char sep = **c, *tmp;
+ line_t *ptr;
+ regex_t re;
+ int l;
+
+ if (bw)
+ tmp = strtok(*c, "?\n");
+ else
+ tmp = strtok(*c, "/\n");
+ fprintf(stderr, " >>>> string to match: \"%s\" bw: %d\n", tmp, bw);
+ if (!tmp)
+ return -1;
+
+ if (regcomp(&re, tmp, REG_EXTENDED)){
+ return -1;
+ }
+ *c += strlen(tmp);
+ l = pos;
+ ptr = cur;
+ do {
+ fprintf(stderr, ".");
+ if (bw){
+ ptr = ptr->prev;
+ l = (l-1 + num) % num;
+ if (!ptr){
+ /* FIXME: if we keep b_end updated we can avoid this search */
+ ptr = __search_pos(num);
+ l = num;
+ }
+ }
+ else{
+ ptr = ptr->next;
+ l = (l+1) % num;
+ if (! ptr){
+ ptr = b_start;
+ l = 1;
+ }
+ }
+ if (!l) l = num;
+ if (!regexec(&re, (char *) ptr->c , (size_t) 0, NULL, 0)){
+ regfree(&re);
+ return l;
+ }
+ fprintf(stderr, " %d\n", l);
+ } while(l != pos);
+ fprintf(stderr, " >>>> match: returning\n");
+ regfree(&re);
+ return -1;
+}
+
+
+
diff --git a/buff.h b/buff.h
index 7745518..2602434 100644
--- a/buff.h
+++ b/buff.h
@@ -36,6 +36,7 @@ void insert_lines();
void delete_lines();
int write_lines();
int read_file();
+int match(char **, char);
#endif /* __BUFF_H__ */
diff --git a/main.c b/main.c
index 1718a5b..5b58011 100644
--- a/main.c
+++ b/main.c
@@ -4,7 +4,8 @@
#include "buff.h"
#include <ctype.h>
-
+#define FW 0
+#define BW 1
line_t *b_start = NULL;
line_t *b_end = NULL;
@@ -26,7 +27,7 @@ const char* skip_blank(const char **c){
int get_addr(const char **cmd){
int addr, n, got;
- char sign = 0, comma = 0, semic = 0;
+ char sign = 0, comma = 0, semic = 0, DIR = FW;
const char *c;
addr1 = addr2 = -1;
@@ -76,6 +77,20 @@ int get_addr(const char **cmd){
return -1;
}
break;
+ case '?': DIR = BW;
+ case '/':
+ c++;
+ addr = match((char**) &c, DIR);
+ DIR = FW;
+ if (addr == -1){
+ return -1;
+ }
+ if (addr1 == -1)
+ addr1 = addr;
+ else
+ addr2 = addr1, addr1 = addr;
+ fprintf(stderr, " match (%d): addr1: %d addr2: %d pos: %d\n", DIR, addr1, addr2, pos);
+ break;
default:
goto check;
}
@@ -231,13 +246,19 @@ void main_loop(){
else
E;
break;
+ case '\0':
case '\n':
- if (addr1 > -1 && addr1 <=pos)
- move_to_line(addr1, 1);
- else if (addr2 <=pos)
- print_cur_line(p);
- else
+ fprintf(stderr, ">>>> void command\n");
+ if (ret < -1 )
E;
+ else {
+ if (addr1 > -1 && addr1 <=num)
+ move_to_line(addr1, 1);
+ else if (addr2 <=pos)
+ print_cur_line(p);
+ else
+ E;
+ }
break;
case 'q':
goto cleanup;
@@ -280,11 +301,8 @@ 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 (!read_file())
- fclose(fin);
- else{
+ if (read_file())
perror(fname);
- }
}
main_loop();
addr1 = pos, addr2 = 1;