2 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
13 #include <mem_config.h>
20 #include <libmemcached-1.0/memcached.h>
22 #include "utilities.h"
24 #define PROGRAM_NAME "memcat"
25 #define PROGRAM_DESCRIPTION "Cat a set of key values to stdout."
29 void options_parse(int argc
, char *argv
[]);
31 static int opt_binary
= 0;
32 static int opt_verbose
= 0;
33 static int opt_displayflag
= 0;
34 static char *opt_servers
= NULL
;
35 static char *opt_hash
= NULL
;
36 static char *opt_username
;
37 static char *opt_passwd
;
38 static char *opt_file
;
40 int main(int argc
, char *argv
[])
46 memcached_return_t rc
;
47 memcached_server_st
*servers
;
49 int return_code
= EXIT_SUCCESS
;
51 options_parse(argc
, argv
);
58 if ((temp
= getenv("MEMCACHED_SERVERS")))
60 opt_servers
= strdup(temp
);
64 std::cerr
<< "No servers provied" << std::endl
;
69 memc
= memcached_create(NULL
);
70 process_hash_option(memc
, opt_hash
);
72 servers
= memcached_servers_parse(opt_servers
);
74 memcached_server_push(memc
, servers
);
75 memcached_server_list_free(servers
);
76 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
77 (uint64_t)opt_binary
);
79 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
82 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
88 memcached_return_t ret
;
89 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
)))
91 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
99 string
= memcached_get(memc
, argv
[optind
], strlen(argv
[optind
]),
100 &string_length
, &flags
, &rc
);
101 if (rc
== MEMCACHED_SUCCESS
)
107 std::cout
<< "key: " << argv
[optind
] << std::endl
<< "flags: " << flags
<< std::endl
;
114 std::cout
<< "key: " << argv
[optind
] << std::endl
<< "flags: " << flags
<< "length: " << string_length
<< std::endl
<< "value: ";
119 FILE *fp
= fopen(opt_file
, "w");
123 return_code
= EXIT_FAILURE
;
127 size_t written
= fwrite(string
, 1, string_length
, fp
);
128 if (written
!= string_length
)
130 std::cerr
<< "error writing file to file " << opt_file
<< " wrote " << written
<< ", should have written" << string_length
<< std::endl
;
131 return_code
= EXIT_FAILURE
;
137 std::cerr
<< "error closing " << opt_file
<< std::endl
;
138 return_code
= EXIT_FAILURE
;
144 std::cout
.write(string
, string_length
);
145 std::cout
<< std::endl
;
150 else if (rc
!= MEMCACHED_NOTFOUND
)
152 std::cerr
<< "error on " << argv
[optind
] << "(" << memcached_strerror(memc
, rc
) << ")";
153 if (memcached_last_error_errno(memc
))
155 std::cerr
<< " system error (" << strerror(memcached_last_error_errno(memc
)) << ")" << std::endl
;
157 std::cerr
<< std::endl
;
159 return_code
= EXIT_FAILURE
;
162 else // Unknown Issue
164 std::cerr
<< "error on " << argv
[optind
] << "("<< memcached_strerror(NULL
, rc
) << ")" << std::endl
;
165 return_code
= EXIT_FAILURE
;
170 memcached_free(memc
);
185 void options_parse(int argc
, char *argv
[])
189 memcached_programs_help_st help_options
[]=
194 static struct option long_options
[]=
196 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
197 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
198 {(OPTIONSTRING
)"quiet", no_argument
, NULL
, OPT_QUIET
},
199 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
200 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
201 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
202 {(OPTIONSTRING
)"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
203 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
204 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
205 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
206 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
207 {(OPTIONSTRING
)"file", required_argument
, NULL
, OPT_FILE
},
213 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
214 if (option_rv
== -1) break;
222 case OPT_VERBOSE
: /* --verbose or -v */
223 opt_verbose
= OPT_VERBOSE
;
225 case OPT_DEBUG
: /* --debug or -d */
226 opt_verbose
= OPT_DEBUG
;
228 case OPT_VERSION
: /* --version or -V */
229 version_command(PROGRAM_NAME
);
231 case OPT_HELP
: /* --help or -h */
232 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
234 case OPT_SERVERS
: /* --servers or -s */
235 opt_servers
= strdup(optarg
);
238 opt_hash
= strdup(optarg
);
241 opt_username
= optarg
;
255 /* getopt_long already printed an error message. */