hautils

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

commit 61d21ca3316cfb7902f0572de6aa93b083426b99
parent cf3272fc2d1559247b1777b9b780a120285f6c0f
Author: Drew DeVault <sir@cmpwn.com>
Date:   Thu,  8 Jul 2021 14:47:47 -0400

uname: new command

Diffstat:
M.gitignore | 1+
MMakefile | 4+++-
Auname.ha | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 61 insertions(+), 1 deletion(-)

diff --git a/.gitignore b/.gitignore @@ -2,3 +2,4 @@ cat false true tee +uname diff --git a/Makefile b/Makefile @@ -6,7 +6,8 @@ utils=\ cat \ false \ tee \ - true + true \ + uname all: $(utils) @@ -23,3 +24,4 @@ cat: cat.ha main/main.ha false: false.ha tee: tee.ha true: true.ha +uname: uname.ha diff --git a/uname.ha b/uname.ha @@ -0,0 +1,57 @@ +use getopt; +use main; +use os; +use fmt; + +type flags = enum uint { + MACHINE = 1 << 0, + NODE = 1 << 1, + RELEASE = 1 << 2, + IMPLNAME = 1 << 3, + VERSION = 1 << 4, + ALL = MACHINE | NODE | RELEASE | IMPLNAME | VERSION, +}; + +export fn utilmain() (void | main::error) = { + const help: []getopt::help = [ + "print system information", + ('a', "behave as if -mnrsv were specified"), + ('m', "write the name of the hardware type to standard out"), + ('n', "write the name of this node to standard out"), + ('r', "write the release level of the operating system to standard out"), + ('s', "write the name of the implementation to standard out"), + ('v', "write the current version of this release to standard out"), + ]; + const cmd = getopt::parse(os::args, help...); + defer getopt::finish(&cmd); + + if (len(cmd.args) != 0) { + getopt::printusage(os::stderr, os::args[0], help); + os::exit(1); + }; + + let flags: flags = if (len(cmd.opts) == 0) flags::IMPLNAME else 0; + 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, + }; + }; + + let items: []fmt::formattable = []; + defer free(items); + + if (flags & flags::MACHINE != 0) append(items, os::machine()); + if (flags & flags::NODE != 0) append(items, os::hostname()); + if (flags & flags::RELEASE != 0) append(items, os::release()); + if (flags & flags::IMPLNAME != 0) append(items, os::sysname()); + if (flags & flags::VERSION != 0) append(items, os::version()); + + fmt::println(items...)?; + return; +};