shithub: femtolisp

ref: 63d9386558212f72a4e6bc6ac70544030545d7c1
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 value_t fl_file(value_t *args, uint32_t nargs);

int
main(int argc, char **argv)
{
    char fname_buf[1024];
    value_t args[2];
    int r;

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

    fl_init(512*1024);

#if defined(INITFILE)
    snprintf(fname_buf, sizeof(fname_buf), "%s", INITFILE);
#else
    value_t str = symbol_value(symbol("*install-dir*"));
    char *exedir = (str == UNBOUND ? NULL : cvalue_data(str));
    snprintf(fname_buf, sizeof(fname_buf), "%s%sflisp.boot",
        exedir ? exedir : "",
        exedir ? PATHSEPSTRING : "");
#endif

    fl_gc_handle(&args[0]);
    fl_gc_handle(&args[1]);
    r = 1;
    FL_TRY_EXTERN {
        args[0] = cvalue_static_cstring(fname_buf);
        args[1] = symbol(":read");
        value_t f = fl_file(&args[0], 2);
        fl_free_gc_handles(2);
        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;
    }
    exit(r);
    return r;
}