hautils

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 1f85c49b5310497454846c18396270b15142eb0e
parent 249e86082c4c66897f8ef49bccce5e34950cd3f5
Author: Byron Torres <b@torresjrjr.com>
Date:   Sat, 25 Sep 2021 23:13:00 +0100

Update for case statements

Diffstat:
Mcat.ha | 8+++++---
Mhead.ha | 24+++++++++++++++---------
Mmain/main.ha | 8+++++---
Mnl.ha | 223++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
Msleep.ha | 6++++--
Mtee.ha | 18+++++++++++-------
Muname.ha | 18++++++++++++------
7 files changed, 183 insertions(+), 122 deletions(-)

diff --git a/cat.ha b/cat.ha @@ -16,9 +16,11 @@ export fn utilmain() (main::error | void) = { for (let i = 0z; i < len(cmd.args); i += 1z) { const file = match (os::open(cmd.args[i])) { - err: fs::error => fmt::fatal("Error opening '{}': {}", - cmd.args[i], fs::strerror(err)), - file: io::file => file, + case err: fs::error => + fmt::fatal("Error opening '{}': {}", + cmd.args[i], fs::strerror(err)); + case file: io::file => + yield file; }; defer io::close(&file); io::copy(os::stdout, &file)?; diff --git a/head.ha b/head.ha @@ -18,9 +18,10 @@ export fn utilmain() (void | main::error) = { const opt = cmd.opts[i]; // Only opt is -n n = match (strconv::stou(opt.1)) { - (strconv::invalid | strconv::overflow) => - fmt::fatal("Invalid number given for -n"), - n: uint => n, + case (strconv::invalid | strconv::overflow) => + fmt::fatal("Invalid number given for -n"); + case n: uint => + yield n; }; }; @@ -30,9 +31,11 @@ export fn utilmain() (void | main::error) = { for (let i = 0z; i < len(cmd.args); i += 1) { const file = match (os::open(cmd.args[i])) { - err: fs::error => fmt::fatal("Error opening '{}': {}", - cmd.args[i], fs::strerror(err)), - file: io::file => file, + case err: fs::error => + fmt::fatal("Error opening '{}': {}", + cmd.args[i], fs::strerror(err)); + case file: io::file => + yield file; }; defer io::close(&file); static let buf: [os::BUFSIZ]u8 = [0...]; @@ -45,9 +48,12 @@ export fn utilmain() (void | main::error) = { fn head(in: *io::stream, n: uint) (void | main::error) = { for (n > 0; n -= 1) { let line = match (bufio::scanline(in)) { - err: io::error => return err, - io::EOF => break, - line: []u8 => line, + case err: io::error => + return err; + case io::EOF => + break; + case line: []u8 => + yield line; }; defer free(line); io::write(os::stdout, line)?; diff --git a/main/main.ha b/main/main.ha @@ -9,8 +9,10 @@ export type error = !(io::error | fs::error); // Shared main function for all utilities, to simplify error handling. export @symbol("main") fn main() void = { match (utilmain()) { - err: io::error => fmt::fatal("I/O error: {}", io::strerror(err)), - err: fs::error => fmt::fatal("Filesystem error: {}", fs::strerror(err)), - void => void, + case err: io::error => + fmt::fatal("I/O error: {}", io::strerror(err)); + case err: fs::error => + fmt::fatal("Filesystem error: {}", fs::strerror(err)); + case void => void; }; }; diff --git a/nl.ha b/nl.ha @@ -75,59 +75,91 @@ export fn utilmain() (void | main::error) = { for (let i = 0z; i < len(cmd.opts); i += 1) { const opt = cmd.opts[i]; switch (opt.0) { - 'p' => paged_numbering = false, - 'b' => body_style = switch (opt.1) { - * => usage(help), - "a" => style::ALL, - "t" => style::TXT, - "n" => style::NON, - }, - 'd' => delim = switch (len(opt.1)) { - * => usage(help), - 1 => fmt::bsprintf(delim_buf, "{}:", opt.1), - 2 => opt.1, - }, - 'f' => foot_style = switch (opt.1) { - * => usage(help), - "a" => style::ALL, - "t" => style::TXT, - "n" => style::NON, - }, - 'h' => head_style = switch (opt.1) { - * => usage(help), - "a" => style::ALL, - "t" => style::TXT, - "n" => style::NON, - }, - 'i' => ctx.incr = match (strconv::stoi(opt.1)) { - (strconv::invalid | strconv::overflow) => usage(help), - incr: int => incr, - }, - 'l' => ctx.maxblanks = match (strconv::stou(opt.1)) { - (strconv::invalid | strconv::overflow) => usage(help), - maxblanks: uint => switch (maxblanks > 0) { - false => usage(help), - true => maxblanks, - }, - }, - 'n' => ctx.mod.padding = switch (opt.1) { - * => usage(help), - "ln" => fmt::padding::ALIGN_LEFT, - "rn" => fmt::padding::ALIGN_RIGHT, - "rz" => fmt::padding::ZEROES, - }, - 's' => ctx.sep = opt.1, - 'w' => ctx.mod.width = match (strconv::stou(opt.1)) { - (strconv::invalid | strconv::overflow) => usage(help), - width: uint => switch (width > 0) { - false => usage(help), - true => width, - }, - }, - 'v' => startnum = match (strconv::stoi(opt.1)) { - (strconv::invalid | strconv::overflow) => usage(help), - startnum: int => startnum, - }, + case 'p' => + paged_numbering = false; + case 'b' => + body_style = switch (opt.1) { + case "a" => + yield style::ALL; + case "t" => + yield style::TXT; + case "n" => + yield style::NON; + case => + usage(help); + }; + case 'd' => + delim = switch (len(opt.1)) { + case 1 => + yield fmt::bsprintf(delim_buf, "{}:", opt.1); + case 2 => + yield opt.1; + case => + usage(help); + }; + case 'f' => + foot_style = switch (opt.1) { + case "a" => + yield style::ALL; + case "t" => + yield style::TXT; + case "n" => + yield style::NON; + case => + usage(help); + }; + case 'h' => + head_style = switch (opt.1) { + case "a" => + yield style::ALL; + case "t" => + yield style::TXT; + case "n" => + yield style::NON; + case => + usage(help); + }; + case 'i' => + ctx.incr = match (strconv::stoi(opt.1)) { + case (strconv::invalid | strconv::overflow) => + usage(help); + case incr: int => + yield incr; + }; + case 'l' => + ctx.maxblanks = match (strconv::stou(opt.1)) { + case (strconv::invalid | strconv::overflow) => + usage(help); + case maxblanks: uint => + yield if (maxblanks > 0) maxblanks else usage(help); + }; + case 'n' => + ctx.mod.padding = switch (opt.1) { + case "ln" => + yield fmt::padding::ALIGN_LEFT; + case "rn" => + yield fmt::padding::ALIGN_RIGHT; + case "rz" => + yield fmt::padding::ZEROES; + case => + usage(help); + }; + case 's' => + ctx.sep = opt.1; + case 'w' => + ctx.mod.width = match (strconv::stou(opt.1)) { + case (strconv::invalid | strconv::overflow) => + usage(help); + case width: uint => + yield if (width > 0) width else usage(help); + }; + case 'v' => + startnum = match (strconv::stoi(opt.1)) { + case (strconv::invalid | strconv::overflow) => + usage(help); + case startnum: int => + yield startnum; + }; }; }; @@ -136,18 +168,19 @@ export fn utilmain() (void | main::error) = { }; const use_file = len(cmd.args) == 1 && cmd.args[0] != "-"; - const input = switch (use_file) { - true => match (os::open(cmd.args[0])) { - err: fs::error => fmt::fatal("Error opening '{}': {}", - cmd.args[0], fs::strerror(err)), - file: io::file => { - static const rbuf: [os::BUFSIZ]u8 = [0...]; - static const wbuf: [os::BUFSIZ]u8 = [0...]; - yield bufio::buffered(&file, rbuf, wbuf); - }, - }, - false => os::stdin, - }; + const input = if (use_file) + match (os::open(cmd.args[0])) { + case err: fs::error => + fmt::fatal("Error opening '{}': {}", + cmd.args[0], fs::strerror(err)); + case file: io::file => + static const rbuf: [os::BUFSIZ]u8 = [0...]; + static const wbuf: [os::BUFSIZ]u8 = [0...]; + yield bufio::buffered(&file, rbuf, wbuf); + } + else + os::stdin; + defer io::close(input); static const delim_head_buf: [2 * 3]u8 = [0...]; @@ -161,9 +194,12 @@ export fn utilmain() (void | main::error) = { for (true) { const rawline = match (bufio::scanline(input)) { - err: io::error => return err, - io::EOF => break, - rawline: []u8 => rawline, + case err: io::error => + return err; + case io::EOF => + break; + case rawline: []u8 => + yield rawline; }; defer free(rawline); const line = fmt::bsprint(rawline); @@ -187,39 +223,40 @@ export fn utilmain() (void | main::error) = { }; switch (section) { - section::HEAD => println(line, head_style, &ctx), - section::BODY => println(line, body_style, &ctx), - section::FOOT => println(line, foot_style, &ctx), + case section::HEAD => + println(line, head_style, &ctx); + case section::BODY => + println(line, body_style, &ctx); + case section::FOOT => + println(line, foot_style, &ctx); }; }; }; fn println(line: str, style: style, ctx: *context) void = { switch (style) { - style::ALL => { - if (!isblank(line)) { + case style::ALL => + if (!isblank(line)) { + fmt::printf("{%}", ctx.linenum, ctx.mod)?; + fmt::print(ctx.sep)?; + ctx.linenum += ctx.incr; + ctx.conblanks = 0; + } else { + ctx.conblanks += 1; + if (ctx.conblanks == ctx.maxblanks) { fmt::printf("{%}", ctx.linenum, ctx.mod)?; fmt::print(ctx.sep)?; ctx.linenum += ctx.incr; ctx.conblanks = 0; - } else { - ctx.conblanks += 1; - if (ctx.conblanks == ctx.maxblanks) { - fmt::printf("{%}", ctx.linenum, ctx.mod)?; - fmt::print(ctx.sep)?; - ctx.linenum += ctx.incr; - ctx.conblanks = 0; - }; - }; - }, - style::TXT => { - if (!isblank(line)) { - fmt::printf("{%}", ctx.linenum, ctx.mod)?; - fmt::print(ctx.sep)?; - ctx.linenum += ctx.incr; }; - }, - style::NON => void, + }; + case style::TXT => + if (!isblank(line)) { + fmt::printf("{%}", ctx.linenum, ctx.mod)?; + fmt::print(ctx.sep)?; + ctx.linenum += ctx.incr; + }; + case style::NON => void; }; fmt::println(line)?; }; @@ -228,8 +265,10 @@ fn isblank(line: str) bool = { const iter = strings::iter(line); for (true) { const r = match (strings::next(&iter)) { - r: rune => r, - void => break, + case r: rune => + yield r; + case void => + break; }; if (!ascii::isspace(r)) { return false; diff --git a/sleep.ha b/sleep.ha @@ -17,8 +17,10 @@ export fn utilmain() (void | main::error) = { }; const seconds = match (strconv::stou(cmd.args[0])) { - (strconv::invalid | strconv::overflow) => usage(help), - s: uint => s: int, + case (strconv::invalid | strconv::overflow) => + usage(help); + case s: uint => + yield s: int; }; time::sleep(seconds * time::SECOND); diff --git a/tee.ha b/tee.ha @@ -18,9 +18,12 @@ export fn utilmain() (main::error | void) = { for (let i = 0z; i < len(cmd.opts); i += 1) { const opt = cmd.opts[i]; switch (opt.0) { - 'a' => flags |= fs::flags::APPEND, - 'i' => abort("TODO: Ignore SIGINT"), - * => abort(), // Invariant + case 'a' => + flags |= fs::flags::APPEND; + case 'i' => + abort("TODO: Ignore SIGINT"); + case => + abort(); // Invariant }; }; @@ -35,10 +38,11 @@ export fn utilmain() (main::error | void) = { for (let i = 0z; i < len(cmd.args); i += 1) { const file = match (os::create(cmd.args[i], 0o666, flags)) { - err: fs::error => fmt::fatal( - "Error opening '{}' for writing: {}", - cmd.args[i], fs::strerror(err)), - file: io::file => file, + case err: fs::error => + fmt::fatal("Error opening '{}' for writing: {}", + cmd.args[i], fs::strerror(err)); + case file: io::file => + yield file; }; source = &io::tee(source, &file); append(files, file); diff --git a/uname.ha b/uname.ha @@ -34,12 +34,18 @@ export fn utilmain() (void | main::error) = { for (let i = 0z; i < len(cmd.opts); i += 1) { const opt = cmd.opts[i]; switch (opt.0) { - 'a' => flags |= flags::ALL, - 'm' => flags |= flags::MACHINE, - 'n' => flags |= flags::NODE, - 'r' => flags |= flags::RELEASE, - 's' => flags |= flags::IMPLNAME, - 'v' => flags |= flags::VERSION, + case 'a' => + flags |= flags::ALL; + case 'm' => + flags |= flags::MACHINE; + case 'n' => + flags |= flags::NODE; + case 'r' => + flags |= flags::RELEASE; + case 's' => + flags |= flags::IMPLNAME; + case 'v' => + flags |= flags::VERSION; }; };