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"
23 #include "common/checks.hpp"
28 memcached_return_t
memcat(const client_options
&opt
, memcached_st
*memc
, const char *key
, std::ostream
*ref
) {
29 memcached_return_t rc
;
32 auto val
= memcached_get(memc
, key
, strlen(key
), &len
, &flags
, &rc
);
33 auto verbose
= opt
.isset("verbose");
35 if (MEMCACHED_SUCCESS
== rc
) {
37 *ref
<< "key: " << key
<< "\n";
39 if (opt
.isset("flags")) {
43 *ref
<< flags
<< "\n";
51 if (verbose
|| !opt
.isset("file")) {
64 int main(int argc
, char *argv
[]) {
65 client_options opt
{PROGRAM_NAME
, PROGRAM_VERSION
, PROGRAM_DESCRIPTION
, "key [key ...]"};
67 for (const auto &def
: opt
.defaults
) {
70 opt
.add("flags", 'F', no_argument
, "Display key flags, too.");
71 opt
.add("file", 'f', required_argument
, "Output to file instead of standard output.");
73 char **argp
= nullptr;
74 if (!opt
.parse(argc
, argv
, &argp
)) {
78 if (opt
.isset("quiet") && !opt
.isset("file")) {
79 std::cerr
<< "--quiet operation was requested, but --file was not set.\n";
84 if (!check_memcached(opt
, memc
)) {
88 if (!opt
.apply(&memc
)) {
89 memcached_free(&memc
);
93 if (!check_argp(opt
, argp
, "No key(s) provided.")) {
94 memcached_free(&memc
);
98 auto exit_code
= EXIT_SUCCESS
;
99 for (auto arg
= argp
; *arg
; ++arg
) {
102 std::ofstream fstream
{};
103 std::ostream
*ostream
= check_ostream(opt
, opt
.argof("file"), fstream
);
105 if (!check_return(opt
, memc
, key
, memcat(opt
, &memc
, key
, ostream
))) {
106 exit_code
= EXIT_FAILURE
;
111 memcached_free(&memc
);