summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMika Havela <mika.havela@gmail.com>2008-01-13 21:16:50 +0000
committerMika Havela <mika.havela@gmail.com>2008-01-13 21:16:50 +0000
commit592b94ed536fa16b80860928132df31b9260cd06 (patch)
tree0fe363a29e81da83771843e59dbca90a30d436a4 /lib
parentda7e1ebc7a57fa48787689b0225e08f7f1ec8479 (diff)
downloadacf-core-592b94ed536fa16b80860928132df31b9260cd06.tar.bz2
acf-core-592b94ed536fa16b80860928132df31b9260cd06.tar.xz
Moving getopts function from openntpd-model to a library.\nMoving daemon-control to a library.\nThese 2 libraries are not documented yet and are not working as they should. Work in progress!
git-svn-id: svn://svn.alpinelinux.org/acf/core/trunk@558 ab2d0c66-481e-0410-8bed-d214d4d58bed
Diffstat (limited to 'lib')
-rw-r--r--lib/daemoncontrol.lua0
-rw-r--r--lib/getopts.lua59
2 files changed, 59 insertions, 0 deletions
diff --git a/lib/daemoncontrol.lua b/lib/daemoncontrol.lua
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lib/daemoncontrol.lua
diff --git a/lib/getopts.lua b/lib/getopts.lua
new file mode 100644
index 0000000..45aca7b
--- /dev/null
+++ b/lib/getopts.lua
@@ -0,0 +1,59 @@
+module (..., package.seeall)
+
+function getoptsfromfile (file, search, filter)
+ local opts = nil
+ local conf_file = fs.read_file_as_array ( file )
+ for i=1,table.maxn(conf_file) do
+ local l = conf_file[i]
+ if not string.find ( l, "^[;#].*" ) then
+ local a = string.match ( l, "^%s*(%S*)=" )
+ if (a) then
+ if not (search) or (search == a) then
+ local b = string.match ( l, '^%s*%S*%=%"?(.-)%s*%"?%s*$' )
+ local optstable = getopts.opts_to_table(b,filter)
+ if (optstable) or not (filter) then
+ if not (opts) then
+ opts = {}
+ end
+ if (optstable) then
+ opts[a] = optstable
+ else
+ opts[a] = b
+ end
+ end
+ end
+ end
+ end
+ end
+ return opts
+end
+
+function opts_to_table ( optstring, filter )
+ local optsparams = nil
+ local optstr = string.match(optstring, "^\"?(.*)%\"?") -- Filter away leading/trailing "
+ if optstr then
+ local option = ""
+ local optvalue = ""
+ for j = 1, string.len(optstr) do
+ if (string.sub(optstr, j, j) == "-") then
+ option=string.sub(optstr, j, j+1)
+ if not (filter) or (filter == option) then
+ for k = j+1, string.len(optstr) do
+ if not (optsparams) then
+ optsparams = {}
+ end
+ if (string.sub(optstr, k, k) == "-") then
+ optsparams[option] = string.match(string.sub(optstr, j+2, k-1),"^%s*(.*)%s?")
+ break
+ end
+ if (k == string.len(optstr)) then
+ optsparams[option] = string.match(string.sub(optstr, j+2, k),"^%s*(.*)%s?")
+ break
+ end
+ end
+ end
+ end
+ end
+ end
+ return optsparams
+end