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

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


From: Ferry Huberts <(spam-protected)>

Signed-off-by: Ferry Huberts <(spam-protected)>
---
 lib/info/info_types.h              |  3 ++
 lib/txtinfo/src/olsrd_txtinfo.c    | 41 +++++++++++++------------
 lib/txtinfo/src/txtinfo_printers.c | 62 ++++++++++++++++++++++++++++++++++++++
 lib/txtinfo/src/txtinfo_printers.h |  2 ++
 4 files changed, 89 insertions(+), 19 deletions(-)

diff --git a/lib/info/info_types.h b/lib/info/info_types.h
index c750c0e..cbf3141 100644
--- a/lib/info/info_types.h
+++ b/lib/info/info_types.h
@@ -58,6 +58,7 @@
 #define SIW_2HOP 0x0100
 #define SIW_SGW 0x0200
 #define SIW_RUNTIME_ALL (SIW_NEIGHBORS | SIW_LINKS | SIW_ROUTES | SIW_HNA | SIW_MID | SIW_TOPOLOGY | SIW_GATEWAYS | SIW_INTERFACES | SIW_2HOP | SIW_SGW)
+#define SIW_NEIGHBORS_FREIFUNK (SIW_NEIGHBORS | SIW_LINKS) /* special */
 
 /* these only change at olsrd startup */
 #define SIW_VERSION 0x0400
@@ -72,12 +73,14 @@
 #define SIW_OLSRD_CONF 0x2000
 
 typedef void (*init_plugin)(const char *plugin_name);
+typedef bool (*determine_command)(const char *str, unsigned int siw);
 typedef const char * (*mime_type)(unsigned int send_what);
 typedef void (*printer_neighbors)(struct autobuf *abuf, bool list_2hop);
 typedef void (*printer_generic)(struct autobuf *abuf);
 
 typedef struct {
     init_plugin init;
+    determine_command is_command;
     mime_type determine_mime_type;
     printer_neighbors neighbors;
     printer_generic links;
diff --git a/lib/txtinfo/src/olsrd_txtinfo.c b/lib/txtinfo/src/olsrd_txtinfo.c
index a83d02a..be7b467 100644
--- a/lib/txtinfo/src/olsrd_txtinfo.c
+++ b/lib/txtinfo/src/olsrd_txtinfo.c
@@ -87,6 +87,7 @@ static struct timer_entry *writetimer_entry;
 static printer_functions_t printer_functions = { //
     //
         .init = NULL, //
+        .is_command = &isCommand, //
         .determine_mime_type = NULL, //
         .neighbors = &ipc_print_neighbors, //
         .links = &ipc_print_links, //
@@ -104,53 +105,55 @@ static printer_functions_t printer_functions = { //
     };
 
 static void determine_action(unsigned int *send_what, char *requ) {
-  if (strstr(requ, "/con"))
+  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, "/nei"))
+    if ((*printer_functions.is_command)(requ, SIW_NEIGHBORS))
       *send_what |= SIW_NEIGHBORS;
-    if (strstr(requ, "/lin"))
+    if ((*printer_functions.is_command)(requ, SIW_LINKS))
       *send_what |= SIW_LINKS;
-    if (strstr(requ, "/rou"))
+    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, "/top"))
+    if ((*printer_functions.is_command)(requ, SIW_TOPOLOGY))
       *send_what |= SIW_TOPOLOGY;
-    if (strstr(requ, "/gat"))
+    if ((*printer_functions.is_command)(requ, SIW_GATEWAYS))
       *send_what |= SIW_GATEWAYS;
-    if (strstr(requ, "/int"))
+    if ((*printer_functions.is_command)(requ, SIW_INTERFACES))
       *send_what |= SIW_INTERFACES;
-    if (strstr(requ, "/2ho"))
+    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, "/ver"))
+    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;
   }
 }
 
diff --git a/lib/txtinfo/src/txtinfo_printers.c b/lib/txtinfo/src/txtinfo_printers.c
index 503f8a7..11da922 100644
--- a/lib/txtinfo/src/txtinfo_printers.c
+++ b/lib/txtinfo/src/txtinfo_printers.c
@@ -51,6 +51,68 @@
 #include "routing_table.h"
 #include "lq_plugin.h"
 #include "gateway.h"
+#include "../../info/info_types.h"
+
+bool isCommand(const char *str, unsigned int siw) {
+  switch (siw) {
+    case SIW_OLSRD_CONF:
+      return strstr(str, "/con");
+
+    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, "/nei");
+
+    case SIW_LINKS:
+      return strstr(str, "/lin");
+
+    case SIW_ROUTES:
+      return strstr(str, "/rou");
+
+    case SIW_HNA:
+      return strstr(str, "/hna");
+
+    case SIW_MID:
+      return strstr(str, "/mid");
+
+    case SIW_TOPOLOGY:
+      return strstr(str, "/top");
+
+    case SIW_GATEWAYS:
+      return strstr(str, "/gat");
+
+    case SIW_INTERFACES:
+      return strstr(str, "/int");
+
+    case SIW_2HOP:
+      return strstr(str, "/2ho");
+
+    case SIW_SGW:
+      return strstr(str, "/sgw");
+
+    case SIW_VERSION:
+      return strstr(str, "/ver");
+
+    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;
+  }
+}
 
 void ipc_print_neighbors(struct autobuf *abuf, bool list_2hop) {
   struct ipaddr_str buf1;
diff --git a/lib/txtinfo/src/txtinfo_printers.h b/lib/txtinfo/src/txtinfo_printers.h
index 3fcd86c..d0a0de9 100644
--- a/lib/txtinfo/src/txtinfo_printers.h
+++ b/lib/txtinfo/src/txtinfo_printers.h
@@ -46,6 +46,8 @@
 
 #include "common/autobuf.h"
 
+bool isCommand(const char *str, unsigned int siw);
+
 void ipc_print_neighbors(struct autobuf *abuf, bool list_2hop);
 void ipc_print_links(struct autobuf *abuf);
 void ipc_print_routes(struct autobuf *abuf);
-- 
2.5.0




More information about the Olsr-dev mailing list