[Olsr-dev] [PATCH v1 035/115] jsoninfo: put command parsing in the functions table

Ferry Huberts (spam-protected)
Wed Dec 2 21:28:08 CET 2015


From: Ferry Huberts <(spam-protected)>

Signed-off-by: Ferry Huberts <(spam-protected)>
---
 lib/jsoninfo/src/jsoninfo_printers.c | 61 ++++++++++++++++++++++++++++++++++++
 lib/jsoninfo/src/jsoninfo_printers.h |  2 ++
 lib/jsoninfo/src/olsrd_jsoninfo.c    | 41 +++++++++++++-----------
 3 files changed, 85 insertions(+), 19 deletions(-)

diff --git a/lib/jsoninfo/src/jsoninfo_printers.c b/lib/jsoninfo/src/jsoninfo_printers.c
index f0bd1cc..d8f0953 100644
--- a/lib/jsoninfo/src/jsoninfo_printers.c
+++ b/lib/jsoninfo/src/jsoninfo_printers.c
@@ -70,6 +70,67 @@ void plugin_init(const char *plugin_name) {
   read_uuid_from_file(plugin_name, uuidfile);
 }
 
+bool isCommand(const char *str, unsigned int siw) {
+  switch (siw) {
+    case SIW_OLSRD_CONF:
+      return strstr(str, "/olsrd.conf");
+
+    case SIW_ALL:
+      return strstr(str, "/all");
+
+    case SIW_RUNTIME_ALL:
+      return strstr(str, "/runtime");
+
+    case SIW_STARTUP_ALL:
+      return strstr(str, "/startup");
+
+    case SIW_NEIGHBORS:
+      return strstr(str, "/neighbors");
+
+    case SIW_LINKS:
+      return strstr(str, "/links");
+
+    case SIW_ROUTES:
+      return strstr(str, "/routes");
+
+    case SIW_HNA:
+      return strstr(str, "/hna");
+
+    case SIW_MID:
+      return strstr(str, "/mid");
+
+    case SIW_TOPOLOGY:
+      return strstr(str, "/topology");
+
+    case SIW_GATEWAYS:
+      return strstr(str, "/gateways");
+
+    case SIW_INTERFACES:
+      return strstr(str, "/interfaces");
+
+    case SIW_2HOP:
+      return strstr(str, "/2hop");
+
+    case SIW_SGW:
+      return strstr(str, "/sgw");
+
+    case SIW_VERSION:
+      return strstr(str, "/version");
+
+    case SIW_CONFIG:
+      return strstr(str, "/config");
+
+    case SIW_PLUGINS:
+      return strstr(str, "/plugins");
+
+    case SIW_NEIGHBORS_FREIFUNK:
+      return strstr(str, "/neighbours");
+
+    default:
+      return false;
+  }
+}
+
 const char * determine_mime_type(unsigned int send_what) {
   return (send_what & SIW_ALL) ? "application/json; charset=utf-8" : "text/plain; charset=utf-8";
 }
diff --git a/lib/jsoninfo/src/jsoninfo_printers.h b/lib/jsoninfo/src/jsoninfo_printers.h
index cf83a97..df0b974 100644
--- a/lib/jsoninfo/src/jsoninfo_printers.h
+++ b/lib/jsoninfo/src/jsoninfo_printers.h
@@ -51,6 +51,8 @@ extern struct timeval start_time;
 
 void plugin_init(const char * plugin_name);
 
+bool isCommand(const char *str, unsigned int siw);
+
 const char * determine_mime_type(unsigned int send_what);
 
 void ipc_print_neighbors(struct autobuf *abuf, bool list_2hop);
diff --git a/lib/jsoninfo/src/olsrd_jsoninfo.c b/lib/jsoninfo/src/olsrd_jsoninfo.c
index 571b0a2..028366b 100644
--- a/lib/jsoninfo/src/olsrd_jsoninfo.c
+++ b/lib/jsoninfo/src/olsrd_jsoninfo.c
@@ -88,6 +88,7 @@ static struct timer_entry *writetimer_entry;
 static printer_functions_t printer_functions = { //
     //
         .init = &plugin_init, //
+        .is_command = &isCommand, //
         .determine_mime_type = &determine_mime_type, //
         .neighbors = &ipc_print_neighbors, //
         .links = &ipc_print_links, //
@@ -105,53 +106,55 @@ static printer_functions_t printer_functions = { //
     };
 
 static void determine_action(unsigned int *send_what, char *requ) {
-  if (strstr(requ, "/olsrd.conf"))
+  if (!printer_functions.is_command)
+    *send_what = 0;
+  else if ((*printer_functions.is_command)(requ, SIW_OLSRD_CONF))
     *send_what |= SIW_OLSRD_CONF;
-  else if (strstr(requ, "/all"))
+  else if ((*printer_functions.is_command)(requ, SIW_ALL))
     *send_what = SIW_ALL;
   else {
     // these are the two overarching categories
-    if (strstr(requ, "/runtime"))
+    if ((*printer_functions.is_command)(requ, SIW_RUNTIME_ALL))
       *send_what |= SIW_RUNTIME_ALL;
-    if (strstr(requ, "/startup"))
+    if ((*printer_functions.is_command)(requ, SIW_STARTUP_ALL))
       *send_what |= SIW_STARTUP_ALL;
 
     // these are the individual sections
-    if (strstr(requ, "/neighbors"))
+    if ((*printer_functions.is_command)(requ, SIW_NEIGHBORS))
       *send_what |= SIW_NEIGHBORS;
-    if (strstr(requ, "/links"))
+    if ((*printer_functions.is_command)(requ, SIW_LINKS))
       *send_what |= SIW_LINKS;
-    if (strstr(requ, "/routes"))
+    if ((*printer_functions.is_command)(requ, SIW_ROUTES))
       *send_what |= SIW_ROUTES;
-    if (strstr(requ, "/hna"))
+    if ((*printer_functions.is_command)(requ, SIW_HNA))
       *send_what |= SIW_HNA;
-    if (strstr(requ, "/mid"))
+    if ((*printer_functions.is_command)(requ, SIW_MID))
       *send_what |= SIW_MID;
-    if (strstr(requ, "/topology"))
+    if ((*printer_functions.is_command)(requ, SIW_TOPOLOGY))
       *send_what |= SIW_TOPOLOGY;
-    if (strstr(requ, "/gateways"))
+    if ((*printer_functions.is_command)(requ, SIW_GATEWAYS))
       *send_what |= SIW_GATEWAYS;
-    if (strstr(requ, "/interfaces"))
+    if ((*printer_functions.is_command)(requ, SIW_INTERFACES))
       *send_what |= SIW_INTERFACES;
-    if (strstr(requ, "/2hop"))
+    if ((*printer_functions.is_command)(requ, SIW_2HOP))
       *send_what |= SIW_2HOP;
-    if (strstr(requ, "/sgw"))
+    if ((*printer_functions.is_command)(requ, SIW_SGW))
       *send_what |= SIW_SGW;
 
     // specials
-    if (strstr(requ, "/version"))
+    if ((*printer_functions.is_command)(requ, SIW_VERSION))
       *send_what |= SIW_VERSION;
-    if (strstr(requ, "/config"))
+    if ((*printer_functions.is_command)(requ, SIW_CONFIG))
       *send_what |= SIW_CONFIG;
-    if (strstr(requ, "/plugins"))
+    if ((*printer_functions.is_command)(requ, SIW_PLUGINS))
       *send_what |= SIW_PLUGINS;
 
     /* To print out neighbours only on the Freifunk Status
      * page the normal output is somewhat lengthy. The
      * header parsing is sufficient for standard wget.
      */
-    if (strstr(requ, "/neighbours"))
-      *send_what = SIW_NEIGHBORS | SIW_LINKS;
+    if ((*printer_functions.is_command)(requ, SIW_NEIGHBORS_FREIFUNK))
+      *send_what = SIW_NEIGHBORS_FREIFUNK;
   }
 }
 
-- 
2.5.0




More information about the Olsr-dev mailing list