diff options
Diffstat (limited to 'lprefix.h')
-rw-r--r-- | lprefix.h | 102 |
1 files changed, 102 insertions, 0 deletions
@@ -173,3 +173,105 @@ LUAMOD_API int luaopen_compat53_string (lua_State *L) { | |||
173 | 173 | ||
174 | #endif | 174 | #endif |
175 | 175 | ||
176 | |||
177 | #ifdef liolib_c | ||
178 | /* move the io library open function out of the way (we only take | ||
179 | * the popen and type functions)! | ||
180 | */ | ||
181 | # define luaopen_io luaopen_io_XXX | ||
182 | |||
183 | #include <locale.h> | ||
184 | #include <lualib.h> | ||
185 | |||
186 | # if !defined(lua_getlocaledecpoint) | ||
187 | # define lua_getlocaledecpoint() (localeconv()->decimal_point[0]) | ||
188 | # endif | ||
189 | |||
190 | # ifndef LUA_INTEGER_FMT | ||
191 | # define LUA_INTEGER_FMT "%ld" | ||
192 | # endif | ||
193 | # ifndef LUAI_UACINT | ||
194 | # define LUAI_UACINT lua_Integer | ||
195 | # endif | ||
196 | |||
197 | /* choose which popen implementation to pick */ | ||
198 | # if (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \ | ||
199 | (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600) || \ | ||
200 | defined(__APPLE__) | ||
201 | # define LUA_USE_POSIX 1 | ||
202 | # elif (defined(_MSC_VER) | ||
203 | # define LUA_USE_WINDOWS 0 | ||
204 | # endif | ||
205 | |||
206 | typedef struct COMPAT53_luaL_Stream { | ||
207 | FILE *f; /* stream (NULL for incompletely created streams) */ | ||
208 | lua_CFunction closef; /* to close stream (NULL for closed streams) */ | ||
209 | } COMPAT53_luaL_Stream; | ||
210 | |||
211 | #define luaL_Stream COMPAT53_luaL_Stream | ||
212 | |||
213 | #define COMPAT53_LUA_PFILEHANDLE "PFILE*" | ||
214 | |||
215 | static int io_ptype (lua_State *L) { | ||
216 | luaL_Stream *p; | ||
217 | luaL_checkany(L, 1); | ||
218 | p = (luaL_Stream *)luaL_testudata(L, 1, COMPAT53_LUA_PFILEHANDLE); | ||
219 | if (p) { | ||
220 | /* Lua 5.3 implementation, for popen files */ | ||
221 | if (p->closef == NULL) | ||
222 | lua_pushliteral(L, "closed file"); | ||
223 | else | ||
224 | lua_pushliteral(L, "file"); | ||
225 | return 1; | ||
226 | } else { | ||
227 | /* Lua 5.1 implementation, for plain files */ | ||
228 | void *ud = lua_touserdata(L, 1); | ||
229 | lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE); | ||
230 | if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1)) | ||
231 | lua_pushnil(L); /* not a file */ | ||
232 | else if (*((FILE **)ud) == NULL) | ||
233 | lua_pushliteral(L, "closed file"); | ||
234 | else | ||
235 | lua_pushliteral(L, "file"); | ||
236 | return 1; | ||
237 | } | ||
238 | } | ||
239 | |||
240 | static int io_popen (lua_State *L); | ||
241 | static void createmeta (lua_State *L); | ||
242 | |||
243 | # undef LUA_FILEHANDLE | ||
244 | # define LUA_FILEHANDLE COMPAT53_LUA_PFILEHANDLE | ||
245 | |||
246 | /* for PUC-Rio Lua 5.1 only */ | ||
247 | # if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501 && !defined(LUA_JITLIBNAME) | ||
248 | |||
249 | LUAMOD_API int luaopen_compat53_io (lua_State *L) { | ||
250 | luaL_Reg const funcs[] = { | ||
251 | { "popen", io_popen }, | ||
252 | { "type", io_ptype }, | ||
253 | { NULL, NULL } | ||
254 | }; | ||
255 | luaL_newlib(L, funcs); | ||
256 | createmeta(L); | ||
257 | return 1; | ||
258 | } | ||
259 | |||
260 | # endif /* for PUC-Rio Lua 5.1 only */ | ||
261 | |||
262 | /* fake CLANG feature detection on other compilers */ | ||
263 | # ifndef __has_attribute | ||
264 | # define __has_attribute(x) 0 | ||
265 | # endif | ||
266 | /* make luaopen_io(_XXX) static, so it (and all other referenced | ||
267 | * io functions) won't be included in the resulting dll | ||
268 | * (hopefully). | ||
269 | */ | ||
270 | # undef LUAMOD_API | ||
271 | # if defined(__GNUC__) || __has_attribute(__unused__) | ||
272 | # define LUAMOD_API __attribute__((__unused__)) static | ||
273 | # else | ||
274 | # define LUAMOD_API static | ||
275 | # endif | ||
276 | |||
277 | #endif /* iolib.c */ | ||