| 1 |
#! /usr/bin/perl |
| 2 |
|
| 3 |
################################################################# |
| 4 |
# create_help.pl -- converts SGML docs to internal psql help |
| 5 |
# |
| 6 |
# Copyright (c) 2000-2025, PostgreSQL Global Development Group |
| 7 |
# |
| 8 |
# src/bin/psql/create_help.pl |
| 9 |
################################################################# |
| 10 |
|
| 11 |
# |
| 12 |
# This script automatically generates the help on SQL in psql from |
| 13 |
# the SGML docs. So far the format of the docs was consistent |
| 14 |
# enough that this worked, but this here is by no means an SGML |
| 15 |
# parser. |
| 16 |
# |
| 17 |
# Call: perl create_help.pl docdir sql_help |
| 18 |
# The name of the header file doesn't matter to this script, but it |
| 19 |
# sure does matter to the rest of the source. |
| 20 |
# |
| 21 |
|
| 22 |
use strict; |
| 23 |
use warnings FATAL => 'all'; |
| 24 |
use Getopt::Long; |
| 25 |
|
| 26 |
my $docdir = ''; |
| 27 |
my $outdir = '.'; |
| 28 |
my $depfile = ''; |
| 29 |
my $hfilebasename = ''; |
| 30 |
|
| 31 |
GetOptions( |
| 32 |
'docdir=s' => \$docdir, |
| 33 |
'outdir=s' => \$outdir, |
| 34 |
'basename=s' => \$hfilebasename, |
| 35 |
'depfile=s' => \$depfile,) or die "$0: wrong arguments"; |
| 36 |
|
| 37 |
$docdir or die "$0: missing required argument: docdir\n"; |
| 38 |
$hfilebasename or die "$0: missing required argument: basename\n"; |
| 39 |
|
| 40 |
my $hfile = $hfilebasename . '.h'; |
| 41 |
my $cfile = $hfilebasename . '.c'; |
| 42 |
|
| 43 |
my $define = $hfilebasename; |
| 44 |
$define =~ tr/a-z/A-Z/; |
| 45 |
$define =~ s/\W/_/g; |
| 46 |
|
| 47 |
opendir(my $dh, $docdir) |
| 48 |
or die "$0: could not open documentation source dir '$docdir': $!\n"; |
| 49 |
open(my $hfile_handle, '>', "$outdir/$hfile") |
| 50 |
or die "$0: could not open output file '$hfile': $!\n"; |
| 51 |
open(my $cfile_handle, '>', "$outdir/$cfile") |
| 52 |
or die "$0: could not open output file '$cfile': $!\n"; |
| 53 |
|
| 54 |
my $depfile_handle; |
| 55 |
if ($depfile) |
| 56 |
{ |
| 57 |
open($depfile_handle, '>', $depfile) |
| 58 |
or die "$0: could not open output file '$depfile': $!\n"; |
| 59 |
} |
| 60 |
|
| 61 |
print $hfile_handle "/* |
| 62 |
* *** Do not change this file by hand. It is automatically |
| 63 |
* *** generated from the DocBook documentation. |
| 64 |
* |
| 65 |
* generated by src/bin/psql/create_help.pl |
| 66 |
* |
| 67 |
*/ |
| 68 |
|
| 69 |
#ifndef $define |
| 70 |
#define $define |
| 71 |
|
| 72 |
#include \"pqexpbuffer.h\" |
| 73 |
|
| 74 |
struct _helpStruct |
| 75 |
{ |
| 76 |
const char *cmd; /* the command name */ |
| 77 |
const char *help; /* the help associated with it */ |
| 78 |
const char *docbook_id; /* DocBook XML id (for generating URL) */ |
| 79 |
void (*syntaxfunc) (PQExpBuffer); /* function that prints the |
| 80 |
* syntax associated with it */ |
| 81 |
int nl_count; /* number of newlines in syntax (for pager) */ |
| 82 |
}; |
| 83 |
|
| 84 |
extern const struct _helpStruct QL_HELP[]; |
| 85 |
"; |
| 86 |
|
| 87 |
print $cfile_handle "/* |
| 88 |
* *** Do not change this file by hand. It is automatically |
| 89 |
* *** generated from the DocBook documentation. |
| 90 |
* |
| 91 |
* generated by src/bin/psql/create_help.pl |
| 92 |
* |
| 93 |
*/ |
| 94 |
|
| 95 |
#define N_(x) (x) /* gettext noop */ |
| 96 |
|
| 97 |
#include \"postgres_fe.h\" |
| 98 |
#include \"$hfile\" |
| 99 |
|
| 100 |
"; |
| 101 |
|
| 102 |
my $maxlen = 0; |
| 103 |
|
| 104 |
my %entries; |
| 105 |
|
| 106 |
foreach my $file (sort readdir $dh) |
| 107 |
{ |
| 108 |
my ($cmdid, @cmdnames, $cmddesc, $cmdsynopsis); |
| 109 |
$file =~ /\.sgml$/ or next; |
| 110 |
|
| 111 |
print $depfile_handle "$outdir/$cfile $outdir/$hfile: $docdir/$file\n" |
| 112 |
if ($depfile); |
| 113 |
|
| 114 |
open(my $fh, '<', "$docdir/$file") or next; |
| 115 |
my $filecontent = join('', <$fh>); |
| 116 |
close $fh; |
| 117 |
|
| 118 |
# Ignore files that are not for SQL language statements |
| 119 |
$filecontent =~ |
| 120 |
m!<refmiscinfo>\s*SQL - Language Statements\s*</refmiscinfo>!i |
| 121 |
or next; |
| 122 |
|
| 123 |
$filecontent =~ m!<refentry id="([a-z-]+)">! |
| 124 |
and $cmdid = $1; |
| 125 |
|
| 126 |
# Collect multiple refnames |
| 127 |
LOOP: |
| 128 |
{ |
| 129 |
$filecontent =~ m!\G.*?<refname>\s*([a-z ]+?)\s*</refname>!cgis |
| 130 |
and push @cmdnames, $1 |
| 131 |
and redo LOOP; |
| 132 |
} |
| 133 |
$filecontent =~ m!<refpurpose>\s*(.+?)\s*</refpurpose>!is |
| 134 |
and $cmddesc = $1; |
| 135 |
$filecontent =~ m!<synopsis>\s*(.+?)\s*</synopsis>!is |
| 136 |
and $cmdsynopsis = $1; |
| 137 |
|
| 138 |
if (@cmdnames && $cmddesc && $cmdid && $cmdsynopsis) |
| 139 |
{ |
| 140 |
s/\"/\\"/g foreach @cmdnames; |
| 141 |
|
| 142 |
$cmddesc =~ s/<[^>]+>//g; |
| 143 |
$cmddesc =~ s/\s+/ /g; |
| 144 |
$cmddesc =~ s/\"/\\"/g; |
| 145 |
|
| 146 |
my @params = (); |
| 147 |
|
| 148 |
my $nl_count = () = $cmdsynopsis =~ /\n/g; |
| 149 |
|
| 150 |
$cmdsynopsis =~ s/%/%%/g; |
| 151 |
|
| 152 |
while ($cmdsynopsis =~ m!<(\w+)[^>]*>(.+?)</\1[^>]*>!) |
| 153 |
{ |
| 154 |
my $match = $2; |
| 155 |
$match =~ s/<[^>]+>//g; |
| 156 |
$match =~ s/%%/%/g; |
| 157 |
push @params, $match; |
| 158 |
$cmdsynopsis =~ s!<(\w+)[^>]*>.+?</\1[^>]*>!%s!; |
| 159 |
} |
| 160 |
$cmdsynopsis =~ s/\r?\n/\\n/g; |
| 161 |
$cmdsynopsis =~ s/\"/\\"/g; |
| 162 |
|
| 163 |
foreach my $cmdname (@cmdnames) |
| 164 |
{ |
| 165 |
$entries{$cmdname} = { |
| 166 |
cmdid => $cmdid, |
| 167 |
cmddesc => $cmddesc, |
| 168 |
cmdsynopsis => $cmdsynopsis, |
| 169 |
params => \@params, |
| 170 |
nl_count => $nl_count |
| 171 |
}; |
| 172 |
$maxlen = |
| 173 |
($maxlen >= length $cmdname) ? $maxlen : length $cmdname; |
| 174 |
} |
| 175 |
} |
| 176 |
else |
| 177 |
{ |
| 178 |
die "$0: parsing file '$file' failed (N='@cmdnames' D='$cmddesc')\n"; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
foreach (sort keys %entries) |
| 183 |
{ |
| 184 |
my $prefix = "\t" x 5 . ' '; |
| 185 |
my $id = $_; |
| 186 |
$id =~ s/ /_/g; |
| 187 |
my $synopsis = "\"$entries{$_}{cmdsynopsis}\""; |
| 188 |
$synopsis =~ s/\\n/\\n"\n$prefix"/g; |
| 189 |
my @args = |
| 190 |
("buf", $synopsis, map("_(\"$_\")", @{ $entries{$_}{params} })); |
| 191 |
print $cfile_handle "static void |
| 192 |
sql_help_$id(PQExpBuffer buf) |
| 193 |
{ |
| 194 |
\tappendPQExpBuffer(" . join(",\n$prefix", @args) . "); |
| 195 |
} |
| 196 |
|
| 197 |
"; |
| 198 |
} |
| 199 |
|
| 200 |
print $cfile_handle " |
| 201 |
const struct _helpStruct QL_HELP[] = { |
| 202 |
"; |
| 203 |
foreach (sort keys %entries) |
| 204 |
{ |
| 205 |
my $id = $_; |
| 206 |
$id =~ s/ /_/g; |
| 207 |
print $cfile_handle "\t{\"$_\", |
| 208 |
\t\tN_(\"$entries{$_}{cmddesc}\"), |
| 209 |
\t\t\"$entries{$_}{cmdid}\", |
| 210 |
\t\tsql_help_$id, |
| 211 |
\t$entries{$_}{nl_count}}, |
| 212 |
|
| 213 |
"; |
| 214 |
} |
| 215 |
|
| 216 |
print $cfile_handle " |
| 217 |
\t{NULL, NULL, NULL}\t\t\t/* End of list marker */ |
| 218 |
}; |
| 219 |
"; |
| 220 |
|
| 221 |
print $hfile_handle " |
| 222 |
#define QL_HELP_COUNT " |
| 223 |
. scalar(keys %entries) . " /* number of help items */ |
| 224 |
#define QL_MAX_CMD_LEN $maxlen /* largest strlen(cmd) */ |
| 225 |
|
| 226 |
|
| 227 |
#endif /* $define */ |
| 228 |
"; |
| 229 |
|
| 230 |
close $cfile_handle; |
| 231 |
close $hfile_handle; |
| 232 |
close $depfile_handle if ($depfile); |
| 233 |
closedir $dh; |