1 # SPDX-License-Identifier: GPL-2.0-only
2 ########################################################################
4 # (C) Copyright 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 # grep_syscall() finds the prototype of a syscall in the kernel sources,
27 # printing the filename, line number, and the prototype.
28 # It should be run from the root of the linux kernel source tree.
29 # Usage example: .../linux$ grep_syscall openat2;
31 function grep_syscall()
34 >&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
41 |xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
42 |sed -E 's/^[^:]+:[0-9]+:/&\n/';
47 |xargs pcregrep -Mn "(?s)^asmlinkage\s+[\w\s]+\**sys_${1}\s*\(.*?\)" \
48 |sed -E 's/^[^:]+:[0-9]+:/&\n/';
51 # grep_syscall_def() finds the definition of a syscall in the kernel sources,
52 # printing the filename, line number, and the function definition.
53 # It should be run from the root of the linux kernel source tree.
54 # Usage example: .../linux$ grep_syscall_def openat2;
56 function grep_syscall_def()
59 >&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
66 |xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?^}" \
67 |sed -E 's/^[^:]+:[0-9]+:/&\n/';
70 ########################################################################
73 # man_section() prints a specific manual page section (DESCRIPTION, SYNOPSIS,
74 # ...) of all manual pages in a directory (or in a single manual page file).
75 # Usage example: .../man-pages$ man_section man2 SYNOPSIS;
77 function man_section()
80 >&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>";
85 |xargs grep -l "\.SH ${2}" \
87 |while read -r manpage; do
90 -e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
92 -e "/^\.SH ${2}/,/^\.SH/{/^\.SH/!p}" \
93 |man -P cat -l - 2>/dev/null;
97 # man_lsfunc() prints the name of all C functions 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_lsfunc man2;
102 function man_lsfunc()
105 >&2 echo "Usage: ${FUNCNAME[0]} <dir>";
109 find "${@}" -type f \
110 |xargs grep -l "\.SH SYNOPSIS" \
112 |while read -r manpage; do
115 -e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
116 -e "/^\.SH SYNOPSIS/p" \
117 -e "/^\.SH SYNOPSIS/,/^\.SH/{/^\.SH/!p}" \
121 |man -P cat -l - 2>/dev/null;
123 |sed -n "/^SYNOPSIS/,/^\w/p" \
126 |sed 's/^[^(]* \**\(\w*\)(.*/\1/' \
131 # pdfman() renders a manual page in PDF
132 # Usage example: .../man-pages$ pdfman man2/membarrier.2;
137 >&2 echo "Usage: ${FUNCNAME[0]} <man-page.n>";
141 local tmp="$(mktemp -t "${1##*/}.XXXXXX")";
150 ########################################################################
153 # grep_glibc_prototype() finds a function prototype in the glibc sources,
154 # printing the filename, line number, and the prototype.
155 # It should be run from the root of the glibc source tree.
156 # Usage example: .../glibc$ grep_glibc_prototype printf;
158 function grep_glibc_prototype()
161 >&2 echo "Usage: ${FUNCNAME[0]} <func>";
168 |xargs pcregrep -Mn \
169 "(?s)^[\w[][\w\s(,)[:\]]+\s+\**${1}\s*\([\w\s(,)[\]*]+?(...)?\)[\w\s(,)[:\]]*;" \
170 |sed -E 's/^[^:]+:[0-9]+:/&\n/';