-
Notifications
You must be signed in to change notification settings - Fork 38
/
lprefix.h
282 lines (241 loc) · 7.04 KB
/
lprefix.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
** $Id: lprefix.h,v 1.2 2014/12/29 16:54:13 roberto Exp $
** Definitions for Lua code that must come before any other header file
** See Copyright Notice in lua.h
*/
#ifndef lprefix_h
#define lprefix_h
/*
** Allows POSIX/XSI stuff
*/
#if !defined(LUA_USE_C89) /* { */
#if !defined(_XOPEN_SOURCE)
#define _XOPEN_SOURCE 600
#elif _XOPEN_SOURCE == 0
#undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */
#endif
/*
** Allows manipulation of large files in gcc and some other compilers
*/
#if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS)
#define _LARGEFILE_SOURCE 1
#define _FILE_OFFSET_BITS 64
#endif
#endif /* } */
/*
** Windows stuff
*/
#if defined(_WIN32) /* { */
#if !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */
#endif
#endif /* } */
/* COMPAT53 adaptation */
#include "c-api/compat-5.3.h"
#undef LUAMOD_API
#define LUAMOD_API extern
#ifdef lutf8lib_c
# define luaopen_utf8 luaopen_compat53_utf8
/* we don't support the %U format string of lua_pushfstring!
* code below adapted from the Lua 5.3 sources:
*/
static const char *compat53_utf8_escape (lua_State* L, long x) {
if (x < 0x80) { /* ASCII */
char c = (char)x;
lua_pushlstring(L, &c, 1);
} else {
char buff[8] = { 0 };
unsigned int mfb = 0x3f;
int n = 1;
do {
buff[8 - (n++)] = (char)(0x80|(x & 0x3f));
x >>= 6;
mfb >>= 1;
} while (x > mfb);
buff[8-n] = (char)((~mfb << 1) | x);
lua_pushlstring(L, buff+8-n, n);
}
return lua_tostring(L, -1);
}
# define lua_pushfstring(L, fmt, l) \
compat53_utf8_escape(L, l)
#endif
#ifdef ltablib_c
# define luaopen_table luaopen_compat53_table
# ifndef LUA_MAXINTEGER
/* conservative estimate: */
# define LUA_MAXINTEGER INT_MAX
# endif
#endif /* ltablib_c */
#ifdef lstrlib_c
#include <locale.h>
#include <lualib.h>
/* move the string library open function out of the way (we only take
* the string packing functions)!
*/
# define luaopen_string luaopen_string_XXX
/* used in string.format implementation, which we don't use: */
# ifndef LUA_INTEGER_FRMLEN
# define LUA_INTEGER_FRMLEN ""
# define LUA_NUMBER_FRMLEN ""
# endif
# ifndef LUA_MININTEGER
# define LUA_MININTEGER 0
# endif
# ifndef LUA_INTEGER_FMT
# define LUA_INTEGER_FMT "%d"
# endif
# ifndef LUAI_UACINT
# define LUAI_UACINT lua_Integer
# endif
/* different Lua 5.3 versions have conflicting variants of this macro
* in luaconf.h, there's a fallback implementation in lstrlib.c, and
* the macro isn't used for string (un)packing anyway!
* */
# undef lua_number2strx
# if LUA_VERSION_NUM < 503
/* lstrlib assumes that lua_Integer and lua_Unsigned have the same
* size, so we use the unsigned equivalent of ptrdiff_t! */
# define lua_Unsigned size_t
# endif
# ifndef l_mathlim
# ifdef LUA_NUMBER_DOUBLE
# define l_mathlim(n) (DBL_##n)
# else
# define l_mathlim(n) (FLT_##n)
# endif
# endif
# ifndef l_mathop
# ifdef LUA_NUMBER_DOUBLE
# define l_mathop(op) op
# else
# define l_mathop(op) op##f
# endif
# endif
# ifndef lua_getlocaledecpoint
# define lua_getlocaledecpoint() (localeconv()->decimal_point[0])
# endif
# ifndef l_sprintf
# if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
# define l_sprintf(s,sz,f,i) (snprintf(s, sz, f, i))
# else
# define l_sprintf(s,sz,f,i) ((void)(sz), sprintf(s, f, i))
# endif
# endif
static int str_pack (lua_State *L);
static int str_packsize (lua_State *L);
static int str_unpack (lua_State *L);
LUAMOD_API int luaopen_compat53_string (lua_State *L) {
luaL_Reg const funcs[] = {
{ "pack", str_pack },
{ "packsize", str_packsize },
{ "unpack", str_unpack },
{ NULL, NULL }
};
luaL_newlib(L, funcs);
return 1;
}
/* fake CLANG feature detection on other compilers */
# ifndef __has_attribute
# define __has_attribute(x) 0
# endif
/* make luaopen_string(_XXX) static, so it (and all other referenced
* string functions) won't be included in the resulting dll
* (hopefully).
*/
# undef LUAMOD_API
# if defined(__GNUC__) || __has_attribute(__unused__)
# define LUAMOD_API __attribute__((__unused__)) static
# else
# define LUAMOD_API static
# endif
#endif /* lstrlib.c */
#endif
#ifdef liolib_c
/* move the io library open function out of the way (we only take
* the popen and type functions)!
*/
# define luaopen_io luaopen_io_XXX
#include <locale.h>
#include <lualib.h>
# if !defined(lua_getlocaledecpoint)
# define lua_getlocaledecpoint() (localeconv()->decimal_point[0])
# endif
# ifndef LUA_INTEGER_FMT
# define LUA_INTEGER_FMT "%ld"
# endif
# ifndef LUAI_UACINT
# define LUAI_UACINT lua_Integer
# endif
/* choose which popen implementation to pick */
# if (defined(_WIN32) && !defined(__CYGWIN__))
# define LUA_USE_WINDOWS 1
# endif
# if (!defined(LUA_USE_WINDOWS) && !defined(LUA_USE_POSIX)) && \
((defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \
(defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600) || \
defined(__APPLE__))
# define LUA_USE_POSIX 1
# endif
typedef struct COMPAT53_luaL_Stream {
FILE *f; /* stream (NULL for incompletely created streams) */
lua_CFunction closef; /* to close stream (NULL for closed streams) */
} COMPAT53_luaL_Stream;
#define luaL_Stream COMPAT53_luaL_Stream
#define COMPAT53_LUA_PFILEHANDLE "PFILE*"
static int io_ptype (lua_State *L) {
luaL_Stream *p;
luaL_checkany(L, 1);
p = (luaL_Stream *)luaL_testudata(L, 1, COMPAT53_LUA_PFILEHANDLE);
if (p) {
/* Lua 5.3 implementation, for popen files */
if (p->closef == NULL)
lua_pushliteral(L, "closed file");
else
lua_pushliteral(L, "file");
return 1;
} else {
/* Lua 5.1 implementation, for plain files */
void *ud = lua_touserdata(L, 1);
lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE);
if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1))
lua_pushnil(L); /* not a file */
else if (*((FILE **)ud) == NULL)
lua_pushliteral(L, "closed file");
else
lua_pushliteral(L, "file");
return 1;
}
}
static int io_popen (lua_State *L);
static void createmeta (lua_State *L);
# undef LUA_FILEHANDLE
# define LUA_FILEHANDLE COMPAT53_LUA_PFILEHANDLE
LUAMOD_API int luaopen_compat53_io (lua_State *L) {
luaL_Reg const funcs[] = {
/* for PUC-Rio Lua 5.1 only */
# if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501 && !defined(LUA_JITLIBNAME)
{ "popen", io_popen },
{ "type", io_ptype },
# endif /* for PUC-Rio Lua 5.1 only */
{ NULL, NULL }
};
luaL_newlib(L, funcs);
createmeta(L);
return 1;
}
/* fake CLANG feature detection on other compilers */
# ifndef __has_attribute
# define __has_attribute(x) 0
# endif
/* make luaopen_io(_XXX) static, so it (and all other referenced
* io functions) won't be included in the resulting dll
* (hopefully).
*/
# undef LUAMOD_API
# if defined(__GNUC__) || __has_attribute(__unused__)
# define LUAMOD_API __attribute__((__unused__)) static
# else
# define LUAMOD_API static
# endif
#endif /* iolib.c */