Fix xslt_process() to ensure that it inserts a NULL terminator after the
[PostgreSQL.git] / src / interfaces / ecpg / preproc / output.c
blobba41fafe72f8e890f3106c07549d17bbc6f1bfbf
1 /* $PostgreSQL$ */
3 #include "postgres_fe.h"
5 #include "extern.h"
7 static void output_escaped_str(char *cmd, bool quoted);
9 void
10 output_line_number(void)
12 char *line = hashline_number();
14 fprintf(yyout, "%s", line);
15 free(line);
18 void
19 output_simple_statement(char *stmt)
21 output_escaped_str(stmt, false);
22 output_line_number();
23 free(stmt);
28 * store the whenever action here
30 struct when when_error,
31 when_nf,
32 when_warn;
34 static void
35 print_action(struct when * w)
37 switch (w->code)
39 case W_SQLPRINT:
40 fprintf(yyout, "sqlprint();");
41 break;
42 case W_GOTO:
43 fprintf(yyout, "goto %s;", w->command);
44 break;
45 case W_DO:
46 fprintf(yyout, "%s;", w->command);
47 break;
48 case W_STOP:
49 fprintf(yyout, "exit (1);");
50 break;
51 case W_BREAK:
52 fprintf(yyout, "break;");
53 break;
54 default:
55 fprintf(yyout, "{/* %d not implemented yet */}", w->code);
56 break;
60 void
61 whenever_action(int mode)
63 if ((mode & 1) == 1 && when_nf.code != W_NOTHING)
65 output_line_number();
66 fprintf(yyout, "\nif (sqlca.sqlcode == ECPG_NOT_FOUND) ");
67 print_action(&when_nf);
69 if (when_warn.code != W_NOTHING)
71 output_line_number();
72 fprintf(yyout, "\nif (sqlca.sqlwarn[0] == 'W') ");
73 print_action(&when_warn);
75 if (when_error.code != W_NOTHING)
77 output_line_number();
78 fprintf(yyout, "\nif (sqlca.sqlcode < 0) ");
79 print_action(&when_error);
82 if ((mode & 2) == 2)
83 fputc('}', yyout);
85 output_line_number();
88 char *
89 hashline_number(void)
91 /* do not print line numbers if we are in debug mode */
92 if (input_filename
93 #ifdef YYDEBUG
94 && !yydebug
95 #endif
98 char *line = mm_alloc(strlen("\n#line %d \"%s\"\n") + sizeof(int) * CHAR_BIT * 10 / 3 + strlen(input_filename));
100 sprintf(line, "\n#line %d \"%s\"\n", yylineno, input_filename);
102 return line;
105 return EMPTY;
108 void
109 output_statement(char *stmt, int whenever_mode, enum ECPG_statement_type st)
112 fprintf(yyout, "{ ECPGdo(__LINE__, %d, %d, %s, %d, ", compat, force_indicator, connection ? connection : "NULL", questionmarks);
113 if (st == ECPGst_normal)
115 if (auto_prepare)
116 fputs("ECPGst_prepnormal, \"", yyout);
117 else
118 fputs("ECPGst_normal, \"", yyout);
120 output_escaped_str(stmt, false);
121 fputs("\", ", yyout);
123 else
124 fprintf(yyout, "%d, %s, ", st, stmt);
126 /* dump variables to C file */
127 dump_variables(argsinsert, 1);
128 fputs("ECPGt_EOIT, ", yyout);
129 dump_variables(argsresult, 1);
130 fputs("ECPGt_EORT);", yyout);
131 reset_variables();
133 whenever_action(whenever_mode | 2);
134 free(stmt);
135 if (connection != NULL)
136 free(connection);
139 void
140 output_prepare_statement(char *name, char *stmt)
142 fprintf(yyout, "{ ECPGprepare(__LINE__, %s, %d, ", connection ? connection : "NULL", questionmarks);
143 output_escaped_str(name, true);
144 fputs(", ", yyout);
145 output_escaped_str(stmt, true);
146 fputs(");", yyout);
147 whenever_action(2);
148 free(name);
149 if (connection != NULL)
150 free(connection);
153 void
154 output_deallocate_prepare_statement(char *name)
156 const char *con = connection ? connection : "NULL";
158 if (strcmp(name, "all"))
160 fprintf(yyout, "{ ECPGdeallocate(__LINE__, %d, %s, ", compat, con);
161 output_escaped_str(name, true);
162 fputs(");", yyout);
164 else
165 fprintf(yyout, "{ ECPGdeallocate_all(__LINE__, %d, %s);", compat, con);
167 whenever_action(2);
168 free(name);
169 if (connection != NULL)
170 free(connection);
173 static void
174 output_escaped_str(char *str, bool quoted)
176 int i = 0;
177 int len = strlen(str);
179 if (quoted && str[0] == '\"' && str[len - 1] == '\"') /* do not escape quotes
180 * at beginning and end
181 * if quoted string */
183 i = 1;
184 len--;
185 fputs("\"", yyout);
188 /* output this char by char as we have to filter " and \n */
189 for (; i < len; i++)
191 if (str[i] == '"')
192 fputs("\\\"", yyout);
193 else if (str[i] == '\n')
194 fputs("\\\n", yyout);
195 else if (str[i] == '\\')
197 int j = i;
200 * check whether this is a continuation line if it is, do not
201 * output anything because newlines are escaped anyway
204 /* accept blanks after the '\' as some other compilers do too */
207 j++;
208 } while (str[j] == ' ' || str[j] == '\t');
210 if ((str[j] != '\n') && (str[j] != '\r' || str[j + 1] != '\n')) /* not followed by a
211 * newline */
212 fputs("\\\\", yyout);
214 else if (str[i] == '\r' && str[i + 1] == '\n')
216 fputs("\\\r\n", yyout);
217 i++;
219 else
220 fputc(str[i], yyout);
223 if (quoted && str[0] == '\"' && str[len] == '\"')
224 fputs("\"", yyout);