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