;
; Author: Antonio SERRANO
; Program: COMPR.KIX; Description:
; ------------
; >> When we put the scripts in production, sometimes we'll desire to reduce its size.
; I comment a lot my scripts but when the directors and other people want to connect via RAS
; they found the login script is to slowly to charge the scripts.
; If we reduce the script size we'll improve (a little bit) its speed; however, it's better
; to improve the algorithm or to start KIX32.EXE directly rather than LMSCRIPT.EXE.
;
; >> Another times we can desire to drop all the debugging information without to modifie
; the original program.
;
; How we can reduce the script file size?.
; -- No comments and no superfluous spaces.
; -- No debugging information.
; I have writen a PARSEC to eliminate the superfluous code of a KiXtart script.
; It drops comments from the source file too.
;
; >> This script does that and IT'S READY TO USE.
;
;
; I hope you find this script useful.
; Please, send me any suggestion or improvement.
;
; ---------------------
; Modifications review:
; ---------------------
; v.00.01 (07/08/1999)
; - It changes the TAB by four spaces.
; - It drops the comments between ';' until LF.
; - It drops the spaces, on the left between LF to find a non-space character.
; - It drops the initial spaces.
; - It drops the isolates LF.
; - It shows two different progress indicators.
; v.01.00 (12/23/1999)
; - Adaptation to 3.62 KiXtart version.
; - All the code between ';[' and ';]' are comments. It permits nested comments.
;
; -----------------------------------------------------------------------------
; Variables
; -----------------------------------------------------------------------------
;$FileIN is an argument
;$FileOUT is optional
$LF=CHR(13)+CHR(10) ; Line feed
; Show or not the compacted file. By default show the output lines
;$ShowFile=0 ; Show points
;$ShowFile=1 ; Show the outpup lines
IF 1=$ShowFile ELSE $ShowFile=0 ENDIF
$NTABSpaces=" " ; Number of spaces by TAB
$Output="" ; Output string null
$NLineIN=0 ; Number of lines into the original file
$NLineOUT=0 ; Number of lines into the compacted file
$nc=0 ; Nested comments level
; -----------------------------------------------------------------------------
; Check the parameters
; -----------------------------------------------------------------------------
IF $FileIN ; Check if the input file exist
IF EXIST($FileIN)
$FileIN=UCASE($FileIN)
GOTO CHECKEXT
ELSE
?"$FileIN doesn't exist."
GOTO USAGE
ENDIF
ELSE GOTO USAGE
ENDIF
; Get the input file extension to check if it is a KiXtart file
:CHECKEXT
$ext=UCASE(SUBSTR($FileIN,LEN($FileIN)-2,3))
IF "KIX"<>$ext AND "SCR"<>$ext
FLUSHKB ; Flush any user keystroke
COLOR r+/n
?"The file you want to compact doesn't have a Kixtart extension."
?"If you choose to continue perhaps the results should be undesirable."
COLOR n/n
?"Do you want to continue?. [Y/N]"
DO
GET $op $op=UCASE($op)
UNTIL "Y"=$op OR "N"=$op
IF "N"=$op GOTO BYE ENDIF
ENDIF
; Check the output file
:CHECKOUT
IF $FileOUT
IF EXIST($FileOUT)
; Get the response
FLUSHKB ; Flush any user keystroke
COLOR r+/n ?"The output file exists." COLOR n/n
?"Do you want to overwrite it?. [Y/N] "
DO
GET $op $op=UCASE($op)
UNTIL "Y"=$op OR "N"=$op
"$op" ? ; Show the answer
IF "Y"=$op ; Drop the old file
"Please wait ..." ?
DEL $FileOUT
GOTO BEGIN
ELSE GOTO BYE
ENDIF
ELSE GOTO BEGIN
ENDIF
ELSE ; Search the last '.' and add 'CMP' extension
$FileOUT=UCASE(SUBSTR($FileIN,1,LEN($FileIN)-3))+"CMP"
GOTO CHECKOUT
ENDIF
; -----------------------------------------------------------------------------
; Usage
; -----------------------------------------------------------------------------
:USAGE
COLOR g+/n
$x="----------------------------------"
?$x ?"Script to compact KiXtart Scripts." ?$x
COLOR n/n
?"USAGE: KIX32 COMPR "+CHR(36)+"FileIN="+CHR(34)+"<FileNameIN>"+CHR(34)+" ["+CHR(36)+"ShowFile=<1|0>]"
?" KIX32 COMPR "+CHR(36)+"FileIN="+CHR(34)+"<FileNameIN>"+CHR(34)+" ["+CHR(36)+"FileOUT="+chr(34)+"<FileNameOUT>"+CHR(34)+"]$LF ["+CHR(36)+"ShowFile=<1|0>]"
GOTO BYE
; -----------------------------------------------------------------------------
; Open the input and output files and check for errors
; -----------------------------------------------------------------------------
:BEGIN ; Show a initial message
COLOR g+/n ? ?"Compacting KiXtart file." COLOR n/n
?"Please, wait for a while ..." ?
IF OPEN(9,$FileOUT,5)
?"Error opening $FileOUT"
GOTO BYE
ENDIF
IF OPEN(8,$FileIN)
?"Error opening $FileIN"
GOTO BYE
ENDIF
; -----------------------------------------------------------------------------
; PARSEC
; -----------------------------------------------------------------------------
:INPUT ; Preprocessing a line
IF $ShowFile ? ENDIF
;
; Writing compacted line
IF $Output
$NLineOUT=$NLineOUT+1
$Aux=WRITELINE(9,$Output+CHR(13)+CHR(10))
ENDIF
; Initialize vars.
$Output="" ; Output string null
$Input=READLINE(8) ; Read new line
; Check condition end of script
IF @ERROR GOTO END ENDIF
$LInput=LEN($Input) ; Length of line to calculate the end
$NLineIN=$NLineIN+1 ; Line being processed now
; Show the compressed line or an indicator
IF $ShowFile
"Line $NLineIN("
IF $nc COLOR g+/n $nc COLOR n/n "):"
ELSE "$nc):"
ENDIF
ELSE
$Aux=$NLineIN/10
IF $Aux*10=$NLineIN
"."
IF ($Aux/80)*80=$Aux ? ENDIF
ENDIF
ENDIF
$i=0
;
; STATE (1)
;
:V00S1
$i=$i+1 ; Counter character in each line
$CharIN=SUBSTR($Input,$i,1)
SELECT
CASE CHR(32)=$CharIN OR CHR(9)=$CharIN ; " " OR TAB
GOTO V00S1
CASE CHR(59)=$CharIN ; ";"
$x=SUBSTR($Input,$i+1,1)
IF '['=$x $nc=$nc+1 ENDIF
IF ']'=$x
$nc=$nc-1
IF 0>$nc GOTO ERRORNESTED ENDIF
ENDIF
GOTO INPUT
CASE CHR(34)=$CharIN ; """
IF 0=$nc
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
ENDIF
GOTO V00S2
CASE CHR(39)=$CharIN ; "'"
IF 0=$nc
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
ENDIF
GOTO V00S3
CASE $LInput<$i ; NEWLINE
GOTO INPUT
CASE 1 ; Other character
IF 0=$nc
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
ENDIF
GOTO V00S4
ENDSELECT
;
; STATE (2)
;
:V00S2
$i=$i+1 ; Counter character in each line
$CharIN=SUBSTR($Input,$i,1)
SELECT
CASE CHR(34)=$CharIN ; """
IF 0=$nc
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
ENDIF
GOTO V00S1
CASE CHR(9)=$CharIN ; TAB
IF 0=$nc
$Output=$Output+$NTABSpaces ; Replace TAB by spaces
IF $ShowFile $NTABSpaces ENDIF
ENDIF
GOTO V00S2
CASE 1 ; Other character
IF 0=$nc
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
ENDIF
GOTO V00S2
ENDSELECT
;
; STATE (3)
;
:V00S3
$i=$i+1 ; Counter character in each line
$CharIN=SUBSTR($Input,$i,1)
SELECT
CASE CHR(39)=$CharIN ; "'"
IF 0=$nc
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
ENDIF
GOTO V00S1
CASE CHR(9)=$CharIN ; TAB
IF 0=$nc
$Output=$Output+$NTABSpaces ; Replace TAB by spaces
IF $ShowFile $NTABSpaces ENDIF
ENDIF
GOTO V00S3
CASE 1 ; Other character
IF 0=$nc
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
ENDIF
GOTO V00S3
ENDSELECT
;
; STATE (4)
;
:V00S4
$i=$i+1 ; Counter character in each line
$CharIN=SUBSTR($Input,$i,1)
SELECT
CASE CHR(32)=$CharIN ; " "
$CharINAux=SUBSTR($Input,$i+1,1)
IF CHR(32)<>$CharINAux AND CHR(9)<>$CharINAux AND CHR(59)<>$CharINAux AND $CharINAux AND 0=$nc
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
ENDIF
GOTO V00S5
CASE CHR(9)=$CharIN ; TAB
$CharINAux=SUBSTR($Input,$i+1,1)
IF CHR(32)<>$CharINAux AND CHR(9)<>$CharINAux AND CHR(59)<>$CharINAux AND $CharINAux AND 0=$nc
$Output=$Output+$NTABSpaces
IF $ShowFile $NTABSpaces ENDIF
ENDIF
GOTO V00S5
CASE CHR(34)=$CharIN ; """
IF 0=$nc
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
ENDIF
GOTO V00S2
CASE CHR(39)=$CharIN ; "'"
IF 0=$nc
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
ENDIF
GOTO V00S3
CASE $LInput<$i ; NEWLINE
GOTO INPUT
CASE 1 ; Other character
IF 0=$nc
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
ENDIF
GOTO V00S4
ENDSELECT
;
; STATE (5)
;
:V00S5
$i=$i+1 ; Counter character in each line
$CharIN=SUBSTR($Input,$i,1)
SELECT
CASE CHR(32)=$CharIN OR CHR(9)=$CharIN ; " " or TAB
GOTO V00S5
CASE CHR(34)=$CharIN ; """
IF 0=$nc
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
ENDIF
GOTO V00S2
CASE CHR(39)=$CharIN ; "'"
IF 0=$nc
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
ENDIF
GOTO V00S3
CASE $LInput<$i ; NEWLINE
GOTO INPUT
CASE CHR(59)=$CharIN ; ";" (same than V00S1)
$i=$i-1
GOTO V00S1
CASE 1 ; Other character
IF 0=$nc
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
ENDIF
GOTO V00S4
ENDSELECT
; -----------------------------------------------------------------------------
; End the script
; -----------------------------------------------------------------------------
:END
; Close the open files
$Aux=CLOSE(9) ; File out
$Aux=CLOSE(8) ; File in
; Show statistics information
?
$x="**********************************"
?"$x STATISTICS $x" ?
?" $FileIN lines number: $NlineIN"
?" $FileOUT lines number: $NlineOUT"
?" $FileIN size: "GETFILESIZE($FileIN)
?" $FileOUT size: "GETFILESIZE($FileOUT) ?
?"$x************$x"
:BYE
?"End of script."
?"Have a nice day!." ? ?
EXIT 1
; -----------------------------------------------------------------------------
; Subroutines
; -----------------------------------------------------------------------------
:ERRORNESTED
COLOR r+/n ?"Error on nested level of comments." COLOR n/n
?"Please, check your source code."
EXIT 1
------------------