' Simple grep: pattern search in lines of text with basic pattern matching
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i  AS LONG
    LOCAL j  AS LONG
    LOCAL numLines  AS LONG
    LOCAL lines(30) AS STRING
    LOCAL searchPat AS STRING
    LOCAL matchCount  AS LONG
    LOCAL lineCount  AS LONG
    LOCAL pos  AS LONG
    LOCAL patLen  AS LONG
    LOCAL found  AS LONG
    LOCAL lineStr  AS STRING
    LOCAL s  AS STRING
    LOCAL caseSensitive AS LONG
    LOCAL showLineNum  AS LONG
    LOCAL invertMatch  AS LONG
    LOCAL contextLines  AS LONG
    LOCAL contextBefore  AS LONG
    LOCAL contextAfter  AS LONG
    PRINT "=== Grep Tool ==="
    PRINT

    numLines = 10
    lines(0) = "The quick brown fox jumps over the lazy dog.    "
    lines(1) = "Pack my box with five dozen liquor jugs.       "
    lines(2) = "How vexingly quick daft zebras jump!          "
    lines(3) = "Sphinx of black quartz, judge my vow.          "
    lines(4) = "The five boxing wizards jump quickly.          "
    lines(5) = "Bright vixens jump; dozy fowl quack.           "
    lines(6) = "Quick brown foxes jump over lazy dogs.         "
    lines(7) = "A quick movement of the enemy will jeopardize  "
    lines(8) = "Cwm fjord bank glyphs vext quiz.               "
    lines(9) = "Jaded zombies acted quaintly but kept driving  "

    searchPat = "quick"
    caseSensitive = 0
    showLineNum = 1
    invertMatch = 0
    contextLines = 0

    PRINT "Search pattern: '" & searchPat & "'"
    PRINT "Case sensitive: "
    IF caseSensitive = 1 THEN PRINT "Yes" ELSE PRINT "No"
    PRINT "Input: " & FORMAT$(numLines) & " lines"
    PRINT

    PRINT "--- Results ---"
    matchCount = 0
    lineCount = 0

    FOR i = 0 TO numLines - 1
        lineStr = TRIM$(lines(i))
        lineCount = lineCount + 1
        found = 0

        IF caseSensitive = 0 THEN
            pos = INSTR(1, lineStr, searchPat)
        ELSE
            pos = INSTR(1, lineStr, searchPat)
        END IF

        IF pos > 0 THEN found = 1

        IF invertMatch = 1 THEN
            IF found = 0 THEN found = 1 ELSE found = 0
        END IF

        IF found = 1 THEN
            matchCount = matchCount + 1
            IF showLineNum = 1 THEN
                PRINT "  " & FORMAT$(i + 1) & ": " & lineStr
            ELSE
                PRINT "  " & lineStr
            END IF
        END IF
    NEXT i

    IF matchCount = 0 THEN
        PRINT "  No matches found."
    END IF
    PRINT

    PRINT "--- Summary ---"
    PRINT "  " & FORMAT$(matchCount) & " matching lines out of " & FORMAT$(lineCount)
    PRINT "  Hit rate: " & FORMAT$(matchCount * 100 / lineCount) & "%"
    PRINT

    PRINT "--- Pattern Syntax ---"
    PRINT "  'quick' -> literal match"
    PRINT "  '^The' -> line starts with 'The'"
    PRINT "  'dog$' -> line ends with 'dog'"
    PRINT "  'j.m.' -> 'j' then any char, 'm', any char"
    PRINT "  'fox|dog' -> matches 'fox' or 'dog'"
    PRINT

    PRINT "--- Options ---"
    PRINT "  -i: case insensitive"
    PRINT "  -n: show line numbers"
    PRINT "  -v: invert match"
    PRINT "  -c: count only"
    PRINT "  -C n: n lines of context"
    PRINT "  -r: recursive directory search"

    PRINT
    PRINT "Grep tool simulation complete."

    FUNCTION = 0
END FUNCTION
