futex.2: Rework the description of FUTEX_LOCK_PI2
[man-pages.git] / scripts / bash_aliases
blobecac0029de98f2465344be5f2e8d545dd398c393
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 ########################################################################
18 #       Exit status
20 EX_OK=0;
21 EX_USAGE=64;
23 ########################################################################
24 #       C
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()
33         sed 's%/\*.*\*/%%' \
34         |sed -E '\%/\*%,\%\*/%{\%(\*/|/\*)%!d; s%/\*.*%%; s%.*\*/%%;}' \
35         |sed 's%//.*%%';
38 ########################################################################
39 #       Linux kernel
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()
48         if (($# != 1)); then
49                 >&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
50                 return ${EX_USAGE};
51         fi
53         find * -type f \
54         |grep '\.c$' \
55         |sort \
56         |xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
57         |sed -E 's/^[^:]+:[0-9]+:/&\n/';
59         find * -type f \
60         |grep '\.[ch]$' \
61         |sort \
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()
73         if (($# != 1)); then
74                 >&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
75                 return ${EX_USAGE};
76         fi
78         find * -type f \
79         |grep '\.c$' \
80         |sort \
81         |xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?^}" \
82         |sed -E 's/^[^:]+:[0-9]+:/&\n/';
85 ########################################################################
86 #       Linux man-pages
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()
94         if (($# < 2)); then
95                 >&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>...";
96                 return ${EX_USAGE};
97         fi
99         local page="$1";
100         shift;
101         local sect="$@";
103         find "${page}" -type f \
104         |xargs wc -l \
105         |grep -v -e '\b1 ' -e '\btotal\b' \
106         |awk '{ print $2 }' \
107         |sort \
108         |while read -r manpage; do
109                 cat \
110                 <(<${manpage} sed -n '/^\.TH/,/^\.SH/{/^\.SH/!p}') \
111                 <(for s in ${sect}; do
112                         <${manpage} \
113                         sed -n \
114                                 -e "/^\.SH ${s}/p" \
115                                 -e "/^\.SH ${s}/,/^\.SH/{/^\.SH/!p}"; \
116                   done;) \
117                 |man -P cat -l - 2>/dev/null;
118         done;
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()
128         if (($# < 1)); then
129                 >&2 echo "Usage: ${FUNCNAME[0]} <manpage|manNdir>...";
130                 return ${EX_USAGE};
131         fi
133         for arg in "$@"; do
134                 man_section "${arg}" 'SYNOPSIS';
135         done \
136         |sed_rm_ccomments \
137         |pcregrep -Mn '(?s)^ [\w ]+ \**\w+\([\w\s(,)[\]*]+?(...)?\s*\); *$' \
138         |grep '^[0-9]' \
139         |sed    's/syscall(SYS_\(\w*\),/\1(/' \
140         |sed -E 's/^[^(]+ \**(\w+)\(.*/\1/' \
141         |uniq;
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;
149 function man_lsvar()
151         if (($# < 1)); then
152                 >&2 echo "Usage: ${FUNCNAME[0]} <manpage|manNdir>...";
153                 return ${EX_USAGE};
154         fi
156         for arg in "$@"; do
157                 man_section "${arg}" 'SYNOPSIS';
158         done \
159         |sed_rm_ccomments \
160         |pcregrep -Mv '(?s)^ [\w ]+ \**\w+\([\w\s(,)[\]*]+?(...)?\s*\); *$' \
161         |pcregrep -Mn \
162           -e '(?s)^ +extern [\w ]+ \**\(\*+[\w ]+\)\([\w\s(,)[\]*]+?\s*\); *$' \
163           -e '^ +extern [\w ]+ \**[\w ]+; *$' \
164         |grep '^[0-9]' \
165         |grep -v 'typedef' \
166         |sed -E 's/^[0-9]+: +extern [^(]+ \**\(\*+(\w* )?(\w+)\)\(.*/\2/' \
167         |sed    's/^[0-9]\+: \+extern .* \**\(\w\+\); */\1/' \
168         |uniq;
171 #  pdfman()  renders a manual page in PDF
172 # Usage example:  .../man-pages$ pdfman man2/membarrier.2;
174 function pdfman()
176         if (($# != 1)); then
177                 >&2 echo "Usage: ${FUNCNAME[0]} <man-page.n>";
178                 return ${EX_USAGE};
179         fi;
181         local tmp="$(mktemp -t "${1##*/}.XXXXXX")";
183         <${1} \
184         man -Tps -l - \
185         |ps2pdf - - \
186         >${tmp};
187         xdg-open ${tmp};
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                                   \
197         |sed "s/$/, /"                                                  \
198         |sed "s%man[1-9]/%%"                                            \
199         |tr -d '\n'                                                     \
200         |sed "s/, $//"
203 ########################################################################
204 #       Glibc
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()
213         if (($# != 1)); then
214                 >&2 echo "Usage: ${FUNCNAME[0]} <func>";
215                 return ${EX_USAGE};
216         fi
218         find * -type f \
219         |grep '\.h$' \
220         |sort \
221         |xargs pcregrep -Mn \
222           "(?s)^[\w[][\w\s(,)[:\]]+\s+\**${1}\s*\([\w\s(,)[\]*]+?(...)?\)[\w\s(,)[:\]]*;" \
223         |sed -E 's/^[^:]+:[0-9]+:/&\n/';