1 /* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/ecpg.c,v 1.101 2007/08/29 13:58:13 meskes 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,
22 regression_mode = false,
25 char *output_filename;
27 enum COMPAT_MODE compat = ECPG_COMPAT_PGSQL;
29 struct _include_path *include_paths = NULL;
30 struct cursor *cur = NULL;
31 struct typedefs *types = NULL;
32 struct _defines *defines = NULL;
35 help(const char *progname)
37 printf("%s is the PostgreSQL embedded SQL preprocessor for C programs.\n\n",
40 " %s [OPTION]... FILE...\n\n",
43 printf(" -c automatically generate C code from embedded SQL code;\n"
44 " currently this works for EXEC SQL TYPE\n");
45 printf(" -C MODE set compatibility mode;\n"
46 " MODE can be one of \"INFORMIX\", \"INFORMIX_SE\"\n");
48 printf(" -d generate parser debug output\n");
50 printf(" -D SYMBOL define SYMBOL\n");
51 printf(" -h parse a header file, this option includes option \"-c\"\n");
52 printf(" -i parse system include files as well\n");
53 printf(" -I DIRECTORY search DIRECTORY for include files\n");
54 printf(" -o OUTFILE write result to OUTFILE\n");
55 printf(" -r OPTION specify runtime behaviour;\n"
59 " \"questionmarks\"\n");
60 printf(" -t turn on autocommit of transactions\n");
61 printf(" --help show this help, then exit\n");
62 printf(" --regression run in regression testing mode\n");
63 printf(" --version output version information, then exit\n");
64 printf("\nIf no output file is specified, the name is formed by adding .c to the\n"
65 "input file name, after stripping off .pgc if present.\n");
66 printf("\nReport bugs to <pgsql-bugs@postgresql.org>.\n");
70 add_include_path(char *path)
72 struct _include_path *ip = include_paths,
75 new = mm_alloc(sizeof(struct _include_path));
83 for (; ip->next != NULL; ip = ip->next);
89 add_preprocessor_define(char *define)
91 struct _defines *pd = defines;
93 *define_copy = mm_strdup(define);
95 defines = mm_alloc(sizeof(struct _defines));
98 ptr = strchr(define_copy, '=');
103 /* symbol has a value */
104 for (tmp = ptr - 1; *tmp == ' '; tmp--);
106 defines->old = define_copy;
107 defines->new = ptr + 1;
111 defines->old = define_copy;
112 defines->new = mm_strdup("1");
114 defines->pertinent = true;
115 defines->used = NULL;
119 #define ECPG_GETOPT_LONG_HELP 1
120 #define ECPG_GETOPT_LONG_VERSION 2
121 #define ECPG_GETOPT_LONG_REGRESSION 3
123 main(int argc, char *const argv[])
125 static struct option ecpg_options[] = {
126 {"help", no_argument, NULL, ECPG_GETOPT_LONG_HELP},
127 {"version", no_argument, NULL, ECPG_GETOPT_LONG_VERSION},
128 {"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 progname = get_progname(argv[0]);
143 find_my_exec(argv[0], my_exec_path);
145 output_filename = NULL;
146 while ((c = getopt_long(argc, argv, "vcio:I:tD:dC:r:h?", ecpg_options, NULL)) != -1)
150 case ECPG_GETOPT_LONG_VERSION:
151 printf("ecpg (PostgreSQL %s) %d.%d.%d\n", PG_VERSION,
152 MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL);
154 case ECPG_GETOPT_LONG_HELP:
158 * -? is an alternative spelling of --help. However it is also
159 * returned by getopt_long for unknown options. We can distinguish
160 * both cases by means of the optopt variable which is set to 0 if
161 * it was really -? and not an unknown option character.
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));
422 /* and our variable counter for Informix compatibility */
423 ecpg_informix_var = 0;
425 /* finally the actual connection */
431 /* we need several includes */
432 /* but not if we are in header mode */
434 fprintf(yyout, "/* Processed by ecpg (regression mode) */\n");
436 fprintf(yyout, "/* Processed by ecpg (%d.%d.%d) */\n", MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL);
438 if (header_mode == false)
440 fprintf(yyout, "/* These include files are added by the preprocessor */\n#include <ecpgtype.h>\n#include <ecpglib.h>\n#include <ecpgerrno.h>\n#include <sqlca.h>\n");
442 /* add some compatibility headers */
444 fprintf(yyout, "/* Needed for informix compatibility */\n#include <ecpg_informix.h>\n");
446 fprintf(yyout, "/* End of automatic include section */\n");
450 fprintf(yyout, "#define ECPGdebug(X,Y) ECPGdebug((X)+100,(Y))\n");
452 output_line_number();
454 /* and parse the source */
457 /* check if all cursors were indeed opened */
458 for (ptr = cur; ptr != NULL;)
465 * Does not really make sense to declare a cursor but
468 snprintf(errortext, sizeof(errortext), "cursor \"%s\" has been declared but not opened\n", ptr->name);
469 mmerror(PARSE_ERROR, ET_WARNING, errortext);
474 if (yyin != NULL && yyin != stdin)
476 if (out_option == 0 && yyout != stdout)
480 if (output_filename && out_option == 0)
481 free(output_filename);
483 free(input_filename);