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"
23 #include "libmemcached-1.0/memcached.h"
25 #include "utilities.h"
27 #define PROGRAM_NAME "memcat"
28 #define PROGRAM_DESCRIPTION "Cat a set of key values to stdout."
31 void options_parse(int argc
, char *argv
[]);
33 static int opt_binary
= 0;
34 static int opt_verbose
= 0;
35 static int opt_displayflag
= 0;
36 static char *opt_servers
= NULL
;
37 static char *opt_hash
= NULL
;
38 static char *opt_username
;
39 static char *opt_passwd
;
40 static char *opt_file
;
42 int main(int argc
, char *argv
[]) {
46 memcached_return_t rc
;
48 int return_code
= EXIT_SUCCESS
;
50 options_parse(argc
, argv
);
53 if (opt_servers
== NULL
) {
56 if ((temp
= getenv("MEMCACHED_SERVERS"))) {
57 opt_servers
= strdup(temp
);
60 if (opt_servers
== NULL
) {
61 std::cerr
<< "No servers provided" << std::endl
;
66 memcached_server_st
*servers
= memcached_servers_parse(opt_servers
);
67 if (servers
== NULL
or memcached_server_list_count(servers
) == 0) {
68 std::cerr
<< "Invalid server list provided:" << opt_servers
<< std::endl
;
72 memcached_st
*memc
= memcached_create(NULL
);
73 process_hash_option(memc
, opt_hash
);
75 memcached_server_push(memc
, servers
);
76 memcached_server_list_free(servers
);
77 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, (uint64_t) opt_binary
);
79 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0) {
81 std::cerr
<< "--username was supplied, but binary was not built with SASL support."
87 memcached_return_t ret
;
88 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
))) {
89 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
95 while (optind
< argc
) {
96 string
= memcached_get(memc
, argv
[optind
], strlen(argv
[optind
]), &string_length
, &flags
, &rc
);
97 if (rc
== MEMCACHED_SUCCESS
) {
98 if (opt_displayflag
) {
100 std::cout
<< "key: " << argv
[optind
] << std::endl
<< "flags: ";
102 std::cout
<< flags
<< std::endl
;
105 std::cout
<< "key: " << argv
[optind
] << std::endl
106 << "flags: " << flags
<< std::endl
107 << "length: " << string_length
<< std::endl
112 FILE *fp
= fopen(opt_file
, "w");
115 return_code
= EXIT_FAILURE
;
119 size_t written
= fwrite(string
, 1, string_length
, fp
);
120 if (written
!= string_length
) {
121 std::cerr
<< "error writing file to file " << opt_file
<< " wrote " << written
122 << ", should have written" << string_length
<< std::endl
;
123 return_code
= EXIT_FAILURE
;
128 std::cerr
<< "error closing " << opt_file
<< std::endl
;
129 return_code
= EXIT_FAILURE
;
133 std::cout
.write(string
, string_length
);
134 std::cout
<< std::endl
;
137 } else if (rc
!= MEMCACHED_NOTFOUND
) {
138 std::cerr
<< "error on " << argv
[optind
] << "(" << memcached_strerror(memc
, rc
) << ")";
139 if (memcached_last_error_errno(memc
)) {
140 std::cerr
<< " system error (" << strerror(memcached_last_error_errno(memc
)) << ")"
143 std::cerr
<< std::endl
;
145 return_code
= EXIT_FAILURE
;
147 } else // Unknown Issue
149 std::cerr
<< "error on " << argv
[optind
] << "(" << memcached_strerror(NULL
, rc
) << ")"
151 return_code
= EXIT_FAILURE
;
161 memcached_free(memc
);
173 void options_parse(int argc
, char *argv
[]) {
174 int option_index
= 0;
176 memcached_programs_help_st help_options
[] = {
180 static struct option long_options
[] = {
181 {(OPTIONSTRING
) "version", no_argument
, NULL
, OPT_VERSION
},
182 {(OPTIONSTRING
) "help", no_argument
, NULL
, OPT_HELP
},
183 {(OPTIONSTRING
) "quiet", no_argument
, NULL
, OPT_QUIET
},
184 {(OPTIONSTRING
) "verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
185 {(OPTIONSTRING
) "debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
186 {(OPTIONSTRING
) "servers", required_argument
, NULL
, OPT_SERVERS
},
187 {(OPTIONSTRING
) "flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
188 {(OPTIONSTRING
) "hash", required_argument
, NULL
, OPT_HASH
},
189 {(OPTIONSTRING
) "binary", no_argument
, NULL
, OPT_BINARY
},
190 {(OPTIONSTRING
) "username", required_argument
, NULL
, OPT_USERNAME
},
191 {(OPTIONSTRING
) "password", required_argument
, NULL
, OPT_PASSWD
},
192 {(OPTIONSTRING
) "file", required_argument
, NULL
, OPT_FILE
},
197 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
206 case OPT_VERBOSE
: /* --verbose or -v */
207 opt_verbose
= OPT_VERBOSE
;
209 case OPT_DEBUG
: /* --debug or -d */
210 opt_verbose
= OPT_DEBUG
;
212 case OPT_VERSION
: /* --version or -V */
213 version_command(PROGRAM_NAME
);
215 case OPT_HELP
: /* --help or -h */
216 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
218 case OPT_SERVERS
: /* --servers or -s */
219 opt_servers
= strdup(optarg
);
222 opt_hash
= strdup(optarg
);
225 opt_username
= optarg
;
239 /* getopt_long already printed an error message. */