1
/* src/interfaces/ecpg/preproc/ecpg.c */
3
/* Main for ecpg, the PostgreSQL embedded SQL precompiler. */
4
/* Copyright (c) 1996-2011, PostgreSQL Global Development Group */
6
#include "postgres_fe.h"
10
#include "getopt_long.h"
15
bool autocommit = false,
16
auto_create_c = false,
17
system_includes = false,
18
force_indicator = true,
19
questionmarks = false,
20
regression_mode = false,
23
char *output_filename;
25
enum COMPAT_MODE compat = ECPG_COMPAT_PGSQL;
27
struct _include_path *include_paths = NULL;
28
struct cursor *cur = NULL;
29
struct typedefs *types = NULL;
30
struct _defines *defines = NULL;
33
help(const char *progname)
35
printf(_("%s is the PostgreSQL embedded SQL preprocessor for C programs.\n\n"),
38
" %s [OPTION]... FILE...\n\n"),
40
printf(_("Options:\n"));
41
printf(_(" -c automatically generate C code from embedded SQL code;\n"
42
" this affects EXEC SQL TYPE\n"));
43
printf(_(" -C MODE set compatibility mode; MODE can be one of\n"
44
" \"INFORMIX\", \"INFORMIX_SE\"\n"));
46
printf(_(" -d generate parser debug output\n"));
48
printf(_(" -D SYMBOL define SYMBOL\n"));
49
printf(_(" -h parse a header file, this option includes option \"-c\"\n"));
50
printf(_(" -i parse system include files as well\n"));
51
printf(_(" -I DIRECTORY search DIRECTORY for include files\n"));
52
printf(_(" -o OUTFILE write result to OUTFILE\n"));
53
printf(_(" -r OPTION specify run-time behavior; OPTION can be:\n"
54
" \"no_indicator\", \"prepare\", \"questionmarks\"\n"));
55
printf(_(" --regression run in regression testing mode\n"));
56
printf(_(" -t turn on autocommit of transactions\n"));
57
printf(_(" --help show this help, then exit\n"));
58
printf(_(" --version output version information, then exit\n"));
59
printf(_("\nIf no output file is specified, the name is formed by adding .c to the\n"
60
"input file name, after stripping off .pgc if present.\n"));
61
printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
65
add_include_path(char *path)
67
struct _include_path *ip = include_paths,
70
new = mm_alloc(sizeof(struct _include_path));
78
for (; ip->next != NULL; ip = ip->next);
84
add_preprocessor_define(char *define)
86
struct _defines *pd = defines;
88
*define_copy = mm_strdup(define);
90
defines = mm_alloc(sizeof(struct _defines));
93
ptr = strchr(define_copy, '=');
98
/* symbol has a value */
99
for (tmp = ptr - 1; *tmp == ' '; tmp--);
101
defines->old = define_copy;
102
defines->new = ptr + 1;
106
defines->old = define_copy;
107
defines->new = mm_strdup("1");
109
defines->pertinent = true;
110
defines->used = NULL;
114
#define ECPG_GETOPT_LONG_HELP 1
115
#define ECPG_GETOPT_LONG_VERSION 2
116
#define ECPG_GETOPT_LONG_REGRESSION 3
118
main(int argc, char *const argv[])
120
static struct option ecpg_options[] = {
121
{"help", no_argument, NULL, ECPG_GETOPT_LONG_HELP},
122
{"version", no_argument, NULL, ECPG_GETOPT_LONG_VERSION},
123
{"regression", no_argument, NULL, ECPG_GETOPT_LONG_REGRESSION},
130
bool verbose = false,
132
struct _include_path *ip;
133
const char *progname;
134
char my_exec_path[MAXPGPATH];
135
char include_path[MAXPGPATH];
137
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("ecpg"));
139
progname = get_progname(argv[0]);
141
find_my_exec(argv[0], my_exec_path);
143
output_filename = NULL;
144
while ((c = getopt_long(argc, argv, "vcio:I:tD:dC:r:h?", ecpg_options, NULL)) != -1)
148
case ECPG_GETOPT_LONG_VERSION:
149
printf("ecpg (PostgreSQL %s) %d.%d.%d\n", PG_VERSION,
150
MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL);
152
case ECPG_GETOPT_LONG_HELP:
157
* -? is an alternative spelling of --help. However it is also
158
* returned by getopt_long for unknown options. We can
159
* distinguish both cases by means of the optopt variable
160
* which is set to 0 if it was really -? and not an unknown
170
case ECPG_GETOPT_LONG_REGRESSION:
171
regression_mode = true;
174
output_filename = optarg;
175
if (strcmp(output_filename, "-") == 0)
178
yyout = fopen(output_filename, PG_BINARY_W);
182
fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
183
progname, output_filename, strerror(errno));
184
output_filename = NULL;
190
add_include_path(optarg);
200
/* this must include "-c" to make sense */
201
/* so do not place a "break;" here */
203
auto_create_c = true;
206
system_includes = true;
209
if (strncmp(optarg, "INFORMIX", strlen("INFORMIX")) == 0)
211
char pkginclude_path[MAXPGPATH];
212
char informix_path[MAXPGPATH];
214
compat = (strcmp(optarg, "INFORMIX") == 0) ? ECPG_COMPAT_INFORMIX : ECPG_COMPAT_INFORMIX_SE;
215
get_pkginclude_path(my_exec_path, pkginclude_path);
216
snprintf(informix_path, MAXPGPATH, "%s/informix/esql", pkginclude_path);
217
add_include_path(informix_path);
221
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), argv[0]);
222
return ILLEGAL_OPTION;
226
if (strcmp(optarg, "no_indicator") == 0)
227
force_indicator = false;
228
else if (strcmp(optarg, "prepare") == 0)
230
else if (strcmp(optarg, "questionmarks") == 0)
231
questionmarks = true;
234
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), argv[0]);
235
return ILLEGAL_OPTION;
239
add_preprocessor_define(optarg);
245
fprintf(stderr, _("%s: parser debug support (-d) not available\n"),
250
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), argv[0]);
251
return ILLEGAL_OPTION;
255
add_include_path(".");
256
add_include_path("/usr/local/include");
257
get_include_path(my_exec_path, include_path);
258
add_include_path(include_path);
259
add_include_path("/usr/include");
263
fprintf(stderr, _("%s, the PostgreSQL embedded C preprocessor, version %d.%d.%d\n"),
264
progname, MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL);
265
fprintf(stderr, _("EXEC SQL INCLUDE ... search starts here:\n"));
266
for (ip = include_paths; ip != NULL; ip = ip->next)
267
fprintf(stderr, " %s\n", ip->path);
268
fprintf(stderr, _("end of search list\n"));
272
if (optind >= argc) /* no files specified */
274
fprintf(stderr, _("%s: no input files specified\n"), progname);
275
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), argv[0]);
276
return (ILLEGAL_OPTION);
280
/* after the options there must not be anything but filenames */
281
for (fnr = optind; fnr < argc; fnr++)
285
/* If argv[fnr] is "-" we have to read from stdin */
286
if (strcmp(argv[fnr], "-") == 0)
288
input_filename = mm_alloc(strlen("stdin") + 1);
289
strcpy(input_filename, "stdin");
294
input_filename = mm_alloc(strlen(argv[fnr]) + 5);
295
strcpy(input_filename, argv[fnr]);
297
/* take care of relative paths */
298
ptr2ext = last_dir_separator(input_filename);
299
ptr2ext = (ptr2ext ? strrchr(ptr2ext, '.') : strrchr(input_filename, '.'));
304
ptr2ext = input_filename + strlen(input_filename);
306
/* no extension => add .pgc or .pgh */
310
ptr2ext[3] = (header_mode == true) ? 'h' : 'c';
314
yyin = fopen(input_filename, PG_BINARY_R);
317
if (out_option == 0) /* calculate the output name */
319
if (strcmp(input_filename, "stdin") == 0)
323
output_filename = strdup(input_filename);
325
ptr2ext = strrchr(output_filename, '.');
326
/* make extension = .c resp. .h */
327
ptr2ext[1] = (header_mode == true) ? 'h' : 'c';
330
yyout = fopen(output_filename, PG_BINARY_W);
333
fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
334
progname, output_filename, strerror(errno));
335
free(output_filename);
336
free(input_filename);
343
fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
344
progname, argv[fnr], strerror(errno));
348
struct _defines *defptr;
349
struct typedefs *typeptr;
351
/* remove old cursor definitions if any are still there */
352
for (ptr = cur; ptr != NULL;)
354
struct cursor *this = ptr;
355
struct arguments *l1,
359
free(ptr->connection);
361
for (l1 = ptr->argsinsert; l1; l1 = l2)
366
for (l1 = ptr->argsresult; l1; l1 = l2)
376
/* remove non-pertinent old defines as well */
377
while (defines && !defines->pertinent)
380
defines = defines->next;
387
for (defptr = defines; defptr != NULL; defptr = defptr->next)
389
struct _defines *this = defptr->next;
391
if (this && !this->pertinent)
393
defptr->next = this->next;
401
/* and old typedefs */
402
for (typeptr = types; typeptr != NULL;)
404
struct typedefs *this = typeptr;
407
ECPGfree_struct_member(typeptr->struct_member_list);
409
typeptr = typeptr->next;
414
/* initialize whenever structures */
415
memset(&when_error, 0, sizeof(struct when));
416
memset(&when_nf, 0, sizeof(struct when));
417
memset(&when_warn, 0, sizeof(struct when));
419
/* and structure member lists */
420
memset(struct_member_list, 0, sizeof(struct_member_list));
423
* and our variable counter for out of scope cursors'
426
ecpg_internal_var = 0;
428
/* finally the actual connection */
434
/* we need several includes */
435
/* but not if we are in header mode */
437
fprintf(yyout, "/* Processed by ecpg (regression mode) */\n");
439
fprintf(yyout, "/* Processed by ecpg (%d.%d.%d) */\n", MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL);
441
if (header_mode == false)
443
fprintf(yyout, "/* These include files are added by the preprocessor */\n#include <ecpglib.h>\n#include <ecpgerrno.h>\n#include <sqlca.h>\n");
445
/* add some compatibility headers */
447
fprintf(yyout, "/* Needed for informix compatibility */\n#include <ecpg_informix.h>\n");
449
fprintf(yyout, "/* End of automatic include section */\n");
453
fprintf(yyout, "#define ECPGdebug(X,Y) ECPGdebug((X)+100,(Y))\n");
455
output_line_number();
457
/* and parse the source */
461
* Check whether all cursors were indeed opened. It does not
462
* really make sense to declare a cursor but not open it.
464
for (ptr = cur; ptr != NULL; ptr = ptr->next)
466
mmerror(PARSE_ERROR, ET_WARNING, "cursor \"%s\" has been declared but not opened", ptr->name);
468
if (yyin != NULL && yyin != stdin)
470
if (out_option == 0 && yyout != stdout)
474
* If there was an error, delete the output file.
478
if (strcmp(output_filename, "-") != 0 && unlink(output_filename) != 0)
479
fprintf(stderr, _("could not remove output file \"%s\"\n"), output_filename);
483
if (output_filename && out_option == 0)
484
free(output_filename);
486
free(input_filename);