www.digitalmars.com         C & C++   DMDScript  

c++.announce - Client libpq library and psql client program for DMC

reply Hans Oesterholt-Dijkema <hdnews gawab.com> writes:
L.S.,

I've made a patch against PostgreSQL 7.4.3 to be able to
compile 'libpq'  and 'psql'  with Digital Mars C++.

As Digital Mars C++ doesn't support 'popen()' and 'pclose()'
calls (yet), I was in need to patch some psql .c files to let
them work well with the Digital Mars C++ port.

One needs MSYS and mingw32-make to patch and compile the
postgres sources. The Makefiles (Makefile.sc) are GNU Makefiles. 

Read doc/README.dmc in the postgres source tree and follow
instructions.

One can apply the patch doing:

    tar xzf postgresql-7.4.3.tar.gz
    cd postgresql-7.4.3
    patch -p1 <pgsql-7.4.3-dmc.patch

Best regards,

Hans Oesterholt-Dijkema
Jun 23 2004
parent Hans Oesterholt-Dijkema <hdnews gawab.com> writes:
diff -u -r -N postgresql-7.4.3/doc/README.dmc
postgresql-7.4.3.dmc/doc/README.dmc
--- postgresql-7.4.3/doc/README.dmc	Thu Jan  1 00:00:00 1970
+++ postgresql-7.4.3.dmc/doc/README.dmc	Wed Jun 23 09:29:44 2004
   -0,0 +1,69   
+Introduction
+************
+
+This readme file addresses the client port of PostgreSQL 
+for the Digital Mars C(++) Compiler
+
+Prerequisites
+*************
+
+You will need following packages to compile libpq and psql
+with Digital Mars C(++):
+
+	Digital Mars C(++) >=8.34
+	MSYS >=1.0
+	GNU Make for WIN32 >=3.75 ==> mingw32-make will do the job.
+
+
+
+Makefiles
+*********
+
+To compile the client library 'libpq.lib' and the client
+program 'psql.exe' with Digital Mars C(++), the following
+have been added to the PostgreSQL source directories:
+
+	src/Makefile.sc
+	src/Makefile.sc.in
+	src/bin/psql/Makefile.sc
+	src/interfaces/libpq/Makefile.sc
+	
+To change the default settings for these Makefiles, edit
+'src/Makefile.sc.in'. This makefile contains the default
+paths, prefixes, etc.
+
+
+
+		
+Compiling
+*********
+	
+Important to note is following:
+
+When you have entered the MSYS shell, using 'make -fMakefile.sc' 
+won't work, because of UNIX shell incompatilities with some
+DMC programs, notably:
+
+	lib.exe
+	link.exe
+	
+So you will have to do the following:
+
+
+	1. Run the MSYS shell
+	2. Type:
+		cd <Postgresql Source Base Directory>
+		cd src
+		vi Makefile.sc.in
+		mingw32-make -f Makefile.sc
+		mingw32-make -f Makefile.sc install
+		mingw32-make -f Makefile.sc clean
+
+That's all.
+
+
+Author
+******
+
+(p) 6-2004 Hans Oesterholt-Dijkema <hdnews -at- gawab.com>
+
diff -u -r -N postgresql-7.4.3/src/Makefile.sc
postgresql-7.4.3.dmc/src/Makefile.sc
--- postgresql-7.4.3/src/Makefile.sc	Thu Jan  1 00:00:00 1970
+++ postgresql-7.4.3.dmc/src/Makefile.sc	Wed Jun 23 08:38:18 2004
   -0,0 +1,38   

















+
+include ./Makefile.sc.in
+
+all: 
+	echo "$(OS) $(SHELL)"
+	if [ ! -f include/pg_config.h ]; then $(cp) include/pg_config.h.win32
include/pg_config.h; fi
+	$(MAKE) -C interfaces/libpq -f Makefile.sc
+	$(MAKE) -C bin/psql -f Makefile.sc
+	 echo All Win32 parts have been built!
+
+clean:
+	$(MAKE) -C interfaces/libpq -f Makefile.sc clean
+	$(MAKE) -C bin/psql -f Makefile.sc clean
+	 echo All Win32 parts have been cleaned!
+
+distclean: clean
+	$(rm) include/config.h
+
+install: all
+	$(MAKE) -C interfaces/libpq -f Makefile.sc install
+	$(MAKE) -C bin/psql -f Makefile.sc install
diff -u -r -N postgresql-7.4.3/src/Makefile.sc.in
postgresql-7.4.3.dmc/src/Makefile.sc.in
--- postgresql-7.4.3/src/Makefile.sc.in	Thu Jan  1 00:00:00 1970
+++ postgresql-7.4.3.dmc/src/Makefile.sc.in	Wed Jun 23 08:10:16 2004
   -0,0 +1,36   







+
+DMCDIR=c:/dm
+
+EXE=.exe
+LIB=.lib
+OBJ=.obj
+LOC_INC=-I. -Ic:/local/dm/include -I../../include -D_MSC_VER 
-I../../interfaces/libpq
+INC=-I$(DMCDIR)/stlport/stlport -I$(DMCDIR)/include $(LOC_INC)
+HAVES=-DHAVE_RANDOM -DHAVE_SIG_ATOMIC_T -DHAVE_STRDUP -DHAVE_VSNPRINTF  
+DEFINES=-DHAVE_RANDOM -DHAVE_SIG_ATOMIC_T -D__DMC__ -DFRONTEND -DWIN32
-D_WINDOWS -D_CONSOLE -D_MBCS
+CFLAGS=$(DEFINES) $(HAVES) -Jm -o
+DMCL=$(DMCDIR)/lib
+LOC_LD=
+CXX=dmc
+AR=lib
+ARFLAGS=-c
+LD=$(CXX)
+POD=spod2html --pod
+PREFIX=/c/local/dm
+PREFIX_BIN=$(PREFIX)/bin
+PREFIX_LIB=$(PREFIX)/lib
+PREFIX_INC=$(PREFIX)/include
+PREFIX_DOC=$(PREFIX)/html
+
+mkdir_p=mkdir -p
+cp=cp
+cd=cd
+RM=rm -f
+
diff -u -r -N postgresql-7.4.3/src/bin/psql/Makefile.sc
postgresql-7.4.3.dmc/src/bin/psql/Makefile.sc
--- postgresql-7.4.3/src/bin/psql/Makefile.sc	Thu Jan  1 00:00:00 1970
+++ postgresql-7.4.3.dmc/src/bin/psql/Makefile.sc	Wed Jun 23 08:49:50 2004
   -0,0 +1,84   




















+
+include ../../Makefile.sc.in
+
+INTDIR=.
+PORT=../../port
+BACK=../../backend/lib
+BLIBPQ=../../backend/libpq
+BACKUTIL=../../backend/utils/mb
+LIBPQDIR=../../interfaces/libpq
+

+
+OBJECTS= \
+	$(INTDIR)/command.obj \
+	$(INTDIR)/common.obj \
+	$(INTDIR)/help.obj \
+	$(INTDIR)/input.obj \
+	$(INTDIR)/stringutils.obj \
+	$(INTDIR)/mainloop.obj \
+	$(INTDIR)/copy.obj \
+	$(INTDIR)/startup.obj \
+	$(INTDIR)/prompt.obj \
+	$(INTDIR)/variables.obj \
+	$(INTDIR)/large_obj.obj \
+	$(INTDIR)/print.obj \
+	$(INTDIR)/describe.obj \
+	$(INTDIR)/tab-complete.obj \
+	$(PORT)/sprompt.obj \
+	$(PORT)/getopt.obj \
+	$(PORT)/getopt_long.obj \
+	$(PORT)/path.obj \
+	$(INTDIR)/mbprint.obj
+	
+DMCLIBS=$(DMCL)/wsock32.lib $(DMCL)/advapi32.lib 
+	
+LIBS= \
+	$(LIBPQDIR)/libpq.lib \
+	$(DMCLIBS)
+	

+
+all: psql$(EXE)
+	 echo "done"
+	
+psql$(EXE): $(OBJECTS)
+	if [ ! -d ./release ]; then $(mkdir_p) ./release; fi
+	for obj in $(OBJECTS); do $(cp) $$obj ./release; done
+	for lib in $(LIBS); do $(cp) $$lib ./release; done
+	$(cd) ./release;$(LD) -opsql$(EXE) *$(OBJ) *$(LIB)
+
+
+$(OBJECTS): %$(OBJ): %.c
+	if [ ! -d ./dmc ]; then $(mkdir_p) ./dmc; fi
+	$(CXX) $(CFLAGS) $(INC) $(OPT) -c $< -o$ 
+
+
+clean: 
+	$(RM) $(OBJECTS) 
+	$(RM) -r ./release
+
+
+install:
+	$(cp) ./release/psql$(EXE) $(PREFIX_BIN)
+	
\ No newline at end of file
diff -u -r -N postgresql-7.4.3/src/bin/psql/command.c
postgresql-7.4.3.dmc/src/bin/psql/command.c
--- postgresql-7.4.3/src/bin/psql/command.c	Sat Oct 11 18:04:26 2003
+++ postgresql-7.4.3.dmc/src/bin/psql/command.c	Wed Jun 23 08:21:50 2004
   -782,8 +782,13   
 			{
 				if (fname[0] == '|')
 				{
+#ifdef __DMC__
+					fprintf(stderr,"Pipes are not supported on this variant (compiled with
Digital Mars C++)\n");
+					fd = fopen(fname,"w");
+#else					
 					is_pipe = true;
 					fd = popen(&fname[1], "w");
+#endif					
 				}
 				else
 					fd = fopen(fname, "w");
   -803,10 +808,14   
 			if (query_buf && query_buf->len > 0)
 				fprintf(fd, "%s\n", query_buf->data);
 
+#ifndef __DMC__				
 			if (is_pipe)
 				result = pclose(fd);
 			else
 				result = fclose(fd);
+#else
+			result = fclose(fd);
+#endif
 
 			if (result == EOF)
 			{
   -1005,7 +1014,12   
 				options_string[pos + 1 + len] = '\0';
 				file = options_string + pos + 1;
 
+#ifdef __DMC__				
+				fprintf(stderr,"Pipes are not supported on this variant (compiled with
Digital Mars C++)\n");
+				fd = NULL;
+#else				
 				fd = popen(file, "r");
+#endif				
 				if (!fd)
 				{
 					psql_error("%s: %s\n", file, strerror(errno));
   -1030,11 +1044,13   
 					appendPQExpBufferChar(&output, '\0');
 				}
 
+#ifndef __DMC__
 				if (fd && pclose(fd) == -1)
 				{
 					psql_error("%s: %s\n", file, strerror(errno));
 					error = true;
 				}
+#endif				
 
 				if (!error)
 				{
diff -u -r -N postgresql-7.4.3/src/bin/psql/common.c
postgresql-7.4.3.dmc/src/bin/psql/common.c
--- postgresql-7.4.3/src/bin/psql/common.c	Wed Nov 12 22:55:42 2003
+++ postgresql-7.4.3.dmc/src/bin/psql/common.c	Wed Jun 23 08:17:54 2004
   -107,10 +107,14   
 	/* Close old file/pipe */
 	if (pset.queryFout && pset.queryFout != stdout && pset.queryFout != stderr)
 	{
+#ifdef __DMC__		
+		fclose(pset.queryFout);
+#else
 		if (pset.queryFoutPipe)
 			pclose(pset.queryFout);
 		else
 			fclose(pset.queryFout);
+#endif			
 	}
 
 	/* If no filename, set stdout */
   -121,8 +125,15   
 	}
 	else if (*fname == '|')
 	{
+#ifdef __DMC__		
+		fprintf(stderr,"Pipes are not supported on this variant (compiled with
Digital Mars C++)\n");
+		pset.queryFout = stdout;
+		pset.queryFoutPipe = false;
+#else
 		pset.queryFout = popen(fname + 1, "w");
 		pset.queryFoutPipe = true;
+#endif		
+		
 	}
 	else
 	{
diff -u -r -N postgresql-7.4.3/src/bin/psql/help.c
postgresql-7.4.3.dmc/src/bin/psql/help.c
--- postgresql-7.4.3/src/bin/psql/help.c	Thu Oct  2 06:39:32 2003
+++ postgresql-7.4.3.dmc/src/bin/psql/help.c	Wed Jun 23 08:38:54 2004
   -82,6 +82,9   
 /* If this " is the start of the string then it ought to end there to fit in
80 columns >> " */
 	printf(_("This is psql %s, the PostgreSQL interactive terminal.\n\n"),
 		   PG_VERSION);
+#ifdef __DMC__
+	printf(_("%s can't use Pipes.\n\n"),__DMC_VERSION_STRING__);
+#endif		   
 	puts(_("Usage:"));
 	puts(_("  psql [OPTIONS]... [DBNAME [USERNAME]]\n"));
 
   -252,7 +255,9   
 
 	if (output != stdout)
 	{
+#ifndef __DMC__
 		pclose(output);
+#endif		
 #ifndef WIN32
 		pqsignal(SIGPIPE, SIG_DFL);
 #endif
   -293,7 +298,9   
 		/* Only close if we used the pager */
 		if (output != stdout)
 		{
+#ifndef __DMC__			
 			pclose(output);
+#endif			
 #ifndef WIN32
 			pqsignal(SIGPIPE, SIG_DFL);
 #endif
   -355,7 +362,9   
 		/* Only close if we used the pager */
 		if (output != stdout)
 		{
+#ifndef __DMC__			
 			pclose(output);
+#endif			
 #ifndef WIN32
 			pqsignal(SIGPIPE, SIG_DFL);
 #endif
diff -u -r -N postgresql-7.4.3/src/bin/psql/print.c
postgresql-7.4.3.dmc/src/bin/psql/print.c
--- postgresql-7.4.3/src/bin/psql/print.c	Thu Aug 14 18:49:42 2003
+++ postgresql-7.4.3.dmc/src/bin/psql/print.c	Wed Jun 23 08:18:14 2004
   -995,6 +995,9   
 PageOutput(int lines, unsigned short int pager)
 {
 	/* check whether we need / can / are supposed to use pager */
+#ifdef __DMC__
+	return stdout;
+#else
 	if (pager
 #ifndef WIN32
 		&&
   -1028,6 +1031,7   
 	}
 
 	return stdout;
+#endif	
 }
 
 
   -1117,7 +1121,9   
 	/* Only close if we used the pager */
 	if (fout == stdout && output != stdout)
 	{
+#ifndef __DMC__		
 		pclose(output);
+#endif		
 #ifndef WIN32
 		pqsignal(SIGPIPE, SIG_DFL);
 #endif
diff -u -r -N postgresql-7.4.3/src/bin/psql/prompt.c
postgresql-7.4.3.dmc/src/bin/psql/prompt.c
--- postgresql-7.4.3/src/bin/psql/prompt.c	Sat Oct  4 01:04:46 2003
+++ postgresql-7.4.3.dmc/src/bin/psql/prompt.c	Wed Jun 23 08:18:34 2004
   -250,12 +250,18   
 
 						cmdend = strcspn(file, "`");
 						file[cmdend] = '\0';
+#ifdef __DMC__						
+						fprintf(stderr,"Pipes are not supported on this variant (compiled with
Digital Mars C++)\n");
+#else							
 						if (file)
 							fd = popen(file, "r");
+#endif							
 						if (fd)
 						{
 							fgets(buf, MAX_PROMPT_SIZE - 1, fd);
+#ifndef __DMC__							
 							pclose(fd);
+#endif							
 						}
 						if (strlen(buf) > 0 && buf[strlen(buf) - 1] == '\n')
 							buf[strlen(buf) - 1] = '\0';
diff -u -r -N postgresql-7.4.3/src/include/pg_config.h
postgresql-7.4.3.dmc/src/include/pg_config.h
--- postgresql-7.4.3/src/include/pg_config.h	Thu Jan  1 00:00:00 1970
+++ postgresql-7.4.3.dmc/src/include/pg_config.h	Wed Jun 23 08:05:44 2004
   -0,0 +1,63   
+#ifndef	pg_config_h_win32__
+#define	pg_config_h_win32__
+/*
+ * Parts of pg_config.h that you get with autoconf on other systems
+ */
+#define PG_VERSION "7.4.3"
+#define PG_VERSION_STR "7.4.3 (win32)"
+
+#define SYSCONFDIR ""
+
+#define DEF_PGPORT 5432
+#define DEF_PGPORT_STR "5432"
+
+#define MAXIMUM_ALIGNOF 4
+#define ACCEPT_TYPE_ARG3 int
+
+#define MAXPGPATH 1024
+
+#define INDEX_MAX_KEYS 32
+
+#define HAVE_ATEXIT
+#define HAVE_MEMMOVE
+
+#ifdef __BORLANDC__
+#define HAVE_RANDOM
+#endif
+
+/* use _snprintf instead of snprintf */
+#define	HAVE_DECL_SNPRINTF 1
+#define snprintf	_snprintf
+
+/* defines for dynamic linking on Win32 platform */
+#ifdef __CYGWIN__
+
+#if __GNUC__ && ! defined (__declspec)
+#error You need egcs 1.1 or newer for compiling!
+#endif
+
+#ifdef BUILDING_DLL
+#define DLLIMPORT __declspec (dllexport)
+#else							/* not BUILDING_DLL */
+#define DLLIMPORT __declspec (dllimport)
+#endif
+
+#elif defined(WIN32) && defined(_MSC_VER)		/* not CYGWIN */
+
+#if defined(_DLL)
+#define DLLIMPORT __declspec (dllexport)
+#else							/* not _DLL */
+#define DLLIMPORT __declspec (dllimport)
+#endif
+
+#else							/* not CYGWIN, not MSVC */
+
+#define DLLIMPORT
+
+#endif
+
+#ifndef __CYGWIN__
+#include <windows.h>
+#endif
+
+#endif /* pg_config_h_win32__ */
diff -u -r -N postgresql-7.4.3/src/interfaces/libpq/Makefile.sc
postgresql-7.4.3.dmc/src/interfaces/libpq/Makefile.sc
--- postgresql-7.4.3/src/interfaces/libpq/Makefile.sc	Thu Jan  1 00:00:00 1970
+++ postgresql-7.4.3.dmc/src/interfaces/libpq/Makefile.sc	Wed Jun 23 08:23:38
2004
   -0,0 +1,75   

















+
+include ../../Makefile.sc.in
+
+INTDIR=.
+PORT=../../port
+BACK=../../backend/lib
+BLIBPQ=../../backend/libpq
+BACKUTIL=../../backend/utils/mb
+INCDIR=../../include
+

+
+OBJECTS= \
+	$(INTDIR)/win32.obj \
+	$(PORT)/getaddrinfo.obj \
+	$(PORT)/thread.obj \
+	$(PORT)/inet_aton.obj \
+	$(PORT)/crypt.obj \
+	$(PORT)/path.obj \
+	$(BACK)/dllist.obj \
+	$(BLIBPQ)/md5.obj \
+	$(BLIBPQ)/ip.obj \
+	$(INTDIR)/fe-auth.obj \
+	$(INTDIR)/fe-protocol2.obj \
+	$(INTDIR)/fe-protocol3.obj \
+	$(INTDIR)/fe-connect.obj \
+	$(INTDIR)/fe-exec.obj \
+	$(INTDIR)/fe-lobj.obj \
+	$(INTDIR)/fe-misc.obj \
+	$(INTDIR)/fe-print.obj \
+	$(INTDIR)/fe-secure.obj \
+	$(INTDIR)/pqexpbuffer.obj \
+	$(BACKUTIL)/wchar.obj \
+	$(BACKUTIL)/encnames.obj 
+	
+
+

+
+all: libpq$(LIB)
+	 echo "done"
+	
+libpq$(LIB): $(OBJECTS)
+	$(AR) $(ARFLAGS) libpq$(LIB) $(OBJECTS)
+
+
+$(OBJECTS): %$(OBJ): %.c
+	if [ ! -d ./dmc ]; then $(mkdir_p) ./dmc; fi
+	$(CXX) $(CFLAGS) $(INC) $(OPT) -c $< -o$ 
+
+
+clean: 
+	$(RM) $(OBJECTS) libpq$(LIB)
+
+install:
+	$(cp) libpq$(LIB) $(PREFIX_LIB)
+	$(cp) libpq-fe.h $(PREFIX_INC)
+	$(cp) $(INCDIR)/postgres_ext.h $(PREFIX_INC)
Jun 23 2004