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>";
55 |xargs grep -l "${1}" \
57 |xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
58 |sed -E 's/^[^:]+:[0-9]+:/&\n/';
62 |xargs grep -l "${1}" \
64 |xargs pcregrep -Mn "(?s)^asmlinkage\s+[\w\s]+\**sys_${1}\s*\(.*?\)" \
65 |sed -E 's/^[^:]+:[0-9]+:/&\n/';
68 # grep_syscall_def() finds the definition of a syscall in the kernel sources,
69 # printing the filename, line number, and the function definition.
70 # It should be run from the root of the linux kernel source tree.
71 # Usage example: .../linux$ grep_syscall_def openat2;
73 function grep_syscall_def()
76 >&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
82 |xargs grep -l "${1}" \
84 |xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?^}" \
85 |sed -E 's/^[^:]+:[0-9]+:/&\n/';
88 ########################################################################
91 # man_section() prints specific manual page sections (DESCRIPTION, SYNOPSIS,
92 # ...) of all manual pages in a directory (or in a single manual page file).
93 # Usage example: .../man-pages$ man_section man2 SYNOPSIS 'CONFORMING TO';
95 function man_section()
98 >&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>...";
106 find "${page}" -type f \
108 |grep -v -e '\b1 ' -e '\btotal\b' \
109 |awk '{ print $2 }' \
111 |while read -r manpage; do
113 <(<${manpage} sed -n '/^\.TH/,/^\.SH/{/^\.SH/!p}') \
114 <(for s in ${sect}; do
118 -e "/^\.SH ${s}/,/^\.SH/{/^\.SH/!p}"; \
120 |man -P cat -l - 2>/dev/null;
124 # man_lsfunc() prints the name of all C functions declared in the SYNOPSIS
125 # of all manual pages in a directory (or in a single manual page file).
126 # Each name is printed in a separate line
127 # Usage example: .../man-pages$ man_lsfunc man2;
129 function man_lsfunc()
132 >&2 echo "Usage: ${FUNCNAME[0]} <manpage|manNdir>...";
137 man_section "${arg}" 'SYNOPSIS';
140 |pcregrep -Mn '(?s)^ [\w ]+ \**\w+\([\w\s(,)[\]*]*?(...)?\s*\); *$' \
142 |sed -E 's/syscall\(SYS_(\w*),?/\1(/' \
143 |sed -E 's/^[^(]+ \**(\w+)\(.*/\1/' \
147 # man_lsvar() prints the name of all C variables declared in the SYNOPSIS
148 # of all manual pages in a directory (or in a single manual page file).
149 # Each name is printed in a separate line
150 # Usage example: .../man-pages$ man_lsvar man3;
155 >&2 echo "Usage: ${FUNCNAME[0]} <manpage|manNdir>...";
160 man_section "${arg}" 'SYNOPSIS';
163 |pcregrep -Mv '(?s)^ [\w ]+ \**\w+\([\w\s(,)[\]*]+?(...)?\s*\); *$' \
165 -e '(?s)^ +extern [\w ]+ \**\(\*+[\w ]+\)\([\w\s(,)[\]*]+?\s*\); *$' \
166 -e '^ +extern [\w ]+ \**[\w ]+; *$' \
169 |sed -E 's/^[0-9]+: +extern [^(]+ \**\(\*+(\w* )?(\w+)\)\(.*/\2/' \
170 |sed 's/^[0-9]\+: \+extern .* \**\(\w\+\); */\1/' \
174 # pdfman() renders a manual page in PDF
175 # Usage example: .../man-pages$ pdfman man2/membarrier.2;
180 >&2 echo "Usage: ${FUNCNAME[0]} [man(1) options] [section] page";
184 local tmp="$(mktemp -t "${!###*/}.XXXXXX")";
192 # man_gitstaged prints a list of all files with changes staged for commit
193 # (basename only if the files are within <man?/>), separated by ", ".
194 # Usage example: .../man-pages$ git commit -m "$(man_gitstaged): msg";
196 function man_gitstaged()
198 git diff --staged --name-only \
200 |sed "s%man[0-9]/%%" \
205 ########################################################################
208 # grep_glibc_prototype() finds a function prototype in the glibc sources,
209 # printing the filename, line number, and the prototype.
210 # It should be run from the root of the glibc source tree.
211 # Usage example: .../glibc$ grep_glibc_prototype printf;
213 function grep_glibc_prototype()
216 >&2 echo "Usage: ${FUNCNAME[0]} <func>";
222 |xargs grep -l "${1}" \
224 |xargs pcregrep -Mn \
225 "(?s)^[\w[][\w\s(,)[:\]]+\s+\**${1}\s*\([\w\s(,)[\]*]+?(...)?\)[\w\s(,)[:\]]*;" \
226 |sed -E 's/^[^:]+:[0-9]+:/&\n/';