-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathredis4pl.c
1431 lines (1183 loc) · 32.3 KB
/
redis4pl.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
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2020, SWI-Prolog Solutions b.v.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include <SWI-Stream.h>
#include <SWI-Prolog.h>
#include <assert.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
static int protocol_error(IOSTREAM *in, const char *id);
static int unexpected_eof(IOSTREAM *in);
static int newline_expected(IOSTREAM *in);
static atom_t ATOM_rnil;
static atom_t ATOM_atom;
static atom_t ATOM_auto;
static atom_t ATOM_string;
static atom_t ATOM_bytes;
static atom_t ATOM_codes;
static atom_t ATOM_chars;
static atom_t ATOM_integer;
static atom_t ATOM_float;
static atom_t ATOM_rational;
static atom_t ATOM_number;
static atom_t ATOM_utf8;
static atom_t ATOM_text;
static atom_t ATOM_pairs;
static atom_t ATOM_tagged_integer;
static atom_t ATOM_dict_key;
static atom_t ATOM_dict;
static atom_t ATOM_prolog;
static functor_t FUNCTOR_status1;
static functor_t FUNCTOR_prolog1;
static functor_t FUNCTOR_pair2;
static functor_t FUNCTOR_attrib2;
static functor_t FUNCTOR_colon2;
static functor_t FUNCTOR_as2;
static int64_t MIN_TAGGED_INTEGER;
static int64_t MAX_TAGGED_INTEGER;
/*******************************
* CHAR BUF *
*******************************/
typedef struct charbuf
{ char *base;
char *here;
char *end;
char tmp[256];
} charbuf;
static void
init_charbuf(charbuf *cb)
{ cb->base = cb->here = cb->tmp;
cb->end = &cb->tmp[sizeof(cb->tmp)/sizeof(char)];
}
static void
empty_charbuf(charbuf *cb)
{ cb->here = cb->base;
}
static void
free_charbuf(charbuf *cb)
{ if ( cb->base != cb->tmp )
PL_free(cb->base);
}
static int
ensure_space_charbuf(charbuf *cb, size_t space)
{ if ( cb->here + space < cb->end )
{ return TRUE;
} else
{ size_t len = cb->end - cb->base;
size_t sz = cb->here - cb->base;
size_t nlen = len*2;
while(cb->here-cb->base+space > nlen)
nlen *= 2;
if ( cb->base == cb->tmp )
{ char *n = malloc(nlen);
if ( n )
{ memcpy(n, cb->base, sz);
cb->base = n;
} else
return FALSE;
} else
{ char *n = realloc(cb->base, nlen);
if ( !n )
return PL_resource_error("memory");
cb->base = n;
}
cb->here = &cb->base[sz];
cb->end = &cb->base[nlen];
return TRUE;
}
}
static int
add_byte_charbuf(charbuf *cb, int c)
{ if ( ensure_space_charbuf(cb, 1) )
{ *cb->here++ = c;
return TRUE;
}
return FALSE;
}
/*******************************
* READ PRIMITIVES *
*******************************/
static char *
read_line(IOSTREAM *in, charbuf *cb)
{ for(;;)
{ int c = Sgetcode(in);
if ( c == -1 )
return unexpected_eof(in),NULL;
if ( c == '\r' )
{ add_byte_charbuf(cb, 0);
if ( Sgetcode(in) != '\n' )
return newline_expected(in),NULL;
return cb->base;
}
if ( c == '\n' )
{ add_byte_charbuf(cb, 0);
return cb->base;
}
add_byte_charbuf(cb, c);
}
}
static char *
read_number_line(IOSTREAM *in, charbuf *cb)
{ int maxlen = 100;
for(;maxlen-- > 0;)
{ int c = Sgetcode(in);
if ( c == -1 )
return unexpected_eof(in),NULL;
if ( c == '\r' )
{ add_byte_charbuf(cb, 0);
if ( Sgetcode(in) != '\n' )
return newline_expected(in),NULL;
return cb->base;
}
if ( c == '\n' )
{ add_byte_charbuf(cb, 0);
return cb->base;
}
if ( (c >= '0' && c <= '9') ||
((c == '-' || c == '?') && cb->here == cb->base) )
add_byte_charbuf(cb, c);
else
return protocol_error(in, "integer_expected"),NULL;
}
return protocol_error(in, "integer_maxlen"),NULL;
}
/*******************************
* ERRORS *
*******************************/
static int
protocol_error(IOSTREAM *in, const char *msg)
{ return PL_syntax_error(msg, in),FALSE;
}
static int
unexpected_eof(IOSTREAM *in)
{ return protocol_error(in, "unexpected_eof");
}
static int
newline_expected(IOSTREAM *in)
{ return protocol_error(in, "newline_expected");
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
redis_error() returns an error in `msg`. `msg` is 0 if this happens
inside a nested term. I think this should not be possible and be
considered a protocol error. For now we throw the error as an exception.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static int
redis_error(char *s, term_t msg)
{ term_t code;
char *q;
term_t ex;
for(q=s; *q >= 'A' && *q <= 'Z'; q++)
*q = *q + 'a' - 'A';
if ( msg )
ex = msg;
else
ex = PL_new_term_ref();
if ( (code = PL_new_term_ref()) &&
PL_unify_chars(code, PL_ATOM, q-s, s) &&
PL_unify_term(ex,
PL_FUNCTOR_CHARS, "error", 2,
PL_FUNCTOR_CHARS, "redis_error", 2,
PL_TERM, code,
PL_STRING, q+1,
PL_VARIABLE) )
{ if ( msg )
return TRUE;
else
return PL_raise_exception(ex);
} else
return FALSE;
}
/*******************************
* READ MESSAGE *
*******************************/
typedef enum redis_type_kind
{ T_TEXT,
T_TAGGED_INTEGER,
T_INTEGER,
T_FLOAT,
T_RATIONAL,
T_NUMBER,
T_AUTO,
T_PAIRS,
T_DICT
} redis_type_kind;
typedef struct redis_type
{ redis_type_kind kind; /* T_ATOM, ... */
int pltype; /* PL_* */
int encoding; /* REP_* */
} redis_type;
static int redis_read_stream(IOSTREAM *in, term_t msgin,
term_t error, term_t push, redis_type *type);
static int
is_number_kind(redis_type_kind kind)
{ return kind >= T_TAGGED_INTEGER && kind <= T_NUMBER;
}
#define LEN_STREAM (-2)
#define MSG_END (-2)
static int
read_number(IOSTREAM *in, charbuf *cb, long long *vp)
{ long long v;
char *s, *end;
if ( !(s=read_number_line(in, cb)) )
return FALSE;
v = strtoll(s, &end, 10);
if ( *end )
return newline_expected(in);
*vp = v;
return TRUE;
}
static int
read_length(IOSTREAM *in, charbuf *cb, long long *vp)
{ char *s;
if ( !(s=read_number_line(in, cb)) )
return FALSE;
if ( cb->base[0] == '?' )
{ *vp = LEN_STREAM;
} else
{ long long v;
char *end;
v = strtoll(s, &end, 10);
if ( *end )
return newline_expected(in);
*vp = v;
}
return TRUE;
}
static int
read_double(IOSTREAM *in, charbuf *cb, double *vp)
{ double v;
char *s, *end;
if ( !(s=read_line(in, cb)) )
return FALSE;
if ( cb->here-cb->base == 3 &&
strncmp(cb->here, "inf", 3) == 0 )
{ v = INFINITY;
} else if ( cb->here-cb->base == 4 &&
strncmp(cb->here, "-inf", 4) == 0 )
{ v = -INFINITY;
} else
{ v = strtod(s, &end);
if ( *end )
return newline_expected(in),FALSE;
}
*vp = v;
return TRUE;
}
static int
expect_crlf(IOSTREAM *in)
{ int c;
if ( (c=Sgetcode(in)) == '\r' )
{ if ( Sgetcode(in) != '\n' )
return newline_expected(in);
} else if ( c != '\n' )
{ return newline_expected(in);
}
return TRUE;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Returns: FALSE --> error, TRUE: bulk in cb, -1: got nil.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static int
read_chunk(IOSTREAM *in, charbuf *cb, long long len)
{ long long i;
for(i=0; i<len; i++)
{ int c;
if ( (c=Sgetc(in)) == -1 )
return unexpected_eof(in);
if ( !add_byte_charbuf(cb, c) )
return FALSE;
}
if ( !expect_crlf(in) )
return FALSE;
return TRUE;
}
static int
read_bulk(IOSTREAM *in, charbuf *cb)
{ long long v;
if ( !(read_length(in, cb, &v)) )
return FALSE;
if ( v == LEN_STREAM ) /* RESP3 Streamed string */
{ charbuf nbuf;
init_charbuf(&nbuf);
empty_charbuf(cb);
for(;;)
{ long long chlen;
if ( Sgetc(in) != ';' )
return protocol_error(in, "; expected");
empty_charbuf(&nbuf);
if ( !read_number(in, &nbuf, &chlen) )
return FALSE;
if ( chlen == 0 )
{ return TRUE;
} else
{ if ( !read_chunk(in, cb, chlen) )
return FALSE;
}
}
} else
{ if ( v == -1 )
return -1; /* RESP2 nil */
empty_charbuf(cb);
return read_chunk(in, cb, v);
}
}
static int
dict_from_pairs(term_t map, term_t pairs)
{ static predicate_t pred = NULL;
term_t av = PL_new_term_refs(3);
if ( !pred )
pred = PL_predicate("dict_pairs", 3, "system");
return ( PL_put_term(av+0, map) &&
PL_put_term(av+2, pairs) &&
PL_call_predicate(NULL, PL_Q_PASS_EXCEPTION, pred, av) );
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Read a map to a pair list.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static int
map_length_error(term_t error, int64_t len)
{ return PL_unify_term(error,
PL_FUNCTOR_CHARS, "error", 2,
PL_FUNCTOR_CHARS, "domain_error", 2,
PL_CHARS, "redis_map_length",
PL_INT64, len,
PL_VARIABLE);
}
static int
read_map(IOSTREAM *in, charbuf *cb, term_t map, term_t error,
redis_type *type, int from_array)
{ long long v;
redis_type *key_type, *value_type;
term_t pairs = map;
int rc;
if ( type->kind == T_PAIRS || type->kind == T_DICT )
{ key_type = &type[1];
value_type = &type[4];
if ( type->kind == T_DICT )
pairs = PL_new_term_ref();
} else
{ key_type = type;
value_type = type;
}
if ( !read_length(in, cb, &v) )
return FALSE;
if ( v == LEN_STREAM )
{ term_t head = PL_new_term_ref();
term_t tail = PL_copy_term_ref(pairs);
term_t pav = PL_new_term_refs(2);
int64_t len = 0;
for(;;)
{ int rc;
if ( !PL_put_variable(pav+0) ||
!(rc=redis_read_stream(in, pav+0, error, 0, key_type)) )
return FALSE;
if ( rc == MSG_END )
break;
if ( PL_unify_list(tail, head, tail) &&
PL_put_variable(pav+1) &&
(rc=redis_read_stream(in, pav+1, error, 0, value_type)) )
{ if ( rc == MSG_END )
return map_length_error(error, len);
return PL_unify_term(head, PL_FUNCTOR, FUNCTOR_pair2,
PL_TERM, pav+0,
PL_TERM, pav+1);
}
return FALSE;
}
rc = PL_unify_nil(tail);
} else
{ if ( from_array )
{ if ( v%2 )
return map_length_error(error, v);
v /= 2;
}
term_t head = PL_new_term_ref();
term_t tail = PL_copy_term_ref(pairs);
term_t pav = PL_new_term_refs(2);
long long i;
if ( v == -1 ) /* Can this happen? */
return PL_unify_atom(map, ATOM_rnil);
for(i=0; i<v; i++)
{ if ( !PL_unify_list(tail, head, tail) ||
!PL_put_variable(pav+0) ||
!PL_put_variable(pav+1) ||
!redis_read_stream(in, pav+0, error, 0, key_type) ||
!redis_read_stream(in, pav+1, error, 0, value_type) ||
!PL_unify_term(head, PL_FUNCTOR, FUNCTOR_pair2,
PL_TERM, pav+0, PL_TERM, pav+1) )
return FALSE;
}
rc = PL_unify_nil(tail);
}
if ( rc && type->kind == T_DICT )
rc = dict_from_pairs(map, pairs);
return rc;
}
static int
read_array(IOSTREAM *in, charbuf *cb, term_t array, term_t error,
redis_type *type)
{ long long v;
if ( type->kind == T_PAIRS || type->kind == T_DICT )
return read_map(in, cb, array, error, type, TRUE);
if ( !read_length(in, cb, &v) )
return FALSE;
if ( v == LEN_STREAM )
{ term_t head = PL_new_term_ref();
term_t tail = PL_copy_term_ref(array);
term_t tmp = PL_new_term_ref();
for(;;)
{ int rc;
if ( !(rc=redis_read_stream(in, tmp, error, 0, type)) )
return FALSE;
if ( rc == MSG_END )
break;
if ( !PL_unify_list(tail, head, tail) ||
!PL_unify(head, tmp) )
return FALSE;
}
return PL_unify_nil(tail);
} else
{ term_t head = PL_new_term_ref();
term_t tail = PL_copy_term_ref(array);
long long i;
if ( v == -1 )
return PL_unify_atom(array, ATOM_rnil);
for(i=0; i<v; i++)
{ if ( !PL_unify_list(tail, head, tail) ||
!redis_read_stream(in, head, error, 0, type) )
return FALSE;
}
return PL_unify_nil(tail);
}
}
static char *
type_name(redis_type *type)
{ switch(type->kind)
{ case T_TAGGED_INTEGER: return "tagged_integer";
case T_INTEGER: return "integer";
case T_FLOAT: return "float";
case T_RATIONAL: return "rational";
case T_NUMBER: return "number";
case T_PAIRS: return "pairs";
case T_DICT: return "dict";
default: return "unknown";
}
}
enum ntype
{ N_INTEGER,
N_RATIONAL,
N_FLOAT
};
static int
str_is_number(size_t len, const char *data, enum ntype *nt)
{ int isnum = FALSE;
int signok = TRUE;
int has_dot = FALSE;
int has_e = FALSE;
int israt = FALSE;
for(; len > 0; len--,data++)
{ if ( (*data == '+' || *data == '-') && signok )
{ signok = FALSE;
continue;
}
if ( *data >= '0' && *data <= '9' )
{ isnum = TRUE;
signok = FALSE;
continue;
}
if ( *data == '.' && isnum && !has_dot && !israt )
{ has_dot = TRUE;
signok = FALSE;
continue;
}
if ( (*data == 'e' || *data == 'E') && isnum && !has_e && !israt )
{ has_e = TRUE;
signok = TRUE;
continue;
}
if ( *data == 'r' && isnum && !has_e && !has_dot && !israt )
{ israt = TRUE;
continue;
}
return FALSE;
}
if ( isnum )
{ if ( israt )
*nt = N_RATIONAL;
else if ( has_dot || has_e )
*nt = N_FLOAT;
else
*nt = N_INTEGER;
}
return isnum;
}
static int
compatible_num(enum ntype nt, redis_type_kind kind)
{ switch(kind)
{ case T_INTEGER:
case T_TAGGED_INTEGER:
return nt == N_INTEGER;
case T_RATIONAL:
return nt == N_INTEGER || nt == N_RATIONAL;
case T_FLOAT:
case T_NUMBER:
return TRUE;
default:
assert(0);
return FALSE;
}
}
static int
is_tagged_integer(term_t t)
{ int64_t i;
return ( PL_get_int64(t, &i) &&
i >= MIN_TAGGED_INTEGER && i <= MAX_TAGGED_INTEGER );
}
static int
return_type_error(term_t error, const char *etype, redis_type *type,
size_t len, char *data)
{ term_t t;
return ( (t=PL_new_term_ref()) &&
PL_unify_chars(t, PL_STRING|REP_UTF8, len, data) &&
PL_unify_term(error,
PL_FUNCTOR_CHARS, "error", 2,
PL_FUNCTOR_CHARS, etype, 2,
PL_CHARS, type_name(type),
PL_TERM, t,
PL_VARIABLE) );
}
static int
fixup_number(term_t t, term_t message, term_t error,
size_t len, char *data, redis_type *type)
{ int rc;
const char *error_name = "type_error";
switch(type->kind)
{ case T_INTEGER:
rc = PL_is_integer(t);
break;
case T_TAGGED_INTEGER:
if ( !(rc=is_tagged_integer(t)) && PL_is_integer(t) )
error_name = "domain_error";
break;
case T_FLOAT:
if ( !(rc = PL_is_float(t)) )
{ double d;
rc = ( PL_get_float(t, &d) &&
PL_put_float(t, d) );
}
break;
case T_RATIONAL:
rc = PL_is_rational(t);
break;
case T_NUMBER:
rc = PL_is_number(t);
break;
default:
assert(0);
rc = FALSE;
}
if ( rc )
rc = PL_unify(message, t);
else
rc = return_type_error(error, error_name, type, len, data);
return rc;
}
static int
unify_bulk(term_t message, term_t error, size_t len, char *data, redis_type *type)
{ if ( len > 3 &&
data[0] == '\0' &&
data[2] == '\0' )
{ switch(data[1])
{ case 'T':
{ term_t t;
return ( (t=PL_new_term_ref()) &&
PL_put_term_from_chars(t, REP_UTF8|CVT_EXCEPTION,
len-3, data+3) &&
PL_unify(message, t) &&
(PL_reset_term_refs(t),TRUE) );
}
}
}
if ( type->kind == T_TEXT )
{ return PL_unify_chars(message, type->pltype|type->encoding, len, data);
} else if ( type->kind == T_AUTO )
{ enum ntype nt;
redis_type *ntype = &type[2];
if ( str_is_number(len, data, &nt) &&
compatible_num(nt, ntype->kind) )
{ term_t t;
if ( (t=PL_new_term_ref()) &&
PL_put_term_from_chars(t, REP_ISO_LATIN_1|CVT_EXCEPTION, len, data) )
{ if ( ntype->kind == T_TAGGED_INTEGER &&
!is_tagged_integer(t) )
goto as_text;
return fixup_number(t, message, error, len, data, ntype);
} else
return FALSE;
} else
{ redis_type *ttype;
as_text:
ttype = &type[1];
return PL_unify_chars(message, ttype->pltype|ttype->encoding, len, data);
}
} else if ( type->kind >= T_TAGGED_INTEGER && type->kind <= T_NUMBER)
{ term_t t;
return ( (t=PL_new_term_ref()) &&
PL_put_term_from_chars(t, REP_ISO_LATIN_1|CVT_EXCEPTION,
len, data) &&
fixup_number(t, message, error, len, data, type) );
} else
{ return return_type_error(error, "type_error", type, len, data);
}
}
static int
redis_read_stream(IOSTREAM *in, term_t message, term_t error, term_t push,
redis_type *type)
{ int rc = TRUE;
int c0 = Sgetcode(in);
charbuf cb;
init_charbuf(&cb);
char *s;
switch(c0)
{ case '-':
if ( !(s=read_line(in, &cb)) )
rc = FALSE;
else
rc = redis_error(s, error);
break;
case '!': /* RESP3 Blob error */
if ( (rc=read_bulk(in, &cb)) )
{ assert(rc != -1);
rc = redis_error(cb.base, error);
}
break;
case '+':
if ( !(s=read_line(in, &cb)) )
{ rc = FALSE;
} else
{ char *q;
for(q=s; *q; q++)
*q = tolower(*q);
rc = PL_unify_term(message,
PL_FUNCTOR, FUNCTOR_status1,
PL_UTF8_CHARS, s);
}
break;
case ':':
{ long long v;
rc = ( read_number(in, &cb, &v) &&
PL_unify_int64(message, v) );
break;
}
case ',': /* RESP3 double response */
{ double v;
rc = ( read_double(in, &cb, &v) &&
PL_unify_float(message, v) );
break;
}
case '(': /* RESP3 Big number */
{ if ( !(s=read_line(in, &cb)) )
{ rc = FALSE;
} else
{ term_t t;
rc = ( (t=PL_new_term_ref()) &&
PL_put_term_from_chars(t, REP_ISO_LATIN_1|CVT_EXCEPTION,
cb.here-cb.base, cb.base) &&
PL_unify(message, t) &&
(PL_reset_term_refs(t),TRUE) );
}
break;
}
case '#': /* RESP3 boolean */
{ int c = Sgetcode(in);
if ( (rc=expect_crlf(in)) )
{ if ( c == 't' || c == 'f' )
rc = PL_unify_bool(message, (c == 't'));
else
rc = protocol_error(in, "boolean_expected");
}
break;
}
case '$':
{ if ( (rc=read_bulk(in, &cb)) )
{ if ( rc == -1 )
rc = PL_unify_atom(message, ATOM_rnil);
else
rc = unify_bulk(message, error, cb.here-cb.base, cb.base, type);
}
break;
}
case '=': /* RESP3 Verbatim string */
{ if ( (rc=read_bulk(in, &cb)) )
rc = unify_bulk(message, error, cb.here-cb.base-4, cb.base+4, type);
break;
}
case '~': /* RESP3 set */
case '*': /* Array */
{ rc = read_array(in, &cb, message, error, type);
break;
}
case '>': /* RESP3 push */
{ term_t t;
rc = ( push != 0 && /* only on toplevel term */
(t=PL_new_term_ref()) &&
PL_unify_list(push, t, push) &&
read_array(in, &cb, t, error, type) &&
(PL_reset_term_refs(t),TRUE) );
break;
}
case '|': /* RESP3 attrib */
{ term_t attrib = PL_new_term_ref();
term_t msg = PL_new_term_ref();
rc = ( read_map(in, &cb, attrib, error, type, FALSE) &&
redis_read_stream(in, msg, error, 0, type) &&
PL_unify_term(message, PL_FUNCTOR, FUNCTOR_attrib2,
PL_TERM, attrib, PL_TERM, msg) );
break;
}
case '%': /* RESP3 map */
{ rc = read_map(in, &cb, message, error, type, FALSE);
break;
}
case '_': /* RESP3 nil */
rc = ( expect_crlf(in) &&
PL_unify_atom(message, ATOM_rnil) );
break;
case '.':
if ( push == 0 && expect_crlf(in) )
rc = MSG_END;
else
rc = protocol_error(in, "unexpected_code");
break;
case -1:
rc = unexpected_eof(in);
break;
default:
rc = protocol_error(in, "unexpected_code");
break;
}
free_charbuf(&cb);
return rc;
}
#define AS_TOP 0x0001
#define AS_PAIR_KEY 0x0002
#define AS_PAIR_VALUE 0x0004
#define AS_AUTO_TEXT 0x0008
#define AS_AUTO_NUMBER 0x0010
static int
get_as_type(term_t t, redis_type *type, int flags)
{ atom_t name;
size_t arity;
type->kind = T_TEXT;
type->pltype = (flags&AS_PAIR_KEY) ? PL_ATOM : PL_STRING;
type->encoding = REP_UTF8;
if ( PL_get_name_arity(t, &name, &arity) )
{ if ( name == ATOM_atom )
type->pltype = PL_ATOM;
else if ( name == ATOM_string )
type->pltype = PL_STRING;
else if ( name == ATOM_bytes )
type->pltype = PL_CODE_LIST, type->encoding = REP_ISO_LATIN_1;
else if ( name == ATOM_codes )
type->pltype = PL_CODE_LIST;
else if ( name == ATOM_chars )
type->pltype = PL_CHAR_LIST;
else if ( name == ATOM_integer && arity == 0 )
type->kind = T_INTEGER;
else if ( name == ATOM_tagged_integer && arity == 0 )
type->kind = T_TAGGED_INTEGER;
else if ( name == ATOM_float && arity == 0 )
type->kind = T_FLOAT;
else if ( name == ATOM_rational && arity == 0 )
type->kind = T_RATIONAL;
else if ( name == ATOM_number && arity == 0 )
type->kind = T_NUMBER;
else if ( name == ATOM_auto && (arity == 0 || arity == 2) )
{ type->kind = T_AUTO;
if ( arity == 0 )
{ type[1].kind = T_TEXT;
type[1].pltype = PL_ATOM;
type[1].encoding = REP_UTF8;
type[2].kind = T_NUMBER;
} else
{ term_t arg = PL_new_term_ref();
return ( PL_get_arg(1, t, arg) &&