1998-02-05 16:46:43 +01: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 */
# include <stdio.h>
1998-02-18 02:26:02 +01:00
# if HAVE_GETOPT_H
# include <getopt.h>
# else
# include <unistd.h>
# endif
1998-02-06 14:32:34 +01:00
# include <stdlib.h>
# include <strings.h>
1998-02-05 16:46:43 +01:00
1998-02-11 16:30:00 +01:00
# include "extern.h"
1998-02-06 14:32:34 +01:00
static void
usage ( char * progname )
{
1998-02-11 16:30:00 +01:00
fprintf ( stderr , " ecpg - the postgresql preprocessor, version: %d.%d.%d \n " , MAJOR_VERSION , MINOR_VERSION , PATCHLEVEL ) ;
fprintf ( stderr , " Usage: %s: [-v] [-d] [ -o outout file name] file1 [file2] ... \n " , progname ) ;
1998-02-06 14:32:34 +01:00
}
int
main ( int argc , char * const argv [ ] )
1998-02-05 16:46:43 +01:00
{
1998-02-11 16:30:00 +01:00
char c , out_option = 0 ;
1998-02-06 14:32:34 +01:00
int fnr ;
1998-02-11 16:30:00 +01:00
while ( ( c = getopt ( argc , argv , " vdo: " ) ) ! = EOF )
1998-02-06 14:32:34 +01:00
{
switch ( c )
{
case ' o ' :
yyout = fopen ( optarg , " w " ) ;
if ( yyout = = NULL )
perror ( optarg ) ;
else
out_option = 1 ;
break ;
1998-02-11 16:30:00 +01:00
case ' d ' :
debugging = 1 ;
break ;
case ' v ' :
1998-02-06 14:32:34 +01:00
default :
usage ( argv [ 0 ] ) ;
}
}
1998-02-11 16:30:00 +01:00
if ( optind > = argc ) /* no files specified */
usage ( argv [ 0 ] ) ;
else
1998-02-06 14:32:34 +01:00
{
1998-02-11 16:30:00 +01:00
/* after the options there must not be anything but filenames */
for ( fnr = optind ; fnr < argc ; fnr + + )
1998-02-06 14:32:34 +01:00
{
1998-02-11 16:30:00 +01:00
char * filename , * ptr2ext ;
1998-02-06 14:32:34 +01:00
1998-02-17 02:48:12 +01:00
filename = mm_alloc ( strlen ( argv [ fnr ] ) + 2 ) ;
1998-02-06 14:32:34 +01:00
1998-02-11 16:30:00 +01:00
strcpy ( filename , argv [ fnr ] ) ;
1998-02-06 14:32:34 +01:00
1998-02-11 16:30:00 +01:00
ptr2ext = strrchr ( filename , ' . ' ) ;
/* no extension or extension not equal .pgc */
if ( ptr2ext = = NULL | | strcmp ( ptr2ext , " .pgc " ) ! = 0 )
{
ptr2ext = filename + strlen ( filename ) ;
ptr2ext [ 0 ] = ' . ' ;
}
1998-02-06 14:32:34 +01:00
1998-02-11 16:30:00 +01:00
/* make extension = .c */
ptr2ext [ 1 ] = ' c ' ;
ptr2ext [ 2 ] = ' \0 ' ;
if ( out_option = = 0 ) /* calculate the output name */
1998-02-10 17:44:17 +01:00
{
1998-02-11 16:30:00 +01:00
yyout = fopen ( filename , " w " ) ;
if ( yyout = = NULL )
{
perror ( filename ) ;
free ( filename ) ;
continue ;
}
1998-02-06 14:32:34 +01:00
}
1998-02-11 16:30:00 +01:00
yyin = fopen ( input_filename = argv [ fnr ] , " r " ) ;
if ( yyin = = NULL )
perror ( argv [ fnr ] ) ;
else
{
/* initialize lex */
lex_init ( ) ;
1998-02-06 14:32:34 +01:00
1998-02-11 16:30:00 +01:00
/* we need two includes */
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 " , MAJOR_VERSION , MINOR_VERSION , PATCHLEVEL ) ;
1998-02-06 14:32:34 +01:00
1998-02-11 16:30:00 +01:00
/* and parse the source */
yyparse ( ) ;
1998-02-06 14:32:34 +01:00
1998-02-11 16:30:00 +01:00
fclose ( yyin ) ;
if ( out_option = = 0 )
fclose ( yyout ) ;
}
1998-02-06 14:32:34 +01:00
1998-02-11 16:30:00 +01:00
free ( filename ) ;
}
1998-02-06 14:32:34 +01:00
}
return ( 0 ) ;
1998-02-05 16:46:43 +01:00
}