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  default:
55  fprintf(base_yyout, "{/* %d not implemented yet */}", w->code);
56  break;
57  }
58 }
59 
60 void
61 whenever_action(int mode)
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 }
87 
88 char *
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 }
120 
121 static char *ecpg_statement_type_name[] = {
122  "ECPGst_normal",
123  "ECPGst_execute",
124  "ECPGst_exec_immediate",
125  "ECPGst_prepnormal"
126 };
127 
128 void
129 output_statement(char *stmt, int whenever_mode, enum ECPG_statement_type st)
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 }
159 
160 void
161 output_prepare_statement(char *name, char *stmt)
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 }
173 
174 void
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 }
193 
194 static void
195 output_escaped_str(char *str, bool quoted)
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 }
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:121
void output_statement(char *stmt, int whenever_mode, enum ECPG_statement_type st)
Definition: output.c:129
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:61
struct arguments * argsresult
Definition: variable.c:366
ECPG_statement_type
Definition: ecpgtype.h:94
char * hashline_number(void)
Definition: output.c:89
void * mm_alloc(size_t)
static void output_escaped_str(char *cmd, bool quoted)
Definition: output.c:195
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:161
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:175
#define NULL
Definition: c.h:229
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