[package] lua: add support for loading gzip compressed source files, bump package...
[openwrt.git] / package / lua / patches / 040-gzip-source-loader.patch
1 diff -ur lua-5.1.4.orig/src/Makefile lua-5.1.4/src/Makefile
2 --- lua-5.1.4.orig/src/Makefile 2009-04-04 23:06:04.000000000 +0200
3 +++ lua-5.1.4/src/Makefile      2009-04-04 23:06:15.000000000 +0200
4 @@ -12,7 +12,7 @@
5  AR= ar rcu
6  RANLIB= ranlib
7  RM= rm -f
8 -LIBS= -lm $(MYLIBS)
9 +LIBS= -lm -lz $(MYLIBS)
10  
11  MYCFLAGS=
12  MYLDFLAGS=
13 diff -ur lua-5.1.4.orig/src/lauxlib.c lua-5.1.4/src/lauxlib.c
14 --- lua-5.1.4.orig/src/lauxlib.c        2009-04-04 23:06:04.000000000 +0200
15 +++ lua-5.1.4/src/lauxlib.c     2009-04-05 00:03:33.000000000 +0200
16 @@ -11,6 +11,7 @@
17  #include <stdio.h>
18  #include <stdlib.h>
19  #include <string.h>
20 +#include <zlib.h>
21  
22  
23  /* This file uses only the official API of Lua.
24 @@ -535,6 +536,12 @@
25    char buff[LUAL_BUFFERSIZE];
26  } LoadF;
27  
28 +typedef struct LoadGZ {
29 +  int first_chunk;
30 +  gzFile f;
31 +  char buffer[LUAL_GZLDBUFFER];
32 +} LoadGZ;
33 +
34  
35  static const char *getF (lua_State *L, void *ud, size_t *size) {
36    LoadF *lf = (LoadF *)ud;
37 @@ -550,6 +557,26 @@
38  }
39  
40  
41 +static const char *getGZ (lua_State *L, void *ud, size_t *size) {
42 +  LoadGZ *lf = (LoadGZ *)ud;
43 +  char *sp = 0;
44 +  (void)L;
45 +  if (gzeof(lf->f)) return NULL;
46 +  *size = gzread(lf->f, lf->buffer, sizeof(lf->buffer));
47 +  if (*size > 0) {
48 +    if (lf->first_chunk) {
49 +      lf->first_chunk = 0;
50 +      if (strstr(lf->buffer, "#!") && (sp=strstr(lf->buffer, "\n")) != NULL) {
51 +        *size -= ((uint)sp - (uint)lf->buffer);
52 +        return sp;
53 +      }
54 +    }
55 +    return lf->buffer;
56 +  }
57 +  return NULL;
58 +}
59 +
60 +
61  static int errfile (lua_State *L, const char *what, int fnameindex) {
62    const char *serr = strerror(errno);
63    const char *filename = lua_tostring(L, fnameindex) + 1;
64 @@ -560,6 +587,31 @@
65  
66  
67  LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
68 +  if ((filename != NULL) && strstr(filename, ".lua.gz")) {
69 +    return luaL_loadfile_gzip(L, filename);
70 +  }
71 +  else {
72 +    return luaL_loadfile_plain(L, filename);
73 +  }
74 +}
75 +
76 +
77 +LUALIB_API int luaL_loadfile_gzip (lua_State *L, const char *filename) {
78 +  LoadGZ gzf;
79 +  int status;
80 +  int fnameindex = lua_gettop(L) + 1;  /* index of filename on the stack */
81 +  lua_pushfstring(L, "@%s", filename);
82 +  gzf.f = gzopen(filename, "r");
83 +  gzf.first_chunk = 1;
84 +  if (gzf.f == Z_NULL) return errfile(L, "open", fnameindex);
85 +  status = lua_load(L, getGZ, &gzf, lua_tostring(L, -1));
86 +  (void)gzclose(gzf.f);
87 +  lua_remove(L, fnameindex);
88 +  return status;
89 +}
90 +
91 +  
92 +LUALIB_API int luaL_loadfile_plain (lua_State *L, const char *filename) {
93    LoadF lf;
94    int status, readstatus;
95    int c;
96 diff -ur lua-5.1.4.orig/src/lauxlib.h lua-5.1.4/src/lauxlib.h
97 --- lua-5.1.4.orig/src/lauxlib.h        2009-04-04 23:06:04.000000000 +0200
98 +++ lua-5.1.4/src/lauxlib.h     2009-04-04 23:06:15.000000000 +0200
99 @@ -81,6 +81,8 @@
100  LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref);
101  
102  LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename);
103 +LUALIB_API int (luaL_loadfile_gzip) (lua_State *L, const char *filename);
104 +LUALIB_API int (luaL_loadfile_plain) (lua_State *L, const char *filename);
105  LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz,
106                                    const char *name);
107  LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
108 diff -ur lua-5.1.4.orig/src/luaconf.h lua-5.1.4/src/luaconf.h
109 --- lua-5.1.4.orig/src/luaconf.h        2009-04-04 23:06:04.000000000 +0200
110 +++ lua-5.1.4/src/luaconf.h     2009-04-04 23:27:20.000000000 +0200
111 @@ -101,7 +101,9 @@
112  #define LUA_CDIR       LUA_ROOT "lib/lua/5.1/"
113  #define LUA_PATH_DEFAULT  \
114                 "./?.lua;"  LUA_LDIR"?.lua;"  LUA_LDIR"?/init.lua;" \
115 -                           LUA_CDIR"?.lua;"  LUA_CDIR"?/init.lua"
116 +                           LUA_CDIR"?.lua;"  LUA_CDIR"?/init.lua;" \
117 +               "./?.lua.gz;"  LUA_LDIR"?.lua.gz;"  LUA_LDIR"?/init.lua.gz;" \
118 +                              LUA_CDIR"?.lua.gz;"  LUA_CDIR"?/init.lua.gz"
119  #define LUA_CPATH_DEFAULT \
120         "./?.so;"  LUA_CDIR"?.so;" LUA_CDIR"loadall.so"
121  #endif
122 @@ -506,6 +508,12 @@
123  */
124  #define LUAL_BUFFERSIZE                BUFSIZ
125  
126 +
127 +/*
128 +@@ LUAL_GZLDBUFFER is the buffer size used by the gzip source loader.
129 +*/
130 +#define LUAL_GZLDBUFFER                8192
131 +
132  /* }================================================================== */
133  
134