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, 03 Jun 2013 15:54:28 -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/02/2013 01:57 PM, Paul Eggert wrote:
On 06/01/2013 08:14 PM, Daniel J Sebald wrote:

Please consider placing extern "C" around the sig2str and str2sig declarations 
inside sig2str.h similar to the attached diff/changeset. We're looking at using the 
sig2str routine in Octave and removing some code from that project that is similar to 
what sig2str but not up-to-date with all the Unix SIG definitions that might exist.

Thanks, done.

Thank you.


I would also like you to consider the issue of duplicate signal definitions 
(i.e., different SIGxxxx using the same number such as SIGCHLD and SIGCLD).  
Right now, sig2str will return only one string associated with the signal 
number (first appearance) even though internally the structure numname[] 
contains a complete list.  If someone wants to know all definitions, then 
sig2str doesn't really indicate that.  What do people think?  Should there be a 
routine that provides a complete listing?

I suppose it wouldn't hurt...

Not a straightforward thing to do in C, however. For example, one couldn't return a list pointer which is non-constant (unless left for the programmer to clean up) because that wouldn't be re-entrant for multithreaded environments. That is, one couldn't dynamically create something like

{"SIGCHLD", "SIGCLD"}

and return it to the calling program.

Perhaps the easiest thing for inquiring all strings would be some kind of variation of sending back a pointer to the static constant numname[] structure with an empty string as the last entry. For example, a routine like the following

const char** allsigstr (void);

Would give a simple list of strings. (In sig2str.c one could create a string vector that replicates the numname.name portion of the structure. Then, one could use int str2sig (char const *, int *) to get the number. That doesn't seem too bad. Let me try such a thing quick...

OK, a sample changeset is attached that creates the short routine allsigstr(). Basically, it copies all the strings into a static array and sends back that pointer value. I think that is a safely re-entrant type of routine because it will only ever be copying the exact same data onto the array contents...so no matter where it is interrupted, there is no change to the contents. I was a bit clumsy with the return array field width syntax, but perhaps someone will have a better idea.

Here is how I was using the sig2str routine:

  // Scan for signal names, inherently arranged by number.  Skip 0.
  for (int i = 1; i < _NSIG; i++)
    {
      char signame[SIG2STR_MAX];
      // sig2str assigns strings for unused signals as RTMIN+# or RTMAX-#.
      // Ignore these unused signals.
      if (! sig2str (i, signame) && strncmp (signame, "RTMIN", 5)
                                 && strncmp (signame, "RTMAX", 5))
        m.assign (signame, i);
    }

And here is how I use the prototype allsigstr routine:

  // Scan for signal names, random arrangement.  The sig2str routine, at
  // the time, forced EXIT to be signal 0 even though no such signal is
  // defined in the system kernel header file.  Skip EXIT.
  SigStr const * allnames = allsigstr ();
  if (allnames)
    {
      for (int i = 0; strcmp (allnames[i], ""); i++)
        if (strcmp (allnames[i], "EXIT"))
          {
            int value;
            str2sig (allnames[i], &value);
            m.assign (allnames[i], value);
          }
    }

That produces all the definitions. It's not the most efficient approach, CPU-wise, but the list of signals isn't very long, so efficiency isn't a problem here.

Dan

Attachment: gnulib-hg_allsigstr_2013jun03.patch
Description: Text Data


reply via email to

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