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