9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] [PATCH] feat(grep): add -H flag
@ 2024-06-26  2:24 shurizzle
  2024-06-26  4:56 ` Alex Musolino
  0 siblings, 1 reply; 8+ messages in thread
From: shurizzle @ 2024-06-26  2:24 UTC (permalink / raw)
  To: 9front


-H is the opposite of -h.
When present, print the file name even if only one file is
passed in the parameters.
---
diff a71a76745b5fd2f3e50778268bc2cad262748416 db274004a40a00a08021473b568912c41d8a0cbb
--- a/sys/man/1/grep
+++ b/sys/man/1/grep
@@ -49,6 +49,9 @@
 .B -h
 Do not print file name tags (headers) with output lines.
 .TP
+.B -H
+Print file name tags (headers) with output lines, even for a single file.
+.TP
 .B -e
 The following argument is taken as a
 .IR pattern .
--- a/sys/src/cmd/grep/main.c
+++ b/sys/src/cmd/grep/main.c
@@ -1,7 +1,7 @@
 #define	EXTERN
 #include	"grep.h"

-char *validflags = "bchiLlnsv";
+char *validflags = "bchHiLlnsv";
 void
 usage(void)
 {
@@ -12,8 +12,9 @@
 void
 main(int argc, char *argv[])
 {
-	int i, status;
+	int i, h, status;

+	h = 0;
 	ARGBEGIN {
 	default:
 		if(utfrune(validflags, ARGC()) == nil)
@@ -38,6 +39,12 @@
 		lineno = 1;
 		str2top(filename);
 		break;
+	case 'h':
+		h = -1;
+		break;
+	case 'H':
+		h = 1;
+		break;
 	} ARGEND

 	if(flags['f'] == 0 && flags['e'] == 0) {
@@ -57,12 +64,12 @@
 		status = search(0, 0);
 		break;
 	case 1:
-		status = search(argv[0], 0);
+		status = search(argv[0], h > 0 ? Hflag : 0);
 		break;
 	default:
 		status = 0;
 		for(i=0; i<argc; i++)
-			status |= search(argv[i], Hflag);
+			status |= search(argv[i], h < 0 ? 0 : Hflag);
 		break;
 	}
 	if(status)
@@ -94,8 +101,6 @@
 		flag |= Bflag;		/* dont buffer output */
 	if(flags['c'])
 		flag |= Cflag;		/* count */
-	if(flags['h'])
-		flag &= ~Hflag;		/* do not print file name in output */
 	if(flags['i'])
 		flag |= Iflag;		/* fold upper-lower */
 	if(flags['l'])


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

* Re: [9front] [PATCH] feat(grep): add -H flag
  2024-06-26  2:24 [9front] [PATCH] feat(grep): add -H flag shurizzle
@ 2024-06-26  4:56 ` Alex Musolino
  2024-06-26 11:18   ` me
  0 siblings, 1 reply; 8+ messages in thread
From: Alex Musolino @ 2024-06-26  4:56 UTC (permalink / raw)
  To: 9front

> -H is the opposite of -h.
> When present, print the file name even if only one file is
> passed in the parameters.

Seems unnecessary.  You can already get this behaviour by passing
/dev/null as an extra file argument.

--
Cheers,
Alex Musolino


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

* Re: [9front] [PATCH] feat(grep): add -H flag
  2024-06-26  4:56 ` Alex Musolino
@ 2024-06-26 11:18   ` me
  2024-06-26 13:49     ` Alex Musolino
  2024-06-26 16:31     ` Lyndon Nerenberg (VE7TFX/VE6BBM)
  0 siblings, 2 replies; 8+ messages in thread
From: me @ 2024-06-26 11:18 UTC (permalink / raw)
  To: 9front

On 24/06/26 02:26PM, Alex Musolino wrote:
> > -H is the opposite of -h.
> > When present, print the file name even if only one file is
> > passed in the parameters.
>
> Seems unnecessary.  You can already get this behaviour by passing
> /dev/null as an extra file argument.
>
> --
> Cheers,
> Alex Musolino
>

That's exactly what I do now, but a flag gives me more confidence than a hack.
Additionally, if combined with xargs, it can split the arguments leaving the
last one alone, and in `g` I have no way to pass an extra argument, but I can
pass a flag (`g -Hn w/e`). I hope I made myself clear.


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

* Re: [9front] [PATCH] feat(grep): add -H flag
  2024-06-26 11:18   ` me
@ 2024-06-26 13:49     ` Alex Musolino
  2024-06-26 14:08       ` me
  2024-06-26 16:31     ` Lyndon Nerenberg (VE7TFX/VE6BBM)
  1 sibling, 1 reply; 8+ messages in thread
From: Alex Musolino @ 2024-06-26 13:49 UTC (permalink / raw)
  To: 9front

> That's exactly what I do now, but a flag gives me more confidence than a hack.
> Additionally, if combined with xargs, it can split the arguments leaving the
> last one alone, and in `g` I have no way to pass an extra argument, but I can
> pass a flag (`g -Hn w/e`). I hope I made myself clear.
> 

Not really.  It's not "a hack".  It relies on the documented behaviour
of grep(1) and /dev/null so I don't know why you are lacking
confidence.  I also don't know what you're getting at with xargs(1).
Lastly, g(1) already does this exact thing for you; no need to pass an
extra argument.

--
Cheers,
Alex Musolino


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

* Re: [9front] [PATCH] feat(grep): add -H flag
  2024-06-26 13:49     ` Alex Musolino
@ 2024-06-26 14:08       ` me
  0 siblings, 0 replies; 8+ messages in thread
From: me @ 2024-06-26 14:08 UTC (permalink / raw)
  To: 9front

On 24/06/26 11:19, Alex Musolino wrote:
> Not really.  It's not "a hack".  It relies on the documented behaviour
> of grep(1) and /dev/null so I don't know why you are lacking
> confidence.  I also don't know what you're getting at with xargs(1).
> Lastly, g(1) already does this exact thing for you; no need to pass an
> extra argument.

Never mind the reasoning about xargs(1), if no one is interested in the flag,
it means I'll continue adding /dev/null to my grep.
Thank you for the time spent.


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

* Re: [9front] [PATCH] feat(grep): add -H flag
  2024-06-26 11:18   ` me
  2024-06-26 13:49     ` Alex Musolino
@ 2024-06-26 16:31     ` Lyndon Nerenberg (VE7TFX/VE6BBM)
  2024-06-26 16:35       ` Lyndon Nerenberg (VE7TFX/VE6BBM)
  1 sibling, 1 reply; 8+ messages in thread
From: Lyndon Nerenberg (VE7TFX/VE6BBM) @ 2024-06-26 16:31 UTC (permalink / raw)
  To: 9front, me

me@shurizzle.dev writes:

> That's exactly what I do now, but a flag gives me more confidence
> than a hack.

It's not a 'hack'.  This is idiomatic usage going back to when
grep grew a -l flag.

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

* Re: [9front] [PATCH] feat(grep): add -H flag
  2024-06-26 16:31     ` Lyndon Nerenberg (VE7TFX/VE6BBM)
@ 2024-06-26 16:35       ` Lyndon Nerenberg (VE7TFX/VE6BBM)
  2024-06-27 20:11         ` Romano
  0 siblings, 1 reply; 8+ messages in thread
From: Lyndon Nerenberg (VE7TFX/VE6BBM) @ 2024-06-26 16:35 UTC (permalink / raw)
  To: 9front

Lyndon Nerenberg (VE7TFX/VE6BBM) writes:

> It's not a 'hack'.  This is idiomatic usage going back to when
> grep grew a -l flag.

Ignore me, I'm still on my first coffee :-(.  -l is irrelevant,
the behaviour has been there since day one.

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

* Re: [9front] [PATCH] feat(grep): add -H flag
  2024-06-26 16:35       ` Lyndon Nerenberg (VE7TFX/VE6BBM)
@ 2024-06-27 20:11         ` Romano
  0 siblings, 0 replies; 8+ messages in thread
From: Romano @ 2024-06-27 20:11 UTC (permalink / raw)
  To: 9front

Couldn't you create a wrapper if you want the documented
default with /dev/null?:
; cat $home/bin/rc/grep
#!/bin/rc
/$cputype/bin/grep $* /dev/null
; bind -b $home/bin/rc /bin

That would allow you to still use '-h', too, to not
have the prefixed file names.

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

end of thread, other threads:[~2024-06-27 20:13 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-26  2:24 [9front] [PATCH] feat(grep): add -H flag shurizzle
2024-06-26  4:56 ` Alex Musolino
2024-06-26 11:18   ` me
2024-06-26 13:49     ` Alex Musolino
2024-06-26 14:08       ` me
2024-06-26 16:31     ` Lyndon Nerenberg (VE7TFX/VE6BBM)
2024-06-26 16:35       ` Lyndon Nerenberg (VE7TFX/VE6BBM)
2024-06-27 20:11         ` Romano

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).