#include <string.h>
#include <stdlib.h>
#include <unistd.h>
+#include <ctype.h>
#include "anet.h"
#include "sds.h"
#include "adlist.h"
#include "zmalloc.h"
+#include "linenoise.h"
#define REDIS_CMD_INLINE 1
#define REDIS_CMD_BULK 2
long repeat;
int dbnum;
int interactive;
+ char *auth;
} config;
struct redisCommand {
};
static struct redisCommand cmdTable[] = {
+ {"auth",2,REDIS_CMD_INLINE},
{"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},
{"zmergeweighed",-4,REDIS_CMD_INLINE},
{"zrange",-4,REDIS_CMD_INLINE},
{"zrank",3,REDIS_CMD_BULK},
+ {"zrevrank",3,REDIS_CMD_BULK},
{"zrangebyscore",-4,REDIS_CMD_INLINE},
{"zcount",4,REDIS_CMD_INLINE},
{"zrevrange",-4,REDIS_CMD_INLINE},
{"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},
+ {"hvals",2,REDIS_CMD_INLINE},
+ {"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}
};
return 0;
}
+static void printStringRepr(char *s, int len) {
+ printf("\"");
+ while(len--) {
+ switch(*s) {
+ case '\\':
+ case '"':
+ printf("\\%c",*s);
+ break;
+ case '\n': printf("\\n"); break;
+ case '\r': printf("\\r"); break;
+ case '\t': printf("\\t"); break;
+ case '\a': printf("\\a"); break;
+ case '\b': printf("\\b"); break;
+ default:
+ if (isprint(*s))
+ printf("%c",*s);
+ else
+ printf("\\x%02x",(unsigned char)*s);
+ break;
+ }
+ s++;
+ }
+ printf("\"\n");
+}
+
static int cliReadBulkReply(int fd) {
sds replylen = cliReadLine(fd);
char *reply, crlf[2];
reply = zmalloc(bulklen);
anetRead(fd,reply,bulklen);
anetRead(fd,crlf,2);
- if (bulklen && fwrite(reply,bulklen,1,stdout) == 0) {
- zfree(reply);
- return 1;
+ if (!isatty(fileno(stdout))) {
+ if (bulklen && fwrite(reply,bulklen,1,stdout) == 0) {
+ zfree(reply);
+ return 1;
+ }
+ if (reply[bulklen-1] != '\n') printf("\n");
+ } else {
+ /* If you are producing output for the standard output we want
+ * a more interesting output with quoted characters and so forth */
+ printStringRepr(reply,bulklen);
}
- if (isatty(fileno(stdout)) && reply[bulklen-1] != '\n')
- printf("\n");
zfree(reply);
return 0;
}
return 0;
}
-static int cliSendCommand(int argc, char **argv) {
+static int cliSendCommand(int argc, char **argv, int repeat) {
struct redisCommand *rc = lookupCommand(argv[0]);
int fd, j, retval = 0;
int read_forever = 0;
return 1;
}
- while(config.repeat--) {
+ while(repeat--) {
/* Build the command to send */
cmd = sdsempty();
if (rc->flags & REDIS_CMD_MULTIBULK) {
} else if (!strcmp(argv[i],"-n") && !lastarg) {
config.dbnum = atoi(argv[i+1]);
i++;
+ } else if (!strcmp(argv[i],"-a") && !lastarg) {
+ config.auth = argv[i+1];
+ i++;
} else if (!strcmp(argv[i],"-i")) {
config.interactive = 1;
} else {
}
static void usage() {
- fprintf(stderr, "usage: redis-cli [-h host] [-p port] [-r repeat_times] [-n db_num] [-i] cmd arg1 arg2 arg3 ... argN\n");
- fprintf(stderr, "usage: echo \"argN\" | redis-cli [-h host] [-p port] [-r repeat_times] [-n db_num] cmd arg1 arg2 ... arg(N-1)\n");
+ fprintf(stderr, "usage: redis-cli [-h host] [-p port] [-a authpw] [-r repeat_times] [-n db_num] [-i] cmd arg1 arg2 arg3 ... argN\n");
+ fprintf(stderr, "usage: echo \"argN\" | redis-cli [-h host] [-a authpw] [-p port] [-r repeat_times] [-n db_num] cmd arg1 arg2 ... arg(N-1)\n");
fprintf(stderr, "\nIf a pipe from standard input is detected this data is used as last argument.\n\n");
fprintf(stderr, "example: cat /etc/passwd | redis-cli set my_passwd\n");
fprintf(stderr, "example: redis-cli get my_passwd\n");
return sds;
}
-static char *prompt(char *line, int size) {
- char *retval;
-
- do {
- printf(">> ");
- retval = fgets(line, size, stdin);
- } while (retval && *line == '\n');
- line[strlen(line) - 1] = '\0';
+static char **splitArguments(char *line, int *argc) {
+ char *p = line;
+ char *current = NULL;
+ char **vector = NULL;
- return retval;
+ *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 buffer[size];
- char *line = buffer;
- char **ap, *args[max];
-
- while (prompt(line, size)) {
- 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++;
+ int argc, j;
+ char *line, **argv;
+
+ while((line = linenoise("redis> ")) != NULL) {
+ if (line[0] != '\0') {
+ 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);
}
-
- config.repeat = 1;
- cliSendCommand(argc, convertToSds(argc, args));
- line = buffer;
+ /* linenoise() returns malloc-ed lines like readline() */
+ free(line);
}
-
exit(0);
}
config.repeat = 1;
config.dbnum = 0;
config.interactive = 0;
+ config.auth = NULL;
firstarg = parseOptions(argc,argv);
argc -= firstarg;
argv += firstarg;
+ if (config.auth != NULL) {
+ char *authargv[2];
+
+ authargv[0] = "AUTH";
+ authargv[1] = config.auth;
+ cliSendCommand(2, convertToSds(2, authargv), 1);
+ }
+
if (argc == 0 || config.interactive == 1) repl();
argvcopy = convertToSds(argc, argv);
}
}
- return cliSendCommand(argc, argvcopy);
+ return cliSendCommand(argc, argvcopy, config.repeat);
}