From: walt@mathcs.emory.edu (Walt Hultgren {rmy})
Newsgroups: comp.databases.informix
Subject: Updated sh/awk script to scan .err files
Date: 7 Jan 1993 09:41:13 -0500
Organization: Emory University
Distribution: world
NNTP-Posting-Host: emory.mathcs.emory.edu

This is an updated copy of the sh/awk script I posted in July of last year
that scans Informix .err files for error messages.

There are several differences between this version (1.4) and the one
previously posted (1.2):

  o The command has been renamed from "fglerr" to "inferr" since I was
    worried about a possible name collision with future Informix utilities,
    and since the script *does* handle error files from Informix compilers
    other than I4GL.

  o The method of saving source lines as the script moves through the
    error file has been changed to a circular list (a.k.a ring buffer,
    etc.) which makes it much faster on long flies.

  o The script now sets the default error flag character separately for
    each file on the command line, rather than use the same flag for all
    files.

  o This version fixes a couple of minor bugs.

I have placed a copy of this shar in the ftp archive on mathcs.emory.edu
in "/pub/informix/pub/inferr.shar".  This replaces "fglerr.shar".

If you notice any bugs or have any suggestions for improvement, I like to
hear them.

Happy compiling,

Walt.

------------------------------------------------------------------------------

#!/bin/sh
#
# This is a shell archive.  To extract its contents,
# execute this file with /bin/sh to create the file(s):
#
# inferr		    inferr.1
#
# This shell archive created: Thu Jan  7 09:08:28 EST 1993
#
echo "Extracting file inferr"
sed -e 's/^X//' <<\SHAR_EOF > inferr
X#!/bin/sh
X#
X#   inferr   List Informix errors in *.err files
X#
X#
X#   SCCS ID:  @(#) inferr 1.4 1/7/93 09:04:24
X#
X#
X#   Usage:  inferr [-fc] [-snum] [-enum] [-nv] [files]
X#
X#   where:
X#
X#      -fc     use character "c" as the error line flag
X#      -snum   list a maximum of <num> source lines before each error message
X#      -enum   list a maximum of <num> lines from each error message
X#      -n      list only source line numbers of errors
X#      -v      list file names (may be done automatically based on arguments)
X#
X#
X#   This script scans error files generated by various Informix program, form
X#   and report compilers.  It will list the source line number, error message
X#   and preceding source lines for each error.
X#
X#
X#   Notice:  Copyright 1992, 1993 by Yerkes Research Center of Emory
X#            University, Atlanta, Georgia, U.S.A.  All Rights Reserved.
X#
X#
X#   Author:  Walt Hultgren <walt@rmy.emory.edu>
X#
X#
X
X
XCMD_NAME=`basename $0`
X
XUSAGE="usage:  $CMD_NAME [-fc] [-snum] [-enum] [-nv] [files]"
X
X
X#
X#   Set defaults
X#
X
XMAX_SRC='5'     # number of source lines to list before error message
XMAX_ERR='100'   # number of lines to list from each error message
XNUM_ONLY='0'    # default to not list only line numbers of errors
X
X
X#
X#   Set default error line flag based on command name
X#
X
Xcase "$CMD_NAME" in
X    ace* | form* | frm* | per* | sql* )  DFLT_FLAG='#' ;;
X    * )                                  DFLT_FLAG='|' ;;
Xesac
X
X
X#
X#   Parse command line
X#
X
Xset -- `getopt 'e:f:ns:v' $* 2>/dev/null`
X
Xif [ $? -ne 0 ]
Xthen
X    echo "$USAGE" 1>&2
X    exit 1
Xfi
X
XARG_FLAG=''
XLIST_NAMES=''
X
Xfor ARG in $*
Xdo
X    case "$ARG" in
X        -e )  MAX_ERR="$2" ; shift 2 ;;
X        -f )  ARG_FLAG="$2" ; shift 2 ;;
X        -n )  NUM_ONLY='1' ; MAX_ERR='0' ; MAX_SRC='0' ; shift ;;
X        -s )  MAX_SRC="$2" ; shift 2 ;;
X        -v )  LIST_NAMES='Y' ; shift ;;
X        -- )  shift ; break ;;
X    esac
Xdone
X
X
X#
X#   Set default verbose mode based on number of files specified
X#
X
Xif [ $# -gt 1 ]
Xthen
X    LIST_NAMES='Y'
Xfi
X
X
X#
X#   If no files specified, use standard input
X#
X
Xif [ $# -eq 0 ]
Xthen
X    set - -
Xfi
X
X
X#
X#   Scan error file(s) with awk to extract error messages
X#
X
Xfor ARG_FILE in $*
Xdo
X
X    #
X    #   Get source and error file names from command-line file name
X    #
X
X    SRC_FILE=''
X    ERR_FILE=''
X    ERR_FLAG=''
X
X    case "$ARG_FILE" in
X
X        *.4gl | *.ace | *.per | *.sql )
X            SRC_FILE="$ARG_FILE"
X            ERR_FILE=`echo $SRC_FILE | sed 's/...$/err/'`
X            LIST_NAMES='Y'
X            ;;
X
X        *.err )
X            ERR_FILE="$ARG_FILE"
X            SRC_BASE=`echo $ERR_FILE | sed 's/\.err$//'`
X
X            FILES=`ls $SRC_BASE.* 2>/dev/null | grep -v '\.err$'`
X
X            if [ -n "$FILES" ]
X            then
X                set $FILES
X
X                if [ $# = 1 ]
X                then
X                    SRC_FILE="$1"
X                else
X                    ERR_SUM=`head -2 $ERR_FILE | sum`
X
X                    for FILE in $*
X                    do
X                        case "$FILE" in
X                            *.4gl | *.ace | *.per | *.sql )
X                                SRC_SUM=`head -2 $FILE | sum` ;;
X                            * )
X                                continue ;;
X                        esac
X
X                        if [ "$SRC_SUM" = "$ERR_SUM" ]
X                        then
X                            SRC_FILE="$FILE"
X                            break
X                        fi
X                    done
X
X                    LIST_NAMES='Y'
X                fi
X            fi
X            ;;
X
X        * )
X            ERR_FILE="$ARG_FILE"
X            ;;
X    esac
X
X
X    #
X    #   Set error flag based on source file name if not given on command line
X    #
X
X    if [ -n "$ARG_FLAG" ]
X    then
X        ERR_FLAG="$ARG_FLAG"
X    else
X        case "$SRC_FILE" in
X            *.4gl )                  ERR_FLAG='|' ;;
X            *.ace | *.per | *.sql )  ERR_FLAG='#' ;;
X            * )                      ERR_FLAG="$DFLT_FLAG" ;;
X        esac
X    fi
X
X
X    #
X    #   Print file names if required
X    #
X
X    if [ -n "$LIST_NAMES" ]
X    then
X        echo
X        echo "Source file: $SRC_FILE"
X
X        if [ "$ERR_FILE" = "-" ]
X        then
X            echo "Error file:  standard input"
X        else
X            echo "Error file:  $ERR_FILE"
X        fi
X
X        echo "Error flag:  '$ERR_FLAG'"
X    fi
X
X
X    #
X    #   Make sure error file exists if not standard input
X    #
X
X    if [ "$ERR_FILE" != "-" ]
X    then
X        if [ ! -r "$ERR_FILE" ]
X        then
X            echo "$CMD_NAME:  cannot read file '$ERR_FILE'" 1>&2
X            continue
X        fi
X    else
X        ERR_FILE=''
X    fi
X
X
X    #
X    #   Run file through awk sript
X    #
X
X    nawk '
X        BEGIN {
X            err_expr = "^\\" errflag   # build RE for error message line flag
X
X            max_src  = maxsrc    # work around nawk bug
X            max_err  = maxerr
X            num_only = numonly
X
X            err_msg  = 0   # ==> currently listing error message
X            src_line = 0   # current line number from source file
X
X            num_src  = 0   # number of source lines saved for next error msg
X
X            fst_src  = 1   # pointers to saved source line buffer
X            lst_src  = 0
X            }
X
X
X        $0 ~ err_expr {   # error message line
X
X            if ( err_msg == 0 )
X              {
X                if ( num_only == 1 )
X                  {
X                    printf ( "%d\n", src_line )
X                  }
X                else
X                  {
X                    printf ( "\nSource line" )
X
X                    if ( num_src > 1 )
X                        printf ( "s %d-%d:\n",
X                                 ( src_line - num_src + 1 ), src_line )
X                    else
X                        printf ( " %d:\n", src_line )
X
X                    if ( lst_src > 0 )
X                      {
X                        if ( fst_src <= lst_src )
X                          {
X                            for ( i = fst_src ; i <= lst_src; ++i )
X                                print src_buf[i]
X                          }
X                        else
X                          {
X                            for ( i = fst_src ; i <= max_src; ++i )
X                                print src_buf[i]
X
X                            for ( i = 1 ; i <= lst_src; ++i )
X                                print src_buf[i]
X                          }
X                      }
X
X                    num_err = 0   # initialize error message line count
X                    num_src = 0   # reset count of saved source lines
X                    lst_src = 0   # reset pointer for saved source lines
X                  }
X
X                err_msg = 1   # indicate currently listing error message
X              }
X
X            if ( ++num_err <= max_err ) print
X
X            }
X
X
X        $0 !~ err_expr {   # source line
X
X            err_msg = 0   # turn off error message indicator
X            ++src_line    # increment source line count
X
X            if ( max_src > 0 )
X              {
X                if ( lst_src == 0 )
X                  {
X                    fst_src = 1
X                    lst_src = 1
X                  }
X                else
X                  {
X                    lst_src = ( lst_src == max_src ? 1 : lst_src + 1 )
X
X                    if ( fst_src == lst_src )
X                      {
X                        fst_src = ( fst_src == max_src ? 1 : fst_src + 1 )
X                      }
X                  }
X
X                src_buf[lst_src] = $0
X
X                if ( num_src < max_src ) ++num_src
X
X              }
X            }
X
X        ' errflag="$ERR_FLAG" maxsrc="$MAX_SRC" maxerr="$MAX_ERR" \
X          numonly="$NUM_ONLY" $ERR_FILE
Xdone
SHAR_EOF
if [ `wc -c < inferr` -ne     7700 ]
then
    echo "Lengths do not match -- Bad Copy of inferr"
fi
echo "Extracting file inferr.1"
sed -e 's/^X//' <<\SHAR_EOF > inferr.1
X.\"   inferr.1   Source for man page(s) for inferr command
X.\"
X.\"
X.\"   Copyright 1992, 1993 by Yerkes Research Center of Emory University,
X.\"          Atlanta, Georgia, U.S.A.  All Rights Reserved.
X.\"
X.\"
X.\"   SCCS ID:  @(#) inferr.1 1.2 1/7/93 09:04:27
X.\"
X.\"
X.TH INFERR 1 "January, 1993"
X.UC 4.2
X.SH NAME
Xinferr \- List errors in Informix .err files
X.SH SYNOPSIS
X.B inferr
X[-fc] [-snum] [-enum] [-nv] [files]
X.SH DESCRIPTION
X.I inferr
Xscans
X.I .err
Xerror files generated by various Informix compilers.  It lists the error
Xmessage and preceding source lines for each error.
X.LP
XThe error files to be scanned may be specified by listing their names
Xor the names of their corresponding source or object files on the command
Xline.  If a file name ends in one of the standard Informix source
Xor object suffixes,
X.I inferr
Xwill attempt to read its
X.I .err
Xfile.
X.LP
XIf no files are specified, or if a file name of "-" is used,
Xstandard input will be read.
X.SH OPTIONS
X.IP "-fc" 8
Xuse character
X.I c
Xas the error message line flag.
X.IP "-snum" 8
Xlist a maximum of
X.I num
Xsource lines preceding each error message.
X.IP "-enum" 8
Xlist a maximum of the first
X.I num
Xlines of each error message.
X.IP -n 8
Xlist only the source file line numbers where errors occur.
X.IP -v 8
Xlist the name of each error file before displaying the error messages
Xin it ("verbose" mode).  This may occur automatically.
X.SH DETECTING ERROR MESSAGES
XAn Informix
X.I .err
Xerror file contains a copy of the source file with error messages interspersed
Xthroughout the file.  The message for each error immediately follows the
Xsource line at which the compiler recognized the error.
X.LP
XError message lines are distinguished from source lines by a one-character
Xflag at the beginning of each error line.  The character used depends on
Xwhich compiler generated the file.
X.LP
XIf file names are given that end in one of the standard Informix source
Xor object suffixes,
X.I inferr
Xwill automatically use the appropriate flag character for each file.
XThe default error flag is "|", the character used by the Informix-4GL compiler.
X.LP
XThe error message line flag may be set explicitly using the (-f) option.
X.SH REPORTING ERRORS
XUnder normal operation,
X.I inferr
Xlists each error message and the source lines immediately preceding it.
X.LP
XThe (-s) and (-e) options may be used to specify the maximum number of source
Xlines and error message lines respectively to be listed for each error.  The
Xdefault (-s) value is 5, and the default (-e) value is 100.  Either
Xof these counts may be set to zero (0).
X.LP
XThe (-n) option causes only the line numbers of source lines containing
Xerrors to be listed.  The line number reported for each error is the
Xline number of the last source line before the error message.  That is, it
Xis the number of the offending source line as reported by the
Xcompiler, even though the actual error may occur earlier in the file.
X.LP
XThe (-v) option causes the name of each error file to be displayed before
Xits errors are listed.  This occurs automatically if more than
Xone file is scanned, or if a source or object file name is given.
X.SH RESTRICTIONS
XAce report source files present a potential problem to
X.I inferr.
XAce error message lines begin with "#", yet such lines are also valid
Xcomment lines.  If you use lines with "#" in character position (1) for
Xcomments in Ace source files, such lines will be interpreted as error
Xmessages by
X.I inferr.
XAce comment lines that begin with a "#" not in position (1)
Xwill be correctly processed as source lines.
X.SH NOTICES
XCopyright 1992, 1993 by Yerkes Research Center of Emory University,
XAtlanta, Georgia, U.S.A.  All Rights Reserved.
X.LP
X"Informix" is a registered trademark of Informix Software, Inc.
X.SH AUTHOR
XWalt Hultgren  walt@rmy.emory.edu
SHAR_EOF
if [ `wc -c < inferr.1` -ne     3787 ]
then
    echo "Lengths do not match -- Bad Copy of inferr.1"
fi
echo "Done."

------------------------------------------------------------------------------

-- 
Walt Hultgren              Internet: walt@rmy.emory.edu       (IP 128.140.8.1)
Emory University               UUCP: {...,gatech,rutgers,uunet}!emory!rmy!walt
954 Gatewood Road, NE        BITNET: walt@EMORY
Atlanta, GA  30329  USA       Voice: +1 404 727 0648