Fix all include location, and drop versions of the library that were never shipped.
[awesomized/libmemcached] / clients / memexist.cc
1 /* LibMemcached
2 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
4 * All rights reserved.
5 *
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
8 *
9 * Summary:
10 *
11 */
12 #include "config.h"
13
14 #include <cstdio>
15 #include <cstring>
16 #include <getopt.h>
17 #include <iostream>
18 #include <unistd.h>
19
20 #include <libmemcached-1.0/memcached.h>
21 #include "client_options.h"
22 #include "utilities.h"
23
24 static int opt_binary= 0;
25 static int opt_verbose= 0;
26 static char *opt_servers= NULL;
27 static char *opt_hash= NULL;
28 static char *opt_username;
29 static char *opt_passwd;
30
31 #define PROGRAM_NAME "memexist"
32 #define PROGRAM_DESCRIPTION "Check for the existance of a key within a cluster."
33
34 /* Prototypes */
35 static void options_parse(int argc, char *argv[]);
36
37 int main(int argc, char *argv[])
38 {
39 memcached_st *memc;
40 memcached_server_st *servers;
41
42 options_parse(argc, argv);
43 initialize_sockets();
44
45 if (opt_servers == 0)
46 {
47 char *temp;
48
49 if ((temp= getenv("MEMCACHED_SERVERS")))
50 {
51 opt_servers= strdup(temp);
52 }
53 else
54 {
55 std::cerr << "No Servers provided" << std::endl;
56 return EXIT_FAILURE;
57 }
58 }
59
60 memc= memcached_create(NULL);
61 process_hash_option(memc, opt_hash);
62
63 servers= memcached_servers_parse(opt_servers);
64 memcached_server_push(memc, servers);
65 memcached_server_list_free(servers);
66 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
67 (uint64_t) opt_binary);
68
69 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
70 {
71 memcached_free(memc);
72 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
73 return EXIT_FAILURE;
74 }
75
76 if (opt_username)
77 {
78 memcached_return_t ret;
79 if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
80 {
81 std::cerr << memcached_last_error_message(memc) << std::endl;
82 memcached_free(memc);
83 return EXIT_FAILURE;
84 }
85 }
86
87 int return_code= EXIT_SUCCESS;
88
89 while (optind < argc)
90 {
91 memcached_return_t rc= memcached_exist(memc, argv[optind], strlen(argv[optind]));
92
93 if (rc == MEMCACHED_NOTFOUND)
94 {
95 if (opt_verbose)
96 {
97 std::cout << "Could not find key \"" << argv[optind] << "\"" << std::endl;
98 }
99
100 return_code= EXIT_FAILURE;
101 }
102 else if (memcached_failed(rc))
103 {
104 if (opt_verbose)
105 {
106 std::cerr << "Fatal error for key \"" << argv[optind] << "\" :" << memcached_last_error_message(memc) << std::endl;
107 }
108
109 return_code= EXIT_FAILURE;
110 }
111 else // success
112 {
113 if (opt_verbose)
114 {
115 std::cout << "Found key " << argv[optind] << std::endl;
116 }
117 }
118
119 optind++;
120 }
121
122 memcached_free(memc);
123
124 if (opt_servers)
125 {
126 free(opt_servers);
127 }
128
129 if (opt_hash)
130 {
131 free(opt_hash);
132 }
133
134 return return_code;
135 }
136
137
138 static void options_parse(int argc, char *argv[])
139 {
140 memcached_programs_help_st help_options[]=
141 {
142 {0},
143 };
144
145 static struct option long_options[]=
146 {
147 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
148 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
149 {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
150 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
151 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
152 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
153 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
154 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
155 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
156 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
157 {0, 0, 0, 0},
158 };
159
160 bool opt_version= false;
161 bool opt_help= false;
162 int option_index= 0;
163
164 while (1)
165 {
166 int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
167 if (option_rv == -1)
168 {
169 break;
170 }
171
172 switch (option_rv)
173 {
174 case 0:
175 break;
176
177 case OPT_BINARY:
178 opt_binary = 1;
179 break;
180
181 case OPT_VERBOSE: /* --verbose or -v */
182 opt_verbose = OPT_VERBOSE;
183 break;
184
185 case OPT_DEBUG: /* --debug or -d */
186 opt_verbose = OPT_DEBUG;
187 break;
188
189 case OPT_VERSION: /* --version or -V */
190 opt_version= true;
191 break;
192
193 case OPT_HELP: /* --help or -h */
194 opt_help= true;
195 break;
196
197 case OPT_SERVERS: /* --servers or -s */
198 opt_servers= strdup(optarg);
199 break;
200
201 case OPT_HASH:
202 opt_hash= strdup(optarg);
203 break;
204
205 case OPT_USERNAME:
206 opt_username= optarg;
207 break;
208
209 case OPT_PASSWD:
210 opt_passwd= optarg;
211 break;
212
213 case OPT_QUIET:
214 close_stdio();
215 break;
216
217 case '?':
218 /* getopt_long already printed an error message. */
219 exit(EXIT_SUCCESS);
220
221 default:
222 abort();
223 }
224 }
225
226 if (opt_version)
227 {
228 version_command(PROGRAM_NAME);
229 exit(EXIT_SUCCESS);
230 }
231
232 if (opt_help)
233 {
234 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
235 exit(EXIT_SUCCESS);
236 }
237 }