229 lines
4.9 KiB
C
Raw Normal View History

/* 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>
#include "postgres.h"
#ifdef HAVE_GETOPT_H
1999-07-19 01:18:05 +00:00
#include "getopt.h"
#endif
#include "extern.h"
1999-12-23 12:33:19 +00:00
int ret_value = OK, autocommit = 0;
struct _include_path *include_paths = NULL;
struct cursor *cur = NULL;
struct typedefs *types = NULL;
struct _defines *defines = NULL;
static void
usage(char *progname)
{
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);
}
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;
}
int
main(int argc, char *const argv[])
{
int fnr,
c,
1999-12-23 12:33:19 +00:00
verbose = false,
out_option = 0;
struct _include_path *ip;
add_include_path("/usr/include");
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)
{
switch (c)
{
case 'o':
1999-01-17 06:20:06 +00:00
#ifndef __CYGWIN32__
yyout = fopen(optarg, "w");
1999-01-17 06:20:06 +00:00
#else
yyout = fopen(optarg, "wb");
#endif
if (yyout == NULL)
perror(optarg);
else
out_option = 1;
break;
case 'I':
add_include_path(optarg);
break;
case 't':
1999-04-14 18:51:37 +00:00
autocommit = 1;
break;
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;
default:
usage(argv[0]);
1998-09-01 03:29:17 +00:00
return ILLEGAL_OPTION;
}
}
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;
}
if (optind >= argc) /* no files specified */
{
usage(argv[0]);
return (ILLEGAL_OPTION);
}
else
{
/* after the options there must not be anything but filenames */
for (fnr = optind; fnr < argc; fnr++)
{
char *output_filename = NULL,
*ptr2ext;
input_filename = mm_alloc(strlen(argv[fnr]) + 5);
strcpy(input_filename, argv[fnr]);
1999-12-23 12:33:19 +00:00
ptr2ext = strrchr(input_filename, '.');
/* no extension? */
if (ptr2ext == NULL)
{
ptr2ext = input_filename + strlen(input_filename);
/* no extension => add .pgc */
ptr2ext[0] = '.';
ptr2ext[1] = 'p';
ptr2ext[2] = 'g';
ptr2ext[3] = 'c';
ptr2ext[4] = '\0';
}
if (out_option == 0)/* calculate the output name */
{
output_filename = strdup(input_filename);
ptr2ext = strrchr(output_filename, '.');
/* make extension = .c */
ptr2ext[1] = 'c';
ptr2ext[2] = '\0';
1999-01-17 06:20:06 +00:00
#ifndef __CYGWIN32__
yyout = fopen(output_filename, "w");
1999-01-17 06:20:06 +00:00
#else
yyout = fopen(output_filename, "wb");
#endif
if (yyout == NULL)
{
perror(output_filename);
free(output_filename);
free(input_filename);
continue;
}
}
1999-01-17 06:20:06 +00:00
#ifndef __CYGWIN32__
yyin = fopen(input_filename, "r");
1999-01-17 06:20:06 +00:00
#else
yyin = fopen(input_filename, "rb");
#endif
if (yyin == NULL)
perror(argv[fnr]);
else
{
struct cursor *ptr;
struct _defines *defptr;
struct typedefs *typeptr;
1999-05-25 16:15:34 +00:00
/* remove old cursor definitions if any are still there */
for (ptr = cur; ptr != NULL;)
{
struct cursor *this = ptr;
1999-05-25 16:15:34 +00:00
struct arguments *l1,
*l2;
free(ptr->command);
free(ptr->connection);
free(ptr->name);
for (l1 = ptr->argsinsert; l1; l1 = l2)
{
l2 = l1->next;
free(l1);
}
for (l1 = ptr->argsresult; l1; l1 = l2)
{
l2 = l1->next;
free(l1);
}
ptr = ptr->next;
free(this);
}
1999-12-23 12:33:19 +00:00
/* remove old defines as well */
for (defptr = defines; defptr != NULL;)
{
1999-12-23 12:33:19 +00:00
struct _defines *this = defptr;
1999-12-23 12:33:19 +00:00
free(defptr->new);
free(defptr->old);
defptr = defptr->next;
free(this);
}
1999-05-25 16:15:34 +00:00
/* and old typedefs */
for (typeptr = types; typeptr != NULL;)
{
struct typedefs *this = typeptr;
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
/* initialize lex */
lex_init();
/* 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);
/* and parse the source */
yyparse();
if (yyin != NULL)
fclose(yyin);
if (out_option == 0)
fclose(yyout);
}
if (output_filename)
free(output_filename);
free(input_filename);
}
}
1999-12-08 09:52:29 +00:00
return ret_value;
}