Commit Graph

70 Commits

Author SHA1 Message Date
Quentin Rameau
eeeb7a6e53 grep: make E and F flags mutually exclusive
Don't make F the priority flag when both E and F are given, instead use
the last one.
2016-01-05 13:54:17 +00:00
Quentin Rameau
6e7743eb56 Cleanup usage() across sbase
Some tools didn't use argv0 for tool name, or usage() at all.
2015-12-21 18:07:25 +00:00
sin
2366164de7 No need for semicolon after ARGEND
This is also the style used in Plan 9.
2015-11-01 10:18:55 +00:00
FRIGN
d23cc72490 Simplify return & fshut() logic
Get rid of the !!()-constructs and use ret where available (or introduce it).

In some cases, there would be an "abort" on the first fshut-error, but we want
to close all files and report all warnings and then quit, not just the warning
for the first file.
2015-05-26 16:41:43 +01:00
FRIGN
9a074144c9 Remove handrolled strcmp()'s
Favor readability over bare-metal.
2015-05-21 15:43:38 +01:00
sin
0ba81a0f84 grep: Remove unused variable 2015-05-16 13:34:00 +01:00
FRIGN
0545d32ce9 Handle '-' consistently
In general, POSIX does not define /dev/std{in, out, err} because it
does not want to depend on the dev-filesystem.
For utilities, it thus introduced the '-'-keyword to denote standard
input (and output in some cases) and the programs have to deal with
it accordingly.

Sadly, the design of many tools doesn't allow strict shell-redirections
and many scripts don't even use this feature when possible.

Thus, we made the decision to implement it consistently across all
tools where it makes sense (namely those which read files).

Along the way, I spotted some behavioural bugs in libutil/crypt.c and
others where it was forgotten to fshut the files after use.
2015-05-16 13:34:00 +01:00
FRIGN
11e2d472bf Add *fshut() functions to properly flush file streams
This has been a known issue for a long time. Example:

printf "word" > /dev/full

wouldn't report there's not enough space on the device.
This is due to the fact that every libc has internal buffers
for stdout which store fragments of written data until they reach
a certain size or on some callback to flush them all at once to the
kernel.
You can force the libc to flush them with fflush(). In case flushing
fails, you can check the return value of fflush() and report an error.

However, previously, sbase didn't have such checks and without fflush(),
the libc silently flushes the buffers on exit without checking the errors.
No offense, but there's no way for the libc to report errors in the exit-
condition.

GNU coreutils solve this by having onexit-callbacks to handle the flushing
and report issues, but they have obvious deficiencies.
After long discussions on IRC, we came to the conclusion that checking the
return value of every io-function would be a bit too much, and having a
general-purpose fclose-wrapper would be the best way to go.

It turned out that fclose() alone is not enough to detect errors. The right
way to do it is to fflush() + check ferror on the fp and then to a fclose().
This is what fshut does and that's how it's done before each return.
The return value is obviously affected, reporting an error in case a flush
or close failed, but also when reading failed for some reason, the error-
state is caught.

the !!( ... + ...) construction is used to call all functions inside the
brackets and not "terminating" on the first.
We want errors to be reported, but there's no reason to stop flushing buffers
when one other file buffer has issues.
Obviously, functionales come before the flush and ret-logic comes after to
prevent early exits as well without reporting warnings if there are any.

One more advantage of fshut() is that it is even able to report errors
on obscure NFS-setups which the other coreutils are unable to detect,
because they only check the return-value of fflush() and fclose(),
not ferror() as well.
2015-04-05 09:13:56 +01:00
Hiltjo Posthuma
1e0c3a0ba6 whitespace fixes 2015-03-27 22:48:05 +01:00
Hiltjo Posthuma
fea0a34e13 grep: use len returned from getline and check fmemopen() 2015-03-27 15:54:41 +01:00
FRIGN
9144d51594 Check getline()-return-values properly
It's not useful when 0 is returned anyway, so be sure that we have a
string with length > 0, this also solves some indexing-gotchas like
"len - 1" and so on.
Also, add checked getline()'s whenever it has been forgotten and
clean up the error-messages.
2015-03-27 14:49:48 +01:00
Hiltjo Posthuma
ad6776e9a1 grep, kill, renice, sort: style: put main at bottom 2015-03-08 12:51:33 +01:00
sin
71de7813c0 Include strings.h for strcasecmp()
Fixes another build error on NetBSD.
2015-02-20 16:00:58 +00:00
FRIGN
31572c8b0e Clean up #includes 2015-02-14 21:12:23 +01:00
Jakob Kramer
0fcad66c75 make use of en*alloc functions 2015-02-11 01:17:21 +00:00
Hiltjo Posthuma
6579919fec grep: getline returns signed (ssize_t) 2015-01-31 15:19:42 +01:00
sin
2ba7005ddd Use \< and \> instead of [[:<:]] and [[:>:]]
musl doesn't seem to support the latter, so use the older SVR4
word delimiters.
2015-01-22 17:37:52 +00:00
sin
e91d94a70e Add grep -w support
Require to use abuild on Alpine Linux with sbase.
2015-01-22 17:07:57 +00:00
sin
fb85f99c0a grep: Reuse allocated buffers 2014-12-16 20:21:56 +00:00
sin
a3e4689743 grep: Don't bother free-ing the pattern list 2014-12-16 19:44:16 +00:00
Hiltjo Posthuma
5c821d43ef grep: reset inverse flag if both are set 2014-11-21 23:07:50 +00:00
sin
9e74df6520 Add -h to grep(1) usage line 2014-11-21 13:15:36 +00:00
sin
8d26936b83 Update grep(1) manpage and usage line 2014-11-21 13:12:04 +00:00
sin
2b39f20675 Respect exit status in grep(1) 2014-11-21 11:44:09 +00:00
sin
875f433666 Argh - include strings.h 2014-11-21 00:03:30 +00:00
sin
ce86a05f36 Import strcasestr() from musl and remove -D_GNU_SOURCE 2014-11-20 23:46:06 +00:00
sin
ea4f58ff02 Include strings.h for strcasecmp 2014-11-20 23:38:02 +00:00
sin
8767e4b320 Properly handle multiline patterns in grep(1)
We should be POSIX compliant now.
2014-11-20 19:56:44 +00:00
sin
b6a41b688b If we have a match any pattern also match against blank lines 2014-11-20 18:20:10 +00:00
sin
f4d8ff9598 Properly handle -F and -i 2014-11-20 17:40:47 +00:00
sin
64aac9b504 Just use int for mode 2014-11-20 17:26:57 +00:00
sin
fe48fbc4e1 Break out on first match 2014-11-20 17:03:09 +00:00
sin
a1844fae70 Implement grep -f 2014-11-20 16:58:32 +00:00
sin
5ba4f37ec3 Handle null BRE/ERE and do not add a pattern to the list if it already exists 2014-11-20 16:46:57 +00:00
sin
7627a5069c Implement grep -x 2014-11-20 14:47:47 +00:00
sin
e34ce44192 Test directly, no need for intermediate assignment 2014-11-20 14:37:59 +00:00
sin
6866bcdec8 Implement grep -F 2014-11-20 14:35:55 +00:00
sin
728f36aa77 Implement grep -s 2014-11-20 14:14:41 +00:00
Evan Gates
84b08427a1 remove agetline 2014-11-18 21:05:28 +00:00
sin
afa2e6ec54 Use SLIST_* instead of TAILQ_* in grep(1)
The order of evaluation is unspecified by POSIX so we do not need
to process the patterns in-order.
2014-11-17 10:59:51 +00:00
Hiltjo Posthuma
f6552e3669 grep: add -h (inverse of -H) aswell 2014-11-17 10:05:10 +00:00
Hiltjo Posthuma
1822f70d12 csplit, grep: use eregcomp 2014-11-16 14:37:10 +00:00
sin
67fcc79046 Use queue.h in grep(1) 2014-11-16 12:39:15 +00:00
Hiltjo Posthuma
865869fb28 grep: add -H flag 2014-11-16 11:23:18 +00:00
FRIGN
e17b9cdd0a Convert codebase to use emalloc.c utility-functions
This also definitely increases readability and makes OOM-conditions
more consistent.
2014-11-16 10:22:39 +00:00
FRIGN
ec8246bbc6 Un-boolify sbase
It actually makes the binaries smaller, the code easier to read
(gems like "val == true", "val == false" are gone) and actually
predictable in the sense of that we actually know what we're
working with (one bitwise operator was quite adventurous and
should now be fixed).

This is also more consistent with the other suckless projects
around which don't use boolean types.
2014-11-14 10:54:20 +00:00
FRIGN
eee98ed3a4 Fix coding style
It was about damn time. Consistency is very important in such a
big codebase.
2014-11-13 18:08:43 +00:00
Michael Forney
6327290cf2 grep: Fix -c flag 2014-11-02 09:46:52 +00:00
Michael Forney
1ca8a314f8 grep: Remove newlines before matching a line
Otherwise, a pattern with a '$' anchor will never match and POSIX says that

  "By default, an input line shall be selected if any pattern ... matches any
   part of the line excluding the terminating <newline>"
2014-11-02 09:46:46 +00:00
Hiltjo Posthuma
97fb4a1f9c grep: improvements
improvements:
- improve statuscode behaviour
  - don't exit if a file in a series fails. exit 2 if an error occured
    in a file series. don't exit if there is a read error (like: grep
     input file is a directory).
- use agetline instead of agets().

with the simple test: time seq 1 100000000 | grep 'a'
its 12 seconds (from 24 seconds) on my machine.

Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>
2014-06-01 18:02:01 +01:00