134017fa4fa54d8c76973d78b229ed76f3314592
[awesomized/libmemcached] / src / memcat.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <getopt.h>
4 #include <memcached.h>
5
6 static int opt_verbose;
7 static int opt_displayflag;
8 static char *opt_servers;
9
10 int main(int argc, char *argv[])
11 {
12 memcached_st *memc;
13 char *string;
14 size_t string_length;
15 uint16_t flags;
16 memcached_return rc;
17
18 static struct option long_options[] =
19 {
20 {"version", no_argument, NULL, 257},
21 {"help", no_argument, NULL, 258},
22 {"verbose", no_argument, &opt_verbose, 1},
23 {"debug", no_argument, &opt_verbose, 2},
24 {"servers", required_argument, NULL, 259},
25 {"flag", no_argument, &opt_displayflag, 1},
26 {0, 0, 0, 0},
27 };
28 int option_index = 0;
29 int option_rv;
30
31 while (1) {
32 option_rv = getopt_long(argc, argv, "", long_options, &option_index);
33 if (option_rv == -1) break;
34 switch (option_rv) {
35 case 0:
36 break;
37 case 257: /* --version */
38 printf("memcache tools, memcat, v1.0\n");
39 exit(0);
40 break;
41 case 258: /* --help */
42 printf("useful help messages go here\n");
43 exit(0);
44 break;
45 case 259: /* --servers */
46 opt_servers = strdup(optarg);
47 break;
48 case '?':
49 /* getopt_long already printed an error message. */
50 exit(1);
51 default:
52 abort();
53 }
54 }
55
56 /* todo, turn opt_servers into something to pass to memcached_init */
57 memc= memcached_init(NULL);
58
59 while (optind < argc) {
60 string= memcached_get(memc, argv[optind], strlen(argv[optind]),
61 &string_length, &flags, &rc);
62 if (string) {
63 printf("%.*s\n", string_length, string);
64 free(string);
65 }
66 optind++;
67 }
68
69 memcached_deinit(memc);
70
71 return 0;
72 };