74
74
| ENABLE_EXTENDED_FLAGS | ENABLE_QUICK_EDIT_MODE | ENABLE_INSERT_MODE
75
75
76
76
static HANDLE Std_Out = 0 ;
77
+ static HANDLE Std_Err = 0 ;
77
78
static HANDLE Std_Inp = 0 ;
78
79
static HANDLE Std_Echo = 0 ;
79
80
static REBCHR * Std_Buf = 0 ; // for input and output
@@ -426,13 +427,14 @@ static void Close_StdIO_Local(void)
426
427
const REBYTE * bp ;
427
428
const REBYTE * cp ;
428
429
const REBYTE * ep ;
430
+ HANDLE hOutput = Std_Err ? Std_Err : Std_Out ;
429
431
430
432
if (GET_FLAG (req -> modes , RDM_NULL )) {
431
433
req -> actual = req -> length ;
432
434
return DR_DONE ;
433
435
}
434
436
435
- if (Std_Out ) {
437
+ if (hOutput ) {
436
438
437
439
bp = req -> data ;
438
440
ep = bp + req -> length ;
@@ -447,11 +449,11 @@ static void Close_StdIO_Local(void)
447
449
448
450
if (Redir_Out ) { // for Console SubSystem (always UTF-8)
449
451
if (cp ) {
450
- ok = WriteFile (Std_Out , bp , cp - bp , & total , 0 );
452
+ ok = WriteFile (hOutput , bp , cp - bp , & total , 0 );
451
453
bp = Parse_ANSI_sequence (cp , ep );
452
454
}
453
455
else {
454
- ok = WriteFile (Std_Out , bp , ep - bp , & total , 0 );
456
+ ok = WriteFile (hOutput , bp , ep - bp , & total , 0 );
455
457
bp = ep ;
456
458
}
457
459
if (!ok ) {
@@ -469,7 +471,7 @@ static void Close_StdIO_Local(void)
469
471
bp = ep ;
470
472
}
471
473
if (len > 0 ) {// no error
472
- ok = WriteConsoleW (Std_Out , Std_Buf , len , & total , 0 );
474
+ ok = WriteConsoleW (hOutput , Std_Buf , len , & total , 0 );
473
475
if (!ok ) {
474
476
req -> error = GetLastError ();
475
477
return DR_ERROR ;
@@ -484,7 +486,7 @@ static void Close_StdIO_Local(void)
484
486
} else {
485
487
// using MS built in ANSI processing
486
488
if (Redir_Out ) { // Always UTF-8
487
- ok = WriteFile (Std_Out , req -> data , req -> length , & total , 0 );
489
+ ok = WriteFile (hOutput , req -> data , req -> length , & total , 0 );
488
490
}
489
491
else {
490
492
// Convert UTF-8 buffer to Win32 wide-char format for console.
@@ -494,7 +496,7 @@ static void Close_StdIO_Local(void)
494
496
// because its UTF-8 with variable char sizes.
495
497
len = MultiByteToWideChar (CP_UTF8 , 0 , cs_cast (req -> data ), req -> length , Std_Buf , BUF_SIZE );
496
498
if (len > 0 ) // no error
497
- ok = WriteConsoleW (Std_Out , Std_Buf , len , & total , 0 );
499
+ ok = WriteConsoleW (hOutput , Std_Buf , len , & total , 0 );
498
500
}
499
501
}
500
502
req -> actual = req -> length ; // do not use "total" (can be byte or wide)
@@ -550,9 +552,10 @@ static void Close_StdIO_Local(void)
550
552
// CTRL-C pressed
551
553
Handled_Break = TRUE; // let the break handler (which is signaled later) to know,
552
554
// that we handled it already
553
- SetConsoleTextAttribute (Std_Out , FOREGROUND_INTENSITY | FOREGROUND_MAGENTA );
554
- WriteConsoleW (Std_Out , L"[ESC]\r\n" , 7 , NULL , 0 );
555
- SetConsoleTextAttribute (Std_Out , FOREGROUND_GREY );
555
+ HANDLE hErr = GetStdHandle (STD_ERROR_HANDLE );
556
+ SetConsoleTextAttribute (hErr , FOREGROUND_INTENSITY | FOREGROUND_MAGENTA );
557
+ WriteConsoleW (hErr , L"[ESC]\r\n" , 7 , NULL , 0 );
558
+ SetConsoleTextAttribute (hErr , FOREGROUND_GREY );
556
559
req -> data [0 ] = '\x1B' ; // ESC char
557
560
req -> actual = 1 ;
558
561
return DR_DONE ;
@@ -600,7 +603,7 @@ static void Close_StdIO_Local(void)
600
603
REBEVT evt ;
601
604
DWORD cNumRead , i , repeat ;
602
605
INPUT_RECORD irInBuf [8 ];
603
- if (ReadConsoleInput (GetStdHandle ( STD_INPUT_HANDLE ) , irInBuf , 8 , & cNumRead )) {
606
+ if (ReadConsoleInput (Std_Inp , irInBuf , 8 , & cNumRead )) {
604
607
//printf("cNumRead: %u\n", cNumRead);
605
608
for (i = 0 ; i < cNumRead ; i ++ )
606
609
{
@@ -732,7 +735,7 @@ static void Close_StdIO_Local(void)
732
735
case MODE_CONSOLE_LINE :
733
736
Set_Input_Mode (ENABLE_LINE_INPUT | ENABLE_QUICK_EDIT_MODE | ENABLE_PROCESSED_INPUT , value );
734
737
Set_Input_Mode (ENABLE_MOUSE_INPUT , !value );
735
- if ( req -> modify . value ) {
738
+ if ( value ) {
736
739
SET_FLAG (req -> modes , RDM_READ_LINE );
737
740
}
738
741
else {
@@ -741,6 +744,9 @@ static void Close_StdIO_Local(void)
741
744
SET_FLAG (Devices [1 ]-> flags , RDO_AUTO_POLL );
742
745
}
743
746
break ;
747
+ case MODE_CONSOLE_ERROR :
748
+ Std_Err = value ? GetStdHandle (STD_ERROR_HANDLE ) : 0 ;
749
+ break ;
744
750
}
745
751
return DR_DONE ;
746
752
}
0 commit comments