Scoop API

scoop:scoop.platform:0.1.0

src/process/spawn.scoop

Selected production source · src/process/spawn.scoop

1package scoop.platform.process
3import scoop.bytes.ByteArrayBuilder
4import scoop.collections.Map
5import scoop.platform.env.currentDirectory
6import scoop.platform.fs.*
7import scoop.platform.system.*
9internal class SpawnInput internal constructor(
10 executable: Path,
11 arguments: Array<OsString>,
12 environment: Map<OsString, OsString>,
13 directory: Path
14) {
15 private val directory: Path = if (operatingSystem ==
16 OperatingSystem.Windows &&
17 !directory.isAbsolute
18 )
19 currentDirectory().resolve(directory)
20 else
21 directory
22 private val argumentCount: Long = arguments.size + 1L
23 private val environmentCount: Long = environment.size
24 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 errorFd
41 ]
42 return@errorScope withArguments(path) { executable, argv, argc, envp, envc, cwd ->
43 @Unsafe
44 {
45 return@withArguments redirects
46 .withDataPointer redirectScope@{ fds, _ ->
47 @Unsafe
48 {
49 return@redirectScope result
50 .withDataPointer { values, _ ->
51 @Unsafe
52 {
53 return@withDataPointer nativeSpawn(
54 executable, argv, argc, envp, envc, cwd, fds,
55 values
56 )
57 }
58 }
59 }
60 }
61 }
62 }
63 }
64 }
65 }
67 internal fun spawnPty(path: Path, columns: Int, rows: Int): NativePtyOpened = withArguments(
68 path
69 ) { executable, argv, argc, envp, envc, cwd ->
70 @Unsafe
71 {
72 return@withArguments nativeSpawnPty(
73 executable, argv, argc, envp, envc, cwd, columns, rows
74 )
75 }
76 }
78 private fun <R> withArguments(
79 path: Path,
80 start: (Ptr<Byte>, Ptr<Byte>, Long, Ptr<Byte>, Long, Ptr<Byte>) -> R
81 ): R {
82 val resolved = if (operatingSystem == OperatingSystem.Windows)
83 directory.resolve(path)
84 else
85 path
86 @Unsafe
87 {
88 return resolved
89 .withPlatformPath pathScope@{ executable ->
90 @Unsafe
91 {
92 return@pathScope directory
93 .withPlatformPath directoryScope@{ cwd ->
94 @Unsafe
95 {
96 return@directoryScope argumentBytes
97 .withDataPointer argumentScope@{ argv, _ ->
98 @Unsafe
99 {
100 return@argumentScope environmentBytes
101 .withDataPointer { envp, _ ->
102 start(
103 executable,
104 argv,
105 argumentCount,
106 envp,
107 environmentCount,
108 cwd
109 )
110 }
111 }
112 }
113 }
114 }
115 }
116 }
117 }
118 }
121private 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)) }
128private 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())
145private 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()
156private 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()
172@Extern(name = "scoop_platform_process_spawn")
173private 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): NativeSpawnResult
184@Extern(name = "scoop_platform_pty_spawn")
185private 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: Int
194): NativePtyOpened
196@CLayout
197private struct NativeStdio(val kind: Int, val handle: NativeFileHandle)