Merge in changes for ICC fix.
[m6w6/libmemcached] / clients / memrm.c
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11 #include "config.h"
12
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <getopt.h>
16 #include <libmemcached/memcached.h>
17 #include <string.h>
18 #include "client_options.h"
19 #include "utilities.h"
20
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;
28
29 #define PROGRAM_NAME "memrm"
30 #define PROGRAM_DESCRIPTION "Erase a key or set of keys from a memcached cluster."
31
32 /* Prototypes */
33 static void options_parse(int argc, char *argv[]);
34
35 int main(int argc, char *argv[])
36 {
37 memcached_st *memc;
38 memcached_return_t rc;
39 memcached_server_st *servers;
40
41 int return_code= 0;
42
43 options_parse(argc, argv);
44
45 if (!opt_servers)
46 {
47 char *temp;
48
49 if ((temp= getenv("MEMCACHED_SERVERS")))
50 opt_servers= strdup(temp);
51 else
52 {
53 fprintf(stderr, "No Servers provided\n");
54 exit(1);
55 }
56 }
57
58 memc= memcached_create(NULL);
59 process_hash_option(memc, opt_hash);
60
61 servers= memcached_servers_parse(opt_servers);
62 memcached_server_push(memc, servers);
63 memcached_server_list_free(servers);
64 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
65 (uint64_t) opt_binary);
66
67 if (!initialize_sasl(memc, opt_username, opt_passwd))
68 {
69 memcached_free(memc);
70 return 1;
71 }
72
73 while (optind < argc)
74 {
75 if (opt_verbose)
76 printf("key: %s\nexpires: %llu\n", argv[optind], (unsigned long long)opt_expire);
77 rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire);
78
79 if (rc != MEMCACHED_SUCCESS)
80 {
81 fprintf(stderr, "memrm: %s: memcache error %s",
82 argv[optind], memcached_strerror(memc, rc));
83 if (memc->cached_errno)
84 fprintf(stderr, " system error %s", strerror(memc->cached_errno));
85 fprintf(stderr, "\n");
86
87 return_code= -1;
88 }
89
90 optind++;
91 }
92
93 memcached_free(memc);
94
95 if (opt_servers)
96 free(opt_servers);
97 if (opt_hash)
98 free(opt_hash);
99
100 shutdown_sasl();
101
102 return return_code;
103 }
104
105
106 static void options_parse(int argc, char *argv[])
107 {
108 memcached_programs_help_st help_options[]=
109 {
110 {0},
111 };
112
113 static struct option long_options[]=
114 {
115 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
116 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
117 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
118 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
119 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
120 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
121 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
122 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
123 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
124 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
125 {0, 0, 0, 0},
126 };
127 int option_index= 0;
128 int option_rv;
129
130 while (1)
131 {
132 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
133 if (option_rv == -1) break;
134 switch (option_rv)
135 {
136 case 0:
137 break;
138 case OPT_BINARY:
139 opt_binary = 1;
140 break;
141 case OPT_VERBOSE: /* --verbose or -v */
142 opt_verbose = OPT_VERBOSE;
143 break;
144 case OPT_DEBUG: /* --debug or -d */
145 opt_verbose = OPT_DEBUG;
146 break;
147 case OPT_VERSION: /* --version or -V */
148 version_command(PROGRAM_NAME);
149 break;
150 case OPT_HELP: /* --help or -h */
151 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
152 break;
153 case OPT_SERVERS: /* --servers or -s */
154 opt_servers= strdup(optarg);
155 break;
156 case OPT_EXPIRE: /* --expire */
157 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
158 break;
159 case OPT_HASH:
160 opt_hash= strdup(optarg);
161 break;
162 case OPT_USERNAME:
163 opt_username= optarg;
164 break;
165 case OPT_PASSWD:
166 opt_passwd= optarg;
167 break;
168 case '?':
169 /* getopt_long already printed an error message. */
170 exit(1);
171 default:
172 abort();
173 }
174 }
175 }