#! /bin/sh

# This filter should be applied to *every* stderr result.  It removes
# Valgrind startup stuff and pid numbers.
#
# Nb: The GNU and BSD implementations of 'sed' are quite different, so
# anything remotely complicated (e.g. "\(a\|b\)" alternatives) can't be
# easily done.  Use Perl instead for any such cases.

dir=`dirname $0`

# Remove ==pid== and --pid-- and ++pid++ and **pid** strings 
perl -p -e 's/(==|--|\+\+|\*\*)[0-9]{1,7}\1 //' |

# Remove any --pid:0: strings (debuglog level zero output)
sed "/^--[0-9]\{1,7\}:0:*/d" |

# Remove "WARNING: assuming toc 0x.." strings
sed "/^WARNING: assuming toc 0x*/d" |

# Remove "Using valgrind..." line and the following
# copyright notice line.  Tools have to filter their own line themselves.
sed "/^Using valgrind-.*, a dynamic binary instrumentation framework/ , /./ d" |

# ... and also remove the "Using LibVEX..." line and the one after it.
sed "/^Using LibVEX rev .*, a library for dynamic binary translation/ , /./ d" |

# Remove other introductory lines
sed "/Estimated CPU clock rate is [0-9]* MHz/d" |
sed "/For more details, rerun with: -v/d"       |

# Anonymise line numbers in vg_replace_malloc.c, remove dirname if present
perl -p -e "s/(m_replacemalloc\/)?vg_replace_malloc.c:[0-9]*/vg_replace_malloc.c:.../" |

# Hide suppressed error counts
sed "s/^\(ERROR SUMMARY[^(]*(suppressed: \)[0-9]*\( from \)[0-9]*)$/\10\20)/" |

# Reduce some libc incompatibility
$dir/filter_libc |

# Remove line info out of order warnings
sed "/warning: line info addresses out of order/d" |

# Older bash versions print abnormal termination messages on the stderr
# of the bash process. Newer bash versions redirect such messages properly.
# Suppress any redirected abnormal termination messages. You can find the
# complete list of messages in the bash source file siglist.c.
perl -n -e 'print if !/^(Segmentation fault|Alarm clock|Aborted|Bus error)( \(core dumped\))?$/' |

# Remove any ": dumping core" message as the user might have a
# limit set that prevents the core dump
sed "s/\(signal [0-9]* (SIG[A-Z]*)\): dumping core/\1/" |

# Remove the size in "The main thread stack size..." message.
sed "s/The main thread stack size used in this run was [0-9]*/The main thread stack size used in this run was .../"

