[PATCH 1/4] base/query_handler: Implement a simple help handler

robin.sonefors at op5.com robin.sonefors at op5.com
Tue Nov 27 17:41:49 CET 2012


From: Robin Sonefors <robin.sonefors at op5.com>

Because the query handlers are plentiful and nifty, they're something I
should be using, but because I'm bad at remembering things and tired of
reading the source to find out how things work, I need help.

The way I can think of to get help is to try to exercise peer pressure
on whoever writes the query handler, by telling users that there should
be a help text, and then giving them errors error messages when the
query handler doesn't have any help text.

The protocol I've invented for asking for help text is the "--help"
command to the query handler - a fairly universal convention in
unixland, and also unlikely to clash with something else.

Signed-off-by: Robin Sonefors <robin.sonefors at op5.com>
---
 base/query-handler.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/base/query-handler.c b/base/query-handler.c
index 8d3cfe3..f9c0604 100644
--- a/base/query-handler.c
+++ b/base/query-handler.c
@@ -257,6 +257,31 @@ void qh_deinit(const char *path)
 	unlink(path);
 }
 
+static int qh_help(int sd, char *buf, unsigned int len)
+{
+	struct query_handler *qh;
+	if (!strcmp(buf, "list")) {
+		int i = 0;
+		for (qh = qhandlers; qh; qh = qh->next_qh) {
+			nsock_printf(sd, "%s\n", qh->name);
+		}
+		nsock_printf(sd, 0);
+		return 0;
+	}
+	if ((qh = qh_find_handler(buf)) != FALSE) {
+		int res = qh->handler(sd, "--help", 6);
+		if (res > 200) {
+			nsock_printf_nul(sd, "The handler %s doesn't have any help yet.", buf);
+		}
+		return 0;
+	}
+
+	nsock_printf_nul(sd, "This is the help query handler.\n"
+		"Try \"#help list\" to see all registered handlers, or\n"
+		"\"#help <handler>\" to get help on a specific handler.");
+	return 0;
+}
+
 static int qh_core(int sd, char *buf, unsigned int len)
 {
 	char *space;
@@ -342,5 +367,8 @@ int qh_init(const char *path)
 	if(!qh_register_handler("core", 0, qh_core))
 		logit(NSLOG_INFO_MESSAGE, FALSE, "qh: core query handler registered\n");
 
+	if(!qh_register_handler("help", 0, qh_help))
+		logit(NSLOG_INFO_MESSAGE, FALSE, "qh: help query handler registered\n");
+
 	return 0;
 }
-- 
1.7.11.7


------------------------------------------------------------------------------
Monitor your physical, virtual and cloud infrastructure from a single
web console. Get in-depth insight into apps, servers, databases, vmware,
SAP, cloud infrastructure, etc. Download 30-day Free Trial.
Pricing starts from $795 for 25 servers or applications!
http://p.sf.net/sfu/zoho_dev2dev_nov




More information about the Developers mailing list