| 1 |
#!/usr/bin/env gosh |
| 2 |
|
| 3 |
(define (usage program-name) |
| 4 |
(format (current-error-port) |
| 5 |
"Usage: ~a regexp file ...\n" program-name) |
| 6 |
(exit 2)) |
| 7 |
|
| 8 |
(define (grep rx) |
| 9 |
(generator-for-each |
| 10 |
(lambda (line) |
| 11 |
(when (rx line) |
| 12 |
(format #t "~a:~a: ~a\n" |
| 13 |
(port-name (current-input-port)) |
| 14 |
(- (port-current-line (current-input-port)) 1) |
| 15 |
line))) |
| 16 |
read-line)) |
| 17 |
|
| 18 |
(define (main args) |
| 19 |
(when (null? (cdr args)) (usage (car args))) |
| 20 |
(let1 rx (string->regexp (cadr args)) |
| 21 |
(if (null? (cddr args)) |
| 22 |
(grep rx) |
| 23 |
(for-each (cut with-input-from-file <> (cut grep rx)) |
| 24 |
(cddr args)))) |
| 25 |
0) |