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
|
/* 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;
}
/*
* String concatenation
*/
/*
* Concatenate 2 strings, inserting a space between them unless either is empty
*
* The input strings are freed.
*/
char *
cat2_str(char *str1, char *str2)
{
char *res_str = (char *) mm_alloc(strlen(str1) + strlen(str2) + 2);
strcpy(res_str, str1);
if (strlen(str1) != 0 && strlen(str2) != 0)
strcat(res_str, " ");
strcat(res_str, str2);
free(str1);
free(str2);
return res_str;
}
/*
* Concatenate N strings, inserting spaces between them unless they are empty
*
* The input strings are freed.
*/
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
*
* The input strings are freed.
*/
char *
make2_str(char *str1, char *str2)
{
char *res_str = (char *) mm_alloc(strlen(str1) + strlen(str2) + 1);
strcpy(res_str, str1);
strcat(res_str, str2);
free(str1);
free(str2);
return res_str;
}
/*
* Concatenate 3 strings, with no space between
*
* The input strings are freed.
*/
char *
make3_str(char *str1, char *str2, char *str3)
{
char *res_str = (char *) mm_alloc(strlen(str1) + strlen(str2) + strlen(str3) + 1);
strcpy(res_str, str1);
strcat(res_str, str2);
strcat(res_str, str3);
free(str1);
free(str2);
free(str3);
return res_str;
}
|