#!/usr/bin/perl -w
use strict;
use Carp;

# Sample code to extact intent from vexfile

use constant FINDSCHED => 0;

sub getintent ($) {
  my $vexfile = shift;

  my $mode=0;
  my $scan=undef;

  open(VEX, $vexfile) || croak "Could not open $vexfile: $!\n";

  my @scans;
  while (<VEX>) {
    if ($mode==FINDSCHED) {
      if (/\$SCHED\s*\;/) {
	$mode++;
      }
    } else {
      if (/scan\s+(\S+)\s*\;/) {
	$scan=$1;
	push @scans, [$scan];
      } elsif (/\*\s*intent\s*=\s*\"([^\"]*)\"/) {
	if (!defined $scan) {
	  carp "Intent \"$1\" outside scandef = ignoring\n";
	} else {
	  push @{$scans[$#scans]}, $1;
	}
      } elsif (/endscan\s*\;/) {
	$scan=undef;
      }
    }
  }
  close(VEX);
  return @scans;
}

my @scans = getintent($ARGV[0]);

my $size = 0;
foreach (@scans) {
  $size = length($_->[0]) if (length($_->[0])>$size);
}

my $format = sprintf("%%%ds", $size);

foreach (@scans) {
  printf("$format ", $_->[0]);

  foreach (@{$_}[1..$#$_]) {
    print " $_";
  }
  print "\n";
}
