1 /* $Header: /cvsroot/pgsql/src/interfaces/ecpg/preproc/ecpg.c,v 1.59 2003/02/13 13:11:52 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 PostgresSQL */
7 #include "postgres_fe.h"
21 auto_create_c = false;
23 enum COMPAT_MODE compat = ECPG_COMPAT_PGSQL;
25 struct _include_path *include_paths = NULL;
26 struct cursor *cur = NULL;
27 struct typedefs *types = NULL;
28 struct _defines *defines = NULL;
31 help(const char *progname)
33 printf("%s is the PostgreSQL embedded SQL preprocessor for C programs.\n\n",
36 " %s [OPTION]... FILE...\n\n",
39 printf(" -c automatically generate C code from embedded SQL code;\n"
40 " currently this works for EXEC SQL TYPE\n");
42 printf(" -d generate parser debug output\n");
44 printf(" -C <mode> set compatibility mode\n"
45 " mode may be INFORMIX only at the moment\n");
46 printf(" -D SYMBOL define SYMBOL\n");
47 printf(" -I DIRECTORY search DIRECTORY for include files\n");
48 printf(" -o OUTFILE write result to OUTFILE\n");
49 printf(" -t turn on autocommit of transactions\n");
50 printf(" --help show this help, then exit\n");
51 printf(" --version output version information, then exit\n");
52 printf("\nIf no output file is specified, the name is formed by adding .c to the\n"
53 "input file name, after stripping off .pgc if present.\n");
54 printf("\nReport bugs to <pgsql-bugs@postgresql.org>.\n");
58 add_include_path(char *path)
60 struct _include_path *ip = include_paths;
62 include_paths = mm_alloc(sizeof(struct _include_path));
63 include_paths->path = path;
64 include_paths->next = ip;
69 add_preprocessor_define(char *define)
71 struct _defines *pd = defines;
73 defines = mm_alloc(sizeof(struct _defines));
74 defines->old = strdup(define);
75 defines->new = strdup("");
76 defines->pertinent = true;
81 main(int argc, char *const argv[])
87 struct _include_path *ip;
90 if (!strrchr(argv[0], '/'))
93 progname = strrchr(argv[0], '/') + 1;
97 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
102 else if (strcmp(argv[1], "--version") == 0)
104 printf("ecpg (PostgreSQL %s) %d.%d.%d\n", PG_VERSION,
105 MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL);
110 add_include_path("/usr/include");
111 add_include_path(INCLUDE_PATH);
112 add_include_path("/usr/local/include");
113 add_include_path(".");
115 while ((c = getopt(argc, argv, "vco:I:tD:dC:")) != -1)
120 yyout = fopen(optarg, PG_BINARY_W);
127 add_include_path(optarg);
136 auto_create_c = true;
139 if (strcmp(optarg, "INFORMIX") == 0)
140 compat = ECPG_COMPAT_INFORMIX;
143 fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
144 return ILLEGAL_OPTION;
148 add_preprocessor_define(optarg);
154 fprintf(stderr, "%s: parser debug support (-d) not available\n",
159 fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
160 return ILLEGAL_OPTION;
166 fprintf(stderr, "%s, the PostgreSQL embedded C preprocessor, version %d.%d.%d\n",
167 progname, MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL);
168 fprintf(stderr, "exec sql include ... search starts here:\n");
169 for (ip = include_paths; ip != NULL; ip = ip->next)
170 fprintf(stderr, " %s\n", ip->path);
171 fprintf(stderr, "end of search list\n");
175 if (optind >= argc) /* no files specified */
177 fprintf(stderr, "%s: no input files specified\n", progname);
178 fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
179 return (ILLEGAL_OPTION);
183 /* after the options there must not be anything but filenames */
184 for (fnr = optind; fnr < argc; fnr++)
186 char *output_filename = NULL,
189 input_filename = mm_alloc(strlen(argv[fnr]) + 5);
191 strcpy(input_filename, argv[fnr]);
193 /* take care of relative paths */
194 ptr2ext = strrchr(input_filename, '/');
195 ptr2ext = (ptr2ext ? strrchr(ptr2ext, '.') : strrchr(input_filename, '.'));
200 ptr2ext = input_filename + strlen(input_filename);
202 /* no extension => add .pgc */
210 if (out_option == 0) /* calculate the output name */
212 output_filename = strdup(input_filename);
214 ptr2ext = strrchr(output_filename, '.');
215 /* make extension = .c */
219 yyout = fopen(output_filename, PG_BINARY_W);
222 perror(output_filename);
223 free(output_filename);
224 free(input_filename);
229 yyin = fopen(input_filename, PG_BINARY_R);
235 struct _defines *defptr;
236 struct typedefs *typeptr;
238 /* remove old cursor definitions if any are still there */
239 for (ptr = cur; ptr != NULL;)
241 struct cursor *this = ptr;
242 struct arguments *l1,
246 free(ptr->connection);
248 for (l1 = ptr->argsinsert; l1; l1 = l2)
253 for (l1 = ptr->argsresult; l1; l1 = l2)
263 /* remove non-pertinent old defines as well */
264 while (defines && !defines->pertinent)
267 defines = defines->next;
274 for (defptr = defines; defptr != NULL; defptr = defptr->next)
276 struct _defines *this = defptr->next;
278 if (this && !this->pertinent)
280 defptr->next = this->next;
288 /* and old typedefs */
289 for (typeptr = types; typeptr != NULL;)
291 struct typedefs *this = typeptr;
294 ECPGfree_struct_member(typeptr->struct_member_list);
296 typeptr = typeptr->next;
301 /* initialize whenever structures */
302 memset(&when_error, 0, sizeof(struct when));
303 memset(&when_nf, 0, sizeof(struct when));
304 memset(&when_warn, 0, sizeof(struct when));
306 /* and structure member lists */
307 memset(struct_member_list, 0, sizeof(struct_member_list));
309 /* finally the actual connection */
315 /* we need several includes */
316 fprintf(yyout, "/* Processed by ecpg (%d.%d.%d) */\n/* These four 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);
318 /* and parse the source */
328 free(output_filename);
330 free(input_filename);