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: " << flags
<< std::endl
;
104 std::cout
<< "key: " << argv
[optind
] << std::endl
105 << "flags: " << flags
<< std::endl
106 << "length: " << string_length
<< std::endl
111 FILE *fp
= fopen(opt_file
, "w");
114 return_code
= EXIT_FAILURE
;
118 size_t written
= fwrite(string
, 1, string_length
, fp
);
119 if (written
!= string_length
) {
120 std::cerr
<< "error writing file to file " << opt_file
<< " wrote " << written
121 << ", should have written" << string_length
<< std::endl
;
122 return_code
= EXIT_FAILURE
;
127 std::cerr
<< "error closing " << opt_file
<< std::endl
;
128 return_code
= EXIT_FAILURE
;
132 std::cout
.write(string
, string_length
);
133 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
;
156 memcached_free(memc
);
168 void options_parse(int argc
, char *argv
[]) {
169 int option_index
= 0;
171 memcached_programs_help_st help_options
[] = {
175 static struct option long_options
[] = {
176 {(OPTIONSTRING
) "version", no_argument
, NULL
, OPT_VERSION
},
177 {(OPTIONSTRING
) "help", no_argument
, NULL
, OPT_HELP
},
178 {(OPTIONSTRING
) "quiet", no_argument
, NULL
, OPT_QUIET
},
179 {(OPTIONSTRING
) "verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
180 {(OPTIONSTRING
) "debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
181 {(OPTIONSTRING
) "servers", required_argument
, NULL
, OPT_SERVERS
},
182 {(OPTIONSTRING
) "flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
183 {(OPTIONSTRING
) "hash", required_argument
, NULL
, OPT_HASH
},
184 {(OPTIONSTRING
) "binary", no_argument
, NULL
, OPT_BINARY
},
185 {(OPTIONSTRING
) "username", required_argument
, NULL
, OPT_USERNAME
},
186 {(OPTIONSTRING
) "password", required_argument
, NULL
, OPT_PASSWD
},
187 {(OPTIONSTRING
) "file", required_argument
, NULL
, OPT_FILE
},
192 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
197 case OPT_BINARY
: opt_binary
= 1; break;
198 case OPT_VERBOSE
: /* --verbose or -v */ opt_verbose
= OPT_VERBOSE
; break;
199 case OPT_DEBUG
: /* --debug or -d */ opt_verbose
= OPT_DEBUG
; break;
200 case OPT_VERSION
: /* --version or -V */ version_command(PROGRAM_NAME
); break;
201 case OPT_HELP
: /* --help or -h */
202 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
204 case OPT_SERVERS
: /* --servers or -s */ opt_servers
= strdup(optarg
); break;
205 case OPT_HASH
: opt_hash
= strdup(optarg
); break;
206 case OPT_USERNAME
: opt_username
= optarg
; break;
207 case OPT_PASSWD
: opt_passwd
= optarg
; break;
208 case OPT_FILE
: opt_file
= optarg
; break;
210 case OPT_QUIET
: close_stdio(); break;
213 /* getopt_long already printed an error message. */