! f90grep - Fortran 90 で作られた grep(1) の模倣
!

program f90grep
    use regex, only: match
    use dc_string, only: VSTRING, operator(==), assignment(=), put
    use sysdep, only: sysdepArgGet
    character(100):: line, pattern
    type(VSTRING):: vpattern
    integer::        ios, start, length, linelen
    call sysdepArgGet(1, vpattern)
    if (vpattern == "") then
        call put("f90grep: pattern not given, so type here:")
        read(*, "(A)") pattern
    else
        pattern = vpattern
    endif
    do
        read(*, '(A)', iostat=ios) line
        if (ios /= 0) exit
	linelen = len_trim(line)
        call match(trim(pattern), line(1:linelen), start, length)
        if (start > 0) then
            write(*, '(A)') line(1:start-1) // &
                & char(27) // "[42;30;1m" // line(start:start+length-1) &
                & // char(27) // '[m' // line(start+length:linelen)
	endif
    enddo
end program
