/* SearchFaqs.rexx
 * Copyright © 1995-1996 Nils Bandener
 * Requires "Most" by Uwe Röhm
 * Tested with Most v1.56
 * $VER: searchfaqs.rexx 4.4 (9.4.96)
 */

Options Results
Address FIASCO

Signal on Syntax
Signal on Halt
Signal on Break_C
Signal on Failure

F_LockGUI

/*
 *  Get params
 */

F_RequestString 'Text "Enter a string to search for:"'
if rc ~= 0 then call Quit
pat = Result

F_RequestNumber '1 Text "Start searching at record no.:"'
if rc ~= 0 then call Quit
start = result

F_RequestChoice '"Which records do you want to be searched?"' '"All|Marked only"'

if Result = 0 then
    Monly = 1
else
    Monly = 0

F_RequestChoice '"Write Results to File?"' '"Yes|No"'

if Result = 1 then
    WriteFile = 1
else
    WriteFile = 0

if WriteFile = 1 then do

    /*
     * Get a file to write to
     */

    F_RequestFile Savemode

    if rc ~= 0 then do

        F_UnlockGUI

        exit

    end

    FName = Result

    /*
     *  Open the file
     */

    if ~Open("o", FName, "write") then do

        F_RequestChoice '"Couldn`t open file"' '"Cancel"'

        F_UnlockGUI

        exit

    end

end

/*
 *  Interactive searching puts up a requester on each occurence
 */

F_RequestChoice '"Interactive Searching?"' '"Yes|No"'

if Result = 1 then
    Interact = 1
else
    Interact = 0

F_CountRecs
num = Result

say "Searching for" pat "... (Ctrl-C for break)"

F_SetStatus "Searching..."

/*
 * If start is out of range, make it fit
 */

if start > num then start = 1
if start < 1 then start = 1

do i = start to num

   Address FIASCO

   F_Progress i num

   /*
    *  Check for marking
    */

   F_IsMarked i

   if (Monly = 1 & rc = 0) | (Monly = 0) then do

       F_GetFieldCont "File" Record i
       file = Result

       /*
        *  search for the pattern, write the results to t:found
        */

       Address Command 'search >t:found' '"' || file || '"' '"' || pat || '"'

       if rc = 0 then do

            /*
             *  something has been found, open the result file
             */

           if open("f", "t:found", "read") then do

                if ~eof("f") then do

                    say "*****" file ":"
                    if Writefile = 1 then call writeln("o", "*****" file ":")

                    /* If SkipAd = 1, no Requester will be put up */

                    skipad = 0

                    do while ~eof("f")

                        ln = readln("f")

                        say ln

                        if Writefile = 1 then call writeln("o", ln)

                        if interact = 1 & skipad = 0 then do

                            Parse var ln Line Rest

                            if length(line) > 0 then do

                                Address FIASCO

                                rest = compress(rest, '"*')
                                if length(rest) > 75 then rest = left(rest, 75)

                                F_RequestChoice '"Found Pattern in File*n' || file || '*nLine ' || Line || '*n' || Rest || '"' '"Display|Next occurence|Next file|Break"'

                                if Result = 1 then do

                                    found = 0
                                    retry = 0

                                    do while found = 0

                                        retry = retry + 1

                                        if ~show(ports, "MOST.0") then do

                                            address command
                                            "run most" file
                                            "sys:rexxc/waitforport MOST.0"
                                        end

                                        if show(ports, "MOST.0") then do

                                            Address MOST.0

                                            GetFileList
                                            FileList = Result

                                            k = 1

                                            do while k <= words(FileList) & found = 0

                                                filepos = lastpos("/", file)
                                                if filepos = 0 then filepos = lastpos(":", file)
                                                if filepos ~= 0 then filepos = filepos + 1

                                                if upper(word(FileList, k)) = upper(substr(file,filepos)) then do

                                                    found = 1

                                                    /* Skip Strings like 1/1 */
                                                    if pos("/", word(FileList, k + 1)) ~= 0 then k = k + 1

                                                    host = word(FileList, k + 1)

                                                    Interpret "Address" host
                                                    tofront

                                                end

                                                k = k + 2
                                            end

                                            if found = 0 then do

                                                if retry = 1 then do
                                                    address command

                                                    "run sys:rexxc/rx runmost" file
                                                end
                                                else if retry = 2 | retry = 3 then do
                                                    address command

                                                    wait 1 sec
                                                end
                                                else break

                                            end
                                        end
                                    end

                                    if found = 1 then do

                                        Interpret "Address" host

                                        goto 'line' line
                                    end
                                end
                                else if Result = 3 then do

                                    skipad = 1

                                end
                                else if Result = 0 then do

                                    call close("f")

                                    if Writefile = 1 then call close("o")

                                    Address FIASCO

                                    F_UnlockGUI

                                    F_ResetStatus

                                    exit

                                end
                            end
                        end

                    end
                end

                call close("f")

           end
        end
   end
end

if Writefile = 1 then do

    call close("o")

end

say "Done"

F_UnlockGUI
F_ResetStatus

exit

Syntax:
Failure:
say "Error" rc "in line" sigl ":" errortext(rc)
say file pat

Halt:
Break_C:
Quit:

Address FIASCO

F_UnlockGUI
F_ResetStatus

say "Break"

exit

