Rem Program Finddoc v1.4a for Morutl Version v56.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 "Keyword finder v1.4a:"

' 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 Left$(Ucase$(CommandLine$),2)="/D" Then
   Write.Data.File=True
   CommandLine$=Mid$(CommandLine$,3)
Endif
If CommandLine$="/?" Or CommandLine$="-?" Then
   Color 14, 0
   Print "Finds keywords in basic text 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 keyword? ";
   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
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
   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
   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
For File.Types=1 To 2
   Select Case File.Types
   Case 1
      FileSpec$="*.txt"
   Case 2
      FileSpec$="*.doc"
   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=1 To 2
   If File.Types=1 Then
      FileSpec$="*.txt"
   Else
      FileSpec$="*.doc"
   Endif
   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 "Files counted:"; Variable3
If Variable2=1 Then
   Var$=" 1 keyword "
Else
   Var$=Str$(Variable2)+" keywords "
Endif
Select Case Variable1
Case 0
   Print "No keywords found in any files."
Case 1
   Print "Total of"+Var$+"found in 1 file."
Case Else
   Print "Total of"+Var$+"found in"+Str$(Variable1)+" files."
End Select
Page=2
Color 14, 0
If Variable1>False Then
   If Write.Data.File Then
      Close
      Open "OUTPUT.LST" For Output 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
               Wend
               Print
               Color 14, 0
            Endif
         Endif
         Line1=False
      Endif
   Next
Endif
If Write.Data.File Then
   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
