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.

Function Documentation

◆ add_include_path()

static void add_include_path ( char *  path)
static

Definition at line 67 of file ecpg.c.

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
void * mm_alloc(size_t size)
Definition: type.c:13
struct _include_path * next
Definition: type.h:133
char * path
Definition: type.h:132

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

Referenced by main().

◆ add_preprocessor_define()

static void add_preprocessor_define ( char *  define)
static

Definition at line 86 of file ecpg.c.

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 }
struct _defines * defines
Definition: ecpg.c:30
char * mm_strdup(const char *string)
Definition: type.c:25
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

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

Referenced by main().

◆ help()

static void help ( const char *  progname)
static

Definition at line 34 of file ecpg.c.

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

References _, printf, and progname.

Referenced by main().

◆ main()

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

Definition at line 118 of file ecpg.c.

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\": %s\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\": %s\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\": %s\n"),
346  progname, argv[fnr], strerror(errno));
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:219
#define PG_BINARY_R
Definition: c.h:1262
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1204
#define PG_BINARY_W
Definition: c.h:1263
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:152
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:460
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
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
@ ECPG_COMPAT_ORACLE
@ ECPG_COMPAT_INFORMIX
@ ECPG_COMPAT_INFORMIX_SE
#define INFORMIX_MODE(X)
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
#define no_argument
Definition: getopt_long.h:24
char my_exec_path[MAXPGPATH]
Definition: globals.c:76
int base_yyparse(core_yyscan_t yyscanner)
#define free(a)
Definition: header.h:65
exit(1)
void output_line_number(void)
Definition: output.c:10
struct when when_error when_nf when_warn
Definition: output.c:32
static int verbose
#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:843
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 strerror
Definition: port.h:251
#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:852
char * c
void lex_init(void)
void mmerror(int error_code, enum errortype type, const char *error,...) pg_attribute_printf(3
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
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

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, cur, defines, ECPG_COMPAT_INFORMIX, ECPG_COMPAT_INFORMIX_SE, ECPG_COMPAT_ORACLE, ECPG_GETOPT_LONG_REGRESSION, ecpg_internal_var, ECPGfree_struct_member(), ET_WARNING, exit(), find_my_exec(), force_indicator, fprintf, free, g_declared_list, get_include_path(), get_pkginclude_path(), get_progname(), getopt_long(), help(), ILLEGAL_OPTION, include_paths, 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, next, _include_path::next, cursor::next, typedefs::next, _defines::next, arguments::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, types, verbose, and when_warn.

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

◆ g_declared_list

struct declared_list* g_declared_list = NULL

Definition at line 31 of file ecpg.c.

Referenced by main().

◆ include_paths

struct _include_path* include_paths = NULL

Definition at line 27 of file ecpg.c.

Referenced by add_include_path(), and main().

◆ output_filename

char* output_filename

Definition at line 23 of file ecpg.c.

Referenced by main().

◆ questionmarks

◆ 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