Re: [PATCH] tools: perf: prefer clarity in setup_pager

From: Michal Nazarewicz
Date: Mon Jul 07 2014 - 08:32:20 EST


On Sun, Jul 06 2014, Michael Lentine <mlentine@xxxxxxxxxx> wrote:
> Unless I'm missing something this removes defaulting the pager to cat
> if nothing is found which is very useful for devices/oses without less
> or pager.

The last two conditions are merged together, i.e.

if (!pager)
pager = "cat";
if (!*pager || !strcmp(pager, "cat"))
return;

becomes:

if (!pager || !*pager || !strcmp(pager, "cat"))
return;

This also in my opinion simplifies the code since the original is a bit
convoluted. If pager is NULL it first assigns "cat" to it and then in
the very next condition compares pager to "cat". With this change, it's
more obvious that if pager is NULL, the function will terminate.

> On Sun, Jul 6, 2014 at 10:42 AM, Michal Nazarewicz <mina86@xxxxxxxxxx>
> wrote:
>> â!(pager || access(â))â is indeed pretty smart way to write
>> â!pager && access(â) == 0â but other than being clever it gives
>> no advantages and merely confuses the reader who needs to wonder
>> what is actually going on.
>>
>> As such, replace the checks with much cleaner ones.
>>
>> Also, while at it, merge the lest â!pagerâ test with the next
>> test that yields true after the â!pagerâ if's body is executed.
>> ---
>> tools/perf/util/pager.c | 8 +++-----
>> 1 file changed, 3 insertions(+), 5 deletions(-)
>>
>> diff --git a/tools/perf/util/pager.c b/tools/perf/util/pager.c
>> index 31ee02d..14da1b0 100644
>> --- a/tools/perf/util/pager.c
>> +++ b/tools/perf/util/pager.c
>> @@ -57,13 +57,11 @@ void setup_pager(void)
>> }
>> if (!pager)
>> pager = getenv("PAGER");
>> - if (!(pager || access("/usr/bin/pager", X_OK)))
>> + if (!pager && access("/usr/bin/pager", X_OK) == 0)
>> pager = "/usr/bin/pager";
>> - if (!(pager || access("/usr/bin/less", X_OK)))
>> + if (!pager && access("/usr/bin/less", X_OK) == 0)
>> pager = "/usr/bin/less";
>> - if (!pager)
>> - pager = "cat";
>> - if (!*pager || !strcmp(pager, "cat"))
>> + if (!pager || !*pager || !strcmp(pager, "cat"))
>> return;
>>
>> spawned_pager = 1; /* means we are emitting to terminal */

--
Best regards, _ _
.o. | Liege of Serenely Enlightened Majesty of o' \,=./ `o
..o | Computer Science, MichaÅ âmina86â Nazarewicz (o o)
ooo +--<mpn@xxxxxxxxxx>--<xmpp:mina86@xxxxxxxxxx>--ooO--(_)--Ooo--
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/