Fix obsolete comment regarding FSM truncation.
[PostgreSQL.git] / src / bin / psql / create_help.pl
blob7fff50ccafda25a9aecd2cacad1dd165a76e878e
1 #! /usr/bin/perl -w
3 #################################################################
4 # create_help.pl -- converts SGML docs to internal psql help
6 # Copyright (c) 2000-2008, PostgreSQL Global Development Group
8 # $PostgreSQL$
9 #################################################################
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.
17 # Call: perl create_help.pl docdir sql_help.h
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.
22 use strict;
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";
27 my $outputfilebasename;
28 if ($outputfile =~ m!.*/([^/]+)$!) {
29 $outputfilebasename = $1;
31 else {
32 $outputfilebasename = $outputfile;
35 my $define = $outputfilebasename;
36 $define =~ tr/a-z/A-Z/;
37 $define =~ s/\W/_/g;
39 opendir(DIR, $docdir)
40 or die "$0: could not open documentation source dir '$docdir': $!\n";
41 open(OUT, ">$outputfile")
42 or die "$0: could not open output file '$outputfile': $!\n";
44 print OUT
45 "/*
46 * *** Do not change this file by hand. It is automatically
47 * *** generated from the DocBook documentation.
49 * generated by
50 * $^X $0 @ARGV
54 #ifndef $define
55 #define $define
57 #define N_(x) (x) /* gettext noop */
59 struct _helpStruct
61 const char *cmd; /* the command name */
62 const char *help; /* the help associated with it */
63 const char *syntax; /* the syntax associated with it */
67 static const struct _helpStruct QL_HELP[] = {
70 my $maxlen = 0;
72 my %entries;
74 foreach my $file (sort readdir DIR) {
75 my (@cmdnames, $cmddesc, $cmdsynopsis);
76 $file =~ /\.sgml$/ or next;
78 open(FILE, "$docdir/$file") or next;
79 my $filecontent = join('', <FILE>);
80 close FILE;
82 # Ignore files that are not for SQL language statements
83 $filecontent =~ m!<refmiscinfo>\s*SQL - Language Statements\s*</refmiscinfo>!i
84 or next;
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;
91 if (@cmdnames && $cmddesc && $cmdsynopsis) {
92 s/\"/\\"/g foreach @cmdnames;
94 $cmddesc =~ s/<[^>]+>//g;
95 $cmddesc =~ s/\s+/ /g;
96 $cmddesc =~ s/\"/\\"/g;
98 $cmdsynopsis =~ s/<[^>]+>//g;
99 $cmdsynopsis =~ s/\r?\n/\\n/g;
100 $cmdsynopsis =~ s/\"/\\"/g;
102 foreach my $cmdname (@cmdnames) {
103 $entries{$cmdname} = { cmddesc => $cmddesc, cmdsynopsis => $cmdsynopsis };
104 $maxlen = ($maxlen >= length $cmdname) ? $maxlen : length $cmdname;
107 else {
108 die "$0: parsing file '$file' failed (N='@cmdnames' D='$cmddesc')\n";
112 print OUT " { \"$_\",\n N_(\"".$entries{$_}{cmddesc}."\"),\n N_(\"".$entries{$_}{cmdsynopsis}."\") },\n\n" foreach (sort keys %entries);
114 print OUT "
115 { NULL, NULL, NULL } /* End of list marker */
119 #define QL_HELP_COUNT ".scalar(keys %entries)." /* number of help items */
120 #define QL_MAX_CMD_LEN $maxlen /* largest strlen(cmd) */
123 #endif /* $define */
126 close OUT;
127 closedir DIR;