Fix for bug #15450
[m6w6/libmemcached] / clients / memcat.c
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11
12 #include "libmemcached/common.h"
13 #include <stdio.h>
14 #include <inttypes.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <getopt.h>
18 #include <libmemcached/memcached.h>
19
20 #include "utilities.h"
21
22 #define PROGRAM_NAME "memcat"
23 #define PROGRAM_DESCRIPTION "Cat a set of key values to stdout."
24
25
26 /* Prototypes */
27 void options_parse(int argc, char *argv[]);
28
29 static int opt_binary= 0;
30 static int opt_verbose= 0;
31 static int opt_displayflag= 0;
32 static char *opt_servers= NULL;
33 static char *opt_hash= NULL;
34
35 int main(int argc, char *argv[])
36 {
37 memcached_st *memc;
38 char *string;
39 size_t string_length;
40 uint32_t flags;
41 memcached_return_t rc;
42 memcached_server_st *servers;
43
44 options_parse(argc, argv);
45
46 if (!opt_servers)
47 {
48 char *temp;
49
50 if ((temp= getenv("MEMCACHED_SERVERS")))
51 opt_servers= strdup(temp);
52 else
53 {
54 fprintf(stderr, "No Servers provided\n");
55 exit(1);
56 }
57 }
58
59 memc= memcached_create(NULL);
60 process_hash_option(memc, opt_hash);
61
62 servers= memcached_servers_parse(opt_servers);
63
64 memcached_server_push(memc, servers);
65 memcached_server_list_free(servers);
66 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
67 (uint64_t)opt_binary);
68
69 while (optind < argc)
70 {
71 string= memcached_get(memc, argv[optind], strlen(argv[optind]),
72 &string_length, &flags, &rc);
73 if (rc == MEMCACHED_SUCCESS)
74 {
75 if (opt_displayflag)
76 {
77 if (opt_verbose)
78 printf("key: %s\nflags: ", argv[optind]);
79 printf("%x\n", flags);
80 }
81 else
82 {
83 if (opt_verbose)
84 printf("key: %s\nflags: %x\nlength: %zu\nvalue: ",
85 argv[optind], flags, string_length);
86 printf("%.*s\n", (int)string_length, string);
87 free(string);
88 }
89 }
90 else if (rc != MEMCACHED_NOTFOUND)
91 {
92 fprintf(stderr, "memcat: %s: memcache error %s",
93 argv[optind], memcached_strerror(memc, rc));
94 if (memc->cached_errno)
95 fprintf(stderr, " system error %s", strerror(memc->cached_errno));
96 fprintf(stderr, "\n");
97 }
98 optind++;
99 }
100
101 memcached_free(memc);
102
103 if (opt_servers)
104 free(opt_servers);
105 if (opt_hash)
106 free(opt_hash);
107
108 return 0;
109 }
110
111
112 void options_parse(int argc, char *argv[])
113 {
114 int option_index= 0;
115 int option_rv;
116
117 memcached_programs_help_st help_options[]=
118 {
119 {0},
120 };
121
122 static struct option long_options[]=
123 {
124 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
125 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
126 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
127 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
128 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
129 {(OPTIONSTRING)"flag", no_argument, &opt_displayflag, OPT_FLAG},
130 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
131 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
132 {0, 0, 0, 0},
133 };
134
135 while (1)
136 {
137 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
138 if (option_rv == -1) break;
139 switch (option_rv)
140 {
141 case 0:
142 break;
143 case OPT_BINARY:
144 opt_binary = 1;
145 break;
146 case OPT_VERBOSE: /* --verbose or -v */
147 opt_verbose = OPT_VERBOSE;
148 break;
149 case OPT_DEBUG: /* --debug or -d */
150 opt_verbose = OPT_DEBUG;
151 break;
152 case OPT_VERSION: /* --version or -V */
153 version_command(PROGRAM_NAME);
154 break;
155 case OPT_HELP: /* --help or -h */
156 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
157 break;
158 case OPT_SERVERS: /* --servers or -s */
159 opt_servers= strdup(optarg);
160 break;
161 case OPT_HASH:
162 opt_hash= strdup(optarg);
163 break;
164 case '?':
165 /* getopt_long already printed an error message. */
166 exit(1);
167 default:
168 abort();
169 }
170 }
171 }