#include <unistd.h>
#include <getopt.h>
#include <memcached.h>
+#include <string.h>
#include "client_options.h"
#include "utilities.h"
static int opt_verbose= 0;
static time_t opt_expire= 0;
static char *opt_servers= NULL;
+static char *opt_hash= NULL;
+
+#define PROGRAM_NAME "memrm"
+#define PROGRAM_DESCRIPTION "Erase a key or set of keys from a memcached cluster."
/* Prototypes */
void options_parse(int argc, char *argv[]);
options_parse(argc, argv);
if (!opt_servers)
- return 0;
+ {
+ char *temp;
+
+ if ((temp= getenv("MEMCACHED_SERVERS")))
+ opt_servers= strdup(temp);
+ else
+ exit(1);
+ }
- memc= memcached_init(NULL);
+ memc= memcached_create(NULL);
+ process_hash_option(memc, opt_hash);
- servers= parse_opt_servers(opt_servers);
+ servers= memcached_servers_parse(opt_servers);
memcached_server_push(memc, servers);
memcached_server_list_free(servers);
if (rc != MEMCACHED_SUCCESS)
{
- fprintf(stderr, "memrm: %s: memcache error %s\n",
+ fprintf(stderr, "memrm: %s: memcache error %s",
argv[optind], memcached_strerror(memc, rc));
+ if (memc->cached_errno)
+ fprintf(stderr, " system error %s", strerror(memc->cached_errno));
+ fprintf(stderr, "\n");
}
optind++;
}
- memcached_deinit(memc);
+ memcached_free(memc);
- free(opt_servers);
+ if (opt_servers)
+ free(opt_servers);
+ if (opt_hash)
+ free(opt_hash);
return 0;
}
void options_parse(int argc, char *argv[])
{
+ memcached_programs_help_st help_options[]=
+ {
+ {0},
+ };
+
static struct option long_options[]=
{
{"version", no_argument, NULL, OPT_VERSION},
{"debug", no_argument, &opt_verbose, OPT_DEBUG},
{"servers", required_argument, NULL, OPT_SERVERS},
{"expire", required_argument, NULL, OPT_EXPIRE},
+ {"hash", required_argument, NULL, OPT_HASH},
{0, 0, 0, 0},
};
int option_index= 0;
opt_verbose = OPT_DEBUG;
break;
case OPT_VERSION: /* --version or -V */
- printf("memcache tools, memrm, v1.0\n");
- exit(0);
+ version_command(PROGRAM_NAME);
break;
case OPT_HELP: /* --help or -h */
- printf("useful help messages go here\n");
- exit(0);
+ help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
break;
case OPT_SERVERS: /* --servers or -s */
opt_servers= strdup(optarg);
case OPT_EXPIRE: /* --expire */
opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
break;
+ case OPT_HASH:
+ opt_hash= strdup(optarg);
+ break;
case '?':
/* getopt_long already printed an error message. */
exit(1);