Line data Source code
1 : /* src/interfaces/ecpg/preproc/output.c */
2 :
3 : #include "postgres_fe.h"
4 :
5 : #include "preproc_extern.h"
6 :
7 : static void output_escaped_str(char *str, bool quoted);
8 :
9 : void
10 5122 : output_line_number(void)
11 : {
12 5122 : char *line = hashline_number();
13 :
14 5122 : fprintf(base_yyout, "%s", line);
15 5122 : free(line);
16 5122 : }
17 :
18 : void
19 286 : output_simple_statement(char *stmt, int whenever_mode)
20 : {
21 286 : output_escaped_str(stmt, false);
22 286 : if (whenever_mode)
23 22 : whenever_action(whenever_mode);
24 286 : output_line_number();
25 286 : free(stmt);
26 286 : }
27 :
28 :
29 : /*
30 : * store the whenever action here
31 : */
32 : struct when when_error,
33 : when_nf,
34 : when_warn;
35 :
36 : static void
37 2078 : print_action(struct when *w)
38 : {
39 2078 : switch (w->code)
40 : {
41 1114 : case W_SQLPRINT:
42 1114 : fprintf(base_yyout, "sqlprint();");
43 1114 : break;
44 4 : case W_GOTO:
45 4 : fprintf(base_yyout, "goto %s;", w->command);
46 4 : break;
47 340 : case W_DO:
48 340 : fprintf(base_yyout, "%s;", w->command);
49 340 : break;
50 594 : case W_STOP:
51 594 : fprintf(base_yyout, "exit (1);");
52 594 : break;
53 24 : case W_BREAK:
54 24 : fprintf(base_yyout, "break;");
55 24 : break;
56 2 : case W_CONTINUE:
57 2 : fprintf(base_yyout, "continue;");
58 2 : break;
59 0 : default:
60 0 : fprintf(base_yyout, "{/* %d not implemented yet */}", w->code);
61 0 : break;
62 : }
63 2078 : }
64 :
65 : void
66 2028 : whenever_action(int mode)
67 : {
68 2028 : if ((mode & 1) == 1 && when_nf.code != W_NOTHING)
69 : {
70 58 : output_line_number();
71 58 : fprintf(base_yyout, "\nif (sqlca.sqlcode == ECPG_NOT_FOUND) ");
72 58 : print_action(&when_nf);
73 : }
74 2028 : if (when_warn.code != W_NOTHING)
75 : {
76 296 : output_line_number();
77 296 : fprintf(base_yyout, "\nif (sqlca.sqlwarn[0] == 'W') ");
78 296 : print_action(&when_warn);
79 : }
80 2028 : if (when_error.code != W_NOTHING)
81 : {
82 1724 : output_line_number();
83 1724 : fprintf(base_yyout, "\nif (sqlca.sqlcode < 0) ");
84 1724 : print_action(&when_error);
85 : }
86 :
87 2028 : if ((mode & 2) == 2)
88 1938 : fputc('}', base_yyout);
89 :
90 2028 : output_line_number();
91 2028 : }
92 :
93 : char *
94 5708 : hashline_number(void)
95 : {
96 : /* do not print line numbers if we are in debug mode */
97 5708 : if (input_filename
98 : #ifdef YYDEBUG
99 : && !base_yydebug
100 : #endif
101 : )
102 : {
103 : /* "* 2" here is for escaping '\' and '"' below */
104 5708 : 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 5708 : sprintf(line, "\n#line %d \"", base_yylineno);
109 5708 : src = input_filename;
110 5708 : dest = line + strlen(line);
111 73286 : while (*src)
112 : {
113 67578 : if (*src == '\\' || *src == '"')
114 0 : *dest++ = '\\';
115 67578 : *dest++ = *src++;
116 : }
117 5708 : *dest = '\0';
118 5708 : strcat(dest, "\"\n");
119 :
120 5708 : return line;
121 : }
122 :
123 0 : return EMPTY;
124 : }
125 :
126 : static char *ecpg_statement_type_name[] = {
127 : "ECPGst_normal",
128 : "ECPGst_execute",
129 : "ECPGst_exec_immediate",
130 : "ECPGst_prepnormal",
131 : "ECPGst_prepare",
132 : "ECPGst_exec_with_exprlist"
133 : };
134 :
135 : void
136 1096 : output_statement(char *stmt, int whenever_mode, enum ECPG_statement_type st)
137 : {
138 1096 : fprintf(base_yyout, "{ ECPGdo(__LINE__, %d, %d, %s, %d, ", compat, force_indicator, connection ? connection : "NULL", questionmarks);
139 :
140 1096 : if (st == ECPGst_prepnormal && !auto_prepare)
141 422 : 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 : */
148 1096 : fprintf(base_yyout, "%s, ", ecpg_statement_type_name[st]);
149 1096 : if (st == ECPGst_execute || st == ECPGst_exec_immediate)
150 62 : fprintf(base_yyout, "%s, ", stmt);
151 : else
152 : {
153 1034 : fputs("\"", base_yyout);
154 1034 : output_escaped_str(stmt, false);
155 1034 : fputs("\", ", base_yyout);
156 : }
157 :
158 : /* dump variables to C file */
159 1096 : dump_variables(argsinsert, 1);
160 1096 : fputs("ECPGt_EOIT, ", base_yyout);
161 1096 : dump_variables(argsresult, 1);
162 1096 : fputs("ECPGt_EORT);", base_yyout);
163 1096 : reset_variables();
164 :
165 1096 : whenever_action(whenever_mode | 2);
166 1096 : free(stmt);
167 1096 : }
168 :
169 : void
170 96 : output_prepare_statement(char *name, char *stmt)
171 : {
172 96 : fprintf(base_yyout, "{ ECPGprepare(__LINE__, %s, %d, ", connection ? connection : "NULL", questionmarks);
173 96 : output_escaped_str(name, true);
174 96 : fputs(", ", base_yyout);
175 96 : output_escaped_str(stmt, true);
176 96 : fputs(");", base_yyout);
177 96 : whenever_action(2);
178 96 : free(name);
179 96 : }
180 :
181 : void
182 76 : output_deallocate_prepare_statement(char *name)
183 : {
184 76 : const char *con = connection ? connection : "NULL";
185 :
186 76 : if (strcmp(name, "all") != 0)
187 : {
188 74 : fprintf(base_yyout, "{ ECPGdeallocate(__LINE__, %d, %s, ", compat, con);
189 74 : output_escaped_str(name, true);
190 74 : fputs(");", base_yyout);
191 : }
192 : else
193 2 : fprintf(base_yyout, "{ ECPGdeallocate_all(__LINE__, %d, %s);", compat, con);
194 :
195 76 : whenever_action(2);
196 76 : free(name);
197 76 : }
198 :
199 : static void
200 1586 : output_escaped_str(char *str, bool quoted)
201 : {
202 1586 : int i = 0;
203 1586 : int len = strlen(str);
204 :
205 1586 : if (quoted && str[0] == '"' && str[len - 1] == '"') /* do not escape quotes
206 : * at beginning and end
207 : * if quoted string */
208 : {
209 170 : i = 1;
210 170 : len--;
211 170 : fputs("\"", base_yyout);
212 : }
213 :
214 : /* output this char by char as we have to filter " and \n */
215 59378 : for (; i < len; i++)
216 : {
217 57792 : if (str[i] == '"')
218 196 : fputs("\\\"", base_yyout);
219 57596 : else if (str[i] == '\n')
220 36 : fputs("\\\n", base_yyout);
221 57560 : else if (str[i] == '\\')
222 : {
223 44 : int j = i;
224 :
225 : /*
226 : * check whether this is a continuation line if it is, do not
227 : * output anything because newlines are escaped anyway
228 : */
229 :
230 : /* accept blanks after the '\' as some other compilers do too */
231 : do
232 : {
233 44 : j++;
234 44 : } while (str[j] == ' ' || str[j] == '\t');
235 :
236 44 : if ((str[j] != '\n') && (str[j] != '\r' || str[j + 1] != '\n')) /* not followed by a
237 : * newline */
238 44 : fputs("\\\\", base_yyout);
239 : }
240 57516 : else if (str[i] == '\r' && str[i + 1] == '\n')
241 : {
242 0 : fputs("\\\r\n", base_yyout);
243 0 : i++;
244 : }
245 : else
246 57516 : fputc(str[i], base_yyout);
247 : }
248 :
249 1586 : if (quoted && str[0] == '"' && str[len] == '"')
250 170 : fputs("\"", base_yyout);
251 1586 : }
|