about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2013-07-12 00:46:33 +0100
committerJames Booth <boothj5@gmail.com>2013-07-12 00:46:33 +0100
commitbb550fed5547c7885e3f9c486051c742ac0b9b67 (patch)
tree204227c98492787c2d8281560bbcaaca50ef7909
parent5233000498a30c61b22f68ac34ff10d60815f979 (diff)
downloadprofani-tty-bb550fed5547c7885e3f9c486051c742ac0b9b67.tar.gz
Handle unicode chars in command parser
-rw-r--r--src/tools/parser.c23
1 files changed, 14 insertions, 9 deletions
diff --git a/src/tools/parser.c b/src/tools/parser.c
index 96c56628..c8335879 100644
--- a/src/tools/parser.c
+++ b/src/tools/parser.c
@@ -58,7 +58,7 @@ parse_args(const char * const inp, int min, int max)
     char *copy = strdup(inp);
     g_strstrip(copy);
 
-    int inp_size = strlen(copy);
+    int inp_size = g_utf8_strlen(copy, -1);
     gboolean in_token = FALSE;
     gboolean in_quotes = FALSE;
     char *token_start = &copy[0];
@@ -67,43 +67,48 @@ parse_args(const char * const inp, int min, int max)
 
     // add tokens to GSList
     int i;
-    for (i = 0; i <= inp_size; i++) {
+    for (i = 0; i < inp_size; i++) {
+        gchar *curr_ch = g_utf8_offset_to_pointer(copy, i);
+        gunichar curr_uni = g_utf8_get_char(curr_ch);
         if (!in_token) {
-            if (copy[i] == ' ') {
+            if (curr_uni  == ' ') {
                 continue;
             } else {
                 in_token = TRUE;
-                if (copy[i] == '"') {
+                if (curr_uni == '"') {
                     in_quotes = TRUE;
                     i++;
                 }
-                token_start = &copy[i];
-                token_size++;
+                token_start = curr_ch;
+                token_size += g_unichar_to_utf8(curr_uni, NULL);
             }
         } else {
             if (in_quotes) {
-                if ((copy[i] == '\0') || (copy[i] == '"')) {
+                if (curr_uni == '"') {
                     tokens = g_slist_append(tokens, g_strndup(token_start,
                         token_size));
                     token_size = 0;
                     in_token = FALSE;
                     in_quotes = FALSE;
                 } else {
-                    token_size++;
+                    token_size += g_unichar_to_utf8(curr_uni, NULL);
                 }
             } else {
-                if (copy[i] == ' ' || copy[i] == '\0') {
+                if (curr_uni == ' ') {
                     tokens = g_slist_append(tokens, g_strndup(token_start,
                         token_size));
                     token_size = 0;
                     in_token = FALSE;
                 } else {
+                    token_size += g_unichar_to_utf8(curr_uni, NULL);
                     token_size++;
                 }
             }
         }
     }
 
+    tokens = g_slist_append(tokens, g_strndup(token_start, token_size));
+
     int num = g_slist_length(tokens) - 1;
 
     // if num args not valid return NULL