7789ec9b4e0f72f813257c0320c441ba6adee291
[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
25 memc= memcached_init(NULL);
26
27 options_parse(argc, argv);
28
29 parse_opt_servers(memc, opt_servers);
30
31 while (optind <= argc)
32 {
33 string= memcached_get(memc, argv[argc], strlen(argv[argc]),
34 &string_length, &flags, &rc);
35 if (rc == MEMCACHED_SUCCESS)
36 {
37 if (opt_displayflag)
38 {
39 if (opt_verbose)
40 printf("key: %.*s\nflags: ", argv[argc]);
41 printf("%d\n", flags);
42 }
43 else
44 {
45 if (opt_verbose)
46 printf("key: %.*s\nflags: %d\nlength: %d\nvalue: ",
47 argv[argc], flags, string_length);
48 printf("%.*s\n", string_length, string);
49 free(string);
50 }
51 }
52 optind++;
53 }
54
55 memcached_deinit(memc);
56
57 return 0;
58 };
59
60
61 void options_parse(int argc, char *argv[])
62 {
63 int option_index = 0;
64 int option_rv;
65
66 static struct option long_options[] =
67 {
68 {"version", no_argument, NULL, OPT_VERSION},
69 {"help", no_argument, NULL, OPT_HELP},
70 {"verbose", no_argument, &opt_verbose, 1},
71 {"debug", no_argument, &opt_verbose, 2},
72 {"servers", required_argument, NULL, OPT_SERVERS},
73 {"flag", no_argument, &opt_displayflag, OPT_FLAG},
74 {0, 0, 0, 0},
75 };
76
77 while (1)
78 {
79 option_rv = getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
80 if (option_rv == -1) break;
81 switch (option_rv) {
82 case 0:
83 break;
84 case OPT_VERSION: /* --version or -V */
85 printf("memcache tools, memcat, v1.0\n");
86 exit(0);
87 break;
88 case OPT_HELP: /* --help or -h */
89 printf("useful help messages go here\n");
90 exit(0);
91 break;
92 case OPT_SERVERS: /* --servers or -s */
93 opt_servers= optarg;
94 break;
95 case '?':
96 /* getopt_long already printed an error message. */
97 exit(1);
98 default:
99 abort();
100 }
101 }
102 }