#!/usr/bin/perl
use strict;
use Getopt::Std;

my %opts;
getopts 'aAbeEnstTuv', \%opts;  # 'u' is ignored

my $showline = $opts{b} || $opts{n};
my $reline = $opts{b}? qr/^(?=.)/m: qr/^/m;
my $quotectrl = $opts{v} || $opts{e} || $opts{t} || $opts{A};
my $quotetab = $opts{t} || $opts{T} || $opts{A};
my $showeol = $opts{e} || $opts{E} || $opts{A};
my $singlespace = $opts{s};

undef $/;

while (<>) {
    s/\n\n+/\n\n/g if $singlespace;
    if ($showline) {
        my $n = 0;
        s/$reline/sprintf '%6d ', ++$n/ge;
    }
    s/$/\$/mg if $showeol;
    s/\t/^I/g if $quotetab;
    # skip \11 (tab) and \12 (newline)
    s/[\0-\10\13-\37\177]/sprintf '^%c', (ord($&)+64)&0177/ge if $quotectrl;
    print;
}
