]> git.saurik.com Git - redis.git/blobdiff - src/redis-cli.c
minor merge conflicts merging cli-help branch fixed
[redis.git] / src / redis-cli.c
index e22ba3766a46152891795984a707655c2fa4bb2f..a3987472283bd9de657464f7e143a724cc3daf55 100644 (file)
@@ -402,6 +402,11 @@ static int cliSendCommand(int argc, char **argv, int repeat) {
     size_t *argvlen;
     int j;
 
+    if (context == NULL) {
+        printf("Not connected, please use: connect <host> <port>\n");
+        return REDIS_OK;
+    }
+
     config.raw_output = !strcasecmp(command,"info");
     if (!strcasecmp(command,"help") || !strcasecmp(command,"?")) {
         cliOutputHelp(--argc, ++argv);
@@ -448,7 +453,8 @@ static int parseOptions(int argc, char **argv) {
         int lastarg = i==argc-1;
 
         if (!strcmp(argv[i],"-h") && !lastarg) {
-            config.hostip = argv[i+1];
+            sdsfree(config.hostip);
+            config.hostip = sdsnew(argv[i+1]);
             i++;
         } else if (!strcmp(argv[i],"-h") && lastarg) {
             usage();
@@ -536,7 +542,8 @@ static void repl() {
 
     config.interactive = 1;
     linenoiseSetCompletionCallback(completionCallback);
-    while((line = linenoise("redis> ")) != NULL) {
+
+    while((line = linenoise(context ? "redis> " : "not connected> ")) != NULL) {
         if (line[0] != '\0') {
             argv = sdssplitargs(line,&argc);
             linenoiseHistoryAdd(line);
@@ -549,14 +556,16 @@ static void repl() {
                     strcasecmp(argv[0],"exit") == 0)
                 {
                     exit(0);
+                } else if (argc == 3 && !strcasecmp(argv[0],"connect")) {
+                    sdsfree(config.hostip);
+                    config.hostip = sdsnew(argv[1]);
+                    config.hostport = atoi(argv[2]);
+                    cliConnect(1);
                 } else {
                     long long start_time = mstime(), elapsed;
 
                     if (cliSendCommand(argc,argv,1) != REDIS_OK) {
-                        printf("Reconnecting... ");
-                        fflush(stdout);
-                        if (cliConnect(1) != REDIS_OK) exit(1);
-                        printf("OK\n");
+                        cliConnect(1);
 
                         /* If we still cannot send the command,
                          * print error and abort. */
@@ -596,7 +605,7 @@ static int noninteractive(int argc, char **argv) {
 int main(int argc, char **argv) {
     int firstarg;
 
-    config.hostip = "127.0.0.1";
+    config.hostip = sdsnew("127.0.0.1");
     config.hostport = 6379;
     config.hostsocket = NULL;
     config.repeat = 1;