Annotation For lib/xvfs/xvfs.tcl

Origin for each line in lib/xvfs/xvfs.tcl from check-in 3c8c52a9f8:

47dcf5fc27 2019-05-01    1: #! /usr/bin/env tclsh
47dcf5fc27 2019-05-01    2: 
47dcf5fc27 2019-05-01    3: namespace eval ::xvfs {}
0e8537c65f 2019-09-20    4: namespace eval ::xvfs::callback {}
2b7fa3a8fa 2019-09-20    5: 
2b7fa3a8fa 2019-09-20    6: set ::xvfs::_xvfsDir [file dirname [info script]]
47dcf5fc27 2019-05-01    7: 
47dcf5fc27 2019-05-01    8: # Functions
0bdbe4333e 2019-09-20    9: proc ::xvfs::_emitLine {line} {
12383d30b7 2019-09-20   10: 	if {[info command ::minirivet::_emitOutput] ne ""} {
12383d30b7 2019-09-20   11: 		::minirivet::_emitOutput "${line}\n"
12383d30b7 2019-09-20   12: 	} else {
12383d30b7 2019-09-20   13: 		puts $line
12383d30b7 2019-09-20   14: 	}
0bdbe4333e 2019-09-20   15: }
0bdbe4333e 2019-09-20   16: 
47dcf5fc27 2019-05-01   17: proc ::xvfs::printHelp {channel {errors ""}} {
47dcf5fc27 2019-05-01   18: 	if {[llength $errors] != 0} {
47dcf5fc27 2019-05-01   19: 		foreach error $errors {
702c74c153 2019-09-20   20: 			puts $channel "error: $error"
47dcf5fc27 2019-05-01   21: 		}
702c74c153 2019-09-20   22: 		puts $channel ""
47dcf5fc27 2019-05-01   23: 	}
702c74c153 2019-09-20   24: 	puts $channel "Usage: dir2c \[--help\] \[--output <filename>\] --directory <rootDirectory> --name <fsName>"
47dcf5fc27 2019-05-01   25: 	flush $channel
47dcf5fc27 2019-05-01   26: }
47dcf5fc27 2019-05-01   27: 
47dcf5fc27 2019-05-01   28: proc ::xvfs::sanitizeCString {string} {
47dcf5fc27 2019-05-01   29: 	set output [join [lmap char [split $string ""] {
47dcf5fc27 2019-05-01   30: 		if {![regexp {[A-Za-z0-9./-]} $char]} {
47dcf5fc27 2019-05-01   31: 			binary scan $char H* char
47dcf5fc27 2019-05-01   32: 			set char "\\[format %03o 0x$char]"
47dcf5fc27 2019-05-01   33: 		}
47dcf5fc27 2019-05-01   34: 
47dcf5fc27 2019-05-01   35: 		set char
47dcf5fc27 2019-05-01   36: 	}] ""]
47dcf5fc27 2019-05-01   37: 
47dcf5fc27 2019-05-01   38: 	return $output
47dcf5fc27 2019-05-01   39: }
47dcf5fc27 2019-05-01   40: 
32b55a907b 2019-05-02   41: proc ::xvfs::sanitizeCStringList {list {prefix ""} {width 80}} {
32b55a907b 2019-05-02   42: 	set lines [list]
32b55a907b 2019-05-02   43: 	set row [list]
32b55a907b 2019-05-02   44: 	foreach item $list {
32b55a907b 2019-05-02   45: 		lappend row "\"[sanitizeCString $item]\""
32b55a907b 2019-05-02   46: 		
32b55a907b 2019-05-02   47: 		set rowString [join $row {, }]
32b55a907b 2019-05-02   48: 		set rowString "${prefix}${rowString}"
32b55a907b 2019-05-02   49: 		if {[string length $rowString] > $width} {
32b55a907b 2019-05-02   50: 			set row [list]
d961175fd9 2019-09-20   51: 			lappend lines "${rowString},"
32b55a907b 2019-05-02   52: 			unset rowString
32b55a907b 2019-05-02   53: 		}
32b55a907b 2019-05-02   54: 	}
32b55a907b 2019-05-02   55: 	if {[info exists rowString]} {
32b55a907b 2019-05-02   56: 		lappend lines $rowString
32b55a907b 2019-05-02   57: 	}
32b55a907b 2019-05-02   58: 	
32b55a907b 2019-05-02   59: 	return [join $lines "\n"]
32b55a907b 2019-05-02   60: }
32b55a907b 2019-05-02   61: 
47dcf5fc27 2019-05-01   62: proc ::xvfs::binaryToCHex {binary {prefix ""} {width 10}} {
2176e9cacf 2019-09-18   63: 	set binary [binary encode hex $binary]
47dcf5fc27 2019-05-01   64: 	set output [list]
47dcf5fc27 2019-05-01   65: 
47dcf5fc27 2019-05-01   66: 	set width [expr {$width * 2}]
47dcf5fc27 2019-05-01   67: 	set stopAt [expr {$width - 1}]
47dcf5fc27 2019-05-01   68: 
2176e9cacf 2019-09-18   69: 	set offset 0
2176e9cacf 2019-09-18   70: 	while 1 {
2176e9cacf 2019-09-18   71: 		set row [string range $binary $offset [expr {$offset + $stopAt}]]
2176e9cacf 2019-09-18   72: 		if {[string length $row] == 0} {
2176e9cacf 2019-09-18   73: 			break
2176e9cacf 2019-09-18   74: 		}
2176e9cacf 2019-09-18   75: 		incr offset [string length $row]
47dcf5fc27 2019-05-01   76: 
47dcf5fc27 2019-05-01   77: 		set rowOutput [list]
47dcf5fc27 2019-05-01   78: 		while {$row ne ""} {
47dcf5fc27 2019-05-01   79: 			set value [string range $row 0 1]
47dcf5fc27 2019-05-01   80: 			set row [string range $row 2 end]
47dcf5fc27 2019-05-01   81: 
47dcf5fc27 2019-05-01   82: 			lappend rowOutput "\\x$value"
47dcf5fc27 2019-05-01   83: 		}
47dcf5fc27 2019-05-01   84: 		set rowOutput [join $rowOutput {}]
47dcf5fc27 2019-05-01   85: 		set rowOutput "${prefix}\"${rowOutput}\""
47dcf5fc27 2019-05-01   86: 		lappend output $rowOutput
47dcf5fc27 2019-05-01   87: 	}
47dcf5fc27 2019-05-01   88: 
47dcf5fc27 2019-05-01   89: 	if {[llength $output] == 0} {
47dcf5fc27 2019-05-01   90: 		return "${prefix}\"\""
47dcf5fc27 2019-05-01   91: 	}
47dcf5fc27 2019-05-01   92: 
47dcf5fc27 2019-05-01   93: 	set output [join $output "\n"]
47dcf5fc27 2019-05-01   94: }
47dcf5fc27 2019-05-01   95: 
47dcf5fc27 2019-05-01   96: proc ::xvfs::processFile {fsName inputFile outputFile fileInfoDict} {
47dcf5fc27 2019-05-01   97: 	array set fileInfo $fileInfoDict
47dcf5fc27 2019-05-01   98: 
47dcf5fc27 2019-05-01   99: 	switch -exact -- $fileInfo(type) {
47dcf5fc27 2019-05-01  100: 		"file" {
47dcf5fc27 2019-05-01  101: 			set type "XVFS_FILE_TYPE_REG"
d8e00cd4a3 2019-09-20  102: 			if {[info exists fileInfo(fileContents)]} {
d8e00cd4a3 2019-09-20  103: 				set data $fileInfo(fileContents)
d8e00cd4a3 2019-09-20  104: 			} else {
d8e00cd4a3 2019-09-20  105: 				set fd [open $inputFile]
d8e00cd4a3 2019-09-20  106: 				fconfigure $fd -encoding binary -translation binary -blocking true
d8e00cd4a3 2019-09-20  107: 				set data [read $fd]
d8e00cd4a3 2019-09-20  108: 				close $fd
d8e00cd4a3 2019-09-20  109: 			}
47dcf5fc27 2019-05-01  110: 			set size [string length $data]
47dcf5fc27 2019-05-01  111: 			set data [string trimleft [binaryToCHex $data "\t\t\t"]]
47dcf5fc27 2019-05-01  112: 		}
47dcf5fc27 2019-05-01  113: 		"directory" {
47dcf5fc27 2019-05-01  114: 			set type "XVFS_FILE_TYPE_DIR"
32b55a907b 2019-05-02  115: 			set children $fileInfo(children)
32b55a907b 2019-05-02  116: 			set size [llength $children]
32b55a907b 2019-05-02  117: 			
32b55a907b 2019-05-02  118: 			if {$size == 0} {
32b55a907b 2019-05-02  119: 				set children "NULL"
32b55a907b 2019-05-02  120: 			} else {
32b55a907b 2019-05-02  121: 				set children [string trimleft [sanitizeCStringList $children "\t\t\t"]]
32b55a907b 2019-05-02  122: 				# This initializes it using a C99 compound literal, C99 is required
32b55a907b 2019-05-02  123: 				set children "(const char *\[\]) \{$children\}"
32b55a907b 2019-05-02  124: 			}
47dcf5fc27 2019-05-01  125: 		}
47dcf5fc27 2019-05-01  126: 		default {
47dcf5fc27 2019-05-01  127: 			return -code error "Unable to process $inputFile, unknown type: $fileInfo(type)"
47dcf5fc27 2019-05-01  128: 		}
47dcf5fc27 2019-05-01  129: 	}
47dcf5fc27 2019-05-01  130: 
0bdbe4333e 2019-09-20  131: 	::xvfs::_emitLine "\t\{"
0bdbe4333e 2019-09-20  132: 	::xvfs::_emitLine "\t\t.name = \"[sanitizeCString $outputFile]\","
0bdbe4333e 2019-09-20  133: 	::xvfs::_emitLine "\t\t.type = $type,"
0bdbe4333e 2019-09-20  134: 	::xvfs::_emitLine "\t\t.size = $size,"
32b55a907b 2019-05-02  135: 	switch -exact -- $fileInfo(type) {
32b55a907b 2019-05-02  136: 		"file" {
0bdbe4333e 2019-09-20  137: 			::xvfs::_emitLine "\t\t.data.fileContents = (const unsigned char *) $data"
32b55a907b 2019-05-02  138: 		}
32b55a907b 2019-05-02  139: 		"directory" {
0bdbe4333e 2019-09-20  140: 			::xvfs::_emitLine "\t\t.data.dirChildren  = $children"
32b55a907b 2019-05-02  141: 		}
32b55a907b 2019-05-02  142: 	}
0bdbe4333e 2019-09-20  143: 	::xvfs::_emitLine "\t\},"
47dcf5fc27 2019-05-01  144: }
47dcf5fc27 2019-05-01  145: 
47dcf5fc27 2019-05-01  146: proc ::xvfs::processDirectory {fsName directory {subDirectory ""}} {
47dcf5fc27 2019-05-01  147: 	set subDirectories [list]
47dcf5fc27 2019-05-01  148: 	set outputFiles [list]
47dcf5fc27 2019-05-01  149: 	set workingDirectory [file join $directory $subDirectory]
47dcf5fc27 2019-05-01  150: 	set outputDirectory $subDirectory
47dcf5fc27 2019-05-01  151: 
47dcf5fc27 2019-05-01  152: 	if {$subDirectory eq ""} {
47dcf5fc27 2019-05-01  153: 		set isTopLevel true
47dcf5fc27 2019-05-01  154: 	} else {
47dcf5fc27 2019-05-01  155: 		set isTopLevel false
47dcf5fc27 2019-05-01  156: 	}
47dcf5fc27 2019-05-01  157: 
47dcf5fc27 2019-05-01  158: 	if {$isTopLevel} {
0bdbe4333e 2019-09-20  159: 		::xvfs::_emitLine "static const struct xvfs_file_data xvfs_${fsName}_data\[\] = \{"
47dcf5fc27 2019-05-01  160: 	}
47dcf5fc27 2019-05-01  161: 
47dcf5fc27 2019-05-01  162: 	# XXX:TODO: Include hidden files ?
32b55a907b 2019-05-02  163: 	set children [list]
47dcf5fc27 2019-05-01  164: 	foreach file [glob -nocomplain -tails -directory $workingDirectory *] {
47dcf5fc27 2019-05-01  165: 		if {$file in {. ..}} {
47dcf5fc27 2019-05-01  166: 			continue
47dcf5fc27 2019-05-01  167: 		}
47dcf5fc27 2019-05-01  168: 
47dcf5fc27 2019-05-01  169: 		set inputFile [file join $workingDirectory $file]
d99958bdd3 2019-05-03  170: 		set outputFile [file join $outputDirectory [encoding convertto utf-8 $file]]
30c469fcf7 2019-09-20  171: 		set subDirectoryName [file join $outputDirectory $file]
0e8537c65f 2019-09-20  172: 
0e8537c65f 2019-09-20  173: 		if {[info command ::xvfs::callback::setOutputFileName] ne ""} {
ed3da129b8 2019-09-20  174: 			set outputFile [::xvfs::callback::setOutputFileName $file $workingDirectory $inputFile $outputDirectory $outputFile]
e592c85e70 2019-09-20  175: 			if {$outputFile eq "/"} {
0e8537c65f 2019-09-20  176: 				continue
0e8537c65f 2019-09-20  177: 			}
0e8537c65f 2019-09-20  178: 		}
47dcf5fc27 2019-05-01  179: 
47dcf5fc27 2019-05-01  180: 		unset -nocomplain fileInfo
47dcf5fc27 2019-05-01  181: 		catch {
47dcf5fc27 2019-05-01  182: 			file lstat $inputFile fileInfo
47dcf5fc27 2019-05-01  183: 		}
47dcf5fc27 2019-05-01  184: 		if {![info exists fileInfo]} {
30ffb49c05 2019-09-20  185: 			puts stderr "warning: Unable to access $inputFile, skipping"
47dcf5fc27 2019-05-01  186: 		}
47dcf5fc27 2019-05-01  187: 
47dcf5fc27 2019-05-01  188: 		if {$fileInfo(type) eq "directory"} {
30c469fcf7 2019-09-20  189: 			lappend subDirectories $subDirectoryName
32b55a907b 2019-05-02  190: 			continue
47dcf5fc27 2019-05-01  191: 		}
47dcf5fc27 2019-05-01  192: 
47dcf5fc27 2019-05-01  193: 		processFile $fsName $inputFile $outputFile [array get fileInfo]
47dcf5fc27 2019-05-01  194: 		lappend outputFiles $outputFile
47dcf5fc27 2019-05-01  195: 	}
47dcf5fc27 2019-05-01  196: 
47dcf5fc27 2019-05-01  197: 	foreach subDirectory $subDirectories {
47dcf5fc27 2019-05-01  198: 		lappend outputFiles {*}[processDirectory $fsName $directory $subDirectory]
47dcf5fc27 2019-05-01  199: 	}
32b55a907b 2019-05-02  200: 	
32b55a907b 2019-05-02  201: 	set inputFile $directory
32b55a907b 2019-05-02  202: 	set outputFile $outputDirectory
e592c85e70 2019-09-20  203: 	if {[info command ::xvfs::callback::setOutputFileName] ne ""} {
e592c85e70 2019-09-20  204: 		set outputFile [::xvfs::callback::setOutputFileName $directory $directory $inputFile $outputDirectory $outputFile]
e592c85e70 2019-09-20  205: 	}
32b55a907b 2019-05-02  206: 
e592c85e70 2019-09-20  207: 	if {$outputFile ne "/"} {
e592c85e70 2019-09-20  208: 		unset -nocomplain fileInfo
e592c85e70 2019-09-20  209: 		file stat $inputFile fileInfo
30c469fcf7 2019-09-20  210: 		set children [list]
30c469fcf7 2019-09-20  211: 		set outputFileLen [string length $outputFile]
30c469fcf7 2019-09-20  212: 		foreach child $outputFiles {
30c469fcf7 2019-09-20  213: 			if {[string range /$child 0 $outputFileLen] eq "/${outputFile}"} {
30c469fcf7 2019-09-20  214: 				set child [string trimleft [string range $child $outputFileLen end] /]
30c469fcf7 2019-09-20  215: 				if {![string match "*/*" $child]} {
30c469fcf7 2019-09-20  216: 					lappend children $child
30c469fcf7 2019-09-20  217: 				}
30c469fcf7 2019-09-20  218: 			}
30c469fcf7 2019-09-20  219: 		}
e592c85e70 2019-09-20  220: 		set fileInfo(children) $children
e592c85e70 2019-09-20  221: 
e592c85e70 2019-09-20  222: 		processFile $fsName $inputFile $outputFile [array get fileInfo]
e592c85e70 2019-09-20  223: 		lappend outputFiles $outputFile
e592c85e70 2019-09-20  224: 	}
47dcf5fc27 2019-05-01  225: 
47dcf5fc27 2019-05-01  226: 	if {$isTopLevel} {
d8e00cd4a3 2019-09-20  227: 		if {[info command ::xvfs::callback::addOutputFiles] ne ""} {
e592c85e70 2019-09-20  228: 			lappend outputFiles {*}[::xvfs::callback::addOutputFiles $fsName]
d8e00cd4a3 2019-09-20  229: 		}
d8e00cd4a3 2019-09-20  230: 
0bdbe4333e 2019-09-20  231: 		::xvfs::_emitLine "\};"
47dcf5fc27 2019-05-01  232: 	}
47dcf5fc27 2019-05-01  233: 
47dcf5fc27 2019-05-01  234: 	return $outputFiles
47dcf5fc27 2019-05-01  235: }
47dcf5fc27 2019-05-01  236: 
47dcf5fc27 2019-05-01  237: proc ::xvfs::main {argv} {
47dcf5fc27 2019-05-01  238: 	# Main entry point
47dcf5fc27 2019-05-01  239: 	## 1. Parse arguments
47dcf5fc27 2019-05-01  240: 	if {[llength $argv] % 2 != 0} {
47dcf5fc27 2019-05-01  241: 		lappend argv ""
47dcf5fc27 2019-05-01  242: 	}
47dcf5fc27 2019-05-01  243: 
47dcf5fc27 2019-05-01  244: 	foreach {arg val} $argv {
47dcf5fc27 2019-05-01  245: 		switch -exact -- $arg {
47dcf5fc27 2019-05-01  246: 			"--help" {
47dcf5fc27 2019-05-01  247: 				printHelp stdout
47dcf5fc27 2019-05-01  248: 				exit 0
47dcf5fc27 2019-05-01  249: 			}
47dcf5fc27 2019-05-01  250: 			"--directory" {
47dcf5fc27 2019-05-01  251: 				set rootDirectory $val
47dcf5fc27 2019-05-01  252: 			}
47dcf5fc27 2019-05-01  253: 			"--name" {
47dcf5fc27 2019-05-01  254: 				set fsName $val
0bdbe4333e 2019-09-20  255: 			}
09e53d3c38 2019-09-20  256: 			"--output" - "--header" {
0bdbe4333e 2019-09-20  257: 				# Ignored, handled as part of some other process
32b55a907b 2019-05-02  258: 			}
47dcf5fc27 2019-05-01  259: 			default {
47dcf5fc27 2019-05-01  260: 				printHelp stderr [list "Invalid option: $arg $val"]
47dcf5fc27 2019-05-01  261: 				exit 1
47dcf5fc27 2019-05-01  262: 			}
47dcf5fc27 2019-05-01  263: 		}
47dcf5fc27 2019-05-01  264: 	}
47dcf5fc27 2019-05-01  265: 
47dcf5fc27 2019-05-01  266: 	## 2. Validate arguments
47dcf5fc27 2019-05-01  267: 	set errors [list]
47dcf5fc27 2019-05-01  268: 	if {![info exists rootDirectory]} {
47dcf5fc27 2019-05-01  269: 		lappend errors "--directory must be specified"
47dcf5fc27 2019-05-01  270: 	}
47dcf5fc27 2019-05-01  271: 	if {![info exists fsName]} {
47dcf5fc27 2019-05-01  272: 		lappend errors "--name must be specified"
47dcf5fc27 2019-05-01  273: 	}
47dcf5fc27 2019-05-01  274: 
47dcf5fc27 2019-05-01  275: 	if {[llength $errors] != 0} {
47dcf5fc27 2019-05-01  276: 		printHelp stderr $errors
47dcf5fc27 2019-05-01  277: 		exit 1
47dcf5fc27 2019-05-01  278: 	}
47dcf5fc27 2019-05-01  279: 
47dcf5fc27 2019-05-01  280: 	## 3. Start processing directory and producing initial output
32b55a907b 2019-05-02  281: 	set ::xvfs::outputFiles [processDirectory $fsName $rootDirectory]
47dcf5fc27 2019-05-01  282: 
47dcf5fc27 2019-05-01  283: 	set ::xvfs::fsName $fsName
47dcf5fc27 2019-05-01  284: 	set ::xvfs::rootDirectory $rootDirectory
2b7fa3a8fa 2019-09-20  285: }
2b7fa3a8fa 2019-09-20  286: 
e592c85e70 2019-09-20  287: proc ::xvfs::run {args} {
d36db7c01b 2019-09-20  288: 	uplevel #0 { package require minirivet }
3cb72a0d20 2019-09-20  289: 
e592c85e70 2019-09-20  290: 	set ::xvfs::argv $args
2b7fa3a8fa 2019-09-20  291: 	::minirivet::parse [file join $::xvfs::_xvfsDir xvfs.c.rvt]
2b7fa3a8fa 2019-09-20  292: }
2b7fa3a8fa 2019-09-20  293: 
d36db7c01b 2019-09-20  294: proc ::xvfs::setOutputChannel {channel} {
d36db7c01b 2019-09-20  295: 	uplevel #0 { package require minirivet }
d36db7c01b 2019-09-20  296: 	tailcall ::minirivet::setOutputChannel $channel
d36db7c01b 2019-09-20  297: }
d36db7c01b 2019-09-20  298: 
d36db7c01b 2019-09-20  299: proc ::xvfs::setOutputVariable {variable} {
d36db7c01b 2019-09-20  300: 	uplevel #0 { package require minirivet }
d36db7c01b 2019-09-20  301: 	tailcall ::minirivet::setOutputVariable $variable
09e53d3c38 2019-09-20  302: }
09e53d3c38 2019-09-20  303: 
09e53d3c38 2019-09-20  304: proc ::xvfs::staticIncludeHeaderData {headerData} {
09e53d3c38 2019-09-20  305: 	set ::xvfs::xvfsCoreH $headerData
09e53d3c38 2019-09-20  306: }
09e53d3c38 2019-09-20  307: 
09e53d3c38 2019-09-20  308: proc ::xvfs::staticIncludeHeader {pathToHeaderFile} {
09e53d3c38 2019-09-20  309: 	set fd [open $pathToHeaderFile]
09e53d3c38 2019-09-20  310: 	::xvfs::staticIncludeHeaderData [read $fd]
09e53d3c38 2019-09-20  311: 	close $fd
a719156faf 2019-10-09  312: }
a719156faf 2019-10-09  313: 
37d00c3cfb 2019-11-04  314: proc ::xvfs::_tryFit {list} {
37d00c3cfb 2019-11-04  315: 	set idx -1
37d00c3cfb 2019-11-04  316: 	set lastItem -100000
37d00c3cfb 2019-11-04  317: 	foreach item $list {
37d00c3cfb 2019-11-04  318: 		incr idx
37d00c3cfb 2019-11-04  319: 
37d00c3cfb 2019-11-04  320: 		if {$item <= $lastItem} {
37d00c3cfb 2019-11-04  321: 			return ""
37d00c3cfb 2019-11-04  322: 		}
37d00c3cfb 2019-11-04  323: 
37d00c3cfb 2019-11-04  324: 		set difference [expr {$item - $idx}]
37d00c3cfb 2019-11-04  325: 		if {$idx != 0} {
37d00c3cfb 2019-11-04  326: 			set divisor [expr {$item / $idx}]
37d00c3cfb 2019-11-04  327: 		} else {
37d00c3cfb 2019-11-04  328: 			set divisor 1
37d00c3cfb 2019-11-04  329: 		}
37d00c3cfb 2019-11-04  330: 		lappend differences $difference
37d00c3cfb 2019-11-04  331: 		lappend divisors $divisor
37d00c3cfb 2019-11-04  332: 
37d00c3cfb 2019-11-04  333: 		set lastItem $item
37d00c3cfb 2019-11-04  334: 	}
37d00c3cfb 2019-11-04  335: 
37d00c3cfb 2019-11-04  336: 	foreach divisor [lrange $divisors 1 end] {
37d00c3cfb 2019-11-04  337: 		incr divisorCount
37d00c3cfb 2019-11-04  338: 		incr divisorValue $divisor
37d00c3cfb 2019-11-04  339: 	}
37d00c3cfb 2019-11-04  340: 	set divisor [expr {$divisorValue / $divisorCount}]
37d00c3cfb 2019-11-04  341: 
37d00c3cfb 2019-11-04  342: 	for {set i 0} {$i < [llength $list]} {incr i} {
37d00c3cfb 2019-11-04  343: 		lappend outList $i
37d00c3cfb 2019-11-04  344: 	}
37d00c3cfb 2019-11-04  345: 
37d00c3cfb 2019-11-04  346: 	set mapFunc " - ${difference}"
37d00c3cfb 2019-11-04  347: 
37d00c3cfb 2019-11-04  348: 	set newList [lmap v $list { expr "\$v${mapFunc}" }]
37d00c3cfb 2019-11-04  349: 	if {$newList eq $outList} {
37d00c3cfb 2019-11-04  350: 		return $mapFunc
37d00c3cfb 2019-11-04  351: 	}
37d00c3cfb 2019-11-04  352: 
37d00c3cfb 2019-11-04  353: 	if {$divisor != 1} {
37d00c3cfb 2019-11-04  354: 		set mapFunc " / ${divisor}"
37d00c3cfb 2019-11-04  355: 		set newList [lmap v $list { expr "\$v${mapFunc}" }]
37d00c3cfb 2019-11-04  356: 		if {$newList eq $outList} {
37d00c3cfb 2019-11-04  357: 			return $mapFunc
37d00c3cfb 2019-11-04  358: 		}
37d00c3cfb 2019-11-04  359: 
37d00c3cfb 2019-11-04  360: 		set subMapFunc [_tryFit $newList]
37d00c3cfb 2019-11-04  361: 		if {$subMapFunc != ""} {
37d00c3cfb 2019-11-04  362: 			return " / ${divisor}${subMapFunc}"
37d00c3cfb 2019-11-04  363: 		}
37d00c3cfb 2019-11-04  364: 	}
37d00c3cfb 2019-11-04  365: 
37d00c3cfb 2019-11-04  366: 	return ""
37d00c3cfb 2019-11-04  367: }
37d00c3cfb 2019-11-04  368: 
f615eecc64 2019-10-10  369: proc ::xvfs::generatePerfectHashFunctionCall {cVarName cVarLength invalidValue nameList args} {
37d00c3cfb 2019-11-04  370: 	# Manage config
37d00c3cfb 2019-11-04  371: 	## Default config
f615eecc64 2019-10-10  372: 	array set config {
37d00c3cfb 2019-11-04  373: 		useCacheFirst  false
37d00c3cfb 2019-11-04  374: 		cacheValue     true
37d00c3cfb 2019-11-04  375: 		enableCache    false
f615eecc64 2019-10-10  376: 	}
37d00c3cfb 2019-11-04  377: 	set config(cacheFile) [file join [file normalize ~/.cache] xvfs phf-cache.db]
f615eecc64 2019-10-10  378: 
37d00c3cfb 2019-11-04  379: 	## User config
f615eecc64 2019-10-10  380: 	foreach {configKey configVal} $args {
f615eecc64 2019-10-10  381: 		if {![info exists config($configKey)]} {
f615eecc64 2019-10-10  382: 			error "Invalid option: $configKey"
f615eecc64 2019-10-10  383: 		}
f615eecc64 2019-10-10  384: 	}
f615eecc64 2019-10-10  385: 	array set config $args
f615eecc64 2019-10-10  386: 
37d00c3cfb 2019-11-04  387: 	if {$config(enableCache)} {
37d00c3cfb 2019-11-04  388: 		package require sqlite3
37d00c3cfb 2019-11-04  389: 	}
37d00c3cfb 2019-11-04  390: 
37d00c3cfb 2019-11-04  391: 	# Adjustment for computing the expense of a function call by its length
37d00c3cfb 2019-11-04  392: 	# Calls that take longer should be made longer, so make CRC32 longer
37d00c3cfb 2019-11-04  393: 	# than Adler32
37d00c3cfb 2019-11-04  394: 	set lengthAdjustment [list Tcl_ZlibCRC32 Tcl_CRCxxx32]
37d00c3cfb 2019-11-04  395: 
37d00c3cfb 2019-11-04  396: 	# Check for a cached entry
37d00c3cfb 2019-11-04  397: 	if {$config(enableCache) && $config(useCacheFirst)} {
37d00c3cfb 2019-11-04  398: 		catch {
37d00c3cfb 2019-11-04  399: 			set hashKey $nameList
37d00c3cfb 2019-11-04  400: 
37d00c3cfb 2019-11-04  401: 			sqlite3 ::xvfs::phfCache $config(cacheFile)
37d00c3cfb 2019-11-04  402: 			::xvfs::phfCache eval {CREATE TABLE IF NOT EXISTS cache(hashKey PRIMARY KEY, function BLOB);}
37d00c3cfb 2019-11-04  403: 			::xvfs::phfCache eval {SELECT function FROM cache WHERE hashKey = $hashKey LIMIT 1;} cacheRow {}
37d00c3cfb 2019-11-04  404: 		}
37d00c3cfb 2019-11-04  405: 		catch {
37d00c3cfb 2019-11-04  406: 			::xvfs::phfCache close
37d00c3cfb 2019-11-04  407: 		}
37d00c3cfb 2019-11-04  408: 
37d00c3cfb 2019-11-04  409: 		if {[info exists cacheRow(function)]} {
37d00c3cfb 2019-11-04  410: 			set phfCall $cacheRow(function)
37d00c3cfb 2019-11-04  411: 			set phfCall [string map [list @@CVARNAME@@ $cVarName @@CVARLENGTH@@ $cVarLength @@INVALIDVALUE@@ $invalidValue] $phfCall]
37d00c3cfb 2019-11-04  412: 
37d00c3cfb 2019-11-04  413: 			return $phfCall
37d00c3cfb 2019-11-04  414: 		}
37d00c3cfb 2019-11-04  415: 	}
37d00c3cfb 2019-11-04  416: 
37d00c3cfb 2019-11-04  417: 	set minVal 0
37d00c3cfb 2019-11-04  418: 	set maxVal [llength $nameList]
37d00c3cfb 2019-11-04  419: 	set testExpr_(0) {[zlib adler32 $nameItem $alpha] % $gamma}
37d00c3cfb 2019-11-04  420: 	set testExpr(1) {[zlib crc32 $nameItem $alpha] % $gamma}
37d00c3cfb 2019-11-04  421: 	set testExpr_(2) {[zlib adler32 $nameItem [zlib crc32 $nameItem $alpha]] % $gamma}
37d00c3cfb 2019-11-04  422: 	set testExpr_(3) {[zlib crc32 $nameItem [zlib adler32 $nameItem $alpha]] % $gamma}
37d00c3cfb 2019-11-04  423: 	set testExprC(0) {((Tcl_ZlibAdler32(${alpha}LU, (unsigned char *) @@CVARNAME@@, @@CVARLENGTH@@) % ${gamma}LU)${fitMod})}
37d00c3cfb 2019-11-04  424: 	set testExprC(1) {((Tcl_ZlibCRC32(${alpha}LU, (unsigned char *) @@CVARNAME@@, @@CVARLENGTH@@) % ${gamma}LU)${fitMod})}
37d00c3cfb 2019-11-04  425: 	set testExprC(2) {((Tcl_ZlibAdler32(Tcl_ZlibCRC32(${alpha}LU, (unsigned char *) @@CVARNAME@@, @@CVARLENGTH@@), (unsigned char *) @@CVARNAME@@, @@CVARLENGTH@@) % ${gamma}LU)${fitMod})}
37d00c3cfb 2019-11-04  426: 	set testExprC(3) {((Tcl_ZlibCRC32(Tcl_ZlibAdler32(${alpha}LU, (unsigned char *) @@CVARNAME@@, @@CVARLENGTH@@), (unsigned char *) @@CVARNAME@@, @@CVARLENGTH@@) % ${gamma}LU)${fitMod})}
37d00c3cfb 2019-11-04  427: 
37d00c3cfb 2019-11-04  428: 	# Short-circuit for known cases
37d00c3cfb 2019-11-04  429: 	if {$maxVal == 1} {
37d00c3cfb 2019-11-04  430: 		return 0
37d00c3cfb 2019-11-04  431: 	}
37d00c3cfb 2019-11-04  432: 
37d00c3cfb 2019-11-04  433: 	set round -1
37d00c3cfb 2019-11-04  434: 
37d00c3cfb 2019-11-04  435: 	while true {
37d00c3cfb 2019-11-04  436: 		incr round
37d00c3cfb 2019-11-04  437: 
37d00c3cfb 2019-11-04  438: 		set gamma [expr {$maxVal + ($round % ($maxVal * 128))}]
37d00c3cfb 2019-11-04  439: 		set alpha [expr {$round / 6}]
37d00c3cfb 2019-11-04  440: 
37d00c3cfb 2019-11-04  441: 		foreach {testExprID testExprContents} [array get testExpr] {
37d00c3cfb 2019-11-04  442: 			set unFitList [list]
37d00c3cfb 2019-11-04  443: 			foreach nameItem $nameList {
37d00c3cfb 2019-11-04  444: 				set testExprVal [expr $testExprContents]
37d00c3cfb 2019-11-04  445: 				lappend unFitList $testExprVal
37d00c3cfb 2019-11-04  446: 			}
37d00c3cfb 2019-11-04  447: 
37d00c3cfb 2019-11-04  448: 			set failed false
37d00c3cfb 2019-11-04  449: 			set fitMod [_tryFit $unFitList]
37d00c3cfb 2019-11-04  450: 			if {$fitMod eq ""} {
37d00c3cfb 2019-11-04  451: 				set failed true
f615eecc64 2019-10-10  452: 			}
f615eecc64 2019-10-10  453: 
f615eecc64 2019-10-10  454: 			if {!$failed} {
f615eecc64 2019-10-10  455: 				break
f615eecc64 2019-10-10  456: 			}
a719156faf 2019-10-09  457: 		}
a719156faf 2019-10-09  458: 
a719156faf 2019-10-09  459: 		if {!$failed} {
a719156faf 2019-10-09  460: 			break
a719156faf 2019-10-09  461: 		}
37d00c3cfb 2019-11-04  462: 
37d00c3cfb 2019-11-04  463: 	}
37d00c3cfb 2019-11-04  464: 
37d00c3cfb 2019-11-04  465: 	set phfCall [string map [list { - 0LU} ""] [subst $testExprC($testExprID)]]
37d00c3cfb 2019-11-04  466: 
37d00c3cfb 2019-11-04  467: 	# Check cache for a better answer
37d00c3cfb 2019-11-04  468: 	if {$config(enableCache)} {
37d00c3cfb 2019-11-04  469: 		catch {
37d00c3cfb 2019-11-04  470: 			set hashKey $nameList
37d00c3cfb 2019-11-04  471: 			set cacheDir [file dirname $config(cacheFile)]
37d00c3cfb 2019-11-04  472: 			file mkdir $cacheDir
37d00c3cfb 2019-11-04  473: 
37d00c3cfb 2019-11-04  474: 			unset -nocomplain cacheRow
37d00c3cfb 2019-11-04  475: 
37d00c3cfb 2019-11-04  476: 			sqlite3 ::xvfs::phfCache $config(cacheFile)
37d00c3cfb 2019-11-04  477: 			::xvfs::phfCache eval {CREATE TABLE IF NOT EXISTS cache(hashKey PRIMARY KEY, function BLOB);}
37d00c3cfb 2019-11-04  478: 			::xvfs::phfCache eval {SELECT function FROM cache WHERE hashKey = $hashKey LIMIT 1;} cacheRow {}
37d00c3cfb 2019-11-04  479: 
37d00c3cfb 2019-11-04  480: 			set updateCache false
37d00c3cfb 2019-11-04  481: 			if {[info exists cacheRow(function)]} {
37d00c3cfb 2019-11-04  482: 				if {[string length [string map $lengthAdjustment $cacheRow(function)]] <= [string length [string map $lengthAdjustment $phfCall]]} {
37d00c3cfb 2019-11-04  483: 					# Use the cached value since it is better
37d00c3cfb 2019-11-04  484: 					set phfCall $cacheRow(function)
37d00c3cfb 2019-11-04  485: 				} else {
37d00c3cfb 2019-11-04  486: 					set updateCache true
37d00c3cfb 2019-11-04  487: 				}
37d00c3cfb 2019-11-04  488: 			} else {
37d00c3cfb 2019-11-04  489: 				set updateCache true
37d00c3cfb 2019-11-04  490: 			}
37d00c3cfb 2019-11-04  491: 
37d00c3cfb 2019-11-04  492: 			if {$updateCache && $config(cacheValue)} {
37d00c3cfb 2019-11-04  493: 				# Save to cache
37d00c3cfb 2019-11-04  494: 				::xvfs::phfCache eval {INSERT OR REPLACE INTO cache (hashKey, function) VALUES ($hashKey, $phfCall);}
37d00c3cfb 2019-11-04  495: 			}
37d00c3cfb 2019-11-04  496: 		}
37d00c3cfb 2019-11-04  497: 
37d00c3cfb 2019-11-04  498: 		catch {
37d00c3cfb 2019-11-04  499: 			::xvfs::phfCache close
37d00c3cfb 2019-11-04  500: 		}
37d00c3cfb 2019-11-04  501: 	}
37d00c3cfb 2019-11-04  502: 
37d00c3cfb 2019-11-04  503: 	set phfCall [string map [list @@CVARNAME@@ $cVarName @@CVARLENGTH@@ $cVarLength @@INVALIDVALUE@@ $invalidValue] $phfCall]
a719156faf 2019-10-09  504: 
a719156faf 2019-10-09  505: 	return $phfCall
37d00c3cfb 2019-11-04  506: }
37d00c3cfb 2019-11-04  507: 
37d00c3cfb 2019-11-04  508: proc ::xvfs::generateHashTable {outCVarName cVarName cVarLength invalidValue nameList args} {
37d00c3cfb 2019-11-04  509: 	# Manage config
37d00c3cfb 2019-11-04  510: 	## Default config
37d00c3cfb 2019-11-04  511: 	array set config {
37d00c3cfb 2019-11-04  512: 		prefix        ""
37d00c3cfb 2019-11-04  513: 		hashTableSize 10
37d00c3cfb 2019-11-04  514: 		validate      0
37d00c3cfb 2019-11-04  515: 		onValidated   "break;"
37d00c3cfb 2019-11-04  516: 	}
37d00c3cfb 2019-11-04  517: 
37d00c3cfb 2019-11-04  518: 	## User config
37d00c3cfb 2019-11-04  519: 	foreach {configKey configVal} $args {
37d00c3cfb 2019-11-04  520: 		if {![info exists config($configKey)]} {
37d00c3cfb 2019-11-04  521: 			error "Invalid option: $configKey"
37d00c3cfb 2019-11-04  522: 		}
37d00c3cfb 2019-11-04  523: 	}
37d00c3cfb 2019-11-04  524: 	array set config $args
37d00c3cfb 2019-11-04  525: 
37d00c3cfb 2019-11-04  526: 	if {[llength $nameList] < $config(hashTableSize)} {
37d00c3cfb 2019-11-04  527: 		set config(hashTableSize) [llength $nameList]
37d00c3cfb 2019-11-04  528: 	}
37d00c3cfb 2019-11-04  529: 
37d00c3cfb 2019-11-04  530: 	set maxLength 0
37d00c3cfb 2019-11-04  531: 	set index -1
37d00c3cfb 2019-11-04  532: 	foreach name $nameList {
37d00c3cfb 2019-11-04  533: 		incr index
37d00c3cfb 2019-11-04  534: 		set length [string length $name]
37d00c3cfb 2019-11-04  535: 		set hash [expr {[zlib adler32 $name 0] % $config(hashTableSize)}]
37d00c3cfb 2019-11-04  536: 
37d00c3cfb 2019-11-04  537: 		lappend indexesAtLength($length) $index
37d00c3cfb 2019-11-04  538: 		lappend indexesAtHash($hash) $index
37d00c3cfb 2019-11-04  539: 
37d00c3cfb 2019-11-04  540: 		if {$length > $maxLength} {
37d00c3cfb 2019-11-04  541: 			set maxLength $length
37d00c3cfb 2019-11-04  542: 		}
37d00c3cfb 2019-11-04  543: 	}
37d00c3cfb 2019-11-04  544: 
37d00c3cfb 2019-11-04  545: 	set maxIndexes 0
37d00c3cfb 2019-11-04  546: 	foreach {hash indexes} [array get indexesAtHash] {
37d00c3cfb 2019-11-04  547: 		set indexesCount [llength $indexes]
37d00c3cfb 2019-11-04  548: 
37d00c3cfb 2019-11-04  549: 		if {$indexesCount > $maxIndexes} {
37d00c3cfb 2019-11-04  550: 			set maxIndexes $indexesCount
37d00c3cfb 2019-11-04  551: 		}
37d00c3cfb 2019-11-04  552: 	}
37d00c3cfb 2019-11-04  553: 
37d00c3cfb 2019-11-04  554: 	lappend outputHeader "${config(prefix)}long ${outCVarName}_idx;"
37d00c3cfb 2019-11-04  555: 	lappend outputHeader "${config(prefix)}int ${outCVarName}_hash;"
37d00c3cfb 2019-11-04  556: 
37d00c3cfb 2019-11-04  557: 	for {set hash 0} {$hash < $config(hashTableSize)} {incr hash} {
37d00c3cfb 2019-11-04  558: 		if {[info exists indexesAtHash($hash)]} {
37d00c3cfb 2019-11-04  559: 			set indexes $indexesAtHash($hash)
37d00c3cfb 2019-11-04  560: 		} else {
37d00c3cfb 2019-11-04  561: 			set indexes [list]
37d00c3cfb 2019-11-04  562: 		}
37d00c3cfb 2019-11-04  563: 
3c8c52a9f8 2019-11-04  564: 		if {[llength $indexes] != $maxIndexes} {
3c8c52a9f8 2019-11-04  565: 			lappend indexes $invalidValue
3c8c52a9f8 2019-11-04  566: 		}
37d00c3cfb 2019-11-04  567: 		lappend outputHeader "${config(prefix)}static const long ${outCVarName}_hashTable_${hash}\[\] = \{"
37d00c3cfb 2019-11-04  568: 		lappend outputHeader "${config(prefix)}\t[join $indexes {, }]"
37d00c3cfb 2019-11-04  569: 		lappend outputHeader "${config(prefix)}\};"
37d00c3cfb 2019-11-04  570: 	}
37d00c3cfb 2019-11-04  571: 
37d00c3cfb 2019-11-04  572: 	lappend outputHeader "${config(prefix)}static const long * const ${outCVarName}_hashTable\[${config(hashTableSize)}\] = \{"
37d00c3cfb 2019-11-04  573: 
37d00c3cfb 2019-11-04  574: 	for {set hash 0} {$hash < $config(hashTableSize)} {incr hash} {
37d00c3cfb 2019-11-04  575: 		lappend outputHeader "${config(prefix)}\t${outCVarName}_hashTable_${hash},"
37d00c3cfb 2019-11-04  576: 	}
37d00c3cfb 2019-11-04  577: 
37d00c3cfb 2019-11-04  578: 	lappend outputHeader "${config(prefix)}\};"
37d00c3cfb 2019-11-04  579: 	lappend outputBody "${config(prefix)}${outCVarName}_hash = Tcl_ZlibAdler32(0, (unsigned char *) ${cVarName}, ${cVarLength}) % ${config(hashTableSize)};"
3c8c52a9f8 2019-11-04  580: 	lappend outputBody "${config(prefix)}for (${outCVarName}_idx = 0; ${outCVarName}_idx < ${maxIndexes}; ${outCVarName}_idx++) \{"
37d00c3cfb 2019-11-04  581: 	lappend outputBody "${config(prefix)}\t${outCVarName} = ${outCVarName}_hashTable\[${outCVarName}_hash\]\[${outCVarName}_idx\];"
37d00c3cfb 2019-11-04  582: 	lappend outputBody "${config(prefix)}\tif (${outCVarName} == $invalidValue) \{"
37d00c3cfb 2019-11-04  583: 	lappend outputBody "${config(prefix)}\t\tbreak;"
37d00c3cfb 2019-11-04  584: 	lappend outputBody "${config(prefix)}\t\}"
37d00c3cfb 2019-11-04  585: 	lappend outputBody ""
37d00c3cfb 2019-11-04  586: 	lappend outputBody "${config(prefix)}\tif (${config(validate)}) \{"
37d00c3cfb 2019-11-04  587: 	lappend outputBody "${config(prefix)}\t\t${config(onValidated)}"
37d00c3cfb 2019-11-04  588: 	lappend outputBody "${config(prefix)}\t\}"
37d00c3cfb 2019-11-04  589: 	lappend outputBody "${config(prefix)}\}"
37d00c3cfb 2019-11-04  590: 
37d00c3cfb 2019-11-04  591: 	return [dict create header [join $outputHeader "\n"] body [join $outputBody "\n"]]
d36db7c01b 2019-09-20  592: }
47dcf5fc27 2019-05-01  593: 
47dcf5fc27 2019-05-01  594: package provide xvfs 1