00001
00002
00003
00004
00005
00006
00007
00008 #include <errno.h>
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <string.h>
00012
00013 #define liolib_c
00014 #define LUA_LIB
00015
00016 #include "lua.h"
00017
00018 #include "lauxlib.h"
00019 #include "lualib.h"
00020
00021
00022
00023 #define IO_INPUT 1
00024 #define IO_OUTPUT 2
00025
00026
00027 static const char *const fnames[] = {"input", "output"};
00028
00029
00030 static int pushresult (lua_State *L, int i, const char *filename) {
00031 int en = errno;
00032 if (i) {
00033 lua_pushboolean(L, 1);
00034 return 1;
00035 }
00036 else {
00037 lua_pushnil(L);
00038 if (filename)
00039 lua_pushfstring(L, "%s: %s", filename, strerror(en));
00040 else
00041 lua_pushfstring(L, "%s", strerror(en));
00042 lua_pushinteger(L, en);
00043 return 3;
00044 }
00045 }
00046
00047
00048 static void fileerror (lua_State *L, int arg, const char *filename) {
00049 lua_pushfstring(L, "%s: %s", filename, strerror(errno));
00050 luaL_argerror(L, arg, lua_tostring(L, -1));
00051 }
00052
00053
00054 #define tofilep(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))
00055
00056
00057 static int io_type (lua_State *L) {
00058 void *ud;
00059 luaL_checkany(L, 1);
00060 ud = lua_touserdata(L, 1);
00061 lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE);
00062 if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1))
00063 lua_pushnil(L);
00064 else if (*((FILE **)ud) == NULL)
00065 lua_pushliteral(L, "closed file");
00066 else
00067 lua_pushliteral(L, "file");
00068 return 1;
00069 }
00070
00071
00072 static FILE *tofile (lua_State *L) {
00073 FILE **f = tofilep(L);
00074 if (*f == NULL)
00075 luaL_error(L, "attempt to use a closed file");
00076 return *f;
00077 }
00078
00079
00080
00081
00082
00083
00084
00085
00086 static FILE **newfile (lua_State *L) {
00087 FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *));
00088 *pf = NULL;
00089 luaL_getmetatable(L, LUA_FILEHANDLE);
00090 lua_setmetatable(L, -2);
00091 return pf;
00092 }
00093
00094
00095
00096
00097
00098 static int io_noclose (lua_State *L) {
00099 lua_pushnil(L);
00100 lua_pushliteral(L, "cannot close standard file");
00101 return 2;
00102 }
00103
00104
00105
00106
00107
00108 static int io_pclose (lua_State *L) {
00109 FILE **p = tofilep(L);
00110 int ok = lua_pclose(L, *p);
00111 *p = NULL;
00112 return pushresult(L, ok, NULL);
00113 }
00114
00115
00116
00117
00118
00119 static int io_fclose (lua_State *L) {
00120 FILE **p = tofilep(L);
00121 int ok = (fclose(*p) == 0);
00122 *p = NULL;
00123 return pushresult(L, ok, NULL);
00124 }
00125
00126
00127 static int aux_close (lua_State *L) {
00128 lua_getfenv(L, 1);
00129 lua_getfield(L, -1, "__close");
00130 return (lua_tocfunction(L, -1))(L);
00131 }
00132
00133
00134 static int io_close (lua_State *L) {
00135 if (lua_isnone(L, 1))
00136 lua_rawgeti(L, LUA_ENVIRONINDEX, IO_OUTPUT);
00137 tofile(L);
00138 return aux_close(L);
00139 }
00140
00141
00142 static int io_gc (lua_State *L) {
00143 FILE *f = *tofilep(L);
00144
00145 if (f != NULL)
00146 aux_close(L);
00147 return 0;
00148 }
00149
00150
00151 static int io_tostring (lua_State *L) {
00152 FILE *f = *tofilep(L);
00153 if (f == NULL)
00154 lua_pushliteral(L, "file (closed)");
00155 else
00156 lua_pushfstring(L, "file (%p)", f);
00157 return 1;
00158 }
00159
00160
00161 static int io_open (lua_State *L) {
00162 const char *filename = luaL_checkstring(L, 1);
00163 const char *mode = luaL_optstring(L, 2, "r");
00164 FILE **pf = newfile(L);
00165 *pf = fopen(filename, mode);
00166 return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
00167 }
00168
00169
00170
00171
00172
00173
00174 static int io_popen (lua_State *L) {
00175 const char *filename = luaL_checkstring(L, 1);
00176 const char *mode = luaL_optstring(L, 2, "r");
00177 FILE **pf = newfile(L);
00178 *pf = lua_popen(L, filename, mode);
00179 return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
00180 }
00181
00182
00183 static int io_tmpfile (lua_State *L) {
00184 FILE **pf = newfile(L);
00185 *pf = tmpfile();
00186 return (*pf == NULL) ? pushresult(L, 0, NULL) : 1;
00187 }
00188
00189
00190 static FILE *getiofile (lua_State *L, int findex) {
00191 FILE *f;
00192 lua_rawgeti(L, LUA_ENVIRONINDEX, findex);
00193 f = *(FILE **)lua_touserdata(L, -1);
00194 if (f == NULL)
00195 luaL_error(L, "standard %s file is closed", fnames[findex - 1]);
00196 return f;
00197 }
00198
00199
00200 static int g_iofile (lua_State *L, int f, const char *mode) {
00201 if (!lua_isnoneornil(L, 1)) {
00202 const char *filename = lua_tostring(L, 1);
00203 if (filename) {
00204 FILE **pf = newfile(L);
00205 *pf = fopen(filename, mode);
00206 if (*pf == NULL)
00207 fileerror(L, 1, filename);
00208 }
00209 else {
00210 tofile(L);
00211 lua_pushvalue(L, 1);
00212 }
00213 lua_rawseti(L, LUA_ENVIRONINDEX, f);
00214 }
00215
00216 lua_rawgeti(L, LUA_ENVIRONINDEX, f);
00217 return 1;
00218 }
00219
00220
00221 static int io_input (lua_State *L) {
00222 return g_iofile(L, IO_INPUT, "r");
00223 }
00224
00225
00226 static int io_output (lua_State *L) {
00227 return g_iofile(L, IO_OUTPUT, "w");
00228 }
00229
00230
00231 static int io_readline (lua_State *L);
00232
00233
00234 static void aux_lines (lua_State *L, int idx, int toclose) {
00235 lua_pushvalue(L, idx);
00236 lua_pushboolean(L, toclose);
00237 lua_pushcclosure(L, io_readline, 2);
00238 }
00239
00240
00241 static int f_lines (lua_State *L) {
00242 tofile(L);
00243 aux_lines(L, 1, 0);
00244 return 1;
00245 }
00246
00247
00248 static int io_lines (lua_State *L) {
00249 if (lua_isnoneornil(L, 1)) {
00250
00251 lua_rawgeti(L, LUA_ENVIRONINDEX, IO_INPUT);
00252 return f_lines(L);
00253 }
00254 else {
00255 const char *filename = luaL_checkstring(L, 1);
00256 FILE **pf = newfile(L);
00257 *pf = fopen(filename, "r");
00258 if (*pf == NULL)
00259 fileerror(L, 1, filename);
00260 aux_lines(L, lua_gettop(L), 1);
00261 return 1;
00262 }
00263 }
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273 static int read_number (lua_State *L, FILE *f) {
00274 lua_Number d;
00275 if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
00276 lua_pushnumber(L, d);
00277 return 1;
00278 }
00279 else {
00280 lua_pushnil(L);
00281 return 0;
00282 }
00283 }
00284
00285
00286 static int test_eof (lua_State *L, FILE *f) {
00287 int c = getc(f);
00288 ungetc(c, f);
00289 lua_pushlstring(L, NULL, 0);
00290 return (c != EOF);
00291 }
00292
00293
00294 static int read_line (lua_State *L, FILE *f) {
00295 luaL_Buffer b;
00296 luaL_buffinit(L, &b);
00297 for (;;) {
00298 size_t l;
00299 char *p = luaL_prepbuffer(&b);
00300 if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) {
00301 luaL_pushresult(&b);
00302 return (lua_objlen(L, -1) > 0);
00303 }
00304 l = strlen(p);
00305 if (l == 0 || p[l-1] != '\n')
00306 luaL_addsize(&b, l);
00307 else {
00308 luaL_addsize(&b, l - 1);
00309 luaL_pushresult(&b);
00310 return 1;
00311 }
00312 }
00313 }
00314
00315
00316 static int read_chars (lua_State *L, FILE *f, size_t n) {
00317 size_t rlen;
00318 size_t nr;
00319 luaL_Buffer b;
00320 luaL_buffinit(L, &b);
00321 rlen = LUAL_BUFFERSIZE;
00322 do {
00323 char *p = luaL_prepbuffer(&b);
00324 if (rlen > n) rlen = n;
00325 nr = fread(p, sizeof(char), rlen, f);
00326 luaL_addsize(&b, nr);
00327 n -= nr;
00328 } while (n > 0 && nr == rlen);
00329 luaL_pushresult(&b);
00330 return (n == 0 || lua_objlen(L, -1) > 0);
00331 }
00332
00333
00334 static int g_read (lua_State *L, FILE *f, int first) {
00335 int nargs = lua_gettop(L) - 1;
00336 int success;
00337 int n;
00338 clearerr(f);
00339 if (nargs == 0) {
00340 success = read_line(L, f);
00341 n = first+1;
00342 }
00343 else {
00344 luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
00345 success = 1;
00346 for (n = first; nargs-- && success; n++) {
00347 if (lua_type(L, n) == LUA_TNUMBER) {
00348 size_t l = (size_t)lua_tointeger(L, n);
00349 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
00350 }
00351 else {
00352 const char *p = lua_tostring(L, n);
00353 luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
00354 switch (p[1]) {
00355 case 'n':
00356 success = read_number(L, f);
00357 break;
00358 case 'l':
00359 success = read_line(L, f);
00360 break;
00361 case 'a':
00362 read_chars(L, f, ~((size_t)0));
00363 success = 1;
00364 break;
00365 default:
00366 return luaL_argerror(L, n, "invalid format");
00367 }
00368 }
00369 }
00370 }
00371 if (ferror(f))
00372 return pushresult(L, 0, NULL);
00373 if (!success) {
00374 lua_pop(L, 1);
00375 lua_pushnil(L);
00376 }
00377 return n - first;
00378 }
00379
00380
00381 static int io_read (lua_State *L) {
00382 return g_read(L, getiofile(L, IO_INPUT), 1);
00383 }
00384
00385
00386 static int f_read (lua_State *L) {
00387 return g_read(L, tofile(L), 2);
00388 }
00389
00390
00391 static int io_readline (lua_State *L) {
00392 FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1));
00393 int sucess;
00394 if (f == NULL)
00395 luaL_error(L, "file is already closed");
00396 sucess = read_line(L, f);
00397 if (ferror(f))
00398 return luaL_error(L, "%s", strerror(errno));
00399 if (sucess) return 1;
00400 else {
00401 if (lua_toboolean(L, lua_upvalueindex(2))) {
00402 lua_settop(L, 0);
00403 lua_pushvalue(L, lua_upvalueindex(1));
00404 aux_close(L);
00405 }
00406 return 0;
00407 }
00408 }
00409
00410
00411
00412
00413 static int g_write (lua_State *L, FILE *f, int arg) {
00414 int nargs = lua_gettop(L) - 1;
00415 int status = 1;
00416 for (; nargs--; arg++) {
00417 if (lua_type(L, arg) == LUA_TNUMBER) {
00418
00419 status = status &&
00420 fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
00421 }
00422 else {
00423 size_t l;
00424 const char *s = luaL_checklstring(L, arg, &l);
00425 status = status && (fwrite(s, sizeof(char), l, f) == l);
00426 }
00427 }
00428 return pushresult(L, status, NULL);
00429 }
00430
00431
00432 static int io_write (lua_State *L) {
00433 return g_write(L, getiofile(L, IO_OUTPUT), 1);
00434 }
00435
00436
00437 static int f_write (lua_State *L) {
00438 return g_write(L, tofile(L), 2);
00439 }
00440
00441
00442 static int f_seek (lua_State *L) {
00443 static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
00444 static const char *const modenames[] = {"set", "cur", "end", NULL};
00445 FILE *f = tofile(L);
00446 int op = luaL_checkoption(L, 2, "cur", modenames);
00447 long offset = luaL_optlong(L, 3, 0);
00448 op = fseek(f, offset, mode[op]);
00449 if (op)
00450 return pushresult(L, 0, NULL);
00451 else {
00452 lua_pushinteger(L, ftell(f));
00453 return 1;
00454 }
00455 }
00456
00457
00458 static int f_setvbuf (lua_State *L) {
00459 static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
00460 static const char *const modenames[] = {"no", "full", "line", NULL};
00461 FILE *f = tofile(L);
00462 int op = luaL_checkoption(L, 2, NULL, modenames);
00463 lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
00464 int res = setvbuf(f, NULL, mode[op], sz);
00465 return pushresult(L, res == 0, NULL);
00466 }
00467
00468
00469
00470 static int io_flush (lua_State *L) {
00471 return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
00472 }
00473
00474
00475 static int f_flush (lua_State *L) {
00476 return pushresult(L, fflush(tofile(L)) == 0, NULL);
00477 }
00478
00479
00480 static const luaL_Reg iolib[] = {
00481 {"close", io_close},
00482 {"flush", io_flush},
00483 {"input", io_input},
00484 {"lines", io_lines},
00485 {"open", io_open},
00486 {"output", io_output},
00487 {"popen", io_popen},
00488 {"read", io_read},
00489 {"tmpfile", io_tmpfile},
00490 {"type", io_type},
00491 {"write", io_write},
00492 {NULL, NULL}
00493 };
00494
00495
00496 static const luaL_Reg flib[] = {
00497 {"close", io_close},
00498 {"flush", f_flush},
00499 {"lines", f_lines},
00500 {"read", f_read},
00501 {"seek", f_seek},
00502 {"setvbuf", f_setvbuf},
00503 {"write", f_write},
00504 {"__gc", io_gc},
00505 {"__tostring", io_tostring},
00506 {NULL, NULL}
00507 };
00508
00509
00510 static void createmeta (lua_State *L) {
00511 luaL_newmetatable(L, LUA_FILEHANDLE);
00512 lua_pushvalue(L, -1);
00513 lua_setfield(L, -2, "__index");
00514 luaL_register(L, NULL, flib);
00515 }
00516
00517
00518 static void createstdfile (lua_State *L, FILE *f, int k, const char *fname) {
00519 *newfile(L) = f;
00520 if (k > 0) {
00521 lua_pushvalue(L, -1);
00522 lua_rawseti(L, LUA_ENVIRONINDEX, k);
00523 }
00524 lua_pushvalue(L, -2);
00525 lua_setfenv(L, -2);
00526 lua_setfield(L, -3, fname);
00527 }
00528
00529
00530 static void newfenv (lua_State *L, lua_CFunction cls) {
00531 lua_createtable(L, 0, 1);
00532 lua_pushcfunction(L, cls);
00533 lua_setfield(L, -2, "__close");
00534 }
00535
00536
00537 LUALIB_API int luaopen_io (lua_State *L) {
00538 createmeta(L);
00539
00540 newfenv(L, io_fclose);
00541 lua_replace(L, LUA_ENVIRONINDEX);
00542
00543 luaL_register(L, LUA_IOLIBNAME, iolib);
00544
00545 newfenv(L, io_noclose);
00546 createstdfile(L, stdin, IO_INPUT, "stdin");
00547 createstdfile(L, stdout, IO_OUTPUT, "stdout");
00548 createstdfile(L, stderr, 0, "stderr");
00549 lua_pop(L, 1);
00550 lua_getfield(L, -1, "popen");
00551 newfenv(L, io_pclose);
00552 lua_setfenv(L, -2);
00553 lua_pop(L, 1);
00554 return 1;
00555 }