scoop:scoop.platform:0.1.0
src/process/spawn.scoop
Selected production source · src/process/spawn.scoop
1
package scoop.platform.process3
import scoop.bytes.ByteArrayBuilder4
import scoop.collections.Map5
import scoop.platform.env.currentDirectory6
import scoop.platform.fs.*7
import scoop.platform.system.*9
internal class SpawnInput internal constructor(10
executable: Path,11
arguments: Array<OsString>,12
environment: Map<OsString, OsString>,13
directory: Path14
) {15
private val directory: Path = if (operatingSystem ==16
OperatingSystem.Windows &&17
!directory.isAbsolute18
)19
currentDirectory().resolve(directory)20
else21
directory22
private val argumentCount: Long = arguments.size + 1L23
private val environmentCount: Long = environment.size24
private val argumentBytes: Array<Byte> = packArguments(executable, arguments)25
private val environmentBytes: Array<Byte> = packEnvironment(environment)27
internal fun spawn(28
path: Path,29
input: Stdio,30
output: Stdio,31
error: Stdio,32
merge: Boolean,33
result: MutableArray<NativeFileHandle>34
): NativeSpawnResult = withRedirect(input) inputScope@{ inputFd ->35
withRedirect(output) outputScope@{ outputFd ->36
withRedirect(error) errorScope@{ errorFd ->37
val redirects: Array<NativeStdio> = [38
inputFd,39
outputFd,40
if (merge) NativeStdio(4, NativeFileHandle(ULong.MAX_VALUE)) else errorFd41
]42
return@errorScope withArguments(path) { executable, argv, argc, envp, envc, cwd ->43
@Unsafe44
{45
return@withArguments redirects46
.withDataPointer redirectScope@{ fds, _ ->47
@Unsafe48
{49
return@redirectScope result50
.withDataPointer { values, _ ->51
@Unsafe52
{53
return@withDataPointer nativeSpawn(54
executable, argv, argc, envp, envc, cwd, fds,55
values56
)57
}58
}59
}60
}61
}62
}63
}64
}65
}67
internal fun spawnPty(path: Path, columns: Int, rows: Int): NativePtyOpened = withArguments(68
path69
) { executable, argv, argc, envp, envc, cwd ->70
@Unsafe71
{72
return@withArguments nativeSpawnPty(73
executable, argv, argc, envp, envc, cwd, columns, rows74
)75
}76
}78
private fun <R> withArguments(79
path: Path,80
start: (Ptr<Byte>, Ptr<Byte>, Long, Ptr<Byte>, Long, Ptr<Byte>) -> R81
): R {82
val resolved = if (operatingSystem == OperatingSystem.Windows)83
directory.resolve(path)84
else85
path86
@Unsafe87
{88
return resolved89
.withPlatformPath pathScope@{ executable ->90
@Unsafe91
{92
return@pathScope directory93
.withPlatformPath directoryScope@{ cwd ->94
@Unsafe95
{96
return@directoryScope argumentBytes97
.withDataPointer argumentScope@{ argv, _ ->98
@Unsafe99
{100
return@argumentScope environmentBytes101
.withDataPointer { envp, _ ->102
start(103
executable,104
argv,105
argumentCount,106
envp,107
environmentCount,108
cwd109
)110
}111
}112
}113
}114
}115
}116
}117
}118
}119
}121
private fun <R> withRedirect(value: Stdio, block: (NativeStdio) -> R): R = when (value) {122
case Stdio.Inherit -> block(NativeStdio(0, NativeFileHandle(ULong.MAX_VALUE)))123
case Stdio.Null -> block(NativeStdio(1, NativeFileHandle(ULong.MAX_VALUE)))124
case Stdio.Pipe -> block(NativeStdio(2, NativeFileHandle(ULong.MAX_VALUE)))125
case Stdio.Redirect(val file) -> file.withHandle { handle -> block(NativeStdio(3, handle)) }126
}128
private fun appendString(builder: ByteArrayBuilder, value: OsString, terminate: Boolean = true) {129
val bytes = value.toByteArray()130
for (byte in bytes) {131
if (byte == 0) throw IllegalArgumentException(Some("process string contains NUL"))132
}133
if (operatingSystem == OperatingSystem.Windows) {134
for (unit in value.toUtf16()) {135
builder.append(unit.toByte())136
builder.append((unit.toUInt() shr 8L).toByte())137
}138
if (terminate) builder.append(0.toByte())139
} else {140
builder.append(bytes)141
}142
if (terminate) builder.append(0.toByte())143
}145
private fun packArguments(executable: Path, arguments: Array<OsString>): Array<Byte> {146
if (executable.toOsString().toByteArray().size == 0L)147
throw IllegalArgumentException(Some("empty executable"))148
if (arguments.size == Long.MAX_VALUE) throw ArithmeticException()149
val builder = ByteArrayBuilder()150
appendString(builder, executable.toOsString())151
for (argument in arguments)152
appendString(builder, argument)153
return builder.toByteArray()154
}156
private fun packEnvironment(environment: Map<OsString, OsString>): Array<Byte> {157
val builder = ByteArrayBuilder()158
for ((name, value) in environment) {159
val bytes = name.toByteArray()160
if (bytes.size == 0L) throw IllegalArgumentException(Some("empty environment name"))161
for (byte in bytes) {162
if (byte == 0 || byte == 61)163
throw IllegalArgumentException(Some("invalid environment name"))164
}165
appendString(builder, name, false)166
appendString(builder, OsString.fromString("="), false)167
appendString(builder, value)168
}169
return builder.toByteArray()170
}172
@Extern(name = "scoop_platform_process_spawn")173
private fun nativeSpawn(174
executable: Ptr<Byte>,175
arguments: Ptr<Byte>,176
argumentCount: Long,177
environment: Ptr<Byte>,178
environmentCount: Long,179
directory: Ptr<Byte>,180
redirects: Ptr<NativeStdio>,181
result: Ptr<NativeFileHandle>182
): NativeSpawnResult184
@Extern(name = "scoop_platform_pty_spawn")185
private fun nativeSpawnPty(186
executable: Ptr<Byte>,187
arguments: Ptr<Byte>,188
argumentCount: Long,189
environment: Ptr<Byte>,190
environmentCount: Long,191
directory: Ptr<Byte>,192
columns: Int,193
rows: Int194
): NativePtyOpened196
@CLayout197
private struct NativeStdio(val kind: Int, val handle: NativeFileHandle)