From 70f5f1c083158ea01513bc07955d62b3b3f43795 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sun, 25 Aug 2024 00:13:27 +0200
Subject: Restore basic command line argument support.

---
 src/app.c | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 145 insertions(+), 2 deletions(-)

diff --git a/src/app.c b/src/app.c
index a3a2080..15fe255 100644
--- a/src/app.c
+++ b/src/app.c
@@ -24,6 +24,9 @@
 
 #include <assert.h>
 #include <fcntl.h>
+#include <getopt.h>
+#include <libgen.h>
+#include <string.h>
 #include <gio/gdesktopappinfo.h>
 #include <gio/gio.h>
 #include <sys/auxv.h>
@@ -83,6 +86,12 @@ static void gtk_chrysalide_framework_activate(GApplication *);
 /* ---------------------- POINT D'ENTREE PRINCIPAL D'EXECUTION ---------------------- */
 
 
+/* Affiche des indications quant à l'utilisation du programme. */
+static void show_chrysalide_help(const char *);
+
+/* Affiche des indications sur la version courante du programme. */
+static void show_chrysalide_version(void);
+
 /* Installe au besoin une définition locale pour le système. */
 static void ensure_wm_icon_and_name(void);
 
@@ -252,6 +261,72 @@ static void gtk_chrysalide_framework_activate(GApplication *app)
 
 /******************************************************************************
 *                                                                             *
+*  Paramètres  : name = nom du programme en question.                         *
+*                                                                             *
+*  Description : Affiche des indications quant à l'utilisation du programme.  *
+*                                                                             *
+*  Retour      : -                                                            *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static void show_chrysalide_help(const char *name)
+{
+    char *tmp;                              /* Conservation modifiable     */
+    char *base;                             /* Version courte du nom       */
+
+    tmp = strdup(name);
+
+    base = basename(tmp);
+
+    printf("\n");
+
+    printf("Usage: %s [--help] [--version]\n", base);
+    printf("       %s [args] <filename(s)...>\n", base);
+
+    free(tmp);
+
+    printf("\n");
+
+    printf("\t-h --help\t\tShow this help message.\n");
+    printf("\t-v --version\t\tDisplay the program version.\n");
+
+    printf("\n");
+
+    printf("\t-V --verbosity=level\tSet the log level (0 for all messages, %u for none).\n", LMT_COUNT);
+
+    printf("\n");
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : -                                                            *
+*                                                                             *
+*  Description : Affiche des indications sur la version courante du programme.*
+*                                                                             *
+*  Retour      : -                                                            *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static void show_chrysalide_version(void)
+{
+    printf("\n");
+
+    printf("-o-  Chrysalide r%u  -o-\n", REVISION);
+    printf(_("Compiled on %s at %s\n"), __DATE__, __TIME__);
+
+    printf("\n");
+
+}
+
+
+/******************************************************************************
+*                                                                             *
 *  Paramètres  : -                                                            *
 *                                                                             *
 *  Description : Installe au besoin une définition locale pour le système.    *
@@ -362,21 +437,89 @@ static void ensure_wm_icon_and_name(void)
 int main(int argc, char **argv)
 {
     int result;                             /* Bilan de l'exécution        */
+    bool show_help;                         /* Affichage de l'aide ?       */
+    bool show_version;                      /* Affichage de la version ?   */
+    LogMessageType verbosity;               /* Niveau de filtre de message */
+    int index;                              /* Indice d'argument           */
+    int ret;                                /* Bilan d'un appel            */
     GtkChrysalideFramework *app;            /* Gestion d'application GTK   */
 
+    static struct option long_options[] = {
+        { "help",       no_argument,        NULL,   'h' },
+        { "version",    no_argument,        NULL,   'v' },
+        { "verbosity",  required_argument,  NULL,   'V' },
+        { NULL,         0,                  NULL,   0 }
+    };
+
+    result = EXIT_FAILURE;
+
+    /* Décodage des options */
+
+    show_help = false;
+    show_version = false;
+
+    verbosity = LMT_COUNT;
+
+    while (true)
+    {
+        ret = getopt_long(argc, argv, "hvV:", long_options, &index);
+        if (ret == -1) break;
+
+        switch (ret)
+        {
+            case 'h':
+                show_help = true;
+                break;
+
+            case 'v':
+                show_version = true;
+                break;
+
+            case 'V':
+                verbosity = strtoul(optarg, NULL, 10);
+                break;
+
+        }
+
+    }
+
+    /* Actions de base */
+
+    if (show_help)
+    {
+        show_chrysalide_help(argv[0]);
+        result = EXIT_SUCCESS;
+        goto exit;
+    }
+
+    if (show_version)
+    {
+        show_chrysalide_version();
+        result = EXIT_SUCCESS;
+        goto exit;
+    }
+
+    /* Lancement des choses sérieuses */
+
+    set_log_verbosity(verbosity);
+
     if (!load_gui_components(AGC_BUFFER_FEATURES | AGC_PANELS))
-        return EXIT_FAILURE;
+        goto exit;
 
     ensure_wm_icon_and_name();
 
+    g_set_prgname("Chrysalide");
+
     app = gtk_chrysalide_framework_new();
 
-    result = g_application_run(G_APPLICATION(app), argc, argv);
+    result = g_application_run(G_APPLICATION(app), 0, NULL);
 
     g_object_unref(G_OBJECT(app));
 
     unload_gui_components(AGC_BUFFER_FEATURES | AGC_PANELS);
 
+ exit:
+
     return result;
 
 }
-- 
cgit v0.11.2-87-g4458