diff options
Diffstat (limited to 'src/interfaces/libpq')
-rw-r--r-- | src/interfaces/libpq/fe-auth.c | 8 | ||||
-rw-r--r-- | src/interfaces/libpq/fe-connect.c | 1028 | ||||
-rw-r--r-- | src/interfaces/libpq/fe-exec.c | 200 | ||||
-rw-r--r-- | src/interfaces/libpq/fe-lobj.c | 22 | ||||
-rw-r--r-- | src/interfaces/libpq/fe-misc.c | 144 | ||||
-rw-r--r-- | src/interfaces/libpq/fe-print.c | 80 | ||||
-rw-r--r-- | src/interfaces/libpq/libpq-fe.h | 132 | ||||
-rw-r--r-- | src/interfaces/libpq/libpq-int.h | 53 | ||||
-rw-r--r-- | src/interfaces/libpq/pqexpbuffer.c | 32 | ||||
-rw-r--r-- | src/interfaces/libpq/pqexpbuffer.h | 10 | ||||
-rw-r--r-- | src/interfaces/libpq/win32.h | 1 |
11 files changed, 914 insertions, 796 deletions
diff --git a/src/interfaces/libpq/fe-auth.c b/src/interfaces/libpq/fe-auth.c index fe870963df4..bf5a21bee9a 100644 --- a/src/interfaces/libpq/fe-auth.c +++ b/src/interfaces/libpq/fe-auth.c @@ -10,7 +10,7 @@ * exceed INITIAL_EXPBUFFER_SIZE (currently 256 bytes). * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-auth.c,v 1.38 2000/03/11 03:08:36 tgl Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-auth.c,v 1.39 2000/04/12 17:17:13 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -94,7 +94,7 @@ static const struct authsvc authsvcs[] = { {"password", STARTUP_PASSWORD_MSG, 0} }; -static const int n_authsvcs = sizeof(authsvcs) / sizeof(struct authsvc); +static const int n_authsvcs = sizeof(authsvcs) / sizeof(struct authsvc); #ifdef KRB4 /*---------------------------------------------------------------- @@ -475,12 +475,12 @@ fe_sendauth(AuthRequest areq, PGconn *conn, const char *hostname, const char *password, char *PQerrormsg) { #if !defined(KRB4) && !defined(KRB5) - (void)hostname; /*not used*/ + (void) hostname; /* not used */ #endif switch (areq) { - case AUTH_REQ_OK: + case AUTH_REQ_OK: break; case AUTH_REQ_KRB4: diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index df572884ebf..c400a7ebb16 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.125 2000/03/24 01:39:55 tgl Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.126 2000/04/12 17:17:14 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -46,18 +46,23 @@ #endif #ifdef WIN32 -static int inet_aton(const char *cp, struct in_addr *inp) { +static int +inet_aton(const char *cp, struct in_addr * inp) +{ unsigned long a = inet_addr(cp); + if (a == -1) return 0; - inp->s_addr = a; + inp->s_addr = a; return 1; } + #endif #ifdef USE_SSL static SSL_CTX *SSL_context = NULL; + #endif #define NOTIFYLIST_INITIAL_SIZE 10 @@ -85,15 +90,17 @@ static SSL_CTX *SSL_context = NULL; * * PQconninfoOptions[] is a constant static array that we use to initialize * a dynamically allocated working copy. All the "val" fields in - * PQconninfoOptions[] *must* be NULL. In a working copy, non-null "val" + * PQconninfoOptions[] *must* be NULL. In a working copy, non-null "val" * fields point to malloc'd strings that should be freed when the working * array is freed (see PQconninfoFree). * ---------- */ static const PQconninfoOption PQconninfoOptions[] = { - /* "authtype" is no longer used, so mark it "don't show". We keep it - * in the array so as not to reject conninfo strings from old apps that - * might still try to set it. + + /* + * "authtype" is no longer used, so mark it "don't show". We keep it + * in the array so as not to reject conninfo strings from old apps + * that might still try to set it. */ {"authtype", "PGAUTHTYPE", DefaultAuthtype, NULL, "Database-Authtype", "D", 20}, @@ -111,7 +118,8 @@ static const PQconninfoOption PQconninfoOptions[] = { "Database-Host", "", 40}, {"hostaddr", "PGHOSTADDR", NULL, NULL, - "Database-Host-IPv4-Address", "", 15}, /* Room for abc.def.ghi.jkl */ + "Database-Host-IPv4-Address", "", 15}, /* Room for + * abc.def.ghi.jkl */ {"port", "PGPORT", DEF_PGPORT, NULL, "Database-Port", "", 6}, @@ -156,22 +164,22 @@ static const struct EnvironmentOptions }; -static int connectDBStart(PGconn *conn); -static int connectDBComplete(PGconn *conn); +static int connectDBStart(PGconn *conn); +static int connectDBComplete(PGconn *conn); static bool PQsetenvStart(PGconn *conn); static PostgresPollingStatusType PQsetenvPoll(PGconn *conn); static PGconn *makeEmptyPGconn(void); static void freePGconn(PGconn *conn); static void closePGconn(PGconn *conn); static PQconninfoOption *conninfo_parse(const char *conninfo, - PQExpBuffer errorMessage); + PQExpBuffer errorMessage); static char *conninfo_getval(PQconninfoOption *connOptions, - const char *keyword); + const char *keyword); static void defaultNoticeProcessor(void *arg, const char *message); /* ---------------- - * Connecting to a Database + * Connecting to a Database * * There are now four different ways a user of this API can connect to the * database. Two are not recommended for use in new code, because of their @@ -187,7 +195,7 @@ static void defaultNoticeProcessor(void *arg, const char *message); * * Internally, the static functions connectDBStart, connectDBComplete * are part of the connection procedure. - * + * * ---------------- */ @@ -218,7 +226,7 @@ static void defaultNoticeProcessor(void *arg, const char *message); PGconn * PQconnectdb(const char *conninfo) { - PGconn *conn = PQconnectStart(conninfo); + PGconn *conn = PQconnectStart(conninfo); if (conn && conn->status != CONNECTION_BAD) (void) connectDBComplete(conn); @@ -235,7 +243,7 @@ PQconnectdb(const char *conninfo) * See comment for PQconnectdb for the definition of the string format. * * Returns a PGconn*. If NULL is returned, a malloc error has occurred, and - * you should not attempt to proceed with this connection. If the status + * you should not attempt to proceed with this connection. If the status * field of the connection returned is CONNECTION_BAD, an error has * occurred. In this case you should call PQfinish on the result, (perhaps * inspecting the error message first). Other fields of the structure may not @@ -258,7 +266,7 @@ PQconnectStart(const char *conninfo) * Allocate memory for the conn structure * ---------- */ - + conn = makeEmptyPGconn(); if (conn == NULL) return (PGconn *) NULL; @@ -324,7 +332,7 @@ PQconnectStart(const char *conninfo) * and their current default values. * * NOTE: as of PostgreSQL 7.0, the returned array is dynamically allocated - * and should be freed when no longer needed via PQconninfoFree(). (In prior + * and should be freed when no longer needed via PQconninfoFree(). (In prior * versions, the returned array was static, but that's not thread-safe.) * Pre-7.0 applications that use this function will see a small memory leak * until they are updated to call PQconninfoFree. @@ -333,7 +341,7 @@ PQconnectStart(const char *conninfo) PQconninfoOption * PQconndefaults(void) { - PQExpBufferData errorBuf; + PQExpBufferData errorBuf; PQconninfoOption *connOptions; initPQExpBuffer(&errorBuf); @@ -388,7 +396,8 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions, const char *pwd) { PGconn *conn; - char *tmp; /* An error message from some service we call. */ + char *tmp; /* An error message from some service we + * call. */ bool error = FALSE; /* We encountered an error. */ conn = makeEmptyPGconn(); @@ -466,15 +475,13 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions, conn->dbName = strdup(dbName); if (error) - { conn->status = CONNECTION_BAD; - } else { if (connectDBStart(conn)) (void) connectDBComplete(conn); } - + return conn; } @@ -595,12 +602,13 @@ connectMakeNonblocking(PGconn *conn) #ifndef WIN32 if (fcntl(conn->sock, F_SETFL, O_NONBLOCK) < 0) #else - int on = 1; + int on = 1; + if (ioctlsocket(conn->sock, FIONBIO, &on) != 0) #endif { printfPQExpBuffer(&conn->errorMessage, - "connectMakeNonblocking -- fcntl() failed: errno=%d\n%s\n", + "connectMakeNonblocking -- fcntl() failed: errno=%d\n%s\n", errno, strerror(errno)); return 0; } @@ -637,7 +645,7 @@ connectNoDelay(PGconn *conn) sizeof(on)) < 0) { printfPQExpBuffer(&conn->errorMessage, - "connectNoDelay() -- setsockopt failed: errno=%d\n%s\n", + "connectNoDelay() -- setsockopt failed: errno=%d\n%s\n", errno, strerror(errno)); #ifdef WIN32 printf("Winsock error: %i\n", WSAGetLastError()); @@ -661,13 +669,16 @@ connectDBStart(PGconn *conn) { int portno, family; + #ifdef USE_SSL - StartupPacket np; /* Used to negotiate SSL connection */ - char SSLok; + StartupPacket np; /* Used to negotiate SSL connection */ + char SSLok; + #endif if (!conn) return 0; + /* * parse dbName to get all additional info in it, if any */ @@ -679,8 +690,8 @@ connectDBStart(PGconn *conn) conn->outCount = 0; /* - * Set up the connection to postmaster/backend. - * Note that this supports IPv4 and UDP only. + * Set up the connection to postmaster/backend. Note that this + * supports IPv4 and UDP only. */ MemSet((char *) &conn->raddr, 0, sizeof(conn->raddr)); @@ -691,11 +702,11 @@ connectDBStart(PGconn *conn) /* Note that this supports IPv4 only */ struct in_addr addr; - if(!inet_aton(conn->pghostaddr, &addr)) + if (!inet_aton(conn->pghostaddr, &addr)) { printfPQExpBuffer(&conn->errorMessage, "connectDBStart() -- " - "invalid host address: %s\n", conn->pghostaddr); + "invalid host address: %s\n", conn->pghostaddr); goto connect_errReturn; } @@ -713,7 +724,7 @@ connectDBStart(PGconn *conn) if ((hp == NULL) || (hp->h_addrtype != AF_INET)) { printfPQExpBuffer(&conn->errorMessage, - "connectDBStart() -- unknown hostname: %s\n", + "connectDBStart() -- unknown hostname: %s\n", conn->pghost); goto connect_errReturn; } @@ -740,8 +751,8 @@ connectDBStart(PGconn *conn) conn->raddr_len = sizeof(struct sockaddr_in); } #if !defined(WIN32) && !defined(__CYGWIN32__) - else - conn->raddr_len = UNIXSOCK_PATH(conn->raddr.un, portno); + else + conn->raddr_len = UNIXSOCK_PATH(conn->raddr.un, portno); #endif @@ -775,82 +786,91 @@ connectDBStart(PGconn *conn) * WIN32_NON_BLOCKING_CONNECTIONS before compilation. If it works, then * this code can be cleaned up. * - * Ewan Mellor <[email protected]>. + * Ewan Mellor <[email protected]>. * ---------- */ #if (!defined(WIN32) || defined(WIN32_NON_BLOCKING_CONNECTIONS)) && !defined(USE_SSL) if (connectMakeNonblocking(conn) == 0) goto connect_errReturn; -#endif +#endif #ifdef USE_SSL - /* This needs to be done before we set into nonblocking, since SSL - * negotiation does not like that mode */ + + /* + * This needs to be done before we set into nonblocking, since SSL + * negotiation does not like that mode + */ /* Attempt to negotiate SSL usage */ - if (conn->allow_ssl_try) { - memset((char *)&np, 0, sizeof(np)); - np.protoVersion = htonl(NEGOTIATE_SSL_CODE); - if (pqPacketSend(conn, (char *) &np, sizeof(StartupPacket)) != STATUS_OK) - { - sprintf(conn->errorMessage, - "connectDB() -- couldn't send SSL negotiation packet: errno=%d\n%s\n", - errno, strerror(errno)); - goto connect_errReturn; - } - /* Now receive the postmasters response */ - if (recv(conn->sock, &SSLok, 1, 0) != 1) { - sprintf(conn->errorMessage, "PQconnectDB() -- couldn't read postmaster response: errno=%d\n%s\n", - errno, strerror(errno)); - goto connect_errReturn; - } - if (SSLok == 'S') { - if (!SSL_context) - { - SSL_load_error_strings(); - SSL_library_init(); - SSL_context = SSL_CTX_new(SSLv23_method()); - if (!SSL_context) { - sprintf(conn->errorMessage, - "connectDB() -- couldn't create SSL context: %s\n", - ERR_reason_error_string(ERR_get_error())); - goto connect_errReturn; + if (conn->allow_ssl_try) + { + memset((char *) &np, 0, sizeof(np)); + np.protoVersion = htonl(NEGOTIATE_SSL_CODE); + if (pqPacketSend(conn, (char *) &np, sizeof(StartupPacket)) != STATUS_OK) + { + sprintf(conn->errorMessage, + "connectDB() -- couldn't send SSL negotiation packet: errno=%d\n%s\n", + errno, strerror(errno)); + goto connect_errReturn; + } + /* Now receive the postmasters response */ + if (recv(conn->sock, &SSLok, 1, 0) != 1) + { + sprintf(conn->errorMessage, "PQconnectDB() -- couldn't read postmaster response: errno=%d\n%s\n", + errno, strerror(errno)); + goto connect_errReturn; + } + if (SSLok == 'S') + { + if (!SSL_context) + { + SSL_load_error_strings(); + SSL_library_init(); + SSL_context = SSL_CTX_new(SSLv23_method()); + if (!SSL_context) + { + sprintf(conn->errorMessage, + "connectDB() -- couldn't create SSL context: %s\n", + ERR_reason_error_string(ERR_get_error())); + goto connect_errReturn; + } + } + if (!(conn->ssl = SSL_new(SSL_context)) || + !SSL_set_fd(conn->ssl, conn->sock) || + SSL_connect(conn->ssl) <= 0) + { + sprintf(conn->errorMessage, + "connectDB() -- couldn't establish SSL connection: %s\n", + ERR_reason_error_string(ERR_get_error())); + goto connect_errReturn; + } + /* SSL connection finished. Continue to send startup packet */ + } + else if (SSLok == 'E') + { + /* Received error - probably protocol mismatch */ + if (conn->Pfdebug) + fprintf(conn->Pfdebug, "Postmaster reports error, attempting fallback to pre-6.6.\n"); + close(conn->sock); + conn->allow_ssl_try = FALSE; + return connectDBStart(conn); + } + else if (SSLok != 'N') + { + strcpy(conn->errorMessage, + "Received invalid negotiation response.\n"); + goto connect_errReturn; } - } - if (!(conn->ssl = SSL_new(SSL_context)) || - !SSL_set_fd(conn->ssl, conn->sock) || - SSL_connect(conn->ssl) <= 0) - { - sprintf(conn->errorMessage, - "connectDB() -- couldn't establish SSL connection: %s\n", - ERR_reason_error_string(ERR_get_error())); - goto connect_errReturn; - } - /* SSL connection finished. Continue to send startup packet */ - } - else if (SSLok == 'E') { - /* Received error - probably protocol mismatch */ - if (conn->Pfdebug) - fprintf(conn->Pfdebug, "Postmaster reports error, attempting fallback to pre-6.6.\n"); - close(conn->sock); - conn->allow_ssl_try = FALSE; - return connectDBStart(conn); - } - else if (SSLok != 'N') { - strcpy(conn->errorMessage, - "Received invalid negotiation response.\n"); - goto connect_errReturn; - } } #endif /* ---------- - * Start / make connection. We are hopefully in non-blocking mode + * Start / make connection. We are hopefully in non-blocking mode * now, but it is possible that: - * 1. Older systems will still block on connect, despite the - * non-blocking flag. (Anyone know if this is true?) - * 2. We are running under Windows, and aren't even trying - * to be non-blocking (see above). - * 3. We are using SSL. + * 1. Older systems will still block on connect, despite the + * non-blocking flag. (Anyone know if this is true?) + * 2. We are running under Windows, and aren't even trying + * to be non-blocking (see above). + * 3. We are using SSL. * Thus, we have make arrangements for all eventualities. * ---------- */ @@ -862,8 +882,11 @@ connectDBStart(PGconn *conn) if (WSAGetLastError() == WSAEINPROGRESS) #endif { - /* This is fine - we're in non-blocking mode, and the - * connection is in progress. */ + + /* + * This is fine - we're in non-blocking mode, and the + * connection is in progress. + */ conn->status = CONNECTION_STARTED; } else @@ -888,12 +911,14 @@ connectDBStart(PGconn *conn) conn->status = CONNECTION_MADE; } - /* This makes the connection non-blocking, for all those cases which forced us - not to do it above. */ + /* + * This makes the connection non-blocking, for all those cases which + * forced us not to do it above. + */ #if (defined(WIN32) && !defined(WIN32_NON_BLOCKING_CONNECTIONS)) || defined(USE_SSL) if (connectMakeNonblocking(conn) == 0) goto connect_errReturn; -#endif +#endif return 1; @@ -929,10 +954,13 @@ connectDBComplete(PGconn *conn) if (conn == NULL || conn->status == CONNECTION_BAD) return 0; - for (;;) { + for (;;) + { + /* - * Wait, if necessary. Note that the initial state (just after - * PQconnectStart) is to wait for the socket to select for writing. + * Wait, if necessary. Note that the initial state (just after + * PQconnectStart) is to wait for the socket to select for + * writing. */ switch (flag) { @@ -941,7 +969,7 @@ connectDBComplete(PGconn *conn) case PGRES_POLLING_OK: return 1; /* success! */ - + case PGRES_POLLING_READING: if (pqWait(1, 0, conn)) { @@ -963,6 +991,7 @@ connectDBComplete(PGconn *conn) conn->status = CONNECTION_BAD; return 0; } + /* * Now try to advance the state machine. */ @@ -977,23 +1006,23 @@ connectDBComplete(PGconn *conn) * * Returns a PostgresPollingStatusType. * Before calling this function, use select(2) to determine when data arrive. - * + * * You must call PQfinish whether or not this fails. * * This function and PQconnectStart are intended to allow connections to be * made without blocking the execution of your program on remote I/O. However, * there are a number of caveats: * - * o If you call PQtrace, ensure that the stream object into which you trace - * will not block. - * o If you do not supply an IP address for the remote host (i.e. you - * supply a host name instead) then this function will block on - * gethostbyname. You will be fine if using Unix sockets (i.e. by - * supplying neither a host name nor a host address). - * o If your backend wants to use Kerberos authentication then you must - * supply both a host name and a host address, otherwise this function - * may block on gethostname. - * o This function will block if compiled with USE_SSL. + * o If you call PQtrace, ensure that the stream object into which you trace + * will not block. + * o If you do not supply an IP address for the remote host (i.e. you + * supply a host name instead) then this function will block on + * gethostbyname. You will be fine if using Unix sockets (i.e. by + * supplying neither a host name nor a host address). + * o If your backend wants to use Kerberos authentication then you must + * supply both a host name and a host address, otherwise this function + * may block on gethostname. + * o This function will block if compiled with USE_SSL. * * ---------------- */ @@ -1008,32 +1037,35 @@ PQconnectPoll(PGconn *conn) /* Get the new data */ switch (conn->status) { - /* We really shouldn't have been polled in these two cases, but - we can handle it. */ + + /* + * We really shouldn't have been polled in these two cases, + * but we can handle it. + */ case CONNECTION_BAD: return PGRES_POLLING_FAILED; case CONNECTION_OK: return PGRES_POLLING_OK; - /* These are reading states */ + /* These are reading states */ case CONNECTION_AWAITING_RESPONSE: case CONNECTION_AUTH_OK: - { - /* Load waiting data */ - int n = pqReadData(conn); - - if (n < 0) - goto error_return; - if (n == 0) - return PGRES_POLLING_READING; + { + /* Load waiting data */ + int n = pqReadData(conn); - break; - } + if (n < 0) + goto error_return; + if (n == 0) + return PGRES_POLLING_READING; + + break; + } - /* These are writing states, so we just proceed. */ + /* These are writing states, so we just proceed. */ case CONNECTION_STARTED: case CONNECTION_MADE: - break; + break; case CONNECTION_SETENV: /* We allow PQsetenvPoll to decide whether to proceed */ @@ -1041,281 +1073,319 @@ PQconnectPoll(PGconn *conn) default: printfPQExpBuffer(&conn->errorMessage, - "PQconnectPoll() -- unknown connection state - " - "probably indicative of memory corruption!\n"); + "PQconnectPoll() -- unknown connection state - " + "probably indicative of memory corruption!\n"); goto error_return; } - keep_going: /* We will come back to here until there is nothing left to - parse. */ - switch(conn->status) +keep_going: /* We will come back to here until there + * is nothing left to parse. */ + switch (conn->status) { case CONNECTION_STARTED: - { - SOCKET_SIZE_TYPE laddrlen; + { + SOCKET_SIZE_TYPE laddrlen; + #ifndef WIN32 - int optval; + int optval; + #else - char optval; -#endif - SOCKET_SIZE_TYPE optlen = sizeof(optval); + char optval; - /* Write ready, since we've made it here, so the connection - * has been made. */ +#endif + SOCKET_SIZE_TYPE optlen = sizeof(optval); - /* Now check (using getsockopt) that there is not an error - state waiting for us on the socket. */ + /* + * Write ready, since we've made it here, so the + * connection has been made. + */ - if (getsockopt(conn->sock, SOL_SOCKET, SO_ERROR, - &optval, &optlen) == -1) - { - printfPQExpBuffer(&conn->errorMessage, - "PQconnectPoll() -- getsockopt() failed: " - "errno=%d\n%s\n", - errno, strerror(errno)); - goto error_return; - } - else if (optval != 0) - { /* - * When using a nonblocking connect, we will typically see - * connect failures at this point, so provide a friendly - * error message. + * Now check (using getsockopt) that there is not an error + * state waiting for us on the socket. */ - printfPQExpBuffer(&conn->errorMessage, - "PQconnectPoll() -- connect() failed: %s\n" - "\tIs the postmaster running%s at '%s'\n" - "\tand accepting connections on %s '%s'?\n", - strerror(optval), - (conn->raddr.sa.sa_family == AF_INET) ? " (with -i)" : "", - conn->pghost ? conn->pghost : "localhost", + + if (getsockopt(conn->sock, SOL_SOCKET, SO_ERROR, + &optval, &optlen) == -1) + { + printfPQExpBuffer(&conn->errorMessage, + "PQconnectPoll() -- getsockopt() failed: " + "errno=%d\n%s\n", + errno, strerror(errno)); + goto error_return; + } + else if (optval != 0) + { + + /* + * When using a nonblocking connect, we will typically + * see connect failures at this point, so provide a + * friendly error message. + */ + printfPQExpBuffer(&conn->errorMessage, + "PQconnectPoll() -- connect() failed: %s\n" + "\tIs the postmaster running%s at '%s'\n" + "\tand accepting connections on %s '%s'?\n", + strerror(optval), + (conn->raddr.sa.sa_family == AF_INET) ? " (with -i)" : "", + conn->pghost ? conn->pghost : "localhost", (conn->raddr.sa.sa_family == AF_INET) ? - "TCP/IP port" : "Unix socket", - conn->pgport); - goto error_return; + "TCP/IP port" : "Unix socket", + conn->pgport); + goto error_return; + } + + /* Fill in the client address */ + laddrlen = sizeof(conn->laddr); + if (getsockname(conn->sock, &conn->laddr.sa, &laddrlen) < 0) + { + printfPQExpBuffer(&conn->errorMessage, + "PQconnectPoll() -- getsockname() failed: " + "errno=%d\n%s\n", + errno, strerror(errno)); + goto error_return; + } + + conn->status = CONNECTION_MADE; + return PGRES_POLLING_WRITING; } - /* Fill in the client address */ - laddrlen = sizeof(conn->laddr); - if (getsockname(conn->sock, &conn->laddr.sa, &laddrlen) < 0) + case CONNECTION_MADE: { - printfPQExpBuffer(&conn->errorMessage, - "PQconnectPoll() -- getsockname() failed: " - "errno=%d\n%s\n", - errno, strerror(errno)); - goto error_return; - } + StartupPacket sp; - conn->status = CONNECTION_MADE; - return PGRES_POLLING_WRITING; - } + /* + * Initialize the startup packet. + */ - case CONNECTION_MADE: - { - StartupPacket sp; - - /* - * Initialize the startup packet. - */ + MemSet((char *) &sp, 0, sizeof(StartupPacket)); - MemSet((char *) &sp, 0, sizeof(StartupPacket)); - - sp.protoVersion = (ProtocolVersion) htonl(PG_PROTOCOL_LIBPQ); - - strncpy(sp.user, conn->pguser, SM_USER); - strncpy(sp.database, conn->dbName, SM_DATABASE); - strncpy(sp.tty, conn->pgtty, SM_TTY); - - if (conn->pgoptions) - strncpy(sp.options, conn->pgoptions, SM_OPTIONS); - - /* Send the startup packet. - * - * Theoretically, this could block, but it really shouldn't - * since we only got here if the socket is write-ready. - */ + sp.protoVersion = (ProtocolVersion) htonl(PG_PROTOCOL_LIBPQ); - if (pqPacketSend(conn, (char *) &sp, - sizeof(StartupPacket)) != STATUS_OK) - { - printfPQExpBuffer(&conn->errorMessage, - "PQconnectPoll() -- " - "couldn't send startup packet: " - "errno=%d\n%s\n", - errno, strerror(errno)); - goto error_return; - } + strncpy(sp.user, conn->pguser, SM_USER); + strncpy(sp.database, conn->dbName, SM_DATABASE); + strncpy(sp.tty, conn->pgtty, SM_TTY); - conn->status = CONNECTION_AWAITING_RESPONSE; - return PGRES_POLLING_READING; - } + if (conn->pgoptions) + strncpy(sp.options, conn->pgoptions, SM_OPTIONS); - /* - * Handle the authentication exchange: wait for postmaster messages - * and respond as necessary. - */ - case CONNECTION_AWAITING_RESPONSE: - { - char beresp; - AuthRequest areq; + /* + * Send the startup packet. + * + * Theoretically, this could block, but it really shouldn't + * since we only got here if the socket is write-ready. + */ - /* Scan the message from current point (note that if we find - * the message is incomplete, we will return without advancing - * inStart, and resume here next time). - */ - conn->inCursor = conn->inStart; + if (pqPacketSend(conn, (char *) &sp, + sizeof(StartupPacket)) != STATUS_OK) + { + printfPQExpBuffer(&conn->errorMessage, + "PQconnectPoll() -- " + "couldn't send startup packet: " + "errno=%d\n%s\n", + errno, strerror(errno)); + goto error_return; + } - if (pqGetc(&beresp, conn)) - { - /* We'll come back when there are more data */ + conn->status = CONNECTION_AWAITING_RESPONSE; return PGRES_POLLING_READING; } - /* Handle errors. */ - if (beresp == 'E') + /* + * Handle the authentication exchange: wait for postmaster + * messages and respond as necessary. + */ + case CONNECTION_AWAITING_RESPONSE: { - if (pqGets(&conn->errorMessage, conn)) + char beresp; + AuthRequest areq; + + /* + * Scan the message from current point (note that if we + * find the message is incomplete, we will return without + * advancing inStart, and resume here next time). + */ + conn->inCursor = conn->inStart; + + if (pqGetc(&beresp, conn)) { /* We'll come back when there are more data */ return PGRES_POLLING_READING; } - /* OK, we read the message; mark data consumed */ - conn->inStart = conn->inCursor; - /* The postmaster typically won't end its message with a - * newline, so add one to conform to libpq conventions. - */ - appendPQExpBufferChar(&conn->errorMessage, '\n'); - goto error_return; - } - /* Otherwise it should be an authentication request. */ - if (beresp != 'R') - { - printfPQExpBuffer(&conn->errorMessage, - "PQconnectPoll() -- expected " - "authentication request\n"); - goto error_return; - } + /* Handle errors. */ + if (beresp == 'E') + { + if (pqGets(&conn->errorMessage, conn)) + { + /* We'll come back when there are more data */ + return PGRES_POLLING_READING; + } + /* OK, we read the message; mark data consumed */ + conn->inStart = conn->inCursor; + + /* + * The postmaster typically won't end its message with + * a newline, so add one to conform to libpq + * conventions. + */ + appendPQExpBufferChar(&conn->errorMessage, '\n'); + goto error_return; + } - /* Get the type of request. */ - if (pqGetInt((int *) &areq, 4, conn)) - { - /* We'll come back when there are more data */ - return PGRES_POLLING_READING; - } + /* Otherwise it should be an authentication request. */ + if (beresp != 'R') + { + printfPQExpBuffer(&conn->errorMessage, + "PQconnectPoll() -- expected " + "authentication request\n"); + goto error_return; + } - /* Get the password salt if there is one. */ - if (areq == AUTH_REQ_CRYPT) - { - if (pqGetnchar(conn->salt, sizeof(conn->salt), conn)) + /* Get the type of request. */ + if (pqGetInt((int *) &areq, 4, conn)) { /* We'll come back when there are more data */ return PGRES_POLLING_READING; } - } - /* OK, we successfully read the message; mark data consumed */ - conn->inStart = conn->inCursor; + /* Get the password salt if there is one. */ + if (areq == AUTH_REQ_CRYPT) + { + if (pqGetnchar(conn->salt, sizeof(conn->salt), conn)) + { + /* We'll come back when there are more data */ + return PGRES_POLLING_READING; + } + } + + /* + * OK, we successfully read the message; mark data + * consumed + */ + conn->inStart = conn->inCursor; - /* Respond to the request if necessary. */ - /* Note that conn->pghost must be non-NULL if we are going to - * avoid the Kerberos code doing a hostname look-up. */ - /* XXX fe-auth.c has not been fixed to support PQExpBuffers, so: */ - if (fe_sendauth(areq, conn, conn->pghost, conn->pgpass, - conn->errorMessage.data) != STATUS_OK) - { + /* Respond to the request if necessary. */ + + /* + * Note that conn->pghost must be non-NULL if we are going + * to avoid the Kerberos code doing a hostname look-up. + */ + + /* + * XXX fe-auth.c has not been fixed to support + * PQExpBuffers, so: + */ + if (fe_sendauth(areq, conn, conn->pghost, conn->pgpass, + conn->errorMessage.data) != STATUS_OK) + { + conn->errorMessage.len = strlen(conn->errorMessage.data); + goto error_return; + } conn->errorMessage.len = strlen(conn->errorMessage.data); - goto error_return; - } - conn->errorMessage.len = strlen(conn->errorMessage.data); - /* Just make sure that any data sent by fe_sendauth is flushed - * out. Although this theoretically could block, it really - * shouldn't since we don't send large auth responses. - */ - if (pqFlush(conn)) - goto error_return; + /* + * Just make sure that any data sent by fe_sendauth is + * flushed out. Although this theoretically could block, + * it really shouldn't since we don't send large auth + * responses. + */ + if (pqFlush(conn)) + goto error_return; - if (areq == AUTH_REQ_OK) - { - /* We are done with authentication exchange */ - conn->status = CONNECTION_AUTH_OK; - /* Set asyncStatus so that PQsetResult will think that what - * comes back next is the result of a query. See below. */ - conn->asyncStatus = PGASYNC_BUSY; - } + if (areq == AUTH_REQ_OK) + { + /* We are done with authentication exchange */ + conn->status = CONNECTION_AUTH_OK; + + /* + * Set asyncStatus so that PQsetResult will think that + * what comes back next is the result of a query. See + * below. + */ + conn->asyncStatus = PGASYNC_BUSY; + } - /* Look to see if we have more data yet. */ - goto keep_going; - } + /* Look to see if we have more data yet. */ + goto keep_going; + } case CONNECTION_AUTH_OK: - { - /* ---------- - * Now we expect to hear from the backend. A ReadyForQuery - * message indicates that startup is successful, but we might - * also get an Error message indicating failure. (Notice - * messages indicating nonfatal warnings are also allowed by - * the protocol, as is a BackendKeyData message.) Easiest way - * to handle this is to let PQgetResult() read the messages. We - * just have to fake it out about the state of the connection, - * by setting asyncStatus = PGASYNC_BUSY (done above). - *---------- - */ - - if (PQisBusy(conn)) - return PGRES_POLLING_READING; - - res = PQgetResult(conn); - /* NULL return indicating we have gone to - IDLE state is expected */ - if (res) { - if (res->resultStatus != PGRES_FATAL_ERROR) - printfPQExpBuffer(&conn->errorMessage, - "PQconnectPoll() -- unexpected message " - "during startup\n"); - /* if the resultStatus is FATAL, then conn->errorMessage - * already has a copy of the error; needn't copy it back. - * But add a newline if it's not there already, since - * postmaster error messages may not have one. + /* ---------- + * Now we expect to hear from the backend. A ReadyForQuery + * message indicates that startup is successful, but we might + * also get an Error message indicating failure. (Notice + * messages indicating nonfatal warnings are also allowed by + * the protocol, as is a BackendKeyData message.) Easiest way + * to handle this is to let PQgetResult() read the messages. We + * just have to fake it out about the state of the connection, + * by setting asyncStatus = PGASYNC_BUSY (done above). + *---------- */ - if (conn->errorMessage.len <= 0 || - conn->errorMessage.data[conn->errorMessage.len-1] != '\n') - appendPQExpBufferChar(&conn->errorMessage, '\n'); - PQclear(res); - goto error_return; - } - /* - * Post-connection housekeeping. Prepare to send environment - * variables to server. - */ - if (! PQsetenvStart(conn)) - goto error_return; + if (PQisBusy(conn)) + return PGRES_POLLING_READING; - conn->status = CONNECTION_SETENV; + res = PQgetResult(conn); - goto keep_going; - } + /* + * NULL return indicating we have gone to IDLE state is + * expected + */ + if (res) + { + if (res->resultStatus != PGRES_FATAL_ERROR) + printfPQExpBuffer(&conn->errorMessage, + "PQconnectPoll() -- unexpected message " + "during startup\n"); + + /* + * if the resultStatus is FATAL, then + * conn->errorMessage already has a copy of the error; + * needn't copy it back. But add a newline if it's not + * there already, since postmaster error messages may + * not have one. + */ + if (conn->errorMessage.len <= 0 || + conn->errorMessage.data[conn->errorMessage.len - 1] != '\n') + appendPQExpBufferChar(&conn->errorMessage, '\n'); + PQclear(res); + goto error_return; + } + + /* + * Post-connection housekeeping. Prepare to send + * environment variables to server. + */ + if (!PQsetenvStart(conn)) + goto error_return; + + conn->status = CONNECTION_SETENV; + + goto keep_going; + } case CONNECTION_SETENV: - /* We pretend that the connection is OK for the duration of - these queries. */ + + /* + * We pretend that the connection is OK for the duration of + * these queries. + */ conn->status = CONNECTION_OK; switch (PQsetenvPoll(conn)) { - case PGRES_POLLING_OK: /* Success */ + case PGRES_POLLING_OK: /* Success */ conn->status = CONNECTION_OK; return PGRES_POLLING_OK; - case PGRES_POLLING_READING: /* Still going */ + case PGRES_POLLING_READING: /* Still going */ conn->status = CONNECTION_SETENV; return PGRES_POLLING_READING; - case PGRES_POLLING_WRITING: /* Still going */ + case PGRES_POLLING_WRITING: /* Still going */ conn->status = CONNECTION_SETENV; return PGRES_POLLING_WRITING; @@ -1327,8 +1397,8 @@ PQconnectPoll(PGconn *conn) default: printfPQExpBuffer(&conn->errorMessage, - "PQconnectPoll() -- unknown connection state - " - "probably indicative of memory corruption!\n"); + "PQconnectPoll() -- unknown connection state - " + "probably indicative of memory corruption!\n"); goto error_return; } @@ -1368,7 +1438,7 @@ PQsetenvStart(PGconn *conn) #else conn->setenv_state = SETENV_STATE_OPTION_SEND; #endif - + conn->next_eo = EnvironmentOptions; return true; @@ -1386,8 +1456,10 @@ static PostgresPollingStatusType PQsetenvPoll(PGconn *conn) { PGresult *res; + #ifdef MULTIBYTE static const char envname[] = "PGCLIENTENCODING"; + #endif if (conn == NULL || conn->status == CONNECTION_BAD) @@ -1396,168 +1468,175 @@ PQsetenvPoll(PGconn *conn) /* Check whether there are any data for us */ switch (conn->setenv_state) { - /* These are reading states */ + /* These are reading states */ #ifdef MULTIBYTE case SETENV_STATE_ENCODINGS_WAIT: #endif case SETENV_STATE_OPTION_WAIT: - { - /* Load waiting data */ - int n = pqReadData(conn); - - if (n < 0) - goto error_return; - if (n == 0) - return PGRES_POLLING_READING; + { + /* Load waiting data */ + int n = pqReadData(conn); - break; - } + if (n < 0) + goto error_return; + if (n == 0) + return PGRES_POLLING_READING; - /* These are writing states, so we just proceed. */ + break; + } + + /* These are writing states, so we just proceed. */ #ifdef MULTIBYTE case SETENV_STATE_ENCODINGS_SEND: #endif case SETENV_STATE_OPTION_SEND: break; - /* Should we raise an error if called when not active? */ + /* Should we raise an error if called when not active? */ case SETENV_STATE_IDLE: return PGRES_POLLING_OK; default: printfPQExpBuffer(&conn->errorMessage, "PQsetenvPoll() -- unknown state - " - "probably indicative of memory corruption!\n"); + "probably indicative of memory corruption!\n"); goto error_return; } - keep_going: /* We will come back to here until there is nothing left to - parse. */ - switch(conn->setenv_state) +keep_going: /* We will come back to here until there + * is nothing left to parse. */ + switch (conn->setenv_state) { #ifdef MULTIBYTE case SETENV_STATE_ENCODINGS_SEND: - { - const char *env; - - /* query server encoding */ - env = getenv(envname); - if (!env || *env == '\0') { - if (!PQsendQuery(conn, - "select getdatabaseencoding()")) - goto error_return; + const char *env; - conn->setenv_state = SETENV_STATE_ENCODINGS_WAIT; - return PGRES_POLLING_READING; + /* query server encoding */ + env = getenv(envname); + if (!env || *env == '\0') + { + if (!PQsendQuery(conn, + "select getdatabaseencoding()")) + goto error_return; + + conn->setenv_state = SETENV_STATE_ENCODINGS_WAIT; + return PGRES_POLLING_READING; + } } - } case SETENV_STATE_ENCODINGS_WAIT: - { - if (PQisBusy(conn)) - return PGRES_POLLING_READING; - - res = PQgetResult(conn); - - if (res) { - char *encoding; + if (PQisBusy(conn)) + return PGRES_POLLING_READING; + + res = PQgetResult(conn); - if (PQresultStatus(res) != PGRES_TUPLES_OK) + if (res) { + char *encoding; + + if (PQresultStatus(res) != PGRES_TUPLES_OK) + { + PQclear(res); + goto error_return; + } + + /* set client encoding in pg_conn struct */ + encoding = PQgetvalue(res, 0, 0); + if (!encoding) /* this should not happen */ + conn->client_encoding = SQL_ASCII; + else + conn->client_encoding = pg_char_to_encoding(encoding); PQclear(res); - goto error_return; + + /* + * We have to keep going in order to clear up the + * query + */ + goto keep_going; } - /* set client encoding in pg_conn struct */ - encoding = PQgetvalue(res, 0, 0); - if (!encoding) /* this should not happen */ - conn->client_encoding = SQL_ASCII; - else - conn->client_encoding = pg_char_to_encoding(encoding); - PQclear(res); - /* We have to keep going in order to clear up the query */ + /* NULL result indicates that the query is finished */ + + /* Move on to setting the environment options */ + conn->setenv_state = SETENV_STATE_OPTION_SEND; goto keep_going; } - - /* NULL result indicates that the query is finished */ - - /* Move on to setting the environment options */ - conn->setenv_state = SETENV_STATE_OPTION_SEND; - goto keep_going; - } #endif case SETENV_STATE_OPTION_SEND: - { - /* Send an Environment Option */ - char setQuery[100]; /* note length limits in sprintf's below */ - - if (conn->next_eo->envName) { - const char *val; + /* Send an Environment Option */ + char setQuery[100]; /* note length limits in + * sprintf's below */ - if ((val = getenv(conn->next_eo->envName))) + if (conn->next_eo->envName) { - if (strcasecmp(val, "default") == 0) - sprintf(setQuery, "SET %s = %.60s", - conn->next_eo->pgName, val); - else - sprintf(setQuery, "SET %s = '%.60s'", - conn->next_eo->pgName, val); + const char *val; + + if ((val = getenv(conn->next_eo->envName))) + { + if (strcasecmp(val, "default") == 0) + sprintf(setQuery, "SET %s = %.60s", + conn->next_eo->pgName, val); + else + sprintf(setQuery, "SET %s = '%.60s'", + conn->next_eo->pgName, val); #ifdef CONNECTDEBUG - printf("Use environment variable %s to send %s\n", - conn->next_eo->envName, setQuery); + printf("Use environment variable %s to send %s\n", + conn->next_eo->envName, setQuery); #endif - if (!PQsendQuery(conn, setQuery)) - goto error_return; + if (!PQsendQuery(conn, setQuery)) + goto error_return; - conn->setenv_state = SETENV_STATE_OPTION_WAIT; + conn->setenv_state = SETENV_STATE_OPTION_WAIT; + } + else + conn->next_eo++; } else { - conn->next_eo++; + /* No more options to send, so we are done. */ + conn->setenv_state = SETENV_STATE_IDLE; } - } - else - { - /* No more options to send, so we are done. */ - conn->setenv_state = SETENV_STATE_IDLE; - } - goto keep_going; - } + goto keep_going; + } case SETENV_STATE_OPTION_WAIT: - { - if (PQisBusy(conn)) - return PGRES_POLLING_READING; - - res = PQgetResult(conn); - - if (res) { - if (PQresultStatus(res) != PGRES_COMMAND_OK) + if (PQisBusy(conn)) + return PGRES_POLLING_READING; + + res = PQgetResult(conn); + + if (res) { + if (PQresultStatus(res) != PGRES_COMMAND_OK) + { + PQclear(res); + goto error_return; + } + /* Don't need the result */ PQclear(res); - goto error_return; + + /* + * We have to keep going in order to clear up the + * query + */ + goto keep_going; } - /* Don't need the result */ - PQclear(res); - /* We have to keep going in order to clear up the query */ - goto keep_going; - } - /* NULL result indicates that the query is finished */ + /* NULL result indicates that the query is finished */ - /* Send the next option */ - conn->next_eo++; - conn->setenv_state = SETENV_STATE_OPTION_SEND; - goto keep_going; - } + /* Send the next option */ + conn->next_eo++; + conn->setenv_state = SETENV_STATE_OPTION_SEND; + goto keep_going; + } case SETENV_STATE_IDLE: return PGRES_POLLING_OK; @@ -1565,13 +1644,13 @@ PQsetenvPoll(PGconn *conn) default: printfPQExpBuffer(&conn->errorMessage, "PQsetenvPoll() -- unknown state - " - "probably indicative of memory corruption!\n"); + "probably indicative of memory corruption!\n"); goto error_return; } /* Unreachable */ - error_return: +error_return: conn->setenv_state = SETENV_STATE_IDLE; return PGRES_POLLING_FAILED; } @@ -1597,12 +1676,14 @@ PQsetenv(PGconn *conn) { PostgresPollingStatusType flag = PGRES_POLLING_WRITING; - if (! PQsetenvStart(conn)) + if (!PQsetenvStart(conn)) return false; - for (;;) { + for (;;) + { + /* - * Wait, if necessary. Note that the initial state (just after + * Wait, if necessary. Note that the initial state (just after * PQsetenvStart) is to wait for the socket to select for writing. */ switch (flag) @@ -1612,7 +1693,7 @@ PQsetenv(PGconn *conn) case PGRES_POLLING_OK: return true; /* success! */ - + case PGRES_POLLING_READING: if (pqWait(1, 0, conn)) { @@ -1634,6 +1715,7 @@ PQsetenv(PGconn *conn) conn->status = CONNECTION_BAD; return false; } + /* * Now try to advance the state machine. */ @@ -1641,7 +1723,7 @@ PQsetenv(PGconn *conn) } } -#endif /* NOT_USED */ +#endif /* NOT_USED */ /* @@ -1670,15 +1752,15 @@ makeEmptyPGconn(void) #endif /* - * The output buffer size is set to 8K, which is the usual size of pipe - * buffers on Unix systems. That way, when we are sending a large - * amount of data, we avoid incurring extra kernel context swaps for - * partial bufferloads. Note that we currently don't ever enlarge + * The output buffer size is set to 8K, which is the usual size of + * pipe buffers on Unix systems. That way, when we are sending a + * large amount of data, we avoid incurring extra kernel context swaps + * for partial bufferloads. Note that we currently don't ever enlarge * the output buffer. * * With the same goal of minimizing context swaps, the input buffer will - * be enlarged anytime it has less than 8K free, so we initially allocate - * twice that. + * be enlarged anytime it has less than 8K free, so we initially + * allocate twice that. */ conn->inBufSize = 16 * 1024; conn->inBuffer = (char *) malloc(conn->inBufSize); @@ -1712,7 +1794,7 @@ freePGconn(PGconn *conn) pqClearAsyncResult(conn); /* deallocate result and curTuple */ #ifdef USE_SSL if (conn->ssl) - SSL_free(conn->ssl); + SSL_free(conn->ssl); #endif if (conn->sock >= 0) #ifdef WIN32 @@ -1770,7 +1852,7 @@ closePGconn(PGconn *conn) (void) pqFlush(conn); } - /* + /* * must reset the blocking status so a possible reconnect will work * don't call PQsetnonblocking() because it will fail if it's unable * to flush the connection. @@ -1861,9 +1943,7 @@ PostgresPollingStatusType PQresetPoll(PGconn *conn) { if (conn) - { return PQconnectPoll(conn); - } return PGRES_POLLING_FAILED; } @@ -2028,7 +2108,7 @@ conninfo_parse(const char *conninfo, PQExpBuffer errorMessage) if (options == NULL) { printfPQExpBuffer(errorMessage, - "FATAL: cannot allocate memory for copy of PQconninfoOptions\n"); + "FATAL: cannot allocate memory for copy of PQconninfoOptions\n"); return NULL; } memcpy(options, PQconninfoOptions, sizeof(PQconninfoOptions)); @@ -2076,7 +2156,7 @@ conninfo_parse(const char *conninfo, PQExpBuffer errorMessage) if (*cp != '=') { printfPQExpBuffer(errorMessage, - "ERROR: Missing '=' after '%s' in conninfo\n", + "ERROR: Missing '=' after '%s' in conninfo\n", pname); PQconninfoFree(options); free(buf); @@ -2125,7 +2205,7 @@ conninfo_parse(const char *conninfo, PQExpBuffer errorMessage) if (*cp == '\0') { printfPQExpBuffer(errorMessage, - "ERROR: PQconnectdb() - unterminated quoted string in conninfo\n"); + "ERROR: PQconnectdb() - unterminated quoted string in conninfo\n"); PQconninfoFree(options); free(buf); return NULL; @@ -2375,10 +2455,10 @@ PQclientEncoding(const PGconn *conn) int PQsetClientEncoding(PGconn *conn, const char *encoding) { - char qbuf[128]; + char qbuf[128]; static char query[] = "set client_encoding to '%s'"; - PGresult *res; - int status; + PGresult *res; + int status; if (!conn || conn->status != CONNECTION_OK) return -1; @@ -2394,7 +2474,7 @@ PQsetClientEncoding(PGconn *conn, const char *encoding) sprintf(qbuf, query, encoding); res = PQexec(conn, qbuf); - if (res == (PGresult *)NULL) + if (res == (PGresult *) NULL) return -1; if (res->resultStatus != PGRES_COMMAND_OK) status = -1; @@ -2402,17 +2482,19 @@ PQsetClientEncoding(PGconn *conn, const char *encoding) { /* change libpq internal encoding */ conn->client_encoding = pg_char_to_encoding(encoding); - status = 0; /* everything is ok */ + status = 0; /* everything is ok */ } PQclear(res); - return(status); + return (status); } + #else int PQsetClientEncoding(PGconn *conn, const char *encoding) { return -1; } + #endif void @@ -2465,7 +2547,7 @@ PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg) static void defaultNoticeProcessor(void *arg, const char *message) { - (void)arg; /*not used*/ + (void) arg; /* not used */ /* Note: we expect the supplied string to end with a newline already. */ fprintf(stderr, "%s", message); } diff --git a/src/interfaces/libpq/fe-exec.c b/src/interfaces/libpq/fe-exec.c index 6b3999193d5..6617bb2bc30 100644 --- a/src/interfaces/libpq/fe-exec.c +++ b/src/interfaces/libpq/fe-exec.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.93 2000/03/14 23:59:23 tgl Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.94 2000/04/12 17:17:14 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -27,7 +27,7 @@ #endif /* keep this in same order as ExecStatusType in libpq-fe.h */ -char * const pgresStatus[] = { +char *const pgresStatus[] = { "PGRES_EMPTY_QUERY", "PGRES_COMMAND_OK", "PGRES_TUPLES_OK", @@ -47,7 +47,7 @@ char * const pgresStatus[] = { static void pqCatenateResultError(PGresult *res, const char *msg); static void saveErrorResult(PGconn *conn); static PGresult *prepareAsyncResult(PGconn *conn); -static int addTuple(PGresult *res, PGresAttValue *tup); +static int addTuple(PGresult *res, PGresAttValue * tup); static void parseInput(PGconn *conn); static void handleSendFailure(PGconn *conn); static int getRowDescriptions(PGconn *conn); @@ -178,7 +178,7 @@ PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status) /* defaults... */ result->noticeHook = NULL; result->noticeArg = NULL; - result->client_encoding = 0; /* should be SQL_ASCII */ + result->client_encoding = 0; /* should be SQL_ASCII */ } return result; @@ -324,7 +324,7 @@ pqSetResultError(PGresult *res, const char *msg) static void pqCatenateResultError(PGresult *res, const char *msg) { - PQExpBufferData errorBuf; + PQExpBufferData errorBuf; if (!res || !msg) return; @@ -391,7 +391,9 @@ pqClearAsyncResult(PGconn *conn) static void saveErrorResult(PGconn *conn) { - /* If no old async result, just let PQmakeEmptyPGresult make one. + + /* + * If no old async result, just let PQmakeEmptyPGresult make one. * Likewise if old result is not an error message. */ if (conn->result == NULL || @@ -421,9 +423,9 @@ prepareAsyncResult(PGconn *conn) PGresult *res; /* - * conn->result is the PGresult to return. If it is NULL - * (which probably shouldn't happen) we assume there is an - * appropriate error message in conn->errorMessage. + * conn->result is the PGresult to return. If it is NULL (which + * probably shouldn't happen) we assume there is an appropriate error + * message in conn->errorMessage. */ res = conn->result; conn->result = NULL; /* handing over ownership to caller */ @@ -432,9 +434,10 @@ prepareAsyncResult(PGconn *conn) res = PQmakeEmptyPGresult(conn, PGRES_FATAL_ERROR); else { + /* - * Make sure PQerrorMessage agrees with result; it could - * be different if we have concatenated messages. + * Make sure PQerrorMessage agrees with result; it could be + * different if we have concatenated messages. */ resetPQExpBuffer(&conn->errorMessage); appendPQExpBufferStr(&conn->errorMessage, @@ -449,7 +452,7 @@ prepareAsyncResult(PGconn *conn) * Returns TRUE if OK, FALSE if not enough memory to add the row */ static int -addTuple(PGresult *res, PGresAttValue *tup) +addTuple(PGresult *res, PGresAttValue * tup) { if (res->ntups >= res->tupArrSize) { @@ -521,7 +524,7 @@ PQsendQuery(PGconn *conn, const char *query) if (conn->asyncStatus != PGASYNC_IDLE) { printfPQExpBuffer(&conn->errorMessage, - "PQsendQuery() -- another query already in progress.\n"); + "PQsendQuery() -- another query already in progress.\n"); return 0; } @@ -532,21 +535,21 @@ PQsendQuery(PGconn *conn, const char *query) /* send the query to the backend; */ /* - * in order to guarantee that we don't send a partial query - * where we would become out of sync with the backend and/or - * block during a non-blocking connection we must first flush - * the send buffer before sending more data + * in order to guarantee that we don't send a partial query where we + * would become out of sync with the backend and/or block during a + * non-blocking connection we must first flush the send buffer before + * sending more data * - * an alternative is to implement 'queue reservations' where - * we are able to roll up a transaction - * (the 'Q' along with our query) and make sure we have - * enough space for it all in the send buffer. + * an alternative is to implement 'queue reservations' where we are able + * to roll up a transaction (the 'Q' along with our query) and make + * sure we have enough space for it all in the send buffer. */ if (pqIsnonblocking(conn)) { + /* - * the buffer must have emptied completely before we allow - * a new query to be buffered + * the buffer must have emptied completely before we allow a new + * query to be buffered */ if (pqFlush(conn)) return 0; @@ -555,20 +558,21 @@ PQsendQuery(PGconn *conn, const char *query) if (pqPutnchar("Q", 1, conn) || pqPuts(query, conn)) { - handleSendFailure(conn); + handleSendFailure(conn); return 0; } + /* - * give the data a push, ignore the return value as - * ConsumeInput() will do any aditional flushing if needed + * give the data a push, ignore the return value as ConsumeInput() + * will do any aditional flushing if needed */ - (void) pqFlush(conn); + (void) pqFlush(conn); } else { - /* - * the frontend-backend protocol uses 'Q' to - * designate queries + + /* + * the frontend-backend protocol uses 'Q' to designate queries */ if (pqPutnchar("Q", 1, conn) || pqPuts(query, conn) || @@ -596,16 +600,17 @@ PQsendQuery(PGconn *conn, const char *query) static void handleSendFailure(PGconn *conn) { + /* * Accept any available input data, ignoring errors. Note that if * pqReadData decides the backend has closed the channel, it will * close our side of the socket --- that's just what we want here. */ while (pqReadData(conn) > 0) - /* loop until no more data readable */ ; + /* loop until no more data readable */ ; /* - * Parse any available input messages. Since we are in PGASYNC_IDLE + * Parse any available input messages. Since we are in PGASYNC_IDLE * state, only NOTICE and NOTIFY messages will be eaten. */ parseInput(conn); @@ -631,11 +636,11 @@ PQconsumeInput(PGconn *conn) */ if (pqReadData(conn) < 0) { + /* - * for non-blocking connections - * try to flush the send-queue otherwise we may never get a - * responce for something that may not have already been sent - * because it's in our write buffer! + * for non-blocking connections try to flush the send-queue + * otherwise we may never get a responce for something that may + * not have already been sent because it's in our write buffer! */ if (pqIsnonblocking(conn)) (void) pqFlush(conn); @@ -686,11 +691,11 @@ parseInput(PGconn *conn) * OUT; always process them right away. * * Most other messages should only be processed while in BUSY state. - * (In particular, in READY state we hold off further parsing until - * the application collects the current PGresult.) + * (In particular, in READY state we hold off further parsing + * until the application collects the current PGresult.) * - * However, if the state is IDLE then we got trouble; we need to - * deal with the unexpected message somehow. + * However, if the state is IDLE then we got trouble; we need to deal + * with the unexpected message somehow. */ if (id == 'A') { @@ -707,6 +712,7 @@ parseInput(PGconn *conn) /* If not IDLE state, just wait ... */ if (conn->asyncStatus != PGASYNC_IDLE) return; + /* * Unexpected message in IDLE state; need to recover somehow. * ERROR messages are displayed using the notice processor; @@ -723,7 +729,7 @@ parseInput(PGconn *conn) else { sprintf(noticeWorkspace, - "Backend message type 0x%02x arrived while idle\n", + "Backend message type 0x%02x arrived while idle\n", id); DONOTICE(conn, noticeWorkspace); /* Discard the unexpected message; good idea?? */ @@ -733,6 +739,7 @@ parseInput(PGconn *conn) } else { + /* * In BUSY state, we can process everything. */ @@ -743,13 +750,13 @@ parseInput(PGconn *conn) return; if (conn->result == NULL) conn->result = PQmakeEmptyPGresult(conn, - PGRES_COMMAND_OK); + PGRES_COMMAND_OK); strncpy(conn->result->cmdStatus, conn->workBuffer.data, CMDSTATUS_LEN); conn->asyncStatus = PGASYNC_READY; break; case 'E': /* error return */ - if (pqGets(& conn->errorMessage, conn)) + if (pqGets(&conn->errorMessage, conn)) return; /* build an error result holding the error message */ saveErrorResult(conn); @@ -823,7 +830,7 @@ parseInput(PGconn *conn) else { sprintf(noticeWorkspace, - "Backend sent D message without prior T\n"); + "Backend sent D message without prior T\n"); DONOTICE(conn, noticeWorkspace); /* Discard the unexpected message; good idea?? */ conn->inStart = conn->inEnd; @@ -840,7 +847,7 @@ parseInput(PGconn *conn) else { sprintf(noticeWorkspace, - "Backend sent B message without prior T\n"); + "Backend sent B message without prior T\n"); DONOTICE(conn, noticeWorkspace); /* Discard the unexpected message; good idea?? */ conn->inStart = conn->inEnd; @@ -855,10 +862,10 @@ parseInput(PGconn *conn) break; default: printfPQExpBuffer(&conn->errorMessage, - "Unknown protocol character '%c' read from backend. " - "(The protocol character is the first character the " - "backend sends in response to a query it receives).\n", - id); + "Unknown protocol character '%c' read from backend. " + "(The protocol character is the first character the " + "backend sends in response to a query it receives).\n", + id); /* build an error result holding the error message */ saveErrorResult(conn); /* Discard the unexpected message; good idea?? */ @@ -963,6 +970,7 @@ getAnotherTuple(PGconn *conn, int binary) PGresult *result = conn->result; int nfields = result->numAttributes; PGresAttValue *tup; + /* the backend sends us a bitmap of which attributes are null */ char std_bitmap[64]; /* used unless it doesn't fit */ char *bitmap = std_bitmap; @@ -1055,7 +1063,9 @@ getAnotherTuple(PGconn *conn, int binary) outOfMemory: /* Replace partially constructed result with an error result */ - /* we do NOT use saveErrorResult() here, because of the likelihood + + /* + * we do NOT use saveErrorResult() here, because of the likelihood * that there's not enough memory to concatenate messages... */ pqClearAsyncResult(conn); @@ -1116,8 +1126,10 @@ PQgetResult(PGconn *conn) if (pqWait(TRUE, FALSE, conn) || pqReadData(conn) < 0) { - /* conn->errorMessage has been set by pqWait or pqReadData. - * We want to append it to any already-received error message. + + /* + * conn->errorMessage has been set by pqWait or pqReadData. We + * want to append it to any already-received error message. */ saveErrorResult(conn); conn->asyncStatus = PGASYNC_IDLE; @@ -1173,12 +1185,12 @@ PQexec(PGconn *conn, const char *query) { PGresult *result; PGresult *lastResult; - bool savedblocking; + bool savedblocking; /* - * we assume anyone calling PQexec wants blocking behaviour, - * we force the blocking status of the connection to blocking - * for the duration of this function and restore it on return + * we assume anyone calling PQexec wants blocking behaviour, we force + * the blocking status of the connection to blocking for the duration + * of this function and restore it on return */ savedblocking = pqIsnonblocking(conn); if (PQsetnonblocking(conn, FALSE) == -1) @@ -1205,15 +1217,15 @@ PQexec(PGconn *conn, const char *query) /* OK to send the message */ if (!PQsendQuery(conn, query)) - goto errout; /* restore blocking status */ + goto errout; /* restore blocking status */ /* * For backwards compatibility, return the last result if there are * more than one --- but merge error messages if we get more than one * error result. * - * We have to stop if we see copy in/out, however. - * We will resume parsing when application calls PQendcopy. + * We have to stop if we see copy in/out, however. We will resume parsing + * when application calls PQendcopy. */ lastResult = NULL; while ((result = PQgetResult(conn)) != NULL) @@ -1260,11 +1272,13 @@ errout: static int getNotice(PGconn *conn) { - /* Since the Notice might be pretty long, we create a temporary - * PQExpBuffer rather than using conn->workBuffer. workBuffer is + + /* + * Since the Notice might be pretty long, we create a temporary + * PQExpBuffer rather than using conn->workBuffer. workBuffer is * intended for stuff that is expected to be short. */ - PQExpBufferData noticeBuf; + PQExpBufferData noticeBuf; initPQExpBuffer(¬iceBuf); if (pqGets(¬iceBuf, conn)) @@ -1532,13 +1546,13 @@ PQendcopy(PGconn *conn) conn->asyncStatus != PGASYNC_COPY_OUT) { printfPQExpBuffer(&conn->errorMessage, - "PQendcopy() -- I don't think there's a copy in progress.\n"); + "PQendcopy() -- I don't think there's a copy in progress.\n"); return 1; } /* - * make sure no data is waiting to be sent, - * abort if we are non-blocking and the flush fails + * make sure no data is waiting to be sent, abort if we are + * non-blocking and the flush fails */ if (pqFlush(conn) && pqIsnonblocking(conn)) return (1); @@ -1632,7 +1646,7 @@ PQfn(PGconn *conn, return NULL; } - if (pqPuts("F ", conn) || /* function */ + if (pqPuts("F ", conn) || /* function */ pqPutInt(fnid, 4, conn) || /* function id */ pqPutInt(nargs, 4, conn)) /* # of args */ { @@ -1730,7 +1744,7 @@ PQfn(PGconn *conn, { /* The backend violates the protocol. */ printfPQExpBuffer(&conn->errorMessage, - "FATAL: PQfn: protocol error: id=0x%x\n", + "FATAL: PQfn: protocol error: id=0x%x\n", id); saveErrorResult(conn); conn->inStart = conn->inCursor; @@ -1764,7 +1778,7 @@ PQfn(PGconn *conn, default: /* The backend violates the protocol. */ printfPQExpBuffer(&conn->errorMessage, - "FATAL: PQfn: protocol error: id=0x%x\n", + "FATAL: PQfn: protocol error: id=0x%x\n", id); saveErrorResult(conn); conn->inStart = conn->inCursor; @@ -1775,9 +1789,10 @@ PQfn(PGconn *conn, needInput = false; } - /* We fall out of the loop only upon failing to read data. - * conn->errorMessage has been set by pqWait or pqReadData. - * We want to append it to any already-received error message. + /* + * We fall out of the loop only upon failing to read data. + * conn->errorMessage has been set by pqWait or pqReadData. We want to + * append it to any already-received error message. */ saveErrorResult(conn); return prepareAsyncResult(conn); @@ -1842,7 +1857,7 @@ PQbinaryTuples(const PGresult *res) static int check_field_number(const char *routineName, const PGresult *res, int field_num) { - char noticeBuf[128]; + char noticeBuf[128]; if (!res) return FALSE; /* no way to display error message... */ @@ -1864,7 +1879,7 @@ static int check_tuple_field_number(const char *routineName, const PGresult *res, int tup_num, int field_num) { - char noticeBuf[128]; + char noticeBuf[128]; if (!res) return FALSE; /* no way to display error message... */ @@ -1997,13 +2012,14 @@ PQcmdStatus(PGresult *res) char * PQoidStatus(const PGresult *res) { - /* - * This must be enough to hold the result. Don't laugh, this is - * better than what this function used to do. + + /* + * This must be enough to hold the result. Don't laugh, this is better + * than what this function used to do. */ static char buf[24]; - size_t len; + size_t len; if (!res || !res->cmdStatus || strncmp(res->cmdStatus, "INSERT ", 7) != 0) return ""; @@ -2019,25 +2035,25 @@ PQoidStatus(const PGresult *res) /* PQoidValue - - a perhaps preferable form of the above which just returns + a perhaps preferable form of the above which just returns an Oid type */ Oid PQoidValue(const PGresult *res) { - char * endptr = NULL; - long int result; + char *endptr = NULL; + long int result; - if (!res || !res->cmdStatus || strncmp(res->cmdStatus, "INSERT ", 7) != 0) - return InvalidOid; + if (!res || !res->cmdStatus || strncmp(res->cmdStatus, "INSERT ", 7) != 0) + return InvalidOid; - errno = 0; - result = strtoul(res->cmdStatus + 7, &endptr, 10); + errno = 0; + result = strtoul(res->cmdStatus + 7, &endptr, 10); - if (!endptr || (*endptr != ' ' && *endptr != '\0') || errno == ERANGE) - return InvalidOid; - else - return (Oid)result; + if (!endptr || (*endptr != ' ' && *endptr != '\0') || errno == ERANGE) + return InvalidOid; + else + return (Oid) result; } /* @@ -2048,7 +2064,7 @@ PQoidValue(const PGresult *res) char * PQcmdTuples(PGresult *res) { - char noticeBuf[128]; + char noticeBuf[128]; if (!res) return ""; @@ -2160,9 +2176,9 @@ PQsetnonblocking(PGconn *conn, int arg) /* * to guarantee constancy for flushing/query/result-polling behavior * we need to flush the send queue at this point in order to guarantee - * proper behavior. - * this is ok because either they are making a transition - * _from_ or _to_ blocking mode, either way we can block them. + * proper behavior. this is ok because either they are making a + * transition _from_ or _to_ blocking mode, either way we can block + * them. */ /* if we are going from blocking to non-blocking flush here */ if (pqFlush(conn)) diff --git a/src/interfaces/libpq/fe-lobj.c b/src/interfaces/libpq/fe-lobj.c index 6a7fd71f440..23b9ffb09c0 100644 --- a/src/interfaces/libpq/fe-lobj.c +++ b/src/interfaces/libpq/fe-lobj.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-lobj.c,v 1.28 2000/02/07 23:10:11 petere Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-lobj.c,v 1.29 2000/04/12 17:17:15 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -523,7 +523,7 @@ lo_initialize(PGconn *conn) PGresult *res; PGlobjfuncs *lobjfuncs; int n; - const char *fname; + const char *fname; Oid foid; /* ---------------- @@ -563,7 +563,7 @@ lo_initialize(PGconn *conn) free(lobjfuncs); PQclear(res); printfPQExpBuffer(&conn->errorMessage, - "ERROR: SELECT didn't return data in lo_initialize()\n"); + "ERROR: SELECT didn't return data in lo_initialize()\n"); return -1; } @@ -603,56 +603,56 @@ lo_initialize(PGconn *conn) if (lobjfuncs->fn_lo_open == 0) { printfPQExpBuffer(&conn->errorMessage, - "ERROR: Cannot determine OID for function lo_open\n"); + "ERROR: Cannot determine OID for function lo_open\n"); free(lobjfuncs); return -1; } if (lobjfuncs->fn_lo_close == 0) { printfPQExpBuffer(&conn->errorMessage, - "ERROR: Cannot determine OID for function lo_close\n"); + "ERROR: Cannot determine OID for function lo_close\n"); free(lobjfuncs); return -1; } if (lobjfuncs->fn_lo_creat == 0) { printfPQExpBuffer(&conn->errorMessage, - "ERROR: Cannot determine OID for function lo_creat\n"); + "ERROR: Cannot determine OID for function lo_creat\n"); free(lobjfuncs); return -1; } if (lobjfuncs->fn_lo_unlink == 0) { printfPQExpBuffer(&conn->errorMessage, - "ERROR: Cannot determine OID for function lo_unlink\n"); + "ERROR: Cannot determine OID for function lo_unlink\n"); free(lobjfuncs); return -1; } if (lobjfuncs->fn_lo_lseek == 0) { printfPQExpBuffer(&conn->errorMessage, - "ERROR: Cannot determine OID for function lo_lseek\n"); + "ERROR: Cannot determine OID for function lo_lseek\n"); free(lobjfuncs); return -1; } if (lobjfuncs->fn_lo_tell == 0) { printfPQExpBuffer(&conn->errorMessage, - "ERROR: Cannot determine OID for function lo_tell\n"); + "ERROR: Cannot determine OID for function lo_tell\n"); free(lobjfuncs); return -1; } if (lobjfuncs->fn_lo_read == 0) { printfPQExpBuffer(&conn->errorMessage, - "ERROR: Cannot determine OID for function loread\n"); + "ERROR: Cannot determine OID for function loread\n"); free(lobjfuncs); return -1; } if (lobjfuncs->fn_lo_write == 0) { printfPQExpBuffer(&conn->errorMessage, - "ERROR: Cannot determine OID for function lowrite\n"); + "ERROR: Cannot determine OID for function lowrite\n"); free(lobjfuncs); return -1; } diff --git a/src/interfaces/libpq/fe-misc.c b/src/interfaces/libpq/fe-misc.c index e6dd26d8fbb..d7f1b6e1549 100644 --- a/src/interfaces/libpq/fe-misc.c +++ b/src/interfaces/libpq/fe-misc.c @@ -25,7 +25,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.40 2000/04/11 19:00:31 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.41 2000/04/12 17:17:15 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -91,40 +91,41 @@ pqGetc(char *result, PGconn *conn) static int pqPutBytes(const char *s, size_t nbytes, PGconn *conn) { - size_t avail = Max(conn->outBufSize - conn->outCount, 0); + size_t avail = Max(conn->outBufSize - conn->outCount, 0); /* - * if we are non-blocking and the send queue is too full to buffer this - * request then try to flush some and return an error + * if we are non-blocking and the send queue is too full to buffer + * this request then try to flush some and return an error */ if (pqIsnonblocking(conn) && nbytes > avail && pqFlush(conn)) { - /* - * even if the flush failed we may still have written some - * data, recalculate the size of the send-queue relative - * to the amount we have to send, we may be able to queue it - * afterall even though it's not sent to the database it's - * ok, any routines that check the data coming from the - * database better call pqFlush() anyway. + + /* + * even if the flush failed we may still have written some data, + * recalculate the size of the send-queue relative to the amount + * we have to send, we may be able to queue it afterall even + * though it's not sent to the database it's ok, any routines that + * check the data coming from the database better call pqFlush() + * anyway. */ if (nbytes > Max(conn->outBufSize - conn->outCount, 0)) { printfPQExpBuffer(&conn->errorMessage, - "pqPutBytes -- pqFlush couldn't flush enough" - " data: space available: %d, space needed %d\n", - Max(conn->outBufSize - conn->outCount, 0), nbytes); + "pqPutBytes -- pqFlush couldn't flush enough" + " data: space available: %d, space needed %d\n", + Max(conn->outBufSize - conn->outCount, 0), nbytes); return EOF; } /* fixup avail for while loop */ avail = Max(conn->outBufSize - conn->outCount, 0); } - /* + /* * is the amount of data to be sent is larger than the size of the * output buffer then we must flush it to make more room. * - * the code above will make sure the loop conditional is never - * true for non-blocking connections + * the code above will make sure the loop conditional is never true for + * non-blocking connections */ while (nbytes > avail) { @@ -208,7 +209,7 @@ pqGetnchar(char *s, size_t len, PGconn *conn) conn->inCursor += len; if (conn->Pfdebug) - fprintf(conn->Pfdebug, "From backend (%d)> %.*s\n", len, (int)len, s); + fprintf(conn->Pfdebug, "From backend (%d)> %.*s\n", len, (int) len, s); return 0; } @@ -224,7 +225,7 @@ pqPutnchar(const char *s, size_t len, PGconn *conn) return EOF; if (conn->Pfdebug) - fprintf(conn->Pfdebug, "To backend> %.*s\n", (int)len, s); + fprintf(conn->Pfdebug, "To backend> %.*s\n", (int) len, s); return 0; } @@ -324,7 +325,7 @@ pqReadReady(PGconn *conn) FD_SET(conn->sock, &input_mask); timeout.tv_sec = 0; timeout.tv_usec = 0; - retry: +retry: if (select(conn->sock + 1, &input_mask, (fd_set *) NULL, (fd_set *) NULL, &timeout) < 0) { @@ -333,7 +334,7 @@ pqReadReady(PGconn *conn) goto retry; printfPQExpBuffer(&conn->errorMessage, - "pqReadReady() -- select() failed: errno=%d\n%s\n", + "pqReadReady() -- select() failed: errno=%d\n%s\n", errno, strerror(errno)); return -1; } @@ -358,7 +359,7 @@ pqWriteReady(PGconn *conn) FD_SET(conn->sock, &input_mask); timeout.tv_sec = 0; timeout.tv_usec = 0; - retry: +retry: if (select(conn->sock + 1, (fd_set *) NULL, &input_mask, (fd_set *) NULL, &timeout) < 0) { @@ -367,7 +368,7 @@ pqWriteReady(PGconn *conn) goto retry; printfPQExpBuffer(&conn->errorMessage, - "pqWriteReady() -- select() failed: errno=%d\n%s\n", + "pqWriteReady() -- select() failed: errno=%d\n%s\n", errno, strerror(errno)); return -1; } @@ -415,7 +416,7 @@ pqReadData(PGconn *conn) * buffer size. We enlarge before filling the buffer entirely so as * to avoid asking the kernel for a partial packet. The magic constant * here should be large enough for a TCP packet or Unix pipe - * bufferload. 8K is the usual pipe buffer size, so... + * bufferload. 8K is the usual pipe buffer size, so... */ if (conn->inBufSize - conn->inEnd < 8192) { @@ -432,13 +433,13 @@ pqReadData(PGconn *conn) /* OK, try to read some data */ tryAgain: #ifdef USE_SSL - if (conn->ssl) - nread = SSL_read(conn->ssl, conn->inBuffer + conn->inEnd, - conn->inBufSize - conn->inEnd); + if (conn->ssl) + nread = SSL_read(conn->ssl, conn->inBuffer + conn->inEnd, + conn->inBufSize - conn->inEnd); else #endif - nread = recv(conn->sock, conn->inBuffer + conn->inEnd, - conn->inBufSize - conn->inEnd, 0); + nread = recv(conn->sock, conn->inBuffer + conn->inEnd, + conn->inBufSize - conn->inEnd, 0); if (nread < 0) { if (errno == EINTR) @@ -458,24 +459,26 @@ tryAgain: goto definitelyFailed; #endif printfPQExpBuffer(&conn->errorMessage, - "pqReadData() -- read() failed: errno=%d\n%s\n", + "pqReadData() -- read() failed: errno=%d\n%s\n", errno, strerror(errno)); return -1; } if (nread > 0) { conn->inEnd += nread; + /* * Hack to deal with the fact that some kernels will only give us - * back 1 packet per recv() call, even if we asked for more and there - * is more available. If it looks like we are reading a long message, - * loop back to recv() again immediately, until we run out of data - * or buffer space. Without this, the block-and-restart behavior of - * libpq's higher levels leads to O(N^2) performance on long messages. + * back 1 packet per recv() call, even if we asked for more and + * there is more available. If it looks like we are reading a + * long message, loop back to recv() again immediately, until we + * run out of data or buffer space. Without this, the + * block-and-restart behavior of libpq's higher levels leads to + * O(N^2) performance on long messages. * * Since we left-justified the data above, conn->inEnd gives the - * amount of data already read in the current message. We consider - * the message "long" once we have acquired 32k ... + * amount of data already read in the current message. We + * consider the message "long" once we have acquired 32k ... */ if (conn->inEnd > 32768 && (conn->inBufSize - conn->inEnd) >= 8192) @@ -516,13 +519,13 @@ tryAgain: */ tryAgain2: #ifdef USE_SSL - if (conn->ssl) - nread = SSL_read(conn->ssl, conn->inBuffer + conn->inEnd, - conn->inBufSize - conn->inEnd); + if (conn->ssl) + nread = SSL_read(conn->ssl, conn->inBuffer + conn->inEnd, + conn->inBufSize - conn->inEnd); else #endif - nread = recv(conn->sock, conn->inBuffer + conn->inEnd, - conn->inBufSize - conn->inEnd, 0); + nread = recv(conn->sock, conn->inBuffer + conn->inEnd, + conn->inBufSize - conn->inEnd, 0); if (nread < 0) { if (errno == EINTR) @@ -542,7 +545,7 @@ tryAgain2: goto definitelyFailed; #endif printfPQExpBuffer(&conn->errorMessage, - "pqReadData() -- read() failed: errno=%d\n%s\n", + "pqReadData() -- read() failed: errno=%d\n%s\n", errno, strerror(errno)); return -1; } @@ -558,9 +561,9 @@ tryAgain2: */ definitelyFailed: printfPQExpBuffer(&conn->errorMessage, - "pqReadData() -- backend closed the channel unexpectedly.\n" - "\tThis probably means the backend terminated abnormally\n" - "\tbefore or while processing the request.\n"); + "pqReadData() -- backend closed the channel unexpectedly.\n" + "\tThis probably means the backend terminated abnormally\n" + "\tbefore or while processing the request.\n"); conn->status = CONNECTION_BAD; /* No more connection to backend */ #ifdef WIN32 closesocket(conn->sock); @@ -588,9 +591,9 @@ pqFlush(PGconn *conn) return EOF; } - /* - * don't try to send zero data, allows us to use this function - * without too much worry about overhead + /* + * don't try to send zero data, allows us to use this function without + * too much worry about overhead */ if (len == 0) return (0); @@ -601,16 +604,17 @@ pqFlush(PGconn *conn) /* Prevent being SIGPIPEd if backend has closed the connection. */ #ifndef WIN32 pqsigfunc oldsighandler = pqsignal(SIGPIPE, SIG_IGN); + #endif - int sent; + int sent; #ifdef USE_SSL - if (conn->ssl) - sent = SSL_write(conn->ssl, ptr, len); + if (conn->ssl) + sent = SSL_write(conn->ssl, ptr, len); else #endif - sent = send(conn->sock, ptr, len, 0); + sent = send(conn->sock, ptr, len, 0); #ifndef WIN32 pqsignal(SIGPIPE, oldsighandler); @@ -642,22 +646,24 @@ pqFlush(PGconn *conn) case ECONNRESET: #endif printfPQExpBuffer(&conn->errorMessage, - "pqFlush() -- backend closed the channel unexpectedly.\n" - "\tThis probably means the backend terminated abnormally" - " before or while processing the request.\n"); + "pqFlush() -- backend closed the channel unexpectedly.\n" + "\tThis probably means the backend terminated abnormally" + " before or while processing the request.\n"); + /* * We used to close the socket here, but that's a bad * idea since there might be unread data waiting - * (typically, a NOTICE message from the backend telling - * us it's committing hara-kiri...). Leave the socket - * open until pqReadData finds no more data can be read. + * (typically, a NOTICE message from the backend + * telling us it's committing hara-kiri...). Leave + * the socket open until pqReadData finds no more data + * can be read. */ return EOF; default: printfPQExpBuffer(&conn->errorMessage, - "pqFlush() -- couldn't send data: errno=%d\n%s\n", - errno, strerror(errno)); + "pqFlush() -- couldn't send data: errno=%d\n%s\n", + errno, strerror(errno)); /* We don't assume it's a fatal error... */ return EOF; } @@ -672,9 +678,9 @@ pqFlush(PGconn *conn) { /* We didn't send it all, wait till we can send more */ - /* - * if the socket is in non-blocking mode we may need - * to abort here + /* + * if the socket is in non-blocking mode we may need to abort + * here */ #ifdef USE_SSL /* can't do anything for our SSL users yet */ @@ -723,7 +729,7 @@ pqWait(int forRead, int forWrite, PGconn *conn) if (forRead || forWrite) { - retry: +retry: FD_ZERO(&input_mask); FD_ZERO(&output_mask); if (forRead) @@ -736,7 +742,7 @@ pqWait(int forRead, int forWrite, PGconn *conn) if (errno == EINTR) goto retry; printfPQExpBuffer(&conn->errorMessage, - "pqWait() -- select() failed: errno=%d\n%s\n", + "pqWait() -- select() failed: errno=%d\n%s\n", errno, strerror(errno)); return EOF; } @@ -775,7 +781,7 @@ PQenv2encoding(void) str = getenv("PGCLIENTENCODING"); if (str && *str != '\0') encoding = pg_char_to_encoding(str); - return(encoding); + return (encoding); } #else @@ -784,8 +790,8 @@ PQenv2encoding(void) int PQmblen(const unsigned char *s, int encoding) { - (void)s; - (void)encoding; + (void) s; + (void) encoding; return 1; } int diff --git a/src/interfaces/libpq/fe-print.c b/src/interfaces/libpq/fe-print.c index 840f47e5796..fac34940543 100644 --- a/src/interfaces/libpq/fe-print.c +++ b/src/interfaces/libpq/fe-print.c @@ -10,7 +10,7 @@ * didn't really belong there. * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-print.c,v 1.36 2000/03/14 23:59:23 tgl Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-print.c,v 1.37 2000/04/12 17:17:15 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -38,12 +38,14 @@ #ifdef TIOCGWINSZ static struct winsize screen_size; + #else static struct winsize { int ws_row; int ws_col; } screen_size; + #endif @@ -54,7 +56,7 @@ static void do_field(const PQprintOpt *po, const PGresult *res, unsigned char *fieldNotNum, int *fieldMax, const int fieldMaxLen, FILE *fout); static char *do_header(FILE *fout, const PQprintOpt *po, const int nFields, - int *fieldMax, const char **fieldNames, unsigned char *fieldNotNum, + int *fieldMax, const char **fieldNames, unsigned char *fieldNotNum, const int fs_len, const PGresult *res); static void output_row(FILE *fout, const PQprintOpt *po, const int nFields, char **fields, unsigned char *fieldNotNum, int *fieldMax, char *border, @@ -77,8 +79,8 @@ static void fill(int length, int max, char filler, FILE *fp); void PQprint(FILE *fout, - const PGresult *res, - const PQprintOpt *po) + const PGresult *res, + const PQprintOpt *po) { int nFields; @@ -93,7 +95,7 @@ PQprint(FILE *fout, unsigned char *fieldNotNum = NULL; char *border = NULL; char **fields = NULL; - const char **fieldNames; + const char **fieldNames; int fieldMaxLen = 0; int numFieldName; int fs_len = strlen(po->fieldSep); @@ -125,7 +127,7 @@ PQprint(FILE *fout, for (j = 0; j < nFields; j++) { int len; - const char *s = (j < numFieldName && po->fieldName[j][0]) ? + const char *s = (j < numFieldName && po->fieldName[j][0]) ? po->fieldName[j] : PQfname(res, j); fieldNames[j] = s; @@ -216,7 +218,7 @@ PQprint(FILE *fout, for (j = 0; j < nFields; j++) { - const char *s = fieldNames[j]; + const char *s = fieldNames[j]; fputs(s, fout); len += strlen(s) + fs_len; @@ -313,12 +315,12 @@ static void do_field(const PQprintOpt *po, const PGresult *res, const int i, const int j, const int fs_len, char **fields, - const int nFields, char const **fieldNames, + const int nFields, char const ** fieldNames, unsigned char *fieldNotNum, int *fieldMax, const int fieldMaxLen, FILE *fout) { - const char *pval, + const char *pval, *p; int plen; bool skipit; @@ -341,7 +343,7 @@ do_field(const PQprintOpt *po, const PGresult *res, if (!skipit) { - if (po->align && ! fieldNotNum[j]) + if (po->align && !fieldNotNum[j]) { /* Detect whether field contains non-numeric data */ char ch = '0'; @@ -353,21 +355,22 @@ do_field(const PQprintOpt *po, const PGresult *res, #endif { ch = *p; - if (! ((ch >= '0' && ch <= '9') || - ch == '.' || - ch == 'E' || - ch == 'e' || - ch == ' ' || - ch == '-')) + if (!((ch >= '0' && ch <= '9') || + ch == '.' || + ch == 'E' || + ch == 'e' || + ch == ' ' || + ch == '-')) { fieldNotNum[j] = 1; break; } } + /* - * Above loop will believe E in first column is numeric; also, we - * insist on a digit in the last column for a numeric. This test - * is still not bulletproof but it handles most cases. + * Above loop will believe E in first column is numeric; also, + * we insist on a digit in the last column for a numeric. This + * test is still not bulletproof but it handles most cases. */ if (*pval == 'E' || *pval == 'e' || !(ch >= '0' && ch <= '9')) @@ -483,7 +486,7 @@ do_header(FILE *fout, const PQprintOpt *po, const int nFields, int *fieldMax, fputs(po->fieldSep, fout); for (j = 0; j < nFields; j++) { - const char *s = PQfname(res, j); + const char *s = PQfname(res, j); if (po->html3) { @@ -562,11 +565,11 @@ output_row(FILE *fout, const PQprintOpt *po, const int nFields, char **fields, void PQdisplayTuples(const PGresult *res, - FILE *fp, /* where to send the output */ - int fillAlign, /* pad the fields with spaces */ - const char *fieldSep, /* field separator */ - int printHeader,/* display headers? */ - int quiet + FILE *fp, /* where to send the output */ + int fillAlign, /* pad the fields with spaces */ + const char *fieldSep, /* field separator */ + int printHeader,/* display headers? */ + int quiet ) { #define DEFAULT_FIELD_SEP " " @@ -597,7 +600,8 @@ PQdisplayTuples(const PGresult *res, fLength[j] = strlen(PQfname(res, j)); for (i = 0; i < nTuples; i++) { - int flen = PQgetlength(res, i, j); + int flen = PQgetlength(res, i, j); + if (flen > fLength[j]) fLength[j] = flen; } @@ -653,11 +657,11 @@ PQdisplayTuples(const PGresult *res, void PQprintTuples(const PGresult *res, - FILE *fout, /* output stream */ - int PrintAttNames,/* print attribute names or not */ - int TerseOutput, /* delimiter bars or not? */ - int colWidth /* width of column, if 0, use variable - * width */ + FILE *fout, /* output stream */ + int PrintAttNames,/* print attribute names or not */ + int TerseOutput, /* delimiter bars or not? */ + int colWidth /* width of column, if 0, use variable + * width */ ) { int nFields; @@ -713,7 +717,7 @@ PQprintTuples(const PGresult *res, { for (j = 0; j < nFields; j++) { - const char *pval = PQgetvalue(res, i, j); + const char *pval = PQgetvalue(res, i, j); fprintf(fout, formatString, TerseOutput ? "" : "|", @@ -730,13 +734,13 @@ PQprintTuples(const PGresult *res, /* simply send out max-length number of filler characters to fp */ - + static void fill(int length, int max, char filler, FILE *fp) { - int count; - - count = max - length; - while (count-- >= 0) - putc(filler, fp); + int count; + + count = max - length; + while (count-- >= 0) + putc(filler, fp); } diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h index 2aba91a6905..f82ae6ce17b 100644 --- a/src/interfaces/libpq/libpq-fe.h +++ b/src/interfaces/libpq/libpq-fe.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: libpq-fe.h,v 1.64 2000/03/30 02:59:14 tgl Exp $ + * $Id: libpq-fe.h,v 1.65 2000/04/12 17:17:15 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -30,31 +30,37 @@ extern "C" typedef enum { - /* Although you may decide to change this list in some way, - values which become unused should never be removed, nor - should constants be redefined - that would break - compatibility with existing code. */ + + /* + * Although you may decide to change this list in some way, values + * which become unused should never be removed, nor should + * constants be redefined - that would break compatibility with + * existing code. + */ CONNECTION_OK, CONNECTION_BAD, /* Non-blocking mode only below here */ - /* The existence of these should never be relied upon - they - should only be used for user feedback or similar purposes. */ - CONNECTION_STARTED, /* Waiting for connection to be made. */ - CONNECTION_MADE, /* Connection OK; waiting to send. */ - CONNECTION_AWAITING_RESPONSE, /* Waiting for a response - from the postmaster. */ - CONNECTION_AUTH_OK, /* Received authentication; - waiting for backend startup. */ - CONNECTION_SETENV /* Negotiating environment. */ + + /* + * The existence of these should never be relied upon - they + * should only be used for user feedback or similar purposes. + */ + CONNECTION_STARTED, /* Waiting for connection to be made. */ + CONNECTION_MADE, /* Connection OK; waiting to send. */ + CONNECTION_AWAITING_RESPONSE, /* Waiting for a response from the + * postmaster. */ + CONNECTION_AUTH_OK, /* Received authentication; waiting for + * backend startup. */ + CONNECTION_SETENV /* Negotiating environment. */ } ConnStatusType; typedef enum { PGRES_POLLING_FAILED = 0, - PGRES_POLLING_READING, /* These two indicate that one may */ - PGRES_POLLING_WRITING, /* use select before polling again. */ + PGRES_POLLING_READING, /* These two indicate that one may */ + PGRES_POLLING_WRITING, /* use select before polling again. */ PGRES_POLLING_OK, - PGRES_POLLING_ACTIVE /* Can call poll function immediately.*/ + PGRES_POLLING_ACTIVE /* Can call poll function immediately. */ } PostgresPollingStatusType; typedef enum @@ -104,17 +110,17 @@ extern "C" typedef void (*PQnoticeProcessor) (void *arg, const char *message); /* Print options for PQprint() */ - typedef char pqbool; + typedef char pqbool; typedef struct _PQprintOpt { - pqbool header; /* print output field headings and row + pqbool header; /* print output field headings and row * count */ - pqbool align; /* fill align the fields */ - pqbool standard; /* old brain dead format */ - pqbool html3; /* output html tables */ - pqbool expanded; /* expand tables */ - pqbool pager; /* use pager for output if needed */ + pqbool align; /* fill align the fields */ + pqbool standard; /* old brain dead format */ + pqbool html3; /* output html tables */ + pqbool expanded; /* expand tables */ + pqbool pager; /* use pager for output if needed */ char *fieldSep; /* field separator */ char *tableOpt; /* insert to HTML <table ...> */ char *caption; /* HTML <caption> */ @@ -135,14 +141,13 @@ extern "C" char *keyword; /* The keyword of the option */ char *envvar; /* Fallback environment variable name */ char *compiled; /* Fallback compiled in default value */ - char *val; /* Option's current value, or NULL */ + char *val; /* Option's current value, or NULL */ char *label; /* Label for field in connect dialog */ - char *dispchar; /* Character to display for this field - * in a connect dialog. Values are: - * "" Display entered value as is - * "*" Password field - hide value - * "D" Debug option - don't show by default - */ + char *dispchar; /* Character to display for this field in + * a connect dialog. Values are: "" + * Display entered value as is "*" + * Password field - hide value "D" Debug + * option - don't show by default */ int dispsize; /* Field size in characters for dialog */ } PQconninfoOption; @@ -176,8 +181,8 @@ extern "C" extern PGconn *PQconnectdb(const char *conninfo); extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions, const char *pgtty, - const char *dbName, - const char *login, const char *pwd); + const char *dbName, + const char *login, const char *pwd); #define PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME) \ PQsetdbLogin(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME, NULL, NULL) @@ -195,7 +200,7 @@ extern "C" * parameters */ /* Asynchronous (non-blocking) */ - extern int PQresetStart(PGconn *conn); + extern int PQresetStart(PGconn *conn); extern PostgresPollingStatusType PQresetPoll(PGconn *conn); /* Synchronous (blocking) */ extern void PQreset(PGconn *conn); @@ -258,12 +263,12 @@ extern "C" * use */ extern PGresult *PQfn(PGconn *conn, - int fnid, - int *result_buf, - int *result_len, - int result_is_int, - const PQArgBlock *args, - int nargs); + int fnid, + int *result_buf, + int *result_len, + int result_is_int, + const PQArgBlock *args, + int nargs); /* Accessor functions for PGresult objects */ extern ExecStatusType PQresultStatus(const PGresult *res); @@ -278,8 +283,8 @@ extern "C" extern int PQfsize(const PGresult *res, int field_num); extern int PQfmod(const PGresult *res, int field_num); extern char *PQcmdStatus(PGresult *res); - extern char *PQoidStatus(const PGresult *res); /* old and ugly */ - extern Oid PQoidValue(const PGresult *res); /* new and improved */ + extern char *PQoidStatus(const PGresult *res); /* old and ugly */ + extern Oid PQoidValue(const PGresult *res); /* new and improved */ extern char *PQcmdTuples(PGresult *res); extern char *PQgetvalue(const PGresult *res, int tup_num, int field_num); extern int PQgetlength(const PGresult *res, int tup_num, int field_num); @@ -298,25 +303,27 @@ extern "C" /* === in fe-print.c === */ extern void PQprint(FILE *fout, /* output stream */ - const PGresult *res, - const PQprintOpt *ps); /* option structure */ - - /* - * really old printing routines - */ - extern void PQdisplayTuples(const PGresult *res, - FILE *fp, /* where to send the output */ - int fillAlign, /* pad the fields with spaces */ - const char *fieldSep, /* field separator */ - int printHeader, /* display headers? */ - int quiet); - - extern void PQprintTuples(const PGresult *res, - FILE *fout, /* output stream */ - int printAttName, /* print attribute names */ - int terseOutput, /* delimiter bars */ - int width); /* width of column, if - * 0, use variable width */ + const PGresult *res, + const PQprintOpt *ps); /* option structure */ + + /* + * really old printing routines + */ + extern void PQdisplayTuples(const PGresult *res, + FILE *fp, /* where to send the + * output */ + int fillAlign, /* pad the fields with + * spaces */ + const char *fieldSep, /* field separator */ + int printHeader, /* display headers? */ + int quiet); + + extern void PQprintTuples(const PGresult *res, + FILE *fout, /* output stream */ + int printAttName, /* print attribute names */ + int terseOutput, /* delimiter bars */ + int width); /* width of column, if + * 0, use variable width */ /* === in fe-lobj.c === */ @@ -339,10 +346,11 @@ extern "C" extern int PQmblen(const unsigned char *s, int encoding); /* Get encoding id from environment variable PGCLIENTENCODING */ - extern int PQenv2encoding(void); + extern int PQenv2encoding(void); #ifdef __cplusplus } + #endif #endif /* LIBPQ_FE_H */ diff --git a/src/interfaces/libpq/libpq-int.h b/src/interfaces/libpq/libpq-int.h index 823abf20a33..fbad5fc7a81 100644 --- a/src/interfaces/libpq/libpq-int.h +++ b/src/interfaces/libpq/libpq-int.h @@ -12,7 +12,7 @@ * Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: libpq-int.h,v 1.22 2000/03/24 01:39:55 tgl Exp $ + * $Id: libpq-int.h,v 1.23 2000/04/12 17:17:15 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -81,7 +81,7 @@ typedef struct pgresAttDesc Oid typid; /* type id */ int typlen; /* type size */ int atttypmod; /* type-specific modifier info */ -} PGresAttDesc; +} PGresAttDesc; /* Data for a single attribute of a single tuple */ @@ -106,7 +106,7 @@ typedef struct pgresAttValue int len; /* length in bytes of the value */ char *value; /* actual value, plus terminating zero * byte */ -} PGresAttValue; +} PGresAttValue; struct pg_result { @@ -121,12 +121,13 @@ struct pg_result * last query */ int binary; /* binary tuple values if binary == 1, * otherwise ASCII */ + /* - * The conn link in PGresult is no longer used by any libpq code. - * It should be removed entirely, because it could be a dangling link - * (the application could keep the PGresult around longer than it keeps - * the PGconn!) But there may be apps out there that depend on it, - * so we will leave it here at least for a release or so. + * The conn link in PGresult is no longer used by any libpq code. It + * should be removed entirely, because it could be a dangling link + * (the application could keep the PGresult around longer than it + * keeps the PGconn!) But there may be apps out there that depend on + * it, so we will leave it here at least for a release or so. */ PGconn *xconn; /* connection we did the query on, if any */ @@ -134,9 +135,9 @@ struct pg_result * These fields are copied from the originating PGconn, so that * operations on the PGresult don't have to reference the PGconn. */ - PQnoticeProcessor noticeHook; /* notice/error message processor */ + PQnoticeProcessor noticeHook; /* notice/error message processor */ void *noticeArg; - int client_encoding; /* encoding id */ + int client_encoding;/* encoding id */ char *errMsg; /* error message, or NULL if no error */ @@ -162,7 +163,7 @@ typedef enum PGASYNC_READY, /* result ready for PQgetResult */ PGASYNC_COPY_IN, /* Copy In data transfer in progress */ PGASYNC_COPY_OUT /* Copy Out data transfer in progress */ -} PGAsyncStatusType; +} PGAsyncStatusType; /* PGSetenvStatusType defines the state of the PQSetenv state machine */ typedef enum @@ -170,10 +171,10 @@ typedef enum SETENV_STATE_OPTION_SEND, /* About to send an Environment Option */ SETENV_STATE_OPTION_WAIT, /* Waiting for above send to complete */ /* these next two are only used in MULTIBYTE mode */ - SETENV_STATE_ENCODINGS_SEND, /* About to send an "encodings" query */ - SETENV_STATE_ENCODINGS_WAIT, /* Waiting for query to complete */ + SETENV_STATE_ENCODINGS_SEND,/* About to send an "encodings" query */ + SETENV_STATE_ENCODINGS_WAIT,/* Waiting for query to complete */ SETENV_STATE_IDLE -} PGSetenvStatusType; +} PGSetenvStatusType; /* large-object-access data ... allocated only if large-object code is used. */ typedef struct pgLobjfuncs @@ -186,7 +187,7 @@ typedef struct pgLobjfuncs Oid fn_lo_tell; /* OID of backend function lo_tell */ Oid fn_lo_read; /* OID of backend function LOread */ Oid fn_lo_write; /* OID of backend function LOwrite */ -} PGlobjfuncs; +} PGlobjfuncs; /* PGconn stores all the state data associated with a single connection * to a backend. @@ -197,8 +198,8 @@ struct pg_conn char *pghost; /* the machine on which the server is * running */ char *pghostaddr; /* the IPv4 address of the machine on - * which the server is running, in - * IPv4 numbers-and-dots notation. Takes + * which the server is running, in IPv4 + * numbers-and-dots notation. Takes * precedence over above. */ char *pgport; /* the server's communication port */ char *pgtty; /* tty on which the backend messages is @@ -243,8 +244,8 @@ struct pg_conn int inEnd; /* offset to first position after avail * data */ - int nonblocking; /* whether this connection is using a blocking - * socket to the backend or not */ + int nonblocking; /* whether this connection is using a + * blocking socket to the backend or not */ /* Buffer for data not yet sent to backend */ char *outBuffer; /* currently allocated buffer */ @@ -256,21 +257,21 @@ struct pg_conn PGresAttValue *curTuple; /* tuple currently being read */ /* Status for sending environment info. Used during PQSetenv only. */ - PGSetenvStatusType setenv_state; + PGSetenvStatusType setenv_state; const struct EnvironmentOptions *next_eo; #ifdef USE_SSL - bool allow_ssl_try; /* Allowed to try SSL negotiation */ - SSL *ssl; /* SSL status, if have SSL connection */ + bool allow_ssl_try; /* Allowed to try SSL negotiation */ + SSL *ssl; /* SSL status, if have SSL connection */ #endif /* Buffer for current error message */ - PQExpBufferData errorMessage; /* expansible string */ + PQExpBufferData errorMessage; /* expansible string */ /* Buffer for receiving various parts of messages */ - PQExpBufferData workBuffer; /* expansible string */ + PQExpBufferData workBuffer; /* expansible string */ - int client_encoding; /* encoding id */ + int client_encoding;/* encoding id */ }; /* String descriptions of the ExecStatusTypes. @@ -338,7 +339,7 @@ extern char *sys_errlist[]; #endif /* sunos4 */ #endif /* !strerror */ -/* +/* * this is so that we can check is a connection is non-blocking internally * without the overhead of a function call */ diff --git a/src/interfaces/libpq/pqexpbuffer.c b/src/interfaces/libpq/pqexpbuffer.c index f70d9139c2e..623ec8306e5 100644 --- a/src/interfaces/libpq/pqexpbuffer.c +++ b/src/interfaces/libpq/pqexpbuffer.c @@ -17,7 +17,7 @@ * Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1994, Regents of the University of California * - * $Header: /cvsroot/pgsql/src/interfaces/libpq/pqexpbuffer.c,v 1.5 2000/02/07 23:10:11 petere Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/pqexpbuffer.c,v 1.6 2000/04/12 17:17:15 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -39,7 +39,7 @@ PQExpBuffer createPQExpBuffer(void) { - PQExpBuffer res; + PQExpBuffer res; res = (PQExpBuffer) malloc(sizeof(PQExpBufferData)); if (res != NULL) @@ -156,7 +156,7 @@ enlargePQExpBuffer(PQExpBuffer str, size_t needed) /*------------------------ * printfPQExpBuffer * Format text data under the control of fmt (an sprintf-like format string) - * and insert it into str. More space is allocated to str if necessary. + * and insert it into str. More space is allocated to str if necessary. * This is a convenience routine that does the same thing as * resetPQExpBuffer() followed by appendPQExpBuffer(). */ @@ -165,7 +165,7 @@ printfPQExpBuffer(PQExpBuffer str, const char *fmt,...) { va_list args; size_t avail; - int nprinted; + int nprinted; resetPQExpBuffer(str); @@ -184,12 +184,13 @@ printfPQExpBuffer(PQExpBuffer str, const char *fmt,...) nprinted = vsnprintf(str->data + str->len, avail, fmt, args); va_end(args); + /* * Note: some versions of vsnprintf return the number of chars - * actually stored, but at least one returns -1 on failure. - * Be conservative about believing whether the print worked. + * actually stored, but at least one returns -1 on failure. Be + * conservative about believing whether the print worked. */ - if (nprinted >= 0 && nprinted < avail-1) + if (nprinted >= 0 && nprinted < avail - 1) { /* Success. Note nprinted does not include trailing null. */ str->len += nprinted; @@ -197,7 +198,7 @@ printfPQExpBuffer(PQExpBuffer str, const char *fmt,...) } } /* Double the buffer size and try again. */ - if (! enlargePQExpBuffer(str, str->maxlen)) + if (!enlargePQExpBuffer(str, str->maxlen)) return; /* oops, out of memory */ } } @@ -215,7 +216,7 @@ appendPQExpBuffer(PQExpBuffer str, const char *fmt,...) { va_list args; size_t avail; - int nprinted; + int nprinted; for (;;) { @@ -232,12 +233,13 @@ appendPQExpBuffer(PQExpBuffer str, const char *fmt,...) nprinted = vsnprintf(str->data + str->len, avail, fmt, args); va_end(args); + /* * Note: some versions of vsnprintf return the number of chars - * actually stored, but at least one returns -1 on failure. - * Be conservative about believing whether the print worked. + * actually stored, but at least one returns -1 on failure. Be + * conservative about believing whether the print worked. */ - if (nprinted >= 0 && nprinted < avail-1) + if (nprinted >= 0 && nprinted < avail - 1) { /* Success. Note nprinted does not include trailing null. */ str->len += nprinted; @@ -245,7 +247,7 @@ appendPQExpBuffer(PQExpBuffer str, const char *fmt,...) } } /* Double the buffer size and try again. */ - if (! enlargePQExpBuffer(str, str->maxlen)) + if (!enlargePQExpBuffer(str, str->maxlen)) return; /* oops, out of memory */ } } @@ -270,7 +272,7 @@ void appendPQExpBufferChar(PQExpBuffer str, char ch) { /* Make more room if needed */ - if (! enlargePQExpBuffer(str, 1)) + if (!enlargePQExpBuffer(str, 1)) return; /* OK, append the character */ @@ -289,7 +291,7 @@ void appendBinaryPQExpBuffer(PQExpBuffer str, const char *data, size_t datalen) { /* Make more room if needed */ - if (! enlargePQExpBuffer(str, datalen)) + if (!enlargePQExpBuffer(str, datalen)) return; /* OK, append the data */ diff --git a/src/interfaces/libpq/pqexpbuffer.h b/src/interfaces/libpq/pqexpbuffer.h index 014254465aa..ac50f8ea9a6 100644 --- a/src/interfaces/libpq/pqexpbuffer.h +++ b/src/interfaces/libpq/pqexpbuffer.h @@ -18,7 +18,7 @@ * Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: pqexpbuffer.h,v 1.4 2000/02/07 23:10:11 petere Exp $ + * $Id: pqexpbuffer.h,v 1.5 2000/04/12 17:17:15 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -52,7 +52,7 @@ typedef PQExpBufferData *PQExpBuffer; * be returned by PQrequestCancel() or any routine in fe-auth.c. *------------------------ */ -#define INITIAL_EXPBUFFER_SIZE 256 +#define INITIAL_EXPBUFFER_SIZE 256 /*------------------------ * There are two ways to create a PQExpBuffer object initially: @@ -113,12 +113,12 @@ extern void resetPQExpBuffer(PQExpBuffer str); * * Returns 1 if OK, 0 if failed to enlarge buffer. */ -extern int enlargePQExpBuffer(PQExpBuffer str, size_t needed); +extern int enlargePQExpBuffer(PQExpBuffer str, size_t needed); /*------------------------ * printfPQExpBuffer * Format text data under the control of fmt (an sprintf-like format string) - * and insert it into str. More space is allocated to str if necessary. + * and insert it into str. More space is allocated to str if necessary. * This is a convenience routine that does the same thing as * resetPQExpBuffer() followed by appendPQExpBuffer(). */ @@ -153,6 +153,6 @@ extern void appendPQExpBufferChar(PQExpBuffer str, char ch); * if necessary. */ extern void appendBinaryPQExpBuffer(PQExpBuffer str, - const char *data, size_t datalen); + const char *data, size_t datalen); #endif /* PQEXPBUFFER_H */ diff --git a/src/interfaces/libpq/win32.h b/src/interfaces/libpq/win32.h index 3b57134f02d..126dd152c06 100644 --- a/src/interfaces/libpq/win32.h +++ b/src/interfaces/libpq/win32.h @@ -32,4 +32,3 @@ #define DEF_PGPORT "5432" #define MAXIMUM_ALIGNOF 4 - |