Lines of
lib/xvfs/xvfs.tcl
from check-in e592c85e70
that are changed by the sequence of edits moving toward
check-in 30c469fcf7:
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]
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: }
e592c85e70 2019-09-20 186:
e592c85e70 2019-09-20 187: lappend children [file tail $file]
188:
189: if {$fileInfo(type) eq "directory"} {
e592c85e70 2019-09-20 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
204: if {[info command ::xvfs::callback::setOutputFileName] ne ""} {
205: set outputFile [::xvfs::callback::setOutputFileName $directory $directory $inputFile $outputDirectory $outputFile]
206: }
207:
208: if {$outputFile ne "/"} {
209: unset -nocomplain fileInfo
210: file stat $inputFile fileInfo
211: set fileInfo(children) $children
212:
213: processFile $fsName $inputFile $outputFile [array get fileInfo]
214: lappend outputFiles $outputFile
215: }
216:
217: if {$isTopLevel} {
218: if {[info command ::xvfs::callback::addOutputFiles] ne ""} {
219: lappend outputFiles {*}[::xvfs::callback::addOutputFiles $fsName]
220: }
221:
222: ::xvfs::_emitLine "\};"
223: }
224:
225: return $outputFiles
226: }
227:
228: proc ::xvfs::main {argv} {
229: # Main entry point
230: ## 1. Parse arguments
231: if {[llength $argv] % 2 != 0} {
232: lappend argv ""
233: }
234:
235: foreach {arg val} $argv {
236: switch -exact -- $arg {
237: "--help" {
238: printHelp stdout
239: exit 0
240: }
241: "--directory" {
242: set rootDirectory $val
243: }
244: "--name" {
245: set fsName $val
246: }
247: "--output" - "--header" {
248: # Ignored, handled as part of some other process
249: }
250: default {
251: printHelp stderr [list "Invalid option: $arg $val"]
252: exit 1
253: }
254: }
255: }
256:
257: ## 2. Validate arguments
258: set errors [list]
259: if {![info exists rootDirectory]} {
260: lappend errors "--directory must be specified"
261: }
262: if {![info exists fsName]} {
263: lappend errors "--name must be specified"
264: }
265:
266: if {[llength $errors] != 0} {
267: printHelp stderr $errors
268: exit 1
269: }
270:
271: ## 3. Start processing directory and producing initial output
272: set ::xvfs::outputFiles [processDirectory $fsName $rootDirectory]
273:
274: set ::xvfs::fsName $fsName
275: set ::xvfs::rootDirectory $rootDirectory
276: }
277:
278: proc ::xvfs::run {args} {
279: uplevel #0 { package require minirivet }
280:
281: set ::xvfs::argv $args
282: ::minirivet::parse [file join $::xvfs::_xvfsDir xvfs.c.rvt]
283: }
284:
285: proc ::xvfs::setOutputChannel {channel} {
286: uplevel #0 { package require minirivet }
287: tailcall ::minirivet::setOutputChannel $channel
288: }
289:
290: proc ::xvfs::setOutputVariable {variable} {
291: uplevel #0 { package require minirivet }
292: tailcall ::minirivet::setOutputVariable $variable
293: }
294:
295: proc ::xvfs::staticIncludeHeaderData {headerData} {
296: set ::xvfs::xvfsCoreH $headerData
297: }
298:
299: proc ::xvfs::staticIncludeHeader {pathToHeaderFile} {
300: set fd [open $pathToHeaderFile]
301: ::xvfs::staticIncludeHeaderData [read $fd]
302: close $fd
303: }
304:
305: package provide xvfs 1