[25] | 1 | ## Super aggressive EOL-fixer! |
---|
| 2 | ## |
---|
| 3 | ## Will even understand screwed up ones like CRCRLF. |
---|
| 4 | ## (found in bad CVS repositories, caused by spacey developers |
---|
| 5 | ## abusing CVS) |
---|
| 6 | ## |
---|
| 7 | ## davygrvy@pobox.com 3:41 PM 10/12/2001 |
---|
| 8 | ## |
---|
| 9 | |
---|
| 10 | package provide EOL-fix 1.1 |
---|
| 11 | |
---|
| 12 | namespace eval ::EOL { |
---|
| 13 | variable outMode crlf |
---|
| 14 | } |
---|
| 15 | |
---|
| 16 | proc EOL::fix {filename {newfilename ""}} { |
---|
| 17 | variable outMode |
---|
| 18 | |
---|
| 19 | if {![file exists $filename]} { return } |
---|
| 20 | puts "EOL Fixing: $filename" |
---|
| 21 | |
---|
| 22 | file rename ${filename} ${filename}.o |
---|
| 23 | set fhnd [open ${filename}.o r] |
---|
| 24 | |
---|
| 25 | if {$newfilename != ""} { |
---|
| 26 | set newfhnd [open ${newfilename} w] |
---|
| 27 | } else { |
---|
| 28 | set newfhnd [open ${filename} w] |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | fconfigure $newfhnd -translation [list auto $outMode] |
---|
| 32 | seek $fhnd 0 end |
---|
| 33 | set theEnd [tell $fhnd] |
---|
| 34 | seek $fhnd 0 start |
---|
| 35 | |
---|
| 36 | fconfigure $fhnd -translation binary -buffersize $theEnd |
---|
| 37 | set rawFile [read $fhnd $theEnd] |
---|
| 38 | close $fhnd |
---|
| 39 | |
---|
| 40 | regsub -all {(\r)|(\r){1,2}(\n)} $rawFile "\n" rawFile |
---|
| 41 | |
---|
| 42 | set lineList [split $rawFile \n] |
---|
| 43 | |
---|
| 44 | foreach line $lineList { |
---|
| 45 | puts $newfhnd $line |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | close $newfhnd |
---|
| 49 | file delete ${filename}.o |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | proc EOL::fixall {args} { |
---|
| 53 | if {[llength $args] == 0} { |
---|
| 54 | puts stderr "no files to fix" |
---|
| 55 | exit 1 |
---|
| 56 | } else { |
---|
| 57 | set cmd [lreplace $args -1 -1 glob -nocomplain] |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | foreach f [eval $cmd] { |
---|
| 61 | if {[file isfile $f]} {fix $f} |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | if {$tcl_interactive == 0 && $argc > 0} { |
---|
| 66 | if {[string index [lindex $argv 0] 0] == "-"} { |
---|
| 67 | switch -- [lindex $argv 0] { |
---|
| 68 | -cr { set ::EOL::outMode cr } |
---|
| 69 | -crlf { set ::EOL::outMode crlf } |
---|
| 70 | -lf { set ::EOL::outMode lf } |
---|
| 71 | default { puts stderr "improper mode switch" ; exit 1 } |
---|
| 72 | } |
---|
| 73 | set argv [lrange $argv 1 end] |
---|
| 74 | } |
---|
| 75 | eval EOL::fixall $argv |
---|
| 76 | } else { |
---|
| 77 | return |
---|
| 78 | } |
---|