#50366 - 2000-06-27 08:58 PM
Compiler / formatter for Kix-scripts
|
kholm
Korg Regular
   
Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
|
[Moderator (Sealeopard): Closed topic]
Transform.kix
This script can be used to 'compile', format, 'encrypt' or compress other scripts.
Added support for Pseudo-encryption
Pseudo-encryption When Indent = 0, and KeepRemarks=N and KeepEmptyLines=N (Choices you make when executing Transform.kix) Transform.kix will enter pseudo-encryptionmode, lines can be until 500 characters long, each linefeed from original script is replaced by a random white space: Space, tab, or new line character.
The transformed script will be nearly impossible to read if it is not too small. You can still read the script, but average users will not be able to understand the script, and it will take a lot of effort to recreate it witout the original source, at least the remarks are lost.
The advantage of this form of 'encryption' is that it is time-costless when executing the script.
COMPILING 'Compile' dos not create an exe-file, but the script that is transformed is checked for syntax-errors.
The script will check that strings are correctly terminated, and check that loops and conditions are correctly terminated. (No missing If, EndIf, Select, EndSelect, While, Loop, Do and Until statements). Labels are checked for GoTo and GoSub statements
The script requires some coding diciplin in the script you want to compile.
Requirements:
Reserved words: - DO
- ELSE
- ENDIF
- SELECT
- ENDSELECT
- LOOP
Must be the only word in the line. (Can be followed by a remark) Strings must end in the line where they start. Meaning, if you have " as the first stringdelimitor " must be present later in this line, the same goes for the delimitor '
Tip: Best results are achieved, if you have only one statement in each line, but this is not required.
FORMATTING The script can also be used to format scripts you have made by cut and paste from this or other sites, same indent in entire script. If indents dos not exist this script will create them.
COMPRESSING A third way to use the script is to use it for compressing scripts, it can remove blank lines and comments, and remove indents (Indent = 0)
The script allways creates a new file with extension .kix, so if your script is named Logon.kix the script will create the transformed script Logon.kix.kix
CLEANING Lines will allways be stripped for leading and trailing spaces and tabs.
The script Transform.kix in this post is transformed (by itself) Logfile for this transform:
T R A N S F O R M L O G
Inputfile : Transform Outputfile : Transform.kix
OPTIONS USED Indent : 3 spaces Remarks kept Empty lines kept
Transform.kix successfully created, no errors found.
TIP Keep the script in the same folder as the script you want to transform, then you only have to enter the filename, otherwise you must enter path and filename
Examle log for a script containing errors
T R A N S F O R M L O G
Inputfile : TestError Outputfile : TestError.kix
OPTIONS USED Indent : Tabulator Remarks kept Empty lines removed
Error(s): Mismatching stringdelimitors in line 55 Missing If statement before line 119 Missing 1 EndIf statement(s) Subroutine (label) missing for GoSub GETXY in line 444
TestError.kix created with 4 Error(s) Correct errors in TESTERROR and transform again
Errors are easier to find in TESTERROR.KIX if you are using indents > 2 or indent = tabulator.
code:
; Script : Transform.kix ; Purpose: Check other scripts ; 'COMPILE' (Syntakschecking: Strings, labels, loops and conditions are checked) ; 'CLEANUP' (Remove leading and trailing spaces and tabs) ; Options: ; 'TRANSFORM' (Same indent in entire script) ; 'COMPRESS' (Remove remarks and blank lines) ; Version 1.7 June 30 2000 Erik Kærholm (Erik Kaerholm)
Break On
; Constants $CRLF = Chr(13) + Chr(10) $True = -1 $False = 0 $StrSep1 = "'" $StrSep2 = '"' $LogFile = %TEMP% + "\Transform.log"
; Labelstatistic-vars Dim $LabUsed[100] $LabUsedNum = 0
Dim $LabDefined[100] $LabDefinedNum = 0
Dim $SubCalled[100] $SubCalledNum = 0
; Initialize Color W+/b $IndentNbr = 0 $LineNum = 0 $ErrorsFound = $False $Compress = $False $LongLine = "" $ErrorNum = 0 $TotErr = 0 $Do = 0 $If = 0 $Select = 0 $While = 0
$Err = SetConsole("Maximize")
; *********************** Get user input ************************** Do CLS Box (1,1,3,78,"SINGLE") AT (2,21) "T R A N S F O R M S C R I P T"
AT (5,5) "Script to transform : " GetS $File
$Err = Open(1,"$File") Until $Err = 0
$OutFile = $File + ".kix" $Err = SetFileAttr($OutFile, 128) If $Err = 0 AT (7,5) "Overwrite " + $OutFile + " Y/N " Do Get $x If UCase($x) = "N" Quit EndIf Until Ucase($x) = "Y" "Y" EndIf
$Indent = "" AT (9,5) "Indent (T = Tabulator, 0..9 = number of spaces) : " Do Get $IndentIn $IndentIn = UCase($IndentIn) If $IndentIn = "T" $Indent = Chr(9) Else $Test = Val($IndentIn) If $Test > 0 $i = 0 While $i < $Test $Indent = $Indent + " " $i = $i + 1 Loop EndIf EndIf Until $Indent <> "" Or $IndentIn = 0 $IndentIn
AT (11,5) "Keep remarks Y/N " Do Get $x $x = UCase($x) Until ($x = "Y") Or ($x = "N") $x If $x = "Y" $KeepRemarks = $True Else $KeepRemarks = $False EndIf
AT (13,5) "Keep empty lines Y/N " Do Get $x $x = UCase($x) Until ($x = "Y") Or ($x = "N") $x If $x = "Y" $KeepEmptyLines = $True Else $KeepEmptyLines = $False EndIf
If $IndentIn = 0 And $KeepRemarks = $False And $KeepEmptyLines = $False $Compress = $True $Seed = Val(SubStr("@Time",7,2)) $Seed=$Seed+1 Do $x = Rnd(1) $Seed = $Seed - 1 Until $Seed=0 EndIf
AT (17,5) "Transforming ..........."
Del "$OutFile" $Err = Open(2,"$OutFile",5)
; ******************** Initiate logfile ************************* Del "$LogFile" $Err = Open(3,"$LogFile",5) $Err = WriteLine(3," T R A N S F O R M L O G" + $CRLF + $CRLF) $Err = WriteLine(3,"Inputfile : " + $File + $CRLF) $Err = WriteLine(3,"Outputfile : " + $OutFile + $CRLF + $CRLF) $Err = WriteLine(3,"OPTIONS USED" + $CRLF) $Err = WriteLine(3," Indent : ")
$IndentIn = "" + $IndentIn Select Case $IndentIn = "T" $Err = WriteLine(3,"Tabulator" + $CRLF) Case $IndentIn = "0" $Err = WriteLine(3,"NONE" + $CRLF) Case 1 $Err = WriteLine(3,$IndentIn + " spaces" + $CRLF) EndSelect
If $KeepRemarks $Err = WriteLine(3," Remarks kept" + $CRLF) Else $Err = WriteLine(3," Remarks removed" + $CRLF) EndIf
If $KeepEmptyLines $Err = WriteLine(3," Empty lines kept" + $CRLF + $CRLF) Else $Err = WriteLine(3," Empty lines removed" + $CRLF + $CRLF) EndIf
; ************************ Main loop **************************** $Line = ReadLine(1) While @Error = 0 GoSub Status $LineNum = $LineNum + 1 GoSub WriteOutLine $Line = ReadLine(1) Loop
If $Compress $Err = WriteLine(2, "$LongLine") EndIf
; ****************** Complete and show log ********************** At(17,30) " " ; Remove statussymbol
If $IndentNbr > 0 If $Do > 0 $TotErr = $TotErr + $Do $ErrorNum = $Do $ErrorType = "Until" GoSub ErrorHandler EndIf If $If > 0 $TotErr = $TotErr + $If $ErrorNum = $If $ErrorType = "EndIf" GoSub ErrorHandler EndIf If $Select > 0 $TotErr = $TotErr + $Select $ErrorNum = $Select $ErrorType = "EndSelect" GoSub ErrorHandler EndIf If $While > 0 $TotErr = $TotErr + $While $ErrorNum = $While $ErrorType = "Loop" GoSub ErrorHandler EndIf EndIf
GoSub CheckGoSub GoSub CheckGoTo
$Err = WriteLine(3,$CRLF)
If $ErrorsFound $Err = WriteLine(3,$OutFile + " created with " + $TotErr + " Error(s)" + $CRLF) $Err = WriteLine(3,"Correct errors in " + UCase($File) + " and transform again" + $CRLF + $CRLF) $Err = WriteLine(3,"Errors are easier to find in " + UCase($OutFile) + " if you are using" + $CRLF) $Err = WriteLine(3,"indents > 2 or indent = tabulator.") Else $Err = WriteLine(3,$OutFile + " successfully created, no errors found.") EndIf
$Err = Close(1) $Err = Close(2) $Err = Close(3)
Run "Start /Max NotePad.exe $LogFile"
Return ; End script Transform.kix
;*************** :WriteOutLine ;***************
; Remove leading spaces and tabs While (SubStr($Line,1,1) = " ") Or (SubStr($Line,1,1) = Chr(9)) $Line = SubStr($Line, 2, Len($Line) - 1) Loop
; Remove trailing spaces and tabs While (SubStr($Line,Len($Line),1) = " ") Or (SubStr($Line,Len($Line),1) = Chr(9)) $Line = SubStr($Line, 1, Len($Line) - 1) Loop
; Split code and remarks $TmpLine = $Line $PosRem = 0 GoSub GetPosRem If $PosRem = 0 $TmpLine = $Line GoSub TestStrings EndIf $FrontRem = "" If $PosRem > 0 If $PosRem > 1 $FrontRem = $Indent EndIf $Remark = SubStr($Line, $PosRem , Len($Line) - $PosRem + 1) $Code = SubStr($Line, 1, $PosRem - 1) Else $Remark = "" $Code = $Line EndIf
; Remove trailing spaces and tabs from code While (SubStr($Code,Len($Code),1) = " ") Or (SubStr($Code,Len($Code),1) = Chr(9)) $Code = SubStr($Code, 1, Len($Code) - 1) Loop
; Check if line is empty If $Code = "" And $Remark = "" $LineEmpty = $True Else $LineEmpty = $False EndIf
; Check for reserved, words expected to be entire code $PendingIndent = 0 $NotChecked = $False $Test = UCase($Code) Select Case $Test = "DO" $PendingIndent = 1 $Do = $Do + 1 Case $Test = "ELSE" $PendingIndent = 1 $IndentNbr = $IndentNbr - 1 Case $Test = "ENDIF" If $If > 0 $IndentNbr = $IndentNbr - 1 $If = $If - 1 Else $ErrorType = "If" GoSub ErrorHandler EndIf Case $Test = "SELECT" $Select = $Select + 1 $PendingIndent = 1 $CasePrev = $False Case $Test = "ENDSELECT" If $Select > 0 $IndentNbr = $IndentNbr - 2 $Select = $Select - 1 Else $ErrorType = "Select" GoSub ErrorHandler EndIf Case $Test = "LOOP" If $While > 0 $IndentNbr = $IndentNbr - 1 $While = $While - 1 Else $ErrorType = "While" GoSub ErrorHandler EndIf Case 1 $NotChecked = $True EndSelect
; Check for reserved words expected to be part of code If $NotChecked If SubStr($Test, 1, 3) = "IF " If SubStr($Test, Len($Test) - 5, 6) <> " ENDIF" $PendingIndent = 1 $If = $If + 1 EndIf Else If SubStr($Test, 1, 5) = "CASE " $PendingIndent = 1 If $CasePrev $IndentNbr = $IndentNbr - 1 Else $CasePrev = $True EndIf Else If SubStr($Test, 1, 6) = "UNTIL " If $Do > 0 $IndentNbr = $IndentNbr - 1 $Do = $Do - 1 Else $ErrorType = "Do" GoSub ErrorHandler EndIf Else If SubStr($Test, 1, 6) = "WHILE " $PendingIndent = 1 $While = $While + 1 Else If SubStr($Test, 1, 6) = "GOSUB " $LabName = Ltrim(RTrim(SubStr($Test, 7, Len($Test) - 6))) GoSub SubInfo Else If SubStr($Test, 1, 5) = "GOTO " $LabName = Ltrim(RTrim(SubStr($Test, 6, Len($Test) - 5))) GoSub LabInfo Else If SubStr($Test, 1, 1) = ":" $LabName = SubStr($Test, 2, Len($Test) - 1) GoSub LabDefined EndIf EndIf EndIf EndIf EndIf EndIf EndIf EndIf
; *********************** Create line *************************** If $Code <> "" $i = 0 $Front = "" While $i < $IndentNbr $Front = $Front + $Indent $i = $i + 1 Loop $Line = $Front + $Code Else $Line = "" EndIf
If ($Remark <> "") And $KeepRemarks If $Line <> "" $Line = $Line + $FrontRem EndIf $Line = $Line + $Remark EndIf
If $Line <> "" If $Compress $Rnd = Rnd(34) Select Case $Rnd < 20 $Del = Chr(9) Case $Rnd < 33 $Del = " " Case 1 $Del = Chr(10) EndSelect $LongLine = $LongLine + $Del + $Line If Len($LongLine) > 500 $LineShift = $True $Line = $LongLine $LongLine = "" Else $LineShift = $False EndIf Else $LineShift = $True EndIf EndIf
If $LineShift If $Line <> "" $Line = $Line + $CRLF Else If $KeepEmptyLines And $LineEmpty $Line = $CRLF EndIf EndIf $Err = WriteLine(2, "$Line") EndIf
$IndentNbr = $IndentNbr + $PendingIndent
Return ; *END SUB* WriteOutLine
;************** :GetPosRem ;************** $TestRem = InStr($TmpLine, ";") If $TestRem > 0 $TstStr1 = InStr($TmpLine, $StrSep1) If $TstStr1 = 0 $TstStr1 = 100000 EndIf $TstStr2 = InStr($TmpLine, $StrSep2) If $TstStr2 = 0 $TstStr2 = 100000 EndIf If $TstStr1 < $TstStr2 $StrSep = $StrSep1 $StartStr = $TstStr1 Else $StrSep = $StrSep2 $StartStr = $TstStr2 EndIf If $StartStr = 100000 $StartStr = 0 EndIf If $StartStr < $TestRem And $StartStr > 0 $PosRem = $PosRem + $StartStr $TmpLine = SubStr($TmpLine, $StartStr + 1, Len($TmpLine) - $StartStr) $TestRem = InStr($TmpLine, ";") $TestEnd = InStr($TmpLine, $StrSep) If $TestEnd > 0 If $TestEnd > $TestRem $PosRem = $PosRem + $TestEnd $TmpLine = SubStr($TmpLine, $TestEnd + 1, Len($TmpLine) - $TestEnd) GoSub GetPosRem Else $PosRem = $PosRem + $TestRem EndIf Else If $ErrorsFound = $False $Err = WriteLine(3,"Error(s):" + $CRLF) EndIf $TotErr = $TotErr + 1 $ErrorsFound = $True $Err = WriteLine(3," Mismatching stringdelimitors in line " + $LineNum + $CRLF) $PosRem = 0 $TmpLine = "" EndIf Else $PosRem = $PosRem + InStr($TmpLine, ";") Endif Else $PosRem = 0 EndIf
Return ;*END SUB* GetPosRem
;************** :TestStrings ;************** $TstStr1 = InStr($TmpLine, $StrSep1) If $TstStr1 = 0 $TstStr1 = 100000 EndIf $TstStr2 = InStr($TmpLine, $StrSep2) If $TstStr2 = 0 $TstStr2 = 100000 EndIf If $TstStr1 < $TstStr2 $StrSep = $StrSep1 $StartStr = $TstStr1 Else $StrSep = $StrSep2 $StartStr = $TstStr2 EndIf If $StartStr = 100000 $StartStr = 0 EndIf If $StartStr > 0 $TmpLine = SubStr($TmpLine, $StartStr + 1, Len($TmpLine) - $StartStr) $TestEnd = InStr($TmpLine, $StrSep) If $TestEnd > 0 $TmpLine = SubStr($TmpLine, $TestEnd + 1, Len($TmpLine) - $TestEnd) GoSub TestStrings Else If $ErrorsFound = $False $Err = WriteLine(3,"Error(s):" + $CRLF) EndIf $TotErr = $TotErr + 1 $ErrorsFound = $True $Err = WriteLine(3," Mismatching stringdelimitors in line " + $LineNum + $CRLF) $TmpLine = "" EndIf EndIf
Return ; *END SUB* TestStrings
;************** :Status ;************** $s = $s + 1 Select Case $s = 1 $Ch = "|" Case $s = 2 $Ch = "/" Case $s = 3 $Ch = "-" Case 1 $Ch = "\" $s = 0 EndSelect At(17,30) $Ch
Return ; *END SUB* Status
;************** :ErrorHandler ;************** If $ErrorsFound = $False $Err = WriteLine(3,"Error(s):" + $CRLF) EndIf If $ErrorNum > 0 $Err = WriteLine(3," Missing " + $ErrorNum + " " + $ErrorType + " statement(s)" + $CRLF) Else $TotErr = $TotErr + 1 $Err = WriteLine(3," Missing " + $ErrorType + " statement before line " + $LineNum + $CRLF) EndIf $ErrorsFound = $True
Return ; *END SUB* ErrorHandler
;************** :SubInfo ;************** If InStr($LabName, $StrSep1) Or InStr($LabName, $StrSep2) $LabName = SubStr($LabName, 2, Len($LabName) - 2) EndIf $SubCalledNum = $SubCalledNum + 1 $SubCalled[$SubCalledNum] = $LabName + "," + $LineNum
Return ; *END SUB* SubInfo
;************** :LabInfo ;************** If InStr($LabName, $StrSep1) Or InStr($LabName, $StrSep2) $LabName = SubStr($LabName, 2, Len($LabName) - 2) EndIf $LabUsedNum = $LabUsedNum + 1 $LabUsed[$LabUsedNum] = $LabName + "," + $LineNum
Return ; *END SUB* LabInfo
;************** :LabDefined ;************** Dim $i, $NotFound $i = 1 $NotFound = $True While $i <= $LabDefinedNum And $NotFound If $LabName = $LabDefined[$i] $NotFound = $False EndIf $i = $i + 1 Loop If $NotFound $LabDefinedNum = $LabDefinedNum + 1 $LabDefined[$LabDefinedNum] = $LabName EndIf
Return ; *END SUB* LabDefined
;************** :CheckGoTo ;************** Dim $i, $j, $Pos, $Len, $NotFound, $TestName, $LineNum, $LabU $i = 1 While $i <= $LabUsedNum $LabU = $LabUsed[$i] $Pos = InStr($LabU, ",") $TestName = SubStr($LabU, 1, $Pos - 1) $NotFound = $True $j = 1 While $j <= $LabDefinedNum And $NotFound If $TestName = $LabDefined[$j] $NotFound = $False EndIf $j = $j + 1 Loop If $NotFound If $ErrorsFound = $False $Err = WriteLine(3,"Error(s):" + $CRLF) EndIf $TotErr = $TotErr + 1 $Len = Len($LabU) $LineNum = SubStr($LabU, $Pos + 1, $Len - $Pos) $Err = WriteLine(3," Label missing for GoTo " + $TestName + " in line " + $LineNum + $CRLF) $ErrorsFound = $True EndIf $i = $i + 1 Loop
Return ; *END SUB* CheckGoTo
;************** :CheckGoSub ;************** Dim $i, $j, $Pos, $Len, $NotFound, $TestName, $LineNum, $SubC $i = 1 While $i <= $SubCalledNum $SubC = $SubCalled[$i] $Pos = InStr($SubC, ",") $TestName = SubStr($SubC, 1, $Pos - 1) $NotFound = $True $j = 1 While $j <= $LabDefinedNum And $NotFound If $TestName = $LabDefined[$j] $NotFound = $False EndIf $j = $j + 1 Loop If $NotFound If $ErrorsFound = $False $Err = WriteLine(3,"Error(s):" + $CRLF) EndIf $TotErr = $TotErr + 1 $Len = Len($SubCalled[$i]) $LineNum = SubStr($SubCalled[$i], $Pos + 1, $Len - $Pos) $Err = WriteLine(3," Subroutine (label) missing for GoSub " + $TestName + " in line " + $LineNum + $CRLF) $ErrorsFound = $True EndIf $i = $i + 1 Loop
Return ; *END SUB* CheckGoSub
[This message has been edited by kholm (edited 30 June 2000).] [ 18. October 2003, 02:32: Message edited by: sealeopard ]
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 293 anonymous users online.
|
|
|