shithub: femtolisp

ref: f5e20c5823337a159618f02c861af69879b0f0e8
dir: /flmain.c/

View raw version
#include "llt.h"
#include "flisp.h"

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;
}

extern fltype_t *iostreamtype;

#if defined(__plan9__)
void
#else
int
#endif
main(int argc, char **argv)
{
	static const char bootraw[] = {
#include "boot.h"
	};
	value_t f;
	ios_t *s;
	int r;

#if defined(__plan9__)
	argv0 = argv[0];
	setfcr(FPPDBL|FPRNR|FPOVFL);
	tmfmtinstall();
#endif

	fl_init(512*1024);

	f = cvalue(iostreamtype, sizeof(ios_t));
	s = value2c(ios_t*, f);
	ios_static_buffer(s, bootraw, sizeof(bootraw));

	r = 1;
	FL_TRY_EXTERN{
		if(fl_load_system_image(f) == 0){
			fl_applyn(1, symbol_value(symbol("__start")), argv_list(argc, argv));
			r = 0;
		}
	}
	FL_CATCH_EXTERN_NO_RESTORE{
		ios_puts("fatal error:\n", ios_stderr);
		fl_print(ios_stderr, fl_lasterror);
		ios_putc('\n', ios_stderr);
		break;
	}
#if defined(__plan9__)
	exit(r);
#else
	return r;
#endif
}