Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4a5cda7

Browse files
committed
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
1 parent a31ad27 commit 4a5cda7

File tree

4 files changed

+229
-9
lines changed

4 files changed

+229
-9
lines changed

doc/src/sgml/ref/psql-ref.sgml

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.138 2005/06/02 01:23:48 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.139 2005/06/09 15:27:26 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -1386,9 +1386,10 @@ lo_import 152801
13861386
<listitem>
13871387
<para>
13881388
Sets the output format to one of <literal>unaligned</literal>,
1389-
<literal>aligned</literal>, <literal>html</literal>, or
1390-
<literal>latex</literal>. Unique abbreviations are allowed.
1391-
(That would mean one letter is enough.)
1389+
<literal>aligned</literal>, <literal>html</literal>,
1390+
<literal>latex</literal>, or <literal>troff-ms</literal>.
1391+
Unique abbreviations are allowed. (That would mean one letter
1392+
is enough.)
13921393
</para>
13931394

13941395
<para>

src/bin/psql/command.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.143 2005/04/29 13:42:20 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.144 2005/06/09 15:27:26 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "command.h"
@@ -1357,6 +1357,9 @@ _align2string(enum printFormat in)
13571357
case PRINT_LATEX:
13581358
return "latex";
13591359
break;
1360+
case PRINT_TROFF_MS:
1361+
return "troff-ms";
1362+
break;
13601363
}
13611364
return "unknown";
13621365
}
@@ -1385,9 +1388,11 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
13851388
popt->topt.format = PRINT_HTML;
13861389
else if (pg_strncasecmp("latex", value, vallen) == 0)
13871390
popt->topt.format = PRINT_LATEX;
1391+
else if (pg_strncasecmp("troff-ms", value, vallen) == 0)
1392+
popt->topt.format = PRINT_TROFF_MS;
13881393
else
13891394
{
1390-
psql_error("\\pset: allowed formats are unaligned, aligned, html, latex\n");
1395+
psql_error("\\pset: allowed formats are unaligned, aligned, html, latex, troff-ms\n");
13911396
return false;
13921397
}
13931398

src/bin/psql/print.c

+214-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.55 2005/02/22 04:40:57 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.56 2005/06/09 15:27:27 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "common.h"
@@ -992,6 +992,213 @@ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
992992

993993

994994

995+
/*************************/
996+
/* Troff -ms */
997+
/*************************/
998+
999+
1000+
static void
1001+
troff_ms_escaped_print(const char *in, FILE *fout)
1002+
{
1003+
const char *p;
1004+
1005+
for (p = in; *p; p++)
1006+
switch (*p)
1007+
{
1008+
case '\\':
1009+
fputs("\(rs", fout);
1010+
break;
1011+
default:
1012+
fputc(*p, fout);
1013+
}
1014+
}
1015+
1016+
1017+
1018+
static void
1019+
print_troff_ms_text(const char *title, const char *const * headers,
1020+
const char *const * cells, const char *const * footers,
1021+
const char *opt_align, bool opt_barebones, unsigned short int opt_border,
1022+
FILE *fout)
1023+
{
1024+
unsigned int col_count = 0;
1025+
unsigned int i;
1026+
const char *const * ptr;
1027+
1028+
1029+
/* print title */
1030+
if (!opt_barebones && title)
1031+
{
1032+
fputs(".LP\n.DS C\n", fout);
1033+
troff_ms_escaped_print(title, fout);
1034+
fputs("\n.DE\n", fout);
1035+
}
1036+
1037+
/* count columns */
1038+
for (ptr = headers; *ptr; ptr++)
1039+
col_count++;
1040+
1041+
/* begin environment and set alignments and borders */
1042+
fputs(".LP\n.TS\n", fout);
1043+
if (opt_border == 2)
1044+
fputs("center box;\n", fout);
1045+
else
1046+
fputs("center;\n", fout);
1047+
1048+
for (i = 0; i < col_count; i++)
1049+
{
1050+
fputc(*(opt_align + i), fout);
1051+
if (opt_border > 0 && i < col_count - 1)
1052+
fputs(" | ", fout);
1053+
}
1054+
fputs(".\n", fout);
1055+
1056+
/* print headers and count columns */
1057+
for (i = 0, ptr = headers; i < col_count; i++, ptr++)
1058+
{
1059+
if (!opt_barebones)
1060+
{
1061+
if (i != 0)
1062+
fputc('\t', fout);
1063+
fputs("\\fI", fout);
1064+
troff_ms_escaped_print(*ptr, fout);
1065+
fputs("\\fP", fout);
1066+
}
1067+
}
1068+
1069+
if (!opt_barebones)
1070+
{
1071+
fputs("\n_\n", fout);
1072+
}
1073+
1074+
/* print cells */
1075+
for (i = 0, ptr = cells; *ptr; i++, ptr++)
1076+
{
1077+
troff_ms_escaped_print(*ptr, fout);
1078+
1079+
if ((i + 1) % col_count == 0)
1080+
fputc('\n', fout);
1081+
else
1082+
fputc('\t', fout);
1083+
}
1084+
1085+
fputs(".TE\n.DS L\n", fout);
1086+
1087+
1088+
/* print footers */
1089+
1090+
if (footers && !opt_barebones)
1091+
for (ptr = footers; *ptr; ptr++)
1092+
{
1093+
troff_ms_escaped_print(*ptr, fout);
1094+
fputc('\n', fout);
1095+
}
1096+
1097+
fputs(".DE\n", fout);
1098+
}
1099+
1100+
1101+
1102+
static void
1103+
print_troff_ms_vertical(const char *title, const char *const * headers,
1104+
const char *const * cells, const char *const * footers,
1105+
const char *opt_align, bool opt_barebones, unsigned short int opt_border,
1106+
FILE *fout)
1107+
{
1108+
unsigned int col_count = 0;
1109+
unsigned int i;
1110+
const char *const * ptr;
1111+
unsigned int record = 1;
1112+
unsigned short current_format = 0; /* 0=none, 1=header, 2=body */
1113+
1114+
(void) opt_align; /* currently unused parameter */
1115+
1116+
/* print title */
1117+
if (!opt_barebones && title)
1118+
{
1119+
fputs(".LP\n.DS C\n", fout);
1120+
troff_ms_escaped_print(title, fout);
1121+
fputs("\n.DE\n", fout);
1122+
}
1123+
1124+
/* begin environment and set alignments and borders */
1125+
fputs(".LP\n.TS\n", fout);
1126+
if (opt_border == 2)
1127+
fputs("center box;\n", fout);
1128+
else
1129+
fputs("center;\n", fout);
1130+
1131+
/* basic format */
1132+
if (opt_barebones)
1133+
fputs("c l;\n", fout);
1134+
1135+
1136+
/* count columns */
1137+
for (ptr = headers; *ptr; ptr++)
1138+
col_count++;
1139+
1140+
1141+
/* print records */
1142+
for (i = 0, ptr = cells; *ptr; i++, ptr++)
1143+
{
1144+
/* new record */
1145+
if (i % col_count == 0)
1146+
{
1147+
if (!opt_barebones)
1148+
{
1149+
1150+
if (current_format != 1)
1151+
{
1152+
if (opt_border == 2 && i > 0)
1153+
fputs("_\n", fout);
1154+
if (current_format != 0)
1155+
fputs(".T&\n", fout);
1156+
fputs("c s.\n", fout);
1157+
current_format = 1;
1158+
}
1159+
fprintf(fout, "\\fIRecord %d\\fP\n", record++);
1160+
}
1161+
if (opt_border >= 1)
1162+
fputs("_\n", fout);
1163+
}
1164+
1165+
if (!opt_barebones)
1166+
{
1167+
if (current_format != 2)
1168+
{
1169+
if (current_format != 0)
1170+
fputs(".T&\n", fout);
1171+
if (opt_border != 1)
1172+
fputs("c l.\n", fout);
1173+
else
1174+
fputs("c | l.\n", fout);
1175+
current_format = 2;
1176+
}
1177+
}
1178+
1179+
troff_ms_escaped_print(headers[i % col_count], fout);
1180+
fputc('\t', fout);
1181+
troff_ms_escaped_print(*ptr, fout);
1182+
fputc('\n', fout);
1183+
}
1184+
1185+
fputs(".TE\n.DS L\n", fout);
1186+
1187+
1188+
/* print footers */
1189+
1190+
if (footers && !opt_barebones)
1191+
for (ptr = footers; *ptr; ptr++)
1192+
{
1193+
troff_ms_escaped_print(*ptr, fout);
1194+
fputc('\n', fout);
1195+
}
1196+
1197+
fputs(".DE\n", fout);
1198+
}
1199+
1200+
1201+
9951202
/********************************/
9961203
/* Public functions */
9971204
/********************************/
@@ -1121,6 +1328,12 @@ printTable(const char *title,
11211328
else
11221329
print_latex_text(title, headers, cells, footers, align, opt->tuples_only, border, output);
11231330
break;
1331+
case PRINT_TROFF_MS:
1332+
if (opt->expanded)
1333+
print_troff_ms_vertical(title, headers, cells, footers, align, opt->tuples_only, border, output);
1334+
else
1335+
print_troff_ms_text(title, headers, cells, footers, align, opt->tuples_only, border, output);
1336+
break;
11241337
default:
11251338
fprintf(stderr, "+ Oops, you shouldn't see this!\n");
11261339
}

src/bin/psql/print.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/print.h,v 1.22 2005/01/01 05:43:08 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/print.h,v 1.23 2005/06/09 15:27:27 momjian Exp $
77
*/
88
#ifndef PRINT_H
99
#define PRINT_H
@@ -21,7 +21,8 @@ enum printFormat
2121
PRINT_UNALIGNED,
2222
PRINT_ALIGNED,
2323
PRINT_HTML,
24-
PRINT_LATEX
24+
PRINT_LATEX,
25+
PRINT_TROFF_MS
2526
/* add your favourite output format here ... */
2627
};
2728

0 commit comments

Comments
 (0)