From 6da248cf83efa8f63d2f61d6d275129d3d80c8af Mon Sep 17 00:00:00 2001
From: Ralf Habacker <ralf.habacker@freenet.de>
Date: Mon, 3 Jan 2022 16:20:09 +0100
Subject: [PATCH 3/3] Refactor gcttool to use GWEN_Funcs_xxx functions

---
 po/de.po                  | 62 ++++++++----------------------
 tools/gcttool/Makefile.am |  5 ++-
 tools/gcttool/main.c      | 81 +++++++++++----------------------------
 3 files changed, 41 insertions(+), 107 deletions(-)

diff --git a/po/de.po b/po/de.po
index 78049eae..6ea7bb5e 100644
--- a/po/de.po
+++ b/po/de.po
@@ -1133,7 +1133,7 @@ msgstr ""
 "  Dieser Befehl erzeugt Quelldateien für die angegebene Datei\n"
 "\n"
 
-#: tools/gcttool/main.c:89
+#: tools/gcttool/main.c:90
 msgid ""
 "The plugin could not be found.\n"
 "Please make sure that you have the following packages installed (at least on "
@@ -1147,56 +1147,26 @@ msgstr ""
 "- libchipcard-plugins-libgwenhywfar (for chipcards)\n"
 "- libaqbanking-plugins-libgwenhywfar\n"
 
-#: tools/gcttool/main.c:215
-msgid ""
-"  create:\n"
-"    This command creates a crypt token\n"
-"\n"
-msgstr ""
-"  create:\n"
-"    Dieser Befehl erzeugt ein CryptToken\n"
-"\n"
+#: tools/gcttool/main.c:153
+msgid "This command creates a crypt token"
+msgstr "Dieser Befehl erzeugt ein CryptToken"
 
-#: tools/gcttool/main.c:219
-msgid ""
-"  showuser:\n"
-"    Display user data stored on the token\n"
-"\n"
-msgstr ""
-"  showuser:\n"
-"    Zeige auf dem CryptToken gespeicherte Benutzer an\n"
-"\n"
+#: tools/gcttool/main.c:154
+msgid "Display user data stored on the token"
+msgstr " Zeige auf dem CryptToken gespeicherte Benutzer an"
 
-#: tools/gcttool/main.c:223
-msgid ""
-"  showkey:\n"
-"    Display key info stored on the token\n"
-"\n"
-msgstr ""
-"  showkey:\n"
-"    Zeige auf dem CryptToken gespeicherte Schlüsselinformationen an\n"
-"\n"
+#: tools/gcttool/main.c:155
+msgid "Display key info stored on the token"
+msgstr "Zeige auf dem CryptToken gespeicherte Schlüsselinformationen an"
 
-#: tools/gcttool/main.c:227
-msgid ""
-"  update:\n"
-"    Update Crypt Token to newer version (e.g. OpenHBCI keyfiles)\n"
-"\n"
-msgstr ""
-"  update:\n"
-"    Aktualisiert Sicherheitsmedien (z.B. OpenHBCI Schlüsseldateien)\n"
-"\n"
+#: tools/gcttool/main.c:158
+msgid "Update Crypt Token to newer version (e.g. OpenHBCI key files"
+msgstr "Aktualisiert Sicherheitsmedien (z.B. OpenHBCI Schlüsseldateien)"
 
-#: tools/gcttool/main.c:231
-msgid ""
-"  showpasswords:\n"
-"    Display passwords store in a GWEN_PASSWD_STORE file\n"
-"\n"
+#: tools/gcttool/main.c:164
+msgid "Display passwords store in a GWEN_PASSWD_STORE file"
 msgstr ""
-"  showpasswords:\n"
-"    Zeige die Passwörter, die in einer GWEN_PASSWD_STORE Datei gespeichert "
-"sind\n"
-"\n"
+"Zeige die Passwörter, die in einer GWEN_PASSWD_STORE Datei gespeichert sind"
 
 #: tools/gsa/main.c:132
 msgid "GWEN's Simple Archiver"
diff --git a/tools/gcttool/Makefile.am b/tools/gcttool/Makefile.am
index c179b751..c2ed48fd 100644
--- a/tools/gcttool/Makefile.am
+++ b/tools/gcttool/Makefile.am
@@ -1,4 +1,5 @@
-AM_CPPFLAGS= -I$(top_builddir) -I$(top_srcdir) -I$(gwenhywfar_symlinkdir)
+AM_CPPFLAGS= -I$(top_builddir) -I$(top_srcdir) -I$(gwenhywfar_symlinkdir) -I$(top_srcdir)/src/base
+
 if IF_HAVE_OPENSSL
 AM_CPPFLAGS+= $(OPENSSL_CFLAGS)
 endif
@@ -25,7 +26,7 @@ gct_tool_SOURCES=\
   showpasswords.c \
   main.c
 
-gct_tool_LDADD=$(top_builddir)/src/$(gwenhywfar_internal_libname)
+gct_tool_LDADD=$(top_builddir)/src/$(gwenhywfar_internal_libname) $(top_builddir)/src/base/libbase.la
 
 if IS_WINDOWS
 
diff --git a/tools/gcttool/main.c b/tools/gcttool/main.c
index 321e1ba6..0023f8f7 100644
--- a/tools/gcttool/main.c
+++ b/tools/gcttool/main.c
@@ -18,6 +18,7 @@
 #include <gwenhywfar/pathmanager.h>
 #include <gwenhywfar/cgui.h>
 
+#include "funcs.h"
 #include "globals.h"
 
 #ifdef OS_WIN32
@@ -148,6 +149,22 @@ int main(int argc, char **argv)
       "Show this help screen"       /* long description */
     }
   };
+  const GWEN_FUNCS funcs[]= {
+    GWEN_Funcs_Entry_DB_NODE_Args_Help("create", createToken, I18N("This command creates a crypt token")),
+    GWEN_Funcs_Entry_DB_NODE_Args_Help("showuser", showUser, I18N("Display user data stored on the token")),
+    GWEN_Funcs_Entry_DB_NODE_Args_Help("showkey", showKey, I18N("Display key info stored on the token")),
+    GWEN_Funcs_Entry_DB_NODE_Args("genkey", genKey),
+    GWEN_Funcs_Entry_DB_NODE_Args("activateKey", activateKey),
+    GWEN_Funcs_Entry_DB_NODE_Args_Help("update", updateToken, I18N("Update Crypt Token to newer version (e.g. OpenHBCI key files")),
+    GWEN_Funcs_Entry_DB_NODE_Args("setsignseq", setSignSeq),
+    GWEN_Funcs_Entry_DB_NODE_Args("changepin", changePin),
+    GWEN_Funcs_Entry_DB_NODE_Args("setkey", setKey),
+    GWEN_Funcs_Entry_DB_NODE_Args("hashTree", hashTree),
+    GWEN_Funcs_Entry_DB_NODE_Args("checkTree", checkTree),
+    GWEN_Funcs_Entry_DB_NODE_Args_Help("showpasswords", showPasswords, I18N("Display passwords store in a GWEN_PASSWD_STORE file")),
+    GWEN_Funcs_Entry_End(),
+  };
+  const GWEN_FUNCS *func;
 
   err=GWEN_Init();
   if (err) {
@@ -209,30 +226,8 @@ int main(int argc, char **argv)
       fprintf(stderr, "ERROR: Could not create help string\n");
       return 1;
     }
-    GWEN_Buffer_AppendString(ubuf,
-                             I18N("\nCommands:\n\n"));
-    GWEN_Buffer_AppendString(ubuf,
-                             I18N("  create:\n"
-                                  "    This command creates a crypt token"
-                                  "\n\n"));
-    GWEN_Buffer_AppendString(ubuf,
-                             I18N("  showuser:\n"
-                                  "    Display user data stored on the "
-                                  "token\n\n"));
-    GWEN_Buffer_AppendString(ubuf,
-                             I18N("  showkey:\n"
-                                  "    Display key info stored on the "
-                                  "token\n\n"));
-    GWEN_Buffer_AppendString(ubuf,
-                             I18N("  update:\n"
-                                  "    Update Crypt Token to newer version (e.g. OpenHBCI key"
-                                  "files)\n\n"));
-    GWEN_Buffer_AppendString(ubuf,
-                             I18N("  showpasswords:\n"
-                                  "    Display passwords store in a GWEN_PASSWD_STORE file\n\n"));
-
-    fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
-    GWEN_Buffer_free(ubuf);
+    fprintf(stderr, "%s\n", I18N("\nCommands:\n\n"));
+    GWEN_Funcs_Usage_With_Help(funcs);
     return 0;
   }
   if (rv) {
@@ -246,41 +241,9 @@ int main(int argc, char **argv)
     return 1;
   }
 
-  if (strcasecmp(cmd, "create")==0) {
-    rv=createToken(db, argc, argv);
-  }
-  else if (strcasecmp(cmd, "showuser")==0) {
-    rv=showUser(db, argc, argv);
-  }
-  else if (strcasecmp(cmd, "showkey")==0) {
-    rv=showKey(db, argc, argv);
-  }
-  else if (strcasecmp(cmd, "genkey")==0) {
-    rv=genKey(db, argc, argv);
-  }
-  else if (strcasecmp(cmd, "activatekey")==0) {
-    rv=activateKey(db, argc, argv);
-  }
-  else if (strcasecmp(cmd, "update")==0) {
-    rv=updateToken(db, argc, argv);
-  }
-  else if (strcasecmp(cmd, "setsignseq")==0) {
-    rv=setSignSeq(db, argc, argv);
-  }
-  else if (strcasecmp(cmd, "changepin")==0) {
-    rv=changePin(db, argc, argv);
-  }
-  else if (strcasecmp(cmd, "setkey")==0) {
-    rv=setKey(db, argc, argv);
-  }
-  else if (strcasecmp(cmd, "hashTree")==0) {
-    rv=hashTree(db, argc, argv);
-  }
-  else if (strcasecmp(cmd, "checkTree")==0) {
-    rv=checkTree(db, argc, argv);
-  }
-  else if (strcasecmp(cmd, "showpasswords")==0) {
-    rv=showPasswords(db, argc, argv);
+  func=GWEN_Funcs_Find(funcs, cmd);
+  if (func!=NULL) {
+    rv=GWEN_Funcs_Call_DB_NODE_Args(func, db, argc, argv);
   }
   else {
     fprintf(stderr, "ERROR: Unknown command \"%s\".\n", cmd);
-- 
2.26.2

