Skip to content

Commit 129c66e

Browse files
committed
Now works on 12/28 master
1 parent d38468e commit 129c66e

10 files changed

Lines changed: 159 additions & 157 deletions

File tree

build.zig.zon

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
.zfat = .{
77
// .url = "git+https://github.com/CascadeOS/zfat#2806bfd983dd810c9c74b0ea6a63f783208518b5",
88
// .hash = "zfat-0.16.0-SNNK9fKtTgASssfmCblZwRMLU4pndVtwxTNhYCegBOyA",
9-
.path = "../zfat"
9+
.path = "../zfat",
1010
},
1111
.args = .{
12-
.url = "git+https://github.com/ikskuh/zig-args.git#8ae26b44a884ff20dca98ee84c098e8f8e94902f",
13-
.hash = "args-0.0.0-CiLiqojRAACGzDRO7A9dw7kWSchNk29caJZkXuMCb0Cn",
12+
.path = "../zig-args",
13+
// .url = "git+https://github.com/ikskuh/zig-args.git#8ae26b44a884ff20dca98ee84c098e8f8e94902f",
14+
// .hash = "args-0.0.0-CiLiqojRAACGzDRO7A9dw7kWSchNk29caJZkXuMCb0Cn",
1415
},
1516
},
1617
.paths = .{

src/Parser.zig

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ pub const Error = Tokenizer.Error || error{
2222
};
2323

2424
pub const IO = struct {
25-
fetch_file_fn: *const fn (io: *const IO, std.mem.Allocator, path: []const u8) error{ FileNotFound, IoError, OutOfMemory, InvalidPath, Canceled }![]const u8,
26-
resolve_variable_fn: *const fn (io: *const IO, name: []const u8) error{UnknownVariable}![]const u8,
25+
fetch_file_fn: *const fn (stdio: std.Io, io: *const IO, std.mem.Allocator, path: []const u8) error{ FileNotFound, IoError, OutOfMemory, InvalidPath, Canceled }![]const u8,
26+
resolve_variable_fn: *const fn (stdio: std.Io, io: *const IO, name: []const u8) error{UnknownVariable}![]const u8,
2727

28-
pub fn fetch_file(io: *const IO, allocator: std.mem.Allocator, path: []const u8) error{ FileNotFound, IoError, OutOfMemory, InvalidPath, Canceled }![]const u8 {
29-
return io.fetch_file_fn(io, allocator, path);
28+
pub fn fetch_file(io: *const IO, stdio: std.Io, allocator: std.mem.Allocator, path: []const u8) error{ FileNotFound, IoError, OutOfMemory, InvalidPath, Canceled }![]const u8 {
29+
return io.fetch_file_fn(stdio,io, allocator, path);
3030
}
3131

32-
pub fn resolve_variable(io: *const IO, name: []const u8) error{UnknownVariable}![]const u8 {
33-
return io.resolve_variable_fn(io, name);
32+
pub fn resolve_variable(io: *const IO, stdio: std.Io, name: []const u8) error{UnknownVariable}![]const u8 {
33+
return io.resolve_variable_fn(stdio,io, name);
3434
}
3535
};
3636

@@ -85,10 +85,10 @@ pub fn push_source(parser: *Parser, options: struct {
8585
};
8686
}
8787

88-
pub fn push_file(parser: *Parser, include_path: []const u8) !void {
88+
pub fn push_file(parser: *Parser, stdio: std.Io, include_path: []const u8) !void {
8989
const abs_include_path = try parser.get_include_path(parser.arena.allocator(), include_path);
9090

91-
const file_contents = try parser.io.fetch_file(parser.arena.allocator(), abs_include_path);
91+
const file_contents = try parser.io.fetch_file(stdio , parser.arena.allocator(), abs_include_path);
9292

9393
const index = parser.file_stack.len;
9494
parser.file_stack.len += 1;
@@ -121,14 +121,14 @@ pub fn get_include_path(parser: Parser, allocator: std.mem.Allocator, rel_includ
121121
return abs_include_path;
122122
}
123123

124-
pub fn next(parser: *Parser) (Error || error{UnexpectedEndOfFile})![]const u8 {
125-
return if (try parser.next_or_eof()) |word|
124+
pub fn next(parser: *Parser, stdio: std.Io) (Error || error{UnexpectedEndOfFile})![]const u8 {
125+
return if (try parser.next_or_eof(stdio)) |word|
126126
word
127127
else
128128
error.UnexpectedEndOfFile;
129129
}
130130

131-
pub fn next_or_eof(parser: *Parser) Error!?[]const u8 {
131+
pub fn next_or_eof(parser: *Parser, stdio: std.Io) Error!?[]const u8 {
132132
fetch_loop: while (parser.file_stack.len > 0) {
133133
const top = &parser.file_stack[parser.file_stack.len - 1];
134134

@@ -142,7 +142,7 @@ pub fn next_or_eof(parser: *Parser) Error!?[]const u8 {
142142
switch (token.type) {
143143
.whitespace, .comment => unreachable,
144144

145-
.word, .variable, .string => return try parser.resolve_value(
145+
.word, .variable, .string => return try parser.resolve_value(stdio,
146146
token.type,
147147
top.tokenizer.get_text(token),
148148
),
@@ -153,14 +153,14 @@ pub fn next_or_eof(parser: *Parser) Error!?[]const u8 {
153153
if (std.mem.eql(u8, directive, "!include")) {
154154
if (try fetch_token(&top.tokenizer)) |path_token| {
155155
const rel_include_path = switch (path_token.type) {
156-
.word, .variable, .string => try parser.resolve_value(
156+
.word, .variable, .string => try parser.resolve_value(stdio,
157157
path_token.type,
158158
top.tokenizer.get_text(path_token),
159159
),
160160
.comment, .directive, .whitespace => return error.BadDirective,
161161
};
162162

163-
try parser.push_file(rel_include_path);
163+
try parser.push_file(stdio, rel_include_path);
164164
} else {
165165
return error.ExpectedIncludePath;
166166
}
@@ -190,11 +190,11 @@ fn fetch_token(tok: *Tokenizer) Tokenizer.Error!?Token {
190190
}
191191
}
192192

193-
fn resolve_value(parser: *Parser, token_type: TokenType, text: []const u8) ![]const u8 {
193+
fn resolve_value(parser: *Parser, stdio: std.Io, token_type: TokenType, text: []const u8) ![]const u8 {
194194
return switch (token_type) {
195195
.word => text,
196196

197-
.variable => try parser.io.resolve_variable(
197+
.variable => try parser.io.resolve_variable(stdio,
198198
text[1..],
199199
),
200200

src/components/EmptyData.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ const dim = @import("../dim.zig");
88

99
const EmptyData = @This();
1010

11-
pub fn parse(ctx: dim.Context) !dim.Content {
11+
pub fn parse(ctx: dim.Context, stdio: std.Io) !dim.Content {
1212
_ = ctx;
13+
_ = stdio;
1314
return .create_handle(undefined, .create(@This(), .{
1415
.render_fn = render,
1516
}));

src/components/FillData.zig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,18 @@ const FillData = @This();
99

1010
fill_value: u8,
1111

12-
pub fn parse(ctx: dim.Context) !dim.Content {
12+
pub fn parse(ctx: dim.Context, stdio: std.Io) !dim.Content {
1313
const pf = try ctx.alloc_object(FillData);
1414
pf.* = .{
15-
.fill_value = try ctx.parse_integer(u8, 0),
15+
.fill_value = try ctx.parse_integer(stdio, u8, 0),
1616
};
1717
return .create_handle(pf, .create(@This(), .{
1818
.render_fn = render,
1919
}));
2020
}
2121

2222
fn render(self: *FillData, io: std.Io, stream: *dim.BinaryStream) dim.Content.RenderError!void {
23-
_ = io;
24-
var writer = stream.writer();
23+
var writer = stream.writer(io);
2524
writer.interface.splatByteAll(
2625
self.fill_value,
2726
stream.length,

src/components/PasteFile.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ const PasteFile = @This();
55

66
file_handle: dim.FileName,
77

8-
pub fn parse(ctx: dim.Context) !dim.Content {
8+
pub fn parse(ctx: dim.Context, stdio: std.Io) !dim.Content {
99
const pf = try ctx.alloc_object(PasteFile);
1010
pf.* = .{
11-
.file_handle = try ctx.parse_file_name(),
11+
.file_handle = try ctx.parse_file_name(stdio),
1212
};
1313
return .create_handle(pf, .create(@This(), .{
1414
.render_fn = render,

src/components/fs/FatFileSystem.zig

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ ops: std.array_list.Managed(common.FsOperation),
1818
sector_align: ?c_uint = null,
1919
cluster_size: ?u32 = null,
2020

21-
pub fn parse(ctx: dim.Context) !dim.Content {
22-
const fat_type = try ctx.parse_enum(FatType);
21+
pub fn parse(ctx: dim.Context, stdio: std.Io) !dim.Content {
22+
const fat_type = try ctx.parse_enum(stdio,FatType);
2323

2424
const pf = try ctx.alloc_object(FAT);
2525
pf.* = .{
@@ -32,7 +32,7 @@ pub fn parse(ctx: dim.Context) !dim.Content {
3232
.updater = .init(ctx, pf),
3333
};
3434

35-
try common.parse_ops(ctx, "endfat", &appender);
35+
try common.parse_ops(ctx, stdio, "endfat", &appender);
3636

3737
try appender.updater.validate();
3838

@@ -59,7 +59,7 @@ const Appender = struct {
5959
try self.fat.ops.append(op);
6060
}
6161

62-
pub fn parse_custom_op(self: *@This(), ctx: dim.Context, str_op: []const u8) !void {
62+
pub fn parse_custom_op(self: *@This(), stdio: std.Io, ctx: dim.Context, str_op: []const u8) !void {
6363
const Op = enum {
6464
label,
6565
fats,
@@ -72,11 +72,11 @@ const Appender = struct {
7272
.{str_op},
7373
);
7474
switch (op) {
75-
.label => try self.updater.set(.label, try ctx.parse_string()),
76-
.fats => try self.updater.set(.fats, try ctx.parse_enum(fatfs.FatTables)),
77-
.@"root-size" => try self.updater.set(.rootdir_size, try ctx.parse_integer(c_uint, 0)),
78-
.@"sector-align" => try self.updater.set(.sector_align, try ctx.parse_integer(c_uint, 0)),
79-
.@"cluster-size" => try self.updater.set(.cluster_size, try ctx.parse_integer(u32, 0)),
75+
.label => try self.updater.set(.label, try ctx.parse_string(stdio)),
76+
.fats => try self.updater.set(.fats, try ctx.parse_enum(stdio, fatfs.FatTables)),
77+
.@"root-size" => try self.updater.set(.rootdir_size, try ctx.parse_integer(stdio,c_uint, 0)),
78+
.@"sector-align" => try self.updater.set(.sector_align, try ctx.parse_integer(stdio, c_uint, 0)),
79+
.@"cluster-size" => try self.updater.set(.cluster_size, try ctx.parse_integer(stdio, u32, 0)),
8080
}
8181
}
8282
};
@@ -284,10 +284,9 @@ const BinaryStreamDisk = struct {
284284
}
285285

286286
fn disk_write(intf: *fatfs.Disk, io: std.Io, buff: [*]const u8, sector: fatfs.LBA, count: c_uint) fatfs.Disk.Error!void {
287-
_ = io;
288287
const bsd: *BinaryStreamDisk = @fieldParentPtr("disk", intf);
289288

290-
bsd.stream.write(block_size * sector, buff[0 .. count * block_size]) catch |err| {
289+
bsd.stream.write(io, block_size * sector, buff[0 .. count * block_size]) catch |err| {
291290
bsd.disk_error = err;
292291
return error.IoError;
293292
};

src/components/fs/common.zig

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn Executor(comptime T: type) type {
4949
},
5050

5151
.copy_file => |data| {
52-
var handle = data.source.open() catch |err| switch (err) {
52+
var handle = data.source.open(io) catch |err| switch (err) {
5353
error.FileNotFound => return, // open() already reported the error
5454
else => |e| return e,
5555
};
@@ -61,11 +61,11 @@ fn Executor(comptime T: type) type {
6161
try exec.add_file(data.path, &adapter.interface);
6262
},
6363
.copy_dir => |data| {
64-
var iter_dir = data.source.open_dir() catch |err| switch (err) {
64+
var iter_dir = data.source.open_dir(io) catch |err| switch (err) {
6565
error.FileNotFound => return, // open() already reported the error
6666
else => |e| return e,
6767
};
68-
defer iter_dir.close();
68+
defer iter_dir.close(io);
6969

7070
var walker_memory: [16384]u8 = undefined;
7171
var temp_allocator: std.heap.FixedBufferAllocator = .init(&walker_memory);
@@ -75,7 +75,7 @@ fn Executor(comptime T: type) type {
7575
var walker = try iter_dir.walk(temp_allocator.allocator());
7676
defer walker.deinit();
7777

78-
while (walker.next() catch |err| return walk_err(err)) |entry| {
78+
while (walker.next(io) catch |err| return walk_err(err)) |entry| {
7979
const path = std.fmt.bufPrintZ(&path_memory, "{s}/{s}", .{
8080
data.path,
8181
entry.path,
@@ -91,7 +91,7 @@ fn Executor(comptime T: type) type {
9191
.rel_path = entry.basename,
9292
};
9393

94-
var file = try fname.open();
94+
var file = try fname.open(io);
9595
defer file.close(io);
9696

9797
var buffer: [1024]u8 = undefined;
@@ -106,8 +106,8 @@ fn Executor(comptime T: type) type {
106106

107107
else => {
108108
var realpath_buffer: [std.fs.max_path_bytes]u8 = undefined;
109-
std.log.warn("cannot copy file {!s}: {s} is not a supported file type!", .{
110-
entry.dir.realpath(entry.path, &realpath_buffer),
109+
std.log.warn("cannot copy file {s}: {s} is not a supported file type!", .{
110+
if (entry.dir.realPathFile(io,entry.path, &realpath_buffer)) |l| realpath_buffer[0..l] else |e| @errorName(e),
111111
@tagName(entry.kind),
112112
});
113113
},
@@ -157,10 +157,9 @@ fn Executor(comptime T: type) type {
157157
try exec.inner.mkdir(path);
158158
}
159159

160-
fn walk_err(err: (std.fs.Dir.OpenError || std.mem.Allocator.Error)) dim.Content.RenderError {
160+
fn walk_err(err: (std.Io.Dir.OpenError || std.mem.Allocator.Error)) dim.Content.RenderError {
161161
return switch (err) {
162-
error.BadPathName,
163-
error.NameTooLong => error.InvalidPath,
162+
error.BadPathName, error.NameTooLong => error.InvalidPath,
164163

165164
error.OutOfMemory => error.OutOfMemory,
166165
error.FileNotFound => error.FileNotFound,
@@ -177,14 +176,15 @@ fn Executor(comptime T: type) type {
177176
error.ProcessFdQuotaExceeded,
178177
error.SystemFdQuotaExceeded,
179178
error.NotDir,
180-
error.PermissionDenied, => error.IoError,
179+
error.PermissionDenied,
180+
=> error.IoError,
181181
};
182182
}
183183
};
184184
}
185185

186-
fn parse_path(ctx: dim.Context) ![:0]const u8 {
187-
const path = try ctx.parse_string();
186+
fn parse_path(ctx: dim.Context, stdio: std.Io) ![:0]const u8 {
187+
const path = try ctx.parse_string(stdio);
188188

189189
if (path.len == 0) {
190190
try ctx.report_nonfatal_error("Path cannot be empty!", .{});
@@ -216,41 +216,41 @@ fn parse_path(ctx: dim.Context) ![:0]const u8 {
216216
return try normalize(ctx.get_arena(), path);
217217
}
218218

219-
pub fn parse_ops(ctx: dim.Context, end_seq: []const u8, handler: anytype) !void {
219+
pub fn parse_ops(ctx: dim.Context, stdio: std.Io, end_seq: []const u8, handler: anytype) !void {
220220
while (true) {
221-
const opsel = try ctx.parse_string();
221+
const opsel = try ctx.parse_string(stdio);
222222
if (std.mem.eql(u8, opsel, end_seq))
223223
return;
224224

225225
if (std.mem.eql(u8, opsel, "mkdir")) {
226-
const path = try parse_path(ctx);
226+
const path = try parse_path(ctx, stdio);
227227
try handler.append_common_op(FsOperation{
228228
.make_dir = .{ .path = path },
229229
});
230230
} else if (std.mem.eql(u8, opsel, "copy-dir")) {
231-
const path = try parse_path(ctx);
232-
const src = try ctx.parse_file_name();
231+
const path = try parse_path(ctx, stdio);
232+
const src = try ctx.parse_file_name(stdio);
233233

234234
try handler.append_common_op(FsOperation{
235235
.copy_dir = .{ .path = path, .source = src },
236236
});
237237
} else if (std.mem.eql(u8, opsel, "copy-file")) {
238-
const path = try parse_path(ctx);
239-
const src = try ctx.parse_file_name();
238+
const path = try parse_path(ctx, stdio);
239+
const src = try ctx.parse_file_name(stdio);
240240

241241
try handler.append_common_op(FsOperation{
242242
.copy_file = .{ .path = path, .source = src },
243243
});
244244
} else if (std.mem.eql(u8, opsel, "create-file")) {
245-
const path = try parse_path(ctx);
246-
const size = try ctx.parse_mem_size();
247-
const contents = try ctx.parse_content();
245+
const path = try parse_path(ctx, stdio);
246+
const size = try ctx.parse_mem_size(stdio);
247+
const contents = try ctx.parse_content(stdio);
248248

249249
try handler.append_common_op(FsOperation{
250250
.create_file = .{ .path = path, .size = size, .contents = contents },
251251
});
252252
} else {
253-
try handler.parse_custom_op(ctx, opsel);
253+
try handler.parse_custom_op(stdio, ctx, opsel);
254254
}
255255
}
256256
}

0 commit comments

Comments
 (0)