PostgreSQL Source Code  git master
ecpg.c File Reference
#include "postgres_fe.h"
#include <unistd.h>
#include "getopt_long.h"
#include "preproc_extern.h"
Include dependency graph for ecpg.c:

Go to the source code of this file.

Macros

#define ECPG_GETOPT_LONG_REGRESSION   1
 

Functions

static void help (const char *progname)
 
static void add_include_path (char *path)
 
static void add_preprocessor_define (char *define)
 
int main (int argc, char *const argv[])
 

Variables

int ret_value = 0
 
bool autocommit = false
 
bool auto_create_c = false
 
bool system_includes = false
 
bool force_indicator = true
 
bool questionmarks = false
 
bool regression_mode = false
 
bool auto_prepare = false
 
char * output_filename
 
enum COMPAT_MODE compat = ECPG_COMPAT_PGSQL
 
struct _include_pathinclude_paths = NULL
 
struct cursorcur = NULL
 
struct typedefstypes = NULL
 
struct _definesdefines = NULL
 
struct declared_listg_declared_list = NULL
 

Macro Definition Documentation

◆ ECPG_GETOPT_LONG_REGRESSION

#define ECPG_GETOPT_LONG_REGRESSION   1

Definition at line 116 of file ecpg.c.

Referenced by main().

Function Documentation

◆ add_include_path()

static void add_include_path ( char *  path)
static

Definition at line 67 of file ecpg.c.

References include_paths, mm_alloc(), _include_path::next, and _include_path::path.

Referenced by main().

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 }
struct _include_path * include_paths
Definition: ecpg.c:27
char * path
Definition: type.h:126
void * mm_alloc(size_t)
Definition: type.c:13
struct _include_path * next
Definition: type.h:127

◆ add_preprocessor_define()

static void add_preprocessor_define ( char *  define)
static

Definition at line 86 of file ecpg.c.

References defines, mm_alloc(), mm_strdup(), _defines::newdef, _defines::next, _defines::olddef, _defines::pertinent, and _defines::used.

Referenced by main().

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 }
char * newdef
Definition: type.h:163
void * used
Definition: type.h:165
Definition: type.h:160
char * mm_strdup(const char *)
Definition: type.c:25
char * olddef
Definition: type.h:162
struct _defines * next
Definition: type.h:166
int pertinent
Definition: type.h:164
struct _defines * defines
Definition: ecpg.c:30
void * mm_alloc(size_t)
Definition: type.c:13

◆ help()

static void help ( const char *  progname)
static

Definition at line 34 of file ecpg.c.

References _, and printf.

Referenced by main().

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 }
const char * progname
Definition: main.c:46
#define printf(...)
Definition: port.h:222
#define _(x)
Definition: elog.c:89

◆ main()

int main ( int  argc,
char *const  argv[] 
)

Definition at line 118 of file ecpg.c.

References _, add_include_path(), add_preprocessor_define(), cursor::argsinsert, cursor::argsresult, auto_create_c, auto_prepare, autocommit, base_yyin, base_yyout, base_yyparse(), cursor::command, compat, cursor::connection, defines, ECPG_COMPAT_INFORMIX, ECPG_COMPAT_INFORMIX_SE, ECPG_COMPAT_ORACLE, ECPG_GETOPT_LONG_REGRESSION, ecpg_internal_var, ECPGfree_struct_member(), ET_WARNING, find_my_exec(), force_indicator, fprintf, free, get_include_path(), get_pkginclude_path(), get_progname(), getopt_long(), help(), ILLEGAL_OPTION, INFORMIX_MODE, input_filename, last_dir_separator(), lex_init(), sort-test::list, MAXPGPATH, mm_alloc(), mm_strdup(), mmerror(), my_exec_path, cursor::name, typedefs::name, _defines::newdef, _include_path::next, cursor::next, declared_list::next, typedefs::next, _defines::next, arguments::next, next, no_argument, _defines::olddef, cursor::opened, optarg, optind, output_filename, output_line_number(), PARSE_ERROR, _include_path::path, _defines::pertinent, PG_BINARY_R, PG_BINARY_W, pg_strcasecmp(), PG_TEXTDOMAIN, printf, progname, questionmarks, regression_mode, ret_value, set_pglocale_pgservice(), snprintf, generate_unaccent_rules::stdout, strerror, struct_member_list, typedefs::struct_member_list, system_includes, typedefs::type, verbose, and when_warn.

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 declared statements if any are still there */
379  for (list = g_declared_list; list != NULL;)
380  {
381  struct declared_list *this = list;
382 
383  list = list->next;
384  free(this);
385  }
386 
387  /* remove non-pertinent old defines as well */
388  while (defines && !defines->pertinent)
389  {
390  defptr = defines;
391  defines = defines->next;
392 
393  free(defptr->newdef);
394  free(defptr->olddef);
395  free(defptr);
396  }
397 
398  for (defptr = defines; defptr != NULL; defptr = defptr->next)
399  {
400  struct _defines *this = defptr->next;
401 
402  if (this && !this->pertinent)
403  {
404  defptr->next = this->next;
405 
406  free(this->newdef);
407  free(this->olddef);
408  free(this);
409  }
410  }
411 
412  /* and old typedefs */
413  for (typeptr = types; typeptr != NULL;)
414  {
415  struct typedefs *this = typeptr;
416 
417  free(typeptr->name);
419  free(typeptr->type);
420  typeptr = typeptr->next;
421  free(this);
422  }
423  types = NULL;
424 
425  /* initialize whenever structures */
426  memset(&when_error, 0, sizeof(struct when));
427  memset(&when_nf, 0, sizeof(struct when));
428  memset(&when_warn, 0, sizeof(struct when));
429 
430  /* and structure member lists */
431  memset(struct_member_list, 0, sizeof(struct_member_list));
432 
433  /*
434  * and our variable counter for out of scope cursors'
435  * variables
436  */
437  ecpg_internal_var = 0;
438 
439  /* finally the actual connection */
440  connection = NULL;
441 
442  /* initialize lex */
443  lex_init();
444 
445  /* we need several includes */
446  /* but not if we are in header mode */
447  if (regression_mode)
448  fprintf(base_yyout, "/* Processed by ecpg (regression mode) */\n");
449  else
450  fprintf(base_yyout, "/* Processed by ecpg (%s) */\n", PG_VERSION);
451 
452  if (header_mode == false)
453  {
454  fprintf(base_yyout, "/* These include files are added by the preprocessor */\n#include <ecpglib.h>\n#include <ecpgerrno.h>\n#include <sqlca.h>\n");
455 
456  /* add some compatibility headers */
457  if (INFORMIX_MODE)
458  fprintf(base_yyout, "/* Needed for informix compatibility */\n#include <ecpg_informix.h>\n");
459 
460  fprintf(base_yyout, "/* End of automatic include section */\n");
461  }
462 
463  if (regression_mode)
464  fprintf(base_yyout, "#define ECPGdebug(X,Y) ECPGdebug((X)+100,(Y))\n");
465 
467 
468  /* and parse the source */
469  base_yyparse();
470 
471  /*
472  * Check whether all cursors were indeed opened. It does not
473  * really make sense to declare a cursor but not open it.
474  */
475  for (ptr = cur; ptr != NULL; ptr = ptr->next)
476  if (!(ptr->opened))
477  mmerror(PARSE_ERROR, ET_WARNING, "cursor \"%s\" has been declared but not opened", ptr->name);
478 
479  if (base_yyin != NULL && base_yyin != stdin)
480  fclose(base_yyin);
481  if (out_option == 0 && base_yyout != stdout)
482  fclose(base_yyout);
483 
484  /*
485  * If there was an error, delete the output file.
486  */
487  if (ret_value != 0)
488  {
489  if (strcmp(output_filename, "-") != 0 && unlink(output_filename) != 0)
490  fprintf(stderr, _("could not remove output file \"%s\"\n"), output_filename);
491  }
492  }
493 
494  if (output_filename && out_option == 0)
495  {
497  output_filename = NULL;
498  }
499 
501  }
502  }
503  return ret_value;
504 }
struct declared_list * next
Definition: type.h:148
bool regression_mode
Definition: ecpg.c:20
int ret_value
Definition: ecpg.c:14
#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
#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
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

Variable Documentation

◆ auto_create_c

bool auto_create_c = false

Definition at line 16 of file ecpg.c.

Referenced by main().

◆ auto_prepare

bool auto_prepare = false

Definition at line 21 of file ecpg.c.

Referenced by main(), and output_statement().

◆ autocommit

bool autocommit = false

Definition at line 15 of file ecpg.c.

Referenced by ECPGconnect(), and main().

◆ compat

◆ cur

◆ defines

struct _defines* defines = NULL

Definition at line 30 of file ecpg.c.

Referenced by add_preprocessor_define(), and main().

◆ force_indicator

bool force_indicator = true

Definition at line 18 of file ecpg.c.

Referenced by ecpg_do_prologue(), main(), and output_statement().

◆ g_declared_list

struct declared_list* g_declared_list = NULL

Definition at line 31 of file ecpg.c.

◆ include_paths

struct _include_path* include_paths = NULL

Definition at line 27 of file ecpg.c.

Referenced by add_include_path().

◆ output_filename

char* output_filename

Definition at line 23 of file ecpg.c.

Referenced by main().

◆ questionmarks

bool questionmarks = false

Definition at line 19 of file ecpg.c.

Referenced by ecpg_do_prologue(), main(), output_prepare_statement(), and output_statement().

◆ regression_mode

bool regression_mode = false

Definition at line 20 of file ecpg.c.

Referenced by main().

◆ ret_value

int ret_value = 0

Definition at line 14 of file ecpg.c.

Referenced by main().

◆ system_includes

bool system_includes = false

Definition at line 17 of file ecpg.c.

Referenced by main().

◆ types