-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmdize.cmd
294 lines (265 loc) · 8.78 KB
/
cmdize.cmd
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
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION&::(Don't pollute the global environment with the following)
::**********************************************************************
SET $NAME=%~n0
SET $DESCRIPTION=Gives VBScript
SET $AUTHOR=Ildar Shaimordanov
SET $SOURCE=%~f0
::@(#)NAME
::@(-) The name of the command or function, followed by a one-line description of what it does.
::@(#) %$NAME% -- %$DESCRIPTION%
::@(#)
::@(#)SYNOPSIS
::@(-) In the case of a command, a formal description of how to run it and what command line options it takes.
::@(-) For program functions, a list of the parameters the function takes and which header file contains its definition.
::@(-)
::@(#) %$NAME% [vbs] [...]
::@(#)
::@(#)OPTIONS
::@(-) Flags, parameters, arguments (NOT the Monty Python way)
::@(#) -h Help page
::@(#)
::@ (#)
::@(#)DESCRIPTION
::@(-) A textual description of the functioning of the command or function.
::@(#) This tool converts a supported code into a batch file that can be
::@(#) executed without explicit invoking the executable engine. The script
::@(#) creates new file and places it under the same directory as the original
::@(#) one with the same name, replacing the original extension with ".bat".
::@(#) The content of the new file consists of the original file and the
::@(#) special header that being the "polyglot" and having some tricks to be a
::@(#) valid code in batch file and the wrapped code at the same time.
::@(#)
::@(#) FEATURES
::@(#) It does comment on "Option Explicit" in VBScript.
::@(#) "<xml>" declaration for wsf-files is expected.
::@(#) "Option Explicit" and "<xml>" in a single line only are supported.
::@(#) BOM is not supported at all.
::@(#)
::@(#)EXAMPLES
::@(-) Some examples of common usage.
::@(#)
::@(#)
::@(#)
::@ (#)EXIT STATUS
::@(-) Exit status / errorlevel is 0 if OK, otherwise 1+.
::@ (#)
::@ (#)ENVIRONMENT
::@(-) Variables affected
::@ (#)
::@ (#)
::@ (#)FILES,
::@(-) Files used, required, affected
::@ (#)
::@ (#)
::@ (#)BUGS / KNOWN PROBLEMS
::@(-) If any known
::@ (#)
::@ (#)
::@ (#)REQUIRES
::@(-) Dependencies
::@ (#)
::@ (#)
::@ (#)SEE ALSO
::@(-) A list of related commands or functions.
::@ (#)
::@ (#)
::@(#)REFERENCE
::@(-) References to inspiration, clips and other documentation
::@ (#) Author:
::@(#) URL: http://www.dostips.com/forum/viewtopic.php?p=37780#p37780
::@(#) .js
::@(#) http://forum.script-coding.com/viewtopic.php?pid=79210#p79210
::@(#) http://www.dostips.com/forum/viewtopic.php?p=33879#p33879
::@(#)
::@(#) .vbs
::@(#) http://www.dostips.com/forum/viewtopic.php?p=33882#p33882
::@(#) http://www.dostips.com/forum/viewtopic.php?p=32485#p32485
::@(#)
::@(#) .pl
::@(#) For details and better support see "pl2bat.bat" from Perl distribution
::@(#)
::@(#) .ps1
::@(#) http://blogs.msdn.com/b/jaybaz_ms/archive/2007/04/26/powershell-polyglot.aspx
::@(#) http://stackoverflow.com/a/2611487/3627676
::@(#)
::@(#) .hta and .html?
::@(#) http://forum.script-coding.com/viewtopic.php?pid=79322#p79322
::@(#)
::@(#) .wsf
::@(#) http://www.dostips.com/forum/viewtopic.php?p=33963#p33963
::@ (#)
::@(#)
::@(#)SOURCE
::@(-) Where to find this source
::@(#) %$Source%
::@(#)
::@ (#)AUTHOR
::@(-) Who did what
::@ (#) %$AUTHOR%
::*** HISTORY **********************************************************
::SET $VERSION=xx.xxx&SET $REVISION=YYYY-MM-DDThh:mm&SET $COMMENT=Description/init
::SET $VERSION=2015-02-19&SET $REVISION=00:00:00&SET $COMMENT=Initial/ErikBachmann
SET $VERSION=2015-10-08&SET $REVISION=11:20:00&SET $COMMENT=GetOpt: Calling usage on -h and exit on error / ErikBachmann
::**********************************************************************
::@(#)(c)%$Version:~0,4% %$Author%
::**********************************************************************
CALL "%~dp0\_DEBUG"
CALL "%~dp0\_Getopt" %*&IF ERRORLEVEL 1 EXIT /B 1
::ENDLOCAL
:: USAGE
:: cmdize name [...]
::
:: This tool converts a supported code into a batch file that can be
:: executed without explicit invoking the executable engine. The script
:: creates new file and places it under the same directory as the original
:: one with the same name, replacing the original extension with ".bat".
:: The content of the new file consists of the original file and the
:: special header that being the "polyglot" and having some tricks to be a
:: valid code in batch file and the wrapped code at the same time.
::
:: FEATURES
:: It does comment on "Option Explicit" in VBScript.
:: "<?xml?>" declaration for wsf-files is expected.
:: "Option Explicit" and "<?xml?>" in a single line only are supported.
:: BOM is not supported at all.
::
:: SEE ALSO
:: Proceed the following links to learn more the origins
::
:: .js
:: http://forum.script-coding.com/viewtopic.php?pid=79210#p79210
:: http://www.dostips.com/forum/viewtopic.php?p=33879#p33879
::
:: .vbs
:: http://www.dostips.com/forum/viewtopic.php?p=33882#p33882
:: http://www.dostips.com/forum/viewtopic.php?p=32485#p32485
::
:: .pl
:: For details and better support see "pl2bat.bat" from Perl distribution
::
:: .ps1
:: http://blogs.msdn.com/b/jaybaz_ms/archive/2007/04/26/powershell-polyglot.aspx
:: http://stackoverflow.com/a/2611487/3627676
::
:: .hta and .html?
:: http://forum.script-coding.com/viewtopic.php?pid=79322#p79322
::
:: .wsf
:: http://www.dostips.com/forum/viewtopic.php?p=33963#p33963
::
:: COPYRIGHTS
:: Copyright (c) 2014 Ildar Shaimordanov
:: URL=http://www.dostips.com/forum/viewtopic.php?p=37780#p37780
::
@echo off
if "%~1" == "" (
for %%p in ( powershell.exe ) do if not "%%~$PATH:p" == "" (
"%%~$PATH:p" -NoProfile -NoLogo -Command "cat '%~f0' | where { $_ -match '^::' } | %% { $_ -replace '::', '' }"
goto :EOF
)
for /f "usebackq tokens=* delims=:" %%s in ( "%~f0" ) do (
if /i "%%s" == "@echo off" goto :EOF
echo:%%s
)
goto :EOF
)
:cmdize.loop.begin
if "%~1" == "" goto :cmdize.loop.end
if not exist "%~f1" (
echo:%~n0: File not found: "%~1">&2
goto :cmdize.loop.continue
)
for %%x in ( .js .vbs .pl .ps1 .hta .htm .html .wsf ) do (
if /i "%~x1" == "%%~x" (
call :cmdize%%~x "%~1" >"%~dpn1.bat"
goto :cmdize.loop.continue
)
)
echo:%~n0: Unsupported extension: "%~1">&2
:cmdize.loop.continue
shift
goto :cmdize.loop.begin
:cmdize.loop.end
goto :EOF
:cmdize.js
echo:@if ^(true == false^) @end /*!
echo:@echo off
echo:"%%windir%%\System32\cscript.exe" //nologo //e:javascript "%%~f0" %%*
echo:goto :EOF */
type "%~f1"
goto :EOF
:cmdize.vbs.h
set /p "=::'" <nul
type "%TEMP%\%~n0.$$"
echo:%*
goto :EOF
:cmdize.vbs
copy /y nul + nul /a "%TEMP%\%~n0.$$" /a 1>nul
call :cmdize.vbs.h @echo off
call :cmdize.vbs.h "%%%%windir%%%%\System32\cscript.exe" //nologo //e:vbscript "%%%%~f0" %%%%*
call :cmdize.vbs.h goto :EOF
del /q "%TEMP%\%~n0.$$"
rem type "%~f1"
for /f "tokens=1,* delims=]" %%r in ( ' call "%windir%\System32\find.exe" /n /v "" ^<"%~f1" ' ) do (
rem Filtering and commenting "Option Explicit".
rem This ugly code tries as much as possible to recognize and
rem comment this directive. It fails if "Option" and "Explicit"
rem are located on two neighbor lines, consecutively, one by one.
rem But it is too hard to imagine that there is someone who
rem practices such a strange coding style.
for /f "usebackq tokens=1,2" %%a in ( '%%s' ) do if /i "%%~a" == "Option" for /f "usebackq tokens=1,* delims=:'" %%i in ( 'x%%b' ) do if /i "%%~i" == "xExplicit" (
echo:%~n0: Commenting Option Explicit in "%~1">&2
echo:rem To prevent compilation error due to embedding into a batch file,
echo:rem the following line was commented automatically.
set /p "=rem " <nul
)
echo:%%s
)
goto :EOF
:cmdize.pl
echo:@rem = '--*-Perl-*--
echo:@echo off
echo:perl -x -S "%%~f0" %%*
echo:goto :EOF
echo:@rem ';
echo:#!perl
type "%~f1"
goto :EOF
:cmdize.ps1
echo:^<# :
echo:@echo off
echo:setlocal
echo:set "POWERSHELL_BAT_ARGS=%%*"
echo:if defined POWERSHELL_BAT_ARGS set "POWERSHELL_BAT_ARGS=%%POWERSHELL_BAT_ARGS:"=\"%%"
echo:endlocal ^& powershell -NoLogo -NoProfile -Command "$_ = $input; Invoke-Expression $( '$input = $_; $_ = \"\"; $args = @( &{ $args } %%POWERSHELL_BAT_ARGS%% );' + [String]::Join( [char]10, $( Get-Content \"%%~f0\" ) ) )"
echo:goto :EOF
echo:#^>
type "%~f1"
goto :EOF
:cmdize.hta
:cmdize.htm
:cmdize.html
echo:^<!-- :
echo:@echo off
echo:start "" "%%windir%%\System32\mshta.exe" "%%~f0" %%*
echo:goto :EOF
echo:--^>
type "%~f1"
goto :EOF
:cmdize.wsf
for /f "usebackq tokens=1,2,* delims=?" %%a in ( "%~f1" ) do for /f "tokens=1,*" %%d in ( "%%b" ) do (
rem We use this code to transform the "<?xml?>" declaration
rem located at the very beginning of the file to the "polyglot"
rem form to do it acceptable by the batch file.
echo:%%a?%%d :
echo:: %%e ?^>^<!--
echo:@echo off
echo:"%%windir%%\System32\cscript.exe" //nologo "%%~f0?.wsf" %%*
echo:goto :EOF
echo:: --%%c
more +1 <"%~f1"
goto :EOF
)
goto :EOF
rem EOF