Filter LaTeX output for errors.

Pipe the output of LaTeX through a Perl script and filter out information that
is irrelevant in deciding if building the manual actually worked. Format errors
in a similar way to gcc output to allow existing scripts catching it.

Enabling verbose output during the make run will not remove parts of the output
but only do some reflowing. The full log is always available in the manual
subfolder.

Change-Id: I15d35b4d3c73fafe2a4357168ca8ada51355f221
Reviewed-on: http://gerrit.rockbox.org/247
Reviewed-by: Dominik Riebeling <Dominik.Riebeling@gmail.com>
Tested-by: Dominik Riebeling <Dominik.Riebeling@gmail.com>
diff --git a/manual/LaTeX.Rules b/manual/LaTeX.Rules
index 5a1e585..cfe43b0 100644
--- a/manual/LaTeX.Rules
+++ b/manual/LaTeX.Rules
@@ -119,6 +119,14 @@
 INDEX_ARGS := -s mkidx.ist
 LATEXOPTS := -interaction=nonstopmode
 
+# program to pipe stdout through. Note: this needs to start with a pipe symbol
+# to not make the command fail if no filter is defined.
+ifndef V
+LATEXFILTER := | ./latexfilter.pl
+else
+LATEXFILTER := | ./latexfilter.pl -v
+endif
+
 # grab the contents of \bibliograph{} commands
 ifeq ($(BIB_FILES),)
 	BIB_FILES := $(shell for source in $(TEX_FILES) ; do sed -e '{' -e 'y/,/ /' -e 's?.*\\bibliography[{]\(.*\)[}].*?\1?' -e 't' -e 'd' -e '}' <$$source ; done)
@@ -222,7 +230,7 @@
 	makeobsolete() { touch -r $$(ls *.old | tail -n 1) $${1} ; true ; } ; \
 	nochange() { for file ; do [ ! -f $${1} ] || cmp $${1} $${1}.old >/dev/null || return ; done ; true ; } ; \
 	saveold $(MONITOR_FILES) ; \
-	if $(LATEX) $(LATEXOPTS) $* ; then \
+	if $(LATEX) $(LATEXOPTS) $* $(LATEXFILTER) ; then \
 	   if nochange $(MONITOR_FILES) ; then \
 	      echo "$(MAKE): LaTeX auxiliary files did not change (processing is complete)" ; \
 	      restoreold $(MONITOR_FILES) ; \
@@ -267,7 +275,7 @@
 #
 
 %.aux %.idx : $(XFIG_TEX)
-	$(LATEX) $(LATEXOPTS) $*
+	$(LATEX) $(LATEXOPTS) $* $(LATEXFILTER)
 
 #
 # Distill xfig .fig files into .fig.tex and either .fig.pdf or .fig.ps
diff --git a/manual/latexfilter.pl b/manual/latexfilter.pl
new file mode 100755
index 0000000..45d7ced
--- /dev/null
+++ b/manual/latexfilter.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/perl -s
+#
+#             __________               __   ___.
+#   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+#   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+#   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+#   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+#                     \/            \/     \/    \/            \/
+#
+
+my $verbose = $v;
+my $reflowed = "";
+my $last = "";
+my $currentfile;
+while(<STDIN>) {
+    chomp $_;
+    $reflowed .= $_;
+    if(/^.{79,}$/) {
+        # collapse all "full" lines
+    }
+    elsif(/^!/) {
+        # collapse lines indicating an error with next one and append a space.
+        $reflowed .= " ";
+    }
+    else {
+        # skip empty lines
+        if(length($reflowed) > 0) {
+            # collapse with previous line if it continues some "area".
+            if($reflowed =~ /^\s*(\]|\[|\\|\)|<)/) {
+                $last .= $reflowed;
+            }
+            else {
+                # find current input file
+                my @inputfile = $last =~ /\(([a-zA-Z_\-\/\.]+\.tex)/g;
+                foreach(@inputfile) {
+                    if($verbose) {
+                        print "\n";
+                    }
+                    print "LaTeX processing $_\n";
+                    $currentfile = $_;
+                }
+                if($verbose) {
+                    print $last;
+                }
+                # check for error
+                if($reflowed =~ /^!\s*(.*)/) {
+                    my $l = $reflowed;
+                    $l =~ s/^!\s*(.*)l.(\d+) /$2: $1/;
+                    print "$currentfile:$l\n";
+                }
+                # restart for next reflowed line
+                $last = $reflowed;
+            }
+        }
+        # restart reflowing.
+        $reflowed = "";
+    }
+}
+if($verbose) {
+    print $last;
+}
+