1 /* $Header: /cvsroot/pgsql/src/interfaces/ecpg/preproc/ecpg.c,v 1.78 2003/09/09 10:46:38 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"
22 auto_create_c = false,
23 system_includes = false,
24 force_indicator = true;
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",
42 printf(" -c automatically generate C code from embedded SQL code;\n"
43 " currently this works for EXEC SQL TYPE\n");
45 printf(" -d generate parser debug output\n");
47 printf(" -C <mode> set compatibility mode\n"
48 " mode may be one of\n"
50 " \"INFORMIX_SE\"\n");
51 printf(" -r <option> specify runtime behaviour\n"
52 " option may be only \"no_indicator\" at the moment\n");
53 printf(" -D SYMBOL define SYMBOL\n");
54 printf(" -I DIRECTORY search DIRECTORY for include files\n");
55 printf(" -o OUTFILE write result to OUTFILE\n");
56 printf(" -t turn on autocommit of transactions\n");
57 printf(" -i parse system include files as well\n");
58 printf(" --help show this help, then exit\n");
59 printf(" --version output version information, then exit\n");
60 printf("\nIf no output file is specified, the name is formed by adding .c to the\n"
61 "input file name, after stripping off .pgc if present.\n");
62 printf("\nReport bugs to <pgsql-bugs@postgresql.org>.\n");
66 add_include_path(char *path)
68 struct _include_path *ip = include_paths,
71 new = mm_alloc(sizeof(struct _include_path));
79 for (; ip->next != NULL; ip = ip->next);
85 add_preprocessor_define(char *define)
87 struct _defines *pd = defines;
89 *define_copy = mm_strdup(define);
91 defines = mm_alloc(sizeof(struct _defines));
94 ptr = strchr(define_copy, '=');
99 /* symbol gets a value */
100 for (tmp = ptr - 1; *tmp == ' '; tmp--);
102 defines->old = define_copy;
103 defines->new = ptr + 1;
107 defines->old = define_copy;
108 defines->new = mm_strdup("");
110 defines->pertinent = true;
115 main(int argc, char *const argv[])
121 struct _include_path *ip;
124 progname = get_progname(argv[0]);
128 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
133 else if (strcmp(argv[1], "--version") == 0)
135 printf("ecpg (PostgreSQL %s) %d.%d.%d\n", PG_VERSION,
136 MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL);
141 while ((c = getopt(argc, argv, "vcio:I:tD:dC:r:")) != -1)
146 if (strcmp(optarg, "-") == 0)
149 yyout = fopen(optarg, PG_BINARY_W);
157 add_include_path(optarg);
166 auto_create_c = true;
169 system_includes = true;
172 if (strncmp(optarg, "INFORMIX", strlen("INFORMIX")) == 0)
174 compat = (strcmp(optarg, "INFORMIX") == 0) ? ECPG_COMPAT_INFORMIX : ECPG_COMPAT_INFORMIX_SE;
175 /* system_includes = true; */
176 add_preprocessor_define("dec_t=numeric");
177 add_preprocessor_define("intrvl_t=interval");
178 add_preprocessor_define("dtime_t=timestamp");
182 fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
183 return ILLEGAL_OPTION;
187 if (strcmp(optarg, "no_indicator") == 0)
188 force_indicator = false;
191 fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
192 return ILLEGAL_OPTION;
196 add_preprocessor_define(optarg);
202 fprintf(stderr, "%s: parser debug support (-d) not available\n",
207 fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
208 return ILLEGAL_OPTION;
212 add_include_path(".");
213 add_include_path("/usr/local/include");
214 add_include_path(INCLUDE_PATH);
215 add_include_path("/usr/include");
219 fprintf(stderr, "%s, the PostgreSQL embedded C preprocessor, version %d.%d.%d\n",
220 progname, MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL);
221 fprintf(stderr, "exec sql include ... search starts here:\n");
222 for (ip = include_paths; ip != NULL; ip = ip->next)
223 fprintf(stderr, " %s\n", ip->path);
224 fprintf(stderr, "end of search list\n");
228 if (optind >= argc) /* no files specified */
230 fprintf(stderr, "%s: no input files specified\n", progname);
231 fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
232 return (ILLEGAL_OPTION);
236 /* after the options there must not be anything but filenames */
237 for (fnr = optind; fnr < argc; fnr++)
239 char *output_filename = NULL,
242 /* If argv[fnr] is "-" we have to read from stdin */
243 if (strcmp(argv[fnr], "-") == 0)
245 input_filename = mm_alloc(strlen("stdin") + 1);
246 strcpy(input_filename, "stdin");
251 input_filename = mm_alloc(strlen(argv[fnr]) + 5);
252 strcpy(input_filename, argv[fnr]);
254 /* take care of relative paths */
255 ptr2ext = last_path_separator(input_filename);
256 ptr2ext = (ptr2ext ? strrchr(ptr2ext, '.') : strrchr(input_filename, '.'));
261 ptr2ext = input_filename + strlen(input_filename);
263 /* no extension => add .pgc */
271 yyin = fopen(input_filename, PG_BINARY_R);
274 if (out_option == 0) /* calculate the output name */
276 if (strcmp(input_filename, "stdin") == 0)
280 output_filename = strdup(input_filename);
282 ptr2ext = strrchr(output_filename, '.');
283 /* make extension = .c */
287 yyout = fopen(output_filename, PG_BINARY_W);
290 perror(output_filename);
291 free(output_filename);
292 free(input_filename);
303 struct _defines *defptr;
304 struct typedefs *typeptr;
306 /* remove old cursor definitions if any are still there */
307 for (ptr = cur; ptr != NULL;)
309 struct cursor *this = ptr;
310 struct arguments *l1,
314 free(ptr->connection);
316 for (l1 = ptr->argsinsert; l1; l1 = l2)
321 for (l1 = ptr->argsresult; l1; l1 = l2)
331 /* remove non-pertinent old defines as well */
332 while (defines && !defines->pertinent)
335 defines = defines->next;
342 for (defptr = defines; defptr != NULL; defptr = defptr->next)
344 struct _defines *this = defptr->next;
346 if (this && !this->pertinent)
348 defptr->next = this->next;
356 /* and old typedefs */
357 for (typeptr = types; typeptr != NULL;)
359 struct typedefs *this = typeptr;
362 ECPGfree_struct_member(typeptr->struct_member_list);
364 typeptr = typeptr->next;
369 /* initialize whenever structures */
370 memset(&when_error, 0, sizeof(struct when));
371 memset(&when_nf, 0, sizeof(struct when));
372 memset(&when_warn, 0, sizeof(struct when));
374 /* and structure member lists */
375 memset(struct_member_list, 0, sizeof(struct_member_list));
377 /* and our variable counter for Informix compatibility */
378 ecpg_informix_var = 0;
380 /* finally the actual connection */
386 /* we need several includes */
387 fprintf(yyout, "/* Processed by ecpg (%d.%d.%d) */\n/* 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#line 1 \"%s\"\n", MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL, input_filename);
389 /* add some compatibility headers */
391 fprintf(yyout, "/* Needed for informix compatibility */\n#include <ecpg_informix.h>\n");
393 fprintf(yyout, "/* End of automatic include section */\n");
395 /* and parse the source */
398 if (yyin != NULL && yyin != stdin)
400 if (out_option == 0 && yyout != stdout)
405 free(output_filename);
407 free(input_filename);