2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
16 #include "mem_config.h"
24 #include "libmemcached-1.0/memcached.h"
25 #include "client_options.h"
26 #include "utilities.h"
28 static int opt_binary
= 0;
29 static int opt_verbose
= 0;
30 static char *opt_servers
= NULL
;
31 static char *opt_hash
= NULL
;
32 static char *opt_username
;
33 static char *opt_passwd
;
35 #define PROGRAM_NAME "memexist"
36 #define PROGRAM_DESCRIPTION "Check for the existance of a key within a cluster."
39 static void options_parse(int argc
, char *argv
[]);
41 int main(int argc
, char *argv
[]) {
42 options_parse(argc
, argv
);
45 if (opt_servers
== NULL
) {
48 if ((temp
= getenv("MEMCACHED_SERVERS"))) {
49 opt_servers
= strdup(temp
);
52 if (opt_servers
== NULL
) {
53 std::cerr
<< "No Servers provided" << std::endl
;
58 memcached_server_st
*servers
= memcached_servers_parse(opt_servers
);
59 if (servers
== NULL
or memcached_server_list_count(servers
) == 0) {
60 std::cerr
<< "Invalid server list provided:" << opt_servers
<< std::endl
;
64 memcached_st
*memc
= memcached_create(NULL
);
65 process_hash_option(memc
, opt_hash
);
67 memcached_server_push(memc
, servers
);
68 memcached_server_list_free(servers
);
69 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, (uint64_t) opt_binary
);
71 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0) {
73 std::cerr
<< "--username was supplied, but binary was not built with SASL support."
79 memcached_return_t ret
;
80 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
))) {
81 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
87 int return_code
= EXIT_SUCCESS
;
89 while (optind
< argc
) {
90 memcached_return_t rc
= memcached_exist(memc
, argv
[optind
], strlen(argv
[optind
]));
92 if (rc
== MEMCACHED_NOTFOUND
) {
94 std::cout
<< "Could not find key \"" << argv
[optind
] << "\"" << std::endl
;
97 return_code
= EXIT_FAILURE
;
98 } else if (memcached_failed(rc
)) {
100 std::cerr
<< "Fatal error for key \"" << argv
[optind
]
101 << "\" :" << memcached_last_error_message(memc
) << std::endl
;
104 return_code
= EXIT_FAILURE
;
108 std::cout
<< "Found key " << argv
[optind
] << std::endl
;
115 memcached_free(memc
);
128 static void options_parse(int argc
, char *argv
[]) {
129 memcached_programs_help_st help_options
[] = {
133 static struct option long_options
[] = {
134 {(OPTIONSTRING
) "version", no_argument
, NULL
, OPT_VERSION
},
135 {(OPTIONSTRING
) "help", no_argument
, NULL
, OPT_HELP
},
136 {(OPTIONSTRING
) "quiet", no_argument
, NULL
, OPT_QUIET
},
137 {(OPTIONSTRING
) "verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
138 {(OPTIONSTRING
) "debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
139 {(OPTIONSTRING
) "servers", required_argument
, NULL
, OPT_SERVERS
},
140 {(OPTIONSTRING
) "hash", required_argument
, NULL
, OPT_HASH
},
141 {(OPTIONSTRING
) "binary", no_argument
, NULL
, OPT_BINARY
},
142 {(OPTIONSTRING
) "username", required_argument
, NULL
, OPT_USERNAME
},
143 {(OPTIONSTRING
) "password", required_argument
, NULL
, OPT_PASSWD
},
147 bool opt_version
= false;
148 bool opt_help
= false;
149 int option_index
= 0;
152 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
153 if (option_rv
== -1) {
160 case OPT_BINARY
: opt_binary
= 1; break;
162 case OPT_VERBOSE
: /* --verbose or -v */ opt_verbose
= OPT_VERBOSE
; break;
164 case OPT_DEBUG
: /* --debug or -d */ opt_verbose
= OPT_DEBUG
; break;
166 case OPT_VERSION
: /* --version or -V */ opt_version
= true; break;
168 case OPT_HELP
: /* --help or -h */ opt_help
= true; break;
170 case OPT_SERVERS
: /* --servers or -s */ opt_servers
= strdup(optarg
); break;
172 case OPT_HASH
: opt_hash
= strdup(optarg
); break;
174 case OPT_USERNAME
: opt_username
= optarg
; break;
176 case OPT_PASSWD
: opt_passwd
= optarg
; break;
178 case OPT_QUIET
: close_stdio(); break;
181 /* getopt_long already printed an error message. */
189 version_command(PROGRAM_NAME
);
194 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);