Rem Program Findvar v1.5a for Morutl Version v57.0a

Rem $Include: 'qbx.bi'
Rem $Include: 'const.inc'
Common Shared Variables() As String
Max.Variables=1024
Redim Variables(Max.Variables) As String
Dim InregsX As RegTypeX
Dim OutRegsX As RegTypeX
Color 15, 0
Print "Variable finder v1.5a:"

' read command line from PSP
CommandLine$ = Nul
InregsX.AX = &H6200
CALL InterruptX(&H21, InregsX, OutregsX)
PSPsegment = OutregsX.BX
PSPoffset = 128
DEF SEG = PSPsegment
FOR Count = 1 TO 127
   Command.Char = PEEK(PSPoffset + Count)
   SELECT CASE Command.Char
   CASE 0, 10, 13
      EXIT FOR
   CASE ELSE
      CommandLine$ = CommandLine$ + CHR$(Command.Char)
   END SELECT
NEXT
DEF SEG

CommandLine$ = LTRIM$(CommandLine$)
Var=Instr(Ucase$(CommandLine$),"/D")
If Var Then
   Write.Data.File=True
   CommandLine$=Left$(CommandLine$,Var-1)+Mid$(CommandLine$,Var+2)
Endif
Var=Instr(Ucase$(CommandLine$),"/S")
If Var Then
   Skip.Data.File=True
   CommandLine$=Left$(CommandLine$,Var-1)+Mid$(CommandLine$,Var+2)
Endif
If Left$(CommandLine$,1)=Chr$(34) Then
   Var=Instr(2,CommandLine$,Chr$(34))
   If Var Then
      Var2$=Left$(CommandLine$,Var)
      Print "Search filename ";Var2$;"(y/n)? ";
      Locate ,,1
      Do
         Var$=Inkey$
         If Len(Var$) Then
            Select Case Lcase$(Var$)
            Case "y"
               Print "y"
               FileSpec2$=Left$(CommandLine$,Var)
               CommandLine$=Mid$(CommandLine$,Var+1)
               FileSpec2$=Mid$(FileSpec2$,2)
               FileSpec2$=Left$(FileSpec2$,Len(FileSpec2$)-1)
               Exit Do
            Case "n"
               Print "n"
               Exit Do
            End Select
         Endif
         Gosub TimeSlice
      Loop
   Endif
Endif
If CommandLine$="/?" Or CommandLine$="-?" Then
   Color 14, 0
   Print "Finds variables in basic source files;"
   Print "Specify variable to find on command line."
   Print "Precede variable with /D to write to disk."
   Print "Also imbed ? for wildcard character"
   Print "replacement, and & for boolean and split."
   Color 7, 0
   End
Endif
If CommandLine$=Nul Then
   Color 15, 0
   Print "Enter variable name? ";
   Line Input CommandLine$
   If CommandLine$ = "" Then
      End
   Endif
Endif
Print "Case sensitive(y/n)? ";
Locate ,,1
Do
   Var$=Inkey$
   If Len(Var$) Then
      Select Case Lcase$(Var$)
      Case "y"
         Print "y"
         Sensitive=True
         Exit Do
      Case "n"
         Print "n"
         Sensitive=False
         Exit Do
      End Select
   Endif
   Gosub TimeSlice
Loop
If Instr(CommandLine$,"?") Then
   Print "Use wildcards(y/n)? ";
   Locate ,,1
   Do
      Var$=Inkey$
      If Len(Var$) Then
         Select Case Lcase$(Var$)
         Case "y"
            Print "y"
            Wildcard=True
            Exit Do
         Case "n"
            Print "n"
            Wildcard=False
            Exit Do
         End Select
      Endif
      Gosub TimeSlice
   Loop
Endif
If Instr(CommandLine$,"&") Then
   Print "Use boolean 'and'(y/n)? ";
   Locate ,,1
   Do
      Var$=Inkey$
      If Len(Var$) Then
         Select Case Lcase$(Var$)
         Case "y"
            Print "y"
            Boolean=True
            Exit Do
         Case "n"
            Print "n"
            Boolean=False
            Exit Do
         End Select
      Endif
      Gosub TimeSlice
   Loop
Endif
Color 15, 0
Print "Finding: "; CHR$(34);
Color 14, 0
Print CommandLine$;
Color 15, 0
Print CHR$(34); " ";
If Wildcard Then
   Print "    ";
Endif
VarF!=Timer
If Sensitive=False Then
   CommandLine$=Lcase$(CommandLine$)
Endif
Start.File=1
If Skip.Data.File Then
   Start.File=3
Endif
If Len(FileSpec2$) Then
   Start.File=3
Endif
For File.Types=Start.File To 3
   Select Case File.Types
   Case 1
      FileSpec$="*.bi"
   Case 2
      FileSpec$="*.inc"
   Case 3
      If Len(FileSpec2$) Then
         FileSpec$=FileSpec2$
      Else
         FileSpec$="*.bas"
      Endif
   End Select
   Filename$=Dir$(FileSpec$)
   While Len(Filename$)
      Total.Files=Total.Files+1
      Filename$=Dir$
   Wend
Next
If Total.Files = 0 Then
   Color 14, 0
   Print "No search files found."
   Color 7, 0
   End
Endif
For File.Types=Start.File To 3
   Select Case File.Types
   Case 1
      FileSpec$="*.bi"
   Case 2
      FileSpec$="*.inc"
   Case 3
      If Len(FileSpec2$) Then
         FileSpec$=FileSpec2$
      Else
         FileSpec$="*.bas"
      Endif
   End Select
   Filename$=Dir$(FileSpec$)
   While Len(Filename$)
      If Inkey$=Chr$(27) Then
         Color 7, 0
         Print
         Print "Exiting to system:"
         End
      Endif
      Close
      Open Filename$ For Input As #1
      Variable3=Variable3+1
      Current.File=Current.File+1
      Gosub Dot.Display
      ' go through file searching for variable
      Do While Not Eof(1)
         Line Input #1,InputLine$
         InputLine$=Ltrim$(InputLine$)
         InputLine$=Rtrim$(InputLine$)
         If Sensitive=False Then
            InputLine$=Lcase$(InputLine$)
         Endif
         If Len(InputLine$)>False Then
            Gosub Count.Pattern
            If VarX Then
               Variable1=Variable1+1
               Variable2=Variable2+1
               IF Variable1 > Max.Variables THEN
                  Max.Variables = Max.Variables + 64
                  REDIM PRESERVE Variables(Max.Variables) AS STRING
               END IF
               Variables(Variable1)=Filename$
               ' go through remaining file counting variables
               Do While Not Eof(1)
                  Line Input #1,InputLine$
                  InputLine$=Ltrim$(InputLine$)
                  InputLine$=Rtrim$(InputLine$)
                  If Sensitive=False Then
                     InputLine$=Lcase$(InputLine$)
                  Endif
                  If Len(InputLine$)>False Then
                     Gosub Count.Pattern
                     If VarX Then
                        Variable2=Variable2+1
                     Endif
                  Endif
               Loop
            Endif
         Endif
      Loop
      Filename$=Dir$
   Wend
Next
Print
If Variable1>1 Then
   For Sort1=1 To Variable1
      For Sort2=Sort1+1 To Variable1
         If Variables(Sort1)>Variables(Sort2) Then
            Swap Variables(Sort1),Variables(Sort2)
         Endif
      Next
   Next
Endif
Color 15, 0
Print "Subroutines counted:"; Variable3
If Variable2=1 Then
   Var$=" 1 variable "
Else
   Var$=Str$(Variable2)+" variables "
Endif
Select Case Variable1
Case 0
   Print "No variables found in any subroutines."
Case 1
   Print "Total of"+Var$+"found in 1 subroutine."
Case Else
   Print "Total of"+Var$+"found in"+Str$(Variable1)+" subroutines."
End Select
Page=2
Color 14, 0
If Variable1>False Then
   If Write.Data.File Then
      Close
      Open "OUTPUT.LST" For Append As #1
      Print #1,"Results for: "+CommandLine$
   Endif
   For Var=1 To Variable1
      Filename$=Variables(Var)
      Filename$=Filename$+Space$(15-Len(Filename$))
      If Write.Data.File Then
         Print #1, Filename$;
      Else
         Print Filename$;
      Endif
      Line1=Line1+1
      If Line1=5 Then
         If Write.Data.File Then
            Print #1,""
         Else
            Print
            Page=Page+1
            If Page=23 Then
               Page=False
               Color 15, 0
               Print "More:";
               While Inkey$=Nul
                  Gosub TimeSlice
               Wend
               Print
               Color 14, 0
            Endif
         Endif
         Line1=False
      Endif
   Next
Endif
If Write.Data.File Then
   Print #1,Chr$(13)+Chr$(10)
   Print "Data written to 'output.lst'"
Endif
Color 7, 0
Print
Print "Exiting to system:"
End

Count.Pattern:
 VarX=False
 If Boolean Then
    CommandLine2$=CommandLine$
    First=Instr(CommandLine2$,"&")
    SearchPattern$=Left$(CommandLine2$,First-1)
    CommandLine2$=Mid$(CommandLine2$,First+1)
    Do
       Gosub Get.Match
       If VarX=False Then
          Exit Do
       Endif
       If CommandLine2$=Nul Then
          Exit Do
       Endif
       First=Instr(CommandLine2$,"&")
       If First Then
          SearchPattern$=Left$(CommandLine2$,First-1)
          CommandLine2$=Mid$(CommandLine2$,First+1)
       Else
          SearchPattern$=CommandLine2$
          CommandLine2$=Nul
       Endif
    Loop
    Return
 Endif
 SearchPattern$=CommandLine$
 Gosub Get.Match
 Return

Get.Match:
 VarX=False
 If Wildcard Then
    Gosub CheckWildcard
    If Match Then
       VarX=True
    Endif
 Else
    If Instr(InputLine$,SearchPattern$) Then
       VarX=True
    Endif
 Endif
 Return

Dot.Display:
 If Wildcard Then
    Percent!=(Current.File/Total.Files)*100!
    Percent!=Int(Percent!)
    FOR Imbedded = 1 TO 4
       PRINT CHR$(29);" ";CHR$(29);
    NEXT
    PRINT RIGHT$(STR$(Percent! + 1000!), 3) + "%";
    Return
 Endif
 VarG!=Timer-VarE!
 If VarG!<SFalse Then
    VarG!=Elapsed.Time!+86400!
 Endif
 If VarG!>=1! Then
    VarE!=Timer
    If VarD=False Then
       VarC=VarC+1
       Print ".";
       If VarC=5 Then
          VarD=True
          VarC=False
       Endif
    Else
       VarC=VarC+1
       Locate Csrlin,Pos(0)-1,0
       Print " ";
       Locate Csrlin,Pos(0)-1,1
       If VarC=5 Then
          VarD=False
          VarC=False
       Endif
    Endif
 Endif
 Return

CheckWildcard:
 Match = False
 Length1 = LEN(SearchPattern$)
 FOR I1 = 1 TO LEN(InputLine$)
    S$ = MID$(InputLine$, I1)
    S$ = LEFT$(S$, Length1)
    IF LEN(S$) <> Length1 THEN
       EXIT FOR
    END IF
    ' compare search string to wildcard
    VarQ = True
    FOR I2 = 1 TO Length1
       IF MID$(SearchPattern$, I2, 1) <> "?" THEN
          IF MID$(SearchPattern$, I2, 1) <> MID$(S$, I2, 1) THEN
             VarQ = False
             EXIT FOR
          END IF
       END IF
    NEXT
    IF VarQ THEN
       Match = True
       EXIT FOR
    END IF
 NEXT
 RETURN

TimeSlice:
 InregsX.AX=&H1680
 InregsX.BX=&H0000
 Call InterruptX(&H2F,InregsX,OutregsX)
 Return
