From owner-svn-src-all@freebsd.org Wed Aug 29 17:09:05 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 7A5F7108F646; Wed, 29 Aug 2018 17:09:05 +0000 (UTC) (envelope-from markj@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 2F44B74F25; Wed, 29 Aug 2018 17:09:05 +0000 (UTC) (envelope-from markj@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 0FA9C1FFC4; Wed, 29 Aug 2018 17:09:05 +0000 (UTC) (envelope-from markj@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w7TH94GY001572; Wed, 29 Aug 2018 17:09:04 GMT (envelope-from markj@FreeBSD.org) Received: (from markj@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w7TH94Mn001568; Wed, 29 Aug 2018 17:09:04 GMT (envelope-from markj@FreeBSD.org) Message-Id: <201808291709.w7TH94Mn001568@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: markj set sender to markj@FreeBSD.org using -f From: Mark Johnston Date: Wed, 29 Aug 2018 17:09:04 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r338375 - in head/usr.bin/sed: . tests X-SVN-Group: head X-SVN-Commit-Author: markj X-SVN-Commit-Paths: in head/usr.bin/sed: . tests X-SVN-Commit-Revision: 338375 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.27 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: Wed, 29 Aug 2018 17:09:05 -0000 Author: markj Date: Wed Aug 29 17:09:03 2018 New Revision: 338375 URL: https://svnweb.freebsd.org/changeset/base/338375 Log: sed: Fix -i option behavior with 'q' command. Don't just exit when encountering the 'q' command if we edit file inplace, and give mf_fgets() a chance to actually handle the inplace case. Also add a regression test. Submitted by: Yuri Pankov Approved by: re (kib) MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D16798 Modified: head/usr.bin/sed/extern.h head/usr.bin/sed/main.c head/usr.bin/sed/process.c head/usr.bin/sed/tests/sed2_test.sh Modified: head/usr.bin/sed/extern.h ============================================================================== --- head/usr.bin/sed/extern.h Wed Aug 29 16:59:19 2018 (r338374) +++ head/usr.bin/sed/extern.h Wed Aug 29 17:09:03 2018 (r338375) @@ -46,6 +46,8 @@ extern int aflag, eflag, nflag; extern const char *fname, *outfname; extern FILE *infile, *outfile; extern int rflags; /* regex flags to use */ +extern const char *inplace; +extern int quit; void cfclose(struct s_command *, struct s_command *); void compile(void); Modified: head/usr.bin/sed/main.c ============================================================================== --- head/usr.bin/sed/main.c Wed Aug 29 16:59:19 2018 (r338374) +++ head/usr.bin/sed/main.c Wed Aug 29 17:09:03 2018 (r338375) @@ -102,6 +102,7 @@ FILE *outfile; /* Current output file */ int aflag, eflag, nflag; int rflags = 0; +int quit = 0; static int rval; /* Exit status */ static int ispan; /* Whether inplace editing spans across files */ @@ -115,7 +116,7 @@ const char *fname; /* File name. */ const char *outfname; /* Output file name */ static char oldfname[PATH_MAX]; /* Old file name (for in-place editing) */ static char tmpfname[PATH_MAX]; /* Temporary file name (for in-place editing) */ -static const char *inplace; /* Inplace edit file extension. */ +const char *inplace; /* Inplace edit file extension. */ u_long linenum; static void add_compunit(enum e_cut, char *); @@ -338,7 +339,7 @@ mf_fgets(SPACE *sp, enum e_spflag spflag) } for (;;) { - if (infile != NULL && (c = getc(infile)) != EOF) { + if (infile != NULL && (c = getc(infile)) != EOF && !quit) { (void)ungetc(c, infile); break; } Modified: head/usr.bin/sed/process.c ============================================================================== --- head/usr.bin/sed/process.c Wed Aug 29 16:59:19 2018 (r338374) +++ head/usr.bin/sed/process.c Wed Aug 29 17:09:03 2018 (r338375) @@ -210,10 +210,14 @@ redirect: } break; case 'q': - if (!nflag && !pd) - OUT(); - flush_appends(); - exit(0); + if (inplace == NULL) { + if (!nflag && !pd) + OUT(); + flush_appends(); + exit(0); + } + quit = 1; + break; case 'r': if (appendx >= appendnum) if ((appends = realloc(appends, Modified: head/usr.bin/sed/tests/sed2_test.sh ============================================================================== --- head/usr.bin/sed/tests/sed2_test.sh Wed Aug 29 16:59:19 2018 (r338374) +++ head/usr.bin/sed/tests/sed2_test.sh Wed Aug 29 17:09:03 2018 (r338375) @@ -38,6 +38,7 @@ inplace_hardlink_src_body() atf_check ln a b atf_check sed -i '' -e 's,foo,bar,g' b atf_check -o 'inline:bar\n' -s exit:0 cat b + atf_check -s not-exit:0 stat -q '.!'* } atf_test_case inplace_symlink_src @@ -50,10 +51,27 @@ inplace_symlink_src_body() echo foo > a atf_check ln -s a b atf_check -e not-empty -s not-exit:0 sed -i '' -e 's,foo,bar,g' b + atf_check -s not-exit:0 stat -q '.!'* } +atf_test_case inplace_command_q +inplace_command_q_head() +{ + atf_set "descr" "Verify -i works correctly with the 'q' command" +} +inplace_command_q_body() +{ + printf '1\n2\n3\n' > a + atf_check -o 'inline:1\n2\n' sed '2q' a + atf_check sed -i.bak '2q' a + atf_check -o 'inline:1\n2\n' cat a + atf_check -o 'inline:1\n2\n3\n' cat a.bak + atf_check -s not-exit:0 stat -q '.!'* +} + atf_init_test_cases() { + atf_add_test_case inplace_command_q atf_add_test_case inplace_hardlink_src atf_add_test_case inplace_symlink_src }