From 5776c16640c599d16737468029444c7f8f1b21be Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Thu, 18 Apr 2024 13:16:05 -0400
Subject: [PATCH v1 5/6] 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 and mmfatal, too.
---
 src/interfaces/ecpg/preproc/Makefile    |   1 +
 src/interfaces/ecpg/preproc/ecpg.header |  65 ---------------
 src/interfaces/ecpg/preproc/meson.build |   1 +
 src/interfaces/ecpg/preproc/type.c      |  24 ------
 src/interfaces/ecpg/preproc/util.c      | 105 ++++++++++++++++++++++++
 5 files changed, 107 insertions(+), 89 deletions(-)
 create mode 100644 src/interfaces/ecpg/preproc/util.c

diff --git a/src/interfaces/ecpg/preproc/Makefile b/src/interfaces/ecpg/preproc/Makefile
index 934b7cef1b..7866037cbb 100644
--- a/src/interfaces/ecpg/preproc/Makefile
+++ b/src/interfaces/ecpg/preproc/Makefile
@@ -36,6 +36,7 @@ OBJS = \
 	preproc.o \
 	type.o \
 	typename.o \
+	util.o \
 	variable.o
 
 # where to find gen_keywordlist.pl and subsidiary files
diff --git a/src/interfaces/ecpg/preproc/ecpg.header b/src/interfaces/ecpg/preproc/ecpg.header
index 46023a0106..48a4670191 100644
--- a/src/interfaces/ecpg/preproc/ecpg.header
+++ b/src/interfaces/ecpg/preproc/ecpg.header
@@ -55,73 +55,8 @@ struct variable no_indicator = {"no_indicator", &ecpg_no_indicator, 0, NULL};
 
 static struct ECPGtype ecpg_query = {ECPGt_char_variable, NULL, NULL, NULL, {NULL}, 0};
 
-static void vmmerror(int error_code, enum errortype type, const char *error, va_list ap) pg_attribute_printf(3, 0);
-
 static bool check_declared_list(const char *name);
 
-/*
- * Handle parsing 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");
-
-	switch (type)
-	{
-		case ET_WARNING:
-			break;
-		case ET_ERROR:
-			ret_value = error_code;
-			break;
-	}
-}
-
-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);
-}
-
-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);
-}
 
 /*
  * string concatenation
diff --git a/src/interfaces/ecpg/preproc/meson.build b/src/interfaces/ecpg/preproc/meson.build
index ddd7a66547..f680e5d59e 100644
--- a/src/interfaces/ecpg/preproc/meson.build
+++ b/src/interfaces/ecpg/preproc/meson.build
@@ -10,6 +10,7 @@ ecpg_sources = files(
   'output.c',
   'parser.c',
   'type.c',
+  'util.c',
   'variable.c',
 )
 
diff --git a/src/interfaces/ecpg/preproc/type.c b/src/interfaces/ecpg/preproc/type.c
index a842bb6a1f..5610a8dc76 100644
--- a/src/interfaces/ecpg/preproc/type.c
+++ b/src/interfaces/ecpg/preproc/type.c
@@ -8,30 +8,6 @@
 
 static struct ECPGstruct_member struct_no_indicator = {"no_indicator", &ecpg_no_indicator, NULL};
 
-/* 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;
-}
-
 /* duplicate memberlist */
 struct ECPGstruct_member *
 ECPGstruct_member_dup(struct ECPGstruct_member *rm)
diff --git a/src/interfaces/ecpg/preproc/util.c b/src/interfaces/ecpg/preproc/util.c
new file mode 100644
index 0000000000..b80802ca9f
--- /dev/null
+++ b/src/interfaces/ecpg/preproc/util.c
@@ -0,0 +1,105 @@
+/* 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;
+}
-- 
2.39.3

