[comp.sources.d] combining cc stderr with c programs

cml@sppy00.UUCP (Christopher Lott) (12/04/86)

I am new to UNIX and c, and was used to receiving output from
compiler(s) that consisted of the source code with lines that
it didn't like flagged and/or the error beneath the line.
When I first met the UNIX environment, I was disappointed that
this option was not available for the c compiler.  So, I wrote
a small file processor with a controlling script that combines
the stderr output from cc with the cprogam and shows it to you.

Try it, certainly customize it, and _Please_ let me know what you think!!
No guarantees on portability, but I think it's simple enough
that it shouldn't give anyone fits.

'inline' is the controlling script; you will need to extract
this archive and then compile combine.c into a file called
combine, for now.  Customize the inline script as needed.

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#       inline combine.c
# This archive created: Thu Dec  4 14:29:41 EST 1986
PATH=/bin:$PATH; export PATH
echo "shar: extracting 'inline' (524 characters)"
if [ -f inline ]
then
      echo "shar: will not over-write existing file 'inline'"
else
sed 's/^X//' << \SHAR_EOF > inline
X
X#! /bin/sh
X# First distribution of inline and combine.c - 861204
X#
X# written by Christopher Lott
X# ...osu-eddie!sppy00!cml
X# sh controller for combiner program
X
Xif [ $# -lt 1 ]
X   then echo "Usage: $0 prog.c"
X	exit 1
Xfi
X
Xcc $1 2> stderr.tmp
X
Xif [ -s stderr.tmp ]
X   then
X# editing out >"prog.c", line <
X	editstring1='"'$1'", line '
X	editstring2=$1": "
X	sed -e "s/$editstring1//" -e "s/$editstring2//" stderr.tmp > stderr
X	rm stderr.tmp
X	combine $1 stderr | more
X   else
X      echo "clean compile.  executable in a.out."
Xfi
SHAR_EOF
fi
# set inline so it will run!
chmod 755 inline

echo "shar: extracting 'combine.c' (1910 characters)"
if [ -f combine.c ]
then
      echo "shar: will not over-write existing file 'combine.c'"
else
sed 's/^X//' << \END-OF-CPROG > combine.c
X/* written by Christopher Lott          */
X/* Online Computer Library Center       */
X/* ...osu-eddie!sppy00!cml              */
X/* combines cprogram with error output  */
X/* to produce a listing of program      */
X/* with error msgs following the line.  */
X
X#include <stdio.h>
X
X#define MAXLINE 255
X
XFILE *fopen();
X
Xmain (argc, argv)
X    int argc;
X    char *argv[];
X
X   {
X      FILE *openfile(), *pgmfile, *errfile;
X
X      /* must have 'command prog.c stderr'; anything else is ingnored */
X      if (argc == 3)
X	 {
X	 pgmfile = openfile (++argv);	/* open the program file */
X
X	 errfile = openfile (++argv);	/* open cc stderr output */
X
X	 processfiles (pgmfile, errfile);	/* do the work */
X	 }
X      else 
X	 printf ("usage: %s prog.c stderr\n", *argv);
X   }
X
XFILE *openfile (name)
Xchar *name[]; 	
X  {
X  FILE *fp;
X
X         if ((fp = fopen(*name, "r")) == NULL)
X	     {
X	     printf ("can't open %s\n", *name);
X	     exit (1);
X	     }
X	 else
X	     {
X	     return (fp);
X	     }
X  }
X
Xprocessfiles (cfile, efile)
XFILE *cfile, *efile;
X   {
X
X   long clineno, elineno;
X   char cline[MAXLINE], eline[MAXLINE];
X
X      clineno = 0;
X      /* get first error line */
X      fgets (eline, MAXLINE, efile);
X      sscanf (eline, "%d:", &elineno);
X
X      while ( (fgets (cline, MAXLINE, cfile) ) != NULL)
X	 {
X	 clineno++;
X	 printf ("%s", cline);
X
X	 if (clineno == elineno) 
X	    {
X	    printf ("line %s", eline);
X
X	    while ( (fgets (eline, MAXLINE, efile) ) != NULL)
X	       {
X	       /* may have several errors for same line */
X	       sscanf (eline, "%d:", &elineno);
X	       if (clineno == elineno)
X		  printf ("line %s", eline);
X	       else
X		  break;
X	       }  /* end while retrieving elines */
X
X	    }  /* end if have a error line match */
X
X         }  /* end while retrieving clines */
X
X	 /* may have one left over */
X	 if (elineno >= clineno) 
X	    printf ("line %s", eline);
X
X	 return;
X
X   }  /* end processfiles */
END-OF-CPROG
fi
-- 
Christopher Lott (cml@sppy00)

woods@hao.UUCP (Greg Woods) (12/09/86)

  I won't be the only one to tell you this, but a compiler output processor
that does what you want is already available, at least under 4.2BSD: error(1).
If you do "cc blotto.c |& error" (csh) or "cc blotto.c 2>1 | error" (Bourne)
it will actually modify the C source code and stick in the error messages as
comments.

--Greg
{ucbvax!hplabs | decvax!noao | mcvax!seismo | ihnp4!seismo}
       		        !hao!woods

CSNET: woods@ncar.csnet  ARPA: woods%ncar@CSNET-RELAY.ARPA