*
* Copyright (c) 1994, Regents of the University of California
*
- * $Id: pqcomm.c,v 1.81 1999/07/23 03:00:10 tgl Exp $
+ * $Id: pqcomm.c,v 1.82 1999/08/31 04:26:37 tgl Exp $
*
*-------------------------------------------------------------------------
*/
/* --------------------------------
* pq_getstring - get a null terminated string from connection
*
+ * The return value is placed in an expansible StringInfo.
+ * Note that space allocation comes from the current memory context!
+ *
* NOTE: this routine does not do any MULTIBYTE conversion,
* even though it is presumably useful only for text, because
* no code in this module should depend on MULTIBYTE mode.
* See pq_getstr in pqformat.c for that.
*
- * FIXME: we ought to use an expansible StringInfo buffer,
- * rather than dropping data if the message is too long.
- *
* returns 0 if OK, EOF if trouble
* --------------------------------
*/
int
-pq_getstring(char *s, size_t len)
+pq_getstring(StringInfo s)
{
int c;
- /*
- * Keep on reading until we get the terminating '\0', discarding any
- * bytes we don't have room for.
- */
+ /* Reset string to empty */
+ s->len = 0;
+ s->data[0] = '\0';
+ /* Read until we get the terminating '\0' */
while ((c = pq_getbyte()) != EOF && c != '\0')
{
- if (len > 1)
- {
- *s++ = c;
- len--;
- }
+ appendStringInfoChar(s, c);
}
- *s = '\0';
-
if (c == EOF)
return EOF;
*
* Copyright (c) 1994, Regents of the University of California
*
- * $Id: pqformat.c,v 1.7 1999/07/17 20:17:03 momjian Exp $
+ * $Id: pqformat.c,v 1.8 1999/08/31 04:26:37 tgl Exp $
*
*-------------------------------------------------------------------------
*/
/* --------------------------------
* pq_getstr - get a null terminated string from connection
*
- * FIXME: we ought to use an expansible StringInfo buffer,
- * rather than dropping data if the message is too long.
+ * The return value is placed in an expansible StringInfo.
+ * Note that space allocation comes from the current memory context!
*
* returns 0 if OK, EOF if trouble
* --------------------------------
*/
int
-pq_getstr(char *s, int maxlen)
+pq_getstr(StringInfo s)
{
int c;
-
#ifdef MULTIBYTE
char *p;
-
#endif
- c = pq_getstring(s, maxlen);
+ c = pq_getstring(s);
#ifdef MULTIBYTE
- p = (char *) pg_client_to_server((unsigned char *) s, strlen(s));
- if (p != s) /* actual conversion has been done? */
+ p = (char *) pg_client_to_server((unsigned char *) s->data, s->len);
+ if (p != s->data) /* actual conversion has been done? */
{
- int newlen = strlen(p);
-
- if (newlen < maxlen)
- strcpy(s, p);
- else
- {
- strncpy(s, p, maxlen);
- s[maxlen - 1] = '\0';
- }
+ /* reset s to empty, and append the new string p */
+ s->len = 0;
+ s->data[0] = '\0';
+ appendBinaryStringInfo(s, p, strlen(p));
}
#endif
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.127 1999/07/22 02:40:07 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.128 1999/08/31 04:26:40 tgl Exp $
*
* NOTES
* this is the "main" module of the postgres backend and
* decls for routines only used in this file
* ----------------------------------------------------------------
*/
-static int InteractiveBackend(char *inBuf);
-static int SocketBackend(char *inBuf);
-static int ReadCommand(char *inBuf);
+static int InteractiveBackend(StringInfo inBuf);
+static int SocketBackend(StringInfo inBuf);
+static int ReadCommand(StringInfo inBuf);
static void pg_exec_query(char *query_string);
*/
static int
-InteractiveBackend(char *inBuf)
+InteractiveBackend(StringInfo inBuf)
{
- char *stuff = inBuf; /* current place in input buffer */
int c; /* character read from getc() */
bool end = false; /* end-of-input flag */
bool backslashSeen = false; /* have we seen a \ ? */
printf("backend> ");
fflush(stdout);
+ /* Reset inBuf to empty */
+ inBuf->len = 0;
+ inBuf->data[0] = '\0';
+
for (;;)
{
if (UseNewLine)
{
if (backslashSeen)
{
- stuff--;
+ /* discard backslash from inBuf */
+ inBuf->data[--inBuf->len] = '\0';
+ backslashSeen = false;
continue;
}
else
{
/* keep the newline character */
- *stuff++ = '\n';
- *stuff++ = '\0';
+ appendStringInfoChar(inBuf, '\n');
break;
}
}
else
backslashSeen = false;
- *stuff++ = (char) c;
+ appendStringInfoChar(inBuf, (char) c);
}
if (c == EOF)
* ----------------
*/
while ((c = getc(stdin)) != EOF)
- *stuff++ = (char) c;
+ appendStringInfoChar(inBuf, (char) c);
- if (stuff == inBuf)
+ if (inBuf->len == 0)
end = true;
}
* ----------------
*/
if (EchoQuery)
- printf("query: %s\n", inBuf);
+ printf("query: %s\n", inBuf->data);
fflush(stdout);
return 'Q';
* the user is placed in its parameter inBuf.
*
* If the input is a fastpath function call (case 'F') then
- * the function call is processed in HandleFunctionRequest().
+ * the function call is processed in HandleFunctionRequest()
* (now called from PostgresMain()).
*
* EOF is returned if the connection is lost.
*/
static int
-SocketBackend(char *inBuf)
+SocketBackend(StringInfo inBuf)
{
char qtype;
char result = '\0';
* ----------------
*/
case 'Q':
- if (pq_getstr(inBuf, MAX_PARSE_BUFFER))
+ if (pq_getstr(inBuf))
return EOF;
result = 'Q';
break;
* ----------------
*/
case 'F':
- if (pq_getstr(inBuf, MAX_PARSE_BUFFER))
+ if (pq_getstr(inBuf))
return EOF; /* ignore "string" at start of F message */
result = 'F';
break;
* ----------------
*/
static int
-ReadCommand(char *inBuf)
+ReadCommand(StringInfo inBuf)
{
+ MemoryContext oldcontext;
+ int result;
+
+ /* Make sure any expansion of inBuf happens in permanent memory context,
+ * so that we can keep using it for future command cycles.
+ */
+ oldcontext = MemoryContextSwitchTo(TopMemoryContext);
if (IsUnderPostmaster)
- return SocketBackend(inBuf);
+ result = SocketBackend(inBuf);
else
- return InteractiveBackend(inBuf);
+ result = InteractiveBackend(inBuf);
+ MemoryContextSwitchTo(oldcontext);
+ return result;
}
List *
if (DebugPrintQuery)
{
- if (DebugPrintQuery > 3)
- {
- /* Print the query string as is if query debug level > 3 */
- TPRINTF(TRACE_QUERY, "query: %s", query_string);
- }
- else
- {
- /* Print condensed query string to fit in one log line */
- char buff[MAX_QUERY_SIZE + 1];
- char c,
- *s,
- *d;
- int n,
- is_space = 1;
-
- for (s = query_string, d = buff, n = 0; (c = *s) && (n < MAX_QUERY_SIZE); s++)
- {
- switch (c)
- {
- case '\r':
- case '\n':
- case '\t':
- c = ' ';
- /* fall through */
- case ' ':
- if (is_space)
- continue;
- is_space = 1;
- break;
- default:
- is_space = 0;
- break;
- }
- *d++ = c;
- n++;
- }
- *d = '\0';
- TPRINTF(TRACE_QUERY, "query: %s", buff);
- }
+ TPRINTF(TRACE_QUERY, "query: %s", query_string);
}
/* ----------------
int errs = 0;
int firstchar;
- char parser_input[MAX_PARSE_BUFFER];
+ StringInfo parser_input;
char *userName;
/* Used if verbose is set, must be initialized */
on_shmem_exit(remove_all_temp_relations, NULL);
+ parser_input = makeStringInfo(); /* initialize input buffer */
+
/* ----------------
* Set up handler for cancel-request signal, and
* send this backend's cancellation info to the frontend.
if (!IsUnderPostmaster)
{
puts("\nPOSTGRES backend interactive interface ");
- puts("$Revision: 1.127 $ $Date: 1999/07/22 02:40:07 $\n");
+ puts("$Revision: 1.128 $ $Date: 1999/08/31 04:26:40 $\n");
}
/* ----------------
* (3) read a command.
* ----------------
*/
- MemSet(parser_input, 0, MAX_PARSE_BUFFER);
-
firstchar = ReadCommand(parser_input);
QueryCancel = false; /* forget any earlier CANCEL signal */
* ----------------
*/
case 'Q':
- if (strspn(parser_input, " \t\n") == strlen(parser_input))
+ if (strspn(parser_input->data, " \t\n") == parser_input->len)
{
/* ----------------
* if there is nothing in the input buffer, don't bother
TPRINTF(TRACE_VERBOSE, "StartTransactionCommand");
StartTransactionCommand();
- pg_exec_query(parser_input);
+ pg_exec_query(parser_input->data);
if (ShowStats)
ShowUsage();