scripts/bash_aliases: tfix
[man-pages.git] / scripts / bash_aliases
bloba14c65cd40d6dec876d709499a5b80bfc073bbad
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 ########################################################################
18 #       Exit status
20 EX_OK=0;
21 EX_USAGE=64;
23 ########################################################################
24 #       Linux kernel
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()
33         if ! [ -v 1 ]; then
34                 >&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
35                 return ${EX_USAGE};
36         fi
38         find * -type f \
39         |grep '\.c$' \
40         |sort -V \
41         |xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
42         |sed -E 's/^[^:]+:[0-9]+:/&\n/';
44         find * -type f \
45         |grep '\.[ch]$' \
46         |sort -V \
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()
58         if ! [ -v 1 ]; then
59                 >&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
60                 return ${EX_USAGE};
61         fi
63         find * -type f \
64         |grep '\.c$' \
65         |sort -V \
66         |xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?^}" \
67         |sed -E 's/^[^:]+:[0-9]+:/&\n/';
70 ########################################################################
71 #       Linux man-pages
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()
79         if ! [ -v 2 ]; then
80                 >&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>";
81                 return ${EX_USAGE};
82         fi
84         find "${1}" -type f \
85         |xargs grep -l "\.SH ${2}" \
86         |sort -V \
87         |while read -r manpage; do
88                 <${manpage} \
89                 sed -n \
90                         -e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
91                         -e "/^\.SH ${2}/p" \
92                         -e "/^\.SH ${2}/,/^\.SH/{/^\.SH/!p}" \
93                 |man -P cat -l - 2>/dev/null;
94         done;
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()
104         if ! [ -v 1 ]; then
105                 >&2 echo "Usage: ${FUNCNAME[0]} <dir>";
106                 return ${EX_USAGE};
107         fi
109         find "${@}" -type f \
110         |xargs grep -l "\.SH SYNOPSIS" \
111         |sort -V \
112         |while read -r manpage; do
113                 <${manpage} \
114                 sed -n \
115                         -e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
116                         -e "/^\.SH SYNOPSIS/p" \
117                         -e "/^\.SH SYNOPSIS/,/^\.SH/{/^\.SH/!p}" \
118                 |sed \
119                         -e '/Feature/,$d' \
120                         -e '/{/,/}/d' \
121                 |man -P cat -l - 2>/dev/null;
122         done \
123         |sed -n "/^SYNOPSIS/,/^\w/p" \
124         |grep '^       \w' \
125         |grep -v ':' \
126         |sed 's/^[^(]* \**\(\w*\)(.*/\1/' \
127         |grep '^\w' \
128         |uniq;
131 #  pdfman()  renders a manual page in PDF
132 # Usage example:  .../man-pages$ pdfman man2/membarrier.2;
134 function pdfman()
136         if ! [ -v 1 ]; then
137                 >&2 echo "Usage: ${FUNCNAME[0]} <man-page.n>";
138                 return ${EX_USAGE};
139         fi;
141         local tmp="$(mktemp -t "${1##*/}.XXXXXX")";
143         <${1} \
144         man -Tps -l - \
145         |ps2pdf - - \
146         >${tmp};
147         xdg-open ${tmp};
150 ########################################################################
151 #       Glibc
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()
160         if ! [ -v 1 ]; then
161                 >&2 echo "Usage: ${FUNCNAME[0]} <func>";
162                 return ${EX_USAGE};
163         fi
165         find * -type f \
166         |grep '\.h$' \
167         |sort -V \
168         |xargs pcregrep -Mn \
169           "(?s)^[\w[][\w\s(,)[:\]]+\s+\**${1}\s*\([\w\s(,)[\]*]+?(...)?\)[\w\s(,)[:\]]*;" \
170         |sed -E 's/^[^:]+:[0-9]+:/&\n/';