-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathflutter_pty_win.c
More file actions
468 lines (343 loc) · 9.88 KB
/
Copy pathflutter_pty_win.c
File metadata and controls
468 lines (343 loc) · 9.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include <stdio.h>
#include <Windows.h>
#include "flutter_pty.h"
#include "include/dart_api.h"
#include "include/dart_api_dl.h"
#include "include/dart_native_api.h"
static LPWSTR build_command(char *executable, char **arguments)
{
int command_length = 0;
if (executable != NULL)
{
command_length += (int)strlen(executable);
}
if (arguments != NULL)
{
int i = 0;
while (arguments[i] != NULL)
{
command_length += (int)strlen(arguments[i]) + 1;
i++;
}
}
LPWSTR command = malloc((command_length + 1) * sizeof(WCHAR));
if (command != NULL)
{
int i = 0;
if (executable != NULL)
{
int j = 0;
while (executable[j] != 0)
{
command[i] = (WCHAR)executable[j];
i++;
j++;
}
}
if (arguments != NULL)
{
int j = 0;
while (arguments[j] != NULL)
{
command[i++] = ' ';
int k = 0;
while (arguments[j][k] != 0)
{
command[i] = (WCHAR)arguments[j][k];
i++;
k++;
}
j++;
}
}
command[i] = 0;
}
return command;
}
static LPWSTR build_environment(char **environment)
{
LPWSTR environment_block = NULL;
int environment_block_length = 0;
if (environment != NULL)
{
int i = 0;
while (environment[i] != NULL)
{
environment_block_length += (int)strlen(environment[i]) + 1;
i++;
}
}
environment_block = malloc((environment_block_length + 1) * sizeof(WCHAR));
if (environment_block != NULL)
{
int i = 0;
if (environment != NULL)
{
int j = 0;
while (environment[j] != NULL)
{
int k = 0;
while (environment[j][k] != 0)
{
environment_block[i] = (WCHAR)environment[j][k];
i++;
k++;
}
environment_block[i++] = 0;
j++;
}
}
environment_block[i] = 0;
}
return environment_block;
}
static LPWSTR build_working_directory(char *working_directory)
{
if (working_directory == NULL)
{
return NULL;
}
int working_directory_length = (int)strlen(working_directory);
LPWSTR working_directory_block = malloc((working_directory_length + 1) * sizeof(WCHAR));
if (working_directory_block == NULL)
{
return NULL;
}
int i = 0;
while (working_directory[i] != 0)
{
working_directory_block[i] = (WCHAR)working_directory[i++];
}
working_directory_block[i] = 0;
return working_directory_block;
}
typedef struct ReadLoopOptions
{
HANDLE fd;
Dart_Port port;
HANDLE hMutex;
BOOL ackRead;
} ReadLoopOptions;
static DWORD WINAPI read_loop(LPVOID arg)
{
ReadLoopOptions *options = (ReadLoopOptions *)arg;
char buffer[1024];
while (1)
{
DWORD readlen = 0;
if (options->ackRead)
{
WaitForSingleObject(options->hMutex, INFINITE);
}
BOOL ok = ReadFile(options->fd, buffer, sizeof(buffer), &readlen, NULL);
if (!ok)
{
break;
}
if (readlen <= 0)
{
break;
}
Dart_CObject result;
result.type = Dart_CObject_kTypedData;
result.value.as_typed_data.type = Dart_TypedData_kUint8;
result.value.as_typed_data.length = readlen;
result.value.as_typed_data.values = (uint8_t *)buffer;
Dart_PostCObject_DL(options->port, &result);
}
return 0;
}
static void start_read_thread(HANDLE fd, Dart_Port port, HANDLE mutex, BOOL ackRead)
{
ReadLoopOptions *options = malloc(sizeof(ReadLoopOptions));
options->fd = fd;
options->port = port;
options->hMutex = mutex;
options->ackRead = ackRead;
DWORD thread_id;
HANDLE thread = CreateThread(NULL, 0, read_loop, options, 0, &thread_id);
if (thread == NULL)
{
free(options);
}
}
typedef struct WaitExitOptions
{
HANDLE pid;
Dart_Port port;
HANDLE hMutex;
} WaitExitOptions;
static DWORD WINAPI wait_exit_thread(LPVOID arg)
{
WaitExitOptions *options = (WaitExitOptions *)arg;
DWORD exit_code = 0;
WaitForSingleObject(options->pid, INFINITE);
GetExitCodeProcess(options->pid, &exit_code);
CloseHandle(options->pid);
CloseHandle(options->hMutex);
Dart_PostInteger_DL(options->port, exit_code);
return 0;
}
static void start_wait_exit_thread(HANDLE pid, Dart_Port port, HANDLE mutex)
{
WaitExitOptions *options = malloc(sizeof(WaitExitOptions));
options->pid = pid;
options->port = port;
options->hMutex = mutex;
DWORD thread_id;
HANDLE thread = CreateThread(NULL, 0, wait_exit_thread, options, 0, &thread_id);
if (thread == NULL)
{
free(options);
}
}
typedef struct PtyHandle
{
PHANDLE inputWriteSide;
PHANDLE outputReadSide;
HPCON hPty;
DWORD dwProcessId;
BOOL ackRead;
HANDLE hMutex;
} PtyHandle;
char *error_message = NULL;
FFI_PLUGIN_EXPORT PtyHandle *pty_create(PtyOptions *options)
{
HANDLE inputReadSide = NULL;
HANDLE inputWriteSide = NULL;
HANDLE outputReadSide = NULL;
HANDLE outputWriteSide = NULL;
if (!CreatePipe(&inputReadSide, &inputWriteSide, NULL, 0))
{
error_message = "Failed to create input pipe";
return NULL;
}
if (!CreatePipe(&outputReadSide, &outputWriteSide, NULL, 0))
{
error_message = "Failed to create output pipe";
return NULL;
}
COORD size;
size.X = options->cols;
size.Y = options->rows;
HPCON hPty;
HRESULT result = CreatePseudoConsole(size, inputReadSide, outputWriteSide, 0, &hPty);
if (FAILED(result))
{
error_message = "Failed to create pseudo console";
return NULL;
}
STARTUPINFOEX startupInfo;
ZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.StartupInfo.cb = sizeof(startupInfo);
startupInfo.StartupInfo.dwFlags = STARTF_USESTDHANDLES;
startupInfo.StartupInfo.hStdInput = NULL;
startupInfo.StartupInfo.hStdOutput = NULL;
startupInfo.StartupInfo.hStdError = NULL;
SIZE_T bytesRequired;
InitializeProcThreadAttributeList(NULL, 1, 0, &bytesRequired);
startupInfo.lpAttributeList = (PPROC_THREAD_ATTRIBUTE_LIST)malloc(bytesRequired);
BOOL ok = InitializeProcThreadAttributeList(startupInfo.lpAttributeList, 1, 0, &bytesRequired);
if (!ok)
{
error_message = "Failed to initialize proc thread attribute list";
return NULL;
}
ok = UpdateProcThreadAttribute(startupInfo.lpAttributeList,
0,
PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE,
hPty,
sizeof(hPty),
NULL,
NULL);
if (!ok)
{
error_message = "Failed to update proc thread attribute list";
return NULL;
}
LPWSTR command = build_command(options->executable, options->arguments);
LPWSTR environment_block = build_environment(options->environment);
LPWSTR working_directory = build_working_directory(options->working_directory);
PROCESS_INFORMATION processInfo;
ZeroMemory(&processInfo, sizeof(processInfo));
ok = CreateProcessW(NULL,
command,
NULL,
NULL,
FALSE,
EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT,
environment_block,
working_directory,
&startupInfo.StartupInfo,
&processInfo);
if (command != NULL)
{
free(command);
}
if (environment_block != NULL)
{
free(environment_block);
}
if (working_directory != NULL)
{
free(working_directory);
}
if (!ok)
{
error_message = "Failed to create process";
DWORD error = GetLastError();
printf("error no: %d\n", error);
return NULL;
}
// free(startupInfo.lpAttributeList);
// CloseHandle(processInfo.hThread);
HANDLE mutex = CreateSemaphore(
NULL, // default security attributes
1, // initial count
1, // maximum count
NULL);
start_read_thread(outputReadSide, options->stdout_port, mutex, options->ackRead);
start_wait_exit_thread(processInfo.hProcess, options->exit_port, mutex);
PtyHandle *pty = malloc(sizeof(PtyHandle));
if (pty == NULL)
{
error_message = "Failed to allocate pty handle";
return NULL;
}
pty->inputWriteSide = inputWriteSide;
pty->outputReadSide = outputReadSide;
pty->hPty = hPty;
pty->dwProcessId = processInfo.dwProcessId;
pty->ackRead = options->ackRead;
pty->hMutex = mutex;
return pty;
}
FFI_PLUGIN_EXPORT void pty_write(PtyHandle *handle, char *buffer, int length)
{
DWORD bytesWritten;
WriteFile(handle->inputWriteSide, buffer, length, &bytesWritten, NULL);
FlushFileBuffers(handle->inputWriteSide);
return;
}
FFI_PLUGIN_EXPORT void pty_ack_read(PtyHandle *handle)
{
if (handle->ackRead)
{
ReleaseSemaphore(handle->hMutex, 1, NULL);
}
}
FFI_PLUGIN_EXPORT int pty_resize(PtyHandle *handle, int rows, int cols)
{
COORD size;
size.X = cols;
size.Y = rows;
return ResizePseudoConsole(handle->hPty, size);
}
FFI_PLUGIN_EXPORT int pty_getpid(PtyHandle *handle)
{
return (int)handle->dwProcessId;
}
FFI_PLUGIN_EXPORT char *pty_error()
{
return error_message;
}