Adding memstat framework.
[awesomized/libmemcached] / src / memcat.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <getopt.h>
4 #include <memcached.h>
5
6 #include "client_options.h"
7 #include "utilities.h"
8
9
10 /* Prototypes */
11 void options_parse(int argc, char *argv[]);
12
13 static int opt_verbose;
14 static int opt_displayflag;
15 static char *opt_servers;
16
17 int main(int argc, char *argv[])
18 {
19 memcached_st *memc;
20 char *string;
21 size_t string_length;
22 uint16_t flags;
23 memcached_return rc;
24 unsigned int x;
25
26 memc= memcached_init(NULL);
27
28 options_parse(argc, argv);
29
30 parse_opt_servers(memc, opt_servers);
31
32 for (x= 1; x < argc; x++)
33 {
34 string= memcached_get(memc, argv[x], strlen(argv[x]),
35 &string_length, &flags, &rc);
36 if (rc == MEMCACHED_SUCCESS) {
37 if (opt_displayflag) {
38 printf("%d\n", flags);
39 } else {
40 if (string)
41 {
42 printf("%.*s\n", string_length, string);
43 free(string);
44 }
45 }
46 }
47 }
48
49 memcached_deinit(memc);
50
51 return 0;
52 };
53
54
55 void options_parse(int argc, char *argv[])
56 {
57 int option_index = 0;
58 int option_rv;
59
60 static struct option long_options[] =
61 {
62 {"version", no_argument, NULL, OPT_VERSION},
63 {"help", no_argument, NULL, OPT_HELP},
64 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
65 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
66 {"servers", required_argument, NULL, OPT_SERVERS},
67 {"flag", no_argument, &opt_displayflag, OPT_FLAG},
68 {0, 0, 0, 0},
69 };
70
71 while (1)
72 {
73 option_rv = getopt_long(argc, argv, "", long_options, &option_index);
74 if (option_rv == -1) break;
75 switch (option_rv) {
76 case 0:
77 break;
78 case OPT_VERSION: /* --version */
79 printf("memcache tools, memcat, v1.0\n");
80 exit(0);
81 break;
82 case OPT_HELP: /* --help */
83 printf("useful help messages go here\n");
84 exit(0);
85 break;
86 case OPT_SERVERS: /* --servers */
87 opt_servers= strdup(optarg);
88 break;
89 case '?':
90 /* getopt_long already printed an error message. */
91 exit(1);
92 default:
93 abort();
94 }
95 }
96 }