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 sinlge 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 a specific manual page section (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;
92 function man_section()
95 >&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>";
100 |xargs grep -l "\.SH ${2}" \
102 |while read -r manpage; do
105 -e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
107 -e "/^\.SH ${2}/,/^\.SH/{/^\.SH/!p}" \
108 |man -P cat -l - 2>/dev/null;
112 # man_lsfunc() prints the name of all C functions declared in the SYNOPSIS
113 # of all manual pages in a directory (or in a single manual page file).
114 # Each name is printed in a separate line
115 # Usage example: .../man-pages$ man_lsfunc man2;
117 function man_lsfunc()
120 >&2 echo "Usage: ${FUNCNAME[0]} <dir>";
125 man_section "${arg}" 'SYNOPSIS';
128 |pcregrep -Mn '(?s)^ [\w ]+ \**\w+\([\w\s(,)[\]*]+?(...)?\s*\); *$' \
130 |sed -E 's/^[^(]+ \**(\w+)\(.*/\1/' \
134 # pdfman() renders a manual page in PDF
135 # Usage example: .../man-pages$ pdfman man2/membarrier.2;
140 >&2 echo "Usage: ${FUNCNAME[0]} <man-page.n>";
144 local tmp="$(mktemp -t "${1##*/}.XXXXXX")";
153 # man_gitstaged prints a list of all files with changes staged for commit
154 # (basename only if the files are within <man?/>), separated by ", ".
155 # Usage example: .../man-pages$ git commit -m "$(man_gitstaged): msg";
157 function man_gitstaged()
159 git diff --staged --name-only \
161 |sed "s%man[1-9]/%%" \
166 ########################################################################
169 # grep_glibc_prototype() finds a function prototype in the glibc sources,
170 # printing the filename, line number, and the prototype.
171 # It should be run from the root of the glibc source tree.
172 # Usage example: .../glibc$ grep_glibc_prototype printf;
174 function grep_glibc_prototype()
177 >&2 echo "Usage: ${FUNCNAME[0]} <func>";
184 |xargs pcregrep -Mn \
185 "(?s)^[\w[][\w\s(,)[:\]]+\s+\**${1}\s*\([\w\s(,)[\]*]+?(...)?\)[\w\s(,)[:\]]*;" \
186 |sed -E 's/^[^:]+:[0-9]+:/&\n/';