ref: e7ec1e13f5e1814f647bcbc18e73eb6be5ce4f16
dir: /flmain.c/
#include "flisp.h"
#include "cvalues.h"
#include "print.h"
#include "iostream.h"
#include "ieee754.h"
#include "random.h"
#include "brieflz.h"
double D_PNAN, D_NNAN, D_PINF, D_NINF;
static value_t
argv_list(int argc, char *argv[])
{
int i;
value_t lst = FL_nil, temp;
fl_gc_handle(&lst);
fl_gc_handle(&temp);
for(i = argc-1; i >= 0; i--){
temp = cvalue_static_cstring(argv[i]);
lst = fl_cons(temp, lst);
}
fl_free_gc_handles(2);
return lst;
}
_Noreturn void
flmain(const uint8_t *boot, int bootsz, int argc, char **argv)
{
D_PNAN = D_NNAN = strtod("+NaN", nil);
D_PINF = D_NINF = strtod("+Inf", nil);
union ieee754_double *d;
d = (union ieee754_double *)&D_NNAN;
d->ieee.negative = 1;
d = (union ieee754_double *)&D_NINF;
d->ieee.negative = 1;
randomize();
ios_init_stdstreams();
mpsetminbits(sizeof(fixnum_t)*8);
#if defined(__macos__)
if(fl_init(INITIAL_HEAP_SIZE) != 0){
fprintf(stderr, "fl_init failed\n");
getchar();
#else
if(fl_init(2*1024*1024) != 0){
#endif
exit(1);
}
value_t f = cvalue(FL(iostreamtype), (int)sizeof(ios_t));
ios_t *s = value2c(ios_t*, f);
uint8_t *unpacked = nil;
if(boot[0] == 0){
uint32_t unpackedsz =
boot[1]<<0 |
boot[2]<<8 |
boot[3]<<16|
boot[4]<<24;
unpacked = MEM_ALLOC(unpackedsz);
unsigned long n = blz_depack_safe(boot+5, bootsz-5, unpacked, unpackedsz);
if(n == BLZ_ERROR){
ios_puts(ios_stderr, "failed to unpack boot image\n");
fl_exit(1);
}
boot = unpacked;
bootsz = n;
}
ios_static_buffer(s, boot, bootsz);
int r = 1;
FL_TRY_EXTERN{
if(fl_load_system_image(f) == 0){
MEM_FREE(unpacked);
ios_close(s);
fl_applyn(1, symbol_value(symbol("__start", false)), argv_list(argc, argv));
r = 0;
}
}
FL_CATCH_EXTERN_NO_RESTORE{
ios_puts(ios_stderr, "fatal error:\n");
fl_print(ios_stderr, FL(lasterror));
ios_putc(ios_stderr, '\n');
break;
}
fl_exit(r);
}