Lines of
lib/xvfs/xvfs.tcl
from check-in d8e00cd4a3
that are changed by the sequence of edits moving toward
check-in e592c85e70:
1: #! /usr/bin/env tclsh
2:
3: namespace eval ::xvfs {}
4: namespace eval ::xvfs::callback {}
5:
6: set ::xvfs::_xvfsDir [file dirname [info script]]
7:
8: # Functions
9: proc ::xvfs::_emitLine {line} {
10: if {[info command ::minirivet::_emitOutput] ne ""} {
11: ::minirivet::_emitOutput "${line}\n"
12: } else {
13: puts $line
14: }
15: }
16:
17: proc ::xvfs::printHelp {channel {errors ""}} {
18: if {[llength $errors] != 0} {
19: foreach error $errors {
20: puts $channel "error: $error"
21: }
22: puts $channel ""
23: }
24: puts $channel "Usage: dir2c \[--help\] \[--output <filename>\] --directory <rootDirectory> --name <fsName>"
25: flush $channel
26: }
27:
28: proc ::xvfs::sanitizeCString {string} {
29: set output [join [lmap char [split $string ""] {
30: if {![regexp {[A-Za-z0-9./-]} $char]} {
31: binary scan $char H* char
32: set char "\\[format %03o 0x$char]"
33: }
34:
35: set char
36: }] ""]
37:
38: return $output
39: }
40:
41: proc ::xvfs::sanitizeCStringList {list {prefix ""} {width 80}} {
42: set lines [list]
43: set row [list]
44: foreach item $list {
45: lappend row "\"[sanitizeCString $item]\""
46:
47: set rowString [join $row {, }]
48: set rowString "${prefix}${rowString}"
49: if {[string length $rowString] > $width} {
50: set row [list]
51: lappend lines $rowString
52: unset rowString
53: }
54: }
55: if {[info exists rowString]} {
56: lappend lines $rowString
57: }
58:
59: return [join $lines "\n"]
60: }
61:
62: proc ::xvfs::binaryToCHex {binary {prefix ""} {width 10}} {
63: set binary [binary encode hex $binary]
64: set output [list]
65:
66: set width [expr {$width * 2}]
67: set stopAt [expr {$width - 1}]
68:
69: set offset 0
70: while 1 {
71: set row [string range $binary $offset [expr {$offset + $stopAt}]]
72: if {[string length $row] == 0} {
73: break
74: }
75: incr offset [string length $row]
76:
77: set rowOutput [list]
78: while {$row ne ""} {
79: set value [string range $row 0 1]
80: set row [string range $row 2 end]
81:
82: lappend rowOutput "\\x$value"
83: }
84: set rowOutput [join $rowOutput {}]
85: set rowOutput "${prefix}\"${rowOutput}\""
86: lappend output $rowOutput
87: }
88:
89: if {[llength $output] == 0} {
90: return "${prefix}\"\""
91: }
92:
93: set output [join $output "\n"]
94: }
95:
96: proc ::xvfs::processFile {fsName inputFile outputFile fileInfoDict} {
97: array set fileInfo $fileInfoDict
98:
99: switch -exact -- $fileInfo(type) {
100: "file" {
101: set type "XVFS_FILE_TYPE_REG"
102: if {[info exists fileInfo(fileContents)]} {
103: set data $fileInfo(fileContents)
104: } else {
105: set fd [open $inputFile]
106: fconfigure $fd -encoding binary -translation binary -blocking true
107: set data [read $fd]
108: close $fd
109: }
110: set size [string length $data]
111: set data [string trimleft [binaryToCHex $data "\t\t\t"]]
112: }
113: "directory" {
114: set type "XVFS_FILE_TYPE_DIR"
115: set children $fileInfo(children)
116: set size [llength $children]
117:
118: if {$size == 0} {
119: set children "NULL"
120: } else {
121: set children [string trimleft [sanitizeCStringList $children "\t\t\t"]]
122: # This initializes it using a C99 compound literal, C99 is required
123: set children "(const char *\[\]) \{$children\}"
124: }
125: }
126: default {
127: return -code error "Unable to process $inputFile, unknown type: $fileInfo(type)"
128: }
129: }
130:
131: ::xvfs::_emitLine "\t\{"
132: ::xvfs::_emitLine "\t\t.name = \"[sanitizeCString $outputFile]\","
133: ::xvfs::_emitLine "\t\t.type = $type,"
134: ::xvfs::_emitLine "\t\t.size = $size,"
135: switch -exact -- $fileInfo(type) {
136: "file" {
137: ::xvfs::_emitLine "\t\t.data.fileContents = (const unsigned char *) $data"
138: }
139: "directory" {
140: ::xvfs::_emitLine "\t\t.data.dirChildren = $children"
141: }
142: }
143: ::xvfs::_emitLine "\t\},"
144: }
145:
146: proc ::xvfs::processDirectory {fsName directory {subDirectory ""}} {
147: set subDirectories [list]
148: set outputFiles [list]
149: set workingDirectory [file join $directory $subDirectory]
150: set outputDirectory $subDirectory
151:
152: if {$subDirectory eq ""} {
153: set isTopLevel true
154: } else {
155: set isTopLevel false
156: }
157:
158: if {$isTopLevel} {
159: ::xvfs::_emitLine "static const struct xvfs_file_data xvfs_${fsName}_data\[\] = \{"
160: }
161:
162: # XXX:TODO: Include hidden files ?
163: set children [list]
164: foreach file [glob -nocomplain -tails -directory $workingDirectory *] {
165: if {$file in {. ..}} {
166: continue
167: }
168:
169: set inputFile [file join $workingDirectory $file]
170: set outputFile [file join $outputDirectory [encoding convertto utf-8 $file]]
171:
172: if {[info command ::xvfs::callback::setOutputFileName] ne ""} {
173: set outputFile [::xvfs::callback::setOutputFileName $file $workingDirectory $inputFile $outputDirectory $outputFile]
d8e00cd4a3 2019-09-20 174: if {$outputFile eq ""} {
175: continue
176: }
177: }
178:
179: unset -nocomplain fileInfo
180: catch {
181: file lstat $inputFile fileInfo
182: }
183: if {![info exists fileInfo]} {
184: puts stderr "warning: Unable to access $inputFile, skipping"
185: }
186:
187: lappend children [file tail $file]
188:
189: if {$fileInfo(type) eq "directory"} {
190: lappend subDirectories $outputFile
191: continue
192: }
193:
194: processFile $fsName $inputFile $outputFile [array get fileInfo]
195: lappend outputFiles $outputFile
196: }
197:
198: foreach subDirectory $subDirectories {
199: lappend outputFiles {*}[processDirectory $fsName $directory $subDirectory]
200: }
201:
202: set inputFile $directory
203: set outputFile $outputDirectory
d8e00cd4a3 2019-09-20 204: unset -nocomplain fileInfo
d8e00cd4a3 2019-09-20 205: file stat $inputFile fileInfo
d8e00cd4a3 2019-09-20 206: set fileInfo(children) $children
d8e00cd4a3 2019-09-20 207:
d8e00cd4a3 2019-09-20 208: processFile $fsName $inputFile $outputFile [array get fileInfo]
d8e00cd4a3 2019-09-20 209: lappend outputFiles $outputFile
210:
211: if {$isTopLevel} {
212: if {[info command ::xvfs::callback::addOutputFiles] ne ""} {
d8e00cd4a3 2019-09-20 213: lappend outputFiles {*}[::xvfs::callback::addOutputFiles]
214: }
215:
216: ::xvfs::_emitLine "\};"
217: }
218:
219: return $outputFiles
220: }
221:
222: proc ::xvfs::main {argv} {
223: # Main entry point
224: ## 1. Parse arguments
225: if {[llength $argv] % 2 != 0} {
226: lappend argv ""
227: }
228:
229: foreach {arg val} $argv {
230: switch -exact -- $arg {
231: "--help" {
232: printHelp stdout
233: exit 0
234: }
235: "--directory" {
236: set rootDirectory $val
237: }
238: "--name" {
239: set fsName $val
240: }
241: "--output" - "--header" {
242: # Ignored, handled as part of some other process
243: }
244: default {
245: printHelp stderr [list "Invalid option: $arg $val"]
246: exit 1
247: }
248: }
249: }
250:
251: ## 2. Validate arguments
252: set errors [list]
253: if {![info exists rootDirectory]} {
254: lappend errors "--directory must be specified"
255: }
256: if {![info exists fsName]} {
257: lappend errors "--name must be specified"
258: }
259:
260: if {[llength $errors] != 0} {
261: printHelp stderr $errors
262: exit 1
263: }
264:
265: ## 3. Start processing directory and producing initial output
266: set ::xvfs::outputFiles [processDirectory $fsName $rootDirectory]
267:
268: set ::xvfs::fsName $fsName
269: set ::xvfs::rootDirectory $rootDirectory
270: }
271:
d8e00cd4a3 2019-09-20 272: proc ::xvfs::run {argv} {
273: uplevel #0 { package require minirivet }
274:
d8e00cd4a3 2019-09-20 275: set ::xvfs::argv $argv
276: ::minirivet::parse [file join $::xvfs::_xvfsDir xvfs.c.rvt]
277: }
278:
279: proc ::xvfs::setOutputChannel {channel} {
280: uplevel #0 { package require minirivet }
281: tailcall ::minirivet::setOutputChannel $channel
282: }
283:
284: proc ::xvfs::setOutputVariable {variable} {
285: uplevel #0 { package require minirivet }
286: tailcall ::minirivet::setOutputVariable $variable
287: }
288:
289: proc ::xvfs::staticIncludeHeaderData {headerData} {
290: set ::xvfs::xvfsCoreH $headerData
291: }
292:
293: proc ::xvfs::staticIncludeHeader {pathToHeaderFile} {
294: set fd [open $pathToHeaderFile]
295: ::xvfs::staticIncludeHeaderData [read $fd]
296: close $fd
297: }
298:
299: package provide xvfs 1