Commit 1e9199e8 authored by Alvaro Herrera's avatar Alvaro Herrera

Improve psql's internal print.c code by introducing an actual print API.

Provides for better code readability, but mainly this is infrastructure changes
to allow further changes such as arbitrary footers on printed tables.  Also,
the translation status of each element in the table is more easily customized.

Brendan Jurd, with some editorialization by me.
parent 9340c637
This diff is collapsed.
This diff is collapsed.
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2008, PostgreSQL Global Development Group * Copyright (c) 2000-2008, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/print.h,v 1.36 2008/05/08 17:04:26 momjian Exp $ * $PostgreSQL: pgsql/src/bin/psql/print.h,v 1.37 2008/05/12 22:59:58 alvherre Exp $
*/ */
#ifndef PRINT_H #ifndef PRINT_H
#define PRINT_H #define PRINT_H
...@@ -11,11 +11,6 @@ ...@@ -11,11 +11,6 @@
#include "libpq-fe.h" #include "libpq-fe.h"
extern FILE *PageOutput(int lines, unsigned short int pager);
extern void ClosePager(FILE *pagerpipe);
extern void html_escaped_print(const char *in, FILE *fout);
enum printFormat enum printFormat
{ {
PRINT_NOTHING = 0, /* to make sure someone initializes this */ PRINT_NOTHING = 0, /* to make sure someone initializes this */
...@@ -29,7 +24,7 @@ enum printFormat ...@@ -29,7 +24,7 @@ enum printFormat
}; };
typedef struct _printTableOpt typedef struct printTableOpt
{ {
enum printFormat format; /* one of the above */ enum printFormat format; /* one of the above */
bool expanded; /* expanded/vertical output (if supported by bool expanded; /* expanded/vertical output (if supported by
...@@ -52,25 +47,42 @@ typedef struct _printTableOpt ...@@ -52,25 +47,42 @@ typedef struct _printTableOpt
int columns; /* target width for wrapped format */ int columns; /* target width for wrapped format */
} printTableOpt; } printTableOpt;
/* /*
* Use this to print just any table in the supported formats. * Table footers are implemented as a singly-linked list.
* - title is just any string (NULL is fine) *
* - headers is the column headings (NULL ptr terminated). It must be given and * This is so that you don't need to know the number of footers in order to
* complete since the column count is generated from this. * initialise the printTableContent struct, which is very convenient when
* - cells are the data cells to be printed. Now you know why the correct * preparing complex footers (as in describeOneTableDetails).
* column count is important */
* - footers are lines to be printed below the table typedef struct printTableFooter
* - 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.) char *data;
*/ struct printTableFooter *next;
void printTable(const char *title, const char *const * headers, } printTableFooter;
const char *const * cells, const char *const * footers,
const char *align,
const printTableOpt *opt, FILE *fout, FILE *flog);
/*
* The table content struct holds all the information which will be displayed
* by printTable().
*/
typedef struct printTableContent
{
const printTableOpt *opt;
const char *title; /* May be NULL */
int ncolumns; /* Specified in Init() */
int nrows; /* Specified in Init() */
const char **headers; /* NULL-terminated array of header strings */
const char **header; /* Pointer to the last added header */
const char **cells; /* NULL-terminated array of cell content
strings */
const char **cell; /* Pointer to the last added cell */
printTableFooter *footers; /* Pointer to the first footer */
printTableFooter *footer; /* Pointer to the last added footer */
char *aligns; /* Array of alignment specifiers; 'l' or 'r',
one per column */
char *align; /* Pointer to the last added alignment */
} printTableContent;
typedef struct _printQueryOpt typedef struct printQueryOpt
{ {
printTableOpt topt; /* the options above */ printTableOpt topt; /* the options above */
char *nullPrint; /* how to print null entities */ char *nullPrint; /* how to print null entities */
...@@ -82,15 +94,29 @@ typedef struct _printQueryOpt ...@@ -82,15 +94,29 @@ typedef struct _printQueryOpt
const bool *trans_columns; /* trans_columns[i-1] => do gettext on col i */ const bool *trans_columns; /* trans_columns[i-1] => do gettext on col i */
} printQueryOpt; } printQueryOpt;
/*
* Use this to print query results extern FILE *PageOutput(int lines, unsigned short int pager);
* extern void ClosePager(FILE *pagerpipe);
* It calls the printTable above with all the things set straight.
*/ extern void html_escaped_print(const char *in, FILE *fout);
void printQuery(const PGresult *result, const printQueryOpt *opt,
extern void printTableInit(printTableContent *const content,
const printTableOpt *opt, const char *title,
const int ncolumns, const int nrows);
extern void printTableAddHeader(printTableContent *const content,
const char *header, const bool translate, const char align);
extern void printTableAddCell(printTableContent *const content,
const char *cell, const bool translate);
extern void printTableAddFooter(printTableContent *const content,
const char *footer);
extern void printTableSetFooter(printTableContent *const content,
const char *footer);
extern void printTableCleanup(printTableContent *const content);
extern void printTable(const printTableContent *cont, FILE *fout, FILE *flog);
extern void printQuery(const PGresult *result, const printQueryOpt *opt,
FILE *fout, FILE *flog); FILE *fout, FILE *flog);
void setDecimalLocale(void); extern void setDecimalLocale(void);
#ifndef __CYGWIN__ #ifndef __CYGWIN__
#define DEFAULT_PAGER "more" #define DEFAULT_PAGER "more"
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/bin/scripts/common.c,v 1.31 2008/01/01 19:45:56 momjian Exp $ * $PostgreSQL: pgsql/src/bin/scripts/common.c,v 1.32 2008/05/12 22:59:58 alvherre Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -229,6 +229,27 @@ executeMaintenanceCommand(PGconn *conn, const char *query, bool echo) ...@@ -229,6 +229,27 @@ executeMaintenanceCommand(PGconn *conn, const char *query, bool echo)
return r; return r;
} }
/*
* "Safe" wrapper around strdup(). Pulled from psql/common.c
*/
char *
pg_strdup(const char *string)
{
char *tmp;
if (!string)
{
fprintf(stderr, _("pg_strdup: cannot duplicate null pointer (internal error)\n"));
exit(EXIT_FAILURE);
}
tmp = strdup(string);
if (!tmp)
{
fprintf(stderr, _("out of memory\n"));
exit(EXIT_FAILURE);
}
return tmp;
}
/* /*
* Check yes/no answer in a localized way. 1=yes, 0=no, -1=neither. * Check yes/no answer in a localized way. 1=yes, 0=no, -1=neither.
...@@ -274,7 +295,6 @@ yesno_prompt(const char *question) ...@@ -274,7 +295,6 @@ yesno_prompt(const char *question)
} }
} }
/* /*
* SetCancelConn * SetCancelConn
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment