1c9082f55a886a101f60f7517e19de37e0571786
[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 #define PROGRAM_NAME "memcat"
10 #define PROGRAM_DESCRIPTION "Cat a set of key values to stdout."
11
12
13 /* Prototypes */
14 void options_parse(int argc, char *argv[]);
15
16 static int opt_verbose= 0;
17 static int opt_displayflag= 0;
18 static char *opt_servers;
19
20 int main(int argc, char *argv[])
21 {
22 memcached_st *memc;
23 char *string;
24 size_t string_length;
25 uint16_t flags;
26 memcached_return rc;
27 memcached_server_st *servers;
28
29 options_parse(argc, argv);
30
31 if (!opt_servers)
32 {
33 char *temp;
34
35 if ((temp= getenv("MEMCACHED_SERVERS")))
36 opt_servers= strdup(temp);
37 else
38 exit(1);
39 }
40
41 memc= memcached_create(NULL);
42
43 servers= memcached_servers_parse(opt_servers);
44
45 memcached_server_push(memc, servers);
46 memcached_server_list_free(servers);
47
48 while (optind < argc)
49 {
50 string= memcached_get(memc, argv[optind], strlen(argv[optind]),
51 &string_length, &flags, &rc);
52 if (rc == MEMCACHED_SUCCESS)
53 {
54 if (opt_displayflag)
55 {
56 if (opt_verbose)
57 printf("key: %s\nflags: ", argv[optind]);
58 printf("%x\n", flags);
59 }
60 else
61 {
62 if (opt_verbose)
63 printf("key: %s\nflags: %x\nlength: %zu\nvalue: ",
64 argv[optind], flags, string_length);
65 printf("%.*s\n", (int)string_length, string);
66 free(string);
67 }
68 }
69 else if (rc != MEMCACHED_NOTFOUND)
70 {
71 fprintf(stderr, "memcat: %s: memcache error %s\n",
72 argv[optind], memcached_strerror(memc, rc));
73 }
74 optind++;
75 }
76
77 memcached_free(memc);
78
79 free(opt_servers);
80
81 return 0;
82 }
83
84
85 void options_parse(int argc, char *argv[])
86 {
87 int option_index= 0;
88 int option_rv;
89
90 memcached_programs_help_st help_options[]=
91 {
92 {0},
93 };
94
95 static struct option long_options[]=
96 {
97 {"version", no_argument, NULL, OPT_VERSION},
98 {"help", no_argument, NULL, OPT_HELP},
99 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
100 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
101 {"servers", required_argument, NULL, OPT_SERVERS},
102 {"flag", no_argument, &opt_displayflag, OPT_FLAG},
103 {0, 0, 0, 0},
104 };
105
106 while (1)
107 {
108 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
109 if (option_rv == -1) break;
110 switch (option_rv)
111 {
112 case 0:
113 break;
114 case OPT_VERBOSE: /* --verbose or -v */
115 opt_verbose = OPT_VERBOSE;
116 break;
117 case OPT_DEBUG: /* --debug or -d */
118 opt_verbose = OPT_DEBUG;
119 break;
120 case OPT_VERSION: /* --version or -V */
121 version_command(PROGRAM_NAME);
122 break;
123 case OPT_HELP: /* --help or -h */
124 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
125 break;
126 case OPT_SERVERS: /* --servers or -s */
127 opt_servers= strdup(optarg);
128 break;
129 case '?':
130 /* getopt_long already printed an error message. */
131 exit(1);
132 default:
133 abort();
134 }
135 }
136 }