Rem Program Findsubs v1.6a for Dndbbs Version v5.0a Release r2.0
Rem $Include: 'io9.inc'

' declare functions
FUNCTION Form$(Var#)
 Form$=Format$(Var#,"#,##0;;"+Chr$(34)+"zero"+Chr$(34))
END FUNCTION

Function Timeit!
 t1$ = Format$(Now, "hh:mm:ss") ' seconds past midnight
 t1! = CSNG(INT(VAL(MID$(t1$, 1, 2)))) * 3600! + CSNG(INT(VAL(MID$(t1$, 4, 2)))) * 60! + CSNG(INT(VAL(MID$(t1$, 7, 2))))
 Timeit!=t1!
End Function

Max.Variables=1024

On Error Goto Error.Routine
Call Stdin(Var)

Redim Variables(Max.Variables) As String
Color 15, 0
Print "CMD Subs Variable finder v1.6a:"

' 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$)
If CommandLine$="/?" Or CommandLine$="-?" Then
   Color 14, 0
   Print "Finds variables in dndbbs source modules:"
   Color 15,0
   Print "Where:"
   Color 14,0
   Print "Command line switches(preceded):"
   Color 15,0
   Print " /D  -  write to disk."
   Print " /P  -  force percent display"
   Color 14,0
   Print "Command line(remaining):"
   Color 15,0
   Print " Specify variable to find on command line."
   Print " Also use ? for wildcard character replacement,"
   Print " and use & character for boolean 'and' split."
   Color 7, 0
   End
Endif
Do
   CommandLine$ = LTRIM$(CommandLine$)
   If Left$(CommandLine$,1)="/" Then
      If Mid$(Ucase$(CommandLine$),2,1)="D" Then
         Write.Data.File=True
         CommandLine$=Mid$(CommandLine$,3)
      Else
         If Mid$(Ucase$(CommandLine$),2,1)="P" Then
            Percent.Display=True
            CommandLine$=Mid$(CommandLine$,3)
         Else
            Exit Do
         Endif
      Endif
   Else
      Exit Do
   Endif
Loop
If CommandLine$=Nul Then
   Color 15, 0
   Print "Enter variable name? ";
   Do
      X$=Inkey$
      Locate ,,1
      If Len(X$)=1 Then
         If X$=Chr$(13) Then
            Exit Do
         Endif
         If X$=Chr$(8) Then
            If Len(CommandLine$) Then
               Print Chr$(29);" ";Chr$(29);
               CommandLine$=Left$(CommandLine$,Len(CommandLine$)-1)
            Endif
         Else
            CommandLine$=CommandLine$+X$
            Print X$;
         Endif
      Endif
      Gosub TimeSlice
   Loop
   Print
   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 Or Percent.Display Then
   Print "    ";
Endif
VarF!=Timeit!
If Sensitive=False Then
   CommandLine$=Lcase$(CommandLine$)
Endif
FileSpec$="cmd*.bas"
Filename$=Dir$(FileSpec$)
While Len(Filename$)
   Total.Files=Total.Files+1
   Filename$=Dir$
Wend
If Total.Files = 0 Then
   Color 14, 0
   Print "No search files found."
   Color 7, 0
   PRINT
   PRINT "Exiting to system:"
   End
Endif
FileSpec$="cmd*.bas"
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
   ' go through file searching for variable
   Current.File=Current.File+1
   Gosub Dot.Display
   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=Variable1+2
            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
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
Select Case Variable1
Case 0
   Print "No variables found in any subroutines."
   Color 7, 0
   PRINT
   PRINT "Exiting to system:"
   End
Case 1
   Print "Total of "+Form$(Cdbl(Variable2))+" variables found in 1 subroutine."
Case Else
   Print "Total of "+Form$(Cdbl(Variable2))+" variables found in "; Form$(Cdbl(Variable1));" subroutines."
End Select
Page=2
Color 14, 0
If Variable1>False Then
   If Write.Data.File Then
      Close
      Open "TODO.LST" For Append As #1
      Print #1,"Results for: "+CommandLine$ + " " + Format$(Now,"mm-dd-yyyy")
   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
   If Write.Data.File Then
      Print #1,Chr$(13)+Chr$(10)
      Print "Data appended to 'todo.lst'"
   Endif
Endif
IF Write.Data.File=False THEN
   If Page Then
      If Line1>0 Then
         Print
      Endif
      Color 15, 0
      Print "More:";
      While Inkey$=Nul
         Gosub TimeSlice
      Wend
   Endif
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

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

Dot.Display:
 If Wildcard OR Percent.Display 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!=Timeit!-VarE!
 If VarG!<SFalse Then
    VarG!=Elapsed.Time!+86400!
 Endif
 If VarG!>=1! Then
    VarE!=Timeit!
    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

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

Error.Routine:
 If Err=5 Then
    InputLine$=""
    Resume Next
 Endif
 Print "Error";Err
 End

Sub Stdin(Var)
 Var=0
 ' check stdin length
 InregsX.AX = &H4202 ' eof
 InregsX.BX = 0 ' stdin
 InregsX.CX = 0
 InregsX.DX = 0
 CALL InterruptX(&H21, InregsX, OutregsX)
 IF OutregsX.AX = 0 THEN ' no input found
     EXIT SUB
 END IF
 Print "Illegal standard input attempted."
 END
End Sub


