git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [COGITO PATCH] Optimized print_help()

From
PRPavel Roskin <proski@gnu.org>
Date
Jun 15, 2005, 21:14 UTC
Message-ID
<1118870099.21882.16.camel@dv>
In-Reply-To
<42B07A6F.3000001@lsrfire.ath.cx>
Hello, Rene!
On Wed, 2005-06-15 at 20:58 +0200, Rene Scharfe wrote:
> It doesn't matter if the shell opens the file or if sed does it itself,
> sed's ability to close the file and quit when done doesn't depend on
> that.
I checked sed 4.1.4 and found following:
1) sed won't close stdin ever, but it will close files specified on the
command line.
2) sed will exit when told to do so by a command such as "q", and it
won't read the reminder of the file, whether it's stdin or not.
3) sed has code that makes it exit after it has processed all address
ranges and the "-n" option is used.  However, this feature is buggy and
disabled by default (see EXPERIMENTAL_DASH_N_OPTIMIZATION). It may be
enabled in the future versions.

In other words, you are correct (except the part about closing the file).

>   So this call is equivalent and has the advantage of being
> resistant against filenames starting with a "-":
> 
>    sed -n '3,/^$/s/^# *//p' <"$toolpath"
OK.  Here's the third version of the patch.
Signed-off-by: Pavel Roskin <proski@gnu.org>
diff --git a/cg-Xlib b/cg-Xlib
--- a/cg-Xlib
+++ b/cg-Xlib
@@ -130,10 +130,11 @@ update_index () {
 
 
 print_help () {
-	which "cg-$1" >/dev/null 2>&1 || exit 1
-	sed -n '/^USAGE=/,0s/.*"\(.*\)"/Usage: \1/p' < $(which cg-$1) 
+	local toolpath=$(which cg-$1 2>/dev/null)
+	[ -z "$toolpath" ] && exit 1
+	sed -n '/^USAGE=/,0s/.*"\(.*\)"/Usage: \1/p' <"$toolpath"
 	echo
-	cat $(which cg-$1) | sed -n '3,/^$/s/^# *//p'
+	sed -n '3,/^$/s/^# *//p' <"$toolpath"
 	exit
 }
 
-- 
Regards,
Pavel Roskin
Previous: Rene Scharfe
Message 5 of 5 in “Optimized print_help()”
  1. Optimized print_help()Pavel Roskin, Jun 14, 2005
  2. Jonas FonsecaJun 15, 2005
  3. Pavel RoskinJun 15, 2005
  4. Rene ScharfeJun 15, 2005
  5. Pavel RoskinJun 15, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.