5 puts stderr "$argv0: $msg"
11 puts $file "Usage: \n\
13 $argv0 -u OUTPUT FILE FILE ..."
17 while {[llength $argv]} {
18 switch -glob -- [lindex $argv 0] {
21 if {[llength $argv] < 2} { die "option `-u' needs an argument" }
22 set output [lindex $argv 1]
23 set argv [lrange $argv 1 end]
25 "-d" - "--delete" { set job "delete" }
26 "-s" - "--split" { set job "split" }
27 "-h" - "--help" { usage stdout; exit 0 }
29 "--" { set argv [lrange $argv 1 end]; break }
30 "-*" { die "unknown option `[lindex $argv 0]'"; exit 1 }
33 set argv [lrange $argv 1 end]
36 proc clear-arrays {args} {
44 proc write-safe {stuff {tidy {}}} {
45 global _ws_close _ws_del _ws_new
46 clear-arrays _ws_del _ws_new
52 foreach f $_ws_close { catch { close $f } }
53 foreach f [array names _ws_new] { catch { file delete -- $f.new } }
54 catch { uplevel 1 $tidy }
57 foreach f $_ws_close { catch { close $f } }
59 foreach f [concat [array names _ws_old] [array names _ws_del]] {
63 foreach f [array names all] {
64 if {[file exists $f]} {
66 file copy -force -- $f $f.old
70 foreach f [array names _ws_new] { file rename -force -- $f.new $f }
71 foreach f [array names _ws_del] { file delete -- $f }
73 foreach f [array names _ws_new] { catch { file delete -- $f.new } }
74 foreach f [array names old] { file rename -force -- $f.old $f }
75 catch { uplevel 1 $tidy }
78 foreach i [array names all] { catch { file delete -- $i.old } }
79 catch { uplevel 1 $tidy }
83 proc write-safe-open {name {trans auto}} {
84 global _ws_close _ws_new
85 if {[file isdirectory $name]} { error "`$name' is a directory" }
86 set f [open $name.new w]
87 fconfigure $f -translation $trans
93 proc write-safe-delete {name} {
98 proc write-safe-file {name contents {trans auto}} {
99 set f [write-safe-open $name $trans]
100 puts -nonewline $f $contents
104 proc read-file {name {trans auto}} {
106 fconfigure $f -translation $trans
112 proc write-safe-manifest {f l} {
113 set f [write-safe-open $f.files]
114 foreach i $l { puts $f $i }
118 proc old-files {conf} {
120 if {[file exists $conf.files]} {
121 set f [open $conf.files]
122 while {[gets $f line] >= 0} { lappend old $line }
137 set f "\#\# automatically generated by splitconf\n\n"
142 append f "\[$i\]\n$c\n"
149 write-safe-file $output $f
150 write-safe-manifest $output $ff
154 if {[llength $argv] != 1} { die "need exactly one filename" }
155 set conf [lindex $argv 0]
156 set old [old-files $conf]
158 foreach i $old { write-safe-delete $i }
159 write-safe-delete $conf.files
163 if {[llength $argv] != 1} { die "need exactly one filename" }
164 set conf [lindex $argv 0]
165 set old [old-files $conf]
172 while {[gets $c line] >= 0} {
173 if {[regexp -- {^\[(.*)\]\s*$} $line . name]} {
174 if {[info exists o]} {
175 puts -nonewline $o $file
178 exec "sh" "-c" $opt(before)
180 set name "$opt(prefix)$name"
181 set o [write-safe-open $name]
185 } elseif {[info exists o]} {
186 switch -regexp -- $line {
187 {^\s*$} { append spill "$line\n" }
189 {^\!} { append file "$spill[string range $line 1 end]\n" }
190 default { append file "$spill$line\n" }
192 } elseif {[regexp -- {^\s*(\#|$)} $line]} {
194 } elseif {[regexp -- {^\s*([-\w]+)\s*=\s*(.*\S|)\s*$} $line . k v]} {
195 if {![info exists opt($k)]} {
196 error "unknown configuration option `$k'"
201 error "unknown preamble directive"
204 if {[info exists o]} {
205 puts -nonewline $o $file
210 if {![info exists new($i)]} { write-safe-delete $i }
212 write-safe-manifest $conf [array names new]
214 exec "sh" "-c" $opt(after)