X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fmemcp.c;fp=src%2Fmemcp.c;h=88cbc351372588341757f1caecd46a4127ba7845;hb=6d0bec130624e22e695dbf4038fbeed9e1c26c60;hp=517ac940d6f9701107a40021589bdc791f4fb3c2;hpb=8ed85f5e5e82146dcd869c5f260252e92bad2662;p=awesomized%2Flibmemcached diff --git a/src/memcp.c b/src/memcp.c index 517ac940..88cbc351 100644 --- a/src/memcp.c +++ b/src/memcp.c @@ -10,86 +10,29 @@ #include #include "client_options.h" +/* Prototypes */ +void options_parse(int argc, char *argv[]); + 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; -} +uint16_t opt_flags= 0; +time_t opt_expires= 0; int main(int argc, char *argv[]) { memcached_st *memc; char *string; - unsigned int x; size_t string_length; - uint16_t flags = 0; - time_t expires = 0; memcached_return rc; - static struct option long_options[] = - { - {"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(); - } - } + options_parse(argc, argv); memc= memcached_init(NULL); - memc= parse_opt_servers(memc, opt_servers); + parse_opt_servers(memc, opt_servers); - while (optind <= argc) { + while (optind <= argc) + { char *mptr; struct stat sbuf; int fd; @@ -108,29 +51,24 @@ int main(int argc, char *argv[]) ptr= rindex(argv[optind], '/'); if (ptr) - { ptr++; - } else - { ptr= argv[optind]; - } - if (opt_replace == 0) { + if (opt_replace == 0) rc= memcached_set(memc, ptr, strlen(ptr), mptr, sbuf.st_size, - expires, flags); - } else if (opt_replace == 1) { + opt_expires, opt_flags); + else if (opt_replace == 1) rc= memcached_add(memc, ptr, strlen(ptr), mptr, sbuf.st_size, - expires, flags); - } else if (opt_replace == 2) { + opt_expires, opt_flags); + else if (opt_replace == 2) rc= memcached_replace(memc, ptr, strlen(ptr), mptr, sbuf.st_size, - expires, flags); - } else { + opt_expires, opt_flags); + else abort(); - } munmap(mptr, sbuf.st_size); close(fd); @@ -141,3 +79,57 @@ int main(int argc, char *argv[]) return 0; }; + +void options_parse(int argc, char *argv[]) +{ + int option_index= 0; + int option_rv; + + 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}, + {"servers", required_argument, NULL, OPT_SERVERS}, + {"flag", required_argument, NULL, OPT_FLAG}, + {"expire", required_argument, NULL, OPT_EXPIRE}, + {"set", no_argument, &opt_replace, OPT_SET}, + {"add", no_argument, &opt_replace, OPT_ADD}, + {"replace", no_argument, &opt_replace, OPT_REPLACE}, + {0, 0, 0, 0}, + }; + + 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); + case OPT_HELP: /* --help */ + printf("useful help messages go here\n"); + exit(0); + case OPT_SERVERS: /* --servers */ + opt_servers= optarg; + break; + case OPT_FLAG: /* --flag */ + opt_flags= (uint16_t)strtol(optarg, (char **)NULL, 10); + break; + case OPT_EXPIRE: /* --expire */ + opt_expires= (time_t)strtol(optarg, (char **)NULL, 10); + break; + case '?': + /* getopt_long already printed an error message. */ + exit(1); + default: + abort(); + } + } +}