X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=clients%2Fmemflush.cc;h=71545eabb43d13c04b1ee0658a181e515bc41fee;hb=dc1e22ba362663bf052e7cfbbdf933bb462a7006;hp=1ef1bfde52629e9775573c8f6ef57691c46503a0;hpb=fd632c80c5d393253d394c9a8721339876602882;p=awesomized%2Flibmemcached diff --git a/clients/memflush.cc b/clients/memflush.cc index 1ef1bfde..71545eab 100644 --- a/clients/memflush.cc +++ b/clients/memflush.cc @@ -1,4 +1,5 @@ /* LibMemcached + * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/ * Copyright (C) 2006-2009 Brian Aker * All rights reserved. * @@ -8,15 +9,16 @@ * Summary: * */ -#include "config.h" +#include "mem_config.h" +#include #include #include #include #include #include -#include +#include #include "client_options.h" #include "utilities.h" @@ -35,28 +37,32 @@ void options_parse(int argc, char *argv[]); int main(int argc, char *argv[]) { - memcached_st *memc; - memcached_return_t rc; - memcached_server_st *servers; - options_parse(argc, argv); - if (!opt_servers) + if (opt_servers == NULL) { char *temp; if ((temp= getenv("MEMCACHED_SERVERS"))) + { opt_servers= strdup(temp); - else + } + + if (opt_servers == NULL) { - fprintf(stderr, "No Servers provided\n"); - exit(1); + std::cerr << "No Servers provided" << std::endl; + exit(EXIT_FAILURE); } } - memc= memcached_create(NULL); + memcached_server_st* servers= memcached_servers_parse(opt_servers); + if (servers == NULL or memcached_server_list_count(servers) == 0) + { + std::cerr << "Invalid server list provided:" << opt_servers << std::endl; + return EXIT_FAILURE; + } - servers= memcached_servers_parse(opt_servers); + memcached_st *memc= memcached_create(NULL); memcached_server_push(memc, servers); memcached_server_list_free(servers); memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, @@ -80,14 +86,10 @@ int main(int argc, char *argv[]) } } - rc = memcached_flush(memc, opt_expire); + memcached_return_t rc = memcached_flush(memc, opt_expire); if (rc != MEMCACHED_SUCCESS) { - fprintf(stderr, "memflush: memcache error %s", - memcached_strerror(memc, rc)); - if (memcached_last_error_errno(memc)) - fprintf(stderr, " system error %s", strerror(memcached_last_error_errno(memc))); - fprintf(stderr, "\n"); + std::cerr << memcached_last_error_message(memc) << std::endl; } memcached_free(memc); @@ -100,15 +102,11 @@ int main(int argc, char *argv[]) void options_parse(int argc, char *argv[]) { - memcached_programs_help_st help_options[]= - { - {0}, - }; - static struct option long_options[]= { {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION}, {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP}, + {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET}, {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE}, {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG}, {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS}, @@ -118,49 +116,83 @@ void options_parse(int argc, char *argv[]) {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD}, {0, 0, 0, 0}, }; - int option_index= 0; - int option_rv; + bool opt_version= false; + bool opt_help= false; + int option_index= 0; while (1) { - option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); + int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); if (option_rv == -1) break; switch (option_rv) { case 0: break; + case OPT_BINARY: - opt_binary = 1; + opt_binary= true; break; + case OPT_VERBOSE: /* --verbose or -v */ - opt_verbose = OPT_VERBOSE; + opt_verbose= OPT_VERBOSE; break; + case OPT_DEBUG: /* --debug or -d */ - opt_verbose = OPT_DEBUG; + opt_verbose= OPT_DEBUG; break; + case OPT_VERSION: /* --version or -V */ - version_command(PROGRAM_NAME); + opt_version= true; break; + case OPT_HELP: /* --help or -h */ - help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options); + opt_help= true; break; + case OPT_SERVERS: /* --servers or -s */ opt_servers= strdup(optarg); break; + case OPT_EXPIRE: /* --expire */ + errno= 0; opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10); + if (errno != 0) + { + std::cerr << "Incorrect value passed to --expire: `" << optarg << "`" << std::endl; + exit(EXIT_FAILURE); + } break; + case OPT_USERNAME: opt_username= optarg; break; + case OPT_PASSWD: opt_passwd= optarg; break; + + case OPT_QUIET: + close_stdio(); + break; + case '?': /* getopt_long already printed an error message. */ - exit(1); + exit(EXIT_FAILURE); + default: abort(); } } + + if (opt_version) + { + version_command(PROGRAM_NAME); + exit(EXIT_SUCCESS); + } + + if (opt_help) + { + help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, NULL); + exit(EXIT_SUCCESS); + } }