1 #include "libmemcached/common.h"
7 #include <libmemcached/memcached.h>
11 #define PROGRAM_NAME "memcat"
12 #define PROGRAM_DESCRIPTION "Cat a set of key values to stdout."
16 void options_parse(int argc
, char *argv
[]);
18 static int opt_binary
= 0;
19 static int opt_verbose
= 0;
20 static int opt_displayflag
= 0;
21 static char *opt_servers
= NULL
;
22 static char *opt_hash
= NULL
;
24 int main(int argc
, char *argv
[])
31 memcached_server_st
*servers
;
33 options_parse(argc
, argv
);
39 if ((temp
= getenv("MEMCACHED_SERVERS")))
40 opt_servers
= strdup(temp
);
43 fprintf(stderr
, "No Servers provided\n");
48 memc
= memcached_create(NULL
);
49 process_hash_option(memc
, opt_hash
);
51 servers
= memcached_servers_parse(opt_servers
);
53 memcached_server_push(memc
, servers
);
54 memcached_server_list_free(servers
);
55 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
56 (uint64_t)opt_binary
);
60 string
= memcached_get(memc
, argv
[optind
], strlen(argv
[optind
]),
61 &string_length
, &flags
, &rc
);
62 if (rc
== MEMCACHED_SUCCESS
)
67 printf("key: %s\nflags: ", argv
[optind
]);
68 printf("%x\n", flags
);
73 printf("key: %s\nflags: %x\nlength: %zu\nvalue: ",
74 argv
[optind
], flags
, string_length
);
75 printf("%.*s\n", (int)string_length
, string
);
79 else if (rc
!= MEMCACHED_NOTFOUND
)
81 fprintf(stderr
, "memcat: %s: memcache error %s",
82 argv
[optind
], memcached_strerror(memc
, rc
));
83 if (memc
->cached_errno
)
84 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
85 fprintf(stderr
, "\n");
101 void options_parse(int argc
, char *argv
[])
106 memcached_programs_help_st help_options
[]=
111 static struct option long_options
[]=
113 {"version", no_argument
, NULL
, OPT_VERSION
},
114 {"help", no_argument
, NULL
, OPT_HELP
},
115 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
116 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
117 {"servers", required_argument
, NULL
, OPT_SERVERS
},
118 {"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
119 {"hash", required_argument
, NULL
, OPT_HASH
},
120 {"binary", no_argument
, NULL
, OPT_BINARY
},
126 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
127 if (option_rv
== -1) break;
135 case OPT_VERBOSE
: /* --verbose or -v */
136 opt_verbose
= OPT_VERBOSE
;
138 case OPT_DEBUG
: /* --debug or -d */
139 opt_verbose
= OPT_DEBUG
;
141 case OPT_VERSION
: /* --version or -V */
142 version_command(PROGRAM_NAME
);
144 case OPT_HELP
: /* --help or -h */
145 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
147 case OPT_SERVERS
: /* --servers or -s */
148 opt_servers
= strdup(optarg
);
151 opt_hash
= strdup(optarg
);
154 /* getopt_long already printed an error message. */