2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
16 #include <libmemcached/memcached.h>
18 #include "client_options.h"
19 #include "utilities.h"
21 static int opt_binary
= 0;
22 static int opt_verbose
= 0;
23 static time_t opt_expire
= 0;
24 static char *opt_servers
= NULL
;
25 static char *opt_hash
= NULL
;
26 static char *opt_username
;
27 static char *opt_passwd
;
29 #define PROGRAM_NAME "memrm"
30 #define PROGRAM_DESCRIPTION "Erase a key or set of keys from a memcached cluster."
33 static void options_parse(int argc
, char *argv
[]);
35 int main(int argc
, char *argv
[])
38 memcached_return_t rc
;
39 memcached_server_st
*servers
;
43 options_parse(argc
, argv
);
50 if ((temp
= getenv("MEMCACHED_SERVERS")))
51 opt_servers
= strdup(temp
);
54 fprintf(stderr
, "No Servers provided\n");
59 memc
= memcached_create(NULL
);
60 process_hash_option(memc
, opt_hash
);
62 servers
= memcached_servers_parse(opt_servers
);
63 memcached_server_push(memc
, servers
);
64 memcached_server_list_free(servers
);
65 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
66 (uint64_t) opt_binary
);
68 if (!initialize_sasl(memc
, opt_username
, opt_passwd
))
77 printf("key: %s\nexpires: %llu\n", argv
[optind
], (unsigned long long)opt_expire
);
78 rc
= memcached_delete(memc
, argv
[optind
], strlen(argv
[optind
]), opt_expire
);
80 if (rc
!= MEMCACHED_SUCCESS
)
82 fprintf(stderr
, "memrm: %s: memcache error %s",
83 argv
[optind
], memcached_strerror(memc
, rc
));
84 if (memc
->cached_errno
)
85 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
86 fprintf(stderr
, "\n");
107 static void options_parse(int argc
, char *argv
[])
109 memcached_programs_help_st help_options
[]=
114 static struct option long_options
[]=
116 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
117 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
118 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
119 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
120 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
121 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
122 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
123 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
124 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
125 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
133 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
134 if (option_rv
== -1) break;
142 case OPT_VERBOSE
: /* --verbose or -v */
143 opt_verbose
= OPT_VERBOSE
;
145 case OPT_DEBUG
: /* --debug or -d */
146 opt_verbose
= OPT_DEBUG
;
148 case OPT_VERSION
: /* --version or -V */
149 version_command(PROGRAM_NAME
);
151 case OPT_HELP
: /* --help or -h */
152 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
154 case OPT_SERVERS
: /* --servers or -s */
155 opt_servers
= strdup(optarg
);
157 case OPT_EXPIRE
: /* --expire */
158 opt_expire
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
161 opt_hash
= strdup(optarg
);
164 opt_username
= optarg
;
170 /* getopt_long already printed an error message. */