diff options
| author | ruki <[email protected]> | 2019-08-19 22:41:58 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-08-19 10:11:49 +0800 |
| commit | c15242c0449d6f70be73138133708f7244c64e04 (patch) | |
| tree | cc095a1557a8a00274b98dd36bfe9339f563c2b5 | |
| parent | 236988050bfe9f51ebe6b72c6e3d760bb2e349ef (diff) | |
fix stdfile
| -rw-r--r-- | core/src/xmake/io/file_close.c | 10 | ||||
| -rw-r--r-- | core/src/xmake/io/file_flush.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/io/file_isatty.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/io/file_open.c | 11 | ||||
| -rw-r--r-- | core/src/xmake/io/file_read.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/io/file_seek.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/io/file_size.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/io/file_write.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/io/prefix.h | 2 | ||||
| -rw-r--r-- | core/src/xmake/io/stdfile.c (renamed from core/src/xmake/io/std.c) | 25 | ||||
| -rw-r--r-- | core/src/xmake/machine.c | 30 | ||||
| -rw-r--r-- | core/src/xmake/makefile | 2 | ||||
| -rw-r--r-- | xmake/core/_xmake_main.lua | 6 | ||||
| -rw-r--r-- | xmake/core/base/io.lua | 32 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/io.lua | 38 |
15 files changed, 97 insertions, 71 deletions
diff --git a/core/src/xmake/io/file_close.c b/core/src/xmake/io/file_close.c index b808d3e44..5c7d5c75d 100644 --- a/core/src/xmake/io/file_close.c +++ b/core/src/xmake/io/file_close.c @@ -42,7 +42,7 @@ tb_int_t xm_io_file_close(lua_State* lua) // is user data? if (!lua_isuserdata(lua, 1)) - return 0; + xm_io_file_return_error(lua, "close(invalid file)!"); // get file xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1); @@ -84,9 +84,11 @@ tb_int_t xm_io_file_close(lua_State* lua) // exit file tb_free(file); - } - lua_pushboolean(lua, tb_true); - return 1; + // ok + lua_pushboolean(lua, tb_true); + return 1; + } + else xm_io_file_return_error(lua, "cannot close this file!"); } diff --git a/core/src/xmake/io/file_flush.c b/core/src/xmake/io/file_flush.c index 1c1939db1..72969b09d 100644 --- a/core/src/xmake/io/file_flush.c +++ b/core/src/xmake/io/file_flush.c @@ -69,7 +69,7 @@ tb_int_t xm_io_file_flush(lua_State* lua) // is user data? if (!lua_isuserdata(lua, 1)) - return 0; + xm_io_file_return_error(lua, "flush(invalid file)!"); // get file xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1); diff --git a/core/src/xmake/io/file_isatty.c b/core/src/xmake/io/file_isatty.c index 548fc8b2b..d8c5183d8 100644 --- a/core/src/xmake/io/file_isatty.c +++ b/core/src/xmake/io/file_isatty.c @@ -42,7 +42,7 @@ tb_int_t xm_io_file_isatty(lua_State* lua) // is user data? if (!lua_isuserdata(lua, 1)) - return 0; + xm_io_file_return_error(lua, "isatty(invalid file)!"); // get file xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1); diff --git a/core/src/xmake/io/file_open.c b/core/src/xmake/io/file_open.c index 3f1f0e9e2..fa39fc125 100644 --- a/core/src/xmake/io/file_open.c +++ b/core/src/xmake/io/file_open.c @@ -210,17 +210,10 @@ tb_int_t xm_io_file_open(lua_State* lua) else { if (stream) tb_stream_exit(stream); - lua_pushnil(lua); - lua_pushliteral(lua, "file not found!"); - return 2; + xm_io_file_return_error(lua, "file not found!"); } } - else - { - lua_pushnil(lua); - lua_pushliteral(lua, "invalid open mode!"); - return 2; - } + else xm_io_file_return_error(lua, "invalid open mode!"); tb_assert_and_check_return_val(encoding != XM_IO_FILE_ENCODING_UNKNOWN, 0); // open file diff --git a/core/src/xmake/io/file_read.c b/core/src/xmake/io/file_read.c index d183c0011..06e5569d0 100644 --- a/core/src/xmake/io/file_read.c +++ b/core/src/xmake/io/file_read.c @@ -504,7 +504,7 @@ tb_int_t xm_io_file_read(lua_State* lua) // is user data? if (!lua_isuserdata(lua, 1)) - return 0; + xm_io_file_return_error(lua, "read(invalid file)!"); // get file xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1); diff --git a/core/src/xmake/io/file_seek.c b/core/src/xmake/io/file_seek.c index 8387a6083..a4e14bffa 100644 --- a/core/src/xmake/io/file_seek.c +++ b/core/src/xmake/io/file_seek.c @@ -42,7 +42,7 @@ tb_int_t xm_io_file_seek(lua_State* lua) // is user data? if (!lua_isuserdata(lua, 1)) - return 0; + xm_io_file_return_error(lua, "seek(invalid file)!"); // get file xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1); diff --git a/core/src/xmake/io/file_size.c b/core/src/xmake/io/file_size.c index ca0343e8a..f8f126bbf 100644 --- a/core/src/xmake/io/file_size.c +++ b/core/src/xmake/io/file_size.c @@ -42,7 +42,7 @@ tb_int_t xm_io_file_size(lua_State* lua) // is user data? if (!lua_isuserdata(lua, 1)) - return 0; + xm_io_file_return_error(lua, "get size for invalid file!"); // get file xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1); diff --git a/core/src/xmake/io/file_write.c b/core/src/xmake/io/file_write.c index 7dc38cdce..43aaf606e 100644 --- a/core/src/xmake/io/file_write.c +++ b/core/src/xmake/io/file_write.c @@ -116,7 +116,7 @@ tb_int_t xm_io_file_write(lua_State* lua) // is user data? if (!lua_isuserdata(lua, 1)) - return 0; + xm_io_file_return_error(lua, "write(invalid file)!"); // get file xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1); diff --git a/core/src/xmake/io/prefix.h b/core/src/xmake/io/prefix.h index 761d4cdf8..ba567f6fb 100644 --- a/core/src/xmake/io/prefix.h +++ b/core/src/xmake/io/prefix.h @@ -38,7 +38,7 @@ do \ { \ lua_pushnil(lua); \ - lua_pushstring(lua, error); \ + lua_pushliteral(lua, error); \ return 2; \ } while (0) diff --git a/core/src/xmake/io/std.c b/core/src/xmake/io/stdfile.c index b549f3da7..381c34a67 100644 --- a/core/src/xmake/io/std.c +++ b/core/src/xmake/io/stdfile.c @@ -15,25 +15,30 @@ * Copyright (C) 2015 - 2019, TBOOX Open Source Group. * * @author OpportunityLiu, ruki - * @file file_std.c + * @file stdfile.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "std" +#define TB_TRACE_MODULE_NAME "stdfile" #define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes */ #include "prefix.h" +#ifdef TB_CONFIG_OS_WINDOWS +# include <io.h> +#else +# include <unistd.h> +#endif /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ -static tb_size_t xm_io_std_isatty(tb_size_t type) +static tb_size_t xm_io_stdfile_isatty(tb_size_t type) { tb_bool_t answer = tb_false; #ifdef TB_CONFIG_OS_WINDOWS @@ -63,25 +68,25 @@ static tb_size_t xm_io_std_isatty(tb_size_t type) * interfaces */ -// io.std(stdin: 1, stdout: 2, stderr: 3) -tb_int_t xm_io_std(lua_State* lua) +// io.stdfile(stdin: 1, stdout: 2, stderr: 3) +tb_int_t xm_io_stdfile(lua_State* lua) { // check tb_assert_and_check_return_val(lua, 0); // get std type - tB_int_t type = lua_tointeger(lua, 1); + tb_int_t type = lua_tointeger(lua, 1); tb_stdfile_ref_t fp = tb_null; switch (type) { case XM_IO_FILE_TYPE_STDIN: - fp = tb_stdfile_input(); + fp = tb_stdfile_input(); break; case XM_IO_FILE_TYPE_STDOUT: - fp = tb_stdfile_output(); + fp = tb_stdfile_output(); break; case XM_IO_FILE_TYPE_STDERR: - fp = tb_stdfile_error(); + fp = tb_stdfile_error(); break; } @@ -93,7 +98,7 @@ tb_int_t xm_io_std(lua_State* lua) file->std_ref = fp; file->stream = tb_null; file->fstream = tb_null; - file->type = xm_io_std_isatty(type); + file->type = xm_io_stdfile_isatty(type); file->encoding = TB_CHARSET_TYPE_UTF8; // init the read/write line cache buffer diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c index 83b218760..86f1018c2 100644 --- a/core/src/xmake/machine.c +++ b/core/src/xmake/machine.c @@ -90,7 +90,7 @@ tb_int_t xm_os_getown(lua_State* lua); #endif // the io/file functions -tb_int_t xm_io_std(lua_State* lua); +tb_int_t xm_io_stdfile(lua_State* lua); tb_int_t xm_io_file_open(lua_State* lua); tb_int_t xm_io_file_read(lua_State* lua); tb_int_t xm_io_file_seek(lua_State* lua); @@ -223,7 +223,7 @@ static luaL_Reg const g_winos_functions[] = // the io functions static luaL_Reg const g_io_functions[] = { - { "std", xm_io_std } + { "stdfile", xm_io_stdfile } , { "file_open", xm_io_file_open } , { "file_read", xm_io_file_read } , { "file_seek", xm_io_file_seek } @@ -547,32 +547,6 @@ static tb_void_t xm_machine_init_arch(xm_machine_t* machine) #endif lua_setglobal(machine->lua, "_ARCH"); } -static tb_void_t xm_machine_register_metatable(xm_machine_t* machine, tb_char_t const* module, tb_char_t const* metaname, tb_char_t const* metatype, luaL_Reg const* funcs) -{ - // check - tb_assert_and_check_return(machine && machine->lua); - - /* register metatable and functions - * - * metatype module.metaname = metatable {__index = metatable, funcs ...} - * - * e.g. io._file* io._file = metatable {__index = metatable, funcs ...} - */ - luaL_newmetatable(machine->lua, metatype); - // stack: {metatable}, {metatable} - lua_pushvalue(machine->lua, -1); - // stack: {metatable, __index = {metatable}} - lua_setfield(machine->lua, -2, "__index"); - // stack: {metatable}, {io} - lua_getglobal(machine->lua, module); - // stack: {metatable}, {io}, {metatable} - lua_pushvalue(machine->lua, -2); - // stack: {metatable}, {io, file = {metatable}} - lua_setfield(machine->lua, -2, metaname); - // stack: {metatable} - lua_pop(machine->lua, 1); - luaL_register(machine->lua, tb_null, funcs); -} /* ////////////////////////////////////////////////////////////////////////////////////// * implementation diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index 9385211cb..6d0f02117 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -44,7 +44,7 @@ xmake_C_FILES += \ os/uid \ os/gid \ os/getown \ - io/std \ + io/stdfile \ io/file_size \ io/file_close \ io/file_flush \ diff --git a/xmake/core/_xmake_main.lua b/xmake/core/_xmake_main.lua index 43987c742..f7aebd3e3 100644 --- a/xmake/core/_xmake_main.lua +++ b/xmake/core/_xmake_main.lua @@ -46,16 +46,16 @@ function _loadfile_impl(filepath, mode) end -- load script data from file - local file, ferrors = io.open(filepath, binary and "rb" or "r") + local file, ferrors = io.file_open(filepath, binary and "rb" or "r") if not file then return nil, ferrors end - local data, rerrors = file:read("a") + local data, rerrors = io.file_read(file, "a") if not data then return nil, rerrors end - file:close() + io.file_close(file) -- load script from string return load(data, "@" .. displaypath, mode) diff --git a/xmake/core/base/io.lua b/xmake/core/base/io.lua index b86d4dd1a..6c42354ab 100644 --- a/xmake/core/base/io.lua +++ b/xmake/core/base/io.lua @@ -28,16 +28,17 @@ local path = require("base/path") local table = require("base/table") local string = require("base/string") --- save metatable +-- save metatable and builtin functions io._file = _file io._filelock = _filelock +io._stdfile = io._stdfile or io.stdfile -- new an file -function _file.new(filepath, file) +function _file.new(filepath, fileref) local file = table.inherit(_file) file._NAME = path.filename(filepath) file._PATH = filepath - file._FILE = file + file._FILE = fileref setmetatable(file, _file) return file end @@ -100,7 +101,7 @@ function _file:read(fmt, opt) end opt = opt or {} local result, errors = io.file_read(self._FILE, fmt, opt.continuation) - if not result then + if not result and errors then errors = string.format("file(%s): %s", self:name(), errors) end return result, errors @@ -393,7 +394,24 @@ function io.isatty(file) return file:isatty() end --- replace the original open interface +-- get std file, /dev/stdin, /dev/stdout, /dev/stderr +function io.stdfile(filepath) + local file = nil + if filepath == "/dev/stdin" then + file = io._stdfile(1) + elseif filepath == "/dev/stdout" then + file = io._stdfile(2) + elseif filepath == "/dev/stderr" then + file = io._stdfile(3) + end + if file then + return _file.new(filepath, file) + else + return nil, string.format("failed to get std file: %s", filepath) + end +end + +-- open file function io.open(filepath, mode, opt) -- check @@ -602,6 +620,10 @@ function io.tail(filepath, linecount, opt) end end +-- init stdfile +io.stdin = io.stdfile("/dev/stdin") +io.stdout = io.stdfile("/dev/stdout") +io.stderr = io.stdfile("/dev/stderr") -- return module return io diff --git a/xmake/core/sandbox/modules/io.lua b/xmake/core/sandbox/modules/io.lua index 752026e78..6a2fb035c 100644 --- a/xmake/core/sandbox/modules/io.lua +++ b/xmake/core/sandbox/modules/io.lua @@ -92,22 +92,21 @@ function sandbox_io_file.write(file, ...) if not ok then raise(errors) end - return ok end -- print file function sandbox_io_file.print(file, ...) - return sandbox_io_file.write(file, vformat(...), "\n") + sandbox_io_file.write(file, vformat(...), "\n") end -- printf file function sandbox_io_file.printf(file, ...) - return sandbox_io_file.write(file, vformat(...)) + sandbox_io_file.write(file, vformat(...)) end -- writef file (without value filter) function sandbox_io_file.writef(file, ...) - return sandbox_io_file.write(file, string.format(...)) + sandbox_io_file.write(file, string.format(...)) end -- load object from file @@ -179,6 +178,32 @@ function sandbox_io.gsub(filepath, pattern, replace, opt) return data, count end +-- get std file +function sandbox_io.stdfile(filepath) + + -- check + assert(filepath) + + -- open it + local file, errors = io.stdfile(filepath) + if not file then + raise(errors) + end + + -- hook file interfaces + local hooked = {} + for name, func in pairs(sandbox_io_file) do + if not name:startswith("_") and type(func) == "function" then + hooked["_" .. name] = file["_" .. name] or file[name] + hooked[name] = func + end + end + for name, func in pairs(hooked) do + file[name] = func + end + return file +end + -- open file function sandbox_io.open(filepath, mode, opt) @@ -364,6 +389,11 @@ function sandbox_io.tail(filepath, linecount, opt) io.tail(filepath, linecount, opt) end +-- init stdfile +sandbox_io.stdin = sandbox_io.stdin or sandbox_io.stdfile("/dev/stdin") +sandbox_io.stdout = sandbox_io.stdout or sandbox_io.stdfile("/dev/stdout") +sandbox_io.stderr = sandbox_io.stderr or sandbox_io.stdfile("/dev/stderr") + -- return module return sandbox_io |
