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

program f90grep
    use regex, only: match
    use dc_string, only: put
    use gtool, only: gtargget
    character(100):: line, pattern
    integer::        ios, start, length, linelen
    call gtargget(1, pattern)
    if (pattern == "") then
        call put("f90grep: pattern not given, so type here:")
        read(*, "(A)") pattern
    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
