From e6dc3b4ffd622e1f2bd5bc8887ec1da9a98134bb Mon Sep 17 00:00:00 2001 From: Simon Lindholm Date: Wed, 29 Jun 2022 01:29:26 +0200 Subject: [PATCH] Command line parsing --- Makefile | 2 +- common.h | 5 +++++ main.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index e1b99fd..9cbf0eb 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ all: wibo -CXXFLAGS = -Wall -g -m32 -std=c++20 -lstdc++ +CXXFLAGS = -Wall -g -m32 -std=c++20 -lstdc++ -MD BUILD_DIR := build CPP_FILES := $(wildcard *.cpp) diff --git a/common.h b/common.h index bdcdcec..76db4e4 100644 --- a/common.h +++ b/common.h @@ -6,15 +6,20 @@ #include #define WIN_FUNC __attribute__((stdcall)) +#define DEBUG_LOG(...) /* nothing */ +// #define DEBUG_LOG(...) wibo::debug_log(__VA_ARGS__) namespace wibo { extern uint32_t lastError; extern char *commandLine; + void debug_log(const char *fmt, ...); + void *resolveVersion(const char *name); void *resolveKernel32(const char *name); void *resolveUser32(const char *name); void *resolveAdvApi32(const char *name); + void *resolveLmgr11(uint16_t ordinal); void *resolveStubByName(const char *dllName, const char *funcName); void *resolveStubByOrdinal(const char *dllName, uint16_t ordinal); diff --git a/main.cpp b/main.cpp index 3940b43..309dba5 100644 --- a/main.cpp +++ b/main.cpp @@ -106,6 +106,11 @@ struct TIB { }; int main(int argc, char **argv) { + if (argc <= 1) { + printf("Usage: ./wibo program.exe ...\n"); + return 1; + } + // Create TIB TIB tib; tib.tib = &tib; @@ -125,13 +130,46 @@ int main(int argc, char **argv) { return 1; } - // Build a command line (todo, fill this with argv etc) - wibo::commandLine = new char[1024]; - strcpy(wibo::commandLine, "mwcceppc.exe"); + // Build a command line + std::string cmdLine; + for (int i = 1; i < argc; i++) { + if (i != 1) cmdLine += ' '; + std::string arg = argv[i]; + bool needQuotes = arg.find_first_of("\\\" \t\n") != std::string::npos; + if (needQuotes) + cmdLine += '"'; + int backslashes = 0; + for (const char *p = arg.c_str(); ; p++) { + char c = *p; + if (c == '\\') { + backslashes++; + continue; + } + + // Backslashes are doubled *before quotes* + for (int j = 0; j < backslashes; j++) { + cmdLine += '\\'; + if (c == '\0' || c == '"') + cmdLine += '\\'; + } + + if (c == '\0') + break; + if (c == '\"') + cmdLine += '\\'; + cmdLine += c; + } + if (needQuotes) + cmdLine += '"'; + } + cmdLine += '\0'; + + wibo::commandLine = cmdLine.data(); + DEBUG_LOG("Command line: %s\n", wibo::commandLine); wibo::Executable exec; - FILE *f = fopen("mwcceppc.exe", "rb"); + FILE *f = fopen(argv[1], "rb"); exec.loadPE(f); fclose(f); @@ -144,5 +182,5 @@ int main(int argc, char **argv) { ); DEBUG_LOG("We came back\n"); - return 0; + return 1; }