~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/bin/psql/create_help.pl

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/perl
 
2
 
 
3
#################################################################
 
4
# create_help.pl -- converts SGML docs to internal psql help
 
5
#
 
6
# Copyright (c) 2000-2005, PostgreSQL Global Development Group
 
7
#
 
8
# $PostgreSQL: pgsql/src/bin/psql/create_help.pl,v 1.13 2005-01-01 20:44:25 tgl Exp $
 
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.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.
 
20
#
 
21
 
 
22
$docdir = $ARGV[0] || die "$0: missing required argument: docdir\n";
 
23
$outputfile = $ARGV[1] || die "$0: missing required argument: output file\n";
 
24
 
 
25
if ($outputfile =~ m!.*/([^/]+)$!) {
 
26
    $outputfilebasename = $1;
 
27
}
 
28
else {
 
29
    $outputfilebasename = $outputfile;
 
30
}
 
31
 
 
32
$define = $outputfilebasename;
 
33
$define =~ tr/a-z/A-Z/;
 
34
$define =~ s/\W/_/g;
 
35
 
 
36
opendir(DIR, $docdir)
 
37
    || die "$0: could not open documentation source dir '$docdir': $!\n";
 
38
open(OUT, ">$outputfile")
 
39
    || die "$0: could not open output file '$outputfile': $!\n";
 
40
 
 
41
print OUT
 
42
"/*
 
43
 * *** Do not change this file by hand. It is automatically
 
44
 * *** generated from the DocBook documentation.
 
45
 *
 
46
 * generated by
 
47
 *     $^X $0 @ARGV
 
48
 *
 
49
 */
 
50
 
 
51
#ifndef $define
 
52
#define $define
 
53
 
 
54
#define N_(x) (x) /* gettext noop */
 
55
 
 
56
struct _helpStruct
 
57
{
 
58
    char           *cmd;           /* the command name */
 
59
    char           *help;          /* the help associated with it */
 
60
    char           *syntax;        /* the syntax associated with it */
 
61
};
 
62
 
 
63
 
 
64
static struct _helpStruct QL_HELP[] = {
 
65
";
 
66
 
 
67
$count = 0;
 
68
 
 
69
foreach $file (sort readdir DIR) {
 
70
    local ($cmdname, $cmddesc, $cmdsynopsis);
 
71
    $file =~ /\.sgml$/ || next;
 
72
 
 
73
    open(FILE, "$docdir/$file") || next;
 
74
    $filecontent = join('', <FILE>);
 
75
    close FILE;
 
76
 
 
77
    # Ignore files that are not for SQL language statements
 
78
    $filecontent =~ m!<refmiscinfo>\s*SQL - Language Statements\s*</refmiscinfo>!i
 
79
        || next;
 
80
 
 
81
    # Extract <refname>, <refpurpose>, and <synopsis> fields, taking the
 
82
    # first one if there are more than one.  NOTE: we cannot just say
 
83
    # "<synopsis>(.*)</synopsis>", because that will match the first
 
84
    # occurrence of <synopsis> and the last one of </synopsis>!  Under
 
85
    # Perl 5 we could use a non-greedy wildcard, .*?, to ensure we match
 
86
    # the first </synopsis>, but we want this script to run under Perl 4
 
87
    # too, and Perl 4 hasn't got that feature.  So, do it the hard way.
 
88
    # Also, use [\000-\377] where we want to match anything including
 
89
    # newline --- Perl 4 does not have Perl 5's /s modifier.
 
90
    $filecontent =~ m!<refname>\s*([a-z ]*[a-z])\s*</refname>!i && ($cmdname = $1);
 
91
    if ($filecontent =~ m!<refpurpose>\s*([\000-\377]+)$!i) {
 
92
        $tmp = $1;              # everything after first <refpurpose>
 
93
        if ($tmp =~ s!\s*</refpurpose>[\000-\377]*$!!i) {
 
94
            $cmddesc = $tmp;
 
95
        }
 
96
    }
 
97
    if ($filecontent =~ m!<synopsis>\s*([\000-\377]+)$!i) {
 
98
        $tmp = $1;              # everything after first <synopsis>
 
99
        if ($tmp =~ s!\s*</synopsis>[\000-\377]*$!!i) {
 
100
            $cmdsynopsis = $tmp;
 
101
        }
 
102
    }
 
103
 
 
104
    if ($cmdname && $cmddesc && $cmdsynopsis) {
 
105
        $cmdname =~ s/\"/\\"/g;
 
106
 
 
107
        $cmddesc =~ s/<[^>]+>//g;
 
108
        $cmddesc =~ s/\s+/ /g;
 
109
        $cmddesc =~ s/\"/\\"/g;
 
110
 
 
111
        $cmdsynopsis =~ s/<[^>]+>//g;
 
112
        $cmdsynopsis =~ s/\r?\n/\\n/g;
 
113
        $cmdsynopsis =~ s/\"/\\"/g;
 
114
 
 
115
        print OUT "    { \"$cmdname\",\n      N_(\"$cmddesc\"),\n      N_(\"$cmdsynopsis\") },\n\n";
 
116
        $count++;
 
117
    }
 
118
    else {
 
119
        print STDERR "$0: parsing file '$file' failed (N='$cmdname' D='$cmddesc')\n";
 
120
    }
 
121
}
 
122
 
 
123
print OUT "
 
124
    { NULL, NULL, NULL }    /* End of list marker */
 
125
};
 
126
 
 
127
 
 
128
#define QL_HELP_COUNT $count
 
129
 
 
130
 
 
131
#endif /* $define */
 
132
";
 
133
 
 
134
close OUT;
 
135
closedir DIR;