1*a7c91847Schristos#!/bin/sh 2*a7c91847Schristos 3*a7c91847Schristos# Copyright (C) 2000-2005 The Free Software Foundation, Inc. 4*a7c91847Schristos 5*a7c91847Schristos# This program is free software; you can redistribute it and/or modify 6*a7c91847Schristos# it under the terms of the GNU General Public License as published by 7*a7c91847Schristos# the Free Software Foundation; either version 2, or (at your option) 8*a7c91847Schristos# any later version. 9*a7c91847Schristos# 10*a7c91847Schristos# This program is distributed in the hope that it will be useful, 11*a7c91847Schristos# but WITHOUT ANY WARRANTY; without even the implied warranty of 12*a7c91847Schristos# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13*a7c91847Schristos# GNU General Public License for more details. 14*a7c91847Schristos 15*a7c91847Schristos# 16*a7c91847Schristos# This program is intended to take a check.log file generated by a failed run of 17*a7c91847Schristos# sanity.sh as input and run expr line by line on it. It seems a much easier 18*a7c91847Schristos# way of spotting a single failed line in a 100 line test result. 19*a7c91847Schristos# 20*a7c91847Schristos 21*a7c91847Schristos# 22*a7c91847Schristos# Contributed by Derek R. Price <derek.price@openavenue.com> 23*a7c91847Schristos# 24*a7c91847Schristos 25*a7c91847Schristos 26*a7c91847Schristos 27*a7c91847Schristosusage () 28*a7c91847Schristos{ 29*a7c91847Schristos echo "\ 30*a7c91847Schristosusage: $0 [-afh] [file...] 31*a7c91847Schristos 32*a7c91847Schristos -a process alternate pattern 33*a7c91847Schristos -f process first pattern (default) 34*a7c91847Schristos -h print this text 35*a7c91847Schristos 36*a7c91847Schristos file files to process (default = check.log)" 37*a7c91847Schristos} 38*a7c91847Schristos 39*a7c91847Schristos# Do a line by line match with expr 40*a7c91847Schristos# 41*a7c91847Schristos# INPUTS 42*a7c91847Schristos# $1 = text file name 43*a7c91847Schristos# $2 = pattern file name 44*a7c91847Schristosexpr_line_by_line () 45*a7c91847Schristos{ 46*a7c91847Schristos dcl_line=0 47*a7c91847Schristos dcl_wrong= 48*a7c91847Schristos # We are assuming a newline at the end of the file. The way sanity.sh 49*a7c91847Schristos # uses echo to create the log message guarantees this newline and since 50*a7c91847Schristos # expr ignores the last newline when the anchor is present anyhow, no 51*a7c91847Schristos # information is being lost in the transition 52*a7c91847Schristos while test $dcl_line -lt `wc -l <$1` -a $dcl_line -lt `wc -l <$2`; do 53*a7c91847Schristos dcl_line=`expr $dcl_line + 1` 54*a7c91847Schristos if test `sed -ne${dcl_line}p <$1 |wc -c` -eq 1 \ 55*a7c91847Schristos -a `sed -ne${dcl_line}p <$2 |wc -c` -eq 1; then 56*a7c91847Schristos # This is a workaround for what I am calling a bug in GNU 57*a7c91847Schristos # expr - it won't match the empty string to the empty 58*a7c91847Schristos # string. In this case the assumption is that a single 59*a7c91847Schristos # character is always a newline. Since we already checked 60*a7c91847Schristos # for the end of the file, we know sed will echo the 61*a7c91847Schristos # newline. 62*a7c91847Schristos : 63*a7c91847Schristos elif expr "`sed -ne${dcl_line}p <$1`" : \ 64*a7c91847Schristos "`sed -ne${dcl_line}p <$2`\$" >/dev/null; then 65*a7c91847Schristos : 66*a7c91847Schristos else 67*a7c91847Schristos echo "$dcl_line: `sed -ne${dcl_line}p <$1`" 68*a7c91847Schristos echo "$dcl_line: `sed -ne${dcl_line}p <$2`\$" 69*a7c91847Schristos dcl_wrong="$dcl_wrong $dcl_line" 70*a7c91847Schristos fi 71*a7c91847Schristos done 72*a7c91847Schristos if test `wc -l <$1` -ne `wc -l <$2`; then 73*a7c91847Schristos echo "output & pattern contain differing number of lines" 74*a7c91847Schristos elif test -z "$dcl_wrong"; then 75*a7c91847Schristos echo "no mismatched lines" 76*a7c91847Schristos else 77*a7c91847Schristos echo "mismatched lines: $dcl_wrong" 78*a7c91847Schristos fi 79*a7c91847Schristos} 80*a7c91847Schristos 81*a7c91847Schristos# Process a single check.log file 82*a7c91847Schristos# 83*a7c91847Schristos# INPUTS 84*a7c91847Schristos# $1 = filename 85*a7c91847Schristosprocess_check_log () 86*a7c91847Schristos{ 87*a7c91847Schristos # abort if we can't find any expressions 88*a7c91847Schristos if grep '^\*\* got: $' <$1 >/dev/null; then 89*a7c91847Schristos : 90*a7c91847Schristos else 91*a7c91847Schristos echo "WARNING: No expressions in file: $1" >&2 92*a7c91847Schristos echo " Either not a check.log or sanity.sh exited for some other reason," >&2 93*a7c91847Schristos echo " like bad exit status. Try tail." >&2 94*a7c91847Schristos return 95*a7c91847Schristos fi 96*a7c91847Schristos 97*a7c91847Schristos dcl_exprfiles="" 98*a7c91847Schristos if grep '^\*\* or: $' <$1 >/dev/null; then 99*a7c91847Schristos # file contains a second regex 100*a7c91847Schristos if test $dcl_dofirst -eq 1; then 101*a7c91847Schristos # get the first pattern 102*a7c91847Schristos sed -ne '/^\*\* expected: $/,/^\*\* or: $/p' <$1 >/tmp/dcle$$ 103*a7c91847Schristos dcl_exprfiles="$dcl_exprfiles /tmp/dcle$$" 104*a7c91847Schristos fi 105*a7c91847Schristos if test $dcl_doalternate -eq 1; then 106*a7c91847Schristos # get the alternate pattern 107*a7c91847Schristos sed -ne '/^\*\* or: $/,/^\*\* got: $/p' <$1 >/tmp/dclo$$ 108*a7c91847Schristos dcl_exprfiles="$dcl_exprfiles /tmp/dclo$$" 109*a7c91847Schristos else 110*a7c91847Schristos echo "WARNING: Ignoring alternate pattern in file: $1" >&2 111*a7c91847Schristos fi 112*a7c91847Schristos else 113*a7c91847Schristos # file doesn't contain a second regex 114*a7c91847Schristos if test $dcl_dofirst = 1; then 115*a7c91847Schristos # get the only pattern 116*a7c91847Schristos sed -ne '/^\*\* expected: $/,/^\*\* got: $/p' <$1 >/tmp/dcle$$ 117*a7c91847Schristos dcl_exprfiles="$dcl_exprfiles /tmp/dcle$$" 118*a7c91847Schristos fi 119*a7c91847Schristos if test $dcl_doalternate -eq 1; then 120*a7c91847Schristos echo "WARNING: No alternate pattern in file: $1" >&2 121*a7c91847Schristos fi 122*a7c91847Schristos fi 123*a7c91847Schristos 124*a7c91847Schristos # and get the actual output 125*a7c91847Schristos sed -ne '/^\*\* got: $/,$p' <$1 >/tmp/dclg$$ 126*a7c91847Schristos sed -ne '1D 127*a7c91847Schristos$D 128*a7c91847Schristosp' </tmp/dclg$$ >/tmp/dclh$$ 129*a7c91847Schristos mv /tmp/dclh$$ /tmp/dclg$$ 130*a7c91847Schristos 131*a7c91847Schristos # compare the output against each pattern requested 132*a7c91847Schristos for dcl_f in $dcl_exprfiles; do 133*a7c91847Schristos sed -ne '1D 134*a7c91847Schristos$D 135*a7c91847Schristosp' <$dcl_f >/tmp/dclp$$ 136*a7c91847Schristos mv /tmp/dclp$$ $dcl_f 137*a7c91847Schristos 138*a7c91847Schristos case $dcl_f in 139*a7c91847Schristos /tmp/dcle*) 140*a7c91847Schristos echo "********** $1 : Primary **********" 141*a7c91847Schristos ;; 142*a7c91847Schristos /tmp/dclo*) 143*a7c91847Schristos echo "********** $1 : Alternate **********" 144*a7c91847Schristos ;; 145*a7c91847Schristos esac 146*a7c91847Schristos 147*a7c91847Schristos expr_line_by_line /tmp/dclg$$ $dcl_f 148*a7c91847Schristos 149*a7c91847Schristos rm $dcl_f 150*a7c91847Schristos done 151*a7c91847Schristos 152*a7c91847Schristos rm /tmp/dclg$$ 153*a7c91847Schristos} 154*a7c91847Schristos 155*a7c91847Schristos### 156*a7c91847Schristos### MAIN 157*a7c91847Schristos### 158*a7c91847Schristos 159*a7c91847Schristos# set up defaults 160*a7c91847Schristosdcl_doalternate=0 161*a7c91847Schristosdcl_dofirst=0 162*a7c91847Schristos 163*a7c91847Schristos# process options 164*a7c91847Schristoswhile getopts afh arg; do 165*a7c91847Schristos case $arg in 166*a7c91847Schristos a) 167*a7c91847Schristos dcl_doalternate=1 168*a7c91847Schristos ;; 169*a7c91847Schristos f) 170*a7c91847Schristos dcl_dofirst=1 171*a7c91847Schristos ;; 172*a7c91847Schristos \?|h) 173*a7c91847Schristos usage 174*a7c91847Schristos exit 1 175*a7c91847Schristos ;; 176*a7c91847Schristos esac 177*a7c91847Schristosdone 178*a7c91847Schristos 179*a7c91847Schristos# dispose of processed args 180*a7c91847Schristosshift `expr $OPTIND - 1` 181*a7c91847SchristosOPTIND=1 182*a7c91847Schristos 183*a7c91847Schristos# set the default mode 184*a7c91847Schristosif test $dcl_doalternate -eq 0; then 185*a7c91847Schristos dcl_dofirst=1 186*a7c91847Schristosfi 187*a7c91847Schristos 188*a7c91847Schristos# set default arg 189*a7c91847Schristosif test $# -eq 0; then 190*a7c91847Schristos if test -f src/check.log && test -r src/check.log; then 191*a7c91847Schristos set src/check.log 192*a7c91847Schristos else 193*a7c91847Schristos set check.log 194*a7c91847Schristos fi 195*a7c91847Schristosfi 196*a7c91847Schristos 197*a7c91847Schristosfor file in "$@"; do 198*a7c91847Schristos process_check_log $file; 199*a7c91847Schristosdone 200*a7c91847Schristos 201*a7c91847Schristosexit 0 202