1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
module (..., package.seeall)
require("fs")
-- Search the option string for separate options (-x or --xyz) and put them in a table
local opts_to_table = function ( optstring, filter )
local optsparams
if optstring then
local optstr = " " .. optstring .. " "
for o in string.gmatch(optstr, "%s%-%-?%a+%s+%a*") do
local option = string.match(o, "%-%-?%a+")
if not filter or filter == option then
if not optsparams then optsparams = {} end
optsparams[option] = string.match(o, "%a*$")
end
end
end
return optsparams
end
function setoptsinfile (file, search, option, value)
local opts = {}
local newfilecontent = nil
local filecontent = nil
opts = getoptsfromfile(file) or {}
filecontent = fs.read_file(file) or ""
if (filecontent == "") or (opts[search] == "") or (opts[search] == nil) then
opts[search] = {}
end
if not (search) or not (option) then
return fales, nil, "Systeminformation - Invalid usage of function getopts.setoptsinfile()"
end
--Change to new value
opts[search][option] = value
local optstr = ""
for k,v in pairs(opts) do
if (k == search) then
optstr = optstr.. k .. "=\""
for kk,vv in pairs(v) do
optstr = optstr .. kk .. " " .. vv .. " "
end
optstr = string.match(optstr, "(.-)%s*$") .. "\""
end
end
newfilecontent = string.gsub(filecontent, "%s*[;#]?" .. search .. "%s*=.-\n?$", "\n" .. optstr .. "\n") or ""
if (string.find(newfilecontent, search .. "%s*=" ) == nil) or (newfilecontent == "") then
fs.write_file(file,string.match(filecontent, "(.-)\n*$") .. "\n" .. optstr .. "\n")
else
fs.write_file(file,string.match(newfilecontent, "(.-)\n*$"))
end
return true, "File '" .. file .. "' has been modifyed!", nil
end
-- Parse file for options returned in a table
-- If search_section is defined, only report options in matching section
-- If search_option is defined, only report matching options
-- If to_table is true, attempt to convert option string to array of options
-- If filter is defined (and table is true), only list option matching filter
function getoptsfromfile (file, search_section, search_option, to_table, filter)
local opts = nil
if not (fs.is_file(file)) then return nil end
local conf_file = fs.read_file_as_array ( file )
local section = ""
for i,l in ipairs(conf_file) do
-- check if comment line
if not string.find ( l, "^%s*#" ) then
-- first, concat lines
local j = 1
while string.find ( l, "\\%s*$" ) and conf_file[i+j] do
l = string.match ( l, "^(.*)\\%s*$" ) .. " " .. conf_file[i+j]
j = j+1
end
-- find section name
local a = string.match ( l, "^%s*%[%s*(%S+)%s*%]" )
if a then
section = a
elseif not (search_section) or (search_section == section) then
-- find option name
a = string.match ( l, "^%s*(%S+)%s*=" )
if a and (not (search_option) or (search_option == a)) then
-- Figure out the value
local b = string.match ( l, '^%s*%S+%s*%=%s*(.*)$' ) or ""
-- remove comments from end of line
if string.find ( b, '#' ) then
b = string.match ( b, '^(.*)#.*$' ) or ""
end
-- remove spaces from front and back
b = string.match ( b, '^%s*(.*%S)%s*$' ) or ""
-- finally, remove quotes
if #b > 1 and string.sub(b,1,1) == '"' and string.sub(b,-1) == '"' then
b = string.sub(b,2,-2) or ""
end
if to_table == true then
local optstable = opts_to_table(b,filter)
if (optstable) then
if not (opts) then opts = {} end
if not (opts[section]) then opts[section] = {} end
opts[section][a] = optstable
---[[ Next line is DEBUG info. Should be commented out!
--opts[a]["debug"] = b
-- End debug info. --]]
end
else
if not (opts) then opts = {} end
if not (opts[section]) then opts[section] = {} end
opts[section][a] = b
end
end
end
end
end
if opts and search_section and search_option then
return opts[search_section][search_option]
elseif opts and search_section then
return opts[search_section]
end
return opts
end
|