PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 (const char *str, bool quoted)
 
void output_line_number (void)
 
void output_simple_statement (const char *stmt, int whenever_mode)
 
static void print_action (struct when *w)
 
void whenever_action (int mode)
 
char * hashline_number (void)
 
void output_statement (const char *stmt, int whenever_mode, enum ECPG_statement_type st)
 
void output_prepare_statement (const char *name, const char *stmt)
 
void output_deallocate_prepare_statement (const 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 92 of file output.c.

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

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

Referenced by output_line_number().

◆ output_deallocate_prepare_statement()

void output_deallocate_prepare_statement ( const char *  name)

Definition at line 178 of file output.c.

179 {
180  const char *con = connection ? connection : "NULL";
181 
182  if (strcmp(name, "all") != 0)
183  {
184  fprintf(base_yyout, "{ ECPGdeallocate(__LINE__, %d, %s, ", compat, con);
185  output_escaped_str(name, true);
186  fputs(");", base_yyout);
187  }
188  else
189  fprintf(base_yyout, "{ ECPGdeallocate_all(__LINE__, %d, %s);", compat, con);
190 
191  whenever_action(2);
192 }
enum COMPAT_MODE compat
Definition: ecpg.c:25
void whenever_action(int mode)
Definition: output.c:64
static void output_escaped_str(const char *str, bool quoted)
Definition: output.c:195
#define fprintf
Definition: port.h:242
char * connection
FILE * base_yyout
const char * name

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

◆ output_escaped_str()

static void output_escaped_str ( const char *  str,
bool  quoted 
)
static

Definition at line 195 of file output.c.

196 {
197  int i = 0;
198  int len = strlen(str);
199 
200  if (quoted && str[0] == '"' && str[len - 1] == '"') /* do not escape quotes
201  * at beginning and end
202  * if quoted string */
203  {
204  i = 1;
205  len--;
206  fputs("\"", base_yyout);
207  }
208 
209  /* output this char by char as we have to filter " and \n */
210  for (; i < len; i++)
211  {
212  if (str[i] == '"')
213  fputs("\\\"", base_yyout);
214  else if (str[i] == '\n')
215  fputs("\\\n", base_yyout);
216  else if (str[i] == '\\')
217  {
218  int j = i;
219 
220  /*
221  * check whether this is a continuation line if it is, do not
222  * output anything because newlines are escaped anyway
223  */
224 
225  /* accept blanks after the '\' as some other compilers do too */
226  do
227  {
228  j++;
229  } while (str[j] == ' ' || str[j] == '\t');
230 
231  if ((str[j] != '\n') && (str[j] != '\r' || str[j + 1] != '\n')) /* not followed by a
232  * newline */
233  fputs("\\\\", base_yyout);
234  }
235  else if (str[i] == '\r' && str[i + 1] == '\n')
236  {
237  fputs("\\\r\n", base_yyout);
238  i++;
239  }
240  else
241  fputc(str[i], base_yyout);
242  }
243 
244  if (quoted && str[0] == '"' && str[len] == '"')
245  fputs("\"", base_yyout);
246 }
const char * str
int j
Definition: isn.c:73
int i
Definition: isn.c:72
const void size_t len

References base_yyout, i, j, len, and str.

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

◆ output_line_number()

void output_line_number ( void  )

Definition at line 10 of file output.c.

11 {
12  char *line = hashline_number();
13 
14  fprintf(base_yyout, "%s", line);
15 }
char * hashline_number(void)
Definition: output.c:92

References base_yyout, fprintf, and hashline_number().

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

◆ output_prepare_statement()

void output_prepare_statement ( const char *  name,
const char *  stmt 
)

Definition at line 167 of file output.c.

168 {
169  fprintf(base_yyout, "{ ECPGprepare(__LINE__, %s, %d, ", connection ? connection : "NULL", questionmarks);
170  output_escaped_str(name, true);
171  fputs(", ", base_yyout);
172  output_escaped_str(stmt, true);
173  fputs(");", base_yyout);
174  whenever_action(2);
175 }
bool questionmarks
Definition: ecpg.c:19
#define stmt
Definition: indent_codes.h:59

References base_yyout, fprintf, name, output_escaped_str(), questionmarks, stmt, and whenever_action().

◆ output_simple_statement()

void output_simple_statement ( const char *  stmt,
int  whenever_mode 
)

Definition at line 18 of file output.c.

19 {
20  output_escaped_str(stmt, false);
21  if (whenever_mode)
22  whenever_action(whenever_mode);
24 }
void output_line_number(void)
Definition: output.c:10

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

◆ output_statement()

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

Definition at line 134 of file output.c.

135 {
136  fprintf(base_yyout, "{ ECPGdo(__LINE__, %d, %d, %s, %d, ", compat, force_indicator, connection ? connection : "NULL", questionmarks);
137 
138  if (st == ECPGst_prepnormal && !auto_prepare)
139  st = ECPGst_normal;
140 
141  /*
142  * In following cases, stmt is CSTRING or char_variable. They must be
143  * output directly. - prepared_name of EXECUTE without exprlist -
144  * execstring of EXECUTE IMMEDIATE
145  */
147  if (st == ECPGst_execute || st == ECPGst_exec_immediate)
148  fprintf(base_yyout, "%s, ", stmt);
149  else
150  {
151  fputs("\"", base_yyout);
152  output_escaped_str(stmt, false);
153  fputs("\", ", base_yyout);
154  }
155 
156  /* dump variables to C file */
158  fputs("ECPGt_EOIT, ", base_yyout);
160  fputs("ECPGt_EORT);", base_yyout);
161  reset_variables();
162 
163  whenever_action(whenever_mode | 2);
164 }
bool auto_prepare
Definition: ecpg.c:21
bool force_indicator
Definition: ecpg.c:18
@ ECPGst_normal
Definition: ecpgtype.h:97
@ ECPGst_execute
Definition: ecpgtype.h:98
@ ECPGst_exec_immediate
Definition: ecpgtype.h:99
@ ECPGst_prepnormal
Definition: ecpgtype.h:100
static char * ecpg_statement_type_name[]
Definition: output.c:124
void dump_variables(struct arguments *list, int mode)
Definition: variable.c:441
void reset_variables(void)
Definition: variable.c:372
struct arguments * argsresult
Definition: variable.c:369
struct arguments * argsinsert
Definition: variable.c:368

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, output_escaped_str(), questionmarks, reset_variables(), stmt, and whenever_action().

◆ print_action()

static void print_action ( struct when w)
static

Definition at line 35 of file output.c.

36 {
37  switch (w->code)
38  {
39  case W_SQLPRINT:
40  fprintf(base_yyout, "sqlprint();");
41  break;
42  case W_GOTO:
43  fprintf(base_yyout, "goto %s;", w->command);
44  break;
45  case W_DO:
46  fprintf(base_yyout, "%s;", w->command);
47  break;
48  case W_STOP:
49  fprintf(base_yyout, "exit (1);");
50  break;
51  case W_BREAK:
52  fprintf(base_yyout, "break;");
53  break;
54  case W_CONTINUE:
55  fprintf(base_yyout, "continue;");
56  break;
57  default:
58  fprintf(base_yyout, "{/* %d not implemented yet */}", w->code);
59  break;
60  }
61 }
char * command
Definition: type.h:90
enum WHEN_TYPE code
Definition: type.h:89
@ W_STOP
Definition: type.h:84
@ W_GOTO
Definition: type.h:82
@ W_SQLPRINT
Definition: type.h:81
@ W_BREAK
Definition: type.h:80
@ W_CONTINUE
Definition: type.h:79
@ W_DO
Definition: type.h:83

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().

◆ whenever_action()

void whenever_action ( int  mode)

Definition at line 64 of file output.c.

65 {
66  if ((mode & 1) == 1 && when_nf.code != W_NOTHING)
67  {
69  fprintf(base_yyout, "\nif (sqlca.sqlcode == ECPG_NOT_FOUND) ");
70  print_action(&when_nf);
71  }
72  if (when_warn.code != W_NOTHING)
73  {
75  fprintf(base_yyout, "\nif (sqlca.sqlwarn[0] == 'W') ");
77  }
78  if (when_error.code != W_NOTHING)
79  {
81  fprintf(base_yyout, "\nif (sqlca.sqlcode < 0) ");
82  print_action(&when_error);
83  }
84 
85  if ((mode & 2) == 2)
86  fputc('}', base_yyout);
87 
89 }
struct when when_error when_nf when_warn
Definition: output.c:30
static void print_action(struct when *w)
Definition: output.c:35
static PgChecksumMode mode
Definition: pg_checksums.c:55
@ W_NOTHING
Definition: type.h:78

References base_yyout, when::code, fprintf, mode, 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().

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 124 of file output.c.

Referenced by output_statement().

◆ when_warn

struct when when_error when_nf when_warn

Definition at line 18 of file output.c.

Referenced by main(), and whenever_action().