home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / indtr100.zip / Indenter.cmd next >
OS/2 REXX Batch file  |  1999-12-07  |  4KB  |  148 lines

  1. /************************************************************************/
  2. /*                                                                      */
  3. /*   Program: Indentr.cmd                                               */
  4. /*                                                                      */
  5. /*   Purpose: Properly indents REXX programs                            */
  6. /*                                                                      */
  7. /*                                                                      */
  8. /* Arguments: Filename.cmd                                              */
  9. /*                                                                      */
  10. /*   Returns: Modified file (original in *.bak)                         */
  11. /*                                                                      */
  12. /* Copyright: 1999 by J R Casey Bralla                                  */
  13. /*            http://www.NerdWorld.org/indenter.html                    */
  14. /*                                                                      */
  15. /*            See ReadMe.Now for additional info                        */
  16. /*                                                                      */
  17. /*            See Shamelss.plg for a shameless advertisement            */
  18. /*                                                                      */
  19. /************************************************************************/
  20.  
  21. Parse Arg SourceFileName
  22.  
  23.  
  24.   FileName = Left(SourceFileName, Length(SourceFileName)-4)
  25.  
  26. DestinationFileName = FileName || ".$$$"
  27.  
  28.  
  29.  
  30.  
  31. Indent = 0              /* Running Indention amount */
  32. IndentAmount = 5        /* The number of spaces to indent each level */
  33.  
  34.  
  35.  
  36.  
  37. /* Read in Source File Line by Line */
  38.  
  39. Do While Lines(SourceFileName)
  40.  
  41.   LineToWorkOn = LineIn(SourceFileName)
  42.  
  43.  
  44.   /* Strip away the comments temporarily */
  45.   /* This will force the formatting to ignore any comments */
  46.   CommentPosition = Pos("/*", LineToWorkOn)  /* */
  47.   CommentLine = ""
  48.   If CommentPosition > 0 Then Do
  49.     CommentPosition = CommentPosition - 1
  50.     CommentLine = Right(LineToWorkOn, Length(LineToWorkOn)- CommentPosition)
  51.     LineToWorkOn = Left(LineToWorkOn, CommentPosition)
  52.   End
  53.  
  54.  
  55.   /* Temporarily pad extra spaces on front and rear of line */
  56.   LineToWorkOn = "   " || LineToWorkOn || " "
  57.  
  58.  
  59.  
  60.   /* Check for Specific Keywords */
  61.  
  62.   /* "Do" Keyword */
  63.   DoPosition = LastPos("Do", LineToWorkOn)
  64.  
  65.   If DoPosition > 0 Then Do
  66.  
  67.     /* Check to be sure these words aren't buried inside another word */
  68.     If Substr(LineToWorkOn, DoPosition + 2 ,1) \= " "  | Substr(LineToWorkOn, DoPosition - 1 ,1) \= " " Then Do
  69.       DoPosition = 0
  70.     End
  71.  
  72.   End
  73.  
  74.  
  75.   /* "End" Keyword */
  76.   EndPosition = Pos("End", LineToWorkOn)
  77.  
  78.   If EndPosition > 0 Then Do
  79.  
  80.     /* Check to be sure these words aren't buried inside another word */
  81.     If Substr(LineToWorkOn, EndPosition + 3 ,1) \= " "  | Substr(LineToWorkOn, EndPosition - 1 ,1) \= " " Then Do
  82.     EndPosition = 0
  83.     End
  84.   End
  85.  
  86.   /* Strip Leading and trailing spaces */
  87.   LineToWorkOn = Strip(LineToWorkOn,"Leading")
  88.   LineLength = Length(LineToWorkOn)
  89.   If LineLength = 0 Then LineToWorkOn = " "  /* Always have at least one space */
  90.   LineToWorkOn =  Left(LineToWorkOn, ( Length(LineToWorkOn) - 1) ) /* Strip trailing space */
  91.  
  92.  
  93.  
  94.   /* Add back in the comment portion */
  95.   LineToWorkOn = LineToWorkOn || CommentLine
  96.  
  97.   /* Rewrite the Line, incorporating the indention count */
  98.   LineToWrite = Copies(" ",Indent) || LineToWorkOn
  99.   Call LineOut DestinationFileName, LineToWrite
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.   /* Adjust Indention based on keywords */
  107.  
  108.   If  DoPosition > 0 Then Indent = Indent + IndentAmount
  109.   If EndPosition > 0 Then Indent = Indent - IndentAmount
  110.  
  111.   If Indent < 0 Then Do
  112.     Say "Error:  Indent less than Zero"
  113.     Call Beep 450, 750
  114.     /* Exit */
  115.     Indent = 0
  116.     Call LineOut DestinationFileName, "/*  Error!   Indentation less than Zero! */"
  117.     End
  118.  
  119.  
  120.  
  121. End
  122.  
  123.  
  124.  
  125. Call Stream DestinationFileName, "Command", "Close"
  126. Call Stream      SourceFileName, "Command", "Close"
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. /* Delete the old backup file */
  134.  
  135. "@IF EXIST " || FileName || ".bak DEL " || FileName || ".bak"
  136.  
  137.  
  138. /* Rename the *.cmd file to *.bak" */
  139.  
  140. "@REN " || SourceFileName || " " || FileName || ".bak"
  141.  
  142.  
  143. /* Rename the *.$$$ file to *.cmd */
  144.  
  145. "@REN " || DestinationFileName || " " || FileName || ".cmd"
  146.  
  147. Return
  148.