Re: [PATCH 1/5] vsprintf/Documentation: Add X to %*ph extension to output upper case hex

From: Petr Mladek
Date: Fri Aug 27 2021 - 06:49:20 EST


On Fri 2021-08-27 11:49:07, Andy Shevchenko wrote:
> On Fri, Aug 27, 2021 at 01:08:10AM -0700, Joe Perches wrote:
> > On Fri, 2021-08-27 at 10:48 +0300, Andy Shevchenko wrote:
> > > On Thu, Aug 26, 2021 at 11:43:01AM -0700, Joe Perches wrote:
> > > > A few sysfs output uses of hex arrays are uppercase and are nominally ABI.
> > > >
> > > > Add a mechanism to the existing vsprintf %*ph hex output extension to
> > > > support upper case hex output.
> > >
> > > ...
> > >
> > > > + The preferred output is lowercase
> > > >   %*ph 00 01 02 ... 3f
> > > >   %*phC 00:01:02: ... :3f
> > > >   %*phD 00-01-02- ... -3f
> > > >   %*phN 000102 ... 3f
> > > > + Formats with X are uppercase, used for backwards compatibility
> > > > + %*phX 00 01 02 ... 3F
> > > > + %*phCX 00:01:02: ... :3F
> > > > + %*phDX 00-01-02- ... -3F
> > > > + %*phNX 000102 ... 3F
> > >
> > > Why not using %*pH...?

I though about this as well.

> > I find X more intelligible.

I would slightly prefer %pH. I always have problems to parse long
sequences of modifiers. So, the shorter format the better.

Of course, it means that 'H' won't be usable for another purpose.
But it will happen one day anyway. Well, this is why I do not
have strong opinion.

I am more and more convinced that we will need another approach.
Mathew Wilcox has had an idea to add support for custom callbacks
that would be able to format the string, something like:

vsprintf("Date: %pX(%p)\n", format_date, time_stamp);

I think that it might even be possible to do something like:

vsprintf("Date: %pX\n", format_date(time));

, where the format_date() would be a macro that would create
a struct at stack a pass it as a pointer:

#define format_date(time) \
({ \
struct vsprintf_callback c = { \
.func = vsprintf_format_date, \
.arg1 = time, \
} \
\
&c; \
})

and vsprintf would internally do something like:

char *custom_format(char *buf, char *end, vsprintf_callback *c,
struct printf_spec spec, const char *fmt)
{
return c->func(buf, end, c->arg1, spec);
}

It would allow to replace all the magic %pXYZ modifiers with
self-explanatory callbacks. While still keeping it easy to use.

Best Regards,
Petr