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 *cmd, bool quoted);
8 :
9 : void
10 4942 : output_line_number(void)
11 : {
12 4942 : char *line = hashline_number();
13 :
14 4942 : fprintf(base_yyout, "%s", line);
15 4942 : free(line);
16 4942 : }
17 :
18 : void
19 278 : output_simple_statement(char *stmt, int whenever_mode)
20 : {
21 278 : output_escaped_str(stmt, false);
22 278 : if (whenever_mode)
23 22 : whenever_action(whenever_mode);
24 278 : output_line_number();
25 278 : free(stmt);
26 278 : }
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 1994 : print_action(struct when *w)
38 : {
39 1994 : switch (w->code)
40 : {
41 1040 : case W_SQLPRINT:
42 1040 : fprintf(base_yyout, "sqlprint();");
43 1040 : break;
44 4 : case W_GOTO:
45 4 : fprintf(base_yyout, "goto %s;", w->command);
46 4 : break;
47 342 : case W_DO:
48 342 : fprintf(base_yyout, "%s;", w->command);
49 342 : break;
50 582 : case W_STOP:
51 582 : fprintf(base_yyout, "exit (1);");
52 582 : 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 1994 : }
64 :
65 : void
66 1956 : whenever_action(int mode)
67 : {
68 1956 : if ((mode & 1) == 1 && when_nf.code != W_NOTHING)
69 : {
70 56 : output_line_number();
71 56 : fprintf(base_yyout, "\nif (sqlca.sqlcode == ECPG_NOT_FOUND) ");
72 56 : print_action(&when_nf);
73 : }
74 1956 : if (when_warn.code != W_NOTHING)
75 : {
76 286 : output_line_number();
77 286 : fprintf(base_yyout, "\nif (sqlca.sqlwarn[0] == 'W') ");
78 286 : print_action(&when_warn);
79 : }
80 1956 : if (when_error.code != W_NOTHING)
81 : {
82 1652 : output_line_number();
83 1652 : fprintf(base_yyout, "\nif (sqlca.sqlcode < 0) ");
84 1652 : print_action(&when_error);
85 : }
86 :
87 1956 : if ((mode & 2) == 2)
88 1866 : fputc('}', base_yyout);
89 :
90 1956 : output_line_number();
91 1956 : }
92 :
93 : char *
94 5520 : hashline_number(void)
95 : {
96 : /* do not print line numbers if we are in debug mode */
97 5520 : if (input_filename
98 : #ifdef YYDEBUG
99 : && !base_yydebug
100 : #endif
101 : )
102 : {
103 : /* "* 2" here is for escaping '\' and '"' below */
104 5520 : 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 5520 : sprintf(line, "\n#line %d \"", base_yylineno);
109 5520 : src = input_filename;
110 5520 : dest = line + strlen(line);
111 70846 : while (*src)
112 : {
113 65326 : if (*src == '\\' || *src == '"')
114 0 : *dest++ = '\\';
115 65326 : *dest++ = *src++;
116 : }
117 5520 : *dest = '\0';
118 5520 : strcat(dest, "\"\n");
119 :
120 5520 : 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 1034 : output_statement(char *stmt, int whenever_mode, enum ECPG_statement_type st)
137 : {
138 1034 : fprintf(base_yyout, "{ ECPGdo(__LINE__, %d, %d, %s, %d, ", compat, force_indicator, connection ? connection : "NULL", questionmarks);
139 :
140 1034 : if (st == ECPGst_prepnormal && !auto_prepare)
141 366 : 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 1034 : fprintf(base_yyout, "%s, ", ecpg_statement_type_name[st]);
149 1034 : if (st == ECPGst_execute || st == ECPGst_exec_immediate)
150 62 : fprintf(base_yyout, "%s, ", stmt);
151 : else
152 : {
153 972 : fputs("\"", base_yyout);
154 972 : output_escaped_str(stmt, false);
155 972 : fputs("\", ", base_yyout);
156 : }
157 :
158 : /* dump variables to C file */
159 1034 : dump_variables(argsinsert, 1);
160 1034 : fputs("ECPGt_EOIT, ", base_yyout);
161 1034 : dump_variables(argsresult, 1);
162 1034 : fputs("ECPGt_EORT);", base_yyout);
163 1034 : reset_variables();
164 :
165 1034 : whenever_action(whenever_mode | 2);
166 1034 : free(stmt);
167 1034 : }
168 :
169 : void
170 94 : output_prepare_statement(char *name, char *stmt)
171 : {
172 94 : fprintf(base_yyout, "{ ECPGprepare(__LINE__, %s, %d, ", connection ? connection : "NULL", questionmarks);
173 94 : output_escaped_str(name, true);
174 94 : fputs(", ", base_yyout);
175 94 : output_escaped_str(stmt, true);
176 94 : fputs(");", base_yyout);
177 94 : whenever_action(2);
178 94 : free(name);
179 94 : }
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 1512 : output_escaped_str(char *str, bool quoted)
201 : {
202 1512 : int i = 0;
203 1512 : int len = strlen(str);
204 :
205 1512 : if (quoted && str[0] == '"' && str[len - 1] == '"') /* do not escape quotes
206 : * at beginning and end
207 : * if quoted string */
208 : {
209 166 : i = 1;
210 166 : len--;
211 166 : fputs("\"", base_yyout);
212 : }
213 :
214 : /* output this char by char as we have to filter " and \n */
215 55616 : for (; i < len; i++)
216 : {
217 54104 : if (str[i] == '"')
218 104 : fputs("\\\"", base_yyout);
219 54000 : else if (str[i] == '\n')
220 36 : fputs("\\\n", base_yyout);
221 53964 : 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 53920 : else if (str[i] == '\r' && str[i + 1] == '\n')
241 : {
242 0 : fputs("\\\r\n", base_yyout);
243 0 : i++;
244 : }
245 : else
246 53920 : fputc(str[i], base_yyout);
247 : }
248 :
249 1512 : if (quoted && str[0] == '"' && str[len] == '"')
250 166 : fputs("\"", base_yyout);
251 1512 : }
|