Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 23 Aug 2010 17:18:49 +0200
From:      =?utf-8?Q?Dag-Erling_Sm=C3=B8rgrav?= <des@des.no>
To:        bf1783@gmail.com
Cc:        freebsd-current@freebsd.org, gabor@freebsd.org
Subject:   Re: Official request: Please make GNU grep the default
Message-ID:  <864oelwe5i.fsf@ds4.des.no>
In-Reply-To: <86r5hpwmf4.fsf@ds4.des.no> ("Dag-Erling =?utf-8?Q?Sm=C3=B8rg?= =?utf-8?Q?rav=22's?= message of "Mon, 23 Aug 2010 14:20:15 %2B0200")
References:  <AANLkTinPj0x=ZZ6w_hdkt1_DdZ19AvUoRzcZyCKY%2BndB@mail.gmail.com> <86mxshdqgh.fsf@ds4.des.no> <AANLkTinZtyz=L32Pqch_m76zuSMK8FWaM=VWM70YZF%2B_@mail.gmail.com> <86r5hpwmf4.fsf@ds4.des.no>

next in thread | previous in thread | raw e-mail | index | archive | help
Dag-Erling Sm=C3=B8rgrav <des@des.no> writes:
> No idea what causes it, but a quick grep (hah!) for qflag turns up the
> following horror:
>
>         /* Find out the correct return value according to the
>            results and the command line option. */
>         exit(c ? (notfound ? (qflag ? 0 : 2) : 0) : (notfound ? 2 : 1));
>
> which shows that -q *does* affect the exit code, but my brain refuses to
> try to understand that code.

My brain is in need of a break from $REALJOB.  POSIX says

EXIT STATUS

    The following exit values shall be returned:

     0
        One or more lines were selected.
     1
        No lines were selected.
    >1
        An error occurred.

CONSEQUENCES OF ERRORS

    If the -q option is specified, the exit status shall be zero if an
    input line is selected, even if an error was detected. Otherwise,
    default actions shall be performed.

I suppose c is supposed to indicate, in some manner, whether an error
occurred, but it's hard to tell; the code seems almost deliberately
obfuscated.  The name gives no clue whatsoever as to its meaning.  It is
incremented like a counter, but tested like a boolean.  Its value is
derived from the value returned by procfile(), but that value is also
named "c", and is derived from values returned by other functions which
I could not be bothered to track down.  In any case -

    c           notfound    qflag       result
    true        true        true        0
    true        true        false       2
    true        false       true        0
    true        false       false       0
    false       true        true        2
    false       true        false       2
    false       false       true        1
    false       false       false       1

By this point, my brain is tied into the shape of a pretzel, but it
looks like c might actually be a count of matching lines and notfound
might be an error flag.  I give it -10 for calling the count "c" instead
of "count" or "matches" (I'm being generous because "c" is the first
letter of "count"), another -10 for testing it as a boolean instead of
comparing it to 0, -1,000 for calling the error flag "notfound", and
-1,000,000 for writing code so convoluted that, even with the source
code in front of me, I had to reverse-engineer it to figure out what it
does.  I think that adds up to -1,001,020.

DES
--=20
Dag-Erling Sm=C3=B8rgrav - des@des.no



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?864oelwe5i.fsf>