From a605b6570fc6de9d7467f6fef5cc3561976c90f9 Mon Sep 17 00:00:00 2001 From: Oskari Timperi Date: Thu, 16 Feb 2017 22:35:25 +0200 Subject: Replace getopt.c with optparse.c for tools The magic getopt.c didn't work out with MSVC for some reason. --- tools/CMakeLists.txt | 6 +- tools/getopt.c | 358 --------------------------------------------------- tools/getopt.h | 175 ------------------------- tools/optparse.c | 264 +++++++++++++++++++++++++++++++++++++ tools/optparse.h | 102 +++++++++++++++ tools/pub.c | 81 ++++++++---- tools/sub.c | 72 +++++++---- 7 files changed, 471 insertions(+), 587 deletions(-) delete mode 100644 tools/getopt.c delete mode 100644 tools/getopt.h create mode 100644 tools/optparse.c create mode 100644 tools/optparse.h (limited to 'tools') diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 1a736ab..ed14e8c 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -1,7 +1,7 @@ -ADD_LIBRARY(getopt OBJECT getopt.c) +ADD_LIBRARY(optparse OBJECT optparse.c optparse.h) -ADD_EXECUTABLE(pub pub.c $) +ADD_EXECUTABLE(pub pub.c $) TARGET_LINK_LIBRARIES(pub mqtt) -ADD_EXECUTABLE(sub sub.c $) +ADD_EXECUTABLE(sub sub.c $) TARGET_LINK_LIBRARIES(sub mqtt) diff --git a/tools/getopt.c b/tools/getopt.c deleted file mode 100644 index 5277ed0..0000000 --- a/tools/getopt.c +++ /dev/null @@ -1,358 +0,0 @@ -#include -#include -#include -#include - -#include "getopt.h" - -/* - * Standard getopt global variables. optreset starts as non-zero in order to - * trigger initialization behaviour. - */ -const char * optarg = NULL; -int optind = 1; -int opterr = 1; -int optreset = 1; - -/* - * Quasi-internal global variables -- these are used via GETOPT macros. - */ -const char * getopt_dummy = "(dummy)"; -int getopt_initialized = 0; - -/* - * Internal variables. - */ -static const char * cmdname = NULL; -static struct opt { - const char * os; - size_t olen; - int hasarg; -} * opts = NULL; -static size_t nopts; -static size_t opt_missing; -static size_t opt_default; -static size_t opt_found; -static const char * packedopts; -static char popt[3]; -static int atexit_registered = 0; - -/* Print a message. */ -#define PRINTMSG(...) do { \ - if (cmdname != NULL) \ - fprintf(stderr, "%s: ", cmdname); \ - fprintf(stderr, __VA_ARGS__); \ - fprintf(stderr, "\n"); \ -} while (0) - -/* Print an error message and die. */ -#define DIE(...) do { \ - PRINTMSG(__VA_ARGS__); \ - abort(); \ -} while (0) - -/* Print a warning, if warnings are enabled. */ -#define WARN(...) do { \ - if (opterr == 0) \ - break; \ - if (opt_missing != opt_default) \ - break; \ - PRINTMSG(__VA_ARGS__); \ -} while (0) - -/* Free allocated options array. */ -static void -atexit_handler(void) -{ - - free(opts); - opts = NULL; -} - -/* Reset internal state. */ -static void -reset(int argc, char * const argv[]) -{ - const char * p; - - /* If we have arguments, stash argv[0] for error messages. */ - if (argc > 0) { - /* Find the basename, without leading directories. */ - for (p = cmdname = argv[0]; *p != '\0'; p++) { - if (*p == '/') - cmdname = p + 1; - } - } - - /* Discard any registered command-line options. */ - free(opts); - opts = NULL; - - /* Register atexit handler if we haven't done so already. */ - if (!atexit_registered) { - atexit(atexit_handler); - atexit_registered = 1; - } - - /* We will start scanning from the first option. */ - optind = 1; - - /* We're not in the middle of any packed options. */ - packedopts = NULL; - - /* We haven't found any option yet. */ - opt_found = (size_t)(-1); - - /* We're not initialized yet. */ - getopt_initialized = 0; - - /* Finished resetting state. */ - optreset = 0; -} - -/* Search for an option string. */ -static size_t -searchopt(const char * os) -{ - size_t i; - - /* Scan the array of options. */ - for (i = 0; i < nopts; i++) { - /* Is there an option in this slot? */ - if (opts[i].os == NULL) - continue; - - /* Does this match up to the length of the option string? */ - if (strncmp(opts[i].os, os, opts[i].olen)) - continue; - - /* Do we have