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