diff --git a/src/main.zig b/src/main.zig index a5b265f..0e58df0 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,27 +1,98 @@ const std = @import("std"); const ink = @import("ink"); +const fatal = std.process.fatal; + +var stdin_buffer: [4096]u8 align(std.heap.page_size_min) = undefined; +var stdout_buffer: [4096]u8 align(std.heap.page_size_min) = undefined; +var debug_allocator: std.heap.DebugAllocator(.{}) = .init; +const max_src_size = std.math.maxInt(u32); pub fn main() !void { - // Prints to stderr, ignoring potential errors. - std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); - try ink.bufferedPrint(); + const gpa = debug_allocator.allocator(); + defer _ = debug_allocator.deinit(); + + var arena_allocator = std.heap.ArenaAllocator.init(gpa); + defer arena_allocator.deinit(); + + const arena = arena_allocator.allocator(); + const args = try std.process.argsAlloc(gpa); + defer std.process.argsFree(gpa, args); + + if (args.len < 2) { + fatal("Not enough arguments!", .{}); + } + return mainArgs(gpa, arena, args); } -test "simple test" { - const gpa = std.testing.allocator; - var list: std.ArrayList(i32) = .empty; - defer list.deinit(gpa); // Try commenting this out and see if zig detects the memory leak! - try list.append(gpa, 42); - try std.testing.expectEqual(@as(i32, 42), list.pop()); -} +fn mainArgs( + gpa: std.mem.Allocator, + arena: std.mem.Allocator, + args_list: []const [:0]const u8, +) !void { + var source_path: ?[]const u8 = null; + var arg_index: usize = 1; -test "fuzz example" { - const Context = struct { - fn testOne(context: @This(), input: []const u8) anyerror!void { - _ = context; - // Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case! - try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input)); + while (arg_index < args_list.len) : (arg_index += 1) { + const arg = args_list[arg_index]; + if (std.mem.startsWith(u8, arg, "-")) { + // TODO: Parse CLI options. + } else if (source_path == null) { + source_path = arg; + } else { + fatal("invalid positional argument: '{s}'", .{arg}); } + } + + const filename = source_path orelse ""; + const source_bytes: [:0]const u8 = s: { + var f = if (source_path) |p| file: { + break :file std.fs.cwd().openFile(p, .{}) catch |err| { + fatal("unable to open file '{s}': {s}", .{ p, @errorName(err) }); + }; + } else std.fs.File.stdin(); + defer if (source_path != null) f.close(); + + var file_reader: std.fs.File.Reader = f.reader(&stdin_buffer); + break :s readSourceFile(arena, &file_reader) catch |err| { + fatal("unable to load file '{s}': {s}", .{ filename, @errorName(err) }); + }; }; - try std.testing.fuzz(Context{}, Context.testOne, .{}); + + var story = try ink.Story.loadFromString(gpa, source_bytes, .{}); + defer story.deinit(); +} + +fn readSourceFile(gpa: std.mem.Allocator, file_reader: *std.fs.File.Reader) ![:0]u8 { + var buffer: std.ArrayListUnmanaged(u8) = .empty; + defer buffer.deinit(gpa); + + if (file_reader.getSize()) |size| { + const casted_size = std.math.cast(u32, size) orelse return error.StreamTooLong; + try buffer.ensureTotalCapacityPrecise(gpa, casted_size + 1); + } else |_| {} + + try file_reader.interface.appendRemaining(gpa, &buffer, .limited(max_src_size)); + + const unsupported_boms = [_][]const u8{ + "\xff\xfe\x00\x00", // UTF-32 little endian + "\xfe\xff\x00\x00", // UTF-32 big endian + "\xfe\xff", // UTF-16 big endian + }; + for (unsupported_boms) |bom| { + if (std.mem.startsWith(u8, buffer.items, bom)) { + return error.UnsupportedEncoding; + } + } + if (std.mem.startsWith(u8, buffer.items, "\xff\xfe")) { + if (buffer.items.len % 2 != 0) { + return error.InvalidEncoding; + } + return std.unicode.utf16LeToUtf8AllocZ(gpa, @ptrCast(@alignCast(buffer.items))) catch |err| switch (err) { + error.DanglingSurrogateHalf => error.UnsupportedEncoding, + error.ExpectedSecondSurrogateHalf => error.UnsupportedEncoding, + error.UnexpectedSecondSurrogateHalf => error.UnsupportedEncoding, + else => |e| return e, + }; + } + return buffer.toOwnedSliceSentinel(gpa, 0); } diff --git a/src/root.zig b/src/root.zig index 94c7cd0..2e51d00 100644 --- a/src/root.zig +++ b/src/root.zig @@ -1,23 +1,16 @@ -//! By convention, root.zig is the root source file when making a library. const std = @import("std"); -pub fn bufferedPrint() !void { - // Stdout is for the actual output of your application, for example if you - // are implementing gzip, then only the compressed bytes should be sent to - // stdout, not any debugging messages. - var stdout_buffer: [1024]u8 = undefined; - var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer); - const stdout = &stdout_writer.interface; +pub const Story = struct { + pub const LoadOptions = struct {}; - try stdout.print("Run `zig build test` to run the tests.\n", .{}); + pub fn loadFromString( + _: std.mem.Allocator, + source_bytes: [:0]const u8, + _: LoadOptions, + ) !Story { + std.debug.print("{s}\n", .{source_bytes}); + return .{}; + } + pub fn deinit(_: *Story) void {} +}; - try stdout.flush(); // Don't forget to flush! -} - -pub fn add(a: i32, b: i32) i32 { - return a + b; -} - -test "basic add functionality" { - try std.testing.expect(add(3, 7) == 10); -}