Skip to content

Commit f18231e

Browse files
committed
ecpg: move some functions into a new file ecpg/preproc/util.c.
mm_alloc and mm_strdup were in type.c, which seems a completely random choice. No doubt the original author thought two small functions didn't deserve their own file. But I'm about to add some more memory-management stuff beside them, so let's put them in a less surprising place. This seems like a better home for mmerror, mmfatal, and the cat_str/make_str family, too. Discussion: https://postgr.es/m/2011420.1713493114@sss.pgh.pa.us
1 parent a542d56 commit f18231e

File tree

6 files changed

+195
-153
lines changed

6 files changed

+195
-153
lines changed

src/interfaces/ecpg/preproc/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ OBJS = \
3636
preproc.o \
3737
type.o \
3838
typename.o \
39+
util.o \
3940
variable.o
4041

4142
# where to find gen_keywordlist.pl and subsidiary files

src/interfaces/ecpg/preproc/ecpg.header

Lines changed: 0 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -60,137 +60,8 @@ struct variable no_indicator = {"no_indicator", &ecpg_no_indicator, 0, NULL};
6060

6161
static struct ECPGtype ecpg_query = {ECPGt_char_variable, NULL, NULL, NULL, {NULL}, 0};
6262

63-
static void vmmerror(int error_code, enum errortype type, const char *error, va_list ap) pg_attribute_printf(3, 0);
64-
6563
static bool check_declared_list(const char *name);
6664

67-
/*
68-
* Handle parsing errors and warnings
69-
*/
70-
static void
71-
vmmerror(int error_code, enum errortype type, const char *error, va_list ap)
72-
{
73-
/* localize the error message string */
74-
error = _(error);
75-
76-
fprintf(stderr, "%s:%d: ", input_filename, base_yylineno);
77-
78-
switch (type)
79-
{
80-
case ET_WARNING:
81-
fprintf(stderr, _("WARNING: "));
82-
break;
83-
case ET_ERROR:
84-
fprintf(stderr, _("ERROR: "));
85-
break;
86-
}
87-
88-
vfprintf(stderr, error, ap);
89-
90-
fprintf(stderr, "\n");
91-
92-
switch (type)
93-
{
94-
case ET_WARNING:
95-
break;
96-
case ET_ERROR:
97-
ret_value = error_code;
98-
break;
99-
}
100-
}
101-
102-
void
103-
mmerror(int error_code, enum errortype type, const char *error,...)
104-
{
105-
va_list ap;
106-
107-
va_start(ap, error);
108-
vmmerror(error_code, type, error, ap);
109-
va_end(ap);
110-
}
111-
112-
void
113-
mmfatal(int error_code, const char *error,...)
114-
{
115-
va_list ap;
116-
117-
va_start(ap, error);
118-
vmmerror(error_code, ET_ERROR, error, ap);
119-
va_end(ap);
120-
121-
if (base_yyin)
122-
fclose(base_yyin);
123-
if (base_yyout)
124-
fclose(base_yyout);
125-
126-
if (strcmp(output_filename, "-") != 0 && unlink(output_filename) != 0)
127-
fprintf(stderr, _("could not remove output file \"%s\"\n"), output_filename);
128-
exit(error_code);
129-
}
130-
131-
/*
132-
* string concatenation
133-
*/
134-
135-
static char *
136-
cat2_str(char *str1, char *str2)
137-
{
138-
char *res_str = (char *) mm_alloc(strlen(str1) + strlen(str2) + 2);
139-
140-
strcpy(res_str, str1);
141-
if (strlen(str1) != 0 && strlen(str2) != 0)
142-
strcat(res_str, " ");
143-
strcat(res_str, str2);
144-
free(str1);
145-
free(str2);
146-
return res_str;
147-
}
148-
149-
static char *
150-
cat_str(int count,...)
151-
{
152-
va_list args;
153-
int i;
154-
char *res_str;
155-
156-
va_start(args, count);
157-
158-
res_str = va_arg(args, char *);
159-
160-
/* now add all other strings */
161-
for (i = 1; i < count; i++)
162-
res_str = cat2_str(res_str, va_arg(args, char *));
163-
164-
va_end(args);
165-
166-
return res_str;
167-
}
168-
169-
static char *
170-
make2_str(char *str1, char *str2)
171-
{
172-
char *res_str = (char *) mm_alloc(strlen(str1) + strlen(str2) + 1);
173-
174-
strcpy(res_str, str1);
175-
strcat(res_str, str2);
176-
free(str1);
177-
free(str2);
178-
return res_str;
179-
}
180-
181-
static char *
182-
make3_str(char *str1, char *str2, char *str3)
183-
{
184-
char *res_str = (char *) mm_alloc(strlen(str1) + strlen(str2) + strlen(str3) + 1);
185-
186-
strcpy(res_str, str1);
187-
strcat(res_str, str2);
188-
strcat(res_str, str3);
189-
free(str1);
190-
free(str2);
191-
free(str3);
192-
return res_str;
193-
}
19465

19566
/*
19667
* "Location tracking" support. We commandeer Bison's location tracking

src/interfaces/ecpg/preproc/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ecpg_sources = files(
1010
'output.c',
1111
'parser.c',
1212
'type.c',
13+
'util.c',
1314
'variable.c',
1415
)
1516

src/interfaces/ecpg/preproc/preproc_extern.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ extern int base_yylex(void);
8282
extern void base_yyerror(const char *error);
8383
extern void *mm_alloc(size_t size);
8484
extern char *mm_strdup(const char *string);
85+
extern char *cat2_str(char *str1, char *str2);
86+
extern char *cat_str(int count,...);
87+
extern char *make2_str(char *str1, char *str2);
88+
extern char *make3_str(char *str1, char *str2, char *str3);
8589
extern void mmerror(int error_code, enum errortype type, const char *error,...) pg_attribute_printf(3, 4);
8690
extern void mmfatal(int error_code, const char *error,...) pg_attribute_printf(2, 3) pg_attribute_noreturn();
8791
extern void output_get_descr_header(char *desc_name);

src/interfaces/ecpg/preproc/type.c

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,6 @@
88

99
static struct ECPGstruct_member struct_no_indicator = {"no_indicator", &ecpg_no_indicator, NULL};
1010

11-
/* malloc + error check */
12-
void *
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 *
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-
3511
/* duplicate memberlist */
3612
struct ECPGstruct_member *
3713
ECPGstruct_member_dup(struct ECPGstruct_member *rm)

src/interfaces/ecpg/preproc/util.c

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/* src/interfaces/ecpg/preproc/util.c */
2+
3+
#include "postgres_fe.h"
4+
5+
#include <unistd.h>
6+
7+
#include "preproc_extern.h"
8+
9+
static void vmmerror(int error_code, enum errortype type, const char *error, va_list ap) pg_attribute_printf(3, 0);
10+
11+
12+
/*
13+
* Handle preprocessor errors and warnings
14+
*/
15+
static void
16+
vmmerror(int error_code, enum errortype type, const char *error, va_list ap)
17+
{
18+
/* localize the error message string */
19+
error = _(error);
20+
21+
fprintf(stderr, "%s:%d: ", input_filename, base_yylineno);
22+
23+
switch (type)
24+
{
25+
case ET_WARNING:
26+
fprintf(stderr, _("WARNING: "));
27+
break;
28+
case ET_ERROR:
29+
fprintf(stderr, _("ERROR: "));
30+
break;
31+
}
32+
33+
vfprintf(stderr, error, ap);
34+
35+
fprintf(stderr, "\n");
36+
37+
/* If appropriate, set error code to be inspected by ecpg.c */
38+
switch (type)
39+
{
40+
case ET_WARNING:
41+
break;
42+
case ET_ERROR:
43+
ret_value = error_code;
44+
break;
45+
}
46+
}
47+
48+
/* Report an error or warning */
49+
void
50+
mmerror(int error_code, enum errortype type, const char *error,...)
51+
{
52+
va_list ap;
53+
54+
va_start(ap, error);
55+
vmmerror(error_code, type, error, ap);
56+
va_end(ap);
57+
}
58+
59+
/* Report an error and abandon execution */
60+
void
61+
mmfatal(int error_code, const char *error,...)
62+
{
63+
va_list ap;
64+
65+
va_start(ap, error);
66+
vmmerror(error_code, ET_ERROR, error, ap);
67+
va_end(ap);
68+
69+
if (base_yyin)
70+
fclose(base_yyin);
71+
if (base_yyout)
72+
fclose(base_yyout);
73+
74+
if (strcmp(output_filename, "-") != 0 && unlink(output_filename) != 0)
75+
fprintf(stderr, _("could not remove output file \"%s\"\n"), output_filename);
76+
exit(error_code);
77+
}
78+
79+
/*
80+
* Basic memory management support
81+
*/
82+
83+
/* malloc + error check */
84+
void *
85+
mm_alloc(size_t size)
86+
{
87+
void *ptr = malloc(size);
88+
89+
if (ptr == NULL)
90+
mmfatal(OUT_OF_MEMORY, "out of memory");
91+
92+
return ptr;
93+
}
94+
95+
/* strdup + error check */
96+
char *
97+
mm_strdup(const char *string)
98+
{
99+
char *new = strdup(string);
100+
101+
if (new == NULL)
102+
mmfatal(OUT_OF_MEMORY, "out of memory");
103+
104+
return new;
105+
}
106+
107+
/*
108+
* String concatenation
109+
*/
110+
111+
/*
112+
* Concatenate 2 strings, inserting a space between them unless either is empty
113+
*
114+
* The input strings are freed.
115+
*/
116+
char *
117+
cat2_str(char *str1, char *str2)
118+
{
119+
char *res_str = (char *) mm_alloc(strlen(str1) + strlen(str2) + 2);
120+
121+
strcpy(res_str, str1);
122+
if (strlen(str1) != 0 && strlen(str2) != 0)
123+
strcat(res_str, " ");
124+
strcat(res_str, str2);
125+
free(str1);
126+
free(str2);
127+
return res_str;
128+
}
129+
130+
/*
131+
* Concatenate N strings, inserting spaces between them unless they are empty
132+
*
133+
* The input strings are freed.
134+
*/
135+
char *
136+
cat_str(int count,...)
137+
{
138+
va_list args;
139+
int i;
140+
char *res_str;
141+
142+
va_start(args, count);
143+
144+
res_str = va_arg(args, char *);
145+
146+
/* now add all other strings */
147+
for (i = 1; i < count; i++)
148+
res_str = cat2_str(res_str, va_arg(args, char *));
149+
150+
va_end(args);
151+
152+
return res_str;
153+
}
154+
155+
/*
156+
* Concatenate 2 strings, with no space between
157+
*
158+
* The input strings are freed.
159+
*/
160+
char *
161+
make2_str(char *str1, char *str2)
162+
{
163+
char *res_str = (char *) mm_alloc(strlen(str1) + strlen(str2) + 1);
164+
165+
strcpy(res_str, str1);
166+
strcat(res_str, str2);
167+
free(str1);
168+
free(str2);
169+
return res_str;
170+
}
171+
172+
/*
173+
* Concatenate 3 strings, with no space between
174+
*
175+
* The input strings are freed.
176+
*/
177+
char *
178+
make3_str(char *str1, char *str2, char *str3)
179+
{
180+
char *res_str = (char *) mm_alloc(strlen(str1) + strlen(str2) + strlen(str3) + 1);
181+
182+
strcpy(res_str, str1);
183+
strcat(res_str, str2);
184+
strcat(res_str, str3);
185+
free(str1);
186+
free(str2);
187+
free(str3);
188+
return res_str;
189+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy