第二十八章 DragonFlyBSD
第五节 利用脚本自动生成 BSDlibc 库文本
此部分来自 FreeBSD 论坛,作者 mrclksr。原文地址 https://forums.freebsd.org/threads/wheres-bsd-libc-documentation.63107/
首先安装依赖:
1
# pkg install netpbm groff ghostscript9-base
Copied!
1
#!/bin/sh
2
3
pstarget="/tmp/$.libcdoc.ps"
4
pdftarget="libcdoc.pdf"
5
pdftarget_noidx="/tmp/$.$pdftarget"
6
pdfindex="/tmp/$.pdfindex.info"
7
index="/tmp/$.index"
8
sorted_index="$index.sorted"
9
flist="/tmp/$.flist"
10
tocin="/tmp/$.toc.mdoc"
11
keywords="/tmp/$.keywords"
12
mandir="/usr/share/man"
13
paths="$mandir/man2 $mandir/man3"
14
content_offset=0
15
16
mkidx()
17
{
18
for i in `find $paths -name "*.gz"`; do
19
if zgrep -q '.Lb libc' $i && zgrep -q '.Sh LIBRARY' $i; then
20
for j in `gettitles $i`; do
21
echo "$j:$i" >> $index
22
done
23
fi
24
done
25
cat $index | sort -n | uniq | awk -F: 'BEGIN { prev = "" } {
26
if ($1 != prev) {
27
print $0;
28
}
29
prev = $1;
30
}' > $index.tmp
31
mv $index.tmp $index
32
33
for i in `cat $index`; do
34
fname=`echo $i | cut -d: -f2`
35
grep $fname $index | sort -n | awk -F: 'BEGIN {n = 0} {
36
if (n++ > 0)
37
printf ",";
38
printf "%s", $1;
39
}'
40
echo ":$fname"
41
done | sort -n | uniq > $index.tmp
42
mv $index.tmp $index
43
44
currp=1
45
for i in `cat $index`; do
46
fname=`echo $i | cut -d: -f2`
47
kwords=`echo $i | cut -d: -f1`
48
nextp=`mandoc -T ps $fname|egrep '%%Pages: [0-9]+'|cut -d: -f2`
49
echo "$kwords:$currp:$fname" >> $index.tmp
50
currp=`expr $currp + $nextp`
51
done
52
mv $index.tmp $index
53
for i in `cat $index | sed -E 's/(^[^:]+):.*/1/' | tr ',' ' '`; do
54
echo $i
55
done | sort -n > $keywords
56
57
for i in `cat $keywords`; do
58
page=`grep -w $i $index | tail -1 | cut -d: -f 2`
59
echo $i:$page
60
done > $sorted_index
61
}
62
63
mkpsdoc()
64
{
65
for i in `cat $index`; do
66
fname=`echo $i | cut -d: -f3`
67
zcat $fname | sed -e 's/^.Dd.*$/.Dd __PAGENO__/'
68
-e '/.Os.*/d' | mandoc -T ps >> $pstarget
69
done
70
}
71
72
mktoc()
73
{
74
echo ".XS 1" > $tocin
75
echo "Table of Contents" >> $tocin
76
for i in `cat $sorted_index`; do
77
kword=`echo $i | cut -d: -f 1`
78
page=`echo $i | cut -d: -f 2`
79
page=`expr $content_offset + $page`
80
printf ".XA $pagen$kwordn" >> $tocin
81
done
82
echo ".XE" >> $tocin
83
echo ".PX" >> $tocin
84
}
85
86
get_content_offset()
87
{
88
mktoc
89
content_offset=`groff -T ps -ms $tocin | egrep '%%Pages: [0-9]+' |
90
cut -d: -f2`
91
content_offset=`expr $content_offset + 0`
92
}
93
94
prepend_toc()
95
{
96
in=$1
97
tmp=$in.tmp
98
99
groff -T ps -ms $tocin > $tmp
100
cat $in >> $tmp
101
mv $tmp $in
102
}
103
104
mkpdfidx()
105
{
106
printf "[/Page 1 /View [/XYZ null null null] " > $pdfindex
107
printf "/Title (Table of Contents) /OUT pdfmarkn" >> $pdfindex
108
109
for i in `cat $sorted_index`; do
110
kword=`echo $i | cut -d: -f 1`
111
page=`echo $i | cut -d: -f 2`
112
page=`expr $page + $content_offset`
113
printf "[/Page $page /View " >> $pdfindex
114
printf "[/XYZ null null null] " >> $pdfindex
115
printf "/Title ($kword) /OUT pdfmarkn" >> $pdfindex
116
done
117
}
118
119
gettitles()
120
{
121
zcat $1 | sed -n '/.Sh NAME/,/.Sh LIBRARY/p' |
122
egrep '^.Nm [^ ]+' | cut -d" " -f 2 | sort -n | uniq
123
}
124
125
mkidx
126
mkpsdoc
127
get_content_offset
128
mktoc
129
prepend_toc $pstarget
130
mkpdfidx
131
132
cat $pstarget | awk -v p=$content_offset '{
133
if ($0 ~ /(__PAGENO__)/) {
134
t = sprintf("(%s)", ++p);
135
sub(/(__PAGENO__)/, t);
136
}
137
print $0;
138
}' > $pstarget.tmp
139
140
mv $pstarget.tmp $pstarget
141
142
ps2pdf $pstarget $pdftarget_noidx
143
144
gs -sDEVICE=pdfwrite -q -dBATCH -dNOPAUSE -sOutputFile=$pdftarget $pdfindex
145
-f $pdftarget_noidx
146
147
rm -f $tocin
148
rm -f $pstarget
149
rm -f $index
150
rm -f $pdftarget_noidx
151
rm -f $pdfindex
152
rm -f $sorted_index
Copied!
运行脚本即可在同路径文件夹下找到 PDF 文档。现成的文档请看:
Copy link
Edit on GitHub