From: Date: Wed, 19 Sep 2007 15:56:14 +0000 (+0200) Subject: Cleanup. X-Git-Tag: 0.2~18 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=6d0bec130624e22e695dbf4038fbeed9e1c26c60;hp=--cc;p=awesomized%2Flibmemcached Cleanup. --- 6d0bec130624e22e695dbf4038fbeed9e1c26c60 diff --cc src/Makefile.am index d4cf2e69,38146fb3..3f003d7c --- a/src/Makefile.am +++ b/src/Makefile.am @@@ -1,15 -1,13 +1,18 @@@ INCLUDES = -I$(top_builddir)/include LDADDS = ../lib/libmemcached.la - bin_PROGRAMS = memcat memcp memstat -bin_PROGRAMS = memcat memcp memrm ++bin_PROGRAMS = memcat memcp memstat memrm -memcat_SOURCES = memcat.c +noinst_includes= client_options.h utilities.h + +memcat_SOURCES = memcat.c utilities.c memcat_LDADD = $(LDADDS) -memcp_SOURCES = memcp.c +memcp_SOURCES = memcp.c utilities.c memcp_LDADD = $(LDADDS) -memrm_SOURCES = memrm.c +memstat_SOURCES = memstat.c utilities.c +memstat_LDADD = $(LDADDS) ++ ++memrm_SOURCES = memrm.c utilities.c + memrm_LDADD = $(LDADDS) diff --cc src/client_options.h index 354b2736,dcf87f01..6dd43135 --- a/src/client_options.h +++ b/src/client_options.h @@@ -1,8 -1,7 +1,12 @@@ typedef enum { - OPT_SERVERS= 257, + OPT_SERVERS= 1, OPT_VERSION, OPT_HELP, + OPT_VERBOSE, + OPT_DEBUG, OPT_FLAG, - OPT_EXPIRE ++ OPT_EXPIRE, ++ OPT_SET, ++ OPT_REPLACE, ++ OPT_ADD, } memcached_options; diff --cc src/memcat.c index dedcb945,5022dd9b..11b42d7d --- a/src/memcat.c +++ b/src/memcat.c @@@ -21,42 -28,7 +21,41 @@@ int main(int argc, char *argv[] size_t string_length; uint16_t flags; memcached_return rc; - unsigned int x; + memc= memcached_init(NULL); + + options_parse(argc, argv); + + parse_opt_servers(memc, opt_servers); + - for (x= 1; x < argc; x++) ++ while (optind <= argc) + { - string= memcached_get(memc, argv[x], strlen(argv[x]), ++ string= memcached_get(memc, argv[argc], strlen(argv[argc]), + &string_length, &flags, &rc); - if (rc == MEMCACHED_SUCCESS) { - if (opt_displayflag) { ++ if (rc == MEMCACHED_SUCCESS) ++ { ++ if (opt_displayflag) + printf("%d\n", flags); - } else { - if (string) - { - printf("%.*s\n", string_length, string); - free(string); - } ++ else ++ { ++ printf("%.*s\n", string_length, string); ++ free(string); + } + } ++ optind++; + } + + memcached_deinit(memc); + + 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}, @@@ -84,7 -58,7 +83,7 @@@ exit(0); break; case OPT_SERVERS: /* --servers */ - opt_servers= strdup(optarg); - opt_servers = strdup(optarg); ++ opt_servers= optarg; break; case '?': /* getopt_long already printed an error message. */ diff --cc src/memcp.c index 3430ab54,517ac940..88cbc351 --- a/src/memcp.c +++ b/src/memcp.c @@@ -6,52 -8,88 +8,31 @@@ #include #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; - uint16_t flags = 0; - time_t expires = 0; memcached_return rc; - if (argc < 3) - return 0; - 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); - /* Get the server name */ - { - char *ptr; - char *hostname; - size_t hostname_length; - unsigned int port; - - ptr= index(argv[argc-1], ':'); - - if (ptr) - { - 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); - } - } - - for (x= 1; x < argc-1; x++) - while (optind <= argc) { ++ while (optind <= argc) + { char *mptr; struct stat sbuf; int fd; @@@ -68,19 -106,31 +49,26 @@@ (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]; - } - - rc= memcached_set(memc, ptr, strlen(ptr), - mptr, sbuf.st_size, - (time_t)0, (uint16_t)0); + 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); @@@ -90,3 -141,3 +79,57 @@@ 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(); ++ } ++ } ++} diff --cc src/memrm.c index 00000000,519d9398..b297379f mode 000000,100644..100644 --- a/src/memrm.c +++ b/src/memrm.c @@@ -1,0 -1,86 +1,72 @@@ + #include + #include + #include + #include + #include "client_options.h" + + static int opt_verbose; + static char *opt_servers; + -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; + size_t string_length; + 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}, + {"expire", required_argument, NULL, OPT_EXPIRE}, + {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: + break; + case OPT_VERSION: /* --version */ + printf("memcache tools, memrm, 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); ++ opt_servers = optarg; + break; + case OPT_EXPIRE: /* --expire */ - expires = (time_t)atoi(optarg); ++ expires = (time_t)strtol(optarg, (char **)NULL, 10); + break; + case '?': + /* getopt_long already printed an error message. */ + exit(1); + default: + abort(); + } + } + + memc= memcached_init(NULL); - memc= parse_opt_servers(memc, opt_servers); ++ parse_opt_servers(memc, opt_servers); + - while (optind <= argc) { - ++ while (optind <= argc) ++ { + rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), expires); - + optind++; + } + + memcached_deinit(memc); + + return 0; + }; diff --cc src/memstat.c index 939f806b,00000000..76fdd9c6 mode 100644,000000..100644 --- a/src/memstat.c +++ b/src/memstat.c @@@ -1,76 -1,0 +1,76 @@@ +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "client_options.h" +#include "utilities.h" + +/* Prototypes */ +void options_parse(int argc, char *argv[]); + +static int opt_verbose; +static int opt_displayflag; +static char *opt_servers; + +int main(int argc, char *argv[]) +{ + memcached_st *memc; + + memc= memcached_init(NULL); + options_parse(argc, argv); + + parse_opt_servers(memc, opt_servers); + + memcached_deinit(memc); + + return 0; +} + +void options_parse(int argc, char *argv[]) +{ + 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", no_argument, &opt_displayflag, OPT_FLAG}, + {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: + break; + case OPT_VERSION: /* --version */ + printf("memcache tools, memcat, 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); ++ opt_servers = optarg; + break; + case '?': + /* getopt_long already printed an error message. */ + exit(1); + default: + abort(); + } + } +}