Skip to content

Commit 3d83cac

Browse files
committed
FEAT: errors and debug messages printed to stderr
related to: Oldes/Rebol-issues#1862
1 parent c8f511d commit 3d83cac

File tree

12 files changed

+58
-37
lines changed

12 files changed

+58
-37
lines changed

src/core/a-lib.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ extern int Do_Callback(REBSER *obj, u32 name, RXIARG *args, RXIARG *result);
340340
val = Do_String(text->data, flags);
341341
UNSAVE_SERIES(text);
342342
if (IS_ERROR(val)) // && (VAL_ERR_NUM(val) != RE_QUIT)) {
343-
Print_Value(val, 1000, FALSE);
343+
Print_Value(val, 1000, FALSE, TRUE);
344344

345345
if (result) {
346346
*result = Value_To_RXI(val);
@@ -457,7 +457,7 @@ extern int Do_Callback(REBSER *obj, u32 name, RXIARG *args, RXIARG *result);
457457
if (SET_JUMP(state)) {
458458
POP_STATE(state, Saved_State);
459459
Catch_Error(DS_NEXT); // Stores error value here
460-
Out_Value(DS_NEXT, 0, FALSE, 0); // error
460+
Out_Value(DS_NEXT, 0, FALSE, 0, TRUE); // error
461461
DSP = 0;
462462
return;
463463
}
@@ -469,25 +469,25 @@ extern int Do_Callback(REBSER *obj, u32 name, RXIARG *args, RXIARG *result);
469469
if (IS_TYPESET(types) && TYPE_CHECK(types, VAL_TYPE(top))) {
470470
if (marker) {
471471
DS_SKIP; // protect `top` from modification
472-
Out_Str(marker, 0);
472+
Out_Str(marker, 0, FALSE);
473473
DS_DROP;
474474
}
475-
Out_Value(top, 500, TRUE, 1); // limit, molded
475+
Out_Value(top, 500, TRUE, 1, FALSE); // limit, molded
476476
}
477477
// else {
478478
// Out_Str(Get_Type_Name(top), 1);
479479
// }
480480
} else {
481481
if (VAL_ERR_NUM(top) != RE_HALT) {
482482
#ifdef COLOR_CONSOLE
483-
Out_Str(cb_cast("\x1B[1;35m"), 0);
484-
Out_Value(top, 640, FALSE, 0); // error FORMed
485-
Out_Str(cb_cast("\x1B[0m"), 0);
483+
Out_Str(cb_cast("\x1B[1;35m"), 0, TRUE);
484+
Out_Value(top, 640, FALSE, 0, TRUE); // error FORMed
485+
Out_Str(cb_cast("\x1B[0m"), 0, TRUE);
486486
#else
487487
Out_Value(top, 640, FALSE, 0); // error FORMed
488488
#endif
489489
// if (VAL_ERR_NUM(top) > RE_THROW_MAX) {
490-
// Out_Str("** Note: use WHY? for more about this error", 1);
490+
// Out_Str("** Note: use WHY? for more about this error", 1, TRUE);
491491
// }
492492
}
493493
}

src/core/b-init.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ extern const REBYTE Str_Banner[];
117117
***********************************************************************/
118118
{
119119
if (rargs->options & RO_VERS) {
120-
Debug_Fmt(Str_Banner, REBOL_VERSION);
120+
puts(REBOL_VERSION);
121121
OS_EXIT(0);
122122
}
123123
}

src/core/c-do.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2304,7 +2304,7 @@ xx*/ REBVAL *Do_Path(REBVAL **path_val, REBVAL *val)
23042304
OS_EXIT(VAL_INT32(VAL_ERR_VALUE(val))); // console quit
23052305
}
23062306
if (VAL_ERR_NUM(val) >= RE_THROW_MAX)
2307-
Print_Value(val, 1000, FALSE);
2307+
Print_Value(val, 1000, FALSE, TRUE);
23082308
}
23092309
return -1;
23102310
}

src/core/c-error.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ static REBOL_STATE Top_State; // Boot var: holds error state during boot
705705
POP_STATE(Top_State, Saved_State);
706706
DSP++; // Room for return value
707707
Catch_Error(DS_TOP); // Stores error value here
708-
Print_Value(DS_TOP, 0, FALSE);
708+
Print_Value(DS_TOP, 0, FALSE, TRUE);
709709
Crash(RP_NO_CATCH);
710710
}
711711
SET_STATE(Top_State, Saved_State);

src/core/c-port.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@
213213
while (wt) {
214214
if (GET_SIGNAL(SIG_ESCAPE)) {
215215
CLR_SIGNAL(SIG_ESCAPE);
216-
Out_Str(cb_cast("[ESC]"), 1);
216+
Out_Str(cb_cast("[ESC]"), 1, TRUE);
217217
Halt_Code(RE_HALT, 0); // Throws!
218218
}
219219

src/core/d-print.c

+23-18
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static REBREQ *Req_SIO;
7676

7777
/***********************************************************************
7878
**
79-
*/ static void Print_OS_Line(void)
79+
*/ static void Print_OS_Line(REBOOL err)
8080
/*
8181
** Print a new line.
8282
**
@@ -89,6 +89,9 @@ static REBREQ *Req_SIO;
8989
Req_SIO->length = 1;
9090
Req_SIO->actual = 0;
9191

92+
if (err)
93+
SET_FLAG(Req_SIO->flags, RRF_ERROR);
94+
9295
OS_DO_DEVICE(Req_SIO, RDC_WRITE);
9396

9497
if (Req_SIO->error) Crash(RP_IO_ERROR);
@@ -97,7 +100,7 @@ static REBREQ *Req_SIO;
97100

98101
/***********************************************************************
99102
**
100-
*/ static void Prin_OS_String(const REBYTE *bp, REBINT len, REBOOL uni)
103+
*/ static void Prin_OS_String(const REBYTE *bp, REBINT len, REBOOL uni, REBOOL err)
101104
/*
102105
** Print a string, but no line terminator or space.
103106
**
@@ -118,6 +121,8 @@ static REBREQ *Req_SIO;
118121
if (len == UNKNOWN) len = (REBINT)(uni ? wcslen((const wchar_t*)up) : LEN_BYTES(bp));
119122

120123
SET_FLAG(Req_SIO->flags, RRF_FLUSH);
124+
if (err)
125+
SET_FLAG(Req_SIO->flags, RRF_ERROR);
121126

122127
Req_SIO->actual = 0;
123128
Req_SIO->data = buf;
@@ -144,23 +149,23 @@ static REBREQ *Req_SIO;
144149

145150
/***********************************************************************
146151
**
147-
*/ void Out_Value(REBVAL *value, REBCNT limit, REBOOL mold, REBINT lines)
152+
*/ void Out_Value(REBVAL *value, REBCNT limit, REBOOL mold, REBINT lines, REBOOL err)
148153
/*
149154
***********************************************************************/
150155
{
151-
Print_Value(value, limit, mold); // higher level!
152-
for (; lines > 0; lines--) Print_OS_Line();
156+
Print_Value(value, limit, mold, err); // higher level!
157+
for (; lines > 0; lines--) Print_OS_Line(err);
153158
}
154159

155160

156161
/***********************************************************************
157162
**
158-
*/ void Out_Str(const REBYTE *bp, REBINT lines)
163+
*/ void Out_Str(const REBYTE *bp, REBINT lines, REBOOL err)
159164
/*
160165
***********************************************************************/
161166
{
162-
Prin_OS_String(bp, UNKNOWN, 0);
163-
for (; lines > 0; lines--) Print_OS_Line();
167+
Prin_OS_String(bp, UNKNOWN, 0, err);
168+
for (; lines > 0; lines--) Print_OS_Line(err);
164169
}
165170

166171

@@ -216,11 +221,11 @@ static REBREQ *Req_SIO;
216221
}
217222

218223
if (lines == 0) i += 2; // start of next line
219-
Prin_OS_String(BIN_SKIP(Trace_Buffer, i), tail-i, 0);
224+
Prin_OS_String(BIN_SKIP(Trace_Buffer, i), tail-i, 0, TRUE);
220225
//RESET_SERIES(Trace_Buffer);
221226
}
222227
else {
223-
Out_Str(cb_cast("backtrace not enabled"), 1);
228+
Out_Str(cb_cast("backtrace not enabled"), 1, TRUE);
224229
}
225230
}
226231

@@ -247,8 +252,8 @@ static REBREQ *Req_SIO;
247252
for (; lines > 0; lines--) Append_Byte(Trace_Buffer, LF);
248253
}
249254
else {
250-
Prin_OS_String(bp, len, uni);
251-
for (; lines > 0; lines--) Print_OS_Line();
255+
Prin_OS_String(bp, len, uni, TRUE);
256+
for (; lines > 0; lines--) Print_OS_Line(TRUE);
252257
}
253258
}
254259

@@ -387,7 +392,7 @@ static REBREQ *Req_SIO;
387392
/*
388393
***********************************************************************/
389394
{
390-
Print_Value(value, limit, mold); // higher level!
395+
Print_Value(value, limit, mold, TRUE); // higher level!
391396
}
392397

393398

@@ -797,29 +802,29 @@ static REBREQ *Req_SIO;
797802

798803
/***********************************************************************
799804
**
800-
*/ void Prin_Value(REBVAL *value, REBCNT limit, REBOOL mold)
805+
*/ void Prin_Value(REBVAL *value, REBCNT limit, REBOOL mold, REBOOL err)
801806
/*
802807
** Print a value or block's contents for user viewing.
803808
** Can limit output to a given size. Set limit to 0 for full size.
804809
**
805810
***********************************************************************/
806811
{
807812
REBSER *out = Mold_Print_Value(value, limit, mold);
808-
Prin_OS_String(out->data, out->tail, TRUE);
813+
Prin_OS_String(out->data, out->tail, TRUE, err);
809814
}
810815

811816

812817
/***********************************************************************
813818
**
814-
*/ void Print_Value(REBVAL *value, REBCNT limit, REBOOL mold)
819+
*/ void Print_Value(REBVAL *value, REBCNT limit, REBOOL mold, REBOOL err)
815820
/*
816821
** Print a value or block's contents for user viewing.
817822
** Can limit output to a given size. Set limit to 0 for full size.
818823
**
819824
***********************************************************************/
820825
{
821-
Prin_Value(value, limit, mold);
822-
Print_OS_Line();
826+
Prin_Value(value, limit, mold, err);
827+
Print_OS_Line(err);
823828
}
824829

825830

src/core/n-io.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ static REBSER *Read_All_File(char *fname)
166166
REBVAL *value = D_ARG(1);
167167

168168
if (IS_BLOCK(value)) Reduce_Block(VAL_SERIES(value), VAL_INDEX(value), 0);
169-
Print_Value(DS_TOP, NO_LIMIT, 0);
169+
Print_Value(DS_TOP, NO_LIMIT, 0, FALSE);
170170
return R_UNSET; // reloads ds
171171
}
172172

@@ -180,7 +180,7 @@ static REBSER *Read_All_File(char *fname)
180180
REBVAL *value = D_ARG(1);
181181

182182
if (IS_BLOCK(value)) Reduce_Block(VAL_SERIES(value), VAL_INDEX(value), 0);
183-
Prin_Value(DS_TOP, 0, 0);
183+
Prin_Value(DS_TOP, 0, 0, FALSE);
184184
return R_UNSET; // reloads ds
185185
}
186186

src/core/n-system.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ char *evoke_help = "Evoke values:\n"
227227
Crash(9999);
228228
break;
229229
default:
230-
Out_Str(cb_cast(evoke_help), 1);
230+
Out_Str(cb_cast(evoke_help), 1, FALSE);
231231
}
232232
}
233233
if (IS_INTEGER(arg)) {
@@ -246,7 +246,7 @@ char *evoke_help = "Evoke values:\n"
246246
Check_Bind_Table();
247247
break;
248248
default:
249-
Out_Str(cb_cast(evoke_help), 1);
249+
Out_Str(cb_cast(evoke_help), 1, FALSE);
250250
}
251251
}
252252
}

src/include/reb-device.h

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ enum {
9696
RRF_ALLOC, // Request is allocated, not a temp on stack
9797
RRF_WIDE, // Wide char IO
9898
RRF_ACTIVE, // Port is active, even no new events yet
99+
RRF_ERROR, // WRITE to std_err
99100
};
100101

101102
// REBOL Device Errors:

src/os/posix/dev-stdio.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,17 @@ static void Close_StdIO_Local(void)
189189
***********************************************************************/
190190
{
191191
long total;
192+
int output;
192193

193194
if (GET_FLAG(req->modes, RDM_NULL)) {
194195
req->actual = req->length;
195196
return DR_DONE;
196197
}
198+
output = GET_FLAG(req->flags, RRF_ERROR) ? STDERR_FILENO : Std_Out;
197199

198-
if (Std_Out >= 0) {
200+
if (output >= 0) {
199201

200-
total = write(Std_Out, req->data, req->length);
202+
total = write(output, req->data, req->length);
201203

202204
if (total < 0) {
203205
req->error = errno;

src/os/win32/dev-stdio.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -427,12 +427,19 @@ static void Close_StdIO_Local(void)
427427
const REBYTE *bp;
428428
const REBYTE *cp;
429429
const REBYTE *ep;
430-
HANDLE hOutput = Std_Err ? Std_Err : Std_Out;
430+
HANDLE hOutput;
431431

432432
if (GET_FLAG(req->modes, RDM_NULL)) {
433433
req->actual = req->length;
434434
return DR_DONE;
435435
}
436+
if (GET_FLAG(req->flags, RRF_ERROR)) {
437+
hOutput = GetStdHandle(STD_ERROR_HANDLE);
438+
}
439+
else {
440+
// use stderr if requested by user
441+
hOutput = Std_Err ? Std_Err : Std_Out;
442+
}
436443

437444
if (hOutput) {
438445

src/tests/units/call-test.r3

+6
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ rebol-cmd: func[cmd][
7777
--assert "13" = out-buffer
7878
--assert "2" = err-buffer
7979

80+
--test-- "Error printed to stderr"
81+
;@@ https://github.com/Oldes/Rebol-issues/issues/1862
82+
--assert -1 = rebol-cmd {--do "prin 1 1 / 0}
83+
--assert "1" = out-buffer
84+
--assert not none? find err-buffer "Math error"
85+
8086

8187
===end-group===
8288

0 commit comments

Comments
 (0)