diff options
| -rw-r--r-- | .github/workflows/compat53-tests.yml | 2 | ||||
| -rw-r--r-- | .travis.yml | 2 | ||||
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | compat53/module.lua | 9 | ||||
| -rw-r--r-- | liolib.c | 776 | ||||
| -rw-r--r-- | lprefix.h | 102 | ||||
| -rw-r--r-- | rockspecs/compat53-scm-0.rockspec | 1 | ||||
| -rwxr-xr-x | tests/test.lua | 14 |
8 files changed, 906 insertions, 2 deletions
diff --git a/.github/workflows/compat53-tests.yml b/.github/workflows/compat53-tests.yml index d0dd3d4..40819ce 100644 --- a/.github/workflows/compat53-tests.yml +++ b/.github/workflows/compat53-tests.yml | |||
| @@ -55,7 +55,7 @@ jobs: | |||
| 55 | if [ "${{ matrix.external }}" = true ]; then DEF="-DCOMPAT53_PREFIX=compat53" SRC="c-api/compat-5.3.c"; fi | 55 | if [ "${{ matrix.external }}" = true ]; then DEF="-DCOMPAT53_PREFIX=compat53" SRC="c-api/compat-5.3.c"; fi |
| 56 | ${CC} ${CFLAGS} -Iold/include ${DEF} -shared -o old/testmod.so tests/testmod.c ${SRC} | 56 | ${CC} ${CFLAGS} -Iold/include ${DEF} -shared -o old/testmod.so tests/testmod.c ${SRC} |
| 57 | ${CC} ${CFLAGS} -Inew/include ${DEF} -shared -o new/testmod.so tests/testmod.c ${SRC} | 57 | ${CC} ${CFLAGS} -Inew/include ${DEF} -shared -o new/testmod.so tests/testmod.c ${SRC} |
| 58 | gcc ${CFLAGS} -Iold/include ${DEF} -shared -o old/compat53.so ltablib.c lutf8lib.c lstrlib.c ${SRC} | 58 | gcc ${CFLAGS} -Iold/include ${DEF} -shared -o old/compat53.so ltablib.c lutf8lib.c lstrlib.c liolib.c ${SRC} |
| 59 | - name: run test scripts | 59 | - name: run test scripts |
| 60 | run: | | 60 | run: | |
| 61 | (cd old && bin/lua ../tests/test.lua) > old.txt | 61 | (cd old && bin/lua ../tests/test.lua) > old.txt |
diff --git a/.travis.yml b/.travis.yml index af458d8..44ae3a0 100644 --- a/.travis.yml +++ b/.travis.yml | |||
| @@ -38,7 +38,7 @@ install: | |||
| 38 | - if [ "x${EXTERNAL:-}" = xtrue ]; then DEF="-DCOMPAT53_PREFIX=compat53" SRC="c-api/compat-5.3.c"; fi | 38 | - if [ "x${EXTERNAL:-}" = xtrue ]; then DEF="-DCOMPAT53_PREFIX=compat53" SRC="c-api/compat-5.3.c"; fi |
| 39 | - ${CC} ${CFLAGS} -Iold/include ${DEF} -shared -o old/testmod.so tests/testmod.c ${SRC} | 39 | - ${CC} ${CFLAGS} -Iold/include ${DEF} -shared -o old/testmod.so tests/testmod.c ${SRC} |
| 40 | - ${CC} ${CFLAGS} -Inew/include ${DEF} -shared -o new/testmod.so tests/testmod.c ${SRC} | 40 | - ${CC} ${CFLAGS} -Inew/include ${DEF} -shared -o new/testmod.so tests/testmod.c ${SRC} |
| 41 | - gcc ${CFLAGS} -Iold/include ${DEF} -shared -o old/compat53.so ltablib.c lutf8lib.c lstrlib.c ${SRC} | 41 | - gcc ${CFLAGS} -Iold/include ${DEF} -shared -o old/compat53.so ltablib.c lutf8lib.c lstrlib.c liolib.c ${SRC} |
| 42 | 42 | ||
| 43 | script: | 43 | script: |
| 44 | - (cd old && bin/lua ../tests/test.lua) > old.txt | 44 | - (cd old && bin/lua ../tests/test.lua) > old.txt |
| @@ -117,6 +117,8 @@ For Lua 5.1 additionally: | |||
| 117 | * `io.write` and `file:write` return file handle | 117 | * `io.write` and `file:write` return file handle |
| 118 | * `io.lines` and `file:lines` accept format arguments (like `io.read`) | 118 | * `io.lines` and `file:lines` accept format arguments (like `io.read`) |
| 119 | (see [here][10] and [here][11]) | 119 | (see [here][10] and [here][11]) |
| 120 | * `file:close` returns three results when a process is opened | ||
| 121 | with `io.popen` | ||
| 120 | * `debug.setmetatable` returns object | 122 | * `debug.setmetatable` returns object |
| 121 | * `debug.getuservalue` (see [here][12]) | 123 | * `debug.getuservalue` (see [here][12]) |
| 122 | * `debug.setuservalue` (see [here][13]) | 124 | * `debug.setuservalue` (see [here][13]) |
diff --git a/compat53/module.lua b/compat53/module.lua index 30be6b5..225d8ca 100644 --- a/compat53/module.lua +++ b/compat53/module.lua | |||
| @@ -67,6 +67,15 @@ if lua_version < "5.3" then | |||
| 67 | end | 67 | end |
| 68 | 68 | ||
| 69 | 69 | ||
| 70 | -- load io functions | ||
| 71 | local io_ok, iolib = pcall(require, "compat53.io") | ||
| 72 | if io_ok then | ||
| 73 | for k,v in pairs(iolib) do | ||
| 74 | M.io[k] = v | ||
| 75 | end | ||
| 76 | end | ||
| 77 | |||
| 78 | |||
| 70 | -- load string packing functions | 79 | -- load string packing functions |
| 71 | local str_ok, strlib = pcall(require, "compat53.string") | 80 | local str_ok, strlib = pcall(require, "compat53.string") |
| 72 | if str_ok then | 81 | if str_ok then |
diff --git a/liolib.c b/liolib.c new file mode 100644 index 0000000..8a9e75c --- /dev/null +++ b/liolib.c | |||
| @@ -0,0 +1,776 @@ | |||
| 1 | /* | ||
| 2 | ** $Id: liolib.c,v 2.151.1.1 2017/04/19 17:29:57 roberto Exp $ | ||
| 3 | ** Standard I/O (and system) library | ||
| 4 | ** See Copyright Notice in lua.h | ||
| 5 | */ | ||
| 6 | |||
| 7 | #define liolib_c | ||
| 8 | #define LUA_LIB | ||
| 9 | |||
| 10 | #include "lprefix.h" | ||
| 11 | |||
| 12 | |||
| 13 | #include <ctype.h> | ||
| 14 | #include <errno.h> | ||
| 15 | #include <locale.h> | ||
| 16 | #include <stdio.h> | ||
| 17 | #include <stdlib.h> | ||
| 18 | #include <string.h> | ||
| 19 | |||
| 20 | #include "lua.h" | ||
| 21 | |||
| 22 | #include "lauxlib.h" | ||
| 23 | #include "lualib.h" | ||
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | /* | ||
| 29 | ** Change this macro to accept other modes for 'fopen' besides | ||
| 30 | ** the standard ones. | ||
| 31 | */ | ||
| 32 | #if !defined(l_checkmode) | ||
| 33 | |||
| 34 | /* accepted extensions to 'mode' in 'fopen' */ | ||
| 35 | #if !defined(L_MODEEXT) | ||
| 36 | #define L_MODEEXT "b" | ||
| 37 | #endif | ||
| 38 | |||
| 39 | /* Check whether 'mode' matches '[rwa]%+?[L_MODEEXT]*' */ | ||
| 40 | static int l_checkmode (const char *mode) { | ||
| 41 | return (*mode != '\0' && strchr("rwa", *(mode++)) != NULL && | ||
| 42 | (*mode != '+' || (++mode, 1)) && /* skip if char is '+' */ | ||
| 43 | (strspn(mode, L_MODEEXT) == strlen(mode))); /* check extensions */ | ||
| 44 | } | ||
| 45 | |||
| 46 | #endif | ||
| 47 | |||
| 48 | /* | ||
| 49 | ** {====================================================== | ||
| 50 | ** l_popen spawns a new process connected to the current | ||
| 51 | ** one through the file streams. | ||
| 52 | ** ======================================================= | ||
| 53 | */ | ||
| 54 | |||
| 55 | #if !defined(l_popen) /* { */ | ||
| 56 | |||
| 57 | #if defined(LUA_USE_POSIX) /* { */ | ||
| 58 | |||
| 59 | #define l_popen(L,c,m) (fflush(NULL), popen(c,m)) | ||
| 60 | #define l_pclose(L,file) (pclose(file)) | ||
| 61 | |||
| 62 | #elif defined(LUA_USE_WINDOWS) /* }{ */ | ||
| 63 | |||
| 64 | #define l_popen(L,c,m) (_popen(c,m)) | ||
| 65 | #define l_pclose(L,file) (_pclose(file)) | ||
| 66 | |||
| 67 | #else /* }{ */ | ||
| 68 | |||
| 69 | /* ISO C definitions */ | ||
| 70 | #define l_popen(L,c,m) \ | ||
| 71 | ((void)((void)c, m), \ | ||
| 72 | luaL_error(L, "'popen' not supported"), \ | ||
| 73 | (FILE*)0) | ||
| 74 | #define l_pclose(L,file) ((void)L, (void)file, -1) | ||
| 75 | |||
| 76 | #endif /* } */ | ||
| 77 | |||
| 78 | #endif /* } */ | ||
| 79 | |||
| 80 | /* }====================================================== */ | ||
| 81 | |||
| 82 | |||
| 83 | #if !defined(l_getc) /* { */ | ||
| 84 | |||
| 85 | #if defined(LUA_USE_POSIX) | ||
| 86 | #define l_getc(f) getc_unlocked(f) | ||
| 87 | #define l_lockfile(f) flockfile(f) | ||
| 88 | #define l_unlockfile(f) funlockfile(f) | ||
| 89 | #else | ||
| 90 | #define l_getc(f) getc(f) | ||
| 91 | #define l_lockfile(f) ((void)0) | ||
| 92 | #define l_unlockfile(f) ((void)0) | ||
| 93 | #endif | ||
| 94 | |||
| 95 | #endif /* } */ | ||
| 96 | |||
| 97 | |||
| 98 | /* | ||
| 99 | ** {====================================================== | ||
| 100 | ** l_fseek: configuration for longer offsets | ||
| 101 | ** ======================================================= | ||
| 102 | */ | ||
| 103 | |||
| 104 | #if !defined(l_fseek) /* { */ | ||
| 105 | |||
| 106 | #if defined(LUA_USE_POSIX) /* { */ | ||
| 107 | |||
| 108 | #include <sys/types.h> | ||
| 109 | |||
| 110 | #define l_fseek(f,o,w) fseeko(f,o,w) | ||
| 111 | #define l_ftell(f) ftello(f) | ||
| 112 | #define l_seeknum off_t | ||
| 113 | |||
| 114 | #elif defined(LUA_USE_WINDOWS) && !defined(_CRTIMP_TYPEINFO) \ | ||
| 115 | && defined(_MSC_VER) && (_MSC_VER >= 1400) /* }{ */ | ||
| 116 | |||
| 117 | /* Windows (but not DDK) and Visual C++ 2005 or higher */ | ||
| 118 | #define l_fseek(f,o,w) _fseeki64(f,o,w) | ||
| 119 | #define l_ftell(f) _ftelli64(f) | ||
| 120 | #define l_seeknum __int64 | ||
| 121 | |||
| 122 | #else /* }{ */ | ||
| 123 | |||
| 124 | /* ISO C definitions */ | ||
| 125 | #define l_fseek(f,o,w) fseek(f,o,w) | ||
| 126 | #define l_ftell(f) ftell(f) | ||
| 127 | #define l_seeknum long | ||
| 128 | |||
| 129 | #endif /* } */ | ||
| 130 | |||
| 131 | #endif /* } */ | ||
| 132 | |||
| 133 | /* }====================================================== */ | ||
| 134 | |||
| 135 | |||
| 136 | #define IO_PREFIX "_IO_" | ||
| 137 | #define IOPREF_LEN (sizeof(IO_PREFIX)/sizeof(char) - 1) | ||
| 138 | #define IO_INPUT (IO_PREFIX "input") | ||
| 139 | #define IO_OUTPUT (IO_PREFIX "output") | ||
| 140 | |||
| 141 | |||
| 142 | typedef luaL_Stream LStream; | ||
| 143 | |||
| 144 | |||
| 145 | #define tolstream(L) ((LStream *)luaL_checkudata(L, 1, LUA_FILEHANDLE)) | ||
| 146 | |||
| 147 | #define isclosed(p) ((p)->closef == NULL) | ||
| 148 | |||
| 149 | |||
| 150 | static int io_type (lua_State *L) { | ||
| 151 | LStream *p; | ||
| 152 | luaL_checkany(L, 1); | ||
| 153 | p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE); | ||
| 154 | if (p == NULL) | ||
| 155 | lua_pushnil(L); /* not a file */ | ||
| 156 | else if (isclosed(p)) | ||
| 157 | lua_pushliteral(L, "closed file"); | ||
| 158 | else | ||
| 159 | lua_pushliteral(L, "file"); | ||
| 160 | return 1; | ||
| 161 | } | ||
| 162 | |||
| 163 | |||
| 164 | static int f_tostring (lua_State *L) { | ||
| 165 | LStream *p = tolstream(L); | ||
| 166 | if (isclosed(p)) | ||
| 167 | lua_pushliteral(L, "file (closed)"); | ||
| 168 | else | ||
| 169 | lua_pushfstring(L, "file (%p)", p->f); | ||
| 170 | return 1; | ||
| 171 | } | ||
| 172 | |||
| 173 | |||
| 174 | static FILE *tofile (lua_State *L) { | ||
| 175 | LStream *p = tolstream(L); | ||
| 176 | if (isclosed(p)) | ||
| 177 | luaL_error(L, "attempt to use a closed file"); | ||
| 178 | lua_assert(p->f); | ||
| 179 | return p->f; | ||
| 180 | } | ||
| 181 | |||
| 182 | |||
| 183 | /* | ||
| 184 | ** When creating file handles, always creates a 'closed' file handle | ||
| 185 | ** before opening the actual file; so, if there is a memory error, the | ||
| 186 | ** handle is in a consistent state. | ||
| 187 | */ | ||
| 188 | static LStream *newprefile (lua_State *L) { | ||
| 189 | LStream *p = (LStream *)lua_newuserdata(L, sizeof(LStream)); | ||
| 190 | p->closef = NULL; /* mark file handle as 'closed' */ | ||
| 191 | luaL_setmetatable(L, LUA_FILEHANDLE); | ||
| 192 | return p; | ||
| 193 | } | ||
| 194 | |||
| 195 | |||
| 196 | /* | ||
| 197 | ** Calls the 'close' function from a file handle. The 'volatile' avoids | ||
| 198 | ** a bug in some versions of the Clang compiler (e.g., clang 3.0 for | ||
| 199 | ** 32 bits). | ||
| 200 | */ | ||
| 201 | static int aux_close (lua_State *L) { | ||
| 202 | LStream *p = tolstream(L); | ||
| 203 | volatile lua_CFunction cf = p->closef; | ||
| 204 | p->closef = NULL; /* mark stream as closed */ | ||
| 205 | return (*cf)(L); /* close it */ | ||
| 206 | } | ||
| 207 | |||
| 208 | |||
| 209 | static int f_close (lua_State *L) { | ||
| 210 | tofile(L); /* make sure argument is an open stream */ | ||
| 211 | return aux_close(L); | ||
| 212 | } | ||
| 213 | |||
| 214 | |||
| 215 | static int io_close (lua_State *L) { | ||
| 216 | if (lua_isnone(L, 1)) /* no argument? */ | ||
| 217 | lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */ | ||
| 218 | return f_close(L); | ||
| 219 | } | ||
| 220 | |||
| 221 | |||
| 222 | static int f_gc (lua_State *L) { | ||
| 223 | LStream *p = tolstream(L); | ||
| 224 | if (!isclosed(p) && p->f != NULL) | ||
| 225 | aux_close(L); /* ignore closed and incompletely open files */ | ||
| 226 | return 0; | ||
| 227 | } | ||
| 228 | |||
| 229 | |||
| 230 | /* | ||
| 231 | ** function to close regular files | ||
| 232 | */ | ||
| 233 | static int io_fclose (lua_State *L) { | ||
| 234 | LStream *p = tolstream(L); | ||
| 235 | int res = fclose(p->f); | ||
| 236 | return luaL_fileresult(L, (res == 0), NULL); | ||
| 237 | } | ||
| 238 | |||
| 239 | |||
| 240 | static LStream *newfile (lua_State *L) { | ||
| 241 | LStream *p = newprefile(L); | ||
| 242 | p->f = NULL; | ||
| 243 | p->closef = &io_fclose; | ||
| 244 | return p; | ||
| 245 | } | ||
| 246 | |||
| 247 | |||
| 248 | static void opencheck (lua_State *L, const char *fname, const char *mode) { | ||
| 249 | LStream *p = newfile(L); | ||
| 250 | p->f = fopen(fname, mode); | ||
| 251 | if (p->f == NULL) | ||
| 252 | luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno)); | ||
| 253 | } | ||
| 254 | |||
| 255 | |||
| 256 | static int io_open (lua_State *L) { | ||
| 257 | const char *filename = luaL_checkstring(L, 1); | ||
| 258 | const char *mode = luaL_optstring(L, 2, "r"); | ||
| 259 | LStream *p = newfile(L); | ||
| 260 | const char *md = mode; /* to traverse/check mode */ | ||
| 261 | luaL_argcheck(L, l_checkmode(md), 2, "invalid mode"); | ||
| 262 | p->f = fopen(filename, mode); | ||
| 263 | return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; | ||
| 264 | } | ||
| 265 | |||
| 266 | |||
| 267 | /* | ||
| 268 | ** function to close 'popen' files | ||
| 269 | */ | ||
| 270 | static int io_pclose (lua_State *L) { | ||
| 271 | LStream *p = tolstream(L); | ||
| 272 | return luaL_execresult(L, l_pclose(L, p->f)); | ||
| 273 | } | ||
| 274 | |||
| 275 | |||
| 276 | static int io_popen (lua_State *L) { | ||
| 277 | const char *filename = luaL_checkstring(L, 1); | ||
| 278 | const char *mode = luaL_optstring(L, 2, "r"); | ||
| 279 | LStream *p = newprefile(L); | ||
| 280 | p->f = l_popen(L, filename, mode); | ||
| 281 | p->closef = &io_pclose; | ||
| 282 | return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; | ||
| 283 | } | ||
| 284 | |||
| 285 | |||
| 286 | static int io_tmpfile (lua_State *L) { | ||
| 287 | LStream *p = newfile(L); | ||
| 288 | p->f = tmpfile(); | ||
| 289 | return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1; | ||
| 290 | } | ||
| 291 | |||
| 292 | |||
| 293 | static FILE *getiofile (lua_State *L, const char *findex) { | ||
| 294 | LStream *p; | ||
| 295 | lua_getfield(L, LUA_REGISTRYINDEX, findex); | ||
| 296 | p = (LStream *)lua_touserdata(L, -1); | ||
| 297 | if (isclosed(p)) | ||
| 298 | luaL_error(L, "standard %s file is closed", findex + IOPREF_LEN); | ||
| 299 | return p->f; | ||
| 300 | } | ||
| 301 | |||
| 302 | |||
| 303 | static int g_iofile (lua_State *L, const char *f, const char *mode) { | ||
| 304 | if (!lua_isnoneornil(L, 1)) { | ||
| 305 | const char *filename = lua_tostring(L, 1); | ||
| 306 | if (filename) | ||
| 307 | opencheck(L, filename, mode); | ||
| 308 | else { | ||
| 309 | tofile(L); /* check that it's a valid file handle */ | ||
| 310 | lua_pushvalue(L, 1); | ||
| 311 | } | ||
| 312 | lua_setfield(L, LUA_REGISTRYINDEX, f); | ||
| 313 | } | ||
| 314 | /* return current value */ | ||
| 315 | lua_getfield(L, LUA_REGISTRYINDEX, f); | ||
| 316 | return 1; | ||
| 317 | } | ||
| 318 | |||
| 319 | |||
| 320 | static int io_input (lua_State *L) { | ||
| 321 | return g_iofile(L, IO_INPUT, "r"); | ||
| 322 | } | ||
| 323 | |||
| 324 | |||
| 325 | static int io_output (lua_State *L) { | ||
| 326 | return g_iofile(L, IO_OUTPUT, "w"); | ||
| 327 | } | ||
| 328 | |||
| 329 | |||
| 330 | static int io_readline (lua_State *L); | ||
| 331 | |||
| 332 | |||
| 333 | /* | ||
| 334 | ** maximum number of arguments to 'f:lines'/'io.lines' (it + 3 must fit | ||
| 335 | ** in the limit for upvalues of a closure) | ||
| 336 | */ | ||
| 337 | #define MAXARGLINE 250 | ||
| 338 | |||
| 339 | static void aux_lines (lua_State *L, int toclose) { | ||
| 340 | int n = lua_gettop(L) - 1; /* number of arguments to read */ | ||
| 341 | luaL_argcheck(L, n <= MAXARGLINE, MAXARGLINE + 2, "too many arguments"); | ||
| 342 | lua_pushinteger(L, n); /* number of arguments to read */ | ||
| 343 | lua_pushboolean(L, toclose); /* close/not close file when finished */ | ||
| 344 | lua_rotate(L, 2, 2); /* move 'n' and 'toclose' to their positions */ | ||
| 345 | lua_pushcclosure(L, io_readline, 3 + n); | ||
| 346 | } | ||
| 347 | |||
| 348 | |||
| 349 | static int f_lines (lua_State *L) { | ||
| 350 | tofile(L); /* check that it's a valid file handle */ | ||
| 351 | aux_lines(L, 0); | ||
| 352 | return 1; | ||
| 353 | } | ||
| 354 | |||
| 355 | |||
| 356 | static int io_lines (lua_State *L) { | ||
| 357 | int toclose; | ||
| 358 | if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */ | ||
| 359 | if (lua_isnil(L, 1)) { /* no file name? */ | ||
| 360 | lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT); /* get default input */ | ||
| 361 | lua_replace(L, 1); /* put it at index 1 */ | ||
| 362 | tofile(L); /* check that it's a valid file handle */ | ||
| 363 | toclose = 0; /* do not close it after iteration */ | ||
| 364 | } | ||
| 365 | else { /* open a new file */ | ||
| 366 | const char *filename = luaL_checkstring(L, 1); | ||
| 367 | opencheck(L, filename, "r"); | ||
| 368 | lua_replace(L, 1); /* put file at index 1 */ | ||
| 369 | toclose = 1; /* close it after iteration */ | ||
| 370 | } | ||
| 371 | aux_lines(L, toclose); | ||
| 372 | return 1; | ||
| 373 | } | ||
| 374 | |||
| 375 | |||
| 376 | /* | ||
| 377 | ** {====================================================== | ||
| 378 | ** READ | ||
| 379 | ** ======================================================= | ||
| 380 | */ | ||
| 381 | |||
| 382 | |||
| 383 | /* maximum length of a numeral */ | ||
| 384 | #if !defined (L_MAXLENNUM) | ||
| 385 | #define L_MAXLENNUM 200 | ||
| 386 | #endif | ||
| 387 | |||
| 388 | |||
| 389 | /* auxiliary structure used by 'read_number' */ | ||
| 390 | typedef struct { | ||
| 391 | FILE *f; /* file being read */ | ||
| 392 | int c; /* current character (look ahead) */ | ||
| 393 | int n; /* number of elements in buffer 'buff' */ | ||
| 394 | char buff[L_MAXLENNUM + 1]; /* +1 for ending '\0' */ | ||
| 395 | } RN; | ||
| 396 | |||
| 397 | |||
| 398 | /* | ||
| 399 | ** Add current char to buffer (if not out of space) and read next one | ||
| 400 | */ | ||
| 401 | static int nextc (RN *rn) { | ||
| 402 | if (rn->n >= L_MAXLENNUM) { /* buffer overflow? */ | ||
| 403 | rn->buff[0] = '\0'; /* invalidate result */ | ||
| 404 | return 0; /* fail */ | ||
| 405 | } | ||
| 406 | else { | ||
| 407 | rn->buff[rn->n++] = rn->c; /* save current char */ | ||
| 408 | rn->c = l_getc(rn->f); /* read next one */ | ||
| 409 | return 1; | ||
| 410 | } | ||
| 411 | } | ||
| 412 | |||
| 413 | |||
| 414 | /* | ||
| 415 | ** Accept current char if it is in 'set' (of size 2) | ||
| 416 | */ | ||
| 417 | static int test2 (RN *rn, const char *set) { | ||
| 418 | if (rn->c == set[0] || rn->c == set[1]) | ||
| 419 | return nextc(rn); | ||
| 420 | else return 0; | ||
| 421 | } | ||
| 422 | |||
| 423 | |||
| 424 | /* | ||
| 425 | ** Read a sequence of (hex)digits | ||
| 426 | */ | ||
| 427 | static int readdigits (RN *rn, int hex) { | ||
| 428 | int count = 0; | ||
| 429 | while ((hex ? isxdigit(rn->c) : isdigit(rn->c)) && nextc(rn)) | ||
| 430 | count++; | ||
| 431 | return count; | ||
| 432 | } | ||
| 433 | |||
| 434 | |||
| 435 | /* | ||
| 436 | ** Read a number: first reads a valid prefix of a numeral into a buffer. | ||
| 437 | ** Then it calls 'lua_stringtonumber' to check whether the format is | ||
| 438 | ** correct and to convert it to a Lua number | ||
| 439 | */ | ||
| 440 | static int read_number (lua_State *L, FILE *f) { | ||
| 441 | RN rn; | ||
| 442 | int count = 0; | ||
| 443 | int hex = 0; | ||
| 444 | char decp[2]; | ||
| 445 | rn.f = f; rn.n = 0; | ||
| 446 | decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */ | ||
| 447 | decp[1] = '.'; /* always accept a dot */ | ||
| 448 | l_lockfile(rn.f); | ||
| 449 | do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */ | ||
| 450 | test2(&rn, "-+"); /* optional signal */ | ||
| 451 | if (test2(&rn, "00")) { | ||
| 452 | if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */ | ||
| 453 | else count = 1; /* count initial '0' as a valid digit */ | ||
| 454 | } | ||
| 455 | count += readdigits(&rn, hex); /* integral part */ | ||
| 456 | if (test2(&rn, decp)) /* decimal point? */ | ||
| 457 | count += readdigits(&rn, hex); /* fractional part */ | ||
| 458 | if (count > 0 && test2(&rn, (hex ? "pP" : "eE"))) { /* exponent mark? */ | ||
| 459 | test2(&rn, "-+"); /* exponent signal */ | ||
| 460 | readdigits(&rn, 0); /* exponent digits */ | ||
| 461 | } | ||
| 462 | ungetc(rn.c, rn.f); /* unread look-ahead char */ | ||
| 463 | l_unlockfile(rn.f); | ||
| 464 | rn.buff[rn.n] = '\0'; /* finish string */ | ||
| 465 | if (lua_stringtonumber(L, rn.buff)) /* is this a valid number? */ | ||
| 466 | return 1; /* ok */ | ||
| 467 | else { /* invalid format */ | ||
| 468 | lua_pushnil(L); /* "result" to be removed */ | ||
| 469 | return 0; /* read fails */ | ||
| 470 | } | ||
| 471 | } | ||
| 472 | |||
| 473 | |||
| 474 | static int test_eof (lua_State *L, FILE *f) { | ||
| 475 | int c = getc(f); | ||
| 476 | ungetc(c, f); /* no-op when c == EOF */ | ||
| 477 | lua_pushliteral(L, ""); | ||
| 478 | return (c != EOF); | ||
| 479 | } | ||
| 480 | |||
| 481 | |||
| 482 | static int read_line (lua_State *L, FILE *f, int chop) { | ||
| 483 | luaL_Buffer b; | ||
| 484 | int c = '\0'; | ||
| 485 | luaL_buffinit(L, &b); | ||
| 486 | while (c != EOF && c != '\n') { /* repeat until end of line */ | ||
| 487 | char *buff = luaL_prepbuffer(&b); /* preallocate buffer */ | ||
| 488 | int i = 0; | ||
| 489 | l_lockfile(f); /* no memory errors can happen inside the lock */ | ||
| 490 | while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n') | ||
| 491 | buff[i++] = c; | ||
| 492 | l_unlockfile(f); | ||
| 493 | luaL_addsize(&b, i); | ||
| 494 | } | ||
| 495 | if (!chop && c == '\n') /* want a newline and have one? */ | ||
| 496 | luaL_addchar(&b, c); /* add ending newline to result */ | ||
| 497 | luaL_pushresult(&b); /* close buffer */ | ||
| 498 | /* return ok if read something (either a newline or something else) */ | ||
| 499 | return (c == '\n' || lua_rawlen(L, -1) > 0); | ||
| 500 | } | ||
| 501 | |||
| 502 | |||
| 503 | static void read_all (lua_State *L, FILE *f) { | ||
| 504 | size_t nr; | ||
| 505 | luaL_Buffer b; | ||
| 506 | luaL_buffinit(L, &b); | ||
| 507 | do { /* read file in chunks of LUAL_BUFFERSIZE bytes */ | ||
| 508 | char *p = luaL_prepbuffer(&b); | ||
| 509 | nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, f); | ||
| 510 | luaL_addsize(&b, nr); | ||
| 511 | } while (nr == LUAL_BUFFERSIZE); | ||
| 512 | luaL_pushresult(&b); /* close buffer */ | ||
| 513 | } | ||
| 514 | |||
| 515 | |||
| 516 | static int read_chars (lua_State *L, FILE *f, size_t n) { | ||
| 517 | size_t nr; /* number of chars actually read */ | ||
| 518 | char *p; | ||
| 519 | luaL_Buffer b; | ||
| 520 | luaL_buffinit(L, &b); | ||
| 521 | p = luaL_prepbuffsize(&b, n); /* prepare buffer to read whole block */ | ||
| 522 | nr = fread(p, sizeof(char), n, f); /* try to read 'n' chars */ | ||
| 523 | luaL_addsize(&b, nr); | ||
| 524 | luaL_pushresult(&b); /* close buffer */ | ||
| 525 | return (nr > 0); /* true iff read something */ | ||
| 526 | } | ||
| 527 | |||
| 528 | |||
| 529 | static int g_read (lua_State *L, FILE *f, int first) { | ||
| 530 | int nargs = lua_gettop(L) - 1; | ||
| 531 | int success; | ||
| 532 | int n; | ||
| 533 | clearerr(f); | ||
| 534 | if (nargs == 0) { /* no arguments? */ | ||
| 535 | success = read_line(L, f, 1); | ||
| 536 | n = first+1; /* to return 1 result */ | ||
| 537 | } | ||
| 538 | else { /* ensure stack space for all results and for auxlib's buffer */ | ||
| 539 | luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); | ||
| 540 | success = 1; | ||
| 541 | for (n = first; nargs-- && success; n++) { | ||
| 542 | if (lua_type(L, n) == LUA_TNUMBER) { | ||
| 543 | size_t l = (size_t)luaL_checkinteger(L, n); | ||
| 544 | success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); | ||
| 545 | } | ||
| 546 | else { | ||
| 547 | const char *p = luaL_checkstring(L, n); | ||
| 548 | if (*p == '*') p++; /* skip optional '*' (for compatibility) */ | ||
| 549 | switch (*p) { | ||
| 550 | case 'n': /* number */ | ||
| 551 | success = read_number(L, f); | ||
| 552 | break; | ||
| 553 | case 'l': /* line */ | ||
| 554 | success = read_line(L, f, 1); | ||
| 555 | break; | ||
| 556 | case 'L': /* line with end-of-line */ | ||
| 557 | success = read_line(L, f, 0); | ||
| 558 | break; | ||
| 559 | case 'a': /* file */ | ||
| 560 | read_all(L, f); /* read entire file */ | ||
| 561 | success = 1; /* always success */ | ||
| 562 | break; | ||
| 563 | default: | ||
| 564 | return luaL_argerror(L, n, "invalid format"); | ||
| 565 | } | ||
| 566 | } | ||
| 567 | } | ||
| 568 | } | ||
| 569 | if (ferror(f)) | ||
| 570 | return luaL_fileresult(L, 0, NULL); | ||
| 571 | if (!success) { | ||
| 572 | lua_pop(L, 1); /* remove last result */ | ||
| 573 | lua_pushnil(L); /* push nil instead */ | ||
| 574 | } | ||
| 575 | return n - first; | ||
| 576 | } | ||
| 577 | |||
| 578 | |||
| 579 | static int io_read (lua_State *L) { | ||
| 580 | return g_read(L, getiofile(L, IO_INPUT), 1); | ||
| 581 | } | ||
| 582 | |||
| 583 | |||
| 584 | static int f_read (lua_State *L) { | ||
| 585 | return g_read(L, tofile(L), 2); | ||
| 586 | } | ||
| 587 | |||
| 588 | |||
| 589 | static int io_readline (lua_State *L) { | ||
| 590 | LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1)); | ||
| 591 | int i; | ||
| 592 | int n = (int)lua_tointeger(L, lua_upvalueindex(2)); | ||
| 593 | if (isclosed(p)) /* file is already closed? */ | ||
| 594 | return luaL_error(L, "file is already closed"); | ||
| 595 | lua_settop(L , 1); | ||
| 596 | luaL_checkstack(L, n, "too many arguments"); | ||
| 597 | for (i = 1; i <= n; i++) /* push arguments to 'g_read' */ | ||
| 598 | lua_pushvalue(L, lua_upvalueindex(3 + i)); | ||
| 599 | n = g_read(L, p->f, 2); /* 'n' is number of results */ | ||
| 600 | lua_assert(n > 0); /* should return at least a nil */ | ||
| 601 | if (lua_toboolean(L, -n)) /* read at least one value? */ | ||
| 602 | return n; /* return them */ | ||
| 603 | else { /* first result is nil: EOF or error */ | ||
| 604 | if (n > 1) { /* is there error information? */ | ||
| 605 | /* 2nd result is error message */ | ||
| 606 | return luaL_error(L, "%s", lua_tostring(L, -n + 1)); | ||
| 607 | } | ||
| 608 | if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */ | ||
| 609 | lua_settop(L, 0); | ||
| 610 | lua_pushvalue(L, lua_upvalueindex(1)); | ||
| 611 | aux_close(L); /* close it */ | ||
| 612 | } | ||
| 613 | return 0; | ||
| 614 | } | ||
| 615 | } | ||
| 616 | |||
| 617 | /* }====================================================== */ | ||
| 618 | |||
| 619 | |||
| 620 | static int g_write (lua_State *L, FILE *f, int arg) { | ||
| 621 | int nargs = lua_gettop(L) - arg; | ||
| 622 | int status = 1; | ||
| 623 | for (; nargs--; arg++) { | ||
| 624 | if (lua_type(L, arg) == LUA_TNUMBER) { | ||
| 625 | /* optimization: could be done exactly as for strings */ | ||
| 626 | int len = lua_isinteger(L, arg) | ||
| 627 | ? fprintf(f, LUA_INTEGER_FMT, | ||
| 628 | (LUAI_UACINT)lua_tointeger(L, arg)) | ||
| 629 | : fprintf(f, LUA_NUMBER_FMT, | ||
| 630 | (LUAI_UACNUMBER)lua_tonumber(L, arg)); | ||
| 631 | status = status && (len > 0); | ||
| 632 | } | ||
| 633 | else { | ||
| 634 | size_t l; | ||
| 635 | const char *s = luaL_checklstring(L, arg, &l); | ||
| 636 | status = status && (fwrite(s, sizeof(char), l, f) == l); | ||
| 637 | } | ||
| 638 | } | ||
| 639 | if (status) return 1; /* file handle already on stack top */ | ||
| 640 | else return luaL_fileresult(L, status, NULL); | ||
| 641 | } | ||
| 642 | |||
| 643 | |||
| 644 | static int io_write (lua_State *L) { | ||
| 645 | return g_write(L, getiofile(L, IO_OUTPUT), 1); | ||
| 646 | } | ||
| 647 | |||
| 648 | |||
| 649 | static int f_write (lua_State *L) { | ||
| 650 | FILE *f = tofile(L); | ||
| 651 | lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */ | ||
| 652 | return g_write(L, f, 2); | ||
| 653 | } | ||
| 654 | |||
| 655 | |||
| 656 | static int f_seek (lua_State *L) { | ||
| 657 | static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; | ||
| 658 | static const char *const modenames[] = {"set", "cur", "end", NULL}; | ||
| 659 | FILE *f = tofile(L); | ||
| 660 | int op = luaL_checkoption(L, 2, "cur", modenames); | ||
| 661 | lua_Integer p3 = luaL_optinteger(L, 3, 0); | ||
| 662 | l_seeknum offset = (l_seeknum)p3; | ||
| 663 | luaL_argcheck(L, (lua_Integer)offset == p3, 3, | ||
| 664 | "not an integer in proper range"); | ||
| 665 | op = l_fseek(f, offset, mode[op]); | ||
| 666 | if (op) | ||
| 667 | return luaL_fileresult(L, 0, NULL); /* error */ | ||
| 668 | else { | ||
| 669 | lua_pushinteger(L, (lua_Integer)l_ftell(f)); | ||
| 670 | return 1; | ||
| 671 | } | ||
| 672 | } | ||
| 673 | |||
| 674 | |||
| 675 | static int f_setvbuf (lua_State *L) { | ||
| 676 | static const int mode[] = {_IONBF, _IOFBF, _IOLBF}; | ||
| 677 | static const char *const modenames[] = {"no", "full", "line", NULL}; | ||
| 678 | FILE *f = tofile(L); | ||
| 679 | int op = luaL_checkoption(L, 2, NULL, modenames); | ||
| 680 | lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); | ||
| 681 | int res = setvbuf(f, NULL, mode[op], (size_t)sz); | ||
| 682 | return luaL_fileresult(L, res == 0, NULL); | ||
| 683 | } | ||
| 684 | |||
| 685 | |||
| 686 | |||
| 687 | static int io_flush (lua_State *L) { | ||
| 688 | return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL); | ||
| 689 | } | ||
| 690 | |||
| 691 | |||
| 692 | static int f_flush (lua_State *L) { | ||
| 693 | return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL); | ||
| 694 | } | ||
| 695 | |||
| 696 | |||
| 697 | /* | ||
| 698 | ** functions for 'io' library | ||
| 699 | */ | ||
| 700 | static const luaL_Reg iolib[] = { | ||
| 701 | {"close", io_close}, | ||
| 702 | {"flush", io_flush}, | ||
| 703 | {"input", io_input}, | ||
| 704 | {"lines", io_lines}, | ||
| 705 | {"open", io_open}, | ||
| 706 | {"output", io_output}, | ||
| 707 | {"popen", io_popen}, | ||
| 708 | {"read", io_read}, | ||
| 709 | {"tmpfile", io_tmpfile}, | ||
| 710 | {"type", io_type}, | ||
| 711 | {"write", io_write}, | ||
| 712 | {NULL, NULL} | ||
| 713 | }; | ||
| 714 | |||
| 715 | |||
| 716 | /* | ||
| 717 | ** methods for file handles | ||
| 718 | */ | ||
| 719 | static const luaL_Reg flib[] = { | ||
| 720 | {"close", f_close}, | ||
| 721 | {"flush", f_flush}, | ||
| 722 | {"lines", f_lines}, | ||
| 723 | {"read", f_read}, | ||
| 724 | {"seek", f_seek}, | ||
| 725 | {"setvbuf", f_setvbuf}, | ||
| 726 | {"write", f_write}, | ||
| 727 | {"__gc", f_gc}, | ||
| 728 | {"__tostring", f_tostring}, | ||
| 729 | {NULL, NULL} | ||
| 730 | }; | ||
| 731 | |||
| 732 | |||
| 733 | static void createmeta (lua_State *L) { | ||
| 734 | luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */ | ||
| 735 | lua_pushvalue(L, -1); /* push metatable */ | ||
| 736 | lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ | ||
| 737 | luaL_setfuncs(L, flib, 0); /* add file methods to new metatable */ | ||
| 738 | lua_pop(L, 1); /* pop new metatable */ | ||
| 739 | } | ||
| 740 | |||
| 741 | |||
| 742 | /* | ||
| 743 | ** function to (not) close the standard files stdin, stdout, and stderr | ||
| 744 | */ | ||
| 745 | static int io_noclose (lua_State *L) { | ||
| 746 | LStream *p = tolstream(L); | ||
| 747 | p->closef = &io_noclose; /* keep file opened */ | ||
| 748 | lua_pushnil(L); | ||
| 749 | lua_pushliteral(L, "cannot close standard file"); | ||
| 750 | return 2; | ||
| 751 | } | ||
| 752 | |||
| 753 | |||
| 754 | static void createstdfile (lua_State *L, FILE *f, const char *k, | ||
| 755 | const char *fname) { | ||
| 756 | LStream *p = newprefile(L); | ||
| 757 | p->f = f; | ||
| 758 | p->closef = &io_noclose; | ||
| 759 | if (k != NULL) { | ||
| 760 | lua_pushvalue(L, -1); | ||
| 761 | lua_setfield(L, LUA_REGISTRYINDEX, k); /* add file to registry */ | ||
| 762 | } | ||
| 763 | lua_setfield(L, -2, fname); /* add file to module */ | ||
| 764 | } | ||
| 765 | |||
| 766 | |||
| 767 | LUAMOD_API int luaopen_io (lua_State *L) { | ||
| 768 | luaL_newlib(L, iolib); /* new module */ | ||
| 769 | createmeta(L); | ||
| 770 | /* create (and set) default files */ | ||
| 771 | createstdfile(L, stdin, IO_INPUT, "stdin"); | ||
| 772 | createstdfile(L, stdout, IO_OUTPUT, "stdout"); | ||
| 773 | createstdfile(L, stderr, NULL, "stderr"); | ||
| 774 | return 1; | ||
| 775 | } | ||
| 776 | |||
| @@ -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 */ | ||
diff --git a/rockspecs/compat53-scm-0.rockspec b/rockspecs/compat53-scm-0.rockspec index db9f4e7..e0820d4 100644 --- a/rockspecs/compat53-scm-0.rockspec +++ b/rockspecs/compat53-scm-0.rockspec | |||
| @@ -27,6 +27,7 @@ build = { | |||
| 27 | ["compat53.utf8"] = "lutf8lib.c", | 27 | ["compat53.utf8"] = "lutf8lib.c", |
| 28 | ["compat53.table"] = "ltablib.c", | 28 | ["compat53.table"] = "ltablib.c", |
| 29 | ["compat53.string"] = "lstrlib.c", | 29 | ["compat53.string"] = "lstrlib.c", |
| 30 | ["compat53.io"] = "liolib.c", | ||
| 30 | } | 31 | } |
| 31 | } | 32 | } |
| 32 | 33 | ||
diff --git a/tests/test.lua b/tests/test.lua index 0640cae..443f6f7 100755 --- a/tests/test.lua +++ b/tests/test.lua | |||
| @@ -625,11 +625,13 @@ do | |||
| 625 | end)) | 625 | end)) |
| 626 | if mode ~= "module" then | 626 | if mode ~= "module" then |
| 627 | local f = assert(io.open(self, "r")) | 627 | local f = assert(io.open(self, "r")) |
| 628 | print("io.type()", io.type(f)) | ||
| 628 | for a,b in f:lines(2, "*l") do | 629 | for a,b in f:lines(2, "*l") do |
| 629 | print("file:lines()", a, b) | 630 | print("file:lines()", a, b) |
| 630 | break | 631 | break |
| 631 | end | 632 | end |
| 632 | f:close() | 633 | f:close() |
| 634 | print("io.type()", io.type(f)) | ||
| 633 | f = assert(io.open("data.txt", "r")) | 635 | f = assert(io.open("data.txt", "r")) |
| 634 | for n1,n2,rest in f:lines("*n", "n", "*a") do | 636 | for n1,n2,rest in f:lines("*n", "n", "*a") do |
| 635 | print("file:lines()", n1, n2, rest) | 637 | print("file:lines()", n1, n2, rest) |
| @@ -650,6 +652,18 @@ do | |||
| 650 | end)) | 652 | end)) |
| 651 | end | 653 | end |
| 652 | os.remove("data.txt") | 654 | os.remove("data.txt") |
| 655 | print("io.popen()", pcall(function() | ||
| 656 | local f = assert(io.popen("echo 'hello' && exit 0", "r")) | ||
| 657 | print("io.popen()", f:read("*a")) | ||
| 658 | print("io.popen()", f:close()) | ||
| 659 | end)) | ||
| 660 | print("io.popen()", pcall(function() | ||
| 661 | local f = assert(io.popen("echo 'hello' && exit 5", "r")) | ||
| 662 | print("io.type()", io.type(f)) | ||
| 663 | print("io.popen()", f:read("*a")) | ||
| 664 | print("io.popen()", f:close()) | ||
| 665 | print("io.type()", io.type(f)) | ||
| 666 | end)) | ||
| 653 | end | 667 | end |
| 654 | ___'' | 668 | ___'' |
| 655 | 669 | ||
