70 int result = curl_global_init( CURL_GLOBAL_SSL );
75 curl_share_setopt(
io_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT );
76 curl_share_setopt(
io_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE );
77 curl_share_setopt(
io_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS );
78 curl_share_setopt(
io_share, CURLSHOPT_SHARE,
79 CURL_LOCK_DATA_SSL_SESSION );
80 curl_share_setopt(
io_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_PSL );
106 wchar_t *buffer = calloc( len + 1,
sizeof(
wchar_t ) );
108 result = calloc( ( len * 4 ) + 1,
sizeof(
char ) );
113 buffer[i++] =
pointer2cell( c ).payload.string.character;
116 wcstombs( result, buffer, len );
137 if ( result != NULL ) {
159 switch ( input->
type ) {
161 fwide( input->
handle.file, 1 );
162 result = fgetwc( input->
handle.file );
167 calloc(
sizeof(
wchar_t ) + 2,
sizeof(
char ) );
168 wchar_t *wbuff = calloc( 2,
sizeof(
wchar_t ) );
172 debug_print( L
"url_fgetwc: about to call url_fgets\n",
177 int c = ( int ) cbuff[0];
180 L
"url_fgetwc: cbuff is '%s'; (first) character = %d (%c)\n",
181 cbuff, c, c & 0xf7 );
192 }
else if ( c >= 0xc2 && c <= 0xdf ) {
194 }
else if ( c >= 0xe0 && c <= 0xef ) {
196 }
else if ( c >= 0xf0 && c <= 0xff ) {
201 url_fgets( (
char * ) &cbuff[1], count, input );
203 mbstowcs( wbuff, cbuff, 2 );
223 switch ( input->
type ) {
225 fwide( input->
handle.file, 1 );
226 result = ungetwc( wc, input->
handle.file );
258 if (
readp( frame->arg[0] ) ||
writep( frame->arg[0] ) ) {
278 value =
trim( value );
279 wchar_t buffer[strlen( value ) + 1];
280 mbstowcs( buffer, value, strlen( value ) + 1 );
290 char datestring[256];
292 strftime( datestring,
293 sizeof( datestring ),
294 nl_langinfo( D_T_FMT ), localtime( value ) );
303static size_t write_meta_callback(
char *
string,
size_t size,
size_t nmemb,
308 char *s = calloc( strlen(
string ),
sizeof(
char ) );
312 if ( strncmp( &cell->
tag.bytes[0],
READTAG, 4 ) ||
316 if ( offset != -1 ) {
317 s[offset] = ( char ) 0;
318 char *name =
trim( s );
319 char *value =
trim( &s[++offset] );
320 wchar_t wname[strlen( name )];
322 mbstowcs( wname, name, strlen( name ) + 1 );
328 L
"write_meta_callback: added header '%s': value '%s'\n",
330 }
else if ( strncmp(
"HTTP", s, 4 ) == 0 ) {
332 char *value =
trim( &s[offset] );
336 ( cell->
payload.stream.meta, L
"status",
337 value ), L
"status-code", strtol( value,
342 L
"write_meta_callback: added header 'status': value '%s'\n",
346 L
"write_meta_callback: header passed with no colon: '%s'\n",
351 ( L
"Pointer passed to write_meta_callback did not point to a stream: ",
357 return strlen(
string );
366 int result = stat( url, &statbuf );
375 if ( ( pwd = getpwuid( statbuf.st_uid ) ) != NULL ) {
381 if ( ( grp = getgrgid( statbuf.st_gid ) ) != NULL ) {
389 ( intmax_t ) statbuf.st_size );
391 meta =
add_meta_time( meta, L
"modified", &statbuf.st_mtime );
395 curl_easy_setopt( s->
handle.curl, CURLOPT_VERBOSE, 1L );
396 curl_easy_setopt( s->
handle.curl, CURLOPT_HEADERFUNCTION,
397 write_meta_callback );
398 curl_easy_setopt( s->
handle.curl, CURLOPT_HEADERDATA, stream );
404 cell->
payload.stream.meta = meta;
415 result =
c_assoc( stream_name, env );
441 if (
stringp( frame->arg[0] ) ) {
444 if (
nilp( frame->arg[1] ) ) {
448 L
"lisp_open: stream @ %ld, stream type = %d, stream handle = %ld\n",
449 (
long int ) &stream, (
int ) stream->
type,
450 (
long int ) stream->
handle.file );
452 switch ( stream->
type ) {
456 ( L
"Could not open stream" ),
460 if ( stream->
handle.file == NULL ) {
463 ( L
"Could not open file" ),
479 if (
pointer2cell( result ).payload.stream.stream == NULL ) {
508 if (
readp( frame->arg[0] ) ) {
537 if (
readp( frame->arg[0] ) ) {
552 cell->
payload.string.cdr = cursor;
struct cons_pointer c_string_to_lisp_keyword(wchar_t *symbol)
Return a lisp keyword representation of this wide character string.
struct cons_pointer c_string_to_lisp_string(wchar_t *string)
Return a lisp string representation of this wide character string.
struct cons_pointer make_cons(struct cons_pointer car, struct cons_pointer cdr)
Construct a cons cell from this pair of pointers.
#define writep(conspoint)
true if conspoint points to a write stream cell, else false.
union cons_space_object::@2 tag
union cons_space_object::@3 payload
#define NIL
a cons pointer which points to the special NIL cell
struct cons_pointer make_exception(struct cons_pointer message, struct cons_pointer frame_pointer)
Construct an exception cell.
#define READTAG
An open read stream.
#define readp(conspoint)
true if conspoint points to a read stream cell, else false
struct cons_pointer make_string(wint_t c, struct cons_pointer tail)
Construct a string from the character c and this tail.
#define nilp(conspoint)
true if conspoint points to the special cell NIL, else false (there should only be one of these so it...
#define symbolp(conspoint)
true if conspoint points to a symbol cell, else false
struct cons_pointer make_write_stream(URL_FILE *output, struct cons_pointer metadata)
Construct a cell which points to a stream open for writing.
struct cons_pointer c_string_to_lisp_string(wchar_t *string)
Return a lisp string representation of this wide character string.
#define TRUE
a cons pointer which points to the special T cell
#define stringp(conspoint)
true if conspoint points to a string cell, else false
struct cons_pointer make_read_stream(URL_FILE *input, struct cons_pointer metadata)
Construct a cell which points to a stream open for reading.
#define WRITETAG
An open write stream.
#define pointer2cell(pointer)
given a cons_pointer as argument, return the cell.
An indirect pointer to a cons cell.
void debug_println(int level)
print a line feed to stderr, if verbosity matches level.
void debug_dump_object(struct cons_pointer pointer, int level)
Like dump_object, q.v., but protected by the verbosity mechanism.
void debug_printf(int level, wchar_t *format,...)
wprintf adapted for the debug logging system.
void debug_print(wchar_t *message, int level)
print this debug message to stderr, if verbosity matches level.
void debug_print_object(struct cons_pointer pointer, int level)
print the object indicated by this pointer to stderr, if verbosity matches level.
#define DEBUG_IO
Print messages debugging input/output operations.
int url_feof(URL_FILE *file)
URL_FILE * url_fopen(const char *url, const char *operation)
char * url_fgets(char *ptr, size_t size, URL_FILE *file)
int url_fclose(URL_FILE *file)
union fcurl_data::@0 handle
struct cons_pointer make_integer(int64_t value, struct cons_pointer more)
Allocate an integer cell representing this value and return a cons_pointer to it.
struct cons_pointer c_assoc(struct cons_pointer key, struct cons_pointer store)
Implementation of assoc in C.
int io_init()
Initialise the I/O subsystem.
struct cons_pointer add_meta_integer(struct cons_pointer meta, wchar_t *key, long int value)
struct cons_pointer add_meta_string(struct cons_pointer meta, wchar_t *key, char *value)
wint_t url_ungetwc(wint_t wc, URL_FILE *input)
struct cons_pointer lisp_io_in
bound to the Lisp string representing C_IO_IN in initialisation.
wint_t url_fgetwc(URL_FILE *input)
get one wide character from the buffer.
struct cons_pointer lisp_open(struct stack_frame *frame, struct cons_pointer frame_pointer, struct cons_pointer env)
Function: return a stream open on the URL indicated by the first argument; if a second argument is pr...
struct cons_pointer lisp_io_out
bound to the Lisp string representing C_IO_OUT in initialisation.
struct cons_pointer lisp_slurp(struct stack_frame *frame, struct cons_pointer frame_pointer, struct cons_pointer env)
Function: return a string representing all characters from the stream indicated by arg 0; further arg...
struct cons_pointer add_meta_time(struct cons_pointer meta, wchar_t *key, time_t *value)
char * lisp_string_to_c_string(struct cons_pointer s)
Convert this lisp string-like-thing (also works for symbols, and, later keywords) into a UTF-8 string...
URL_FILE * file_to_url_file(FILE *f)
given this file handle f, return a new url_file handle wrapping it.
void collect_meta(struct cons_pointer stream, char *url)
struct cons_pointer get_default_stream(bool inputp, struct cons_pointer env)
Resutn the current default input, or of inputp is false, output stream from this environment.
struct cons_pointer lisp_close(struct stack_frame *frame, struct cons_pointer frame_pointer, struct cons_pointer env)
Function, sort-of: close the file indicated by my first arg, and return nil.
CURLSH * io_share
The sharing hub for all connections.
struct cons_pointer lisp_read_char(struct stack_frame *frame, struct cons_pointer frame_pointer, struct cons_pointer env)
Function: return the next character from the stream indicated by arg 0; further arguments are ignored...
wint_t ungotten
Allow a one-character unget facility.
int index_of(char c, const char *s)