fixed spelling in of testudp in .bzrignore
[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 int return_code= 0;
45
46 options_parse(argc, argv);
47
48 if (!opt_servers)
49 {
50 char *temp;
51
52 if ((temp= getenv("MEMCACHED_SERVERS")))
53 opt_servers= strdup(temp);
54 else
55 {
56 fprintf(stderr, "No Servers provided\n");
57 exit(1);
58 }
59 }
60
61 memc= memcached_create(NULL);
62 process_hash_option(memc, opt_hash);
63
64 servers= memcached_servers_parse(opt_servers);
65
66 memcached_server_push(memc, servers);
67 memcached_server_list_free(servers);
68 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
69 (uint64_t)opt_binary);
70
71 while (optind < argc)
72 {
73 string= memcached_get(memc, argv[optind], strlen(argv[optind]),
74 &string_length, &flags, &rc);
75 if (rc == MEMCACHED_SUCCESS)
76 {
77 if (opt_displayflag)
78 {
79 if (opt_verbose)
80 printf("key: %s\nflags: ", argv[optind]);
81 printf("%x\n", flags);
82 }
83 else
84 {
85 if (opt_verbose)
86 printf("key: %s\nflags: %x\nlength: %zu\nvalue: ",
87 argv[optind], flags, string_length);
88 printf("%.*s\n", (int)string_length, string);
89 free(string);
90 }
91 }
92 else if (rc != MEMCACHED_NOTFOUND)
93 {
94 fprintf(stderr, "memcat: %s: memcache error %s",
95 argv[optind], memcached_strerror(memc, rc));
96 if (memc->cached_errno)
97 fprintf(stderr, " system error %s", strerror(memc->cached_errno));
98 fprintf(stderr, "\n");
99
100 return_code= -1;
101 break;
102 }
103 else // Unknown Issue
104 {
105 fprintf(stderr, "memcat: %s not found\n", argv[optind]);
106 return_code= -1;
107 }
108 optind++;
109 }
110
111 memcached_free(memc);
112
113 if (opt_servers)
114 free(opt_servers);
115 if (opt_hash)
116 free(opt_hash);
117
118 return return_code;
119 }
120
121
122 void options_parse(int argc, char *argv[])
123 {
124 int option_index= 0;
125 int option_rv;
126
127 memcached_programs_help_st help_options[]=
128 {
129 {0},
130 };
131
132 static struct option long_options[]=
133 {
134 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
135 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
136 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
137 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
138 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
139 {(OPTIONSTRING)"flag", no_argument, &opt_displayflag, OPT_FLAG},
140 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
141 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
142 {0, 0, 0, 0},
143 };
144
145 while (1)
146 {
147 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
148 if (option_rv == -1) break;
149 switch (option_rv)
150 {
151 case 0:
152 break;
153 case OPT_BINARY:
154 opt_binary = 1;
155 break;
156 case OPT_VERBOSE: /* --verbose or -v */
157 opt_verbose = OPT_VERBOSE;
158 break;
159 case OPT_DEBUG: /* --debug or -d */
160 opt_verbose = OPT_DEBUG;
161 break;
162 case OPT_VERSION: /* --version or -V */
163 version_command(PROGRAM_NAME);
164 break;
165 case OPT_HELP: /* --help or -h */
166 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
167 break;
168 case OPT_SERVERS: /* --servers or -s */
169 opt_servers= strdup(optarg);
170 break;
171 case OPT_HASH:
172 opt_hash= strdup(optarg);
173 break;
174 case '?':
175 /* getopt_long already printed an error message. */
176 exit(1);
177 default:
178 abort();
179 }
180 }
181 }