-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinitool.sml
354 lines (326 loc) · 12.6 KB
/
initool.sml
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
(* initool -- manipulate the contents of INI files from the command line
* Copyright (c) 2015-2018, 2023-2024 D. Bohdan
* License: MIT
*)
exception Encoding of string
val unsupportedEncoding = "unsupported encoding: "
fun checkWrongEncoding (lines: string list) =
let
val _ =
case lines of
first :: _ =>
(case map Char.ord (String.explode first) of
0x00 :: 0x00 :: 0xFE :: 0xFF :: _ =>
raise Encoding (unsupportedEncoding ^ "UTF-32 BE")
| 0xFF :: 0xFE :: 0x00 :: 0x00 :: _ =>
raise Encoding (unsupportedEncoding ^ "UTF-32 LE")
| 0xFE :: 0xFF :: _ =>
raise Encoding (unsupportedEncoding ^ "UTF-16 BE")
| 0xFF :: 0xFE :: _ =>
raise Encoding (unsupportedEncoding ^ "UTF-16 LE")
| _ => ())
| _ => ()
in
lines
end
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 | Error of string
fun processFileCustom quiet passThrough successFn filterFn filename =
let
val parsed =
((Ini.parse passThrough) o checkWrongEncoding 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 replaceUsage = " <filename> <section> <key> <text> <replacement>"
val deleteUsage = " <filename> <section> [<key>]"
val availableCommands =
"available commands: get, exists, set, replace, delete, help, version"
val invalidUsage = "invalid usage: "
val unknownCommand = "unknown command: "
val usage = "usage: "
val allUsage =
(usage ^ "initool [-i|--ignore-case] [-p|--pass-through] "
^ "<command> [<arg> ...]\n\n" ^ "commands:"
^
(String.concatWith "\n "
[ ""
, "get" ^ getUsage
, "exists" ^ existsUsage
, "set" ^ setUsage
, "replace" ^ replaceUsage
, "delete" ^ deleteUsage
]) ^ "\n\n help\n version\n\n"
^ "Each command can be abbreviated to its first letter. "
^ "<section>, <key>, and <text> can be '*' or '_' to match anything. "
^ "Empty <text> matches empty values.")
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 [] = Output allUsage
| helpCommand [_] = helpCommand []
| helpCommand (cmd :: rest) =
Error (invalidUsage ^ (formatArgs (cmd :: rest)) ^ "\n" ^ usage ^ cmd)
fun versionCommand [] =
let val version = "1.0.0"
in Output (version ^ "\n")
end
| versionCommand [_] = versionCommand []
| versionCommand (cmd :: rest) =
Error (invalidUsage ^ (formatArgs (cmd :: rest)) ^ "\n" ^ usage ^ cmd)
type options = {ignoreCase: bool, passThrough: bool}
fun idOptions (opts: options) : Id.options = {ignoreCase = #ignoreCase opts}
fun getCommand (opts: options) [_, filename] =
processFile (#passThrough opts) (fn _ => true) (fn x => x) filename
| getCommand opts [_, filename, section] =
(* Get section *)
processFile (#passThrough opts) (fn (_, filtered) => filtered <> [])
(Ini.select (idOptions 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 (idOptions opts) section key Id.Wildcard filtered
val q =
Ini.SelectProperty
{section = section, key = key, pattern = Id.Wildcard}
val filterFn = fn sections =>
(Ini.removeEmptySections o (Ini.select (idOptions opts) q)) sections
in
processFile (#passThrough opts) 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 (idOptions opts) section key Id.Wildcard filtered
val q =
Ini.SelectProperty
{section = section, key = key, pattern = Id.Wildcard}
val parsed =
((Ini.select (idOptions opts) q) o (Ini.parse (#passThrough opts))
o checkWrongEncoding 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: options) [_, filename, section] =
(* Section exists *)
let
val successFn = fn (parsed, _) =>
Ini.sectionExists (idOptions opts) (Id.fromStringWildcard section)
parsed
in
processFileQuiet (#passThrough opts) 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 (idOptions opts) section key Id.Wildcard parsed
in
processFileQuiet (#passThrough opts) 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: 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 (#passThrough opts) (fn _ => true)
(Ini.merge (idOptions opts) update) filename
end
| setCommand opts (cmd :: rest) =
Error
(invalidUsage ^ (formatArgs (cmd :: rest)) ^ "\n" ^ usage ^ cmd
^ setUsage)
| setCommand opts [] = setCommand opts ["set"]
fun replaceCommand (opts: options)
[_, filename, section, key, pattern, replacement] =
(* Replace pattern in value *)
let
val section = Id.fromStringWildcard section
val key = Id.fromStringWildcard key
val pattern = Id.fromStringWildcard pattern
val q = Ini.ReplaceInValue
{ section = section
, key = key
, pattern = pattern
, replacement = replacement
}
val successFn = fn (parsed, _) =>
Ini.propertyExists (idOptions opts) section key pattern parsed
in
processFile (#passThrough opts) successFn
(Ini.select (idOptions opts) q) filename
end
| replaceCommand opts (cmd :: rest) =
Error
(invalidUsage ^ (formatArgs (cmd :: rest)) ^ "\n" ^ usage ^ cmd
^ replaceUsage)
| replaceCommand opts [] = replaceCommand opts ["replace"]
fun deleteCommand (opts: options) [_, filename, section] =
(* Delete section *)
let
val successFn = fn (parsed, _) =>
Ini.sectionExists (idOptions opts) (Id.fromStringWildcard section)
parsed
in
processFile (#passThrough opts) successFn
(Ini.select (idOptions opts)
(Ini.RemoveSection (Id.fromStringWildcard section))) filename
end
| deleteCommand opts [_, filename, section, key] =
(* Delete property *)
let
val section = Id.fromStringWildcard section
val key = Id.fromStringWildcard key
val q = Ini.RemoveProperty {section = section, key = key}
val successFn = fn (parsed, _) =>
Ini.propertyExists (idOptions opts) section key Id.Wildcard parsed
in
processFile (#passThrough opts) successFn
(Ini.select (idOptions 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: 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, passThrough = #passThrough opts} args
| processArgs opts ("--ignore-case" :: args) =
processArgs {ignoreCase = true, passThrough = #passThrough opts} args
| processArgs opts ("-p" :: args) =
processArgs {ignoreCase = #ignoreCase opts, passThrough = true} args
| processArgs opts ("--pass-through" :: args) =
processArgs {ignoreCase = #ignoreCase opts, passThrough = 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 ("r" :: args) =
replaceCommand opts ("r" :: args)
| processArgs opts ("replace" :: args) =
replaceCommand opts ("replace" :: 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)
fun handleException (message: string) =
exitWithError "" ("Error: " ^ message)
val args = CommandLine.arguments ()
val result =
processArgs {ignoreCase = false, passThrough = false} args
handle
Encoding message => handleException message
| Ini.Tokenization message => handleException message
val _ =
case result of
Output s => printFlush TextIO.stdOut s
| FailureOutput s => exitWithError s ""
| Error s => exitWithError "" s
val _ = OS.Process.exit (OS.Process.success)