about summary refs log tree commit diff stats
path: root/src/tools
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2015-01-16 22:50:40 +0000
committerJames Booth <boothj5@gmail.com>2015-01-16 22:50:40 +0000
commit4c6cfcdca0f7aec1f0e3243a60d88ed494357a5d (patch)
tree262aea8ef8992ae962aab0946d7f948a068d7808 /src/tools
parent7a5deca77c4f1b792dde0a1a361b02fa86807a68 (diff)
downloadprofani-tty-4c6cfcdca0f7aec1f0e3243a60d88ed494357a5d.tar.gz
Simplified autocompleters and command history
Diffstat (limited to 'src/tools')
-rw-r--r--src/tools/autocomplete.c54
-rw-r--r--src/tools/autocomplete.h10
-rw-r--r--src/tools/parser.c4
-rw-r--r--src/tools/parser.h4
4 files changed, 31 insertions, 41 deletions
diff --git a/src/tools/autocomplete.c b/src/tools/autocomplete.c
index 486fd2ba..2623c828 100644
--- a/src/tools/autocomplete.c
+++ b/src/tools/autocomplete.c
@@ -169,7 +169,7 @@ autocomplete_contains(Autocomplete ac, const char *value)
 }
 
 gchar *
-autocomplete_complete(Autocomplete ac, gchar *search_str, gboolean quote)
+autocomplete_complete(Autocomplete ac, const gchar *search_str, gboolean quote)
 {
     gchar *found = NULL;
 
@@ -216,8 +216,7 @@ autocomplete_complete(Autocomplete ac, gchar *search_str, gboolean quote)
 }
 
 char *
-autocomplete_param_with_func(char *input, int *size, char *command,
-    autocomplete_func func)
+autocomplete_param_with_func(const char * const input, char *command, autocomplete_func func)
 {
     GString *auto_msg = NULL;
     char *result = NULL;
@@ -225,15 +224,16 @@ autocomplete_param_with_func(char *input, int *size, char *command,
     sprintf(command_cpy, "%s ", command);
     int len = strlen(command_cpy);
 
-    if ((strncmp(input, command_cpy, len) == 0) && (*size > len)) {
+    if ((strncmp(input, command_cpy, len) == 0) && (strlen(input) > len)) {
         int i;
-        char inp_cpy[*size];
-        for(i = len; i < *size; i++) {
-            inp_cpy[i-len] = input[i];
+        int inp_len = strlen(input);
+        char prefix[inp_len];
+        for(i = len; i < inp_len; i++) {
+            prefix[i-len] = input[i];
         }
-        inp_cpy[(*size) - len] = '\0';
+        prefix[inp_len - len] = '\0';
 
-        char *found = func(inp_cpy);
+        char *found = func(prefix);
         if (found) {
             auto_msg = g_string_new(command_cpy);
             g_string_append(auto_msg, found);
@@ -247,23 +247,23 @@ autocomplete_param_with_func(char *input, int *size, char *command,
 }
 
 char *
-autocomplete_param_with_ac(char *input, int *size, char *command,
-    Autocomplete ac, gboolean quote)
+autocomplete_param_with_ac(const char * const input, char *command, Autocomplete ac, gboolean quote)
 {
     GString *auto_msg = NULL;
     char *result = NULL;
     char *command_cpy = malloc(strlen(command) + 2);
     sprintf(command_cpy, "%s ", command);
     int len = strlen(command_cpy);
-    if ((strncmp(input, command_cpy, len) == 0) && (*size > len)) {
+    int inp_len = strlen(input);
+    if ((strncmp(input, command_cpy, len) == 0) && (strlen(input) > len)) {
         int i;
-        char inp_cpy[*size];
-        for(i = len; i < *size; i++) {
-            inp_cpy[i-len] = input[i];
+        char prefix[inp_len];
+        for(i = len; i < inp_len; i++) {
+            prefix[i-len] = input[i];
         }
-        inp_cpy[(*size) - len] = '\0';
+        prefix[inp_len - len] = '\0';
 
-        char *found = autocomplete_complete(ac, inp_cpy, quote);
+        char *found = autocomplete_complete(ac, prefix, quote);
         if (found) {
             auto_msg = g_string_new(command_cpy);
             g_string_append(auto_msg, found);
@@ -278,28 +278,18 @@ autocomplete_param_with_ac(char *input, int *size, char *command,
 }
 
 char *
-autocomplete_param_no_with_func(char *input, int *size, char *command,
-    int arg_number, autocomplete_func func)
+autocomplete_param_no_with_func(const char * const input, char *command, int arg_number, autocomplete_func func)
 {
-    if (strncmp(input, command, strlen(command)) == 0 && (*size > strlen(command))) {
-        int i = 0;
+    if (strncmp(input, command, strlen(command)) == 0 && (strlen(input) > strlen(command))) {
         GString *result_str = NULL;
 
-        // copy and null terminate input
-        gchar inp_cpy[*size];
-        for (i = 0; i < *size; i++) {
-            inp_cpy[i] = input[i];
-        }
-        inp_cpy[i] = '\0';
-        g_strstrip(inp_cpy);
-
         // count tokens properly
-        int num_tokens = count_tokens(inp_cpy);
+        int num_tokens = count_tokens(input);
 
         // if correct number of tokens, then candidate for autocompletion of last param
         if (num_tokens == arg_number) {
-            gchar *start_str = get_start(inp_cpy, arg_number);
-            gchar *comp_str = g_strdup(&inp_cpy[strlen(start_str)]);
+            gchar *start_str = get_start(input, arg_number);
+            gchar *comp_str = g_strdup(&input[strlen(start_str)]);
 
             // autocomplete param
             if (comp_str) {
diff --git a/src/tools/autocomplete.h b/src/tools/autocomplete.h
index a029b7ef..70cd8f30 100644
--- a/src/tools/autocomplete.h
+++ b/src/tools/autocomplete.h
@@ -37,7 +37,7 @@
 
 #include <glib.h>
 
-typedef char*(*autocomplete_func)(char *);
+typedef char*(*autocomplete_func)(const char * const);
 typedef struct autocomplete_t *Autocomplete;
 
 // allocate new autocompleter with no items
@@ -53,18 +53,18 @@ void autocomplete_add(Autocomplete ac, const char *item);
 void autocomplete_remove(Autocomplete ac, const char * const item);
 
 // find the next item prefixed with search string
-gchar * autocomplete_complete(Autocomplete ac, gchar *search_str, gboolean quote);
+gchar * autocomplete_complete(Autocomplete ac, const gchar *search_str, gboolean quote);
 
 GSList * autocomplete_create_list(Autocomplete ac);
 gint autocomplete_length(Autocomplete ac);
 
-char * autocomplete_param_with_func(char *input, int *size, char *command,
+char * autocomplete_param_with_func(const char * const input, char *command,
     autocomplete_func func);
 
-char * autocomplete_param_with_ac(char *input, int *size, char *command,
+char * autocomplete_param_with_ac(const char * const input, char *command,
     Autocomplete ac, gboolean quote);
 
-char * autocomplete_param_no_with_func(char *input, int *size, char *command,
+char * autocomplete_param_no_with_func(const char * const input, char *command,
     int arg_number, autocomplete_func func);
 
 void autocomplete_reset(Autocomplete ac);
diff --git a/src/tools/parser.c b/src/tools/parser.c
index ae149155..e91b227d 100644
--- a/src/tools/parser.c
+++ b/src/tools/parser.c
@@ -316,7 +316,7 @@ parse_args_with_freetext(const char * const inp, int min, int max, gboolean *res
 }
 
 int
-count_tokens(char *string)
+count_tokens(const char * const string)
 {
     int length = g_utf8_strlen(string, -1);
     gboolean in_quotes = FALSE;
@@ -347,7 +347,7 @@ count_tokens(char *string)
 }
 
 char *
-get_start(char *string, int tokens)
+get_start(const char * const string, int tokens)
 {
     GString *result = g_string_new("");
     int length = g_utf8_strlen(string, -1);
diff --git a/src/tools/parser.h b/src/tools/parser.h
index 7ecef3fc..eeb97df3 100644
--- a/src/tools/parser.h
+++ b/src/tools/parser.h
@@ -39,8 +39,8 @@
 
 gchar** parse_args(const char * const inp, int min, int max, gboolean *result);
 gchar** parse_args_with_freetext(const char * const inp, int min, int max, gboolean *result);
-int count_tokens(char *string);
-char* get_start(char *string, int tokens);
+int count_tokens(const char * const string);
+char* get_start(const char * const string, int tokens);
 GHashTable* parse_options(gchar **args, gchar **keys, gboolean *res);
 void options_destroy(GHashTable *options);