diff options
author | Bruce Momjian <bruce@momjian.us> | 1999-12-21 17:42:16 +0000 |
---|---|---|
committer | Bruce Momjian <bruce@momjian.us> | 1999-12-21 17:42:16 +0000 |
commit | b57b0e044538990550b7169f8489624e73f98ca1 (patch) | |
tree | a908774cc74a403bdff4f76e5c7105706d85f504 /src/interfaces/ecpg/preproc/ecpg.c | |
parent | a0fa26bef60d29fa1c72e9beb0039b8f20b244f8 (diff) |
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
Diffstat (limited to 'src/interfaces/ecpg/preproc/ecpg.c')
-rw-r--r-- | src/interfaces/ecpg/preproc/ecpg.c | 55 |
1 files changed, 44 insertions, 11 deletions
diff --git a/src/interfaces/ecpg/preproc/ecpg.c b/src/interfaces/ecpg/preproc/ecpg.c index 938559670f3..a49cc6b60d5 100644 --- a/src/interfaces/ecpg/preproc/ecpg.c +++ b/src/interfaces/ecpg/preproc/ecpg.c @@ -12,7 +12,9 @@ #include "extern.h" struct _include_path *include_paths; -int ret_value = OK, autocommit = 0; +struct _defines *defines = NULL; +int autocommit = 0; +int ret_value = OK; struct cursor *cur = NULL; struct typedefs *types = NULL; @@ -20,7 +22,7 @@ static void usage(char *progname) { fprintf(stderr, "ecpg - the postgresql preprocessor, version: %d.%d.%d\n", MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL); - fprintf(stderr, "Usage: %s: [-v] [-t] [-I include path] [ -o output file name] file1 [file2] ...\n", progname); + fprintf(stderr, "Usage: %s: [-v] [-t] [-I include path] [ -o output file name] [-D define name] file1 [file2] ...\n", progname); } static void @@ -33,6 +35,18 @@ add_include_path(char *path) include_paths->next = ip; } +static void +add_preprocessor_define(char *define) +{ + struct _defines *pd = defines; + + defines = mm_alloc(sizeof(struct _defines)); + defines->old = strdup(define); + defines->new = strdup(""); + defines->pertinent = true; + defines->next = pd; +} + int main(int argc, char *const argv[]) { @@ -46,7 +60,7 @@ main(int argc, char *const argv[]) add_include_path("/usr/local/include"); add_include_path("."); - while ((c = getopt(argc, argv, "vo:I:t")) != EOF) + while ((c = getopt(argc, argv, "vo:I:tD:")) != EOF) { switch (c) { @@ -74,6 +88,9 @@ main(int argc, char *const argv[]) fprintf(stderr, " %s\n", ip->path); fprintf(stderr, "End of search list.\n"); return OK; + case 'D': + add_preprocessor_define(optarg); + break; default: usage(argv[0]); return ILLEGAL_OPTION; @@ -97,7 +114,9 @@ main(int argc, char *const argv[]) strcpy(input_filename, argv[fnr]); - ptr2ext = strrchr(input_filename, '.'); + /* take care of relative paths */ + ptr2ext = strrchr(input_filename, '/'); + ptr2ext = (ptr2ext ? strrchr(ptr2ext, '.') : strrchr(input_filename, '.')); /* no extension? */ if (ptr2ext == NULL) { @@ -170,16 +189,29 @@ main(int argc, char *const argv[]) ptr = ptr->next; free(this); } + cur = NULL; + + /* remove non-pertinent old defines as well */ + while ( defines && !defines->pertinent ) { + defptr = defines; + defines = defines->next; + + free(defptr->new); + free(defptr->old); + free(defptr); + } - /* remove old defines as well */ - for (defptr = defines; defptr != NULL;) + for (defptr = defines; defptr != NULL; defptr = defptr->next ) { - struct _defines *this = defptr; + struct _defines *this = defptr->next; + + if ( this && !this->pertinent ) { + defptr->next = this->next; - free(defptr->new); - free(defptr->old); - defptr = defptr->next; + free(this->new); + free(this->old); free(this); + } } /* and old typedefs */ @@ -193,12 +225,13 @@ main(int argc, char *const argv[]) typeptr = typeptr->next; free(this); } + types = NULL; /* initialize lex */ lex_init(); /* 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\n", MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL); + 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#line 1 \"%s\"\n", MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL, input_filename); /* and parse the source */ yyparse(); |