Check-in [0bdbe4333e]
Overview
Comment:Add support for writing output to a file rather than stdout
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 0bdbe4333e0a43d2fd05e62f392a659cc6c4d54709fdeaf692a949e2ac02c055
User & Date: rkeene on 2019-09-20 15:00:04
Other Links: manifest | tags
Context
2019-09-20
15:02
Fix help check-in: 702c74c153 user: rkeene tags: trunk
15:00
Add support for writing output to a file rather than stdout check-in: 0bdbe4333e user: rkeene tags: trunk
14:53
Updated minirivet to support outputting to a variable or a different channel check-in: b07616bee9 user: rkeene tags: trunk
Changes

Modified lib/xvfs/xvfs.tcl from [d0a77c36f9] to [3bcab2cb44].

1
2
3
4
5




6
7
8
9

10
11

12
13

14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12

13
14

15
16

17
18
19
20
21
22
23
24





+
+
+
+



-
+

-
+

-
+







#! /usr/bin/env tclsh

namespace eval ::xvfs {}

# Functions
proc ::xvfs::_emitLine {line} {
	::minirivet::_emitOutput "${line}\n"
}

proc ::xvfs::printHelp {channel {errors ""}} {
	if {[llength $errors] != 0} {
		foreach error $errors {
			puts $channel "error: $error"
			::xvfs::_emitLine $channel "error: $error"
		}
		puts $channel ""
		::xvfs::_emitLine $channel ""
	}
	puts $channel "Usage: dir2c \[--help\] --directory <rootDirectory> --name <fsName>"
	::xvfs::_emitLine $channel "Usage: dir2c \[--help\] --directory <rootDirectory> --name <fsName>"
	flush $channel
}

proc ::xvfs::sanitizeCString {string} {
	set output [join [lmap char [split $string ""] {
		if {![regexp {[A-Za-z0-9./-]} $char]} {
			binary scan $char H* char
109
110
111
112
113
114
115
116
117
118
119




120
121
122

123
124
125

126
127
128

129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

163
164
165
166
167
168
169
113
114
115
116
117
118
119




120
121
122
123
124
125

126
127
128

129
130
131

132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

166
167
168
169
170
171
172
173







-
-
-
-
+
+
+
+


-
+


-
+


-
+















-
+

















-
+







			}
		}
		default {
			return -code error "Unable to process $inputFile, unknown type: $fileInfo(type)"
		}
	}

	puts "\t\{"
	puts "\t\t.name = \"[sanitizeCString $outputFile]\","
	puts "\t\t.type = $type,"
	puts "\t\t.size = $size,"
	::xvfs::_emitLine "\t\{"
	::xvfs::_emitLine "\t\t.name = \"[sanitizeCString $outputFile]\","
	::xvfs::_emitLine "\t\t.type = $type,"
	::xvfs::_emitLine "\t\t.size = $size,"
	switch -exact -- $fileInfo(type) {
		"file" {
			puts "\t\t.data.fileContents = (const unsigned char *) $data"
			::xvfs::_emitLine "\t\t.data.fileContents = (const unsigned char *) $data"
		}
		"directory" {
			puts "\t\t.data.dirChildren  = $children"
			::xvfs::_emitLine "\t\t.data.dirChildren  = $children"
		}
	}
	puts "\t\},"
	::xvfs::_emitLine "\t\},"
}

proc ::xvfs::processDirectory {fsName directory {subDirectory ""}} {
	set subDirectories [list]
	set outputFiles [list]
	set workingDirectory [file join $directory $subDirectory]
	set outputDirectory $subDirectory

	if {$subDirectory eq ""} {
		set isTopLevel true
	} else {
		set isTopLevel false
	}

	if {$isTopLevel} {
		puts "static const struct xvfs_file_data xvfs_${fsName}_data\[\] = \{"
		::xvfs::_emitLine "static const struct xvfs_file_data xvfs_${fsName}_data\[\] = \{"
	}

	# XXX:TODO: Include hidden files ?
	set children [list]
	foreach file [glob -nocomplain -tails -directory $workingDirectory *] {
		if {$file in {. ..}} {
			continue
		}

		set inputFile [file join $workingDirectory $file]
		set outputFile [file join $outputDirectory [encoding convertto utf-8 $file]]

		unset -nocomplain fileInfo
		catch {
			file lstat $inputFile fileInfo
		}
		if {![info exists fileInfo]} {
			puts stderr "warning: Unable to access $inputFile, skipping"
			::xvfs::_emitLine stderr "warning: Unable to access $inputFile, skipping"
		}
		
		lappend children [file tail $file]

		if {$fileInfo(type) eq "directory"} {
			lappend subDirectories $outputFile
			continue
183
184
185
186
187
188
189
190

191
192
193
194
195
196
197
187
188
189
190
191
192
193

194
195
196
197
198
199
200
201







-
+







	file stat $inputFile fileInfo
	set fileInfo(children) $children

	processFile $fsName $inputFile $outputFile [array get fileInfo]
	lappend outputFiles $outputFile

	if {$isTopLevel} {
		puts "\};"
		::xvfs::_emitLine "\};"
	}

	return $outputFiles
}

proc ::xvfs::main {argv} {
	# Main entry point
208
209
210
211
212
213
214



215
216
217
218
219
220
221
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228







+
+
+







			}
			"--directory" {
				set rootDirectory $val
			}
			"--name" {
				set fsName $val
			}
			"--output" {
				# Ignored, handled as part of some other process
			}
			default {
				printHelp stderr [list "Invalid option: $arg $val"]
				exit 1
			}
		}
	}

Modified xvfs-create from [a04cb49093] to [36eddd8d96].

8
9
10
11
12
13
14








15
16
17
18
19
20
21
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29







+
+
+
+
+
+
+
+








package require minirivet

set mode "run"
if {[lindex $argv 0] == "--dump-tcl"} {
	set mode "dump-tcl"
}

foreach {arg val} $argv {
	switch -exact -- $arg {
		"--output" {
			set outputFile $val
		}
	}
}

proc remove_debug {input} {
	set output [list]

	set lastLine -
	foreach line [split $input "\n"] {
		if {[string match -nocase "*XVFS_DEBUG*" $line]} {
31
32
33
34
35
36
37





38




39
40
41
42
43
44
45
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62







+
+
+
+
+

+
+
+
+







	}

	return [join $output "\n"]
}

switch -- $mode {
	"run" {
		if {[info exists outputFile]} {
			set fd [open $outputFile w]
			::minirivet::setOutputChannel $fd
		}

		::minirivet::parse $template

		if {[info exists fd]} {
			close $fd
		}
	}
	"dump-tcl" {
		set xvfs_tcl [file join $sourceDirectory lib xvfs xvfs.tcl]
		set xvfs_core_h [file join $sourceDirectory xvfs-core.h]
		set xvfs_core_c [file join $sourceDirectory xvfs-core.c]

		set cleanup {
53
54
55
56
57
58
59



60
61
62
63
64
65
66
67
68
69
70
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90







+
+
+












		if {[lsearch -exact $argv "--remove-debug"] != -1} {
			set core_header_data [remove_debug $core_header_data]
		}

		puts "#! /usr/bin/env tclsh"
		puts ""
		puts [list namespace eval ::minirivet {}]
		puts [list set ::minirivet::_outputChannel stdout]
		puts [list proc ::minirivet::_emitOutput [info args ::minirivet::_emitOutput] [info body ::minirivet::_emitOutput]]
		puts [read [open $xvfs_tcl]]
		puts ""
		puts [list puts -nonewline $core_header_data]
		puts ""
		puts [string map $cleanup [::minirivet::parseStringToCode [read [open $template]]]]
	}
	default {
		puts stderr "error: Invalid mode: $mode"
		exit 1
	}
}