summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvi Levin <avi.levin@samsung.com>2015-08-27 13:32:14 +0300
committerAvi Levin <avi.levin@samsung.com>2015-08-27 13:32:14 +0300
commit19477e5363d7ea4c0dd74b21f2b4b73a587fea96 (patch)
tree5c4a0aab82b125508ae04c95aa71f5155630303c
parentd6338fb1580c9e5bbd2d5323dbf9e8cb54190d12 (diff)
Add autocomplete option to commands with Tab
-rw-r--r--src/bin/cli.c42
-rw-r--r--src/lib/CMakeLists.txt2
-rw-r--r--src/lib/auto_complete.c115
-rw-r--r--src/lib/auto_complete.h29
4 files changed, 187 insertions, 1 deletions
diff --git a/src/bin/cli.c b/src/bin/cli.c
index 346c556..2a61dce 100644
--- a/src/bin/cli.c
+++ b/src/bin/cli.c
@@ -9,9 +9,12 @@
9#include "debug_eo.h" 9#include "debug_eo.h"
10#include "debug_eolian.h" 10#include "debug_eolian.h"
11#include "debug_common.h" 11#include "debug_common.h"
12#include "auto_complete.h"
12 13
13#define MAX_EVENTS 5 14#define MAX_EVENTS 5
14 15
16struct Node *root = NULL;
17
15static Eina_Hash *vars_hash = NULL; 18static Eina_Hash *vars_hash = NULL;
16static Eina_Bool exit_required = EINA_FALSE; 19static Eina_Bool exit_required = EINA_FALSE;
17 20
@@ -343,6 +346,32 @@ _char_consume(Lexer *l, char c)
343 printf("%s", l->buffer); 346 printf("%s", l->buffer);
344 break; 347 break;
345 } 348 }
349 case 0x09: /* Tab */
350 {
351 char *word = _next_word(l, NULL);
352 char buffer[MAX];
353 buffer[0] = 0;
354
355 if(word){
356 struct Node *newR = searchTSTA(root, word);
357 if(newR)
358 traverseTSTUtil(newR , buffer, 0, 1);
359 }
360 printf("\r%*c\r", l->buf_end, ' ');
361
362 if(buffer[0]){
363 strcpy(l->buffer+l->buf_end, buffer);
364 printf("%s", l->buffer+l->buf_end-strlen(word));
365
366 l->buf_end += strlen(buffer);
367 l->current -= strlen(word);
368 }
369 else {
370 l->buffer[l->buf_end] = '\0';
371 printf("%s", l->buffer);
372 }
373 break;
374 }
346 default: 375 default:
347 { 376 {
348 l->buffer[l->buf_end++] = c; 377 l->buffer[l->buf_end++] = c;
@@ -419,6 +448,19 @@ _thread_start(void *arg EINA_UNUSED)
419int 448int
420main() 449main()
421{ 450{
451
452 int i = 0;
453 while (good_commands[i])
454 {
455
456 insert(&root, good_commands[i]);
457 i+=2;
458
459 }
460
461 printf("Following is traversal of ternary search tree\n");
462 traverseTST(root);
463
422 struct termios oldt, newt; 464 struct termios oldt, newt;
423 465
424 Lexer l; 466 Lexer l;
diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt
index 153f19a..cc3a328 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) 3LIST(APPEND EDBG_COMMON_SOURCES network.c debug_common.c debug_eo.c debug_eolian.c auto_complete.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/auto_complete.c b/src/lib/auto_complete.c
new file mode 100644
index 0000000..5f93bad
--- /dev/null
+++ b/src/lib/auto_complete.c
@@ -0,0 +1,115 @@
1#include <stdlib.h>
2#include "auto_complete.h"
3
4// A utility function to create a new ternary search tree node
5struct Node* newNode(char data)
6{
7 struct Node* temp = (struct Node*) malloc(sizeof( struct Node ));
8 temp->data = data;
9 temp->isEndOfString = 0;
10 temp->left = temp->eq = temp->right = NULL;
11 return temp;
12}
13
14// Function to insert a new word in a Ternary Search Tree
15void insert(struct Node** root, char *word)
16{
17 // Base Case: Tree is empty
18 if (!(*root))
19 *root = newNode(*word);
20
21 // If current character of word is smaller than root's character,
22 // then insert this word in left subtree of root
23 if ((*word) < (*root)->data)
24 insert(&( (*root)->left ), word);
25
26 // If current character of word is greate than root's character,
27 // then insert this word in right subtree of root
28 else if ((*word) > (*root)->data)
29 insert(&( (*root)->right ), word);
30
31 // If current character of word is same as root's character,
32 else
33 {
34 if (*(word+1))
35 insert(&( (*root)->eq ), word+1);
36
37 // the last character of the word
38 else
39 (*root)->isEndOfString = 1;
40 }
41}
42
43// A recursive function to traverse Ternary Search Tree
44void traverseTSTUtil(struct Node* root, char* buffer, int depth, int limit)
45{
46 if (root && limit > 0)
47 {
48 // First traverse the left subtree
49 traverseTSTUtil(root->left, buffer, depth, limit);
50
51 // Store the character of this node
52 buffer[depth] = root->data;
53 if (root->isEndOfString)
54 {
55 buffer[depth+1] = '\0';
56 // printf( "%s\n", buffer+1);
57 limit--;
58 }
59
60 // Traverse the subtree using equal pointer (middle subtree)
61 traverseTSTUtil(root->eq, buffer, depth + 1, limit);
62
63 // Finally Traverse the right subtree
64 traverseTSTUtil(root->right, buffer, depth, limit);
65 }
66}
67
68// The main function to traverse a Ternary Search Tree.
69// It mainly uses traverseTSTUtil()
70void traverseTST(struct Node* root)
71{
72 char buffer[MAX];
73 traverseTSTUtil(root, buffer, 0, 100);
74}
75
76// Function to search a given word in TST
77int searchTST(struct Node *root, char *word)
78{
79 if (!root)
80 return 0;
81
82 if (*word < (root)->data)
83 return searchTST(root->left, word);
84
85 else if (*word > (root)->data)
86 return searchTST(root->right, word);
87
88 else
89 {
90 if (*(word+1) == '\0')
91 return root->isEndOfString;
92
93 return searchTST(root->eq, word+1);
94 }
95}
96 // Function to search a given word in TST
97struct Node * searchTSTA(struct Node *root, char *word)
98{
99 if (!root)
100 return NULL;
101
102 if (*word < (root)->data)
103 return searchTSTA(root->left, word);
104
105 else if (*word > (root)->data)
106 return searchTSTA(root->right, word);
107
108 else
109 {
110 if (*(word+1) == '\0')
111 return root->eq;
112
113 return searchTSTA(root->eq, word+1);
114 }
115}
diff --git a/src/lib/auto_complete.h b/src/lib/auto_complete.h
new file mode 100644
index 0000000..d5407cd
--- /dev/null
+++ b/src/lib/auto_complete.h
@@ -0,0 +1,29 @@
1#define MAX 50
2
3// A node of ternary search tree
4struct Node
5{
6 char data;
7
8 // True if this character is last character of one of the words
9 unsigned isEndOfString: 1;
10
11 struct Node *left, *eq, *right;
12};
13
14// Function to insert a new word in a Ternary Search Tree
15void insert(struct Node** root, char *word);
16
17 // Function to search a given word in TST
18struct Node * searchTSTA(struct Node *root, char *word);
19
20// A recursive function to traverse Ternary Search Tree
21void traverseTSTUtil(struct Node* root, char* buffer, int depth, int limit);
22
23// The main function to traverse a Ternary Search Tree.
24// It mainly uses traverseTSTUtil()
25void traverseTST(struct Node* root);
26
27 // Function to search a given word in TST
28struct Node * searchTSTA(struct Node *root, char *word);
29