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

Commit 8b4da40

Browse files
committed
Change this script to Perl 5 style. Add support for multiple refnames.
Sort the output by command name. This previously only worked by source file name, which doesn't always match the command name exactly. And it certainly won't work for multiple refnames.
1 parent 89ad92a commit 8b4da40

File tree

1 file changed

+34
-46
lines changed

1 file changed

+34
-46
lines changed

src/bin/psql/create_help.pl

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#! /usr/bin/perl
1+
#! /usr/bin/perl -w
22

33
#################################################################
44
# create_help.pl -- converts SGML docs to internal psql help
55
#
66
# Copyright (c) 2000-2008, PostgreSQL Global Development Group
77
#
8-
# $PostgreSQL: pgsql/src/bin/psql/create_help.pl,v 1.17 2008/01/20 21:13:55 tgl Exp $
8+
# $PostgreSQL: pgsql/src/bin/psql/create_help.pl,v 1.18 2008/11/19 09:51:55 petere Exp $
99
#################################################################
1010

1111
#
@@ -19,24 +19,27 @@
1919
# sure does matter to the rest of the source.
2020
#
2121

22-
$docdir = $ARGV[0] || die "$0: missing required argument: docdir\n";
23-
$outputfile = $ARGV[1] || die "$0: missing required argument: output file\n";
22+
use strict;
2423

24+
my $docdir = $ARGV[0] or die "$0: missing required argument: docdir\n";
25+
my $outputfile = $ARGV[1] or die "$0: missing required argument: output file\n";
26+
27+
my $outputfilebasename;
2528
if ($outputfile =~ m!.*/([^/]+)$!) {
2629
$outputfilebasename = $1;
2730
}
2831
else {
2932
$outputfilebasename = $outputfile;
3033
}
3134

32-
$define = $outputfilebasename;
35+
my $define = $outputfilebasename;
3336
$define =~ tr/a-z/A-Z/;
3437
$define =~ s/\W/_/g;
3538

3639
opendir(DIR, $docdir)
37-
|| die "$0: could not open documentation source dir '$docdir': $!\n";
40+
or die "$0: could not open documentation source dir '$docdir': $!\n";
3841
open(OUT, ">$outputfile")
39-
|| die "$0: could not open output file '$outputfile': $!\n";
42+
or die "$0: could not open output file '$outputfile': $!\n";
4043

4144
print OUT
4245
"/*
@@ -64,46 +67,29 @@
6467
static const struct _helpStruct QL_HELP[] = {
6568
";
6669

67-
$count = 0;
68-
$maxlen = 0;
70+
my $maxlen = 0;
71+
72+
my %entries;
6973

70-
foreach $file (sort readdir DIR) {
71-
local ($cmdname, $cmddesc, $cmdsynopsis);
72-
$file =~ /\.sgml$/ || next;
74+
foreach my $file (sort readdir DIR) {
75+
my (@cmdnames, $cmddesc, $cmdsynopsis);
76+
$file =~ /\.sgml$/ or next;
7377

74-
open(FILE, "$docdir/$file") || next;
75-
$filecontent = join('', <FILE>);
78+
open(FILE, "$docdir/$file") or next;
79+
my $filecontent = join('', <FILE>);
7680
close FILE;
7781

7882
# Ignore files that are not for SQL language statements
7983
$filecontent =~ m!<refmiscinfo>\s*SQL - Language Statements\s*</refmiscinfo>!i
80-
|| next;
81-
82-
# Extract <refname>, <refpurpose>, and <synopsis> fields, taking the
83-
# first one if there are more than one. NOTE: we cannot just say
84-
# "<synopsis>(.*)</synopsis>", because that will match the first
85-
# occurrence of <synopsis> and the last one of </synopsis>! Under
86-
# Perl 5 we could use a non-greedy wildcard, .*?, to ensure we match
87-
# the first </synopsis>, but we want this script to run under Perl 4
88-
# too, and Perl 4 hasn't got that feature. So, do it the hard way.
89-
# Also, use [\000-\377] where we want to match anything including
90-
# newline --- Perl 4 does not have Perl 5's /s modifier.
91-
$filecontent =~ m!<refname>\s*([a-z ]*[a-z])\s*</refname>!i && ($cmdname = $1);
92-
if ($filecontent =~ m!<refpurpose>\s*([\000-\377]+)$!i) {
93-
$tmp = $1; # everything after first <refpurpose>
94-
if ($tmp =~ s!\s*</refpurpose>[\000-\377]*$!!i) {
95-
$cmddesc = $tmp;
96-
}
97-
}
98-
if ($filecontent =~ m!<synopsis>\s*([\000-\377]+)$!i) {
99-
$tmp = $1; # everything after first <synopsis>
100-
if ($tmp =~ s!\s*</synopsis>[\000-\377]*$!!i) {
101-
$cmdsynopsis = $tmp;
102-
}
103-
}
84+
or next;
85+
86+
# Collect multiple refnames
87+
LOOP: { $filecontent =~ m!\G.*?<refname>\s*([a-z ]+?)\s*</refname>!cgis and push @cmdnames, $1 and redo LOOP; }
88+
$filecontent =~ m!<refpurpose>\s*(.+?)\s*</refpurpose>!is and $cmddesc = $1;
89+
$filecontent =~ m!<synopsis>\s*(.+?)\s*</synopsis>!is and $cmdsynopsis = $1;
10490

105-
if ($cmdname && $cmddesc && $cmdsynopsis) {
106-
$cmdname =~ s/\"/\\"/g;
91+
if (@cmdnames && $cmddesc && $cmdsynopsis) {
92+
s/\"/\\"/g foreach @cmdnames;
10793

10894
$cmddesc =~ s/<[^>]+>//g;
10995
$cmddesc =~ s/\s+/ /g;
@@ -113,22 +99,24 @@
11399
$cmdsynopsis =~ s/\r?\n/\\n/g;
114100
$cmdsynopsis =~ s/\"/\\"/g;
115101

116-
print OUT " { \"$cmdname\",\n N_(\"$cmddesc\"),\n N_(\"$cmdsynopsis\") },\n\n";
117-
118-
$count++;
119-
$maxlen = ($maxlen >= length $cmdname) ? $maxlen : length $cmdname;
102+
foreach my $cmdname (@cmdnames) {
103+
$entries{$cmdname} = { cmddesc => $cmddesc, cmdsynopsis => $cmdsynopsis };
104+
$maxlen = ($maxlen >= length $cmdname) ? $maxlen : length $cmdname;
105+
}
120106
}
121107
else {
122-
print STDERR "$0: parsing file '$file' failed (N='$cmdname' D='$cmddesc')\n";
108+
die "$0: parsing file '$file' failed (N='@cmdnames' D='$cmddesc')\n";
123109
}
124110
}
125111

112+
print OUT " { \"$_\",\n N_(\"".$entries{$_}{cmddesc}."\"),\n N_(\"".$entries{$_}{cmdsynopsis}."\") },\n\n" foreach (sort keys %entries);
113+
126114
print OUT "
127115
{ NULL, NULL, NULL } /* End of list marker */
128116
};
129117
130118
131-
#define QL_HELP_COUNT $count /* number of help items */
119+
#define QL_HELP_COUNT ".scalar(keys %entries)." /* number of help items */
132120
#define QL_MAX_CMD_LEN $maxlen /* largest strlen(cmd) */
133121
134122

0 commit comments

Comments
 (0)