diff --git a/src/SieFs.cpp b/src/SieFs.cpp index d5a605f..4a43bb3 100644 --- a/src/SieFs.cpp +++ b/src/SieFs.cpp @@ -1,22 +1,50 @@ #include "SieFs.h" #include +#include + +#include "log.h" +#include "utils.h" static std::map m_drives; void SieFs::mount(const std::string &drive, const std::string &path) { - m_drives[drive] = path; + m_drives[drive] = std::filesystem::canonical(path + "/"); + if (m_drives[drive] == "/") + m_drives[drive] = ""; + LOGD("mount: %s -> %s\n", drive.c_str(), m_drives[drive].c_str()); } -std::string SieFs::path2sie(const std::string &path) { - return ""; +std::string SieFs::path2sie(const std::string &unix_path) { + std::string siemens_path; + for (auto [drive, mount_path]: m_drives) { + if (unix_path == mount_path) { + siemens_path = drive + ":" + unix_path; + break; + } else if (mount_path.size() > 0 && strStartsWith(unix_path, mount_path + "/")) { + siemens_path = drive + ":" + unix_path.substr(mount_path.size()); + break; + } else if (!mount_path.size()) { + siemens_path = drive + ":" + unix_path; // fallback + } + } + + std::replace(siemens_path.begin(), siemens_path.end(), '/', '\\'); + fprintf(stderr, "path2sie: %s -> %s\n", unix_path.c_str(), siemens_path.c_str()); + + if (!siemens_path.size()) { + fprintf(stderr, "Invalid unix path: %s\n", unix_path.c_str()); + abort(); + } + + return siemens_path; } -std::string SieFs::sie2path(const std::string &uri) { +std::string SieFs::sie2path(const std::string &siemens_path) { std::regex path_re("^([0-9a-f]):\\\\(.*?)$", std::regex_constants::ECMAScript | std::regex_constants::icase); std::smatch m; - if (std::regex_match(uri, m, path_re)) { + if (std::regex_match(siemens_path, m, path_re)) { auto drive = m[1].str(); auto path = m[2].str(); @@ -28,7 +56,7 @@ std::string SieFs::sie2path(const std::string &uri) { return unix_path; } } - fprintf(stderr, "Invalid siemens path: %s\n", uri.c_str()); + fprintf(stderr, "Invalid siemens path: %s\n", siemens_path.c_str()); abort(); return ""; } diff --git a/src/main.cpp b/src/main.cpp index 3356fb6..83a40dc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -76,9 +76,11 @@ int main(int argc, char **argv) { LOGD("run INIT array\n"); loader_run_INIT_Array(ex); + std::string fname = SieFs::path2sie(filename); + auto entry = (int (*)(const char *, const char *, const void *)) loader_elf_entry(ex); - printf("run entry at %p\n", entry); - int ret = entry(filename.c_str(), nullptr, nullptr); + printf("run entry at %p (fname=%s)\n", entry, fname.c_str()); + int ret = entry(fname.c_str(), nullptr, nullptr); LOGD("entry ret = %d\n", ret); // loader_elf_close(ex); diff --git a/src/swi/fs.cpp b/src/swi/fs.cpp index 0f30b1e..9012fd2 100644 --- a/src/swi/fs.cpp +++ b/src/swi/fs.cpp @@ -35,7 +35,7 @@ int SWI_open(const char *path, unsigned int f, unsigned int m, unsigned int *err flags |= O_APPEND; int ret; - if (strcmp(path, "0:\\Misc\\stdout.txt") == 0) { + if (0&&strcmp(path, "0:\\Misc\\stdout.txt") == 0) { ret = dup(STDOUT_FILENO); } else if (strcmp(path, "0:\\Misc\\stderr.txt") == 0) { ret = dup(STDERR_FILENO);