LCOV - code coverage report
Current view: top level - src/interfaces/ecpg/preproc - type.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 82.8 % 373 309
Test Date: 2026-05-30 00:16:21 Functions: 100.0 % 12 12
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* src/interfaces/ecpg/preproc/type.c */
       2              : 
       3              : #include "postgres_fe.h"
       4              : 
       5              : #include "preproc_extern.h"
       6              : 
       7              : #define indicator_set ind_type != NULL && ind_type->type != ECPGt_NO_INDICATOR
       8              : 
       9              : static struct ECPGstruct_member struct_no_indicator = {"no_indicator", &ecpg_no_indicator, NULL};
      10              : 
      11              : /* duplicate memberlist */
      12              : struct ECPGstruct_member *
      13          105 : ECPGstruct_member_dup(struct ECPGstruct_member *rm)
      14              : {
      15          105 :     struct ECPGstruct_member *new = NULL;
      16              : 
      17          329 :     while (rm)
      18              :     {
      19              :         struct ECPGtype *type;
      20              : 
      21          224 :         switch (rm->type->type)
      22              :         {
      23           10 :             case ECPGt_struct:
      24              :             case ECPGt_union:
      25           10 :                 type = ECPGmake_struct_type(rm->type->u.members, rm->type->type, rm->type->type_name, rm->type->struct_sizeof);
      26           10 :                 break;
      27            2 :             case ECPGt_array:
      28              : 
      29              :                 /*
      30              :                  * if this array does contain a struct again, we have to
      31              :                  * create the struct too
      32              :                  */
      33            2 :                 if (rm->type->u.element->type == ECPGt_struct || rm->type->u.element->type == ECPGt_union)
      34            2 :                     type = ECPGmake_struct_type(rm->type->u.element->u.members, rm->type->u.element->type, rm->type->u.element->type_name, rm->type->u.element->struct_sizeof);
      35              :                 else
      36            0 :                     type = ECPGmake_array_type(ECPGmake_simple_type(rm->type->u.element->type, rm->type->u.element->size, rm->type->u.element->counter), rm->type->size);
      37            2 :                 break;
      38          212 :             default:
      39          212 :                 type = ECPGmake_simple_type(rm->type->type, rm->type->size, rm->type->counter);
      40          212 :                 break;
      41              :         }
      42              : 
      43          224 :         ECPGmake_struct_member(rm->name, type, &new);
      44              : 
      45          224 :         rm = rm->next;
      46              :     }
      47              : 
      48          105 :     return new;
      49              : }
      50              : 
      51              : /* The NAME argument is copied. The type argument is preserved as a pointer. */
      52              : void
      53          280 : ECPGmake_struct_member(const char *name, struct ECPGtype *type, struct ECPGstruct_member **start)
      54              : {
      55              :     struct ECPGstruct_member *ptr,
      56              :                *ne =
      57          280 :         (struct ECPGstruct_member *) mm_alloc(sizeof(struct ECPGstruct_member));
      58              : 
      59          280 :     ne->name = mm_strdup(name);
      60          280 :     ne->type = type;
      61          280 :     ne->next = NULL;
      62              : 
      63          382 :     for (ptr = *start; ptr && ptr->next; ptr = ptr->next);
      64              : 
      65          280 :     if (ptr)
      66          162 :         ptr->next = ne;
      67              :     else
      68          118 :         *start = ne;
      69          280 : }
      70              : 
      71              : struct ECPGtype *
      72          888 : ECPGmake_simple_type(enum ECPGttype type, const char *size, int counter)
      73              : {
      74          888 :     struct ECPGtype *ne = (struct ECPGtype *) mm_alloc(sizeof(struct ECPGtype));
      75              : 
      76          888 :     ne->type = type;
      77          888 :     ne->type_name = NULL;
      78          888 :     ne->size = mm_strdup(size);
      79          888 :     ne->u.element = NULL;
      80          888 :     ne->struct_sizeof = NULL;
      81          888 :     ne->counter = counter;       /* only needed for varchar and bytea */
      82              : 
      83          888 :     return ne;
      84              : }
      85              : 
      86              : struct ECPGtype *
      87           74 : ECPGmake_array_type(struct ECPGtype *type, const char *size)
      88              : {
      89           74 :     struct ECPGtype *ne = ECPGmake_simple_type(ECPGt_array, size, 0);
      90              : 
      91           74 :     ne->u.element = type;
      92              : 
      93           74 :     return ne;
      94              : }
      95              : 
      96              : struct ECPGtype *
      97           45 : ECPGmake_struct_type(struct ECPGstruct_member *rm, enum ECPGttype type,
      98              :                      const char *type_name, const char *struct_sizeof)
      99              : {
     100           45 :     struct ECPGtype *ne = ECPGmake_simple_type(type, "1", 0);
     101              : 
     102           45 :     ne->type_name = mm_strdup(type_name);
     103           45 :     ne->u.members = ECPGstruct_member_dup(rm);
     104           45 :     ne->struct_sizeof = mm_strdup(struct_sizeof);
     105              : 
     106           45 :     return ne;
     107              : }
     108              : 
     109              : static const char *
     110          628 : get_type(enum ECPGttype type)
     111              : {
     112          628 :     switch (type)
     113              :     {
     114          202 :         case ECPGt_char:
     115          202 :             return "ECPGt_char";
     116              :             break;
     117            0 :         case ECPGt_unsigned_char:
     118            0 :             return "ECPGt_unsigned_char";
     119              :             break;
     120           32 :         case ECPGt_short:
     121           32 :             return "ECPGt_short";
     122              :             break;
     123            0 :         case ECPGt_unsigned_short:
     124            0 :             return "ECPGt_unsigned_short";
     125              :             break;
     126          222 :         case ECPGt_int:
     127          222 :             return "ECPGt_int";
     128              :             break;
     129            0 :         case ECPGt_unsigned_int:
     130            0 :             return "ECPGt_unsigned_int";
     131              :             break;
     132           12 :         case ECPGt_long:
     133           12 :             return "ECPGt_long";
     134              :             break;
     135            0 :         case ECPGt_unsigned_long:
     136            0 :             return "ECPGt_unsigned_long";
     137              :             break;
     138            0 :         case ECPGt_long_long:
     139            0 :             return "ECPGt_long_long";
     140              :             break;
     141            0 :         case ECPGt_unsigned_long_long:
     142            0 :             return "ECPGt_unsigned_long_long";
     143              :             break;
     144            6 :         case ECPGt_float:
     145            6 :             return "ECPGt_float";
     146              :             break;
     147           16 :         case ECPGt_double:
     148           16 :             return "ECPGt_double";
     149              :             break;
     150           13 :         case ECPGt_bool:
     151           13 :             return "ECPGt_bool";
     152              :             break;
     153           24 :         case ECPGt_varchar:
     154           24 :             return "ECPGt_varchar";
     155           21 :         case ECPGt_bytea:
     156           21 :             return "ECPGt_bytea";
     157            0 :         case ECPGt_NO_INDICATOR:    /* no indicator */
     158            0 :             return "ECPGt_NO_INDICATOR";
     159              :             break;
     160           21 :         case ECPGt_char_variable:   /* string that should not be quoted */
     161           21 :             return "ECPGt_char_variable";
     162              :             break;
     163           16 :         case ECPGt_const:       /* constant string quoted */
     164           16 :             return "ECPGt_const";
     165              :             break;
     166            6 :         case ECPGt_decimal:
     167            6 :             return "ECPGt_decimal";
     168              :             break;
     169            9 :         case ECPGt_numeric:
     170            9 :             return "ECPGt_numeric";
     171              :             break;
     172            4 :         case ECPGt_interval:
     173            4 :             return "ECPGt_interval";
     174              :             break;
     175            0 :         case ECPGt_descriptor:
     176            0 :             return "ECPGt_descriptor";
     177              :             break;
     178            0 :         case ECPGt_sqlda:
     179            0 :             return "ECPGt_sqlda";
     180              :             break;
     181           10 :         case ECPGt_date:
     182           10 :             return "ECPGt_date";
     183              :             break;
     184           13 :         case ECPGt_timestamp:
     185           13 :             return "ECPGt_timestamp";
     186              :             break;
     187            1 :         case ECPGt_string:
     188            1 :             return "ECPGt_string";
     189              :             break;
     190            0 :         default:
     191            0 :             mmerror(PARSE_ERROR, ET_ERROR, "unrecognized variable type code %d", type);
     192              :     }
     193              : 
     194            0 :     return NULL;
     195              : }
     196              : 
     197              : /*
     198              :  * Dump a type.
     199              :  * The type is dumped as:
     200              :  * type-tag <comma>                  - enum ECPGttype
     201              :  * reference-to-variable <comma>         - char *
     202              :  * size <comma>                      - long size of this field (if varchar)
     203              :  * arrsize <comma>                   - long number of elements in the arr
     204              :  * offset <comma>                - offset to the next element
     205              :  * Where:
     206              :  * type-tag is one of the simple types or varchar.
     207              :  * reference-to-variable can be a reference to a struct element.
     208              :  * arrsize is the size of the array in case of array fetches. Otherwise 0.
     209              :  * size is the maxsize in case it is a varchar. Otherwise it is the size of
     210              :  * the variable (required to do array fetches of structs).
     211              :  */
     212              : static void ECPGdump_a_simple(FILE *o, const char *name, enum ECPGttype type,
     213              :                               char *varcharsize,
     214              :                               char *arrsize, const char *size, const char *prefix, int counter);
     215              : static void ECPGdump_a_struct(FILE *o, const char *name, const char *ind_name, char *arrsize,
     216              :                               struct ECPGtype *type, struct ECPGtype *ind_type, const char *prefix, const char *ind_prefix);
     217              : 
     218              : void
     219          641 : ECPGdump_a_type(FILE *o, const char *name, struct ECPGtype *type, const int brace_level,
     220              :                 const char *ind_name, struct ECPGtype *ind_type, const int ind_brace_level,
     221              :                 const char *prefix, const char *ind_prefix,
     222              :                 char *arr_str_size, const char *struct_sizeof,
     223              :                 const char *ind_struct_sizeof)
     224              : {
     225              :     struct variable *var;
     226              : 
     227          641 :     if (type->type != ECPGt_descriptor && type->type != ECPGt_sqlda &&
     228          592 :         type->type != ECPGt_char_variable && type->type != ECPGt_const &&
     229              :         brace_level >= 0)
     230              :     {
     231              :         char       *str;
     232              : 
     233          519 :         str = mm_strdup(name);
     234          519 :         var = find_variable(str);
     235          519 :         free(str);
     236              : 
     237          519 :         if ((var->type->type != type->type) ||
     238          519 :             (var->type->type_name && !type->type_name) ||
     239          519 :             (!var->type->type_name && type->type_name) ||
     240          519 :             (var->type->type_name && type->type_name && strcmp(var->type->type_name, type->type_name) != 0))
     241            0 :             mmerror(PARSE_ERROR, ET_ERROR, "variable \"%s\" is hidden by a local variable of a different type", name);
     242          519 :         else if (var->brace_level != brace_level)
     243            0 :             mmerror(PARSE_ERROR, ET_WARNING, "variable \"%s\" is hidden by a local variable", name);
     244              : 
     245          519 :         if (ind_name && ind_type && ind_type->type != ECPGt_NO_INDICATOR && ind_brace_level >= 0)
     246              :         {
     247           32 :             str = mm_strdup(ind_name);
     248           32 :             var = find_variable(str);
     249           32 :             free(str);
     250              : 
     251           32 :             if ((var->type->type != ind_type->type) ||
     252           32 :                 (var->type->type_name && !ind_type->type_name) ||
     253           32 :                 (!var->type->type_name && ind_type->type_name) ||
     254           32 :                 (var->type->type_name && ind_type->type_name && strcmp(var->type->type_name, ind_type->type_name) != 0))
     255            0 :                 mmerror(PARSE_ERROR, ET_ERROR, "indicator variable \"%s\" is hidden by a local variable of a different type", ind_name);
     256           32 :             else if (var->brace_level != ind_brace_level)
     257            0 :                 mmerror(PARSE_ERROR, ET_WARNING, "indicator variable \"%s\" is hidden by a local variable", ind_name);
     258              :         }
     259              :     }
     260              : 
     261          641 :     switch (type->type)
     262              :     {
     263           83 :         case ECPGt_array:
     264           83 :             if (indicator_set && ind_type->type != ECPGt_array)
     265            0 :                 mmfatal(INDICATOR_NOT_ARRAY, "indicator for array/pointer has to be array/pointer");
     266           83 :             switch (type->u.element->type)
     267              :             {
     268            0 :                 case ECPGt_array:
     269            0 :                     mmerror(PARSE_ERROR, ET_ERROR, "nested arrays are not supported (except strings)"); /* array of array */
     270            0 :                     break;
     271            8 :                 case ECPGt_struct:
     272              :                 case ECPGt_union:
     273           16 :                     ECPGdump_a_struct(o, name,
     274              :                                       ind_name,
     275              :                                       type->size,
     276              :                                       type->u.element,
     277            8 :                                       (ind_type == NULL) ? NULL : ((ind_type->type == ECPGt_NO_INDICATOR) ? ind_type : ind_type->u.element),
     278              :                                       prefix, ind_prefix);
     279            8 :                     break;
     280           75 :                 default:
     281           75 :                     if (!IS_SIMPLE_TYPE(type->u.element->type))
     282            0 :                         base_yyerror("internal error: unknown datatype, please report this to <" PACKAGE_BUGREPORT ">");
     283              : 
     284           75 :                     ECPGdump_a_simple(o, name,
     285           75 :                                       type->u.element->type,
     286           75 :                                       type->u.element->size, type->size, struct_sizeof ? struct_sizeof : NULL,
     287           75 :                                       prefix, type->u.element->counter);
     288              : 
     289           75 :                     if (ind_type != NULL)
     290              :                     {
     291           55 :                         if (ind_type->type == ECPGt_NO_INDICATOR)
     292              :                         {
     293           53 :                             char       *str_neg_one = mm_strdup("-1");
     294              : 
     295           53 :                             ECPGdump_a_simple(o, ind_name, ind_type->type, ind_type->size, str_neg_one, NULL, ind_prefix, 0);
     296           53 :                             free(str_neg_one);
     297              :                         }
     298              :                         else
     299              :                         {
     300            2 :                             ECPGdump_a_simple(o, ind_name, ind_type->u.element->type,
     301            2 :                                               ind_type->u.element->size, ind_type->size, NULL, ind_prefix, 0);
     302              :                         }
     303              :                     }
     304              :             }
     305           83 :             break;
     306            6 :         case ECPGt_struct:
     307              :             {
     308            6 :                 char       *str_one = mm_strdup("1");
     309              : 
     310            6 :                 if (indicator_set && ind_type->type != ECPGt_struct)
     311            0 :                     mmfatal(INDICATOR_NOT_STRUCT, "indicator for struct has to be a struct");
     312              : 
     313            6 :                 ECPGdump_a_struct(o, name, ind_name, str_one, type, ind_type, prefix, ind_prefix);
     314            6 :                 free(str_one);
     315              :             }
     316            6 :             break;
     317            0 :         case ECPGt_union:       /* cannot dump a complete union */
     318            0 :             base_yyerror("type of union has to be specified");
     319            0 :             break;
     320           21 :         case ECPGt_char_variable:
     321              :             {
     322              :                 /*
     323              :                  * Allocate for each, as there are code-paths where the values
     324              :                  * get stomped on.
     325              :                  */
     326           21 :                 char       *str_varchar_one = mm_strdup("1");
     327           21 :                 char       *str_arr_one = mm_strdup("1");
     328           21 :                 char       *str_neg_one = mm_strdup("-1");
     329              : 
     330           21 :                 if (indicator_set && (ind_type->type == ECPGt_struct || ind_type->type == ECPGt_array))
     331            0 :                     mmfatal(INDICATOR_NOT_SIMPLE, "indicator for simple data type has to be simple");
     332              : 
     333           21 :                 ECPGdump_a_simple(o, name, type->type, str_varchar_one, (arr_str_size && strcmp(arr_str_size, "0") != 0) ? arr_str_size : str_arr_one, struct_sizeof, prefix, 0);
     334           21 :                 if (ind_type != NULL)
     335           21 :                     ECPGdump_a_simple(o, ind_name, ind_type->type, ind_type->size, (arr_str_size && strcmp(arr_str_size, "0") != 0) ? arr_str_size : str_neg_one, ind_struct_sizeof, ind_prefix, 0);
     336              : 
     337           21 :                 free(str_varchar_one);
     338           21 :                 free(str_arr_one);
     339           21 :                 free(str_neg_one);
     340              :             }
     341           21 :             break;
     342           24 :         case ECPGt_descriptor:
     343              :             {
     344              :                 /*
     345              :                  * Allocate for each, as there are code-paths where the values
     346              :                  * get stomped on.
     347              :                  */
     348           24 :                 char       *str_neg_one = mm_strdup("-1");
     349           24 :                 char       *ind_type_neg_one = mm_strdup("-1");
     350              : 
     351           24 :                 if (indicator_set && (ind_type->type == ECPGt_struct || ind_type->type == ECPGt_array))
     352            0 :                     mmfatal(INDICATOR_NOT_SIMPLE, "indicator for simple data type has to be simple");
     353              : 
     354           24 :                 ECPGdump_a_simple(o, name, type->type, NULL, str_neg_one, NULL, prefix, 0);
     355           24 :                 if (ind_type != NULL)
     356           24 :                     ECPGdump_a_simple(o, ind_name, ind_type->type, ind_type->size, ind_type_neg_one, NULL, ind_prefix, 0);
     357              : 
     358           24 :                 free(str_neg_one);
     359           24 :                 free(ind_type_neg_one);
     360              :             }
     361           24 :             break;
     362          507 :         default:
     363              :             {
     364              :                 /*
     365              :                  * Allocate for each, as there are code-paths where the values
     366              :                  * get stomped on.
     367              :                  */
     368          507 :                 char       *str_neg_one = mm_strdup("-1");
     369          507 :                 char       *ind_type_neg_one = mm_strdup("-1");
     370              : 
     371          507 :                 if (indicator_set && (ind_type->type == ECPGt_struct || ind_type->type == ECPGt_array))
     372            0 :                     mmfatal(INDICATOR_NOT_SIMPLE, "indicator for simple data type has to be simple");
     373              : 
     374          507 :                 ECPGdump_a_simple(o, name, type->type, type->size, (arr_str_size && strcmp(arr_str_size, "0") != 0) ? arr_str_size : str_neg_one, struct_sizeof, prefix, type->counter);
     375          507 :                 if (ind_type != NULL)
     376          458 :                     ECPGdump_a_simple(o, ind_name, ind_type->type, ind_type->size, (arr_str_size && strcmp(arr_str_size, "0") != 0) ? arr_str_size : ind_type_neg_one, ind_struct_sizeof, ind_prefix, 0);
     377              : 
     378          507 :                 free(str_neg_one);
     379          507 :                 free(ind_type_neg_one);
     380              :             }
     381          507 :             break;
     382              :     }
     383          641 : }
     384              : 
     385              : 
     386              : /*
     387              :  * If size is NULL, then the offset is 0, if not use size as a
     388              :  * string, it represents the offset needed if we are in an array of structs.
     389              :  */
     390              : static void
     391         1185 : ECPGdump_a_simple(FILE *o, const char *name, enum ECPGttype type,
     392              :                   char *varcharsize,
     393              :                   char *arrsize,
     394              :                   const char *size,
     395              :                   const char *prefix,
     396              :                   int counter)
     397              : {
     398         1185 :     if (type == ECPGt_NO_INDICATOR)
     399          508 :         fprintf(o, "\n\tECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ");
     400          677 :     else if (type == ECPGt_descriptor)
     401              :         /* remember that name here already contains quotes (if needed) */
     402           24 :         fprintf(o, "\n\tECPGt_descriptor, %s, 1L, 1L, 1L, ", name);
     403          653 :     else if (type == ECPGt_sqlda)
     404           25 :         fprintf(o, "\n\tECPGt_sqlda, &%s, 0L, 0L, 0L, ", name);
     405              :     else
     406              :     {
     407          628 :         char       *variable = (char *) mm_alloc(strlen(name) + ((prefix == NULL) ? 0 : strlen(prefix)) + 4);
     408          628 :         char       *offset = (char *) mm_alloc(strlen(name) + strlen("sizeof(struct varchar_)") + 1 + strlen(varcharsize) + sizeof(int) * CHAR_BIT * 10 / 3);
     409              :         char       *struct_name;
     410              : 
     411          628 :         switch (type)
     412              :         {
     413              :                 /*
     414              :                  * we have to use the & operator except for arrays and
     415              :                  * pointers
     416              :                  */
     417              : 
     418           45 :             case ECPGt_varchar:
     419              :             case ECPGt_bytea:
     420              : 
     421              :                 /*
     422              :                  * we have to use the pointer except for arrays with given
     423              :                  * bounds
     424              :                  */
     425           45 :                 if (((atoi(arrsize) > 0) ||
     426           45 :                      (atoi(arrsize) == 0 && strcmp(arrsize, "0") != 0)) &&
     427              :                     size == NULL)
     428            2 :                     sprintf(variable, "(%s%s)", prefix ? prefix : "", name);
     429              :                 else
     430           43 :                     sprintf(variable, "&(%s%s)", prefix ? prefix : "", name);
     431              : 
     432              :                 /*
     433              :                  * If we created a varchar structure automatically, counter is
     434              :                  * greater than 0.
     435              :                  */
     436           45 :                 if (type == ECPGt_varchar)
     437           24 :                     struct_name = "struct varchar";
     438              :                 else
     439           21 :                     struct_name = "struct bytea";
     440              : 
     441           45 :                 if (counter)
     442           44 :                     sprintf(offset, "sizeof(%s_%d)", struct_name, counter);
     443              :                 else
     444            1 :                     sprintf(offset, "sizeof(%s)", struct_name);
     445           45 :                 break;
     446          224 :             case ECPGt_char:
     447              :             case ECPGt_unsigned_char:
     448              :             case ECPGt_char_variable:
     449              :             case ECPGt_string:
     450              :                 {
     451          224 :                     char       *sizeof_name = "char";
     452              : 
     453              :                     /*
     454              :                      * we have to use the pointer except for arrays with given
     455              :                      * bounds, ecpglib will distinguish between * and []
     456              :                      */
     457          224 :                     if ((atoi(varcharsize) > 1 ||
     458           86 :                          (atoi(arrsize) > 0) ||
     459           59 :                          (atoi(varcharsize) == 0 && strcmp(varcharsize, "0") != 0) ||
     460           51 :                          (atoi(arrsize) == 0 && strcmp(arrsize, "0") != 0))
     461          173 :                         && size == NULL)
     462              :                     {
     463          166 :                         sprintf(variable, "(%s%s)", prefix ? prefix : "", name);
     464          166 :                         if ((type == ECPGt_char || type == ECPGt_unsigned_char) &&
     465          144 :                             strcmp(varcharsize, "0") == 0)
     466              :                         {
     467              :                             /*
     468              :                              * If this is an array of char *, the offset would
     469              :                              * be sizeof(char *) and not sizeof(char).
     470              :                              */
     471            0 :                             sizeof_name = "char *";
     472              :                         }
     473              :                     }
     474              :                     else
     475           58 :                         sprintf(variable, "&(%s%s)", prefix ? prefix : "", name);
     476              : 
     477          224 :                     sprintf(offset, "(%s)*sizeof(%s)", strcmp(varcharsize, "0") == 0 ? "1" : varcharsize, sizeof_name);
     478          224 :                     break;
     479              :                 }
     480            9 :             case ECPGt_numeric:
     481              : 
     482              :                 /*
     483              :                  * we have to use a pointer here
     484              :                  */
     485            9 :                 sprintf(variable, "&(%s%s)", prefix ? prefix : "", name);
     486            9 :                 sprintf(offset, "sizeof(numeric)");
     487            9 :                 break;
     488            4 :             case ECPGt_interval:
     489              : 
     490              :                 /*
     491              :                  * we have to use a pointer here
     492              :                  */
     493            4 :                 sprintf(variable, "&(%s%s)", prefix ? prefix : "", name);
     494            4 :                 sprintf(offset, "sizeof(interval)");
     495            4 :                 break;
     496           10 :             case ECPGt_date:
     497              : 
     498              :                 /*
     499              :                  * we have to use a pointer and translate the variable type
     500              :                  */
     501           10 :                 sprintf(variable, "&(%s%s)", prefix ? prefix : "", name);
     502           10 :                 sprintf(offset, "sizeof(date)");
     503           10 :                 break;
     504           13 :             case ECPGt_timestamp:
     505              : 
     506              :                 /*
     507              :                  * we have to use a pointer and translate the variable type
     508              :                  */
     509           13 :                 sprintf(variable, "&(%s%s)", prefix ? prefix : "", name);
     510           13 :                 sprintf(offset, "sizeof(timestamp)");
     511           13 :                 break;
     512           16 :             case ECPGt_const:
     513              : 
     514              :                 /*
     515              :                  * just dump the const as string
     516              :                  */
     517           16 :                 sprintf(variable, "\"%s\"", name);
     518           16 :                 sprintf(offset, "strlen(\"%s\")", name);
     519           16 :                 break;
     520          307 :             default:
     521              : 
     522              :                 /*
     523              :                  * we have to use the pointer except for arrays with given
     524              :                  * bounds
     525              :                  */
     526          307 :                 if (((atoi(arrsize) > 0) ||
     527          307 :                      (atoi(arrsize) == 0 && strcmp(arrsize, "0") != 0)) &&
     528              :                     size == NULL)
     529           18 :                     sprintf(variable, "(%s%s)", prefix ? prefix : "", name);
     530              :                 else
     531          289 :                     sprintf(variable, "&(%s%s)", prefix ? prefix : "", name);
     532              : 
     533          307 :                 sprintf(offset, "sizeof(%s)", ecpg_type_name(type));
     534          307 :                 break;
     535              :         }
     536              : 
     537              :         /*
     538              :          * Array size would be -1 for addresses of members within structure,
     539              :          * when pointer to structure is being dumped.
     540              :          */
     541          628 :         if (atoi(arrsize) < 0 && !size)
     542          466 :             strcpy(arrsize, "1");
     543              : 
     544              :         /*
     545              :          * If size i.e. the size of structure of which this variable is part
     546              :          * of, that gives the offset to the next element, if required
     547              :          */
     548          628 :         if (size == NULL || strlen(size) == 0)
     549          567 :             fprintf(o, "\n\t%s,%s,(long)%s,(long)%s,%s, ", get_type(type), variable, varcharsize, arrsize, offset);
     550              :         else
     551           61 :             fprintf(o, "\n\t%s,%s,(long)%s,(long)%s,%s, ", get_type(type), variable, varcharsize, arrsize, size);
     552              : 
     553          628 :         free(variable);
     554          628 :         free(offset);
     555              :     }
     556         1185 : }
     557              : 
     558              : 
     559              : /* Penetrate a struct and dump the contents. */
     560              : static void
     561           14 : ECPGdump_a_struct(FILE *o, const char *name, const char *ind_name, char *arrsize, struct ECPGtype *type, struct ECPGtype *ind_type, const char *prefix, const char *ind_prefix)
     562              : {
     563              :     /*
     564              :      * If offset is NULL, then this is the first recursive level. If not then
     565              :      * we are in a struct and the offset is used as offset.
     566              :      */
     567              :     struct ECPGstruct_member *p,
     568           14 :                *ind_p = NULL;
     569           14 :     char       *pbuf = (char *) mm_alloc(strlen(name) + ((prefix == NULL) ? 0 : strlen(prefix)) + 3);
     570           14 :     char       *ind_pbuf = (char *) mm_alloc(strlen(ind_name) + ((ind_prefix == NULL) ? 0 : strlen(ind_prefix)) + 3);
     571              : 
     572           14 :     if (atoi(arrsize) == 1)
     573            6 :         sprintf(pbuf, "%s%s.", prefix ? prefix : "", name);
     574              :     else
     575            8 :         sprintf(pbuf, "%s%s->", prefix ? prefix : "", name);
     576              : 
     577           14 :     prefix = pbuf;
     578              : 
     579           14 :     if (ind_type == &ecpg_no_indicator)
     580            2 :         ind_p = &struct_no_indicator;
     581           12 :     else if (ind_type != NULL)
     582              :     {
     583           12 :         if (atoi(arrsize) == 1)
     584            4 :             sprintf(ind_pbuf, "%s%s.", ind_prefix ? ind_prefix : "", ind_name);
     585              :         else
     586            8 :             sprintf(ind_pbuf, "%s%s->", ind_prefix ? ind_prefix : "", ind_name);
     587              : 
     588           12 :         ind_prefix = ind_pbuf;
     589           12 :         ind_p = ind_type->u.members;
     590              :     }
     591              : 
     592           50 :     for (p = type->u.members; p; p = p->next)
     593              :     {
     594           72 :         ECPGdump_a_type(o, p->name, p->type, -1,
     595              :                         (ind_p != NULL) ? ind_p->name : NULL,
     596              :                         (ind_p != NULL) ? ind_p->type : NULL,
     597              :                         -1,
     598           36 :                         prefix, ind_prefix, arrsize, type->struct_sizeof,
     599              :                         (ind_p != NULL) ? ind_type->struct_sizeof : NULL);
     600           36 :         if (ind_p != NULL && ind_p != &struct_no_indicator)
     601              :         {
     602           30 :             ind_p = ind_p->next;
     603           30 :             if (ind_p == NULL && p->next != NULL)
     604              :             {
     605            0 :                 mmerror(PARSE_ERROR, ET_WARNING, "indicator struct \"%s\" has too few members", ind_name);
     606            0 :                 ind_p = &struct_no_indicator;
     607              :             }
     608              :         }
     609              :     }
     610              : 
     611           14 :     if (ind_type != NULL && ind_p != NULL && ind_p != &struct_no_indicator)
     612              :     {
     613            0 :         mmerror(PARSE_ERROR, ET_WARNING, "indicator struct \"%s\" has too many members", ind_name);
     614              :     }
     615              : 
     616           14 :     free(pbuf);
     617           14 :     free(ind_pbuf);
     618           14 : }
     619              : 
     620              : void
     621          133 : ECPGfree_struct_member(struct ECPGstruct_member *rm)
     622              : {
     623          330 :     while (rm)
     624              :     {
     625          197 :         struct ECPGstruct_member *p = rm;
     626              : 
     627          197 :         rm = rm->next;
     628          197 :         free(p->name);
     629          197 :         ECPGfree_type(p->type);
     630          197 :         free(p);
     631              :     }
     632          133 : }
     633              : 
     634              : void
     635          698 : ECPGfree_type(struct ECPGtype *type)
     636              : {
     637          698 :     if (!IS_SIMPLE_TYPE(type->type))
     638              :     {
     639          109 :         switch (type->type)
     640              :         {
     641           70 :             case ECPGt_array:
     642           70 :                 switch (type->u.element->type)
     643              :                 {
     644            0 :                     case ECPGt_array:
     645            0 :                         base_yyerror("internal error: found multidimensional array\n");
     646            0 :                         break;
     647           15 :                     case ECPGt_struct:
     648              :                     case ECPGt_union:
     649              :                         /* Array of structs. */
     650           15 :                         ECPGfree_type(type->u.element);
     651           15 :                         break;
     652           55 :                     default:
     653           55 :                         if (!IS_SIMPLE_TYPE(type->u.element->type))
     654            0 :                             base_yyerror("internal error: unknown datatype, please report this to <" PACKAGE_BUGREPORT ">");
     655              : 
     656           55 :                         ECPGfree_type(type->u.element);
     657              :                 }
     658           70 :                 break;
     659           39 :             case ECPGt_struct:
     660              :             case ECPGt_union:
     661           39 :                 ECPGfree_struct_member(type->u.members);
     662           39 :                 break;
     663            0 :             default:
     664            0 :                 mmerror(PARSE_ERROR, ET_ERROR, "unrecognized variable type code %d", type->type);
     665            0 :                 break;
     666              :         }
     667              :     }
     668          698 :     free(type->type_name);
     669          698 :     free(type->size);
     670          698 :     free(type->struct_sizeof);
     671          698 :     free(type);
     672          698 : }
     673              : 
     674              : const char *
     675           69 : get_dtype(enum ECPGdtype type)
     676              : {
     677           69 :     switch (type)
     678              :     {
     679            0 :         case ECPGd_count:
     680            0 :             return "ECPGd_count";
     681              :             break;
     682           34 :         case ECPGd_data:
     683           34 :             return "ECPGd_data";
     684              :             break;
     685            2 :         case ECPGd_di_code:
     686            2 :             return "ECPGd_di_code";
     687              :             break;
     688            0 :         case ECPGd_di_precision:
     689            0 :             return "ECPGd_di_precision";
     690              :             break;
     691           17 :         case ECPGd_indicator:
     692           17 :             return "ECPGd_indicator";
     693              :             break;
     694            0 :         case ECPGd_key_member:
     695            0 :             return "ECPGd_key_member";
     696              :             break;
     697            2 :         case ECPGd_length:
     698            2 :             return "ECPGd_length";
     699              :             break;
     700            9 :         case ECPGd_name:
     701            9 :             return "ECPGd_name";
     702              :             break;
     703            0 :         case ECPGd_nullable:
     704            0 :             return "ECPGd_nullable";
     705              :             break;
     706            1 :         case ECPGd_octet:
     707            1 :             return "ECPGd_octet";
     708              :             break;
     709            1 :         case ECPGd_precision:
     710            1 :             return "ECPGd_precision";
     711              :             break;
     712            0 :         case ECPGd_ret_length:
     713            0 :             return "ECPGd_ret_length";
     714            1 :         case ECPGd_ret_octet:
     715            1 :             return "ECPGd_ret_octet";
     716              :             break;
     717            1 :         case ECPGd_scale:
     718            1 :             return "ECPGd_scale";
     719              :             break;
     720            1 :         case ECPGd_type:
     721            1 :             return "ECPGd_type";
     722              :             break;
     723            0 :         case ECPGd_cardinality:
     724            0 :             return "ECPGd_cardinality";
     725            0 :         default:
     726            0 :             mmerror(PARSE_ERROR, ET_ERROR, "unrecognized descriptor item code %d", type);
     727              :     }
     728              : 
     729            0 :     return NULL;
     730              : }
        

Generated by: LCOV version 2.0-1