uname.ha (1705B)
1 use getopt; 2 use main; 3 use os; 4 use fmt; 5 6 type flags = enum uint { 7 MACHINE = 1 << 0, 8 NODE = 1 << 1, 9 RELEASE = 1 << 2, 10 IMPLNAME = 1 << 3, 11 VERSION = 1 << 4, 12 ALL = MACHINE | NODE | RELEASE | IMPLNAME | VERSION, 13 }; 14 15 export fn utilmain() (void | main::error) = { 16 const help: []getopt::help = [ 17 "print system information", 18 ('a', "behave as if -mnrsv were specified"), 19 ('m', "write the name of the hardware type to standard out"), 20 ('n', "write the name of this node to standard out"), 21 ('r', "write the release level of the operating system to standard out"), 22 ('s', "write the name of the implementation to standard out"), 23 ('v', "write the current version of this release to standard out"), 24 ]; 25 const cmd = getopt::parse(os::args, help...); 26 defer getopt::finish(&cmd); 27 28 if (len(cmd.args) != 0) { 29 main::usage(help); 30 }; 31 32 let flags: flags = if (len(cmd.opts) == 0) flags::IMPLNAME else 0; 33 for (let i = 0z; i < len(cmd.opts); i += 1) { 34 const opt = cmd.opts[i]; 35 switch (opt.0) { 36 case 'a' => 37 flags |= flags::ALL; 38 case 'm' => 39 flags |= flags::MACHINE; 40 case 'n' => 41 flags |= flags::NODE; 42 case 'r' => 43 flags |= flags::RELEASE; 44 case 's' => 45 flags |= flags::IMPLNAME; 46 case 'v' => 47 flags |= flags::VERSION; 48 case => 49 main::usage(help); 50 }; 51 }; 52 53 let items: []fmt::formattable = []; 54 defer free(items); 55 56 if (flags & flags::MACHINE != 0) append(items, os::machine()); 57 if (flags & flags::NODE != 0) append(items, os::hostname()); 58 if (flags & flags::RELEASE != 0) append(items, os::release()); 59 if (flags & flags::IMPLNAME != 0) append(items, os::sysname()); 60 if (flags & flags::VERSION != 0) append(items, os::version()); 61 62 fmt::println(items...)?; 63 return; 64 };