Debian Codegraph @

tools/eolFix.tcl

raw
1 ## Super aggressive EOL-fixer!
2 ##
3 ## Will even understand screwed up ones like CRCRLF.
4 ##
5 ## [email protected] 3:41 PM 10/12/2001
6 ##
7
8 package provide EOL-fix 1.1
9
10 namespace eval ::EOL {
11 variable outMode crlf
12 }
13
14 proc EOL::fix {filename {newfilename {}}} {
15 variable outMode
16
17 if {![file exists $filename]} {
18 return
19 }
20 puts "EOL Fixing: $filename"
21
22 file rename ${filename} ${filename}.o
23 set fhnd [open ${filename}.o r]
24
25 if {$newfilename ne ""} {
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] eq "-"} {
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 }