bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sig2str and str2sig use in C++


From: Daniel J Sebald
Subject: Re: sig2str and str2sig use in C++
Date: Mon, 10 Jun 2013 21:36:12 -0500
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111108 Fedora/3.1.16-1.fc14 Thunderbird/3.1.16

On 06/10/2013 05:47 PM, Paul Eggert wrote:
On 06/05/13 09:57, Daniel J Sebald wrote:


Well, taking this all into consideration, perhaps the easiest thing to do is 
construct a table at runtime on the heap:

I'd rather avoid the runtime overhead.

How about something like this?

enum { HUP_OFFSET = 0 };
#define HUP_STRING "string for SIGHUP"

enum { INT_OFFSET = HUP_OFFSET + sizeof HUP_STRING };
#define INT_STRING "string for SIGINT"

enum { QUIT_OFFSET = INT_OFFSET + sizeof INT_STRING };
#define QUIT_STRING "string for SIGQUIT"

A bit awkward, but no repeated strings; and I've seen worse.

Yes, something like that would work. Keep in mind that there needs to be a conditional. So might it be something like:

enum { HUP_OFFSET = 0 };
#ifdef SIGHUP
#define HUP_STRING "string for SIGHUP"
#else
#define HUP_STRING ""
#endif

enum { INT_OFFSET = HUP_OFFSET + sizeof HUP_STRING };
#ifdef SIGINT
#define INT_STRING "string for SIGINT"
#else
#define INT_STRING ""
#endif

enum { QUIT_OFFSET = INT_OFFSET + sizeof INT_STRING };
#ifdef SIGQUIT
#define QUIT_STRING "string for SIGQUIT"
#else
#define QUIT_STRING ""
#endif

etc. Otherwise one would have to condition the definitions inside the enum with what is actually defined, i.e.,

enum { QUIT_OFFSET = ?????? + sizeof ?????? }

which would get awkward. But actually maybe these definitions could all be left unconditioned because if they appear in this big character string somewhere it is really only the pointer (i.e., offset) that matters.

But then what about indexing that array? One would still need to assign the values of the definitions somehow, and the goal is to somehow group the signal synonyms, so maybe something like:

#define END_OF_STRINGS '\0',

char sig_strings[] = {
#ifdef SIGHUP
HUP_STRING, '\0',
END_OF_STRINGS
#endif

#ifdef SIGINT
INT_STRING, '\0',
END_OF_STRINGS
#endif

#ifdef SIGQUIT
QUIT_STRING, '\0',
END_OF_STRINGS
#endif

...

#ifdef SIGCHLD
CHLD_STRING, '\0',
#ifndef SIGCLD
END_OF_STRINGS
#endif
#endif

#ifdef SIGCLD
CHD_STRING, '\0',
END_OF_STRINGS
#endif

etc.
}

#define NUMOFFSET(name) { SIG##name, #name }

/* Signal names and numbers.  Put the preferred name first.  */
static struct numname { int num; int offset; } numoffset_table[] =
  {
    /* Signals required by POSIX 1003.1-2001 base, listed in
       traditional numeric order where possible.  */
#ifdef SIGHUP
    NUMOFFSET (HUP),
#endif
#ifdef SIGINT
    NUMOFFSET (INT),
#endif
#ifdef SIGQUIT
    NUMOFFSET (QUIT),
#endif
...
#ifdef SIGCHLD
    NUMOFFSET (CHLD),
#else
#ifdef SIGCLD
    NUMOFFSET (CLD),
#endif
#endif
etc.
};

Notice how there is only one possible pointer for a number. But there too it might not make a difference if there is more than one because the algorithm will just pick the first entry it finds and if NUMOFFSET (CHLD) and NUMOFFSET (CLD) produce the same character offset, no problem.

It seems there are a number of variations to defining the array that will work, but the important point is the character string definition:

CHLD_STRING, '\0',
#ifdef SIGCLD
CHD_STRING, '\0',
#endif
END_OF_STRINGS

Dan



reply via email to

[Prev in Thread] Current Thread [Next in Thread]