TLA Line data Source code
1 : /* src/interfaces/ecpg/preproc/ecpg.c */
2 :
3 : /* Main for ecpg, the PostgreSQL embedded SQL precompiler. */
4 : /* Copyright (c) 1996-2023, PostgreSQL Global Development Group */
5 :
6 : #include "postgres_fe.h"
7 :
8 : #include <unistd.h>
9 :
10 : #include "getopt_long.h"
11 :
12 : #include "preproc_extern.h"
13 :
14 : int ret_value = 0;
15 : bool autocommit = false,
16 : auto_create_c = false,
17 : system_includes = false,
18 : force_indicator = true,
19 : questionmarks = false,
20 : regression_mode = false,
21 : auto_prepare = false;
22 :
23 : char *output_filename;
24 :
25 : enum COMPAT_MODE compat = ECPG_COMPAT_PGSQL;
26 :
27 : struct _include_path *include_paths = NULL;
28 : struct cursor *cur = NULL;
29 : struct typedefs *types = NULL;
30 : struct _defines *defines = NULL;
31 : struct declared_list *g_declared_list = NULL;
32 :
33 : static void
34 UBC 0 : help(const char *progname)
35 : {
36 0 : printf(_("%s is the PostgreSQL embedded SQL preprocessor for C programs.\n\n"),
37 : progname);
38 0 : printf(_("Usage:\n"
39 : " %s [OPTION]... FILE...\n\n"),
40 : progname);
41 0 : printf(_("Options:\n"));
42 0 : printf(_(" -c automatically generate C code from embedded SQL code;\n"
43 : " this affects EXEC SQL TYPE\n"));
44 0 : printf(_(" -C MODE set compatibility mode; MODE can be one of\n"
45 : " \"INFORMIX\", \"INFORMIX_SE\", \"ORACLE\"\n"));
46 : #ifdef YYDEBUG
47 : printf(_(" -d generate parser debug output\n"));
48 : #endif
49 0 : printf(_(" -D SYMBOL define SYMBOL\n"));
50 0 : printf(_(" -h parse a header file, this option includes option \"-c\"\n"));
51 0 : printf(_(" -i parse system include files as well\n"));
52 0 : printf(_(" -I DIRECTORY search DIRECTORY for include files\n"));
53 0 : printf(_(" -o OUTFILE write result to OUTFILE\n"));
54 0 : printf(_(" -r OPTION specify run-time behavior; OPTION can be:\n"
55 : " \"no_indicator\", \"prepare\", \"questionmarks\"\n"));
56 0 : printf(_(" --regression run in regression testing mode\n"));
57 0 : printf(_(" -t turn on autocommit of transactions\n"));
58 0 : printf(_(" -V, --version output version information, then exit\n"));
59 0 : printf(_(" -?, --help show this help, then exit\n"));
60 0 : 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 0 : printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
63 0 : printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
64 0 : }
65 :
66 : static void
67 CBC 393 : add_include_path(char *path)
68 : {
69 393 : struct _include_path *ip = include_paths,
70 : *new;
71 :
72 393 : new = mm_alloc(sizeof(struct _include_path));
73 393 : new->path = path;
74 393 : new->next = NULL;
75 :
76 393 : if (ip == NULL)
77 64 : include_paths = new;
78 : else
79 : {
80 1014 : for (; ip->next != NULL; ip = ip->next);
81 329 : ip->next = new;
82 : }
83 393 : }
84 :
85 : static void
86 UBC 0 : add_preprocessor_define(char *define)
87 : {
88 0 : struct _defines *pd = defines;
89 : char *ptr,
90 0 : *define_copy = mm_strdup(define);
91 :
92 0 : defines = mm_alloc(sizeof(struct _defines));
93 :
94 : /* look for = sign */
95 0 : ptr = strchr(define_copy, '=');
96 0 : if (ptr != NULL)
97 : {
98 : char *tmp;
99 :
100 : /* symbol has a value */
101 0 : for (tmp = ptr - 1; *tmp == ' '; tmp--);
102 0 : tmp[1] = '\0';
103 0 : defines->olddef = define_copy;
104 0 : defines->newdef = ptr + 1;
105 : }
106 : else
107 : {
108 0 : defines->olddef = define_copy;
109 0 : defines->newdef = mm_strdup("1");
110 : }
111 0 : defines->pertinent = true;
112 0 : defines->used = NULL;
113 0 : defines->next = pd;
114 0 : }
115 :
116 : #define ECPG_GETOPT_LONG_REGRESSION 1
117 : int
118 CBC 64 : main(int argc, char *const argv[])
119 : {
120 : static struct option ecpg_options[] = {
121 : {"regression", no_argument, NULL, ECPG_GETOPT_LONG_REGRESSION},
122 : {NULL, 0, NULL, 0}
123 : };
124 :
125 : int fnr,
126 : c,
127 64 : out_option = 0;
128 64 : bool verbose = false,
129 64 : header_mode = false;
130 : struct _include_path *ip;
131 : const char *progname;
132 : char my_exec_path[MAXPGPATH];
133 : char include_path[MAXPGPATH];
134 :
135 64 : set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("ecpg"));
136 :
137 64 : progname = get_progname(argv[0]);
138 :
139 64 : if (find_my_exec(argv[0], my_exec_path) < 0)
140 : {
141 UBC 0 : fprintf(stderr, _("%s: could not locate my own executable path\n"), argv[0]);
142 0 : return ILLEGAL_OPTION;
143 : }
144 :
145 CBC 64 : if (argc > 1)
146 : {
147 64 : if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
148 : {
149 UBC 0 : help(progname);
150 0 : exit(0);
151 : }
152 CBC 64 : if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
153 : {
154 UBC 0 : printf("ecpg (PostgreSQL) %s\n", PG_VERSION);
155 0 : exit(0);
156 : }
157 : }
158 :
159 CBC 64 : output_filename = NULL;
160 GNC 336 : while ((c = getopt_long(argc, argv, "cC:dD:hiI:o:r:tv", ecpg_options, NULL)) != -1)
161 : {
162 CBC 272 : switch (c)
163 : {
164 GBC 2 : case 'c':
165 2 : auto_create_c = true;
166 2 : break;
167 10 : case 'C':
168 10 : if (pg_strcasecmp(optarg, "INFORMIX") == 0 || pg_strcasecmp(optarg, "INFORMIX_SE") == 0)
169 CBC 9 : {
170 ECB : char pkginclude_path[MAXPGPATH];
171 : char informix_path[MAXPGPATH];
172 :
173 CBC 9 : compat = (pg_strcasecmp(optarg, "INFORMIX") == 0) ? ECPG_COMPAT_INFORMIX : ECPG_COMPAT_INFORMIX_SE;
174 9 : get_pkginclude_path(my_exec_path, pkginclude_path);
175 9 : snprintf(informix_path, MAXPGPATH, "%s/informix/esql", pkginclude_path);
176 9 : add_include_path(informix_path);
177 ECB : }
178 GBC 1 : else if (pg_strcasecmp(optarg, "ORACLE") == 0)
179 : {
180 CBC 1 : compat = ECPG_COMPAT_ORACLE;
181 : }
182 ECB : else
183 : {
184 UBC 0 : fprintf(stderr, _("Try \"%s --help\" for more information.\n"), argv[0]);
185 0 : return ILLEGAL_OPTION;
186 EUB : }
187 GIC 10 : break;
188 UNC 0 : case 'd':
189 : #ifdef YYDEBUG
190 : base_yydebug = 1;
191 : #else
192 0 : fprintf(stderr, _("%s: parser debug support (-d) not available\n"),
193 : progname);
194 : #endif
195 0 : break;
196 0 : case 'D':
197 0 : add_preprocessor_define(optarg);
198 0 : break;
199 0 : case 'h':
200 0 : header_mode = true;
201 : /* this must include "-c" to make sense: */
202 0 : auto_create_c = true;
203 0 : break;
204 GNC 1 : case 'i':
205 1 : system_includes = true;
206 1 : break;
207 128 : case 'I':
208 128 : add_include_path(optarg);
209 128 : break;
210 64 : case 'o':
211 64 : output_filename = mm_strdup(optarg);
212 64 : if (strcmp(output_filename, "-") == 0)
213 UNC 0 : base_yyout = stdout;
214 : else
215 GNC 64 : base_yyout = fopen(output_filename, PG_BINARY_W);
216 :
217 64 : if (base_yyout == NULL)
218 : {
219 UNC 0 : fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
220 0 : progname, output_filename, strerror(errno));
221 0 : output_filename = NULL;
222 : }
223 : else
224 GNC 64 : out_option = 1;
225 64 : break;
226 GIC 3 : case 'r':
227 CBC 3 : if (pg_strcasecmp(optarg, "no_indicator") == 0)
228 1 : force_indicator = false;
229 2 : else if (pg_strcasecmp(optarg, "prepare") == 0)
230 1 : auto_prepare = true;
231 1 : else if (pg_strcasecmp(optarg, "questionmarks") == 0)
232 1 : questionmarks = true;
233 ECB : else
234 : {
235 LBC 0 : fprintf(stderr, _("Try \"%s --help\" for more information.\n"), argv[0]);
236 UIC 0 : return ILLEGAL_OPTION;
237 : }
238 GBC 3 : break;
239 UNC 0 : case 't':
240 0 : autocommit = true;
241 LBC 0 : break;
242 UNC 0 : case 'v':
243 0 : verbose = true;
244 0 : break;
245 GNC 64 : case ECPG_GETOPT_LONG_REGRESSION:
246 64 : regression_mode = true;
247 CBC 64 : break;
248 LBC 0 : default:
249 UBC 0 : fprintf(stderr, _("Try \"%s --help\" for more information.\n"), argv[0]);
250 0 : return ILLEGAL_OPTION;
251 EUB : }
252 : }
253 :
254 GIC 64 : add_include_path(".");
255 CBC 64 : add_include_path("/usr/local/include");
256 64 : get_include_path(my_exec_path, include_path);
257 64 : add_include_path(include_path);
258 64 : add_include_path("/usr/include");
259 ECB :
260 GIC 64 : if (verbose)
261 ECB : {
262 UIC 0 : fprintf(stderr,
263 UBC 0 : _("%s, the PostgreSQL embedded C preprocessor, version %s\n"),
264 EUB : progname, PG_VERSION);
265 UIC 0 : fprintf(stderr, _("EXEC SQL INCLUDE ... search starts here:\n"));
266 UBC 0 : for (ip = include_paths; ip != NULL; ip = ip->next)
267 0 : fprintf(stderr, " %s\n", ip->path);
268 0 : fprintf(stderr, _("end of search list\n"));
269 0 : return 0;
270 EUB : }
271 :
272 GIC 64 : if (optind >= argc) /* no files specified */
273 ECB : {
274 UIC 0 : fprintf(stderr, _("%s: no input files specified\n"), progname);
275 UBC 0 : fprintf(stderr, _("Try \"%s --help\" for more information.\n"), argv[0]);
276 0 : return ILLEGAL_OPTION;
277 EUB : }
278 : else
279 : {
280 : /* after the options there must not be anything but filenames */
281 GIC 128 : for (fnr = optind; fnr < argc; fnr++)
282 ECB : {
283 : char *ptr2ext;
284 :
285 : /* If argv[fnr] is "-" we have to read from stdin */
286 GIC 64 : if (strcmp(argv[fnr], "-") == 0)
287 ECB : {
288 UIC 0 : input_filename = mm_alloc(strlen("stdin") + 1);
289 UBC 0 : strcpy(input_filename, "stdin");
290 0 : base_yyin = stdin;
291 EUB : }
292 : else
293 : {
294 GIC 64 : input_filename = mm_alloc(strlen(argv[fnr]) + 5);
295 CBC 64 : strcpy(input_filename, argv[fnr]);
296 ECB :
297 : /* take care of relative paths */
298 GIC 64 : ptr2ext = last_dir_separator(input_filename);
299 CBC 64 : ptr2ext = (ptr2ext ? strrchr(ptr2ext, '.') : strrchr(input_filename, '.'));
300 ECB :
301 : /* no extension? */
302 GIC 64 : if (ptr2ext == NULL)
303 ECB : {
304 UIC 0 : ptr2ext = input_filename + strlen(input_filename);
305 EUB :
306 : /* no extension => add .pgc or .pgh */
307 UIC 0 : ptr2ext[0] = '.';
308 UBC 0 : ptr2ext[1] = 'p';
309 0 : ptr2ext[2] = 'g';
310 0 : ptr2ext[3] = (header_mode == true) ? 'h' : 'c';
311 0 : ptr2ext[4] = '\0';
312 EUB : }
313 :
314 GIC 64 : base_yyin = fopen(input_filename, PG_BINARY_R);
315 ECB : }
316 :
317 GIC 64 : if (out_option == 0) /* calculate the output name */
318 ECB : {
319 UIC 0 : if (strcmp(input_filename, "stdin") == 0)
320 UBC 0 : base_yyout = stdout;
321 EUB : else
322 : {
323 UIC 0 : output_filename = mm_alloc(strlen(input_filename) + 3);
324 UBC 0 : strcpy(output_filename, input_filename);
325 EUB :
326 UIC 0 : ptr2ext = strrchr(output_filename, '.');
327 EUB : /* make extension = .c resp. .h */
328 UIC 0 : ptr2ext[1] = (header_mode == true) ? 'h' : 'c';
329 UBC 0 : ptr2ext[2] = '\0';
330 EUB :
331 UIC 0 : base_yyout = fopen(output_filename, PG_BINARY_W);
332 UBC 0 : if (base_yyout == NULL)
333 EUB : {
334 UIC 0 : fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
335 UBC 0 : progname, output_filename, strerror(errno));
336 0 : free(output_filename);
337 0 : output_filename = NULL;
338 0 : free(input_filename);
339 0 : continue;
340 EUB : }
341 : }
342 : }
343 :
344 GIC 64 : if (base_yyin == NULL)
345 LBC 0 : fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
346 UBC 0 : progname, argv[fnr], strerror(errno));
347 EUB : else
348 : {
349 : struct cursor *ptr;
350 : struct _defines *defptr;
351 : struct typedefs *typeptr;
352 : struct declared_list *list;
353 :
354 : /* remove old cursor definitions if any are still there */
355 GIC 64 : for (ptr = cur; ptr != NULL;)
356 ECB : {
357 UIC 0 : struct cursor *this = ptr;
358 EUB : struct arguments *l1,
359 : *l2;
360 :
361 UIC 0 : free(ptr->command);
362 UBC 0 : free(ptr->connection);
363 0 : free(ptr->name);
364 0 : for (l1 = ptr->argsinsert; l1; l1 = l2)
365 EUB : {
366 UIC 0 : l2 = l1->next;
367 UBC 0 : free(l1);
368 EUB : }
369 UIC 0 : for (l1 = ptr->argsresult; l1; l1 = l2)
370 EUB : {
371 UIC 0 : l2 = l1->next;
372 UBC 0 : free(l1);
373 EUB : }
374 UIC 0 : ptr = ptr->next;
375 UBC 0 : free(this);
376 EUB : }
377 GIC 64 : cur = NULL;
378 ECB :
379 : /* remove old declared statements if any are still there */
380 GIC 64 : for (list = g_declared_list; list != NULL;)
381 ECB : {
382 UIC 0 : struct declared_list *this = list;
383 EUB :
384 UIC 0 : list = list->next;
385 UBC 0 : free(this);
386 EUB : }
387 :
388 : /* remove non-pertinent old defines as well */
389 GIC 64 : while (defines && !defines->pertinent)
390 ECB : {
391 UIC 0 : defptr = defines;
392 UBC 0 : defines = defines->next;
393 EUB :
394 UIC 0 : free(defptr->newdef);
395 UBC 0 : free(defptr->olddef);
396 0 : free(defptr);
397 EUB : }
398 :
399 GIC 64 : for (defptr = defines; defptr != NULL; defptr = defptr->next)
400 ECB : {
401 UIC 0 : struct _defines *this = defptr->next;
402 EUB :
403 UIC 0 : if (this && !this->pertinent)
404 EUB : {
405 UIC 0 : defptr->next = this->next;
406 EUB :
407 UIC 0 : free(this->newdef);
408 UBC 0 : free(this->olddef);
409 0 : free(this);
410 EUB : }
411 : }
412 :
413 : /* and old typedefs */
414 GIC 64 : for (typeptr = types; typeptr != NULL;)
415 ECB : {
416 UIC 0 : struct typedefs *this = typeptr;
417 EUB :
418 UIC 0 : free(typeptr->name);
419 UBC 0 : ECPGfree_struct_member(typeptr->struct_member_list);
420 0 : free(typeptr->type);
421 0 : typeptr = typeptr->next;
422 0 : free(this);
423 EUB : }
424 GIC 64 : types = NULL;
425 ECB :
426 : /* initialize whenever structures */
427 GIC 64 : memset(&when_error, 0, sizeof(struct when));
428 CBC 64 : memset(&when_nf, 0, sizeof(struct when));
429 64 : memset(&when_warn, 0, sizeof(struct when));
430 ECB :
431 : /* and structure member lists */
432 GIC 64 : memset(struct_member_list, 0, sizeof(struct_member_list));
433 ECB :
434 : /*
435 : * and our variable counter for out of scope cursors'
436 : * variables
437 : */
438 GIC 64 : ecpg_internal_var = 0;
439 ECB :
440 : /* finally the actual connection */
441 GIC 64 : connection = NULL;
442 ECB :
443 : /* initialize lex */
444 GIC 64 : lex_init();
445 ECB :
446 : /* we need several includes */
447 : /* but not if we are in header mode */
448 GIC 64 : if (regression_mode)
449 CBC 64 : fprintf(base_yyout, "/* Processed by ecpg (regression mode) */\n");
450 ECB : else
451 UIC 0 : fprintf(base_yyout, "/* Processed by ecpg (%s) */\n", PG_VERSION);
452 EUB :
453 GIC 64 : if (header_mode == false)
454 ECB : {
455 GIC 64 : fprintf(base_yyout, "/* These include files are added by the preprocessor */\n#include <ecpglib.h>\n#include <ecpgerrno.h>\n#include <sqlca.h>\n");
456 ECB :
457 : /* add some compatibility headers */
458 GIC 64 : if (INFORMIX_MODE)
459 CBC 9 : fprintf(base_yyout, "/* Needed for informix compatibility */\n#include <ecpg_informix.h>\n");
460 ECB :
461 GIC 64 : fprintf(base_yyout, "/* End of automatic include section */\n");
462 ECB : }
463 :
464 GIC 64 : if (regression_mode)
465 CBC 64 : fprintf(base_yyout, "#define ECPGdebug(X,Y) ECPGdebug((X)+100,(Y))\n");
466 ECB :
467 GIC 64 : output_line_number();
468 ECB :
469 : /* and parse the source */
470 GIC 64 : base_yyparse();
471 ECB :
472 : /*
473 : * Check whether all cursors were indeed opened. It does not
474 : * really make sense to declare a cursor but not open it.
475 : */
476 GIC 100 : for (ptr = cur; ptr != NULL; ptr = ptr->next)
477 CBC 36 : if (!(ptr->opened))
478 LBC 0 : mmerror(PARSE_ERROR, ET_WARNING, "cursor \"%s\" has been declared but not opened", ptr->name);
479 EUB :
480 GIC 64 : if (base_yyin != NULL && base_yyin != stdin)
481 CBC 64 : fclose(base_yyin);
482 64 : if (out_option == 0 && base_yyout != stdout)
483 LBC 0 : fclose(base_yyout);
484 EUB :
485 : /*
486 : * If there was an error, delete the output file.
487 : */
488 GIC 64 : if (ret_value != 0)
489 ECB : {
490 UIC 0 : if (strcmp(output_filename, "-") != 0 && unlink(output_filename) != 0)
491 UBC 0 : fprintf(stderr, _("could not remove output file \"%s\"\n"), output_filename);
492 EUB : }
493 : }
494 :
495 GIC 64 : if (output_filename && out_option == 0)
496 ECB : {
497 UIC 0 : free(output_filename);
498 UBC 0 : output_filename = NULL;
499 EUB : }
500 :
501 GIC 64 : free(input_filename);
502 ECB : }
503 : }
504 GIC 64 : return ret_value;
505 ECB : }
|