Closed smalltalkman closed 4 months ago
The help message is being sent to stderr.
According to POSIX stderr should be used for diagnostic output when the exit code of a utility indicates an error.
That isn't the case here, as generating help text isn't an error.
However, sending help to stderr is what upstream BusyBox does, and for something this fundamental I wouldn't want busybox-w32 to diverge from upstream.
sending help to stderr is what upstream BusyBox does
FWIW, I think this can be considered a bug.
There are also some other (non-busybox) utils which print help to stderr, but most utils do print the intentional help message to stdout (when using -h
or --help
).
When it's part of an error message (like a short usage line, etc), then it should, and typically does, print to stderr.
@rmyorston If you're interested, this small enough patch seem to handle well the non-individual-applet case (the individual-applet case does seem to print to stdout, if I read it correctly - at individual.c
, though I haven't tested it):
diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index 7586f1ce1..81b25783d 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -143,6 +143,8 @@ static const char packed_usage[] ALIGN1 = { PACKED_USAGE };
void FAST_FUNC bb_show_usage(void)
{
+#define full_write2_str (xfunc_error_retval ? full_write2_str : full_write1_str)
+
if (ENABLE_SHOW_USAGE) {
#ifdef SINGLE_APPLET_STR
/* Imagine that this applet is "true". Dont link in printf! */
@@ -193,6 +195,7 @@ void FAST_FUNC bb_show_usage(void)
#endif
}
xfunc_die();
+
+#undef full_write2_str
}
#if ENABLE_PLATFORM_MINGW32 && NUM_APPLETS > 1 && \
If bloat is a concern due to replacing each full_write2_str
call with more code (or the macro seemingly referencing itself), then it can be placed in a function instead, still with the same minimal diff at bb_show_usage
.
EDIT (re bloat/macro-recursion):
Or change the #define
part to something like this, which saves 136 bytes in the x86 build compared to the above (and also seem to actually save 4 bytes compared to master!):
void FAST_FUNC bb_show_usage(void)
{
ssize_t FAST_FUNC (*full_write_fn)(const char *) =
xfunc_error_retval ? full_write2_str : full_write1_str;
#define full_write2_str full_write_fn
The help message is being sent to stderr.
It's weird, but this worked for me: find --help 2>&1 | grep BusyBox
@rmyorston do you want me to open a PR with a minimal patch similar to the suggestion above?
do you want me to open a PR
No thanks. This needs to go upstream and I'm considering how to do that.
I'm considering how to do that.
Add this
ssize_t FAST_FUNC (*full_write_fn)(const char *) =
xfunc_error_retval ? full_write2_str : full_write1_str;
and change all the full_write2_str
in this function to full_write_fn
?
Proposed upstream patches:
LGTM.
Nice sleuthing with the dup2 and full_write2_str at the first patch (I didn't try to verify that all messages go to the same stream as before). EDIT: I did go over it, I think it's fine.
At the second patch, personally I used the commit title "usage: print non-error help messages to stdout", which admittedly Is not too great, but it is a bit more explicit, though it doesn't matter much either way.
At the 2nd patch:
- )
+ ) {
+ xfunc_error_retval = 0;
bb_show_usage();
+ }
Does this suggest that previously bb_show_usage()
could exit with incorrect code?
If yes, then it should probably fixed in its own commit, and if no (I'd think it's "no", because it should default to 0), then I don't think this part is required?
Yes, the return code was wrong and the output went to stderr
. xfunc_error_retval
defaults to 1 (it's set in libbb/default_error_retval.c
).
The change brings that call to bb_show_usage()
into line with the other two in the same file.
Since it ensures the usage message goes to the correct stream it seems appropriate to put it in the same commit.
Yes, the return code was wrong
Hmm.. time --help
does exit with code 0. I guess this change only applies when an applet is built individually (which I never tried)?
In this case, I think it's at least worth mentioning at the commit message that it also fixes incorrect exit code of individual applets with --help, but up to you.
I've just posted off the two patches unmodified. Let's see what happens.