From 27f9f0c9d82366b4b27e2417e2b0cbd35a9f907e Mon Sep 17 00:00:00 2001 From: Dieter Baron Date: Thu, 18 Jan 2024 13:33:50 +0100 Subject: [PATCH] Revert "No longer depend on getopt_long()." This reverts commit c1b9200bb923a0e55ed1062ec7bada232b8cad8b. --- CMakeLists.txt | 2 + cmake-config.h.in | 2 + regress/dbrestore.cc | 102 ++++++---- src/CMakeLists.txt | 3 + src/Commandline.cc | 182 ++++++++--------- src/Commandline.h | 4 - src/compat.h | 11 ++ src/getopt_long.cc | 451 +++++++++++++++++++++++++++++++++++++++++++ src/getopt_long.h | 65 +++++++ src/getprogname.cc | 54 ++++++ 10 files changed, 738 insertions(+), 138 deletions(-) create mode 100644 src/getopt_long.cc create mode 100644 src/getopt_long.h create mode 100644 src/getprogname.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 442276f9..b3eb2347 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,6 +63,8 @@ add_dependencies(distcheck dist) check_function_exists(MD5Init HAVE_MD5INIT) check_function_exists(SHA1Init HAVE_SHA1INIT) check_function_exists(fnmatch HAVE_FNMATCH) +check_function_exists(getopt_long HAVE_GETOPT_LONG) +check_function_exists(getprogname HAVE_GETPROGNAME) check_symbol_exists(_stricmp string.h HAVE__STRICMP) check_symbol_exists(strcasecmp strings.h HAVE_STRCASECMP) diff --git a/cmake-config.h.in b/cmake-config.h.in index d6ab572b..091c9b24 100644 --- a/cmake-config.h.in +++ b/cmake-config.h.in @@ -9,6 +9,8 @@ #cmakedefine HAVE_TOMLPLUSPLUS #cmakedefine HAVE_FNMATCH +#cmakedefine HAVE_GETOPT_LONG +#cmakedefine HAVE_GETPROGNAME #cmakedefine HAVE_MD5INIT #cmakedefine HAVE_SHA1INIT #cmakedefine HAVE_STRCASECMP diff --git a/regress/dbrestore.cc b/regress/dbrestore.cc index 19c281a9..cd2a5892 100644 --- a/regress/dbrestore.cc +++ b/regress/dbrestore.cc @@ -66,61 +66,95 @@ static int restore_table(sqlite3 *db, FILE *f); static void unget_line(const std::string &line); static std::vector split(const std::string &string, const std::string &separaptor, bool strip_whitespace = false); -std::vector dbrestore_options = { - Commandline::Option("db-version", "version", "specify DB schema version"), - Commandline::Option("sql","file", "take SQL schema from FILE"), - Commandline::Option("type", 't', "type", "specify type of database: mamedb (default) or ckmamedb)") + +#define QUERY_COLS_FMT "pragma table_info(%s)" + +const char *usage = "usage: %s [-hV] [--db-version VERSION] [--sql SQL_INIT_FILE] [-t db-type] dump-file db-file\n"; + +char help_head[] = PACKAGE " by Dieter Baron and Thomas Klausner\n\n"; + +char help[] = "\n" + " --db-version VERSION specify version of database schema\n" + " -h, --help display this help message\n" + " --sql SQL_INIT_FILE use table definitions from this SQL init file\n" + " -t, --type TYPE restore database of type TYPE (ckmamedb, mamedb, memdb)\n" + " -V, --version display version number\n" + "\nReport bugs to " PACKAGE_BUGREPORT ".\n"; + +char version_string[] = PACKAGE " " VERSION "\n" + "Copyright (C) 2014-2021 Dieter Baron and Thomas Klausner\n" PACKAGE " comes with ABSOLUTELY NO WARRANTY, to the extent permitted by law.\n"; + + +#define OPTIONS "ht:V" + +enum { + OPT_DB_VERSION = 256, + OPT_SQL +}; + +struct option options[] = { + {"db-version", 1, 0, OPT_DB_VERSION }, + {"help", 0, 0, 'h'}, + {"sql", 1, 0, OPT_SQL }, + {"type", 1, 0, 't'}, + {"version", 0, 0, 'V'} }; -#define PROGRAM_NAME "dbrestore" int main(int argc, char *argv[]) { + setprogname(argv[0]); + DBType type = DBTYPE_ROMDB; std::string sql_file; int db_version = -1; - const char *header = PROGRAM_NAME " by Dieter Baron and Thomas Klausner"; - const char *footer = "Report bugs to " PACKAGE_BUGREPORT "."; - const char *version = "dumpgame (" PACKAGE " " VERSION ")\nCopyright (C) 2021-2024 Dieter Baron and Thomas Klausner\n" - PACKAGE " " VERSION "\n" - PACKAGE " comes with ABSOLUTELY NO WARRANTY, to the extent permitted by law.\n"; - - auto commandline = Commandline(dbrestore_options, "dump-file db-file", header, footer, version); - - auto arguments = commandline.parse(argc, argv); - - for (const auto& option: arguments.options) { - if (option.name == "db-version") { + opterr = 0; + int c; + while ((c = getopt_long(argc, argv, OPTIONS, options, 0)) != EOF) { + switch (c) { + case 'h': + fputs(help_head, stdout); + printf(usage, getprogname()); + fputs(help, stdout); + exit(0); + case 'V': + fputs(version_string, stdout); + exit(0); + + case 't': + if ((type = db_type(optarg)) == DBTYPE_INVALID) { + fprintf(stderr, "%s: unknown db type '%s'\n", getprogname(), optarg); + exit(1); + } + break; + + case OPT_DB_VERSION: try { size_t idx; - db_version = std::stoi(option.argument, &idx); - if (option.argument[idx] != '\0') { + db_version = std::stoi(optarg, &idx); + if (optarg[idx] != '\0') { throw std::invalid_argument(""); } } catch (...) { - fprintf(stderr, "%s: invalid DB schema version '%s'\n", getprogname(), option.argument.c_str()); + fprintf(stderr, "%s: invalid DB schema version '%s'\n", getprogname(), optarg); exit(1); } - } - else if (option.name == "sql") { - sql_file = option.argument; - } - else if (option.name == "type") { - if ((type = db_type(option.argument)) == DBTYPE_INVALID) { - fprintf(stderr, "%s: unknown db type '%s'\n", getprogname(), option.argument.c_str()); - exit(1); - } - } + break; + + case OPT_SQL: + sql_file = optarg; + break; + } } - if (arguments.arguments.size() != 2) { - commandline.usage(false, stderr); + if (optind != argc - 2) { + fprintf(stderr, usage, getprogname()); exit(1); } - std::string dump_fname = arguments.arguments[0]; - std::string db_fname = arguments.arguments[1]; + std::string dump_fname = argv[optind]; + std::string db_fname = argv[optind + 1]; output.set_error_file(dump_fname); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ebfc8edd..c1f1bf3e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,6 @@ +if(NOT HAVE_GETPROGRAME) + list(APPEND COMPATIBILITY getprogname.cc) +endif() if(NOT HAVE_MD5INIT) list(APPEND COMPATIBILITY md5.cc) endif() diff --git a/src/Commandline.cc b/src/Commandline.cc index ab7ab0d9..3f36aff3 100644 --- a/src/Commandline.cc +++ b/src/Commandline.cc @@ -1,5 +1,5 @@ /* - Commandline.cc -- parse command line options and arguments + Commandline.h -- parse command line options and arguments Copyright (C) 2021 Dieter Baron and Thomas Klausner This file is part of ckmame, a program to check rom sets for MAME. @@ -36,119 +36,101 @@ #include #include #include -#include #include #include +#include "compat.h" + +#include "Exception.h" + +extern int optind; + Commandline::Commandline(std::vector