Fix numericlocale psql option when used with a null string and latex and troff

formats; a null string must not be formatted as a numeric. The more exotic
formats latex and troff also incorrectly formatted all strings as numerics
when numericlocale was on.

Backpatch to 8.1 where numericlocale option was added.

This fixes bug #5355 reported by Andy Lester.
This commit is contained in:
Heikki Linnakangas 2010-03-01 20:56:15 +00:00
parent 8a0137d785
commit 64e19ac859

View File

@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2005, PostgreSQL Global Development Group * Copyright (c) 2000-2005, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.79 2005/10/27 13:34:47 momjian Exp $ * $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.79.2.1 2010/03/01 20:56:15 heikki Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "common.h" #include "common.h"
@ -152,7 +152,7 @@ print_unaligned_text(const char *title, const char *const * headers,
const char *const * cells, const char *const * footers, const char *const * cells, const char *const * footers,
const char *opt_align, const char *opt_fieldsep, const char *opt_align, const char *opt_fieldsep,
const char *opt_recordsep, bool opt_tuples_only, const char *opt_recordsep, bool opt_tuples_only,
bool opt_numeric_locale, FILE *fout) FILE *fout)
{ {
unsigned int col_count = 0; unsigned int col_count = 0;
unsigned int i; unsigned int i;
@ -191,15 +191,7 @@ print_unaligned_text(const char *title, const char *const * headers,
fputs(opt_recordsep, fout); fputs(opt_recordsep, fout);
need_recordsep = false; need_recordsep = false;
} }
if (opt_align[i % col_count] == 'r' && opt_numeric_locale) fputs(*ptr, fout);
{
char *my_cell = format_numeric_locale(*ptr);
fputs(my_cell, fout);
free(my_cell);
}
else
fputs(*ptr, fout);
if ((i + 1) % col_count) if ((i + 1) % col_count)
fputs(opt_fieldsep, fout); fputs(opt_fieldsep, fout);
@ -234,7 +226,7 @@ print_unaligned_vertical(const char *title, const char *const * headers,
const char *const * cells, const char *const * cells,
const char *const * footers, const char *opt_align, const char *const * footers, const char *opt_align,
const char *opt_fieldsep, const char *opt_recordsep, const char *opt_fieldsep, const char *opt_recordsep,
bool opt_tuples_only, bool opt_numeric_locale, FILE *fout) bool opt_tuples_only, FILE *fout)
{ {
unsigned int col_count = 0; unsigned int col_count = 0;
unsigned int i; unsigned int i;
@ -265,15 +257,7 @@ print_unaligned_vertical(const char *title, const char *const * headers,
fputs(headers[i % col_count], fout); fputs(headers[i % col_count], fout);
fputs(opt_fieldsep, fout); fputs(opt_fieldsep, fout);
if (opt_align[i % col_count] == 'r' && opt_numeric_locale) fputs(*ptr, fout);
{
char *my_cell = format_numeric_locale(*ptr);
fputs(my_cell, fout);
free(my_cell);
}
else
fputs(*ptr, fout);
} }
/* print footers */ /* print footers */
@ -336,7 +320,7 @@ _print_horizontal_line(const unsigned int col_count, const unsigned int *widths,
static void static void
print_aligned_text(const char *title, const char *const * headers, print_aligned_text(const char *title, const char *const * headers,
const char *const * cells, const char *const * footers, const char *const * cells, const char *const * footers,
const char *opt_align, bool opt_tuples_only, bool opt_numeric_locale, const char *opt_align, bool opt_tuples_only,
unsigned short int opt_border, int encoding, unsigned short int opt_border, int encoding,
FILE *fout) FILE *fout)
{ {
@ -403,14 +387,7 @@ print_aligned_text(const char *title, const char *const * headers,
for (i = 0, ptr = cells; *ptr; ptr++, i++) for (i = 0, ptr = cells; *ptr; ptr++, i++)
{ {
int add_numeric_locale_len; tmp = pg_wcswidth(*ptr, strlen(*ptr), encoding);
if (opt_align[i % col_count] == 'r' && opt_numeric_locale)
add_numeric_locale_len = additional_numeric_locale_len(*ptr);
else
add_numeric_locale_len = 0;
tmp = pg_wcswidth(*ptr, strlen(*ptr), encoding) + add_numeric_locale_len;
if (tmp > widths[i % col_count]) if (tmp > widths[i % col_count])
widths[i % col_count] = tmp; widths[i % col_count] = tmp;
cell_w[i] = tmp; cell_w[i] = tmp;
@ -489,17 +466,7 @@ print_aligned_text(const char *title, const char *const * headers,
/* content */ /* content */
if (opt_align[i % col_count] == 'r') if (opt_align[i % col_count] == 'r')
{ fprintf(fout, "%*s%s", widths[i % col_count] - cell_w[i], "", *ptr);
if (opt_numeric_locale)
{
char *my_cell = format_numeric_locale(*ptr);
fprintf(fout, "%*s%s", widths[i % col_count] - cell_w[i], "", my_cell);
free(my_cell);
}
else
fprintf(fout, "%*s%s", widths[i % col_count] - cell_w[i], "", *ptr);
}
else else
{ {
if ((i + 1) % col_count == 0 && opt_border != 2) if ((i + 1) % col_count == 0 && opt_border != 2)
@ -554,7 +521,7 @@ static void
print_aligned_vertical(const char *title, const char *const * headers, print_aligned_vertical(const char *title, const char *const * headers,
const char *const * cells, const char *const * footers, const char *const * cells, const char *const * footers,
const char *opt_align, bool opt_tuples_only, const char *opt_align, bool opt_tuples_only,
bool opt_numeric_locale, unsigned short int opt_border, unsigned short int opt_border,
int encoding, FILE *fout) int encoding, FILE *fout)
{ {
unsigned int col_count = 0; unsigned int col_count = 0;
@ -617,14 +584,7 @@ print_aligned_vertical(const char *title, const char *const * headers,
/* find longest data cell */ /* find longest data cell */
for (i = 0, ptr = cells; *ptr; ptr++, i++) for (i = 0, ptr = cells; *ptr; ptr++, i++)
{ {
int add_numeric_locale_len; tmp = pg_wcswidth(*ptr, strlen(*ptr), encoding);
if (opt_align[i % col_count] == 'r' && opt_numeric_locale)
add_numeric_locale_len = additional_numeric_locale_len(*ptr);
else
add_numeric_locale_len = 0;
tmp = pg_wcswidth(*ptr, strlen(*ptr), encoding) + add_numeric_locale_len;
if (tmp > dwidth) if (tmp > dwidth)
dwidth = tmp; dwidth = tmp;
cell_w[i] = tmp; cell_w[i] = tmp;
@ -698,23 +658,10 @@ print_aligned_vertical(const char *title, const char *const * headers,
else else
fputs(" ", fout); fputs(" ", fout);
if (opt_align[i % col_count] == 'r' && opt_numeric_locale) if (opt_border < 2)
{ fprintf(fout, "%s\n", *ptr);
char *my_cell = format_numeric_locale(*ptr);
if (opt_border < 2)
fprintf(fout, "%s\n", my_cell);
else
fprintf(fout, "%-s%*s |\n", my_cell, dwidth - cell_w[i], "");
free(my_cell);
}
else else
{ fprintf(fout, "%-s%*s |\n", *ptr, dwidth - cell_w[i], "");
if (opt_border < 2)
fprintf(fout, "%s\n", *ptr);
else
fprintf(fout, "%-s%*s |\n", *ptr, dwidth - cell_w[i], "");
}
} }
if (opt_border == 2) if (opt_border == 2)
@ -795,7 +742,7 @@ static void
print_html_text(const char *title, const char *const * headers, print_html_text(const char *title, const char *const * headers,
const char *const * cells, const char *const * footers, const char *const * cells, const char *const * footers,
const char *opt_align, bool opt_tuples_only, const char *opt_align, bool opt_tuples_only,
bool opt_numeric_locale, unsigned short int opt_border, unsigned short int opt_border,
const char *opt_table_attr, FILE *fout) const char *opt_table_attr, FILE *fout)
{ {
unsigned int col_count = 0; unsigned int col_count = 0;
@ -841,13 +788,6 @@ print_html_text(const char *title, const char *const * headers,
/* is string only whitespace? */ /* is string only whitespace? */
if ((*ptr)[strspn(*ptr, " \t")] == '\0') if ((*ptr)[strspn(*ptr, " \t")] == '\0')
fputs("&nbsp; ", fout); fputs("&nbsp; ", fout);
else if (opt_align[i % col_count] == 'r' && opt_numeric_locale)
{
char *my_cell = format_numeric_locale(*ptr);
html_escaped_print(my_cell, fout);
free(my_cell);
}
else else
html_escaped_print(*ptr, fout); html_escaped_print(*ptr, fout);
@ -880,7 +820,7 @@ static void
print_html_vertical(const char *title, const char *const * headers, print_html_vertical(const char *title, const char *const * headers,
const char *const * cells, const char *const * footers, const char *const * cells, const char *const * footers,
const char *opt_align, bool opt_tuples_only, const char *opt_align, bool opt_tuples_only,
bool opt_numeric_locale, unsigned short int opt_border, unsigned short int opt_border,
const char *opt_table_attr, FILE *fout) const char *opt_table_attr, FILE *fout)
{ {
unsigned int col_count = 0; unsigned int col_count = 0;
@ -924,13 +864,6 @@ print_html_vertical(const char *title, const char *const * headers,
/* is string only whitespace? */ /* is string only whitespace? */
if ((*ptr)[strspn(*ptr, " \t")] == '\0') if ((*ptr)[strspn(*ptr, " \t")] == '\0')
fputs("&nbsp; ", fout); fputs("&nbsp; ", fout);
else if (opt_align[i % col_count] == 'r' && opt_numeric_locale)
{
char *my_cell = format_numeric_locale(*ptr);
html_escaped_print(my_cell, fout);
free(my_cell);
}
else else
html_escaped_print(*ptr, fout); html_escaped_print(*ptr, fout);
@ -1003,7 +936,7 @@ static void
print_latex_text(const char *title, const char *const * headers, print_latex_text(const char *title, const char *const * headers,
const char *const * cells, const char *const * footers, const char *const * cells, const char *const * footers,
const char *opt_align, bool opt_tuples_only, const char *opt_align, bool opt_tuples_only,
bool opt_numeric_locale, unsigned short int opt_border, unsigned short int opt_border,
FILE *fout) FILE *fout)
{ {
unsigned int col_count = 0; unsigned int col_count = 0;
@ -1064,15 +997,7 @@ print_latex_text(const char *title, const char *const * headers,
/* print cells */ /* print cells */
for (i = 0, ptr = cells; *ptr; i++, ptr++) for (i = 0, ptr = cells; *ptr; i++, ptr++)
{ {
if (opt_numeric_locale) latex_escaped_print(*ptr, fout);
{
char *my_cell = format_numeric_locale(*ptr);
latex_escaped_print(my_cell, fout);
free(my_cell);
}
else
latex_escaped_print(*ptr, fout);
if ((i + 1) % col_count == 0) if ((i + 1) % col_count == 0)
fputs(" \\\\\n", fout); fputs(" \\\\\n", fout);
@ -1104,7 +1029,7 @@ static void
print_latex_vertical(const char *title, const char *const * headers, print_latex_vertical(const char *title, const char *const * headers,
const char *const * cells, const char *const * footers, const char *const * cells, const char *const * footers,
const char *opt_align, bool opt_tuples_only, const char *opt_align, bool opt_tuples_only,
bool opt_numeric_locale, unsigned short int opt_border, unsigned short int opt_border,
FILE *fout) FILE *fout)
{ {
unsigned int col_count = 0; unsigned int col_count = 0;
@ -1175,15 +1100,7 @@ print_latex_vertical(const char *title, const char *const * headers,
if (footers && !opt_tuples_only) if (footers && !opt_tuples_only)
for (ptr = footers; *ptr; ptr++) for (ptr = footers; *ptr; ptr++)
{ {
if (opt_numeric_locale) latex_escaped_print(*ptr, fout);
{
char *my_cell = format_numeric_locale(*ptr);
latex_escaped_print(my_cell, fout);
free(my_cell);
}
else
latex_escaped_print(*ptr, fout);
fputs(" \\\\\n", fout); fputs(" \\\\\n", fout);
} }
@ -1219,7 +1136,7 @@ static void
print_troff_ms_text(const char *title, const char *const * headers, print_troff_ms_text(const char *title, const char *const * headers,
const char *const * cells, const char *const * footers, const char *const * cells, const char *const * footers,
const char *opt_align, bool opt_tuples_only, const char *opt_align, bool opt_tuples_only,
bool opt_numeric_locale, unsigned short int opt_border, unsigned short int opt_border,
FILE *fout) FILE *fout)
{ {
unsigned int col_count = 0; unsigned int col_count = 0;
@ -1273,15 +1190,7 @@ print_troff_ms_text(const char *title, const char *const * headers,
/* print cells */ /* print cells */
for (i = 0, ptr = cells; *ptr; i++, ptr++) for (i = 0, ptr = cells; *ptr; i++, ptr++)
{ {
if (opt_numeric_locale) troff_ms_escaped_print(*ptr, fout);
{
char *my_cell = format_numeric_locale(*ptr);
troff_ms_escaped_print(my_cell, fout);
free(my_cell);
}
else
troff_ms_escaped_print(*ptr, fout);
if ((i + 1) % col_count == 0) if ((i + 1) % col_count == 0)
fputc('\n', fout); fputc('\n', fout);
@ -1310,7 +1219,7 @@ static void
print_troff_ms_vertical(const char *title, const char *const * headers, print_troff_ms_vertical(const char *title, const char *const * headers,
const char *const * cells, const char *const * footers, const char *const * cells, const char *const * footers,
const char *opt_align, bool opt_tuples_only, const char *opt_align, bool opt_tuples_only,
bool opt_numeric_locale, unsigned short int opt_border, unsigned short int opt_border,
FILE *fout) FILE *fout)
{ {
unsigned int col_count = 0; unsigned int col_count = 0;
@ -1383,15 +1292,7 @@ print_troff_ms_vertical(const char *title, const char *const * headers,
troff_ms_escaped_print(headers[i % col_count], fout); troff_ms_escaped_print(headers[i % col_count], fout);
fputc('\t', fout); fputc('\t', fout);
if (opt_numeric_locale) troff_ms_escaped_print(*ptr, fout);
{
char *my_cell = format_numeric_locale(*ptr);
troff_ms_escaped_print(my_cell, fout);
free(my_cell);
}
else
troff_ms_escaped_print(*ptr, fout);
fputc('\n', fout); fputc('\n', fout);
} }
@ -1528,7 +1429,7 @@ printTable(const char *title,
/* print the stuff */ /* print the stuff */
if (flog) if (flog)
print_aligned_text(title, headers, cells, footers, align, opt->tuples_only, opt->numericLocale, border, opt->encoding, flog); print_aligned_text(title, headers, cells, footers, align, opt->tuples_only, border, opt->encoding, flog);
switch (opt->format) switch (opt->format)
{ {
@ -1536,50 +1437,50 @@ printTable(const char *title,
if (use_expanded) if (use_expanded)
print_unaligned_vertical(title, headers, cells, footers, align, print_unaligned_vertical(title, headers, cells, footers, align,
opt->fieldSep, opt->recordSep, opt->fieldSep, opt->recordSep,
opt->tuples_only, opt->numericLocale, output); opt->tuples_only, output);
else else
print_unaligned_text(title, headers, cells, footers, align, print_unaligned_text(title, headers, cells, footers, align,
opt->fieldSep, opt->recordSep, opt->fieldSep, opt->recordSep,
opt->tuples_only, opt->numericLocale, output); opt->tuples_only, output);
break; break;
case PRINT_ALIGNED: case PRINT_ALIGNED:
if (use_expanded) if (use_expanded)
print_aligned_vertical(title, headers, cells, footers, align, print_aligned_vertical(title, headers, cells, footers, align,
opt->tuples_only, opt->numericLocale, border, opt->tuples_only, border,
opt->encoding, output); opt->encoding, output);
else else
print_aligned_text(title, headers, cells, footers, align, print_aligned_text(title, headers, cells, footers, align,
opt->tuples_only, opt->numericLocale, opt->tuples_only,
border, opt->encoding, output); border, opt->encoding, output);
break; break;
case PRINT_HTML: case PRINT_HTML:
if (use_expanded) if (use_expanded)
print_html_vertical(title, headers, cells, footers, align, print_html_vertical(title, headers, cells, footers, align,
opt->tuples_only, opt->numericLocale, opt->tuples_only,
border, opt->tableAttr, output); border, opt->tableAttr, output);
else else
print_html_text(title, headers, cells, footers, print_html_text(title, headers, cells, footers,
align, opt->tuples_only, opt->numericLocale, border, align, opt->tuples_only, border,
opt->tableAttr, output); opt->tableAttr, output);
break; break;
case PRINT_LATEX: case PRINT_LATEX:
if (use_expanded) if (use_expanded)
print_latex_vertical(title, headers, cells, footers, align, print_latex_vertical(title, headers, cells, footers, align,
opt->tuples_only, opt->numericLocale, opt->tuples_only,
border, output); border, output);
else else
print_latex_text(title, headers, cells, footers, align, print_latex_text(title, headers, cells, footers, align,
opt->tuples_only, opt->numericLocale, opt->tuples_only,
border, output); border, output);
break; break;
case PRINT_TROFF_MS: case PRINT_TROFF_MS:
if (use_expanded) if (use_expanded)
print_troff_ms_vertical(title, headers, cells, footers, align, print_troff_ms_vertical(title, headers, cells, footers, align,
opt->tuples_only, opt->numericLocale, opt->tuples_only,
border, output); border, output);
else else
print_troff_ms_text(title, headers, cells, footers, align, print_troff_ms_text(title, headers, cells, footers, align,
opt->tuples_only, opt->numericLocale, opt->tuples_only,
border, output); border, output);
break; break;
default: default:
@ -1606,6 +1507,7 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, FILE *f
int ncells; int ncells;
const char **headers; const char **headers;
const char **cells; const char **cells;
bool *cellmustfree = NULL;
char **footers; char **footers;
char *align; char *align;
int i; int i;
@ -1623,6 +1525,32 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, FILE *f
for (i = 0; i < nfields; i++) for (i = 0; i < nfields; i++)
headers[i] = mbvalidate(PQfname(result, i), opt->topt.encoding); headers[i] = mbvalidate(PQfname(result, i), opt->topt.encoding);
/* set alignment */
align = calloc(nfields + 1, sizeof(*align));
if (!align)
{
fprintf(stderr, _("out of memory\n"));
exit(EXIT_FAILURE);
}
for (i = 0; i < nfields; i++)
{
Oid ftype = PQftype(result, i);
if (ftype == 20 || /* int8 */
ftype == 21 || /* int2 */
ftype == 23 || /* int4 */
(ftype >= 26 && ftype <= 30) || /* ?id */
ftype == 700 || /* float4 */
ftype == 701 || /* float8 */
ftype == 790 || /* money */
ftype == 1700 /* numeric */
)
align[i] = 'r';
else
align[i] = 'l';
}
/* set cells */ /* set cells */
ncells = PQntuples(result) * nfields; ncells = PQntuples(result) * nfields;
cells = calloc(ncells + 1, sizeof(*cells)); cells = calloc(ncells + 1, sizeof(*cells));
@ -1637,7 +1565,30 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, FILE *f
if (PQgetisnull(result, i / nfields, i % nfields)) if (PQgetisnull(result, i / nfields, i % nfields))
cells[i] = opt->nullPrint ? opt->nullPrint : ""; cells[i] = opt->nullPrint ? opt->nullPrint : "";
else else
{
cells[i] = mbvalidate(PQgetvalue(result, i / nfields, i % nfields), opt->topt.encoding); cells[i] = mbvalidate(PQgetvalue(result, i / nfields, i % nfields), opt->topt.encoding);
if (align[i % nfields] == 'r' && opt->topt.numericLocale)
{
cells[i] = format_numeric_locale(cells[i]);
/*
* format_numeric_locale() returns a malloc'd string,
* remember that it needs to be freed.
*/
if (cellmustfree == NULL)
{
cellmustfree = calloc(ncells + 1, sizeof(bool));
if (!cellmustfree)
{
fprintf(stderr, _("out of memory\n"));
exit(EXIT_FAILURE);
}
}
cellmustfree[i] = true;
}
}
} }
/* set footers */ /* set footers */
@ -1694,6 +1645,15 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, FILE *f
align, &opt->topt, fout, flog); align, &opt->topt, fout, flog);
free(headers); free(headers);
if (cellmustfree)
{
for (i = 0; i < ncells; i++)
{
if (cellmustfree[i])
free((char *) cells[i]);
}
free(cellmustfree);
}
free(cells); free(cells);
if (footers) if (footers)
{ {