#!/usr/bin/perl -w
#
# $Id: zk_check_make,v 1.1 2000/03/05 17:16:55 luc Exp $
#
#	Luc Boulianne <luc@zks.net>
#
# Script to parse the compiler output and extract errors
#
# Usage:
#	zk_check_make make-output
#
# or
#
#	make |& zk_check_make	
#
# Luc Boulianne <luc@zks.net>
#

use strict;
my(@Make);
my(@Lines);
my($errcond) = 0;  # Error Condition
my($ZK)="";

sub print_stack {
  print "+---------------------------------------------------------------------\n";
  print "| $ZK\n";
  print "| ", join("\n| ",@Make), "\n";
}

sub print_lines {
  print "| ", join("\n| ",@Lines), "\n";
  @Lines=();
}

while (<>) {
  chomp();
  if (/^\[ZK Build\]/) {
	print $_, "\n";
  	next;
  }

  if (! $errcond) {
    if (/^\[ZK/) {
      $ZK="$_";
      @Make=();
      @Lines=();
    } elsif (/^(g|)make.*: Entering/) {
      push(@Make, $_);
      @Lines=();
    } elsif (/^(g|)make.*: Leaving/) {
      pop(@Make);
    } elsif (/^(g|)make.*: \*\*\*/) {
      $errcond = 1;
      print_stack;
      push (@Lines, $_);
      print_lines;
    } elsif (!/^(gcc|cc|insure|purify)/ ) {
      push (@Lines, $_);
    }
  } elsif ($errcond) {
    if (/^\[ZK/) {
      $ZK="$_";
      @Make=();
      @Lines=();
    } elsif (/^(g|)make.*: Entering/) {
      push(@Make, $_);
      @Lines=();
      $errcond = 0;
      print "+---------------------------------------------------------------------\n\n";
    } elsif (/^(g|)make.*: Leaving/) {
      print "| ", $_, "\n";
      pop(@Make);
      if ($#Make == 0) {
	print "+----------------------------------------------------------------------\n\n";
	$errcond = 0;
      }
    } elsif (/^(g|)make.*: \*\*\*/) {
      push (@Lines, $_);
      print_lines;
    } elsif (!/^(gcc|cc|insure|purify)/ ) {
      push (@Lines, $_);
    }
  }
}

if ($errcond) {
  print "+----------------------------------------------------------------------\n\n";
  exit 2;
}

exit 0;


