From: bergquis@gdc.com (Brett Bergquist)
Date: Fri, 29 Apr 1994 11:26:01 GMT
Newsgroups: comp.databases.informix

Here is a script that we use here to fix some of the problems with the
ESQL/C preprocessor.  As an added benifit, this script first preprocesses
the ESQL/C file through the C preprocessor.  This allows us to use all of
the C preprocessor capabilities such within our ESQL/C file, not just the
limited ones the the ESQL/C preprocessor provides.

This script works for us.  If it also works for anyone else, great.

#!/bin/sh
#
#   ESQL/C Pre-Preprocess
#
#   This script allows an ESQL/C file to be preprocessed through the C
#   Preprocessor (/lib/cpp), and then the ESQL/C preprocessor and adjusts
#   the line number directives so that debugging can be done at the ESQL/C
#   source level.
#
########################################################################

# Functions
StripExpansion()
{
awk '
BEGIN	{
	incode = 1
	l = 1
	}
/^# [0-9][0-9]*/	{
	fname = "\""FNAME"\""
	if ($3 == fname)
	{
		while (l != $2)
		{
			print ""
			l += 1
		}
		incode = 1
	}
	else
	{
		if (incode == 1 && $2 == 1 && $4 == 1)
		{	
			incname = substr($3,2,length($3) - 2)
			if ((i = index(incname,"/usr/include/")) != 0)
			{
				incname = substr(incname, length("/usr/include/") + 1)
			}
			printf("#include \"%s\"\n",incname)
			l += 1
		}
		incode = 0
	}
	next
}
			{
	if (incode == 1)
	{
		print
		l += 1
	}
}' FNAME=$1
}

Renumber()
{
awk '
BEGIN	{
	line=0;
	}
/^#line [0-9][0-9]*/	{
	line=$2;
	name=$3;
	print $0;
	next;
	}
/^ _/	{
	print "#line "line" "name;
	}
	{
	print $0;
	}
' $1
}

# Macros
CPPFLAGS=
ECFILE=
PWD=`pwd`

# Mainline code
# check arguments
if [ $# -eq 0 ]
then
    echo "usage: $0 [CPPFLAGS] esqlcfile" >&2
    exit 1
fi

# gather CPPFLAGS and ECFILE
while [ $# -ne 1 ]
do
    CPPFLAGS="$CPPFLAGS $1"
    shift
done

# Build filenames
ECFILE=$1
CFILE=`basename $ECFILE .ec`.c

# Set trap to cleanup
trap "/bin/rm /tmp/$ECFILE /tmp/$CFILE" 0

echo "Preprocessing $ECFILE"
/lib/cpp -E $CPPFLAGS $ECFILE | StripExpansion $ECFILE > /tmp/$ECFILE
if [ $? -ne 0 ]
then
    exit 1
fi
(cd /tmp
echo "Running esql on $ECFILE"
esql $CPPFLAGS -g -e $ECFILE
)
if [ $? -ne 0 ]
then
    exit 1
fi
Renumber /tmp/$CFILE >$CFILE
exit 0


--
Brett M. Bergquist, Principal Engineer | "Remind me" ... "to write an
General DataComm, Inc.,                | "article on the compulsive reading
Middlebury, CT 06762                   | of news." - Stranger in a Strange Land
Email: bergquis@gdc.com		Voice: (203) 758-1811 ext. 7635
