LCOV - code coverage report
Current view: top level - src/interfaces/ecpg/preproc - output.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 122 129 94.6 %
Date: 2024-04-19 06:11:31 Functions: 9 9 100.0 %
Legend: Lines: hit not hit

          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        5174 : output_line_number(void)
      11             : {
      12        5174 :     char       *line = hashline_number();
      13             : 
      14        5174 :     fprintf(base_yyout, "%s", line);
      15        5174 :     free(line);
      16        5174 : }
      17             : 
      18             : void
      19         288 : output_simple_statement(char *stmt, int whenever_mode)
      20             : {
      21         288 :     output_escaped_str(stmt, false);
      22         288 :     if (whenever_mode)
      23          22 :         whenever_action(whenever_mode);
      24         288 :     output_line_number();
      25         288 :     free(stmt);
      26         288 : }
      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        2094 : print_action(struct when *w)
      38             : {
      39        2094 :     switch (w->code)
      40             :     {
      41        1124 :         case W_SQLPRINT:
      42        1124 :             fprintf(base_yyout, "sqlprint();");
      43        1124 :             break;
      44           4 :         case W_GOTO:
      45           4 :             fprintf(base_yyout, "goto %s;", w->command);
      46           4 :             break;
      47         346 :         case W_DO:
      48         346 :             fprintf(base_yyout, "%s;", w->command);
      49         346 :             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        2094 : }
      64             : 
      65             : void
      66        2044 : whenever_action(int mode)
      67             : {
      68        2044 :     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        2044 :     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        2044 :     if (when_error.code != W_NOTHING)
      81             :     {
      82        1740 :         output_line_number();
      83        1740 :         fprintf(base_yyout, "\nif (sqlca.sqlcode < 0) ");
      84        1740 :         print_action(&when_error);
      85             :     }
      86             : 
      87        2044 :     if ((mode & 2) == 2)
      88        1954 :         fputc('}', base_yyout);
      89             : 
      90        2044 :     output_line_number();
      91        2044 : }
      92             : 
      93             : char *
      94        5766 : hashline_number(void)
      95             : {
      96             :     /* do not print line numbers if we are in debug mode */
      97        5766 :     if (input_filename
      98             : #ifdef YYDEBUG
      99             :         && !base_yydebug
     100             : #endif
     101             :         )
     102             :     {
     103             :         /* "* 2" here is for escaping '\' and '"' below */
     104        5766 :         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        5766 :         sprintf(line, "\n#line %d \"", base_yylineno);
     109        5766 :         src = input_filename;
     110        5766 :         dest = line + strlen(line);
     111       74342 :         while (*src)
     112             :         {
     113       68576 :             if (*src == '\\' || *src == '"')
     114           0 :                 *dest++ = '\\';
     115       68576 :             *dest++ = *src++;
     116             :         }
     117        5766 :         *dest = '\0';
     118        5766 :         strcat(dest, "\"\n");
     119             : 
     120        5766 :         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        1106 : output_statement(char *stmt, int whenever_mode, enum ECPG_statement_type st)
     137             : {
     138        1106 :     fprintf(base_yyout, "{ ECPGdo(__LINE__, %d, %d, %s, %d, ", compat, force_indicator, connection ? connection : "NULL", questionmarks);
     139             : 
     140        1106 :     if (st == ECPGst_prepnormal && !auto_prepare)
     141         432 :         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        1106 :     fprintf(base_yyout, "%s, ", ecpg_statement_type_name[st]);
     149        1106 :     if (st == ECPGst_execute || st == ECPGst_exec_immediate)
     150          62 :         fprintf(base_yyout, "%s, ", stmt);
     151             :     else
     152             :     {
     153        1044 :         fputs("\"", base_yyout);
     154        1044 :         output_escaped_str(stmt, false);
     155        1044 :         fputs("\", ", base_yyout);
     156             :     }
     157             : 
     158             :     /* dump variables to C file */
     159        1106 :     dump_variables(argsinsert, 1);
     160        1106 :     fputs("ECPGt_EOIT, ", base_yyout);
     161        1106 :     dump_variables(argsresult, 1);
     162        1106 :     fputs("ECPGt_EORT);", base_yyout);
     163        1106 :     reset_variables();
     164             : 
     165        1106 :     whenever_action(whenever_mode | 2);
     166        1106 :     free(stmt);
     167        1106 : }
     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        1598 : output_escaped_str(char *str, bool quoted)
     201             : {
     202        1598 :     int         i = 0;
     203        1598 :     int         len = strlen(str);
     204             : 
     205        1598 :     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       60050 :     for (; i < len; i++)
     216             :     {
     217       58452 :         if (str[i] == '"')
     218         208 :             fputs("\\\"", base_yyout);
     219       58244 :         else if (str[i] == '\n')
     220          36 :             fputs("\\\n", base_yyout);
     221       58208 :         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       58164 :         else if (str[i] == '\r' && str[i + 1] == '\n')
     241             :         {
     242           0 :             fputs("\\\r\n", base_yyout);
     243           0 :             i++;
     244             :         }
     245             :         else
     246       58164 :             fputc(str[i], base_yyout);
     247             :     }
     248             : 
     249        1598 :     if (quoted && str[0] == '"' && str[len] == '"')
     250         170 :         fputs("\"", base_yyout);
     251        1598 : }

Generated by: LCOV version 1.14