Created
July 22, 2012 06:41
-
-
Save yamasushi/3158712 to your computer and use it in GitHub Desktop.
Gaucheマニュアルのgrepサンプルを改造してみる。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env gosh | |
(use util.match) | |
; マニュアルからコピー | |
(define (usage) | |
(format (current-error-port) | |
"Usage: ~a regexp file ...\n" *program-name*) | |
(exit 2)) | |
(define (grep rx port) | |
(with-input-from-port port | |
(lambda () | |
(port-for-each | |
(lambda (line) | |
(when (rxmatch rx line) | |
(format #t "~a:~a: ~a\n" | |
(port-name port) | |
(- (port-current-line port) 1) | |
line))) | |
read-line)))) | |
(define (call-with-<> files proc :optional (reader #f)) | |
(cond | |
[ (input-port? files) | |
(if reader | |
(port-for-each proc (cut reader files) ) | |
(proc files) ) ] | |
[ (string? files) | |
(call-with-input-file files (cut call-with-<> <> proc reader) ) ] | |
[ (list? files) | |
(for-each (cut call-with-<> <> proc reader) files) ] | |
[else (error "error")] | |
)) | |
(define (main args) | |
(if (null? (cdr args)) | |
(usage) | |
(let ((rx (string->regexp (cadr args)))) | |
(call-with-<> | |
(match (cddr args) | |
[() (current-input-port)] [x x] ) | |
(cut grep rx <>)))) | |
0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment