forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.lua
More file actions
803 lines (705 loc) · 25 KB
/
query.lua
File metadata and controls
803 lines (705 loc) · 25 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
-- Query is a script useful for finding and reading values of data structure fields. Purposes will likely be exclusive to writing lua script code.
-- Written by Josh Cooper(cppcooper) on 2017-12-21, last modified: 2021-06-13
-- Version: 3.2
--luacheck:skip-entirely
local utils=require('utils')
local validArgs = utils.invert({
'help',
'unit',
'item',
'plant',
'building',
'job',
'tile',
'block',
'script',
'json',
'table',
'getfield',
'search',
'findvalue',
'maxdepth',
'maxlength',
'excludetypes',
'excludekinds',
'dumb',
'showpaths',
'setvalue',
'alignto',
'oneline',
'1',
'nopointers',
'disableprint',
'debug',
'debugdata'
})
local args = utils.processArgs({...}, validArgs)
local selection = nil
local path_info = nil
local path_info_pattern = nil
local new_value = nil
local find_value = nil
local maxdepth = nil
local cur_depth = -1
local tilex = nil
local tiley = nil
local bToggle = true
local bool_flags = {}
local help = [====[
devel/query
===========
Query is a script useful for finding and reading values of data structure
fields. Purposes will likely be exclusive to writing lua script code,
possibly C++.
This script takes your data selection eg.{table,unit,item,tile,etc.} then
recursively iterates through it, outputting names and values of what it finds.
As it iterates you can have it do other things, like search for a specific
structure pattern (see lua patterns) or set the value of fields matching the
selection and any search pattern specified.
.. Note::
This is a recursive search function. The data structures are also recursive.
So there are a few things that must be considered (in order):
- Is the search depth too high? (Default: 7)
- Is the data capable of being iterated, or does it only have a value?
- How can the data be iterated?
- Is the iteration count for the data too high? (Default: 257)
- Does the user want to exclude the data's type?
- Is the data recursively indexing (eg. A.B.C.A.*)?
- Does the data match the search pattern?
.. Warning::
This is a recursive script that's primary use is to search recursive data
structures. You can, fairly easily, cause an infinite loop. You can even
more easily run a query that simply requires an inordinate amount of time
to complete.
.. Tip::
Should the need arise, you can kill the command from another shell with
`kill-lua`, e.g. by running it with `dfhack-run` from another terminal.
Usage examples::
devel/query -unit -getfield id
devel/query -unit -search STRENGTH
devel/query -unit -search physical_attrs -maxdepth 2
devel/query -tile -search dig
devel/query -tile -search "occup.*carv"
devel/query -table df -maxdepth 2
devel/query -table df -maxdepth 2 -excludekinds s -excludetypes fsu -oneline
devel/query -table df.profession -findvalue FISH
devel/query -table df.global.ui.main -maxdepth 0
devel/query -table df.global.ui.main -maxdepth 0 -oneline
devel/query -table df.global.ui.main -maxdepth 0 -1
**Selection options:**
``-tile``
Selects the highlighted tile's block, and then
uses the tile's local position to index the 2D data.
``-block``
Selects the highlighted tile's block.
``-unit``
Selects the highlighted unit
``-item``
Selects the highlighted item.
``-plant``
Selects the highlighted plant.
``-building``
Selects the highlighted building.
``-job``
Selects the highlighted job.
``-script <script name>``
Selects the specified script (which must support being included with ``reqscript()``).
``-json <file>``
Loads the specified json file as a table to query.
.. Note::
The path starts at the DF root directory.
eg. -json /hack/scripts/dwarf_profiles.json
``-table <identifier>``
Selects the specified table (ie. 'value').
.. Note::
You must use dot notation to denote sub-tables.
eg. ``df.global.world``
``-getfield <name>``
Gets the specified field from the selection.
Must use in conjunction with one of the above selection
options. Must use dot notation to denote sub-fields.
**Query options:**
``-search <pattern>``
Searches the selection for field names with substrings
matching the specified value.
Usage::
devel/query -table dfhack -search pattern
devel/query -table dfhack -search [ pattern1 pattern2 ]
``-findvalue <value>``
Searches the selection for field values matching the specified value.
``-maxdepth <value>``
Limits the field recursion depth (default: 7)
``-maxlength <value>``
Limits the table sizes that will be walked (default: 257)
``-excludetypes [a|bfnstu0]``
Excludes native Lua data types. Single letters correspond to (in order):
All types listed here, Boolean, Function, Number, String, Table, Userdata, nil
``-excludekinds [a|bces]``
Excludes DF data types. Single letters correspond to (in order):
All types listed here, Bitfield-type, Class-type, Enum-type, Struct-type
``-dumb``
Disables intelligent checking for recursive data
structures (loops) and increases the ``-maxdepth`` to 25 if a
value is not already present
**General options:**
``-showpaths``
Displays the full path of a field instead of indenting.
``-setvalue <value>``
Attempts to set the values of any printed fields.
Supported types: boolean, string, integer
``-oneline``, ``-1``
Reduces output to one line, except with ``-debugdata``
``-alignto <value>``
Specifies the alignment column.
``-nopointers``
Disables printing values which contain memory addresses.
``-disableprint``
Disables printing. Might be useful if you are debugging
this script. Or to see if a query will crash (faster) but
not sure what else you could use it for.
``-debug <value>``
Enables debug log lines equal to or less than the value provided.
``-debugdata``
Enables debugging data. Prints type information under each field.
``-help``
Prints this help information.
]====]
--Section: core logic
function query(table, name, search_term, path, bprinted_parent, parent_table)
-- Is the search depth too high? (Default: 7)
-- Is the data capable of being iterated, or does it only have a value?
-- Is the data recursively indexing (eg. A.B.C.A.*)?
if bprinted_parent == nil then
bprinted_parent = false
end
if parent_table ~= nil then
if is_tiledata(parent_table) then
if setValue(parent_table[tilex], path, tiley, table, new_value) then
table = new_value
end
elseif setValue(parent_table, path, name, table, new_value) then
table = new_value
end
end
local bprinted = printField(path, name, table, bprinted_parent)
--print("table/field printed")
cur_depth = cur_depth + 1
if cur_depth <= maxdepth then
-- check that we can search
if is_searchable(name, table) then
-- iterate over search space
function recurse(field, value)
-- Is the data recursively indexing (eg. A.B.C.A.*)?
local new_tname = tostring(makeName(name, field))
if is_tiledata(value) then
local indexed_name = string.format("%s[%d][%d]", field,tilex,tiley)
query(value[tilex][tiley], indexed_name, search_term, appendField(path, indexed_name), bprinted, value)
elseif not is_looping(path, new_tname) then
query(value, new_tname, search_term, appendField(path, field), bprinted, table)
else
-- I don't know when this prints (if ever)
printField(path, field, value, bprinted)
end
end
foreach(table, name, recurse)
end
end
cur_depth = cur_depth - 1
return bprinted
end
function foreach(table, name, callback)
-- How can the data be iterated?
-- Is the iteration count for the data too high? (Default: 257)
function print_truncation_msg(name,index)
print("<query on " .. name .. " truncated after " .. index .. ">")
end
local index = 0
if getmetatable(table) and table._kind and (table._kind == "enum-type" or table._kind == "bitfield-type") then
for idx, value in ipairs(table) do
if is_exceeding_maxlength(index) then
print_truncation_msg(name, index)
return
end
callback(idx, value)
index = index + 1
end
elseif table._type and string.find(tostring(table._type), "_list_link") then
for field, value in utils.listpairs(table) do
local m = tostring(field):gsub("<.*: ",""):gsub(">.*",""):gsub("%x%x%x%x%x%x","%1 ",1)
local s = string.format("next{%d}->item", index)
if is_exceeding_maxlength(index) then
print_truncation_msg(name, index)
return
end
callback(s, value)
index = index + 1
end
else
for field, value in safe_pairs(table) do
if is_exceeding_maxlength(index) then
print_truncation_msg(name, index)
return
end
callback(field, value)
index = index + 1
end
end
end
function setValue(table, path, field, value, new_value)
-- Does the data match the search pattern?
if args.setvalue then
if not args.search or is_match(path, field, value) then
if type(table[field]) == type(value) and type(value) == type(new_value) then
table[field] = new_value
print(string.format("Set %s value to %s, from %s", path, new_value, value)) --will call tostring
return true
end
end
end
return false
end
--Section: entry/initialization
function main()
if args.help then
print(help)
return
end
processArguments()
selection, path_info = table.unpack{getSelectionData()}
debugf(0, tostring(selection), path_info)
if selection == nil then
qerror(string.format("Selected %s is null. Invalid selection.", path_info))
return
end
debugf(0, string.format("query(%s, %s, %s, %s)", selection, path_info, args.search, path_info))
query(selection, path_info, args.search, path_info)
end
function getSelectionData()
local selection = nil
local path_info = nil
if args.table then
debugf(0,"table selection")
selection = utils.df_expr_to_ref(args.table)
path_info = args.table
path_info_pattern = escapeSpecialChars(path_info)
elseif args.json then
local json = require("json")
local json_file = string.format("%s/%s", dfhack.getDFPath(), args.json)
if dfhack.filesystem.isfile(json_file) then
selection = json.decode_file(json_file)
path_info = args.json
path_info_pattern = path_info
else
qerror(string.format("File '%s' not found.", json_file))
end
elseif args.script then
selection = reqscript(args.script)
path_info = args.script
path_info_pattern = path_info
elseif args.unit then
debugf(0,"unit selection")
selection = dfhack.gui.getSelectedUnit()
path_info = "unit"
path_info_pattern = path_info
elseif args.item then
debugf(0,"item selection")
selection = dfhack.gui.getSelectedItem()
path_info = "item"
path_info_pattern = path_info
elseif args.plant then
debugf(0,"plant selection")
selection = dfhack.gui.getSelectedPlant()
path_info = "plant"
path_info_pattern = path_info
elseif args.building then
debugf(0,"item selection")
selection = dfhack.gui.getSelectedBuilding()
path_info = "building"
path_info_pattern = path_info
elseif args.job then
debugf(0,"job selection")
selection = dfhack.gui.getSelectedJob()
path_info = "job"
path_info_pattern = path_info
elseif args.tile then
debugf(0,"tile selection")
local pos = copyall(df.global.cursor)
selection = dfhack.maps.ensureTileBlock(pos.x,pos.y,pos.z)
bpos = selection.map_pos
path_info = string.format("blocks[%d][%d][%d]",bpos.x,bpos.y,bpos.z)
path_info_pattern = string.format("blocks%%[%d%%]%%[%d%%]%%[%d%%]",bpos.x,bpos.y,bpos.z)
tilex = pos.x%16
tiley = pos.y%16
elseif args.block then
debugf(0,"tile selection")
local pos = copyall(df.global.cursor)
selection = dfhack.maps.ensureTileBlock(pos.x,pos.y,pos.z)
bpos = selection.map_pos
path_info = string.format("blocks[%d][%d][%d]",bpos.x,bpos.y,bpos.z)
path_info_pattern = string.format("blocks%%[%d%%]%%[%d%%]%%[%d%%]",bpos.x,bpos.y,bpos.z)
else
print(help)
end
if args.getfield then
selection = findPath(selection,args.getfield)
path_info = path_info .. "." .. args.getfield
path_info_pattern = path_info_pattern .. "." .. args.getfield
end
--print(selection, path_info)
return selection, path_info
end
function processArguments()
if args["1"] then
args.oneline = true
end
--Table Recursion
if args.maxdepth then
maxdepth = tonumber(args.maxdepth)
if not maxdepth then
qerror(string.format("Must provide a number with -depth"))
end
elseif args.dumb then
maxdepth = 25
else
maxdepth = 7
end
args.maxdepth = maxdepth
--Table Length
if args.maxlength then
args.maxlength = tonumber(args.maxlength)
elseif args.dumb then
args.maxlength = 10000
else
--2048 was chosen as it was the smallest power of 2 that can query the `df` table entirely, and it seems likely it'll be able to query most structures fully
args.maxlength = 2048
end
new_value = toType(args.setvalue)
find_value = toType(args.findvalue)
-- singular or plural
args.excludetypes = args.excludetypes and args.excludetypes or args.excludetype
args.excludekinds = args.excludekinds and args.excludekinds or args.excludekind
-- must be a string
args.excludetypes = args.excludetypes and args.excludetypes or ""
args.excludekinds = args.excludekinds and args.excludekinds or ""
if string.find(args.excludetypes, 'a') then
bool_flags["boolean"] = true
bool_flags["function"] = true
bool_flags["number"] = true
bool_flags["string"] = true
bool_flags["table"] = true
bool_flags["userdata"] = true
else
bool_flags["boolean"] = string.find(args.excludetypes, 'b') and true or false
bool_flags["function"] = string.find(args.excludetypes, 'f') and true or false
bool_flags["number"] = string.find(args.excludetypes, 'n') and true or false
bool_flags["string"] = string.find(args.excludetypes, 's') and true or false
bool_flags["table"] = string.find(args.excludetypes, 't') and true or false
bool_flags["userdata"] = string.find(args.excludetypes, 'u') and true or false
end
if string.find(args.excludekinds, 'a') then
bool_flags["bitfield-type"] = true
bool_flags["class-type"] = true
bool_flags["enum-type"] = true
bool_flags["struct-type"] = true
else
bool_flags["bitfield-type"] = string.find(args.excludekinds, 'b') and true or false
bool_flags["class-type"] = string.find(args.excludekinds, 'c') and true or false
bool_flags["enum-type"] = string.find(args.excludekinds, 'e') and true or false
bool_flags["struct-type"] = string.find(args.excludekinds, 's') and true or false
end
end
local bRunOnce={}
function runOnce(caller)
if bRunOnce[caller] == true then
return false
end
bRunOnce[caller] = true
return true
end
function toType(str)
if str ~= nil then
if string.find(str,"true") then
return true
elseif string.find(str,"false") then
return false
elseif string.find(str,"nil") then
return nil
elseif tonumber(str) then
return tonumber(str)
else
return tostring(str)
end
end
return nil
end
--Section: filters
function is_searchable(field, value)
-- Is the data capable of being iterated, or does it only have a value?
if not df.isnull(value) then
debugf(3,string.format("is_searchable( %s ): type: %s, length: %s, count: %s", value,type(value),getTableLength(value), countTableLength(value)))
if not isEmpty(value) then
if getmetatable(value) then
if value._kind == "primitive" then
return false
elseif value._kind == "struct" then
if args.safer then
return false
else
return true
end
end
debugf(3,string.format("_kind: %s, _type: %s", value._kind, value._type))
end
for _,_ in safe_pairs(value) do
return true
end
end
end
return false
end
function match_searcharg(path,field)
if type(args.search) == "table" then
for _,pattern in pairs(args.search) do
if string.find(tostring(field),pattern) or string.find(path,pattern) then
return true
end
end
elseif string.find(tostring(field),args.search) or string.find(path,args.search) then
return true
end
return false
end
function is_match(path, field, value)
debugf(3, string.format("path: %s\nfield: %s\nvalue: %s", path, field, value))
if not args.search or match_searcharg(path,field) then
if not args.findvalue or (type(value) == type(find_value) and (value == find_value or (type(value) == "string" and string.find(value,find_value)))) then
return true
end
end
return false
end
function is_looping(path, field)
return not args.dumb and string.find(path, tostring(field))
end
function is_tiledata(value)
if args.tile and string.find(tostring(value),"%[16%]") then
if type(value) and string.find(tostring(value[tilex]),"%[16%]") then
return true
end
end
return false
end
function is_excluded(value)
return bool_flags[type(value)] or not isEmpty(value) and getmetatable(value) and bool_flags[value._kind]
end
function is_exceeding_maxlength(index)
return args.maxlength and not (index < args.maxlength)
end
--Section: table helpers
function isEmpty(t)
for _,_ in safe_pairs(t) do
return false
end
return true
end
function countTableLength(t)
local count = 0
for _,_ in safe_pairs(t) do
count = count + 1
end
debugf(1,string.format("countTableEntries( %s ) = %d",t,count))
return count
end
function getTableLength(t)
if type(t) == "table" then
local count=#t
debugf(1,string.format("----getTableLength( %s ) = %d",t,count))
return count
end
return 0
end
function findPath(t, path)
debugf(0,string.format("findPath(%s, %s)",t, path))
local curTable = t
if not curTable then
error("no table to search")
end
local keyParts = string.split(path, '.', true)
for _,v in pairs(keyParts) do
if v and curTable[v] ~= nil then
debugf(1,"found something",v,curTable,curTable[v])
curTable = curTable[v]
else
qerror("Key not found: " .. v)
end
end
--debugf(1,"returning",curTable)
return curTable
end
function hasMetadata(value)
if not isEmpty(value) then
if getmetatable(value) and value._kind and value._kind ~= nil then
return true
end
end
return false
end
--Section: output helpers
function makeName(tname, field)
if tonumber(field) then
return string.format("%s[%s]", tname, field)
end
return field
end
function appendField(parent, field)
newParent=""
if tonumber(field) then
newParent=string.format("%s[%s]",parent,field)
else
newParent=string.format("%s.%s",parent,field)
end
debugf(2, string.format("new parent: %s", newParent))
return newParent
end
function makeIndentation()
local base="| "
local indent=""
for i=0,(cur_depth) do
indent=indent .. string.format("%s",base)
end
--indent=string.format("%s ",base)
return indent
end
function presentField(path, field, value)
local output = tostring(args.showpaths and path or field)
local leading_indent = not args.showpaths and makeIndentation() or ""
output = leading_indent .. output
local align = 40
if tonumber(args.alignto) then
align = tonumber(args.alignto)
end
output = string.format("%-"..align.."s ", output .. ":")
if args.debugdata or not args.oneline or bToggle then
if not args.nopointers or not string.find(tostring(value), "0x%x%x%x%x%x%x") then
output = string.gsub(output," "," ~")
end
bToggle = false
else
bToggle = true
end
return output
end
function presentValue(field, value)
local output = ""
if not args.nopointers or not string.find(tostring(value), "0x%x%x%x%x%x%x") then
if not args.debugdata then
output = string.format(" %s", value)
elseif hasMetadata(value) then
if args.oneline then
output = string.format(" %s [%s]", value, value._kind)
else
local N = math.min(90, string.len(indented_field))
local newline_indent = string.format("%" .. N .. "s", "")
output = string.format("%s %s\n%s [has metatable; _kind: %s]", output, value, newline_indent, value._kind)
end
else
output = string.format("%s %s; type(%s) = %s", output, value, field, type(value))
end
end
return output
end
function presentDebugData(presentedField, field, value)
local output = ""
if args.debugdata then
local N = math.min(90, string.len(presentedField))
local newline_indent = string.format("%" .. N .. "s", "")
if hasMetadata(value) then
-- why not search?
if not args.search and args.oneline then
output = output .. string.format("\n%s type(%s): %s, _kind: %s, _type: %s",
newline_indent, field, type(value), value._kind, value._type)
else
output = output .. string.format("\n%s type(%s): %s\n%s _kind: %s\n%s _type: %s",
newline_indent, field, type(value), newline_indent, value._kind, newline_indent, value._type)
end
end
end
return output
end
function printOnce(key, msg)
if runOnce(key) then
print(msg)
end
end
--sometimes used to print fields, always used to print parents of fields
function printParents(path, field, value)
local value_printed = false
path = string.gsub(path, path_info_pattern, "")
field = string.gsub(field, path_info_pattern, "")
local cd = cur_depth
cur_depth = 0
local cur_path = path_info
local words = {}
local index = 1
for word in string.gmatch(path, '([^.]+)') do
words[index] = word
index = index + 1
end
local last_index = index - 1
for i,word in ipairs(words) do
if i ~= last_index then
cur_path = appendField(cur_path, word)
printOnce(cur_path, string.format("%s%s", makeIndentation(),word))
elseif string.find(word,"%a+%[%d+%]%[%d+%]") then
value_printed = true
cur_path = appendField(cur_path, word)
local f = presentField(path, field, value)
local v = presentValue(field, value)
print(f .. v .. presentDebugData(f, field, value))
end
cur_depth = cur_depth + 1
end
cur_depth = cd
return value_printed
end
function printField(path, field, value, bprinted_parent)
-- Does the data match the search pattern?
if runOnce(printField) then
-- print selection
printOnce(path,string.format("%s: %s", path, value))
return
end
if not args.disableprint and not is_excluded(value) then
if not args.search and not args.findvalue or is_match(path, field, value) then
local bprinted_field = false
if not args.showpaths and not bprinted_parent then
bprinted_field = printParents(path, field, value)
end
if not bprinted_field then
if is_tiledata(value) then
value = value[tilex][tiley]
field = string.format("%s[%d][%d]", field,tilex,tiley)
end
local f = presentField(path, field, value)
local v = presentValue(field, value)
print(f .. v .. presentDebugData(f, field, value))
end
return true
end
end
return false
end
function debugf(level,...)
if args.debug and level <= tonumber(args.debug) then
local str=string.format(" # %s",select(1, ...))
for i = 2, select('#', ...) do
str=string.format("%s\t%s",str,select(i, ...))
end
print(str)
end
end
function escapeSpecialChars(str)
return str:gsub('(['..("%^$()[].*+-?"):gsub("(.)", "%%%1")..'])', "%%%1")
end
main()
print()