1 # SPDX-License-Identifier: GPL-2.0-only
2 ########################################################################
4 # (C) Copyright 2020-2022, Alejandro Colomar
5 # These functions are free software; you can redistribute them and/or
6 # modify them under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; version 2.
9 # These functions are distributed in the hope that they will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details
13 # (http://www.gnu.org/licenses/gpl-2.0.html).
15 ########################################################################
17 ########################################################################
22 ########################################################################
25 # sed_rm_ccomments() removes C comments.
26 # It can't handle mixed //... and /*...*/ comments.
27 # Use as a filter (see man_lsfunc() in this file).
31 perl -p -e 's%/\*.*?\*/%%g' \
32 |sed -E '\%/\*%, \%\*/% {\%(\*/|/\*)%!d}' \
33 |sed -E '\%/\*% {s%/\*.*%%; n; s%.*\*/%%;}' \
34 |sed -E '\%/\*% {s%/\*.*%%; n; s%.*\*/%%;}' \
38 ########################################################################
41 # man_section() prints specific manual page sections (DESCRIPTION, SYNOPSIS,
42 # ...) of all manual pages in a directory (or in a single manual page file).
43 # Usage example: .../man-pages$ man_section man2 SYNOPSIS 'SEE ALSO';
48 >&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>...";
56 find "$page" -type f \
58 |grep -v -e '\b1 ' -e '\btotal\b' \
61 |while read -r manpage; do
62 (sed -n '/^\.TH/,/^\.SH/{/^\.SH/!p}' <"$manpage";
67 -e "/^\.SH $s/,/^\.SH/{/^\.SH/!p}";
69 |mandoc -Tutf8 2>/dev/null \
74 # man_lsfunc() prints the name of all C functions declared in the SYNOPSIS
75 # of all manual pages in a directory (or in a single manual page file).
76 # Each name is printed in a separate line
77 # Usage example: .../man-pages$ man_lsfunc man2;
82 >&2 echo "Usage: ${FUNCNAME[0]} <manpage|manNdir>...";
87 man_section "$arg" 'SYNOPSIS';
90 |pcregrep -Mn '(?s)^ [\w ]+ \**\w+\([\w\s(,)[\]*]*?(...)?\s*\); *$' \
92 |sed -E 's/syscall\(SYS_(\w*),?/\1(/' \
93 |sed -E 's/^[^(]+ \**(\w+)\(.*/\1/' \
97 # man_lsvar() prints the name of all C variables declared in the SYNOPSIS
98 # of all manual pages in a directory (or in a single manual page file).
99 # Each name is printed in a separate line
100 # Usage example: .../man-pages$ man_lsvar man3;
104 if [ $# -lt 1 ]; then
105 >&2 echo "Usage: ${FUNCNAME[0]} <manpage|manNdir>...";
110 man_section "$arg" 'SYNOPSIS';
113 |pcregrep -Mv '(?s)^ [\w ]+ \**\w+\([\w\s(,)[\]*]+?(...)?\s*\); *$' \
115 -e '(?s)^ +extern [\w ]+ \**\(\*+[\w ]+\)\([\w\s(,)[\]*]+?\s*\); *$' \
116 -e '^ +extern [\w ]+ \**[\w ]+; *$' \
119 |sed -E 's/^[0-9]+: +extern [^(]+ \**\(\*+(\w* )?(\w+)\)\(.*/\2/' \
120 |sed 's/^[0-9]\+: \+extern .* \**\(\w\+\); */\1/' \
124 # pdfman() renders a manual page in PDF
125 # Usage example: .../man-pages$ pdfman man2/membarrier.2;
129 if [ $# -eq 0 ]; then
130 >&2 echo "Usage: ${FUNCNAME[0]} [man(1) options] [section] page";
134 local tmp="$(mktemp -t "${!###*/}.XXXXXX")";
142 # man_gitstaged prints a list of all files with changes staged for commit
143 # (basename only if the files are within <man?/>), separated by ", ".
144 # Usage example: .../man-pages$ git commit -m "$(man_gitstaged): msg";
148 git diff --staged --name-only \