On Sat, Feb 22, 2020 at 10:21:56PM +0000, Stuart Henderson wrote:
> On 2020/02/22 17:24, Stefan Sperling wrote:
> > On Sat, Feb 22, 2020 at 02:56:54PM +0100, Mark Kettenis wrote:
> > > IMHO it is a bad idea to make the output of ifconfig locale-dependent.
> >
> > Fine. I'll drop this diff.
> >
>
> Pity, it is quite useful if you are somewhere that uses UTF-8 SSIDs,
> otherwise it's a complete pain to decode to figure out if you're
> connecting to the correct network. An alternative would be to filter
> ifconfig output through something like this
>
> while(<>) {
> if ($_ =~ m/nwid 0x([0-9a-f]+)\s/) {
> my $hex = $1;
> my $decoded = pack "H*", $hex;
> $_ =~ s/$hex/$hex ("$decoded")/;
> }
> print $_;
> }
>
> but it won't validate the characters so best redirect output to a file
> and open it in a UTF-8 editor to view it rather than displaying it
> directly on a terminal.
This removes any characters that are not marked as "printable", which I
expect is what folks want. Still doesn't always help since I still
haven't figured out font fallback in xterm so some chars don't exist in
my current font.
ifconfig wlan scan | perl -C -MEncode=decode -pE 's{\bnwid 0x(\p{AHex}+)\K\b}{
my $n = decode("UTF-8", pack "H*", $1) =~ s/\P{Print}//gr; $n ? qq{ ("$n")} :
"" }e'
Or, the more verbose:
$ cat ~/bin/wlanscan
#!/usr/bin/perl
# perldoc perlunicook
use utf8; # so literals and identifiers can be in UTF-8
use v5.12; # or later to get "unicode_strings" feature
use strict; # quote strings, declare variables
use warnings; # on by default
use warnings qw(FATAL utf8); # fatalize encoding glitches
use open qw(:std :encoding(UTF-8)); # undeclared streams in UTF-8
use Encode qw< decode >;
my $if = shift || 'wlan';
open my $fh, '-|', 'ifconfig', $if, 'scan' or die "Unable to exec ifconfig: $!";
while ( my $line = readline $fh ) {
$line =~ s{\b nwid \s+ 0x(\p{PosixXDigit}+)\K \b}{ decode_nwid($1) }ex;
print $line;
}
close $fh or die "Error closing ifconfig: $!";
sub decode_nwid {
my $nwid = decode 'UTF-8', pack "H*", $_[0];
$nwid =~ s/\P{Print}//g;
return $nwid ? qq{ ("$nwid")} : '';
}