From owner-svn-src-all@freebsd.org Tue May 8 03:53:47 2018 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id BB0C7FCC3B3; Tue, 8 May 2018 03:53:47 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 6976F6FF70; Tue, 8 May 2018 03:53:47 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 6454324F1C; Tue, 8 May 2018 03:53:47 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w483rlDB033543; Tue, 8 May 2018 03:53:47 GMT (envelope-from kevans@FreeBSD.org) Received: (from kevans@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w483rlde033542; Tue, 8 May 2018 03:53:47 GMT (envelope-from kevans@FreeBSD.org) Message-Id: <201805080353.w483rlde033542@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kevans set sender to kevans@FreeBSD.org using -f From: Kyle Evans Date: Tue, 8 May 2018 03:53:47 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r333351 - head/usr.bin/grep X-SVN-Group: head X-SVN-Commit-Author: kevans X-SVN-Commit-Paths: head/usr.bin/grep X-SVN-Commit-Revision: 333351 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 08 May 2018 03:53:48 -0000 Author: kevans Date: Tue May 8 03:53:46 2018 New Revision: 333351 URL: https://svnweb.freebsd.org/changeset/base/333351 Log: bsdgrep: Allow "-" to be passed to -f to mean "standard input" A version of this patch was originally sent to me by se@, matching behavior from newer versions of GNU grep. While there have been some differences of opinion on whether stdin should be closed or not after depleting it in process of -f, I've opted to leave stdin open and just let the later matching stuff fail and result in a no-match. I'm not married to the current behavior- it was generally chosen since we are adopting this in particular from GNU grep, and I would like to stay consistent without a strong argument to the contrary. The current behavior isn't technically wrong, it's just fairly unfriendly to the developer-user of grep that may not realize their usage is trivially invalid. Submitted by: se Modified: head/usr.bin/grep/grep.1 head/usr.bin/grep/grep.c Modified: head/usr.bin/grep/grep.1 ============================================================================== --- head/usr.bin/grep/grep.1 Tue May 8 03:52:26 2018 (r333350) +++ head/usr.bin/grep/grep.1 Tue May 8 03:53:46 2018 (r333351) @@ -30,7 +30,7 @@ .\" .\" @(#)grep.1 8.3 (Berkeley) 4/18/94 .\" -.Dd April 25, 2018 +.Dd May 7, 2018 .Dt GREP 1 .Os .Sh NAME @@ -404,6 +404,13 @@ and block buffered otherwise. .El .Pp If no file arguments are specified, the standard input is used. +Additionally, +.Dq - +may be used in place of a file name, anywhere that a file name is accepted, to +read from standard input. +This includes both +.Fl f +and file arguments. .Sh EXIT STATUS The .Nm grep Modified: head/usr.bin/grep/grep.c ============================================================================== --- head/usr.bin/grep/grep.c Tue May 8 03:52:26 2018 (r333350) +++ head/usr.bin/grep/grep.c Tue May 8 03:53:46 2018 (r333351) @@ -307,7 +307,9 @@ read_patterns(const char *fn) size_t len; ssize_t rlen; - if ((f = fopen(fn, "r")) == NULL) + if (strcmp(fn, "-") == 0) + f = stdin; + else if ((f = fopen(fn, "r")) == NULL) err(2, "%s", fn); if ((fstat(fileno(f), &st) == -1) || (S_ISDIR(st.st_mode))) { fclose(f); @@ -324,7 +326,8 @@ read_patterns(const char *fn) free(line); if (ferror(f)) err(2, "%s", fn); - fclose(f); + if (strcmp(fn, "-") != 0) + fclose(f); } static inline const char *