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
 

Macro Definition Documentation

◆ ECPG_GETOPT_LONG_REGRESSION

#define ECPG_GETOPT_LONG_REGRESSION   1

Definition at line 115 of file ecpg.c.

Referenced by main().

Function Documentation

◆ add_include_path()

static void add_include_path ( char *  path)
static

Definition at line 66 of file ecpg.c.

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

Referenced by main().

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 }
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 85 of file ecpg.c.

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

Referenced by main().

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->olddef = define_copy;
103  defines->newdef = ptr + 1;
104  }
105  else
106  {
107  defines->olddef = define_copy;
108  defines->newdef = mm_strdup("1");
109  }
110  defines->pertinent = true;
111  defines->used = NULL;
112  defines->next = pd;
113 }
char * newdef
Definition: type.h:156
void * used
Definition: type.h:158
Definition: type.h:153
char * mm_strdup(const char *)
Definition: type.c:25
char * olddef
Definition: type.h:155
struct _defines * next
Definition: type.h:159
int pertinent
Definition: type.h:157
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 33 of file ecpg.c.

References _, and printf.

Referenced by main().

34 {
35  printf(_("%s is the PostgreSQL embedded SQL preprocessor for C programs.\n\n"),
36  progname);
37  printf(_("Usage:\n"
38  " %s [OPTION]... FILE...\n\n"),
39  progname);
40  printf(_("Options:\n"));
41  printf(_(" -c automatically generate C code from embedded SQL code;\n"
42  " this affects EXEC SQL TYPE\n"));
43  printf(_(" -C MODE set compatibility mode; MODE can be one of\n"
44  " \"INFORMIX\", \"INFORMIX_SE\", \"ORACLE\"\n"));
45 #ifdef YYDEBUG
46  printf(_(" -d generate parser debug output\n"));
47 #endif
48  printf(_(" -D SYMBOL define SYMBOL\n"));
49  printf(_(" -h parse a header file, this option includes option \"-c\"\n"));
50  printf(_(" -i parse system include files as well\n"));
51  printf(_(" -I DIRECTORY search DIRECTORY for include files\n"));
52  printf(_(" -o OUTFILE write result to OUTFILE\n"));
53  printf(_(" -r OPTION specify run-time behavior; OPTION can be:\n"
54  " \"no_indicator\", \"prepare\", \"questionmarks\"\n"));
55  printf(_(" --regression run in regression testing mode\n"));
56  printf(_(" -t turn on autocommit of transactions\n"));
57  printf(_(" -V, --version output version information, then exit\n"));
58  printf(_(" -?, --help show this help, then exit\n"));
59  printf(_("\nIf no output file is specified, the name is formed by adding .c to the\n"
60  "input file name, after stripping off .pgc if present.\n"));
61  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
62  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
63 }
#define printf(...)
Definition: port.h:199
const char * progname
Definition: pg_standby.c:36
#define _(x)
Definition: elog.c:88

◆ main()

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

Definition at line 117 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(), MAXPGPATH, mm_alloc(), mm_strdup(), mmerror(), my_exec_path, cursor::name, typedefs::name, _defines::newdef, _include_path::next, cursor::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.

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

◆ 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