@@ -5,7 +5,8 @@ const Yaml = @import("yaml").Yaml;
55
66const mem = std .mem ;
77
8- var gpa = std .heap .GeneralPurposeAllocator (.{}){};
8+ var gpa_alloc = std .heap .GeneralPurposeAllocator (.{}){};
9+ const gpa = gpa_alloc .allocator ();
910
1011const usage =
1112 \\Usage: yaml <path-to-yaml>
@@ -54,29 +55,38 @@ fn logFn(
5455
5556pub const std_options : std.Options = .{ .logFn = logFn };
5657
57- pub fn main () ! void {
58- var arena = std .heap .ArenaAllocator .init (gpa . allocator () );
59- defer arena .deinit ();
60- const allocator = arena .allocator ();
58+ pub fn main (init : std.process.Init.Minimal ) ! void {
59+ var arena_alloc = std .heap .ArenaAllocator .init (gpa );
60+ defer arena_alloc .deinit ();
61+ const arena = arena_alloc .allocator ();
6162
62- const all_args = try std .process .argsAlloc (allocator );
63+ var threaded : std.Io.Threaded = .init (gpa , .{
64+ .argv0 = .init (init .args ),
65+ .environ = init .environ ,
66+ });
67+ defer threaded .deinit ();
68+ const io = threaded .io ();
69+
70+ const all_args = try init .args .toSlice (arena );
6371 const args = all_args [1.. ];
6472
65- const stdout = std .fs .File .stdout ();
66- const stderr = std .fs .File .stderr ();
73+ var stdout_buffer : [1024 ]u8 = undefined ;
74+ var stdout = std .Io .File .stdout ().writer (io , & stdout_buffer );
75+ var stderr_buffer : [1024 ]u8 = undefined ;
76+ var stderr = std .Io .File .stderr ().writer (io , & stderr_buffer );
6777
6878 var file_path : ? []const u8 = null ;
6979 var arg_index : usize = 0 ;
7080 while (arg_index < args .len ) : (arg_index += 1 ) {
7181 if (mem .eql (u8 , "-h" , args [arg_index ]) or mem .eql (u8 , "--help" , args [arg_index ])) {
72- return stdout .writeAll (usage );
82+ return stdout .interface . writeAll (usage );
7383 } else if (mem .eql (u8 , "--debug-log" , args [arg_index ])) {
7484 if (arg_index + 1 >= args .len ) {
75- return stderr .writeAll ("fatal: expected [scope] after --debug-log\n\n " );
85+ return stderr .interface . writeAll ("fatal: expected [scope] after --debug-log\n\n " );
7686 }
7787 arg_index += 1 ;
7888 if (! build_options .enable_logging ) {
79- try stderr .writeAll ("warn: --debug-log will have no effect as program was not built with -Dlog\n\n " );
89+ try stderr .interface . writeAll ("warn: --debug-log will have no effect as program was not built with -Dlog\n\n " );
8090 } else {
8191 try log_scopes .append (args [arg_index ]);
8292 }
@@ -86,26 +96,28 @@ pub fn main() !void {
8696 }
8797
8898 if (file_path == null ) {
89- return stderr .writeAll ("fatal: no input path to yaml file specified\n\n " );
99+ return stderr .interface . writeAll ("fatal: no input path to yaml file specified\n\n " );
90100 }
91101
92- const file = try std .fs .cwd ().openFile (file_path .? , .{});
93- defer file .close ();
102+ const file = try std .Io .Dir .cwd ().openFile (io , file_path .? , .{});
103+ defer file .close (io );
104+
105+ var file_buffer : [1024 ]u8 = undefined ;
106+ var file_reader = file .reader (io , & file_buffer );
94107
95- const source = try file . readToEndAlloc ( allocator , std . math . maxInt ( u32 ) );
108+ const source = try file_reader . interface . allocRemaining ( arena , .unlimited );
96109
97110 var yaml : Yaml = .{ .source = source };
98- defer yaml .deinit (allocator );
111+ defer yaml .deinit (arena );
99112
100- yaml .load (allocator ) catch | err | switch (err ) {
113+ yaml .load (arena ) catch | err | switch (err ) {
101114 error .ParseFailure = > {
102115 assert (yaml .parse_errors .errorMessageCount () > 0 );
103- yaml .parse_errors .renderToStdErr (.{ . ttyconf = std . io . tty . detectConfig ( stderr ) }) ;
116+ yaml .parse_errors .renderToStderr ( io , .{}, .auto ) catch {} ;
104117 return error .ParseFailure ;
105118 },
106119 else = > return err ,
107120 };
108121
109- var writer = stdout .writer (&[0 ]u8 {});
110- try yaml .stringify (& writer .interface );
122+ try yaml .stringify (& stdout .interface );
111123}
0 commit comments