Advertisement
Guest User

S/TracerTSuperMax

a guest
Aug 30th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 18.96 KB | None | 0 0
  1. @echo off
  2.  
  3. rem *************** start of 'main'
  4.  
  5. set DEBUG=0
  6. if "%DEBUG%"=="1" (set TRACE=echo) else (set TRACE=rem)
  7.  
  8. rem Note that right now there is a bug in tracerpt.exe cause of which you might want to use tracefmt.exe instead.
  9. rem set the value 1 if you want to use tracefmt.exe instead
  10. set USE_TRACE_FMT=1
  11. %TRACE% The value of variable USE_TRACE_FMT is %USE_TRACE_FMT%
  12.  
  13. rem define variables for each of the switches we support
  14. set SWHELP=h
  15. set SWMODE=mode
  16. set SWTRACELOG=tracelog
  17. set SWMOF=mof
  18. set SWOUTPUT=o
  19. set VALID=0
  20.  
  21. rem define variables for parameters to be passed to tracerpt
  22. set TRACEDIR=%WINDIR%\system32\msdtc\trace
  23. set TRACEFILE1=%TRACEDIR%\dtctrace.log
  24. set TRACEFILE2=%TRACEDIR%\tracetx.log
  25. set MOFFILE=%TRACEDIR%\msdtctr.mof
  26. set ERRORFILE=%TRACEDIR%\errortrace.txt
  27. set OUTPUTFILE=%TRACEDIR%\trace
  28.  
  29. rem Parse command line and setup variables
  30. set CMDLINE=%*
  31. %TRACE% About to call PARSECMDLINE with the argument %CMDLINE%
  32. call :PARSECMDLINE 0
  33.  
  34. rem Validate the command line
  35. %TRACE% About to call the procedure VALIDATE
  36. call :VALIDATE
  37.  
  38. rem if Vaidation fails, we give up
  39. if "%VALID%"=="0" (
  40.     %TRACE% Parameter validation failed, exiting ...
  41.     goto :EOF
  42. )
  43.  
  44. rem depending on the value of the mode, set the tracelogfile
  45. call :MYFINDSWITCH %SWMODE%
  46. if not "%RET%"=="0" (
  47.     if "%RETV%"=="1" set TRACEFILE=%TRACEFILE1%
  48.     if "%RETV%"=="2" set TRACEFILE=%TRACEFILE2%
  49. )
  50.  
  51. rem if the tracelog switch was used, set the tracelogfile
  52. call :MYFINDSWITCH %SWTRACELOG%
  53. if not "%RET%"=="0" (
  54.     set TRACEFILE=%RETV%
  55. )
  56.  
  57. rem if the mof switch was used, set the moffile
  58. call :MYFINDSWITCH %SWMOF%
  59. if not "%RET%"=="0" (
  60.     set MOFFILE=%RETV%
  61. )
  62.  
  63. rem if the output switch was used, set the output file
  64. call :MYFINDSWITCH %SWOUTPUT%
  65. if not "%RET%"=="0" (
  66.     set OUTPUTFILE=%RETV%
  67. )
  68.  
  69. %TRACE% TRACEFILE=%TRACEFILE%
  70. %TRACE% MOFFILE=%MOFFILE%
  71. %TRACE% OUTPUTFILE=%OUTPUTFILE%
  72.  
  73. rem if the specified tracelogfile does not exist, display an error message and give up
  74. if not exist %TRACEFILE% (
  75.     echo The tracelogfile %TRACEFILE% does not exist. exiting ...
  76.     call :HELP
  77.     goto :EOF
  78. )
  79.  
  80. rem if the specified moffile does not exist, display an error message and give up
  81. if not exist %MOFFILE% (
  82.     echo The moffile %MOFFILE% does not exist. exiting ...
  83.     call :HELP
  84.     goto :EOF
  85. )
  86.  
  87. rem set a variable for output file with extension
  88. set OUTPUTFILEWITHEXT=%OUTPUTFILE%.csv
  89. %TRACE% The value of variable OUTPUTFILEWITHEXT=%OUTPUTFILEWITHEXT%
  90.  
  91.  
  92. rem if the specified outputfile exists, ask if the user is ok with it being over-written.
  93. rem if the user wants to continue, delete the old output file, else give up.
  94. if exist %OUTPUTFILEWITHEXT% (
  95.     echo The file %OUTPUTFILEWITHEXT% already exists. You may press Control-C to terminate the batch file. Continuing the batch file will overwrite this file.
  96.     Pause
  97.     del %OUTPUTFILEWITHEXT% 1>nul 2>nul
  98. )
  99.  
  100. rem if the old error file exists, delete it
  101. if exist %ERRORFILE% (
  102.     del %ERRORFILE% 1>nul 2>nul
  103.     %TRACE% Deleted the file %ERRORFILE%
  104. )
  105.  
  106. rem call the utility with the right arguments
  107. %TRACE% About to call the utility tracerpt.exe ...
  108.  
  109. if "%USE_TRACE_FMT%"=="0" (goto :USE_TRACEPRT_UTILITY) else (goto :USE_TRACEFMT_UTILITY)
  110.  
  111. :USE_TRACEPRT_UTILITY
  112. %TRACE% Entered the USE_TRACEPRT_UTILITY block, about to call traceprt
  113. tracerpt %TRACEFILE% -o %OUTPUTFILE% -mof %MOFFILE% > %ERRORFILE% 2>&1
  114. rem if output file does not exist, display an error message and give up
  115. if not exist %OUTPUTFILEWITHEXT% (
  116.     %TRACE% The file %OUTPUTFILEWITHEXT% does not exist, therefore exiting ...
  117.     call :DISPLAY_ERROR_MESSAGE
  118.     goto :EOF
  119. )
  120. notepad %OUTPUTFILEWITHEXT%
  121. goto :EOF
  122.  
  123. :USE_TRACEFMT_UTILITY
  124. %TRACE% Entered the USE_TRACEFMT_UTILITY block, about to call tracefmt
  125. tracefmt %TRACEFILE% -o %OUTPUTFILEWITHEXT% -tmf %MOFFILE%  -nosummary > %ERRORFILE% 2>&1
  126. rem if output file does not exist, display an error message and give up
  127. if not exist %OUTPUTFILEWITHEXT% (
  128.     %TRACE% The file %OUTPUTFILEWITHEXT% does not exist, therefore exiting ...
  129.     call :DISPLAY_ERROR_MESSAGE
  130.     goto :EOF
  131. )
  132. notepad %OUTPUTFILEWITHEXT%
  133. goto :EOF
  134.  
  135.  
  136.  
  137. goto :EOF
  138. rem *************** end of 'main'
  139.  
  140.  
  141.  
  142.  
  143. rem *************** Procedures begin here ****************************
  144.  
  145. rem *************** start of procedure VALIDATE
  146. :VALIDATE
  147.  
  148. set ARG=1
  149. set SWHELPFOUND=0
  150. set SWMODEFOUND=0
  151. set SWTRACELOGFOUND=0
  152. set SWMOFFOUND=0
  153. set SWOUTPUTFOUND=0
  154. set OUTNAMENAME=0
  155.  
  156. rem If no arguments are used at all, don't perform any other validation, just display help and give up
  157. if %CMDARGCOUNT% EQU 0 if %CMDSWCOUNT% EQU 0 (call :HELP) & (goto :EOF)
  158.  
  159. rem If not arguments are given, display help
  160. if %CMDARGCOUNT% GTR 0 goto ERROR_USED_ARGUMENTS_WITHOUT_SWITCHES
  161.    
  162. rem If the switch SWHELP is used anywhere, don't perform any other validation, just display help and give up
  163. call :MYFINDSWITCH %SWHELP%
  164. if not "%RET%"=="0" (call :HELP) & (goto :EOF)
  165.  
  166. :SWLOOP                                                                                                                    
  167.     if %ARG% GTR %CMDSWCOUNT% goto :SWLOOPEND
  168.     call :GETSWITCH %ARG%
  169.     set MYSWITCH=%RET:~1%
  170.    
  171.     rem make sure no switch is used twice
  172.     if /i "%MYSWITCH%"=="%SWHELP%" (if "%SWHELPFOUND%"=="1" (goto ERROR_USED_SAME_SWITCH_TWICE) else (set SWHELPFOUND=1))
  173.     if /i "%MYSWITCH%"=="%SWMODE%" (if "%SWMODEFOUND%"=="1" (goto ERROR_USED_SAME_SWITCH_TWICE) else (set SWMODEFOUND=1))
  174.     if /i "%MYSWITCH%"=="%SWTRACELOG%" (if "%SWTRACELOGFOUND%"=="1" (goto ERROR_USED_SAME_SWITCH_TWICE) else (set SWTRACELOGFOUND=1))
  175.     if /i "%MYSWITCH%"=="%SWMOF%" (if "%SWMOFFOUND%"=="1" (goto ERROR_USED_SAME_SWITCH_TWICE) else (set SWMOFFOUND=1))
  176.     if /i "%MYSWITCH%"=="%SWOUTPUT%" (if "%SWOUTPUTFOUND%"=="1" (goto ERROR_USED_SAME_SWITCH_TWICE) else (set SWOUTPUTFOUND=1))
  177.    
  178.     rem make sure that the switches mode and tracelog are not used simultaneously
  179.     if "%SWMODEFOUND%"=="1" if "%SWTRACELOGFOUND%"=="1" goto ERROR_USED_BOTH_MODE_AND_TRACELOG
  180.    
  181.     rem make sure that there is no switch outside our list
  182.     if /i not "%MYSWITCH%"=="%SWHELP%" (
  183.         if /i not "%MYSWITCH%"=="%SWMODE%" (
  184.             if /i not "%MYSWITCH%"=="%SWTRACELOG%" (
  185.                 if /i not "%MYSWITCH%"=="%SWMOF%" (
  186.                     if /i not "%MYSWITCH%"=="%SWOUTPUT%" (
  187.                         (echo Invalid Switch "%RET%") & (call :HELP) & (goto :EOF) )))))
  188.     set /a ARG+=1
  189. goto :SWLOOP
  190. :SWLOOPEND
  191.  
  192. rem make sure that either the switch "-mode" or "-tracelog" was used
  193. if "%SWMODEFOUND%"=="0" if "%SWTRACELOGFOUND%"=="0" (echo Invalid Usage : neither "-%SWMODE%" nor "-%SWTRACELOG%" was specified) & (call :HELP) & (goto :EOF)
  194.  
  195. rem make sure that the value of the mode entered is valid
  196. call :MYFINDSWITCH %SWMODE%
  197. if not "%RET%"=="0" if not "%RETV%"=="1" if not "%RETV%"=="2" goto ERROR_INVALID_MODE
  198.  
  199. rem make sure that the value of the outputfile entered does not have any extension
  200. call :MYFINDSWITCH %SWOUTPUT%
  201. for /f "tokens=1* delims=." %%I in ("%RETV%") do (set OUTPUTEXT=%%J)
  202. if not "%OUTPUTEXT%"=="" goto ERROR_USED_OUTPUTFILENAME_WITH_EXTENSION
  203.  
  204. rem if we have come this far, everything went well, set the valid flag
  205. set VALID=1
  206. goto :EOF
  207.  
  208. :ERROR_USED_SAME_SWITCH_TWICE
  209. (echo Invalid Usage : use the switch %RET% multiple times) & (call :HELP) & (goto :EOF)
  210.  
  211. :ERROR_USED_BOTH_MODE_AND_TRACELOG
  212. (echo Invalid Usage : cannot use both "-%SWMODE%" and "-%SWTRACELOG%" at the same time) & (call :HELP) & (goto :EOF)
  213.  
  214. :ERROR_USED_ARGUMENTS_WITHOUT_SWITCHES
  215. call :GETARG 1
  216. echo Invalid Usage : "%RET%" used without any switch
  217. call :HELP
  218. goto :EOF
  219.  
  220. :ERROR_INVALID_MODE
  221. (echo Invalid Usage : Valid values for %SWMODE% are 1 and 2) & (call :HELP) & (goto :EOF)
  222.  
  223. :ERROR_USED_OUTPUTFILENAME_WITH_EXTENSION
  224. (echo Invalid Usage : Output filename should not have any extension) & (call :HELP) & (goto :EOF)
  225.  
  226. rem *************** end of procedure VALIDATE
  227.  
  228. rem *************** start of procedure HELP
  229. :HELP
  230. echo Usage
  231. echo "msdtcvtr { -MODE {1 | 2} | -tracelog tracelogfilename } [options]"
  232. echo "All switches can be prefixed with either '-' or '/'"
  233. echo Parameters:
  234. echo    "-MODE 1          to view background tracing"
  235. echo    "-MODE 2          to view tracing generated by ui"
  236. echo    "-tracelog <file> binary Trace log file name"
  237. echo Options:
  238. echo    "-h  OR -?        Display Help"
  239. echo    "-o <filename>    Output Filename without extension"
  240. echo    "-mof <filename>  Mof Filename"
  241. goto :EOF
  242. rem *************** end of procedure HELP
  243.  
  244.  
  245. rem *************** start of procedure DISPLAY_ERROR_MESSAGE
  246. :DISPLAY_ERROR_MESSAGE
  247. echo Failed to convert the binary trace data to text format.
  248. echo Following reasons can cause this to happen:
  249. echo 1) The utility TraceFmt.exe is missing
  250. echo 2) The file %TRACEFILE% is either missing or corrupted
  251. echo 3) The file %MOFFILE% is either missing or corrupted
  252. echo The exact error message can be found in the file '%ERRORFILE%'
  253. goto :EOF
  254. rem *************** end of procedure DISPLAY_ERROR_MESSAGE
  255.  
  256.  
  257. rem /////////////////////////////////////////////////////////////////////////
  258. rem INIT procedure
  259. rem Must be called in local state before other procs are used
  260. rem
  261. :INIT
  262. %TRACE% [proc %0 %*]
  263.  
  264. goto :EOF
  265.  
  266. rem /////////////////////////////////////////////////////////////////////////
  267. rem VARDEL procedure
  268. rem Delete multiple variables by prefix
  269. rem
  270. rem Arguments:  %1=variable name prefix
  271. rem
  272. :VARDEL
  273. %TRACE% [proc %0 %*]
  274.     for /f "tokens=1 delims==" %%I in ('set %1 2^>nul') do set %%I=
  275. goto :EOF
  276.  
  277. rem /////////////////////////////////////////////////////////////////////////
  278. rem PARSECMDLINE procedure
  279. rem Parse a command line into switches and args
  280. rem
  281. rem Arguments:  CMDLINE=command text to parse
  282. rem     %1=0 for new parse (def) or 1 to append to existing
  283. rem
  284. rem Returns:    CMDARG_n=arguments, CMDSW_n=switches
  285. rem     CMDARGCOUNT=arg count, CMDSWCOUNT=switch count
  286. rem     RET=total number of args processed
  287. rem
  288. :PARSECMDLINE
  289. %TRACE% [proc %0 %*]
  290.     if not {%1}=={1} (
  291.         (call :VARDEL CMDARG_)
  292.         (call :VARDEL CMDSW_)
  293.         (set /a CMDARGCOUNT=0)
  294.         (set /a CMDSWCOUNT=0)
  295.     )
  296.     set /a RET=0
  297.     call :PARSECMDLINE1 %CMDLINE% 1>nul
  298.     set _MTPLIB_T1=
  299.     set _LASTARGSWITCH=0
  300.     set _LASTARGSWITCHNAME=0
  301. goto :EOF
  302. :PARSECMDLINE1
  303.     if {%1}=={} goto :EOF
  304.     set _MTPLIB_T1=%1
  305.     set _MTPLIB_T1=%_MTPLIB_T1:"=%
  306.     set /a RET+=1
  307.     shift /1
  308.     if "%_MTPLIB_T1:~0,1%"=="/" goto :PARSECMDLINESW
  309.     if "%_MTPLIB_T1:~0,1%"=="-" goto :PARSECMDLINESW
  310.     if "%_LASTARGSWITCH%"=="1" (
  311.         set CMDSW_%CMDSWCOUNT%=%_LASTARGSWITCHNAME%:%_MTPLIB_T1%
  312.         set _LASTARGSWITCH=0
  313.         goto :PARSECMDLINE1
  314.     )
  315.     set /a CMDARGCOUNT+=1
  316.     set CMDARG_%CMDARGCOUNT%=%_MTPLIB_T1%
  317.     set _LASTARGSWITCH=0
  318.     goto :PARSECMDLINE1
  319.     :PARSECMDLINESW
  320.     set /a CMDSWCOUNT+=1
  321.     set CMDSW_%CMDSWCOUNT%=%_MTPLIB_T1%
  322.     set _LASTARGSWITCH=1
  323.     set _LASTARGSWITCHNAME=%_MTPLIB_T1%
  324.     goto :PARSECMDLINE1
  325. goto :EOF
  326.  
  327. rem /////////////////////////////////////////////////////////////////////////
  328. rem GETARG procedure
  329. rem Get a parsed argument by index
  330. rem
  331. rem Arguments:  %1=argument index (1st arg has index 1)
  332. rem
  333. rem Returns:    RET=argument text or empty if no argument
  334. rem
  335. :GETARG
  336. %TRACE% [proc %0 %*]
  337.     set RET=
  338.     if %1 GTR %CMDARGCOUNT% goto :EOF
  339.     if %1 EQU 0 goto :EOF
  340.     if not defined CMDARG_%1 goto :EOF
  341.     set RET=%%CMDARG_%1%%
  342.     call :RESOLVE
  343. goto :EOF
  344.  
  345. rem /////////////////////////////////////////////////////////////////////////
  346. rem GETSWITCH procedure
  347. rem Get a switch argument by index
  348. rem
  349. rem Arguments:  %1=switch index (1st switch has index 1)
  350. rem
  351. rem Returns:    RET=switch text or empty if none
  352. rem     RETV=switch value (after colon char) or empty
  353. rem
  354. :GETSWITCH
  355. %TRACE% [proc %0 %*]
  356.     (set RET=) & (set RETV=)
  357.     if %1 GTR %CMDSWCOUNT% goto :EOF
  358.     if %1 EQU 0 goto :EOF
  359.     if not defined CMDSW_%1 goto :EOF
  360.     set RET=%%CMDSW_%1%%
  361.     call :RESOLVE
  362.     for /f "tokens=1* delims=:" %%I in ("%RET%") do (set RET=%%I) & (set RETV=%%J)
  363. goto :EOF
  364.  
  365. rem /////////////////////////////////////////////////////////////////////////
  366. rem FINDSWITCH procedure
  367. rem Finds the index of the named switch
  368. rem
  369. rem Arguments:  %1=switch name
  370. rem     %2=search start index (def: 1)
  371. rem
  372. rem Returns:    RET=index (0 if not found)
  373. rem     RETV=switch value (text after colon)
  374. rem
  375. :FINDSWITCH
  376. %TRACE% [proc %0 %*]
  377.     if {%2}=={} (set /a _MTPLIB_T4=1) else (set /a _MTPLIB_T4=%2)
  378.     :FINDSWITCHLOOP
  379.         call :GETSWITCH %_MTPLIB_T4%
  380.         if "%RET%"=="" (set RET=0) & (goto :FINDSWITCHEND)
  381.         if /i "%RET%"=="%1" (set RET=%_MTPLIB_T4%) & (goto :FINDSWITCHEND)
  382.         set /a _MTPLIB_T4+=1
  383.     goto :FINDSWITCHLOOP
  384.     :FINDSWITCHEND
  385.     set _MTPLIB_T4=
  386. goto :EOF
  387.  
  388. rem /////////////////////////////////////////////////////////////////////////
  389. rem MYFINDSWITCH procedure
  390. rem Finds the index of the named switch
  391. rem
  392. rem Arguments:  %1=switch name without the leading / or -
  393. rem     %2=search start index (def: 1)
  394. rem
  395. rem Returns:    RET=index (0 if not found)
  396. rem     RETV=switch value (text after colon)
  397. rem
  398. :MYFINDSWITCH
  399. %TRACE% [proc %0 %*]
  400.     if {%2}=={} (set /a _MTPLIB_T4=1) else (set /a _MTPLIB_T4=%2)
  401.     :MYFINDSWITCHLOOP
  402.         call :GETSWITCH %_MTPLIB_T4%
  403.         if "%RET%"=="" (set RET=0) & (goto :MYFINDSWITCHEND)
  404.         if /i "%RET:~1%"=="%1" (set RET=%_MTPLIB_T4%) & (goto :MYFINDSWITCHEND)
  405.         set /a _MTPLIB_T4+=1
  406.     goto :MYFINDSWITCHLOOP
  407.     :MYFINDSWITCHEND
  408.     set _MTPLIB_T4=
  409. goto :EOF
  410.  
  411. rem /////////////////////////////////////////////////////////////////////////
  412. rem REGSETM and REGSETU procedures
  413. rem Set registry values from variables
  414. rem
  415. rem Arguments:  %1=reg context (usually script name)
  416. rem     %2=variable to save (or prefix to save set of vars)
  417. rem
  418. :REGSETM
  419. %TRACE% [proc %0 %*]
  420.     for /f "tokens=1* delims==" %%I in ('set %2 2^>nul') do call :REGSET1 HKLM %1 %%I "%%J"
  421. goto :EOF
  422. :REGSETU
  423. %TRACE% [proc %0 %*]
  424.     for /f "tokens=1* delims==" %%I in ('set %2 2^>nul') do call :REGSET1 HKCU %1 %%I "%%J"
  425. goto :EOF
  426. :REGSET1
  427.     set _MTPLIB_T10=%4
  428.     set _MTPLIB_T10=%_MTPLIB_T10:\=\\%
  429.     reg add %1\Software\MTPScriptContexts\%2\%3=%_MTPLIB_T10% >nul
  430.     reg update %1\Software\MTPScriptContexts\%2\%3=%_MTPLIB_T10% >nul
  431. goto :EOF
  432.  
  433. rem /////////////////////////////////////////////////////////////////////////
  434. rem REGGETM and REGGETU procedures
  435. rem Get registry value or values to variables
  436. rem
  437. rem Arguments:  %1=reg context (usually script name)
  438. rem     %2=variable to restore (def: restore entire context)
  439. rem
  440. rem Returns:    RET=value of last variable loaded
  441. rem
  442. rem WARNING:    The "delims" value in the FOR commands below is a TAB
  443. rem     character, followed by a space. If this file is edited by
  444. rem     an editor which converts tabs to spaces, this procedure
  445. rem     will break!!!!!
  446. rem
  447. :REGGETM
  448. %TRACE% [proc %0 %*]
  449.     for /f "delims=  tokens=2*" %%I in ('reg query HKLM\Software\MTPScriptContexts\%1\%2 ^|find "REG_SZ"') do call :REGGETM1 %%I "%%J"
  450. goto :EOF
  451. :REGGETU
  452. %TRACE% [proc %0 %*]
  453.     for /f "delims=  tokens=2*" %%I in ('reg query HKCU\Software\MTPScriptContexts\%1\%2 ^|find "REG_SZ"') do call :REGGETM1 %%I "%%J"
  454. goto :EOF
  455. :REGGETM1
  456.     set _MTPLIB_T10=%2
  457.     set _MTPLIB_T10=%_MTPLIB_T10:\\=\%
  458.     set _MTPLIB_T10=%_MTPLIB_T10:"=%
  459.     set %1=%_MTPLIB_T10%
  460.     set RET=%_MTPLIB_T10%
  461. goto :EOF
  462.  
  463. rem /////////////////////////////////////////////////////////////////////////
  464. rem REGDELM and REGDELU procedures
  465. rem Delete registry values
  466. rem
  467. rem Arguments:  %1=reg context (usually script name)
  468. rem     %2=variable to delete (def: delete entire context)
  469. rem
  470. :REGDELM
  471. %TRACE% [proc %0 %*]
  472.     call :GETTEMPNAME
  473.     echo y >%RET%
  474.     reg delete HKLM\Software\MTPScriptContexts\%1\%2 <%RET% >nul
  475.     del %RET%
  476. goto :EOF
  477. :REGDELU
  478. %TRACE% [proc %0 %*]
  479.     call :GETTEMPNAME
  480.     echo y >%RET%
  481.     reg delete HKCU\Software\MTPScriptContexts\%1\%2 <%RET% >nul
  482.     del %RET%
  483. goto :EOF
  484.  
  485.  
  486. rem /////////////////////////////////////////////////////////////////////////
  487. rem SRAND procedure
  488. rem Seed the random number generator
  489. rem
  490. rem Arguments:  %1=new seed value
  491. rem
  492. :SRAND
  493. %TRACE% [proc %0 %*]
  494.     set /a _MTPLIB_NEXTRAND=%1
  495. goto :EOF
  496.  
  497. rem /////////////////////////////////////////////////////////////////////////
  498. rem RAND procedure
  499. rem Get next random number (0 to 32767)
  500. rem
  501. rem Returns:    RET=next random number
  502. rem
  503. :RAND
  504. %TRACE% [proc %0 %*]
  505.     if not defined _MTPLIB_NEXTRAND set /a _MTPLIB_NEXTRAND=1
  506.     set /a _MTPLIB_NEXTRAND=_MTPLIB_NEXTRAND * 214013 + 2531011
  507.     set /a RET=_MTPLIB_NEXTRAND ^>^> 16 ^& 0x7FFF
  508. goto :EOF
  509.  
  510. rem /////////////////////////////////////////////////////////////////////////
  511. rem RESOLVE procedure
  512. rem Fully resolve all indirect variable references in RET variable
  513. rem
  514. rem Arguments:  RET=value to resolve
  515. rem
  516. rem Returns:    RET=as passed in, with references resolved
  517. rem
  518. :RESOLVE
  519. %TRACE% [proc %0 %*]
  520.     :RESOLVELOOP
  521.         if "%RET%"=="" goto :EOF
  522.         set RET1=%RET%
  523.         for /f "tokens=*" %%I in ('echo %RET%') do set RET=%%I
  524.     if not "%RET%"=="%RET1%" goto :RESOLVELOOP
  525. goto :EOF
  526.  
  527. rem /////////////////////////////////////////////////////////////////////////
  528. rem GETINPUTLINE procedure
  529. rem Get a single line of keyboard input
  530. rem
  531. rem Returns:    RET=Entered line
  532. rem
  533. :GETINPUTLINE
  534. %TRACE% [proc %0 %*]
  535.     call :GETTEMPNAME
  536.     set _MTPLIB_T1=%RET%
  537.     copy con "%_MTPLIB_T1%" >nul
  538.     for /f "tokens=*" %%I in ('type "%_MTPLIB_T1%"') do set RET=%%I
  539.     if exist "%_MTPLIB_T1%" del "%_MTPLIB_T1%"
  540.     set _MTPLIB_T1=
  541. goto :EOF
  542.  
  543. rem /////////////////////////////////////////////////////////////////////////
  544. rem GETSYNCFILE procedure
  545. rem Get a sync file name (file will not exist)
  546. rem
  547. rem Returns:    RET=Name of sync file to use
  548. rem
  549. :GETSYNCFILE
  550. %TRACE% [proc %0 %*]
  551.     call :GETTEMPNAME
  552. goto :EOF
  553.  
  554. rem /////////////////////////////////////////////////////////////////////////
  555. rem SETSYNCFILE procedure
  556. rem Flag sync event (creates the file)
  557. rem
  558. rem Arguments:  %1=sync filename to flag
  559. rem
  560. :SETSYNCFILE
  561. %TRACE% [proc %0 %*]
  562.     echo . >%1
  563. goto :EOF
  564.  
  565. rem /////////////////////////////////////////////////////////////////////////
  566. rem DELSYNCFILE procedure
  567. rem Delete sync file
  568. rem
  569. rem Arguments:  %1=sync filename
  570. rem
  571. :DELSYNCFILE
  572. %TRACE% [proc %0 %*]
  573.     if exist %1 del %1
  574. goto :EOF
  575.  
  576. rem /////////////////////////////////////////////////////////////////////////
  577. rem WAITSYNCFILE
  578. rem Wait for sync file to flag
  579. rem
  580. rem Arguments:  %1=sync filename
  581. rem     %2=timeout in seconds (def: 60)
  582. rem
  583. rem Returns:    RET=Timeout remaining, or 0 if timeout
  584. rem
  585. :WAITSYNCFILE
  586. %TRACE% [proc %0 %*]
  587.     if {%2}=={} (set /a RET=60) else (set /a RET=%2)
  588.     if exist %1 goto :EOF
  589.     :WAITSYNCFILELOOP
  590.         sleep 1
  591.         set /a RET-=1
  592.     if %RET% GTR 0 if not exist %1 goto :WAITSYNCFILELOOP
  593. goto :EOF
  594.  
  595. rem /////////////////////////////////////////////////////////////////////////
  596. rem GETTEMPNAME procedure
  597. rem Create a temporary file name
  598. rem
  599. rem Returns:    RET=Temporary file name
  600. rem
  601. :GETTEMPNAME
  602. %TRACE% [proc %0 %*]
  603.     if not defined _MTPLIB_NEXTTEMP set /a _MTPLIB_NEXTTEMP=1
  604.     if defined TEMP (
  605.         (set RET=%TEMP%)
  606.     ) else if defined TMP (
  607.         (set RET=%TMP%)
  608.     ) else (set RET=%SystemRoot%)
  609.     :GETTEMPNAMELOOP
  610.         set /a _MTPLIB_NEXTTEMP=_MTPLIB_NEXTTEMP * 214013 + 2531011
  611.         set /a _MTPLIB_T1=_MTPLIB_NEXTTEMP ^>^> 16 ^& 0x7FFF
  612.         set RET=%RET%\~SH%_MTPLIB_T1%.tmp
  613.     if exist "%RET%" goto :GETTEMPNAMELOOP
  614.     set _MTPLIB_T1=
  615. goto :EOF
  616.  
  617. rem These must be the FINAL LINES in the script...
  618. :DOSEXIT
  619. echo This script requires Windows NT
  620.  
  621. rem /////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement