All *_init() have been changed to _create()
[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 return 0;
33
34 memc= memcached_create(NULL);
35
36 servers= parse_opt_servers(opt_servers);
37 memcached_server_push(memc, servers);
38 memcached_server_list_free(servers);
39
40 while (optind < argc)
41 {
42 string= memcached_get(memc, argv[optind], strlen(argv[optind]),
43 &string_length, &flags, &rc);
44 if (rc == MEMCACHED_SUCCESS)
45 {
46 if (opt_displayflag)
47 {
48 if (opt_verbose)
49 printf("key: %s\nflags: ", argv[optind]);
50 printf("%x\n", flags);
51 }
52 else
53 {
54 if (opt_verbose)
55 printf("key: %s\nflags: %x\nlength: %zu\nvalue: ",
56 argv[optind], flags, string_length);
57 printf("%.*s\n", (int)string_length, string);
58 free(string);
59 }
60 }
61 else
62 {
63 fprintf(stderr, "memcat: %s: memcache error %s\n",
64 argv[optind], memcached_strerror(memc, rc));
65 }
66 optind++;
67 }
68
69 memcached_free(memc);
70
71 free(opt_servers);
72
73 return 0;
74 }
75
76
77 void options_parse(int argc, char *argv[])
78 {
79 int option_index= 0;
80 int option_rv;
81
82 memcached_programs_help_st help_options[]=
83 {
84 {0},
85 };
86
87 static struct option long_options[]=
88 {
89 {"version", no_argument, NULL, OPT_VERSION},
90 {"help", no_argument, NULL, OPT_HELP},
91 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
92 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
93 {"servers", required_argument, NULL, OPT_SERVERS},
94 {"flag", no_argument, &opt_displayflag, OPT_FLAG},
95 {0, 0, 0, 0},
96 };
97
98 while (1)
99 {
100 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
101 if (option_rv == -1) break;
102 switch (option_rv)
103 {
104 case 0:
105 break;
106 case OPT_VERBOSE: /* --verbose or -v */
107 opt_verbose = OPT_VERBOSE;
108 break;
109 case OPT_DEBUG: /* --debug or -d */
110 opt_verbose = OPT_DEBUG;
111 break;
112 case OPT_VERSION: /* --version or -V */
113 version_command(PROGRAM_NAME);
114 break;
115 case OPT_HELP: /* --help or -h */
116 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
117 break;
118 case OPT_SERVERS: /* --servers or -s */
119 opt_servers= strdup(optarg);
120 break;
121 case '?':
122 /* getopt_long already printed an error message. */
123 exit(1);
124 default:
125 abort();
126 }
127 }
128 }