From owner-freebsd-current@FreeBSD.ORG  Mon Aug 23 15:18:51 2010
Return-Path: <owner-freebsd-current@FreeBSD.ORG>
Delivered-To: freebsd-current@freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id A072D1065697;
	Mon, 23 Aug 2010 15:18:51 +0000 (UTC) (envelope-from des@des.no)
Received: from smtp.des.no (smtp.des.no [194.63.250.102])
	by mx1.freebsd.org (Postfix) with ESMTP id 603CD8FC08;
	Mon, 23 Aug 2010 15:18:51 +0000 (UTC)
Received: from ds4.des.no (des.no [84.49.246.2])
	by smtp.des.no (Postfix) with ESMTP id AA90A1FFC37;
	Mon, 23 Aug 2010 15:18:49 +0000 (UTC)
Received: by ds4.des.no (Postfix, from userid 1001)
	id 7F9E38452E; Mon, 23 Aug 2010 17:18:49 +0200 (CEST)
From: =?utf-8?Q?Dag-Erling_Sm=C3=B8rgrav?= <des@des.no>
To: bf1783@gmail.com
References: <AANLkTinPj0x=ZZ6w_hdkt1_DdZ19AvUoRzcZyCKY+ndB@mail.gmail.com>
	<86mxshdqgh.fsf@ds4.des.no>
	<AANLkTinZtyz=L32Pqch_m76zuSMK8FWaM=VWM70YZF+_@mail.gmail.com>
	<86r5hpwmf4.fsf@ds4.des.no>
Date: Mon, 23 Aug 2010 17:18:49 +0200
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 +0200")
Message-ID: <864oelwe5i.fsf@ds4.des.no>
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (berkeley-unix)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
Cc: freebsd-current@freebsd.org, gabor@freebsd.org
Subject: Re: Official request: Please make GNU grep the default
X-BeenThere: freebsd-current@freebsd.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Discussions about the use of FreeBSD-current
	<freebsd-current.freebsd.org>
List-Unsubscribe: <http://lists.freebsd.org/mailman/listinfo/freebsd-current>, 
	<mailto:freebsd-current-request@freebsd.org?subject=unsubscribe>
List-Archive: <http://lists.freebsd.org/pipermail/freebsd-current>
List-Post: <mailto:freebsd-current@freebsd.org>
List-Help: <mailto:freebsd-current-request@freebsd.org?subject=help>
List-Subscribe: <http://lists.freebsd.org/mailman/listinfo/freebsd-current>,
	<mailto:freebsd-current-request@freebsd.org?subject=subscribe>
X-List-Received-Date: Mon, 23 Aug 2010 15:18:51 -0000

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