| ... | @@ -7,11 +7,10 @@ import "std.zig"; | ... | @@ -7,11 +7,10 @@ import "std.zig"; |
| 7 | // * update std API | 7 | // * update std API |
| 8 | // * defer | 8 | // * defer |
| 9 | // * %return | 9 | // * %return |
| 10 | // * %% operator | 10 | // * %% binary operator |
| | 11 | // * %% prefix operator |
| 11 | // * cast err type to string | 12 | // * cast err type to string |
| 12 | | 13 | |
| 13 | pub %.Invalid; | | |
| 14 | | | |
| 15 | pub fn main(args: [][]u8) %void => { | 14 | pub fn main(args: [][]u8) %void => { |
| 16 | const exe = args[0]; | 15 | const exe = args[0]; |
| 17 | var catted_anything = false; | 16 | var catted_anything = false; |
| ... | @@ -24,7 +23,7 @@ pub fn main(args: [][]u8) %void => { | ... | @@ -24,7 +23,7 @@ pub fn main(args: [][]u8) %void => { |
| 24 | } else { | 23 | } else { |
| 25 | var is: InputStream; | 24 | var is: InputStream; |
| 26 | is.open(arg, OpenReadOnly) %% (err) => { | 25 | is.open(arg, OpenReadOnly) %% (err) => { |
| 27 | stderr.print("Unable to open file: {}", ([]u8])(err)); | 26 | %%stderr.print("Unable to open file: {}", ([]u8])(err)); |
| 28 | return err; | 27 | return err; |
| 29 | } | 28 | } |
| 30 | defer is.close(); | 29 | defer is.close(); |
| ... | @@ -39,7 +38,7 @@ pub fn main(args: [][]u8) %void => { | ... | @@ -39,7 +38,7 @@ pub fn main(args: [][]u8) %void => { |
| 39 | } | 38 | } |
| 40 | | 39 | |
| 41 | fn usage(exe: []u8) %void => { | 40 | fn usage(exe: []u8) %void => { |
| 42 | stderr.print("Usage: {} [FILE]...\n", exe); | 41 | %%stderr.print("Usage: {} [FILE]...\n", exe); |
| 43 | return %.Invalid; | 42 | return %.Invalid; |
| 44 | } | 43 | } |
| 45 | | 44 | |
| ... | @@ -47,16 +46,18 @@ fn cat_stream(is: InputStream) %void => { | ... | @@ -47,16 +46,18 @@ fn cat_stream(is: InputStream) %void => { |
| 47 | var buf: [1024 * 4]u8; | 46 | var buf: [1024 * 4]u8; |
| 48 | | 47 | |
| 49 | while (true) { | 48 | while (true) { |
| 50 | const bytes_read = is.read(buf); | 49 | const bytes_read = is.read(buf) %% (err) => { |
| 51 | if (bytes_read < 0) { | 50 | %%stderr.print("Unable to read from stream: {}", ([]u8)(err)); |
| 52 | stderr.print("Unable to read from stream: {}", ([]u8)(is.err)); | 51 | return err; |
| 53 | return is.err; | 52 | } |
| | 53 | |
| | 54 | if (bytes_read == 0) { |
| | 55 | break; |
| 54 | } | 56 | } |
| 55 | | 57 | |
| 56 | const bytes_written = stdout.write(buf[0...bytes_read]); | 58 | stdout.write(buf[0...bytes_read]) %% (err) => { |
| 57 | if (bytes_written < bytes_read) { | 59 | %%stderr.print("Unable to write to stdout: {}", ([]u8)(err)); |
| 58 | stderr.print("Unable to write to stdout: {}", ([]u8)(stdout.err)); | 60 | return err; |
| 59 | return stdout.err; | | |
| 60 | } | 61 | } |
| 61 | } | 62 | } |
| 62 | } | 63 | } |