@@ -9,7 +9,8 @@ struct TerminalDescriptor
99 HANDLE handle ;
1010};
1111
12- typedef struct {
12+ typedef struct
13+ {
1314 TerminalDescriptor descriptor ;
1415 DWORD original_mode ;
1516 UINT original_code_page ;
@@ -38,7 +39,6 @@ static HANDLE open_console_handle(const wchar_t *nonnull name)
3839 SECURITY_ATTRIBUTES attrs =
3940 {
4041 .nLength = sizeof (SECURITY_ATTRIBUTES ),
41- .lpSecurityDescriptor = nullptr ,
4242 .bInheritHandle = true,
4343 };
4444
@@ -310,14 +310,10 @@ TerminalResult cathode_generate_signal(TerminalSignal signal)
310310 };
311311}
312312
313- TerminalResult cathode_read (
314- const TerminalDescriptor * nonnull descriptor , uint8_t * nullable buffer , int32_t length , int32_t * nonnull progress )
313+ static TerminalResult create_io_result (BOOL result , const int32_t * nonnull progress )
315314{
316- assert (descriptor );
317- assert (buffer );
318315 assert (progress );
319316
320- BOOL result = ReadFile (descriptor -> handle , buffer , (DWORD )length , (LPDWORD )progress , nullptr );
321317 DWORD error = GetLastError ();
322318
323319 // See driver-unix.c for the error handling rationale.
@@ -326,39 +322,43 @@ TerminalResult cathode_read(
326322 {
327323 .exception = TerminalException_None ,
328324 }
329- : (TerminalResult )
330- {
331- .exception = TerminalException_Terminal ,
332- .message = u"Could not read from input handle." ,
333- .error = (int32_t )error ,
334- };
325+ : error == ERROR_OPERATION_ABORTED
326+ ? (TerminalResult )
327+ {
328+ .exception = TerminalException_OperationCanceled ,
329+ }
330+ : (TerminalResult )
331+ {
332+ .exception = TerminalException_Terminal ,
333+ .message = u"Could not read from input handle." ,
334+ .error = (int32_t )error ,
335+ };
336+ }
337+
338+ TerminalResult cathode_read (
339+ TerminalDescriptor * nonnull descriptor , uint8_t * nullable buffer , int32_t length , int32_t * nonnull progress )
340+ {
341+ assert (descriptor );
342+ assert (buffer );
343+ assert (progress );
344+
345+ return create_io_result (ReadFile (descriptor -> handle , buffer , (DWORD )length , (LPDWORD )progress , nullptr ), progress );
335346}
336347
337348TerminalResult cathode_write (
338- const TerminalDescriptor * nonnull descriptor ,
339- const uint8_t * nullable buffer ,
340- int32_t length ,
341- int32_t * nonnull progress )
349+ TerminalDescriptor * nonnull descriptor , const uint8_t * nullable buffer , int32_t length , int32_t * nonnull progress )
342350{
343351 assert (descriptor );
344352 assert (buffer );
345353 assert (progress );
346354
347- BOOL result = WriteFile (descriptor -> handle , buffer , (DWORD )length , (LPDWORD )progress , nullptr );
348- DWORD error = GetLastError ();
355+ return create_io_result ( WriteFile (descriptor -> handle , buffer , (DWORD )length , (LPDWORD )progress , nullptr ), progress );
356+ }
349357
350- // See driver-unix.c for the error handling rationale.
351- return result || * progress || error == ERROR_HANDLE_EOF || error == ERROR_BROKEN_PIPE || error == ERROR_NO_DATA
352- ? (TerminalResult )
353- {
354- .exception = TerminalException_None ,
355- }
356- : (TerminalResult )
357- {
358- .exception = TerminalException_Terminal ,
359- .message = u"Could not write to output handle." ,
360- .error = (int32_t )error ,
361- };
358+ void cathode_cancel (TerminalDescriptor * nonnull descriptor )
359+ {
360+ // This is a best-effort situation; nothing we can do if this fails.
361+ CancelIoEx (descriptor -> handle , nullptr );
362362}
363363
364364#endif
0 commit comments