1998-02-05 15:46:43 +00:00
/* New main for ecpg, the PostgreSQL embedded SQL precompiler. */
/* (C) Michael Meskes <meskes@debian.org> Feb 5th, 1998 */
/* Placed under the same copyright as PostgresSQL */
1999-07-19 01:18:05 +00:00
# include <unistd.h>
1998-02-24 04:02:20 +00:00
# include "postgres.h"
1999-07-19 02:27:16 +00:00
# ifdef HAVE_GETOPT_H
1999-07-19 01:18:05 +00:00
# include "getopt.h"
1998-02-24 04:02:20 +00:00
# endif
1998-02-05 15:46:43 +00:00
1998-02-11 15:30:00 +00:00
# include "extern.h"
1998-02-06 13:32:34 +00:00
1999-12-23 12:33:19 +00:00
int ret_value = OK , autocommit = 0 ;
1999-12-26 21:31:35 +00:00
struct _include_path * include_paths = NULL ;
1998-08-11 18:33:37 +00:00
struct cursor * cur = NULL ;
1999-02-20 07:01:08 +00:00
struct typedefs * types = NULL ;
1999-12-26 21:31:35 +00:00
struct _defines * defines = NULL ;
1998-05-06 13:03:47 +00:00
1998-02-06 13:32:34 +00:00
static void
usage ( char * progname )
{
1998-02-11 15:30:00 +00:00
fprintf ( stderr , " ecpg - the postgresql preprocessor, version: %d.%d.%d \n " , MAJOR_VERSION , MINOR_VERSION , PATCHLEVEL ) ;
1999-12-23 12:33:19 +00:00
fprintf ( stderr , " Usage: %s: [-v] [-t] [-I include path] [ -o output file name] file1 [file2] ... \n " , progname ) ;
1998-03-20 03:08:11 +00:00
}
static void
1998-09-01 04:40:42 +00:00
add_include_path ( char * path )
1998-03-20 03:08:11 +00:00
{
struct _include_path * ip = include_paths ;
1998-09-01 04:40:42 +00:00
include_paths = mm_alloc ( sizeof ( struct _include_path ) ) ;
include_paths - > path = path ;
include_paths - > next = ip ;
1998-02-06 13:32:34 +00:00
}
int
main ( int argc , char * const argv [ ] )
1998-02-05 15:46:43 +00:00
{
1998-09-01 04:40:42 +00:00
int fnr ,
c ,
1999-12-23 12:33:19 +00:00
verbose = false ,
1998-09-01 04:40:42 +00:00
out_option = 0 ;
struct _include_path * ip ;
add_include_path ( " /usr/include " ) ;
1998-03-20 03:08:11 +00:00
add_include_path ( INCLUDE_PATH ) ;
add_include_path ( " /usr/local/include " ) ;
add_include_path ( " . " ) ;
1999-12-23 12:33:19 +00:00
while ( ( c = getopt ( argc , argv , " vo:I:t " ) ) ! = EOF )
1998-02-06 13:32:34 +00:00
{
switch ( c )
{
case ' o ' :
1999-01-17 06:20:06 +00:00
# ifndef __CYGWIN32__
1998-02-06 13:32:34 +00:00
yyout = fopen ( optarg , " w " ) ;
1999-01-17 06:20:06 +00:00
# else
yyout = fopen ( optarg , " wb " ) ;
# endif
1998-02-06 13:32:34 +00:00
if ( yyout = = NULL )
perror ( optarg ) ;
else
out_option = 1 ;
break ;
1998-03-20 03:08:11 +00:00
case ' I ' :
add_include_path ( optarg ) ;
1998-09-01 04:40:42 +00:00
break ;
case ' t ' :
1999-04-14 18:51:37 +00:00
autocommit = 1 ;
1998-09-01 04:40:42 +00:00
break ;
1998-02-11 15:30:00 +00:00
case ' v ' :
1999-12-23 12:33:19 +00:00
verbose = true ;
The first fix is to allow an input file with a relative path and without
a ".pgc " extension. The second patch fixes a coredump when there is
more than one input file (in that case, cur and types were not set to
NULL before processing the second f ile)
The patch below modifies the accepted grammar of ecpg to accept
FETCH [direction] [amount] cursor name
i.e. the IN|FROM clause becomes optional (as in Oracle and Informix).
This removes the incompatibility mentioned in section "Porting From
Other RDBMS Packages" p169, PostgreSQL Programmer's Guide. The grammar
is modified in such a way as to avoid shift/reduce conflicts. It does
not accept the statement "EXEC SQL FETCH;" anymore, as the old grammar
did (this seems to be a bug of the old grammar anyway).
This patch cleans up the handling of space characters in the scanner;
some patte rns require \n to be in {space}, some do not. A second fix is
the handling of cpp continuati on lines; the old pattern did not match
these. The parser is patched to fix an off-by-one error in the #line
directives. The pa rser is also enhanced to report the correct location
of errors in declarations in the "E XEC SQL DECLARE SECTION". Finally,
some right recursions in the parser were replaced by left-recursions.
This patch adds preprocessor directives to ecpg; in particular
EXEC SQL IFDEF, EXEC SQL IFNDEF, EXEC SQL ELSE, EXEC SQL ELIF and EXEC SQL ENDIF
"EXEC SQL IFDEF" is used with defines made with "EXEC SQL DEFINE" and
defines, specified on the command line with -D. Defines, specified on
the command line are persistent across multiple input files. Defines can
be nested up to a maximum level of 128 (see patch). There is a fair
amount of error checking to make sure directives are matched properly. I
need preprocessor directives for porting code, that is written for an
Informix database, to a PostgreSQL database, while maintaining
compatibility with the original code. I decided not to extend the
already large ecpg grammar. Everything is done in the scanner by adding
some states, e.g. to skip all input except newlines and directives. The
preprocessor commands are compatible with Informix. Oracle uses a cpp
replacement.
Rene Hogendoorn
1999-12-21 17:42:16 +00:00
break ;
1998-02-06 13:32:34 +00:00
default :
usage ( argv [ 0 ] ) ;
1998-09-01 03:29:17 +00:00
return ILLEGAL_OPTION ;
1998-02-06 13:32:34 +00:00
}
}
1999-12-23 12:33:19 +00:00
if ( verbose )
{
fprintf ( stderr , " ecpg - the postgresql preprocessor, version: %d.%d.%d \n " , MAJOR_VERSION , MINOR_VERSION , PATCHLEVEL ) ;
fprintf ( stderr , " exec sql include ... search starts here: \n " ) ;
for ( ip = include_paths ; ip ! = NULL ; ip = ip - > next )
fprintf ( stderr , " %s \n " , ip - > path ) ;
fprintf ( stderr , " End of search list. \n " ) ;
return OK ;
}
1998-02-26 04:46:47 +00:00
if ( optind > = argc ) /* no files specified */
1998-03-20 03:08:11 +00:00
{
1998-02-11 15:30:00 +00:00
usage ( argv [ 0 ] ) ;
1998-09-01 04:40:42 +00:00
return ( ILLEGAL_OPTION ) ;
1998-03-20 03:08:11 +00:00
}
1998-02-11 15:30:00 +00:00
else
1998-02-06 13:32:34 +00:00
{
1998-02-11 15:30:00 +00:00
/* after the options there must not be anything but filenames */
for ( fnr = optind ; fnr < argc ; fnr + + )
1998-02-06 13:32:34 +00:00
{
1998-09-01 04:40:42 +00:00
char * output_filename = NULL ,
* ptr2ext ;
1998-02-06 13:32:34 +00:00
1998-03-20 03:08:11 +00:00
input_filename = mm_alloc ( strlen ( argv [ fnr ] ) + 5 ) ;
1998-02-06 13:32:34 +00:00
1998-03-20 03:08:11 +00:00
strcpy ( input_filename , argv [ fnr ] ) ;
1998-02-06 13:32:34 +00:00
1999-12-23 12:33:19 +00:00
ptr2ext = strrchr ( input_filename , ' . ' ) ;
1998-03-20 03:08:11 +00:00
/* no extension? */
if ( ptr2ext = = NULL )
1998-02-26 04:46:47 +00:00
{
1998-03-20 03:08:11 +00:00
ptr2ext = input_filename + strlen ( input_filename ) ;
1998-09-01 04:40:42 +00:00
1998-03-20 03:08:11 +00:00
/* no extension => add .pgc */
1998-02-11 15:30:00 +00:00
ptr2ext [ 0 ] = ' . ' ;
1998-03-20 03:08:11 +00:00
ptr2ext [ 1 ] = ' p ' ;
ptr2ext [ 2 ] = ' g ' ;
ptr2ext [ 3 ] = ' c ' ;
ptr2ext [ 4 ] = ' \0 ' ;
1998-02-11 15:30:00 +00:00
}
1998-02-06 13:32:34 +00:00
1998-02-26 04:46:47 +00:00
if ( out_option = = 0 ) /* calculate the output name */
1998-02-10 16:44:17 +00:00
{
1998-04-21 13:23:24 +00:00
output_filename = strdup ( input_filename ) ;
1998-09-01 04:40:42 +00:00
1998-03-20 03:08:11 +00:00
ptr2ext = strrchr ( output_filename , ' . ' ) ;
/* make extension = .c */
ptr2ext [ 1 ] = ' c ' ;
ptr2ext [ 2 ] = ' \0 ' ;
1998-09-01 04:40:42 +00:00
1999-01-17 06:20:06 +00:00
# ifndef __CYGWIN32__
1998-03-20 03:08:11 +00:00
yyout = fopen ( output_filename , " w " ) ;
1999-01-17 06:20:06 +00:00
# else
yyout = fopen ( output_filename , " wb " ) ;
# endif
1998-02-11 15:30:00 +00:00
if ( yyout = = NULL )
{
1998-03-20 03:08:11 +00:00
perror ( output_filename ) ;
free ( output_filename ) ;
free ( input_filename ) ;
1998-02-11 15:30:00 +00:00
continue ;
}
1998-02-06 13:32:34 +00:00
}
1999-01-17 06:20:06 +00:00
# ifndef __CYGWIN32__
1998-02-19 13:52:17 +00:00
yyin = fopen ( input_filename , " r " ) ;
1999-01-17 06:20:06 +00:00
# else
yyin = fopen ( input_filename , " rb " ) ;
# endif
1998-02-11 15:30:00 +00:00
if ( yyin = = NULL )
perror ( argv [ fnr ] ) ;
else
{
1998-04-21 13:23:24 +00:00
struct cursor * ptr ;
1998-08-25 12:17:27 +00:00
struct _defines * defptr ;
1999-02-20 07:01:08 +00:00
struct typedefs * typeptr ;
1999-05-25 16:15:34 +00:00
1998-08-11 18:33:37 +00:00
/* remove old cursor definitions if any are still there */
1998-08-25 12:17:27 +00:00
for ( ptr = cur ; ptr ! = NULL ; )
1998-08-11 18:33:37 +00:00
{
1998-08-25 12:17:27 +00:00
struct cursor * this = ptr ;
1999-05-25 16:15:34 +00:00
struct arguments * l1 ,
* l2 ;
1998-09-01 04:40:42 +00:00
1998-08-11 18:33:37 +00:00
free ( ptr - > command ) ;
1999-02-23 12:57:03 +00:00
free ( ptr - > connection ) ;
1998-08-11 18:33:37 +00:00
free ( ptr - > name ) ;
1999-02-20 07:01:08 +00:00
for ( l1 = ptr - > argsinsert ; l1 ; l1 = l2 )
1998-08-11 18:33:37 +00:00
{
l2 = l1 - > next ;
free ( l1 ) ;
}
1999-02-20 07:01:08 +00:00
for ( l1 = ptr - > argsresult ; l1 ; l1 = l2 )
1998-08-11 18:33:37 +00:00
{
l2 = l1 - > next ;
free ( l1 ) ;
}
1998-08-25 12:17:27 +00:00
ptr = ptr - > next ;
free ( this ) ;
1998-08-11 18:33:37 +00:00
}
1998-09-01 04:40:42 +00:00
1999-12-23 12:33:19 +00:00
/* remove old defines as well */
for ( defptr = defines ; defptr ! = NULL ; )
1998-08-25 12:17:27 +00:00
{
1999-12-23 12:33:19 +00:00
struct _defines * this = defptr ;
1998-09-01 04:40:42 +00:00
1999-12-23 12:33:19 +00:00
free ( defptr - > new ) ;
free ( defptr - > old ) ;
defptr = defptr - > next ;
1998-08-25 12:17:27 +00:00
free ( this ) ;
}
1999-05-25 16:15:34 +00:00
1999-02-20 07:01:08 +00:00
/* and old typedefs */
for ( typeptr = types ; typeptr ! = NULL ; )
{
struct typedefs * this = typeptr ;
1998-09-01 04:40:42 +00:00
1999-02-20 07:01:08 +00:00
free ( typeptr - > name ) ;
free ( typeptr - > type ) ;
ECPGfree_struct_member ( typeptr - > struct_member_list ) ;
typeptr = typeptr - > next ;
free ( this ) ;
}
1999-05-25 16:15:34 +00:00
1998-02-11 15:30:00 +00:00
/* initialize lex */
lex_init ( ) ;
1998-09-01 04:40:42 +00:00
1998-12-22 18:50:56 +00:00
/* we need two includes */
1999-12-23 12:33:19 +00:00
fprintf ( yyout , " /* Processed by ecpg (%d.%d.%d) */ \n /* These two include files are added by the preprocessor */ \n #include <ecpgtype.h> \n #include <ecpglib.h> \n \n " , MAJOR_VERSION , MINOR_VERSION , PATCHLEVEL ) ;
1998-02-06 13:32:34 +00:00
1998-02-11 15:30:00 +00:00
/* and parse the source */
yyparse ( ) ;
1998-02-06 13:32:34 +00:00
1998-03-20 03:08:11 +00:00
if ( yyin ! = NULL )
fclose ( yyin ) ;
1998-02-11 15:30:00 +00:00
if ( out_option = = 0 )
fclose ( yyout ) ;
}
1998-04-21 13:23:24 +00:00
if ( output_filename )
free ( output_filename ) ;
1998-09-01 04:40:42 +00:00
1998-03-20 03:08:11 +00:00
free ( input_filename ) ;
1998-02-11 15:30:00 +00:00
}
1998-02-06 13:32:34 +00:00
}
1999-12-08 09:52:29 +00:00
return ret_value ;
1998-02-05 15:46:43 +00:00
}