-
Notifications
You must be signed in to change notification settings - Fork 97
/
TclSetup.bat
2230 lines (2073 loc) · 106 KB
/
TclSetup.bat
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
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<# :# PowerShell comment block protecting the Batch section
@echo off
:#----------------------------------------------------------------------------#
:# #
:# File name TclSetup.bat #
:# #
:# Description Configure Windows for running Tcl scripts #
:# #
:# Note Work around shortcomings of ActiveTCL distributions, #
:# which do not set environment variables correctly for #
:# running console-based scripts with tclsh. #
:# Also after updates, the interpreter name changes, like #
:# from tclsh84.exe to tclsh85.exe. #
:# #
:# Run "TclSetup -?" to get a list of available options. #
:# #
:# History #
:# 2010-04-02 JFL Created the tclsh.bat script. #
:# 2011-08-17 JFL Explicitely enable CMD extensions and delayed expansion. #
:# 2011-09-06 JFL Setup now associates .tcl->tclsh and .tk->wish. #
:# Added a -t option to test setup without changing it. #
:# 2012-04-23 JFL Setup now updates environment variable PATHEXT if needed. #
:# 2012-04-24 JFL Added a PowerShell routine to notify all windows of the #
:# environment block change. #
:# 2014-05-13 JFL Fixed the setting of variable PATHEXT. #
:# 2014-06-23 JFL Make sure the local path includes the Tcl's bin directory.#
:# Bug fix: Avoid adding trailing spaces to variable PATHEXT.#
:# 2018-10-02 JFL Split off of tclsh.bat, to match the existing PySetup.bat.#
:# Significantly improved routine :FindTclsh. #
:# Made the old option -t the default for this new script. #
:# Fixed the WM_SETTINGCHANGE broadcast. #
:# 2018-11-18 JFL Finished porting the PySetup.bat changes to TclSetup.bat. #
:# 2018-11-19 JFL Accept start commands with quotes, or without if valid. #
:# Accept start commands using copies of the default command.#
:# Added a verification that there's no additional command #
:# associated with the class. #
:# 2020-10-01 JFL Merged in Batch debug library updates. #
:# Use either setx.exe (preferred) or PowerShell to #
:# broadcast a WM_SETTINGCHANGE message in the end. #
:# Bug fix: Allow running as non-administrator, to be able #
:# to at least update local settings. #
:# 2020-11-03 JFL Fixed the PS call when there's a ' in the script path. #
:# 2023-03-06 JFL Fixed failure reading default registry keys in Windows fr.#
:# 2023-04-13 JFL Fixed :SysVar.Set NEED_BROADCAST update. #
:# #
:# © Copyright 2016 Hewlett Packard Enterprise Development LP #
:# Licensed under the Apache 2.0 license www.apache.org/licenses/LICENSE-2.0 #
:#----------------------------------------------------------------------------#
setlocal EnableExtensions EnableDelayedExpansion
set "VERSION=2023-03-06"
set "SCRIPT=%~nx0" &:# Script name
set "SPATH=%~dp0" & set "SPATH=!SPATH:~0,-1!" &:# Script path, without the trailing \
set "SFULL=%~f0" &:# Script full pathname
set ^"ARG0=%0^" &:# Script invokation name
set ^"ARGS=%*^" &:# Argument line
:# Mechanism for calling subroutines in a second instance of a script, from its main instance.
:# Done by (%XCALL% :label [arguments]), with XCALL defined in the Call module below.
if '%1'=='-call' !ARGS:~1!& exit /b
:# Initialize the most commonly used library components.
call :Library.Init
goto Main
:#----------------------------------------------------------------------------#
:# #
:# Function Library.Init #
:# #
:# Description Initialize the most commonly used library components #
:# #
:#----------------------------------------------------------------------------#
:Library.Init
:# Initialize this library modules definitions.
:# Each one depends on the preceding ones, so if you need one, you need all the preceding ones as well.
call :Call.Init &:# Function calls and argument extraction
call :Macro.Init &:# Inline macros generation
call :Debug.Init &:# Debug routines
call :Exec.Init &:# Conditional execution routines
call :Echo.Color.Init
set "ECHO-N=call :Echo-n"
:# FOREACHLINE macro. (Changes the delimiter to none to catch the whole lines.)
set FOREACHLINE=for /f "delims="
:# HOME variable. For analogy with Unix systems.
if not defined HOME set "HOME=%HOMEDRIVE%%HOMEPATH%"
goto :eof
:#----------------------------------------------------------------------------#
:# #
:# Module Call #
:# #
:# Description Manage function calls and argument extraction #
:# #
:# Functions PopArg Pop the first argument from %ARGS% into #
:# %ARG% and %"ARG"% #
:# PopSimpleArg Simpler and faster version, incompatible #
:# with ! or ^ characters in ARG values. #
:# Prep2ExpandVars Prepare variables to return from the #
:# local scope (with expansion on or off) #
:# to a parent scope with expansion on. #
:# PrepArgVars Prepare variables containing pathnames #
:# that will be passed as arguments. #
:# #
:# Macros %POPARG% Pop one argument using :PopArg #
:# %POPSARG% Pop one argument using :PopSimpleArg #
:# %LCALL% Call a routine in this library, either #
:# locally, or from an outside script. #
:# %XCALL% Call an outside script routine, from #
:# another instance of that outside script. #
:# %XCALL@% Idem, but with all args stored in one var.#
:# #
:# Variables %ARG% The unquoted argument #
:# %"ARG"% The actual argument, possibly quoted #
:# %ARGS% Remaining command line arguments #
:# #
:# %CR% An ASCII Carrier Return character '\x0D' #
:# %LF% An ASCII Line Feed character '\x0A' #
:# %BS% An ASCII Back Space character '\x08' #
:# %FF% An ASCII Form Feed character '\x0C' #
:# #
:# Notes PopArg works around the defect of the shift command, #
:# which pops the first argument from the %* list, but does #
:# not remove it from %*. #
:# Also works around another defect with tricky characters #
:# like ! or ^ being lost when variable expansion is on. #
:# #
:# Important: The performance of this routine is much better #
:# when invoked with variable expansion disabled. This is #
:# due to the complex processing done to avoid issues with #
:# tricky characters like ! or ^ when expansion is enabled. #
:# If you're sure that NONE of the arguments contain such #
:# tricky characters, then call :PopSimpleArg. #
:# #
:# Uses an inner call to make sure the argument parsing is #
:# done by the actual cmd.exe parser. This guaranties that #
:# arguments are split exactly as shift would have done. #
:# #
:# But call itself has a quirk, which requires a convoluted #
:# workaround to process the /? argument. #
:# #
:# Known limitation: Special character ^ is preserved within #
:# "quoted" arguments, but not within unquoted arguments. #
:# #
:# Known limitation: After using :PopArg, all consecutive #
:# argument separators in %ARGS% are replaced by one space. #
:# For example: "A==B" becomes "A B" #
:# This does not change the result of subsequent calls to #
:# :PopArg, but this prevents from using the tail itself as #
:# an argument. => Do not use :PopArg to get :Exec args! #
:# #
:# To do: Detect if the last arg has mismatched quotes, and #
:# if it does, append one. #
:# Right now such mismatched quotes will cause an error here.#
:# Do not work around this error to only pass back the bad #
:# argument, as this will only cause more errors further down#
:# #
:# History #
:# 2015-04-03 JFL Bug fix: Quoted args with an & inside failed to be poped. #
:# 2015-07-06 JFL Bug fix: Call quirk prevented inner call from popping /?. #
:# 2016-11-18 JFL Fixed popping arguments containing % characters. #
:# 2016-11-21 JFL Fixed popping quoted arguments containing &|<> characters.#
:# 2016-11-22 JFL Fixed popping arguments containing ^ characters. #
:# 2016-11-24 JFL Updated %POPARG% to work with trick characters ! and ^ in #
:# delayed expansion mode. The old and faster version is now #
:# called %POPSARG%. #
:# Added routine :Prep2ExpandVars allowing to pass any #
:# tricky string across call or endlocal barriers. #
:# 2016-12-01 JFL Added a %FF% Form Feed character variable. #
:# #
:#----------------------------------------------------------------------------#
call :Call.Init
goto Call.end
:Sub.Init # Create a SUB variable containing a SUB (Ctrl-Z) character
>NUL copy /y NUL + NUL /a "%TEMP%\1A.chr" /a
for /f %%c in (%TEMP%\1A.chr) do set "SUB=%%c"
exit /b
:Call.Init
if not defined LCALL set "LCALL=call" &:# Macro to call functions in this library
set "POPARG=%LCALL% :PopArg"
set "POPSARG=%LCALL% :PopSimpleArg"
:# Mechanism for calling subroutines in a second external instance of the top script.
set ^"XCALL=call "!SFULL!" -call^" &:# This is the full path to the top script's (or this lib's if called directly) ARG0
set ^"XCALL@=!XCALL! :CallVar^" &:# Indirect call, with the label and arguments in a variable
:# Define a LF variable containing a Line Feed ('\x0A')
set LF=^
%# The two blank lines here are necessary. #%
%# The two blank lines here are necessary. #%
:# Define a CR variable containing a Carriage Return ('\x0D')
for /f %%a in ('copy /Z %COMSPEC% nul') do set "CR=%%a"
:# Define a BS variable containing a BackSpace ('\x08')
:# Use prompt to store a backspace+space+backspace into a DEL variable.
for /F "tokens=1 delims=#" %%a in ('"prompt #$H# & echo on & for %%b in (1) do rem"') do set "DEL=%%a"
:# Then extract the first backspace
set "BS=%DEL:~0,1%"
:# Define a FF variable containing a Form Feed ('\x0C')
for /f %%A in ('cls') do set "FF=%%A"
:# Define variables for problematic characters, that cause parsing issues.
:# Use the ASCII control character name, or the html entity name.
:# Warning: The excl and hat characters need different quoting depending on context.
set "DEBUG.percnt=%%" &:# One percent sign
set "DEBUG.excl=^!" &:# One exclamation mark
set "DEBUG.hat=^" &:# One caret, aka. circumflex accent, or hat sign
set ^"DEBUG.quot="" &:# One double quote
set "DEBUG.apos='" &:# One apostrophe
set "DEBUG.amp=&" &:# One ampersand
set "DEBUG.vert=|" &:# One vertical bar
set "DEBUG.gt=>" &:# One greater than sign
set "DEBUG.lt=<" &:# One less than sign
set "DEBUG.lpar=(" &:# One left parenthesis
set "DEBUG.rpar=)" &:# One right parenthesis
set "DEBUG.lbrack=[" &:# One left bracket
set "DEBUG.rbrack=]" &:# One right bracket
set "DEBUG.sp= " &:# One space
set "DEBUG.tab= " &:# One tabulation
set "DEBUG.quest=?" &:# One question mark
set "DEBUG.ast=*" &:# One asterisk
set "DEBUG.cr=!CR!" &:# One carrier return
set "DEBUG.lf=!LF!" &:# One line feed
set "DEBUG.bs=!BS!" &:# One backspace
set "DEBUG.ff=!FF!" &:# One form feed
goto :eof
:PopArg
if "!!"=="" goto :PopArg.Eon
:PopArg.Eoff
:PopSimpleArg :# Will corrupt result if expansion is on and ARG contains ^ or ! characters.
:# Gotcha: The call parser first scans its command line for an unquoted /?.
:# If it finds one anywhere on the command line, then it ignores the target label and displays call help.
:# To work around that, we initialize %ARG% and %"ARG"% with an impossible combination of values.
set "ARG=Yes"
set ""ARG"=No"
set "PopArg.ARGS="
if defined ARGS (
setlocal EnableDelayedExpansion
for /f "delims=" %%a in ("!ARGS:%%=%%%%!") do endlocal & set ^"PopArg.ARGS=%%a^"
)
:# Note: The following call doubles ^ within "quotes", but not those outside of quotes.
:# So :PopArg.Helper will correctly record ^ within quotes, but miss those outside. (Unless quadrupled!)
:# The only way to fix this would be to completely rewrite :PopArg as a full fledged batch parser written in batch!
call :PopArg.Helper %PopArg.ARGS% >NUL 2>NUL &:# Output redirections ensure the call help is not actually output.
:# Finding that impossible combination now is proof that the call was not executed.
:# In this case, try again with the /? quoted, to prevent the call parser from processing it.
:# Note that we can not systematically do this /? enquoting, else it's "/?" that would break the call.
if "%ARG%"=="Yes" if [%"ARG"%]==[No] call :PopArg.Helper %PopArg.ARGS:/?="/?"%
set "PopArg.ARGS="
goto :eof
:PopArg.Helper
set "ARG=%~1" &:# Remove quotes from the argument
set ^""ARG"=%1^" &:# The same with quotes, if any, should we need them
if defined ARG set "ARG=%ARG:^^=^%"
if defined "ARG" set ^""ARG"=%"ARG":^^=^%^"
:# Rebuild the tail of the argument line, as shift does not do it
:# Never quote the set ARGS command, else some complex quoted strings break
set ARGS=%2
:PopArg.GetNext
shift
if defined ARGS set ^"ARGS=%ARGS:^^=^%^"
if [%2]==[] goto :eof
:# Leave quotes in the tail of the argument line
set ARGS=%ARGS% %2
goto :PopArg.GetNext
:PopArg.Eon
setlocal DisableDelayedExpansion
call :PopArg.Eoff
call :Prep2ExpandVars ARG ^""ARG"^" ARGS
setlocal EnableDelayedExpansion
for /f %%a in ("-!ARG!") do for /f %%b in ("-!"ARG"!") do for /f %%c in ("-!ARGS!") do (
endlocal
endlocal
set "ARG=%%a"
set "ARG=!ARG:~1!"
set ^""ARG"=%%b^"
set ^""ARG"=!"ARG":~1!^"
set ^"ARGS=%%c^"
set "ARGS=!ARGS:~1!"
)
goto :eof
:# Prepare variables to return from the local scope (with expansion on or off) to a parent scope with expansion on
:Prep2ExpandVars VAR [VAR ...]
if "!!"=="" goto :Prep2ExpandVars.Eon
:Prep2ExpandVars.Eoff :# The local scope has expansion off
setlocal EnableDelayedExpansion
set "VALUE=!%~1!"
call :Prep2ExpandVars.Eon VALUE
endlocal & set "%~1=%VALUE%"
if not [%2]==[] shift & goto :Prep2ExpandVars.Eoff
goto :eof
:# Prepare variables, assuming the local scope itself has expansion on
:Prep2ExpandVars.Eon VAR [VAR ...]
if defined %1 (
for %%e in (sp tab cr lf quot amp vert lt gt hat percnt) do ( :# Encode named character entities
for %%c in ("!DEBUG.%%e!") do (
set "%~1=!%~1:%%~c= DEBUG.%%e !"
)
)
call set "%~1=%%%~1:^!= DEBUG.excl %%" & rem :# Encode exclamation points
call set "%~1=%%%~1: =^!%%" & rem :# Encode final expandable entities
)
if not [%2]==[] shift & goto :Prep2ExpandVars.Eon
goto :eof
:# Prepare variables containing pathnames that will be passed as "arguments"
:PrepArgVars
set "%~1=!%~1:%%=%%%%!" &:# Escape percent signs
if not [%2]==[] shift & goto :PrepArgVars
goto :eof
:# Indirect call, with the label and arguments in a variable
:CallVar CMDVAR
call !%1:%%=%%%%!
exit /b
:Call.end
:#----------------------------------------------------------------------------#
:# #
:# Module Macro #
:# #
:# Description Tools for defining inline functions, #
:# also known as macros by analogy with Unix shells macros #
:# #
:# Macros %MACRO% Define the prolog code of a macro #
:# %/MACRO% Define the epilog code of a macro #
:# #
:# Variables %LF1% A Line Feed ASCII character '\x0A' #
:# %LF2% Generates a LF when expanded twice #
:# %LF3% Generates a LF when expanded 3 times #
:# Etc... #
:# %\n% Macro command line separator #
:# #
:# Notes The principle is to define a variable containing the #
:# complete body of a function, like this: #
:# set $macro=for %%$ in (1 2) do if %%$==2 ( %\n% #
:# :# Define the body of your macro here %\n% #
:# :# Then return the result to the caller %\n% #
:# for /f "delims=" %%r in ('echo.%!%RETVAL%!%') do ( %\n% #
:# endlocal %&% set "RETVAL=%%~r" %\n% #
:# ) %\n% #
:# ) else setlocal enableDelayedExpansion %&% set ARGS= #
:# #
:# It is then invoked just like an external command: #
:# %$macro% ARG1 ARG2 ... #
:# #
:# The ideas that make all this possible were published on #
:# the dostips.com web site, in multiple messages exchanged #
:# by community experts. #
:# By convention on the dostips.com web site, macro names #
:# begin by a $ character; And the %\n% variable ends lines. #
:# The other variables are mine. #
:# #
:# The use of a for loop executed twice, is critical for #
:# allowing to place arguments behind the macro. #
:# The first loop executes only the tail line, which defines #
:# the arguments; The second loop executes the main body of #
:# the macro, which processes the arguments, and returns the #
:# result(s). #
:# To improve the readability of macros, replace the code in #
:# the first line by %MACRO%, and the code in the last line #
:# by %/MACRO% #
:# #
:# The use of the Line Feed character as command separator #
:# within macros is a clever trick, that helps debugging, #
:# but it is not necessary for macros to work. #
:# This helps debugging, because this allows to output the #
:# macro definition as a structured string spanning several #
:# lines, looking exactly like a normal function with one #
:# instruction per line. #
:# But it would be equally possible to define macros using #
:# the documented & character as command separator. #
:# #
:# Limitations: #
:# - A macro cannot call another macro. #
:# (This would require escaping all control characters in #
:# the sub-macro, so that they survive an additional #
:# level of expansion.) #
:# #
:# History #
:# 2015-04-15 JFL Initial version, based on dostips.com samples, with #
:# changes so that they work with DelayedExpansion on. #
:# 2015-11-27 JFL Added a primitive macro debugging capability. #
:# #
:#----------------------------------------------------------------------------#
call :Macro.Init
goto :Macro.End
:Macro.Init
:# LF generator variables, that become an LF after N expansions
:# %LF1% == %LF% ; %LF2% == To expand twice ; %LF3% == To expand 3 times ; Etc
:# Starting with LF2, the right # of ^ doubles on every line,
:# and the left # of ^ is 3 times the right # of ^.
set ^"LF1=^%LF%%LF%"
set ^"LF2=^^^%LF1%%LF1%^%LF1%%LF1%"
set ^"LF3=^^^^^^%LF2%%LF2%^^%LF2%%LF2%"
set ^"LF4=^^^^^^^^^^^^%LF3%%LF3%^^^^%LF3%%LF3%"
set ^"LF5=^^^^^^^^^^^^^^^^^^^^^^^^%LF4%%LF4%^^^^^^^^%LF4%%LF4%"
:# Variables for use in inline macro functions
set ^"\n=%LF3%^^^" &:# Insert a LF and continue macro on next line
set "^!=^^^^^^^!" &:# Define a %!%DelayedExpansion%!% variable
set "'^!=^^^!" &:# Idem, but inside a quoted string
set ">=^^^>" &:# Insert a redirection character
set "<=^^^<" &:# Insert a redirection character
set "&=^^^&" &:# Insert a command separator in a macro
:# Idem, to be expanded twice, for use in macros within macros
set "^!2=^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^!"
set "'^!2=^^^^^^^!"
set "&2=^^^^^^^^^^^^^^^&"
set "MACRO=for %%$ in (1 2) do if %%$==2" &:# Prolog code of a macro
set "/MACRO=else setlocal enableDelayedExpansion %&% set MACRO.ARGS=" &:# Epilog code of a macro
set "ENDMACRO=endlocal" &:# Ends the macro local scope started in /MACRO. Necessary before macro exit.
set "ON_MACRO_EXIT=for /f "delims=" %%r in ('echo" &:# Begin the return variables definitions
set "/ON_MACRO_EXIT=') do %ENDMACRO% %&% %%r" &:# End the return variables definitions
:# Primitive macro debugging definitions
:# Macros, usable anywhere, including within other macros, for conditionally displaying debug information
:# Use option -xd to set a > 0 macro debugging level.
:# Usage: %IF_XDLEVEL% N command
:# Runs command if the current macro debugging level is at least N.
:# Ex: %IF_XDLEVEL% 2 set VARIABLE
:# Recommended: Use set, instead of echo, to display variable values. This is sometimes
:# annoying because this displays other unwanted variables. But this is the only way
:# to be sure to display _all_ tricky characters correctly in any expansion mode.
:# Note: These debugging macros slow down a lot their enclosing macro.
:# They should be removed from the released code.
set "XDLEVEL=0" &:# 0=No macro debug; 1=medium debug; 2=full debug; 3=Even more debug
set "IF_XDLEVEL=for /f %%' in ('call echo.%%XDLEVEL%%') do if %%' GEQ"
goto :eof
:Macro.end
:#----------------------------------------------------------------------------#
:# #
:# Module Debug #
:# #
:# Description A collection of debug routines #
:# #
:# Functions Debug.Init Initialize debugging. Call once at first. #
:# Debug.Off Disable the debugging mode #
:# Debug.On Enable the debugging mode #
:# Debug.SetLog Set the log file #
:# Debug.Entry Log entry into a routine #
:# Debug.Return Log exit from a routine #
:# Verbose.Off Disable the verbose mode #
:# Verbose.On Enable the verbose mode #
:# #
:# Echo Echo and log strings, indented #
:# EchoVars Display a set of variables name=value #
:# EchoStringVars Display a string, then a set of variables #
:# EchoArgs Display all arguments name=value #
:# EchoVals Display the value of multiple variables #
:# All functions in that series have two other derivatives, #
:# with the .debug and .verbose suffix. Ex: Echo.Debug #
:# These display only in debug and verbose mode respectively,#
:# but always log the string (if a log file is defined). #
:# #
:# Macros %FUNCTION% Define and trace the entry in a function. #
:# %UPVAR% Declare a var. to pass back to the caller.#
:# %RETURN% Return from a function and trace it #
:# #
:# Always match uses of %FUNCTION% and %RETURN%. That is #
:# never use %RETURN% if there was no %FUNCTION% before it. #
:# #
:# :# Example of a factorial routine using this framework #
:# :Fact #
:# %FUNCTION% enableextensions enabledelayedexpansion #
:# %UPVAR% RETVAL #
:# set N=%1 #
:# if .%N%.==.0. ( #
:# set RETVAL=1 #
:# ) else ( #
:# set /A M=N-1 #
:# call :Fact !M! #
:# set /A RETVAL=N*RETVAL #
:# ) #
:# %RETURN% #
:# #
:# %ECHO% Echo and log a string, indented #
:# %LOG% Log a string, indented #
:# %ECHO.V% Idem, but display it in verbose mode only #
:# %ECHO.D% Idem, but display it in debug mode only #
:# #
:# %ECHOVARS% Indent, echo and log variables values #
:# %ECHOVARS.V% Idem, but display them in verb. mode only #
:# %ECHOVARS.D% Idem, but display them in debug mode only #
:# #
:# %ECHOSVARS% Echo ARG1 before each variable. #
:# %ECHOSVARS.V% Idem, but display them in verb. mode only #
:# %ECHOSVARS.D% Idem, but display them in debug mode only #
:# #
:# %ECHOVALS% Echo the value of multiple variables #
:# %ECHOVALS.V% Idem, but display them in verb. mode only #
:# %ECHOVALS.D% Idem, but display them in debug mode only #
:# #
:# %ECHOSTRINGS% Echo the value of multiple quoted strings #
:# %ECHOSTRINGS.V% Idem, but display them in verb. mode only #
:# %ECHOSTRINGS.D% Idem, but display them in debug mode only #
:# #
:# %IF_DEBUG% Execute a command in debug mode only #
:# %IF_VERBOSE% Execute a command in verbose mode only #
:# #
:# %FUNCTION0% Weak functions with no local variables. #
:# %RETURN0% Return from a %FUNCTION0% and trace it #
:# %RETURN#% Idem, with comments after the return #
:# #
:# %+INDENT% Manually increase the debug INDENT #
:# %-INDENT% Manually decrease the debug INDENT #
:# #
:# Variables %>DEBUGOUT% Debug output redirect. Either "" or ">&2".#
:# %LOGFILE% Log file name. Inherited. Default=""==NUL #
:# Always set using call :Debug.SetLog #
:# %DEBUG% Debug mode. 0=Off; 1=On. Use functions #
:# Debug.Off and Debug.On to change it. #
:# Inherited. Default=0. #
:# %VERBOSE% Verbose mode. 0=Off; 1=On. Use functions #
:# Verbose.Off and Verbose.On to change it. #
:# Inherited. Default=0. #
:# %INDENT% Spaces to put ahead of all debug output. #
:# Inherited. Default=. (empty string) #
:# #
:# Notes All output from these routines is sent to the log file. #
:# The debug output is sent stdout or stderr, depending on #
:# variable %>DEBUGOUT%. #
:# #
:# Traced functions are indented, based on the call depth. #
:# Use %ECHO% to get the same indentation of normal output. #
:# #
:# The output format matches the batch language syntax #
:# exactly. This allows copying the debug output directly #
:# into another command window, to check troublesome code. #
:# #
:# History #
:# 2011-11-15 JFL Split Debug.Init from Debug.Off, to improve clarity. #
:# 2011-12-12 JFL Output debug information to stderr, so that stdout can be #
:# used for returning information from the subroutine. #
:# 2011-12-13 JFL Standardize use of RETVAR/RETVAL, and display it on return.
:# 2012-07-09 JFL Restructured functions to a more "object-like" style. #
:# Added the three flavors of the Echo and EchoVars routines.#
:# 2012-07-19 JFL Added optimizations to improve performance in non-debug #
:# and non-verbose mode. Added routine Debug.SetLog. #
:# 2012-11-13 JFL Added macro LOG. Fixed setlocal bug in :EchoVars. #
:# 2013-08-27 JFL Changed %RETURN% to do exit /b. This allows returning #
:# an errorlevel by doing: %RETURN% %ERRORLEVEL% #
:# 2013-11-12 JFL Added macros %IF_DEBUG% and %IF_VERBOSE%. #
:# 2013-12-04 JFL Added variable %>DEBUGOUT% to allow sending debug output #
:# either to stdout or to stderr. #
:# 2015-10-29 JFL Added macro %RETURN#% to return with a comment. #
:# 2015-11-19 JFL %FUNCTION% now automatically generates its name & %* args.#
:# (Simplifies usage, but comes at a cost of about a 5% slow #
:# down when running in debug mode.) #
:# Added an %UPVAR% macro allowing to define the list of #
:# variables that need to make it back to the caller. #
:# %RETURN% (Actually the Debug.return routine) now handles #
:# this variable back propagation using the (goto) trick. #
:# This works well, but the performance is poor. #
:# 2015-11-25 JFL Rewrote the %FUNCTION% and %RETURN% macros to manage #
:# most common cases without calling a subroutine. This #
:# resolves the performance issues of the previous version. #
:# 2015-11-27 JFL Redesigned the problematic character return mechanism #
:# using a table of predefined generic entities. Includes #
:# support for returning strings with CR & LF. #
:# 2015-11-29 JFL Streamlined the macros and added lots of comments. #
:# The FUNCTION macro now runs with expansion enabled, then #
:# does a second setlocal in the end as requested. #
:# The RETURN macro now displays strings in debug mode with #
:# delayed expansion enabled. This fixes issues with CR & LF.#
:# Added a backspace entity. #
:# 2015-12-01 JFL Bug fix: %FUNCTION% with no arg did change the exp. mode. #
:# 2016-09-01 JFL Bug fix: %RETURN% incorrectly returned empty variables. #
:# 2016-11-02 JFL Bug fix: Avoid log file redirection failures in recursive #
:# scripts. #
:# 2016-11-13 JFL Bug fix: Correctly return special characters & | < > ? * #
:# 2016-11-24 JFL Fixed tracing %FUNCTION% arguments with ^ and % chars. #
:# #
:#----------------------------------------------------------------------------#
call :Debug.Init
goto :Debug.End
:Debug.Init
:# Preliminary checks to catch common problems
if exist echo >&2 echo WARNING: The file "echo" in the current directory will cause problems. Please delete it and retry.
:# Inherited variables from the caller: DEBUG, VERBOSE, INDENT, >DEBUGOUT
:# Initialize other debug variables
set "ECHO=%LCALL% :Echo"
set "ECHOVARS=%LCALL% :EchoVars"
set "ECHOSVARS=%LCALL% :EchoStringVars"
:# The FUNCTION, UPVAR, and RETURN macros should work with delayed expansion on or off
set MACRO.GETEXP=(if "%'!2%%'!2%"=="" (set MACRO.EXP=EnableDelayedExpansion) else set MACRO.EXP=DisableDelayedExpansion)
set UPVAR=call set DEBUG.RETVARS=%%DEBUG.RETVARS%%
set RETURN=call set "DEBUG.ERRORLEVEL=%%ERRORLEVEL%%" %&% %MACRO% ( %\n%
set DEBUG.EXITCODE=%!%MACRO.ARGS%!%%\n%
if defined DEBUG.EXITCODE set DEBUG.EXITCODE=%!%DEBUG.EXITCODE: =%!%%\n%
if not defined DEBUG.EXITCODE set DEBUG.EXITCODE=%!%DEBUG.ERRORLEVEL%!%%\n%
for %%l in ("%'!%LF%'!%") do ( %# Make it easy to insert line-feeds in any mode #% %\n%
set "DEBUG.SETARGS=""" %# The initial "" makes sure that for loops below never get an empty arg list #% %\n%
for %%v in (%!%DEBUG.RETVARS%!%) do ( %\n%
set "DEBUG.VALUE=%'!%%%v%'!%" %# We must remove problematic characters in that value #% %\n%
if defined DEBUG.VALUE ( %# Else the following lines will generate phantom characters #% %\n%
set "DEBUG.VALUE=%'!%DEBUG.VALUE:%%=%%DEBUG.percnt%%%'!%" %# Encode percent #% %\n%
for %%e in (sp tab cr lf quot amp vert lt gt) do for %%c in ("%'!%DEBUG.%%e%'!%") do ( %# Encode named character entities #% %\n%
set "DEBUG.VALUE=%'!%DEBUG.VALUE:%%~c=%%DEBUG.%%e%%%'!%" %\n%
) %\n%
set "DEBUG.VALUE=%'!%DEBUG.VALUE:^^=%%DEBUG.hat%%%'!%" %# Encode carets #% %\n%
call set "DEBUG.VALUE=%%DEBUG.VALUE:%!%=^^^^%%" %# Encode exclamation points #% %\n%
set "DEBUG.VALUE=%'!%DEBUG.VALUE:^^^^=%%DEBUG.excl%%%'!%" %# Encode exclamation points #% %\n%
) %\n%
set DEBUG.SETARGS=%!%DEBUG.SETARGS%!% "%%v=%'!%DEBUG.VALUE%'!%"%\n%
) %\n%
if %!%DEBUG%!%==1 ( %# Build the debug message and display it #% %\n%
set "DEBUG.MSG=return %'!%DEBUG.EXITCODE%'!%" %\n%
for /f "delims=" %%v in ("%'!%DEBUG.SETARGS: =%%~l%'!%") do if not %%v=="" ( %# for /f avoids issues with ? and * #% %\n%
set "DEBUG.MSG=%'!%DEBUG.MSG%'!% %%DEBUG.amp%% set %%v" %!% %\n%
) %\n%
call set "DEBUG.MSG=%'!%DEBUG.MSG:%%=%%DEBUG.excl%%%'!%" %# Change all percent to ! #% %\n%
if defined ^^%>%DEBUGOUT ( %# If we use a debugging stream distinct from stdout #% %\n%
%LCALL% :Echo.Eval2DebugOut DEBUG.MSG %# Use a helper routine, as delayed redirection does not work #% %\n%
) else ( %# Output directly here, which is faster #% %\n%
for /f "delims=" %%c in ("%'!%INDENT%'!%%'!%DEBUG.MSG%'!%") do echo %%c%# Use a for loop to do a double !variable! expansion #%%\n%
) %\n%
if defined LOGFILE ( %# If we have to send a copy to a log file #% %\n%
%LCALL% :Echo.Eval2LogFile DEBUG.MSG %# Use a helper routine, as delayed redirection does not work #% %\n%
) %\n%
) %\n%
for %%r in (%!%DEBUG.EXITCODE%!%) do ( %# Carry the return values through the endlocal barriers #% %\n%
for /f "delims=" %%a in ("%'!%DEBUG.SETARGS%'!%") do ( %\n%
endlocal %&% endlocal %&% endlocal %# Exit the RETURN and FUNCTION local scopes #% %\n%
set "DEBUG.SETARGS=%%a" %\n%
if "%'!%%'!%"=="" ( %# Delayed expansion is ON #% %\n%
call set "DEBUG.SETARGS=%'!%DEBUG.SETARGS:%%=%%DEBUG.excl%%%'!%" %# Change all percent to ! #% %\n%
for /f "delims=" %%v in ("%'!%DEBUG.SETARGS: =%%~l%'!%") do if not %%v=="" ( %# for /f avoids issues with ? and * #% %\n%
set %%v %# Set each upvar variable in the caller's scope #% %\n%
) %\n%
) else ( %# Delayed expansion is OFF #% %\n%
setlocal EnableDelayedExpansion %\n%
for /f "delims=" %%v in ("%'!%DEBUG.SETARGS: =%%~l%'!%") do if %%v=="" ( %# for /f avoids issues with ? and * #% %\n%
endlocal %\n%
) else ( %\n%
call set %%v %# Set each upvar variable in the caller's scope #% %\n%
) %\n%
) %\n%
set "DEBUG.SETARGS=" %\n%
exit /b %%r %# Return to the caller #% %\n%
) %\n%
) %\n%
) %\n%
) %/MACRO%
:Debug.Init.2
set "LOG=%LCALL% :Echo.Log"
set ">>LOGFILE=>>%LOGFILE%"
if not defined LOGFILE set "LOG=rem" & set ">>LOGFILE=rem"
if .%LOGFILE%.==.NUL. set "LOG=rem" & set ">>LOGFILE=rem"
if .%NOREDIR%.==.1. set "LOG=rem" & set ">>LOGFILE=rem" &:# A parent script is already redirecting output. Trying to do it again here would fail.
set "ECHO.V=%LCALL% :Echo.Verbose"
set "ECHO.D=%LCALL% :Echo.Debug"
set "ECHOVARS.V=%LCALL% :EchoVars.Verbose"
set "ECHOVARS.D=%LCALL% :EchoVars.Debug"
set "ECHOSVARS.V=%LCALL% :EchoStringVars.Verbose"
set "ECHOSVARS.D=%LCALL% :EchoStringVars.Debug"
set "ECHOVALS=%LCALL% :EchoVals"
set "ECHOVALS.V=%LCALL% :EchoVals.Verbose"
set "ECHOVALS.D=%LCALL% :EchoVals.Debug"
set "ECHOSTRINGS=%LCALL% :EchoStrings"
set "ECHOSTRINGS.V=%LCALL% :EchoStrings.Verbose"
set "ECHOSTRINGS.D=%LCALL% :EchoStrings.Debug"
set "+INDENT=%LCALL% :Debug.IncIndent"
set "-INDENT=%LCALL% :Debug.DecIndent"
set ">MSGOUT.V[0]=rem"
set ">MSGOUT.V[1]="
set ">MSGOUT.D[0]=rem"
set ">MSGOUT.D[1]=%>DEBUGOUT%"
:# Variables inherited from the caller...
:# Preserve INDENT if it contains just spaces, else clear it.
for /f %%s in ('echo.%INDENT%') do set "INDENT="
:# Preserve the log file name, else by default use NUL.
:# if not defined LOGFILE set "LOGFILE=NUL"
:# VERBOSE mode can only be 0 or 1. Default is 0.
if not .%VERBOSE%.==.1. set "VERBOSE=0"
call :Verbose.%VERBOSE%
:# DEBUG mode can only be 0 or 1. Default is 0.
if not .%DEBUG%.==.1. set "DEBUG=0"
goto :Debug.%DEBUG%
:Debug.SetLog
set "LOGFILE=%~1"
goto :Debug.Init.2
:Debug.Off
:Debug.0
set "DEBUG=0"
set "DEBUG.ENTRY=rem"
set "IF_DEBUG=if .%DEBUG%.==.1."
set "FUNCTION0=rem"
set FUNCTION=%MACRO.GETEXP% %&% %MACRO% ( %\n%
call set "FUNCTION.NAME=%%0" %\n%
call set ARGS=%%*%# Do not quote this, to keep string/non string alternance #%%\n%
if defined ARGS set ARGS=%!%ARGS:^^^^^^^^^^^^^^^^=^^^^^^^^%!%%# ^carets are doubled in quoted strings, halved outside. => Quadruple them if using unquoted ones #%%\n%
set "DEBUG.RETVARS=" %\n%
if not defined MACRO.ARGS set "MACRO.ARGS=%'!%MACRO.EXP%'!%" %\n%
setlocal %!%MACRO.ARGS%!% %\n%
) %/MACRO%
set "RETURN0=exit /b"
set "RETURN#=exit /b & rem"
set "EXEC.ARGS= %EXEC.ARGS%"
set "EXEC.ARGS=%EXEC.ARGS: -d=%"
set "EXEC.ARGS=%EXEC.ARGS:~1%"
:# Optimization to speed things up in non-debug mode
if not defined LOGFILE set "ECHO.D=echo >NUL"
if .%LOGFILE%.==.NUL. set "ECHO.D=echo >NUL"
if not defined LOGFILE set "ECHOVARS.D=echo >NUL"
if .%LOGFILE%.==.NUL. set "ECHOVARS.D=echo >NUL"
goto :eof
:Debug.On
:Debug.1
set "DEBUG=1"
set "DEBUG.ENTRY=:Debug.Entry"
set "IF_DEBUG=if .%DEBUG%.==.1."
set "FUNCTION0=call %LCALL% :Debug.Entry0 %%0 %%*"
set FUNCTION=%MACRO.GETEXP% %&% %MACRO% ( %\n%
call set "FUNCTION.NAME=%%0" %\n%
call set ARGS=%%*%# Do not quote this, to keep string/non string aternance #%%\n%
if defined ARGS set ARGS=%!%ARGS:^^^^^^^^^^^^^^^^=^^^^^^^^%!%%# ^carets are doubled in quoted strings, halved outside. => Quadruple them if using unquoted ones #%%\n%
if %!%DEBUG%!%==1 ( %# Build the debug message and display it #% %\n%
set DEBUG.MSG=call %!%FUNCTION.NAME%!% %!%ARGS%!%%\n%
if defined ^^%>%DEBUGOUT ( %# If we use a debugging stream distinct from stdout #% %\n%
%LCALL% :Echo.2DebugOut DEBUG.MSG %# Use a helper routine, as delayed redirection does not work #% %\n%
) else ( %# Output directly here, which is faster #% %\n%
echo%!%INDENT%!% %!%DEBUG.MSG%!%%\n%
) %\n%
if defined LOGFILE ( %# If we have to send a copy to a log file #% %\n%
%LCALL% :Echo.2LogFile DEBUG.MSG %# Use a helper routine, as delayed redirection does not work #% %\n%
) %\n%
call set "INDENT=%'!%INDENT%'!% " %\n%
) %\n%
set "DEBUG.RETVARS=" %\n%
if not defined MACRO.ARGS set "MACRO.ARGS=%'!%MACRO.EXP%'!%" %\n%
setlocal %!%MACRO.ARGS%!% %\n%
) %/MACRO%
set "RETURN0=call %LCALL% :Debug.Return0 %%ERRORLEVEL%% & exit /b"
:# Macro for displaying comments on the return log line
set RETURN#=call set "RETURN.ERR=%%ERRORLEVEL%%" %&% %MACRO% ( %\n%
%LCALL% :Debug.Return# %# Redirections can't work in macro. Do it in a function. #% %\n%
for %%r in (%!%RETURN.ERR%!%) do %ENDMACRO% %&% set "RETURN.ERR=" %&% call set "INDENT=%%INDENT:~2%%" %&% exit /b %%r %\n%
) %/MACRO%
set "EXEC.ARGS= %EXEC.ARGS%"
set "EXEC.ARGS=%EXEC.ARGS: -d=% -d"
set "EXEC.ARGS=%EXEC.ARGS:~1%"
:# Reverse the above optimization
set "ECHO.D=%LCALL% :Echo.Debug"
set "ECHOVARS.D=%LCALL% :EchoVars.Debug"
goto :eof
:Debug.Entry0
setlocal DisableDelayedExpansion
%>DEBUGOUT% echo %INDENT%call %*
if defined LOGFILE %>>LOGFILE% echo %INDENT%call %*
endlocal
:Debug.IncIndent
set "INDENT=%INDENT% "
goto :eof
:Debug.Entry
setlocal DisableDelayedExpansion
%>DEBUGOUT% echo %INDENT%call %FUNCTION.NAME% %ARGS%
if defined LOGFILE %>>LOGFILE% echo %INDENT%call %FUNCTION.NAME% %ARGS%
endlocal
set "INDENT=%INDENT% "
goto :eof
:Debug.Return0 %1=Exit code
%>DEBUGOUT% echo %INDENT%return %1
if defined LOGFILE %>>LOGFILE% echo %INDENT%return %1
:Debug.DecIndent
if defined INDENT set "INDENT=%INDENT:~2%"
exit /b %1
:Debug.Return# :# %RETURN.ERR% %MACRO.ARGS%
setlocal DisableDelayedExpansion
%>DEBUGOUT% echo %INDENT%return %RETURN.ERR% ^&:#%MACRO.ARGS%
if defined LOGFILE %>>LOGFILE% echo %INDENT%return %RETURN.ERR% ^&:#%MACRO.ARGS%
endlocal
goto :eof &:# %RETURN.ERR% will be processed in the %DEBUG#% macro.
:# A lightweight alternative for the %RETURN% macro.
:# Only traces the %ERRORLEVEL%, but not the variables returned.
:# Trace the return from a subroutine, and do the actual return, in a single call
:Return
setlocal
set "ERR=%~1"
if not defined ERR set "ERR=%ERRORLEVEL%"
%>DEBUGOUT% echo exit /b %ERR%
2>NUL (goto) & exit /b %ERR% &:# Endlocal and pop one call stack, then return to the upper level
:# Routine to set the VERBOSE mode, in response to the -v argument.
:Verbose.Off
:Verbose.0
set "VERBOSE=0"
set "IF_VERBOSE=if .%VERBOSE%.==.1."
set "EXEC.ARGS= %EXEC.ARGS%"
set "EXEC.ARGS=%EXEC.ARGS: -v=%"
set "EXEC.ARGS=%EXEC.ARGS:~1%"
:# Optimization to speed things up in non-verbose mode
if not defined LOGFILE set "ECHO.V=echo >NUL"
if .%LOGFILE%.==.NUL. set "ECHO.V=echo >NUL"
if not defined LOGFILE set "ECHOVARS.V=echo >NUL"
if .%LOGFILE%.==.NUL. set "ECHOVARS.V=echo >NUL"
goto :eof
:Verbose.On
:Verbose.1
set "VERBOSE=1"
set "IF_VERBOSE=if .%VERBOSE%.==.1."
set "EXEC.ARGS= %EXEC.ARGS%"
set "EXEC.ARGS=%EXEC.ARGS: -v=% -v"
set "EXEC.ARGS=%EXEC.ARGS:~1%"
:# Reverse the above optimization
set "ECHO.V=%LCALL% :Echo.Verbose"
set "ECHOVARS.V=%LCALL% :EchoVars.Verbose"
goto :eof
:# Echo and log a string, indented at the same level as the debug output.
:Echo
echo.%INDENT%%*
:Echo.Log
if defined LOGFILE %>>LOGFILE% echo.%INDENT%%*
goto :eof
:Echo.Verbose
:Echo.V
%IF_VERBOSE% goto :Echo
goto :Echo.Log
:Echo.Debug
:Echo.D
%IF_DEBUG% %>DEBUGOUT% echo.%INDENT%%*
goto :Echo.Log
:Echo.Eval2DebugOut %1=Name of string, with !variables! that need to be evaluated first
setlocal EnableDelayedExpansion &:# Make sure that !variables! get expanded
set "STRING=!%1!" &:# !variables! not yet expanded; They will be on next line
%>DEBUGOUT% echo.%INDENT%%STRING%
goto :eof
:Echo.2DebugOut %1=Name of string to output to the DEBUGOUT stream
setlocal EnableDelayedExpansion &:# Make sure that !variables! get expanded
%>DEBUGOUT% echo.%INDENT%!%1!
goto :eof
:Echo.Eval2LogFile %1=Name of string, with variables that need to be evaluated first
setlocal EnableDelayedExpansion &:# Make sure that !variables! get expanded
set "STRING=!%1!" &:# !variables! not yet expanded; They will be on next line
%>>LOGFILE% echo.%INDENT%%STRING%
goto :eof
:Echo.2LogFile %1=Name of string to output to the LOGFILE
setlocal EnableDelayedExpansion &:# Make sure that !variables! get expanded
%>>LOGFILE% echo.%INDENT%!%1!
goto :eof
:# Echo and log variable values, indented at the same level as the debug output.
:EchoStringVars %1=string %2=VARNAME %3=VARNAME ...
setlocal EnableExtensions EnableDelayedExpansion
set "INDENT=%INDENT%%~1 "
shift
goto :EchoVars.loop
:EchoVars %1=VARNAME %2=VARNAME %3=VARNAME ...
setlocal EnableExtensions EnableDelayedExpansion
:EchoVars.loop
if "%~1"=="" endlocal & goto :eof
%>DEBUGOUT% echo %INDENT%set "%~1=!%~1!"
if defined LOGFILE %>>LOGFILE% echo %INDENT%set "%~1=!%~1!"
shift
goto EchoVars.loop
:EchoVars.Verbose
%IF_VERBOSE% (
call :EchoVars %*
) else ( :# Make sure the variables are logged
call :EchoVars %* >NUL 2>NUL
)
goto :eof
:EchoVars.Debug
%IF_DEBUG% (
call :EchoVars %*
) else ( :# Make sure the variables are logged
call :EchoVars %* >NUL 2>NUL
)
goto :eof
:EchoStringVars.Verbose
%IF_VERBOSE% (
call :EchoStringVars %*
) else ( :# Make sure the variables are logged
call :EchoStringVars %* >NUL 2>NUL
)
goto :eof
:EchoStringVars.Debug
%IF_DEBUG% (
call :EchoStringVars %*
) else ( :# Make sure the variables are logged
call :EchoStringVars %* >NUL 2>NUL
)
goto :eof
:# Echo a list of arguments.
:EchoArgs
setlocal EnableExtensions DisableDelayedExpansion
set N=0
:EchoArgs.loop
if .%1.==.. endlocal & goto :eof
set /a N=N+1
%>DEBUGOUT% echo %INDENT%set "ARG%N%=%1"
shift
goto EchoArgs.loop
:# Echo the value of multiple variables on the same line
:EchoVals %1=VARNAME, %2=VARNAME, ...
setlocal EnableDelayedExpansion
set ">MSGOUT="
:EchoVals.1
set "EchoVals.LINE=" &:# Use a qualified name, in case the caller passes a variable called LINE
for %%v in (%*) do set "EchoVals.LINE=!EchoVals.LINE! !%%v!"
if not defined EchoVals.LINE set "EchoVals.LINE= " &:# Make sure there's a head space even if the variable list was empty
%>MSGOUT% echo.%INDENT%!EchoVals.LINE:~1!
if defined LOGFILE %>>LOGFILE% echo.%INDENT%!EchoVals.LINE:~1!
endlocal & exit /b
:EchoVals.Verbose
setlocal EnableDelayedExpansion
set ">MSGOUT=!>MSGOUT.V[%VERBOSE%]!"
goto :EchoVals.1
:EchoVals.Debug
setlocal EnableDelayedExpansion
set ">MSGOUT=!>MSGOUT.D[%DEBUG%]!"
goto :EchoVals.1
:# Echo the value of multiple strings on the same line. They must not contain double quotes.
:EchoStrings %1=Quoted_String, %2=Quoted_String, ...
setlocal DisableDelayedExpansion
set ">MSGOUT="
:EchoStrings.1
set "LINE=" &:# No need for a qualified name, since we don't use caller variables
for %%v in (%*) do set "LINE=%LINE% %%~v"
if not defined LINE set "LINE= " &:# Make sure there's a head space even if the string list was empty
%>MSGOUT% echo.%INDENT%%LINE:~1%
if defined LOGFILE %>>LOGFILE% echo.%INDENT%%LINE:~1%
endlocal & exit /b
:EchoStrings.Verbose
setlocal DisableDelayedExpansion
%IF_VERBOSE% (
set ">MSGOUT="
) else ( :# Make sure the variables are logged
set ">MSGOUT=rem"
)
goto :EchoStrings.1
:EchoString1.Debug
setlocal DisableDelayedExpansion
%IF_DEBUG% (
set ">MSGOUT=%>DEBUGOUT%"
) else ( :# Make sure the variables are logged
set ">MSGOUT=rem"
)
goto :EchoStrings.1
:Debug.End
:#----------------------------------------------------------------------------#
:# #
:# Module Exec #
:# #
:# Description Run a command, logging its output to the log file. #
:# #
:# In VERBOSE mode, display the command line first. #
:# In DEBUG mode, display the command line and the exit code.#
:# In NOEXEC mode, display the command line, but don't run it.