]> granicus.if.org Git - postgresql/commitdiff
From: Michael Meskes <meskes@topsystem.de>
authorMarc G. Fournier <scrappy@hub.org>
Fri, 6 Feb 1998 13:32:34 +0000 (13:32 +0000)
committerMarc G. Fournier <scrappy@hub.org>
Fri, 6 Feb 1998 13:32:34 +0000 (13:32 +0000)
I have implemented a better user interface (well part of) so you can use it
as expected. As usual there are some bug fixes. :-)

src/interfaces/ecpg/src/preproc/ecpg.c
src/interfaces/ecpg/src/preproc/preproc.y
src/interfaces/ecpg/src/test/Makefile
src/interfaces/ecpg/src/test/test2.c
src/interfaces/ecpg/src/test/test2.qc

index 31d5d77777702c059a5d0bcf64c0daf8f93b97ea..a7d96072af9101709e654b16228e3296924d3d65 100644 (file)
 /* Placed under the same copyright as PostgresSQL */
 
 #include <stdio.h>
+#include <getopt.h>
+#include <stdlib.h>
+#include <strings.h>
 
 extern void lex_init(void);
-int yyparse (void);
+extern FILE *yyin,
+                  *yyout;
 
-int main(int argc, char *argv[])
+int                    yyparse(void);
+
+static void
+usage(char *progname)
+{
+       fprintf(stderr, "Usage: %s: [ -o outout file name] file1 [file2] ...\n", progname);
+}
+
+int
+main(int argc, char *const argv[])
 {
-    lex_init();
-    fprintf(stdout, "/* These two include files are added by the preprocessor */\n#include <ecpgtype.h>\n#include <ecpglib.h>\n");
-    yyparse();
-    return(0);
+       char            c, out_option = 0;
+       int                     fnr;
+
+       while ((c = getopt(argc, argv, "o:")) != EOF)
+       {
+               switch (c)
+               {
+                       case 'o':
+                               yyout = fopen(optarg, "w");
+                               if (yyout == NULL)
+                                       perror(optarg);
+                               else
+                                       out_option = 1;
+                               break;
+                       default:
+                               usage(argv[0]);
+               }
+       }
+
+       /* after the options there must not be anything but filenames */
+       for (fnr = optind; fnr < argc; fnr++)
+       {
+               char       *filename,
+                                  *ptr2ext;
+
+               filename = malloc(strlen(argv[fnr]) + 2);
+               if (filename == NULL)
+               {
+                       perror("malloc");
+                       continue;
+               }
+
+               strcpy(filename, argv[fnr]);
+
+               ptr2ext = strrchr(filename, '.');
+               /* no extension or extension not equal .pgc */
+               if (ptr2ext == NULL || strcmp(ptr2ext, ".pgc") != 0) {
+                       ptr2ext = filename + strlen(filename);
+                       ptr2ext[0] = '.';
+               }
+
+               /* make extension = .c */
+               ptr2ext[1] = 'c';
+               ptr2ext[2] = '\0';
+
+               if (out_option == 0)            /* calculate the output name */
+               {
+                       yyout = fopen(filename, "w");
+                       if (yyout == NULL) {
+                               perror(filename);
+                               free(filename);
+                               continue;
+                       }
+               }
+
+               yyin = fopen(argv[fnr], "r");
+               if (yyin == NULL)
+               {
+                       perror(argv[fnr]);
+               }
+               else
+               {
+                       /* initialize lex */
+                       lex_init();
+
+                       /* we need two includes everytime */
+                       fprintf(yyout, "/* These two include files are added by the preprocessor */\n#include <ecpgtype.h>\n#include <ecpglib.h>\n");
+
+                       /* and parse the source */
+                       yyparse();
+
+                       fclose(yyin);
+                       if (out_option == 0)
+                               fclose (yyout);
+               }
+
+               free(filename);
+       }
+       return (0);
 }
index 4d84a81a19ecd501400a3ec6309ff6766c736205..f53c9a561333a15aa7ff88f544fc5b83804eccbd 100644 (file)
@@ -6,6 +6,9 @@
 #include "type.h"
 
 void yyerror(char *);
+extern FILE * yyout;
+extern char * yytext;
+extern int yyleng;
 
 /*
  * Handling of the variables.
@@ -117,16 +120,11 @@ dump_variables(struct arguments * list)
     dump_variables(list->next);
 
     /* Then the current element. */
-    ECPGdump_a_type(stdout, list->variable->name, list->variable->type);
+    ECPGdump_a_type(yyout, list->variable->name, list->variable->type);
 
     /* Then release the list element. */
     free(list);
 }
-
-
-extern FILE * yyout;
-extern char * yytext;
-extern int yyleng;
 %}
 
 %union {
@@ -149,7 +147,7 @@ extern int yyleng;
 %token <tagname> S_LONG S_SHORT S_INT S_CHAR S_FLOAT S_DOUBLE
 %token <tagname> '[' ']' ';' ','
 
-%type <type> type type_detailed varchar_type simple_type
+%type <type> type type_detailed varchar_type simple_type array_type
 %type <symbolname> symbol
 %type <tagname> maybe_storage_clause varchar_tag
 %type <type_enum> simple_tag
@@ -204,7 +202,8 @@ symbol : S_SYMBOL {
 
 type : maybe_storage_clause type_detailed { $<type>$ = $<type>2; };
 type_detailed : varchar_type { $<type>$ = $<type>1; }
-             | simple_type { $<type>$ = $<type>1; };
+             | simple_type { $<type>$ = $<type>1; }
+             | array_type {$<type>$ = $<type>1; };
 
 varchar_type : varchar_tag symbol index {
     fprintf(yyout, "struct varchar_%s { int len; char arr[%d]; } %s", $<symbolname>2, $<indexsize>3, $<symbolname>2);
@@ -221,6 +220,12 @@ simple_type : simple_tag symbol {
     $<type>$.typ = ECPGmake_simple_type($<type_enum>1);
 }
 
+array_type : simple_tag symbol index {
+    fprintf(yyout, "%s %s [%d]", ECPGtype_name($<type_enum>1), $<symbolname>2, $<indexsize>3);
+    $<type>$.name = $<symbolname>2;
+    $<type>$.typ = ECPGmake_array_type(ECPGmake_simple_type($<type_enum>1), $<indexsize>3);
+}
+
 simple_tag : S_CHAR { $<type_enum>$ = ECPGt_char; }
            | S_UNSIGNED S_CHAR { $<type_enum>$ = ECPGt_unsigned_char; }
           | S_SHORT { $<type_enum>$ = ECPGt_short; }
index 9302d55437940be1b9bbcd68c1454f6e030c400e..219fb54e4f82cbafd4ae9395299502ca1026330f 100644 (file)
@@ -1,4 +1,6 @@
 test2: test2.c
        gcc -g -I ../include -I ../../../libpq -o test2 test2.c ../lib/libecpg.a ../../../libpq/libpq.a -lcrypt
-test2.c: test2.qc
-       ../preproc/ecpg < test2.qc > test2.c
+test2.c: test2.pgc
+       ../preproc/ecpg test2.pgc
+clean:
+       /bin/rm test2 test2.c
index 5f22afb79901e4dc996b1ae5901b41957c497bbb..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644 (file)
@@ -1,52 +0,0 @@
-/* These two include files are added by the preprocessor */
-#include <ecpgtype.h>
-#include <ecpglib.h>
-#include "sqlca.h"
-
-#define       SQLCODE    sqlca.sqlcode
-
-void
-db_error (char *msg)
-{
-       sqlca.sqlerrm.sqlerrmc[sqlca.sqlerrm.sqlerrml] = '\0';
-       printf ("%s: db error %s\n", msg, sqlca.sqlerrm.sqlerrmc);
-       exit (1);
-}
-
-int
-main ()
-{
-/* exec sql begin declare section */
-
- struct varchar_text { int len; char arr[8]; } text;
-/* exec sql end declare section */
-
-
-       ECPGconnect("mm");
-       if (SQLCODE)
-               db_error ("connect");
-
-       ECPGdo(__LINE__, "declare cur cursor for select text from test ", ECPGt_EOIT, ECPGt_EORT );
-       if (SQLCODE) db_error ("declare");
-
-       
-       if (SQLCODE)
-               db_error ("open");
-
-       while (1) {
-               ECPGdo(__LINE__, "fetch in cur ", ECPGt_EOIT, ECPGt_varchar,&text,8,0,sizeof(struct varchar_text), ECPGt_EORT );
-               if (SQLCODE)
-                       break;
-               printf ("%8.8s\n", text.arr);
-       }
-
-       if (SQLCODE < 0)
-               db_error ("fetch");
-
-       ECPGdo(__LINE__, "close cur ", ECPGt_EOIT, ECPGt_EORT );
-       if (SQLCODE) db_error ("close");
-       ECPGcommit(__LINE__);
-       if (SQLCODE) db_error ("commit");
-
-       return (0);
-}
index 2652d3418d3cf5118feacbe6e346f3cc762054c9..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644 (file)
@@ -1,48 +0,0 @@
-exec sql include sqlca;
-
-#define       SQLCODE    sqlca.sqlcode
-
-void
-db_error (char *msg)
-{
-       sqlca.sqlerrm.sqlerrmc[sqlca.sqlerrm.sqlerrml] = '\0';
-       printf ("%s: db error %s\n", msg, sqlca.sqlerrm.sqlerrmc);
-       exit (1);
-}
-
-int
-main ()
-{
-exec sql begin declare section;
-varchar text[8];
-exec sql end declare section;
-
-       exec sql connect 'mm';
-       if (SQLCODE)
-               db_error ("connect");
-
-       exec sql declare cur cursor for 
-               select text from test;
-       if (SQLCODE) db_error ("declare");
-
-       exec sql open cur;
-       if (SQLCODE)
-               db_error ("open");
-
-       while (1) {
-               exec sql fetch in cur into :text;
-               if (SQLCODE)
-                       break;
-               printf ("%8.8s\n", text.arr);
-       }
-
-       if (SQLCODE < 0)
-               db_error ("fetch");
-
-       exec sql close cur;
-       if (SQLCODE) db_error ("close");
-       exec sql commit;
-       if (SQLCODE) db_error ("commit");
-
-       return (0);
-}