#A basic utility script to cat a file to another with optional string prefix and suffix #does not interpret escapes e.g \n in arguments - review #Used for example by zig.build or other systems to avoid problems with redirecting echo/cat etc #2024 - zig.build.addSystemCommand doesn't seem to support pipelines #padding #padding #padding #padding #padding #padding #padding set usage "usage: interpreter scriptname -startnl 0|1 -prefix -prefixnl 0|1 -suffix -suffixnl 0|1 -input -inputnl 0|1 -output " set defaults [dict create\ -startnl 0\ -crlf 0\ -prefix ""\ -prefixnl 0\ -suffix ""\ -suffixnl 0\ -input ""\ -inputnl 0\ -output \uFFEF\ ] if {"windows" eq $::tcl_platform(platform)} { package require punk::winrun package require twapi set rawcmdline [twapi::get_process_commandline [pid]] set allargs [punk::winrun::unquote_wintcl $rawcmdline] #first 2 args are the interpreter and the script set scriptargs [lrange $allargs 2 end] } else { set scriptargs $::argv } #puts stdout "scriptargs:$scriptargs" if {[llength $scriptargs] % 2 != 0} { puts stderr $usage exit 1 } set opts [dict merge $defaults $scriptargs] #puts stdout "opts:$opts" if {[dict get $opts -output] eq "\uFFEF"} { puts stderr $usage exit 2 } set infile [dict get $opts -input] set filedata "" if {$infile ne ""} { if {![file exists $infile]} { puts stderr "Unable to read input file '$infile'" exit 3 } set fd [open $infile r] set filedata [read $fd] close $fd } set startnl "" set prefixnl "" set suffixnl "" set inputnl "" if {[dict get $opts -startnl]} { set startnl \n } if {[dict get $opts -prefixnl]} { set prefixnl \n } if {[dict get $opts -suffixnl]} { set suffixnl \n } if {[dict get $opts -inputnl]} { set inputnl \n } set data "$startnl[dict get $opts -prefix]$prefixnl$filedata$inputnl[dict get $opts -suffix]$suffixnl" set fdout [open [dict get $opts -output] w] if {[dict get $opts -crlf] == 0} { chan configure $fdout -translation binary } puts -nonewline $fdout $data close $fdout catch {puts stdout ok} exit 0