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"
18 #define PROGRAM_NAME "memcat"
19 #define PROGRAM_DESCRIPTION "Cat a set of key values to stdout."
20 #define PROGRAM_VERSION "1.1"
22 #include "common/options.hpp"
27 memcached_return_t
memcat(const client_options
&opt
, memcached_st
*memc
, const char *key
, std::ostream
&ref
) {
28 memcached_return_t rc
;
31 auto val
= memcached_get(memc
, key
, strlen(key
), &len
, &flags
, &rc
);
33 if (MEMCACHED_SUCCESS
== rc
) {
34 if (opt
.isset("verbose")) {
35 ref
<< "key: " << key
<< "\n";
37 if (opt
.isset("flags")) {
38 ref
<< "flags: " << flags
<< "\n";
40 if (opt
.isset("verbose")) {
53 int main(int argc
, char *argv
[]) {
54 char **argp
= nullptr;
56 client_options opt
{PROGRAM_NAME
, PROGRAM_VERSION
, PROGRAM_DESCRIPTION
, "key [ key ... ]"};
58 for (const auto &def
: opt
.defaults
) {
61 opt
.add("flags", 'F', no_argument
, "Display key flags, too.");
62 opt
.add("file", 'f', required_argument
, "Output to file instead of standard output.");
64 if (!opt
.parse(argc
, argv
, &argp
)) {
68 if (opt
.isset("quiet") && !opt
.isset("file")) {
69 std::cerr
<< "--quiet operation was requested, but --file was not set.\n";
73 if (!memcached_create(&memc
)) {
74 if (!opt
.isset("quiet")) {
75 std::cerr
<< "Failed to initialize memcached client.\n";
80 if (!opt
.apply(&memc
)) {
81 memcached_free(&memc
);
86 if (!opt
.isset("quiet")) {
87 std::cerr
<< "No key(s) provided.\n";
89 memcached_free(&memc
);
93 auto exit_code
= EXIT_SUCCESS
;
94 for (auto arg
= argp
; *arg
; ++arg
) {
97 memcached_return_t rc
;
98 auto file
= opt
.argof("file");
100 std::ofstream stream
{file
, std::ios::binary
};
101 rc
= memcat(opt
, &memc
, key
, stream
);
103 rc
= memcat(opt
, &memc
, key
, std::cout
);
105 if (MEMCACHED_SUCCESS
!= rc
) {
106 exit_code
= EXIT_FAILURE
;
108 if (MEMCACHED_NOTFOUND
== rc
) {
109 if (opt
.isset("verbose")) {
110 std::cerr
<< "not found: " << key
<< "\n";
114 if (!opt
.isset("quiet")) {
115 std::cerr
<< memcached_last_error_message(&memc
) << "\n";
123 memcached_free(&memc
);