From fa01a6fe27a6ddd0eb864dd8e4bfcf0aa1fbaa5f Mon Sep 17 00:00:00 2001 From: Evan Miller Date: Mon, 3 Aug 2020 19:53:58 -0400 Subject: [PATCH] Implement g_option_context_get_help (fake GLib) This fixes the crashing issues, but the command-line tools are still useless. --- include/mdbfakeglib.h | 5 ++++- src/libmdb/fakeglib.c | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/include/mdbfakeglib.h b/include/mdbfakeglib.h index beb8ee4..9bfd942 100644 --- a/include/mdbfakeglib.h +++ b/include/mdbfakeglib.h @@ -16,6 +16,7 @@ typedef unsigned int guint; typedef void * gpointer; typedef const void * gconstpointer; typedef uint8_t guint8; +typedef guint32 GQuark; typedef guint (*GHashFunc)(gconstpointer); typedef int (*GCompareFunc)(gconstpointer, gconstpointer); @@ -41,7 +42,9 @@ typedef struct GHashTable { } GHashTable; typedef struct GError { - const char *message; + GQuark domain; + gint code; + gchar *message; } GError; typedef enum GOptionArg { diff --git a/src/libmdb/fakeglib.c b/src/libmdb/fakeglib.c index b659090..27244d3 100644 --- a/src/libmdb/fakeglib.c +++ b/src/libmdb/fakeglib.c @@ -242,8 +242,39 @@ void g_option_context_add_main_entries (GOptionContext *context, gchar *g_option_context_get_help (GOptionContext *context, gboolean main_help, void *group) { - /* TODO */ - return NULL; +#if defined(__APPLE__) || defined(__FreeBSD__) + const char * appname = getprogname(); +#elif defined(_GNU_SOURCE) + const char * appname = program_invocation_name; +#else + const char * appname = "mdb-util"; +#endif + + char *help = malloc(4096); + char *end = help + 4096; + char *p = help; + p += snprintf(p, end - p, + "Usage:\n %s [OPTION\xE2\x80\xA6] %s\n\n", appname, context->desc); + p += snprintf(p, end - p, + "Help Options:\n -h, --%-20s%s\n\n", "help", "Show help options"); + p += snprintf(p, end - p, + "Application Options:\n"); + int i=0; + for (i=0; context->entries[i].long_name; i++) { + p += snprintf(p, end - p, " -%c, --", context->entries[i].short_name); + if (context->entries[i].arg_description) { + char *long_name = g_strconcat( + context->entries[i].long_name, "=", + context->entries[i].arg_description, NULL); + p += snprintf(p, end - p, "%-20s", long_name); + free(long_name); + } else { + p += snprintf(p, end - p, "%-20s", context->entries[i].long_name); + } + p += snprintf(p, end - p, "%s\n", context->entries[i].description); + } + p += snprintf(p, end - p, "\n"); + return help; } GOptionContext *g_option_context_new(const char *description) { @@ -254,6 +285,8 @@ GOptionContext *g_option_context_new(const char *description) { gboolean g_option_context_parse(GOptionContext *context, gint *argc, gchar ***argv, GError **error) { + *error = malloc(sizeof(GError)); + (*error)->message = "Not implemented"; /* TODO */ return FALSE; }