1 # SPDX-License-Identifier: GPL-2.0-only
2 ########################################################################
4 # (C) Copyright 2020, 2021, 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 ########################################################################
23 ########################################################################
26 # sed_rm_ccomments() removes C comments.
27 # It can't handle multiple comments in a single line correctly,
28 # nor mixed or embedded //... and /*...*/ comments.
29 # Use as a filter (see man_lsfunc() in this file).
31 function sed_rm_ccomments()
34 |sed -E '\%/\*%,\%\*/%{\%(\*/|/\*)%!d; s%/\*.*%%; s%.*\*/%%;}' \
38 ########################################################################
41 # grep_syscall() finds the prototype of a syscall in the kernel sources,
42 # printing the filename, line number, and the prototype.
43 # It should be run from the root of the linux kernel source tree.
44 # Usage example: .../linux$ grep_syscall openat2;
46 function grep_syscall()
49 >&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
56 |xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
57 |sed -E 's/^[^:]+:[0-9]+:/&\n/';
62 |xargs pcregrep -Mn "(?s)^asmlinkage\s+[\w\s]+\**sys_${1}\s*\(.*?\)" \
63 |sed -E 's/^[^:]+:[0-9]+:/&\n/';
66 # grep_syscall_def() finds the definition of a syscall in the kernel sources,
67 # printing the filename, line number, and the function definition.
68 # It should be run from the root of the linux kernel source tree.
69 # Usage example: .../linux$ grep_syscall_def openat2;
71 function grep_syscall_def()
74 >&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
81 |xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?^}" \
82 |sed -E 's/^[^:]+:[0-9]+:/&\n/';
85 ########################################################################
88 # man_section() prints specific manual page sections (DESCRIPTION, SYNOPSIS,
89 # ...) of all manual pages in a directory (or in a single manual page file).
90 # Usage example: .../man-pages$ man_section man2 SYNOPSIS 'CONFORMING TO';
92 function man_section()
95 >&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>...";
103 find "${page}" -type f \
105 |grep -v -e '\b1 ' -e '\btotal\b' \
106 |awk '{ print $2 }' \
108 |while read -r manpage; do
110 <(<${manpage} sed -n '/^\.TH/,/^\.SH/{/^\.SH/!p}') \
111 <(for s in ${sect}; do
115 -e "/^\.SH ${s}/,/^\.SH/{/^\.SH/!p}"; \
117 |man -P cat -l - 2>/dev/null;
121 # man_lsfunc() prints the name of all C functions declared in the SYNOPSIS
122 # of all manual pages in a directory (or in a single manual page file).
123 # Each name is printed in a separate line
124 # Usage example: .../man-pages$ man_lsfunc man2;
126 function man_lsfunc()
129 >&2 echo "Usage: ${FUNCNAME[0]} <manpage|manNdir>...";
134 man_section "${arg}" 'SYNOPSIS';
137 |pcregrep -Mn '(?s)^ [\w ]+ \**\w+\([\w\s(,)[\]*]+?(...)?\s*\); *$' \
139 |sed 's/syscall(SYS_\(\w*\),/\1(/' \
140 |sed -E 's/^[^(]+ \**(\w+)\(.*/\1/' \
144 # man_lsvar() prints the name of all C variables declared in the SYNOPSIS
145 # of all manual pages in a directory (or in a single manual page file).
146 # Each name is printed in a separate line
147 # Usage example: .../man-pages$ man_lsvar man3;
152 >&2 echo "Usage: ${FUNCNAME[0]} <manpage|manNdir>...";
157 man_section "${arg}" 'SYNOPSIS';
160 |pcregrep -Mv '(?s)^ [\w ]+ \**\w+\([\w\s(,)[\]*]+?(...)?\s*\); *$' \
162 -e '(?s)^ +extern [\w ]+ \**\(\*+[\w ]+\)\([\w\s(,)[\]*]+?\s*\); *$' \
163 -e '^ +extern [\w ]+ \**[\w ]+; *$' \
166 |sed -E 's/^[0-9]+: +extern [^(]+ \**\(\*+(\w* )?(\w+)\)\(.*/\2/' \
167 |sed 's/^[0-9]\+: \+extern .* \**\(\w\+\); */\1/' \
171 # pdfman() renders a manual page in PDF
172 # Usage example: .../man-pages$ pdfman man2/membarrier.2;
177 >&2 echo "Usage: ${FUNCNAME[0]} <man-page.n>";
181 local tmp="$(mktemp -t "${1##*/}.XXXXXX")";
190 # man_gitstaged prints a list of all files with changes staged for commit
191 # (basename only if the files are within <man?/>), separated by ", ".
192 # Usage example: .../man-pages$ git commit -m "$(man_gitstaged): msg";
194 function man_gitstaged()
196 git diff --staged --name-only \
198 |sed "s%man[1-9]/%%" \
203 ########################################################################
206 # grep_glibc_prototype() finds a function prototype in the glibc sources,
207 # printing the filename, line number, and the prototype.
208 # It should be run from the root of the glibc source tree.
209 # Usage example: .../glibc$ grep_glibc_prototype printf;
211 function grep_glibc_prototype()
214 >&2 echo "Usage: ${FUNCNAME[0]} <func>";
221 |xargs pcregrep -Mn \
222 "(?s)^[\w[][\w\s(,)[:\]]+\s+\**${1}\s*\([\w\s(,)[\]*]+?(...)?\)[\w\s(,)[:\]]*;" \
223 |sed -E 's/^[^:]+:[0-9]+:/&\n/';