/* 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);
}
#include "type.h"
void yyerror(char *);
+extern FILE * yyout;
+extern char * yytext;
+extern int yyleng;
/*
* Handling of the variables.
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 {
%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
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);
$<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; }
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
-/* 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);
-}
-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);
-}