From bb9c57170922780f241716aa90327874e3efdf31 Mon Sep 17 00:00:00 2001 From: Steven McDonald Date: Sat, 8 Nov 2014 20:39:24 +1100 Subject: [PATCH] Add initial devtree implementation --- COPYING | 13 ++++++++++ devtree/devtree | 64 +++++++++++++++++++++++++++++++++++++++++++++++ devtree/devtree.1 | 36 ++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 COPYING create mode 100755 devtree/devtree create mode 100644 devtree/devtree.1 diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..5c93f45 --- /dev/null +++ b/COPYING @@ -0,0 +1,13 @@ + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2004 + + Copyright (C) 2004 Sam Hocevar + + Everyone is permitted to copy and distribute verbatim or modified + copies of this license document, and changing it is allowed as long + as the name is changed. + + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO. diff --git a/devtree/devtree b/devtree/devtree new file mode 100755 index 0000000..adadaae --- /dev/null +++ b/devtree/devtree @@ -0,0 +1,64 @@ +#!/usr/bin/perl + +# Copyright (C) 2014 Steven McDonald +# +# This program is free software. It comes without any warranty, to the +# extent permitted by applicable law. You can redistribute it and/or +# modify it under the terms of the Do What The Fuck You Want To Public +# License, Version 2, as published by Sam Hocevar. See the COPYING file +# or visit http://www.wtfpl.net/ for more details. + +use strict; +use warnings; + +sub print_tree; + +my $tree = {}; +my %name_to_node = (root => $tree); +my $show_detached = 0; + +for (@ARGV) { + $show_detached = 1 if /^(-d|--show-detached)$/; +} + +while () { + if (/^(\w+) at (\w+):?\s(.*?["(<])?([^"()<>\n]+)?/) { + my $node_prettyname; + if ($4) { + $node_prettyname = "$1 ($4)"; + } else { + $node_prettyname = $1; + } + + my $new_node = {}; + $name_to_node{$2}{$node_prettyname} = $new_node; + $name_to_node{$1} = $new_node; + } elsif (/^(\w+) detached$/ and my $node = $name_to_node{$1}) { + $node->{_is_detached} = 1; + } +} + +print "root\n"; +print_tree $tree; +exit; + +sub print_tree { + my $tree = shift; + my $prev_indent = shift || ""; + my $indent = $prev_indent . " |"; + my $count = 0; + my @keys = keys $tree; + + for (sort @keys) { + my $name = $_; + if (delete $tree->{$_}->{_is_detached}) { + next unless $show_detached; + $name = "$name [DETACHED]"; + } + + print $indent . "-$name\n"; + $indent = $prev_indent . " " if $count eq $#keys; + print_tree $tree->{$_}, $indent; + $count++; + } +} diff --git a/devtree/devtree.1 b/devtree/devtree.1 new file mode 100644 index 0000000..122659b --- /dev/null +++ b/devtree/devtree.1 @@ -0,0 +1,36 @@ +.\" Copyright (C) 2014 Steven McDonald +.\" +.\" This work is free. You can redistribute it and/or modify it under +.\" the terms of the Do What The Fuck You Want To Public License, +.\" Version 2, as published by Sam Hocevar. See the COPYING file or +.\" visit http://www.wtfpl.net/ for more details. +.\" +.Dd $Mdocdate$ +.Dt DEVTREE 1 +.Os +.Sh NAME +.Nm devtree +.Nd display a tree representing system hardware +.Sh SYNOPSIS +.Nm devtree +.Op Fl d +.Sh DESCRIPTION +The +.Nm +utility accepts output from +.Xr dmesg 8 +on standard input, and produces a human-friendly ASCII tree +representing the currently attached system devices on standard output. +.Pp +If the +.Sy -d +option is provided, it will also include devices which have +been detached from the system, appending +.Em [DETACHED] +to any such entries. All other options are ignored. +.Sh EXAMPLES +Display a tree of all attached devices on the current system: +.Pp +.Dl $ dmesg | devtree +.Sh SEE ALSO +.Xr dmesg 8 -- 2.30.2