2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
19 #include <libmemcached/memcached.h>
21 #include "utilities.h"
23 #define PROGRAM_NAME "memcat"
24 #define PROGRAM_DESCRIPTION "Cat a set of key values to stdout."
28 void options_parse(int argc
, char *argv
[]);
30 static int opt_binary
= 0;
31 static int opt_verbose
= 0;
32 static int opt_displayflag
= 0;
33 static char *opt_servers
= NULL
;
34 static char *opt_hash
= NULL
;
35 static char *opt_username
;
36 static char *opt_passwd
;
37 static char *opt_file
;
39 int main(int argc
, char *argv
[])
45 memcached_return_t rc
;
46 memcached_server_st
*servers
;
48 int return_code
= EXIT_SUCCESS
;
50 options_parse(argc
, argv
);
57 if ((temp
= getenv("MEMCACHED_SERVERS")))
59 opt_servers
= strdup(temp
);
63 std::cerr
<< "No servers provied" << std::endl
;
68 memc
= memcached_create(NULL
);
69 process_hash_option(memc
, opt_hash
);
71 servers
= memcached_servers_parse(opt_servers
);
73 memcached_server_push(memc
, servers
);
74 memcached_server_list_free(servers
);
75 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
76 (uint64_t)opt_binary
);
78 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
81 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
87 memcached_return_t ret
;
88 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
)))
90 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
98 string
= memcached_get(memc
, argv
[optind
], strlen(argv
[optind
]),
99 &string_length
, &flags
, &rc
);
100 if (rc
== MEMCACHED_SUCCESS
)
106 std::cout
<< "key: " << argv
[optind
] << std::endl
<< "flags: " << flags
<< std::endl
;
113 std::cout
<< "key: " << argv
[optind
] << std::endl
<< "flags: " << flags
<< "length: " << string_length
<< std::endl
<< "value: ";
118 FILE *fp
= fopen(opt_file
, "w");
122 return_code
= EXIT_FAILURE
;
126 size_t written
= fwrite(string
, 1, string_length
, fp
);
127 if (written
!= string_length
)
129 std::cerr
<< "error writing file to file " << opt_file
<< " wrote " << written
<< ", should have written" << string_length
<< std::endl
;
130 return_code
= EXIT_FAILURE
;
136 std::cerr
<< "error closing " << opt_file
<< std::endl
;
137 return_code
= EXIT_FAILURE
;
143 std::cout
.write(string
, string_length
);
144 std::cout
<< std::endl
;
149 else if (rc
!= MEMCACHED_NOTFOUND
)
151 std::cerr
<< "error on " << argv
[optind
] << "(" << memcached_strerror(memc
, rc
) << ")";
152 if (memcached_last_error_errno(memc
))
154 std::cerr
<< " system error (" << strerror(memcached_last_error_errno(memc
)) << ")" << std::endl
;
156 std::cerr
<< std::endl
;
158 return_code
= EXIT_FAILURE
;
161 else // Unknown Issue
163 std::cerr
<< "error on " << argv
[optind
] << "("<< memcached_strerror(NULL
, rc
) << ")" << std::endl
;
164 return_code
= EXIT_FAILURE
;
169 memcached_free(memc
);
184 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 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. */