-
-
Notifications
You must be signed in to change notification settings - Fork 648
/
error.c
4794 lines (4096 loc) · 132 KB
/
error.c
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Racket
Copyright (c) 2004-2018 PLT Design Inc.
Copyright (c) 1995-2001 Matthew Flatt
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA.
libscheme
Copyright (c) 1994 Brent Benson
All rights reserved.
*/
#include "schpriv.h"
#include "schrktio.h"
#include <ctype.h>
#ifdef DOS_FILE_SYSTEM
# include <windows.h>
#endif
#ifdef NO_ERRNO_GLOBAL
# define errno -1
#else
# include <errno.h>
#endif
#define mzVA_ARG(x, y) HIDE_FROM_XFORM(va_arg(x, y))
#define TMP_CMARK_VALUE scheme_parameterization_key
#ifndef INIT_SYSLOG_LEVEL
# define INIT_SYSLOG_LEVEL 0
#endif
/* globals */
SHARED_OK scheme_console_printf_t scheme_console_printf;
scheme_console_printf_t scheme_get_console_printf() { return scheme_console_printf; }
void scheme_set_console_printf(scheme_console_printf_t p) { scheme_console_printf = p; }
SHARED_OK Scheme_Exit_Proc scheme_exit;
void scheme_set_exit(Scheme_Exit_Proc p) { scheme_exit = p; }
HOOK_SHARED_OK void (*scheme_console_output)(char *str, intptr_t len);
void scheme_set_console_output(scheme_console_output_t p) { scheme_console_output = p; }
SHARED_OK static Scheme_Object *init_syslog_level = scheme_make_integer(INIT_SYSLOG_LEVEL);
SHARED_OK static Scheme_Object *init_stderr_level = scheme_make_integer(SCHEME_LOG_ERROR);
SHARED_OK static Scheme_Object *init_stdout_level = scheme_make_integer(0);
THREAD_LOCAL_DECL(static Scheme_Logger *scheme_main_logger);
THREAD_LOCAL_DECL(static Scheme_Logger *scheme_gc_logger);
THREAD_LOCAL_DECL(static Scheme_Logger *scheme_future_logger);
THREAD_LOCAL_DECL(static Scheme_Logger *scheme_place_logger);
/* readonly globals */
ROSYM static Scheme_Object *none_symbol;
ROSYM static Scheme_Object *fatal_symbol;
ROSYM static Scheme_Object *error_symbol;
ROSYM static Scheme_Object *warning_symbol;
ROSYM static Scheme_Object *info_symbol;
ROSYM static Scheme_Object *debug_symbol;
ROSYM static Scheme_Object *posix_symbol;
ROSYM static Scheme_Object *windows_symbol;
ROSYM static Scheme_Object *gai_symbol;
ROSYM static Scheme_Object *arity_property;
ROSYM static Scheme_Object *def_err_val_proc;
ROSYM static Scheme_Object *def_error_esc_proc;
ROSYM static Scheme_Object *default_display_handler;
ROSYM static Scheme_Object *emergency_display_handler;
ROSYM static Scheme_Object *def_exe_yield_proc;
READ_ONLY Scheme_Object *scheme_def_exit_proc;
READ_ONLY Scheme_Object *scheme_raise_arity_error_proc;
#ifdef MEMORY_COUNTING_ON
intptr_t scheme_misc_count;
#endif
#ifdef MZ_USE_MZRT
static mzrt_mutex *glib_log_queue_lock;
typedef struct glib_log_queue_entry {
const char *log_domain;
int log_level;
const char *message;
struct glib_log_queue_entry *next;
} glib_log_queue_entry;
static glib_log_queue_entry *glib_log_queue;
static void *glib_log_signal_handle;
#endif
/* locals */
static Scheme_Object *error(int argc, Scheme_Object *argv[]);
static Scheme_Object *raise_user_error(int argc, Scheme_Object *argv[]);
static Scheme_Object *raise_type_error(int argc, Scheme_Object *argv[]);
static Scheme_Object *raise_argument_error(int argc, Scheme_Object *argv[]);
static Scheme_Object *raise_result_error(int argc, Scheme_Object *argv[]);
static Scheme_Object *raise_mismatch_error(int argc, Scheme_Object *argv[]);
static Scheme_Object *raise_arguments_error(int argc, Scheme_Object *argv[]);
static Scheme_Object *raise_range_error(int argc, Scheme_Object *argv[]);
static Scheme_Object *raise_arity_error(int argc, Scheme_Object *argv[]);
static Scheme_Object *raise_result_arity_error(int argc, Scheme_Object *argv[]);
static Scheme_Object *error_escape_handler(int, Scheme_Object *[]);
static Scheme_Object *error_display_handler(int, Scheme_Object *[]);
static Scheme_Object *error_value_string_handler(int, Scheme_Object *[]);
static Scheme_Object *exit_handler(int, Scheme_Object *[]);
static Scheme_Object *exe_yield_handler(int, Scheme_Object *[]);
static Scheme_Object *error_print_width(int, Scheme_Object *[]);
static Scheme_Object *error_print_context_length(int, Scheme_Object *[]);
static Scheme_Object *error_print_srcloc(int, Scheme_Object *[]);
static Scheme_Object *def_error_escape_proc(int, Scheme_Object *[]);
static Scheme_Object *def_error_display_proc(int, Scheme_Object *[]);
static Scheme_Object *emergency_error_display_proc(int, Scheme_Object *[]);
static Scheme_Object *def_error_value_string_proc(int, Scheme_Object *[]);
static Scheme_Object *def_exit_handler_proc(int, Scheme_Object *[]);
static Scheme_Object *default_yield_handler(int, Scheme_Object *[]);
static Scheme_Object *srcloc_to_string(int argc, Scheme_Object **argv);
static Scheme_Object *unquoted_printing_string(int argc, Scheme_Object **argv);
static Scheme_Object *unquoted_printing_string_p(int argc, Scheme_Object **argv);
static Scheme_Object *unquoted_printing_string_value(int argc, Scheme_Object **argv);
static Scheme_Object *log_message(int argc, Scheme_Object *argv[]);
static Scheme_Object *log_level_p(int argc, Scheme_Object *argv[]);
static Scheme_Object *log_max_level(int argc, Scheme_Object *argv[]);
static Scheme_Object *log_all_levels(int argc, Scheme_Object *argv[]);
static Scheme_Object *log_level_evt(int argc, Scheme_Object *argv[]);
static Scheme_Object *make_logger(int argc, Scheme_Object *argv[]);
static Scheme_Object *logger_p(int argc, Scheme_Object *argv[]);
static Scheme_Object *current_logger(int argc, Scheme_Object *argv[]);
static Scheme_Object *logger_name(int argc, Scheme_Object *argv[]);
static Scheme_Object *make_log_reader(int argc, Scheme_Object *argv[]);
static Scheme_Object *log_reader_p(int argc, Scheme_Object *argv[]);
static int log_reader_get(Scheme_Object *ch, Scheme_Schedule_Info *sinfo);
static Scheme_Object *do_raise(Scheme_Object *arg, int need_debug, int barrier);
static Scheme_Object *nested_exn_handler(void *old_exn, int argc, Scheme_Object *argv[]);
static void update_want_level(Scheme_Logger *logger, Scheme_Object *name);
static Scheme_Object *check_arity_property_value_ok(int argc, Scheme_Object *argv[]);
static char *make_provided_list(Scheme_Object *o, int count, intptr_t *lenout);
static char *init_buf(intptr_t *len, intptr_t *blen);
void scheme_set_logging2(int syslog_level, int stderr_level, int stdout_level)
{
if (syslog_level > -1)
init_syslog_level = scheme_make_integer(syslog_level);
if (stderr_level > -1)
init_stderr_level = scheme_make_integer(stderr_level);
if (stdout_level > -1)
init_stdout_level = scheme_make_integer(stdout_level);
}
void scheme_set_logging(int syslog_level, int stderr_level)
{
scheme_set_logging2(syslog_level, stderr_level, -1);
}
void scheme_set_logging2_spec(Scheme_Object *syslog_level, Scheme_Object *stderr_level, Scheme_Object *stdout_level)
{
/* A spec is (list* <int> <byte-string> .... <int>) */
if (syslog_level) {
REGISTER_SO(init_syslog_level);
init_syslog_level = syslog_level;
}
if (stderr_level) {
REGISTER_SO(init_stderr_level);
init_stderr_level = stderr_level;
}
if (stdout_level) {
REGISTER_SO(init_stdout_level);
init_stdout_level = stdout_level;
}
}
void scheme_set_logging_spec(Scheme_Object *syslog_level, Scheme_Object *stderr_level)
{
scheme_set_logging2_spec(syslog_level, stderr_level, NULL);
}
void scheme_init_logging_once(void)
{
/* Convert specs to use symbols */
int j;
Scheme_Object *l, *s;
for (j = 0; j < 3; j++) {
switch (j) {
case 0: l = init_syslog_level; break;
case 1: l = init_stderr_level; break;
default: l = init_stdout_level; break;
}
if (l) {
while (!SCHEME_INTP(l)) {
l = SCHEME_CDR(l);
s = scheme_intern_exact_symbol(SCHEME_BYTE_STR_VAL(SCHEME_CAR(l)),
SCHEME_BYTE_STRLEN_VAL(SCHEME_CAR(l)));
SCHEME_CAR(l) = s;
l = SCHEME_CDR(l);
}
}
}
}
typedef struct {
int args;
Scheme_Object *type;
Scheme_Object **names;
int count;
Scheme_Object *exptime;
int super_pos;
} exn_rec;
#define _MZEXN_TABLE
#include "schexn.h"
#undef _MZEXN_TABLE
static void default_printf(char *msg, ...)
{
GC_CAN_IGNORE va_list args;
HIDE_FROM_XFORM(va_start(args, msg));
vfprintf(stderr, msg, args);
HIDE_FROM_XFORM(va_end(args));
fflush(stderr);
}
static void default_output(char *s, intptr_t len)
{
fwrite(s, len, 1, stderr);
fflush(stderr);
}
intptr_t scheme_errno() {
#ifdef WINDOWS_FILE_HANDLES
return GetLastError();
#else
return errno;
#endif
}
Scheme_Config *scheme_init_error_escape_proc(Scheme_Config *config)
{
if (!def_error_esc_proc) {
REGISTER_SO(def_error_esc_proc);
def_error_esc_proc =
scheme_make_prim_w_arity(def_error_escape_proc,
"default-error-escape-handler",
0, 0);
}
if (config)
return scheme_extend_config(config, MZCONFIG_ERROR_ESCAPE_HANDLER, def_error_esc_proc);
else {
scheme_set_root_param(MZCONFIG_ERROR_ESCAPE_HANDLER, def_error_esc_proc);
return NULL;
}
}
/*
Recognized by scheme_[v]sprintf:
%c = unicode char
%d = int
%gd = long int
%gx = long int
%ld = intptr_t
%Id = intptr_t (for MSVC)
%I64d = intptr_t (for MingGW)
%lx = intptr_t
%Ix = intptr_t (for MSVC)
%I64x = intptr_t (for MingGW)
%o = int, octal
%f = double
%% = percent
%s = string
%5 = mzchar string
%S = Scheme symbol
%t = string with inptr_t size
%u = mzchar string with intptr_t size
%T = Scheme string
%q = truncated-to-256 string
%Q = truncated-to-256 Scheme string
%V = scheme_value
%@ = list of scheme_value to write splice
%D = scheme value to display
%W = scheme value to write
%_ = skip pointer
%- = skip int
%L = line number as intptr_t, -1 means no line
%R = get error numebr and string from rktio
%e = error number for strerror()
%E = error number for platform-specific error string
%Z = potential platform-specific error number; additional char*
is either NULL or a specific error message
%N = boolean then error number like %E (if boolean is 0)
or error number for scheme_hostname_error()
%m = boolean then error number like %e, which
is used only if the boolean is 1
%M = boolean then error number like %E, which
is used only if the boolean is 1
*/
static intptr_t sch_vsprintf(char *s, intptr_t maxlen, const char *msg, va_list args, char **_s,
Scheme_Object **_errno_val, int *_unsupported)
/* NULL for s means allocate the buffer here (and return in (_s), but this function
doesn't allocate before extracting arguments from the stack. */
{
intptr_t i, j;
char buf[100];
/* Since we might malloc, move all pointers into a local array for
the sake of precise GC. We have to do numbers, too, for
consistency. */
int pp = 0, ip = 0, dp = 0;
void *ptrs[25];
intptr_t ints[25];
double dbls[25];
for (j = 0; msg[j]; j++) {
if (msg[j] == '%') {
int type;
j++;
type = msg[j];
switch (type) {
case 'c':
ints[ip++] = mzVA_ARG(args, int);
break;
case 'd':
case 'o':
case '-':
ints[ip++] = mzVA_ARG(args, int);
break;
case 'g':
ints[ip++] = mzVA_ARG(args, long);
break;
case 'l':
case 'I':
ints[ip++] = mzVA_ARG(args, intptr_t);
break;
case 'f':
dbls[dp++] = mzVA_ARG(args, double);
break;
case 'L':
ints[ip++] = mzVA_ARG(args, intptr_t);
break;
case 'e':
case 'E':
ints[ip++] = mzVA_ARG(args, int);
break;
case 'N':
case 'm':
case 'M':
ints[ip++] = mzVA_ARG(args, int);
ints[ip++] = mzVA_ARG(args, int);
break;
case 'Z':
ints[ip++] = mzVA_ARG(args, int);
ptrs[pp++] = mzVA_ARG(args, char*);
break;
case 'S':
case 'V':
case '@':
case 'D':
case 'W':
case 'T':
case 'Q':
case '_':
ptrs[pp++] = mzVA_ARG(args, Scheme_Object*);
break;
default:
ptrs[pp++] = mzVA_ARG(args, char*);
if ((type == 't') || (type == 'u')) {
ints[ip++] = mzVA_ARG(args, intptr_t);
}
}
}
}
pp = 0;
ip = 0;
dp = 0;
if (!s) {
s = init_buf(NULL, &maxlen);
*_s = s;
}
--maxlen;
i = j = 0;
while ((i < maxlen) && msg[j]) {
if (msg[j] == '%') {
int type;
j++;
type = msg[j++];
if (type == '%')
s[i++] = '%';
else {
const char *t;
intptr_t tlen;
int dots = 0;
switch (type) {
case 'c':
{
int c;
c = ints[ip++];
if (c < 128) {
buf[0] = c;
tlen = 1;
} else {
mzchar mc;
mc = c;
tlen = scheme_utf8_encode_all(&mc, 1, (unsigned char *)buf);
}
t = buf;
}
break;
case 'd':
{
int d;
d = ints[ip++];
sprintf(buf, "%d", d);
t = buf;
tlen = strlen(t);
}
break;
case '-':
{
ip++;
t = "";
tlen = 0;
}
break;
case 'o':
{
int d;
d = ints[ip++];
sprintf(buf, "%o", d);
t = buf;
tlen = strlen(t);
}
break;
case 'I':
case 'l':
case 'g':
{
intptr_t d;
int as_hex;
if ((type == 'I') && (msg[j] == '6') && (msg[j+1] == '4'))
j++;
as_hex = (msg[j] == 'x');
j++;
d = ints[ip++];
if (as_hex)
sprintf(buf, "%" PRIxPTR, d);
else
sprintf(buf, "%" PRIdPTR, d);
t = buf;
tlen = strlen(t);
}
break;
case 'f':
{
double f;
f = dbls[dp++];
sprintf(buf, "%f", f);
t = buf;
tlen = strlen(t);
}
break;
case 'L':
{
intptr_t d;
d = ints[ip++];
if (d >= 0) {
sprintf(buf, "%" PRIdPTR ":", d);
t = buf;
tlen = strlen(t);
} else {
t = ":";
tlen = 1;
}
}
break;
case 'R':
{
intptr_t errid;
intptr_t errkind;
const char *es, *errkind_str;
intptr_t elen;
errkind = rktio_get_last_error_kind(scheme_rktio);
errid = rktio_get_last_error(scheme_rktio);
switch (errkind) {
case RKTIO_ERROR_KIND_WINDOWS:
errkind_str = "errid";
break;
case RKTIO_ERROR_KIND_POSIX:
errkind_str = "errno";
break;
case RKTIO_ERROR_KIND_GAI:
errkind_str = "gai_err";
break;
default:
errkind_str = "rktio_err";
break;
}
es = rktio_get_error_string(scheme_rktio, errkind, errid);
sprintf(buf, "; %s=%" PRIdPTR "", errkind_str, errid);
if (es) elen = strlen(es); else elen = 0;
tlen = strlen(buf);
t = (const char *)scheme_malloc_atomic(tlen+elen+1);
memcpy((char *)t, es, elen);
memcpy((char *)t+elen, buf, tlen+1);
tlen += elen;
if (_errno_val) {
Scheme_Object *err_kind;
switch (errkind) {
case RKTIO_ERROR_KIND_WINDOWS:
err_kind = windows_symbol;
break;
case RKTIO_ERROR_KIND_POSIX:
err_kind = posix_symbol;
break;
case RKTIO_ERROR_KIND_GAI:
err_kind = gai_symbol;
break;
default:
err_kind = NULL;
}
if (err_kind) {
err_kind = scheme_make_pair(scheme_make_integer_value(errid), err_kind);
*_errno_val = err_kind;
}
}
if (_unsupported
&& (errid == RKTIO_ERROR_UNSUPPORTED)
&& (errkind == RKTIO_ERROR_KIND_RACKET))
*_unsupported = 1;
}
break;
case 'e':
case 'm':
case 'E':
case 'M':
case 'Z':
case 'N':
{
int en, he, none = 0;
char *es;
const char *errkind_str;
Scheme_Object *err_kind = NULL;
if (type == 'm') {
none = !ints[ip++];
type = 'e';
he = 0;
} else if (type == 'M') {
none = !ints[ip++];
type = 'E';
he = 0;
} else if (type == 'N') {
he = ints[ip++];
type = 'E';
} else
he = 0;
en = ints[ip++];
if (type == 'Z')
es = ptrs[pp++];
else
es = NULL;
if (he) {
es = (char *)scheme_hostname_error(en);
err_kind = gai_symbol;
errkind_str = "gai_err";
}
if ((en || es) && !none) {
#ifdef NO_STRERROR_AVAILABLE
if (!es)
es = "Unknown error";
err_kind = posix_symbol;
#else
# ifdef DOS_FILE_SYSTEM
wchar_t mbuf[256];
int len;
if ((type != 'e') && !es) {
if ((len = FormatMessageW((FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS),
NULL,
en, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
mbuf, 255, NULL))) {
int i;
i = scheme_utf8_encode((const unsigned int *)mbuf, 0, len, NULL, 0, 1);
es = (char *)scheme_malloc_atomic(i + 1);
(void)scheme_utf8_encode((const unsigned int *)mbuf, 0, len, es, 0, 1);
es[i] = 0;
/* Remove newlines: */
for (i = strlen(es) - 1; i > 0; i--) {
if (isspace(es[i]))
es[i] = 0;
else
break;
}
err_kind = windows_symbol;
errkind_str = "win_err";
}
}
# endif
if (!es) {
es = strerror(en);
err_kind = posix_symbol;
errkind_str = "errno";
}
#endif
tlen = strlen(es) + 24;
t = (const char *)scheme_malloc_atomic(tlen);
sprintf((char *)t, "%s; %s=%d", es, errkind_str, en);
tlen = strlen(t);
if (_errno_val) {
err_kind = scheme_make_pair(scheme_make_integer_value(en), err_kind);
*_errno_val = err_kind;
}
} else {
if (none) {
t = "";
tlen = 0;
} else {
t = "errno=?";
tlen = 7;
}
}
}
break;
case 'S':
{
Scheme_Object *sym;
sym = (Scheme_Object *)ptrs[pp++];
t = scheme_symbol_name_and_size(sym, (uintptr_t *)&tlen, 0);
}
break;
case 'V':
{
Scheme_Object *o;
o = (Scheme_Object *)ptrs[pp++];
t = scheme_make_provided_string(o, 1, &tlen);
}
break;
case '@':
{
Scheme_Object *o;
o = (Scheme_Object *)ptrs[pp++];
t = make_provided_list(o, 1, &tlen);
}
break;
case 'D':
{
Scheme_Object *o;
intptr_t dlen;
o = (Scheme_Object *)ptrs[pp++];
t = scheme_display_to_string(o, &dlen);
tlen = dlen;
}
break;
case 'W':
{
Scheme_Object *o;
intptr_t dlen;
o = (Scheme_Object *)ptrs[pp++];
t = scheme_write_to_string(o, &dlen);
tlen = dlen;
}
break;
case '_':
{
pp++;
t = "";
tlen = 0;
}
break;
case 'T':
case 'Q':
{
Scheme_Object *str;
str = (Scheme_Object *)ptrs[pp++];
if (SCHEME_CHAR_STRINGP(str))
str = scheme_char_string_to_byte_string(str);
t = SCHEME_BYTE_STR_VAL(str);
tlen = SCHEME_BYTE_STRLEN_VAL(str);
}
break;
case 'u':
case '5':
{
mzchar *u;
intptr_t ltlen;
u = (mzchar *)ptrs[pp++];
if (type == 'u') {
tlen = ints[ip++];
if (tlen < 0)
tlen = scheme_char_strlen(u);
} else {
tlen = scheme_char_strlen(u);
}
t = scheme_utf8_encode_to_buffer_len(u, tlen, NULL, 0, <len);
tlen = ltlen;
}
break;
default:
{
t = (char *)ptrs[pp++];
if (type == 't') {
tlen = ints[ip++];
if (tlen < 0)
tlen = strlen(t);
} else {
tlen = strlen(t);
}
}
break;
}
if ((type == 'q') || (type == 'Q')) {
if (tlen > 256) {
tlen = 250;
dots = 1;
}
}
while (tlen && i < maxlen) {
s[i++] = *t;
t = t XFORM_OK_PLUS 1;
tlen--;
}
if (dots) {
/* FIXME: avoiding truncating in the middle of a UTF-8 encoding */
if (i < maxlen - 3) {
s[i++] = '.';
s[i++] = '.';
s[i++] = '.';
}
}
}
} else {
s[i++] = msg[j++];
}
}
s[i] = 0;
return i;
}
intptr_t scheme_sprintf(char *s, intptr_t maxlen, const char *msg, ...)
{
intptr_t len;
GC_CAN_IGNORE va_list args;
HIDE_FROM_XFORM(va_start(args, msg));
len = sch_vsprintf(s, maxlen, msg, args, NULL, NULL, NULL);
HIDE_FROM_XFORM(va_end(args));
return len;
}
int scheme_last_error_is_racket(int errid)
{
return ((rktio_get_last_error_kind(scheme_rktio) == RKTIO_ERROR_KIND_RACKET)
&& (rktio_get_last_error(scheme_rktio) == errid));
}
#define ESCAPING_NONCM_PRIM(name, func, a1, a2, env) \
p = scheme_make_noncm_prim(func, name, a1, a2); \
SCHEME_PRIM_PROC_FLAGS(p) |= scheme_intern_prim_opt_flags(SCHEME_PRIM_ALWAYS_ESCAPES); \
scheme_addto_prim_instance(name, p, env);
void scheme_init_error(Scheme_Startup_Env *env)
{
Scheme_Object *p;
if (!scheme_console_printf)
scheme_console_printf = default_printf;
if (!scheme_console_output)
scheme_console_output = default_output;
REGISTER_SO(scheme_raise_arity_error_proc);
/* errors */
ESCAPING_NONCM_PRIM("error", error, 1, -1, env);
ESCAPING_NONCM_PRIM("raise-user-error", raise_user_error, 1, -1, env);
ESCAPING_NONCM_PRIM("raise-type-error", raise_type_error, 3, -1, env);
ESCAPING_NONCM_PRIM("raise-argument-error", raise_argument_error, 3, -1, env);
ESCAPING_NONCM_PRIM("raise-result-error", raise_result_error, 3, -1, env);
ESCAPING_NONCM_PRIM("raise-arguments-error", raise_arguments_error, 2, -1, env);
ESCAPING_NONCM_PRIM("raise-mismatch-error", raise_mismatch_error, 3, -1, env);
ESCAPING_NONCM_PRIM("raise-range-error", raise_range_error, 7, 8, env);
scheme_raise_arity_error_proc = scheme_make_noncm_prim(raise_arity_error, "raise-arity-error", 2, -1);
scheme_addto_prim_instance("raise-arity-error", scheme_raise_arity_error_proc, env);
ESCAPING_NONCM_PRIM("raise-result-arity-error", raise_result_arity_error, 2, -1, env);
ADD_PARAMETER("error-display-handler", error_display_handler, MZCONFIG_ERROR_DISPLAY_HANDLER, env);
ADD_PARAMETER("error-value->string-handler", error_value_string_handler, MZCONFIG_ERROR_PRINT_VALUE_HANDLER, env);
ADD_PARAMETER("error-escape-handler", error_escape_handler, MZCONFIG_ERROR_ESCAPE_HANDLER, env);
ADD_PARAMETER("exit-handler", exit_handler, MZCONFIG_EXIT_HANDLER, env);
ADD_PARAMETER("executable-yield-handler", exe_yield_handler, MZCONFIG_EXE_YIELD_HANDLER, env);
ADD_PARAMETER("error-print-width", error_print_width, MZCONFIG_ERROR_PRINT_WIDTH, env);
ADD_PARAMETER("error-print-context-length", error_print_context_length, MZCONFIG_ERROR_PRINT_CONTEXT_LENGTH, env);
ADD_PARAMETER("error-print-source-location", error_print_srcloc, MZCONFIG_ERROR_PRINT_SRCLOC, env);
ADD_NONCM_PRIM("exit", scheme_do_exit, 0, 1, env);
/* logging */
ADD_NONCM_PRIM("log-level?", log_level_p, 2, 3, env);
ADD_NONCM_PRIM("log-max-level", log_max_level, 1, 2, env);
ADD_NONCM_PRIM("log-all-levels", log_all_levels, 1, 1, env);
ADD_NONCM_PRIM("log-level-evt", log_level_evt, 1, 1, env);
ADD_NONCM_PRIM("make-logger", make_logger, 0, -1, env);
ADD_NONCM_PRIM("make-log-receiver", make_log_reader, 2, -1, env);
ADD_PRIM_W_ARITY("log-message", log_message, 4, 6, env);
ADD_FOLDING_PRIM("logger?", logger_p, 1, 1, 1, env);
ADD_FOLDING_PRIM("logger-name", logger_name, 1, 1, 1, env);
ADD_FOLDING_PRIM("log-receiver?", log_reader_p, 1, 1, 1, env);
ADD_PARAMETER("current-logger", current_logger, MZCONFIG_LOGGER, env);
ADD_NONCM_PRIM("srcloc->string", srcloc_to_string, 1, 1, env);
ADD_NONCM_PRIM("unquoted-printing-string", unquoted_printing_string, 1, 1, env);
ADD_FOLDING_PRIM("unquoted-printing-string?", unquoted_printing_string_p, 1, 1, 1, env);
ADD_IMMED_PRIM("unquoted-printing-string-value", unquoted_printing_string_value, 1, 1, env);
REGISTER_SO(scheme_def_exit_proc);
REGISTER_SO(default_display_handler);
REGISTER_SO(emergency_display_handler);
scheme_def_exit_proc = scheme_make_prim_w_arity(def_exit_handler_proc, "default-exit-handler", 1, 1);
default_display_handler = scheme_make_prim_w_arity(def_error_display_proc, "default-error-display-handler", 2, 2);
emergency_display_handler = scheme_make_prim_w_arity(emergency_error_display_proc, "emergency-error-display-handler", 2, 2);
REGISTER_SO(def_err_val_proc);
def_err_val_proc = scheme_make_prim_w_arity(def_error_value_string_proc, "default-error-value->string-handler", 2, 2);
REGISTER_SO(none_symbol);
REGISTER_SO(fatal_symbol);
REGISTER_SO(error_symbol);
REGISTER_SO(warning_symbol);
REGISTER_SO(info_symbol);
REGISTER_SO(debug_symbol);
none_symbol = scheme_intern_symbol("none");
fatal_symbol = scheme_intern_symbol("fatal");
error_symbol = scheme_intern_symbol("error");
warning_symbol = scheme_intern_symbol("warning");
info_symbol = scheme_intern_symbol("info");
debug_symbol = scheme_intern_symbol("debug");
REGISTER_SO(posix_symbol);
REGISTER_SO(windows_symbol);
REGISTER_SO(gai_symbol);
posix_symbol = scheme_intern_symbol("posix");
windows_symbol = scheme_intern_symbol("windows");
gai_symbol = scheme_intern_symbol("gai");
REGISTER_SO(arity_property);
{
Scheme_Object *guard;
guard = scheme_make_prim_w_arity(check_arity_property_value_ok, "guard-for-prop:arity-string", 2, 2);
arity_property = scheme_make_struct_type_property_w_guard(scheme_intern_symbol("arity-string"), guard);
}
scheme_addto_prim_instance("prop:arity-string", arity_property, env);
REGISTER_SO(def_exe_yield_proc);
def_exe_yield_proc = scheme_make_prim_w_arity(default_yield_handler,
"default-executable-yield-handler",
1, 1);
}
void scheme_init_logger_wait()
{
scheme_add_evt(scheme_log_reader_type, (Scheme_Ready_Fun)log_reader_get, NULL, NULL, 1);
}
void scheme_init_logger()
{
REGISTER_SO(scheme_main_logger);
scheme_main_logger = scheme_make_logger(NULL, NULL);
scheme_main_logger->syslog_level = init_syslog_level;
scheme_main_logger->stderr_level = init_stderr_level;
scheme_main_logger->stdout_level = init_stdout_level;
REGISTER_SO(scheme_gc_logger);
scheme_gc_logger = scheme_make_logger(scheme_main_logger, scheme_intern_symbol("GC"));
REGISTER_SO(scheme_future_logger);
scheme_future_logger = scheme_make_logger(scheme_main_logger, scheme_intern_symbol("future"));
REGISTER_SO(scheme_place_logger);
scheme_place_logger = scheme_make_logger(scheme_main_logger, scheme_intern_symbol("place"));
}
Scheme_Logger *scheme_get_main_logger() {
return scheme_main_logger;
}
Scheme_Logger *scheme_get_gc_logger() {
return scheme_gc_logger;
}
Scheme_Logger *scheme_get_future_logger() {
return scheme_future_logger;
}
Scheme_Logger *scheme_get_place_logger() {
return scheme_place_logger;
}
void scheme_init_error_config(void)
{
scheme_set_root_param(MZCONFIG_EXIT_HANDLER, scheme_def_exit_proc);
scheme_set_root_param(MZCONFIG_ERROR_DISPLAY_HANDLER, default_display_handler);
scheme_set_root_param(MZCONFIG_ERROR_PRINT_VALUE_HANDLER, def_err_val_proc);
scheme_set_root_param(MZCONFIG_EXE_YIELD_HANDLER, def_exe_yield_proc);
}
void scheme_init_logger_config() {
scheme_set_root_param(MZCONFIG_LOGGER, (Scheme_Object *)scheme_main_logger);
}
static void
call_error(char *buffer, int len, Scheme_Object *exn)
{
if (scheme_current_thread->constant_folding) {
if (scheme_current_thread->constant_folding != (Optimize_Info *)scheme_false)
scheme_log(scheme_optimize_info_logger(scheme_current_thread->constant_folding),
SCHEME_LOG_WARNING,
0,
"constant-fold attempt failed%s: %s",
scheme_optimize_info_context(scheme_current_thread->constant_folding),
buffer);
if (SCHEME_CHAPERONE_STRUCTP(exn)
&& scheme_is_struct_instance(exn_table[MZEXN_BREAK].type, exn)) {
/* remember to re-raise exception */
scheme_current_thread->reading_delayed = exn;
}
scheme_longjmp(scheme_error_buf, 1);
} else if (scheme_current_thread->reading_delayed) {
scheme_current_thread->reading_delayed = exn;
scheme_longjmp(scheme_error_buf, 1);
} else {
mz_jmp_buf savebuf;
Scheme_Object *p[2], *display_handler, *escape_handler, *v;
Scheme_Config *config, *orig_config;
Scheme_Cont_Frame_Data cframe, cframe2;
/* For last resort: */
memcpy((void *)&savebuf, &scheme_error_buf, sizeof(mz_jmp_buf));
orig_config = scheme_current_config();
display_handler = scheme_get_param(orig_config, MZCONFIG_ERROR_DISPLAY_HANDLER);
escape_handler = scheme_get_param(orig_config, MZCONFIG_ERROR_ESCAPE_HANDLER);
v = scheme_make_byte_string_without_copying("error display handler");
v = scheme_make_closed_prim_w_arity(nested_exn_handler,
scheme_make_pair(v, exn),
"nested-exception-handler",
1, 1);
config = orig_config;
if (SAME_OBJ(display_handler, default_display_handler))
config = scheme_extend_config(config,
MZCONFIG_ERROR_DISPLAY_HANDLER,
emergency_display_handler);
else
config = scheme_extend_config(config,
MZCONFIG_ERROR_DISPLAY_HANDLER,
default_display_handler);