#!/usr/bin/perl -Tw use strict; $|++; use CGI qw(:all); use HTML::Table; my $sortcol = param('sortcol'); unless (defined $sortcol and $sortcol =~ /\A-?\d+\z/) { $sortcol = 0; } print header, start_html("Processes"); $ENV{PATH} = '/bin:/usr/bin:/usr/local/bin'; my @result = map { [map /\s*(\S.*\S|\S?)/, unpack "A8 x1 A5 x1 A4 x1 A4 x1 A5 x1 A5 x1 A3 x1 A4 x0 A6 x1 A6 x1 A*", $_] } `ps uaxww`; ## first generate the table by generating the header... my $table = HTML::Table->new; for ((my @headers = @{shift @result}), (my $col = 0); $col <= $#headers; $col++) { my $tag = $col + 1; my $icon = ""; if ($tag == $sortcol) { $icon = img({src => "/icons/down.gif"}); $tag = -$tag; } elsif (-$tag == $sortcol) { $icon = img({src => "/icons/up.gif"}); $tag = -$tag; } $table->addCol($icon . a({href => script_name()."?sortcol=$tag"}, escapeHTML($headers[$col]))); } $table->setRowHead(1); ## at this point, we need to sort the data based on $sortcol, which is 1-based if ($sortcol) { my $alpha = 0; my $direction = $sortcol <=> 0; # -1 or +1 my $column = abs($sortcol) - 1; # 0-based now ## detect the need for an alpha sort, if column contains non-numeric data for (@result) { $alpha = 1, last if $_->[$column] =~ /[^\-\d.]/; } if ($alpha) { @result = sort { $direction * ($a->[$column] cmp $b->[$column])} @result; } else { @result = sort { $direction * ($a->[$column] <=> $b->[$column])} @result; } } ## and finally add the sorted data to the table, checking for spanning... my @previous; my @previous_row_number; for (@result) { my @this_row = @$_; my $this_row_number = $table->addRow(map escapeHTML($_), @this_row); # undocumented return value for my $col (0..$#this_row) { if (defined $previous[$col] and $previous[$col] eq $this_row[$col]) { ## we have a span my $previous_row_number = $previous_row_number[$col]; $table->setCellRowSpan($previous_row_number, 1 + $col, $this_row_number - $previous_row_number + 1); } else { $previous[$col] = $this_row[$col]; $previous_row_number[$col] = $this_row_number; } } } $table->setBorder(2); $table->setCellSpacing(0); $table->setCellPadding(2); print "$table"; print end_html;