From 9792a431e8fc98323818e6fb15004553e7ef37bb Mon Sep 17 00:00:00 2001
From: Ralf Habacker <ralf.habacker@freenet.de>
Date: Mon, 3 Jan 2022 16:07:59 +0100
Subject: [PATCH 1/3] Add GWEN_Funcs_xxx functions

These functions provide a common way to add tests and commands
for command line applications.

Since available commands/tests have to be included in the
corresponding structure, it is guaranteed that they are
also available in the help and one can immediately see
if descriptions are still missing.
---
 src/base/Makefile.am |   2 +
 src/base/funcs.c     |  77 +++++++++++++++++++++++++++++++
 src/base/funcs.h     | 107 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 186 insertions(+)
 create mode 100644 src/base/funcs.c
 create mode 100644 src/base/funcs.h

diff --git a/src/base/Makefile.am b/src/base/Makefile.am
index 981ed5c6..0fb31b95 100644
--- a/src/base/Makefile.am
+++ b/src/base/Makefile.am
@@ -70,6 +70,7 @@ iheader_HEADERS=\
  buffer.h \
  debug.h \
  error.h \
+ funcs.h \
  gwensignal.h \
  i18n.h \
  idlist64.h \
@@ -108,6 +109,7 @@ libbase_la_SOURCES=\
  buffer.c \
  debug.c \
  error.c \
+ funcs.c \
  gwensignal.c \
  i18n.c \
  idlist64.c \
diff --git a/src/base/funcs.c b/src/base/funcs.c
new file mode 100644
index 00000000..8b3688c1
--- /dev/null
+++ b/src/base/funcs.c
@@ -0,0 +1,77 @@
+/***************************************************************************
+ $RCSfile$
+                             -------------------
+    cvs         : $Id$
+    begin       : Wed Feb 15 2022
+    copyright   : (C) 2022 by Ralf Habacker
+    email       : ralf.habacker@freenet.de
+
+ ***************************************************************************
+ *                                                                         *
+ *   This library is free software; you can redistribute it and/or         *
+ *   modify it under the terms of the GNU Lesser General Public            *
+ *   License as published by the Free Software Foundation; either          *
+ *   version 2.1 of the License, or (at your option) any later version.    *
+ *                                                                         *
+ *   This library is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
+ *   Lesser General Public License for more details.                       *
+ *                                                                         *
+ *   You should have received a copy of the GNU Lesser General Public      *
+ *   License along with this library; if not, write to the Free Software   *
+ *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *
+ *   MA  02111-1307  USA                                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+typedef struct GWEN_DB_NODE GWEN_DB_NODE;
+
+#include "funcs.h"
+
+#include <stdio.h>
+
+/**
+ * This function prints out a space separated list of all defined functions
+ */
+void GWEN_Funcs_Usage(const GWEN_FUNCS *funcs)
+{
+    GWEN_FUNCS *p;
+
+    for(p = funcs; p->name; p++) {
+      fprintf(stderr, " %s", p->name);
+      if (p->description)
+        fprintf(stderr, " (%s)", p->description);
+    }
+    fprintf(stderr, "\n");
+}
+
+/**
+ * This function prints out list of all defined functions including the description
+ */
+void GWEN_Funcs_Usage_With_Help(const GWEN_FUNCS *funcs)
+{
+    GWEN_FUNCS *p;
+
+    for(p = funcs; p->name; p++) {
+      fprintf(stderr, "  %s:\n\n    %s\n\n", p->name, p->description ? p->description : "");
+    }
+}
+
+/**
+ * This function returns a pointer to the function identified by \p name
+ * or NULL if function has not been found.
+ */
+const GWEN_FUNCS* GWEN_Funcs_Find(const GWEN_FUNCS *funcs, const char *name)
+{
+  GWEN_FUNCS *p;
+
+  for(p = funcs; p->name; p++) {
+    if (strcasecmp(name, p->name)==0)
+      return p;
+  }
+  return NULL;
+}
+
+
+
diff --git a/src/base/funcs.h b/src/base/funcs.h
new file mode 100644
index 00000000..bb7a0569
--- /dev/null
+++ b/src/base/funcs.h
@@ -0,0 +1,107 @@
+/***************************************************************************
+ $RCSfile$
+                             -------------------
+    cvs         : $Id$
+    begin       : Wed Feb 15 2022
+    copyright   : (C) 2022 by Ralf Habacker
+    email       : ralf.habacker@freenet.de
+
+ ***************************************************************************
+ *                                                                         *
+ *   This library is free software; you can redistribute it and/or         *
+ *   modify it under the terms of the GNU Lesser General Public            *
+ *   License as published by the Free Software Foundation; either          *
+ *   version 2.1 of the License, or (at your option) any later version.    *
+ *                                                                         *
+ *   This library is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
+ *   Lesser General Public License for more details.                       *
+ *                                                                         *
+ *   You should have received a copy of the GNU Lesser General Public      *
+ *   License along with this library; if not, write to the Free Software   *
+ *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *
+ *   MA  02111-1307  USA                                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#ifndef GWENHYWFAR_Funcs_H
+#define GWENHYWFAR_Funcs_H
+
+#include <gwenhywfar/gwenhywfarapi.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+/**
+ * This is one of the very few structs inside Gwenhywfar whose
+ * contents are available for direct access to the the program.
+ * Developer's note: Please note that any change within this struct will
+ * make it necessary to increment the SO_VERSION of the library !
+ */
+typedef struct {
+  const char *name;
+  int (*func1)(void);
+  int (*func2)(int, char**);
+  int (*func3)(GWEN_DB_NODE*, int, char**);
+  const char *description;
+} GWEN_FUNCS;
+
+/* Defines a command without parameters but with description  */
+#define GWEN_Funcs_Entry_Help(a,b,c) { a, b, NULL, NULL, c }
+
+/* Defines a command with argc/argv parameter and description */
+#define GWEN_Funcs_Entry_Args_Help(a,b,c) { a, 0, b, NULL, c }
+
+/* Defines a command with DB_NODE type and argc/argv parameter and description */
+#define GWEN_Funcs_Entry_DB_NODE_Args_Help(a,b,c) { a, NULL, NULL, b, c }
+
+/* Defines the end of the command table */
+#define GWEN_Funcs_Entry_End() { NULL, NULL, NULL, NULL, NULL }
+
+/* Defines one of the above commands without description */
+#define GWEN_Funcs_Entry(a,b) { a, b, NULL, NULL, NULL }
+#define GWEN_Funcs_Entry_Args(a,b) { a, 0, b, NULL, NULL }
+#define GWEN_Funcs_Entry_DB_NODE_Args(a,b) { a, NULL, NULL, b, NULL }
+
+/* Checks if a command variant exists */
+#define GWEN_Funcs_Has_Call(func) (func)->func1
+#define GWEN_Funcs_Has_Call_Args(func) (func)->func2
+#define GWEN_Funcs_Has_Call_DB_NODE_Args(func) (func)->func3
+
+/* Functions to call a specified command */
+#define GWEN_Funcs_Call(func) (func->func1)()
+#define GWEN_Funcs_Call_Args(func,a,b) (func->func2)(a,b)
+#define GWEN_Funcs_Call_DB_NODE_Args(func,a,b,c) (func->func3)(a,b,c)
+
+/**
+ * This function prints out a space separated list of all defined functions
+ * without description.
+ */
+GWENHYWFAR_API
+void GWEN_Funcs_Usage(const GWEN_FUNCS* funcs);
+
+/**
+ * This function outputs a list of all defined functions including the description
+ */
+GWENHYWFAR_API
+void GWEN_Funcs_Usage_With_Help(const GWEN_FUNCS* funcs);
+
+/**
+ * This function returns a pointer to the function described by \p name
+ * or NULL if the function was not found.
+ */
+GWENHYWFAR_API
+const GWEN_FUNCS* GWEN_Funcs_Find(const GWEN_FUNCS* funcs, const char *name);
+
+#ifdef __cplusplus
+}
+#endif
+
+
+#endif /* GWENHYWFAR_Funcs_H */
+
+
+
-- 
2.26.2

