1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
/* src/interfaces/ecpg/preproc/util.c */
#include "postgres_fe.h"
#include <unistd.h>
#include "preproc_extern.h"
static void vmmerror(int error_code, enum errortype type, const char *error, va_list ap) pg_attribute_printf(3, 0);
/*
* Handle preprocessor errors and warnings
*/
static void
vmmerror(int error_code, enum errortype type, const char *error, va_list ap)
{
/* localize the error message string */
error = _(error);
fprintf(stderr, "%s:%d: ", input_filename, base_yylineno);
switch (type)
{
case ET_WARNING:
fprintf(stderr, _("WARNING: "));
break;
case ET_ERROR:
fprintf(stderr, _("ERROR: "));
break;
}
vfprintf(stderr, error, ap);
fprintf(stderr, "\n");
/* If appropriate, set error code to be inspected by ecpg.c */
switch (type)
{
case ET_WARNING:
break;
case ET_ERROR:
ret_value = error_code;
break;
}
}
/* Report an error or warning */
void
mmerror(int error_code, enum errortype type, const char *error,...)
{
va_list ap;
va_start(ap, error);
vmmerror(error_code, type, error, ap);
va_end(ap);
}
/* Report an error and abandon execution */
void
mmfatal(int error_code, const char *error,...)
{
va_list ap;
va_start(ap, error);
vmmerror(error_code, ET_ERROR, error, ap);
va_end(ap);
if (base_yyin)
fclose(base_yyin);
if (base_yyout)
fclose(base_yyout);
if (strcmp(output_filename, "-") != 0 && unlink(output_filename) != 0)
fprintf(stderr, _("could not remove output file \"%s\"\n"), output_filename);
exit(error_code);
}
/*
* Basic memory management support
*/
/* malloc + error check */
void *
mm_alloc(size_t size)
{
void *ptr = malloc(size);
if (ptr == NULL)
mmfatal(OUT_OF_MEMORY, "out of memory");
return ptr;
}
/* strdup + error check */
char *
mm_strdup(const char *string)
{
char *new = strdup(string);
if (new == NULL)
mmfatal(OUT_OF_MEMORY, "out of memory");
return new;
}
/*
* "Local" memory management support
*
* These functions manage memory that is only needed for a short time
* (processing of one input statement) within the ecpg grammar.
* Data allocated with these is not meant to be freed separately;
* rather it's freed by calling reclaim_local_storage() at the end
* of each statement cycle.
*/
typedef struct loc_chunk
{
struct loc_chunk *next; /* list link */
unsigned int chunk_used; /* index of first unused byte in data[] */
unsigned int chunk_avail; /* # bytes still available in data[] */
char data[FLEXIBLE_ARRAY_MEMBER]; /* actual storage */
} loc_chunk;
#define LOC_CHUNK_OVERHEAD MAXALIGN(offsetof(loc_chunk, data))
#define LOC_CHUNK_MIN_SIZE 8192
/* Head of list of loc_chunks */
static loc_chunk *loc_chunks = NULL;
/*
* Allocate local space of the requested size.
*
* Exits on OOM.
*/
void *
loc_alloc(size_t size)
{
void *result;
loc_chunk *cur_chunk = loc_chunks;
/* Ensure all allocations are adequately aligned */
size = MAXALIGN(size);
/* Need a new chunk? */
if (cur_chunk == NULL || size > cur_chunk->chunk_avail)
{
size_t chunk_size = Max(size, LOC_CHUNK_MIN_SIZE);
cur_chunk = mm_alloc(chunk_size + LOC_CHUNK_OVERHEAD);
/* Depending on alignment rules, we could waste a bit here */
cur_chunk->chunk_used = LOC_CHUNK_OVERHEAD - offsetof(loc_chunk, data);
cur_chunk->chunk_avail = chunk_size;
/* New chunk becomes the head of the list */
cur_chunk->next = loc_chunks;
loc_chunks = cur_chunk;
}
result = cur_chunk->data + cur_chunk->chunk_used;
cur_chunk->chunk_used += size;
cur_chunk->chunk_avail -= size;
return result;
}
/*
* Copy given string into local storage
*/
char *
loc_strdup(const char *string)
{
char *result = loc_alloc(strlen(string) + 1);
strcpy(result, string);
return result;
}
/*
* Reclaim local storage when appropriate
*/
void
reclaim_local_storage(void)
{
loc_chunk *cur_chunk,
*next_chunk;
for (cur_chunk = loc_chunks; cur_chunk; cur_chunk = next_chunk)
{
next_chunk = cur_chunk->next;
free(cur_chunk);
}
loc_chunks = NULL;
}
/*
* String concatenation support routines. These return "local" (transient)
* storage.
*/
/*
* Concatenate 2 strings, inserting a space between them unless either is empty
*/
char *
cat2_str(const char *str1, const char *str2)
{
char *res_str = (char *) loc_alloc(strlen(str1) + strlen(str2) + 2);
strcpy(res_str, str1);
if (strlen(str1) != 0 && strlen(str2) != 0)
strcat(res_str, " ");
strcat(res_str, str2);
return res_str;
}
/*
* Concatenate N strings, inserting spaces between them unless they are empty
*/
char *
cat_str(int count,...)
{
va_list args;
int i;
char *res_str;
va_start(args, count);
res_str = va_arg(args, char *);
/* now add all other strings */
for (i = 1; i < count; i++)
res_str = cat2_str(res_str, va_arg(args, char *));
va_end(args);
return res_str;
}
/*
* Concatenate 2 strings, with no space between
*/
char *
make2_str(const char *str1, const char *str2)
{
char *res_str = (char *) loc_alloc(strlen(str1) + strlen(str2) + 1);
strcpy(res_str, str1);
strcat(res_str, str2);
return res_str;
}
/*
* Concatenate 3 strings, with no space between
*/
char *
make3_str(const char *str1, const char *str2, const char *str3)
{
char *res_str = (char *) loc_alloc(strlen(str1) + strlen(str2) + strlen(str3) + 1);
strcpy(res_str, str1);
strcat(res_str, str2);
strcat(res_str, str3);
return res_str;
}
|