Skip to content

Commit 2a24ec6

Browse files
committed
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
1 parent c6c6030 commit 2a24ec6

File tree

12 files changed

+267
-261
lines changed

12 files changed

+267
-261
lines changed

src/bin/psql/command.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ do_connect(const char *new_dbname, const char *new_user, PsqlSettings *pset)
828828
PGconn *oldconn = pset->db;
829829
const char *dbparam = NULL;
830830
const char *userparam = NULL;
831-
char *pwparam = NULL;
831+
const char *pwparam = NULL;
832832
char *prompted_password = NULL;
833833
char *prompted_user = NULL;
834834
bool need_pass;

src/bin/psql/describe.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,8 @@ describeTableDetails(const char *name, PsqlSettings *pset)
519519
printTableOpt myopt = pset->popt.topt;
520520
bool description = GetVariableBool(pset->vars, "description");
521521
int i;
522-
char *view_def = NULL;
523-
char *headers[5];
522+
const char *view_def = NULL;
523+
const char *headers[5];
524524
char **cells = NULL;
525525
char *title = NULL;
526526
char **footers = NULL;
@@ -587,11 +587,10 @@ describeTableDetails(const char *name, PsqlSettings *pset)
587587
for (i = 0; i < PQntuples(res); i++)
588588
{
589589
int4 attypmod = atoi(PQgetvalue(res, i, 3));
590-
char *attype = PQgetvalue(res, i, 1);
590+
const char *attype = PQgetvalue(res, i, 1);
591591

592592
/* Name */
593-
cells[i * cols + 0] = PQgetvalue(res, i, 0); /* don't free this
594-
* afterwards */
593+
cells[i * cols + 0] = (char*)PQgetvalue(res, i, 0); /* don't free this afterwards */
595594

596595
/* Type */
597596
cells[i * cols + 1] = xmalloc(NAMEDATALEN + 16);
@@ -609,7 +608,7 @@ describeTableDetails(const char *name, PsqlSettings *pset)
609608

610609
/* Info */
611610
cells[i * cols + 2] = xmalloc(128 + 128); /* I'm cutting off the
612-
* default string at 128 */
611+
* 'default' string at 128 */
613612
cells[i * cols + 2][0] = '\0';
614613
if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
615614
strcat(cells[i * cols + 2], "not null");
@@ -633,7 +632,7 @@ describeTableDetails(const char *name, PsqlSettings *pset)
633632

634633
/* Description */
635634
if (description)
636-
cells[i * cols + 3] = PQgetvalue(res, i, 7);
635+
cells[i * cols + 3] = (char*)PQgetvalue(res, i, 7);
637636
}
638637

639638
/* Make title */
@@ -685,7 +684,7 @@ describeTableDetails(const char *name, PsqlSettings *pset)
685684

686685

687686
myopt.tuples_only = false;
688-
printTable(title, headers, cells, footers, "llll", &myopt, pset->queryFout);
687+
printTable(title, headers, (const char**)cells, (const char**)footers, "llll", &myopt, pset->queryFout);
689688

690689
/* clean up */
691690
free(title);

src/bin/psql/print.c

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@
2828

2929

3030
static void
31-
print_unaligned_text(const char *title, char **headers, char **cells, char **footers,
31+
print_unaligned_text(const char *title, const char * const * headers,
32+
const char * const * cells, const char * const * footers,
3233
const char *opt_fieldsep, bool opt_barebones,
3334
FILE *fout)
3435
{
3536
unsigned int col_count = 0;
3637
unsigned int i;
37-
char **ptr;
38+
const char * const * ptr;
3839

3940
if (!opt_fieldsep)
4041
opt_fieldsep = "";
@@ -80,14 +81,15 @@ print_unaligned_text(const char *title, char **headers, char **cells, char **foo
8081

8182

8283
static void
83-
print_unaligned_vertical(const char *title, char **headers, char **cells, char **footers,
84+
print_unaligned_vertical(const char *title, const char * const * headers,
85+
const char * const * cells, const char * const * footers,
8486
const char *opt_fieldsep, bool opt_barebones,
8587
FILE *fout)
8688
{
8789
unsigned int col_count = 0;
8890
unsigned int i;
8991
unsigned int record = 1;
90-
char **ptr;
92+
const char * const * ptr;
9193

9294
if (!opt_fieldsep)
9395
opt_fieldsep = "";
@@ -167,16 +169,17 @@ _print_horizontal_line(const unsigned int col_count, const unsigned int *widths,
167169

168170

169171
static void
170-
print_aligned_text(const char *title, char **headers, char **cells, char **footers,
171-
const char *opt_align, bool opt_barebones, unsigned short int opt_border,
172+
print_aligned_text(const char *title, const char * const * headers,
173+
const char * const * cells, const char * const * footers,
174+
const char *opt_align, bool opt_barebones, unsigned short int opt_border,
172175
FILE *fout)
173176
{
174177
unsigned int col_count = 0;
175178
unsigned int i,
176179
tmp;
177180
unsigned int *widths,
178181
total_w;
179-
char **ptr;
182+
const char * const * ptr;
180183

181184
/* count columns */
182185
for (ptr = headers; *ptr; ptr++)
@@ -308,13 +311,14 @@ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
308311

309312

310313
static void
311-
print_aligned_vertical(const char *title, char **headers, char **cells, char **footers,
314+
print_aligned_vertical(const char *title, const char * const * headers,
315+
const char * const * cells, const char * const * footers,
312316
bool opt_barebones, unsigned short int opt_border,
313317
FILE *fout)
314318
{
315319
unsigned int col_count = 0;
316320
unsigned int record = 1;
317-
char **ptr;
321+
const char * const *ptr;
318322
unsigned int i,
319323
tmp,
320324
hwidth = 0,
@@ -471,14 +475,15 @@ html_escaped_print(const char *in, FILE *fout)
471475

472476

473477
static void
474-
print_html_text(const char *title, char **headers, char **cells, char **footers,
475-
const char *opt_align, bool opt_barebones, unsigned short int opt_border,
476-
char *opt_table_attr,
478+
print_html_text(const char *title, const char * const * headers,
479+
const char * const * cells, const char * const * footers,
480+
const char *opt_align, bool opt_barebones, unsigned short int opt_border,
481+
const char *opt_table_attr,
477482
FILE *fout)
478483
{
479484
unsigned int col_count = 0;
480485
unsigned int i;
481-
char **ptr;
486+
const char * const *ptr;
482487

483488
fprintf(fout, "<table border=%d", opt_border);
484489
if (opt_table_attr)
@@ -544,15 +549,16 @@ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
544549

545550

546551
static void
547-
print_html_vertical(const char *title, char **headers, char **cells, char **footers,
548-
const char *opt_align, bool opt_barebones, unsigned short int opt_border,
549-
char *opt_table_attr,
552+
print_html_vertical(const char *title, const char * const * headers,
553+
const char * const * cells, const char * const * footers,
554+
const char *opt_align, bool opt_barebones, unsigned short int opt_border,
555+
const char *opt_table_attr,
550556
FILE *fout)
551557
{
552558
unsigned int col_count = 0;
553559
unsigned int i;
554560
unsigned int record = 1;
555-
char **ptr;
561+
const char * const *ptr;
556562

557563
fprintf(fout, "<table border=%d", opt_border);
558564
if (opt_table_attr)
@@ -652,14 +658,15 @@ latex_escaped_print(const char *in, FILE *fout)
652658

653659

654660
static void
655-
print_latex_text(const char *title, char **headers, char **cells, char **footers,
656-
const char *opt_align, bool opt_barebones, unsigned short int opt_border,
661+
print_latex_text(const char *title, const char * const * headers,
662+
const char * const * cells, const char * const * footers,
663+
const char *opt_align, bool opt_barebones, unsigned short int opt_border,
657664
FILE *fout)
658665
{
659666
unsigned int col_count = 0;
660667
unsigned int i;
661668
const char *cp;
662-
char **ptr;
669+
const char * const *ptr;
663670

664671

665672
/* print title */
@@ -747,13 +754,14 @@ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
747754

748755

749756
static void
750-
print_latex_vertical(const char *title, char **headers, char **cells, char **footers,
751-
const char *opt_align, bool opt_barebones, unsigned short int opt_border,
757+
print_latex_vertical(const char *title, const char * const * headers,
758+
const char * const * cells, const char * const * footers,
759+
const char *opt_align, bool opt_barebones, unsigned short int opt_border,
752760
FILE *fout)
753761
{
754762
unsigned int col_count = 0;
755763
unsigned int i;
756-
char **ptr;
764+
const char * const *ptr;
757765
unsigned int record = 1;
758766

759767
(void) opt_align; /* currently unused parameter */
@@ -833,11 +841,14 @@ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
833841

834842

835843
void
836-
printTable(const char *title, char **headers, char **cells, char **footers,
844+
printTable(const char *title,
845+
const char * const * headers,
846+
const char * const * cells,
847+
const char * const * footers,
837848
const char *align,
838849
const printTableOpt * opt, FILE *fout)
839850
{
840-
char *default_footer[] = {NULL};
851+
const char *default_footer[] = {NULL};
841852
unsigned short int border = opt->border;
842853
FILE *pager = NULL,
843854
*output;
@@ -868,7 +879,7 @@ printTable(const char *title, char **headers, char **cells, char **footers,
868879
unsigned int col_count = 0,
869880
row_count = 0,
870881
lines;
871-
char **ptr;
882+
const char * const *ptr;
872883
int result;
873884
struct winsize screen_size;
874885

@@ -952,11 +963,11 @@ printTable(const char *title, char **headers, char **cells, char **footers,
952963

953964

954965
void
955-
printQuery(PGresult *result, const printQueryOpt * opt, FILE *fout)
966+
printQuery(const PGresult *result, const printQueryOpt * opt, FILE *fout)
956967
{
957968
int nfields;
958-
char **headers;
959-
char **cells;
969+
const char **headers;
970+
const char **cells;
960971
char **footers;
961972
char *align;
962973
int i;
@@ -1043,8 +1054,9 @@ printQuery(PGresult *result, const printQueryOpt * opt, FILE *fout)
10431054

10441055
/* call table printer */
10451056

1046-
printTable(opt->title, headers, cells, footers ? footers : opt->footers, align,
1047-
&opt->topt, fout);
1057+
printTable(opt->title, headers, cells,
1058+
footers ? (const char * const *)footers : (const char * const *)(opt->footers),
1059+
align, &opt->topt, fout);
10481060

10491061
free(headers);
10501062
free(cells);

src/bin/psql/print.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ typedef struct _printTableOpt
4444
* - align is an 'l' or an 'r' for every column, if the output format needs it.
4545
* (You must specify this long enough. Otherwise anything could happen.)
4646
*/
47-
void printTable(const char *title, char **headers, char **cells, char **footers,
47+
void printTable(const char *title, const char * const * headers,
48+
const char * const * cells, const char * const * footers,
4849
const char *align,
4950
const printTableOpt * opt, FILE *fout);
5051

@@ -66,7 +67,7 @@ typedef struct _printQueryOpt
6667
* It calls the printTable above with all the things set straight.
6768
*/
6869
void
69-
printQuery(PGresult *result, const printQueryOpt * opt, FILE *fout);
70+
printQuery(const PGresult *result, const printQueryOpt * opt, FILE *fout);
7071

7172

7273
#endif /* PRINT_H */

src/bin/psql/startup.c

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737

3838

3939
static void
40-
process_psqlrc(PsqlSettings *pset);
40+
process_psqlrc(PsqlSettings *pset);
4141

4242
static void
43-
showVersion(PsqlSettings *pset, bool verbose);
43+
showVersion(PsqlSettings *pset);
4444

4545

4646
/* Structures to pass information between the option parsing routine
@@ -68,7 +68,7 @@ struct adhoc_opts
6868
};
6969

7070
static void
71-
parse_options(int argc, char *argv[], PsqlSettings *pset, struct adhoc_opts * options);
71+
parse_options(int argc, char *argv[], PsqlSettings *pset, struct adhoc_opts * options);
7272

7373

7474

@@ -152,7 +152,7 @@ main(int argc, char **argv)
152152
free(username);
153153
free(password);
154154

155-
if (PQstatus(settings.db) == CONNECTION_BAD)
155+
if (PQstatus(settings.db) == CONNECTION_BAD && options.action != ACT_SHOW_VER)
156156
{
157157
fprintf(stderr, "Connection to database '%s' failed.\n%s\n", PQdb(settings.db), PQerrorMessage(settings.db));
158158
PQfinish(settings.db);
@@ -169,19 +169,16 @@ main(int argc, char **argv)
169169

170170
if (options.action == ACT_SHOW_VER)
171171
{
172-
showVersion(&settings, true);
172+
showVersion(&settings);
173173
PQfinish(settings.db);
174174
exit(EXIT_SUCCESS);
175175
}
176176

177177

178178
if (!GetVariable(settings.vars, "quiet") && !settings.notty && !options.action)
179179
{
180-
puts("Welcome to psql, the PostgreSQL interactive terminal.\n");
181-
182-
//showVersion(&settings, false);
183-
184-
puts("Type: \\copyright for distribution terms\n"
180+
puts("Welcome to psql, the PostgreSQL interactive terminal.\n\n"
181+
"Type: \\copyright for distribution terms\n"
185182
" \\h for help with SQL commands\n"
186183
" \\? for help on internal slash commands\n"
187184
" \\g or terminate with semicolon to execute query\n"
@@ -509,28 +506,22 @@ process_psqlrc(PsqlSettings *pset)
509506
* or a mismatch was detected.
510507
*/
511508
static void
512-
showVersion(PsqlSettings *pset, bool verbose)
509+
showVersion(PsqlSettings *pset)
513510
{
514-
PGresult *res;
515-
char *versionstr = NULL;
511+
PGresult *res = NULL;
512+
const char *versionstr = NULL;
516513
long int release = 0,
517514
version = 0,
518515
subversion = 0;
519516

520517
/* get backend version */
518+
if (pset->db && PQstatus(pset->db) == CONNECTION_OK) {
521519
res = PSQLexec(pset, "SELECT version()");
522520
if (PQresultStatus(res) == PGRES_TUPLES_OK)
523521
versionstr = PQgetvalue(res, 0, 0);
524-
525-
if (!verbose)
526-
{
527-
if (versionstr)
528-
puts(versionstr);
529-
PQclear(res);
530-
return;
531522
}
532523

533-
if (strncmp(versionstr, "PostgreSQL ", 11) == 0)
524+
if (versionstr && strncmp(versionstr, "PostgreSQL ", 11) == 0)
534525
{
535526
char *tmp;
536527

@@ -539,9 +530,9 @@ showVersion(PsqlSettings *pset, bool verbose)
539530
subversion = strtol(tmp + 1, &tmp, 10);
540531
}
541532

542-
printf("Server: %s\npsql", versionstr ? versionstr : "(could not connected)");
533+
printf("Server: %s\npsql", versionstr ? versionstr : "(could not connect)");
543534

544-
if (strcmp(versionstr, PG_VERSION_STR) != 0)
535+
if (!versionstr || strcmp(versionstr, PG_VERSION_STR) != 0)
545536
printf(&PG_VERSION_STR[strcspn(PG_VERSION_STR, " ")]);
546537
printf(" (" __DATE__ " " __TIME__ ")");
547538

@@ -569,10 +560,11 @@ showVersion(PsqlSettings *pset, bool verbose)
569560

570561
puts("");
571562

572-
if (release < 6 || (release == 6 && version < 5))
563+
if (versionstr && (release < 6 || (release == 6 && version < 5)))
573564
puts("\nWarning: The server you are connected to is potentially too old for this client\n"
574565
"version. You should ideally be using clients and servers from the same\n"
575566
"distribution.");
576567

568+
if (res)
577569
PQclear(res);
578570
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy