symlink.2: Clarify symlink ownership matters when protected_symlinks=1
[man-pages.git] / scripts / bash_aliases
blobb7a484e32617d4e21b1c1d21d9d085570876b48d
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         |xargs grep -l "${1}" \
56         |sort \
57         |xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
58         |sed -E 's/^[^:]+:[0-9]+:/&\n/';
60         find * -type f \
61         |grep '\.[ch]$' \
62         |xargs grep -l "${1}" \
63         |sort \
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()
75         if (($# != 1)); then
76                 >&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
77                 return ${EX_USAGE};
78         fi
80         find * -type f \
81         |grep '\.c$' \
82         |xargs grep -l "${1}" \
83         |sort \
84         |xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?^}" \
85         |sed -E 's/^[^:]+:[0-9]+:/&\n/';
88 ########################################################################
89 #       Linux man-pages
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()
97         if (($# < 2)); then
98                 >&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>...";
99                 return ${EX_USAGE};
100         fi
102         local page="$1";
103         shift;
104         local sect="$@";
106         find "${page}" -type f \
107         |xargs wc -l \
108         |grep -v -e '\b1 ' -e '\btotal\b' \
109         |awk '{ print $2 }' \
110         |sort \
111         |while read -r manpage; do
112                 cat \
113                 <(<${manpage} sed -n '/^\.TH/,/^\.SH/{/^\.SH/!p}') \
114                 <(for s in ${sect}; do
115                         <${manpage} \
116                         sed -n \
117                                 -e "/^\.SH ${s}/p" \
118                                 -e "/^\.SH ${s}/,/^\.SH/{/^\.SH/!p}"; \
119                   done;) \
120                 |man -P cat -l - 2>/dev/null;
121         done;
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()
131         if (($# < 1)); then
132                 >&2 echo "Usage: ${FUNCNAME[0]} <manpage|manNdir>...";
133                 return ${EX_USAGE};
134         fi
136         for arg in "$@"; do
137                 man_section "${arg}" 'SYNOPSIS';
138         done \
139         |sed_rm_ccomments \
140         |pcregrep -Mn '(?s)^ [\w ]+ \**\w+\([\w\s(,)[\]*]*?(...)?\s*\); *$' \
141         |grep '^[0-9]' \
142         |sed -E 's/syscall\(SYS_(\w*),?/\1(/' \
143         |sed -E 's/^[^(]+ \**(\w+)\(.*/\1/' \
144         |uniq;
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;
152 function man_lsvar()
154         if (($# < 1)); then
155                 >&2 echo "Usage: ${FUNCNAME[0]} <manpage|manNdir>...";
156                 return ${EX_USAGE};
157         fi
159         for arg in "$@"; do
160                 man_section "${arg}" 'SYNOPSIS';
161         done \
162         |sed_rm_ccomments \
163         |pcregrep -Mv '(?s)^ [\w ]+ \**\w+\([\w\s(,)[\]*]+?(...)?\s*\); *$' \
164         |pcregrep -Mn \
165           -e '(?s)^ +extern [\w ]+ \**\(\*+[\w ]+\)\([\w\s(,)[\]*]+?\s*\); *$' \
166           -e '^ +extern [\w ]+ \**[\w ]+; *$' \
167         |grep '^[0-9]' \
168         |grep -v 'typedef' \
169         |sed -E 's/^[0-9]+: +extern [^(]+ \**\(\*+(\w* )?(\w+)\)\(.*/\2/' \
170         |sed    's/^[0-9]\+: \+extern .* \**\(\w\+\); */\1/' \
171         |uniq;
174 #  pdfman()  renders a manual page in PDF
175 # Usage example:  .../man-pages$ pdfman man2/membarrier.2;
177 function pdfman()
179         if (($# == 0)); then
180                 >&2 echo "Usage: ${FUNCNAME[0]} [man(1) options] [section] page";
181                 return ${EX_USAGE};
182         fi;
184         local tmp="$(mktemp -t "${!###*/}.XXXXXX")";
186         man -Tps $@ \
187         |ps2pdf - - \
188         >${tmp};
189         xdg-open ${tmp};
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                                   \
199         |sed "s/$/, /"                                                  \
200         |sed "s%man[0-9]/%%"                                            \
201         |tr -d '\n'                                                     \
202         |sed "s/, $//"
205 ########################################################################
206 #       Glibc
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()
215         if (($# != 1)); then
216                 >&2 echo "Usage: ${FUNCNAME[0]} <func>";
217                 return ${EX_USAGE};
218         fi
220         find * -type f \
221         |grep '\.h$' \
222         |xargs grep -l "${1}" \
223         |sort \
224         |xargs pcregrep -Mn \
225           "(?s)^[\w[][\w\s(,)[:\]]+\s+\**${1}\s*\([\w\s(,)[\]*]+?(...)?\)[\w\s(,)[:\]]*;" \
226         |sed -E 's/^[^:]+:[0-9]+:/&\n/';