Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 33c3492

Browse files
committed
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. :-)
1 parent fe0154b commit 33c3492

File tree

5 files changed

+111
-116
lines changed

5 files changed

+111
-116
lines changed

src/interfaces/ecpg/src/preproc/ecpg.c

Lines changed: 94 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,102 @@
33
/* Placed under the same copyright as PostgresSQL */
44

55
#include <stdio.h>
6+
#include <getopt.h>
7+
#include <stdlib.h>
8+
#include <strings.h>
69

710
extern void lex_init(void);
8-
int yyparse (void);
11+
extern FILE *yyin,
12+
*yyout;
913

10-
int main(int argc, char *argv[])
14+
int yyparse(void);
15+
16+
static void
17+
usage(char *progname)
18+
{
19+
fprintf(stderr, "Usage: %s: [ -o outout file name] file1 [file2] ...\n", progname);
20+
}
21+
22+
int
23+
main(int argc, char *const argv[])
1124
{
12-
lex_init();
13-
fprintf(stdout, "/* These two include files are added by the preprocessor */\n#include <ecpgtype.h>\n#include <ecpglib.h>\n");
14-
yyparse();
15-
return(0);
25+
char c, out_option = 0;
26+
int fnr;
27+
28+
while ((c = getopt(argc, argv, "o:")) != EOF)
29+
{
30+
switch (c)
31+
{
32+
case 'o':
33+
yyout = fopen(optarg, "w");
34+
if (yyout == NULL)
35+
perror(optarg);
36+
else
37+
out_option = 1;
38+
break;
39+
default:
40+
usage(argv[0]);
41+
}
42+
}
43+
44+
/* after the options there must not be anything but filenames */
45+
for (fnr = optind; fnr < argc; fnr++)
46+
{
47+
char *filename,
48+
*ptr2ext;
49+
50+
filename = malloc(strlen(argv[fnr]) + 2);
51+
if (filename == NULL)
52+
{
53+
perror("malloc");
54+
continue;
55+
}
56+
57+
strcpy(filename, argv[fnr]);
58+
59+
ptr2ext = strrchr(filename, '.');
60+
/* no extension or extension not equal .pgc */
61+
if (ptr2ext == NULL || strcmp(ptr2ext, ".pgc") != 0) {
62+
ptr2ext = filename + strlen(filename);
63+
ptr2ext[0] = '.';
64+
}
65+
66+
/* make extension = .c */
67+
ptr2ext[1] = 'c';
68+
ptr2ext[2] = '\0';
69+
70+
if (out_option == 0) /* calculate the output name */
71+
{
72+
yyout = fopen(filename, "w");
73+
if (yyout == NULL) {
74+
perror(filename);
75+
free(filename);
76+
continue;
77+
}
78+
}
79+
80+
yyin = fopen(argv[fnr], "r");
81+
if (yyin == NULL)
82+
{
83+
perror(argv[fnr]);
84+
}
85+
else
86+
{
87+
/* initialize lex */
88+
lex_init();
89+
90+
/* we need two includes everytime */
91+
fprintf(yyout, "/* These two include files are added by the preprocessor */\n#include <ecpgtype.h>\n#include <ecpglib.h>\n");
92+
93+
/* and parse the source */
94+
yyparse();
95+
96+
fclose(yyin);
97+
if (out_option == 0)
98+
fclose (yyout);
99+
}
100+
101+
free(filename);
102+
}
103+
return (0);
16104
}

src/interfaces/ecpg/src/preproc/preproc.y

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
#include "type.h"
77

88
void yyerror(char *);
9+
extern FILE * yyout;
10+
extern char * yytext;
11+
extern int yyleng;
912

1013
/*
1114
* Handling of the variables.
@@ -117,16 +120,11 @@ dump_variables(struct arguments * list)
117120
dump_variables(list->next);
118121

119122
/* Then the current element. */
120-
ECPGdump_a_type(stdout, list->variable->name, list->variable->type);
123+
ECPGdump_a_type(yyout, list->variable->name, list->variable->type);
121124

122125
/* Then release the list element. */
123126
free(list);
124127
}
125-
126-
127-
extern FILE * yyout;
128-
extern char * yytext;
129-
extern int yyleng;
130128
%}
131129

132130
%union {
@@ -149,7 +147,7 @@ extern int yyleng;
149147
%token <tagname> S_LONG S_SHORT S_INT S_CHAR S_FLOAT S_DOUBLE
150148
%token <tagname> '[' ']' ';' ','
151149

152-
%type <type> type type_detailed varchar_type simple_type
150+
%type <type> type type_detailed varchar_type simple_type array_type
153151
%type <symbolname> symbol
154152
%type <tagname> maybe_storage_clause varchar_tag
155153
%type <type_enum> simple_tag
@@ -204,7 +202,8 @@ symbol : S_SYMBOL {
204202

205203
type : maybe_storage_clause type_detailed { $<type>$ = $<type>2; };
206204
type_detailed : varchar_type { $<type>$ = $<type>1; }
207-
| simple_type { $<type>$ = $<type>1; };
205+
| simple_type { $<type>$ = $<type>1; }
206+
| array_type {$<type>$ = $<type>1; };
208207

209208
varchar_type : varchar_tag symbol index {
210209
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 {
221220
$<type>$.typ = ECPGmake_simple_type($<type_enum>1);
222221
}
223222

223+
array_type : simple_tag symbol index {
224+
fprintf(yyout, "%s %s [%d]", ECPGtype_name($<type_enum>1), $<symbolname>2, $<indexsize>3);
225+
$<type>$.name = $<symbolname>2;
226+
$<type>$.typ = ECPGmake_array_type(ECPGmake_simple_type($<type_enum>1), $<indexsize>3);
227+
}
228+
224229
simple_tag : S_CHAR { $<type_enum>$ = ECPGt_char; }
225230
| S_UNSIGNED S_CHAR { $<type_enum>$ = ECPGt_unsigned_char; }
226231
| S_SHORT { $<type_enum>$ = ECPGt_short; }

src/interfaces/ecpg/src/test/Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
test2: test2.c
22
gcc -g -I ../include -I ../../../libpq -o test2 test2.c ../lib/libecpg.a ../../../libpq/libpq.a -lcrypt
3-
test2.c: test2.qc
4-
../preproc/ecpg < test2.qc > test2.c
3+
test2.c: test2.pgc
4+
../preproc/ecpg test2.pgc
5+
clean:
6+
/bin/rm test2 test2.c

src/interfaces/ecpg/src/test/test2.c

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +0,0 @@
1-
/* These two include files are added by the preprocessor */
2-
#include <ecpgtype.h>
3-
#include <ecpglib.h>
4-
#include "sqlca.h"
5-
6-
#define SQLCODE sqlca.sqlcode
7-
8-
void
9-
db_error (char *msg)
10-
{
11-
sqlca.sqlerrm.sqlerrmc[sqlca.sqlerrm.sqlerrml] = '\0';
12-
printf ("%s: db error %s\n", msg, sqlca.sqlerrm.sqlerrmc);
13-
exit (1);
14-
}
15-
16-
int
17-
main ()
18-
{
19-
/* exec sql begin declare section */
20-
21-
struct varchar_text { int len; char arr[8]; } text;
22-
/* exec sql end declare section */
23-
24-
25-
ECPGconnect("mm");
26-
if (SQLCODE)
27-
db_error ("connect");
28-
29-
ECPGdo(__LINE__, "declare cur cursor for select text from test ", ECPGt_EOIT, ECPGt_EORT );
30-
if (SQLCODE) db_error ("declare");
31-
32-
33-
if (SQLCODE)
34-
db_error ("open");
35-
36-
while (1) {
37-
ECPGdo(__LINE__, "fetch in cur ", ECPGt_EOIT, ECPGt_varchar,&text,8,0,sizeof(struct varchar_text), ECPGt_EORT );
38-
if (SQLCODE)
39-
break;
40-
printf ("%8.8s\n", text.arr);
41-
}
42-
43-
if (SQLCODE < 0)
44-
db_error ("fetch");
45-
46-
ECPGdo(__LINE__, "close cur ", ECPGt_EOIT, ECPGt_EORT );
47-
if (SQLCODE) db_error ("close");
48-
ECPGcommit(__LINE__);
49-
if (SQLCODE) db_error ("commit");
50-
51-
return (0);
52-
}

src/interfaces/ecpg/src/test/test2.qc

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +0,0 @@
1-
exec sql include sqlca;
2-
3-
#define SQLCODE sqlca.sqlcode
4-
5-
void
6-
db_error (char *msg)
7-
{
8-
sqlca.sqlerrm.sqlerrmc[sqlca.sqlerrm.sqlerrml] = '\0';
9-
printf ("%s: db error %s\n", msg, sqlca.sqlerrm.sqlerrmc);
10-
exit (1);
11-
}
12-
13-
int
14-
main ()
15-
{
16-
exec sql begin declare section;
17-
varchar text[8];
18-
exec sql end declare section;
19-
20-
exec sql connect 'mm';
21-
if (SQLCODE)
22-
db_error ("connect");
23-
24-
exec sql declare cur cursor for
25-
select text from test;
26-
if (SQLCODE) db_error ("declare");
27-
28-
exec sql open cur;
29-
if (SQLCODE)
30-
db_error ("open");
31-
32-
while (1) {
33-
exec sql fetch in cur into :text;
34-
if (SQLCODE)
35-
break;
36-
printf ("%8.8s\n", text.arr);
37-
}
38-
39-
if (SQLCODE < 0)
40-
db_error ("fetch");
41-
42-
exec sql close cur;
43-
if (SQLCODE) db_error ("close");
44-
exec sql commit;
45-
if (SQLCODE) db_error ("commit");
46-
47-
return (0);
48-
}

0 commit comments

Comments
 (0)