]> git.ipfire.org Git - thirdparty/man-pages.git/blob - scripts/bash_aliases
scripts/bash_aliases: Make man_lsfunc() more robust; Add sed_rm_ccomments().
[thirdparty/man-pages.git] / scripts / bash_aliases
1 # SPDX-License-Identifier: GPL-2.0-only
2 ########################################################################
3 #
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.
8 #
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).
14 #
15 ########################################################################
16
17 ########################################################################
18 # Exit status
19
20 EX_OK=0;
21 EX_USAGE=64;
22
23 ########################################################################
24 # C
25
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).
30
31 function sed_rm_ccomments()
32 {
33 sed 's%/\*.*\*/%%' \
34 |sed -E '\%/\*%,\%\*/%{\%(\*/|/\*)%!d; s%/\*.*%%; s%.*\*/%%;}' \
35 |sed 's%//.*%%';
36 }
37
38 ########################################################################
39 # Linux kernel
40
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;
45
46 function grep_syscall()
47 {
48 if ! [ -v 1 ]; then
49 >&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
50 return ${EX_USAGE};
51 fi
52
53 find * -type f \
54 |grep '\.c$' \
55 |sort -V \
56 |xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
57 |sed -E 's/^[^:]+:[0-9]+:/&\n/';
58
59 find * -type f \
60 |grep '\.[ch]$' \
61 |sort -V \
62 |xargs pcregrep -Mn "(?s)^asmlinkage\s+[\w\s]+\**sys_${1}\s*\(.*?\)" \
63 |sed -E 's/^[^:]+:[0-9]+:/&\n/';
64 }
65
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;
70
71 function grep_syscall_def()
72 {
73 if ! [ -v 1 ]; then
74 >&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
75 return ${EX_USAGE};
76 fi
77
78 find * -type f \
79 |grep '\.c$' \
80 |sort -V \
81 |xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?^}" \
82 |sed -E 's/^[^:]+:[0-9]+:/&\n/';
83 }
84
85 ########################################################################
86 # Linux man-pages
87
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;
91
92 function man_section()
93 {
94 if ! [ -v 2 ]; then
95 >&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>";
96 return ${EX_USAGE};
97 fi
98
99 find "${1}" -type f \
100 |xargs grep -l "\.SH ${2}" \
101 |sort -V \
102 |while read -r manpage; do
103 <${manpage} \
104 sed -n \
105 -e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
106 -e "/^\.SH ${2}/p" \
107 -e "/^\.SH ${2}/,/^\.SH/{/^\.SH/!p}" \
108 |man -P cat -l - 2>/dev/null;
109 done;
110 }
111
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;
116
117 function man_lsfunc()
118 {
119 if ! [ -v 1 ]; then
120 >&2 echo "Usage: ${FUNCNAME[0]} <dir>";
121 return ${EX_USAGE};
122 fi
123
124 for arg in "$@"; do
125 man_section "${arg}" 'SYNOPSIS';
126 done \
127 |sed_rm_ccomments \
128 |pcregrep -Mn '(?s)^ [\w ]+ \**\w+\([\w\s(,)[\]*]+?(...)?\s*\); *$' \
129 |grep '^[0-9]' \
130 |sed -E 's/^[^(]+ \**(\w+)\(.*/\1/' \
131 |uniq;
132 }
133
134 # pdfman() renders a manual page in PDF
135 # Usage example: .../man-pages$ pdfman man2/membarrier.2;
136
137 function pdfman()
138 {
139 if ! [ -v 1 ]; then
140 >&2 echo "Usage: ${FUNCNAME[0]} <man-page.n>";
141 return ${EX_USAGE};
142 fi;
143
144 local tmp="$(mktemp -t "${1##*/}.XXXXXX")";
145
146 <${1} \
147 man -Tps -l - \
148 |ps2pdf - - \
149 >${tmp};
150 xdg-open ${tmp};
151 }
152
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";
156
157 function man_gitstaged()
158 {
159 git diff --staged --name-only \
160 |sed "s/$/, /" \
161 |sed "s%man[1-9]/%%" \
162 |tr -d '\n' \
163 |sed "s/, $//"
164 }
165
166 ########################################################################
167 # Glibc
168
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;
173
174 function grep_glibc_prototype()
175 {
176 if ! [ -v 1 ]; then
177 >&2 echo "Usage: ${FUNCNAME[0]} <func>";
178 return ${EX_USAGE};
179 fi
180
181 find * -type f \
182 |grep '\.h$' \
183 |sort -V \
184 |xargs pcregrep -Mn \
185 "(?s)^[\w[][\w\s(,)[:\]]+\s+\**${1}\s*\([\w\s(,)[\]*]+?(...)?\)[\w\s(,)[:\]]*;" \
186 |sed -E 's/^[^:]+:[0-9]+:/&\n/';
187 }