-
Notifications
You must be signed in to change notification settings - Fork 10
/
wscmd.bat
executable file
·1175 lines (976 loc) · 29.1 KB
/
wscmd.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
@if (true == false) @end /*!
@echo off
setlocal enabledelayedexpansion
set wscmd.started=1
:wscmd.start
:: Set the name and version
set wscmd.name=Windows Scripting Command
set wscmd.version=0.23.20 Beta
set wscmd.copyright=Copyright ^(C^) 2009-2015, 2019, 2020 Ildar Shaimordanov
:: Prevent re-parsing of command line arguments
if not defined wscmd.started goto wscmd.2
set wscmd.started=
:: Parse command line arguments and set needful variables
set wscmd.temp=
set wscmd.inline=
set wscmd.inproc=
set wscmd.script=
set wscmd.script.n=
set wscmd.script.p=
set wscmd.script.begin=
set wscmd.script.end=
set wscmd.script.beginfile=
set wscmd.script.endfile=
set wscmd.engine=javascript
set wscmd.var=
set wscmd.compile=
set wscmd.debug=
set wscmd.quiet=
set wscmd.execute=
:: Help
if /i "%~1" == "/h" (
goto wscmd.help
)
if /i "%~1" == "/help" (
goto wscmd.help
)
:: Configuration file manual
if /i "%~1" == "/man" (
goto wscmd.man
)
:: Compiling and debugging modes
if /i "%~1" == "/compile" (
set wscmd.compile=1
shift /1
) else if /i "%~1" == "/embed" (
set wscmd.compile=2
shift /1
) else if /i "%~1" == "/debug" (
set wscmd.debug=1
shift /1
)
:: Variable definition
:wscmd.var.again
if /i not "%~1" == "/v" goto wscmd.var.end
set "wscmd.var=!wscmd.var!var %2 = "%3";"
shift /1
shift /1
shift /1
goto wscmd.var.again
:wscmd.var.end
if defined wscmd.var (
set wscmd.var="!wscmd.var!"
call :wscmd.unquote wscmd.var
)
:: Interactive mode
if "%~1" == "" (
shift /1
goto wscmd.1
)
if /i "%~1" == "/i" (
shift /1
goto wscmd.1
)
if /i "%~1" == "/q" (
set wscmd.quiet=/q
shift /1
goto wscmd.1
)
:: What language will be used
if /i "%~1" == "/js" (
set wscmd.engine=javascript
shift /1
) else if /i "%~1" == "/vbs" (
set wscmd.engine=vbscript
shift /1
)
:: Code or script file
if /i "%~1" == "/e" goto wscmd.opt.e.1
rem wscmd ... "filename" ...
set wscmd.inline=
set wscmd.inproc=
set wscmd.script=%~1
if defined wscmd.script if not exist "!wscmd.script!" (
echo.File not found "!wscmd.script!".
endlocal
exit /b 1
)
call :wscmd.engine "!wscmd.script!"
shift /1
goto wscmd.opt.e.2
:wscmd.opt.e.1
rem wscmd ... /e ... "string" ...
set wscmd.inline=1
set wscmd.inproc=
:wscmd.opt.e.again
for %%k in ( p n begin end beginfile endfile ) do (
if /i "%~2" == "/%%~k" (
set wscmd.inproc=1
set wscmd.script.%%~k=%3
call :wscmd.unquote wscmd.script.%%~k
shift /1
shift /1
goto wscmd.opt.e.again
)
)
rem wscmd ... /e "string" ...
if not defined wscmd.inproc (
set wscmd.script=%2
call :wscmd.unquote wscmd.script
shift /1
)
shift /1
:wscmd.opt.e.2
:wscmd.1
:: Parse program arguments
if "%~1" == "" goto wscmd.2
set wscmd.args=%wscmd.args% %1
shift /1
goto wscmd.1
:wscmd.2
if defined wscmd.debug call :wscmd.version>&2
:: Lookup and read a configuration file
call :wscmd.ini
:: Set defaults
if not defined wscmd.ini.session-reload set wscmd.ini.session-reload=65535
if not defined wscmd.ini.session-renew set wscmd.ini.session-renew=65534
if not defined wscmd.ini.include set wscmd.ini.include="%~dp0js\*.js" "%~dp0js\win32\*.js" "%~dp0vbs\win32\*.vbs"
if not defined wscmd.ini.execute set "wscmd.ini.execute=%TEMP%\$$$%~n0_$UID.wsf"
if not defined wscmd.ini.command set wscmd.ini.command=%WINDIR%\system32\cscript.exe //NoLogo
if not defined wscmd.ini.xml-encoding set wscmd.ini.xml-encoding=utf-8
if not defined wscmd.ini.enable-error set wscmd.ini.enable-error=false
if not defined wscmd.ini.enable-debug set wscmd.ini.enable-debug=false
:: Check imports
if defined wscmd.noimport set wscmd.ini.include=
:: Make the program name unique
call :wscmd.execute
:: Compile and link the source with libraries
call :wscmd.compile > "%wscmd.execute%"
:: Exit on /compile or /embed
if defined wscmd.compile goto wscmd.stop
if defined wscmd.debug echo.Running:>&2
%wscmd.ini.command% "%wscmd.execute%" %wscmd.args%
:: Reread the ini-file and reload the script in the current session
if !errorlevel! == !wscmd.ini.session-reload! goto wscmd.start
:: Remove the script
if exist "%wscmd.execute%" del "%wscmd.execute%"
:: Rereead the ini-file and reload the script in new session
if !errorlevel! == !wscmd.ini.session-renew! (
set wscmd.execute=
goto wscmd.start
)
:wscmd.stop
endlocal
goto :EOF
:wscmd.unquote
if !%1! == "" set %1=" "
if defined %1 set %1=!%1:~1,-1!
if defined %1 set %1=!%1:""="!
goto :EOF
:wscmd.engine
set wscmd.engine=javascript
if /i "%~x1" == ".vbs" set wscmd.engine=vbscript
goto :EOF
:wscmd.version
echo.%wscmd.name% Version %wscmd.version%
goto :EOF
:wscmd.help
call :wscmd.version
echo.
echo.%~n0 [/h ^| /help ^| /man]
echo.%~n0 [/compile ^| /embed] [/v var "val"] [/i ^| /q]
echo.%~n0 [/compile ^| /embed] [/v var "val"] [/js ^| /vbs] /e "code"
echo.%~n0 [/compile ^| /embed] [/v var "val"] [/js ^| /vbs] scriptfile
echo.%~n0 [/debug] [/v var "val"] [/i ^| /q] [arguments]
echo.%~n0 [/debug] [/v var "val"] [/js ^| /vbs] /e "code" [arguments]
echo.%~n0 [/debug] [/v var "val"] [/js ^| /vbs] scriptfile [arguments]
echo.
echo. /h, /help - Display this help
echo. /man - Display the configuration files guide
echo. /compile - Compile but not execute. Just store to a temporary file
echo. /embed - The same as above but embed external scripts into a file
echo. /debug - Output debugging information and execute
echo. /v var "val" - Assign the value "val" to the variable var, before
echo. execution of the program begins
echo. /i - Interactive mode
echo. /q - The same as /i but in quiet mode
echo. /js - Assume a value as a JavaScript
echo. /vbs - Assume a value as a VBScript
echo. /e "code" - Assume a value as a code to be executed
echo. /e /n "code" - Apply the code in a loop per each line of file^(s^)
echo. /e /p "code" - The same as /e /n but print a line also
echo.
echo.Extra options are available with /e /n or /e /p:
echo. /d file - Opens the file using the system default
echo. /u file - Opens the file as Unicode
echo. /a file - Opens the file as ASCII
echo.
echo."/" and "CON" (case-insensitive) are specified for the console.
echo.Using them allows reading data from the standard input.
echo.
echo.Extra options are used like /n or /p in the same way
echo. /begin - A code will be executed at the very beginning
echo. /end - A code will be executed at the very end
echo. /beginfile - A code will be executed before a file
echo. /endfile - A code will be executed after a file
goto wscmd.stop
:wscmd.man
echo.PREAMBLE
echo.
echo.This page shows how to control a content and behavior of the resulting
echo.script using configuration files. You can do this using by one of three
echo.ways. All options described below are common for all of them.
echo.
echo.1. Create the "%~n0.ini" file in the same directory where "%~nx0" is
echo. located. This file is common and it will be used in all other cases.
echo. That means that it will affect on all scripts always if other ways
echo. are not used.
echo.
echo.2. Create the "%~n0.ini" file in the current directory where some script
echo. will be launched. This file will affect on all files launched from the
echo. current directory only.
echo.
echo.3. Create the "SCRIPT.ini" file nearby the SCRIPT file (where SCRIPT stands
echo. for both the name and the extension of the current file). This file will
echo. affect on the SCRIPT file only.
echo.
echo.SYNTAX
echo.
echo.There are documented options available in ini-files. The syntax for all
echo.options is common and looks like below:
echo.
echo. name=value
echo.
echo.You are able to use them in any order but it is recommended to group them
echo.and use as described below:
echo.
echo.import
echo. This option specifies a path to librarian files that will be linked to
echo. the resulting script. Placeholders "*" or "?" are available to specify
echo. a group of files. Environment variables are enabled. In addition, you
echo. can use the following modifiers to refer to librarian files relatively
echo. the location of "%~nx0". Other modifiers are available but useless.
echo.
echo. %%~d0 - means a drive letter
echo. %%~p0 - means a path only
echo.
echo. There is special value "import=no" that suppresses inclusion of files.
echo. Just write out it directly in a custom configurational file to suppress
echo. inclusion of files.
echo.
echo.execute
echo. This option defines a name of the resulting file. If it is not specially
echo. specified, the default value will be used. There are two placeholders
echo. $TIME, the current time, and $UID, the unique number generated by
echo. the program, to make the resulting filename unique.
echo.
echo.command
echo. This option specifies a binary executable file that will be invoked to
echo. launch a script.
echo.
echo.xml-encoding
echo. A string that describes the character set encoding used by the resulting
echo. XML document. The string may include any of the character sets supported
echo. by Microsoft Internet Explorer. The default value is utf-8.
echo.
echo.enable-error
echo. A Boolean value. False is the default value. Set to true to allow error
echo. messages for syntax or run-time errors in the resulting.wsf file.
echo.
echo.enable-debug
echo. A Boolean value. False is the default value. Set to true to enable
echo. debugging. If debugging is not enabled, you will be unable to launch
echo. the script debugger for a Windows Script file.
echo.
echo.EXAMPLE
echo.
echo.The following example orders to add all js-files and vbs-files relatively
echo.the directory where "%~nx0" was run. The name of the executed script
echo.will be created in the current directory with the specified filename.
echo.The launcher is "CSCRIPT.EXE" with the suppressed banner.
echo.
echo. import=%%~dp0\js\*.js
echo. import=%%~dp0\vbs\*.vbs
echo. execute=.\$$$%%~n0_$UID.wsf
echo. command=%%windir%%\system32\cscript.exe //nologo
goto wscmd.stop
:wscmd.ini
:: Load settings from ini-files
:: there are special macros available to be substituted
:: %~d0 - the disk
:: %~p0 - the path
:: %~n0 - the filename
:: %~x0 - the extension
set wscmd.noimport=
set wscmd.ini.include=
set wscmd.ini.execute=
set wscmd.ini.command=
set wscmd.inifiles=".\%~n0.ini" "%~dpn0.ini"
if not defined wscmd.inline set wscmd.inifiles="!wscmd.script!.ini" ".\%~n0.ini" "%~dpn0.ini"
for %%i in ( !wscmd.inifiles! ) do (
if not "%%~ni" == "" if exist "%%~i" (
if defined wscmd.debug echo.Configuring from "%%~i">&2
for /f "usebackq tokens=1,* delims==" %%k in ( "%%~i" ) do (
call set wscmd.temp=%%~l
if defined wscmd.temp (
if /i "%%k" == "import" (
if /i "!wscmd.temp!" == "no" (
set wscmd.noimport=1
) else (
set wscmd.ini.include=!wscmd.ini.include! "!wscmd.temp!"
)
) else (
set wscmd.ini.%%k=!wscmd.temp!
)
)
)
goto :EOF
)
)
goto :EOF
:wscmd.execute
if defined wscmd.execute goto :EOF
set wscmd.execute=%wscmd.ini.execute%
setlocal
set POSH=
call :wscmd.execute.find.powershell powershell.exe
set WMIC=%windir%\System32\Wbem\wmic.exe
set GREP=%windir%\System32\findstr.exe
set FIND=%windir%\System32\find.exe
echo %wscmd.execute% | %FIND% "$TIME">nul 2>&1 && call :wscmd.execute.time
echo %wscmd.execute% | %FIND% "$UID">nul 2>&1 && call :wscmd.execute.uid
endlocal && set wscmd.execute=%wscmd.execute%
goto :EOF
:wscmd.execute.find.powershell
set "POSH=%~$PATH:1"
goto :EOF
:wscmd.execute.time
if defined POSH (
for /f %%d in ( '%POSH% -Command "Get-Date -UFormat '%%H%%M%%S'" ^<nul' ) do (
set "wscmd.execute=!wscmd.execute:$TIME=%%d!"
)
goto :EOF
)
for /f "tokens=1,2 delims==" %%i in (
'%WMIC% path win32_localtime get Hour^,Minute^,Second /value ^| %GREP% /v "^$"'
) do (
set wscmd.time.%%i=00%%j
set wscmd.time.%%i=!wscmd.time.%%i:~-3,-1!
)
set wscmd.execute=!wscmd.execute:$TIME=%wscmd.time.Hour%%wscmd.time.Minute%%wscmd.time.Second%!
goto :EOF
:wscmd.execute.uid
if defined POSH (
for /f %%p in ( '
%POSH% -Command "$p = (Get-WmiObject Win32_Process -Filter ProcessId=$pid).ParentProcessId; (Get-WmiObject Win32_Process -Filter ProcessId=$p).ParentProcessId" ^<nul
' ) do (
set "wscmd.execute=!wscmd.execute:$UID=%%p!"
)
goto :EOF
)
:wscmd.execute.uid.loop
rem delims is "=", ";", TAB, WS
for /f "skip=5 tokens=1,2 delims==; " %%a in (
'%WMIC% Process call create "%windir%\System32\wscript.exe //b" 2^>nul'
) do if "%%~a" == "ProcessId" (
set wscmd.uid=%%b
)
set wscmd.tmpfile=!wscmd.execute:$UID=%wscmd.uid%!
if exist "!wscmd.tmpfile!" goto wscmd.execute.uid.loop
set wscmd.execute=%wscmd.tmpfile%
goto :EOF
:wscmd.compile
echo.^<?xml version="1.0" encoding="%wscmd.ini.xml-encoding%" ?^>
echo.
echo.^<package^>
echo.^<job id="wscmd"^>
echo.^<?job error="%wscmd.ini.enable-error%" debug="%wscmd.ini.enable-debug%" ?^>
echo.
echo.^<runtime^>
echo.^<description^>^<^^^![CDATA[Created by %wscmd.name% Version %wscmd.version%
echo.%wscmd.copyright%
echo.]]^>^</description^>
echo.^</runtime^>
if not defined wscmd.compile (
echo.^<script language="javascript"^>^<^^^![CDATA[
echo.
echo.new ActiveXObject^('Scripting.FileSystemObject'^).DeleteFile^('!wscmd.execute:\=\\!'^);
echo.
echo.]]^>^</script^>
)
echo.^<script language="javascript"^>^<^^^![CDATA[
echo.
echo.var help = usage = function^(^)
echo.{
echo. WScript.Arguments.ShowUsage^(^);
echo.};
echo.
echo.var alert = echo = print = ^(function^(^)
echo.{
echo. var slice = Array.prototype.slice;
echo. return function^(^)
echo. {
echo. WScript.Echo^(slice.call^(arguments^)^);
echo. };
echo.}^)^(^);
echo.
echo.var quit = exit = function^(exitCode^)
echo.{
echo. WScript.Quit^(exitCode^);
echo.};
echo.
echo.var cmd = shell = function^(^)
echo.{
echo. var shell = new ActiveXObject^('WSCript.Shell'^);
echo. shell.Run^('%%COMSPEC%%'^);
echo.};
echo.
echo.var sleep = function^(time^)
echo.{
echo. return WScript.Sleep^(time^);
echo.};
echo.
echo.var clip = function^(^)
echo.{
echo. return new ActiveXObject^('htmlfile'^).parentWindow.clipboardData.getData^('Text'^);
echo.};
echo.
echo.var gc = CollectGarbage;
echo.
echo.var cArgs = WScript.Arguments;
echo.var nArgs = WScript.Arguments.Named;
echo.var uArgs = WScript.Arguments.Unnamed;
echo.
echo.]]^>^</script^>
if defined wscmd.debug echo.Libraries:>&2
set wscmd.link=include
if "%wscmd.compile%" == "2" set wscmd.link=embed
setlocal
for %%l in ( !wscmd.ini.include! ) do (
if defined wscmd.debug echo. "%%~l">&2
call :wscmd.engine "%%~l"
call :wscmd.%wscmd.link% "%%~l"
)
endlocal
if defined wscmd.var (
if defined wscmd.debug (
echo.Variables:
echo. !wscmd.var!
)>&2
echo.^<script language="javascript"^>^<^^^![CDATA[
echo.
echo.!wscmd.var!;
echo.
echo.]]^>^</script^>
)
if defined wscmd.inproc (
rem wscmd ... /e ... "string" ...
call :wscmd.inproc
) else if defined wscmd.inline (
rem wscmd ... /e "string" ...
call :wscmd.inline
) else if defined wscmd.script (
rem wscmd ... "filename" ...
if defined wscmd.debug echo.File: "!wscmd.script!">&2
call :wscmd.%wscmd.link% "!wscmd.script!"
) else (
rem Console mode, no inline scripts and no script files
rem wscmd
call :wscmd.%wscmd.link% "%~dpnx0"
)
echo.^</job^>
echo.^</package^>
goto :EOF
:wscmd.include
set "wscmd.filename=%~f1"
set "wscmd.filename=%wscmd.filename:&=&%"
echo.^<script language="%wscmd.engine%" src="%wscmd.filename%"^>^</script^>
goto :EOF
:wscmd.embed
echo.^<^^^!-- "%~1" --^>
echo.^<script language="%wscmd.engine%"^>^<^^^![CDATA[
echo.
type "%~1"
echo.
echo.]]^>^</script^>
goto :EOF
:wscmd.inline
if defined wscmd.debug (
echo.Inline:
echo. !wscmd.script!
)>&2
echo.^<script language="%wscmd.engine%"^>^<^^^![CDATA[
echo.
echo.!wscmd.script!
echo.
echo.]]^>^</script^>
goto :EOF
:wscmd.inproc
if defined wscmd.debug (
echo.Inline:
if defined wscmd.script.begin echo. !wscmd.script.begin!
echo. for each file
if defined wscmd.script.beginfile echo. !wscmd.script.beginfile!
echo. while not EOF
if defined wscmd.script.n echo. !wscmd.script.n!
if defined wscmd.script.p echo. !wscmd.script.p!
if defined wscmd.script.p echo. print line
echo. end while
if defined wscmd.script.endfile echo. !wscmd.script.endfile!
echo. end for
if defined wscmd.script.end echo. !wscmd.script.end!
)>&2
call :wscmd.inproc.%wscmd.engine%
setlocal
set wscmd.engine=javascript
call :wscmd.%wscmd.link% "%~dpnx0"
endlocal
goto :EOF
:wscmd.inproc.javascript
echo.^<script language="javascript"^>^<^^^![CDATA[
echo.
echo.//@cc_on
echo.//@set @user_inproc_mode = 2
echo.
echo.var userFunc = function^(line, currentNumber, filename, lineNumber, fso, stdin, stdout, stderr^)
echo.{
if defined wscmd.script.n (
echo. !wscmd.script.n!;
)
if defined wscmd.script.p (
echo. !wscmd.script.p!;
echo. if ^( line ^^^!== void 0 ^) {
echo. WScript.StdOut.WriteLine^(line^);
echo. }
)
echo.};
echo.
echo.var userFuncBegin = function^(lineNumber, fso, stdin, stdout, stderr^)
echo.{
echo. !wscmd.script.begin!;
echo.};
echo.
echo.var userFuncEnd = function^(lineNumber, fso, stdin, stdout, stderr^)
echo.{
echo. !wscmd.script.end!;
echo.};
echo.
echo.var userFuncBeginfile = function^(currentNumber, filename, lineNumber, fso, stdin, stdout, stderr^)
echo.{
echo. !wscmd.script.beginfile!;
echo.};
echo.
echo.var userFuncEndfile = function^(currentNumber, filename, lineNumber, fso, stdin, stdout, stderr^)
echo.{
echo. !wscmd.script.endfile!;
echo.};
echo.
echo.]]^>^</script^>
goto :EOF
:wscmd.inproc.vbscript
echo.^<script language="javascript"^>^<^^^![CDATA[
echo.
echo.//@cc_on
echo.//@set @user_inproc_mode = 1
echo.
echo.function Var^(name, value^)
echo.{
echo. this[name] = value;
echo.};
echo.
echo.]]^>^</script^>
echo.^<script language="vbscript"^>^<^^^![CDATA[
echo.
echo.Sub userFunc^(line, currentNumber, filename, lineNumber, fso, stdin, stdout, stderr^)
if defined wscmd.script.n (
echo. !wscmd.script.n!
)
if defined wscmd.script.p (
echo. !wscmd.script.p!
echo. If Not IsEmpty^(line^) Then
echo. WScript.StdOut.WriteLine line
echo. End If
)
echo.End Sub
echo.
echo.Sub userFuncBegin^(lineNumber, fso, stdin, stdout, stderr^)
echo. !wscmd.script.begin!
echo.End Sub
echo.
echo.Sub userFuncEnd^(lineNumber, fso, stdin, stdout, stderr^)
echo. !wscmd.script.end!
echo.End Sub
echo.
echo.Sub userFuncBeginfile^(currentNumber, filename, lineNumber, fso, stdin, stdout, stderr^)
echo. !wscmd.script.beginfile!
echo.End Sub
echo.
echo.Sub userFuncEndfile^(currentNumber, filename, lineNumber, fso, stdin, stdout, stderr^)
echo. !wscmd.script.endfile!
echo.End Sub
echo.
echo.]]^>^</script^>
goto :EOF
@goto:eof */
(function()
{
//@cc_on
//@if ( ! @user_inproc_mode )
return;
//@end
//@if ( @user_inproc_mode == 2 )
var userFunc = this.userFunc;
var userFuncBegin = this.userFuncBegin;
var userFuncEnd = this.userFuncEnd;
var userFuncBeginfile = this.userFuncBeginfile;
var userFuncEndfile = this.userFuncEndfile;
//@end
var uc = String.prototype.toUpperCase;
var fso = new ActiveXObject('Scripting.FileSystemObject');
var files = WScript.Arguments;
if ( files.length == 0 ) {
// Emulate empty list of arguments
files = ['/'];
files.item = function(i) { return this[i]; };
}
// The number of all lines of all files
var lineNumber = 0;
// The function is called before processing any file.
// The total number of input lines is 0.
userFuncBegin(
lineNumber,
fso, WScript.StdIn, WScript.StdOut, WScript.StdErr);
var format = 0;
var file;
var isFile;
var stream;
for (var i = 0; i < files.length; i++) {
file = files.item(i);
// Opens the file using the system default
if ( file == '/D' || file == '/d' ) {
format = -2;
continue;
}
// Opens the file as Unicode
if ( file == '/U' || file == '/u' ) {
format = -1;
continue;
}
// Opens the file as ASCII
if ( file == '/A' || file == '/a' ) {
format = 0;
continue;
}
isFile = true;
if ( file == '/' || uc.call(file) == 'CON' ) {
file = '<stdin>';
isFile = false;
}
// The number of the current line for the actual file.
var currentNumber = 0;
// The function is called before opening of the file.
// The file name is known, the number of line of the file is 0.
userFuncBeginfile(
currentNumber, file, lineNumber,
fso, WScript.StdIn, WScript.StdOut, WScript.StdErr);
var e;
try {
stream = ! isFile
? WScript.StdIn
: fso.OpenTextFile(file, 1, false, format);
} catch (e) {
WScript.StdErr.WriteLine(e.message + ': ' + file);
continue;
}
// Prevent fail of reading out of STDIN stream
// The real exception number is 800a005b
// "Object variable or With block variable not set"
try {
stream.AtEndOfStream;
} catch (e) {
WScript.StdErr.WriteLine('Out of stream: ' + file);
continue;
}
while ( ! stream.AtEndOfStream ) {
currentNumber++;
lineNumber++;
var line = stream.ReadLine();
try {
// Processing of the file. Available parameters are the
// current line, it's number in the file, and the number
// of the line in the list of all files.
userFunc(
line, currentNumber, file, lineNumber,
fso, WScript.StdIn, WScript.StdOut, WScript.StdErr);
} catch (e) {
if ( isFile ) {
stream.Close();
}
WScript.StdErr.WriteLine(e.message);
WScript.Quit();
}
} // while
if ( isFile ) {
stream.Close();
}
// A file processing is completed, and a file is closed already.
// The filename and the total number of lines are known.
// The currentNumber is the number of lines of the last file.
userFuncEndfile(
currentNumber, file, lineNumber,
fso, WScript.StdIn, WScript.StdOut, WScript.StdErr);
} // for
// The function will be executed when all files have been processed.
// Only lineNumber, the total amount of lines is known.
userFuncEnd(
lineNumber,
fso, WScript.StdIn, WScript.StdOut, WScript.StdErr);
WScript.Quit();
})();
/**
*
* Useful functions
*
*/
var help = usage = (function()
{
var helpMsg = '\n'
+ 'Commands Descriptions\n'
+ '======== ============\n'
+ 'help(), usage() Display this help\n'
+ 'alert(), echo(), print() Print expressions\n'
+ 'quit(), exit() Quit this shell\n'
+ 'eval.history Display the history\n'
+ 'eval.save([format]) Save the history to the file\n'
+ 'eval.inspect() The stub to transform output additionally\n'
+ 'cmd(), shell() Run new DOS-session\n'
+ 'sleep(n) Sleep n milliseconds\n'
+ 'clip() Get from the clipboard data formatted as text\n'
+ 'reload([true]) Reload this session or open new one\n'
+ 'gc() Run the garbage collector\n'
;
return function()
{
WScript.Echo(helpMsg);
};
})();
var alert = echo = print = (function()
{
var slice = Array.prototype.slice;
return function()
{
WScript.Echo(slice.call(arguments));
};
})();
var quit = exit = function(exitCode)
{
WScript.Quit(exitCode);
};
var cmd = shell = function()
{
var shell = new ActiveXObject('WSCript.Shell');
shell.Run('%COMSPEC%');
};
var sleep = function(time)
{
return WScript.Sleep(time);
};
var clip = function()
{
return new ActiveXObject('htmlfile').parentWindow.clipboardData.getData('Text');
};
var reload = function(newSession)
{
var shell = new ActiveXObject('WScript.Shell');
var env = shell.Environment('PROCESS');
var name = newSession ? 'wscmd.ini.session-renew' : 'wscmd.ini.session-reload';
var code = env(name);
WScript.Quit(code);
};
var gc = CollectGarbage;
/**
*
* Enabled in the CLI mode ONLY
*
*/
if ( ! WScript.FullName.match(/cscript/i) ) {
help();
exit();
}
/**
*
* The line number
*
*/
eval.number = 0;
/**
*
* The history of commands
*
*/
eval.history = '';
eval.save = function(format)
{
var fso = new ActiveXObject('Scripting.FileSystemObject');
var f = fso.OpenTextFile('.\\wscmd.history', 8, true, format);
f.Write(eval.history);
f.Close();
};
/**
*
* The references to the command line arguments
*
*/
var cArgs = WScript.Arguments;
var nArgs = WScript.Arguments.Named;
var uArgs = WScript.Arguments.Unnamed;
while ( true ) {
/*
This is internally used variable for catching run-time errors. We
do not try to hide it completely from the user but we do an attempt to
complicate its name as much as possible to prevent intersection with
user-defined objects. Also we understand that the user can give the