PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
output.c
Go to the documentation of this file.
1 /* src/interfaces/ecpg/preproc/output.c */
2 
3 #include "postgres_fe.h"
4 
5 #include "extern.h"
6 
7 static void output_escaped_str(char *cmd, bool quoted);
8 
9 void
11 {
12  char *line = hashline_number();
13 
14  fprintf(base_yyout, "%s", line);
15  free(line);
16 }
17 
18 void
20 {
21  output_escaped_str(stmt, false);
23  free(stmt);
24 }
25 
26 
27 /*
28  * store the whenever action here
29  */
30 struct when when_error,
31  when_nf,
32  when_warn;
33 
34 static void
35 print_action(struct when *w)
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 }
62 
63 void
64 whenever_action(int mode)
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 }
90 
91 char *
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 = mm_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 EMPTY;
122 }
123 
124 static char *ecpg_statement_type_name[] = {
125  "ECPGst_normal",
126  "ECPGst_execute",
127  "ECPGst_exec_immediate",
128  "ECPGst_prepnormal"
129 };
130 
131 void
132 output_statement(char *stmt, int whenever_mode, enum ECPG_statement_type st)
133 {
134  fprintf(base_yyout, "{ ECPGdo(__LINE__, %d, %d, %s, %d, ", compat, force_indicator, connection ? connection : "NULL", questionmarks);
135  if (st == ECPGst_execute || st == ECPGst_exec_immediate)
136  {
137  fprintf(base_yyout, "%s, %s, ", ecpg_statement_type_name[st], stmt);
138  }
139  else
140  {
141  if (st == ECPGst_prepnormal && auto_prepare)
142  fputs("ECPGst_prepnormal, \"", base_yyout);
143  else
144  fputs("ECPGst_normal, \"", base_yyout);
145 
146  output_escaped_str(stmt, false);
147  fputs("\", ", base_yyout);
148  }
149 
150  /* dump variables to C file */
152  fputs("ECPGt_EOIT, ", base_yyout);
154  fputs("ECPGt_EORT);", base_yyout);
155  reset_variables();
156 
157  whenever_action(whenever_mode | 2);
158  free(stmt);
159  if (connection != NULL)
160  free(connection);
161 }
162 
163 void
164 output_prepare_statement(char *name, char *stmt)
165 {
166  fprintf(base_yyout, "{ ECPGprepare(__LINE__, %s, %d, ", connection ? connection : "NULL", questionmarks);
167  output_escaped_str(name, true);
168  fputs(", ", base_yyout);
169  output_escaped_str(stmt, true);
170  fputs(");", base_yyout);
171  whenever_action(2);
172  free(name);
173  if (connection != NULL)
174  free(connection);
175 }
176 
177 void
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  free(name);
193  if (connection != NULL)
194  free(connection);
195 }
196 
197 static void
198 output_escaped_str(char *str, bool quoted)
199 {
200  int i = 0;
201  int len = strlen(str);
202 
203  if (quoted && str[0] == '"' && str[len - 1] == '"') /* do not escape quotes
204  * at beginning and end
205  * if quoted string */
206  {
207  i = 1;
208  len--;
209  fputs("\"", base_yyout);
210  }
211 
212  /* output this char by char as we have to filter " and \n */
213  for (; i < len; i++)
214  {
215  if (str[i] == '"')
216  fputs("\\\"", base_yyout);
217  else if (str[i] == '\n')
218  fputs("\\\n", base_yyout);
219  else if (str[i] == '\\')
220  {
221  int j = i;
222 
223  /*
224  * check whether this is a continuation line if it is, do not
225  * output anything because newlines are escaped anyway
226  */
227 
228  /* accept blanks after the '\' as some other compilers do too */
229  do
230  {
231  j++;
232  } while (str[j] == ' ' || str[j] == '\t');
233 
234  if ((str[j] != '\n') && (str[j] != '\r' || str[j + 1] != '\n')) /* not followed by a
235  * newline */
236  fputs("\\\\", base_yyout);
237  }
238  else if (str[i] == '\r' && str[i + 1] == '\n')
239  {
240  fputs("\\\r\n", base_yyout);
241  i++;
242  }
243  else
244  fputc(str[i], base_yyout);
245  }
246 
247  if (quoted && str[0] == '"' && str[len] == '"')
248  fputs("\"", base_yyout);
249 }
void output_simple_statement(char *stmt)
Definition: output.c:19
int base_yylineno
static void print_action(struct when *w)
Definition: output.c:35
Definition: type.h:73
static char * ecpg_statement_type_name[]
Definition: output.c:124
void output_statement(char *stmt, int whenever_mode, enum ECPG_statement_type st)
Definition: output.c:132
void output_line_number(void)
Definition: output.c:10
enum WHEN_TYPE code
Definition: type.h:84
void whenever_action(int mode)
Definition: output.c:64
struct arguments * argsresult
Definition: variable.c:366
ECPG_statement_type
Definition: ecpgtype.h:94
char * hashline_number(void)
Definition: output.c:92
void * mm_alloc(size_t)
static void output_escaped_str(char *cmd, bool quoted)
Definition: output.c:198
char * input_filename
struct arguments * argsinsert
Definition: variable.c:365
char * command
Definition: type.h:85
Definition: type.h:82
bool questionmarks
Definition: ecpg.c:19
FILE * base_yyout
enum COMPAT_MODE compat
Definition: ecpg.c:25
void output_prepare_statement(char *name, char *stmt)
Definition: output.c:164
Definition: type.h:79
Definition: type.h:78
#define free(a)
Definition: header.h:65
void output_deallocate_prepare_statement(char *name)
Definition: output.c:178
Definition: type.h:75
bool auto_prepare
Definition: ecpg.c:21
#define EMPTY
Definition: regcomp.c:274
const char * name
Definition: encode.c:521
bool force_indicator
Definition: ecpg.c:18
int i
Definition: type.h:77
void reset_variables(void)
Definition: variable.c:369
char * str
Definition: type.h:86
void dump_variables(struct arguments *, int)
Definition: variable.c:438
struct when when_error when_nf when_warn
Definition: output.c:30