PostgreSQL Source Code  git master
output.c File Reference
#include "postgres_fe.h"
#include "preproc_extern.h"
Include dependency graph for output.c:

Go to the source code of this file.

Functions

static void output_escaped_str (char *cmd, bool quoted)
 
void output_line_number (void)
 
void output_simple_statement (char *stmt, int whenever_mode)
 
static void print_action (struct when *w)
 
void whenever_action (int mode)
 
char * hashline_number (void)
 
void output_statement (char *stmt, int whenever_mode, enum ECPG_statement_type st)
 
void output_prepare_statement (char *name, char *stmt)
 
void output_deallocate_prepare_statement (char *name)
 

Variables

struct when when_error when_nf when_warn
 
static char * ecpg_statement_type_name []
 

Function Documentation

◆ hashline_number()

char* hashline_number ( void  )

Definition at line 94 of file output.c.

References base_yylineno, generate_unaccent_rules::dest, EMPTY, input_filename, mm_alloc(), and sprintf.

Referenced by output_line_number().

95 {
96  /* do not print line numbers if we are in debug mode */
97  if (input_filename
98 #ifdef YYDEBUG
99  && !base_yydebug
100 #endif
101  )
102  {
103  /* "* 2" here is for escaping '\' and '"' below */
104  char *line = mm_alloc(strlen("\n#line %d \"%s\"\n") + sizeof(int) * CHAR_BIT * 10 / 3 + strlen(input_filename) * 2);
105  char *src,
106  *dest;
107 
108  sprintf(line, "\n#line %d \"", base_yylineno);
109  src = input_filename;
110  dest = line + strlen(line);
111  while (*src)
112  {
113  if (*src == '\\' || *src == '"')
114  *dest++ = '\\';
115  *dest++ = *src++;
116  }
117  *dest = '\0';
118  strcat(dest, "\"\n");
119 
120  return line;
121  }
122 
123  return EMPTY;
124 }
#define sprintf
Definition: port.h:194
int base_yylineno
#define EMPTY
Definition: regcomp.c:274
void * mm_alloc(size_t)
Definition: type.c:13
char * input_filename

◆ output_deallocate_prepare_statement()

void output_deallocate_prepare_statement ( char *  name)

Definition at line 188 of file output.c.

References base_yyout, compat, connection, fprintf, free, output_escaped_str(), and whenever_action().

189 {
190  const char *con = connection ? connection : "NULL";
191 
192  if (strcmp(name, "all") != 0)
193  {
194  fprintf(base_yyout, "{ ECPGdeallocate(__LINE__, %d, %s, ", compat, con);
195  output_escaped_str(name, true);
196  fputs(");", base_yyout);
197  }
198  else
199  fprintf(base_yyout, "{ ECPGdeallocate_all(__LINE__, %d, %s);", compat, con);
200 
201  whenever_action(2);
202  free(name);
203  if (connection != NULL)
204  free(connection);
205  connection = NULL;
206 }
#define fprintf
Definition: port.h:196
void whenever_action(int mode)
Definition: output.c:66
static void output_escaped_str(char *cmd, bool quoted)
Definition: output.c:209
enum COMPAT_MODE compat
Definition: ecpg.c:25
#define free(a)
Definition: header.h:65
const char * name
Definition: encode.c:521
char * connection
FILE * base_yyout

◆ output_escaped_str()

static void output_escaped_str ( char *  cmd,
bool  quoted 
)
static

Definition at line 209 of file output.c.

References base_yyout, and i.

Referenced by output_deallocate_prepare_statement(), output_prepare_statement(), output_simple_statement(), and output_statement().

210 {
211  int i = 0;
212  int len = strlen(str);
213 
214  if (quoted && str[0] == '"' && str[len - 1] == '"') /* do not escape quotes
215  * at beginning and end
216  * if quoted string */
217  {
218  i = 1;
219  len--;
220  fputs("\"", base_yyout);
221  }
222 
223  /* output this char by char as we have to filter " and \n */
224  for (; i < len; i++)
225  {
226  if (str[i] == '"')
227  fputs("\\\"", base_yyout);
228  else if (str[i] == '\n')
229  fputs("\\\n", base_yyout);
230  else if (str[i] == '\\')
231  {
232  int j = i;
233 
234  /*
235  * check whether this is a continuation line if it is, do not
236  * output anything because newlines are escaped anyway
237  */
238 
239  /* accept blanks after the '\' as some other compilers do too */
240  do
241  {
242  j++;
243  } while (str[j] == ' ' || str[j] == '\t');
244 
245  if ((str[j] != '\n') && (str[j] != '\r' || str[j + 1] != '\n')) /* not followed by a
246  * newline */
247  fputs("\\\\", base_yyout);
248  }
249  else if (str[i] == '\r' && str[i + 1] == '\n')
250  {
251  fputs("\\\r\n", base_yyout);
252  i++;
253  }
254  else
255  fputc(str[i], base_yyout);
256  }
257 
258  if (quoted && str[0] == '"' && str[len] == '"')
259  fputs("\"", base_yyout);
260 }
int i
FILE * base_yyout

◆ output_line_number()

void output_line_number ( void  )

Definition at line 10 of file output.c.

References base_yyout, fprintf, free, and hashline_number().

Referenced by main(), output_simple_statement(), and whenever_action().

11 {
12  char *line = hashline_number();
13 
14  fprintf(base_yyout, "%s", line);
15  free(line);
16 }
#define fprintf
Definition: port.h:196
char * hashline_number(void)
Definition: output.c:94
#define free(a)
Definition: header.h:65
FILE * base_yyout

◆ output_prepare_statement()

void output_prepare_statement ( char *  name,
char *  stmt 
)

Definition at line 173 of file output.c.

References base_yyout, fprintf, free, output_escaped_str(), questionmarks, and whenever_action().

174 {
175  fprintf(base_yyout, "{ ECPGprepare(__LINE__, %s, %d, ", connection ? connection : "NULL", questionmarks);
176  output_escaped_str(name, true);
177  fputs(", ", base_yyout);
178  output_escaped_str(stmt, true);
179  fputs(");", base_yyout);
180  whenever_action(2);
181  free(name);
182  if (connection != NULL)
183  free(connection);
184  connection = NULL;
185 }
#define fprintf
Definition: port.h:196
void whenever_action(int mode)
Definition: output.c:66
static void output_escaped_str(char *cmd, bool quoted)
Definition: output.c:209
bool questionmarks
Definition: ecpg.c:19
#define free(a)
Definition: header.h:65
const char * name
Definition: encode.c:521
FILE * base_yyout

◆ output_simple_statement()

void output_simple_statement ( char *  stmt,
int  whenever_mode 
)

Definition at line 19 of file output.c.

References free, output_escaped_str(), output_line_number(), and whenever_action().

20 {
21  output_escaped_str(stmt, false);
22  if (whenever_mode)
23  whenever_action(whenever_mode);
25  free(stmt);
26 }
void output_line_number(void)
Definition: output.c:10
void whenever_action(int mode)
Definition: output.c:66
static void output_escaped_str(char *cmd, bool quoted)
Definition: output.c:209
#define free(a)
Definition: header.h:65

◆ output_statement()

void output_statement ( char *  stmt,
int  whenever_mode,
enum ECPG_statement_type  st 
)

Definition at line 136 of file output.c.

References argsinsert, argsresult, auto_prepare, base_yyout, compat, dump_variables(), ecpg_statement_type_name, ECPGst_exec_immediate, ECPGst_execute, ECPGst_normal, ECPGst_prepnormal, force_indicator, fprintf, free, output_escaped_str(), questionmarks, reset_variables(), and whenever_action().

137 {
138  fprintf(base_yyout, "{ ECPGdo(__LINE__, %d, %d, %s, %d, ", compat, force_indicator, connection ? connection : "NULL", questionmarks);
139 
140  if (st == ECPGst_prepnormal && !auto_prepare)
141  st = ECPGst_normal;
142 
143  /*
144  * In following cases, stmt is CSTRING or char_variable. They must be
145  * output directly. - prepared_name of EXECUTE without exprlist -
146  * execstring of EXECUTE IMMEDIATE
147  */
149  if (st == ECPGst_execute || st == ECPGst_exec_immediate)
150  fprintf(base_yyout, "%s, ", stmt);
151  else
152  {
153  fputs("\"", base_yyout);
154  output_escaped_str(stmt, false);
155  fputs("\", ", base_yyout);
156  }
157 
158  /* dump variables to C file */
160  fputs("ECPGt_EOIT, ", base_yyout);
162  fputs("ECPGt_EORT);", base_yyout);
163  reset_variables();
164 
165  whenever_action(whenever_mode | 2);
166  free(stmt);
167  if (connection != NULL)
168  free(connection);
169  connection = NULL;
170 }
static char * ecpg_statement_type_name[]
Definition: output.c:126
#define fprintf
Definition: port.h:196
void whenever_action(int mode)
Definition: output.c:66
void reset_variables(void)
Definition: variable.c:369
static void output_escaped_str(char *cmd, bool quoted)
Definition: output.c:209
void dump_variables(struct arguments *, int)
Definition: variable.c:438
bool questionmarks
Definition: ecpg.c:19
enum COMPAT_MODE compat
Definition: ecpg.c:25
#define free(a)
Definition: header.h:65
bool auto_prepare
Definition: ecpg.c:21
bool force_indicator
Definition: ecpg.c:18
struct arguments * argsresult
Definition: variable.c:366
struct arguments * argsinsert
Definition: variable.c:365
FILE * base_yyout

◆ print_action()

static void print_action ( struct when w)
static

Definition at line 37 of file output.c.

References base_yyout, when::code, when::command, fprintf, W_BREAK, W_CONTINUE, W_DO, W_GOTO, W_SQLPRINT, and W_STOP.

Referenced by whenever_action().

38 {
39  switch (w->code)
40  {
41  case W_SQLPRINT:
42  fprintf(base_yyout, "sqlprint();");
43  break;
44  case W_GOTO:
45  fprintf(base_yyout, "goto %s;", w->command);
46  break;
47  case W_DO:
48  fprintf(base_yyout, "%s;", w->command);
49  break;
50  case W_STOP:
51  fprintf(base_yyout, "exit (1);");
52  break;
53  case W_BREAK:
54  fprintf(base_yyout, "break;");
55  break;
56  case W_CONTINUE:
57  fprintf(base_yyout, "continue;");
58  break;
59  default:
60  fprintf(base_yyout, "{/* %d not implemented yet */}", w->code);
61  break;
62  }
63 }
#define fprintf
Definition: port.h:196
enum WHEN_TYPE code
Definition: type.h:84
char * command
Definition: type.h:85
Definition: type.h:79
Definition: type.h:78
Definition: type.h:75
Definition: type.h:77
FILE * base_yyout

◆ whenever_action()

void whenever_action ( int  mode)

Definition at line 66 of file output.c.

References base_yyout, when::code, fprintf, output_line_number(), print_action(), W_NOTHING, and when_warn.

Referenced by output_deallocate_prepare_statement(), output_get_descr(), output_get_descr_header(), output_prepare_statement(), output_set_descr(), output_set_descr_header(), output_simple_statement(), and output_statement().

67 {
68  if ((mode & 1) == 1 && when_nf.code != W_NOTHING)
69  {
71  fprintf(base_yyout, "\nif (sqlca.sqlcode == ECPG_NOT_FOUND) ");
72  print_action(&when_nf);
73  }
74  if (when_warn.code != W_NOTHING)
75  {
77  fprintf(base_yyout, "\nif (sqlca.sqlwarn[0] == 'W') ");
79  }
80  if (when_error.code != W_NOTHING)
81  {
83  fprintf(base_yyout, "\nif (sqlca.sqlcode < 0) ");
84  print_action(&when_error);
85  }
86 
87  if ((mode & 2) == 2)
88  fputc('}', base_yyout);
89 
91 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
static void print_action(struct when *w)
Definition: output.c:37
Definition: type.h:73
void output_line_number(void)
Definition: output.c:10
#define fprintf
Definition: port.h:196
enum WHEN_TYPE code
Definition: type.h:84
struct when when_error when_nf when_warn
Definition: output.c:32
FILE * base_yyout

Variable Documentation

◆ ecpg_statement_type_name

char* ecpg_statement_type_name[]
static
Initial value:
= {
"ECPGst_normal",
"ECPGst_execute",
"ECPGst_exec_immediate",
"ECPGst_prepnormal",
"ECPGst_prepare",
"ECPGst_exec_with_exprlist"
}

Definition at line 126 of file output.c.

Referenced by output_statement().

◆ when_warn

struct when when_error when_nf when_warn

Definition at line 32 of file output.c.

Referenced by main(), and whenever_action().