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

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