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-2024, 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, "cC:dD:hiI:o:r:tv", ecpg_options, NULL)) != -1)
161  {
162  switch (c)
163  {
164  case 'c':
165  auto_create_c = true;
166  break;
167  case 'C':
168  if (pg_strcasecmp(optarg, "INFORMIX") == 0 || pg_strcasecmp(optarg, "INFORMIX_SE") == 0)
169  {
170  char pkginclude_path[MAXPGPATH];
171  char informix_path[MAXPGPATH];
172 
174  get_pkginclude_path(my_exec_path, pkginclude_path);
175  snprintf(informix_path, MAXPGPATH, "%s/informix/esql", pkginclude_path);
176  add_include_path(informix_path);
177  }
178  else if (pg_strcasecmp(optarg, "ORACLE") == 0)
179  {
181  }
182  else
183  {
184  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), argv[0]);
185  return ILLEGAL_OPTION;
186  }
187  break;
188  case 'd':
189 #ifdef YYDEBUG
190  base_yydebug = 1;
191 #else
192  fprintf(stderr, _("%s: parser debug support (-d) not available\n"),
193  progname);
194 #endif
195  break;
196  case 'D':
198  break;
199  case 'h':
200  header_mode = true;
201  /* this must include "-c" to make sense: */
202  auto_create_c = true;
203  break;
204  case 'i':
205  system_includes = true;
206  break;
207  case 'I':
209  break;
210  case 'o':
212  if (strcmp(output_filename, "-") == 0)
213  base_yyout = stdout;
214  else
216 
217  if (base_yyout == NULL)
218  {
219  fprintf(stderr, _("%s: could not open file \"%s\": %m\n"),
221  output_filename = NULL;
222  }
223  else
224  out_option = 1;
225  break;
226  case 'r':
227  if (pg_strcasecmp(optarg, "no_indicator") == 0)
228  force_indicator = false;
229  else if (pg_strcasecmp(optarg, "prepare") == 0)
230  auto_prepare = true;
231  else if (pg_strcasecmp(optarg, "questionmarks") == 0)
232  questionmarks = true;
233  else
234  {
235  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), argv[0]);
236  return ILLEGAL_OPTION;
237  }
238  break;
239  case 't':
240  autocommit = true;
241  break;
242  case 'v':
243  verbose = true;
244  break;
246  regression_mode = true;
247  break;
248  default:
249  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), argv[0]);
250  return ILLEGAL_OPTION;
251  }
252  }
253 
254  add_include_path(".");
255  add_include_path("/usr/local/include");
256  get_include_path(my_exec_path, include_path);
257  add_include_path(include_path);
258  add_include_path("/usr/include");
259 
260  if (verbose)
261  {
262  fprintf(stderr,
263  _("%s, the PostgreSQL embedded C preprocessor, version %s\n"),
264  progname, PG_VERSION);
265  fprintf(stderr, _("EXEC SQL INCLUDE ... search starts here:\n"));
266  for (ip = include_paths; ip != NULL; ip = ip->next)
267  fprintf(stderr, " %s\n", ip->path);
268  fprintf(stderr, _("end of search list\n"));
269  return 0;
270  }
271 
272  if (optind >= argc) /* no files specified */
273  {
274  fprintf(stderr, _("%s: no input files specified\n"), progname);
275  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), argv[0]);
276  return ILLEGAL_OPTION;
277  }
278  else
279  {
280  /* after the options there must not be anything but filenames */
281  for (fnr = optind; fnr < argc; fnr++)
282  {
283  char *ptr2ext;
284 
285  /* If argv[fnr] is "-" we have to read from stdin */
286  if (strcmp(argv[fnr], "-") == 0)
287  {
288  input_filename = mm_alloc(strlen("stdin") + 1);
289  strcpy(input_filename, "stdin");
290  base_yyin = stdin;
291  }
292  else
293  {
294  input_filename = mm_alloc(strlen(argv[fnr]) + 5);
295  strcpy(input_filename, argv[fnr]);
296 
297  /* take care of relative paths */
299  ptr2ext = (ptr2ext ? strrchr(ptr2ext, '.') : strrchr(input_filename, '.'));
300 
301  /* no extension? */
302  if (ptr2ext == NULL)
303  {
304  ptr2ext = input_filename + strlen(input_filename);
305 
306  /* no extension => add .pgc or .pgh */
307  ptr2ext[0] = '.';
308  ptr2ext[1] = 'p';
309  ptr2ext[2] = 'g';
310  ptr2ext[3] = (header_mode == true) ? 'h' : 'c';
311  ptr2ext[4] = '\0';
312  }
313 
315  }
316 
317  if (out_option == 0) /* calculate the output name */
318  {
319  if (strcmp(input_filename, "stdin") == 0)
320  base_yyout = stdout;
321  else
322  {
323  output_filename = mm_alloc(strlen(input_filename) + 3);
325 
326  ptr2ext = strrchr(output_filename, '.');
327  /* make extension = .c resp. .h */
328  ptr2ext[1] = (header_mode == true) ? 'h' : 'c';
329  ptr2ext[2] = '\0';
330 
332  if (base_yyout == NULL)
333  {
334  fprintf(stderr, _("%s: could not open file \"%s\": %m\n"),
337  output_filename = NULL;
339  continue;
340  }
341  }
342  }
343 
344  if (base_yyin == NULL)
345  fprintf(stderr, _("%s: could not open file \"%s\": %m\n"),
346  progname, argv[fnr]);
347  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  for (ptr = cur; ptr != NULL;)
356  {
357  struct cursor *this = ptr;
358  struct arguments *l1,
359  *l2;
360 
361  free(ptr->command);
362  free(ptr->connection);
363  free(ptr->name);
364  for (l1 = ptr->argsinsert; l1; l1 = l2)
365  {
366  l2 = l1->next;
367  free(l1);
368  }
369  for (l1 = ptr->argsresult; l1; l1 = l2)
370  {
371  l2 = l1->next;
372  free(l1);
373  }
374  ptr = ptr->next;
375  free(this);
376  }
377  cur = NULL;
378 
379  /* remove old declared statements if any are still there */
380  for (list = g_declared_list; list != NULL;)
381  {
382  struct declared_list *this = list;
383 
384  list = list->next;
385  free(this);
386  }
387 
388  /* remove non-pertinent old defines as well */
389  while (defines && !defines->pertinent)
390  {
391  defptr = defines;
392  defines = defines->next;
393 
394  free(defptr->newdef);
395  free(defptr->olddef);
396  free(defptr);
397  }
398 
399  for (defptr = defines; defptr != NULL; defptr = defptr->next)
400  {
401  struct _defines *this = defptr->next;
402 
403  if (this && !this->pertinent)
404  {
405  defptr->next = this->next;
406 
407  free(this->newdef);
408  free(this->olddef);
409  free(this);
410  }
411  }
412 
413  /* and old typedefs */
414  for (typeptr = types; typeptr != NULL;)
415  {
416  struct typedefs *this = typeptr;
417 
418  free(typeptr->name);
420  free(typeptr->type);
421  typeptr = typeptr->next;
422  free(this);
423  }
424  types = NULL;
425 
426  /* initialize whenever structures */
427  memset(&when_error, 0, sizeof(struct when));
428  memset(&when_nf, 0, sizeof(struct when));
429  memset(&when_warn, 0, sizeof(struct when));
430 
431  /* and structure member lists */
432  memset(struct_member_list, 0, sizeof(struct_member_list));
433 
434  /*
435  * and our variable counter for out of scope cursors'
436  * variables
437  */
438  ecpg_internal_var = 0;
439 
440  /* finally the actual connection */
441  connection = NULL;
442 
443  /* initialize lex */
444  lex_init();
445 
446  /* we need several includes */
447  /* but not if we are in header mode */
448  if (regression_mode)
449  fprintf(base_yyout, "/* Processed by ecpg (regression mode) */\n");
450  else
451  fprintf(base_yyout, "/* Processed by ecpg (%s) */\n", PG_VERSION);
452 
453  if (header_mode == false)
454  {
455  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 
457  /* add some compatibility headers */
458  if (INFORMIX_MODE)
459  fprintf(base_yyout, "/* Needed for informix compatibility */\n#include <ecpg_informix.h>\n");
460 
461  fprintf(base_yyout, "/* End of automatic include section */\n");
462  }
463 
464  if (regression_mode)
465  fprintf(base_yyout, "#define ECPGdebug(X,Y) ECPGdebug((X)+100,(Y))\n");
466 
468 
469  /* and parse the source */
470  base_yyparse();
471 
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  for (ptr = cur; ptr != NULL; ptr = ptr->next)
477  if (!(ptr->opened))
478  mmerror(PARSE_ERROR, ET_WARNING, "cursor \"%s\" has been declared but not opened", ptr->name);
479 
480  if (base_yyin != NULL && base_yyin != stdin)
481  fclose(base_yyin);
482  if (out_option == 0 && base_yyout != stdout)
483  fclose(base_yyout);
484 
485  /*
486  * If there was an error, delete the output file.
487  */
488  if (ret_value != 0)
489  {
490  if (strcmp(output_filename, "-") != 0 && unlink(output_filename) != 0)
491  fprintf(stderr, _("could not remove output file \"%s\"\n"), output_filename);
492  }
493  }
494 
495  if (output_filename && out_option == 0)
496  {
498  output_filename = NULL;
499  }
500 
502  }
503  }
504  return ret_value;
505 }
static int32 next
Definition: blutils.c:221
#define PG_BINARY_R
Definition: c.h:1262
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1201
#define PG_BINARY_W
Definition: c.h:1263
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:160
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:448
struct _include_path * include_paths
Definition: ecpg.c:27
bool auto_prepare
Definition: ecpg.c:21
char * output_filename
Definition: ecpg.c:23
bool regression_mode
Definition: ecpg.c:20
static void help(const char *progname)
Definition: ecpg.c:34
static void add_include_path(char *path)
Definition: ecpg.c:67
struct _defines * defines
Definition: ecpg.c:30
bool system_includes
Definition: ecpg.c:17
bool autocommit
Definition: ecpg.c:15
static void add_preprocessor_define(char *define)
Definition: ecpg.c:86
struct typedefs * types
Definition: ecpg.c:29
enum COMPAT_MODE compat
Definition: ecpg.c:25
int ret_value
Definition: ecpg.c:14
bool auto_create_c
Definition: ecpg.c:16
bool force_indicator
Definition: ecpg.c:18
#define ECPG_GETOPT_LONG_REGRESSION
Definition: ecpg.c:116
struct declared_list * g_declared_list
Definition: ecpg.c:31
bool questionmarks
Definition: ecpg.c:19
struct cursor * cur
Definition: ecpg.c:28
int main(int argc, char *const argv[])
Definition: ecpg.c:118
COMPAT_MODE
@ ECPG_COMPAT_PGSQL
@ ECPG_COMPAT_ORACLE
@ ECPG_COMPAT_INFORMIX
@ ECPG_COMPAT_INFORMIX_SE
#define INFORMIX_MODE(X)
#define _(x)
Definition: elog.c:90
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:60
#define no_argument
Definition: getopt_long.h:24
char my_exec_path[MAXPGPATH]
Definition: globals.c:78
int base_yyparse(core_yyscan_t yyscanner)
#define free(a)
Definition: header.h:65
int verbose
exit(1)
const char * progname
Definition: main.c:44
void output_line_number(void)
Definition: output.c:10
struct when when_error when_nf when_warn
Definition: output.c:32
#define MAXPGPATH
PGDLLIMPORT int optind
Definition: getopt.c:50
PGDLLIMPORT char * optarg
Definition: getopt.c:52
char * last_dir_separator(const char *filename)
Definition: path.c:139
void get_include_path(const char *my_exec_path, char *ret_path)
Definition: path.c:842
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
const char * get_progname(const char *argv0)
Definition: path.c:574
#define snprintf
Definition: port.h:238
#define fprintf
Definition: port.h:242
void get_pkginclude_path(const char *my_exec_path, char *ret_path)
Definition: path.c:851
#define printf(...)
Definition: port.h:244
char * c
void lex_init(void)
void mmerror(int error_code, enum errortype type, const char *error,...) pg_attribute_printf(3
char * mm_strdup(const char *string)
Definition: type.c:25
int ecpg_internal_var
char * input_filename
#define PARSE_ERROR
struct ECPGstruct_member * struct_member_list[STRUCT_DEPTH]
#define ILLEGAL_OPTION
FILE * base_yyin
FILE * base_yyout
void * mm_alloc(size_t size)
Definition: type.c:13
Definition: type.h:167
struct _defines * next
Definition: type.h:172
char * olddef
Definition: type.h:168
int pertinent
Definition: type.h:170
char * newdef
Definition: type.h:169
void * used
Definition: type.h:171
struct _include_path * next
Definition: type.h:133
char * path
Definition: type.h:132
struct arguments * next
Definition: type.h:188
Definition: type.h:137
bool opened
Definition: type.h:142
char * command
Definition: type.h:140
struct arguments * argsinsert
Definition: type.h:143
char * name
Definition: type.h:138
char * connection
Definition: type.h:141
struct cursor * next
Definition: type.h:147
struct arguments * argsresult
Definition: type.h:145
Definition: type.h:158
char * name
Definition: type.h:159
struct ECPGstruct_member * struct_member_list
Definition: type.h:161
struct typedefs * next
Definition: type.h:163
struct this_type * type
Definition: type.h:160
Definition: type.h:88
void ECPGfree_struct_member(struct ECPGstruct_member *rm)
Definition: type.c:641
@ ET_WARNING
Definition: type.h:207