* [COGITO PATCH] Optimized print_help()
@ 2005-06-14 23:26 Pavel Roskin
2005-06-15 0:00 ` Jonas Fonseca
0 siblings, 1 reply; 5+ messages in thread
From: Pavel Roskin @ 2005-06-14 23:26 UTC (permalink / raw)
To: git
Hello!
print_help() in cg-Xlib should not be calling "which" 3 times. It's an
external command. The patch caches the result in a local variable.
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'
+ cat $toolpath | sed -n '3,/^$/s/^# *//p'
exit
}
--
Regards,
Pavel Roskin
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [COGITO PATCH] Optimized print_help()
2005-06-14 23:26 [COGITO PATCH] Optimized print_help() Pavel Roskin
@ 2005-06-15 0:00 ` Jonas Fonseca
2005-06-15 0:33 ` Pavel Roskin
0 siblings, 1 reply; 5+ messages in thread
From: Jonas Fonseca @ 2005-06-15 0:00 UTC (permalink / raw)
To: Pavel Roskin; +Cc: git
Pavel Roskin <proski@gnu.org> wrote Tue, Jun 14, 2005:
> Hello!
>
> print_help() in cg-Xlib should not be calling "which" 3 times. It's an
> external command. The patch caches the result in a local variable.
>
> 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
Shouldn't $toolpath be quoted here to make it safer? Although it seems
the make install don't handle irregular toolpaths at the moment.
> echo
> - cat $(which cg-$1) | sed -n '3,/^$/s/^# *//p'
> + cat $toolpath | sed -n '3,/^$/s/^# *//p'
What about also removing UUOC from this line ...
sed -n '3,/^$/s/^# *//p' < "$toolpath"
> exit
> }
--
Jonas Fonseca
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [COGITO PATCH] Optimized print_help()
2005-06-15 0:00 ` Jonas Fonseca
@ 2005-06-15 0:33 ` Pavel Roskin
2005-06-15 18:58 ` Rene Scharfe
0 siblings, 1 reply; 5+ messages in thread
From: Pavel Roskin @ 2005-06-15 0:33 UTC (permalink / raw)
To: Jonas Fonseca; +Cc: git
On Wed, 2005-06-15 at 02:00 +0200, Jonas Fonseca wrote:
> Shouldn't $toolpath be quoted here to make it safer? Although it seems
> the make install don't handle irregular toolpaths at the moment.
Sure.
> > + cat $toolpath | sed -n '3,/^$/s/^# *//p'
>
> What about also removing UUOC from this line ...
UUOC? I had too look it up :-)
It's not just "useless use of cat", but also useless use of redirection
in both cases. I guess sed can exit after it finishes processing the
range (I don't know if it actually does), so feeding it the whole file
is not need.
OK, here's an improved 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
}
P.S. Maybe we could use "type" rather than "which" because the former is
a shell built-in. Or maybe we should hardcode the prefix on install.
Anyway, it's out of scope of this patch.
P.P.S. I hope that $toolpath doesn't start with "-". I could add "--"
to the sed command line, but I'm not sure if BSD sed would like it.
--
Regards,
Pavel Roskin
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [COGITO PATCH] Optimized print_help()
2005-06-15 0:33 ` Pavel Roskin
@ 2005-06-15 18:58 ` Rene Scharfe
2005-06-15 21:14 ` Pavel Roskin
0 siblings, 1 reply; 5+ messages in thread
From: Rene Scharfe @ 2005-06-15 18:58 UTC (permalink / raw)
To: Pavel Roskin; +Cc: git
Pavel Roskin schrieb:
>>> + cat $toolpath | sed -n '3,/^$/s/^# *//p'
>>
>> What about also removing UUOC from this line ...
>
>
> UUOC? I had too look it up :-)
>
> It's not just "useless use of cat", but also useless use of
> redirection in both cases. I guess sed can exit after it finishes
> processing the range (I don't know if it actually does), so feeding
> it the whole file is not need.
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. So this call is equivalent and has the advantage of being
resistant against filenames starting with a "-":
sed -n '3,/^$/s/^# *//p' <"$toolpath"
Rene
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [COGITO PATCH] Optimized print_help()
2005-06-15 18:58 ` Rene Scharfe
@ 2005-06-15 21:14 ` Pavel Roskin
0 siblings, 0 replies; 5+ messages in thread
From: Pavel Roskin @ 2005-06-15 21:14 UTC (permalink / raw)
To: Rene Scharfe; +Cc: git
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
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2005-06-15 21:10 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-06-14 23:26 [COGITO PATCH] Optimized print_help() Pavel Roskin
2005-06-15 0:00 ` Jonas Fonseca
2005-06-15 0:33 ` Pavel Roskin
2005-06-15 18:58 ` Rene Scharfe
2005-06-15 21:14 ` Pavel Roskin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).