om: "Martin J. Laubach" <mjl@CSlab.tuwien.ac.at>
Subject: [HACKERS] Patch for io routines I am currently trying to improve on the front-backend communication routines; and noticed that lots of code are duplicated for libpq and the backend. This is a first patch that tries to share code between the two, more to follow. mjl
This commit is contained in:
parent
a9049a4a28
commit
ea58f28ee8
|
@ -6,7 +6,7 @@
|
|||
*
|
||||
* Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* $Id: pqcomm.h,v 1.8 1997/03/12 21:22:19 scrappy Exp $
|
||||
* $Id: pqcomm.h,v 1.9 1997/03/16 18:50:47 scrappy Exp $
|
||||
*
|
||||
* NOTES
|
||||
* Some of this should move to libpq.h
|
||||
|
@ -123,6 +123,11 @@ typedef struct Port {
|
|||
extern FILE *Pfout, *Pfin;
|
||||
extern int PQAsyncNotifyWaiting;
|
||||
|
||||
/* in pqcompriv.c */
|
||||
int pqGetShort(int *, FILE *);
|
||||
int pqGetLong(int *, FILE *);
|
||||
int pqPutShort(int, FILE *);
|
||||
int pqPutLong(int, FILE *);
|
||||
/*
|
||||
* prototypes for functions in pqpacket.c
|
||||
*/
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
#
|
||||
#
|
||||
# IDENTIFICATION
|
||||
# $Header: /cvsroot/pgsql/src/interfaces/libpq/Makefile,v 1.29 1997/03/15 19:17:03 scrappy Exp $
|
||||
# $Header: /cvsroot/pgsql/src/interfaces/libpq/Makefile,v 1.30 1997/03/16 18:51:13 scrappy Exp $
|
||||
#
|
||||
#-------------------------------------------------------------------------
|
||||
|
||||
|
@ -24,7 +24,7 @@ CFLAGS+= $(KRBFLAGS)
|
|||
endif
|
||||
|
||||
OBJS= fe-auth.o fe-connect.o fe-exec.o fe-misc.o fe-lobj.o \
|
||||
dllist.o pqsignal.o
|
||||
dllist.o pqsignal.o pqcomprim.o
|
||||
|
||||
# Shared library stuff
|
||||
shlib :=
|
||||
|
@ -58,7 +58,10 @@ fe-lobj.o: ../backend/fmgr.h
|
|||
# We need to compile this with special options for shared libs,
|
||||
# so we can't use the object in ../backend
|
||||
dllist.c: ../backend/lib/dllist.c
|
||||
-ln ../backend/lib/dllist.c
|
||||
-ln -s ../backend/lib/dllist.c
|
||||
|
||||
pqcomprim.c: ../backend/libpq/pqcomprim.c
|
||||
-ln -s ../backend/libpq/pqcomprim.c
|
||||
|
||||
# The following rules cause dependencies in the backend directory to
|
||||
# get made if they don't exist, but don't cause them to get remade if they
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.4 1996/12/31 07:29:17 bryanh Exp $
|
||||
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.5 1997/03/16 18:51:29 scrappy Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
|
@ -23,76 +23,73 @@
|
|||
|
||||
#include "libpq-fe.h"
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
/* pqGetc:
|
||||
get a character from stream f
|
||||
|
||||
if debug is set, also echo the character fetched
|
||||
*/
|
||||
int
|
||||
pqGetc(FILE* fin, FILE* debug)
|
||||
int pqGetc(FILE* fin, FILE* debug)
|
||||
{
|
||||
int c;
|
||||
|
||||
c = getc(fin);
|
||||
|
||||
if (debug && c != EOF)
|
||||
fprintf(debug, "From backend> %c\n", c);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
/* pqPutnchar:
|
||||
send a string of exactly len length into stream f
|
||||
|
||||
returns 1 if there was an error, 0 otherwise.
|
||||
*/
|
||||
int
|
||||
pqPutnchar(const char* s, int len, FILE *f, FILE *debug)
|
||||
int pqPutnchar(const char* s, int len, FILE *f, FILE *debug)
|
||||
{
|
||||
int status;
|
||||
|
||||
if (f == NULL)
|
||||
return 1;
|
||||
|
||||
if (debug) fputs("To backend>", debug);
|
||||
while (len--) {
|
||||
status = fputc(*s,f);
|
||||
if (debug)
|
||||
fputc(*s,debug);
|
||||
s++;
|
||||
if (status == EOF)
|
||||
if(debug)
|
||||
fprintf(debug, "To backend> %s\n", s);
|
||||
|
||||
if(fwrite(s, 1, len, f) != len)
|
||||
return 1;
|
||||
}
|
||||
if (debug) fputc('\n', debug);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
/* pqGetnchar:
|
||||
get a string of exactly len length from stream f
|
||||
*/
|
||||
int
|
||||
pqGetnchar(char* s, int len, FILE *f, FILE *debug)
|
||||
int pqGetnchar(char* s, int len, FILE *f, FILE *debug)
|
||||
{
|
||||
int c;
|
||||
int cnt;
|
||||
|
||||
if (f == NULL)
|
||||
return 1;
|
||||
|
||||
while (len-- && (c = getc(f)) != EOF)
|
||||
*s++ = c;
|
||||
*s = '\0';
|
||||
cnt = fread(s, 1, len, f);
|
||||
s[cnt] = '\0';
|
||||
/* mjl: actually needs up to len+1 bytes, is this okay? XXX */
|
||||
|
||||
if (debug)
|
||||
fprintf(debug, "From backend (%d)> %s\n", len, s);
|
||||
|
||||
if (debug) {
|
||||
fprintf(debug, "From backend> %s\n", s);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
/* pqGets:
|
||||
get a string of up to length len from stream f
|
||||
*/
|
||||
int
|
||||
pqGets(char* s, int len, FILE *f, FILE *debug)
|
||||
int pqGets(char* s, int len, FILE *f, FILE *debug)
|
||||
{
|
||||
int c;
|
||||
const char *str = s;
|
||||
|
||||
if (f == NULL)
|
||||
return 1;
|
||||
|
@ -100,104 +97,98 @@ pqGets(char* s, int len, FILE *f, FILE *debug)
|
|||
while (len-- && (c = getc(f)) != EOF && c)
|
||||
*s++ = c;
|
||||
*s = '\0';
|
||||
/* mjl: actually needs up to len+1 bytes, is this okay? XXX */
|
||||
|
||||
if (debug)
|
||||
fprintf(debug, "From backend> \"%s\"\n", str);
|
||||
|
||||
if (debug) {
|
||||
fprintf(debug, "From backend> %s\n", s);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
/* pgPutInt
|
||||
send an integer of up to 4 bytesto the file stream
|
||||
do this one byte at at time.
|
||||
This insures that machines with different ENDIANness can talk to each other
|
||||
get a n-byte integer from the stream into result
|
||||
send an integer of 2 or 4 bytes to the file stream, compensate
|
||||
for host endianness.
|
||||
returns 0 if successful, 1 otherwise
|
||||
*/
|
||||
int
|
||||
pqPutInt(const int integer, int bytes, FILE* f, FILE *debug)
|
||||
int pqPutInt(const int integer, int bytes, FILE* f, FILE *debug)
|
||||
{
|
||||
int i;
|
||||
int status;
|
||||
int retval = 0;
|
||||
|
||||
i = integer;
|
||||
switch(bytes)
|
||||
{
|
||||
case 2:
|
||||
retval = pqPutShort(integer, f);
|
||||
break;
|
||||
case 4:
|
||||
retval = pqPutLong(integer, f);
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "** int size %d not supported\n", bytes);
|
||||
retval = 1;
|
||||
}
|
||||
|
||||
if (bytes > 4)
|
||||
bytes = 4;
|
||||
if (debug) fprintf(debug, "To backend (%d#)> %d\n", bytes, integer);
|
||||
|
||||
while (bytes--) {
|
||||
status = fputc(i & 0xff, f);
|
||||
i >>= 8;
|
||||
if (status == EOF) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
if (debug) fprintf(debug, "To backend (#)> %d\n", integer);
|
||||
return 0;
|
||||
return retval;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
/* pgGetInt
|
||||
reconstructs the integer one byte at a time.
|
||||
This insures that machines with different ENDIANness can talk to each other
|
||||
get a n-byte integer from the stream into result
|
||||
read a 2 or 4 byte integer from the stream and swab it around
|
||||
to compensate for different endianness
|
||||
returns 0 if successful
|
||||
*/
|
||||
int
|
||||
pqGetInt(int* result, int bytes, FILE* f, FILE *debug)
|
||||
int pqGetInt(int* result, int bytes, FILE* f, FILE *debug)
|
||||
{
|
||||
int c;
|
||||
int p;
|
||||
int n;
|
||||
|
||||
if (f == NULL)
|
||||
return 1;
|
||||
int retval = 0;
|
||||
|
||||
p = 0;
|
||||
n = 0;
|
||||
while (bytes && (c = getc(f)) != EOF)
|
||||
switch(bytes)
|
||||
{
|
||||
n |= (c & 0xff) << p;
|
||||
p += 8;
|
||||
bytes--;
|
||||
case 2:
|
||||
retval = pqGetShort(result, f);
|
||||
break;
|
||||
case 4:
|
||||
retval = pqGetLong(result, f);
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "** int size %d not supported\n", bytes);
|
||||
retval = 1;
|
||||
}
|
||||
|
||||
if (bytes != 0)
|
||||
return 1;
|
||||
|
||||
*result = n;
|
||||
if (debug)
|
||||
fprintf(debug,"From backend (#)> %d\n",*result);
|
||||
return 0;
|
||||
fprintf(debug,"From backend (#%d)> %d\n", bytes, *result);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
pqPuts(const char* s, FILE *f, FILE *debug)
|
||||
/* --------------------------------------------------------------------- */
|
||||
int pqPuts(const char* s, FILE *f, FILE *debug)
|
||||
{
|
||||
if (f == NULL)
|
||||
return 1;
|
||||
|
||||
if (fputs(s,f) == EOF)
|
||||
if (fputs(s, f) == EOF)
|
||||
return 1;
|
||||
|
||||
fputc('\0',f); /* important to send an ending EOF since backend expects it */
|
||||
fputc('\0', f); /* important to send an ending \0 since backend expects it */
|
||||
fflush(f);
|
||||
|
||||
if (debug) {
|
||||
fprintf(debug, "To backend> %s\n", s);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
pqFlush(FILE *f, FILE *debug)
|
||||
/* --------------------------------------------------------------------- */
|
||||
void pqFlush(FILE *f, FILE *debug)
|
||||
{
|
||||
if (f)
|
||||
fflush(f);
|
||||
|
||||
if (debug)
|
||||
fflush(debug);
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
|
Loading…
Reference in New Issue