summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/luatex/lualibs/lualibs-os.lua
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/tex/luatex/lualibs/lualibs-os.lua')
-rw-r--r--Master/texmf-dist/tex/luatex/lualibs/lualibs-os.lua122
1 files changed, 104 insertions, 18 deletions
diff --git a/Master/texmf-dist/tex/luatex/lualibs/lualibs-os.lua b/Master/texmf-dist/tex/luatex/lualibs/lualibs-os.lua
index a4c0ac82085..bfafa4f95db 100644
--- a/Master/texmf-dist/tex/luatex/lualibs/lualibs-os.lua
+++ b/Master/texmf-dist/tex/luatex/lualibs/lualibs-os.lua
@@ -127,7 +127,13 @@ function io.popen (...) ioflush() return iopopen(...) end
function os.resultof(command)
local handle = io.popen(command,"r")
- return handle and handle:read("*all") or ""
+ if handle then
+ local result = handle:read("*all") or ""
+ handle:close()
+ return result
+ else
+ return ""
+ end
end
if not io.fileseparator then
@@ -191,9 +197,8 @@ end
-- no need for function anymore as we have more clever code and helpers now
-- this metatable trickery might as well disappear
-os.resolvers = os.resolvers or { } -- will become private
-
-local resolvers = os.resolvers
+local resolvers = os.resolvers or { }
+os.resolvers = resolvers
setmetatable(os, { __index = function(t,k)
local r = resolvers[k]
@@ -216,6 +221,8 @@ local function guess()
return os.resultof("echo $HOSTTYPE") or ""
end
+-- os.bits = 32 | 64
+
if platform ~= "" then
os.platform = platform
@@ -224,10 +231,14 @@ elseif os.type == "windows" then
-- we could set the variable directly, no function needed here
- function os.resolvers.platform(t,k)
+ -- PROCESSOR_ARCHITECTURE : binary platform
+ -- PROCESSOR_ARCHITEW6432 : OS platform
+
+ function resolvers.platform(t,k)
local platform, architecture = "", os.getenv("PROCESSOR_ARCHITECTURE") or ""
if find(architecture,"AMD64") then
- platform = "mswin-64"
+ -- platform = "mswin-64"
+ platform = "win64"
else
platform = "mswin"
end
@@ -238,7 +249,7 @@ elseif os.type == "windows" then
elseif name == "linux" then
- function os.resolvers.platform(t,k)
+ function resolvers.platform(t,k)
-- we sometimes have HOSTTYPE set so let's check that first
local platform, architecture = "", os.getenv("HOSTTYPE") or os.resultof("uname -m") or ""
if find(architecture,"x86_64") then
@@ -265,7 +276,7 @@ elseif name == "macosx" then
therefore not permitted to run the 64 bit kernel.
]]--
- function os.resolvers.platform(t,k)
+ function resolvers.platform(t,k)
-- local platform, architecture = "", os.getenv("HOSTTYPE") or ""
-- if architecture == "" then
-- architecture = os.resultof("echo $HOSTTYPE") or ""
@@ -288,7 +299,7 @@ elseif name == "macosx" then
elseif name == "sunos" then
- function os.resolvers.platform(t,k)
+ function resolvers.platform(t,k)
local platform, architecture = "", os.resultof("uname -m") or ""
if find(architecture,"sparc") then
platform = "solaris-sparc"
@@ -302,7 +313,7 @@ elseif name == "sunos" then
elseif name == "freebsd" then
- function os.resolvers.platform(t,k)
+ function resolvers.platform(t,k)
local platform, architecture = "", os.resultof("uname -m") or ""
if find(architecture,"amd64") then
platform = "freebsd-amd64"
@@ -316,7 +327,7 @@ elseif name == "freebsd" then
elseif name == "kfreebsd" then
- function os.resolvers.platform(t,k)
+ function resolvers.platform(t,k)
-- we sometimes have HOSTTYPE set so let's check that first
local platform, architecture = "", os.getenv("HOSTTYPE") or os.resultof("uname -m") or ""
if find(architecture,"x86_64") then
@@ -335,7 +346,7 @@ else
-- os.setenv("MTX_PLATFORM",platform)
-- os.platform = platform
- function os.resolvers.platform(t,k)
+ function resolvers.platform(t,k)
local platform = "linux"
os.setenv("MTX_PLATFORM",platform)
os.platform = platform
@@ -344,6 +355,12 @@ else
end
+function resolvers.bits(t,k)
+ local bits = find(os.platform,"64") and 64 or 32
+ os.bits = bits
+ return bits
+end
+
-- beware, we set the randomseed
-- from wikipedia: Version 4 UUIDs use a scheme relying only on random numbers. This algorithm sets the
@@ -382,31 +399,43 @@ end
local timeformat = format("%%s%s",os.timezone(true))
local dateformat = "!%Y-%m-%d %H:%M:%S"
+local lasttime = nil
+local lastdate = nil
function os.fulltime(t,default)
- t = tonumber(t) or 0
+ t = t and tonumber(t) or 0
if t > 0 then
-- valid time
elseif default then
return default
else
- t = nil
+ t = time()
+ end
+ if t ~= lasttime then
+ lasttime = t
+ lastdate = format(timeformat,date(dateformat))
end
- return format(timeformat,date(dateformat,t))
+ return lastdate
end
local dateformat = "%Y-%m-%d %H:%M:%S"
+local lasttime = nil
+local lastdate = nil
function os.localtime(t,default)
- t = tonumber(t) or 0
+ t = t and tonumber(t) or 0
if t > 0 then
-- valid time
elseif default then
return default
else
- t = nil
+ t = time()
end
- return date(dateformat,t)
+ if t ~= lasttime then
+ lasttime = t
+ lastdate = date(dateformat,t)
+ end
+ return lastdate
end
function os.converttime(t,default)
@@ -474,3 +503,60 @@ end
-- print(os.which("inkscape"))
-- print(os.which("gs.exe"))
-- print(os.which("ps2pdf"))
+
+-- These are moved from core-con.lua (as I needed them elsewhere).
+
+local function isleapyear(year)
+ return (year % 400 == 0) or ((year % 100 ~= 0) and (year % 4 == 0))
+end
+
+os.isleapyear = isleapyear
+
+-- nicer:
+--
+-- local days = {
+-- [false] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
+-- [true] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
+-- }
+--
+-- local function nofdays(year,month)
+-- return days[isleapyear(year)][month]
+-- return month == 2 and isleapyear(year) and 29 or days[month]
+-- end
+--
+-- more efficient:
+
+local days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
+
+local function nofdays(year,month)
+ if not month then
+ return isleapyear(year) and 365 or 364
+ else
+ return month == 2 and isleapyear(year) and 29 or days[month]
+ end
+end
+
+os.nofdays = nofdays
+
+function os.weekday(day,month,year)
+ return date("%w",time { year = year, month = month, day = day }) + 1
+end
+
+function os.validdate(year,month,day)
+ -- we assume that all three values are set
+ -- year is always ok, even if lua has a 1970 time limit
+ if month < 1 then
+ month = 1
+ elseif month > 12 then
+ month = 12
+ end
+ if day < 1 then
+ day = 1
+ else
+ local max = nofdays(year,month)
+ if day > max then
+ day = max
+ end
+ end
+ return year, month, day
+end