diff --git a/CMakeLists.txt b/CMakeLists.txt index dd7c4cc..c8c3ba5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,6 +73,10 @@ ENDIF() CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_BINARY_DIR}/config.h) +if (MSVC) + add_subdirectory(winclude) +endif() + add_subdirectory(bin) add_subdirectory(cpu) add_subdirectory(toolbox) diff --git a/bin/CMakeLists.txt b/bin/CMakeLists.txt index 59fc8a7..0a6d11f 100644 --- a/bin/CMakeLists.txt +++ b/bin/CMakeLists.txt @@ -93,6 +93,10 @@ IF (ENABLE_DEBUGGER) ENDIF() +if (MSVC) + target_link_libraries(mpw WINLIB_LIB) +endif() + target_link_libraries(mpw CPU_LIB) target_link_libraries(mpw TOOLBOX_LIB) target_link_libraries(mpw MPW_LIB) diff --git a/bin/main.cpp b/bin/main.cpp index fa70ba0..89e2c73 100644 --- a/bin/main.cpp +++ b/bin/main.cpp @@ -39,6 +39,7 @@ #include #include #include +#include #include diff --git a/config.h.in b/config.h.in index 368959b..0564261 100644 --- a/config.h.in +++ b/config.h.in @@ -17,6 +17,7 @@ #ifdef _WIN32 typedef long ssize_t; +extern "C" int asprintf(char **, const char *, ...); #endif #endif diff --git a/mpw/CMakeLists.txt b/mpw/CMakeLists.txt index 68b4917..e21c0e9 100644 --- a/mpw/CMakeLists.txt +++ b/mpw/CMakeLists.txt @@ -31,13 +31,14 @@ add_custom_command( - +if(NOT MSVC) set_source_files_properties( environment.cpp # environ.cpp ep.cpp epv.cpp PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-variable" ) +endif() add_library(MPW_LIB ${MPW_SRC}) diff --git a/mpw/environment.rl b/mpw/environment.rl index b0144ca..17a180a 100644 --- a/mpw/environment.rl +++ b/mpw/environment.rl @@ -33,8 +33,11 @@ #include #include -extern char **environ; +#include "config.h" +#ifndef _WIN32 +extern char **environ; +#endif namespace _env_rl { #if __APPLE__ && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1050 diff --git a/mpw/mpw.cpp b/mpw/mpw.cpp index ae3cc74..105a252 100644 --- a/mpw/mpw.cpp +++ b/mpw/mpw.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -40,9 +41,7 @@ #include #include -#include -#include -#include + #include #include @@ -56,8 +55,15 @@ #include -extern char **environ; +#include +#include + +namespace fs = std::experimental::filesystem; + +#ifndef _WIN32 +extern char **environ; +#endif namespace MPW { @@ -78,108 +84,6 @@ namespace MPW const std::string RootDir(); - - static bool isdir(const std::string &path) - { - struct stat st; - if (stat(path.c_str(), &st) < 0) return false; - return S_ISDIR(st.st_mode); - } - - std::string RootDirPathForFile(const std::string &file) - { - std::string dir(RootDir()); - if (dir.length() && dir.back() != '/') dir.push_back('/'); - dir.append(file); - - return dir; - } - - const std::string RootDir() - { - static bool initialized = false; - static std::string path; - - static const std::string paths[] = { - "/usr/local/share/mpw", - "/usr/share/mpw", - }; - - char *cp; - struct passwd *pw; - - if (initialized) return path; - - initialized = true; - - // check $MPW, $HOME/mpw, /usr/local/share/mpw/, /usr/share/mpw - // for a directory. - - cp = getenv("MPW"); - if (cp && *cp) - { - std::string s(cp); - if (isdir(s)) - { - path = std::move(s); - return path; - } - } - - // home/mpw - pw = getpwuid(getuid()); - if (pw && pw->pw_dir && pw->pw_dir[0]) - { - std::string s(pw->pw_dir); - if (s.back() != '/') s.push_back('/'); - s.append("mpw"); - - if (isdir(s)) - { - path = std::move(s); - return path; - } - } -#if 0 - // thread-safe - { - int size; - - size = sysconf(_SC_GETPW_R_SIZE_MAX); - if (size >= 0) - { - struct passwd pwd, *result = nullptr; - char *buffer = alloca(size); - - if (getpwuid_r(getuid(), &pwd, buffer, size, &result) == 0 && result) - { - std::string s(pwd.pw_dir); - if (s.back() != '/') s.push_back('/'); - s.append("mpw"); - if (isdir(s)) - { - path = std::move(s); - return path; - } - } - } - } -#endif - for (auto &iter : paths) - { - if (isdir(iter)) - { - path = iter; - return path; - } - - } - - - - return path; // unknown. - } - uint16_t InitEnvironment(const std::vector &defines) { void EnvLoadFile(const std::string &envfile); diff --git a/mpw/mpw_errno.cpp b/mpw/mpw_errno.cpp index 8ef814f..a2a8384 100644 --- a/mpw/mpw_errno.cpp +++ b/mpw/mpw_errno.cpp @@ -128,7 +128,9 @@ namespace MPW { case kENOMEM: return ENOMEM; case kEACCES: return EACCES; case kEFAULT: return EFAULT; +#ifdef ENOTBLK case kENOTBLK: return ENOTBLK; +#endif case kEBUSY: return EBUSY; case kEEXIST: return EEXIST; case kEXDEV: return EXDEV; @@ -193,7 +195,9 @@ namespace MPW { case ENOMEM: return kENOMEM; case EACCES: return kEACCES; case EFAULT: return kEFAULT; +#ifdef ENOTBLK case ENOTBLK: return kENOTBLK; +#endif case EBUSY: return kEBUSY; case EEXIST: return kEEXIST; case EXDEV: return kEXDEV; diff --git a/native/Windows.cpp b/native/Windows.cpp index 9b3014f..900fb31 100644 --- a/native/Windows.cpp +++ b/native/Windows.cpp @@ -23,4 +23,49 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ - \ No newline at end of file + + +#include +#include + +#include +#include + +namespace fs = std::experimental::filesystem; + +namespace native { + + static fs::path FindRoot() { + fs::path p; + std::error_code ec; + + PWSTR tmp = nullptr; + auto ok = SHGetKnownFolderPath( + FOLDERID_Profile, + 0, + nullptr, + &tmp + ); + if ((ok == S_OK) && (tmp != nullptr)) { + p = tmp; + p /= "mpw"; + if (fs::is_directory(p, ec)) return p; + } + + p = "c:\\mpw\\"; + if (!fs::is_directory(p, ec)) { + fprintf(stderr, "Warning: %ls does not exist\n", p.c_str()); + } + return p; + } + fs::path RootPath() { + // todo -- do it. + static fs::path root; + if (root.empty()) { + root = FindRoot(); + } + return root; + } + + +} \ No newline at end of file diff --git a/winclude/strings.h b/winclude/strings.h index e69de29..709349c 100644 --- a/winclude/strings.h +++ b/winclude/strings.h @@ -0,0 +1,9 @@ +#ifndef __winclude_strings_h__ +#define __winclude_strings_h__ + +#include + +//int strcasecmp(const char *s1, const char *s2); +#define strcasecmp stricmp + +#endif \ No newline at end of file diff --git a/winclude/unistd.h b/winclude/unistd.h index d24f124..97e2aa5 100644 --- a/winclude/unistd.h +++ b/winclude/unistd.h @@ -5,6 +5,9 @@ #define ftruncate(a,b) chsize(a,b) -typedef long ssize_t; + +#define STDIN_FILENO 0 +#define STDOUT_FILENO 1 +#define STDERR_FILENO 2 #endif