OSDN Git Service

Set psql client encoding from locale by default
[pg-rex/syncrep.git] / src / interfaces / libpq / fe-protocol3.c
1 /*-------------------------------------------------------------------------
2  *
3  * fe-protocol3.c
4  *        functions that are specific to frontend/backend protocol version 3
5  *
6  * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        src/interfaces/libpq/fe-protocol3.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres_fe.h"
16
17 #include <ctype.h>
18 #include <fcntl.h>
19
20 #include "libpq-fe.h"
21 #include "libpq-int.h"
22
23 #include "mb/pg_wchar.h"
24
25 #ifdef WIN32
26 #include "win32.h"
27 #else
28 #include <unistd.h>
29 #include <netinet/in.h>
30 #ifdef HAVE_NETINET_TCP_H
31 #include <netinet/tcp.h>
32 #endif
33 #include <arpa/inet.h>
34 #endif
35
36
37 /*
38  * This macro lists the backend message types that could be "long" (more
39  * than a couple of kilobytes).
40  */
41 #define VALID_LONG_MESSAGE_TYPE(id) \
42         ((id) == 'T' || (id) == 'D' || (id) == 'd' || (id) == 'V' || \
43          (id) == 'E' || (id) == 'N' || (id) == 'A')
44
45
46 static void handleSyncLoss(PGconn *conn, char id, int msgLength);
47 static int      getRowDescriptions(PGconn *conn);
48 static int      getParamDescriptions(PGconn *conn);
49 static int      getAnotherTuple(PGconn *conn, int msgLength);
50 static int      getParameterStatus(PGconn *conn);
51 static int      getNotify(PGconn *conn);
52 static int      getCopyStart(PGconn *conn, ExecStatusType copytype);
53 static int      getReadyForQuery(PGconn *conn);
54 static void reportErrorPosition(PQExpBuffer msg, const char *query,
55                                         int loc, int encoding);
56 static int build_startup_packet(const PGconn *conn, char *packet,
57                                          const PQEnvironmentOption *options);
58
59
60 /*
61  * parseInput: if appropriate, parse input data from backend
62  * until input is exhausted or a stopping state is reached.
63  * Note that this function will NOT attempt to read more data from the backend.
64  */
65 void
66 pqParseInput3(PGconn *conn)
67 {
68         char            id;
69         int                     msgLength;
70         int                     avail;
71
72         /*
73          * Loop to parse successive complete messages available in the buffer.
74          */
75         for (;;)
76         {
77                 /*
78                  * Try to read a message.  First get the type code and length. Return
79                  * if not enough data.
80                  */
81                 conn->inCursor = conn->inStart;
82                 if (pqGetc(&id, conn))
83                         return;
84                 if (pqGetInt(&msgLength, 4, conn))
85                         return;
86
87                 /*
88                  * Try to validate message type/length here.  A length less than 4 is
89                  * definitely broken.  Large lengths should only be believed for a few
90                  * message types.
91                  */
92                 if (msgLength < 4)
93                 {
94                         handleSyncLoss(conn, id, msgLength);
95                         return;
96                 }
97                 if (msgLength > 30000 && !VALID_LONG_MESSAGE_TYPE(id))
98                 {
99                         handleSyncLoss(conn, id, msgLength);
100                         return;
101                 }
102
103                 /*
104                  * Can't process if message body isn't all here yet.
105                  */
106                 msgLength -= 4;
107                 avail = conn->inEnd - conn->inCursor;
108                 if (avail < msgLength)
109                 {
110                         /*
111                          * Before returning, enlarge the input buffer if needed to hold
112                          * the whole message.  This is better than leaving it to
113                          * pqReadData because we can avoid multiple cycles of realloc()
114                          * when the message is large; also, we can implement a reasonable
115                          * recovery strategy if we are unable to make the buffer big
116                          * enough.
117                          */
118                         if (pqCheckInBufferSpace(conn->inCursor + (size_t) msgLength,
119                                                                          conn))
120                         {
121                                 /*
122                                  * XXX add some better recovery code... plan is to skip over
123                                  * the message using its length, then report an error. For the
124                                  * moment, just treat this like loss of sync (which indeed it
125                                  * might be!)
126                                  */
127                                 handleSyncLoss(conn, id, msgLength);
128                         }
129                         return;
130                 }
131
132                 /*
133                  * NOTIFY and NOTICE messages can happen in any state; always process
134                  * them right away.
135                  *
136                  * Most other messages should only be processed while in BUSY state.
137                  * (In particular, in READY state we hold off further parsing until
138                  * the application collects the current PGresult.)
139                  *
140                  * However, if the state is IDLE then we got trouble; we need to deal
141                  * with the unexpected message somehow.
142                  *
143                  * ParameterStatus ('S') messages are a special case: in IDLE state we
144                  * must process 'em (this case could happen if a new value was adopted
145                  * from config file due to SIGHUP), but otherwise we hold off until
146                  * BUSY state.
147                  */
148                 if (id == 'A')
149                 {
150                         if (getNotify(conn))
151                                 return;
152                 }
153                 else if (id == 'N')
154                 {
155                         if (pqGetErrorNotice3(conn, false))
156                                 return;
157                 }
158                 else if (conn->asyncStatus != PGASYNC_BUSY)
159                 {
160                         /* If not IDLE state, just wait ... */
161                         if (conn->asyncStatus != PGASYNC_IDLE)
162                                 return;
163
164                         /*
165                          * Unexpected message in IDLE state; need to recover somehow.
166                          * ERROR messages are displayed using the notice processor;
167                          * ParameterStatus is handled normally; anything else is just
168                          * dropped on the floor after displaying a suitable warning
169                          * notice.      (An ERROR is very possibly the backend telling us why
170                          * it is about to close the connection, so we don't want to just
171                          * discard it...)
172                          */
173                         if (id == 'E')
174                         {
175                                 if (pqGetErrorNotice3(conn, false /* treat as notice */ ))
176                                         return;
177                         }
178                         else if (id == 'S')
179                         {
180                                 if (getParameterStatus(conn))
181                                         return;
182                         }
183                         else
184                         {
185                                 pqInternalNotice(&conn->noticeHooks,
186                                                 "message type 0x%02x arrived from server while idle",
187                                                                  id);
188                                 /* Discard the unexpected message */
189                                 conn->inCursor += msgLength;
190                         }
191                 }
192                 else
193                 {
194                         /*
195                          * In BUSY state, we can process everything.
196                          */
197                         switch (id)
198                         {
199                                 case 'C':               /* command complete */
200                                         if (pqGets(&conn->workBuffer, conn))
201                                                 return;
202                                         if (conn->result == NULL)
203                                         {
204                                                 conn->result = PQmakeEmptyPGresult(conn,
205                                                                                                                    PGRES_COMMAND_OK);
206                                                 if (!conn->result)
207                                                         return;
208                                         }
209                                         strncpy(conn->result->cmdStatus, conn->workBuffer.data,
210                                                         CMDSTATUS_LEN);
211                                         conn->asyncStatus = PGASYNC_READY;
212                                         break;
213                                 case 'E':               /* error return */
214                                         if (pqGetErrorNotice3(conn, true))
215                                                 return;
216                                         conn->asyncStatus = PGASYNC_READY;
217                                         break;
218                                 case 'Z':               /* backend is ready for new query */
219                                         if (getReadyForQuery(conn))
220                                                 return;
221                                         conn->asyncStatus = PGASYNC_IDLE;
222                                         break;
223                                 case 'I':               /* empty query */
224                                         if (conn->result == NULL)
225                                         {
226                                                 conn->result = PQmakeEmptyPGresult(conn,
227                                                                                                                    PGRES_EMPTY_QUERY);
228                                                 if (!conn->result)
229                                                         return;
230                                         }
231                                         conn->asyncStatus = PGASYNC_READY;
232                                         break;
233                                 case '1':               /* Parse Complete */
234                                         /* If we're doing PQprepare, we're done; else ignore */
235                                         if (conn->queryclass == PGQUERY_PREPARE)
236                                         {
237                                                 if (conn->result == NULL)
238                                                 {
239                                                         conn->result = PQmakeEmptyPGresult(conn,
240                                                                                                                    PGRES_COMMAND_OK);
241                                                         if (!conn->result)
242                                                                 return;
243                                                 }
244                                                 conn->asyncStatus = PGASYNC_READY;
245                                         }
246                                         break;
247                                 case '2':               /* Bind Complete */
248                                 case '3':               /* Close Complete */
249                                         /* Nothing to do for these message types */
250                                         break;
251                                 case 'S':               /* parameter status */
252                                         if (getParameterStatus(conn))
253                                                 return;
254                                         break;
255                                 case 'K':               /* secret key data from the backend */
256
257                                         /*
258                                          * This is expected only during backend startup, but it's
259                                          * just as easy to handle it as part of the main loop.
260                                          * Save the data and continue processing.
261                                          */
262                                         if (pqGetInt(&(conn->be_pid), 4, conn))
263                                                 return;
264                                         if (pqGetInt(&(conn->be_key), 4, conn))
265                                                 return;
266                                         break;
267                                 case 'T':               /* Row Description */
268                                         if (conn->result == NULL ||
269                                                 conn->queryclass == PGQUERY_DESCRIBE)
270                                         {
271                                                 /* First 'T' in a query sequence */
272                                                 if (getRowDescriptions(conn))
273                                                         return;
274
275                                                 /*
276                                                  * If we're doing a Describe, we're ready to pass the
277                                                  * result back to the client.
278                                                  */
279                                                 if (conn->queryclass == PGQUERY_DESCRIBE)
280                                                         conn->asyncStatus = PGASYNC_READY;
281                                         }
282                                         else
283                                         {
284                                                 /*
285                                                  * A new 'T' message is treated as the start of
286                                                  * another PGresult.  (It is not clear that this is
287                                                  * really possible with the current backend.) We stop
288                                                  * parsing until the application accepts the current
289                                                  * result.
290                                                  */
291                                                 conn->asyncStatus = PGASYNC_READY;
292                                                 return;
293                                         }
294                                         break;
295                                 case 'n':               /* No Data */
296
297                                         /*
298                                          * NoData indicates that we will not be seeing a
299                                          * RowDescription message because the statement or portal
300                                          * inquired about doesn't return rows.
301                                          *
302                                          * If we're doing a Describe, we have to pass something
303                                          * back to the client, so set up a COMMAND_OK result,
304                                          * instead of TUPLES_OK.  Otherwise we can just ignore
305                                          * this message.
306                                          */
307                                         if (conn->queryclass == PGQUERY_DESCRIBE)
308                                         {
309                                                 if (conn->result == NULL)
310                                                 {
311                                                         conn->result = PQmakeEmptyPGresult(conn,
312                                                                                                                    PGRES_COMMAND_OK);
313                                                         if (!conn->result)
314                                                                 return;
315                                                 }
316                                                 conn->asyncStatus = PGASYNC_READY;
317                                         }
318                                         break;
319                                 case 't':               /* Parameter Description */
320                                         if (getParamDescriptions(conn))
321                                                 return;
322                                         break;
323                                 case 'D':               /* Data Row */
324                                         if (conn->result != NULL &&
325                                                 conn->result->resultStatus == PGRES_TUPLES_OK)
326                                         {
327                                                 /* Read another tuple of a normal query response */
328                                                 if (getAnotherTuple(conn, msgLength))
329                                                         return;
330                                         }
331                                         else if (conn->result != NULL &&
332                                                          conn->result->resultStatus == PGRES_FATAL_ERROR)
333                                         {
334                                                 /*
335                                                  * We've already choked for some reason.  Just discard
336                                                  * tuples till we get to the end of the query.
337                                                  */
338                                                 conn->inCursor += msgLength;
339                                         }
340                                         else
341                                         {
342                                                 /* Set up to report error at end of query */
343                                                 printfPQExpBuffer(&conn->errorMessage,
344                                                                                   libpq_gettext("server sent data (\"D\" message) without prior row description (\"T\" message)\n"));
345                                                 pqSaveErrorResult(conn);
346                                                 /* Discard the unexpected message */
347                                                 conn->inCursor += msgLength;
348                                         }
349                                         break;
350                                 case 'G':               /* Start Copy In */
351                                         if (getCopyStart(conn, PGRES_COPY_IN))
352                                                 return;
353                                         conn->asyncStatus = PGASYNC_COPY_IN;
354                                         break;
355                                 case 'H':               /* Start Copy Out */
356                                         if (getCopyStart(conn, PGRES_COPY_OUT))
357                                                 return;
358                                         conn->asyncStatus = PGASYNC_COPY_OUT;
359                                         conn->copy_already_done = 0;
360                                         break;
361                                 case 'W':               /* Start Copy Both */
362                                         if (getCopyStart(conn, PGRES_COPY_BOTH))
363                                                 return;
364                                         conn->asyncStatus = PGASYNC_COPY_BOTH;
365                                         conn->copy_already_done = 0;
366                                         break;
367                                 case 'd':               /* Copy Data */
368
369                                         /*
370                                          * If we see Copy Data, just silently drop it.  This would
371                                          * only occur if application exits COPY OUT mode too
372                                          * early.
373                                          */
374                                         conn->inCursor += msgLength;
375                                         break;
376                                 case 'c':               /* Copy Done */
377
378                                         /*
379                                          * If we see Copy Done, just silently drop it.  This is
380                                          * the normal case during PQendcopy.  We will keep
381                                          * swallowing data, expecting to see command-complete for
382                                          * the COPY command.
383                                          */
384                                         break;
385                                 default:
386                                         printfPQExpBuffer(&conn->errorMessage,
387                                                                           libpq_gettext(
388                                                                                                         "unexpected response from server; first received character was \"%c\"\n"),
389                                                                           id);
390                                         /* build an error result holding the error message */
391                                         pqSaveErrorResult(conn);
392                                         /* not sure if we will see more, so go to ready state */
393                                         conn->asyncStatus = PGASYNC_READY;
394                                         /* Discard the unexpected message */
395                                         conn->inCursor += msgLength;
396                                         break;
397                         }                                       /* switch on protocol character */
398                 }
399                 /* Successfully consumed this message */
400                 if (conn->inCursor == conn->inStart + 5 + msgLength)
401                 {
402                         /* Normal case: parsing agrees with specified length */
403                         conn->inStart = conn->inCursor;
404                 }
405                 else
406                 {
407                         /* Trouble --- report it */
408                         printfPQExpBuffer(&conn->errorMessage,
409                                                           libpq_gettext("message contents do not agree with length in message type \"%c\"\n"),
410                                                           id);
411                         /* build an error result holding the error message */
412                         pqSaveErrorResult(conn);
413                         conn->asyncStatus = PGASYNC_READY;
414                         /* trust the specified message length as what to skip */
415                         conn->inStart += 5 + msgLength;
416                 }
417         }
418 }
419
420 /*
421  * handleSyncLoss: clean up after loss of message-boundary sync
422  *
423  * There isn't really a lot we can do here except abandon the connection.
424  */
425 static void
426 handleSyncLoss(PGconn *conn, char id, int msgLength)
427 {
428         printfPQExpBuffer(&conn->errorMessage,
429                                           libpq_gettext(
430         "lost synchronization with server: got message type \"%c\", length %d\n"),
431                                           id, msgLength);
432         /* build an error result holding the error message */
433         pqSaveErrorResult(conn);
434         conn->asyncStatus = PGASYNC_READY;      /* drop out of GetResult wait loop */
435
436         pqsecure_close(conn);
437         closesocket(conn->sock);
438         conn->sock = -1;
439         conn->status = CONNECTION_BAD;          /* No more connection to backend */
440 }
441
442 /*
443  * parseInput subroutine to read a 'T' (row descriptions) message.
444  * We'll build a new PGresult structure (unless called for a Describe
445  * command for a prepared statement) containing the attribute data.
446  * Returns: 0 if completed message, EOF if not enough data yet.
447  *
448  * Note that if we run out of data, we have to release the partially
449  * constructed PGresult, and rebuild it again next time.  Fortunately,
450  * that shouldn't happen often, since 'T' messages usually fit in a packet.
451  */
452 static int
453 getRowDescriptions(PGconn *conn)
454 {
455         PGresult   *result;
456         int                     nfields;
457         int                     i;
458
459         /*
460          * When doing Describe for a prepared statement, there'll already be a
461          * PGresult created by getParamDescriptions, and we should fill data into
462          * that.  Otherwise, create a new, empty PGresult.
463          */
464         if (conn->queryclass == PGQUERY_DESCRIBE)
465         {
466                 if (conn->result)
467                         result = conn->result;
468                 else
469                         result = PQmakeEmptyPGresult(conn, PGRES_COMMAND_OK);
470         }
471         else
472                 result = PQmakeEmptyPGresult(conn, PGRES_TUPLES_OK);
473         if (!result)
474                 goto failure;
475
476         /* parseInput already read the 'T' label and message length. */
477         /* the next two bytes are the number of fields */
478         if (pqGetInt(&(result->numAttributes), 2, conn))
479                 goto failure;
480         nfields = result->numAttributes;
481
482         /* allocate space for the attribute descriptors */
483         if (nfields > 0)
484         {
485                 result->attDescs = (PGresAttDesc *)
486                         pqResultAlloc(result, nfields * sizeof(PGresAttDesc), TRUE);
487                 if (!result->attDescs)
488                         goto failure;
489                 MemSet(result->attDescs, 0, nfields * sizeof(PGresAttDesc));
490         }
491
492         /* result->binary is true only if ALL columns are binary */
493         result->binary = (nfields > 0) ? 1 : 0;
494
495         /* get type info */
496         for (i = 0; i < nfields; i++)
497         {
498                 int                     tableid;
499                 int                     columnid;
500                 int                     typid;
501                 int                     typlen;
502                 int                     atttypmod;
503                 int                     format;
504
505                 if (pqGets(&conn->workBuffer, conn) ||
506                         pqGetInt(&tableid, 4, conn) ||
507                         pqGetInt(&columnid, 2, conn) ||
508                         pqGetInt(&typid, 4, conn) ||
509                         pqGetInt(&typlen, 2, conn) ||
510                         pqGetInt(&atttypmod, 4, conn) ||
511                         pqGetInt(&format, 2, conn))
512                 {
513                         goto failure;
514                 }
515
516                 /*
517                  * Since pqGetInt treats 2-byte integers as unsigned, we need to
518                  * coerce these results to signed form.
519                  */
520                 columnid = (int) ((int16) columnid);
521                 typlen = (int) ((int16) typlen);
522                 format = (int) ((int16) format);
523
524                 result->attDescs[i].name = pqResultStrdup(result,
525                                                                                                   conn->workBuffer.data);
526                 if (!result->attDescs[i].name)
527                         goto failure;
528                 result->attDescs[i].tableid = tableid;
529                 result->attDescs[i].columnid = columnid;
530                 result->attDescs[i].format = format;
531                 result->attDescs[i].typid = typid;
532                 result->attDescs[i].typlen = typlen;
533                 result->attDescs[i].atttypmod = atttypmod;
534
535                 if (format != 1)
536                         result->binary = 0;
537         }
538
539         /* Success! */
540         conn->result = result;
541         return 0;
542
543 failure:
544
545         /*
546          * Discard incomplete result, unless it's from getParamDescriptions.
547          *
548          * Note that if we hit a bufferload boundary while handling the
549          * describe-statement case, we'll forget any PGresult space we just
550          * allocated, and then reallocate it on next try.  This will bloat the
551          * PGresult a little bit but the space will be freed at PQclear, so it
552          * doesn't seem worth trying to be smarter.
553          */
554         if (result != conn->result)
555                 PQclear(result);
556         return EOF;
557 }
558
559 /*
560  * parseInput subroutine to read a 't' (ParameterDescription) message.
561  * We'll build a new PGresult structure containing the parameter data.
562  * Returns: 0 if completed message, EOF if not enough data yet.
563  *
564  * Note that if we run out of data, we have to release the partially
565  * constructed PGresult, and rebuild it again next time.  Fortunately,
566  * that shouldn't happen often, since 't' messages usually fit in a packet.
567  */
568 static int
569 getParamDescriptions(PGconn *conn)
570 {
571         PGresult   *result;
572         int                     nparams;
573         int                     i;
574
575         result = PQmakeEmptyPGresult(conn, PGRES_COMMAND_OK);
576         if (!result)
577                 goto failure;
578
579         /* parseInput already read the 't' label and message length. */
580         /* the next two bytes are the number of parameters */
581         if (pqGetInt(&(result->numParameters), 2, conn))
582                 goto failure;
583         nparams = result->numParameters;
584
585         /* allocate space for the parameter descriptors */
586         if (nparams > 0)
587         {
588                 result->paramDescs = (PGresParamDesc *)
589                         pqResultAlloc(result, nparams * sizeof(PGresParamDesc), TRUE);
590                 if (!result->paramDescs)
591                         goto failure;
592                 MemSet(result->paramDescs, 0, nparams * sizeof(PGresParamDesc));
593         }
594
595         /* get parameter info */
596         for (i = 0; i < nparams; i++)
597         {
598                 int                     typid;
599
600                 if (pqGetInt(&typid, 4, conn))
601                         goto failure;
602                 result->paramDescs[i].typid = typid;
603         }
604
605         /* Success! */
606         conn->result = result;
607         return 0;
608
609 failure:
610         PQclear(result);
611         return EOF;
612 }
613
614 /*
615  * parseInput subroutine to read a 'D' (row data) message.
616  * We add another tuple to the existing PGresult structure.
617  * Returns: 0 if completed message, EOF if error or not enough data yet.
618  *
619  * Note that if we run out of data, we have to suspend and reprocess
620  * the message after more data is received.  We keep a partially constructed
621  * tuple in conn->curTuple, and avoid reallocating already-allocated storage.
622  */
623 static int
624 getAnotherTuple(PGconn *conn, int msgLength)
625 {
626         PGresult   *result = conn->result;
627         int                     nfields = result->numAttributes;
628         PGresAttValue *tup;
629         int                     tupnfields;             /* # fields from tuple */
630         int                     vlen;                   /* length of the current field value */
631         int                     i;
632
633         /* Allocate tuple space if first time for this data message */
634         if (conn->curTuple == NULL)
635         {
636                 conn->curTuple = (PGresAttValue *)
637                         pqResultAlloc(result, nfields * sizeof(PGresAttValue), TRUE);
638                 if (conn->curTuple == NULL)
639                         goto outOfMemory;
640                 MemSet(conn->curTuple, 0, nfields * sizeof(PGresAttValue));
641         }
642         tup = conn->curTuple;
643
644         /* Get the field count and make sure it's what we expect */
645         if (pqGetInt(&tupnfields, 2, conn))
646                 return EOF;
647
648         if (tupnfields != nfields)
649         {
650                 /* Replace partially constructed result with an error result */
651                 printfPQExpBuffer(&conn->errorMessage,
652                                  libpq_gettext("unexpected field count in \"D\" message\n"));
653                 pqSaveErrorResult(conn);
654                 /* Discard the failed message by pretending we read it */
655                 conn->inCursor = conn->inStart + 5 + msgLength;
656                 return 0;
657         }
658
659         /* Scan the fields */
660         for (i = 0; i < nfields; i++)
661         {
662                 /* get the value length */
663                 if (pqGetInt(&vlen, 4, conn))
664                         return EOF;
665                 if (vlen == -1)
666                 {
667                         /* null field */
668                         tup[i].value = result->null_field;
669                         tup[i].len = NULL_LEN;
670                         continue;
671                 }
672                 if (vlen < 0)
673                         vlen = 0;
674                 if (tup[i].value == NULL)
675                 {
676                         bool            isbinary = (result->attDescs[i].format != 0);
677
678                         tup[i].value = (char *) pqResultAlloc(result, vlen + 1, isbinary);
679                         if (tup[i].value == NULL)
680                                 goto outOfMemory;
681                 }
682                 tup[i].len = vlen;
683                 /* read in the value */
684                 if (vlen > 0)
685                         if (pqGetnchar((char *) (tup[i].value), vlen, conn))
686                                 return EOF;
687                 /* we have to terminate this ourselves */
688                 tup[i].value[vlen] = '\0';
689         }
690
691         /* Success!  Store the completed tuple in the result */
692         if (!pqAddTuple(result, tup))
693                 goto outOfMemory;
694         /* and reset for a new message */
695         conn->curTuple = NULL;
696
697         return 0;
698
699 outOfMemory:
700
701         /*
702          * Replace partially constructed result with an error result. First
703          * discard the old result to try to win back some memory.
704          */
705         pqClearAsyncResult(conn);
706         printfPQExpBuffer(&conn->errorMessage,
707                                           libpq_gettext("out of memory for query result\n"));
708         pqSaveErrorResult(conn);
709
710         /* Discard the failed message by pretending we read it */
711         conn->inCursor = conn->inStart + 5 + msgLength;
712         return 0;
713 }
714
715
716 /*
717  * Attempt to read an Error or Notice response message.
718  * This is possible in several places, so we break it out as a subroutine.
719  * Entry: 'E' or 'N' message type and length have already been consumed.
720  * Exit: returns 0 if successfully consumed message.
721  *               returns EOF if not enough data.
722  */
723 int
724 pqGetErrorNotice3(PGconn *conn, bool isError)
725 {
726         PGresult   *res = NULL;
727         PQExpBufferData workBuf;
728         char            id;
729         const char *val;
730         const char *querytext = NULL;
731         int                     querypos = 0;
732
733         /*
734          * Since the fields might be pretty long, we create a temporary
735          * PQExpBuffer rather than using conn->workBuffer.      workBuffer is intended
736          * for stuff that is expected to be short.      We shouldn't use
737          * conn->errorMessage either, since this might be only a notice.
738          */
739         initPQExpBuffer(&workBuf);
740
741         /*
742          * Make a PGresult to hold the accumulated fields.      We temporarily lie
743          * about the result status, so that PQmakeEmptyPGresult doesn't uselessly
744          * copy conn->errorMessage.
745          */
746         res = PQmakeEmptyPGresult(conn, PGRES_EMPTY_QUERY);
747         if (!res)
748                 goto fail;
749         res->resultStatus = isError ? PGRES_FATAL_ERROR : PGRES_NONFATAL_ERROR;
750
751         /*
752          * Read the fields and save into res.
753          */
754         for (;;)
755         {
756                 if (pqGetc(&id, conn))
757                         goto fail;
758                 if (id == '\0')
759                         break;                          /* terminator found */
760                 if (pqGets(&workBuf, conn))
761                         goto fail;
762                 pqSaveMessageField(res, id, workBuf.data);
763         }
764
765         /*
766          * Now build the "overall" error message for PQresultErrorMessage.
767          *
768          * Also, save the SQLSTATE in conn->last_sqlstate.
769          */
770         resetPQExpBuffer(&workBuf);
771         val = PQresultErrorField(res, PG_DIAG_SEVERITY);
772         if (val)
773                 appendPQExpBuffer(&workBuf, "%s:  ", val);
774         val = PQresultErrorField(res, PG_DIAG_SQLSTATE);
775         if (val)
776         {
777                 if (strlen(val) < sizeof(conn->last_sqlstate))
778                         strcpy(conn->last_sqlstate, val);
779                 if (conn->verbosity == PQERRORS_VERBOSE)
780                         appendPQExpBuffer(&workBuf, "%s: ", val);
781         }
782         val = PQresultErrorField(res, PG_DIAG_MESSAGE_PRIMARY);
783         if (val)
784                 appendPQExpBufferStr(&workBuf, val);
785         val = PQresultErrorField(res, PG_DIAG_STATEMENT_POSITION);
786         if (val)
787         {
788                 if (conn->verbosity != PQERRORS_TERSE && conn->last_query != NULL)
789                 {
790                         /* emit position as a syntax cursor display */
791                         querytext = conn->last_query;
792                         querypos = atoi(val);
793                 }
794                 else
795                 {
796                         /* emit position as text addition to primary message */
797                         /* translator: %s represents a digit string */
798                         appendPQExpBuffer(&workBuf, libpq_gettext(" at character %s"),
799                                                           val);
800                 }
801         }
802         else
803         {
804                 val = PQresultErrorField(res, PG_DIAG_INTERNAL_POSITION);
805                 if (val)
806                 {
807                         querytext = PQresultErrorField(res, PG_DIAG_INTERNAL_QUERY);
808                         if (conn->verbosity != PQERRORS_TERSE && querytext != NULL)
809                         {
810                                 /* emit position as a syntax cursor display */
811                                 querypos = atoi(val);
812                         }
813                         else
814                         {
815                                 /* emit position as text addition to primary message */
816                                 /* translator: %s represents a digit string */
817                                 appendPQExpBuffer(&workBuf, libpq_gettext(" at character %s"),
818                                                                   val);
819                         }
820                 }
821         }
822         appendPQExpBufferChar(&workBuf, '\n');
823         if (conn->verbosity != PQERRORS_TERSE)
824         {
825                 if (querytext && querypos > 0)
826                         reportErrorPosition(&workBuf, querytext, querypos,
827                                                                 conn->client_encoding);
828                 val = PQresultErrorField(res, PG_DIAG_MESSAGE_DETAIL);
829                 if (val)
830                         appendPQExpBuffer(&workBuf, libpq_gettext("DETAIL:  %s\n"), val);
831                 val = PQresultErrorField(res, PG_DIAG_MESSAGE_HINT);
832                 if (val)
833                         appendPQExpBuffer(&workBuf, libpq_gettext("HINT:  %s\n"), val);
834                 val = PQresultErrorField(res, PG_DIAG_INTERNAL_QUERY);
835                 if (val)
836                         appendPQExpBuffer(&workBuf, libpq_gettext("QUERY:  %s\n"), val);
837                 val = PQresultErrorField(res, PG_DIAG_CONTEXT);
838                 if (val)
839                         appendPQExpBuffer(&workBuf, libpq_gettext("CONTEXT:  %s\n"), val);
840         }
841         if (conn->verbosity == PQERRORS_VERBOSE)
842         {
843                 const char *valf;
844                 const char *vall;
845
846                 valf = PQresultErrorField(res, PG_DIAG_SOURCE_FILE);
847                 vall = PQresultErrorField(res, PG_DIAG_SOURCE_LINE);
848                 val = PQresultErrorField(res, PG_DIAG_SOURCE_FUNCTION);
849                 if (val || valf || vall)
850                 {
851                         appendPQExpBufferStr(&workBuf, libpq_gettext("LOCATION:  "));
852                         if (val)
853                                 appendPQExpBuffer(&workBuf, libpq_gettext("%s, "), val);
854                         if (valf && vall)       /* unlikely we'd have just one */
855                                 appendPQExpBuffer(&workBuf, libpq_gettext("%s:%s"),
856                                                                   valf, vall);
857                         appendPQExpBufferChar(&workBuf, '\n');
858                 }
859         }
860
861         /*
862          * Either save error as current async result, or just emit the notice.
863          */
864         if (isError)
865         {
866                 res->errMsg = pqResultStrdup(res, workBuf.data);
867                 if (!res->errMsg)
868                         goto fail;
869                 pqClearAsyncResult(conn);
870                 conn->result = res;
871                 appendPQExpBufferStr(&conn->errorMessage, workBuf.data);
872         }
873         else
874         {
875                 /* We can cheat a little here and not copy the message. */
876                 res->errMsg = workBuf.data;
877                 if (res->noticeHooks.noticeRec != NULL)
878                         (*res->noticeHooks.noticeRec) (res->noticeHooks.noticeRecArg, res);
879                 PQclear(res);
880         }
881
882         termPQExpBuffer(&workBuf);
883         return 0;
884
885 fail:
886         PQclear(res);
887         termPQExpBuffer(&workBuf);
888         return EOF;
889 }
890
891 /*
892  * Add an error-location display to the error message under construction.
893  *
894  * The cursor location is measured in logical characters; the query string
895  * is presumed to be in the specified encoding.
896  */
897 static void
898 reportErrorPosition(PQExpBuffer msg, const char *query, int loc, int encoding)
899 {
900 #define DISPLAY_SIZE    60              /* screen width limit, in screen cols */
901 #define MIN_RIGHT_CUT   10              /* try to keep this far away from EOL */
902
903         char       *wquery;
904         int                     slen,
905                                 cno,
906                                 i,
907                            *qidx,
908                            *scridx,
909                                 qoffset,
910                                 scroffset,
911                                 ibeg,
912                                 iend,
913                                 loc_line;
914         bool            mb_encoding,
915                                 beg_trunc,
916                                 end_trunc;
917
918         /* Convert loc from 1-based to 0-based; no-op if out of range */
919         loc--;
920         if (loc < 0)
921                 return;
922
923         /* Need a writable copy of the query */
924         wquery = strdup(query);
925         if (wquery == NULL)
926                 return;                                 /* fail silently if out of memory */
927
928         /*
929          * Each character might occupy multiple physical bytes in the string, and
930          * in some Far Eastern character sets it might take more than one screen
931          * column as well.      We compute the starting byte offset and starting
932          * screen column of each logical character, and store these in qidx[] and
933          * scridx[] respectively.
934          */
935
936         /* we need a safe allocation size... */
937         slen = strlen(wquery) + 1;
938
939         qidx = (int *) malloc(slen * sizeof(int));
940         if (qidx == NULL)
941         {
942                 free(wquery);
943                 return;
944         }
945         scridx = (int *) malloc(slen * sizeof(int));
946         if (scridx == NULL)
947         {
948                 free(qidx);
949                 free(wquery);
950                 return;
951         }
952
953         /* We can optimize a bit if it's a single-byte encoding */
954         mb_encoding = (pg_encoding_max_length(encoding) != 1);
955
956         /*
957          * Within the scanning loop, cno is the current character's logical
958          * number, qoffset is its offset in wquery, and scroffset is its starting
959          * logical screen column (all indexed from 0).  "loc" is the logical
960          * character number of the error location.      We scan to determine loc_line
961          * (the 1-based line number containing loc) and ibeg/iend (first character
962          * number and last+1 character number of the line containing loc). Note
963          * that qidx[] and scridx[] are filled only as far as iend.
964          */
965         qoffset = 0;
966         scroffset = 0;
967         loc_line = 1;
968         ibeg = 0;
969         iend = -1;                                      /* -1 means not set yet */
970
971         for (cno = 0; wquery[qoffset] != '\0'; cno++)
972         {
973                 char            ch = wquery[qoffset];
974
975                 qidx[cno] = qoffset;
976                 scridx[cno] = scroffset;
977
978                 /*
979                  * Replace tabs with spaces in the writable copy.  (Later we might
980                  * want to think about coping with their variable screen width, but
981                  * not today.)
982                  */
983                 if (ch == '\t')
984                         wquery[qoffset] = ' ';
985
986                 /*
987                  * If end-of-line, count lines and mark positions. Each \r or \n
988                  * counts as a line except when \r \n appear together.
989                  */
990                 else if (ch == '\r' || ch == '\n')
991                 {
992                         if (cno < loc)
993                         {
994                                 if (ch == '\r' ||
995                                         cno == 0 ||
996                                         wquery[qidx[cno - 1]] != '\r')
997                                         loc_line++;
998                                 /* extract beginning = last line start before loc. */
999                                 ibeg = cno + 1;
1000                         }
1001                         else
1002                         {
1003                                 /* set extract end. */
1004                                 iend = cno;
1005                                 /* done scanning. */
1006                                 break;
1007                         }
1008                 }
1009
1010                 /* Advance */
1011                 if (mb_encoding)
1012                 {
1013                         int                     w;
1014
1015                         w = pg_encoding_dsplen(encoding, &wquery[qoffset]);
1016                         /* treat any non-tab control chars as width 1 */
1017                         if (w <= 0)
1018                                 w = 1;
1019                         scroffset += w;
1020                         qoffset += pg_encoding_mblen(encoding, &wquery[qoffset]);
1021                 }
1022                 else
1023                 {
1024                         /* We assume wide chars only exist in multibyte encodings */
1025                         scroffset++;
1026                         qoffset++;
1027                 }
1028         }
1029         /* Fix up if we didn't find an end-of-line after loc */
1030         if (iend < 0)
1031         {
1032                 iend = cno;                             /* query length in chars, +1 */
1033                 qidx[iend] = qoffset;
1034                 scridx[iend] = scroffset;
1035         }
1036
1037         /* Print only if loc is within computed query length */
1038         if (loc <= cno)
1039         {
1040                 /* If the line extracted is too long, we truncate it. */
1041                 beg_trunc = false;
1042                 end_trunc = false;
1043                 if (scridx[iend] - scridx[ibeg] > DISPLAY_SIZE)
1044                 {
1045                         /*
1046                          * We first truncate right if it is enough.  This code might be
1047                          * off a space or so on enforcing MIN_RIGHT_CUT if there's a wide
1048                          * character right there, but that should be okay.
1049                          */
1050                         if (scridx[ibeg] + DISPLAY_SIZE >= scridx[loc] + MIN_RIGHT_CUT)
1051                         {
1052                                 while (scridx[iend] - scridx[ibeg] > DISPLAY_SIZE)
1053                                         iend--;
1054                                 end_trunc = true;
1055                         }
1056                         else
1057                         {
1058                                 /* Truncate right if not too close to loc. */
1059                                 while (scridx[loc] + MIN_RIGHT_CUT < scridx[iend])
1060                                 {
1061                                         iend--;
1062                                         end_trunc = true;
1063                                 }
1064
1065                                 /* Truncate left if still too long. */
1066                                 while (scridx[iend] - scridx[ibeg] > DISPLAY_SIZE)
1067                                 {
1068                                         ibeg++;
1069                                         beg_trunc = true;
1070                                 }
1071                         }
1072                 }
1073
1074                 /* truncate working copy at desired endpoint */
1075                 wquery[qidx[iend]] = '\0';
1076
1077                 /* Begin building the finished message. */
1078                 i = msg->len;
1079                 appendPQExpBuffer(msg, libpq_gettext("LINE %d: "), loc_line);
1080                 if (beg_trunc)
1081                         appendPQExpBufferStr(msg, "...");
1082
1083                 /*
1084                  * While we have the prefix in the msg buffer, compute its screen
1085                  * width.
1086                  */
1087                 scroffset = 0;
1088                 for (; i < msg->len; i += pg_encoding_mblen(encoding, &msg->data[i]))
1089                 {
1090                         int                     w = pg_encoding_dsplen(encoding, &msg->data[i]);
1091
1092                         if (w <= 0)
1093                                 w = 1;
1094                         scroffset += w;
1095                 }
1096
1097                 /* Finish up the LINE message line. */
1098                 appendPQExpBufferStr(msg, &wquery[qidx[ibeg]]);
1099                 if (end_trunc)
1100                         appendPQExpBufferStr(msg, "...");
1101                 appendPQExpBufferChar(msg, '\n');
1102
1103                 /* Now emit the cursor marker line. */
1104                 scroffset += scridx[loc] - scridx[ibeg];
1105                 for (i = 0; i < scroffset; i++)
1106                         appendPQExpBufferChar(msg, ' ');
1107                 appendPQExpBufferChar(msg, '^');
1108                 appendPQExpBufferChar(msg, '\n');
1109         }
1110
1111         /* Clean up. */
1112         free(scridx);
1113         free(qidx);
1114         free(wquery);
1115 }
1116
1117
1118 /*
1119  * Attempt to read a ParameterStatus message.
1120  * This is possible in several places, so we break it out as a subroutine.
1121  * Entry: 'S' message type and length have already been consumed.
1122  * Exit: returns 0 if successfully consumed message.
1123  *               returns EOF if not enough data.
1124  */
1125 static int
1126 getParameterStatus(PGconn *conn)
1127 {
1128         PQExpBufferData valueBuf;
1129
1130         /* Get the parameter name */
1131         if (pqGets(&conn->workBuffer, conn))
1132                 return EOF;
1133         /* Get the parameter value (could be large) */
1134         initPQExpBuffer(&valueBuf);
1135         if (pqGets(&valueBuf, conn))
1136         {
1137                 termPQExpBuffer(&valueBuf);
1138                 return EOF;
1139         }
1140         /* And save it */
1141         pqSaveParameterStatus(conn, conn->workBuffer.data, valueBuf.data);
1142         termPQExpBuffer(&valueBuf);
1143         return 0;
1144 }
1145
1146
1147 /*
1148  * Attempt to read a Notify response message.
1149  * This is possible in several places, so we break it out as a subroutine.
1150  * Entry: 'A' message type and length have already been consumed.
1151  * Exit: returns 0 if successfully consumed Notify message.
1152  *               returns EOF if not enough data.
1153  */
1154 static int
1155 getNotify(PGconn *conn)
1156 {
1157         int                     be_pid;
1158         char       *svname;
1159         int                     nmlen;
1160         int                     extralen;
1161         PGnotify   *newNotify;
1162
1163         if (pqGetInt(&be_pid, 4, conn))
1164                 return EOF;
1165         if (pqGets(&conn->workBuffer, conn))
1166                 return EOF;
1167         /* must save name while getting extra string */
1168         svname = strdup(conn->workBuffer.data);
1169         if (!svname)
1170                 return EOF;
1171         if (pqGets(&conn->workBuffer, conn))
1172         {
1173                 free(svname);
1174                 return EOF;
1175         }
1176
1177         /*
1178          * Store the strings right after the PQnotify structure so it can all be
1179          * freed at once.  We don't use NAMEDATALEN because we don't want to tie
1180          * this interface to a specific server name length.
1181          */
1182         nmlen = strlen(svname);
1183         extralen = strlen(conn->workBuffer.data);
1184         newNotify = (PGnotify *) malloc(sizeof(PGnotify) + nmlen + extralen + 2);
1185         if (newNotify)
1186         {
1187                 newNotify->relname = (char *) newNotify + sizeof(PGnotify);
1188                 strcpy(newNotify->relname, svname);
1189                 newNotify->extra = newNotify->relname + nmlen + 1;
1190                 strcpy(newNotify->extra, conn->workBuffer.data);
1191                 newNotify->be_pid = be_pid;
1192                 newNotify->next = NULL;
1193                 if (conn->notifyTail)
1194                         conn->notifyTail->next = newNotify;
1195                 else
1196                         conn->notifyHead = newNotify;
1197                 conn->notifyTail = newNotify;
1198         }
1199
1200         free(svname);
1201         return 0;
1202 }
1203
1204 /*
1205  * getCopyStart - process CopyInResponse, CopyOutResponse or
1206  * CopyBothResponse message
1207  *
1208  * parseInput already read the message type and length.
1209  */
1210 static int
1211 getCopyStart(PGconn *conn, ExecStatusType copytype)
1212 {
1213         PGresult   *result;
1214         int                     nfields;
1215         int                     i;
1216
1217         result = PQmakeEmptyPGresult(conn, copytype);
1218         if (!result)
1219                 goto failure;
1220
1221         if (pqGetc(&conn->copy_is_binary, conn))
1222                 goto failure;
1223         result->binary = conn->copy_is_binary;
1224         /* the next two bytes are the number of fields  */
1225         if (pqGetInt(&(result->numAttributes), 2, conn))
1226                 goto failure;
1227         nfields = result->numAttributes;
1228
1229         /* allocate space for the attribute descriptors */
1230         if (nfields > 0)
1231         {
1232                 result->attDescs = (PGresAttDesc *)
1233                         pqResultAlloc(result, nfields * sizeof(PGresAttDesc), TRUE);
1234                 if (!result->attDescs)
1235                         goto failure;
1236                 MemSet(result->attDescs, 0, nfields * sizeof(PGresAttDesc));
1237         }
1238
1239         for (i = 0; i < nfields; i++)
1240         {
1241                 int                     format;
1242
1243                 if (pqGetInt(&format, 2, conn))
1244                         goto failure;
1245
1246                 /*
1247                  * Since pqGetInt treats 2-byte integers as unsigned, we need to
1248                  * coerce these results to signed form.
1249                  */
1250                 format = (int) ((int16) format);
1251                 result->attDescs[i].format = format;
1252         }
1253
1254         /* Success! */
1255         conn->result = result;
1256         return 0;
1257
1258 failure:
1259         PQclear(result);
1260         return EOF;
1261 }
1262
1263 /*
1264  * getReadyForQuery - process ReadyForQuery message
1265  */
1266 static int
1267 getReadyForQuery(PGconn *conn)
1268 {
1269         char            xact_status;
1270
1271         if (pqGetc(&xact_status, conn))
1272                 return EOF;
1273         switch (xact_status)
1274         {
1275                 case 'I':
1276                         conn->xactStatus = PQTRANS_IDLE;
1277                         break;
1278                 case 'T':
1279                         conn->xactStatus = PQTRANS_INTRANS;
1280                         break;
1281                 case 'E':
1282                         conn->xactStatus = PQTRANS_INERROR;
1283                         break;
1284                 default:
1285                         conn->xactStatus = PQTRANS_UNKNOWN;
1286                         break;
1287         }
1288
1289         return 0;
1290 }
1291
1292 /*
1293  * getCopyDataMessage - fetch next CopyData message, process async messages
1294  *
1295  * Returns length word of CopyData message (> 0), or 0 if no complete
1296  * message available, -1 if end of copy, -2 if error.
1297  */
1298 static int
1299 getCopyDataMessage(PGconn *conn)
1300 {
1301         char            id;
1302         int                     msgLength;
1303         int                     avail;
1304
1305         for (;;)
1306         {
1307                 /*
1308                  * Do we have the next input message?  To make life simpler for async
1309                  * callers, we keep returning 0 until the next message is fully
1310                  * available, even if it is not Copy Data.
1311                  */
1312                 conn->inCursor = conn->inStart;
1313                 if (pqGetc(&id, conn))
1314                         return 0;
1315                 if (pqGetInt(&msgLength, 4, conn))
1316                         return 0;
1317                 if (msgLength < 4)
1318                 {
1319                         handleSyncLoss(conn, id, msgLength);
1320                         return -2;
1321                 }
1322                 avail = conn->inEnd - conn->inCursor;
1323                 if (avail < msgLength - 4)
1324                 {
1325                         /*
1326                          * Before returning, enlarge the input buffer if needed to hold
1327                          * the whole message.  See notes in parseInput.
1328                          */
1329                         if (pqCheckInBufferSpace(conn->inCursor + (size_t) msgLength - 4,
1330                                                                          conn))
1331                         {
1332                                 /*
1333                                  * XXX add some better recovery code... plan is to skip over
1334                                  * the message using its length, then report an error. For the
1335                                  * moment, just treat this like loss of sync (which indeed it
1336                                  * might be!)
1337                                  */
1338                                 handleSyncLoss(conn, id, msgLength);
1339                                 return -2;
1340                         }
1341                         return 0;
1342                 }
1343
1344                 /*
1345                  * If it's a legitimate async message type, process it.  (NOTIFY
1346                  * messages are not currently possible here, but we handle them for
1347                  * completeness.)  Otherwise, if it's anything except Copy Data,
1348                  * report end-of-copy.
1349                  */
1350                 switch (id)
1351                 {
1352                         case 'A':                       /* NOTIFY */
1353                                 if (getNotify(conn))
1354                                         return 0;
1355                                 break;
1356                         case 'N':                       /* NOTICE */
1357                                 if (pqGetErrorNotice3(conn, false))
1358                                         return 0;
1359                                 break;
1360                         case 'S':                       /* ParameterStatus */
1361                                 if (getParameterStatus(conn))
1362                                         return 0;
1363                                 break;
1364                         case 'd':                       /* Copy Data, pass it back to caller */
1365                                 return msgLength;
1366                         default:                        /* treat as end of copy */
1367                                 return -1;
1368                 }
1369
1370                 /* Drop the processed message and loop around for another */
1371                 conn->inStart = conn->inCursor;
1372         }
1373 }
1374
1375 /*
1376  * PQgetCopyData - read a row of data from the backend during COPY OUT
1377  * or COPY BOTH
1378  *
1379  * If successful, sets *buffer to point to a malloc'd row of data, and
1380  * returns row length (always > 0) as result.
1381  * Returns 0 if no row available yet (only possible if async is true),
1382  * -1 if end of copy (consult PQgetResult), or -2 if error (consult
1383  * PQerrorMessage).
1384  */
1385 int
1386 pqGetCopyData3(PGconn *conn, char **buffer, int async)
1387 {
1388         int                     msgLength;
1389
1390         for (;;)
1391         {
1392                 /*
1393                  * Collect the next input message.      To make life simpler for async
1394                  * callers, we keep returning 0 until the next message is fully
1395                  * available, even if it is not Copy Data.
1396                  */
1397                 msgLength = getCopyDataMessage(conn);
1398                 if (msgLength < 0)
1399                 {
1400                         /*
1401                          * On end-of-copy, exit COPY_OUT or COPY_BOTH mode and let caller
1402                          * read status with PQgetResult().      The normal case is that it's
1403                          * Copy Done, but we let parseInput read that.  If error, we expect
1404                          * the state was already changed.
1405                          */
1406                         if (msgLength == -1)
1407                                 conn->asyncStatus = PGASYNC_BUSY;
1408                         return msgLength;       /* end-of-copy or error */
1409                 }
1410                 if (msgLength == 0)
1411                 {
1412                         /* Don't block if async read requested */
1413                         if (async)
1414                                 return 0;
1415                         /* Need to load more data */
1416                         if (pqWait(TRUE, FALSE, conn) ||
1417                                 pqReadData(conn) < 0)
1418                                 return -2;
1419                         continue;
1420                 }
1421
1422                 /*
1423                  * Drop zero-length messages (shouldn't happen anyway).  Otherwise
1424                  * pass the data back to the caller.
1425                  */
1426                 msgLength -= 4;
1427                 if (msgLength > 0)
1428                 {
1429                         *buffer = (char *) malloc(msgLength + 1);
1430                         if (*buffer == NULL)
1431                         {
1432                                 printfPQExpBuffer(&conn->errorMessage,
1433                                                                   libpq_gettext("out of memory\n"));
1434                                 return -2;
1435                         }
1436                         memcpy(*buffer, &conn->inBuffer[conn->inCursor], msgLength);
1437                         (*buffer)[msgLength] = '\0';            /* Add terminating null */
1438
1439                         /* Mark message consumed */
1440                         conn->inStart = conn->inCursor + msgLength;
1441
1442                         return msgLength;
1443                 }
1444
1445                 /* Empty, so drop it and loop around for another */
1446                 conn->inStart = conn->inCursor;
1447         }
1448 }
1449
1450 /*
1451  * PQgetline - gets a newline-terminated string from the backend.
1452  *
1453  * See fe-exec.c for documentation.
1454  */
1455 int
1456 pqGetline3(PGconn *conn, char *s, int maxlen)
1457 {
1458         int                     status;
1459
1460         if (conn->sock < 0 ||
1461                 conn->asyncStatus != PGASYNC_COPY_OUT ||
1462                 conn->copy_is_binary)
1463         {
1464                 printfPQExpBuffer(&conn->errorMessage,
1465                                           libpq_gettext("PQgetline: not doing text COPY OUT\n"));
1466                 *s = '\0';
1467                 return EOF;
1468         }
1469
1470         while ((status = PQgetlineAsync(conn, s, maxlen - 1)) == 0)
1471         {
1472                 /* need to load more data */
1473                 if (pqWait(TRUE, FALSE, conn) ||
1474                         pqReadData(conn) < 0)
1475                 {
1476                         *s = '\0';
1477                         return EOF;
1478                 }
1479         }
1480
1481         if (status < 0)
1482         {
1483                 /* End of copy detected; gin up old-style terminator */
1484                 strcpy(s, "\\.");
1485                 return 0;
1486         }
1487
1488         /* Add null terminator, and strip trailing \n if present */
1489         if (s[status - 1] == '\n')
1490         {
1491                 s[status - 1] = '\0';
1492                 return 0;
1493         }
1494         else
1495         {
1496                 s[status] = '\0';
1497                 return 1;
1498         }
1499 }
1500
1501 /*
1502  * PQgetlineAsync - gets a COPY data row without blocking.
1503  *
1504  * See fe-exec.c for documentation.
1505  */
1506 int
1507 pqGetlineAsync3(PGconn *conn, char *buffer, int bufsize)
1508 {
1509         int                     msgLength;
1510         int                     avail;
1511
1512         if (conn->asyncStatus != PGASYNC_COPY_OUT)
1513                 return -1;                              /* we are not doing a copy... */
1514
1515         /*
1516          * Recognize the next input message.  To make life simpler for async
1517          * callers, we keep returning 0 until the next message is fully available
1518          * even if it is not Copy Data.  This should keep PQendcopy from blocking.
1519          * (Note: unlike pqGetCopyData3, we do not change asyncStatus here.)
1520          */
1521         msgLength = getCopyDataMessage(conn);
1522         if (msgLength < 0)
1523                 return -1;                              /* end-of-copy or error */
1524         if (msgLength == 0)
1525                 return 0;                               /* no data yet */
1526
1527         /*
1528          * Move data from libpq's buffer to the caller's.  In the case where a
1529          * prior call found the caller's buffer too small, we use
1530          * conn->copy_already_done to remember how much of the row was already
1531          * returned to the caller.
1532          */
1533         conn->inCursor += conn->copy_already_done;
1534         avail = msgLength - 4 - conn->copy_already_done;
1535         if (avail <= bufsize)
1536         {
1537                 /* Able to consume the whole message */
1538                 memcpy(buffer, &conn->inBuffer[conn->inCursor], avail);
1539                 /* Mark message consumed */
1540                 conn->inStart = conn->inCursor + avail;
1541                 /* Reset state for next time */
1542                 conn->copy_already_done = 0;
1543                 return avail;
1544         }
1545         else
1546         {
1547                 /* We must return a partial message */
1548                 memcpy(buffer, &conn->inBuffer[conn->inCursor], bufsize);
1549                 /* The message is NOT consumed from libpq's buffer */
1550                 conn->copy_already_done += bufsize;
1551                 return bufsize;
1552         }
1553 }
1554
1555 /*
1556  * PQendcopy
1557  *
1558  * See fe-exec.c for documentation.
1559  */
1560 int
1561 pqEndcopy3(PGconn *conn)
1562 {
1563         PGresult   *result;
1564
1565         if (conn->asyncStatus != PGASYNC_COPY_IN &&
1566                 conn->asyncStatus != PGASYNC_COPY_OUT)
1567         {
1568                 printfPQExpBuffer(&conn->errorMessage,
1569                                                   libpq_gettext("no COPY in progress\n"));
1570                 return 1;
1571         }
1572
1573         /* Send the CopyDone message if needed */
1574         if (conn->asyncStatus == PGASYNC_COPY_IN)
1575         {
1576                 if (pqPutMsgStart('c', false, conn) < 0 ||
1577                         pqPutMsgEnd(conn) < 0)
1578                         return 1;
1579
1580                 /*
1581                  * If we sent the COPY command in extended-query mode, we must issue a
1582                  * Sync as well.
1583                  */
1584                 if (conn->queryclass != PGQUERY_SIMPLE)
1585                 {
1586                         if (pqPutMsgStart('S', false, conn) < 0 ||
1587                                 pqPutMsgEnd(conn) < 0)
1588                                 return 1;
1589                 }
1590         }
1591
1592         /*
1593          * make sure no data is waiting to be sent, abort if we are non-blocking
1594          * and the flush fails
1595          */
1596         if (pqFlush(conn) && pqIsnonblocking(conn))
1597                 return 1;
1598
1599         /* Return to active duty */
1600         conn->asyncStatus = PGASYNC_BUSY;
1601         resetPQExpBuffer(&conn->errorMessage);
1602
1603         /*
1604          * Non blocking connections may have to abort at this point.  If everyone
1605          * played the game there should be no problem, but in error scenarios the
1606          * expected messages may not have arrived yet.  (We are assuming that the
1607          * backend's packetizing will ensure that CommandComplete arrives along
1608          * with the CopyDone; are there corner cases where that doesn't happen?)
1609          */
1610         if (pqIsnonblocking(conn) && PQisBusy(conn))
1611                 return 1;
1612
1613         /* Wait for the completion response */
1614         result = PQgetResult(conn);
1615
1616         /* Expecting a successful result */
1617         if (result && result->resultStatus == PGRES_COMMAND_OK)
1618         {
1619                 PQclear(result);
1620                 return 0;
1621         }
1622
1623         /*
1624          * Trouble. For backwards-compatibility reasons, we issue the error
1625          * message as if it were a notice (would be nice to get rid of this
1626          * silliness, but too many apps probably don't handle errors from
1627          * PQendcopy reasonably).  Note that the app can still obtain the error
1628          * status from the PGconn object.
1629          */
1630         if (conn->errorMessage.len > 0)
1631         {
1632                 /* We have to strip the trailing newline ... pain in neck... */
1633                 char            svLast = conn->errorMessage.data[conn->errorMessage.len - 1];
1634
1635                 if (svLast == '\n')
1636                         conn->errorMessage.data[conn->errorMessage.len - 1] = '\0';
1637                 pqInternalNotice(&conn->noticeHooks, "%s", conn->errorMessage.data);
1638                 conn->errorMessage.data[conn->errorMessage.len - 1] = svLast;
1639         }
1640
1641         PQclear(result);
1642
1643         return 1;
1644 }
1645
1646
1647 /*
1648  * PQfn - Send a function call to the POSTGRES backend.
1649  *
1650  * See fe-exec.c for documentation.
1651  */
1652 PGresult *
1653 pqFunctionCall3(PGconn *conn, Oid fnid,
1654                                 int *result_buf, int *actual_result_len,
1655                                 int result_is_int,
1656                                 const PQArgBlock *args, int nargs)
1657 {
1658         bool            needInput = false;
1659         ExecStatusType status = PGRES_FATAL_ERROR;
1660         char            id;
1661         int                     msgLength;
1662         int                     avail;
1663         int                     i;
1664
1665         /* PQfn already validated connection state */
1666
1667         if (pqPutMsgStart('F', false, conn) < 0 ||      /* function call msg */
1668                 pqPutInt(fnid, 4, conn) < 0 ||  /* function id */
1669                 pqPutInt(1, 2, conn) < 0 ||             /* # of format codes */
1670                 pqPutInt(1, 2, conn) < 0 ||             /* format code: BINARY */
1671                 pqPutInt(nargs, 2, conn) < 0)   /* # of args */
1672         {
1673                 pqHandleSendFailure(conn);
1674                 return NULL;
1675         }
1676
1677         for (i = 0; i < nargs; ++i)
1678         {                                                       /* len.int4 + contents     */
1679                 if (pqPutInt(args[i].len, 4, conn))
1680                 {
1681                         pqHandleSendFailure(conn);
1682                         return NULL;
1683                 }
1684                 if (args[i].len == -1)
1685                         continue;                       /* it's NULL */
1686
1687                 if (args[i].isint)
1688                 {
1689                         if (pqPutInt(args[i].u.integer, args[i].len, conn))
1690                         {
1691                                 pqHandleSendFailure(conn);
1692                                 return NULL;
1693                         }
1694                 }
1695                 else
1696                 {
1697                         if (pqPutnchar((char *) args[i].u.ptr, args[i].len, conn))
1698                         {
1699                                 pqHandleSendFailure(conn);
1700                                 return NULL;
1701                         }
1702                 }
1703         }
1704
1705         if (pqPutInt(1, 2, conn) < 0)           /* result format code: BINARY */
1706         {
1707                 pqHandleSendFailure(conn);
1708                 return NULL;
1709         }
1710
1711         if (pqPutMsgEnd(conn) < 0 ||
1712                 pqFlush(conn))
1713         {
1714                 pqHandleSendFailure(conn);
1715                 return NULL;
1716         }
1717
1718         for (;;)
1719         {
1720                 if (needInput)
1721                 {
1722                         /* Wait for some data to arrive (or for the channel to close) */
1723                         if (pqWait(TRUE, FALSE, conn) ||
1724                                 pqReadData(conn) < 0)
1725                                 break;
1726                 }
1727
1728                 /*
1729                  * Scan the message. If we run out of data, loop around to try again.
1730                  */
1731                 needInput = true;
1732
1733                 conn->inCursor = conn->inStart;
1734                 if (pqGetc(&id, conn))
1735                         continue;
1736                 if (pqGetInt(&msgLength, 4, conn))
1737                         continue;
1738
1739                 /*
1740                  * Try to validate message type/length here.  A length less than 4 is
1741                  * definitely broken.  Large lengths should only be believed for a few
1742                  * message types.
1743                  */
1744                 if (msgLength < 4)
1745                 {
1746                         handleSyncLoss(conn, id, msgLength);
1747                         break;
1748                 }
1749                 if (msgLength > 30000 && !VALID_LONG_MESSAGE_TYPE(id))
1750                 {
1751                         handleSyncLoss(conn, id, msgLength);
1752                         break;
1753                 }
1754
1755                 /*
1756                  * Can't process if message body isn't all here yet.
1757                  */
1758                 msgLength -= 4;
1759                 avail = conn->inEnd - conn->inCursor;
1760                 if (avail < msgLength)
1761                 {
1762                         /*
1763                          * Before looping, enlarge the input buffer if needed to hold the
1764                          * whole message.  See notes in parseInput.
1765                          */
1766                         if (pqCheckInBufferSpace(conn->inCursor + (size_t) msgLength,
1767                                                                          conn))
1768                         {
1769                                 /*
1770                                  * XXX add some better recovery code... plan is to skip over
1771                                  * the message using its length, then report an error. For the
1772                                  * moment, just treat this like loss of sync (which indeed it
1773                                  * might be!)
1774                                  */
1775                                 handleSyncLoss(conn, id, msgLength);
1776                                 break;
1777                         }
1778                         continue;
1779                 }
1780
1781                 /*
1782                  * We should see V or E response to the command, but might get N
1783                  * and/or A notices first. We also need to swallow the final Z before
1784                  * returning.
1785                  */
1786                 switch (id)
1787                 {
1788                         case 'V':                       /* function result */
1789                                 if (pqGetInt(actual_result_len, 4, conn))
1790                                         continue;
1791                                 if (*actual_result_len != -1)
1792                                 {
1793                                         if (result_is_int)
1794                                         {
1795                                                 if (pqGetInt(result_buf, *actual_result_len, conn))
1796                                                         continue;
1797                                         }
1798                                         else
1799                                         {
1800                                                 if (pqGetnchar((char *) result_buf,
1801                                                                            *actual_result_len,
1802                                                                            conn))
1803                                                         continue;
1804                                         }
1805                                 }
1806                                 /* correctly finished function result message */
1807                                 status = PGRES_COMMAND_OK;
1808                                 break;
1809                         case 'E':                       /* error return */
1810                                 if (pqGetErrorNotice3(conn, true))
1811                                         continue;
1812                                 status = PGRES_FATAL_ERROR;
1813                                 break;
1814                         case 'A':                       /* notify message */
1815                                 /* handle notify and go back to processing return values */
1816                                 if (getNotify(conn))
1817                                         continue;
1818                                 break;
1819                         case 'N':                       /* notice */
1820                                 /* handle notice and go back to processing return values */
1821                                 if (pqGetErrorNotice3(conn, false))
1822                                         continue;
1823                                 break;
1824                         case 'Z':                       /* backend is ready for new query */
1825                                 if (getReadyForQuery(conn))
1826                                         continue;
1827                                 /* consume the message and exit */
1828                                 conn->inStart += 5 + msgLength;
1829                                 /* if we saved a result object (probably an error), use it */
1830                                 if (conn->result)
1831                                         return pqPrepareAsyncResult(conn);
1832                                 return PQmakeEmptyPGresult(conn, status);
1833                         case 'S':                       /* parameter status */
1834                                 if (getParameterStatus(conn))
1835                                         continue;
1836                                 break;
1837                         default:
1838                                 /* The backend violates the protocol. */
1839                                 printfPQExpBuffer(&conn->errorMessage,
1840                                                                   libpq_gettext("protocol error: id=0x%x\n"),
1841                                                                   id);
1842                                 pqSaveErrorResult(conn);
1843                                 /* trust the specified message length as what to skip */
1844                                 conn->inStart += 5 + msgLength;
1845                                 return pqPrepareAsyncResult(conn);
1846                 }
1847                 /* Completed this message, keep going */
1848                 /* trust the specified message length as what to skip */
1849                 conn->inStart += 5 + msgLength;
1850                 needInput = false;
1851         }
1852
1853         /*
1854          * We fall out of the loop only upon failing to read data.
1855          * conn->errorMessage has been set by pqWait or pqReadData. We want to
1856          * append it to any already-received error message.
1857          */
1858         pqSaveErrorResult(conn);
1859         return pqPrepareAsyncResult(conn);
1860 }
1861
1862
1863 /*
1864  * Construct startup packet
1865  *
1866  * Returns a malloc'd packet buffer, or NULL if out of memory
1867  */
1868 char *
1869 pqBuildStartupPacket3(PGconn *conn, int *packetlen,
1870                                           const PQEnvironmentOption *options)
1871 {
1872         char       *startpacket;
1873
1874         *packetlen = build_startup_packet(conn, NULL, options);
1875         startpacket = (char *) malloc(*packetlen);
1876         if (!startpacket)
1877                 return NULL;
1878         *packetlen = build_startup_packet(conn, startpacket, options);
1879         return startpacket;
1880 }
1881
1882 /*
1883  * Build a startup packet given a filled-in PGconn structure.
1884  *
1885  * We need to figure out how much space is needed, then fill it in.
1886  * To avoid duplicate logic, this routine is called twice: the first time
1887  * (with packet == NULL) just counts the space needed, the second time
1888  * (with packet == allocated space) fills it in.  Return value is the number
1889  * of bytes used.
1890  */
1891 static int
1892 build_startup_packet(const PGconn *conn, char *packet,
1893                                          const PQEnvironmentOption *options)
1894 {
1895         int                     packet_len = 0;
1896         const PQEnvironmentOption *next_eo;
1897         const char *val;
1898
1899         /* Protocol version comes first. */
1900         if (packet)
1901         {
1902                 ProtocolVersion pv = htonl(conn->pversion);
1903
1904                 memcpy(packet + packet_len, &pv, sizeof(ProtocolVersion));
1905         }
1906         packet_len += sizeof(ProtocolVersion);
1907
1908         /* Add user name, database name, options */
1909
1910 #define ADD_STARTUP_OPTION(optname, optval) \
1911         do { \
1912                 if (packet) \
1913                         strcpy(packet + packet_len, optname); \
1914                 packet_len += strlen(optname) + 1; \
1915                 if (packet) \
1916                         strcpy(packet + packet_len, optval); \
1917                 packet_len += strlen(optval) + 1; \
1918         } while(0)
1919
1920         if (conn->pguser && conn->pguser[0])
1921                 ADD_STARTUP_OPTION("user", conn->pguser);
1922         if (conn->dbName && conn->dbName[0])
1923                 ADD_STARTUP_OPTION("database", conn->dbName);
1924         if (conn->replication && conn->replication[0])
1925                 ADD_STARTUP_OPTION("replication", conn->replication);
1926         if (conn->pgoptions && conn->pgoptions[0])
1927                 ADD_STARTUP_OPTION("options", conn->pgoptions);
1928         if (conn->send_appname)
1929         {
1930                 /* Use appname if present, otherwise use fallback */
1931                 val = conn->appname ? conn->appname : conn->fbappname;
1932                 if (val && val[0])
1933                         ADD_STARTUP_OPTION("application_name", val);
1934         }
1935
1936         if (conn->client_encoding_initial && conn->client_encoding_initial[0])
1937                 ADD_STARTUP_OPTION("client_encoding", conn->client_encoding_initial);
1938
1939         /* Add any environment-driven GUC settings needed */
1940         for (next_eo = options; next_eo->envName; next_eo++)
1941         {
1942                 if ((val = getenv(next_eo->envName)) != NULL)
1943                 {
1944                         if (pg_strcasecmp(val, "default") != 0)
1945                                 ADD_STARTUP_OPTION(next_eo->pgName, val);
1946                 }
1947         }
1948
1949         /* Add trailing terminator */
1950         if (packet)
1951                 packet[packet_len] = '\0';
1952         packet_len++;
1953
1954         return packet_len;
1955 }