PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
ecpg.c File Reference
#include "postgres_fe.h"
#include <unistd.h>
#include "getopt_long.h"
#include "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

#define ECPG_GETOPT_LONG_REGRESSION   1

Definition at line 114 of file ecpg.c.

Referenced by main().

Function Documentation

static void add_include_path ( char *  path)
static

Definition at line 65 of file ecpg.c.

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

Referenced by main().

66 {
67  struct _include_path *ip = include_paths,
68  *new;
69 
70  new = mm_alloc(sizeof(struct _include_path));
71  new->path = path;
72  new->next = NULL;
73 
74  if (ip == NULL)
75  include_paths = new;
76  else
77  {
78  for (; ip->next != NULL; ip = ip->next);
79  ip->next = new;
80  }
81 }
struct _include_path * include_paths
Definition: ecpg.c:27
void * mm_alloc(size_t)
#define NULL
Definition: c.h:229
char * path
Definition: type.h:120
struct _include_path * next
Definition: type.h:121
static void add_preprocessor_define ( char *  define)
static

Definition at line 84 of file ecpg.c.

References defines, mm_alloc(), mm_strdup(), _defines::new, _defines::next, NULL, _defines::old, _defines::pertinent, and _defines::used.

Referenced by main().

85 {
86  struct _defines *pd = defines;
87  char *ptr,
88  *define_copy = mm_strdup(define);
89 
90  defines = mm_alloc(sizeof(struct _defines));
91 
92  /* look for = sign */
93  ptr = strchr(define_copy, '=');
94  if (ptr != NULL)
95  {
96  char *tmp;
97 
98  /* symbol has a value */
99  for (tmp = ptr - 1; *tmp == ' '; tmp--);
100  tmp[1] = '\0';
101  defines->old = define_copy;
102  defines->new = ptr + 1;
103  }
104  else
105  {
106  defines->old = define_copy;
107  defines->new = mm_strdup("1");
108  }
109  defines->pertinent = true;
110  defines->used = NULL;
111  defines->next = pd;
112 }
char * new
Definition: type.h:150
void * used
Definition: type.h:152
char * old
Definition: type.h:149
Definition: type.h:147
void * mm_alloc(size_t)
struct _defines * next
Definition: type.h:153
int pertinent
Definition: type.h:151
#define NULL
Definition: c.h:229
struct _defines * defines
Definition: ecpg.c:30
char * mm_strdup(const char *)
Definition: type.c:25
static void help ( const char *  progname)
static

Definition at line 33 of file ecpg.c.

References _.

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\"\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 <pgsql-bugs@postgresql.org>.\n"));
62 }
const char * progname
Definition: pg_standby.c:37
#define _(x)
Definition: elog.c:84
int main ( int  argc,
char *const  argv[] 
)

Definition at line 116 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_GETOPT_LONG_REGRESSION, ecpg_internal_var, ECPGfree_struct_member(), ET_WARNING, find_my_exec(), force_indicator, 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::new, _include_path::next, cursor::next, typedefs::next, _defines::next, arguments::next, next, no_argument, NULL, _defines::old, cursor::opened, optarg, optind, output_filename, output_line_number(), PARSE_ERROR, _include_path::path, _defines::pertinent, PG_BINARY_R, PG_BINARY_W, PG_TEXTDOMAIN, progname, questionmarks, regression_mode, ret_value, set_pglocale_pgservice(), snprintf(), strerror(), struct_member_list, typedefs::struct_member_list, system_includes, typedefs::type, unlink(), verbose, and when_warn.

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

Variable Documentation

bool auto_create_c = false

Definition at line 16 of file ecpg.c.

Referenced by main().

bool auto_prepare = false

Definition at line 21 of file ecpg.c.

Referenced by main(), and output_statement().

bool autocommit = false

Definition at line 15 of file ecpg.c.

Referenced by ECPGconnect(), and main().

struct _defines* defines = NULL

Definition at line 30 of file ecpg.c.

Referenced by add_preprocessor_define(), and main().

bool force_indicator = true

Definition at line 18 of file ecpg.c.

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

struct _include_path* include_paths = NULL

Definition at line 27 of file ecpg.c.

Referenced by add_include_path().

char* output_filename

Definition at line 23 of file ecpg.c.

Referenced by main().

bool questionmarks = false

Definition at line 19 of file ecpg.c.

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

bool regression_mode = false

Definition at line 20 of file ecpg.c.

Referenced by main().

int ret_value = 0

Definition at line 14 of file ecpg.c.

Referenced by main().

bool system_includes = false

Definition at line 17 of file ecpg.c.

Referenced by main().

struct typedefs* types = NULL