fix os.host

This commit is contained in:
ruki
2018-11-20 00:31:46 +08:00
parent af34ce822c
commit 1f9b177f72
6 changed files with 36 additions and 21 deletions

View File

@@ -63,6 +63,14 @@ Notes:
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "luaconf.h"
#undef LUA_API
#if defined(__cplusplus)
#define LUA_API extern "C"
#else
#define LUA_API extern
#endif
#include "luajit.h" #include "luajit.h"
#include "lualib.h" #include "lualib.h"
#include "lauxlib.h" #include "lauxlib.h"

View File

@@ -22,3 +22,6 @@ target("lcurses")
else else
add_links("curses") add_links("curses")
end end
-- set languages
set_languages("c89")

View File

@@ -7,6 +7,9 @@ target("pdcurses")
-- add the common source files -- add the common source files
add_files("**.c") add_files("**.c")
-- add include directories
add_includedirs(".")
-- add defines -- add defines
add_defines("PDC_WIDE") add_defines("PDC_WIDE")

View File

@@ -25,7 +25,7 @@ if is_plat("windows") then
add_cxflags("-MT") add_cxflags("-MT")
add_defines("_CRT_SECURE_NO_WARNINGS") add_defines("_CRT_SECURE_NO_WARNINGS")
add_shflags("-nodefaultlib:msvcrt.lib") add_shflags("-nodefaultlib:msvcrt.lib")
add_links("kernel32", "user32", "gdi32") add_links("kernel32", "user32", "gdi32", "advapi32")
end end
-- add requires -- add requires

View File

@@ -66,7 +66,11 @@ end
-- clear log -- clear log
function log:clear(state) function log:clear(state)
if os.isfile(self:outputfile()) then if os.isfile(self:outputfile()) then
io.writefile(self:outputfile(), "") local file = io.open(self:outputfile(), "w")
if file then
file:write("")
file:close()
end
end end
end end

View File

@@ -58,29 +58,32 @@ end
-- run program and get io output -- run program and get io output
function os.iorun(cmd, ...) function os.iorun(cmd, ...)
local ok = false
local outs = nil local outs = nil
local file = io.popen(string.tryformat(cmd, ...), "r") local file = io.popen(string.tryformat(cmd, ...), "r")
if file then if file then
outs = file:read("*a"):trim() outs = file:read("*a"):trim()
file:close() file:close()
ok = true
end end
return ok, outs return outs
end end
-- get host name -- get host name
function os.host() function os.host()
if os._HOST == nil then if os._HOST == nil then
local ok, result = os.iorun("uname") if jit and jit.os then
if ok then local hosts = {OSX = "macosx", Windows = "windows", Linux = "linux"}
os._HOST = hosts[jit.os]
elseif package.config:sub(1, 1) == '\\' then
os._HOST = "windows"
else
local result = os.iorun("uname")
if result then
if result:lower():find("linux", 1, true) then if result:lower():find("linux", 1, true) then
os._HOST = "linux" os._HOST = "linux"
elseif result:lower():find("darwin", 1, true) then elseif result:lower():find("darwin", 1, true) then
os._HOST = "macosx" os._HOST = "macosx"
end end
elseif os.run("cmd /c ver") == 0 then end
os._HOST = "windows"
end end
end end
return os._HOST return os._HOST
@@ -89,15 +92,9 @@ end
-- read string data from pasteboard -- read string data from pasteboard
function os.pbpaste() function os.pbpaste()
if os.host() == "macosx" then if os.host() == "macosx" then
local ok, result = os.iorun("pbpaste") return os.iorun("pbpaste")
if ok then
return result
end
elseif os.host() == "linux" then elseif os.host() == "linux" then
local ok, result = os.iorun("xsel --clipboard --output") return os.iorun("xsel --clipboard --output")
if ok then
return result
end
else else
-- TODO -- TODO
end end