Rewrote server parsing routines.
INCLUDES = -I$(top_builddir)/include\r
LDADDS = ../lib/libmemcached.la\r
\r
-bin_PROGRAMS = memcat memcp\r
+bin_PROGRAMS = memcat memcp memstat\r
\r
-memcat_SOURCES = memcat.c\r
+noinst_includes= client_options.h utilities.h\r
+\r
+memcat_SOURCES = memcat.c utilities.c\r
memcat_LDADD = $(LDADDS)\r
\r
-memcp_SOURCES = memcp.c\r
+memcp_SOURCES = memcp.c utilities.c\r
memcp_LDADD = $(LDADDS)\r
+\r
+memstat_SOURCES = memstat.c utilities.c\r
+memstat_LDADD = $(LDADDS)\r
#include <unistd.h>\r
#include <getopt.h>\r
#include <memcached.h>\r
+\r
#include "client_options.h"\r
+#include "utilities.h"\r
+\r
+\r
+/* Prototypes */\r
+void options_parse(int argc, char *argv[]);\r
\r
static int opt_verbose;\r
static int opt_displayflag;\r
static char *opt_servers;\r
\r
-struct memcached_st *parse_opt_servers (struct memcached_st *m,\r
- char *opt_servers)\r
-{\r
- char *s, *hostname;\r
- unsigned int portnum;\r
- while (s = strsep(&opt_servers, ",")) {\r
- hostname = strsep(&s, ":");\r
- portnum = atoi(s);\r
- memcached_server_add(m, hostname, portnum);\r
- }\r
- return m;\r
-}\r
-\r
int main(int argc, char *argv[])\r
{\r
memcached_st *memc;\r
memcached_return rc;\r
unsigned int x;\r
\r
+ memc= memcached_init(NULL);\r
+\r
+ options_parse(argc, argv);\r
+\r
+ parse_opt_servers(memc, opt_servers);\r
+\r
+ for (x= 1; x < argc; x++)\r
+ {\r
+ string= memcached_get(memc, argv[x], strlen(argv[x]),\r
+ &string_length, &flags, &rc);\r
+ if (rc == MEMCACHED_SUCCESS) {\r
+ if (opt_displayflag) {\r
+ printf("%d\n", flags);\r
+ } else {\r
+ if (string) \r
+ {\r
+ printf("%.*s\n", string_length, string);\r
+ free(string);\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ memcached_deinit(memc);\r
+\r
+ return 0;\r
+};\r
+\r
+\r
+void options_parse(int argc, char *argv[])\r
+{\r
+ int option_index = 0;\r
+ int option_rv;\r
+\r
static struct option long_options[] =\r
{\r
{"version", no_argument, NULL, OPT_VERSION},\r
{"flag", no_argument, &opt_displayflag, OPT_FLAG},\r
{0, 0, 0, 0},\r
};\r
- int option_index = 0;\r
- int option_rv;\r
\r
while (1) \r
{\r
exit(0);\r
break;\r
case OPT_SERVERS: /* --servers */\r
- opt_servers = strdup(optarg);\r
+ opt_servers= strdup(optarg);\r
break;\r
case '?':\r
/* getopt_long already printed an error message. */\r
abort();\r
}\r
}\r
-\r
- memc = malloc(sizeof(struct memcached_st));\r
- memcached_init(memc);\r
- memc= parse_opt_servers(memc, opt_servers);\r
- memc= memcached_init(memc);\r
-\r
- for (x= 1; x < argc; x++)\r
- {\r
- string= memcached_get(memc, argv[x], strlen(argv[x]),\r
- &string_length, &flags, &rc);\r
- if (rc == MEMCACHED_SUCCESS) {\r
- if (opt_displayflag) {\r
- printf("%d\n", flags);\r
- } else {\r
- if (string) \r
- {\r
- printf("%.*s\n", string_length, string);\r
- free(string);\r
- }\r
- }\r
- }\r
- }\r
-\r
- memcached_deinit(memc);\r
-\r
- return 0;\r
-};\r
+}\r
--- /dev/null
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <getopt.h>
+
+#include <memcached.h>
+
+#include "client_options.h"
+#include "utilities.h"
+
+/* Prototypes */
+void options_parse(int argc, char *argv[]);
+
+static int opt_verbose;
+static int opt_displayflag;
+static char *opt_servers;
+
+int main(int argc, char *argv[])
+{
+ memcached_st *memc;
+
+ memc= memcached_init(NULL);
+ options_parse(argc, argv);
+
+ parse_opt_servers(memc, opt_servers);
+
+ memcached_deinit(memc);
+
+ return 0;
+}
+
+void options_parse(int argc, char *argv[])
+{
+ static struct option long_options[] =
+ {
+ {"version", no_argument, NULL, OPT_VERSION},
+ {"help", no_argument, NULL, OPT_HELP},
+ {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
+ {"debug", no_argument, &opt_verbose, OPT_DEBUG},
+ {"servers", required_argument, NULL, OPT_SERVERS},
+ {"flag", no_argument, &opt_displayflag, OPT_FLAG},
+ {0, 0, 0, 0},
+ };
+
+ int option_index = 0;
+ int option_rv;
+
+ while (1)
+ {
+ option_rv = getopt_long(argc, argv, "", long_options, &option_index);
+ if (option_rv == -1) break;
+ switch (option_rv) {
+ case 0:
+ break;
+ case OPT_VERSION: /* --version */
+ printf("memcache tools, memcat, v1.0\n");
+ exit(0);
+ break;
+ case OPT_HELP: /* --help */
+ printf("useful help messages go here\n");
+ exit(0);
+ break;
+ case OPT_SERVERS: /* --servers */
+ opt_servers = strdup(optarg);
+ break;
+ case '?':
+ /* getopt_long already printed an error message. */
+ exit(1);
+ default:
+ abort();
+ }
+ }
+}
--- /dev/null
+#include <memcached.h>
+
+void parse_opt_servers(memcached_st *memc,
+ char *opt_servers)
+{
+ char *string;
+ unsigned int port;
+ char *begin_ptr;
+
+ for (begin_ptr= opt_servers, string= index(opt_servers, ',');
+ begin_ptr[0];
+ begin_ptr= ++string, string= index(begin_ptr, ','))
+ {
+ char buffer[HUGE_STRING_LEN];
+ char *ptr;
+
+ memset(buffer, 0, HUGE_STRING_LEN);
+ if (string)
+ {
+ memcpy(buffer, begin_ptr, string - begin_ptr);
+ }
+ else
+ {
+ size_t length= strlen(opt_servers);
+ memcpy(buffer, begin_ptr, length);
+ string= opt_servers+length;
+ }
+
+ ptr= index(buffer, ':');
+
+ if (ptr)
+ {
+ ptr[0]= 0;
+
+ ptr++;
+
+ port= strtol(ptr, (char **)NULL, 10);
+
+ memcached_server_add(memc, buffer, port);
+ }
+ else
+ {
+ memcached_server_add(memc, buffer, 0);
+ }
+ }
+}
+
--- /dev/null
+#include <memcached.h>
+
+void parse_opt_servers (memcached_st *ptr,
+ char *opt_servers);