Ignore:
Timestamp:
Aug 13, 2020, 11:49:06 AM (5 years ago)
Author:
bird
Message:

kash: Generate the signal names at compile time rather than lazily at runtime. This should be more efficient, though may cause trouble if cross building.

File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/kash/bld_signames.c

    r3408 r3409  
    1 /*
    2  * Fake sys_signame.
    3  */
    41
    52#include "shinstance.h" /* for MSC */
     
    74#include <stdio.h>
    85
    9 static char sys_signame_initialized = 0;
    10 char sys_signame[NSIG][16];
     6int main(int argc, char **argv)
     7{
     8    char    aszSigName[NSIG][16];
     9    FILE   *pFile;
     10    int     i;
    1111
    12 void init_sys_signame(void)
    13 {
    14         unsigned i;
    15         if (sys_signame_initialized)
    16                 return;
    17         for (i = 0; i < NSIG; ++i)
    18                 sprintf(sys_signame[i], "%d", i);
    19 #define SET_SIG_STR(sig) strcpy(sys_signame[SIG##sig], #sig);
     12    if (argc != 2 || *argv[1] == '\0')
     13    {
     14        fprintf(stderr, "syntax error: Expected exactly one parameter, the output-file!\n");
     15        return 2;
     16    }
     17
     18    /*
     19     * Populate the name array.
     20     */
     21    strcpy(aszSigName[0], "Signal 0");
     22    for (i = 1; i < NSIG; ++i)
     23        sprintf(aszSigName[i], "%i", i);
     24
     25#define SET_SIG_STR(sig) strcpy(aszSigName[SIG##sig], #sig);
    2026#ifdef SIGHUP
    21         SET_SIG_STR(HUP);
     27    SET_SIG_STR(HUP);
    2228#endif
    2329#ifdef SIGINT
    24         SET_SIG_STR(INT);
     30    SET_SIG_STR(INT);
    2531#endif
    2632#ifdef SIGQUIT
    27         SET_SIG_STR(QUIT);
     33    SET_SIG_STR(QUIT);
    2834#endif
    2935#ifdef SIGILL
    30         SET_SIG_STR(ILL);
     36    SET_SIG_STR(ILL);
    3137#endif
    3238#ifdef SIGTRAP
    33         SET_SIG_STR(TRAP);
     39    SET_SIG_STR(TRAP);
    3440#endif
    3541#ifdef SIGABRT
    36         SET_SIG_STR(ABRT);
     42    SET_SIG_STR(ABRT);
    3743#endif
    3844#ifdef SIGIOT
    39         SET_SIG_STR(IOT);
     45    SET_SIG_STR(IOT);
    4046#endif
    4147#ifdef SIGBUS
    42         SET_SIG_STR(BUS);
     48    SET_SIG_STR(BUS);
    4349#endif
    4450#ifdef SIGFPE
    45         SET_SIG_STR(FPE);
     51    SET_SIG_STR(FPE);
    4652#endif
    4753#ifdef SIGKILL
    48         SET_SIG_STR(KILL);
     54    SET_SIG_STR(KILL);
    4955#endif
    5056#ifdef SIGUSR1
    51         SET_SIG_STR(USR1);
     57    SET_SIG_STR(USR1);
    5258#endif
    5359#ifdef SIGSEGV
    54         SET_SIG_STR(SEGV);
     60    SET_SIG_STR(SEGV);
    5561#endif
    5662#ifdef SIGUSR2
    57         SET_SIG_STR(USR2);
     63    SET_SIG_STR(USR2);
    5864#endif
    5965#ifdef SIGPIPE
    60         SET_SIG_STR(PIPE);
     66    SET_SIG_STR(PIPE);
    6167#endif
    6268#ifdef SIGALRM
    63         SET_SIG_STR(ALRM);
     69    SET_SIG_STR(ALRM);
    6470#endif
    6571#ifdef SIGTERM
    66         SET_SIG_STR(TERM);
     72    SET_SIG_STR(TERM);
    6773#endif
    6874#ifdef SIGSTKFLT
    69         SET_SIG_STR(STKFLT);
     75    SET_SIG_STR(STKFLT);
    7076#endif
    7177#ifdef SIGCHLD
    72         SET_SIG_STR(CHLD);
     78    SET_SIG_STR(CHLD);
    7379#endif
    7480#ifdef SIGCONT
    75         SET_SIG_STR(CONT);
     81    SET_SIG_STR(CONT);
    7682#endif
    7783#ifdef SIGSTOP
    78         SET_SIG_STR(STOP);
     84    SET_SIG_STR(STOP);
    7985#endif
    8086#ifdef SIGTSTP
    81         SET_SIG_STR(TSTP);
     87    SET_SIG_STR(TSTP);
    8288#endif
    8389#ifdef SIGTTIN
    84         SET_SIG_STR(TTIN);
     90    SET_SIG_STR(TTIN);
    8591#endif
    8692#ifdef SIGTTOU
    87         SET_SIG_STR(TTOU);
     93    SET_SIG_STR(TTOU);
    8894#endif
    8995#ifdef SIGURG
    90         SET_SIG_STR(URG);
     96    SET_SIG_STR(URG);
    9197#endif
    9298#ifdef SIGXCPU
    93         SET_SIG_STR(XCPU);
     99    SET_SIG_STR(XCPU);
    94100#endif
    95101#ifdef SIGXFSZ
    96         SET_SIG_STR(XFSZ);
     102    SET_SIG_STR(XFSZ);
    97103#endif
    98104#ifdef SIGVTALRM
    99         SET_SIG_STR(VTALRM);
     105    SET_SIG_STR(VTALRM);
    100106#endif
    101107#ifdef SIGPROF
    102         SET_SIG_STR(PROF);
     108    SET_SIG_STR(PROF);
    103109#endif
    104110#ifdef SIGWINCH
    105         SET_SIG_STR(WINCH);
     111    SET_SIG_STR(WINCH);
    106112#endif
    107113#ifdef SIGIO
    108         SET_SIG_STR(IO);
     114    SET_SIG_STR(IO);
    109115#endif
    110116#ifdef SIGPWR
    111         SET_SIG_STR(PWR);
     117    SET_SIG_STR(PWR);
    112118#endif
    113119#ifdef SIGSYS
    114         SET_SIG_STR(SYS);
     120    SET_SIG_STR(SYS);
    115121#endif
    116122#ifdef SIGBREAK
    117         SET_SIG_STR(BREAK);
     123    SET_SIG_STR(BREAK);
    118124#endif
    119125#undef SET_SIG_STR
    120         sys_signame_initialized = 1;
     126
     127    /*
     128     * Write out the list.
     129     */
     130    pFile = fopen(argv[1], "w");
     131    if (!pFile)
     132    {
     133        fprintf(stderr, "error: failed to open '%s' for writing\n", argv[1]);
     134        return 1;
     135    }
     136    fputs("/* autogenerate */\n"
     137          "\n"
     138          "#include \"shinstance.h\"\n"
     139          "\n"
     140          "const char * const sys_signame[NSIG] = \n"
     141          "{\n"
     142          , pFile);
     143    for (i = 0; i < NSIG; i++)
     144        fprintf(pFile, "    \"%s\",\n", aszSigName[i]);
     145    fputs("};\n", pFile);
     146
     147    if (fclose(pFile) != 0)
     148    {
     149        fprintf(stderr, "error: error writing/closing '%s' after writing it\n", argv[1]);
     150        return 1;
     151    }
     152    return 0;
    121153}
    122154
    123 #if defined(_MSC_VER)
    124 const char *strsignal(int iSig)
    125 {
    126     if (!sys_signame_initialized)
    127         init_sys_signame();
    128     if (iSig < NSIG)
    129         return sys_signame(iSig);
    130     return NULL;
    131 }
    132 #endif
    133 
Note: See TracChangeset for help on using the changeset viewer.