Example #1
0
int terra_loadfile(lua_State * L, const char * file) {
    FileReaderCtx ctx;
    ctx.fp = file ? fopen(file,"r") : stdin;
    if(!ctx.fp) {
       terra_State * T = getterra(L);
       terra_pusherror(T,"failed to open file '%s'",file);
       return LUA_ERRFILE;
    }
    /*peek to see if we have a POSIX comment '#', which we repect on the first like for #! */
    int c = fgetc(ctx.fp);
    ungetc(c,ctx.fp);
    if(c == '#') { /* skip the POSIX comment */
        do {
            c = fgetc(ctx.fp);   
        } while(c != '\n' && c != EOF);
        if(c == '\n')
            ungetc(c,ctx.fp); /* keep line count accurate */
    }
    if(file) {
        char * name = (char *) malloc(strlen(file) + 2);
        sprintf(name,"@%s",file);
        int r = terra_load(L,reader_file,&ctx,name);    
        free(name);
        fclose(ctx.fp);
        return r;
    } else {
        return terra_load(L,reader_file,&ctx,"@=stdin");
    }
}
Example #2
0
int terra_luaload(lua_State * L) {
    const char * chunkname = "=(load)";
    size_t fnvalue;
    if(lua_gettop(L) == 2) {
        chunkname = luaL_checkstring(L,-1);
        fnvalue = lua_gettop(L) - 1;
    } else {
        fnvalue = lua_gettop(L);
    }
    if(terra_load(L,reader_luaload,(void*)fnvalue,chunkname)) {
        lua_pushnil(L);
        lua_pushvalue(L,-2);
        lua_remove(L,-3);
        return 2;
    }
    return 1;
}
Example #3
0
int terra_loadbuffer(lua_State * L, const char *buf, size_t size, const char *name) {
    StringReaderCtx ctx;
    ctx.str = buf;
    ctx.size = size;
    return terra_load(L,reader_string,&ctx,name);
}