From: Michael Meskes <meskes@topsystem.de>

I have implemented a better user interface (well part of) so you can use it
as expected. As usual there are some bug fixes. :-)
This commit is contained in:
Marc G. Fournier 1998-02-06 13:32:34 +00:00
parent fe0154ba87
commit 33c34920ff
5 changed files with 111 additions and 116 deletions

View File

@ -3,14 +3,102 @@
/* 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)
{
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);
fprintf(stderr, "Usage: %s: [ -o outout file name] file1 [file2] ...\n", progname);
}
int
main(int argc, char *const argv[])
{
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);
}

View 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; }

View 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

View 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);
}

View 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);
}