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
[])
45 memcached_return_t rc
;
47 int return_code
= EXIT_SUCCESS
;
49 options_parse(argc
, argv
);
52 if (opt_servers
== NULL
)
56 if ((temp
= getenv("MEMCACHED_SERVERS")))
58 opt_servers
= strdup(temp
);
61 if (opt_servers
== NULL
)
63 std::cerr
<< "No servers provied" << std::endl
;
68 memcached_server_st
* servers
= memcached_servers_parse(opt_servers
);
69 if (servers
== NULL
or memcached_server_list_count(servers
) == 0)
71 std::cerr
<< "Invalid server list provided:" << opt_servers
<< std::endl
;
75 memcached_st
* memc
= memcached_create(NULL
);
76 process_hash_option(memc
, opt_hash
);
78 memcached_server_push(memc
, servers
);
79 memcached_server_list_free(servers
);
80 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
81 (uint64_t)opt_binary
);
83 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
86 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
92 memcached_return_t ret
;
93 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
)))
95 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
101 while (optind
< argc
)
103 string
= memcached_get(memc
, argv
[optind
], strlen(argv
[optind
]),
104 &string_length
, &flags
, &rc
);
105 if (rc
== MEMCACHED_SUCCESS
)
111 std::cout
<< "key: " << argv
[optind
] << std::endl
<< "flags: " << flags
<< std::endl
;
118 std::cout
<< "key: " << argv
[optind
] << std::endl
<< "flags: " << flags
<< "length: " << string_length
<< std::endl
<< "value: ";
123 FILE *fp
= fopen(opt_file
, "w");
127 return_code
= EXIT_FAILURE
;
131 size_t written
= fwrite(string
, 1, string_length
, fp
);
132 if (written
!= string_length
)
134 std::cerr
<< "error writing file to file " << opt_file
<< " wrote " << written
<< ", should have written" << string_length
<< std::endl
;
135 return_code
= EXIT_FAILURE
;
141 std::cerr
<< "error closing " << opt_file
<< std::endl
;
142 return_code
= EXIT_FAILURE
;
148 std::cout
.write(string
, string_length
);
149 std::cout
<< std::endl
;
154 else if (rc
!= MEMCACHED_NOTFOUND
)
156 std::cerr
<< "error on " << argv
[optind
] << "(" << memcached_strerror(memc
, rc
) << ")";
157 if (memcached_last_error_errno(memc
))
159 std::cerr
<< " system error (" << strerror(memcached_last_error_errno(memc
)) << ")" << std::endl
;
161 std::cerr
<< std::endl
;
163 return_code
= EXIT_FAILURE
;
166 else // Unknown Issue
168 std::cerr
<< "error on " << argv
[optind
] << "("<< memcached_strerror(NULL
, rc
) << ")" << std::endl
;
169 return_code
= EXIT_FAILURE
;
174 memcached_free(memc
);
189 void options_parse(int argc
, char *argv
[])
193 memcached_programs_help_st help_options
[]=
198 static struct option long_options
[]=
200 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
201 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
202 {(OPTIONSTRING
)"quiet", no_argument
, NULL
, OPT_QUIET
},
203 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
204 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
205 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
206 {(OPTIONSTRING
)"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
207 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
208 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
209 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
210 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
211 {(OPTIONSTRING
)"file", required_argument
, NULL
, OPT_FILE
},
217 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
218 if (option_rv
== -1) break;
226 case OPT_VERBOSE
: /* --verbose or -v */
227 opt_verbose
= OPT_VERBOSE
;
229 case OPT_DEBUG
: /* --debug or -d */
230 opt_verbose
= OPT_DEBUG
;
232 case OPT_VERSION
: /* --version or -V */
233 version_command(PROGRAM_NAME
);
235 case OPT_HELP
: /* --help or -h */
236 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
238 case OPT_SERVERS
: /* --servers or -s */
239 opt_servers
= strdup(optarg
);
242 opt_hash
= strdup(optarg
);
245 opt_username
= optarg
;
259 /* getopt_long already printed an error message. */