1. int main()
int main(int argc, char **argv) {
int firstarg, j;
char **argvcopy;
struct redisCommand *rc;
config.hostip = "127.0.0.1";
config.hostport = 6379;
firstarg = parseOptions(argc,argv);
argc -= firstarg;
argv += firstarg;
/* Turn the plain C strings into Sds strings */
argvcopy = zmalloc(sizeof(char*)*argc+1);
for(j = 0; j < argc; j++)
argvcopy[j] = sdsnew(argv[j]);
if (argc < 1) {
fprintf(stderr, "usage: redis-cli [-h host] [-p port] cmd arg1 arg2 arg3 ... argN\n");
fprintf(stderr, "usage: echo \"argN\" | redis-cli [-h host] [-p port] 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");
exit(1);
}
/* Read the last argument from stdandard input if needed */
if ((rc = lookupCommand(argv[0])) != NULL) {
if (rc->arity > 0 && argc == rc->arity-1) {
sds lastarg = readArgFromStdin();
argvcopy[argc] = lastarg;
argc++;
}
}
return cliSendCommand(argc, argvcopy);
}
2. parseOptions(int argc, char **argv) 进行ip和port的改变
static int parseOptions(int argc, char **argv) {
int i;
for (i = 1; i < argc; i++) {
int lastarg = i==argc-1;
if (!strcmp(argv[i],"-h") && !lastarg) {
char *ip = zmalloc(32);
if (anetResolve(NULL,argv[i+1],ip) == ANET_ERR) {
printf("Can't resolve %s\n", argv[i]);
exit(1);
}
config.hostip = ip;
i++;
} else if (!strcmp(argv[i],"-p") && !lastarg) {
config.hostport = atoi(argv[i+1]);
i++;
} else {
break;
}
}
return i;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#inclu