Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 1 Jul 2009 22:19:58 +1000 (EST)
From:      Bruce Evans <brde@optusnet.com.au>
To:        =?utf-8?Q?Dag-Erling_Sm=C3=B8rgrav?= <des@des.no>
Cc:        src-committers@freebsd.org, d@delphij.net, svn-src-all@freebsd.org, Xin LI <delphij@freebsd.org>, Bruce Evans <brde@optusnet.com.au>, svn-src-head@freebsd.org
Subject:   Re: svn commit: r194789 - head/usr.bin/usbhidctl
Message-ID:  <20090701214927.V39172@delplex.bde.org>
In-Reply-To: <8663ed2265.fsf@ds4.des.no>
References:  <200906232316.n5NNG1iT094289@svn.freebsd.org> <20090625154007.H33864@delplex.bde.org> <4A43B727.7010204@delphij.net> <20090627033608.P35379@delplex.bde.org> <8663ed2265.fsf@ds4.des.no>

next in thread | previous in thread | raw e-mail | index | archive | help
  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.

--0-1165440652-1246450798=:39172
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

On Tue, 30 Jun 2009, [utf-8] Dag-Erling Sm=C3=B8rgrav wrote:

> Bruce Evans <brde@optusnet.com.au> writes:
>> Actually, it is OK to use it in FreeBSD iff the system supports the
>> same program having different names, like reboot/halt/etc.
>
> I don't see the point - you would still need different usage messages
> for each version.  Using your example:
>
> % reboot -h
> reboot: illegal option -- h
> usage: reboot [-dlnpq] [-k kernel]
> % halt -h
> halt: illegal option -- h
> usage: halt [-lnpq] [-k kernel]
>
> The code that implements this is needlessly complicated:
>
> static void
> usage()
> {
>        (void)fprintf(stderr, "usage: %s [-%slnpq] [-k kernel]\n",
>            getprogname(), dohalt ? "" : "d");
>        exit(1);
> }

Yes, I missed that.  I had only looked at an old version which was just
broken -- it always printed -[dnpq] (was also missing -l).

> The following is far more readable:
>
> static void
> usage(void)
> {
>
> =09(void)fprintf(stderr, dohalt ?
> =09    "usage: halt [-lnpq] [-k kernel]\n" :
> =09    "usage: reboot [-dlnpq] [-k kernel]\n");
> =09exit(1);
> }

However, this is broken since it doesn't handle the program being named
fasthalt or fastboot.  The dohalt condition in the current version
barely works for these since fasthalt takes the same args as halt, etc.

I like using the `? :'  operator in sub-expressions, but it only works
well for simpler statements than the above.

reboot in Lite2 uses it instead of getprogname().  reboot in Lite2
also has a home-made err() with another ?: instead of getprogname().
It uses "dohalt ? :", and this is actually correct since Lite2 doesn't
have fasthalt or fastboot, at least in reboot's own Makefile.

> BTW, there are numerous style issues in sbin/reboot/reboot.c.

Really?  According to knfom, it is 94.772% knf (up from 94.520% in
~5.2 and 92.896% in Lite2).  It is a small program so it is easy to
get right, but anything above 90% is a nice colour.  (94.520% is due
to 7 lines fixed by indent(1) and 5 lines broken by indent(1)).

Bruce
--0-1165440652-1246450798=:39172--



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