47dcf5fc27 2019-05-01 rkeene: #! /usr/bin/env tclsh
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: namespace eval ::xvfs {}
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: # Functions
47dcf5fc27 2019-05-01 rkeene: proc ::xvfs::printHelp {channel {errors ""}} {
47dcf5fc27 2019-05-01 rkeene: if {[llength $errors] != 0} {
47dcf5fc27 2019-05-01 rkeene: foreach error $errors {
47dcf5fc27 2019-05-01 rkeene: puts $channel "error: $error"
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene: puts $channel ""
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene: puts $channel "Usage: dir2c \[--help\] --directory <rootDirectory> --name <fsName>"
47dcf5fc27 2019-05-01 rkeene: flush $channel
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: proc ::xvfs::sanitizeCString {string} {
47dcf5fc27 2019-05-01 rkeene: set output [join [lmap char [split $string ""] {
47dcf5fc27 2019-05-01 rkeene: if {![regexp {[A-Za-z0-9./-]} $char]} {
47dcf5fc27 2019-05-01 rkeene: binary scan $char H* char
47dcf5fc27 2019-05-01 rkeene: set char "\\[format %03o 0x$char]"
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: set char
47dcf5fc27 2019-05-01 rkeene: }] ""]
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: return $output
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
32b55a907b 2019-05-02 rkeene: proc ::xvfs::sanitizeCStringList {list {prefix ""} {width 80}} {
32b55a907b 2019-05-02 rkeene: set lines [list]
32b55a907b 2019-05-02 rkeene: set row [list]
32b55a907b 2019-05-02 rkeene: foreach item $list {
32b55a907b 2019-05-02 rkeene: lappend row "\"[sanitizeCString $item]\""
32b55a907b 2019-05-02 rkeene:
32b55a907b 2019-05-02 rkeene: set rowString [join $row {, }]
32b55a907b 2019-05-02 rkeene: set rowString "${prefix}${rowString}"
32b55a907b 2019-05-02 rkeene: if {[string length $rowString] > $width} {
32b55a907b 2019-05-02 rkeene: set row [list]
32b55a907b 2019-05-02 rkeene: lappend lines $rowString
32b55a907b 2019-05-02 rkeene: unset rowString
32b55a907b 2019-05-02 rkeene: }
32b55a907b 2019-05-02 rkeene: }
32b55a907b 2019-05-02 rkeene: if {[info exists rowString]} {
32b55a907b 2019-05-02 rkeene: lappend lines $rowString
32b55a907b 2019-05-02 rkeene: }
32b55a907b 2019-05-02 rkeene:
32b55a907b 2019-05-02 rkeene: return [join $lines "\n"]
32b55a907b 2019-05-02 rkeene: }
32b55a907b 2019-05-02 rkeene:
47dcf5fc27 2019-05-01 rkeene: proc ::xvfs::binaryToCHex {binary {prefix ""} {width 10}} {
47dcf5fc27 2019-05-01 rkeene: binary scan $binary H* binary
47dcf5fc27 2019-05-01 rkeene: set output [list]
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: set width [expr {$width * 2}]
47dcf5fc27 2019-05-01 rkeene: set stopAt [expr {$width - 1}]
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: while {$binary ne ""} {
47dcf5fc27 2019-05-01 rkeene: set row [string range $binary 0 $stopAt]
47dcf5fc27 2019-05-01 rkeene: set binary [string range $binary $width end]
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: set rowOutput [list]
47dcf5fc27 2019-05-01 rkeene: while {$row ne ""} {
47dcf5fc27 2019-05-01 rkeene: set value [string range $row 0 1]
47dcf5fc27 2019-05-01 rkeene: set row [string range $row 2 end]
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: lappend rowOutput "\\x$value"
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene: set rowOutput [join $rowOutput {}]
47dcf5fc27 2019-05-01 rkeene: set rowOutput "${prefix}\"${rowOutput}\""
47dcf5fc27 2019-05-01 rkeene: lappend output $rowOutput
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: if {[llength $output] == 0} {
47dcf5fc27 2019-05-01 rkeene: return "${prefix}\"\""
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: set output [join $output "\n"]
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: proc ::xvfs::processFile {fsName inputFile outputFile fileInfoDict} {
47dcf5fc27 2019-05-01 rkeene: array set fileInfo $fileInfoDict
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: switch -exact -- $fileInfo(type) {
47dcf5fc27 2019-05-01 rkeene: "file" {
47dcf5fc27 2019-05-01 rkeene: set type "XVFS_FILE_TYPE_REG"
47dcf5fc27 2019-05-01 rkeene: set fd [open $inputFile]
47dcf5fc27 2019-05-01 rkeene: fconfigure $fd -encoding binary -translation binary -blocking true
47dcf5fc27 2019-05-01 rkeene: set data [read $fd]
47dcf5fc27 2019-05-01 rkeene: set size [string length $data]
47dcf5fc27 2019-05-01 rkeene: set data [string trimleft [binaryToCHex $data "\t\t\t"]]
47dcf5fc27 2019-05-01 rkeene: close $fd
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene: "directory" {
47dcf5fc27 2019-05-01 rkeene: set type "XVFS_FILE_TYPE_DIR"
32b55a907b 2019-05-02 rkeene: set children $fileInfo(children)
32b55a907b 2019-05-02 rkeene: set size [llength $children]
32b55a907b 2019-05-02 rkeene:
32b55a907b 2019-05-02 rkeene: if {$size == 0} {
32b55a907b 2019-05-02 rkeene: set children "NULL"
32b55a907b 2019-05-02 rkeene: } else {
32b55a907b 2019-05-02 rkeene: set children [string trimleft [sanitizeCStringList $children "\t\t\t"]]
32b55a907b 2019-05-02 rkeene: # This initializes it using a C99 compound literal, C99 is required
32b55a907b 2019-05-02 rkeene: set children "(const char *\[\]) \{$children\}"
32b55a907b 2019-05-02 rkeene: }
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene: default {
47dcf5fc27 2019-05-01 rkeene: return -code error "Unable to process $inputFile, unknown type: $fileInfo(type)"
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: puts "\t\{"
47dcf5fc27 2019-05-01 rkeene: puts "\t\t.name = \"[sanitizeCString $outputFile]\","
47dcf5fc27 2019-05-01 rkeene: puts "\t\t.type = $type,"
47dcf5fc27 2019-05-01 rkeene: puts "\t\t.size = $size,"
32b55a907b 2019-05-02 rkeene: switch -exact -- $fileInfo(type) {
32b55a907b 2019-05-02 rkeene: "file" {
32b55a907b 2019-05-02 rkeene: puts "\t\t.data.fileContents = (const unsigned char *) $data"
32b55a907b 2019-05-02 rkeene: }
32b55a907b 2019-05-02 rkeene: "directory" {
32b55a907b 2019-05-02 rkeene: puts "\t\t.data.dirChildren = $children"
32b55a907b 2019-05-02 rkeene: }
32b55a907b 2019-05-02 rkeene: }
47dcf5fc27 2019-05-01 rkeene: puts "\t\},"
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: proc ::xvfs::processDirectory {fsName directory {subDirectory ""}} {
47dcf5fc27 2019-05-01 rkeene: set subDirectories [list]
47dcf5fc27 2019-05-01 rkeene: set outputFiles [list]
47dcf5fc27 2019-05-01 rkeene: set workingDirectory [file join $directory $subDirectory]
47dcf5fc27 2019-05-01 rkeene: set outputDirectory $subDirectory
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: if {$subDirectory eq ""} {
47dcf5fc27 2019-05-01 rkeene: set isTopLevel true
47dcf5fc27 2019-05-01 rkeene: } else {
47dcf5fc27 2019-05-01 rkeene: set isTopLevel false
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: if {$isTopLevel} {
47dcf5fc27 2019-05-01 rkeene: puts "static struct xvfs_file_data xvfs_${fsName}_data\[\] = \{"
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: # XXX:TODO: Include hidden files ?
32b55a907b 2019-05-02 rkeene: set children [list]
47dcf5fc27 2019-05-01 rkeene: foreach file [glob -nocomplain -tails -directory $workingDirectory *] {
47dcf5fc27 2019-05-01 rkeene: if {$file in {. ..}} {
47dcf5fc27 2019-05-01 rkeene: continue
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: set inputFile [file join $workingDirectory $file]
47dcf5fc27 2019-05-01 rkeene: set outputFile [file join $outputDirectory $file]
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: unset -nocomplain fileInfo
47dcf5fc27 2019-05-01 rkeene: catch {
47dcf5fc27 2019-05-01 rkeene: file lstat $inputFile fileInfo
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene: if {![info exists fileInfo]} {
47dcf5fc27 2019-05-01 rkeene: puts stderr "warning: Unable to access $inputFile, skipping"
47dcf5fc27 2019-05-01 rkeene: }
32b55a907b 2019-05-02 rkeene:
32b55a907b 2019-05-02 rkeene: lappend children [file tail $file]
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: if {$fileInfo(type) eq "directory"} {
47dcf5fc27 2019-05-01 rkeene: lappend subDirectories $outputFile
32b55a907b 2019-05-02 rkeene: continue
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: processFile $fsName $inputFile $outputFile [array get fileInfo]
47dcf5fc27 2019-05-01 rkeene: lappend outputFiles $outputFile
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: foreach subDirectory $subDirectories {
47dcf5fc27 2019-05-01 rkeene: lappend outputFiles {*}[processDirectory $fsName $directory $subDirectory]
47dcf5fc27 2019-05-01 rkeene: }
32b55a907b 2019-05-02 rkeene:
32b55a907b 2019-05-02 rkeene: set inputFile $directory
32b55a907b 2019-05-02 rkeene: set outputFile $outputDirectory
32b55a907b 2019-05-02 rkeene: unset -nocomplain fileInfo
32b55a907b 2019-05-02 rkeene: file stat $inputFile fileInfo
32b55a907b 2019-05-02 rkeene: set fileInfo(children) $children
32b55a907b 2019-05-02 rkeene:
32b55a907b 2019-05-02 rkeene: processFile $fsName $inputFile $outputFile [array get fileInfo]
32b55a907b 2019-05-02 rkeene: lappend outputFiles $outputFile
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: if {$isTopLevel} {
47dcf5fc27 2019-05-01 rkeene: puts "\};"
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: return $outputFiles
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: proc ::xvfs::main {argv} {
47dcf5fc27 2019-05-01 rkeene: # Main entry point
47dcf5fc27 2019-05-01 rkeene: ## 1. Parse arguments
47dcf5fc27 2019-05-01 rkeene: if {[llength $argv] % 2 != 0} {
47dcf5fc27 2019-05-01 rkeene: lappend argv ""
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: foreach {arg val} $argv {
47dcf5fc27 2019-05-01 rkeene: switch -exact -- $arg {
47dcf5fc27 2019-05-01 rkeene: "--help" {
47dcf5fc27 2019-05-01 rkeene: printHelp stdout
47dcf5fc27 2019-05-01 rkeene: exit 0
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene: "--directory" {
47dcf5fc27 2019-05-01 rkeene: set rootDirectory $val
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene: "--name" {
47dcf5fc27 2019-05-01 rkeene: set fsName $val
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene: default {
47dcf5fc27 2019-05-01 rkeene: printHelp stderr [list "Invalid option: $arg $val"]
47dcf5fc27 2019-05-01 rkeene: exit 1
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: ## 2. Validate arguments
47dcf5fc27 2019-05-01 rkeene: set errors [list]
47dcf5fc27 2019-05-01 rkeene: if {![info exists rootDirectory]} {
47dcf5fc27 2019-05-01 rkeene: lappend errors "--directory must be specified"
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene: if {![info exists fsName]} {
47dcf5fc27 2019-05-01 rkeene: lappend errors "--name must be specified"
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: if {[llength $errors] != 0} {
47dcf5fc27 2019-05-01 rkeene: printHelp stderr $errors
47dcf5fc27 2019-05-01 rkeene: exit 1
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: ## 3. Start processing directory and producing initial output
32b55a907b 2019-05-02 rkeene: set ::xvfs::outputFiles [processDirectory $fsName $rootDirectory]
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: set ::xvfs::fsName $fsName
47dcf5fc27 2019-05-01 rkeene: set ::xvfs::rootDirectory $rootDirectory
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: package provide xvfs 1