forked from dbohdan/initool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitool.sml
More file actions
281 lines (259 loc) · 9.56 KB
/
initool.sml
File metadata and controls
281 lines (259 loc) · 9.56 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
(* initool -- manipulate the contents of INI files from the command line
* Copyright (c) 2015-2018, 2023 D. Bohdan
* License: MIT
*)
fun readLines (filename: string) : string list =
let
val file =
case filename of
"-" => TextIO.stdIn
| _ => TextIO.openIn filename
val contents = TextIO.inputAll file
val _ = TextIO.closeIn file
val contentsNoTrailingNewline =
if String.isSuffix "\n" contents then
String.extract (contents, 0, SOME ((String.size contents) - 1))
else
contents
in
String.fields (fn c => c = #"\n") contentsNoTrailingNewline
end
fun withNewlineIfNotEmpty (s: string) =
if s = "" orelse String.isSuffix "\n" s then s else s ^ "\n"
fun printFlush (stream: TextIO.outstream) (s: string) =
let val _ = TextIO.output (stream, withNewlineIfNotEmpty s)
in TextIO.flushOut stream
end
fun exitWithError (output: string) (err: string) =
let
val _ = printFlush TextIO.stdOut output
val _ = printFlush TextIO.stdErr err
in
OS.Process.exit (OS.Process.failure)
end
datatype result =
Output of string
| FailureOutput of string
| Notification of string
| Error of string
fun processFileCustom quiet successFn filterFn filename =
let
val parsed = (Ini.parse o readLines) filename
val filtered = filterFn parsed
val success = successFn (parsed, filtered)
val output = if quiet then "" else Ini.stringify filtered
in
if success then Output output else FailureOutput output
end
val processFile = processFileCustom false
val processFileQuiet = processFileCustom true
val getUsage = " <filename> [<section> [<key> [-v|--value-only]]]"
val existsUsage = " <filename> <section> [<key>]"
val setUsage = " <filename> <section> <key> <value>"
val deleteUsage = " <filename> <section> [<key>]"
val availableCommands =
"available commands: get, exists, set, delete, help, version"
val invalidUsage = "invalid usage: "
val unknownCommand = "unknown command: "
val usage = "usage: "
val allUsage =
(usage ^ "initool [-i|--ignore-case] <command> [<arg> ...]\n\n" ^ "commands:"
^
(String.concatWith "\n "
[ ""
, "get" ^ getUsage
, "exists" ^ existsUsage
, "set" ^ setUsage
, "delete" ^ deleteUsage
]) ^ "\n\n help\n version\n\n"
^ "Each command can be abbreviated to its first letter. "
^ "<section> and <key> can be '*' to match anything.")
fun formatArgs (args: string list) =
let
val escapeSpecial = fn s =>
String.translate
(fn #"\"" => "\\\"" | #"\\" => "\\\\" | c => String.str c) s
val shouldQuote = fn s =>
List.exists (fn c => Char.isSpace c orelse c = #"\"" orelse c = #"\\")
(String.explode s)
val quoteArg = fn arg =>
if shouldQuote arg then "\"" ^ (escapeSpecial arg) ^ "\"" else arg
in
String.concatWith " " (List.map quoteArg args)
end
fun helpCommand [] = Notification allUsage
| helpCommand [_] = helpCommand []
| helpCommand (cmd :: rest) =
Error (invalidUsage ^ (formatArgs (cmd :: rest)) ^ "\n" ^ usage ^ cmd)
fun versionCommand [] =
let val version = "0.13.0"
in Output (version ^ "\n")
end
| versionCommand [_] = versionCommand []
| versionCommand (cmd :: rest) =
Error (invalidUsage ^ (formatArgs (cmd :: rest)) ^ "\n" ^ usage ^ cmd)
fun getCommand (opts: Id.options) [_, filename] =
processFile (fn _ => true) (fn x => x) filename
| getCommand opts [_, filename, section] =
(* Get section *)
processFile (fn (_, filtered) => filtered <> [])
(Ini.select opts ((Ini.SelectSection o Id.fromStringWildcard) section))
filename
| getCommand opts [_, filename, section, key] =
(* Get property *)
let
val section = Id.fromStringWildcard section
val key = Id.fromStringWildcard key
val successFn = fn (_, filtered) =>
Ini.propertyExists opts section key filtered
val q = Ini.SelectProperty {section = section, key = key}
val filterFn = fn sections =>
(Ini.removeEmptySections o (Ini.select opts q)) sections
in
processFile successFn filterFn filename
end
| getCommand opts [cmd, filename, section, key, "-v"] =
getCommand opts [cmd, filename, section, key, "--value-only"]
| getCommand opts [_, filename, section, key, "--value-only"] =
(* Get only the value *)
let
val section = Id.fromStringWildcard section
val key = Id.fromStringWildcard key
val successFn = fn (_, filtered) =>
Ini.propertyExists opts section key filtered
val q = Ini.SelectProperty {section = section, key = key}
val parsed = ((Ini.select opts q) o Ini.parse o readLines) filename
val allItems = List.concat
(List.map (fn {name = _, contents = xs} => xs) parsed)
val values =
List.mapPartial
(fn Ini.Property {key = _, value = value} => SOME value | _ => NONE)
allItems
val output = String.concatWith "\n" values
in
if values = [] then Error output else Output output
end
| getCommand opts (cmd :: rest) =
Error
(invalidUsage ^ (formatArgs (cmd :: rest)) ^ "\n" ^ usage ^ cmd
^ getUsage)
| getCommand opts [] = getCommand opts ["get"]
fun existsCommand (opts: Id.options) [_, filename, section] =
(* Section exists *)
let
val successFn = fn (parsed, _) =>
Ini.sectionExists opts (Id.fromStringWildcard section) parsed
in
processFileQuiet successFn (fn x => x) filename
end
| existsCommand opts [_, filename, section, key] =
(* Property exists *)
let
val section = Id.fromStringWildcard section
val key = Id.fromStringWildcard key
val successFn = fn (parsed, _) =>
Ini.propertyExists opts section key parsed
in
processFileQuiet successFn (fn x => x) filename
end
| existsCommand opts (cmd :: rest) =
Error
(invalidUsage ^ (formatArgs (cmd :: rest)) ^ "\n" ^ usage ^ cmd
^ existsUsage)
| existsCommand opts [] = existsCommand opts ["exists"]
fun setCommand (opts: Id.options) [_, filename, section, key, value] =
(* Set value *)
let
val update =
[{ name = Id.fromStringWildcard section
, contents =
[Ini.Property {key = Id.fromStringWildcard key, value = value}]
}]
in
processFile (fn _ => true) (Ini.merge opts update) filename
end
| setCommand opts (cmd :: rest) =
Error
(invalidUsage ^ (formatArgs (cmd :: rest)) ^ "\n" ^ usage ^ cmd
^ setUsage)
| setCommand opts [] = setCommand opts ["set"]
fun deleteCommand (opts: Id.options) [_, filename, section] =
(* Delete section *)
let
val successFn = fn (parsed, _) =>
Ini.sectionExists opts (Id.fromStringWildcard section) parsed
in
processFile successFn
(Ini.select opts (Ini.RemoveSection (Id.fromStringWildcard section)))
filename
end
| deleteCommand opts [_, filename, section, key] =
(* Delete property *)
let
val q =
Ini.RemoveProperty
{ section = Id.fromStringWildcard section
, key = Id.fromStringWildcard key
}
val successFn = fn (parsed, _) =>
Ini.sectionExists opts (Id.fromStringWildcard section) parsed
in
processFile successFn (Ini.select opts q) filename
end
| deleteCommand opts (cmd :: rest) =
Error
(invalidUsage ^ (formatArgs (cmd :: rest)) ^ "\n" ^ usage ^ cmd
^ deleteUsage)
| deleteCommand opts [] = deleteCommand opts ["delete"]
fun processArgs (opts: Id.options) [] = helpCommand []
| processArgs opts ("h" :: args) =
helpCommand ("h" :: args)
| processArgs opts ("-h" :: args) =
helpCommand ("-h" :: args)
| processArgs opts ("-help" :: args) =
helpCommand ("-help" :: args)
| processArgs opts ("--help" :: args) =
helpCommand ("--help" :: args)
| processArgs opts ("-?" :: args) =
helpCommand ("-?" :: args)
| processArgs opts ("/?" :: args) =
helpCommand ("/?" :: args)
| processArgs opts ("help" :: args) =
helpCommand ("help" :: args)
| processArgs opts ("v" :: args) =
versionCommand ("v" :: args)
| processArgs opts ("version" :: args) =
versionCommand ("version" :: args)
| processArgs opts ("-i" :: args) =
processArgs {ignoreCase = true} args
| processArgs opts ("--ignore-case" :: args) =
processArgs {ignoreCase = true} args
| processArgs opts ("g" :: args) =
getCommand opts ("g" :: args)
| processArgs opts ("get" :: args) =
getCommand opts ("get" :: args)
| processArgs opts ("e" :: args) =
existsCommand opts ("e" :: args)
| processArgs opts ("exists" :: args) =
existsCommand opts ("exists" :: args)
| processArgs opts ("s" :: args) =
setCommand opts ("s" :: args)
| processArgs opts ("set" :: args) =
setCommand opts ("set" :: args)
| processArgs opts ("d" :: args) =
deleteCommand opts ("d" :: args)
| processArgs opts ("delete" :: args) =
deleteCommand opts ("delete" :: args)
| processArgs opts (cmd :: _) =
Error (unknownCommand ^ (formatArgs [cmd]) ^ "\n" ^ availableCommands)
val args = CommandLine.arguments ()
val result =
processArgs {ignoreCase = false} args
handle Ini.Tokenization (message) => exitWithError "" ("Error: " ^ message)
val _ =
case result of
Output s => printFlush TextIO.stdOut s
| FailureOutput s => exitWithError s ""
| Notification s => printFlush TextIO.stdErr s
| Error s => exitWithError "" s
val _ = OS.Process.exit (OS.Process.success)