Initial Commit
This commit is contained in:
706 files changed
+135430
No files matched your search
@@ -0,0 +1,213 @@
|
||||
#! @file windows.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains what the runtime needs of Windows: the functions of the system it
|
||||
#! calls, and how an executable finds the arguments and variables it started
|
||||
#! with.
|
||||
#!
|
||||
#! Windows has no system calls a program makes itself. The runtime calls the
|
||||
#! functions of kernel32 instead, as C functions the executable imports, and
|
||||
#! those of the library Windows names for the waits on an address, which
|
||||
#! `system::windows` declares for everything that reaches the system. The other
|
||||
#! files of the runtime declare their functions that call the system once for
|
||||
#! each system, in sections of their own.
|
||||
|
||||
import system::windows;
|
||||
|
||||
module runtime when backend == native && os == windows;
|
||||
|
||||
|
||||
# What the functions of the system are given is written where they are called,
|
||||
# as numbers, since `system_start` and what it calls run before the globals,
|
||||
# constants among them, are set: UTF-8's code page is 65001, memory is
|
||||
# committed with 0x1000, reserved with 0x2000, and released with 0x8000, and it
|
||||
# is read and written with 0x04, or not at all with 0x01.
|
||||
|
||||
#! Finds the arguments and variables the program started with, before anything
|
||||
#! else runs, as Linux leaves them on the first thread's stack: views of their
|
||||
#! texts in a run of words, the arguments, a zero, the variables, and a zero,
|
||||
#! which the state's `argv` points at and whose arguments its `argc` counts.
|
||||
#! Windows gives them in UTF-16, the arguments as one line, so they are
|
||||
#! converted to UTF-8, and the line split as Microsoft's C library splits it.
|
||||
#! Every block is mapped and kept for as long as the program runs.
|
||||
#! @remarks It runs before the globals are set, and before a trap could be
|
||||
#! reported, so it exits with 2 where there is no memory, as the start
|
||||
#! does.
|
||||
def system_start(): void {
|
||||
# A failure of the program nothing reports ends it, as one would on Linux,
|
||||
# rather than asking the user what to do: SEM_FAILCRITICALERRORS,
|
||||
# SEM_NOGPFAULTERRORBOX, and SEM_NOOPENFILEERRORBOX.
|
||||
SetErrorMode(0x8003);
|
||||
|
||||
val line := GetCommandLineW();
|
||||
var units := 0u;
|
||||
while line[units] != 0 {
|
||||
units++;
|
||||
}
|
||||
val line_room := units * 3 + 1;
|
||||
val line_text := start_map(line_room);
|
||||
var length := 0u;
|
||||
if units > 0 {
|
||||
length = WideCharToMultiByte(65001, 0, line, units as i32, line_text, line_room as i32, null, null) as u64;
|
||||
}
|
||||
# Splitting gives no more bytes than the line has, and a zero after each argument.
|
||||
val arguments := start_map(2 * length + 2);
|
||||
val argc := split_arguments(line_text, arguments);
|
||||
|
||||
# The variables end at an empty one, and those Windows keeps of each drive,
|
||||
# named from `=`, are left out.
|
||||
val block := GetEnvironmentStringsW();
|
||||
var total := 0u;
|
||||
while block != null && (block[total] != 0 || block[total + 1] != 0) {
|
||||
total++;
|
||||
}
|
||||
val variables_room := total * 3 + 2;
|
||||
val variables := start_map(variables_room);
|
||||
var bytes := 0u;
|
||||
if total > 0 {
|
||||
bytes = WideCharToMultiByte(65001, 0, block, (total + 1) as i32, variables, variables_room as i32, null, null) as u64;
|
||||
}
|
||||
if block != null {
|
||||
FreeEnvironmentStringsW(block);
|
||||
}
|
||||
var envc := 0u;
|
||||
var at := 0u;
|
||||
while at < bytes && variables[at] != 0 {
|
||||
if variables[at] != '=' as u8 {
|
||||
envc++;
|
||||
}
|
||||
while variables[at] != 0 {
|
||||
at++;
|
||||
}
|
||||
at++;
|
||||
}
|
||||
|
||||
val words := start_map((argc + envc + 2) * 8) as *mut u64;
|
||||
var word := 0u;
|
||||
at = 0;
|
||||
for i in 0..argc {
|
||||
words[word] = (arguments + at) as u64;
|
||||
word++;
|
||||
while arguments[at] != 0 {
|
||||
at++;
|
||||
}
|
||||
at++;
|
||||
}
|
||||
word++;
|
||||
at = 0;
|
||||
while at < bytes && variables[at] != 0 {
|
||||
if variables[at] != '=' as u8 {
|
||||
words[word] = (variables + at) as u64;
|
||||
word++;
|
||||
}
|
||||
while variables[at] != 0 {
|
||||
at++;
|
||||
}
|
||||
at++;
|
||||
}
|
||||
|
||||
val first := (*executable()).state;
|
||||
(*first).argc = argc;
|
||||
(*first).argv = words;
|
||||
}
|
||||
|
||||
# Maps zeroed memory for `system_start`, exiting with 2 where there is none.
|
||||
def start_map(size: u64): *mut u8 {
|
||||
val address := VirtualAlloc(0, size, 0x3000, 0x04);
|
||||
if address == 0 {
|
||||
ExitProcess(2);
|
||||
}
|
||||
return address as *mut u8;
|
||||
}
|
||||
|
||||
# Splits a command line into arguments as Microsoft's C library does, writing
|
||||
# each into `into`, which is zeroed, with a zero after it, and gives how many
|
||||
# there are. The path of the program ends at the first space or tab outside
|
||||
# quotes, and quotes only begin and end the parts of it within them. In each
|
||||
# argument after it, quotes do too, but within quotes two are one, and
|
||||
# backslashes are themselves but before a quote, where each two are one, and
|
||||
# one left over makes the quote one.
|
||||
def split_arguments(line: *u8, into: *mut u8): u64 {
|
||||
var at := 0u;
|
||||
var out := 0u;
|
||||
var quoted := false;
|
||||
while line[at] != 0 && (quoted || (line[at] != ' ' as u8 && line[at] != '\t' as u8)) {
|
||||
if line[at] == '"' as u8 {
|
||||
quoted = !quoted;
|
||||
} else {
|
||||
into[out] = line[at];
|
||||
out++;
|
||||
}
|
||||
at++;
|
||||
}
|
||||
out++;
|
||||
var count := 1u;
|
||||
|
||||
quoted = false;
|
||||
while true {
|
||||
while line[at] == ' ' as u8 || line[at] == '\t' as u8 {
|
||||
at++;
|
||||
}
|
||||
break if line[at] == 0;
|
||||
while true {
|
||||
var kept := true;
|
||||
var backslash := 0u;
|
||||
while line[at] == '\\' as u8 {
|
||||
at++;
|
||||
backslash++;
|
||||
}
|
||||
if line[at] == '"' as u8 {
|
||||
if backslash % 2 == 0 {
|
||||
if quoted && line[at + 1] == '"' as u8 {
|
||||
at++;
|
||||
} else {
|
||||
kept = false;
|
||||
quoted = !quoted;
|
||||
}
|
||||
}
|
||||
backslash = backslash / 2;
|
||||
}
|
||||
while backslash > 0 {
|
||||
into[out] = '\\' as u8;
|
||||
out++;
|
||||
backslash--;
|
||||
}
|
||||
break if line[at] == 0 || (!quoted && (line[at] == ' ' as u8 || line[at] == '\t' as u8));
|
||||
if kept {
|
||||
into[out] = line[at];
|
||||
out++;
|
||||
}
|
||||
at++;
|
||||
}
|
||||
out++;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
# Writes text to a console, which takes UTF-16, converting it a part at a time
|
||||
# in memory of its own, or giving up where there is none, and gives whether it
|
||||
# did.
|
||||
def write_console(handle: u64, data: *u8, length: u64): bool {
|
||||
val part := 16384u;
|
||||
val units := VirtualAlloc(0, part * 2, 0x3000, 0x04) as *mut u16;
|
||||
return false if units == null;
|
||||
var at := 0u;
|
||||
while at < length {
|
||||
# A part ends before the bytes after the first of a character, never within it.
|
||||
var end := length - at > part ? at + part : length;
|
||||
while end < length && end > at && (data[end] & 0xC0) == 0x80 {
|
||||
end--;
|
||||
}
|
||||
val count := MultiByteToWideChar(65001, 0, data + at, (end - at) as i32, units, part as i32);
|
||||
var done: u32 = 0;
|
||||
if count <= 0 || WriteConsoleW(handle, units, count as u32, &mut done, null) == 0 {
|
||||
break;
|
||||
}
|
||||
at = end;
|
||||
}
|
||||
VirtualFree(units as u64, 0, 0x8000);
|
||||
return true;
|
||||
}
|
||||
Reference in new issue
Block a user