]> git.saurik.com Git - redis.git/blobdiff - redis-cli.c
Initial support for quoted strings in redis-cli
[redis.git] / redis-cli.c
index 1a45e03571aa66c691ccddc29a82fd913dd8e624..2522396fb5baf0df441c1373fec24155ca5528ad 100644 (file)
@@ -34,6 +34,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <ctype.h>
 
 #include "anet.h"
 #include "sds.h"
@@ -67,6 +68,7 @@ static struct redisCommand cmdTable[] = {
     {"get",2,REDIS_CMD_INLINE},
     {"set",3,REDIS_CMD_BULK},
     {"setnx",3,REDIS_CMD_BULK},
+    {"setex",4,REDIS_CMD_BULK},
     {"append",3,REDIS_CMD_BULK},
     {"substr",4,REDIS_CMD_INLINE},
     {"del",-2,REDIS_CMD_INLINE},
@@ -151,6 +153,9 @@ static struct redisCommand cmdTable[] = {
     {"discard",1,REDIS_CMD_INLINE},
     {"hset",4,REDIS_CMD_MULTIBULK},
     {"hget",3,REDIS_CMD_BULK},
+    {"hmset",-4,REDIS_CMD_MULTIBULK},
+    {"hmget",-3,REDIS_CMD_MULTIBULK},
+    {"hincrby",4,REDIS_CMD_INLINE},
     {"hdel",3,REDIS_CMD_BULK},
     {"hlen",2,REDIS_CMD_INLINE},
     {"hkeys",2,REDIS_CMD_INLINE},
@@ -158,6 +163,9 @@ static struct redisCommand cmdTable[] = {
     {"hgetall",2,REDIS_CMD_INLINE},
     {"hexists",3,REDIS_CMD_BULK},
     {"config",-2,REDIS_CMD_BULK},
+    {"subscribe",-2,REDIS_CMD_INLINE},
+    {"unsubscribe",-1,REDIS_CMD_INLINE},
+    {"publish",3,REDIS_CMD_BULK},
     {NULL,0,0}
 };
 
@@ -453,32 +461,95 @@ static char **convertToSds(int count, char** args) {
   return sds;
 }
 
+static char **splitArguments(char *line, int *argc) {
+    char *p = line;
+    char *current = NULL;
+    char **vector = NULL;
+
+    *argc = 0;
+    while(1) {
+        /* skip blanks */
+        while(*p && isspace(*p)) p++;
+        if (*p) {
+            /* get a token */
+            int inq=0; /* set to 1 if we are in "quotes" */
+            int done = 0;
+
+            if (current == NULL) current = sdsempty();
+            while(!done) {
+                if (inq) {
+                    if (*p == '\\' && *(p+1)) {
+                        char c;
+
+                        p++;
+                        switch(*p) {
+                        case 'n': c = '\n'; break;
+                        case 'r': c = '\r'; break;
+                        case 't': c = '\t'; break;
+                        case 'b': c = '\b'; break;
+                        case 'a': c = '\a'; break;
+                        default: c = *p; break;
+                        }
+                        current = sdscatlen(current,&c,1);
+                    } else if (*p == '"') {
+                        done = 1;
+                    } else {
+                        current = sdscatlen(current,p,1);
+                    }
+                } else {
+                    switch(*p) {
+                    case ' ':
+                    case '\n':
+                    case '\r':
+                    case '\t':
+                    case '\0':
+                        done=1;
+                        break;
+                    case '"':
+                        inq=1;
+                        break;
+                    default:
+                        current = sdscatlen(current,p,1);
+                        break;
+                    }
+                }
+                if (*p) p++;
+            }
+            /* add the token to the vector */
+            vector = zrealloc(vector,((*argc)+1)*sizeof(char*));
+            vector[*argc] = current;
+            (*argc)++;
+            current = NULL;
+        } else {
+            return vector;
+        }
+    }
+}
+
+#define LINE_BUFLEN 4096
 static void repl() {
-    int size = 4096, max = size >> 1, argc;
-    char *line;
-    char **ap, *args[max];
+    int argc, j;
+    char *line, **argv;
 
     while((line = linenoise("redis> ")) != NULL) {
         if (line[0] != '\0') {
-          linenoiseHistoryAdd(line);
-          argc = 0;
-
-          for (ap = args; (*ap = strsep(&line, " \t")) != NULL;) {
-              if (**ap != '\0') {
-                  if (argc >= max) break;
-                  if (strcasecmp(*ap,"quit") == 0 || strcasecmp(*ap,"exit") == 0)
-                      exit(0);
-                  ap++;
-                  argc++;
-              }
-          }
-
-          cliSendCommand(argc, convertToSds(argc, args), 1);
+            argv = splitArguments(line,&argc);
+            linenoiseHistoryAdd(line);
+            if (argc > 0) {
+                if (strcasecmp(argv[0],"quit") == 0 ||
+                    strcasecmp(argv[0],"exit") == 0)
+                        exit(0);
+                else
+                    cliSendCommand(argc, argv, 1);
+            }
+            /* Free the argument vector */
+            for (j = 0; j < argc; j++)
+                sdsfree(argv[j]);
+            free(argv);
         }
-
+        /* linenoise() returns malloc-ed lines like readline() */
         free(line);
     }
-
     exit(0);
 }