-
Notifications
You must be signed in to change notification settings - Fork 172
/
mkman
executable file
·217 lines (194 loc) · 6.55 KB
/
mkman
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#! /usr/bin/perl
#
# mkman - Generates man pages from C source and header files.
#
# Syntax: './mkman srcname [outp rootsrcdir]'
# Must be executed in doc/ subdirectory under build workspace root.
# srcname - basename of .c or .cc source file located directly in the
# $rootsrcdir/src/ directory. For classes (MAN3) also implies
# a header (.h) file located directly in $rootsrcdir/include/.
# outp - (optional) filename without extension (will be chopped if
# provided) of the resulting TXT and DOC files; may be a
# slash-separated path e.g. for out-of-tree builds (distcheck).
# Defaults to: "$srcname"
# rootsrcdir - (optional) location of project source root (may differ
# from build root) where to look for input source-code files.
# Defaults to: ".." (good for in-tree builds)
#
# Copyright (c) 1996-2016 iMatix Corporation
# Copyright (c) 2016-2017 zproject community
#
# This is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# This software is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABIL-
# ITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
use File::Basename;
use Cwd;
sub pull {
local ($_) = @_;
if (/^(.*)(@[a-zA-Z0-9]+)(,(\w*)\s*)?/) {
$file = $1;
$tag = $2;
$opts = $4;
$text = "";
$ext = (fileparse("$file", qr/[^.]*/))[2];
die "Can't read '$file': $!"
unless open (SOURCE, $file);
while (<SOURCE>) {
if (/$tag/) {
while (<SOURCE>) {
last if /\@discuss/ || /\@end/ || /\@ignore/ || /\@header/;
$_ = " $_" if ($opts eq "code");
s/^ // if ($opts eq "left");
$_ = " $_" if ($opts eq "test");
s/^ / / if ($opts eq "test");
$text .= $_;
}
}
}
close (SOURCE);
# Add code fences for markdown highlighting
$text = "```$ext\n$text```\n" if (length $text) and ($opts eq "code" or $opts eq "test");
$text = "Please add '$tag' section in '$file'.\n" unless $text;
return $text;
}
else {
print "E: bad pull request: $_\n";
}
}
sub generate_manpage {
local ($name, $outp, $rootsrcdir) = @_;
$name = $1 if $name =~ /(\w+)\.\w+/; # Chop off extensions
$outp = $1 if $outp =~ /^(.+)\.[^\.\/]+$/;
$outp_basename = basename ($outp);
$outp_basename = $2 if $outp =~ /^(.*\/)?(\w+)$/;
if ($ENV{"V"} == "1") {
printf "D: generate_manpage() got name='$name' outp='$outp' outp_basename='$outp_basename' rootsrcdir='$rootsrcdir'\n";
}
# Check if we're making the man page for a main program, or a class
$cat = 0; # Unknown category
$cat_text = "";
die "Can't open '" . cwd() . "/Makefile'"
unless open (MAKEFILE, "Makefile");
while (<MAKEFILE>) {
if (/MAN1.*$outp_basename\.1/) {
$source = "$rootsrcdir/src/$name.c";
$header = "$rootsrcdir/src/$name.c";
$cat = 1;
$cat_text = "Program for ";
last;
}
elsif (/MAN3.*$outp_basename\.3/) {
$source = "$rootsrcdir/src/$name.c";
$header = "$rootsrcdir/include/$name.h";
$cat = 3;
$cat_text = "Class for ";
last;
}
}
close MAKEFILE;
die "The Makefile defined no manpage category for $outp_basename.1 or $name.3"
unless ($source ne "");
# Look for class title in 2nd line of source
# If there's no class file, leave hand-written man page alone
if (! open (SOURCE, $source)) {
# Support mixed-source projects (named C in config,
# but having both .c and .cc files in reality)
printf "Can't open C '$source', retry for C++\n";
if ($header eq $source) {
$header .= "c";
}
$source .= "c"; # Retry for a *.cc filename
}
die "Can't open '$source'"
unless open (SOURCE, $source);
# NOTE: This duplication of lines is needed for proper work:
$_ = <SOURCE>;
$_ = <SOURCE>;
$title = "no title found";
$title = $1 if (/ \w+ - (.*)/);
close (SOURCE);
printf " MKMAN[txt]\t$source ";
if ($source ne $header) {
printf "+ $header ";
}
printf "\t=> $outp.txt\n";
# Open output file
die "Can't create '$outp.txt': $!"
unless open (OUTPUT, ">$outp.txt");
$underline = "=" x (length ($name) + 3);
$template = <<"END";
$name($cat)
$underline
NAME
----
$outp_basename - $cat_text$title
SYNOPSIS
--------
----
pull $header\@interface
pull $source\@interface,left
----
DESCRIPTION
-----------
pull $source\@header,left
pull $source\@discuss,left
EXAMPLE
-------
.From $name\_test method
----
pull $source\@selftest,left
----
END
# Now process template
for (split /^/, $template) {
if (/^pull (.*)$/) {
print OUTPUT pull ($1);
}
else {
print OUTPUT $_;
}
}
close OUTPUT;
# Generate a simple text documentation for README.txt
printf " MKMAN[doc]\t$source ";
if ($source ne $header) {
printf "+ $header ";
}
printf "\t=> $outp.doc\n";
die "Can't create '$outp.doc': $!"
unless open (OUTPUT, ">$outp.doc");
print OUTPUT "#### $outp_basename - $title\n\n";
print OUTPUT pull ("$source\@header,left");
print OUTPUT "\n";
print OUTPUT pull ("$source\@discuss,left");
print OUTPUT "\nThis is the class interface:\n\n";
print OUTPUT pull ("$header\@interface,code");
print OUTPUT pull ("$source\@interface,left");
print OUTPUT "\nThis is the class self test code:\n\n";
print OUTPUT pull ("$source\@selftest,test");
print OUTPUT "\n";
close OUTPUT;
}
$name = shift (@ARGV);
$outp = shift (@ARGV);
if (!$outp) {
$outp=$name;
}
$rootsrcdir = shift (@ARGV);
if (!$rootsrcdir) {
$rootsrcdir="..";
}
if ($ENV{"V"} == "1") {
printf "D: got name='$name' outp='$outp' rootsrcdir='$rootsrcdir' from the start\n";
}
generate_manpage ($name, $outp, $rootsrcdir);