Commit Graph

66 Commits

Author SHA1 Message Date
Mark Johnston
b480502648 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 <yuripv@yuripv.net>
Approved by:	re (kib)
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D16798
2018-08-29 17:09:03 +00:00
Pedro F. Giffuni
0649b6706f sed: unsign some indexes to fix sign-compare warnings.
Hinted by:	OpenBSD (CVS 1.32)

MFC after:	 1 week
2018-07-30 18:29:46 +00:00
Pedro F. Giffuni
8a16b7a18f General further adoption of SPDX licensing ID tags.
Mainly focus on files that use BSD 3-Clause license.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.

Special thanks to Wind River for providing access to "The Duke of
Highlander" tool: an older (2014) run over FreeBSD tree was useful as a
starting point.
2017-11-20 19:49:47 +00:00
Warner Losh
fbbd9655e5 Renumber copyright clause 4
Renumber cluase 4 to 3, per what everybody else did when BSD granted
them permission to remove clause 3. My insistance on keeping the same
numbering for legal reasons is too pedantic, so give up on that point.

Submitted by:	Jan Schaumann <jschauma@stevens.edu>
Pull Request:	https://github.com/freebsd/freebsd/pull/96
2017-02-28 23:42:47 +00:00
Pedro F. Giffuni
249a8a8003 sed(1): Revert r303047 "cleanup" and therefore r303572.
While big, the change was meant to have no effect on behavior and instead
so far we have found two regressions: one in the etcupdate tests and
another one in the games/openttd port[1].

Revert to a known working state. We will likely have to split the patch in
functional parts before bringing back the changes.

PR:		195929
Reported by:	danfe, madpilot [1]
2016-08-02 15:35:53 +00:00
Pedro F. Giffuni
f05dbca7c7 sed(1): Appease older GCC. 2016-07-21 14:17:36 +00:00
Pedro F. Giffuni
b4932d18ee sed(1): Assorted cleanups and simplifications.
Const-ify several variables, make it build cleanly with WARNS level 5.

Submitted by:	mi
PR:		195929
MFC after:	1 month
2016-07-19 22:56:40 +00:00
Pedro F. Giffuni
4386fba70c sed(1): Fix off by one introduced in r299211.
Detected by running the gsed tests.

Submitted by:	Mikhail Teterin
PR:		195929
MFC after:	3 days
2016-07-17 21:49:53 +00:00
Pedro F. Giffuni
7e77774311 sed(1): Fix a mismatch and sync with the OpenBSD's commit.
This was causing some strange behaviour.

Reported by:	olivier
Obtained from:	OpenBSD (CVS rev. 1.28)
2016-06-01 16:53:02 +00:00
Pedro F. Giffuni
924b84c108 sed: convert sed to use REG_STARTEND more explicitly.
Summarizing the findings in the OpenBSD list:

This solves a reproduceable issue with very recent Mesa where REG_NOTBOL
combined with a match at the begin of the string causes our regex library
to treat the word as not begin of word.

Thanks to Martijn van Duren and Ingo Schwarze for taking the time to
solve this in the least invasive way.

PR:		209352, 209387
Taken from:     openbsd-tech (Martijn van Duren)
MFC after:	1 month
2016-05-25 15:42:39 +00:00
Pedro F. Giffuni
b6f5aa5715 sed: rewrite the main loop.
Rewrite the main loop of the "sed s/..." command, shortening it by ten
lines and simplifying it by removing the switch statement implementing
/g, /1, and /2 separately and repetitively.

This will be needed to bring a fix from OpenBSD later.

Obtained from:	OpenBSD (schwarze CVS Rev. 1.18)
MFC after:	3 weeks
2016-05-07 01:44:22 +00:00
Pedro F. Giffuni
fc04dce059 Fix resource leak and dereference after NULL.
process.c:
Protect access against NULL.

main.c:
Prevent outfile overwrite resource leak.

CID:	271181
CID:	1006930

Obtained from:	NetBSD
MFC after:	3 days
2015-01-27 18:58:24 +00:00
Pedro F. Giffuni
a161398ab8 Replace __inline GNUism with the standard inline.
MFC after:	1 week
2015-01-13 00:04:22 +00:00
Pedro F. Giffuni
707c9cc5d1 sed: Address warnings with clang and gcc48.
MFC after:	2 weeks
2015-01-08 16:33:15 +00:00
Jean-Sébastien Pédron
96d68291af sed(1): Don't force a newline on last line, if input stream doesn't have one
While here, change how we check if the current line is the last one.
Before, we just checked if there were more files after the current one.
Now, we check the actual content of those files: they files may not have
a line at all. This matches the definition of the "last line" by the
Open Group.

The new behavior is closer to GNU sed.

PR:		160745
Phabric:	https://phabric.freebsd.org/D431
Reviewed by:	jilles
Approved by:	jilles
Exp-run by:	antoine
2014-08-08 17:29:01 +00:00
Jeremie Le Hen
b38ebc0538 Fix relative numerical addressing (addr,+N).
As a bonus the patch untangles a bit the logic and makes the code
easier to grasp.

PR:		192108
MFC after:	1 week
2014-07-30 14:46:39 +00:00
Brian Somers
f879e8d923 Implement "addr1,+N" ranges - not dissimilar to grep's -A switch.
PR:		134856
Submitted by:	Jeremie Le Hen - jeremie at le-hen dot org
2009-05-25 06:45:33 +00:00
David Malone
d3e5e11ce6 WARNS fixes:
1) Add missing parens around assignment that is compared to zero.
2) Make some variables that only take non-negative values unsigned.
3) Some casts/type changes to fix other constness warnings.
4) Make one variable a const char *.
5) Make sure termwidth is positive, it doesn't make sense for it to be negative.

Approved by:	dds
2008-02-09 09:12:02 +00:00
Yaroslav Tykhiy
85ae5d2f94 Drop the argument to the OUT macro because it can't emit
anything but the pattern space anyway.  Apply style(9)
to the macro.

Tested with:	md5(1)
2007-06-12 12:17:25 +00:00
Yaroslav Tykhiy
26a5710c40 Don't forget to clear out the hold space for each subsequent file
when in -i mode so that each file gets a clean context of its own.

Add a regression test for the bug.

Tested with:	regression tests
2007-06-12 12:05:24 +00:00
Yaroslav Tykhiy
bebfe2adf7 There is a symbolic antonym for REPLACE as a flag to cspace()
and mf_fgets(): APPEND.  So use it instead of a 0 constant for
clarity.

Tested with:	md5(1)
2007-06-12 10:56:38 +00:00
Yaroslav Tykhiy
f6703c9c0a Change the semantics of -i (in-place editing) so that it treats
each file independently from other files.  The new semantics are
desired in the most of practical cases, e.g.: delete lines 5-9
from each file.

Keep the previous semantics of -i under a new option, -I, which
uses a single continuous address space covering all files to edit
in-place -- they are too cool to just drop them.

Add regression tests for -i and -I.

Approved by:	dds
Compared with:	GNU sed
Discussed on:	-hackers
MFC after:	2 weeks
2007-04-21 01:21:36 +00:00
Yaroslav Tykhiy
e9a0eed451 Don't forget to close the range if we branched over its end
and had no chance to match it by the 2nd address precisely.
Otherwise the unclosed range would bogusly extend to the end
of stream.

Add a basic regression test for the bug fixed.  (This change
also fixes the more complex case 5.3 from `multitest.t'.)

Compared with:	SUN and GNU seds
Tested by:	regression tests
MFC after:	1 week
2007-04-02 08:14:46 +00:00
Yaroslav Tykhiy
b1b46280cb Prevent foot-shooting in advance: Put the MATCH() macro's value
in parentheses.  The ?: operator has a remarkably low precedence, so
expressions like (MATCH(foo) && bar) would have an unexpected meaning
w/o the parentheses around MATCH().

Tested with:	md5(1)
2007-04-02 06:47:48 +00:00
Yaroslav Tykhiy
6b8ef68111 This trivial change should fix at least 3 similar bugs. All of
them are related to the `c' function's need to know if we are at
the actual end of the address range.  (It must print the text not
earlier than the whole pattern space was deleted.)  It appears the
only sed function with this requirement.

There is `lastaddr' set by applies(), which is to notify the `c'
function, but it can't always help because it's false when we are
hitting the end of file early.  There is also a bug in applies()
due to which `lastaddr' isn't set to true on degenerate ranges such
as `$,$' or `N,$' if N appears the last line number.

Handling early EOF condition in applies() could look more logical,
but it would effectively revert sed to the unreasonable behaviour
rev. 1.26 of main.c fought against, as it would require lastline()
be called for each line within each address range.  So it's better
to call lastline() only if needed by the `c' function.

Together with this change to sed go regression tests for the bugs
fixed (c1-c3).  A basic test of `c' (c0) is also added as it helped
me to spot my own error.

Discussed with:		dds
Tested by:		the regression tests
MFC after:		1 week
2007-04-01 13:25:03 +00:00
Yaroslav Tykhiy
c691c0ed18 Make the comment for cspace() match reality. 2007-03-31 09:08:22 +00:00
Kirill Ponomarev
85184b8b23 Fix using "P" command on empty pattern space. If the "P" command is
used once on a non-empty pattern space and then again on an empty
pattern space, the second usage restores the pattern space length to
the length that it had when the first "P" was used.

PR:		bin/96052
Submitted by:	Andrey Zholos <aaz@althenia.net>
MFC after:	7 days
2006-05-27 19:32:28 +00:00
Stefan Farfeleder
6c97c3d1d3 Remove unused variables. 2005-04-09 14:31:41 +00:00
Diomidis Spinellis
30fd73fb81 Per letter dated July 22, 1999 remove 3rd clause of Berkeley derived
software (original contributor).

Reviewed by:	imp
2004-08-09 15:29:41 +00:00
Tim J. Robbins
81a8648adb Make the 'y' (translate) command aware of multibyte characters. 2004-07-14 10:06:22 +00:00
Tim J. Robbins
b4828720f7 Overhaul lputs() to deal with multibyte characters, characters that take
up more than one column position, and null bytes.
2004-07-13 04:35:43 +00:00
Dag-Erling Smørgrav
f54cda1467 Reimplement in-place editing in a slightly less disgusting manner. Also,
make an effort to preserve the ownership and mode of the file we are
editing.

Sponsored by:	Registrar AS
2003-11-04 13:09:16 +00:00
Dag-Erling Smørgrav
9cde9a2e85 Whitespace cleanup 2003-11-04 12:16:47 +00:00
Dag-Erling Smørgrav
e6478125c8 ANSIfy 2003-11-04 12:15:20 +00:00
Tim J. Robbins
ce20a8e9be Change the buffer length test in NEEDSP() so that it does not
subtract one unsigned number from another potentially smaller
one, leading to wraparound (and heap corruption, eventually).

PR:		58813
MFC after:	2 weeks
2003-11-02 23:20:24 +00:00
Tony Finch
c2b9b6eba8 Do not dribble zero bytes into the output, by replacing an obfuscated if
whose true and false clauses were equivalent with a check that we are
not about to stumble off the end of the line.

Reported by:	peter
Pointy hat to:	fanf
2003-06-05 12:10:19 +00:00
Tony Finch
2f17b7e279 Fix substitution when the regex matches the zero-length string.
There are two bugs: in the s///g case, the substitution didn't occur
at the end of the line; in the s///N case, the code didn't count
forwards along the line properly. See the sg, s3, s4, and s5 tests
in src/tools/regression/usr.bin/sed/.

Reviewed by:	tjr
2003-06-04 15:31:55 +00:00
Eric Melville
b81df0819d Initialize a variable in order to fix when faced with picky compilers.
Sponsored by:	Apple
2002-09-20 19:40:23 +00:00
Tim J. Robbins
13ee49d316 Correct boundary condition error in D' and P' commands when the last
line of the pattern space is empty. Don't emit spurious newline when
EOF is reached with the `N' command.

Pointed out by:	Oleg Osyka
MFC after:	1 week
2002-08-11 09:53:44 +00:00
Mark Murray
8bd89d7e04 s/inline/__inline/ 2002-07-19 14:12:27 +00:00
Tim J. Robbins
dfb072f405 Make cspace()'s second argument const. Make `escapes' in lputs() const. 2002-07-08 06:00:55 +00:00
Tim J. Robbins
8523e9a677 Avoid checking whether each line is the last line of the file when we don't
need to know. Instead, check when we are trying to match a "$" address.
This does not change the way sed processes regular files, but makes it behave
more sensibly when used interactively.

PR:		40101
MFC after:	2 weeks
2002-07-03 14:38:27 +00:00
Tim J. Robbins
5a3232258e Don't let the pattern space become null if the `x' command is used when the
hold space is null; some functions assume it's never null.

MFC after:	3 days
2002-07-03 14:32:43 +00:00
Tim J. Robbins
d1a1392d8f Fix off by one in `y' (transliterate) command which caused the last character
of the pattern space not to be examined.

Noticed by:	Motoyuki Konno <motoyuki@bsdclub.org>
2002-06-24 11:24:02 +00:00
Tim J. Robbins
5953156dca Now that the pattern space contains no trailing newline, modify the `l'
command's output so it's the same as what SUSv3 specifies.
2002-06-22 03:00:52 +00:00
Tim J. Robbins
ed92199d30 Don't store newlines at the end of each line in the hold/pattern spaces,
instead add the newline when the pattern space is printed. Make the `G' and
`H' commands add a newline to the space before the data, remove bogus
addition of newline from `x' command.

PR:		29790, 38195
2002-06-22 01:42:26 +00:00
Tim J. Robbins
e307a7a172 Don't write escape newlines with `l' command (SUSv3) 2002-06-10 07:25:35 +00:00
Tim J. Robbins
03fef72fe9 Correctly handle global substitutions where the pattern is only "$", the
EOL anchor, when the last input line does not end in a newline character.
Picked up by the GNU sed test suite.
2002-06-10 06:06:20 +00:00
Tim J. Robbins
d1ccc822a5 Wrap lines correctly for the `l' command. 2002-06-04 10:40:14 +00:00
Tim J. Robbins
3714ae1254 Ignore COLUMNS environment variable if it is empty (SUSv3) 2002-06-04 10:00:08 +00:00