X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/e3c7f0027fe857a200425218c1faa39b9a6407e0..ee5cfe385305071ed0ab293be3ad18b13f2f9c86:/redis-cli.c diff --git a/redis-cli.c b/redis-cli.c index 46ee29f0..807f676d 100644 --- a/redis-cli.c +++ b/redis-cli.c @@ -65,6 +65,7 @@ static struct redisCommand cmdTable[] = { {"set",3,REDIS_CMD_BULK}, {"setnx",3,REDIS_CMD_BULK}, {"append",3,REDIS_CMD_BULK}, + {"substr",4,REDIS_CMD_INLINE}, {"del",-2,REDIS_CMD_INLINE}, {"exists",2,REDIS_CMD_INLINE}, {"incr",2,REDIS_CMD_INLINE}, @@ -101,6 +102,7 @@ static struct redisCommand cmdTable[] = { {"zrem",3,REDIS_CMD_BULK}, {"zremrangebyscore",4,REDIS_CMD_INLINE}, {"zrange",-4,REDIS_CMD_INLINE}, + {"zrank",3,REDIS_CMD_BULK}, {"zrangebyscore",-4,REDIS_CMD_INLINE}, {"zcount",4,REDIS_CMD_INLINE}, {"zrevrange",-4,REDIS_CMD_INLINE}, @@ -138,6 +140,11 @@ static struct redisCommand cmdTable[] = { {"mset",-3,REDIS_CMD_MULTIBULK}, {"msetnx",-3,REDIS_CMD_MULTIBULK}, {"monitor",1,REDIS_CMD_INLINE}, + {"multi",1,REDIS_CMD_INLINE}, + {"exec",1,REDIS_CMD_INLINE}, + {"discard",1,REDIS_CMD_INLINE}, + {"hset",4,REDIS_CMD_MULTIBULK}, + {"hget",3,REDIS_CMD_BULK}, {NULL,0,0} }; @@ -155,14 +162,16 @@ static struct redisCommand *lookupCommand(char *name) { static int cliConnect(void) { char err[ANET_ERR_LEN]; - int fd; + static int fd = ANET_ERR; - fd = anetTcpConnect(err,config.hostip,config.hostport); if (fd == ANET_ERR) { - fprintf(stderr,"Connect: %s\n",err); - return -1; + fd = anetTcpConnect(err,config.hostip,config.hostport); + if (fd == ANET_ERR) { + fprintf(stderr, "Could not connect to Redis at %s:%d: %s", config.hostip, config.hostport, err); + return -1; + } + anetTcpNoDelay(NULL,fd); } - anetTcpNoDelay(NULL,fd); return fd; } @@ -282,7 +291,6 @@ static int selectDb(int fd) { if (type <= 0 || type != '+') return 1; retval = cliReadSingleLineReply(fd,1); if (retval) { - close(fd); return retval; } return 0; @@ -350,11 +358,9 @@ static int cliSendCommand(int argc, char **argv) { retval = cliReadReply(fd); if (retval) { - close(fd); return retval; } } - close(fd); return 0; } @@ -416,7 +422,7 @@ static void usage() { fprintf(stderr, "example: cat /etc/passwd | redis-cli set my_passwd\n"); fprintf(stderr, "example: redis-cli get my_passwd\n"); fprintf(stderr, "example: redis-cli -r 100 lpush mylist x\n"); - fprintf(stderr, "\nRun in interactive mode: redis-cli -i \n"); + fprintf(stderr, "\nRun in interactive mode: redis-cli -i or just don't pass any command\n"); exit(1); } @@ -438,11 +444,8 @@ static char *prompt(char *line, int size) { printf(">> "); retval = fgets(line, size, stdin); } while (retval && *line == '\n'); - line[strlen(line) - 1] = '\0'; - fpurge(stdin); - return retval; } @@ -458,6 +461,8 @@ static void repl() { 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++; } @@ -483,12 +488,10 @@ int main(int argc, char **argv) { config.interactive = 0; firstarg = parseOptions(argc,argv); - argc -= firstarg; argv += firstarg; - if (config.interactive == 1) repl(); - if (argc < 1) usage(); + if (argc == 0 || config.interactive == 1) repl(); argvcopy = convertToSds(argc, argv);