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