1 /* New main for ecpg, the PostgreSQL embedded SQL precompiler. */
2 /* (C) Michael Meskes <meskes@postgresql.org> Feb 5th, 1998 */
3 /* Placed under the same copyright as PostgresSQL */
5 #include "postgres_fe.h"
17 struct _include_path *include_paths = NULL;
18 struct cursor *cur = NULL;
19 struct typedefs *types = NULL;
20 struct _defines *defines = NULL;
25 fprintf(stderr, "ecpg - the postgresql preprocessor, version: %d.%d.%d\n", MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL);
26 fprintf(stderr, "Usage: %s: "
30 " [-v] [-t] [-I include path] [ -o output file name] [-D define name] file1 [file2] ...\n", progname);
34 add_include_path(char *path)
36 struct _include_path *ip = include_paths;
38 include_paths = mm_alloc(sizeof(struct _include_path));
39 include_paths->path = path;
40 include_paths->next = ip;
44 add_preprocessor_define(char *define)
46 struct _defines *pd = defines;
48 defines = mm_alloc(sizeof(struct _defines));
49 defines->old = strdup(define);
50 defines->new = strdup("");
51 defines->pertinent = true;
56 main(int argc, char *const argv[])
62 struct _include_path *ip;
67 add_include_path("/usr/include");
68 add_include_path(INCLUDE_PATH);
69 add_include_path("/usr/local/include");
70 add_include_path(".");
72 while ((c = getopt(argc, argv, "vo:I:tD:d")) != EOF)
77 yyout = fopen(optarg, PG_BINARY_W);
84 add_include_path(optarg);
93 add_preprocessor_define(optarg);
102 return ILLEGAL_OPTION;
108 fprintf(stderr, "ecpg - the postgresql preprocessor, version: %d.%d.%d\n", MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL);
109 fprintf(stderr, "exec sql include ... search starts here:\n");
110 for (ip = include_paths; ip != NULL; ip = ip->next)
111 fprintf(stderr, " %s\n", ip->path);
112 fprintf(stderr, "End of search list.\n");
116 if (optind >= argc) /* no files specified */
119 return (ILLEGAL_OPTION);
123 /* after the options there must not be anything but filenames */
124 for (fnr = optind; fnr < argc; fnr++)
126 char *output_filename = NULL,
129 input_filename = mm_alloc(strlen(argv[fnr]) + 5);
131 strcpy(input_filename, argv[fnr]);
133 /* take care of relative paths */
134 ptr2ext = strrchr(input_filename, '/');
135 ptr2ext = (ptr2ext ? strrchr(ptr2ext, '.') : strrchr(input_filename, '.'));
140 ptr2ext = input_filename + strlen(input_filename);
142 /* no extension => add .pgc */
150 if (out_option == 0)/* calculate the output name */
152 output_filename = strdup(input_filename);
154 ptr2ext = strrchr(output_filename, '.');
155 /* make extension = .c */
159 yyout = fopen(output_filename, PG_BINARY_W);
162 perror(output_filename);
163 free(output_filename);
164 free(input_filename);
169 yyin = fopen(input_filename, PG_BINARY_R);
175 struct _defines *defptr;
176 struct typedefs *typeptr;
178 /* remove old cursor definitions if any are still there */
179 for (ptr = cur; ptr != NULL;)
181 struct cursor *this = ptr;
182 struct arguments *l1,
186 free(ptr->connection);
188 for (l1 = ptr->argsinsert; l1; l1 = l2)
193 for (l1 = ptr->argsresult; l1; l1 = l2)
203 /* remove non-pertinent old defines as well */
204 while (defines && !defines->pertinent)
207 defines = defines->next;
214 for (defptr = defines; defptr != NULL; defptr = defptr->next)
216 struct _defines *this = defptr->next;
218 if (this && !this->pertinent)
220 defptr->next = this->next;
228 /* and old typedefs */
229 for (typeptr = types; typeptr != NULL;)
231 struct typedefs *this = typeptr;
234 ECPGfree_struct_member(typeptr->struct_member_list);
236 typeptr = typeptr->next;
241 /* initialize whenever structures */
242 memset(&when_error, 0, sizeof(struct when));
243 memset(&when_nf, 0, sizeof(struct when));
244 memset(&when_warn, 0, sizeof(struct when));
246 /* and structure member lists */
247 memset(struct_member_list, 0, sizeof(struct_member_list));
249 /* finally the actual connection */
255 /* we need two includes */
256 fprintf(yyout, "/* Processed by ecpg (%d.%d.%d) */\n/* These three include files are added by the preprocessor */\n#include <ecpgtype.h>\n#include <ecpglib.h>\n#include <ecpgerrno.h>\n#line 1 \"%s\"\n", MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL, input_filename);
258 /* and parse the source */
268 free(output_filename);
270 free(input_filename);