/* CompactRX 1.01 (18.07.97) by NDY's

Removes all comments (exept first line), "PROC"s and leading spaces from an Arexx program.
Also removes all lines enclosed by "/* BEGIN_DEBUG */" and "/* END_DEBUG */" (debug code).

Bugs: Won't work correctly if the comment "brackets" /* and */ are part of a string inside
      the program (for instance it would produce wrong code when processing itself)

PROC main: */
  PARSE ARG filename
  IF ~Exists(filename) THEN
    DO
      SAY "No such file!"
      EXIT 5
    END
  in="InFile"
  out="OutFile"
  ok=Open(in,filename,"R")
  IF ~ok THEN
    DO
      SAY "Couln't open input file!"
      EXIT 5
    END
  ok=Open(out,filename||".trim","W")
  IF ~ok THEN
    DO
      SAY "Couln't open output file!"
      EXIT 5
    END
  i=1
  first=1
  ret=0
  comment=0
  debug=0
  SAY ""
  DO WHILE ~Eof(in)
    line=ReadLn(in)
    IF (i//50)=0 THEN SAY "0B0D"x||"Line" i
    IF line~="" THEN line=Strip(rembad(line))
    IF line~="" THEN
      IF first THEN
        DO
          IF Left(line,2)~="/*" THEN
            DO
              SAY "0B0D"x||"No Arexx program!"
              ret=5
              LEAVE
            END
          IF Pos("*/",line)=0 THEN
            DO
              line=line "*/"
              comment=1
            END
          first=0
        END
      ELSE
        DO
          IF debug=0 THEN
            IF line="/* BEGIN_DEBUG */" THEN
              DO
                debug=1
                line=""
              END
            ELSE
              DO
                IF comment THEN
                  IF Pos("*/",line)>0 THEN
                    DO
                      line=SubStr(line,Pos("*/",line)+2)
                      comment=0
                    END
                  ELSE
                    line=""
                IF Left(line,5)="PROC " THEN line=SubStr(line,6)
                start=1
                beg=Pos("/*",line)
                DO WHILE line~="" & beg>0 & ~comment
                  fin=Pos("*/",line,beg+2)
                  line=Left(line,beg-1)
                  IF fin>0 THEN
                    line=line||SubStr(line,fin+2)
                  ELSE
                    comment=1
                  start=beg
                  beg=Pos("/*",line,start)
                END
              END
           ELSE
             DO
               IF line="/* END_DEBUG */" THEN debug=0
               line=""
             END
        END
    IF line~="" THEN
      IF WriteLn(out,line)~=Length(line)+1 THEN
        DO
          SAY "0B0D"x||"Error while writing!"
          ret=5
          LEAVE
        END
    i=i+1
  END
  Close(in)
  Close(out)
  IF ret=0 THEN SAY "0B0D"x||"Operation successfully completed."
  EXIT ret
RETURN
PROC rembad: PROCEDURE /* newstr=rembad(str) */
  /* Replace unprintable characters by spaces */
  PARSE ARG t
  bad=XRange("00"x,"1F"x)||XRange("7F"x,"A0"x)
  i=Verify(t,bad,"m")
  l=Length(t)
  DO WHILE i>0
    t=Left(t,i-1) Right(t,l-i)
    i=Verify(t,bad,"m")
  END
RETURN t

