public inbox for linux-man@vger.kernel.org
 help / color / mirror / Atom feed
From: Oskari Pirhonen <xxc3ncoredxx@gmail.com>
To: Alejandro Colomar <alx@kernel.org>
Cc: linux-man@vger.kernel.org, Matt Jolly <Matt.Jolly@footclan.ninja>,
	Brian Inglis <Brian.Inglis@Shaw.ca>,
	Guillem Jover <guillem@hadrons.org>,
	Tom Schwindl <schwindl@posteo.de>, Sam James <sam@gentoo.org>
Subject: [PATCH] stdc: some improvements
Date: Thu, 23 Mar 2023 23:35:19 -0500	[thread overview]
Message-ID: <ZB0oh8/A5Ll/0SY0@dj3ntoo> (raw)

- Some small stylistic changes such as removing trailing semicolons and
  noisy `shift` calls.
- Ensure pcregrep exists. It is installed as pcre2grep on my machine, so
  check for both and error out if neither is found.
- Make libc_summ() standard-agnostics by passing in the filter
  expression and the path to the doc as arguments.
- Make libc_summ() error out if the doc is not found.
- Give basic usage information on usage errors.
- Make the standard version match case insensitive.

Signed-off-by: Oskari Pirhonen <xxc3ncoredxx@gmail.com>
---
 bin/stdc | 101 +++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 68 insertions(+), 33 deletions(-)

diff --git a/bin/stdc b/bin/stdc
index 8c725a2..375be76 100755
--- a/bin/stdc
+++ b/bin/stdc
@@ -1,65 +1,100 @@
-#!/bin/bash
+#!/usr/bin/env bash
 
-set -Eefuo pipefail;
+set -Efuo pipefail
 
-prefix="/usr/local";
-datarootdir="$prefix/share";
-docdir="$datarootdir/doc";
+prefix="/usr/local"
+datarootdir="$prefix/share"
+docdir="$datarootdir/doc"
+
+c89_filter='/A.3 LIBRARY SUMMARY/,$p'
+c89_doc="$docdir/c/c89/c89-draft.txt"
+c99_filter='/Library summary$/,/Sequence points$/p'
+c99_doc="$docdir/c/c99/n1256.txt"
+c11_filter='/Library summary$/,/Sequence points$/p'
+c11_doc="$docdir/c/c11/n1570.txt"
+
+pcregrep="$(type -P pcregrep)"
+if [[ -z "$pcregrep" ]]; then
+	pcregrep="$(type -P pcre2grep)"
+fi
 
 err()
 {
-	>&2 echo "$(basename "$0"): error: $*";
-	exit 1;
+	>&2 echo "$*"
 }
 
-grep_proto()
+die()
+{
+	err "$(basename "$0"): error: $*"
+	exit 1
+}
+
+# Args:
+# 	1: usage error
+usage()
 {
-	pcregrep -M "(?s)\b$1 *\([[:alnum:]*,._\s\(\)-]*\);$";
+	err "$*"
+	err
+	err "usage: $(basename "$0") <version> <function>"
+	err
+	err "    version    ISO C version. Supported versions:"
+	err "               C89, C99, C11"
+	err "               (case insensitive)"
+	err "    function   Function to look for"
+	exit 1
 }
 
-libc_summ_c89()
+# Args:
+# 	1: declaration to look for
+grep_proto()
 {
-	sed -n '/A.3 LIBRARY SUMMARY/,$p' <"$docdir/c/c89/c89-draft.txt";
+	"$pcregrep" -M "(?s)\b$1 *\([[:alnum:]*,._\s\(\)-]*\);\$"
 }
 
+# Args:
+# 	1: filter expression
+# 	2: path to plaintext standard
 libc_summ()
 {
-	sed -n '/Library summary$/,/Sequence points$/p';
+	if [[ ! -r "$2" ]]; then
+		die "cannot find or read '$2'"
+	fi
+	sed -n -e "$1" "$2"
 }
 
+if [[ -z "$pcregrep" ]]; then
+	die "pcregrep or pcre2grep required but is not installed"
+fi
+
 case $# in
-0)
-	err "missing ISO C version.";
-	;;
-1)
-	err "missing function name.";
+0|1)
+	usage "missing ISO C version and/or function name."
 	;;
 2)
 	;;
 *)
-	shift;
-	shift;
-	err "unsupported extra argument(s): $*";
+	shift
+	shift
+	usage "unsupported extra argument(s): ${*@Q}"
 	;;
-esac;
+esac
 
-case "$1" in
+case "${1@L}" in
 c89)
-	shift;
-	libc_summ_c89 \
-	| grep_proto $@;
+	libc_summ "$c89_filter" "$c89_doc" \
+	| grep_proto "$2"
 	;;
 c99)
-	shift;
-	libc_summ <"$docdir/c/c99/n1256.txt" \
-	| grep_proto $@;
+	libc_summ "$c99_filter" "$c99_doc" \
+	| grep_proto "$2"
 	;;
 c11)
-	shift;
-	libc_summ <"$docdir/c/c11/n1570.txt" \
-	| grep_proto $@;
+	libc_summ "$c11_filter" "$c11_doc" \
+	| grep_proto "$2"
 	;;
 *)
-	err "$1: unsupported ISO C version.";
+	usage "$1: unsupported ISO C version."
 	;;
-esac;
+esac
+
+# vim: set noexpandtab:
-- 
2.39.2


                 reply	other threads:[~2023-03-24  4:35 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZB0oh8/A5Ll/0SY0@dj3ntoo \
    --to=xxc3ncoredxx@gmail.com \
    --cc=Brian.Inglis@Shaw.ca \
    --cc=Matt.Jolly@footclan.ninja \
    --cc=alx@kernel.org \
    --cc=guillem@hadrons.org \
    --cc=linux-man@vger.kernel.org \
    --cc=sam@gentoo.org \
    --cc=schwindl@posteo.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox