1 /* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/ecpg.c,v 1.105 2008/05/16 15:20:04 petere Exp $ */
3 /* New main for ecpg, the PostgreSQL embedded SQL precompiler. */
4 /* (C) Michael Meskes <meskes@postgresql.org> Feb 5th, 1998 */
5 /* Placed under the same license as PostgreSQL */
7 #include "postgres_fe.h"
11 #include "getopt_long.h"
17 auto_create_c = false,
18 system_includes = false,
19 force_indicator = true,
20 questionmarks = false,
21 regression_mode = false,
24 char *output_filename;
26 enum COMPAT_MODE compat = ECPG_COMPAT_PGSQL;
28 struct _include_path *include_paths = NULL;
29 struct cursor *cur = NULL;
30 struct typedefs *types = NULL;
31 struct _defines *defines = NULL;
34 help(const char *progname)
36 printf(_("%s is the PostgreSQL embedded SQL preprocessor for C programs.\n\n"),
39 " %s [OPTION]... FILE...\n\n"),
41 printf(_("Options:\n"));
42 printf(_(" -c automatically generate C code from embedded SQL code;\n"
43 " currently this works for EXEC SQL TYPE\n"));
44 printf(_(" -C MODE set compatibility mode;\n"
45 " MODE can be one of \"INFORMIX\", \"INFORMIX_SE\"\n"));
47 printf(_(" -d generate parser debug output\n"));
49 printf(_(" -D SYMBOL define SYMBOL\n"));
50 printf(_(" -h parse a header file, this option includes option \"-c\"\n"));
51 printf(_(" -i parse system include files as well\n"));
52 printf(_(" -I DIRECTORY search DIRECTORY for include files\n"));
53 printf(_(" -o OUTFILE write result to OUTFILE\n"));
54 printf(_(" -r OPTION specify runtime behaviour;\n"
58 " \"questionmarks\"\n"));
59 printf(_(" -t turn on autocommit of transactions\n"));
60 printf(_(" --help show this help, then exit\n"));
61 printf(_(" --regression run in regression testing mode\n"));
62 printf(_(" --version output version information, then exit\n"));
63 printf(_("\nIf no output file is specified, the name is formed by adding .c to the\n"
64 "input file name, after stripping off .pgc if present.\n"));
65 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
69 add_include_path(char *path)
71 struct _include_path *ip = include_paths,
74 new = mm_alloc(sizeof(struct _include_path));
82 for (; ip->next != NULL; ip = ip->next);
88 add_preprocessor_define(char *define)
90 struct _defines *pd = defines;
92 *define_copy = mm_strdup(define);
94 defines = mm_alloc(sizeof(struct _defines));
97 ptr = strchr(define_copy, '=');
102 /* symbol has a value */
103 for (tmp = ptr - 1; *tmp == ' '; tmp--);
105 defines->old = define_copy;
106 defines->new = ptr + 1;
110 defines->old = define_copy;
111 defines->new = mm_strdup("1");
113 defines->pertinent = true;
114 defines->used = NULL;
118 #define ECPG_GETOPT_LONG_HELP 1
119 #define ECPG_GETOPT_LONG_VERSION 2
120 #define ECPG_GETOPT_LONG_REGRESSION 3
122 main(int argc, char *const argv[])
124 static struct option ecpg_options[] = {
125 {"help", no_argument, NULL, ECPG_GETOPT_LONG_HELP},
126 {"version", no_argument, NULL, ECPG_GETOPT_LONG_VERSION},
127 {"regression", no_argument, NULL, ECPG_GETOPT_LONG_REGRESSION},
136 struct _include_path *ip;
137 const char *progname;
138 char my_exec_path[MAXPGPATH];
139 char include_path[MAXPGPATH];
141 set_pglocale_pgservice(argv[0], "ecpg");
143 progname = get_progname(argv[0]);
145 find_my_exec(argv[0], my_exec_path);
147 output_filename = NULL;
148 while ((c = getopt_long(argc, argv, "vcio:I:tD:dC:r:h?", ecpg_options, NULL)) != -1)
152 case ECPG_GETOPT_LONG_VERSION:
153 printf("ecpg (PostgreSQL %s) %d.%d.%d\n", PG_VERSION,
154 MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL);
156 case ECPG_GETOPT_LONG_HELP:
161 * -? is an alternative spelling of --help. However it is also
162 * returned by getopt_long for unknown options. We can
163 * distinguish both cases by means of the optopt variable
164 * which is set to 0 if it was really -? and not an unknown
174 case ECPG_GETOPT_LONG_REGRESSION:
175 regression_mode = true;
178 output_filename = optarg;
179 if (strcmp(output_filename, "-") == 0)
182 yyout = fopen(output_filename, PG_BINARY_W);
186 fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
187 progname, output_filename, strerror(errno));
188 output_filename = NULL;
194 add_include_path(optarg);
204 /* this must include "-c" to make sense */
205 /* so do not place a "break;" here */
207 auto_create_c = true;
210 system_includes = true;
213 if (strncmp(optarg, "INFORMIX", strlen("INFORMIX")) == 0)
215 char pkginclude_path[MAXPGPATH];
216 char informix_path[MAXPGPATH];
218 compat = (strcmp(optarg, "INFORMIX") == 0) ? ECPG_COMPAT_INFORMIX : ECPG_COMPAT_INFORMIX_SE;
219 get_pkginclude_path(my_exec_path, pkginclude_path);
220 snprintf(informix_path, MAXPGPATH, "%s/informix/esql", pkginclude_path);
221 add_include_path(informix_path);
225 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), argv[0]);
226 return ILLEGAL_OPTION;
230 if (strcmp(optarg, "no_indicator") == 0)
231 force_indicator = false;
232 else if (strcmp(optarg, "prepare") == 0)
234 else if (strcmp(optarg, "questionmarks") == 0)
235 questionmarks = true;
238 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), argv[0]);
239 return ILLEGAL_OPTION;
243 add_preprocessor_define(optarg);
249 fprintf(stderr, _("%s: parser debug support (-d) not available\n"),
254 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), argv[0]);
255 return ILLEGAL_OPTION;
259 add_include_path(".");
260 add_include_path("/usr/local/include");
261 get_include_path(my_exec_path, include_path);
262 add_include_path(include_path);
263 add_include_path("/usr/include");
267 fprintf(stderr, _("%s, the PostgreSQL embedded C preprocessor, version %d.%d.%d\n"),
268 progname, MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL);
269 fprintf(stderr, _("exec sql include ... search starts here:\n"));
270 for (ip = include_paths; ip != NULL; ip = ip->next)
271 fprintf(stderr, " %s\n", ip->path);
272 fprintf(stderr, _("end of search list\n"));
276 if (optind >= argc) /* no files specified */
278 fprintf(stderr, _("%s: no input files specified\n"), progname);
279 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), argv[0]);
280 return (ILLEGAL_OPTION);
284 /* after the options there must not be anything but filenames */
285 for (fnr = optind; fnr < argc; fnr++)
289 /* If argv[fnr] is "-" we have to read from stdin */
290 if (strcmp(argv[fnr], "-") == 0)
292 input_filename = mm_alloc(strlen("stdin") + 1);
293 strcpy(input_filename, "stdin");
298 input_filename = mm_alloc(strlen(argv[fnr]) + 5);
299 strcpy(input_filename, argv[fnr]);
301 /* take care of relative paths */
302 ptr2ext = last_dir_separator(input_filename);
303 ptr2ext = (ptr2ext ? strrchr(ptr2ext, '.') : strrchr(input_filename, '.'));
308 ptr2ext = input_filename + strlen(input_filename);
310 /* no extension => add .pgc or .pgh */
314 ptr2ext[3] = (header_mode == true) ? 'h' : 'c';
318 yyin = fopen(input_filename, PG_BINARY_R);
321 if (out_option == 0) /* calculate the output name */
323 if (strcmp(input_filename, "stdin") == 0)
327 output_filename = strdup(input_filename);
329 ptr2ext = strrchr(output_filename, '.');
330 /* make extension = .c resp. .h */
331 ptr2ext[1] = (header_mode == true) ? 'h' : 'c';
334 yyout = fopen(output_filename, PG_BINARY_W);
337 fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
338 progname, output_filename, strerror(errno));
339 free(output_filename);
340 free(input_filename);
347 fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
348 progname, argv[fnr], strerror(errno));
352 struct _defines *defptr;
353 struct typedefs *typeptr;
355 /* remove old cursor definitions if any are still there */
356 for (ptr = cur; ptr != NULL;)
358 struct cursor *this = ptr;
359 struct arguments *l1,
363 free(ptr->connection);
365 for (l1 = ptr->argsinsert; l1; l1 = l2)
370 for (l1 = ptr->argsresult; l1; l1 = l2)
380 /* remove non-pertinent old defines as well */
381 while (defines && !defines->pertinent)
384 defines = defines->next;
391 for (defptr = defines; defptr != NULL; defptr = defptr->next)
393 struct _defines *this = defptr->next;
395 if (this && !this->pertinent)
397 defptr->next = this->next;
405 /* and old typedefs */
406 for (typeptr = types; typeptr != NULL;)
408 struct typedefs *this = typeptr;
411 ECPGfree_struct_member(typeptr->struct_member_list);
413 typeptr = typeptr->next;
418 /* initialize whenever structures */
419 memset(&when_error, 0, sizeof(struct when));
420 memset(&when_nf, 0, sizeof(struct when));
421 memset(&when_warn, 0, sizeof(struct when));
423 /* and structure member lists */
424 memset(struct_member_list, 0, sizeof(struct_member_list));
426 /* and our variable counter for Informix compatibility */
427 ecpg_informix_var = 0;
429 /* finally the actual connection */
435 /* we need several includes */
436 /* but not if we are in header mode */
438 fprintf(yyout, "/* Processed by ecpg (regression mode) */\n");
440 fprintf(yyout, "/* Processed by ecpg (%d.%d.%d) */\n", MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL);
442 if (header_mode == false)
444 fprintf(yyout, "/* These include files are added by the preprocessor */\n#include <ecpglib.h>\n#include <ecpgerrno.h>\n#include <sqlca.h>\n");
446 /* add some compatibility headers */
448 fprintf(yyout, "/* Needed for informix compatibility */\n#include <ecpg_informix.h>\n");
450 fprintf(yyout, "/* End of automatic include section */\n");
454 fprintf(yyout, "#define ECPGdebug(X,Y) ECPGdebug((X)+100,(Y))\n");
456 output_line_number();
458 /* and parse the source */
461 /* check if all cursors were indeed opened */
462 for (ptr = cur; ptr != NULL;)
469 * Does not really make sense to declare a cursor but
472 snprintf(errortext, sizeof(errortext), _("cursor \"%s\" has been declared but not opened\n"), ptr->name);
473 mmerror(PARSE_ERROR, ET_WARNING, errortext);
478 if (yyin != NULL && yyin != stdin)
480 if (out_option == 0 && yyout != stdout)
484 if (output_filename && out_option == 0)
485 free(output_filename);
487 free(input_filename);