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
|
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 2007-2020, University of Amsterdam,
VU University Amsterdam,
CWI, Amsterdam
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 <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <errno.h>
static atom_t ATOM_size; /* size(Int) */
static atom_t ATOM_onclose; /* onclose(:CallBack) */
/*******************************
* TYPES *
*******************************/
#define BUFSIZE SIO_BUFSIZE /* raw I/O buffer */
typedef struct range_context
{ IOSTREAM *stream; /* Original stream */
IOSTREAM *range_stream; /* Stream I'm handle of */
IOENC parent_encoding; /* Saved encoding of parent */
size_t read; /* data already read */
size_t size; /* #bytes of data available */
module_t context; /* onclose context module */
record_t onclose; /* Call-back when closed */
} range_context;
static range_context*
alloc_range_context(IOSTREAM *s)
{ range_context *ctx = PL_malloc(sizeof(*ctx));
memset(ctx, 0, sizeof(*ctx));
ctx->stream = s;
return ctx;
}
static void
free_range_context(range_context *ctx)
{ if ( ctx->stream->upstream )
Sset_filter(ctx->stream, NULL);
else
PL_release_stream(ctx->stream);
if ( ctx->onclose )
{ PL_erase(ctx->onclose);
ctx->onclose = 0;
}
PL_free(ctx);
}
/*******************************
* RANGE LIMITED INPUT *
*******************************/
static ssize_t /* range-limited read */
range_read(void *handle, char *buf, size_t size)
{ range_context *ctx = handle;
size_t max_rd;
ssize_t rd;
if ( ctx->read == ctx->size )
return 0;
if ( ctx->size - ctx->read < size )
max_rd = ctx->size - ctx->read;
else
max_rd = size;
if ( (rd = Sfread(buf, sizeof(char), max_rd, ctx->stream)) >= 0 )
{ ctx->read += rd;
return rd;
}
return rd;
}
static ssize_t /* no writing! */
range_write(void *handle, char *buf, size_t size)
{ return -1;
}
static int
range_control(void *handle, int op, void *data)
{ range_context *ctx = handle;
switch(op)
{ case SIO_FLUSHOUTPUT:
case SIO_SETENCODING:
return 0; /* allow switching encoding */
case SIO_GETSIZE:
{ int64_t *rval = data;
*rval = ctx->size;
return 0;
}
default:
{ IOSTREAM *s;
IOFUNCTIONS *fs;
Scontrol_function fc;
if ( (s = ctx->stream) && /* be careful about race conditions */
(fs = s->functions) &&
(fc = fs->control) &&
s->magic == SIO_MAGIC )
return (*fc)(s->handle, op, data);
return -1;
}
}
}
static int
range_close(void *handle)
{ int rc = 0;
range_context *ctx = handle;
ctx->stream->encoding = ctx->parent_encoding;
if ( ctx->onclose )
{ static predicate_t call3 = 0;
fid_t fid;
term_t av;
size_t left = ctx->size - ctx->read;
if ( !call3 )
call3 = PL_predicate("call", 3, "system");
if ( (fid=PL_open_foreign_frame()) &&
(av=PL_new_term_refs(3)) &&
PL_recorded(ctx->onclose, av+0) &&
PL_unify_stream(av+1, ctx->stream) &&
PL_put_int64(av+2, (int64_t)left)
)
{ term_t ex;
IOSTREAM *parent = ctx->stream;
free_range_context(ctx);
if ( PL_call_predicate(ctx->context, PL_Q_PASS_EXCEPTION, call3, av) )
{ rc = 0;
} else if ( (ex=PL_exception(0)) )
{ Sset_exception(parent, ex);
rc = -1;
} else
{ Sseterr(parent, SIO_FERR, "onclose hook failed");
rc = -1;
}
} else
{ free_range_context(ctx);
}
if ( fid )
PL_close_foreign_frame(fid);
} else
{ free_range_context(ctx);
}
return rc;
}
static IOFUNCTIONS range_functions =
{ range_read,
range_write,
NULL, /* seek */
range_close,
range_control, /* control */
NULL, /* seek64 */
};
/*******************************
* PROLOG CONNECTION *
*******************************/
#define COPY_FLAGS (SIO_INPUT|SIO_OUTPUT| \
SIO_TEXT| \
SIO_REPXML|SIO_REPPL|\
SIO_RECORDPOS)
static foreign_t
pl_stream_range_open(term_t org, term_t new, term_t options)
{ term_t tail = PL_copy_term_ref(options);
term_t head = PL_new_term_ref();
range_context *ctx;
IOSTREAM *s, *s2;
int size = 0;
module_t ocm = NULL;
record_t onclose = 0;
while(PL_get_list(tail, head, tail))
{ atom_t name;
size_t arity;
term_t arg = PL_new_term_ref();
if ( !PL_get_name_arity(head, &name, &arity) || arity != 1 )
return type_error(head, "option");
_PL_get_arg(1, head, arg);
if ( name == ATOM_size )
{ if ( !get_int_ex(arg, &size) )
return FALSE;
if ( size < 0 )
return domain_error(arg, "nonneg");
} else if ( name == ATOM_onclose )
{ if ( !PL_strip_module(arg, &ocm, arg) )
return FALSE;
onclose = PL_record(arg);
}
}
if ( !PL_get_nil(tail) )
return type_error(tail, "list");
if ( !PL_get_stream(org, &s, SIO_INPUT) )
return FALSE; /* Error */
ctx = alloc_range_context(s);
ctx->size = size;
ctx->onclose = onclose;
ctx->context = ocm;
if ( !(s2 = Snew(ctx,
(s->flags©_FLAGS)|SIO_FBUF,
&range_functions)) )
{ free_range_context(ctx); /* no memory */
return FALSE;
}
s2->encoding = s->encoding;
ctx->parent_encoding = s->encoding;
s->encoding = ENC_OCTET;
ctx->range_stream = s2;
if ( PL_unify_stream(new, s2) )
{ Sset_filter(s, s2);
PL_release_stream(s);
return TRUE;
} else
{ return instantiation_error();
}
}
/*******************************
* INSTALL *
*******************************/
static void
install_stream_range()
{ ATOM_size = PL_new_atom("size");
ATOM_onclose = PL_new_atom("onclose");
PL_register_foreign("stream_range_open", 3, pl_stream_range_open, 0);
}
|