elf.5, ld.so.8: Undeprecate DT_RPATH; explain DT_RPATH vs DT_RUNPATH
[man-pages.git] / scripts / bash_aliases
blobe461707c8c2311ef1f586a38e0e6b02d344303ea
1 # SPDX-License-Identifier: GPL-2.0-only
2 ########################################################################
4 # (C) Copyright 2020-2022, 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_USAGE=64;
22 ########################################################################
23 #       C
25 #  sed_rm_ccomments()  removes C comments.
26 # It can't handle mixed //... and /*...*/ comments.
27 # Use as a filter (see man_lsfunc() in this file).
29 sed_rm_ccomments()
31         perl -p -e 's%/\*.*?\*/%%g' \
32         |sed -E '\%/\*%, \%\*/% {\%(\*/|/\*)%!d}' \
33         |sed -E '\%/\*% {s%/\*.*%%; n; s%.*\*/%%;}' \
34         |sed -E '\%/\*% {s%/\*.*%%; n; s%.*\*/%%;}' \
35         |sed 's%//.*%%';
38 ########################################################################
39 #       Linux man-pages
41 #  man_section()  prints specific manual page sections (DESCRIPTION, SYNOPSIS,
42 # ...) of all manual pages in a directory (or in a single manual page file).
43 # Usage example:  .../man-pages$ man_section man2 SYNOPSIS 'SEE ALSO';
45 man_section()
47         if [ $# -lt 2 ]; then
48                 >&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>...";
49                 return $EX_USAGE;
50         fi
52         local page="$1";
53         shift;
54         local sect="$*";
56         find "$page" -type f \
57         |xargs wc -l \
58         |grep -v -e '\b1 ' -e '\btotal\b' \
59         |awk '{ print $2 }' \
60         |sort \
61         |while read -r manpage; do
62                 (sed -n '/^\.TH/,/^\.SH/{/^\.SH/!p}' <"$manpage";
63                  for s in $sect; do
64                         <"$manpage" \
65                         sed -n \
66                                 -e "/^\.SH $s/p" \
67                                 -e "/^\.SH $s/,/^\.SH/{/^\.SH/!p}";
68                  done;) \
69                 |mandoc -Tutf8 2>/dev/null \
70                 |col -pbx;
71         done;
74 #  man_lsfunc()  prints the name of all C functions declared in the SYNOPSIS
75 # of all manual pages in a directory (or in a single manual page file).
76 # Each name is printed in a separate line
77 # Usage example:  .../man-pages$ man_lsfunc man2;
79 man_lsfunc()
81         if [ $# -lt 1 ]; then
82                 >&2 echo "Usage: ${FUNCNAME[0]} <manpage|manNdir>...";
83                 return $EX_USAGE;
84         fi
86         for arg in "$@"; do
87                 man_section "$arg" 'SYNOPSIS';
88         done \
89         |sed_rm_ccomments \
90         |pcregrep -Mn '(?s)^ [\w ]+ \**\w+\([\w\s(,)[\]*]*?(...)?\s*\); *$' \
91         |grep '^[0-9]' \
92         |sed -E 's/syscall\(SYS_(\w*),?/\1(/' \
93         |sed -E 's/^[^(]+ \**(\w+)\(.*/\1/' \
94         |uniq;
97 #  man_lsvar()  prints the name of all C variables 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_lsvar man3;
102 man_lsvar()
104         if [ $# -lt 1 ]; then
105                 >&2 echo "Usage: ${FUNCNAME[0]} <manpage|manNdir>...";
106                 return $EX_USAGE;
107         fi
109         for arg in "$@"; do
110                 man_section "$arg" 'SYNOPSIS';
111         done \
112         |sed_rm_ccomments \
113         |pcregrep -Mv '(?s)^ [\w ]+ \**\w+\([\w\s(,)[\]*]+?(...)?\s*\); *$' \
114         |pcregrep -Mn \
115           -e '(?s)^ +extern [\w ]+ \**\(\*+[\w ]+\)\([\w\s(,)[\]*]+?\s*\); *$' \
116           -e '^ +extern [\w ]+ \**[\w ]+; *$' \
117         |grep '^[0-9]' \
118         |grep -v 'typedef' \
119         |sed -E 's/^[0-9]+: +extern [^(]+ \**\(\*+(\w* )?(\w+)\)\(.*/\2/' \
120         |sed    's/^[0-9]\+: \+extern .* \**\(\w\+\); */\1/' \
121         |uniq;
124 #  pdfman()  renders a manual page in PDF
125 # Usage example:  .../man-pages$ pdfman man2/membarrier.2;
127 pdfman()
129         if [ $# -eq 0 ]; then
130                 >&2 echo "Usage: ${FUNCNAME[0]} [man(1) options] [section] page";
131                 return $EX_USAGE;
132         fi;
134         local tmp="$(mktemp -t "${!###*/}.XXXXXX")";
136         man -Tps "$@" \
137         |ps2pdf - - \
138         >"$tmp";
139         xdg-open "$tmp";
142 #  man_gitstaged  prints a list of all files with changes staged for commit
143 # (basename only if the files are within <man?/>), separated by ", ".
144 # Usage example:  .../man-pages$ git commit -m "$(man_gitstaged): msg";
146 man_gitstaged()
148         git diff --staged --name-only                                   \
149         |sed 's/$/, /'                                                  \
150         |sed 's%.*/%%'                                                  \
151         |tr -d '\n'                                                     \
152         |sed 's/, $//'