summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Kendrick (plinth) <rjek@rjek.com>2013-11-25 08:58:46 +0000
committerRob Kendrick (plinth) <rjek@rjek.com>2013-11-25 08:58:46 +0000
commit785c8fadf519118a9c74e2bbc4f85e27c25a5f0f (patch)
tree6c43943975e924e08ce3bc29d82011f0e5f6749c
parent7ccbc8e4f585b669c762298489d24066cf6f6218 (diff)
downloadluxio-785c8fadf519118a9c74e2bbc4f85e27c25a5f0f.tar.gz
Add string flag to number convience function
-rw-r--r--luxio/simple.lua54
1 files changed, 53 insertions, 1 deletions
diff --git a/luxio/simple.lua b/luxio/simple.lua
index 22ecfd4..8d1dbd0 100644
--- a/luxio/simple.lua
+++ b/luxio/simple.lua
@@ -190,7 +190,13 @@
-- nil, errmsg, errno
-- getcwd()
-- Returns the current working directory, or nil, errmsg, errno
-
+--
+-- flags()
+-- Parses a list of comma-seperated flag strings, looks them up
+-- as UNIX constants, and ORs them together.
+-- Common suffixes are optional, and the system is case-insensitive.
+-- For example, flags("rdrw,creat,excl") would be the same as ORing
+-- together O_RDWR, O_CREAT, and O_EXCL.
local l = require "luxio"
@@ -288,6 +294,50 @@ local function err(s, errno)
return nil, ("%s: %s"):format(s, strerror(errno)), errno
end
+local flag_prefixes = {
+ "O_", "SEEK_", "SPLICE_F_", "S_", "F_", "PF_", "AF_", "IPPROTO_", "SOCK_",
+ "SHUT_", "SO_", "TCP_", "AI_", "POLL", "LOG_", "EXIT_"
+}
+
+local function flags_find_flag(f)
+ for flag in pairs(l) do
+ if type(l[flag]) == "number" then
+ if f == flag or f == flag:lower() then return flag end
+ -- now look for both lower and upper using a set of prefixes
+ for _, prefix in ipairs(flag_prefixes) do
+ local try = prefix .. f
+ if try == flag or try:upper() == flag:upper() then return flag end
+ end
+ end
+ end
+
+ return nil
+end
+
+local flag_cache = { }
+
+local function flags(...)
+ local r = 0
+ local f = table.concat({ ... }, ",")
+
+ if flag_cache[f] then return flag_cache[f] end
+
+
+ for i in f:gmatch("([^,]*)") do
+ if i ~= "" then
+ local flag = flags_find_flag(i)
+ if not flag then
+ error("unknown flag " .. i)
+ end
+ r = l_bit_bor(r, luxio[flag])
+ end
+ end
+
+ flag_cache[f] = r
+
+ return r
+end
+
local function sio_perm_flags_help(s, read, write, execute, sid)
local c = { }
for l in s:gmatch "." do
@@ -1251,6 +1301,8 @@ return {
stderr = sio_wrap_mt(l.STDERR_FILENO, true, "<stderr>"),
luxio = l,
+
+ flags = flags,
_VERSION = "Luxio Simple Interface " .. tostring(l._RELEASE),
_COPYRIGHT = "Copyright 2012 Rob Kendrick <rjek+luxio@rjek.com>",