diff --git a/.gitignore b/.gitignore index 803aeabe..58cb2528 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,8 @@ *.wasm *.cwasm libcbqn.so +*.exe +*.dll # build system things /build/obj/ diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index 45cbca33..047e1e6e 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -9,7 +9,11 @@ #include "../nfns.h" #include -#include +#if !defined(_WIN32) && !defined(_WIN64) + #include +#else + #include "../windows/getline.c" +#endif #include static bool eqStr(B w, u32* x) { diff --git a/src/main.c b/src/main.c index 102f22de..6872c8e5 100644 --- a/src/main.c +++ b/src/main.c @@ -899,7 +899,11 @@ int main(int argc, char* argv[]) { } char* ln = NULL; size_t gl = 0; - i64 read = getline(&ln, &gl, stdin); + #if defined(_WIN32) || defined(_WIN64) + i64 read = 0; + #else + i64 read = getline(&ln, &gl, stdin); + #endif if (read<=0 || ln[0]==0) { if(!silentREPL) putchar('\n'); break; } if (ln[read-1]==10) ln[--read] = 0; cbqn_runLine(ln, read); diff --git a/src/utils/file.c b/src/utils/file.c index 71f3267d..00eada8b 100644 --- a/src/utils/file.c +++ b/src/utils/file.c @@ -6,6 +6,10 @@ #include #include +#if defined(_WIN32) || defined(_WIN64) + #include "../windows/realpath.c" +#endif + FILE* file_open(B path, char* desc, char* mode) { // doesn't consume char* p = toCStr(path); @@ -318,7 +322,11 @@ void mmap_init() { } bool dir_create(B path) { char* p = toCStr(path); - bool r = mkdir(p, S_IRWXU) == 0; + #if defined(_WIN32) || defined(_WIN64) + bool r = 1; + #else + bool r = mkdir(p, S_IRWXU) == 0; + #endif freeCStr(p); return r; } diff --git a/src/vm.c b/src/vm.c index e81219b6..c7895f2c 100644 --- a/src/vm.c +++ b/src/vm.c @@ -6,6 +6,10 @@ #include "utils/interrupt.h" #include +#if defined(_WIN32) || defined(_WIN64) + #include "windows/sysconf.c" +#endif + #ifndef UNWIND_COMPILER // whether to hide stackframes of the compiler in compiling errors #define UNWIND_COMPILER 1 #endif diff --git a/src/windows/getline.c b/src/windows/getline.c new file mode 100644 index 00000000..285e7f5d --- /dev/null +++ b/src/windows/getline.c @@ -0,0 +1,41 @@ + +#include "getline.h" + +ssize_t getline (char **lptr, size_t *n, FILE *fp) { + wchar_t buf[MAX_LINE_LENGTH/3] = {0}; + DWORD bytes; + DWORD chars, read_chars; + + int convertResult; + char *m; + + bytes = MAX_LINE_LENGTH; + chars = bytes/3; + + HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE); + + if (!ReadConsoleW(hIn, &buf, chars, &read_chars, NULL)) { + fprintf(stderr, "Failed to read console input: %d", GetLastError()); + goto error; + } + + convertResult = WideCharToMultiByte(CP_ACP, 0, buf, -1, NULL, 0, NULL, NULL); + if (convertResult == 0) { + fprintf(stderr, "Failed to get MultiByte length: %d", GetLastError()); + goto error; + } + + m = *lptr = (char*) calloc(convertResult, sizeof(char)); + + if (WideCharToMultiByte(CP_ACP, 0, buf, -1, m, convertResult, NULL, NULL) == 0 ) { + fprintf(stderr, "Failed to convert wide characters: %d", GetLastError()); + free(m); + goto error; + } + + return convertResult; + +error: + CloseHandle(hIn); + return -1; +} \ No newline at end of file diff --git a/src/windows/getline.h b/src/windows/getline.h new file mode 100644 index 00000000..11512b8b --- /dev/null +++ b/src/windows/getline.h @@ -0,0 +1,11 @@ +#ifndef GETLINE_H +#define GETLINE_H + +#include +#include + +#define MAX_LINE_LENGTH 8192 + +//ssize_t processinput (char **lptr, size_t *n, FILE *hIn); + +#endif /* GETLINE_H */ \ No newline at end of file diff --git a/src/windows/realpath.c b/src/windows/realpath.c new file mode 100644 index 00000000..327a2652 --- /dev/null +++ b/src/windows/realpath.c @@ -0,0 +1,9 @@ + +#include "realpath.h" + +char* realpath (const char *__restrict path, char *__restrict resolved_path) { + unsigned long len=strlen(path)+1; + void* r = malloc(len); + memcpy(r, path, len); + return r; +} \ No newline at end of file diff --git a/src/windows/realpath.h b/src/windows/realpath.h new file mode 100644 index 00000000..ba43b8ae --- /dev/null +++ b/src/windows/realpath.h @@ -0,0 +1,9 @@ +#ifndef REALPATH_H +#define REALPATH_H + +#define S_ISLNK(m) (0) +#define S_ISSOCK(m) (0) + +char* realpath (const char *__restrict path, char *__restrict resolved_path); + +#endif /* REALPATH_H */ \ No newline at end of file diff --git a/src/windows/sysconf.c b/src/windows/sysconf.c new file mode 100644 index 00000000..34c1bdbe --- /dev/null +++ b/src/windows/sysconf.c @@ -0,0 +1,6 @@ + +#include "sysconf.h" + +long int sysconf (int in) { + return 1L; +} \ No newline at end of file diff --git a/src/windows/sysconf.h b/src/windows/sysconf.h new file mode 100644 index 00000000..1d20ef2b --- /dev/null +++ b/src/windows/sysconf.h @@ -0,0 +1,8 @@ +#ifndef SYSCONF_H +#define SYSCONF_H + +#define _SC_PAGESIZE 8 + +long int sysconf (int in); + +#endif /* SYSCONF_H */ \ No newline at end of file