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

char* hashline_number ( void  )

Definition at line 89 of file output.c.

References base_yylineno, EMPTY, input_filename, and mm_alloc().

Referenced by output_line_number().

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

Definition at line 175 of file output.c.

References base_yyout, compat, free, NULL, output_escaped_str(), and whenever_action().

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

Definition at line 195 of file output.c.

References base_yyout, and i.

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

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 }
FILE * base_yyout
int i
void output_line_number ( void  )

Definition at line 10 of file output.c.

References base_yyout, 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 }
char * hashline_number(void)
Definition: output.c:89
FILE * base_yyout
#define free(a)
Definition: header.h:65
void output_prepare_statement ( char *  name,
char *  stmt 
)

Definition at line 161 of file output.c.

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

162 {
163  fprintf(base_yyout, "{ ECPGprepare(__LINE__, %s, %d, ", connection ? connection : "NULL", questionmarks);
164  output_escaped_str(name, true);
165  fputs(", ", base_yyout);
166  output_escaped_str(stmt, true);
167  fputs(");", base_yyout);
168  whenever_action(2);
169  free(name);
170  if (connection != NULL)
171  free(connection);
172 }
void whenever_action(int mode)
Definition: output.c:61
static void output_escaped_str(char *cmd, bool quoted)
Definition: output.c:195
bool questionmarks
Definition: ecpg.c:19
FILE * base_yyout
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
const char * name
Definition: encode.c:521
void output_simple_statement ( char *  stmt)

Definition at line 19 of file output.c.

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

20 {
21  output_escaped_str(stmt, false);
23  free(stmt);
24 }
void output_line_number(void)
Definition: output.c:10
static void output_escaped_str(char *cmd, bool quoted)
Definition: output.c:195
#define free(a)
Definition: header.h:65
void output_statement ( char *  stmt,
int  whenever_mode,
enum ECPG_statement_type  st 
)

Definition at line 129 of file output.c.

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

130 {
131  fprintf(base_yyout, "{ ECPGdo(__LINE__, %d, %d, %s, %d, ", compat, force_indicator, connection ? connection : "NULL", questionmarks);
132  if (st == ECPGst_execute || st == ECPGst_exec_immediate)
133  {
134  fprintf(base_yyout, "%s, %s, ", ecpg_statement_type_name[st], stmt);
135  }
136  else
137  {
138  if (st == ECPGst_prepnormal && auto_prepare)
139  fputs("ECPGst_prepnormal, \"", base_yyout);
140  else
141  fputs("ECPGst_normal, \"", base_yyout);
142 
143  output_escaped_str(stmt, false);
144  fputs("\", ", base_yyout);
145  }
146 
147  /* dump variables to C file */
149  fputs("ECPGt_EOIT, ", base_yyout);
151  fputs("ECPGt_EORT);", base_yyout);
152  reset_variables();
153 
154  whenever_action(whenever_mode | 2);
155  free(stmt);
156  if (connection != NULL)
157  free(connection);
158 }
static char * ecpg_statement_type_name[]
Definition: output.c:121
void whenever_action(int mode)
Definition: output.c:61
struct arguments * argsresult
Definition: variable.c:366
static void output_escaped_str(char *cmd, bool quoted)
Definition: output.c:195
struct arguments * argsinsert
Definition: variable.c:365
bool questionmarks
Definition: ecpg.c:19
FILE * base_yyout
enum COMPAT_MODE compat
Definition: ecpg.c:25
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
bool auto_prepare
Definition: ecpg.c:21
bool force_indicator
Definition: ecpg.c:18
void reset_variables(void)
Definition: variable.c:369
void dump_variables(struct arguments *, int)
Definition: variable.c:438
static void print_action ( struct when w)
static

Definition at line 35 of file output.c.

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

Referenced by whenever_action().

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  default:
55  fprintf(base_yyout, "{/* %d not implemented yet */}", w->code);
56  break;
57  }
58 }
enum WHEN_TYPE code
Definition: type.h:85
char * command
Definition: type.h:86
FILE * base_yyout
Definition: type.h:80
Definition: type.h:79
Definition: type.h:76
Definition: type.h:78
void whenever_action ( int  mode)

Definition at line 61 of file output.c.

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

62 {
63  if ((mode & 1) == 1 && when_nf.code != W_NOTHING)
64  {
66  fprintf(base_yyout, "\nif (sqlca.sqlcode == ECPG_NOT_FOUND) ");
67  print_action(&when_nf);
68  }
69  if (when_warn.code != W_NOTHING)
70  {
72  fprintf(base_yyout, "\nif (sqlca.sqlwarn[0] == 'W') ");
74  }
75  if (when_error.code != W_NOTHING)
76  {
78  fprintf(base_yyout, "\nif (sqlca.sqlcode < 0) ");
79  print_action(&when_error);
80  }
81 
82  if ((mode & 2) == 2)
83  fputc('}', base_yyout);
84 
86 }
static void print_action(struct when *w)
Definition: output.c:35
Definition: type.h:74
void output_line_number(void)
Definition: output.c:10
enum WHEN_TYPE code
Definition: type.h:85
FILE * base_yyout
struct when when_error when_nf when_warn
Definition: output.c:30

Variable Documentation

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

Definition at line 121 of file output.c.

Referenced by output_statement().

struct when when_error when_nf when_warn

Definition at line 30 of file output.c.

Referenced by main(), and whenever_action().