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 */
1998-02-24 05:02:20 +01:00
# include "postgres.h"
1998-02-05 16:46:43 +01:00
# include <stdio.h>
1998-02-18 02:26:02 +01:00
# if HAVE_GETOPT_H
1998-02-26 05:46:47 +01:00
# include <getopt.h>
1998-02-18 02:26:02 +01:00
# else
1998-02-26 05:46:47 +01:00
# include <unistd.h>
1998-03-20 04:08:11 +01:00
extern int optind ;
extern char * optarg ;
1998-02-18 02:26:02 +01:00
# endif
1998-02-06 14:32:34 +01:00
# include <stdlib.h>
1998-02-24 05:02:20 +01:00
# if defined(HAVE_STRING_H)
1998-02-26 05:46:47 +01:00
# include <string.h>
1998-02-24 05:02:20 +01:00
# else
1998-02-26 05:46:47 +01:00
# include <strings.h>
1998-02-24 05:02:20 +01:00
# endif
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
1998-03-20 04:08:11 +01:00
struct _include_path * include_paths ;
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 ) ;
1998-04-24 14:10:20 +02:00
fprintf ( stderr , " Usage: %s: [-v] [-I include path] [ -o output file name] file1 [file2] ... \n " , progname ) ;
1998-03-20 04:08:11 +01:00
}
static void
add_include_path ( char * path )
{
struct _include_path * ip = include_paths ;
include_paths = mm_alloc ( sizeof ( struct _include_path ) ) ;
include_paths - > path = path ;
include_paths - > next = ip ;
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-27 13:59:33 +01:00
int fnr , c , out_option = 0 ;
1998-03-20 04:08:11 +01:00
struct _include_path * ip ;
add_include_path ( " /usr/include " ) ;
add_include_path ( INCLUDE_PATH ) ;
add_include_path ( " /usr/local/include " ) ;
add_include_path ( " . " ) ;
1998-04-24 14:10:20 +02:00
while ( ( c = getopt ( argc , argv , " vo:I: " ) ) ! = 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-03-20 04:08:11 +01:00
case ' I ' :
add_include_path ( optarg ) ;
break ;
1998-02-11 16:30:00 +01:00
case ' v ' :
1998-03-20 04:08:11 +01:00
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 ( 0 ) ;
1998-02-06 14:32:34 +01:00
default :
usage ( argv [ 0 ] ) ;
1998-03-20 04:08:11 +01:00
return ( 1 ) ;
1998-02-06 14:32:34 +01:00
}
}
1998-02-26 05:46:47 +01:00
if ( optind > = argc ) /* no files specified */
1998-03-20 04:08:11 +01:00
{
1998-02-11 16:30:00 +01:00
usage ( argv [ 0 ] ) ;
1998-03-20 04:08:11 +01:00
return ( 1 ) ;
}
1998-02-11 16:30:00 +01:00
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-04-21 15:23:24 +02:00
char * output_filename = NULL , * ptr2ext ;
1998-02-06 14:32:34 +01:00
1998-03-20 04:08:11 +01:00
input_filename = mm_alloc ( strlen ( argv [ fnr ] ) + 5 ) ;
1998-02-06 14:32:34 +01:00
1998-03-20 04:08:11 +01:00
strcpy ( input_filename , argv [ fnr ] ) ;
1998-02-06 14:32:34 +01:00
1998-03-20 04:08:11 +01:00
ptr2ext = strrchr ( input_filename , ' . ' ) ;
/* no extension? */
if ( ptr2ext = = NULL )
1998-02-26 05:46:47 +01:00
{
1998-03-20 04:08:11 +01:00
ptr2ext = input_filename + strlen ( input_filename ) ;
/* no extension => add .pgc */
1998-02-11 16:30:00 +01:00
ptr2ext [ 0 ] = ' . ' ;
1998-03-20 04:08:11 +01:00
ptr2ext [ 1 ] = ' p ' ;
ptr2ext [ 2 ] = ' g ' ;
ptr2ext [ 3 ] = ' c ' ;
ptr2ext [ 4 ] = ' \0 ' ;
1998-02-11 16:30:00 +01:00
}
1998-02-06 14:32:34 +01:00
1998-02-26 05:46:47 +01:00
if ( out_option = = 0 ) /* calculate the output name */
1998-02-10 17:44:17 +01:00
{
1998-04-21 15:23:24 +02:00
output_filename = strdup ( input_filename ) ;
1998-03-20 04:08:11 +01:00
ptr2ext = strrchr ( output_filename , ' . ' ) ;
/* make extension = .c */
ptr2ext [ 1 ] = ' c ' ;
ptr2ext [ 2 ] = ' \0 ' ;
yyout = fopen ( output_filename , " w " ) ;
1998-02-11 16:30:00 +01:00
if ( yyout = = NULL )
{
1998-03-20 04:08:11 +01:00
perror ( output_filename ) ;
free ( output_filename ) ;
free ( input_filename ) ;
1998-02-11 16:30:00 +01:00
continue ;
}
1998-02-06 14:32:34 +01:00
}
1998-02-19 14:52:17 +01:00
yyin = fopen ( input_filename , " r " ) ;
1998-02-11 16:30:00 +01:00
if ( yyin = = NULL )
perror ( argv [ fnr ] ) ;
else
{
1998-04-21 15:23:24 +02:00
struct cursor * ptr ;
1998-02-11 16:30:00 +01:00
/* initialize lex */
lex_init ( ) ;
1998-04-21 15:23:24 +02:00
/* initialize cursor list */
for ( ptr = cur ; ptr ! = NULL ; )
{
struct cursor * c ;
free ( ptr - > name ) ;
free ( ptr - > command ) ;
c = ptr ;
ptr = ptr - > next ;
free ( c ) ;
}
cur = NULL ;
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-03-20 04:08:11 +01:00
if ( yyin ! = NULL )
fclose ( yyin ) ;
1998-02-11 16:30:00 +01:00
if ( out_option = = 0 )
fclose ( yyout ) ;
}
1998-04-21 15:23:24 +02:00
if ( output_filename )
free ( output_filename ) ;
1998-03-20 04:08:11 +01:00
free ( input_filename ) ;
1998-02-11 16:30:00 +01:00
}
1998-02-06 14:32:34 +01:00
}
return ( 0 ) ;
1998-02-05 16:46:43 +01:00
}