1 /*-------------------------------------------------------------------------
7 * miscellaneous useful functions
9 * The communication routines here are analogous to the ones in
10 * backend/libpq/pqcomm.c and backend/libpq/pqcomprim.c, but operate
11 * in the considerably different environment of the frontend libpq.
12 * In particular, we work with a bare nonblock-mode socket, rather than
13 * a stdio stream, so that we can avoid unwanted blocking of the application.
15 * XXX: MOVE DEBUG PRINTOUT TO HIGHER LEVEL. As is, block and restart
16 * will cause repeat printouts.
18 * We must speak the same transmitted data representations as the backend
22 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
23 * Portions Copyright (c) 1994, Regents of the University of California
26 * $PostgreSQL: pgsql/src/interfaces/libpq/fe-misc.c,v 1.127 2006/05/23 19:28:45 momjian Exp $
28 *-------------------------------------------------------------------------
31 #include "postgres_fe.h"
37 #ifndef WIN32_CLIENT_ONLY
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
52 #ifdef HAVE_SYS_POLL_H
55 #ifdef HAVE_SYS_SELECT_H
56 #include <sys/select.h>
60 #include "libpq-int.h"
62 #include "mb/pg_wchar.h"
63 #include "pg_config_paths.h"
66 static int pqPutMsgBytes(const void *buf, size_t len, PGconn *conn);
67 static int pqSendSome(PGconn *conn, int len);
68 static int pqSocketCheck(PGconn *conn, int forRead, int forWrite,
70 static int pqSocketPoll(int sock, int forRead, int forWrite, time_t end_time);
74 * pqGetc: get 1 character from the connection
76 * All these routines return 0 on success, EOF on error.
77 * Note that for the Get routines, EOF only means there is not enough
78 * data in the buffer, not that there is necessarily a hard error.
81 pqGetc(char *result, PGconn *conn)
83 if (conn->inCursor >= conn->inEnd)
86 *result = conn->inBuffer[conn->inCursor++];
89 fprintf(conn->Pfdebug, "From backend> %c\n", *result);
96 * pqPutc: write 1 char to the current message
99 pqPutc(char c, PGconn *conn)
101 if (pqPutMsgBytes(&c, 1, conn))
105 fprintf(conn->Pfdebug, "To backend> %c\n", c);
113 * get a null-terminated string from the connection,
114 * and store it in an expansible PQExpBuffer.
115 * If we run out of memory, all of the string is still read,
116 * but the excess characters are silently discarded.
119 pqGets(PQExpBuffer buf, PGconn *conn)
121 /* Copy conn data to locals for faster search loop */
122 char *inBuffer = conn->inBuffer;
123 int inCursor = conn->inCursor;
124 int inEnd = conn->inEnd;
127 while (inCursor < inEnd && inBuffer[inCursor])
130 if (inCursor >= inEnd)
133 slen = inCursor - conn->inCursor;
135 resetPQExpBuffer(buf);
136 appendBinaryPQExpBuffer(buf, inBuffer + conn->inCursor, slen);
138 conn->inCursor = ++inCursor;
141 fprintf(conn->Pfdebug, "From backend> \"%s\"\n",
149 * pqPuts: write a null-terminated string to the current message
152 pqPuts(const char *s, PGconn *conn)
154 if (pqPutMsgBytes(s, strlen(s) + 1, conn))
158 fprintf(conn->Pfdebug, "To backend> \"%s\"\n", s);
165 * get a string of exactly len bytes in buffer s, no null termination
168 pqGetnchar(char *s, size_t len, PGconn *conn)
170 if (len < 0 || len > (size_t) (conn->inEnd - conn->inCursor))
173 memcpy(s, conn->inBuffer + conn->inCursor, len);
174 /* no terminating null */
176 conn->inCursor += len;
179 fprintf(conn->Pfdebug, "From backend (%lu)> %.*s\n",
180 (unsigned long) len, (int) len, s);
187 * write exactly len bytes to the current message
190 pqPutnchar(const char *s, size_t len, PGconn *conn)
192 if (pqPutMsgBytes(s, len, conn))
196 fprintf(conn->Pfdebug, "To backend> %.*s\n", (int) len, s);
203 * read a 2 or 4 byte integer and convert from network byte order
204 * to local byte order
207 pqGetInt(int *result, size_t bytes, PGconn *conn)
215 if (conn->inCursor + 2 > conn->inEnd)
217 memcpy(&tmp2, conn->inBuffer + conn->inCursor, 2);
219 *result = (int) ntohs(tmp2);
222 if (conn->inCursor + 4 > conn->inEnd)
224 memcpy(&tmp4, conn->inBuffer + conn->inCursor, 4);
226 *result = (int) ntohl(tmp4);
229 pqInternalNotice(&conn->noticeHooks,
230 "integer of size %lu not supported by pqGetInt",
231 (unsigned long) bytes);
236 fprintf(conn->Pfdebug, "From backend (#%lu)> %d\n", (unsigned long) bytes, *result);
243 * write an integer of 2 or 4 bytes, converting from host byte order
244 * to network byte order.
247 pqPutInt(int value, size_t bytes, PGconn *conn)
255 tmp2 = htons((uint16) value);
256 if (pqPutMsgBytes((const char *) &tmp2, 2, conn))
260 tmp4 = htonl((uint32) value);
261 if (pqPutMsgBytes((const char *) &tmp4, 4, conn))
265 pqInternalNotice(&conn->noticeHooks,
266 "integer of size %lu not supported by pqPutInt",
267 (unsigned long) bytes);
272 fprintf(conn->Pfdebug, "To backend (%lu#)> %d\n", (unsigned long) bytes, value);
278 * Make sure conn's output buffer can hold bytes_needed bytes (caller must
279 * include already-stored data into the value!)
281 * Returns 0 on success, EOF if failed to enlarge buffer
284 pqCheckOutBufferSpace(int bytes_needed, PGconn *conn)
286 int newsize = conn->outBufSize;
289 if (bytes_needed <= newsize)
293 * If we need to enlarge the buffer, we first try to double it in size; if
294 * that doesn't work, enlarge in multiples of 8K. This avoids thrashing
295 * the malloc pool by repeated small enlargements.
297 * Note: tests for newsize > 0 are to catch integer overflow.
302 } while (bytes_needed > newsize && newsize > 0);
304 if (bytes_needed <= newsize)
306 newbuf = realloc(conn->outBuffer, newsize);
309 /* realloc succeeded */
310 conn->outBuffer = newbuf;
311 conn->outBufSize = newsize;
316 newsize = conn->outBufSize;
320 } while (bytes_needed > newsize && newsize > 0);
322 if (bytes_needed <= newsize)
324 newbuf = realloc(conn->outBuffer, newsize);
327 /* realloc succeeded */
328 conn->outBuffer = newbuf;
329 conn->outBufSize = newsize;
334 /* realloc failed. Probably out of memory */
335 printfPQExpBuffer(&conn->errorMessage,
336 "cannot allocate memory for output buffer\n");
341 * Make sure conn's input buffer can hold bytes_needed bytes (caller must
342 * include already-stored data into the value!)
344 * Returns 0 on success, EOF if failed to enlarge buffer
347 pqCheckInBufferSpace(int bytes_needed, PGconn *conn)
349 int newsize = conn->inBufSize;
352 if (bytes_needed <= newsize)
356 * If we need to enlarge the buffer, we first try to double it in size; if
357 * that doesn't work, enlarge in multiples of 8K. This avoids thrashing
358 * the malloc pool by repeated small enlargements.
360 * Note: tests for newsize > 0 are to catch integer overflow.
365 } while (bytes_needed > newsize && newsize > 0);
367 if (bytes_needed <= newsize)
369 newbuf = realloc(conn->inBuffer, newsize);
372 /* realloc succeeded */
373 conn->inBuffer = newbuf;
374 conn->inBufSize = newsize;
379 newsize = conn->inBufSize;
383 } while (bytes_needed > newsize && newsize > 0);
385 if (bytes_needed <= newsize)
387 newbuf = realloc(conn->inBuffer, newsize);
390 /* realloc succeeded */
391 conn->inBuffer = newbuf;
392 conn->inBufSize = newsize;
397 /* realloc failed. Probably out of memory */
398 printfPQExpBuffer(&conn->errorMessage,
399 "cannot allocate memory for input buffer\n");
404 * pqPutMsgStart: begin construction of a message to the server
406 * msg_type is the message type byte, or 0 for a message without type byte
407 * (only startup messages have no type byte)
409 * force_len forces the message to have a length word; otherwise, we add
410 * a length word if protocol 3.
412 * Returns 0 on success, EOF on error
414 * The idea here is that we construct the message in conn->outBuffer,
415 * beginning just past any data already in outBuffer (ie, at
416 * outBuffer+outCount). We enlarge the buffer as needed to hold the message.
417 * When the message is complete, we fill in the length word (if needed) and
418 * then advance outCount past the message, making it eligible to send.
420 * The state variable conn->outMsgStart points to the incomplete message's
421 * length word: it is either outCount or outCount+1 depending on whether
422 * there is a type byte. If we are sending a message without length word
423 * (pre protocol 3.0 only), then outMsgStart is -1. The state variable
424 * conn->outMsgEnd is the end of the data collected so far.
427 pqPutMsgStart(char msg_type, bool force_len, PGconn *conn)
432 /* allow room for message type byte */
434 endPos = conn->outCount + 1;
436 endPos = conn->outCount;
438 /* do we want a length word? */
439 if (force_len || PG_PROTOCOL_MAJOR(conn->pversion) >= 3)
442 /* allow room for message length */
448 /* make sure there is room for message header */
449 if (pqCheckOutBufferSpace(endPos, conn))
451 /* okay, save the message type byte if any */
453 conn->outBuffer[conn->outCount] = msg_type;
454 /* set up the message pointers */
455 conn->outMsgStart = lenPos;
456 conn->outMsgEnd = endPos;
457 /* length word, if needed, will be filled in by pqPutMsgEnd */
460 fprintf(conn->Pfdebug, "To backend> Msg %c\n",
461 msg_type ? msg_type : ' ');
467 * pqPutMsgBytes: add bytes to a partially-constructed message
469 * Returns 0 on success, EOF on error
472 pqPutMsgBytes(const void *buf, size_t len, PGconn *conn)
474 /* make sure there is room for it */
475 if (pqCheckOutBufferSpace(conn->outMsgEnd + len, conn))
477 /* okay, save the data */
478 memcpy(conn->outBuffer + conn->outMsgEnd, buf, len);
479 conn->outMsgEnd += len;
480 /* no Pfdebug call here, caller should do it */
485 * pqPutMsgEnd: finish constructing a message and possibly send it
487 * Returns 0 on success, EOF on error
489 * We don't actually send anything here unless we've accumulated at least
490 * 8K worth of data (the typical size of a pipe buffer on Unix systems).
491 * This avoids sending small partial packets. The caller must use pqFlush
492 * when it's important to flush all the data out to the server.
495 pqPutMsgEnd(PGconn *conn)
498 fprintf(conn->Pfdebug, "To backend> Msg complete, length %u\n",
499 conn->outMsgEnd - conn->outCount);
501 /* Fill in length word if needed */
502 if (conn->outMsgStart >= 0)
504 uint32 msgLen = conn->outMsgEnd - conn->outMsgStart;
506 msgLen = htonl(msgLen);
507 memcpy(conn->outBuffer + conn->outMsgStart, &msgLen, 4);
510 /* Make message eligible to send */
511 conn->outCount = conn->outMsgEnd;
513 if (conn->outCount >= 8192)
515 int toSend = conn->outCount - (conn->outCount % 8192);
517 if (pqSendSome(conn, toSend) < 0)
519 /* in nonblock mode, don't complain if unable to send it all */
526 * pqReadData: read more data, if any is available
527 * Possible return values:
528 * 1: successfully loaded at least one more byte
529 * 0: no data is presently available, but no error detected
530 * -1: error detected (including EOF = connection closure);
531 * conn->errorMessage set
532 * NOTE: callers must not assume that pointers or indexes into conn->inBuffer
533 * remain valid across this call!
537 pqReadData(PGconn *conn)
545 printfPQExpBuffer(&conn->errorMessage,
546 libpq_gettext("connection not open\n"));
550 /* Left-justify any data in the buffer to make room */
551 if (conn->inStart < conn->inEnd)
553 if (conn->inStart > 0)
555 memmove(conn->inBuffer, conn->inBuffer + conn->inStart,
556 conn->inEnd - conn->inStart);
557 conn->inEnd -= conn->inStart;
558 conn->inCursor -= conn->inStart;
564 /* buffer is logically empty, reset it */
565 conn->inStart = conn->inCursor = conn->inEnd = 0;
569 * If the buffer is fairly full, enlarge it. We need to be able to enlarge
570 * the buffer in case a single message exceeds the initial buffer size. We
571 * enlarge before filling the buffer entirely so as to avoid asking the
572 * kernel for a partial packet. The magic constant here should be large
573 * enough for a TCP packet or Unix pipe bufferload. 8K is the usual pipe
576 if (conn->inBufSize - conn->inEnd < 8192)
578 if (pqCheckInBufferSpace(conn->inEnd + 8192, conn))
581 * We don't insist that the enlarge worked, but we need some room
583 if (conn->inBufSize - conn->inEnd < 100)
584 return -1; /* errorMessage already set */
588 /* OK, try to read some data */
590 nread = pqsecure_read(conn, conn->inBuffer + conn->inEnd,
591 conn->inBufSize - conn->inEnd);
594 if (SOCK_ERRNO == EINTR)
596 /* Some systems return EAGAIN/EWOULDBLOCK for no data */
598 if (SOCK_ERRNO == EAGAIN)
601 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
602 if (SOCK_ERRNO == EWOULDBLOCK)
605 /* We might get ECONNRESET here if using TCP and backend died */
607 if (SOCK_ERRNO == ECONNRESET)
608 goto definitelyFailed;
610 printfPQExpBuffer(&conn->errorMessage,
611 libpq_gettext("could not receive data from server: %s\n"),
612 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
617 conn->inEnd += nread;
620 * Hack to deal with the fact that some kernels will only give us back
621 * 1 packet per recv() call, even if we asked for more and there is
622 * more available. If it looks like we are reading a long message,
623 * loop back to recv() again immediately, until we run out of data or
624 * buffer space. Without this, the block-and-restart behavior of
625 * libpq's higher levels leads to O(N^2) performance on long messages.
627 * Since we left-justified the data above, conn->inEnd gives the
628 * amount of data already read in the current message. We consider
629 * the message "long" once we have acquired 32k ...
631 if (conn->inEnd > 32768 &&
632 (conn->inBufSize - conn->inEnd) >= 8192)
641 return 1; /* got a zero read after successful tries */
644 * A return value of 0 could mean just that no data is now available, or
645 * it could mean EOF --- that is, the server has closed the connection.
646 * Since we have the socket in nonblock mode, the only way to tell the
647 * difference is to see if select() is saying that the file is ready.
648 * Grumble. Fortunately, we don't expect this path to be taken much,
649 * since in normal practice we should not be trying to read data unless
650 * the file selected for reading already.
652 * In SSL mode it's even worse: SSL_read() could say WANT_READ and then
653 * data could arrive before we make the pqReadReady() test. So we must
654 * play dumb and assume there is more data, relying on the SSL layer to
663 switch (pqReadReady(conn))
666 /* definitely no data available */
672 goto definitelyFailed;
676 * Still not sure that it's EOF, because some data could have just
680 nread = pqsecure_read(conn, conn->inBuffer + conn->inEnd,
681 conn->inBufSize - conn->inEnd);
684 if (SOCK_ERRNO == EINTR)
686 /* Some systems return EAGAIN/EWOULDBLOCK for no data */
688 if (SOCK_ERRNO == EAGAIN)
691 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
692 if (SOCK_ERRNO == EWOULDBLOCK)
695 /* We might get ECONNRESET here if using TCP and backend died */
697 if (SOCK_ERRNO == ECONNRESET)
698 goto definitelyFailed;
700 printfPQExpBuffer(&conn->errorMessage,
701 libpq_gettext("could not receive data from server: %s\n"),
702 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
707 conn->inEnd += nread;
712 * OK, we are getting a zero read even though select() says ready. This
713 * means the connection has been closed. Cope.
716 printfPQExpBuffer(&conn->errorMessage,
718 "server closed the connection unexpectedly\n"
719 "\tThis probably means the server terminated abnormally\n"
720 "\tbefore or while processing the request.\n"));
721 conn->status = CONNECTION_BAD; /* No more connection to backend */
722 pqsecure_close(conn);
723 closesocket(conn->sock);
730 * pqSendSome: send data waiting in the output buffer.
732 * len is how much to try to send (typically equal to outCount, but may
735 * Return 0 on success, -1 on failure and 1 when not all data could be sent
736 * because the socket would block and the connection is non-blocking.
739 pqSendSome(PGconn *conn, int len)
741 char *ptr = conn->outBuffer;
742 int remaining = conn->outCount;
747 printfPQExpBuffer(&conn->errorMessage,
748 libpq_gettext("connection not open\n"));
752 /* while there's still data to send */
758 sent = pqsecure_write(conn, ptr, len);
763 * Anything except EAGAIN/EWOULDBLOCK/EINTR is trouble. If it's
764 * EPIPE or ECONNRESET, assume we've lost the backend connection
773 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
784 printfPQExpBuffer(&conn->errorMessage,
786 "server closed the connection unexpectedly\n"
787 "\tThis probably means the server terminated abnormally\n"
788 "\tbefore or while processing the request.\n"));
791 * We used to close the socket here, but that's a bad idea
792 * since there might be unread data waiting (typically, a
793 * NOTICE message from the backend telling us it's
794 * committing hara-kiri...). Leave the socket open until
795 * pqReadData finds no more data can be read. But abandon
796 * attempt to send data.
802 printfPQExpBuffer(&conn->errorMessage,
803 libpq_gettext("could not send data to server: %s\n"),
804 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
805 /* We don't assume it's a fatal error... */
820 * We didn't send it all, wait till we can send more.
822 * If the connection is in non-blocking mode we don't wait, but
823 * return 1 to indicate that data is still pending.
825 if (pqIsnonblocking(conn))
832 * There are scenarios in which we can't send data because the
833 * communications channel is full, but we cannot expect the server
834 * to clear the channel eventually because it's blocked trying to
835 * send data to us. (This can happen when we are sending a large
836 * amount of COPY data, and the server has generated lots of
837 * NOTICE responses.) To avoid a deadlock situation, we must be
838 * prepared to accept and buffer incoming data before we try
839 * again. Furthermore, it is possible that such incoming data
840 * might not arrive until after we've gone to sleep. Therefore,
841 * we wait for either read ready or write ready.
843 if (pqReadData(conn) < 0)
845 result = -1; /* error message already set up */
848 if (pqWait(TRUE, TRUE, conn))
856 /* shift the remaining contents of the buffer */
858 memmove(conn->outBuffer, ptr, remaining);
859 conn->outCount = remaining;
866 * pqFlush: send any data waiting in the output buffer
868 * Return 0 on success, -1 on failure and 1 when not all data could be sent
869 * because the socket would block and the connection is non-blocking.
872 pqFlush(PGconn *conn)
875 fflush(conn->Pfdebug);
877 if (conn->outCount > 0)
878 return pqSendSome(conn, conn->outCount);
885 * pqWait: wait until we can read or write the connection socket
887 * JAB: If SSL enabled and used and forRead, buffered bytes short-circuit the
890 * We also stop waiting and return if the kernel flags an exception condition
891 * on the socket. The actual error condition will be detected and reported
892 * when the caller tries to read or write the socket.
895 pqWait(int forRead, int forWrite, PGconn *conn)
897 return pqWaitTimed(forRead, forWrite, conn, (time_t) -1);
901 * pqWaitTimed: wait, but not past finish_time.
903 * If finish_time is exceeded then we return failure (EOF). This is like
904 * the response for a kernel exception because we don't want the caller
905 * to try to read/write in that case.
907 * finish_time = ((time_t) -1) disables the wait limit.
910 pqWaitTimed(int forRead, int forWrite, PGconn *conn, time_t finish_time)
914 result = pqSocketCheck(conn, forRead, forWrite, finish_time);
917 return EOF; /* errorMessage is already set */
921 printfPQExpBuffer(&conn->errorMessage,
922 libpq_gettext("timeout expired\n"));
930 * pqReadReady: is select() saying the file is ready to read?
931 * Returns -1 on failure, 0 if not ready, 1 if ready.
934 pqReadReady(PGconn *conn)
936 return pqSocketCheck(conn, 1, 0, (time_t) 0);
940 * pqWriteReady: is select() saying the file is ready to write?
941 * Returns -1 on failure, 0 if not ready, 1 if ready.
944 pqWriteReady(PGconn *conn)
946 return pqSocketCheck(conn, 0, 1, (time_t) 0);
950 * Checks a socket, using poll or select, for data to be read, written,
951 * or both. Returns >0 if one or more conditions are met, 0 if it timed
952 * out, -1 if an error occurred.
954 * If SSL is in use, the SSL buffer is checked prior to checking the socket
955 * for read data directly.
958 pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
966 printfPQExpBuffer(&conn->errorMessage,
967 libpq_gettext("socket not open\n"));
972 /* Check for SSL library buffering read bytes */
973 if (forRead && conn->ssl && SSL_pending(conn->ssl) > 0)
975 /* short-circuit the select */
980 /* We will retry as long as we get EINTR */
982 result = pqSocketPoll(conn->sock, forRead, forWrite, end_time);
983 while (result < 0 && SOCK_ERRNO == EINTR);
989 printfPQExpBuffer(&conn->errorMessage,
990 libpq_gettext("select() failed: %s\n"),
991 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
999 * Check a file descriptor for read and/or write data, possibly waiting.
1000 * If neither forRead nor forWrite are set, immediately return a timeout
1001 * condition (without waiting). Return >0 if condition is met, 0
1002 * if a timeout occurred, -1 if an error or interrupt occurred.
1004 * Timeout is infinite if end_time is -1. Timeout is immediate (no blocking)
1005 * if end_time is 0 (or indeed, any time before now).
1008 pqSocketPoll(int sock, int forRead, int forWrite, time_t end_time)
1010 /* We use poll(2) if available, otherwise select(2) */
1012 struct pollfd input_fd;
1015 if (!forRead && !forWrite)
1019 input_fd.events = POLLERR;
1020 input_fd.revents = 0;
1023 input_fd.events |= POLLIN;
1025 input_fd.events |= POLLOUT;
1027 /* Compute appropriate timeout interval */
1028 if (end_time == ((time_t) -1))
1032 time_t now = time(NULL);
1035 timeout_ms = (end_time - now) * 1000;
1040 return poll(&input_fd, 1, timeout_ms);
1041 #else /* !HAVE_POLL */
1046 struct timeval timeout;
1047 struct timeval *ptr_timeout;
1049 if (!forRead && !forWrite)
1052 FD_ZERO(&input_mask);
1053 FD_ZERO(&output_mask);
1054 FD_ZERO(&except_mask);
1056 FD_SET(sock, &input_mask);
1058 FD_SET(sock, &output_mask);
1059 FD_SET(sock, &except_mask);
1061 /* Compute appropriate timeout interval */
1062 if (end_time == ((time_t) -1))
1066 time_t now = time(NULL);
1069 timeout.tv_sec = end_time - now;
1072 timeout.tv_usec = 0;
1073 ptr_timeout = &timeout;
1076 return select(sock + 1, &input_mask, &output_mask,
1077 &except_mask, ptr_timeout);
1078 #endif /* HAVE_POLL */
1083 * A couple of "miscellaneous" multibyte related functions. They used
1084 * to be in fe-print.c but that file is doomed.
1088 * returns the byte length of the word beginning s, using the
1089 * specified encoding.
1092 PQmblen(const char *s, int encoding)
1094 return pg_encoding_mblen(encoding, s);
1098 * returns the display length of the word beginning s, using the
1099 * specified encoding.
1102 PQdsplen(const char *s, int encoding)
1104 return pg_encoding_dsplen(encoding, s);
1108 * Get encoding id from environment variable PGCLIENTENCODING.
1111 PQenv2encoding(void)
1114 int encoding = PG_SQL_ASCII;
1116 str = getenv("PGCLIENTENCODING");
1117 if (str && *str != '\0')
1118 encoding = pg_char_to_encoding(str);
1126 libpq_gettext(const char *msgid)
1128 static bool already_bound = false;
1132 /* dgettext() preserves errno, but bindtextdomain() doesn't */
1134 int save_errno = GetLastError();
1136 int save_errno = errno;
1140 already_bound = true;
1141 /* No relocatable lookup here because the binary could be anywhere */
1142 ldir = getenv("PGLOCALEDIR");
1145 bindtextdomain("libpq", ldir);
1147 SetLastError(save_errno);
1153 return dgettext("libpq", msgid);
1156 #endif /* ENABLE_NLS */