#!/usr/local/bin/tclsh

# This isn't what you think it is.  It converts a text file to a "C" string
# literal containing the whole file.  It takes pairs of command line arguments.
# The first (a quoted string such as "char itsname [] =") is echoed to
# stdout.  The second (a filename) is read.  The following transformations are
# done to each line:
#	Remove comments (/^#/ or /;\w*#/ where \w = any whitespace)
#	Remove leading and trailing whitespace
#	Remove empty lines
#	Perform inverse backslash interpretation:
#	    tab -> \t, \ -> \\, " -> \"
#	Surround with "content\n" and write on stdout.
# The last line is followed by ';'

for {set i 0} {$i < [llength $argv]} {incr i} {
    set head [lindex $argv $i]
    incr i
    set filename [lindex $argv $i]
    set FD [open $filename r]
    puts "/* Machine generated from $filename; edit the source, not this file! */\n$head"
    while {[gets $FD line] >= 0} {
	regsub "(^|;)\[ \t\]*#.*\$" $line {} line	;#Remove comments
	set line [string trim $line]			; #Lose whitespace
	if {$line == ""} continue			;#Lose empty lines
	regsub -all {["\\]} $line {\\&} line		;#Escape quote, backsl.
	puts "\"$line\\n\""
    }
    puts ";"
}
