OSDN Git Service

pgindent run. Make it all clean.
[pg-rex/syncrep.git] / src / interfaces / ecpg / preproc / ecpg.c
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 */
4
5 #include "postgres_fe.h"
6
7 #include <unistd.h>
8
9 #ifdef HAVE_GETOPT_H
10 #include "getopt.h"
11 #endif
12
13 #include "extern.h"
14
15 int                     ret_value = OK,
16                         autocommit = 0;
17 struct _include_path *include_paths = NULL;
18 struct cursor *cur = NULL;
19 struct typedefs *types = NULL;
20 struct _defines *defines = NULL;
21
22 static void
23 usage(char *progname)
24 {
25         fprintf(stderr, "ecpg - the postgresql preprocessor, version: %d.%d.%d\n", MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL);
26         fprintf(stderr, "Usage: %s: "
27 #ifdef YYDEBUG
28                         "[-d]"
29 #endif
30                         " [-v] [-t] [-I include path] [ -o output file name] [-D define name] file1 [file2] ...\n", progname);
31 }
32
33 static void
34 add_include_path(char *path)
35 {
36         struct _include_path *ip = include_paths;
37
38         include_paths = mm_alloc(sizeof(struct _include_path));
39         include_paths->path = path;
40         include_paths->next = ip;
41 }
42
43 static void
44 add_preprocessor_define(char *define)
45 {
46         struct _defines *pd = defines;
47
48         defines = mm_alloc(sizeof(struct _defines));
49         defines->old = strdup(define);
50         defines->new = strdup("");
51         defines->pertinent = true;
52         defines->next = pd;
53 }
54
55 int
56 main(int argc, char *const argv[])
57 {
58         int                     fnr,
59                                 c,
60                                 verbose = false,
61                                 out_option = 0;
62         struct _include_path *ip;
63
64         extern int      optind;
65         extern char *optarg;
66
67         add_include_path("/usr/include");
68         add_include_path(INCLUDE_PATH);
69         add_include_path("/usr/local/include");
70         add_include_path(".");
71
72         while ((c = getopt(argc, argv, "vo:I:tD:d")) != EOF)
73         {
74                 switch (c)
75                 {
76                         case 'o':
77                                 yyout = fopen(optarg, PG_BINARY_W);
78                                 if (yyout == NULL)
79                                         perror(optarg);
80                                 else
81                                         out_option = 1;
82                                 break;
83                         case 'I':
84                                 add_include_path(optarg);
85                                 break;
86                         case 't':
87                                 autocommit = 1;
88                                 break;
89                         case 'v':
90                                 verbose = true;
91                                 break;
92                         case 'D':
93                                 add_preprocessor_define(optarg);
94                                 break;
95 #ifdef YYDEBUG
96                         case 'd':
97                                 yydebug = 1;
98                                 break;
99 #endif
100                         default:
101                                 usage(argv[0]);
102                                 return ILLEGAL_OPTION;
103                 }
104         }
105
106         if (verbose)
107         {
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");
113                 return OK;
114         }
115
116         if (optind >= argc)                     /* no files specified */
117         {
118                 usage(argv[0]);
119                 return (ILLEGAL_OPTION);
120         }
121         else
122         {
123                 /* after the options there must not be anything but filenames */
124                 for (fnr = optind; fnr < argc; fnr++)
125                 {
126                         char       *output_filename = NULL,
127                                            *ptr2ext;
128
129                         input_filename = mm_alloc(strlen(argv[fnr]) + 5);
130
131                         strcpy(input_filename, argv[fnr]);
132
133                         /* take care of relative paths */
134                         ptr2ext = strrchr(input_filename, '/');
135                         ptr2ext = (ptr2ext ? strrchr(ptr2ext, '.') : strrchr(input_filename, '.'));
136
137                         /* no extension? */
138                         if (ptr2ext == NULL)
139                         {
140                                 ptr2ext = input_filename + strlen(input_filename);
141
142                                 /* no extension => add .pgc */
143                                 ptr2ext[0] = '.';
144                                 ptr2ext[1] = 'p';
145                                 ptr2ext[2] = 'g';
146                                 ptr2ext[3] = 'c';
147                                 ptr2ext[4] = '\0';
148                         }
149
150                         if (out_option == 0)/* calculate the output name */
151                         {
152                                 output_filename = strdup(input_filename);
153
154                                 ptr2ext = strrchr(output_filename, '.');
155                                 /* make extension = .c */
156                                 ptr2ext[1] = 'c';
157                                 ptr2ext[2] = '\0';
158
159                                 yyout = fopen(output_filename, PG_BINARY_W);
160                                 if (yyout == NULL)
161                                 {
162                                         perror(output_filename);
163                                         free(output_filename);
164                                         free(input_filename);
165                                         continue;
166                                 }
167                         }
168
169                         yyin = fopen(input_filename, PG_BINARY_R);
170                         if (yyin == NULL)
171                                 perror(argv[fnr]);
172                         else
173                         {
174                                 struct cursor *ptr;
175                                 struct _defines *defptr;
176                                 struct typedefs *typeptr;
177
178                                 /* remove old cursor definitions if any are still there */
179                                 for (ptr = cur; ptr != NULL;)
180                                 {
181                                         struct cursor *this = ptr;
182                                         struct arguments *l1,
183                                                            *l2;
184
185                                         free(ptr->command);
186                                         free(ptr->connection);
187                                         free(ptr->name);
188                                         for (l1 = ptr->argsinsert; l1; l1 = l2)
189                                         {
190                                                 l2 = l1->next;
191                                                 free(l1);
192                                         }
193                                         for (l1 = ptr->argsresult; l1; l1 = l2)
194                                         {
195                                                 l2 = l1->next;
196                                                 free(l1);
197                                         }
198                                         ptr = ptr->next;
199                                         free(this);
200                                 }
201                                 cur = NULL;
202
203                                 /* remove non-pertinent old defines as well */
204                                 while (defines && !defines->pertinent)
205                                 {
206                                         defptr = defines;
207                                         defines = defines->next;
208
209                                         free(defptr->new);
210                                         free(defptr->old);
211                                         free(defptr);
212                                 }
213
214                                 for (defptr = defines; defptr != NULL; defptr = defptr->next)
215                                 {
216                                         struct _defines *this = defptr->next;
217
218                                         if (this && !this->pertinent)
219                                         {
220                                                 defptr->next = this->next;
221
222                                                 free(this->new);
223                                                 free(this->old);
224                                                 free(this);
225                                         }
226                                 }
227
228                                 /* and old typedefs */
229                                 for (typeptr = types; typeptr != NULL;)
230                                 {
231                                         struct typedefs *this = typeptr;
232
233                                         free(typeptr->name);
234                                         ECPGfree_struct_member(typeptr->struct_member_list);
235                                         free(typeptr->type);
236                                         typeptr = typeptr->next;
237                                         free(this);
238                                 }
239                                 types = NULL;
240
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));
245
246                                 /* and structure member lists */
247                                 memset(struct_member_list, 0, sizeof(struct_member_list));
248
249                                 /* finally the actual connection */
250                                 connection = NULL;
251
252                                 /* initialize lex */
253                                 lex_init();
254
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);
257
258                                 /* and parse the source */
259                                 yyparse();
260
261                                 if (yyin != NULL)
262                                         fclose(yyin);
263                                 if (out_option == 0)
264                                         fclose(yyout);
265                         }
266
267                         if (output_filename)
268                                 free(output_filename);
269
270                         free(input_filename);
271                 }
272         }
273         return ret_value;
274 }