aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBruce Momjian <bruce@momjian.us>2005-06-09 15:27:27 +0000
committerBruce Momjian <bruce@momjian.us>2005-06-09 15:27:27 +0000
commit4a5cda7bba7c5aee9298f5bc3f287b299cdee634 (patch)
treee6133c692122c96b78a4c360ce257225893c25bb /src
parenta31ad27fc5dc32a1453233575b3cf7b5c34cf515 (diff)
downloadpostgresql-4a5cda7bba7c5aee9298f5bc3f287b299cdee634.tar.gz
postgresql-4a5cda7bba7c5aee9298f5bc3f287b299cdee634.zip
I've created a patch which adds support for troff "-ms" output to
psql. i.e. "\pset format troff-ms". The patch also corrects some problems with the "latex" format, notably defining an extra column in the output table, and correcting some alignment issues; it also changes the output to match the border setting as documented in the manual page and as shown with the "aligned" format. The troff-ms output is mostly identical to the latex output allowing for the differences between the two typesetters. The output should be saved in a file and piped as follows: cat file | tbl | troff -T ps -ms > file.ps or tbl file | troff -T ps -ms > file.ps Because it contains tabs, you'll need to redirect psql output or use "script", rather than pasting from a terminal window, due to the tabs which can be replaced with spaces. Roger Leigh
Diffstat (limited to 'src')
-rw-r--r--src/bin/psql/command.c9
-rw-r--r--src/bin/psql/print.c215
-rw-r--r--src/bin/psql/print.h5
3 files changed, 224 insertions, 5 deletions
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 7523f67eb63..5b646ee130f 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -3,7 +3,7 @@
*
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
*
- * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.143 2005/04/29 13:42:20 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.144 2005/06/09 15:27:26 momjian Exp $
*/
#include "postgres_fe.h"
#include "command.h"
@@ -1357,6 +1357,9 @@ _align2string(enum printFormat in)
case PRINT_LATEX:
return "latex";
break;
+ case PRINT_TROFF_MS:
+ return "troff-ms";
+ break;
}
return "unknown";
}
@@ -1385,9 +1388,11 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
popt->topt.format = PRINT_HTML;
else if (pg_strncasecmp("latex", value, vallen) == 0)
popt->topt.format = PRINT_LATEX;
+ else if (pg_strncasecmp("troff-ms", value, vallen) == 0)
+ popt->topt.format = PRINT_TROFF_MS;
else
{
- psql_error("\\pset: allowed formats are unaligned, aligned, html, latex\n");
+ psql_error("\\pset: allowed formats are unaligned, aligned, html, latex, troff-ms\n");
return false;
}
diff --git a/src/bin/psql/print.c b/src/bin/psql/print.c
index 1321e99582b..e0848624170 100644
--- a/src/bin/psql/print.c
+++ b/src/bin/psql/print.c
@@ -3,7 +3,7 @@
*
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
*
- * $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.55 2005/02/22 04:40:57 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.56 2005/06/09 15:27:27 momjian Exp $
*/
#include "postgres_fe.h"
#include "common.h"
@@ -992,6 +992,213 @@ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
+/*************************/
+/* Troff -ms */
+/*************************/
+
+
+static void
+troff_ms_escaped_print(const char *in, FILE *fout)
+{
+ const char *p;
+
+ for (p = in; *p; p++)
+ switch (*p)
+ {
+ case '\\':
+ fputs("\(rs", fout);
+ break;
+ default:
+ fputc(*p, fout);
+ }
+}
+
+
+
+static void
+print_troff_ms_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 *const * ptr;
+
+
+ /* print title */
+ if (!opt_barebones && title)
+ {
+ fputs(".LP\n.DS C\n", fout);
+ troff_ms_escaped_print(title, fout);
+ fputs("\n.DE\n", fout);
+ }
+
+ /* count columns */
+ for (ptr = headers; *ptr; ptr++)
+ col_count++;
+
+ /* begin environment and set alignments and borders */
+ fputs(".LP\n.TS\n", fout);
+ if (opt_border == 2)
+ fputs("center box;\n", fout);
+ else
+ fputs("center;\n", fout);
+
+ for (i = 0; i < col_count; i++)
+ {
+ fputc(*(opt_align + i), fout);
+ if (opt_border > 0 && i < col_count - 1)
+ fputs(" | ", fout);
+ }
+ fputs(".\n", fout);
+
+ /* print headers and count columns */
+ for (i = 0, ptr = headers; i < col_count; i++, ptr++)
+ {
+ if (!opt_barebones)
+ {
+ if (i != 0)
+ fputc('\t', fout);
+ fputs("\\fI", fout);
+ troff_ms_escaped_print(*ptr, fout);
+ fputs("\\fP", fout);
+ }
+ }
+
+ if (!opt_barebones)
+ {
+ fputs("\n_\n", fout);
+ }
+
+ /* print cells */
+ for (i = 0, ptr = cells; *ptr; i++, ptr++)
+ {
+ troff_ms_escaped_print(*ptr, fout);
+
+ if ((i + 1) % col_count == 0)
+ fputc('\n', fout);
+ else
+ fputc('\t', fout);
+ }
+
+ fputs(".TE\n.DS L\n", fout);
+
+
+ /* print footers */
+
+ if (footers && !opt_barebones)
+ for (ptr = footers; *ptr; ptr++)
+ {
+ troff_ms_escaped_print(*ptr, fout);
+ fputc('\n', fout);
+ }
+
+ fputs(".DE\n", fout);
+}
+
+
+
+static void
+print_troff_ms_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;
+ const char *const * ptr;
+ unsigned int record = 1;
+ unsigned short current_format = 0; /* 0=none, 1=header, 2=body */
+
+ (void) opt_align; /* currently unused parameter */
+
+ /* print title */
+ if (!opt_barebones && title)
+ {
+ fputs(".LP\n.DS C\n", fout);
+ troff_ms_escaped_print(title, fout);
+ fputs("\n.DE\n", fout);
+ }
+
+ /* begin environment and set alignments and borders */
+ fputs(".LP\n.TS\n", fout);
+ if (opt_border == 2)
+ fputs("center box;\n", fout);
+ else
+ fputs("center;\n", fout);
+
+ /* basic format */
+ if (opt_barebones)
+ fputs("c l;\n", fout);
+
+
+ /* count columns */
+ for (ptr = headers; *ptr; ptr++)
+ col_count++;
+
+
+ /* print records */
+ for (i = 0, ptr = cells; *ptr; i++, ptr++)
+ {
+ /* new record */
+ if (i % col_count == 0)
+ {
+ if (!opt_barebones)
+ {
+
+ if (current_format != 1)
+ {
+ if (opt_border == 2 && i > 0)
+ fputs("_\n", fout);
+ if (current_format != 0)
+ fputs(".T&\n", fout);
+ fputs("c s.\n", fout);
+ current_format = 1;
+ }
+ fprintf(fout, "\\fIRecord %d\\fP\n", record++);
+ }
+ if (opt_border >= 1)
+ fputs("_\n", fout);
+ }
+
+ if (!opt_barebones)
+ {
+ if (current_format != 2)
+ {
+ if (current_format != 0)
+ fputs(".T&\n", fout);
+ if (opt_border != 1)
+ fputs("c l.\n", fout);
+ else
+ fputs("c | l.\n", fout);
+ current_format = 2;
+ }
+ }
+
+ troff_ms_escaped_print(headers[i % col_count], fout);
+ fputc('\t', fout);
+ troff_ms_escaped_print(*ptr, fout);
+ fputc('\n', fout);
+ }
+
+ fputs(".TE\n.DS L\n", fout);
+
+
+ /* print footers */
+
+ if (footers && !opt_barebones)
+ for (ptr = footers; *ptr; ptr++)
+ {
+ troff_ms_escaped_print(*ptr, fout);
+ fputc('\n', fout);
+ }
+
+ fputs(".DE\n", fout);
+}
+
+
+
/********************************/
/* Public functions */
/********************************/
@@ -1121,6 +1328,12 @@ printTable(const char *title,
else
print_latex_text(title, headers, cells, footers, align, opt->tuples_only, border, output);
break;
+ case PRINT_TROFF_MS:
+ if (opt->expanded)
+ print_troff_ms_vertical(title, headers, cells, footers, align, opt->tuples_only, border, output);
+ else
+ print_troff_ms_text(title, headers, cells, footers, align, opt->tuples_only, border, output);
+ break;
default:
fprintf(stderr, "+ Oops, you shouldn't see this!\n");
}
diff --git a/src/bin/psql/print.h b/src/bin/psql/print.h
index b30d302883a..8560ebd737f 100644
--- a/src/bin/psql/print.h
+++ b/src/bin/psql/print.h
@@ -3,7 +3,7 @@
*
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
*
- * $PostgreSQL: pgsql/src/bin/psql/print.h,v 1.22 2005/01/01 05:43:08 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/print.h,v 1.23 2005/06/09 15:27:27 momjian Exp $
*/
#ifndef PRINT_H
#define PRINT_H
@@ -21,7 +21,8 @@ enum printFormat
PRINT_UNALIGNED,
PRINT_ALIGNED,
PRINT_HTML,
- PRINT_LATEX
+ PRINT_LATEX,
+ PRINT_TROFF_MS
/* add your favourite output format here ... */
};