summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvi Levin <avi.levin@samsung.com>2015-09-03 18:43:28 +0300
committerAvi Levin <avi.levin@samsung.com>2015-09-03 18:43:28 +0300
commitf8b56d827e9fe5f306cc52aa1ed780acfcb258df (patch)
tree28c01c7c83243daa467c5813a242d825f8b82cc4
parentdfd9a754ac6ff30632ad4b5b88cb2de538b2500a (diff)
Adding our own implemention of regex
Currently support : *, + and . . * = zero or more times the code/char char that comes before. . = any character
-rw-r--r--src/bin/cli.c4
-rw-r--r--src/lib/CMakeLists.txt2
-rw-r--r--src/lib/debug_eo.c13
-rw-r--r--src/lib/regex.c105
-rw-r--r--src/lib/regex.h16
5 files changed, 130 insertions, 10 deletions
diff --git a/src/bin/cli.c b/src/bin/cli.c
index 35b093c..5778b11 100644
--- a/src/bin/cli.c
+++ b/src/bin/cli.c
@@ -272,7 +272,7 @@ _char_consume(Lexer *l, char c)
272 { 272 {
273 int size, size2; 273 int size, size2;
274 void *buf, *buf2; 274 void *buf, *buf2;
275 char *word = _next_word(l, NULL); 275 char *word = _next_word(l, "|.^$*+?()[");
276 debug_eo_list_request_pack(word, &buf, &size); 276 debug_eo_list_request_pack(word, &buf, &size);
277 free(word); 277 free(word);
278 debug_pack("LIST_REQ", "LIST_RESP", buf, size, &buf2, &size2); 278 debug_pack("LIST_REQ", "LIST_RESP", buf, size, &buf2, &size2);
@@ -369,7 +369,7 @@ _char_consume(Lexer *l, char c)
369 } 369 }
370 case 0x09: /* Tab */ 370 case 0x09: /* Tab */
371 { 371 {
372 char *word = _last_word(l, NULL); 372 const char *word = _last_word(l, NULL);
373 373
374 char *buffer; 374 char *buffer;
375 Eina_List *comps = NULL; 375 Eina_List *comps = NULL;
diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt
index 9713fb7..ee46053 100644
--- a/src/lib/CMakeLists.txt
+++ b/src/lib/CMakeLists.txt
@@ -1,6 +1,6 @@
1set(CMAKE_BUILD_TYPE Debug) 1set(CMAKE_BUILD_TYPE Debug)
2 2
3LIST(APPEND EDBG_COMMON_SOURCES network.c debug_common.c debug_eo.c debug_eolian.c auto_complete.c) 3LIST(APPEND EDBG_COMMON_SOURCES network.c debug_common.c debug_eo.c debug_eolian.c auto_complete.c regex.c)
4LIST(APPEND EDBG_PRELOAD_SOURCES edbg_preload.c ${EDBG_COMMON_SOURCES}) 4LIST(APPEND EDBG_PRELOAD_SOURCES edbg_preload.c ${EDBG_COMMON_SOURCES})
5 5
6STRING(REGEX REPLACE "\n" "" EOLIAN_EO_DIR ${EOLIAN_EO_DIR}) 6STRING(REGEX REPLACE "\n" "" EOLIAN_EO_DIR ${EOLIAN_EO_DIR})
diff --git a/src/lib/debug_eo.c b/src/lib/debug_eo.c
index 4355827..03dc38f 100644
--- a/src/lib/debug_eo.c
+++ b/src/lib/debug_eo.c
@@ -1,5 +1,5 @@
1#include <Eet.h> 1#include <Eet.h>
2#include <regex.h> 2#include "regex.h"
3 3
4#include "debug_eo.h" 4#include "debug_eo.h"
5#include "debug_common.h" 5#include "debug_common.h"
@@ -29,9 +29,9 @@ _list_req_handle(const void *buf, int size, void **buf_ret, int *size_ret)
29 Obj_Info *info; 29 Obj_Info *info;
30 if (req->classname) 30 if (req->classname)
31 { 31 {
32 regex_t regex; 32 struct regex_compiled regex;
33 33
34 if (regcomp(&regex, req->classname, 0)) 34 if (!regex_compile(&regex, req->classname))
35 { 35 {
36 fprintf(stderr, "Could not compile regex\n"); 36 fprintf(stderr, "Could not compile regex\n");
37 resp.objs = objs_list;; 37 resp.objs = objs_list;;
@@ -40,11 +40,11 @@ _list_req_handle(const void *buf, int size, void **buf_ret, int *size_ret)
40 { 40 {
41 EINA_LIST_FOREACH(objs_list, itr, info) 41 EINA_LIST_FOREACH(objs_list, itr, info)
42 { 42 {
43 if (!regexec(&regex, info->kl_name, 0, NULL, 0)){ 43 if (regex_match(&regex, info->kl_name, 0)){
44 /* Update regex parent */ 44 /* Update regex parent */
45 Eo *curr_parent = info->ptr_parent; 45 Eo *curr_parent = info->ptr_parent;
46 while (curr_parent && regexec(&regex, 46 while (curr_parent && regex_match(&regex,
47 eo_class_name_get(curr_parent), 0, NULL, 0)) 47 eo_class_name_get(curr_parent), 0))
48 { 48 {
49 eo_do(curr_parent, curr_parent = eo_parent_get()); 49 eo_do(curr_parent, curr_parent = eo_parent_get());
50 } 50 }
@@ -53,7 +53,6 @@ _list_req_handle(const void *buf, int size, void **buf_ret, int *size_ret)
53 } 53 }
54 } 54 }
55 } 55 }
56 regfree(&regex);
57 } 56 }
58 else resp.objs = objs_list; 57 else resp.objs = objs_list;
59 58
diff --git a/src/lib/regex.c b/src/lib/regex.c
new file mode 100644
index 0000000..e5777a4
--- /dev/null
+++ b/src/lib/regex.c
@@ -0,0 +1,105 @@
1#include <string.h>
2#include <stdio.h>
3#include "regex.h"
4
5enum {BRANCH, ANY, EXACT, STAR, PLUS};
6
7char* opcodes[] = {
8 "BRANCH", /* Alternative operator, "|" */
9 "ANY", /* Match any character, "." */
10 "EXACT", /* Match exact string */
11 "STAR", /* Match zero or more times "*" */
12 "PLUS" /* Match one or more times, "+" */
13};
14
15//copy just last letter from last code
16void fix_prev(struct regex_compiled *sre)
17{
18 int temp_code = sre->code[sre->code_size - 1][0];
19
20 if(sre->code[sre->code_size - 2][2] > 1){
21 /* Split to two codes */
22 sre->code[sre->code_size - 2][2]--;
23
24 sre->code[sre->code_size - 1][2] = 1;
25 sre->code[sre->code_size - 1][1] = sre->code[sre->code_size - 2][1]+sre->code[sre->code_size - 2][2];
26 sre->code[sre->code_size - 1][0] = sre->code[sre->code_size - 2][0];
27 sre->code_size++;
28 }
29 /* Exchange with new code */
30 sre->code[sre->code_size - 1][0] = sre->code[sre->code_size - 2][0];
31 sre->code[sre->code_size - 1][1] = sre->code[sre->code_size - 2][1];
32 sre->code[sre->code_size - 1][2] = sre->code[sre->code_size - 2][2];
33 sre->code[sre->code_size - 2][0] = temp_code;
34}
35
36void regex_dump(struct regex_compiled *sre)
37{
38 int i = 0;
39
40 for(; i < sre->code_size; i++)
41 printf("%s %d %d\n", opcodes[sre->code[i][0]], sre->code[i][1], sre->code[i][2]);
42}
43
44int regex_compile(struct regex_compiled *sre, const char *re)
45{
46 sre->code_size = sre->data_size = 0;
47
48 while(*re){
49 sre->code[sre->code_size][1] = 0;
50 sre->code[sre->code_size][2] = 1;
51 switch(*re){
52
53 case '*':
54 case '+':
55 sre->code[sre->code_size++][0] = *re == '*' ? STAR : PLUS;
56 if(sre->code_size > 1 )
57 fix_prev(sre);
58 else return -1;
59 break;
60 case '.':
61 sre->code[sre->code_size++][0] = ANY;
62 break;
63 default:
64 sre->data[sre->data_size++] = *re;
65 if(sre->data_size == 0 || sre->code[sre->code_size-1][0] != EXACT)
66 {
67 sre->code[sre->code_size++][0] = EXACT;
68 sre->code[sre->code_size-1][1] = sre->data_size - 1;
69 }
70 sre->code[sre->code_size-1][2] = sre->data_size - sre->code[sre->code_size-1][1];
71 }
72 re++;
73 }
74 sre->data[sre->data_size] = 0;
75
76 return 1;
77}
78
79int regex_match(struct regex_compiled *sre, const char *str, int code_pos)
80{
81 int res = 1;
82
83 while(res && (code_pos < sre->code_size) && *str)
84 {
85 switch(sre->code[code_pos][0]){
86
87 case STAR:
88 case PLUS:
89 if(sre->code[code_pos][0] == PLUS || !regex_match(sre, str, code_pos+2))
90 while(*str && !(res = regex_match(sre, str, code_pos+1))){str++;}
91 return res;
92 case ANY:
93 str++;
94 break;
95 default:
96 if (strncmp(sre->data+sre->code[code_pos][1], str, sre->code[code_pos][2]))
97 res = 0;
98 str += sre->code[code_pos][2];
99 }
100 code_pos++;
101 }
102
103 return (code_pos >= sre->code_size || ( code_pos = sre->code_size - 2 && sre->code[code_pos][0] == STAR))
104 && res && !*str;
105}
diff --git a/src/lib/regex.h b/src/lib/regex.h
new file mode 100644
index 0000000..e83cb30
--- /dev/null
+++ b/src/lib/regex.h
@@ -0,0 +1,16 @@
1/*
2 * Compiled regular expression
3 */
4struct regex_compiled {
5 unsigned int code[128][3];
6 char data[128];
7 int code_size;
8 int data_size;
9};
10
11int regex_compile(struct regex_compiled *sre, const char *re);
12
13int regex_match(struct regex_compiled *sre, const char *str, int code_pos);
14
15
16void regex_dump(struct regex_compiled *sre);