Post Scarcity
A prototype for a post scarcity programming environment
Loading...
Searching...
No Matches
io.c File Reference
#include <grp.h>
#include <langinfo.h>
#include <pwd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <uuid/uuid.h>
#include <wchar.h>
#include <wctype.h>
#include <curl/curl.h>
#include "arith/integer.h"
#include "debug.h"
#include "io/fopen.h"
#include "io/io.h"
#include "memory/conspage.h"
#include "memory/consspaceobject.h"
#include "ops/intern.h"
#include "ops/lispops.h"
#include "utils.h"
Include dependency graph for io.c:

Go to the source code of this file.

Functions

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)
 
struct cons_pointer add_meta_time (struct cons_pointer meta, wchar_t *key, time_t *value)
 
void collect_meta (struct cons_pointer stream, char *url)
 
URL_FILEfile_to_url_file (FILE *f)
 given this file handle f, return a new url_file handle wrapping it.
 
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.
 
int io_init ()
 Initialise the I/O subsystem.
 
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.
 
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 present and is non-nil, open it for reading.
 
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.
 
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 arguments are ignored.
 
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.
 
wint_t url_fgetwc (URL_FILE *input)
 get one wide character from the buffer.
 
wint_t url_ungetwc (wint_t wc, URL_FILE *input)
 

Variables

CURLSH * io_share
 The sharing hub for all connections.
 
struct cons_pointer lisp_io_in = NIL
 bound to the Lisp string representing C_IO_IN in initialisation.
 
struct cons_pointer lisp_io_out = NIL
 bound to the Lisp string representing C_IO_OUT in initialisation.
 
wint_t ungotten = 0
 Allow a one-character unget facility.
 

Function Documentation

◆ add_meta_integer()

struct cons_pointer add_meta_integer ( struct cons_pointer  meta,
wchar_t *  key,
long int  value 
)

Definition at line 268 of file io.c.

References c_string_to_lisp_keyword(), make_cons(), make_integer(), and NIL.

Referenced by collect_meta().

◆ add_meta_string()

struct cons_pointer add_meta_string ( struct cons_pointer  meta,
wchar_t *  key,
char *  value 
)

Definition at line 276 of file io.c.

References c_string_to_lisp_keyword(), c_string_to_lisp_string(), make_cons(), and trim().

Referenced by add_meta_time(), and collect_meta().

◆ add_meta_time()

struct cons_pointer add_meta_time ( struct cons_pointer  meta,
wchar_t *  key,
time_t *  value 
)

Definition at line 286 of file io.c.

References add_meta_string().

Referenced by collect_meta().

◆ collect_meta()

void collect_meta ( struct cons_pointer  stream,
char *  url 
)

◆ file_to_url_file()

URL_FILE * file_to_url_file ( FILE *  f)

given this file handle f, return a new url_file handle wrapping it.

Parameters
fthe file to be wrapped;
Returns
the new handle, or null if no such handle could be allocated.

Definition at line 134 of file io.c.

References CFTYPE_FILE, fcurl_data::handle, and fcurl_data::type.

Referenced by check_exception(), debug_dump_object(), debug_print_object(), lisp_inspect(), lisp_print(), lisp_read(), and main().

◆ get_default_stream()

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.

Definition at line 411 of file io.c.

References c_assoc(), lisp_io_in, lisp_io_out, and NIL.

Referenced by lisp_inspect(), lisp_print(), lisp_read(), and lisp_repl().

◆ io_init()

int io_init ( )

Initialise the I/O subsystem.

Returns
0 on success; any other value means failure.

Definition at line 69 of file io.c.

References io_share.

Referenced by main().

◆ lisp_close()

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.

If the first arg is not a stream, does nothing. All other args are ignored.

  • (close stream)
Parameters
framemy stack_frame.
frame_pointera pointer to my stack_frame.
envmy environment.
Returns
T if the stream was successfully closed, else NIL.

Definition at line 254 of file io.c.

References NIL, pointer2cell, readp, TRUE, url_fclose(), and writep.

Referenced by main().

◆ lisp_open()

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 present and is non-nil, open it for reading.

At present, further arguments are ignored and there is no mechanism to open to append, or error if the URL is faulty or indicates an unavailable resource.

  • (open url)
Parameters
framemy stack_frame.
frame_pointera pointer to my stack_frame.
envmy environment.
Returns
a string of one character, namely the next available character on my stream, if any, else NIL.

Definition at line 437 of file io.c.

References c_string_to_lisp_string(), CFTYPE_CURL, CFTYPE_FILE, CFTYPE_NONE, collect_meta(), DEBUG_IO, debug_printf(), fcurl_data::handle, lisp_string_to_c_string(), make_exception(), make_read_stream(), make_write_stream(), NIL, nilp, pointer2cell, stringp, fcurl_data::type, and url_fopen().

Referenced by main().

◆ lisp_read_char()

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.

  • (read-char stream)
Parameters
framemy stack_frame.
frame_pointera pointer to my stack_frame.
envmy environment.
Returns
a string of one character, namely the next available character on my stream, if any, else NIL.

Definition at line 504 of file io.c.

References make_string(), NIL, pointer2cell, readp, and url_fgetwc().

Referenced by main().

◆ lisp_slurp()

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 arguments are ignored.

TODO: it should be possible to optionally pass a string URL to this function,

  • (slurp stream)
Parameters
framemy stack_frame.
frame_pointera pointer to my stack_frame.
envmy environment.
Returns
a string of one character, namely the next available character on my stream, if any, else NIL.

Definition at line 533 of file io.c.

References debug_dump_object(), DEBUG_IO, debug_print(), debug_println(), make_string(), NIL, cons_space_object::payload, pointer2cell, readp, url_feof(), and url_fgetwc().

Referenced by main().

◆ lisp_string_to_c_string()

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.

NOTE that the returned value has been malloced and must be freed. TODO: candidate to moving into a utilities file.

Parameters
sthe lisp string or symbol;
Returns
the c string.

Definition at line 95 of file io.c.

References DEBUG_IO, debug_print(), debug_print_object(), debug_printf(), nilp, pointer2cell, stringp, and symbolp.

Referenced by lisp_open(), and set().

◆ url_fgetwc()

wint_t url_fgetwc ( URL_FILE input)

get one wide character from the buffer.

Parameters
filethe stream to read from;
Returns
the next wide character on the stream, or zero if no more.

Definition at line 151 of file io.c.

References CFTYPE_CURL, CFTYPE_FILE, CFTYPE_NONE, DEBUG_IO, debug_print(), debug_printf(), fcurl_data::handle, fcurl_data::type, ungotten, and url_fgets().

Referenced by lisp_car(), lisp_cdr(), lisp_read_char(), lisp_slurp(), read(), read_continuation(), read_list(), read_map(), read_number(), read_path(), read_string(), and read_symbol_or_key().

◆ url_ungetwc()

wint_t url_ungetwc ( wint_t  wc,
URL_FILE input 
)

Variable Documentation

◆ io_share

CURLSH* io_share

The sharing hub for all connections.

TODO: Ultimately this probably doesn't work for a multi-user environment and we will need one sharing hub for each user, or else we will need to not share at least cookies and ssl sessions.

Definition at line 46 of file io.c.

Referenced by io_init(), and url_fopen().

◆ lisp_io_in

struct cons_pointer lisp_io_in = NIL

bound to the Lisp string representing C_IO_IN in initialisation.

Definition at line 51 of file io.c.

Referenced by get_default_stream(), and main().

◆ lisp_io_out

struct cons_pointer lisp_io_out = NIL

bound to the Lisp string representing C_IO_OUT in initialisation.

Definition at line 55 of file io.c.

Referenced by get_default_stream(), and main().

◆ ungotten

wint_t ungotten = 0

Allow a one-character unget facility.

This may not be enough - we may need to allocate a buffer.

Definition at line 62 of file io.c.

Referenced by url_fgetwc(), and url_ungetwc().