All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] scripts/bash_aliases: Add useful functions
@ 2021-02-15 13:15 Alejandro Colomar
  2021-02-15 13:36 ` Alejandro Colomar (man-pages)
                   ` (8 more replies)
  0 siblings, 9 replies; 18+ messages in thread
From: Alejandro Colomar @ 2021-02-15 13:15 UTC (permalink / raw)
  To: mtk.manpages; +Cc: Alejandro Colomar, linux-man

That file should be sourced (.) from 'bashrc' (or 'bash_aliases').

It contains functions that are useful for the maintenance of this
project.

- grep_syscall()
- grep_syscall_def()
- man_section()
- man_lsfunc()
- pdfman()
- grep_glibc_prototype()

Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
---
 scripts/bash_aliases | 172 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 172 insertions(+)
 create mode 100644 scripts/bash_aliases

diff --git a/scripts/bash_aliases b/scripts/bash_aliases
new file mode 100644
index 000000000..4fcd66e62
--- /dev/null
+++ b/scripts/bash_aliases
@@ -0,0 +1,172 @@
+# SPDX-License-Identifier: GPL-2.0-only
+########################################################################
+#
+# (C) Copyright 2021, Alejandro Colomar
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; version 2.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details
+# (http://www.gnu.org/licenses/gpl-2.0.html).
+#
+########################################################################
+
+########################################################################
+#	Exit status
+
+EX_OK=0;
+EX_USAGE=64;
+
+########################################################################
+#	Linux kernel
+
+#  grep_syscall()  finds the prototype of a syscall in the kernel sources,
+# printing the filename, line number, and the prototype.
+# It should be run from the root of the linux kernel source tree.
+# Usage example:  .../linux$ grep_syscall openat2;
+
+function grep_syscall()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.c$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+
+	find * -type f \
+	|grep '\.[ch]$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^asmlinkage\s+[\w\s]+\**sys_${1}\s*\(.*?\)" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
+
+#  grep_syscall_def()  finds the definition of a syscall in the kernel sources,
+# printing the filename, line number, and the function definition.
+# It should be run from the root of the linux kernel source tree.
+# Usage example:  .../linux$ grep_syscall_def openat2;
+
+function grep_syscall_def()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.c$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?^}" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
+
+########################################################################
+#	Linux man-pages
+
+#  man_section()  prints a specific manual page section (DESCRIPTION, SYNOPSIS,
+# ...) of all manual pages in a directory (or in a single manual page file).
+# Usage example:  .../man-pages$ man_section man2 SYNOPSIS;
+
+function man_section()
+{
+	if ! [ -v 2 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>";
+		return ${EX_USAGE};
+	fi
+
+	find "${1}" -type f \
+	|xargs grep -l "\.SH ${2}" \
+	|sort -V \
+	|while read -r manpage; do
+		<${manpage} \
+		sed -n \
+			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
+			-e "/^\.SH ${2}/p" \
+			-e "/^\.SH ${2}/,/^\.SH/{/^\.SH/!p}" \
+		|man -P cat -l - 2>/dev/null;
+	done;
+}
+
+#  man_lsfunc()  prints the name of all C functions declared in the SYNOPSIS
+# of all manual pages in a directory (or in a single manual page file).
+# Each name is printed in a separate line
+# Usage example:  .../man-pages$ man_lsfunc man2;
+
+function man_lsfunc()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <dir>";
+		return ${EX_USAGE};
+	fi
+
+	find "${1}" -type f \
+	|xargs grep -l "\.SH SYNOPSIS" \
+	|sort -V \
+	|while read -r manpage; do
+		<${manpage} \
+		sed -n \
+			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
+			-e "/^\.SH SYNOPSIS/p" \
+			-e "/^\.SH SYNOPSIS/,/^\.SH/{/^\.SH/!p}" \
+		|sed \
+			-e '/Feature/,$d' \
+			-e '/{/,/}/d' \
+		|man -P cat -l - 2>/dev/null;
+	done \
+	|sed -n "/^SYNOPSIS/,/^\w/p" \
+	|grep '^       \w' \
+	|grep -v ':' \
+	|sed 's/^[^(]* \**\(\w*\)(.*/\1/' \
+	|grep '^\w' \
+	|uniq;
+}
+
+#  pdfman()  reanders a manual page in PDF
+# Usage example:  .../man-pages$ pdfman man2/membarrier.2
+
+function pdfman()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <man-page.n>";
+		return ${EX_USAGE};
+	fi;
+
+	local tmp="$(mktemp -t "${1##*/}.XXXXXX")";
+
+	<${1} \
+	man -Tps -l - \
+	|ps2pdf - - \
+	>${tmp};
+	xdg-open ${tmp};
+}
+
+########################################################################
+#	Glibc
+
+#  grep_glibc_prototype()  finds a function prototype in the glibc sources,
+# printing the filename, line number, and the prototype.
+# It should be run from the root of the glibc source tree.
+# Usage example:  .../glibc$ grep_glibc_prototype printf;
+
+function grep_glibc_prototype()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <func>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.h$' \
+	|sort -V \
+	|xargs pcregrep -Mn \
+	  "(?s)^[^\s#][\w\s]+\s+\**${1}\s*\([\w\s()[\]*,]*?(...)?\)[\w\s()]*;" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
+
-- 
2.30.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH] scripts/bash_aliases: Add useful functions
  2021-02-15 13:15 [PATCH] scripts/bash_aliases: Add useful functions Alejandro Colomar
@ 2021-02-15 13:36 ` Alejandro Colomar (man-pages)
  2021-02-15 20:31 ` Alejandro Colomar
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 18+ messages in thread
From: Alejandro Colomar (man-pages) @ 2021-02-15 13:36 UTC (permalink / raw)
  To: mtk.manpages; +Cc: linux-man

On 2/15/21 2:15 PM, Alejandro Colomar wrote:
> That file should be sourced (.) from 'bashrc' (or 'bash_aliases').
> 
> It contains functions that are useful for the maintenance of this
> project.
> 
> - grep_syscall()
> - grep_syscall_def()
> - man_section()
> - man_lsfunc()
> - pdfman()
> - grep_glibc_prototype()
> 
> Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
> ---
>  scripts/bash_aliases | 172 +++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 172 insertions(+)
>  create mode 100644 scripts/bash_aliases
> 
> diff --git a/scripts/bash_aliases b/scripts/bash_aliases
> new file mode 100644
> index 000000000..4fcd66e62
> --- /dev/null
> +++ b/scripts/bash_aliases
> @@ -0,0 +1,172 @@
> +# SPDX-License-Identifier: GPL-2.0-only
> +########################################################################
> +#
> +# (C) Copyright 2021, Alejandro Colomar
> +# This program is free software; you can redistribute it and/or
> +# modify it under the terms of the GNU General Public License
> +# as published by the Free Software Foundation; version 2.
> +#
> +# This program is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details
> +# (http://www.gnu.org/licenses/gpl-2.0.html).
> +#
> +########################################################################
> +
> +########################################################################
> +#	Exit status
> +
> +EX_OK=0;
> +EX_USAGE=64;
> +
> +########################################################################
> +#	Linux kernel
> +
> +#  grep_syscall()  finds the prototype of a syscall in the kernel sources,
> +# printing the filename, line number, and the prototype.
> +# It should be run from the root of the linux kernel source tree.
> +# Usage example:  .../linux$ grep_syscall openat2;
> +
> +function grep_syscall()
> +{
> +	if ! [ -v 1 ]; then
> +		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
> +		return ${EX_USAGE};
> +	fi
> +
> +	find * -type f \
> +	|grep '\.c$' \
> +	|sort -V \
> +	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
> +	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
> +
> +	find * -type f \
> +	|grep '\.[ch]$' \
> +	|sort -V \
> +	|xargs pcregrep -Mn "(?s)^asmlinkage\s+[\w\s]+\**sys_${1}\s*\(.*?\)" \
> +	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
> +}
> +
> +#  grep_syscall_def()  finds the definition of a syscall in the kernel sources,
> +# printing the filename, line number, and the function definition.
> +# It should be run from the root of the linux kernel source tree.
> +# Usage example:  .../linux$ grep_syscall_def openat2;
> +
> +function grep_syscall_def()
> +{
> +	if ! [ -v 1 ]; then
> +		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
> +		return ${EX_USAGE};
> +	fi
> +
> +	find * -type f \
> +	|grep '\.c$' \
> +	|sort -V \
> +	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?^}" \
> +	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
> +}
> +
> +########################################################################
> +#	Linux man-pages
> +
> +#  man_section()  prints a specific manual page section (DESCRIPTION, SYNOPSIS,
> +# ...) of all manual pages in a directory (or in a single manual page file).
> +# Usage example:  .../man-pages$ man_section man2 SYNOPSIS;
> +
> +function man_section()
> +{
> +	if ! [ -v 2 ]; then
> +		>&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>";
> +		return ${EX_USAGE};
> +	fi
> +
> +	find "${1}" -type f \
> +	|xargs grep -l "\.SH ${2}" \
> +	|sort -V \
> +	|while read -r manpage; do
> +		<${manpage} \
> +		sed -n \
> +			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
> +			-e "/^\.SH ${2}/p" \
> +			-e "/^\.SH ${2}/,/^\.SH/{/^\.SH/!p}" \
> +		|man -P cat -l - 2>/dev/null;
> +	done;
> +}
> +
> +#  man_lsfunc()  prints the name of all C functions declared in the SYNOPSIS
> +# of all manual pages in a directory (or in a single manual page file).
> +# Each name is printed in a separate line
> +# Usage example:  .../man-pages$ man_lsfunc man2;
> +
> +function man_lsfunc()
> +{
> +	if ! [ -v 1 ]; then
> +		>&2 echo "Usage: ${FUNCNAME[0]} <dir>";
> +		return ${EX_USAGE};
> +	fi
> +
> +	find "${1}" -type f \
> +	|xargs grep -l "\.SH SYNOPSIS" \
> +	|sort -V \
> +	|while read -r manpage; do
> +		<${manpage} \
> +		sed -n \
> +			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
> +			-e "/^\.SH SYNOPSIS/p" \
> +			-e "/^\.SH SYNOPSIS/,/^\.SH/{/^\.SH/!p}" \
> +		|sed \
> +			-e '/Feature/,$d' \
> +			-e '/{/,/}/d' \
> +		|man -P cat -l - 2>/dev/null;
> +	done \
> +	|sed -n "/^SYNOPSIS/,/^\w/p" \
> +	|grep '^       \w' \
> +	|grep -v ':' \
> +	|sed 's/^[^(]* \**\(\w*\)(.*/\1/' \
> +	|grep '^\w' \
> +	|uniq;
> +}
> +
> +#  pdfman()  reanders a manual page in PDF
> +# Usage example:  .../man-pages$ pdfman man2/membarrier.2

Hi Michael,

Please, add a ';' there for consistency.

Thanks,

Alex

> +
> +function pdfman()
> +{
> +	if ! [ -v 1 ]; then
> +		>&2 echo "Usage: ${FUNCNAME[0]} <man-page.n>";
> +		return ${EX_USAGE};
> +	fi;
> +
> +	local tmp="$(mktemp -t "${1##*/}.XXXXXX")";
> +
> +	<${1} \
> +	man -Tps -l - \
> +	|ps2pdf - - \
> +	>${tmp};
> +	xdg-open ${tmp};
> +}
> +
> +########################################################################
> +#	Glibc
> +
> +#  grep_glibc_prototype()  finds a function prototype in the glibc sources,
> +# printing the filename, line number, and the prototype.
> +# It should be run from the root of the glibc source tree.
> +# Usage example:  .../glibc$ grep_glibc_prototype printf;
> +
> +function grep_glibc_prototype()
> +{
> +	if ! [ -v 1 ]; then
> +		>&2 echo "Usage: ${FUNCNAME[0]} <func>";
> +		return ${EX_USAGE};
> +	fi
> +
> +	find * -type f \
> +	|grep '\.h$' \
> +	|sort -V \
> +	|xargs pcregrep -Mn \
> +	  "(?s)^[^\s#][\w\s]+\s+\**${1}\s*\([\w\s()[\]*,]*?(...)?\)[\w\s()]*;" \
> +	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
> +}
> +
> 


-- 
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH] scripts/bash_aliases: Add useful functions
  2021-02-15 13:15 [PATCH] scripts/bash_aliases: Add useful functions Alejandro Colomar
  2021-02-15 13:36 ` Alejandro Colomar (man-pages)
@ 2021-02-15 20:31 ` Alejandro Colomar
  2021-02-15 20:32 ` [PATCH v3] " Alejandro Colomar
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 18+ messages in thread
From: Alejandro Colomar @ 2021-02-15 20:31 UTC (permalink / raw)
  To: mtk.manpages; +Cc: Alejandro Colomar, linux-man

That file should be sourced (.) from 'bashrc' (or 'bash_aliases').

It contains functions that are useful for the maintenance of this
project.

- grep_syscall()
- grep_syscall_def()
- man_section()
- man_lsfunc()
- pdfman()
- grep_glibc_prototype()

Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
---

v2:
	- Reword license to adequate it for many functions instead
	  of a single program.
	- Add a trailing ';' for consistency.

---
 scripts/bash_aliases | 171 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 171 insertions(+)
 create mode 100644 scripts/bash_aliases

diff --git a/scripts/bash_aliases b/scripts/bash_aliases
new file mode 100644
index 000000000..7bdadac43
--- /dev/null
+++ b/scripts/bash_aliases
@@ -0,0 +1,171 @@
+# SPDX-License-Identifier: GPL-2.0-only
+########################################################################
+#
+# (C) Copyright 2021, Alejandro Colomar
+# These functions are free software; you can redistribute them and/or
+# modify them under the terms of the GNU General Public License
+# as published by the Free Software Foundation; version 2.
+#
+# These functions are distributed in the hope that they will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details
+# (http://www.gnu.org/licenses/gpl-2.0.html).
+#
+########################################################################
+
+########################################################################
+#	Exit status
+
+EX_OK=0;
+EX_USAGE=64;
+
+########################################################################
+#	Linux kernel
+
+#  grep_syscall()  finds the prototype of a syscall in the kernel sources,
+# printing the filename, line number, and the prototype.
+# It should be run from the root of the linux kernel source tree.
+# Usage example:  .../linux$ grep_syscall openat2;
+
+function grep_syscall()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.c$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+
+	find * -type f \
+	|grep '\.[ch]$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^asmlinkage\s+[\w\s]+\**sys_${1}\s*\(.*?\)" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
+
+#  grep_syscall_def()  finds the definition of a syscall in the kernel sources,
+# printing the filename, line number, and the function definition.
+# It should be run from the root of the linux kernel source tree.
+# Usage example:  .../linux$ grep_syscall_def openat2;
+
+function grep_syscall_def()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.c$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?^}" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
+
+########################################################################
+#	Linux man-pages
+
+#  man_section()  prints a specific manual page section (DESCRIPTION, SYNOPSIS,
+# ...) of all manual pages in a directory (or in a single manual page file).
+# Usage example:  .../man-pages$ man_section man2 SYNOPSIS;
+
+function man_section()
+{
+	if ! [ -v 2 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>";
+		return ${EX_USAGE};
+	fi
+
+	find "${1}" -type f \
+	|xargs grep -l "\.SH ${2}" \
+	|sort -V \
+	|while read -r manpage; do
+		<${manpage} \
+		sed -n \
+			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
+			-e "/^\.SH ${2}/p" \
+			-e "/^\.SH ${2}/,/^\.SH/{/^\.SH/!p}" \
+		|man -P cat -l - 2>/dev/null;
+	done;
+}
+
+#  man_lsfunc()  prints the name of all C functions declared in the SYNOPSIS
+# of all manual pages in a directory (or in a single manual page file).
+# Each name is printed in a separate line
+# Usage example:  .../man-pages$ man_lsfunc man2;
+
+function man_lsfunc()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <dir>";
+		return ${EX_USAGE};
+	fi
+
+	find "${1}" -type f \
+	|xargs grep -l "\.SH SYNOPSIS" \
+	|sort -V \
+	|while read -r manpage; do
+		<${manpage} \
+		sed -n \
+			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
+			-e "/^\.SH SYNOPSIS/p" \
+			-e "/^\.SH SYNOPSIS/,/^\.SH/{/^\.SH/!p}" \
+		|sed \
+			-e '/Feature/,$d' \
+			-e '/{/,/}/d' \
+		|man -P cat -l - 2>/dev/null;
+	done \
+	|sed -n "/^SYNOPSIS/,/^\w/p" \
+	|grep '^       \w' \
+	|grep -v ':' \
+	|sed 's/^[^(]* \**\(\w*\)(.*/\1/' \
+	|grep '^\w' \
+	|uniq;
+}
+
+#  pdfman()  reanders a manual page in PDF
+# Usage example:  .../man-pages$ pdfman man2/membarrier.2;
+
+function pdfman()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <man-page.n>";
+		return ${EX_USAGE};
+	fi;
+
+	local tmp="$(mktemp -t "${1##*/}.XXXXXX")";
+
+	<${1} \
+	man -Tps -l - \
+	|ps2pdf - - \
+	>${tmp};
+	xdg-open ${tmp};
+}
+
+########################################################################
+#	Glibc
+
+#  grep_glibc_prototype()  finds a function prototype in the glibc sources,
+# printing the filename, line number, and the prototype.
+# It should be run from the root of the glibc source tree.
+# Usage example:  .../glibc$ grep_glibc_prototype printf;
+
+function grep_glibc_prototype()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <func>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.h$' \
+	|sort -V \
+	|xargs pcregrep -Mn \
+	  "(?s)^[^\s#][\w\s]+\s+\**${1}\s*\([\w\s()[\]*,]*?(...)?\)[\w\s()]*;" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
-- 
2.30.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v3] scripts/bash_aliases: Add useful functions
  2021-02-15 13:15 [PATCH] scripts/bash_aliases: Add useful functions Alejandro Colomar
  2021-02-15 13:36 ` Alejandro Colomar (man-pages)
  2021-02-15 20:31 ` Alejandro Colomar
@ 2021-02-15 20:32 ` Alejandro Colomar
  2021-02-16 10:54   ` AW: " Walter Harms
  2021-02-18 18:47 ` [PATCH v4] " Alejandro Colomar
                   ` (5 subsequent siblings)
  8 siblings, 1 reply; 18+ messages in thread
From: Alejandro Colomar @ 2021-02-15 20:32 UTC (permalink / raw)
  To: mtk.manpages; +Cc: Alejandro Colomar, linux-man

That file should be sourced (.) from 'bashrc' (or 'bash_aliases').

It contains functions that are useful for the maintenance of this
project.

- grep_syscall()
- grep_syscall_def()
- man_section()
- man_lsfunc()
- pdfman()
- grep_glibc_prototype()

Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
---

v2:
	- Reword license to adequate it for many functions instead
	  of a single program.
	- Add a trailing ';' for consistency.
v3:
	- Resend to avoid confusion:  I forgot to use [PATCH v2]!

---
 scripts/bash_aliases | 171 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 171 insertions(+)
 create mode 100644 scripts/bash_aliases

diff --git a/scripts/bash_aliases b/scripts/bash_aliases
new file mode 100644
index 000000000..7bdadac43
--- /dev/null
+++ b/scripts/bash_aliases
@@ -0,0 +1,171 @@
+# SPDX-License-Identifier: GPL-2.0-only
+########################################################################
+#
+# (C) Copyright 2021, Alejandro Colomar
+# These functions are free software; you can redistribute them and/or
+# modify them under the terms of the GNU General Public License
+# as published by the Free Software Foundation; version 2.
+#
+# These functions are distributed in the hope that they will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details
+# (http://www.gnu.org/licenses/gpl-2.0.html).
+#
+########################################################################
+
+########################################################################
+#	Exit status
+
+EX_OK=0;
+EX_USAGE=64;
+
+########################################################################
+#	Linux kernel
+
+#  grep_syscall()  finds the prototype of a syscall in the kernel sources,
+# printing the filename, line number, and the prototype.
+# It should be run from the root of the linux kernel source tree.
+# Usage example:  .../linux$ grep_syscall openat2;
+
+function grep_syscall()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.c$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+
+	find * -type f \
+	|grep '\.[ch]$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^asmlinkage\s+[\w\s]+\**sys_${1}\s*\(.*?\)" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
+
+#  grep_syscall_def()  finds the definition of a syscall in the kernel sources,
+# printing the filename, line number, and the function definition.
+# It should be run from the root of the linux kernel source tree.
+# Usage example:  .../linux$ grep_syscall_def openat2;
+
+function grep_syscall_def()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.c$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?^}" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
+
+########################################################################
+#	Linux man-pages
+
+#  man_section()  prints a specific manual page section (DESCRIPTION, SYNOPSIS,
+# ...) of all manual pages in a directory (or in a single manual page file).
+# Usage example:  .../man-pages$ man_section man2 SYNOPSIS;
+
+function man_section()
+{
+	if ! [ -v 2 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>";
+		return ${EX_USAGE};
+	fi
+
+	find "${1}" -type f \
+	|xargs grep -l "\.SH ${2}" \
+	|sort -V \
+	|while read -r manpage; do
+		<${manpage} \
+		sed -n \
+			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
+			-e "/^\.SH ${2}/p" \
+			-e "/^\.SH ${2}/,/^\.SH/{/^\.SH/!p}" \
+		|man -P cat -l - 2>/dev/null;
+	done;
+}
+
+#  man_lsfunc()  prints the name of all C functions declared in the SYNOPSIS
+# of all manual pages in a directory (or in a single manual page file).
+# Each name is printed in a separate line
+# Usage example:  .../man-pages$ man_lsfunc man2;
+
+function man_lsfunc()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <dir>";
+		return ${EX_USAGE};
+	fi
+
+	find "${1}" -type f \
+	|xargs grep -l "\.SH SYNOPSIS" \
+	|sort -V \
+	|while read -r manpage; do
+		<${manpage} \
+		sed -n \
+			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
+			-e "/^\.SH SYNOPSIS/p" \
+			-e "/^\.SH SYNOPSIS/,/^\.SH/{/^\.SH/!p}" \
+		|sed \
+			-e '/Feature/,$d' \
+			-e '/{/,/}/d' \
+		|man -P cat -l - 2>/dev/null;
+	done \
+	|sed -n "/^SYNOPSIS/,/^\w/p" \
+	|grep '^       \w' \
+	|grep -v ':' \
+	|sed 's/^[^(]* \**\(\w*\)(.*/\1/' \
+	|grep '^\w' \
+	|uniq;
+}
+
+#  pdfman()  reanders a manual page in PDF
+# Usage example:  .../man-pages$ pdfman man2/membarrier.2;
+
+function pdfman()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <man-page.n>";
+		return ${EX_USAGE};
+	fi;
+
+	local tmp="$(mktemp -t "${1##*/}.XXXXXX")";
+
+	<${1} \
+	man -Tps -l - \
+	|ps2pdf - - \
+	>${tmp};
+	xdg-open ${tmp};
+}
+
+########################################################################
+#	Glibc
+
+#  grep_glibc_prototype()  finds a function prototype in the glibc sources,
+# printing the filename, line number, and the prototype.
+# It should be run from the root of the glibc source tree.
+# Usage example:  .../glibc$ grep_glibc_prototype printf;
+
+function grep_glibc_prototype()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <func>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.h$' \
+	|sort -V \
+	|xargs pcregrep -Mn \
+	  "(?s)^[^\s#][\w\s]+\s+\**${1}\s*\([\w\s()[\]*,]*?(...)?\)[\w\s()]*;" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
-- 
2.30.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* AW: [PATCH v3] scripts/bash_aliases: Add useful functions
  2021-02-15 20:32 ` [PATCH v3] " Alejandro Colomar
@ 2021-02-16 10:54   ` Walter Harms
  2021-02-16 11:00     ` Alejandro Colomar (man-pages)
  0 siblings, 1 reply; 18+ messages in thread
From: Walter Harms @ 2021-02-16 10:54 UTC (permalink / raw)
  To: Alejandro Colomar, mtk.manpages; +Cc: linux-man

My version of grep (V3) has a -P option and claims to support pcre.
Maybe that is a replacement for pcregrep ?

Would reduce the dependencies by one.

re,
 wh

________________________________________
Von: Alejandro Colomar <alx.manpages@gmail.com>
Gesendet: Montag, 15. Februar 2021 21:32:50
An: mtk.manpages@gmail.com
Cc: Alejandro Colomar; linux-man@vger.kernel.org
Betreff: [PATCH v3] scripts/bash_aliases: Add useful functions

That file should be sourced (.) from 'bashrc' (or 'bash_aliases').

It contains functions that are useful for the maintenance of this
project.

- grep_syscall()
- grep_syscall_def()
- man_section()
- man_lsfunc()
- pdfman()
- grep_glibc_prototype()

Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
---

v2:
        - Reword license to adequate it for many functions instead
          of a single program.
        - Add a trailing ';' for consistency.
v3:
        - Resend to avoid confusion:  I forgot to use [PATCH v2]!

---
 scripts/bash_aliases | 171 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 171 insertions(+)
 create mode 100644 scripts/bash_aliases

diff --git a/scripts/bash_aliases b/scripts/bash_aliases
new file mode 100644
index 000000000..7bdadac43
--- /dev/null
+++ b/scripts/bash_aliases
@@ -0,0 +1,171 @@
+# SPDX-License-Identifier: GPL-2.0-only
+########################################################################
+#
+# (C) Copyright 2021, Alejandro Colomar
+# These functions are free software; you can redistribute them and/or
+# modify them under the terms of the GNU General Public License
+# as published by the Free Software Foundation; version 2.
+#
+# These functions are distributed in the hope that they will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details
+# (http://www.gnu.org/licenses/gpl-2.0.html).
+#
+########################################################################
+
+########################################################################
+#      Exit status
+
+EX_OK=0;
+EX_USAGE=64;
+
+########################################################################
+#      Linux kernel
+
+#  grep_syscall()  finds the prototype of a syscall in the kernel sources,
+# printing the filename, line number, and the prototype.
+# It should be run from the root of the linux kernel source tree.
+# Usage example:  .../linux$ grep_syscall openat2;
+
+function grep_syscall()
+{
+       if ! [ -v 1 ]; then
+               >&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
+               return ${EX_USAGE};
+       fi
+
+       find * -type f \
+       |grep '\.c$' \
+       |sort -V \
+       |xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
+       |sed -E 's/^[^:]+:[0-9]+:/&\n/';
+
+       find * -type f \
+       |grep '\.[ch]$' \
+       |sort -V \
+       |xargs pcregrep -Mn "(?s)^asmlinkage\s+[\w\s]+\**sys_${1}\s*\(.*?\)" \
+       |sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
+
+#  grep_syscall_def()  finds the definition of a syscall in the kernel sources,
+# printing the filename, line number, and the function definition.
+# It should be run from the root of the linux kernel source tree.
+# Usage example:  .../linux$ grep_syscall_def openat2;
+
+function grep_syscall_def()
+{
+       if ! [ -v 1 ]; then
+               >&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
+               return ${EX_USAGE};
+       fi
+
+       find * -type f \
+       |grep '\.c$' \
+       |sort -V \
+       |xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?^}" \
+       |sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
+
+########################################################################
+#      Linux man-pages
+
+#  man_section()  prints a specific manual page section (DESCRIPTION, SYNOPSIS,
+# ...) of all manual pages in a directory (or in a single manual page file).
+# Usage example:  .../man-pages$ man_section man2 SYNOPSIS;
+
+function man_section()
+{
+       if ! [ -v 2 ]; then
+               >&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>";
+               return ${EX_USAGE};
+       fi
+
+       find "${1}" -type f \
+       |xargs grep -l "\.SH ${2}" \
+       |sort -V \
+       |while read -r manpage; do
+               <${manpage} \
+               sed -n \
+                       -e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
+                       -e "/^\.SH ${2}/p" \
+                       -e "/^\.SH ${2}/,/^\.SH/{/^\.SH/!p}" \
+               |man -P cat -l - 2>/dev/null;
+       done;
+}
+
+#  man_lsfunc()  prints the name of all C functions declared in the SYNOPSIS
+# of all manual pages in a directory (or in a single manual page file).
+# Each name is printed in a separate line
+# Usage example:  .../man-pages$ man_lsfunc man2;
+
+function man_lsfunc()
+{
+       if ! [ -v 1 ]; then
+               >&2 echo "Usage: ${FUNCNAME[0]} <dir>";
+               return ${EX_USAGE};
+       fi
+
+       find "${1}" -type f \
+       |xargs grep -l "\.SH SYNOPSIS" \
+       |sort -V \
+       |while read -r manpage; do
+               <${manpage} \
+               sed -n \
+                       -e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
+                       -e "/^\.SH SYNOPSIS/p" \
+                       -e "/^\.SH SYNOPSIS/,/^\.SH/{/^\.SH/!p}" \
+               |sed \
+                       -e '/Feature/,$d' \
+                       -e '/{/,/}/d' \
+               |man -P cat -l - 2>/dev/null;
+       done \
+       |sed -n "/^SYNOPSIS/,/^\w/p" \
+       |grep '^       \w' \
+       |grep -v ':' \
+       |sed 's/^[^(]* \**\(\w*\)(.*/\1/' \
+       |grep '^\w' \
+       |uniq;
+}
+
+#  pdfman()  reanders a manual page in PDF
+# Usage example:  .../man-pages$ pdfman man2/membarrier.2;
+
+function pdfman()
+{
+       if ! [ -v 1 ]; then
+               >&2 echo "Usage: ${FUNCNAME[0]} <man-page.n>";
+               return ${EX_USAGE};
+       fi;
+
+       local tmp="$(mktemp -t "${1##*/}.XXXXXX")";
+
+       <${1} \
+       man -Tps -l - \
+       |ps2pdf - - \
+       >${tmp};
+       xdg-open ${tmp};
+}
+
+########################################################################
+#      Glibc
+
+#  grep_glibc_prototype()  finds a function prototype in the glibc sources,
+# printing the filename, line number, and the prototype.
+# It should be run from the root of the glibc source tree.
+# Usage example:  .../glibc$ grep_glibc_prototype printf;
+
+function grep_glibc_prototype()
+{
+       if ! [ -v 1 ]; then
+               >&2 echo "Usage: ${FUNCNAME[0]} <func>";
+               return ${EX_USAGE};
+       fi
+
+       find * -type f \
+       |grep '\.h$' \
+       |sort -V \
+       |xargs pcregrep -Mn \
+         "(?s)^[^\s#][\w\s]+\s+\**${1}\s*\([\w\s()[\]*,]*?(...)?\)[\w\s()]*;" \
+       |sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
--
2.30.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: AW: [PATCH v3] scripts/bash_aliases: Add useful functions
  2021-02-16 10:54   ` AW: " Walter Harms
@ 2021-02-16 11:00     ` Alejandro Colomar (man-pages)
  0 siblings, 0 replies; 18+ messages in thread
From: Alejandro Colomar (man-pages) @ 2021-02-16 11:00 UTC (permalink / raw)
  To: Walter Harms; +Cc: linux-man, mtk.manpages

On 2/16/21 11:54 AM, Walter Harms wrote:
> My version of grep (V3) has a -P option and claims to support pcre.
> Maybe that is a replacement for pcregrep ?
> 
> Would reduce the dependencies by one.
> 
> re,
>  wh

Hi Walter,

There's a problem with grep:  it doesn't support the '-M' option, which
allows for multi-line matches.  So pcregrep is needed.

Thanks,

Alex


-- 
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH v4] scripts/bash_aliases: Add useful functions
  2021-02-15 13:15 [PATCH] scripts/bash_aliases: Add useful functions Alejandro Colomar
                   ` (2 preceding siblings ...)
  2021-02-15 20:32 ` [PATCH v3] " Alejandro Colomar
@ 2021-02-18 18:47 ` Alejandro Colomar
  2021-02-18 19:33 ` [PATCH v5] " Alejandro Colomar
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 18+ messages in thread
From: Alejandro Colomar @ 2021-02-18 18:47 UTC (permalink / raw)
  To: mtk.manpages; +Cc: Alejandro Colomar, linux-man, Walter Harms

That file should be sourced (.) from 'bashrc' (or 'bash_aliases').

It contains functions that are useful for the maintenance of this
project.

- grep_syscall()
- grep_syscall_def()
- man_section()
- man_lsfunc()
- pdfman()
- grep_glibc_prototype()

Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
---


v2:
	- Reword license to adequate it for many functions instead
	  of a single program.
	- Add a trailing ';' for consistency.
v3:
	- Resend to avoid confusion:  I forgot to use [PATCH v2]!
v4:
	- Fix some corner cases where a function invocation was printed

---
 scripts/bash_aliases | 171 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 171 insertions(+)
 create mode 100644 scripts/bash_aliases

diff --git a/scripts/bash_aliases b/scripts/bash_aliases
new file mode 100644
index 000000000..8697371f6
--- /dev/null
+++ b/scripts/bash_aliases
@@ -0,0 +1,171 @@
+# SPDX-License-Identifier: GPL-2.0-only
+########################################################################
+#
+# (C) Copyright 2021, Alejandro Colomar
+# These functions are free software; you can redistribute them and/or
+# modify them under the terms of the GNU General Public License
+# as published by the Free Software Foundation; version 2.
+#
+# These functions are distributed in the hope that they will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details
+# (http://www.gnu.org/licenses/gpl-2.0.html).
+#
+########################################################################
+
+########################################################################
+#	Exit status
+
+EX_OK=0;
+EX_USAGE=64;
+
+########################################################################
+#	Linux kernel
+
+#  grep_syscall()  finds the prototype of a syscall in the kernel sources,
+# printing the filename, line number, and the prototype.
+# It should be run from the root of the linux kernel source tree.
+# Usage example:  .../linux$ grep_syscall openat2;
+
+function grep_syscall()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.c$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+
+	find * -type f \
+	|grep '\.[ch]$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^asmlinkage\s+[\w\s]+\**sys_${1}\s*\(.*?\)" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
+
+#  grep_syscall_def()  finds the definition of a syscall in the kernel sources,
+# printing the filename, line number, and the function definition.
+# It should be run from the root of the linux kernel source tree.
+# Usage example:  .../linux$ grep_syscall_def openat2;
+
+function grep_syscall_def()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.c$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?^}" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
+
+########################################################################
+#	Linux man-pages
+
+#  man_section()  prints a specific manual page section (DESCRIPTION, SYNOPSIS,
+# ...) of all manual pages in a directory (or in a single manual page file).
+# Usage example:  .../man-pages$ man_section man2 SYNOPSIS;
+
+function man_section()
+{
+	if ! [ -v 2 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>";
+		return ${EX_USAGE};
+	fi
+
+	find "${1}" -type f \
+	|xargs grep -l "\.SH ${2}" \
+	|sort -V \
+	|while read -r manpage; do
+		<${manpage} \
+		sed -n \
+			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
+			-e "/^\.SH ${2}/p" \
+			-e "/^\.SH ${2}/,/^\.SH/{/^\.SH/!p}" \
+		|man -P cat -l - 2>/dev/null;
+	done;
+}
+
+#  man_lsfunc()  prints the name of all C functions declared in the SYNOPSIS
+# of all manual pages in a directory (or in a single manual page file).
+# Each name is printed in a separate line
+# Usage example:  .../man-pages$ man_lsfunc man2;
+
+function man_lsfunc()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <dir>";
+		return ${EX_USAGE};
+	fi
+
+	find "${1}" -type f \
+	|xargs grep -l "\.SH SYNOPSIS" \
+	|sort -V \
+	|while read -r manpage; do
+		<${manpage} \
+		sed -n \
+			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
+			-e "/^\.SH SYNOPSIS/p" \
+			-e "/^\.SH SYNOPSIS/,/^\.SH/{/^\.SH/!p}" \
+		|sed \
+			-e '/Feature/,$d' \
+			-e '/{/,/}/d' \
+		|man -P cat -l - 2>/dev/null;
+	done \
+	|sed -n "/^SYNOPSIS/,/^\w/p" \
+	|grep '^       \w' \
+	|grep -v ':' \
+	|sed 's/^[^(]* \**\(\w*\)(.*/\1/' \
+	|grep '^\w' \
+	|uniq;
+}
+
+#  pdfman()  reanders a manual page in PDF
+# Usage example:  .../man-pages$ pdfman man2/membarrier.2;
+
+function pdfman()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <man-page.n>";
+		return ${EX_USAGE};
+	fi;
+
+	local tmp="$(mktemp -t "${1##*/}.XXXXXX")";
+
+	<${1} \
+	man -Tps -l - \
+	|ps2pdf - - \
+	>${tmp};
+	xdg-open ${tmp};
+}
+
+########################################################################
+#	Glibc
+
+#  grep_glibc_prototype()  finds a function prototype in the glibc sources,
+# printing the filename, line number, and the prototype.
+# It should be run from the root of the glibc source tree.
+# Usage example:  .../glibc$ grep_glibc_prototype printf;
+
+function grep_glibc_prototype()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <func>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.h$' \
+	|sort -V \
+	|xargs pcregrep -Mn \
+	  "(?s)^[^\s#][\w\s]+\s+\**${1}\s*\([\w\s()[\]*,]+?(...)?\)[\w\s()]*;" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
-- 
2.30.1.721.g45526154a5


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v5] scripts/bash_aliases: Add useful functions
  2021-02-15 13:15 [PATCH] scripts/bash_aliases: Add useful functions Alejandro Colomar
                   ` (3 preceding siblings ...)
  2021-02-18 18:47 ` [PATCH v4] " Alejandro Colomar
@ 2021-02-18 19:33 ` Alejandro Colomar
  2021-02-18 21:17 ` [PATCH v6] " Alejandro Colomar
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 18+ messages in thread
From: Alejandro Colomar @ 2021-02-18 19:33 UTC (permalink / raw)
  To: mtk.manpages; +Cc: Alejandro Colomar, linux-man, Walter Harms

That file should be sourced (.) from 'bashrc' (or 'bash_aliases').

It contains functions that are useful for the maintenance of this
project.

- grep_syscall()
- grep_syscall_def()
- man_section()
- man_lsfunc()
- pdfman()
- grep_glibc_prototype()

Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
---

v2:
	- Reword license to adequate it for many functions instead
	  of a single program.
	- Add a trailing ';' for consistency.
v3:
	- Resend to avoid confusion:  I forgot to use [PATCH v2]!
v4:
	- grep_glibc_prototype(): Fix some corner cases where
	  a function invocation was printed.
v5:
	- grep_glibc_prototype(): Fix some corner cases where
	  a function prototype with many attributes wasn't printed.

---
 scripts/bash_aliases | 171 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 171 insertions(+)
 create mode 100644 scripts/bash_aliases

diff --git a/scripts/bash_aliases b/scripts/bash_aliases
new file mode 100644
index 000000000..486788ad3
--- /dev/null
+++ b/scripts/bash_aliases
@@ -0,0 +1,171 @@
+# SPDX-License-Identifier: GPL-2.0-only
+########################################################################
+#
+# (C) Copyright 2021, Alejandro Colomar
+# These functions are free software; you can redistribute them and/or
+# modify them under the terms of the GNU General Public License
+# as published by the Free Software Foundation; version 2.
+#
+# These functions are distributed in the hope that they will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details
+# (http://www.gnu.org/licenses/gpl-2.0.html).
+#
+########################################################################
+
+########################################################################
+#	Exit status
+
+EX_OK=0;
+EX_USAGE=64;
+
+########################################################################
+#	Linux kernel
+
+#  grep_syscall()  finds the prototype of a syscall in the kernel sources,
+# printing the filename, line number, and the prototype.
+# It should be run from the root of the linux kernel source tree.
+# Usage example:  .../linux$ grep_syscall openat2;
+
+function grep_syscall()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.c$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+
+	find * -type f \
+	|grep '\.[ch]$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^asmlinkage\s+[\w\s]+\**sys_${1}\s*\(.*?\)" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
+
+#  grep_syscall_def()  finds the definition of a syscall in the kernel sources,
+# printing the filename, line number, and the function definition.
+# It should be run from the root of the linux kernel source tree.
+# Usage example:  .../linux$ grep_syscall_def openat2;
+
+function grep_syscall_def()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.c$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?^}" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
+
+########################################################################
+#	Linux man-pages
+
+#  man_section()  prints a specific manual page section (DESCRIPTION, SYNOPSIS,
+# ...) of all manual pages in a directory (or in a single manual page file).
+# Usage example:  .../man-pages$ man_section man2 SYNOPSIS;
+
+function man_section()
+{
+	if ! [ -v 2 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>";
+		return ${EX_USAGE};
+	fi
+
+	find "${1}" -type f \
+	|xargs grep -l "\.SH ${2}" \
+	|sort -V \
+	|while read -r manpage; do
+		<${manpage} \
+		sed -n \
+			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
+			-e "/^\.SH ${2}/p" \
+			-e "/^\.SH ${2}/,/^\.SH/{/^\.SH/!p}" \
+		|man -P cat -l - 2>/dev/null;
+	done;
+}
+
+#  man_lsfunc()  prints the name of all C functions declared in the SYNOPSIS
+# of all manual pages in a directory (or in a single manual page file).
+# Each name is printed in a separate line
+# Usage example:  .../man-pages$ man_lsfunc man2;
+
+function man_lsfunc()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <dir>";
+		return ${EX_USAGE};
+	fi
+
+	find "${1}" -type f \
+	|xargs grep -l "\.SH SYNOPSIS" \
+	|sort -V \
+	|while read -r manpage; do
+		<${manpage} \
+		sed -n \
+			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
+			-e "/^\.SH SYNOPSIS/p" \
+			-e "/^\.SH SYNOPSIS/,/^\.SH/{/^\.SH/!p}" \
+		|sed \
+			-e '/Feature/,$d' \
+			-e '/{/,/}/d' \
+		|man -P cat -l - 2>/dev/null;
+	done \
+	|sed -n "/^SYNOPSIS/,/^\w/p" \
+	|grep '^       \w' \
+	|grep -v ':' \
+	|sed 's/^[^(]* \**\(\w*\)(.*/\1/' \
+	|grep '^\w' \
+	|uniq;
+}
+
+#  pdfman()  reanders a manual page in PDF
+# Usage example:  .../man-pages$ pdfman man2/membarrier.2;
+
+function pdfman()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <man-page.n>";
+		return ${EX_USAGE};
+	fi;
+
+	local tmp="$(mktemp -t "${1##*/}.XXXXXX")";
+
+	<${1} \
+	man -Tps -l - \
+	|ps2pdf - - \
+	>${tmp};
+	xdg-open ${tmp};
+}
+
+########################################################################
+#	Glibc
+
+#  grep_glibc_prototype()  finds a function prototype in the glibc sources,
+# printing the filename, line number, and the prototype.
+# It should be run from the root of the glibc source tree.
+# Usage example:  .../glibc$ grep_glibc_prototype printf;
+
+function grep_glibc_prototype()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <func>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.h$' \
+	|sort -V \
+	|xargs pcregrep -Mn \
+	  "(?s)^[^\s#][\w\s]+\s+\**${1}\s*\([\w\s()[\]*,]+?(...)?\)[\w\s(),]*;" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
-- 
2.30.1.721.g45526154a5


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v6] scripts/bash_aliases: Add useful functions
  2021-02-15 13:15 [PATCH] scripts/bash_aliases: Add useful functions Alejandro Colomar
                   ` (4 preceding siblings ...)
  2021-02-18 19:33 ` [PATCH v5] " Alejandro Colomar
@ 2021-02-18 21:17 ` Alejandro Colomar
  2021-02-19 14:13 ` [PATCH v7] " Alejandro Colomar
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 18+ messages in thread
From: Alejandro Colomar @ 2021-02-18 21:17 UTC (permalink / raw)
  To: mtk.manpages; +Cc: Alejandro Colomar, linux-man, Walter Harms

That file should be sourced (.) from 'bashrc' (or 'bash_aliases').

It contains functions that are useful for the maintenance of this
project.

- grep_syscall()
- grep_syscall_def()
- man_section()
- man_lsfunc()
- pdfman()
- grep_glibc_prototype()

Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
---

v2:
	- Reword license to adequate it for many functions instead
	  of a single program.
	- Add a trailing ';' for consistency.
v3:
	- Resend to avoid confusion:  I forgot to use [PATCH v2]!
v4:
	- grep_glibc_prototype(): Fix some corner cases where
	  a function invocation was printed.
v5:
	- grep_glibc_prototype(): Fix some corner cases where
	  a function prototype with many attributes wasn't printed.
v6:
	- man_lsfunc(): Allow listing more than one dir.
	  Example: $ man_lsfunc man[23];

---
 scripts/bash_aliases | 171 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 171 insertions(+)
 create mode 100644 scripts/bash_aliases

diff --git a/scripts/bash_aliases b/scripts/bash_aliases
new file mode 100644
index 000000000..c88ef9b36
--- /dev/null
+++ b/scripts/bash_aliases
@@ -0,0 +1,171 @@
+# SPDX-License-Identifier: GPL-2.0-only
+########################################################################
+#
+# (C) Copyright 2021, Alejandro Colomar
+# These functions are free software; you can redistribute them and/or
+# modify them under the terms of the GNU General Public License
+# as published by the Free Software Foundation; version 2.
+#
+# These functions are distributed in the hope that they will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details
+# (http://www.gnu.org/licenses/gpl-2.0.html).
+#
+########################################################################
+
+########################################################################
+#	Exit status
+
+EX_OK=0;
+EX_USAGE=64;
+
+########################################################################
+#	Linux kernel
+
+#  grep_syscall()  finds the prototype of a syscall in the kernel sources,
+# printing the filename, line number, and the prototype.
+# It should be run from the root of the linux kernel source tree.
+# Usage example:  .../linux$ grep_syscall openat2;
+
+function grep_syscall()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.c$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+
+	find * -type f \
+	|grep '\.[ch]$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^asmlinkage\s+[\w\s]+\**sys_${1}\s*\(.*?\)" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
+
+#  grep_syscall_def()  finds the definition of a syscall in the kernel sources,
+# printing the filename, line number, and the function definition.
+# It should be run from the root of the linux kernel source tree.
+# Usage example:  .../linux$ grep_syscall_def openat2;
+
+function grep_syscall_def()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.c$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?^}" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
+
+########################################################################
+#	Linux man-pages
+
+#  man_section()  prints a specific manual page section (DESCRIPTION, SYNOPSIS,
+# ...) of all manual pages in a directory (or in a single manual page file).
+# Usage example:  .../man-pages$ man_section man2 SYNOPSIS;
+
+function man_section()
+{
+	if ! [ -v 2 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>";
+		return ${EX_USAGE};
+	fi
+
+	find "${1}" -type f \
+	|xargs grep -l "\.SH ${2}" \
+	|sort -V \
+	|while read -r manpage; do
+		<${manpage} \
+		sed -n \
+			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
+			-e "/^\.SH ${2}/p" \
+			-e "/^\.SH ${2}/,/^\.SH/{/^\.SH/!p}" \
+		|man -P cat -l - 2>/dev/null;
+	done;
+}
+
+#  man_lsfunc()  prints the name of all C functions declared in the SYNOPSIS
+# of all manual pages in a directory (or in a single manual page file).
+# Each name is printed in a separate line
+# Usage example:  .../man-pages$ man_lsfunc man2;
+
+function man_lsfunc()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <dir>";
+		return ${EX_USAGE};
+	fi
+
+	find "${@}" -type f \
+	|xargs grep -l "\.SH SYNOPSIS" \
+	|sort -V \
+	|while read -r manpage; do
+		<${manpage} \
+		sed -n \
+			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
+			-e "/^\.SH SYNOPSIS/p" \
+			-e "/^\.SH SYNOPSIS/,/^\.SH/{/^\.SH/!p}" \
+		|sed \
+			-e '/Feature/,$d' \
+			-e '/{/,/}/d' \
+		|man -P cat -l - 2>/dev/null;
+	done \
+	|sed -n "/^SYNOPSIS/,/^\w/p" \
+	|grep '^       \w' \
+	|grep -v ':' \
+	|sed 's/^[^(]* \**\(\w*\)(.*/\1/' \
+	|grep '^\w' \
+	|uniq;
+}
+
+#  pdfman()  reanders a manual page in PDF
+# Usage example:  .../man-pages$ pdfman man2/membarrier.2;
+
+function pdfman()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <man-page.n>";
+		return ${EX_USAGE};
+	fi;
+
+	local tmp="$(mktemp -t "${1##*/}.XXXXXX")";
+
+	<${1} \
+	man -Tps -l - \
+	|ps2pdf - - \
+	>${tmp};
+	xdg-open ${tmp};
+}
+
+########################################################################
+#	Glibc
+
+#  grep_glibc_prototype()  finds a function prototype in the glibc sources,
+# printing the filename, line number, and the prototype.
+# It should be run from the root of the glibc source tree.
+# Usage example:  .../glibc$ grep_glibc_prototype printf;
+
+function grep_glibc_prototype()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <func>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.h$' \
+	|sort -V \
+	|xargs pcregrep -Mn \
+	  "(?s)^[^\s#][\w\s]+\s+\**${1}\s*\([\w\s()[\]*,]+?(...)?\)[\w\s(),]*;" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
-- 
2.30.1.721.g45526154a5


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v7] scripts/bash_aliases: Add useful functions
  2021-02-15 13:15 [PATCH] scripts/bash_aliases: Add useful functions Alejandro Colomar
                   ` (5 preceding siblings ...)
  2021-02-18 21:17 ` [PATCH v6] " Alejandro Colomar
@ 2021-02-19 14:13 ` Alejandro Colomar
  2021-02-19 14:22 ` [PATCH v8] " Alejandro Colomar
  2021-02-19 14:32 ` [PATCH v9] " Alejandro Colomar
  8 siblings, 0 replies; 18+ messages in thread
From: Alejandro Colomar @ 2021-02-19 14:13 UTC (permalink / raw)
  To: mtk.manpages; +Cc: Alejandro Colomar, linux-man, Walter Harms

That file should be sourced (.) from 'bashrc' (or 'bash_aliases').

It contains functions that are useful for the maintenance of this
project.

- grep_syscall()
- grep_syscall_def()
- man_section()
- man_lsfunc()
- pdfman()
- grep_glibc_prototype()

Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
---

v2:
	- Reword license to adequate it for many functions instead
	  of a single program.
	- Add a trailing ';' for consistency.
v3:
	- Resend to avoid confusion:  I forgot to use [PATCH v2]!
v4:
	- grep_glibc_prototype(): Fix some corner cases where
	  a function invocation was printed.
v5:
	- grep_glibc_prototype(): Fix some corner cases where
	  a function prototype with many attributes wasn't printed.
v6:
	- man_lsfunc(): Allow listing more than one dir.
	  Example: $ man_lsfunc man[23];
v7:
	- grep_glibc_prototype(): Fix some corner cases where
	  a function invocation was printed.

---
 scripts/bash_aliases | 171 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 171 insertions(+)
 create mode 100644 scripts/bash_aliases

diff --git a/scripts/bash_aliases b/scripts/bash_aliases
new file mode 100644
index 000000000..f718f603d
--- /dev/null
+++ b/scripts/bash_aliases
@@ -0,0 +1,171 @@
+# SPDX-License-Identifier: GPL-2.0-only
+########################################################################
+#
+# (C) Copyright 2021, Alejandro Colomar
+# These functions are free software; you can redistribute them and/or
+# modify them under the terms of the GNU General Public License
+# as published by the Free Software Foundation; version 2.
+#
+# These functions are distributed in the hope that they will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details
+# (http://www.gnu.org/licenses/gpl-2.0.html).
+#
+########################################################################
+
+########################################################################
+#	Exit status
+
+EX_OK=0;
+EX_USAGE=64;
+
+########################################################################
+#	Linux kernel
+
+#  grep_syscall()  finds the prototype of a syscall in the kernel sources,
+# printing the filename, line number, and the prototype.
+# It should be run from the root of the linux kernel source tree.
+# Usage example:  .../linux$ grep_syscall openat2;
+
+function grep_syscall()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.c$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+
+	find * -type f \
+	|grep '\.[ch]$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^asmlinkage\s+[\w\s]+\**sys_${1}\s*\(.*?\)" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
+
+#  grep_syscall_def()  finds the definition of a syscall in the kernel sources,
+# printing the filename, line number, and the function definition.
+# It should be run from the root of the linux kernel source tree.
+# Usage example:  .../linux$ grep_syscall_def openat2;
+
+function grep_syscall_def()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.c$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?^}" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
+
+########################################################################
+#	Linux man-pages
+
+#  man_section()  prints a specific manual page section (DESCRIPTION, SYNOPSIS,
+# ...) of all manual pages in a directory (or in a single manual page file).
+# Usage example:  .../man-pages$ man_section man2 SYNOPSIS;
+
+function man_section()
+{
+	if ! [ -v 2 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>";
+		return ${EX_USAGE};
+	fi
+
+	find "${1}" -type f \
+	|xargs grep -l "\.SH ${2}" \
+	|sort -V \
+	|while read -r manpage; do
+		<${manpage} \
+		sed -n \
+			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
+			-e "/^\.SH ${2}/p" \
+			-e "/^\.SH ${2}/,/^\.SH/{/^\.SH/!p}" \
+		|man -P cat -l - 2>/dev/null;
+	done;
+}
+
+#  man_lsfunc()  prints the name of all C functions declared in the SYNOPSIS
+# of all manual pages in a directory (or in a single manual page file).
+# Each name is printed in a separate line
+# Usage example:  .../man-pages$ man_lsfunc man2;
+
+function man_lsfunc()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <dir>";
+		return ${EX_USAGE};
+	fi
+
+	find "${@}" -type f \
+	|xargs grep -l "\.SH SYNOPSIS" \
+	|sort -V \
+	|while read -r manpage; do
+		<${manpage} \
+		sed -n \
+			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
+			-e "/^\.SH SYNOPSIS/p" \
+			-e "/^\.SH SYNOPSIS/,/^\.SH/{/^\.SH/!p}" \
+		|sed \
+			-e '/Feature/,$d' \
+			-e '/{/,/}/d' \
+		|man -P cat -l - 2>/dev/null;
+	done \
+	|sed -n "/^SYNOPSIS/,/^\w/p" \
+	|grep '^       \w' \
+	|grep -v ':' \
+	|sed 's/^[^(]* \**\(\w*\)(.*/\1/' \
+	|grep '^\w' \
+	|uniq;
+}
+
+#  pdfman()  reanders a manual page in PDF
+# Usage example:  .../man-pages$ pdfman man2/membarrier.2;
+
+function pdfman()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <man-page.n>";
+		return ${EX_USAGE};
+	fi;
+
+	local tmp="$(mktemp -t "${1##*/}.XXXXXX")";
+
+	<${1} \
+	man -Tps -l - \
+	|ps2pdf - - \
+	>${tmp};
+	xdg-open ${tmp};
+}
+
+########################################################################
+#	Glibc
+
+#  grep_glibc_prototype()  finds a function prototype in the glibc sources,
+# printing the filename, line number, and the prototype.
+# It should be run from the root of the glibc source tree.
+# Usage example:  .../glibc$ grep_glibc_prototype printf;
+
+function grep_glibc_prototype()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <func>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.h$' \
+	|sort -V \
+	|xargs pcregrep -Mn \
+	  "(?s)^[\w[][\w\s]+\s+\**${1}\s*\([\w\s()[\]*,]+?(...)?\)[\w\s(),]*;" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
-- 
2.30.1.721.g45526154a5


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v8] scripts/bash_aliases: Add useful functions
  2021-02-15 13:15 [PATCH] scripts/bash_aliases: Add useful functions Alejandro Colomar
                   ` (6 preceding siblings ...)
  2021-02-19 14:13 ` [PATCH v7] " Alejandro Colomar
@ 2021-02-19 14:22 ` Alejandro Colomar
  2021-02-19 14:32 ` [PATCH v9] " Alejandro Colomar
  8 siblings, 0 replies; 18+ messages in thread
From: Alejandro Colomar @ 2021-02-19 14:22 UTC (permalink / raw)
  To: mtk.manpages; +Cc: Alejandro Colomar, linux-man, Walter Harms

That file should be sourced (.) from 'bashrc' (or 'bash_aliases').

It contains functions that are useful for the maintenance of this
project.

- grep_syscall()
- grep_syscall_def()
- man_section()
- man_lsfunc()
- pdfman()
- grep_glibc_prototype()

Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
---


v2:
	- Reword license to adequate it for many functions instead
	  of a single program.
	- Add a trailing ';' for consistency.
v3:
	- Resend to avoid confusion:  I forgot to use [PATCH v2]!
v4:
	- grep_glibc_prototype(): Fix some corner cases where
	  a function invocation was printed.
v5:
	- grep_glibc_prototype(): Fix some corner cases where
	  a function prototype with many attributes wasn't printed.
v6:
	- man_lsfunc(): Allow listing more than one dir.
	  Example: $ man_lsfunc man[23];
v7:
	- grep_glibc_prototype(): Fix some corner cases where
	  a function invocation was printed.
v8:
	- grep_glibc_prototype():
		- Print attributes before the prototype too.
		  (AFAICS, there are none; but just in case...)
		- Accept C2x attributes (glibc doesn't use them yet,
		  but it doesn't hurt to accept them).

---
 scripts/bash_aliases | 171 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 171 insertions(+)
 create mode 100644 scripts/bash_aliases

diff --git a/scripts/bash_aliases b/scripts/bash_aliases
new file mode 100644
index 000000000..d66766ee6
--- /dev/null
+++ b/scripts/bash_aliases
@@ -0,0 +1,171 @@
+# SPDX-License-Identifier: GPL-2.0-only
+########################################################################
+#
+# (C) Copyright 2021, Alejandro Colomar
+# These functions are free software; you can redistribute them and/or
+# modify them under the terms of the GNU General Public License
+# as published by the Free Software Foundation; version 2.
+#
+# These functions are distributed in the hope that they will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details
+# (http://www.gnu.org/licenses/gpl-2.0.html).
+#
+########################################################################
+
+########################################################################
+#	Exit status
+
+EX_OK=0;
+EX_USAGE=64;
+
+########################################################################
+#	Linux kernel
+
+#  grep_syscall()  finds the prototype of a syscall in the kernel sources,
+# printing the filename, line number, and the prototype.
+# It should be run from the root of the linux kernel source tree.
+# Usage example:  .../linux$ grep_syscall openat2;
+
+function grep_syscall()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.c$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+
+	find * -type f \
+	|grep '\.[ch]$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^asmlinkage\s+[\w\s]+\**sys_${1}\s*\(.*?\)" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
+
+#  grep_syscall_def()  finds the definition of a syscall in the kernel sources,
+# printing the filename, line number, and the function definition.
+# It should be run from the root of the linux kernel source tree.
+# Usage example:  .../linux$ grep_syscall_def openat2;
+
+function grep_syscall_def()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.c$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?^}" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
+
+########################################################################
+#	Linux man-pages
+
+#  man_section()  prints a specific manual page section (DESCRIPTION, SYNOPSIS,
+# ...) of all manual pages in a directory (or in a single manual page file).
+# Usage example:  .../man-pages$ man_section man2 SYNOPSIS;
+
+function man_section()
+{
+	if ! [ -v 2 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>";
+		return ${EX_USAGE};
+	fi
+
+	find "${1}" -type f \
+	|xargs grep -l "\.SH ${2}" \
+	|sort -V \
+	|while read -r manpage; do
+		<${manpage} \
+		sed -n \
+			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
+			-e "/^\.SH ${2}/p" \
+			-e "/^\.SH ${2}/,/^\.SH/{/^\.SH/!p}" \
+		|man -P cat -l - 2>/dev/null;
+	done;
+}
+
+#  man_lsfunc()  prints the name of all C functions declared in the SYNOPSIS
+# of all manual pages in a directory (or in a single manual page file).
+# Each name is printed in a separate line
+# Usage example:  .../man-pages$ man_lsfunc man2;
+
+function man_lsfunc()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <dir>";
+		return ${EX_USAGE};
+	fi
+
+	find "${@}" -type f \
+	|xargs grep -l "\.SH SYNOPSIS" \
+	|sort -V \
+	|while read -r manpage; do
+		<${manpage} \
+		sed -n \
+			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
+			-e "/^\.SH SYNOPSIS/p" \
+			-e "/^\.SH SYNOPSIS/,/^\.SH/{/^\.SH/!p}" \
+		|sed \
+			-e '/Feature/,$d' \
+			-e '/{/,/}/d' \
+		|man -P cat -l - 2>/dev/null;
+	done \
+	|sed -n "/^SYNOPSIS/,/^\w/p" \
+	|grep '^       \w' \
+	|grep -v ':' \
+	|sed 's/^[^(]* \**\(\w*\)(.*/\1/' \
+	|grep '^\w' \
+	|uniq;
+}
+
+#  pdfman()  reanders a manual page in PDF
+# Usage example:  .../man-pages$ pdfman man2/membarrier.2;
+
+function pdfman()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <man-page.n>";
+		return ${EX_USAGE};
+	fi;
+
+	local tmp="$(mktemp -t "${1##*/}.XXXXXX")";
+
+	<${1} \
+	man -Tps -l - \
+	|ps2pdf - - \
+	>${tmp};
+	xdg-open ${tmp};
+}
+
+########################################################################
+#	Glibc
+
+#  grep_glibc_prototype()  finds a function prototype in the glibc sources,
+# printing the filename, line number, and the prototype.
+# It should be run from the root of the glibc source tree.
+# Usage example:  .../glibc$ grep_glibc_prototype printf;
+
+function grep_glibc_prototype()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <func>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.h$' \
+	|sort -V \
+	|xargs pcregrep -Mn \
+	  "(?s)^[\w[][\w\s(),[\]]+\s+\**${1}\s*\([\w\s()[\]*,]+?(...)?\)[\w\s(),[\]]*;" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
-- 
2.30.1.721.g45526154a5


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v9] scripts/bash_aliases: Add useful functions
  2021-02-15 13:15 [PATCH] scripts/bash_aliases: Add useful functions Alejandro Colomar
                   ` (7 preceding siblings ...)
  2021-02-19 14:22 ` [PATCH v8] " Alejandro Colomar
@ 2021-02-19 14:32 ` Alejandro Colomar
  2021-02-20 14:35   ` Michael Kerrisk (man-pages)
                     ` (2 more replies)
  8 siblings, 3 replies; 18+ messages in thread
From: Alejandro Colomar @ 2021-02-19 14:32 UTC (permalink / raw)
  To: mtk.manpages; +Cc: Alejandro Colomar, linux-man, Walter Harms

That file should be sourced (.) from 'bashrc' (or 'bash_aliases').

It contains functions that are useful for the maintenance of this
project.

- grep_syscall()
- grep_syscall_def()
- man_section()
- man_lsfunc()
- pdfman()
- grep_glibc_prototype()

Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
---

v2:
	- Reword license to adequate it for many functions instead
	  of a single program.
	- Add a trailing ';' for consistency.
v3:
	- Resend to avoid confusion:  I forgot to use [PATCH v2]!
v4:
	- grep_glibc_prototype(): Fix some corner cases where
	  a function invocation was printed.
v5:
	- grep_glibc_prototype(): Fix some corner cases where
	  a function prototype with many attributes wasn't printed.
v6:
	- man_lsfunc(): Allow listing more than one dir.
	  Example: $ man_lsfunc man[23];
v7:
	- grep_glibc_prototype(): Fix some corner cases where
	  a function invocation was printed.
v8:
	- grep_glibc_prototype():
		- Print attributes before the prototype too.
		  (AFAICS, there are none; but just in case...)
		- Accept C2x attributes (glibc doesn't use them yet,
		  but it doesn't hurt to accept them).
v9:
	- grep_glibc_prototype(): Accept namespaced C2x attributes,
	  such as [[gnu::nonnull]].

---
 scripts/bash_aliases | 171 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 171 insertions(+)
 create mode 100644 scripts/bash_aliases

diff --git a/scripts/bash_aliases b/scripts/bash_aliases
new file mode 100644
index 000000000..5e4b424af
--- /dev/null
+++ b/scripts/bash_aliases
@@ -0,0 +1,171 @@
+# SPDX-License-Identifier: GPL-2.0-only
+########################################################################
+#
+# (C) Copyright 2021, Alejandro Colomar
+# These functions are free software; you can redistribute them and/or
+# modify them under the terms of the GNU General Public License
+# as published by the Free Software Foundation; version 2.
+#
+# These functions are distributed in the hope that they will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details
+# (http://www.gnu.org/licenses/gpl-2.0.html).
+#
+########################################################################
+
+########################################################################
+#	Exit status
+
+EX_OK=0;
+EX_USAGE=64;
+
+########################################################################
+#	Linux kernel
+
+#  grep_syscall()  finds the prototype of a syscall in the kernel sources,
+# printing the filename, line number, and the prototype.
+# It should be run from the root of the linux kernel source tree.
+# Usage example:  .../linux$ grep_syscall openat2;
+
+function grep_syscall()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.c$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+
+	find * -type f \
+	|grep '\.[ch]$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^asmlinkage\s+[\w\s]+\**sys_${1}\s*\(.*?\)" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
+
+#  grep_syscall_def()  finds the definition of a syscall in the kernel sources,
+# printing the filename, line number, and the function definition.
+# It should be run from the root of the linux kernel source tree.
+# Usage example:  .../linux$ grep_syscall_def openat2;
+
+function grep_syscall_def()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.c$' \
+	|sort -V \
+	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?^}" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
+
+########################################################################
+#	Linux man-pages
+
+#  man_section()  prints a specific manual page section (DESCRIPTION, SYNOPSIS,
+# ...) of all manual pages in a directory (or in a single manual page file).
+# Usage example:  .../man-pages$ man_section man2 SYNOPSIS;
+
+function man_section()
+{
+	if ! [ -v 2 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>";
+		return ${EX_USAGE};
+	fi
+
+	find "${1}" -type f \
+	|xargs grep -l "\.SH ${2}" \
+	|sort -V \
+	|while read -r manpage; do
+		<${manpage} \
+		sed -n \
+			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
+			-e "/^\.SH ${2}/p" \
+			-e "/^\.SH ${2}/,/^\.SH/{/^\.SH/!p}" \
+		|man -P cat -l - 2>/dev/null;
+	done;
+}
+
+#  man_lsfunc()  prints the name of all C functions declared in the SYNOPSIS
+# of all manual pages in a directory (or in a single manual page file).
+# Each name is printed in a separate line
+# Usage example:  .../man-pages$ man_lsfunc man2;
+
+function man_lsfunc()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <dir>";
+		return ${EX_USAGE};
+	fi
+
+	find "${@}" -type f \
+	|xargs grep -l "\.SH SYNOPSIS" \
+	|sort -V \
+	|while read -r manpage; do
+		<${manpage} \
+		sed -n \
+			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
+			-e "/^\.SH SYNOPSIS/p" \
+			-e "/^\.SH SYNOPSIS/,/^\.SH/{/^\.SH/!p}" \
+		|sed \
+			-e '/Feature/,$d' \
+			-e '/{/,/}/d' \
+		|man -P cat -l - 2>/dev/null;
+	done \
+	|sed -n "/^SYNOPSIS/,/^\w/p" \
+	|grep '^       \w' \
+	|grep -v ':' \
+	|sed 's/^[^(]* \**\(\w*\)(.*/\1/' \
+	|grep '^\w' \
+	|uniq;
+}
+
+#  pdfman()  reanders a manual page in PDF
+# Usage example:  .../man-pages$ pdfman man2/membarrier.2;
+
+function pdfman()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <man-page.n>";
+		return ${EX_USAGE};
+	fi;
+
+	local tmp="$(mktemp -t "${1##*/}.XXXXXX")";
+
+	<${1} \
+	man -Tps -l - \
+	|ps2pdf - - \
+	>${tmp};
+	xdg-open ${tmp};
+}
+
+########################################################################
+#	Glibc
+
+#  grep_glibc_prototype()  finds a function prototype in the glibc sources,
+# printing the filename, line number, and the prototype.
+# It should be run from the root of the glibc source tree.
+# Usage example:  .../glibc$ grep_glibc_prototype printf;
+
+function grep_glibc_prototype()
+{
+	if ! [ -v 1 ]; then
+		>&2 echo "Usage: ${FUNCNAME[0]} <func>";
+		return ${EX_USAGE};
+	fi
+
+	find * -type f \
+	|grep '\.h$' \
+	|sort -V \
+	|xargs pcregrep -Mn \
+	  "(?s)^[\w[][\w\s(,)[:\]]+\s+\**${1}\s*\([\w\s(,)[\]*]+?(...)?\)[\w\s(,)[:\]]*;" \
+	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
+}
-- 
2.30.1.721.g45526154a5


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH v9] scripts/bash_aliases: Add useful functions
  2021-02-19 14:32 ` [PATCH v9] " Alejandro Colomar
@ 2021-02-20 14:35   ` Michael Kerrisk (man-pages)
  2021-02-20 21:45   ` Michael Kerrisk (man-pages)
  2021-02-27 15:09   ` Stefan Puiu
  2 siblings, 0 replies; 18+ messages in thread
From: Michael Kerrisk (man-pages) @ 2021-02-20 14:35 UTC (permalink / raw)
  To: Alejandro Colomar; +Cc: mtk.manpages, linux-man, Walter Harms

Hey Alex,

On 2/19/21 3:32 PM, Alejandro Colomar wrote:
> That file should be sourced (.) from 'bashrc' (or 'bash_aliases').
> 
> It contains functions that are useful for the maintenance of this
> project.
> 
> - grep_syscall()
> - grep_syscall_def()
> - man_section()
> - man_lsfunc()
> - pdfman()
> - grep_glibc_prototype()
> 
> Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
> ---
> 
> v2:
> 	- Reword license to adequate it for many functions instead
> 	  of a single program.
> 	- Add a trailing ';' for consistency.
> v3:
> 	- Resend to avoid confusion:  I forgot to use [PATCH v2]!
> v4:
> 	- grep_glibc_prototype(): Fix some corner cases where
> 	  a function invocation was printed.
> v5:
> 	- grep_glibc_prototype(): Fix some corner cases where
> 	  a function prototype with many attributes wasn't printed.
> v6:
> 	- man_lsfunc(): Allow listing more than one dir.
> 	  Example: $ man_lsfunc man[23];
> v7:
> 	- grep_glibc_prototype(): Fix some corner cases where
> 	  a function invocation was printed.
> v8:
> 	- grep_glibc_prototype():
> 		- Print attributes before the prototype too.
> 		  (AFAICS, there are none; but just in case...)
> 		- Accept C2x attributes (glibc doesn't use them yet,
> 		  but it doesn't hurt to accept them).
> v9:
> 	- grep_glibc_prototype(): Accept namespaced C2x attributes,
> 	  such as [[gnu::nonnull]].

I'm wondering if I should apply this, or wait until you hit SEND
again :-).

Cheers,

Michael

> ---
>  scripts/bash_aliases | 171 +++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 171 insertions(+)
>  create mode 100644 scripts/bash_aliases
> 
> diff --git a/scripts/bash_aliases b/scripts/bash_aliases
> new file mode 100644
> index 000000000..5e4b424af
> --- /dev/null
> +++ b/scripts/bash_aliases
> @@ -0,0 +1,171 @@
> +# SPDX-License-Identifier: GPL-2.0-only
> +########################################################################
> +#
> +# (C) Copyright 2021, Alejandro Colomar
> +# These functions are free software; you can redistribute them and/or
> +# modify them under the terms of the GNU General Public License
> +# as published by the Free Software Foundation; version 2.
> +#
> +# These functions are distributed in the hope that they will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details
> +# (http://www.gnu.org/licenses/gpl-2.0.html).
> +#
> +########################################################################
> +
> +########################################################################
> +#	Exit status
> +
> +EX_OK=0;
> +EX_USAGE=64;
> +
> +########################################################################
> +#	Linux kernel
> +
> +#  grep_syscall()  finds the prototype of a syscall in the kernel sources,
> +# printing the filename, line number, and the prototype.
> +# It should be run from the root of the linux kernel source tree.
> +# Usage example:  .../linux$ grep_syscall openat2;
> +
> +function grep_syscall()
> +{
> +	if ! [ -v 1 ]; then
> +		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
> +		return ${EX_USAGE};
> +	fi
> +
> +	find * -type f \
> +	|grep '\.c$' \
> +	|sort -V \
> +	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
> +	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
> +
> +	find * -type f \
> +	|grep '\.[ch]$' \
> +	|sort -V \
> +	|xargs pcregrep -Mn "(?s)^asmlinkage\s+[\w\s]+\**sys_${1}\s*\(.*?\)" \
> +	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
> +}
> +
> +#  grep_syscall_def()  finds the definition of a syscall in the kernel sources,
> +# printing the filename, line number, and the function definition.
> +# It should be run from the root of the linux kernel source tree.
> +# Usage example:  .../linux$ grep_syscall_def openat2;
> +
> +function grep_syscall_def()
> +{
> +	if ! [ -v 1 ]; then
> +		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
> +		return ${EX_USAGE};
> +	fi
> +
> +	find * -type f \
> +	|grep '\.c$' \
> +	|sort -V \
> +	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?^}" \
> +	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
> +}
> +
> +########################################################################
> +#	Linux man-pages
> +
> +#  man_section()  prints a specific manual page section (DESCRIPTION, SYNOPSIS,
> +# ...) of all manual pages in a directory (or in a single manual page file).
> +# Usage example:  .../man-pages$ man_section man2 SYNOPSIS;
> +
> +function man_section()
> +{
> +	if ! [ -v 2 ]; then
> +		>&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>";
> +		return ${EX_USAGE};
> +	fi
> +
> +	find "${1}" -type f \
> +	|xargs grep -l "\.SH ${2}" \
> +	|sort -V \
> +	|while read -r manpage; do
> +		<${manpage} \
> +		sed -n \
> +			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
> +			-e "/^\.SH ${2}/p" \
> +			-e "/^\.SH ${2}/,/^\.SH/{/^\.SH/!p}" \
> +		|man -P cat -l - 2>/dev/null;
> +	done;
> +}
> +
> +#  man_lsfunc()  prints the name of all C functions declared in the SYNOPSIS
> +# of all manual pages in a directory (or in a single manual page file).
> +# Each name is printed in a separate line
> +# Usage example:  .../man-pages$ man_lsfunc man2;
> +
> +function man_lsfunc()
> +{
> +	if ! [ -v 1 ]; then
> +		>&2 echo "Usage: ${FUNCNAME[0]} <dir>";
> +		return ${EX_USAGE};
> +	fi
> +
> +	find "${@}" -type f \
> +	|xargs grep -l "\.SH SYNOPSIS" \
> +	|sort -V \
> +	|while read -r manpage; do
> +		<${manpage} \
> +		sed -n \
> +			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
> +			-e "/^\.SH SYNOPSIS/p" \
> +			-e "/^\.SH SYNOPSIS/,/^\.SH/{/^\.SH/!p}" \
> +		|sed \
> +			-e '/Feature/,$d' \
> +			-e '/{/,/}/d' \
> +		|man -P cat -l - 2>/dev/null;
> +	done \
> +	|sed -n "/^SYNOPSIS/,/^\w/p" \
> +	|grep '^       \w' \
> +	|grep -v ':' \
> +	|sed 's/^[^(]* \**\(\w*\)(.*/\1/' \
> +	|grep '^\w' \
> +	|uniq;
> +}
> +
> +#  pdfman()  reanders a manual page in PDF
> +# Usage example:  .../man-pages$ pdfman man2/membarrier.2;
> +
> +function pdfman()
> +{
> +	if ! [ -v 1 ]; then
> +		>&2 echo "Usage: ${FUNCNAME[0]} <man-page.n>";
> +		return ${EX_USAGE};
> +	fi;
> +
> +	local tmp="$(mktemp -t "${1##*/}.XXXXXX")";
> +
> +	<${1} \
> +	man -Tps -l - \
> +	|ps2pdf - - \
> +	>${tmp};
> +	xdg-open ${tmp};
> +}
> +
> +########################################################################
> +#	Glibc
> +
> +#  grep_glibc_prototype()  finds a function prototype in the glibc sources,
> +# printing the filename, line number, and the prototype.
> +# It should be run from the root of the glibc source tree.
> +# Usage example:  .../glibc$ grep_glibc_prototype printf;
> +
> +function grep_glibc_prototype()
> +{
> +	if ! [ -v 1 ]; then
> +		>&2 echo "Usage: ${FUNCNAME[0]} <func>";
> +		return ${EX_USAGE};
> +	fi
> +
> +	find * -type f \
> +	|grep '\.h$' \
> +	|sort -V \
> +	|xargs pcregrep -Mn \
> +	  "(?s)^[\w[][\w\s(,)[:\]]+\s+\**${1}\s*\([\w\s(,)[\]*]+?(...)?\)[\w\s(,)[:\]]*;" \
> +	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
> +}
> 


-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v9] scripts/bash_aliases: Add useful functions
  2021-02-19 14:32 ` [PATCH v9] " Alejandro Colomar
  2021-02-20 14:35   ` Michael Kerrisk (man-pages)
@ 2021-02-20 21:45   ` Michael Kerrisk (man-pages)
  2021-02-27 15:09   ` Stefan Puiu
  2 siblings, 0 replies; 18+ messages in thread
From: Michael Kerrisk (man-pages) @ 2021-02-20 21:45 UTC (permalink / raw)
  To: Alejandro Colomar; +Cc: mtk.manpages, linux-man, Walter Harms

Hi Alex,

On 2/19/21 3:32 PM, Alejandro Colomar wrote:
> That file should be sourced (.) from 'bashrc' (or 'bash_aliases').
> 
> It contains functions that are useful for the maintenance of this
> project.
> 
> - grep_syscall()
> - grep_syscall_def()
> - man_section()
> - man_lsfunc()
> - pdfman()
> - grep_glibc_prototype()
> 
> Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>

Patch applied! Thanks!

Cheers,

Michael

> ---
> 
> v2:
> 	- Reword license to adequate it for many functions instead
> 	  of a single program.
> 	- Add a trailing ';' for consistency.
> v3:
> 	- Resend to avoid confusion:  I forgot to use [PATCH v2]!
> v4:
> 	- grep_glibc_prototype(): Fix some corner cases where
> 	  a function invocation was printed.
> v5:
> 	- grep_glibc_prototype(): Fix some corner cases where
> 	  a function prototype with many attributes wasn't printed.
> v6:
> 	- man_lsfunc(): Allow listing more than one dir.
> 	  Example: $ man_lsfunc man[23];
> v7:
> 	- grep_glibc_prototype(): Fix some corner cases where
> 	  a function invocation was printed.
> v8:
> 	- grep_glibc_prototype():
> 		- Print attributes before the prototype too.
> 		  (AFAICS, there are none; but just in case...)
> 		- Accept C2x attributes (glibc doesn't use them yet,
> 		  but it doesn't hurt to accept them).
> v9:
> 	- grep_glibc_prototype(): Accept namespaced C2x attributes,
> 	  such as [[gnu::nonnull]].
> 
> ---
>  scripts/bash_aliases | 171 +++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 171 insertions(+)
>  create mode 100644 scripts/bash_aliases
> 
> diff --git a/scripts/bash_aliases b/scripts/bash_aliases
> new file mode 100644
> index 000000000..5e4b424af
> --- /dev/null
> +++ b/scripts/bash_aliases
> @@ -0,0 +1,171 @@
> +# SPDX-License-Identifier: GPL-2.0-only
> +########################################################################
> +#
> +# (C) Copyright 2021, Alejandro Colomar
> +# These functions are free software; you can redistribute them and/or
> +# modify them under the terms of the GNU General Public License
> +# as published by the Free Software Foundation; version 2.
> +#
> +# These functions are distributed in the hope that they will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details
> +# (http://www.gnu.org/licenses/gpl-2.0.html).
> +#
> +########################################################################
> +
> +########################################################################
> +#	Exit status
> +
> +EX_OK=0;
> +EX_USAGE=64;
> +
> +########################################################################
> +#	Linux kernel
> +
> +#  grep_syscall()  finds the prototype of a syscall in the kernel sources,
> +# printing the filename, line number, and the prototype.
> +# It should be run from the root of the linux kernel source tree.
> +# Usage example:  .../linux$ grep_syscall openat2;
> +
> +function grep_syscall()
> +{
> +	if ! [ -v 1 ]; then
> +		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
> +		return ${EX_USAGE};
> +	fi
> +
> +	find * -type f \
> +	|grep '\.c$' \
> +	|sort -V \
> +	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
> +	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
> +
> +	find * -type f \
> +	|grep '\.[ch]$' \
> +	|sort -V \
> +	|xargs pcregrep -Mn "(?s)^asmlinkage\s+[\w\s]+\**sys_${1}\s*\(.*?\)" \
> +	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
> +}
> +
> +#  grep_syscall_def()  finds the definition of a syscall in the kernel sources,
> +# printing the filename, line number, and the function definition.
> +# It should be run from the root of the linux kernel source tree.
> +# Usage example:  .../linux$ grep_syscall_def openat2;
> +
> +function grep_syscall_def()
> +{
> +	if ! [ -v 1 ]; then
> +		>&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
> +		return ${EX_USAGE};
> +	fi
> +
> +	find * -type f \
> +	|grep '\.c$' \
> +	|sort -V \
> +	|xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?^}" \
> +	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
> +}
> +
> +########################################################################
> +#	Linux man-pages
> +
> +#  man_section()  prints a specific manual page section (DESCRIPTION, SYNOPSIS,
> +# ...) of all manual pages in a directory (or in a single manual page file).
> +# Usage example:  .../man-pages$ man_section man2 SYNOPSIS;
> +
> +function man_section()
> +{
> +	if ! [ -v 2 ]; then
> +		>&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>";
> +		return ${EX_USAGE};
> +	fi
> +
> +	find "${1}" -type f \
> +	|xargs grep -l "\.SH ${2}" \
> +	|sort -V \
> +	|while read -r manpage; do
> +		<${manpage} \
> +		sed -n \
> +			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
> +			-e "/^\.SH ${2}/p" \
> +			-e "/^\.SH ${2}/,/^\.SH/{/^\.SH/!p}" \
> +		|man -P cat -l - 2>/dev/null;
> +	done;
> +}
> +
> +#  man_lsfunc()  prints the name of all C functions declared in the SYNOPSIS
> +# of all manual pages in a directory (or in a single manual page file).
> +# Each name is printed in a separate line
> +# Usage example:  .../man-pages$ man_lsfunc man2;
> +
> +function man_lsfunc()
> +{
> +	if ! [ -v 1 ]; then
> +		>&2 echo "Usage: ${FUNCNAME[0]} <dir>";
> +		return ${EX_USAGE};
> +	fi
> +
> +	find "${@}" -type f \
> +	|xargs grep -l "\.SH SYNOPSIS" \
> +	|sort -V \
> +	|while read -r manpage; do
> +		<${manpage} \
> +		sed -n \
> +			-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
> +			-e "/^\.SH SYNOPSIS/p" \
> +			-e "/^\.SH SYNOPSIS/,/^\.SH/{/^\.SH/!p}" \
> +		|sed \
> +			-e '/Feature/,$d' \
> +			-e '/{/,/}/d' \
> +		|man -P cat -l - 2>/dev/null;
> +	done \
> +	|sed -n "/^SYNOPSIS/,/^\w/p" \
> +	|grep '^       \w' \
> +	|grep -v ':' \
> +	|sed 's/^[^(]* \**\(\w*\)(.*/\1/' \
> +	|grep '^\w' \
> +	|uniq;
> +}
> +
> +#  pdfman()  reanders a manual page in PDF
> +# Usage example:  .../man-pages$ pdfman man2/membarrier.2;
> +
> +function pdfman()
> +{
> +	if ! [ -v 1 ]; then
> +		>&2 echo "Usage: ${FUNCNAME[0]} <man-page.n>";
> +		return ${EX_USAGE};
> +	fi;
> +
> +	local tmp="$(mktemp -t "${1##*/}.XXXXXX")";
> +
> +	<${1} \
> +	man -Tps -l - \
> +	|ps2pdf - - \
> +	>${tmp};
> +	xdg-open ${tmp};
> +}
> +
> +########################################################################
> +#	Glibc
> +
> +#  grep_glibc_prototype()  finds a function prototype in the glibc sources,
> +# printing the filename, line number, and the prototype.
> +# It should be run from the root of the glibc source tree.
> +# Usage example:  .../glibc$ grep_glibc_prototype printf;
> +
> +function grep_glibc_prototype()
> +{
> +	if ! [ -v 1 ]; then
> +		>&2 echo "Usage: ${FUNCNAME[0]} <func>";
> +		return ${EX_USAGE};
> +	fi
> +
> +	find * -type f \
> +	|grep '\.h$' \
> +	|sort -V \
> +	|xargs pcregrep -Mn \
> +	  "(?s)^[\w[][\w\s(,)[:\]]+\s+\**${1}\s*\([\w\s(,)[\]*]+?(...)?\)[\w\s(,)[:\]]*;" \
> +	|sed -E 's/^[^:]+:[0-9]+:/&\n/';
> +}
> 


-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v9] scripts/bash_aliases: Add useful functions
  2021-02-19 14:32 ` [PATCH v9] " Alejandro Colomar
  2021-02-20 14:35   ` Michael Kerrisk (man-pages)
  2021-02-20 21:45   ` Michael Kerrisk (man-pages)
@ 2021-02-27 15:09   ` Stefan Puiu
  2021-02-27 17:16     ` Alejandro Colomar (man-pages)
  2 siblings, 1 reply; 18+ messages in thread
From: Stefan Puiu @ 2021-02-27 15:09 UTC (permalink / raw)
  To: Alejandro Colomar; +Cc: Michael Kerrisk, lnx-man, Walter Harms

Hi Alejandro,

Sorry for jumping in this discussion so late, but I was wondering
about one thing (see below).

On Fri, Feb 19, 2021 at 4:38 PM Alejandro Colomar
<alx.manpages@gmail.com> wrote:
>
> That file should be sourced (.) from 'bashrc' (or 'bash_aliases').
>
> It contains functions that are useful for the maintenance of this
> project.
>
> - grep_syscall()
> - grep_syscall_def()
> - man_section()
> - man_lsfunc()
> - pdfman()
> - grep_glibc_prototype()
>
> Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
> ---
>
> v2:
>         - Reword license to adequate it for many functions instead
>           of a single program.
>         - Add a trailing ';' for consistency.
> v3:
>         - Resend to avoid confusion:  I forgot to use [PATCH v2]!
> v4:
>         - grep_glibc_prototype(): Fix some corner cases where
>           a function invocation was printed.
> v5:
>         - grep_glibc_prototype(): Fix some corner cases where
>           a function prototype with many attributes wasn't printed.
> v6:
>         - man_lsfunc(): Allow listing more than one dir.
>           Example: $ man_lsfunc man[23];
> v7:
>         - grep_glibc_prototype(): Fix some corner cases where
>           a function invocation was printed.
> v8:
>         - grep_glibc_prototype():
>                 - Print attributes before the prototype too.
>                   (AFAICS, there are none; but just in case...)
>                 - Accept C2x attributes (glibc doesn't use them yet,
>                   but it doesn't hurt to accept them).
> v9:
>         - grep_glibc_prototype(): Accept namespaced C2x attributes,
>           such as [[gnu::nonnull]].
>
> ---
>  scripts/bash_aliases | 171 +++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 171 insertions(+)
>  create mode 100644 scripts/bash_aliases
>
> diff --git a/scripts/bash_aliases b/scripts/bash_aliases
> new file mode 100644
> index 000000000..5e4b424af
> --- /dev/null
> +++ b/scripts/bash_aliases
> @@ -0,0 +1,171 @@
> +# SPDX-License-Identifier: GPL-2.0-only
> +########################################################################
> +#
> +# (C) Copyright 2021, Alejandro Colomar
> +# These functions are free software; you can redistribute them and/or
> +# modify them under the terms of the GNU General Public License
> +# as published by the Free Software Foundation; version 2.
> +#
> +# These functions are distributed in the hope that they will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details
> +# (http://www.gnu.org/licenses/gpl-2.0.html).
> +#
> +########################################################################
> +
> +########################################################################
> +#      Exit status
> +
> +EX_OK=0;
> +EX_USAGE=64;
> +
> +########################################################################
> +#      Linux kernel
> +
> +#  grep_syscall()  finds the prototype of a syscall in the kernel sources,
> +# printing the filename, line number, and the prototype.
> +# It should be run from the root of the linux kernel source tree.
> +# Usage example:  .../linux$ grep_syscall openat2;
> +
> +function grep_syscall()
> +{
> +       if ! [ -v 1 ]; then
> +               >&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
> +               return ${EX_USAGE};
> +       fi
> +
> +       find * -type f \
> +       |grep '\.c$' \
> +       |sort -V \
> +       |xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
> +       |sed -E 's/^[^:]+:[0-9]+:/&\n/';
> +
> +       find * -type f \
> +       |grep '\.[ch]$' \

Any reason not to use "find . -type f -name '*.[ch]'" when you need to
restrict the files you're looking at? I would expect that to be
faster. Also, not sure what you are trying to do with 'sort -V' - why
not feed the results directly to pcregrep?

> +       |sort -V \
> +       |xargs pcregrep -Mn "(?s)^asmlinkage\s+[\w\s]+\**sys_${1}\s*\(.*?\)" \
> +       |sed -E 's/^[^:]+:[0-9]+:/&\n/';
> +}
> +
> +#  grep_syscall_def()  finds the definition of a syscall in the kernel sources,
> +# printing the filename, line number, and the function definition.
> +# It should be run from the root of the linux kernel source tree.
> +# Usage example:  .../linux$ grep_syscall_def openat2;
> +
> +function grep_syscall_def()
> +{
> +       if ! [ -v 1 ]; then
> +               >&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
> +               return ${EX_USAGE};
> +       fi
> +
> +       find * -type f \
> +       |grep '\.c$' \
> +       |sort -V \
> +       |xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?^}" \
> +       |sed -E 's/^[^:]+:[0-9]+:/&\n/';
> +}
> +
> +########################################################################
> +#      Linux man-pages
> +
> +#  man_section()  prints a specific manual page section (DESCRIPTION, SYNOPSIS,
> +# ...) of all manual pages in a directory (or in a single manual page file).
> +# Usage example:  .../man-pages$ man_section man2 SYNOPSIS;
> +
> +function man_section()
> +{
> +       if ! [ -v 2 ]; then
> +               >&2 echo "Usage: ${FUNCNAME[0]} <dir> <section>";
> +               return ${EX_USAGE};
> +       fi
> +
> +       find "${1}" -type f \
> +       |xargs grep -l "\.SH ${2}" \
> +       |sort -V \
> +       |while read -r manpage; do
> +               <${manpage} \
> +               sed -n \
> +                       -e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
> +                       -e "/^\.SH ${2}/p" \
> +                       -e "/^\.SH ${2}/,/^\.SH/{/^\.SH/!p}" \
> +               |man -P cat -l - 2>/dev/null;
> +       done;
> +}
> +
> +#  man_lsfunc()  prints the name of all C functions declared in the SYNOPSIS
> +# of all manual pages in a directory (or in a single manual page file).
> +# Each name is printed in a separate line
> +# Usage example:  .../man-pages$ man_lsfunc man2;
> +
> +function man_lsfunc()
> +{
> +       if ! [ -v 1 ]; then
> +               >&2 echo "Usage: ${FUNCNAME[0]} <dir>";
> +               return ${EX_USAGE};
> +       fi
> +
> +       find "${@}" -type f \
> +       |xargs grep -l "\.SH SYNOPSIS" \
> +       |sort -V \
> +       |while read -r manpage; do
> +               <${manpage} \
> +               sed -n \
> +                       -e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
> +                       -e "/^\.SH SYNOPSIS/p" \
> +                       -e "/^\.SH SYNOPSIS/,/^\.SH/{/^\.SH/!p}" \
> +               |sed \
> +                       -e '/Feature/,$d' \
> +                       -e '/{/,/}/d' \
> +               |man -P cat -l - 2>/dev/null;
> +       done \
> +       |sed -n "/^SYNOPSIS/,/^\w/p" \
> +       |grep '^       \w' \
> +       |grep -v ':' \
> +       |sed 's/^[^(]* \**\(\w*\)(.*/\1/' \
> +       |grep '^\w' \
> +       |uniq;
> +}
> +
> +#  pdfman()  reanders a manual page in PDF
> +# Usage example:  .../man-pages$ pdfman man2/membarrier.2;
> +
> +function pdfman()
> +{
> +       if ! [ -v 1 ]; then
> +               >&2 echo "Usage: ${FUNCNAME[0]} <man-page.n>";
> +               return ${EX_USAGE};
> +       fi;
> +
> +       local tmp="$(mktemp -t "${1##*/}.XXXXXX")";
> +
> +       <${1} \
> +       man -Tps -l - \
> +       |ps2pdf - - \
> +       >${tmp};
> +       xdg-open ${tmp};
> +}
> +
> +########################################################################
> +#      Glibc
> +
> +#  grep_glibc_prototype()  finds a function prototype in the glibc sources,
> +# printing the filename, line number, and the prototype.
> +# It should be run from the root of the glibc source tree.
> +# Usage example:  .../glibc$ grep_glibc_prototype printf;
> +
> +function grep_glibc_prototype()
> +{
> +       if ! [ -v 1 ]; then
> +               >&2 echo "Usage: ${FUNCNAME[0]} <func>";
> +               return ${EX_USAGE};
> +       fi
> +
> +       find * -type f \
> +       |grep '\.h$' \
> +       |sort -V \
> +       |xargs pcregrep -Mn \
> +         "(?s)^[\w[][\w\s(,)[:\]]+\s+\**${1}\s*\([\w\s(,)[\]*]+?(...)?\)[\w\s(,)[:\]]*;" \
> +       |sed -E 's/^[^:]+:[0-9]+:/&\n/';
> +}
> --
> 2.30.1.721.g45526154a5
>

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v9] scripts/bash_aliases: Add useful functions
  2021-02-27 15:09   ` Stefan Puiu
@ 2021-02-27 17:16     ` Alejandro Colomar (man-pages)
  2021-03-01 10:19       ` Stefan Puiu
  0 siblings, 1 reply; 18+ messages in thread
From: Alejandro Colomar (man-pages) @ 2021-02-27 17:16 UTC (permalink / raw)
  To: Stefan Puiu; +Cc: Michael Kerrisk, lnx-man, Walter Harms

Hi Stefan,

On 2/27/21 4:09 PM, Stefan Puiu wrote:
> Sorry for jumping in this discussion so late, but I was wondering
> about one thing (see below).
> 
> On Fri, Feb 19, 2021 at 4:38 PM Alejandro Colomar
> <alx.manpages@gmail.com> wrote:
>> +#  grep_syscall()  finds the prototype of a syscall in the kernel sources,
>> +# printing the filename, line number, and the prototype.
>> +# It should be run from the root of the linux kernel source tree.
>> +# Usage example:  .../linux$ grep_syscall openat2;
>> +
>> +function grep_syscall()
>> +{
>> +       if ! [ -v 1 ]; then
>> +               >&2 echo "Usage: ${FUNCNAME[0]} <syscall>";
>> +               return ${EX_USAGE};
>> +       fi
>> +
>> +       find * -type f \
>> +       |grep '\.c$' \
>> +       |sort -V \
>> +       |xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
>> +       |sed -E 's/^[^:]+:[0-9]+:/&\n/';
>> +
>> +       find * -type f \
>> +       |grep '\.[ch]$' \
> 
> Any reason not to use "find . -type f -name '*.[ch]'" when you need to
> restrict the files you're looking at? I would expect that to be
> faster.

I don't like find syntax.  I never remember how all of its options work.
grep is much simpler, and everyone knows how it works.

find has: -[i]lname, -[i]name, -[i]path, -[i]regex, -[i]wholename
I don't want to be reading the manual for all of them each time I use 
find.  grep does the same with optional -i and some simple regex which 
anyone could understand with some basic regex knowledge.

For the performance part, I don't know; but we might be surprised.  At 
most it might be a bit faster (nothing like 200%), but I care more about 
readability.

I also avoid using find -exec option, and instead use xargs.  It's way 
simpler to understand, at least for me.

See also:
<http://doc.cat-v.org/unix/find-history>
<http://harmful.cat-v.org/cat-v/>

> Also, not sure what you are trying to do with 'sort -V' - why
> not feed the results directly to pcregrep?

Some consistency.  Sometimes this function finds more than one prototype 
for a syscall.  I prefer that everyone using this function gets the 
results in the same predictable order, instead of some random order.

> 
>> +       |sort -V \
>> +       |xargs pcregrep -Mn "(?s)^asmlinkage\s+[\w\s]+\**sys_${1}\s*\(.*?\)" \
>> +       |sed -E 's/^[^:]+:[0-9]+:/&\n/';
>> +}

Regards,

Alex

-- 
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v9] scripts/bash_aliases: Add useful functions
  2021-02-27 17:16     ` Alejandro Colomar (man-pages)
@ 2021-03-01 10:19       ` Stefan Puiu
  2021-03-01 14:16         ` Alejandro Colomar (man-pages)
  0 siblings, 1 reply; 18+ messages in thread
From: Stefan Puiu @ 2021-03-01 10:19 UTC (permalink / raw)
  To: Alejandro Colomar (man-pages); +Cc: Michael Kerrisk, lnx-man, Walter Harms

Hi Alejandro,

On Sat, Feb 27, 2021 at 7:16 PM Alejandro Colomar (man-pages)
<alx.manpages@gmail.com> wrote:
[...snip]
> >> +       find * -type f \
> >> +       |grep '\.c$' \
> >> +       |sort -V \
> >> +       |xargs pcregrep -Mn "(?s)^\w*SYSCALL_DEFINE.\(${1},.*?\)" \
> >> +       |sed -E 's/^[^:]+:[0-9]+:/&\n/';
> >> +
> >> +       find * -type f \
> >> +       |grep '\.[ch]$' \
> >
> > Any reason not to use "find . -type f -name '*.[ch]'" when you need to
> > restrict the files you're looking at? I would expect that to be
> > faster.
>
> I don't like find syntax.  I never remember how all of its options work.
> grep is much simpler, and everyone knows how it works.
>
> find has: -[i]lname, -[i]name, -[i]path, -[i]regex, -[i]wholename
> I don't want to be reading the manual for all of them each time I use
> find.  grep does the same with optional -i and some simple regex which
> anyone could understand with some basic regex knowledge.

I've always used find -name, I think most of the time it's enough. But
I can understand that people might prefer writing certain snippets in
a certain way, and you need to be comfortable with scripts you are
maintaining.

>
> For the performance part, I don't know; but we might be surprised.  At
> most it might be a bit faster (nothing like 200%), but I care more about
> readability.

Actually, as far as I can tell there's not much difference
performance-wise between the two, as far as I can tell. At least when
searching the kernel source on my Linux VM. So it seems I'm wrong on
that point:

stefan@spuiu-vm:~/rpmbuild/BUILD/kernel-3.10.0-1160.2.2.el7/linux-3.10.0-1160.2.2.el7.x86_64$
time ( find . | grep '\.c' &>/dev/null )

real    0m0.076s
user    0m0.031s
sys     0m0.046s
stefan@spuiu-vm:~/rpmbuild/BUILD/kernel-3.10.0-1160.2.2.el7/linux-3.10.0-1160.2.2.el7.x86_64$
time ( find . -name  '*.c' &>/dev/null )

real    0m0.088s
user    0m0.016s
sys     0m0.066s

>
> I also avoid using find -exec option, and instead use xargs.  It's way
> simpler to understand, at least for me.
>
> See also:
> <http://doc.cat-v.org/unix/find-history>
> <http://harmful.cat-v.org/cat-v/>

Well, I understand the sentiment in those texts, but I would argue
that finding files by name is a core functionality of find :). It's
true that other extra functionality might not be exactly warranted,
and yes, '-print' feels kind of weird.

Thanks for bearing with me,
Stefan.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v9] scripts/bash_aliases: Add useful functions
  2021-03-01 10:19       ` Stefan Puiu
@ 2021-03-01 14:16         ` Alejandro Colomar (man-pages)
  0 siblings, 0 replies; 18+ messages in thread
From: Alejandro Colomar (man-pages) @ 2021-03-01 14:16 UTC (permalink / raw)
  To: Stefan Puiu; +Cc: Michael Kerrisk, lnx-man, Walter Harms



On 3/1/21 11:19 AM, Stefan Puiu wrote:
> On Sat, Feb 27, 2021 at 7:16 PM Alejandro Colomar (man-pages)

> I've always used find -name, I think most of the time it's enough. But
> I can understand that people might prefer writing certain snippets in
> a certain way, and you need to be comfortable with scripts you are
> maintaining.

Yes
> 
> Actually, as far as I can tell there's not much difference
> performance-wise between the two, as far as I can tell. At least when
> searching the kernel source on my Linux VM. So it seems I'm wrong on
> that point:
> 
> stefan@spuiu-vm:~/rpmbuild/BUILD/kernel-3.10.0-1160.2.2.el7/linux-3.10.0-1160.2.2.el7.x86_64$
> time ( find . | grep '\.c' &>/dev/null )
> 
> real    0m0.076s
> user    0m0.031s
> sys     0m0.046s
> stefan@spuiu-vm:~/rpmbuild/BUILD/kernel-3.10.0-1160.2.2.el7/linux-3.10.0-1160.2.2.el7.x86_64$
> time ( find . -name  '*.c' &>/dev/null )
> 
> real    0m0.088s
> user    0m0.016s
> sys     0m0.066s

It seems that in real time, piping to grep is even faster than using 
find options :-)

Nevertheless, pcregrep is the bottleneck in these functions.  So I guess 
it would make absolutely no difference.


> 
> Well, I understand the sentiment in those texts, but I would argue
> that finding files by name is a core functionality of find :).

Yes, it is.  However, I think the following applies to grep and find:

"The use of cat to feed a single input file to a program has to some 
degree superseded the shell’s < operator, which illustrates that 
general-purpose constructs --\x14like cat and pipes\x14-- are often more 
natural than convenient special-purpose ones."  [Program design in the 
UNIX(TM) environment]

At least to me, knowing the more general-purpose grep, is easier than 
learning the special purpose find -name :-)

However, the filename options to find may be necessary in some specific 
cases, so I'm not blaming them, only their syntax.  And as long as my 
use cases are simple enough to work with grep, I'll keep using it.

> It's
> true that other extra functionality might not be exactly warranted,
> and yes, '-print' feels kind of weird.
> 
> Thanks for bearing with me,
> Stefan.
> 

You're welcome!  Thanks for commenting on my scripts! :-}

Cheers,

Alex

-- 
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2021-03-01 14:17 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-15 13:15 [PATCH] scripts/bash_aliases: Add useful functions Alejandro Colomar
2021-02-15 13:36 ` Alejandro Colomar (man-pages)
2021-02-15 20:31 ` Alejandro Colomar
2021-02-15 20:32 ` [PATCH v3] " Alejandro Colomar
2021-02-16 10:54   ` AW: " Walter Harms
2021-02-16 11:00     ` Alejandro Colomar (man-pages)
2021-02-18 18:47 ` [PATCH v4] " Alejandro Colomar
2021-02-18 19:33 ` [PATCH v5] " Alejandro Colomar
2021-02-18 21:17 ` [PATCH v6] " Alejandro Colomar
2021-02-19 14:13 ` [PATCH v7] " Alejandro Colomar
2021-02-19 14:22 ` [PATCH v8] " Alejandro Colomar
2021-02-19 14:32 ` [PATCH v9] " Alejandro Colomar
2021-02-20 14:35   ` Michael Kerrisk (man-pages)
2021-02-20 21:45   ` Michael Kerrisk (man-pages)
2021-02-27 15:09   ` Stefan Puiu
2021-02-27 17:16     ` Alejandro Colomar (man-pages)
2021-03-01 10:19       ` Stefan Puiu
2021-03-01 14:16         ` Alejandro Colomar (man-pages)

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.