aboutsummaryrefslogtreecommitdiff
path: root/src/bin/psql/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/psql/common.c')
-rw-r--r--src/bin/psql/common.c58
1 files changed, 49 insertions, 9 deletions
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index b1241bd53d8..0565d0cac21 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1,3 +1,10 @@
+/*
+ * psql - the PostgreSQL interactive terminal
+ *
+ * Copyright 2000 by PostgreSQL Global Development Team
+ *
+ * $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.9 2000/01/18 23:30:23 petere Exp $
+ */
#include <c.h>
#include "common.h"
@@ -5,6 +12,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <stdarg.h>
#ifdef HAVE_TERMIOS_H
#include <termios.h>
#endif
@@ -49,13 +57,13 @@ xstrdup(const char *string)
if (!string)
{
- fprintf(stderr, "xstrdup: Cannot duplicate null pointer.\n");
+ fprintf(stderr, "%s: xstrdup: cannot duplicate null pointer\n", pset.progname);
exit(EXIT_FAILURE);
}
tmp = strdup(string);
if (!tmp)
{
- perror("strdup");
+ psql_error("out of memory\n");
exit(EXIT_FAILURE);
}
return tmp;
@@ -104,7 +112,7 @@ setQFout(const char *fname)
if (!(pset.queryFout))
{
- fprintf(stderr, "%s: %s: %s\n", pset.progname, fname, strerror(errno));
+ psql_error("%s: %s\n", fname, strerror(errno));
pset.queryFout = stdout;
pset.queryFoutPipe = false;
status = false;
@@ -124,6 +132,38 @@ setQFout(const char *fname)
/*
+ * Error reporting for scripts. Errors should look like
+ * filename:lineno: message
+ *
+ */
+void
+psql_error(const char *fmt, ...)
+{
+ va_list ap;
+
+ fflush(stdout);
+ if (pset.queryFout!=stdout)
+ fflush(pset.queryFout);
+
+ if (pset.inputfile)
+ fprintf(stderr, "%s:%u: ", pset.inputfile ? pset.inputfile : pset.progname, pset.lineno);
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+}
+
+/* for backend NOTICES */
+
+void
+NoticeProcessor(void * arg, const char * message)
+{
+ (void)arg; /* not used */
+ psql_error("%s", message);
+}
+
+
+
+/*
* simple_prompt
*
* Generalized function especially intended for reading in usernames and
@@ -239,7 +279,7 @@ PSQLexec(const char *query)
if (!pset.db)
{
- fputs("You are currently not connected to a database.\n", stderr);
+ psql_error("You are currently not connected to a database.\n");
return NULL;
}
@@ -268,7 +308,7 @@ PSQLexec(const char *query)
{
if (!pset.cur_cmd_interactive)
{
- fprintf(stderr, "%s: connection to server was lost", pset.progname);
+ psql_error("connection to server was lost");
exit(EXIT_BADCONN);
}
fputs("The connection to the server was lost. Attempting reset: ", stderr);
@@ -297,7 +337,7 @@ PSQLexec(const char *query)
return res;
else
{
- fputs(PQerrorMessage(pset.db), stderr);
+ psql_error("%s", PQerrorMessage(pset.db));
PQclear(res);
return NULL;
}
@@ -326,7 +366,7 @@ SendQuery(const char *query)
if (!pset.db)
{
- fputs("You are currently not connected to a database.\n", stderr);
+ psql_error("you are currently not connected to a database.\n");
return false;
}
@@ -436,7 +476,7 @@ SendQuery(const char *query)
case PGRES_FATAL_ERROR:
case PGRES_BAD_RESPONSE:
success = false;
- fputs(PQerrorMessage(pset.db), stderr);
+ psql_error("%s", PQerrorMessage(pset.db));
break;
}
@@ -446,7 +486,7 @@ SendQuery(const char *query)
{
if (!pset.cur_cmd_interactive)
{
- fprintf(stderr, "%s: connection to server was lost", pset.progname);
+ psql_error("connection to server was lost");
exit(EXIT_BADCONN);
}
fputs("The connection to the server was lost. Attempting reset: ", stderr);