-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck_library.cmd
More file actions
278 lines (230 loc) · 7.22 KB
/
check_library.cmd
File metadata and controls
278 lines (230 loc) · 7.22 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
@echo off
FOR /F "tokens=3 delims=:" %%L in ("%~0") DO goto :%%L
echo STARTED %*
setlocal DisableDelayedExpansion
REM *** Options
call :options %*
if defined _store (
break > "%temp%\%~n0.tmp"
)
REM ***
set /a step=0
REM *** Check 4 different invokation
echo -------- Checking %library_name% ---------------
if defined _check_syntax (
call :check_syntax
)
if "%_only_case:1%=1%" == "1" (
call :testLoadLib DisableDelayedExpansion simple
)
if "%_only_case:2%=2%" == "2" (
call :testLoadLib EnableDelayedExpansion simple
)
if "%_only_case:3%=3%" == "3" (
call :testLoadLib DisableDelayedExpansion all-for-variables
)
if "%_only_case:4%=4%" == "4" (
call :testLoadLib EnableDelayedExpansion all-for-variables
)
if not defined _only_case (
if defined _store (
call :check_macro_mode_diff
)
)
:finish
echo -------- Tests finished -------------------
exit /b
:testLoadLib
set /a step+=1
echo #%step% Load library %library_name% with %1 Type: %2
if "%1"=="DisableDelayedExpansion" (
set "check_lib_mode=DDE"
) ELSE (
set "check_lib_mode=EDE"
)
set "check_lib_type=%2"
IF "%2" == "simple" (
setlocal %1
call %%library_name%%
) ELSE IF "%2" == "all-for-variables" (
call :for-variables %1
) ELSE (
echo ERROR: Unkonw mode "%2"
)
endlocal
exit /b
:for-variables
copy /z "%~f0" nul > out.tmp
for /F "delims= " %%C in (out.tmp) do set "$CR=%%C"
for /F "delims=#" %%a in ('prompt #$E# ^& for %%a in ^(1^) do rem') do set "ESC=%%a"
FOR /F "tokens=1-24" %%a in ("#####-a #####-b #####-c #####-d #####-e #####-f #####-g ####-h #####-i #####-j #####-k #####-l ####-m ####-n ####-o #####-p #####-q #####-r ####-s ####-t ####-u #####-v ######-w ####-x #####-y #####-z") DO ^
FOR /F "tokens=1-24" %%A IN ("#####-A #####-B #####-C #####-D #####-E #####-F #####-G ####-H #####-I #####-J #####-K #####-L ####-M ####-N ####-O #####-P #####-Q #####-R ####-S ####-T ####-U #####-V ######-W ####-X #####-Y #####-Z") DO ^
FOR /F "tokens=1-6" %%: in ("#####-: #####-; #####-< #####-= #####-> #####-? #####-@") DO ^
FOR /F "tokens=1-20" %%! in ("#####EXC #####QUOT #####-# #####-$ #####-PERCENT #####-& #####-' #####-( #####-) #####-* #####-+ #####-, #####-- #####-. #####-/") DO ^
FOR /F "tokens=1-10" %%0 in ("#####0 #####1 #####2 #####3 #####4 #####5 #####6 #####7 #####8 #####9") DO ^
FOR %%^" in (quote-quote#################) DO ^
FOR %%Z in (zzzzzzzzzzzz) DO ^
FOR /F "tokens=1-6" %%%ESC% in ("####-0x1B ####-0x1C ####-0x1D ####-0x1E ####-0x1F ####-SPACE") DO ^
FOR %%$ in (DOLLAR) DO (
call :_step2
)
exit /b
:_step2
setlocal %1
call %%library_name%%
exit /b
:check_syntax
REM *** Loads a macro with echo on, this shouldn't output anything
REM *** Then the macro is
set /a step+=1
echo #%step% Syntax check for library %library_name%
echo Check for errors in macro definitions, and for missing new lines/trailing white spaces/unbalanced quotes
setlocal
set "$FOR=%%%%~$=_F1_FOR-variable_DOLLAR_is_required_while_MACRO_DEFINITION_=:$param"
set "$PERCENT=%%%%~$=_p1_FOR-variable_DOLLAR_is_required_while_MACRO_DEFINITION_=:$"
set "$PERCENT-CALL=%%%%~$==:$%$PERCENT%~$==:$"
set "$$LIBRARY_NEW_MACRO_PRE_HOOK=(call echo #MACRO-DEF-START# %$FOR:param=Z% in "%$PERCENT-CALL%~n0") ^& echo on"
set "$$LIBRARY_NEW_MACRO_POST_HOOK=(echo #MACRO-DEF-END# %$FOR:param=Z%) ^& @echo off ^& call "%~d0\:_SYNTAX_POST_HOOK:\..\%~pnx0" "%$PERCENT-CALL%~f0" "
break > "%temp%\%~n0-macro-output.tmp"
echo %time% > "%temp%\%~n0-macro-output.tmp"
set "LIBRARY_DEBUG="
set "prompt=#FAIL#$G"
>> "%temp%\%~n0-macro-output.tmp" 2>&1 cmd /V:ON /c "%~dp0\%library_name%"
if %errorlevel% NEQ 0 (
echo ERROR: %errorlevel% ***************** SYNTAX ***************
)
REM findstr /V /R /C:"^#FAIL#>REM" /C:"^$" "%temp%\%~n0-macro-output.tmp"
findstr /n /R /C:"^#MACRO-ERROR#" "%temp%\%~n0-macro-output.tmp"
endlocal
exit /b
:_SYNTAX_POST_HOOK
if "%~n1" == "libBase" exit /b
REM *** Build the executor bat file
REM *** @TODO: Using (goto) technology que to detect if block is correctly closed
FOR %%$ in (1) DO (
ECHO @echo off
ECHO echo #MACRO-EXEC-START# %%Z
ECHO echo on
ECHO @(
echo echo #MACRO-EXEC-SUCCESS#
echo break ^> "%temp%\%~n0-signal.tmp"
ECHO exit /b 47
ECHO( %%%%Z%%
ECHO ^)
ECHO(
REM echo POST_HOOK: %%Z > CON
) > "%temp%\%~n0-macro-exec.bat"
REM *** Delete signal file
del "%temp%\%~n0-signal.tmp" 2> nul
REM *** Start the executer batch
REM *** That batch only checks if the macro can be expanded without syntax error
REM *** The macro isn't started
cmd /c "%temp%\%~n0-macro-exec.bat"
REM call "%temp%\%~n0-macro-exec.bat" 2> CON
setlocal EnableDelayedExpansion
echo #MACRO-EXEC-ERR# %errorlevel%
if not exist "!temp!\%~n0-signal.tmp" (
FOR %%$ in (1) DO (
set ^"text="%%Z" in "%~n1" fails to expands, perhaps a syntax error or not enough closing parenthesis"
echo #MACRO-ERROR# !text!
> CON echo ERROR: !text!
)
)
endlocal
exit /b
:_NEW_MACRO_HOOK
setlocal EnableDelayedExpansion
set "_libName=%~nx1"
if "!_libName!" NEQ "!library_name!" exit /b
setlocal DisableDelayedExpansion
FOR %%1 in ("1") DO (
set "filename=debug_%%Z.%check_lib_type%.%check_lib_mode%"
setlocal EnableDelayedExpansion
set "filename=!filename:$=#!"
set "filename=!filename:\=_!"
(set %%Z) > "!filename!"
(echo(!filename!) >> "%temp%\%~n0.tmp"
if defined LIBRARY_DEBUG (
echo ----- NEW_MACRO_HOOK: %%Z saved to: "!filename!"
)
endlocal
)
exit /b
:options
set "$PERCENT=%%%%~$=_p1_FOR-variable_DOLLAR_is_required_while_MACRO_DEFINITION_=:$"
set "$PERCENT-CALL=%%%%~$==:$%$PERCENT%~$==:$"
set "_hook=call "%~d0\:_NEW_MACRO_HOOK:\..\%~pnx0" %$PERCENT-CALL%~f0"
set "_only_case="
set "_store="
set "_check_syntax=1"
set "library_name="
set "LIBRARY_DEBUG="
set "$$LIBRARY_NEW_MACRO_HOOK="
:_option_loop
if "%~1" == "" exit /b
set "arg=%~1"
if "%arg:~0,1%" EQU "-" (
if "%arg%" == "-v" (
echo Option: Debug mode on
set "LIBRARY_DEBUG=1"
) ELSE if "%arg%" == "--verbose" (
echo Option: Debug mode on
set "LIBRARY_DEBUG=1"
) ELSE if "%arg%" == "--store" (
echo Option: Store macros to files
setlocal EnableDelayedExpansion
for /F "delims=" %%Q in ("!_hook!") DO (
endlocal
set "$$LIBRARY_NEW_MACRO_HOOK=%%Q"
)
set "_store=1"
) ELSE if "%arg%" == "--no-syntax" (
echo Option: Suppress syntax check
set "_check_syntax="
) ELSE if "%arg%" == "--case1" (
echo Option: Only test case1, simple DDE
set "_only_case=1"
) ELSE if "%arg%" == "--case3" (
echo Option: Only test case3, all-for-DDE
set "_only_case=3"
) ELSE (
echo ERROR: Unknown option "%arg%"
goto :halt
)
) ELSE (
echo Lib: %arg%
set "library_name=%arg%"
)
shift
goto :_option_loop
:halt
()
:check_macro_mode_diff
setlocal EnableDelayedExpansion
set "wildcard=debug_*simple.DDE"
for %%Q in (%wildcard%) do (
set "f_DDE=%%Q"
set "f_EDE=%%~nQ.EDE"
set "foundDifference=0"
FOR %%Q in (%%~nQ) DO set "f_base=%%~nQ"
set "f_all_DDE=!f_base!.all-for-variables.DDE"
set "f_all_EDE=!f_base!.all-for-variables.EDE"
(call)
call :isFileDiff "!f_DDE!" "!f_EDE!"
call :isFileDiff "!f_DDE!" "!f_all_DDE!"
call :isFileDiff "!f_all_DDE!" "!f_all_EDE!"
if "!foundDifference!" EQU "0" (
echo([ 'OKAY' ]; : .. !f_base!
) ELSE (
echo([ 'FAIL' ]; diff '!file1!' '!file2!'
)
)
exit /b
:isFileDiff
if "!foundDifference!" NEQ "0" exit /b
set "file1=%~1"
set "file2=%~2"
(call)
fc /b "!file1!" "!file2!" > NUL
set "foundDifference=!errorlevel!"
exit /B