Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
.fingerprint = 0x9947018c924eecb2,
.dependencies = .{
.zfat = .{
.url = "https://github.com/ZigEmbeddedGroup/zfat/archive/3ce06d43a4e04d387034dcae2f486b050701f321.tar.gz",
.hash = "zfat-0.0.0-AAAAAMYlcABdh06Mn9CNk8Ccy_3bBFgJr8wo4jKza1q-",
.url = "https://github.com/CascadeOS/zfat/archive/refs/heads/0.15.zip",
.hash = "zfat-0.16.0-SNNK9fKtTgASssfmCblZwRMLU4pndVtwxTNhYCegBOyA",
},
.args = .{
.url = "git+https://github.com/ikskuh/zig-args.git#9425b94c103a031777fdd272c555ce93a7dea581",
.hash = "args-0.0.0-CiLiqv_NAAC97fGpk9hS2K681jkiqPsWP6w3ucb_ctGH",
.url = "git+https://github.com/ikskuh/zig-args.git#8ae26b44a884ff20dca98ee84c098e8f8e94902f",
.hash = "args-0.0.0-CiLiqojRAACGzDRO7A9dw7kWSchNk29caJZkXuMCb0Cn",
},
},
},
.paths = .{
"build.zig",
"build.zig.zon",
Expand Down
49 changes: 21 additions & 28 deletions src/BuildInterface.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub fn createDisk(dimmer: Interface, size: u64, content: Content) std.Build.Lazy

const compile_script = b.addRunArtifact(dimmer.dimmer_exe);

compile_script.setCwd(script_file.dirname());

_ = compile_script.addPrefixedDepFileOutputArg("--deps-file=", "image.d");

compile_script.addArg(b.fmt("--size={d}", .{size}));
Expand Down Expand Up @@ -63,13 +65,13 @@ pub fn createDisk(dimmer: Interface, size: u64, content: Content) std.Build.Lazy
}

fn renderContent(wfs: *std.Build.Step.WriteFile, allocator: std.mem.Allocator, content: Content) struct { []const u8, ContentWriter.VariableMap } {
var code: std.ArrayList(u8) = .init(allocator);
var code = std.Io.Writer.Allocating.init(allocator);
defer code.deinit();

var variables: ContentWriter.VariableMap = .init(allocator);

var cw: ContentWriter = .{
.code = code.writer(),
.code = &code.writer,
.wfs = wfs,
.vars = &variables,
};
Expand Down Expand Up @@ -99,7 +101,7 @@ const ContentWriter = struct {
pub const VariableMap = std.StringArrayHashMap(struct { std.Build.LazyPath, ContentWriter.UsageHint });

wfs: *std.Build.Step.WriteFile,
code: std.ArrayList(u8).Writer,
code: *std.Io.Writer,
vars: *VariableMap,

fn render(cw: ContentWriter, content: Content) !void {
Expand All @@ -117,7 +119,7 @@ const ContentWriter = struct {
},

.paste_file => |data| {
try cw.code.print("paste-file {}", .{cw.fmtLazyPath(data, .file)});
try cw.code.print("paste-file {f}", .{cw.fmtLazyPath(data, .file)});
},

.mbr_part_table => |data| {
Expand Down Expand Up @@ -176,7 +178,7 @@ const ContentWriter = struct {
try cw.code.writeByte('\n');

if (part.name) |name| {
try cw.code.print(" name \"{}\"\n", .{std.zig.fmtEscapes(name)});
try cw.code.print(" name \"{f}\"\n", .{std.zig.fmtString(name)});
}
if (part.offset) |offset| {
try cw.code.print(" offset {d}\n", .{offset});
Expand All @@ -198,7 +200,7 @@ const ContentWriter = struct {
@tagName(data.format),
});
if (data.label) |label| {
try cw.code.print(" label {}\n", .{
try cw.code.print(" label {f}\n", .{
fmtPath(label),
});
}
Expand All @@ -213,29 +215,29 @@ const ContentWriter = struct {
fn renderFileSystemTree(cw: ContentWriter, fs: FileSystem) !void {
for (fs.items) |item| {
switch (item) {
.empty_dir => |dir| try cw.code.print("mkdir {}\n", .{
.empty_dir => |dir| try cw.code.print("mkdir {f}\n", .{
fmtPath(dir),
}),

.copy_dir => |copy| try cw.code.print("copy-dir {} {}\n", .{
.copy_dir => |copy| try cw.code.print("copy-dir {f} {f}\n", .{
fmtPath(copy.destination),
cw.fmtLazyPath(copy.source, .directory),
}),

.copy_file => |copy| try cw.code.print("copy-file {} {}\n", .{
.copy_file => |copy| try cw.code.print("copy-file {f} {f}\n", .{
fmtPath(copy.destination),
cw.fmtLazyPath(copy.source, .file),
}),

.include_script => |script| try cw.code.print("!include {}\n", .{
.include_script => |script| try cw.code.print("!include {f}\n", .{
cw.fmtLazyPath(script, .file),
}),
}
}
}

const PathFormatter = std.fmt.Formatter(formatPath);
const LazyPathFormatter = std.fmt.Formatter(formatLazyPath);
const PathFormatter = std.fmt.Alt([]const u8, formatPath);
const LazyPathFormatter = std.fmt.Alt(struct { ContentWriter, std.Build.LazyPath, UsageHint }, formatLazyPath);
const UsageHint = enum { file, directory };

fn fmtLazyPath(cw: ContentWriter, path: std.Build.LazyPath, hint: UsageHint) LazyPathFormatter {
Expand All @@ -248,13 +250,9 @@ const ContentWriter = struct {

fn formatLazyPath(
data: struct { ContentWriter, std.Build.LazyPath, UsageHint },
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void {
writer: *std.Io.Writer,
) error{WriteFailed}!void {
const cw, const path, const hint = data;
_ = fmt;
_ = options;

switch (path) {
.cwd_relative,
Expand All @@ -267,7 +265,7 @@ const ContentWriter = struct {

std.debug.assert(std.fs.path.isAbsolute(full_path));

try writer.print("{}", .{
try writer.print("{f}", .{
fmtPath(full_path),
});
},
Expand All @@ -278,7 +276,7 @@ const ContentWriter = struct {
const var_id = cw.vars.count() + 1;
const var_name = cw.wfs.step.owner.fmt("PATH{}", .{var_id});

try cw.vars.put(var_name, .{ path, hint });
cw.vars.put(var_name, .{ path, hint }) catch return error.WriteFailed;

try writer.print("${s}", .{var_name});
},
Expand All @@ -287,13 +285,8 @@ const ContentWriter = struct {

fn formatPath(
path: []const u8,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
writer: *std.Io.Writer,
) !void {
_ = fmt;
_ = options;

const is_safe_word = for (path) |char| {
switch (char) {
'A'...'Z',
Expand All @@ -318,7 +311,7 @@ const ContentWriter = struct {
if (c == '\\') {
try writer.writeAll("/");
} else {
try writer.print("{}", .{std.zig.fmtEscapes(&[_]u8{c})});
try writer.print("{f}", .{std.zig.fmtString(&[_]u8{c})});
}
}

Expand Down Expand Up @@ -419,7 +412,7 @@ pub const FatFs = struct {

pub const FileSystemBuilder = struct {
b: *std.Build,
list: std.ArrayListUnmanaged(FileSystem.Item),
list: std.ArrayList(FileSystem.Item),

pub fn init(b: *std.Build) FileSystemBuilder {
return FileSystemBuilder{
Expand Down
28 changes: 15 additions & 13 deletions src/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub fn get_include_path(parser: Parser, allocator: std.mem.Allocator, rel_includ
const top_path = if (parser.file_stack.len > 0)
parser.file_stack[parser.file_stack.len - 1].path
else
"";
"";

const abs_include_path = try std.fs.path.resolvePosix(
allocator,
Expand Down Expand Up @@ -208,10 +208,12 @@ fn resolve_value(parser: *Parser, token_type: TokenType, text: []const u8) ![]co
if (!has_includes)
return content_slice;

var unescaped: std.ArrayList(u8) = .init(parser.arena.allocator());
defer unescaped.deinit();
const allocator = parser.arena.allocator();

try unescaped.ensureTotalCapacityPrecise(content_slice.len);
var unescaped = std.ArrayList(u8).empty;
defer unescaped.deinit(allocator);

try unescaped.ensureTotalCapacityPrecise(allocator, content_slice.len);

{
var i: usize = 0;
Expand All @@ -220,7 +222,7 @@ fn resolve_value(parser: *Parser, token_type: TokenType, text: []const u8) ![]co
i += 1;

if (c != '\\') {
try unescaped.append(c);
try unescaped.append(allocator, c);
continue;
}

Expand All @@ -233,20 +235,20 @@ fn resolve_value(parser: *Parser, token_type: TokenType, text: []const u8) ![]co
errdefer std.log.err("invalid escape sequence: \\{s}", .{[_]u8{esc_code}});

switch (esc_code) {
'r' => try unescaped.append('\r'),
'n' => try unescaped.append('\n'),
't' => try unescaped.append('\t'),
'\\' => try unescaped.append('\\'),
'\"' => try unescaped.append('\"'),
'\'' => try unescaped.append('\''),
'e' => try unescaped.append('\x1B'),
'r' => try unescaped.append(allocator, '\r'),
'n' => try unescaped.append(allocator, '\n'),
't' => try unescaped.append(allocator, '\t'),
'\\' => try unescaped.append(allocator, '\\'),
'\"' => try unescaped.append(allocator, '\"'),
'\'' => try unescaped.append(allocator, '\''),
'e' => try unescaped.append(allocator, '\x1B'),

else => return error.InvalidEscapeSequence,
}
}
}

return try unescaped.toOwnedSlice();
return try unescaped.toOwnedSlice(allocator);
},

.comment, .directive, .whitespace => unreachable,
Expand Down
4 changes: 2 additions & 2 deletions src/Tokenizer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ test Tokenizer {
var offset: u32 = 0;
for (seq) |expected| {
const actual = (try tokenizer.next()) orelse return error.Unexpected;
errdefer std.debug.print("unexpected token: .{} \"{}\"\n", .{
errdefer std.debug.print("unexpected token: .{f} \"{f}\"\n", .{
std.zig.fmtId(@tagName(actual.type)),
std.zig.fmtEscapes(tokenizer.source[actual.offset..][0..actual.len]),
std.zig.fmtString(tokenizer.source[actual.offset..][0..actual.len]),
});
try std.testing.expectEqualStrings(expected.@"1", tokenizer.get_text(actual));
try std.testing.expectEqual(offset, actual.offset);
Expand Down
6 changes: 4 additions & 2 deletions src/components/FillData.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ pub fn parse(ctx: dim.Context) !dim.Content {
}

fn render(self: *FillData, stream: *dim.BinaryStream) dim.Content.RenderError!void {
try stream.writer().writeByteNTimes(
var writer = stream.writer();
writer.interface.splatByteAll(
self.fill_value,
stream.length,
);
) catch return error.Overflow; // TODO FIX we don't know actually why this failed.
// std.Io.Writer only returns error.WriteFailed.
}
47 changes: 28 additions & 19 deletions src/components/fs/FatFileSystem.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ format_as: FatType,
label: ?[]const u8 = null,
fats: ?fatfs.FatTables = null,
rootdir_size: ?c_uint = null,
ops: std.ArrayList(common.FsOperation),
ops: std.array_list.Managed(common.FsOperation),
sector_align: ?c_uint = null,
cluster_size: ?u32 = null,

Expand Down Expand Up @@ -88,7 +88,7 @@ fn render(self: *FAT, stream: *dim.BinaryStream) dim.Content.RenderError!void {

if (stream.length < min_size) {
// TODO(fqu): Report fatal erro!
std.log.err("cannot format {} bytes with {s}: min required size is {}", .{
std.log.err("cannot format {f} bytes with {s}: min required size is {f}", .{
@as(dim.DiskSize, @enumFromInt(stream.length)),
@tagName(self.format_as),
@as(dim.DiskSize, @enumFromInt(min_size)),
Expand All @@ -98,7 +98,7 @@ fn render(self: *FAT, stream: *dim.BinaryStream) dim.Content.RenderError!void {

if (stream.length > max_size) {
// TODO(fqu): Report warning
std.log.warn("will not use all available space: available space is {}, but maximum size for {s} is {}", .{
std.log.warn("will not use all available space: available space is {f}, but maximum size for {s} is {f}", .{
@as(dim.DiskSize, @enumFromInt(stream.length)),
@tagName(self.format_as),
@as(dim.DiskSize, @enumFromInt(min_size)),
Expand Down Expand Up @@ -147,8 +147,8 @@ fn render(self: *FAT, stream: *dim.BinaryStream) dim.Content.RenderError!void {
return error.IoError;
}
} else {
std.log.err("label \"{}\" is {} characters long, but only up to {} are permitted.", .{
std.zig.fmtEscapes(label),
std.log.err("label \"{f}\" is {} characters long, but only up to {} are permitted.", .{
std.zig.fmtString(label),
label.len,
max_label_len,
});
Expand Down Expand Up @@ -212,7 +212,7 @@ const AtomicOps = struct {
};
}

pub fn mkfile(ops: AtomicOps, path: []const u8, reader: anytype) dim.Content.RenderError!void {
pub fn mkfile(ops: AtomicOps, path: []const u8, reader: *std.Io.Reader) dim.Content.RenderError!void {
_ = ops;

var path_buffer: [max_path_len:0]u8 = undefined;
Expand Down Expand Up @@ -244,19 +244,28 @@ const AtomicOps = struct {
};
defer fs_file.close();

var fifo: std.fifo.LinearFifo(u8, .{ .Static = 8192 }) = .init();
fifo.pump(
reader,
fs_file.writer(),
) catch |err| switch (@as(dim.FileHandle.ReadError || fatfs.File.ReadError.Error, err)) {
error.Overflow => return error.IoError,
error.ReadFileFailed => return error.IoError,
error.Timeout => @panic("implementation bug in fatfs glue"),
error.DiskErr => return error.IoError,
error.IntErr => return error.IoError,
error.Denied => @panic("implementation bug in fatfs glue"),
error.InvalidObject => @panic("implementation bug in fatfs glue"),
};
var writer_buf: [8192]u8 = undefined;
var writer = fs_file.writer(&writer_buf);

_ = reader.streamRemaining(&writer.interface) catch return error.IoError;

writer.interface.flush() catch return error.IoError;

// TODO we've lost a lot of error specificity due to the use of the new APIs
// See old code:

// fifo.pump(
// reader,
// fs_file.writer(),
// ) catch |err| switch (@as(dim.FileHandle.ReadError || fatfs.File.ReadError.Error, err)) {
// error.Overflow => return error.IoError,
// error.ReadFileFailed => return error.IoError,
// error.Timeout => @panic("implementation bug in fatfs glue"),
// error.DiskErr => return error.IoError,
// error.IntErr => return error.IoError,
// error.Denied => @panic("implementation bug in fatfs glue"),
// error.InvalidObject => @panic("implementation bug in fatfs glue"),
// };
}
};

Expand Down
Loading
Loading