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