From: Mark Atwood Date: Wed, 19 Sep 2007 11:59:23 +0000 (-0700) Subject: add options to memcp, fix options in memcat X-Git-Tag: 0.2~18^2~4 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=8a5eacba0596082949034333adbc4e01f339069b;p=awesomized%2Flibmemcached add options to memcp, fix options in memcat --- diff --git a/src/client_options.h b/src/client_options.h index 354b2736..dcf87f01 100644 --- a/src/client_options.h +++ b/src/client_options.h @@ -1,8 +1,7 @@ typedef enum { - OPT_SERVERS= 1, + OPT_SERVERS= 257, OPT_VERSION, OPT_HELP, - OPT_VERBOSE, - OPT_DEBUG, OPT_FLAG, + OPT_EXPIRE } memcached_options; diff --git a/src/memcat.c b/src/memcat.c index 645b54ac..eb853291 100644 --- a/src/memcat.c +++ b/src/memcat.c @@ -28,14 +28,13 @@ int main(int argc, char *argv[]) size_t string_length; uint16_t flags; memcached_return rc; - unsigned int x; static struct option long_options[] = { {"version", no_argument, NULL, OPT_VERSION}, {"help", no_argument, NULL, OPT_HELP}, - {"verbose", no_argument, &opt_verbose, OPT_VERBOSE}, - {"debug", no_argument, &opt_verbose, OPT_DEBUG}, + {"verbose", no_argument, &opt_verbose, 1}, + {"debug", no_argument, &opt_verbose, 2}, {"servers", required_argument, NULL, OPT_SERVERS}, {"flag", no_argument, &opt_displayflag, OPT_FLAG}, {0, 0, 0, 0}, @@ -69,14 +68,11 @@ int main(int argc, char *argv[]) } } - memc = malloc(sizeof(struct memcached_st)); - memcached_init(memc); + memc= memcached_init(NULL); memc= parse_opt_servers(memc, opt_servers); - memc= memcached_init(memc); - - for (x= 1; x < argc; x++) - { - string= memcached_get(memc, argv[x], strlen(argv[x]), + + while (optind < argc) { + string= memcached_get(memc, argv[optind], strlen(argv[optind]), &string_length, &flags, &rc); if (rc == MEMCACHED_SUCCESS) { if (opt_displayflag) { @@ -89,6 +85,8 @@ int main(int argc, char *argv[]) } } } + + optind++; } memcached_deinit(memc); diff --git a/src/memcp.c b/src/memcp.c index 3430ab54..52c373a1 100644 --- a/src/memcp.c +++ b/src/memcp.c @@ -1,4 +1,6 @@ #include +#include +#include #include #include #include @@ -7,83 +9,119 @@ #include +static int opt_verbose; +static char *opt_servers; +static int opt_replace; + +struct memcached_st *parse_opt_servers (struct memcached_st *m, + char *opt_servers) +{ + char *s, *hostname; + unsigned int portnum; + while (s = strsep(&opt_servers, ",")) { + hostname = strsep(&s, ":"); + portnum = atoi(s); + memcached_server_add(m, hostname, portnum); + } + return m; +} + int main(int argc, char *argv[]) { memcached_st *memc; char *string; unsigned int x; size_t string_length; - uint16_t flags; + uint16_t flags = 0; + time_t expires = 0; memcached_return rc; - if (argc < 3) - return 0; - - memc= memcached_init(NULL); - - /* Get the server name */ - { - char *ptr; - char *hostname; - size_t hostname_length; - unsigned int port; - - ptr= index(argv[argc-1], ':'); - - if (ptr) + static struct option long_options[] = { - hostname_length= ptr - argv[argc-1]; - hostname= (char *)malloc(hostname_length+1); - memset(hostname, 0, hostname_length+1); - memcpy(hostname, argv[argc-1], hostname_length); - - ptr++; - - port= strtol(ptr, (char **)NULL, 10); - - memcached_server_add(memc, hostname, port); - free(hostname); - } - else - { - memcached_server_add(memc, argv[argc -1], 0); + {"version", no_argument, NULL, OPT_VERSION}, + {"help", no_argument, NULL, OPT_HELP}, + {"verbose", no_argument, &opt_verbose, 1}, + {"debug", no_argument, &opt_verbose, 2}, + {"servers", required_argument, NULL, OPT_SERVERS}, + {"flag", required_argument, NULL, OPT_FLAG}, + {"expire", required_argument, NULL, OPT_EXPIRE}, + {"set", no_argument, &opt_replace, 0}, + {"add", no_argument, &opt_replace, 1}, + {"replace", no_argument, &opt_replace, 2}, + {0, 0, 0, 0}, + }; + int option_index = 0; + int option_rv; + while (1) + { + option_rv = getopt_long(argc, argv, "", long_options, &option_index); + if (option_rv == -1) break; + switch (option_rv) { + case 0: + if (long_options[option_index].name) + break; + case OPT_VERSION: /* --version */ + printf("memcache tools, memcp, v1.0\n"); + exit(0); + break; + case OPT_HELP: /* --help */ + printf("useful help messages go here\n"); + exit(0); + break; + case OPT_SERVERS: /* --servers */ + opt_servers = strdup(optarg); + break; + case OPT_FLAG: /* --flag */ + flags = (uint16_t) atoi(optarg); + break; + case OPT_EXPIRE: /* --expire */ + expires = (time_t)atoi(optarg); + break; + case '?': + /* getopt_long already printed an error message. */ + exit(1); + default: + abort(); } } - for (x= 1; x < argc-1; x++) - { + memc= memcached_init(NULL); + memc= parse_opt_servers(memc, opt_servers); + + while (optind < argc) { char *mptr; struct stat sbuf; int fd; char *ptr; - fd= open(argv[x], O_RDONLY); + fd= open(argv[optind], O_RDONLY); if (fd == -1) { - fprintf(stderr, "Failed opening %s\n", argv[x]); - exit(1); + fprintf(stderr, "Failed opening %s\n", argv[optind]); + continue; } (void)fstat(fd, &sbuf); mptr= mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0); - ptr= rindex(argv[x], '/'); + ptr= rindex(argv[optind], '/'); if (ptr) { ptr++; } else { - ptr= argv[x]; + ptr= argv[optind]; } rc= memcached_set(memc, ptr, strlen(ptr), mptr, sbuf.st_size, - (time_t)0, (uint16_t)0); + expire, flags); munmap(mptr, sbuf.st_size); close(fd); + optind++; } memcached_deinit(memc);