authorgravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2020-06-15 21:51:53+03:00
committergravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2020-06-15 21:51:53+03:00
log242246f79312f3106290b9b1aea6dfd5a18368b0
treef8110edf645ab8c5c5105a9126dbbe11aa018591
parentc34bdff4bbba88feefeca6491730526b2cc9f72a

UTF16 create process, utf8->utf16 fix


1 files changed, 32 insertions(+), 28 deletions(-)

src/os.cpp+32-28
......@@ -7,6 +7,7 @@
77
88#include "os.hpp"
99#include "buffer.hpp"
10#include "heap.hpp"
1011#include "util.hpp"
1112#include "error.hpp"
1213#include "util_base.hpp"
......@@ -78,6 +79,7 @@ typedef SSIZE_T ssize_t;
7879
7980#if defined(ZIG_OS_WINDOWS)
8081static void utf16le_ptr_to_utf8(Buf *out, WCHAR *utf16le);
82static size_t utf8_to_utf16le(WCHAR *utf16_le, Slice<uint8_t> utf8);
8183static uint64_t windows_perf_freq;
8284#elif defined(__MACH__)
8385static clock_serv_t macos_calendar_clock;
......@@ -153,15 +155,21 @@ static void os_spawn_process_windows(ZigList<const char *> &args, Termination *t
153155 os_windows_create_command_line(&command_line, args);
154156
155157 PROCESS_INFORMATION piProcInfo = {0};
156 STARTUPINFO siStartInfo = {0};
157 siStartInfo.cb = sizeof(STARTUPINFO);
158 STARTUPINFOW siStartInfo = {0};
159 siStartInfo.cb = sizeof(STARTUPINFOW);
158160
159 const char *exe = args.at(0);
160 BOOL success = CreateProcessA(exe, buf_ptr(&command_line), nullptr, nullptr, TRUE, 0, nullptr, nullptr,
161 Slice<uint8_t> exe_slice = str(args.at(0));
162 auto exe_utf16_slice = Slice<WCHAR>::alloc(exe_slice.len + 1);
163 exe_utf16_slice.ptr[utf8_to_utf16le(exe_utf16_slice.ptr, exe_slice)] = 0;
164
165 auto command_line_utf16 = Slice<WCHAR>::alloc(buf_len(&command_line) + 1);
166 command_line_utf16.ptr[utf8_to_utf16le(command_line_utf16.ptr, buf_to_slice(&command_line))] = 0;
167
168 BOOL success = CreateProcessW(exe_utf16_slice.ptr, command_line_utf16.ptr, nullptr, nullptr, TRUE, CREATE_UNICODE_ENVIRONMENT, nullptr, nullptr,
161169 &siStartInfo, &piProcInfo);
162170
163171 if (!success) {
164 zig_panic("CreateProcess failed. exe: %s command_line: %s", exe, buf_ptr(&command_line));
172 zig_panic("CreateProcess failed. exe: %s command_line: %s", args.at(0), buf_ptr(&command_line));
165173 }
166174
167175 WaitForSingleObject(piProcInfo.hProcess, INFINITE);
......@@ -274,7 +282,7 @@ void os_path_join(Buf *dirname, Buf *basename, Buf *out_full_path) {
274282
275283Error os_path_real(Buf *rel_path, Buf *out_abs_path) {
276284#if defined(ZIG_OS_WINDOWS)
277 PathSpace rel_path_space = slice_to_prefixed_file_w({ (uint8_t*)buf_ptr(rel_path), buf_len(rel_path) });
285 PathSpace rel_path_space = slice_to_prefixed_file_w(buf_to_slice(rel_path));
278286 PathSpace out_abs_path_space;
279287
280288 if (_wfullpath(&out_abs_path_space.data.items[0], &rel_path_space.data.items[0], PATH_MAX_WIDE) == nullptr) {
......@@ -780,7 +788,7 @@ Error os_fetch_file(FILE *f, Buf *out_buf) {
780788
781789Error os_file_exists(Buf *full_path, bool *result) {
782790#if defined(ZIG_OS_WINDOWS)
783 PathSpace path_space = slice_to_prefixed_file_w({ (uint8_t*)buf_ptr(full_path), buf_len(full_path) });
791 PathSpace path_space = slice_to_prefixed_file_w(buf_to_slice(full_path));
784792 *result = GetFileAttributesW(&path_space.data.items[0]) != INVALID_FILE_ATTRIBUTES;
785793 return ErrorNone;
786794#else
......@@ -1338,8 +1346,8 @@ Error os_rename(Buf *src_path, Buf *dest_path) {
13381346 return ErrorNone;
13391347 }
13401348#if defined(ZIG_OS_WINDOWS)
1341 PathSpace src_path_space = slice_to_prefixed_file_w({ (uint8_t*)buf_ptr(src_path), buf_len(src_path) });
1342 PathSpace dest_path_space = slice_to_prefixed_file_w({ (uint8_t*)buf_ptr(dest_path), buf_len(dest_path) });
1349 PathSpace src_path_space = slice_to_prefixed_file_w(buf_to_slice(src_path));
1350 PathSpace dest_path_space = slice_to_prefixed_file_w(buf_to_slice(dest_path));
13431351 if (!MoveFileExW(&src_path_space.data.items[0], &dest_path_space.data.items[0], MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) {
13441352 return ErrorFileSystem;
13451353 }
......@@ -1436,7 +1444,14 @@ Error os_make_path(Buf *path) {
14361444
14371445Error os_make_dir(Buf *path) {
14381446#if defined(ZIG_OS_WINDOWS)
1439 PathSpace path_space = slice_to_prefixed_file_w({ (uint8_t*)buf_ptr(path), buf_len(path) });
1447 PathSpace path_space = slice_to_prefixed_file_w(buf_to_slice(path));
1448 if (memEql(buf_to_slice(path), str("C:\\dev\\tést"))) {
1449 for (size_t i = 0; i < path_space.len; i++) {
1450 fprintf(stderr, "%d ", path_space.data.items[i]);
1451 }
1452 fprintf(stderr, "\n");
1453 }
1454
14401455 if (!CreateDirectoryW(&path_space.data.items[0], NULL)) {
14411456 if (GetLastError() == ERROR_ALREADY_EXISTS)
14421457 return ErrorPathAlreadyExists;
......@@ -1727,22 +1742,11 @@ static uint8_t utf8CodepointSequenceLength(uint32_t c) {
17271742
17281743// Ported from std.unicode.utf8ByteSequenceLength
17291744static uint8_t utf8ByteSequenceLength(uint8_t first_byte) {
1730 switch (clzll(~first_byte)) {
1731 case 0:
1732 return 1;
1733 break;
1734 case 2:
1735 return 2;
1736 break;
1737 case 3:
1738 return 3;
1739 break;
1740 case 4:
1741 return 4;
1742 break;
1743 default:
1744 zig_unreachable();
1745 }
1745 if (first_byte < 0b10000000) return 1;
1746 if ((first_byte & 0b11100000) == 0b11000000) return 2;
1747 if ((first_byte & 0b11110000) == 0b11100000) return 3;
1748 if ((first_byte & 0b11111000) == 0b11110000) return 4;
1749 zig_unreachable();
17461750}
17471751
17481752// Ported from std/unicode.zig
......@@ -2016,7 +2020,7 @@ Error os_self_exe_shared_libs(ZigList<Buf *> &paths) {
20162020
20172021Error os_file_open_rw(Buf *full_path, OsFile *out_file, OsFileAttr *attr, bool need_write, uint32_t mode) {
20182022#if defined(ZIG_OS_WINDOWS)
2019 PathSpace path_space = slice_to_prefixed_file_w({ (uint8_t*)buf_ptr(full_path), buf_len(full_path) });
2023 PathSpace path_space = slice_to_prefixed_file_w(buf_to_slice(full_path));
20202024 HANDLE result = CreateFileW(&path_space.data.items[0],
20212025 need_write ? (GENERIC_READ|GENERIC_WRITE) : GENERIC_READ,
20222026 need_write ? 0 : FILE_SHARE_READ,
......@@ -2121,7 +2125,7 @@ Error os_file_open_w(Buf *full_path, OsFile *out_file, OsFileAttr *attr, uint32_
21212125
21222126Error os_file_open_lock_rw(Buf *full_path, OsFile *out_file) {
21232127#if defined(ZIG_OS_WINDOWS)
2124 PathSpace path_space = slice_to_prefixed_file_w({ (uint8_t*)buf_ptr(full_path), buf_len(full_path) });
2128 PathSpace path_space = slice_to_prefixed_file_w(buf_to_slice(full_path));
21252129 for (;;) {
21262130 HANDLE result = CreateFileW(&path_space.data.items[0], GENERIC_READ | GENERIC_WRITE,
21272131 0, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);