summaryrefslogtreecommitdiff
path: root/src/bin
diff options
context:
space:
mode:
authorBruce Momjian <bruce@momjian.us>1999-11-11 00:10:14 +0000
committerBruce Momjian <bruce@momjian.us>1999-11-11 00:10:14 +0000
commit2a24ec6f167a21ef074609e165d77f1f7c715259 (patch)
tree579eb1fde3baf6cf0edd60d62e3c5bf7a60698cf /src/bin
parentc6c60302ba45ac89440ec7e2a4e1c5de3a1a61c2 (diff)
downloadpostgresql-2a24ec6f167a21ef074609e165d77f1f7c715259.tar.gz
In the spirit of TODO item
* Add use of 'const' for varibles in source tree (which is misspelled, btw.) I went through the front-end libpq code and did so. This affects in particular the various accessor functions (such as PQdb() and PQgetvalue()) as well as, by necessity, the internal helpers they use. I have been really thorough in that regard, perhaps some people will find it annoying that things like char * foo = PQgetvalue(res, 0, 0) will generate a warning. On the other hand it _should_ generate one. This is no real compatibility break, although a few clients will have to be fixed to suppress warnings. (Which again would be in the spirit of the above TODO.) In addition I replaced some int's by size_t's and removed some warnings (and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it actually honors the const!) and supplied a new function PQoidValue that returns a proper Oid type. This is only front-end stuff, none of the communicaton stuff was touched. The psql patch also adds some new consts to honor the new libpq situation, as well as fixes a fatal condition that resulted when using the -V (--version) option and there is no database listening. So, to summarize, the psql you should definitely put in (with or without the libpq). If you think I went too far with the const-mania in libpq, let me know and I'll make adjustments. If you approve it, I will also update the docs. -Peter -- Peter Eisentraut Sernanders vaeg 10:115
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/psql/command.c2
-rw-r--r--src/bin/psql/describe.c15
-rw-r--r--src/bin/psql/print.c74
-rw-r--r--src/bin/psql/print.h5
-rw-r--r--src/bin/psql/startup.c40
5 files changed, 70 insertions, 66 deletions
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 29df279262..986ee365a5 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -828,7 +828,7 @@ do_connect(const char *new_dbname, const char *new_user, PsqlSettings *pset)
PGconn *oldconn = pset->db;
const char *dbparam = NULL;
const char *userparam = NULL;
- char *pwparam = NULL;
+ const char *pwparam = NULL;
char *prompted_password = NULL;
char *prompted_user = NULL;
bool need_pass;
diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index 58534e2575..5e40779cd1 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -519,8 +519,8 @@ describeTableDetails(const char *name, PsqlSettings *pset)
printTableOpt myopt = pset->popt.topt;
bool description = GetVariableBool(pset->vars, "description");
int i;
- char *view_def = NULL;
- char *headers[5];
+ const char *view_def = NULL;
+ const char *headers[5];
char **cells = NULL;
char *title = NULL;
char **footers = NULL;
@@ -587,11 +587,10 @@ describeTableDetails(const char *name, PsqlSettings *pset)
for (i = 0; i < PQntuples(res); i++)
{
int4 attypmod = atoi(PQgetvalue(res, i, 3));
- char *attype = PQgetvalue(res, i, 1);
+ const char *attype = PQgetvalue(res, i, 1);
/* Name */
- cells[i * cols + 0] = PQgetvalue(res, i, 0); /* don't free this
- * afterwards */
+ cells[i * cols + 0] = (char*)PQgetvalue(res, i, 0); /* don't free this afterwards */
/* Type */
cells[i * cols + 1] = xmalloc(NAMEDATALEN + 16);
@@ -609,7 +608,7 @@ describeTableDetails(const char *name, PsqlSettings *pset)
/* Info */
cells[i * cols + 2] = xmalloc(128 + 128); /* I'm cutting off the
- * default string at 128 */
+ * 'default' string at 128 */
cells[i * cols + 2][0] = '\0';
if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
strcat(cells[i * cols + 2], "not null");
@@ -633,7 +632,7 @@ describeTableDetails(const char *name, PsqlSettings *pset)
/* Description */
if (description)
- cells[i * cols + 3] = PQgetvalue(res, i, 7);
+ cells[i * cols + 3] = (char*)PQgetvalue(res, i, 7);
}
/* Make title */
@@ -685,7 +684,7 @@ describeTableDetails(const char *name, PsqlSettings *pset)
myopt.tuples_only = false;
- printTable(title, headers, cells, footers, "llll", &myopt, pset->queryFout);
+ printTable(title, headers, (const char**)cells, (const char**)footers, "llll", &myopt, pset->queryFout);
/* clean up */
free(title);
diff --git a/src/bin/psql/print.c b/src/bin/psql/print.c
index 5c6525b6f3..90b4f59d61 100644
--- a/src/bin/psql/print.c
+++ b/src/bin/psql/print.c
@@ -28,13 +28,14 @@
static void
-print_unaligned_text(const char *title, char **headers, char **cells, char **footers,
+print_unaligned_text(const char *title, const char * const * headers,
+ const char * const * cells, const char * const * footers,
const char *opt_fieldsep, bool opt_barebones,
FILE *fout)
{
unsigned int col_count = 0;
unsigned int i;
- char **ptr;
+ const char * const * ptr;
if (!opt_fieldsep)
opt_fieldsep = "";
@@ -80,14 +81,15 @@ print_unaligned_text(const char *title, char **headers, char **cells, char **foo
static void
-print_unaligned_vertical(const char *title, char **headers, char **cells, char **footers,
+print_unaligned_vertical(const char *title, const char * const * headers,
+ const char * const * cells, const char * const * footers,
const char *opt_fieldsep, bool opt_barebones,
FILE *fout)
{
unsigned int col_count = 0;
unsigned int i;
unsigned int record = 1;
- char **ptr;
+ const char * const * ptr;
if (!opt_fieldsep)
opt_fieldsep = "";
@@ -167,8 +169,9 @@ _print_horizontal_line(const unsigned int col_count, const unsigned int *widths,
static void
-print_aligned_text(const char *title, char **headers, char **cells, char **footers,
-const char *opt_align, bool opt_barebones, unsigned short int opt_border,
+print_aligned_text(const char *title, const char * const * headers,
+ const char * const * cells, const char * const * footers,
+ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
FILE *fout)
{
unsigned int col_count = 0;
@@ -176,7 +179,7 @@ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
tmp;
unsigned int *widths,
total_w;
- char **ptr;
+ const char * const * ptr;
/* count columns */
for (ptr = headers; *ptr; ptr++)
@@ -308,13 +311,14 @@ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
static void
-print_aligned_vertical(const char *title, char **headers, char **cells, char **footers,
+print_aligned_vertical(const char *title, const char * const * headers,
+ const char * const * cells, const char * const * footers,
bool opt_barebones, unsigned short int opt_border,
FILE *fout)
{
unsigned int col_count = 0;
unsigned int record = 1;
- char **ptr;
+ const char * const *ptr;
unsigned int i,
tmp,
hwidth = 0,
@@ -471,14 +475,15 @@ html_escaped_print(const char *in, FILE *fout)
static void
-print_html_text(const char *title, char **headers, char **cells, char **footers,
-const char *opt_align, bool opt_barebones, unsigned short int opt_border,
- char *opt_table_attr,
+print_html_text(const char *title, const char * const * headers,
+ const char * const * cells, const char * const * footers,
+ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
+ const char *opt_table_attr,
FILE *fout)
{
unsigned int col_count = 0;
unsigned int i;
- char **ptr;
+ const char * const *ptr;
fprintf(fout, "<table border=%d", opt_border);
if (opt_table_attr)
@@ -544,15 +549,16 @@ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
static void
-print_html_vertical(const char *title, char **headers, char **cells, char **footers,
-const char *opt_align, bool opt_barebones, unsigned short int opt_border,
- char *opt_table_attr,
+print_html_vertical(const char *title, const char * const * headers,
+ const char * const * cells, const char * const * footers,
+ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
+ const char *opt_table_attr,
FILE *fout)
{
unsigned int col_count = 0;
unsigned int i;
unsigned int record = 1;
- char **ptr;
+ const char * const *ptr;
fprintf(fout, "<table border=%d", opt_border);
if (opt_table_attr)
@@ -652,14 +658,15 @@ latex_escaped_print(const char *in, FILE *fout)
static void
-print_latex_text(const char *title, char **headers, char **cells, char **footers,
-const char *opt_align, bool opt_barebones, unsigned short int opt_border,
+print_latex_text(const char *title, const char * const * headers,
+ const char * const * cells, const char * const * footers,
+ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
FILE *fout)
{
unsigned int col_count = 0;
unsigned int i;
const char *cp;
- char **ptr;
+ const char * const *ptr;
/* print title */
@@ -747,13 +754,14 @@ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
static void
-print_latex_vertical(const char *title, char **headers, char **cells, char **footers,
-const char *opt_align, bool opt_barebones, unsigned short int opt_border,
+print_latex_vertical(const char *title, const char * const * headers,
+ const char * const * cells, const char * const * footers,
+ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
FILE *fout)
{
unsigned int col_count = 0;
unsigned int i;
- char **ptr;
+ const char * const *ptr;
unsigned int record = 1;
(void) opt_align; /* currently unused parameter */
@@ -833,11 +841,14 @@ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
void
-printTable(const char *title, char **headers, char **cells, char **footers,
+printTable(const char *title,
+ const char * const * headers,
+ const char * const * cells,
+ const char * const * footers,
const char *align,
const printTableOpt * opt, FILE *fout)
{
- char *default_footer[] = {NULL};
+ const char *default_footer[] = {NULL};
unsigned short int border = opt->border;
FILE *pager = NULL,
*output;
@@ -868,7 +879,7 @@ printTable(const char *title, char **headers, char **cells, char **footers,
unsigned int col_count = 0,
row_count = 0,
lines;
- char **ptr;
+ const char * const *ptr;
int result;
struct winsize screen_size;
@@ -952,11 +963,11 @@ printTable(const char *title, char **headers, char **cells, char **footers,
void
-printQuery(PGresult *result, const printQueryOpt * opt, FILE *fout)
+printQuery(const PGresult *result, const printQueryOpt * opt, FILE *fout)
{
int nfields;
- char **headers;
- char **cells;
+ const char **headers;
+ const char **cells;
char **footers;
char *align;
int i;
@@ -1043,8 +1054,9 @@ printQuery(PGresult *result, const printQueryOpt * opt, FILE *fout)
/* call table printer */
- printTable(opt->title, headers, cells, footers ? footers : opt->footers, align,
- &opt->topt, fout);
+ printTable(opt->title, headers, cells,
+ footers ? (const char * const *)footers : (const char * const *)(opt->footers),
+ align, &opt->topt, fout);
free(headers);
free(cells);
diff --git a/src/bin/psql/print.h b/src/bin/psql/print.h
index 64d0271f50..5b1c7671b1 100644
--- a/src/bin/psql/print.h
+++ b/src/bin/psql/print.h
@@ -44,7 +44,8 @@ typedef struct _printTableOpt
* - align is an 'l' or an 'r' for every column, if the output format needs it.
* (You must specify this long enough. Otherwise anything could happen.)
*/
-void printTable(const char *title, char **headers, char **cells, char **footers,
+void printTable(const char *title, const char * const * headers,
+ const char * const * cells, const char * const * footers,
const char *align,
const printTableOpt * opt, FILE *fout);
@@ -66,7 +67,7 @@ typedef struct _printQueryOpt
* It calls the printTable above with all the things set straight.
*/
void
- printQuery(PGresult *result, const printQueryOpt * opt, FILE *fout);
+printQuery(const PGresult *result, const printQueryOpt * opt, FILE *fout);
#endif /* PRINT_H */
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index c94f2526b3..1a1688f081 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -37,10 +37,10 @@
static void
- process_psqlrc(PsqlSettings *pset);
+process_psqlrc(PsqlSettings *pset);
static void
- showVersion(PsqlSettings *pset, bool verbose);
+showVersion(PsqlSettings *pset);
/* Structures to pass information between the option parsing routine
@@ -68,7 +68,7 @@ struct adhoc_opts
};
static void
- parse_options(int argc, char *argv[], PsqlSettings *pset, struct adhoc_opts * options);
+parse_options(int argc, char *argv[], PsqlSettings *pset, struct adhoc_opts * options);
@@ -152,7 +152,7 @@ main(int argc, char **argv)
free(username);
free(password);
- if (PQstatus(settings.db) == CONNECTION_BAD)
+ if (PQstatus(settings.db) == CONNECTION_BAD && options.action != ACT_SHOW_VER)
{
fprintf(stderr, "Connection to database '%s' failed.\n%s\n", PQdb(settings.db), PQerrorMessage(settings.db));
PQfinish(settings.db);
@@ -169,7 +169,7 @@ main(int argc, char **argv)
if (options.action == ACT_SHOW_VER)
{
- showVersion(&settings, true);
+ showVersion(&settings);
PQfinish(settings.db);
exit(EXIT_SUCCESS);
}
@@ -177,11 +177,8 @@ main(int argc, char **argv)
if (!GetVariable(settings.vars, "quiet") && !settings.notty && !options.action)
{
- puts("Welcome to psql, the PostgreSQL interactive terminal.\n");
-
- //showVersion(&settings, false);
-
- puts("Type: \\copyright for distribution terms\n"
+ puts("Welcome to psql, the PostgreSQL interactive terminal.\n\n"
+ "Type: \\copyright for distribution terms\n"
" \\h for help with SQL commands\n"
" \\? for help on internal slash commands\n"
" \\g or terminate with semicolon to execute query\n"
@@ -509,28 +506,22 @@ process_psqlrc(PsqlSettings *pset)
* or a mismatch was detected.
*/
static void
-showVersion(PsqlSettings *pset, bool verbose)
+showVersion(PsqlSettings *pset)
{
- PGresult *res;
- char *versionstr = NULL;
+ PGresult *res = NULL;
+ const char *versionstr = NULL;
long int release = 0,
version = 0,
subversion = 0;
/* get backend version */
+ if (pset->db && PQstatus(pset->db) == CONNECTION_OK) {
res = PSQLexec(pset, "SELECT version()");
if (PQresultStatus(res) == PGRES_TUPLES_OK)
versionstr = PQgetvalue(res, 0, 0);
-
- if (!verbose)
- {
- if (versionstr)
- puts(versionstr);
- PQclear(res);
- return;
}
- if (strncmp(versionstr, "PostgreSQL ", 11) == 0)
+ if (versionstr && strncmp(versionstr, "PostgreSQL ", 11) == 0)
{
char *tmp;
@@ -539,9 +530,9 @@ showVersion(PsqlSettings *pset, bool verbose)
subversion = strtol(tmp + 1, &tmp, 10);
}
- printf("Server: %s\npsql", versionstr ? versionstr : "(could not connected)");
+ printf("Server: %s\npsql", versionstr ? versionstr : "(could not connect)");
- if (strcmp(versionstr, PG_VERSION_STR) != 0)
+ if (!versionstr || strcmp(versionstr, PG_VERSION_STR) != 0)
printf(&PG_VERSION_STR[strcspn(PG_VERSION_STR, " ")]);
printf(" (" __DATE__ " " __TIME__ ")");
@@ -569,10 +560,11 @@ showVersion(PsqlSettings *pset, bool verbose)
puts("");
- if (release < 6 || (release == 6 && version < 5))
+ if (versionstr && (release < 6 || (release == 6 && version < 5)))
puts("\nWarning: The server you are connected to is potentially too old for this client\n"
"version. You should ideally be using clients and servers from the same\n"
"distribution.");
+ if (res)
PQclear(res);
}