diff --git a/src/init.c b/src/init.c index 6cceadd..47ba772 100644 --- a/src/init.c +++ b/src/init.c @@ -41,17 +41,16 @@ * more readable and aid debugging generally. */ void bind_function( wchar_t *name, struct cons_pointer ( *executable ) - ( struct stack_frame *, - struct cons_pointer, struct cons_pointer ) ) { - struct cons_pointer n = c_string_to_lisp_symbol( name ); - struct cons_pointer meta = make_cons( - make_cons(c_string_to_lisp_keyword(L"primitive"), TRUE), - make_cons( make_cons( - c_string_to_lisp_keyword(L"name"), - n), - NIL)); + ( struct stack_frame *, + struct cons_pointer, struct cons_pointer ) ) { + struct cons_pointer n = c_string_to_lisp_symbol( name ); + struct cons_pointer meta = + make_cons( make_cons( c_string_to_lisp_keyword( L"primitive" ), TRUE ), + make_cons( make_cons( c_string_to_lisp_keyword( L"name" ), + n ), + NIL ) ); - deep_bind( n, make_function( meta, executable ) ); + deep_bind( n, make_function( meta, executable ) ); } /** @@ -62,12 +61,11 @@ void bind_special( wchar_t *name, struct cons_pointer ( *executable ) ( struct stack_frame *, struct cons_pointer, struct cons_pointer ) ) { struct cons_pointer n = c_string_to_lisp_symbol( name ); - struct cons_pointer meta = make_cons( - make_cons(c_string_to_lisp_keyword(L"primitive"), TRUE), - make_cons( make_cons( - c_string_to_lisp_keyword(L"name"), - n), - NIL)); + struct cons_pointer meta = + make_cons( make_cons( c_string_to_lisp_keyword( L"primitive" ), TRUE ), + make_cons( make_cons( c_string_to_lisp_keyword( L"name" ), + n ), + NIL ) ); deep_bind( n, make_special( NIL, executable ) ); } @@ -94,9 +92,9 @@ int main( int argc, char *argv[] ) { bool show_prompt = false; setlocale( LC_ALL, "" ); - if (io_init() != 0) { - fputs("Failed to initialise I/O subsystem\n", stderr); - exit(1); + if ( io_init( ) != 0 ) { + fputs( "Failed to initialise I/O subsystem\n", stderr ); + exit( 1 ); } while ( ( option = getopt( argc, argv, "cpdv:" ) ) != -1 ) { diff --git a/src/io/fopen.c b/src/io/fopen.c index 50c09b5..d5e4cd6 100644 --- a/src/io/fopen.c +++ b/src/io/fopen.c @@ -223,7 +223,7 @@ URL_FILE *url_fopen( const char *url, const char *operation ) { curl_easy_setopt( file->handle.curl, CURLOPT_WRITEFUNCTION, write_callback ); /* use the share object */ - curl_easy_setopt(file->handle.curl, CURLOPT_SHARE, io_share); + curl_easy_setopt( file->handle.curl, CURLOPT_SHARE, io_share ); if ( !multi_handle ) diff --git a/src/io/io.c b/src/io/io.c index 3d9eb36..82a6b32 100644 --- a/src/io/io.c +++ b/src/io/io.c @@ -44,22 +44,23 @@ wint_t ungotten = 0; * * @return 0 on success; any other value means failure. */ -int io_init() { - CURL *curl; - CURLcode res; - int result = curl_global_init( CURL_GLOBAL_SSL ); +int io_init( ) { + CURL *curl; + CURLcode res; + int result = curl_global_init( CURL_GLOBAL_SSL ); - io_share = curl_share_init(); + io_share = curl_share_init( ); - if (result == 0) { - curl_share_setopt(io_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT); - curl_share_setopt(io_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE ); - curl_share_setopt(io_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS ); - curl_share_setopt(io_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION ); - curl_share_setopt(io_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_PSL ); - } + if ( result == 0 ) { + curl_share_setopt( io_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT ); + curl_share_setopt( io_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE ); + curl_share_setopt( io_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS ); + curl_share_setopt( io_share, CURLSHOPT_SHARE, + CURL_LOCK_DATA_SSL_SESSION ); + curl_share_setopt( io_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_PSL ); + } - return result; + return result; } /** @@ -243,67 +244,72 @@ lisp_close( struct stack_frame *frame, struct cons_pointer frame_pointer, return result; } -int index_of( char c, char * s) { - int i; +int index_of( char c, char *s ) { + int i; - for (i = 0; s[i] != c && s[i] != 0; i++); + for ( i = 0; s[i] != c && s[i] != 0; i++ ); - return s[i] == c ? i : -1; + return s[i] == c ? i : -1; } -char * trim(char *s) { - int i; +char *trim( char *s ) { + int i; - for (i = strlen(s); (isblank(s[i]) || iscntrl(s[i])) && i > -1; i--) { - s[i] = (char) 0; - } - for (i = 0; isblank(s[i]) && s[i] != 0; i++); + for ( i = strlen( s ); ( isblank( s[i] ) || iscntrl( s[i] ) ) && i > -1; + i-- ) { + s[i] = ( char ) 0; + } + for ( i = 0; isblank( s[i] ) && s[i] != 0; i++ ); - return (char *)&s[i]; + return ( char * ) &s[i]; } /** * Callback to assemble metadata for a URL stream. This is naughty because * it modifies data, but it's really the only way to create metadata. */ -static size_t write_meta_callback(void *ptr, size_t size, size_t nmemb, struct cons_pointer stream) -{ - struct cons_space_object * cell = &pointer2cell(stream); +static size_t write_meta_callback( void *ptr, size_t size, size_t nmemb, + struct cons_pointer stream ) { + struct cons_space_object *cell = &pointer2cell( stream ); - if (strncmp(&cell->tag.bytes[0], READTAG, 4) || - strncmp(&cell->tag.bytes[0], WRITETAG, 4)) { - char * s = (char *)ptr; - int offset = index_of (':', ptr); + if ( strncmp( &cell->tag.bytes[0], READTAG, 4 ) || + strncmp( &cell->tag.bytes[0], WRITETAG, 4 ) ) { + char *s = ( char * ) ptr; + int offset = index_of( ':', ptr ); - if (offset != -1) { - s[offset] = (char)0; - char * name = s; - char * value = trim( &s[++offset]); - wchar_t * wname = calloc(strlen(name), sizeof(wchar_t)); - wchar_t * wvalue = calloc(strlen(value), sizeof(wchar_t)); + if ( offset != -1 ) { + s[offset] = ( char ) 0; + char *name = s; + char *value = trim( &s[++offset] ); + wchar_t *wname = calloc( strlen( name ), sizeof( wchar_t ) ); + wchar_t *wvalue = calloc( strlen( value ), sizeof( wchar_t ) ); - mbstowcs(wname, name, strlen(name)); - mbstowcs(wvalue, value, strlen(value)); + mbstowcs( wname, name, strlen( name ) ); + mbstowcs( wvalue, value, strlen( value ) ); - cell->payload.stream.meta = make_cons( - make_cons( - c_string_to_lisp_keyword( wname), - c_string_to_lisp_string(wvalue)), - cell->payload.stream.meta); + cell->payload.stream.meta = + make_cons( make_cons + ( c_string_to_lisp_keyword( wname ), + c_string_to_lisp_string( wvalue ) ), + cell->payload.stream.meta ); - debug_printf( DEBUG_IO, L"write_meta_callback: added header '%s': value '%s'\n", name, value); + debug_printf( DEBUG_IO, + L"write_meta_callback: added header '%s': value '%s'\n", + name, value ); + } + } else { + debug_print + ( L"Pointer passed to write_meta_callback did not point to a stream: ", + DEBUG_IO ); + debug_dump_object( stream, DEBUG_IO ); } - } else { - debug_print( L"Pointer passed to write_meta_callback did not point to a stream: ", DEBUG_IO); - debug_dump_object(stream, DEBUG_IO); - } - return nmemb; + return nmemb; } void collect_meta( struct cons_pointer stream, struct cons_pointer url ) { - URL_FILE * s = pointer2cell(stream).payload.stream.stream; + URL_FILE *s = pointer2cell( stream ).payload.stream.stream; switch ( s->type ) { case CFTYPE_NONE: @@ -315,8 +321,9 @@ void collect_meta( struct cons_pointer stream, struct cons_pointer url ) { case CFTYPE_CURL: curl_easy_setopt( s->handle.curl, CURLOPT_VERBOSE, 1L ); curl_easy_setopt( s->handle.curl, CURLOPT_HEADER, 1L ); - curl_easy_setopt( s->handle.curl, CURLOPT_HEADERFUNCTION, write_meta_callback); - curl_easy_setopt( s->handle.curl, CURLOPT_HEADERDATA, stream); + curl_easy_setopt( s->handle.curl, CURLOPT_HEADERFUNCTION, + write_meta_callback ); + curl_easy_setopt( s->handle.curl, CURLOPT_HEADERDATA, stream ); break; } } @@ -338,38 +345,37 @@ void collect_meta( struct cons_pointer stream, struct cons_pointer url ) { * on my stream, if any, else NIL. */ struct cons_pointer - lisp_open( struct stack_frame *frame, struct cons_pointer frame_pointer, - struct cons_pointer env ) { - struct cons_pointer result = NIL; +lisp_open( struct stack_frame *frame, struct cons_pointer frame_pointer, + struct cons_pointer env ) { + struct cons_pointer result = NIL; - if ( stringp( frame->arg[0] ) ) { - struct cons_pointer meta = - make_cons( make_cons( - c_string_to_lisp_keyword( L"url" ), - frame->arg[0] ), - NIL ); + if ( stringp( frame->arg[0] ) ) { + struct cons_pointer meta = + make_cons( make_cons( c_string_to_lisp_keyword( L"url" ), + frame->arg[0] ), + NIL ); - char *url = lisp_string_to_c_string( frame->arg[0] ); + char *url = lisp_string_to_c_string( frame->arg[0] ); - if ( nilp( frame->arg[1] ) ) { - URL_FILE *stream = url_fopen( url, "r" ); - result = make_read_stream( stream, meta ); - } else { - // TODO: anything more complex is a problem for another day. - URL_FILE *stream = url_fopen( url, "w" ); - result = make_write_stream( stream, meta); + if ( nilp( frame->arg[1] ) ) { + URL_FILE *stream = url_fopen( url, "r" ); + result = make_read_stream( stream, meta ); + } else { + // TODO: anything more complex is a problem for another day. + URL_FILE *stream = url_fopen( url, "w" ); + result = make_write_stream( stream, meta ); + } + + free( url ); + + if ( pointer2cell( result ).payload.stream.stream == NULL ) { + result = NIL; + } else { + collect_meta( result, frame->arg[0] ); + } } - free( url ); - - if ( pointer2cell( result ).payload.stream.stream == NULL ) { - result = NIL; - } else { - collect_meta( result, frame->arg[0]); - } - } - - return result; + return result; } /** @@ -392,8 +398,8 @@ lisp_read_char( struct stack_frame *frame, struct cons_pointer frame_pointer, if ( readp( frame->arg[0] ) ) { result = make_string( url_fgetwc - ( pointer2cell( frame->arg[0] ).payload. - stream.stream ), NIL ); + ( pointer2cell( frame->arg[0] ).payload.stream. + stream ), NIL ); } return result; diff --git a/src/io/io.h b/src/io/io.h index 167660b..33f733f 100644 --- a/src/io/io.h +++ b/src/io/io.h @@ -15,7 +15,7 @@ extern CURLSH *io_share; -int io_init(); +int io_init( ); URL_FILE *file_to_url_file( FILE * f ); wint_t url_fgetwc( URL_FILE * input ); diff --git a/src/ops/print.c b/src/io/print.c similarity index 99% rename from src/ops/print.c rename to src/io/print.c index e13f17a..854c63a 100644 --- a/src/ops/print.c +++ b/src/io/print.c @@ -35,7 +35,7 @@ int print_use_colours = 0; * don't print anything but just return. */ void print_string_contents( URL_FILE * output, struct cons_pointer pointer ) { - while ( stringp( pointer ) || symbolp( pointer ) || keywordp(pointer)) { + while ( stringp( pointer ) || symbolp( pointer ) || keywordp( pointer ) ) { struct cons_space_object *cell = &pointer2cell( pointer ); wchar_t c = cell->payload.string.character; diff --git a/src/ops/print.h b/src/io/print.h similarity index 100% rename from src/ops/print.h rename to src/io/print.h diff --git a/src/ops/read.c b/src/io/read.c similarity index 99% rename from src/ops/read.c rename to src/io/read.c index 7362ecb..c49d043 100644 --- a/src/ops/read.c +++ b/src/io/read.c @@ -355,8 +355,8 @@ struct cons_pointer read_symbol_or_key( URL_FILE * input, char *tag, make_symbol_or_key( initial, read_symbol_or_key( input, tag, - url_fgetwc( input ) ), - tag ); + url_fgetwc + ( input ) ), tag ); } else { result = NIL; /* diff --git a/src/ops/read.h b/src/io/read.h similarity index 100% rename from src/ops/read.h rename to src/io/read.h diff --git a/src/memory/conspage.c b/src/memory/conspage.c index 5f8c3a8..2d0958d 100644 --- a/src/memory/conspage.c +++ b/src/memory/conspage.c @@ -168,7 +168,7 @@ void free_cell( struct cons_pointer pointer ) { break; case READTV: case WRITETV: - dec_ref(cell->payload.stream.meta); + dec_ref( cell->payload.stream.meta ); url_fclose( cell->payload.stream.stream ); break; case SPECIALTV: diff --git a/src/memory/consspaceobject.c b/src/memory/consspaceobject.c index f7b5ca9..816618f 100644 --- a/src/memory/consspaceobject.c +++ b/src/memory/consspaceobject.c @@ -158,7 +158,7 @@ make_function( struct cons_pointer meta, struct cons_pointer ( *executable ) struct cons_pointer, struct cons_pointer ) ) { struct cons_pointer pointer = allocate_cell( FUNCTIONTAG ); struct cons_space_object *cell = &pointer2cell( pointer ); - inc_ref( meta); + inc_ref( meta ); cell->payload.function.meta = meta; cell->payload.function.executable = executable; @@ -261,11 +261,11 @@ struct cons_pointer make_symbol_or_key( wint_t c, struct cons_pointer tail, if ( strncmp( tag, KEYTAG, 4 ) == 0 ) { struct cons_pointer r = internedp( result, oblist ); - if ( nilp(r)) { - intern(result, oblist); - } else { - result = r; - } + if ( nilp( r ) ) { + intern( result, oblist ); + } else { + result = r; + } } return result; @@ -280,7 +280,7 @@ make_special( struct cons_pointer meta, struct cons_pointer ( *executable ) struct cons_pointer, struct cons_pointer env ) ) { struct cons_pointer pointer = allocate_cell( SPECIALTAG ); struct cons_space_object *cell = &pointer2cell( pointer ); - inc_ref( meta); + inc_ref( meta ); cell->payload.special.meta = meta; cell->payload.special.executable = executable; diff --git a/src/memory/dump.c b/src/memory/dump.c index 7f7701f..28bd36a 100644 --- a/src/memory/dump.c +++ b/src/memory/dump.c @@ -108,14 +108,14 @@ void dump_object( URL_FILE * output, struct cons_pointer pointer ) { case RATIOTV: url_fwprintf( output, L"\t\tRational cell: value %ld/%ld, count %u\n", - pointer2cell( cell.payload.ratio.dividend ).payload. - integer.value, - pointer2cell( cell.payload.ratio.divisor ).payload. - integer.value, cell.count ); + pointer2cell( cell.payload.ratio.dividend ). + payload.integer.value, + pointer2cell( cell.payload.ratio.divisor ). + payload.integer.value, cell.count ); break; case READTV: - url_fputws( L"\t\tInput stream; metadata: ", output ); - print(output, cell.payload.stream.meta); + url_fputws( L"\t\tInput stream; metadata: ", output ); + print( output, cell.payload.stream.meta ); url_fputws( L"\n", output ); break; case REALTV: @@ -150,8 +150,8 @@ void dump_object( URL_FILE * output, struct cons_pointer pointer ) { } break; case WRITETV: - url_fputws( L"\t\tOutput stream; metadata: ", output ); - print(output, cell.payload.stream.meta); + url_fputws( L"\t\tOutput stream; metadata: ", output ); + print( output, cell.payload.stream.meta ); url_fputws( L"\n", output ); break; } diff --git a/src/ops/lispops.c b/src/ops/lispops.c index 91ec2cf..e390ac0 100644 --- a/src/ops/lispops.c +++ b/src/ops/lispops.c @@ -352,10 +352,9 @@ c_apply( struct stack_frame *frame, struct cons_pointer frame_pointer, result = next_pointer; } else { result = - ( *fn_cell.payload. - special.executable ) ( get_stack_frame - ( next_pointer ), - next_pointer, env ); + ( *fn_cell.payload.special. + executable ) ( get_stack_frame( next_pointer ), + next_pointer, env ); debug_print( L"Special form returning: ", DEBUG_EVAL ); debug_print_object( result, DEBUG_EVAL ); debug_println( DEBUG_EVAL ); @@ -842,7 +841,9 @@ struct cons_pointer c_reverse( struct cons_pointer arg ) { result = make_string( o.payload.string.character, result ); break; case SYMBOLTV: - result = make_symbol_or_key( o.payload.string.character, result, SYMBOLTAG ); + result = + make_symbol_or_key( o.payload.string.character, result, + SYMBOLTAG ); break; } } @@ -1208,13 +1209,13 @@ struct cons_pointer lisp_source( struct stack_frame *frame, struct cons_pointer env ) { struct cons_pointer result = NIL; struct cons_space_object cell = pointer2cell( frame->arg[0] ); - struct cons_pointer source_key = c_string_to_lisp_keyword(L"source"); + struct cons_pointer source_key = c_string_to_lisp_keyword( L"source" ); switch ( cell.tag.value ) { case FUNCTIONTV: - result = c_assoc( source_key, cell.payload.function.meta); + result = c_assoc( source_key, cell.payload.function.meta ); break; case SPECIALTV: - result = c_assoc( source_key, cell.payload.special.meta); + result = c_assoc( source_key, cell.payload.special.meta ); break; case LAMBDATV: result = make_cons( c_string_to_lisp_symbol( L"lambda" ), diff --git a/src/ops/meta.c b/src/ops/meta.c index 5e48709..a27d2af 100644 --- a/src/ops/meta.c +++ b/src/ops/meta.c @@ -17,31 +17,29 @@ * * @return a pointer to the metadata of my first argument, or nil if none. */ -struct cons_pointer lisp_metadata( struct stack_frame *frame, struct cons_pointer frame_pointer, - struct cons_pointer env ) { - debug_print(L"lisp_metadata: entered\n", DEBUG_EVAL); - debug_dump_object(frame->arg[0], DEBUG_EVAL); - struct cons_pointer result = NIL; - struct cons_space_object cell = pointer2cell(frame->arg[0]); +struct cons_pointer lisp_metadata( struct stack_frame *frame, + struct cons_pointer frame_pointer, + struct cons_pointer env ) { + debug_print( L"lisp_metadata: entered\n", DEBUG_EVAL ); + debug_dump_object( frame->arg[0], DEBUG_EVAL ); + struct cons_pointer result = NIL; + struct cons_space_object cell = pointer2cell( frame->arg[0] ); - switch( cell.tag.value) { - case FUNCTIONTV: - result = cell.payload.function.meta; - break; - case SPECIALTV: - result = cell.payload.special.meta; - break; - case READTV: - case WRITETV: - result = cell.payload.special.meta; - break; - } + switch ( cell.tag.value ) { + case FUNCTIONTV: + result = cell.payload.function.meta; + break; + case SPECIALTV: + result = cell.payload.special.meta; + break; + case READTV: + case WRITETV: + result = cell.payload.stream.meta; + break; + } - return make_cons( - make_cons( - c_string_to_lisp_keyword( L"type"), - c_type(frame->arg[0])), - result); + return make_cons( make_cons( c_string_to_lisp_keyword( L"type" ), + c_type( frame->arg[0] ) ), result ); // return result; } diff --git a/src/ops/meta.h b/src/ops/meta.h index 2c6ccf2..f441a50 100644 --- a/src/ops/meta.h +++ b/src/ops/meta.h @@ -11,7 +11,8 @@ #define __psse_meta_h -struct cons_pointer lisp_metadata( struct stack_frame *frame, struct cons_pointer frame_pointer, - struct cons_pointer env ) ; +struct cons_pointer lisp_metadata( struct stack_frame *frame, + struct cons_pointer frame_pointer, + struct cons_pointer env ); #endif