From c7aacd521bec70dc013c71614cdfd7ad7ae543d7 Mon Sep 17 00:00:00 2001 From: John McMaster Date: Wed, 5 Dec 2018 16:52:14 -0800 Subject: [PATCH] tcl: reformat tool Signed-off-by: John McMaster --- Makefile | 1 + utils/reformat.tcl | 108 ++++++++++++++++++++++++++++++++++++++++++ utils/tcl-reformat.sh | 15 ++++++ 3 files changed, 124 insertions(+) create mode 100644 utils/reformat.tcl create mode 100755 utils/tcl-reformat.sh diff --git a/Makefile b/Makefile index 9e58cc7b..2f5c485f 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,7 @@ format: find . -name \*.cc -and -not -path './third_party/*' -and -not -path './.git/*' -exec $(CLANG_FORMAT) -style=file -i {} \; find . -name \*.h -and -not -path './third_party/*' -and -not -path './.git/*' -exec $(CLANG_FORMAT) -style=file -i {} \; find . -name \*.py -and -not -path './third_party/*' -and -not -path './.git/*' -exec yapf -p -i {} \; + find . -name \*.tcl -and -not -path './third_party/*' -and -not -path './.git/*' -exec ${XRAY_TCL_REFORMAT} {} \; 2>/dev/null clean: $(MAKE) -C database clean diff --git a/utils/reformat.tcl b/utils/reformat.tcl new file mode 100644 index 00000000..c4efb5d8 --- /dev/null +++ b/utils/reformat.tcl @@ -0,0 +1,108 @@ +#!/usr/bin/env tclsh +# From: https://gist.github.com/yyamasak/af250f7ca74e18526734#file-reformat-tcl-L10 +# Which is based on https://wiki.tcl-lang.org/page/Reformatting+Tcl+code+indentation +# See for licensing + +proc reformat {tclcode {pad 4}} { + set lines [split $tclcode \n] + set out "" + set continued no + set oddquotes 0 + set line [lindex $lines 0] + set indent [expr {([string length $line]-[string length [string trimleft $line \ \t]])/$pad}] + set pad [string repeat " " $pad] + + foreach orig $lines { + set newline [string trim $orig \ \t] + set line [string repeat $pad $indent]$newline + if {[string index $line end] eq "\\"} { + if {!$continued} { + incr indent 2 + set continued yes + } + } elseif {$continued} { + incr indent -2 + set continued no + } + + if { ! [regexp {^[ \t]*\#} $line] } { + + # oddquotes contains : 0 when quotes are balanced + # and 1 when they are not + set oddquotes [expr {([count $line \"] + $oddquotes) % 2}] + if {! $oddquotes} { + set nbbraces [count $line \{] + incr nbbraces -[count $line \}] + set brace [string equal [string index $newline end] \{] + set unbrace [string equal [string index $newline 0] \}] + if {$nbbraces!=0 || $brace || $unbrace} { + incr indent $nbbraces ;# [GWM] 010409 multiple close braces + if {$indent<0} { + error "unbalanced braces" + } + puts $unbrace + puts $pad + puts $nbbraces + set np [expr {$unbrace? [string length $pad]:-$nbbraces*[string length $pad]}] + set line [string range $line $np end] + } + } else { + # unbalanced quotes, preserve original indentation + set line $orig + } + } + append out $line\n + } + return $out +} + +proc eol {} { + switch -- $::tcl_platform(platform) { + windows {return \r\n} + unix {return \n} + macintosh {return \r} + default {error "no such platform: $::tc_platform(platform)"} + } +} + +proc count {string char} { + set count 0 + while {[set idx [string first $char $string]]>=0} { + set backslashes 0 + set nidx $idx + while {[string equal [string index $string [incr nidx -1]] \\]} { + incr backslashes + } + if {$backslashes % 2 == 0} { + incr count + } + set string [string range $string [incr idx] end] + } + return $count +} + +set usage "reformat.tcl ?-indent number? filename" + +if {[llength $argv]!=0} { + if {[lindex $argv 0] eq "-indent"} { + set indent [lindex $argv 1] + set argv [lrange $argv 2 end] + } else { + set indent 4 + } + if {[llength $argv]>1} { + error $usage + } + set f [open $argv r] + set data [read $f] + close $f + + set filename "$argv.tmp" + set f [open $filename w] + + puts -nonewline $f [reformat [string map [list [eol] \n] $data] $indent] + close $f + file copy -force $filename $argv + file delete -force $filename + +} diff --git a/utils/tcl-reformat.sh b/utils/tcl-reformat.sh new file mode 100755 index 00000000..63624a61 --- /dev/null +++ b/utils/tcl-reformat.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +# Wrapper to clean up newlines +# We could do this in tcl...but tcl + +fn=$1 + +$XRAY_REFORMAT_TCL $fn >/dev/null +# Always puts a newline at the end, even if there was one before +# remove duplicates, but keep at least one +printf "%s\n" "$(< $fn)" >$fn.tmp +mv $fn.tmp $fn + +# Remove trailing spaces +sed -i 's/[ \t]*$//' "$fn" +