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