;***********************************************
; RSERCH.M68    -       Random File SEARCH
;
; Searches any random file for a text string
;
; Usage: RSERCH filename string
;
; by Dave Heyliger - AMUS Staff
;***********************************************

       SEARCH  SYS                             ;SEARCH normals
       SEARCH  SYSSYM
       SEARCH  AAA                             ;from [100,133]

       .OFINI                                  ;only need 1 DDB as variable
       .OFDEF  FILE,D.DDB
       .OFSIZ  IMPSIZ

       PHDR    -1,0,PH$REE!PH$REU              ;anybody can run it anywhere
       GETIMP  IMPSIZ,A5                       ;A5 points to variable "DISK"

       ;Process input line of RSERCH filename string
TOP:    BYP                                     ;bypass all whitespace
       LIN                                     ;just a CR?
       BNE     10$                             ;nope
5$:     TYPECR  <Usage: RSERCH filename string> ;yup, give instructions
       EXIT                                    ;and quit

       ;input contains something - A2 should be pointing to file
10$:    FSPEC   FILE(A5)                        ;fill the DDB with the stuff
       INIT    FILE(A5)                        ;find the driver & get buff
       LOOKUP  FILE(A5)                        ;is it there?
       BEQ     20$                             ;yup
       TYPECR  <?File not found.>              ;nope, give error msg
       EXIT                                    ;and quit

       ;see if file is random file....
20$:    CMPW    FILE+D.WRK+6(A5),#-1            ;random?
       BEQ     25$                             ;yup, continue
       TYPECR  <?File is not a random file.>   ;mssg
       EXIT

       ;now get the string (that's hopefully there)
25$:    MOVW    FILE+D.WRK+2(A5),D2             ;D2 holds total blocks in file
       BYP                                     ;bypass all whitespace
       LIN                                     ;at a CR? (shouldn't be...)
       JEQ     5$                              ;yup, give instructions above

       ;find length of string
30$:    MOV     A2,A0                           ;A0 always points to string
       CLR     D3                              ;D3 string length
35$:    CMPB    (A2)+,#15                       ;look for end of string
       BEQ     40$                             ;there, got count
       INC     D3                              ;not there, increment count
       BR      35$                             ;and still count characters

       ;label the output
40$:    CRLF                                    ;output a blank line
       PRTTAB  -1,11.                          ;DIM
       TYPE    <Blocks containing >            ;label output
       PRTTAB  -1,12.                          ;BRIGHT
       MOV     A0,A2                           ;A2 points to string
45$:    MOVB    (A2)+,D1                        ;get a character
       CMPB    D1,#15                          ;CR?
       BEQ     50$                             ;yup, all done
       TTY                                     ;else type out character
       BR      45$                             ;and go here

       ;read in a block from the file
50$:    TYPECR  <:>                             ;end output label
       OPENR   FILE(A5)                        ;open the file
       MOV     #0,A1                           ;block count (temp)
       CLR     D5                              ;block count (total w/ match)
READ:   CTRLC   EXIT                            ;if ^C then quit
       READ    FILE(A5)                        ;read in a block
       MOV     FILE+D.BUF(A5),A4               ;A4 points to block read in

       ;now look for the string....
       MOV     #512.,D0                        ;D0 counter (512 bytes/block)
POINT:  MOV     A0,A2                           ;A2 points to start of string
       MOVB    (A4)+,D1                        ;get the byte
       UCS                                     ;upper case it
       CMPB    D1,@A2                          ;still match?
       JEQ     MAYBE                           ;yes, maybe a match....
NO:     DEC     D0                              ;one less byte in block
       BNE     POINT                           ;and look again if not done

       ;done with block... no match
NXTBLK: DECW    D2                              ;one less block to search
       BNE     20$                             ;still more to go
       CRLF
       TYPE    <Total number of blocks fulfilling goal: >
       MOV     D5,D1                           ;get total match count
       DCVT    0,OT$TRM                        ;type out number
       CRLF                                    ;return
       EXIT                                    ;and quit
20$:    INCW    FILE+D.REC+2(A5)                ;point to next block
       JMP     READ                            ;and read it again

       ;see if total match
MAYBE:  MOV     D3,D4                           ;D4 holds character count
30$:    DEC     D4                              ;one less char. in string
       BEQ     40$                             ;if done, block holds match
       INC     A2                              ;A2 points to next character
       DEC     D0                              ;decrement byte count
       JEQ     NXTBLK                          ;end of block - stop searching
       MOVB    (A4)+,D1                        ;get the byte
       UCS                                     ;upper case it
       CMPB    D1,@A2                          ;still match?
       JNE     NO                              ;no!
       BR      30$                             ;yes, but not done...

       ;total match found - output the block, and get the next block
40$:    DEC     D0                              ;decrement byte count
       INC     A1                              ;increment block count
       CMP     A1,#11.                         ;need a CR?
       BNE     10$                             ;nope
       MOV     #1,A1                           ;yes, clear A1
       CRLF                                    ;and output a CR
10$:    MOV     FILE+D.REC(A5),D1               ;get the block number
       OCVT    0,OT$TRM                        ;type out the number
       TYPE    <  >                            ;and space over
       INC     D5                              ;increment total match count
       JMP     NXTBLK                          ;and get next block

EXIT:   EXIT
       END