An Ancient Perl Input Filter

[Linux]

#!/usr/bin/perl

# Add the text
$first = 1;
$raw = 0;
while ()
{
  if ($first)
  {
    if (/\@PJL/ || /^%%STX/)
    {
      s/^\x04//;
      $raw = 1;
    }
    else
    {
      &init;
    }
    $first = 0;
  }

  unless ($raw)
  {
    # Check for tags
    &check_tags;

    # Manipulate the line depending on flags
    &modify_line;
  }

  # Print what's left of the line
  print;
}

unless ($raw)
{
  # Reset the printer
  print "\x1bE";
}

sub check_tags
{
  # Check for line numbering
  $lineno = 1 if (s///i);
  $lineno = 0 if (s/<\/numberline>//i);

  # Check for orientation
  $_ = "\x1b&l1O" if //i;
  $_ = "\x1b&l0O" if //i;

  # Check for new page
  $_ = "\x1b&l0H" if //i;

  # Check for print enhancement
  s//\x1b(s1S/ig;
  s/<\/italic>/\x1b(s0S/ig;

  s//\x1b(s3B/ig;
  s/<\/bold>/\x1b(s0B/ig;

  s//\x1b&d0D/ig;
  s/<\/underline>/\x1b&d@/ig;

  while (s//\x1b(s\1H/i) {}

  # Check for margins and paper size
  if (//i)
  {
    $left = $1;
    s//\x1b&a\1L/i;
  }
  if (//i)
  {
    $right = 100 - $1 - 1;
    $temp = "\x1b&a" . $right . "M";
    s//$temp/i;
  }
  if (//i)
  {
    $top = $1;
    s//\x1b&l\1E/i;
  }
  if (//i)
  {
    $bottom = 70 - $top - $1;
    $temp = "\x1b&l" . $bottom . "L";
    s//$temp/i;
  }

}

sub modify_line
{
  # Add line numbers
  if ($lineno == 1)
  {
    $_ = sprintf ("%4d: %s", $., $_);
  }


}

################################################
# Initialise the printer
################################################

sub init
{
  # Reset the printer
  print "\x1bE";

  # Avoid the step effect
  print "\x1b&k2G";

  # Set top margin
  print "\x1b&l4E";
  $top = 4;

  # Set left margin
  print "\x1b&a6L";
  $left = 6;

  # Set right margin
  print "\x1b&a93M";
  $right = 6;

  # Set text length
  print "\x1b&l62L";
  $bottom = 4;

  # Set to Elite pitch
  print "\x1b&k4S";
}