lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


>From lhf Wed May 21 16:17:36 2003
To: lua@bazar2.conectiva.com.br
Subject: Re: Cross Compiling Lua

I wrote:
>I've written a version of etc/min.c to test this that I attach below.

but forgot to attach the file. Here it is.
--lhf

/*
* zlib.c -- minimal Lua interpreter that loads compressed and uncompressed files
*/

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <zlib.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

static const char *getF(lua_State *L, void *ud, size_t *size)
{
 gzFile *f=(gzFile *)ud;
 static char buff[512];
 if (gzeof(f)) return NULL;
 *size=gzread(f,buff,sizeof(buff));
 return (*size>0) ? buff : NULL;
}

int luaL_gzloadfile(lua_State *L, const char *filename)
{
 int status;
 gzFile f=gzopen(filename,"rb");
 if (f==NULL) {
  const char *why = (errno==0) ? "not enough memory in zlib" : strerror(errno);
  luaL_error(L,"cannot open %s: %s",filename,why); 
 }
 status=lua_load(L,getF,f,filename);
 gzclose(f);
 return status;
}

static int pmain(lua_State *L)
{
 lua_baselibopen(L);
 lua_settop(L,0);
 if (luaL_gzloadfile(L,"luac.out.gz") || lua_pcall(L,0,0,0))
  fprintf(stderr,"pmain: %s\n",lua_tostring(L,-1));
 return 0;
}

int main(void)
{
 lua_State *L=lua_open();
 if (lua_cpcall(L,&pmain,NULL)!=0)
  fprintf(stderr,"main: %s\n",lua_tostring(L,-1));
 lua_close(L);
 return 0;
}