Command line parsing

This commit is contained in:
Simon Lindholm 2022-06-29 01:29:26 +02:00
parent 82c05df74e
commit e6dc3b4ffd
3 changed files with 49 additions and 6 deletions

View File

@ -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)

View File

@ -6,15 +6,20 @@
#include <assert.h>
#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);

View File

@ -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;
}