Changeset 6097 for trunk/tools


Ignore:
Timestamp:
Jun 25, 2001, 4:15:50 PM (24 years ago)
Author:
bird
Message:

Corrected separator again... *sigh*

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/wrc/preproc.c

    r5524 r6097  
    1616
    1717
    18 extern void set_pp_ignore(int); /* From parser.l */
    19 
    20 #define HASHKEY         2039
     18extern void set_pp_ignore(int); /* From parser.l */
     19
     20#define HASHKEY     2039
    2121static pp_entry_t *pp_defines[HASHKEY];
    2222
    23 #define MAXIFSTACK      64
     23#define MAXIFSTACK  64
    2424static if_state_t if_stack[MAXIFSTACK];
    2525static int if_stack_idx = 0;
     
    2929void pp_status(void)
    3030{
    31         int i;
    32         int sum;
    33         int total = 0;
    34         pp_entry_t *ppp;
    35 
    36         fprintf(stderr, "Defines statistics:\n");
    37         for(i = 0; i < HASHKEY; i++)
    38         {
    39                 sum = 0;
    40                 for(ppp = pp_defines[i]; ppp; ppp = ppp->next)
    41                         sum++;
    42                 total += sum;
    43                 fprintf(stderr, "%4d, %3d\n", i, sum);
    44         }
    45         fprintf(stderr, "Total defines: %d\n", total);
     31    int i;
     32    int sum;
     33    int total = 0;
     34    pp_entry_t *ppp;
     35
     36    fprintf(stderr, "Defines statistics:\n");
     37    for(i = 0; i < HASHKEY; i++)
     38    {
     39        sum = 0;
     40        for(ppp = pp_defines[i]; ppp; ppp = ppp->next)
     41            sum++;
     42        total += sum;
     43        fprintf(stderr, "%4d, %3d\n", i, sum);
     44    }
     45    fprintf(stderr, "Total defines: %d\n", total);
    4646}
    4747#endif
     
    5050int pphash(char *str)
    5151{
    52         int sum = 0;
    53         while(*str)
    54                 sum += *str++;
    55         return sum % HASHKEY;
     52    int sum = 0;
     53    while(*str)
     54        sum += *str++;
     55    return sum % HASHKEY;
    5656}
    5757
    5858pp_entry_t *pplookup(char *ident)
    5959{
    60         int idx = pphash(ident);
    61         pp_entry_t *ppp;
    62 
    63         for(ppp = pp_defines[idx]; ppp; ppp = ppp->next)
    64         {
    65                 if(!strcmp(ident, ppp->ident))
    66                         return ppp;
    67         }
    68         return NULL;
     60    int idx = pphash(ident);
     61    pp_entry_t *ppp;
     62
     63    for(ppp = pp_defines[idx]; ppp; ppp = ppp->next)
     64    {
     65        if(!strcmp(ident, ppp->ident))
     66            return ppp;
     67    }
     68    return NULL;
    6969}
    7070
    7171void del_define(char *name)
    7272{
    73         int idx;
    74         pp_entry_t *ppp;
    75 
    76         if((ppp = pplookup(name)) == NULL)
    77         {
    78                 if(pedantic)
    79                         yywarning("%s was not defined", name);
    80                 return;
    81         }
    82 
    83         if(ppp->iep)
    84         {
    85                 if(debuglevel & DEBUGLEVEL_PPMSG)
    86                         fprintf(stderr, "del_define: %s:%d: includelogic removed, include_ppp='%s', file=%s\n", input_name, line_number, name, ppp->iep->filename);
    87                 if(ppp->iep == includelogiclist)
    88                 {
    89                         includelogiclist = ppp->iep->next;
    90                         if(includelogiclist)
    91                                 includelogiclist->prev = NULL;
    92                 }
    93                 else
    94                 {
    95                         ppp->iep->prev->next = ppp->iep->next;
    96                         if(ppp->iep->next)
    97                                 ppp->iep->next->prev = ppp->iep->prev;
    98                 }
    99                 free(ppp->iep->filename);
    100                 free(ppp->iep);
    101         }
    102 
    103         idx = pphash(name);
    104         if(pp_defines[idx] == ppp)
    105         {
    106                 pp_defines[idx] = ppp->next;
    107                 if(pp_defines[idx])
    108                         pp_defines[idx]->prev = NULL;
    109         }
    110         else
    111         {
    112                 ppp->prev->next = ppp->next;
    113                 if(ppp->next)
    114                         ppp->next->prev = ppp->prev;
    115         }
    116 
    117         free(ppp);
    118 
    119         if(debuglevel & DEBUGLEVEL_PPMSG)
    120                 printf("Deleted (%s, %d) <%s>\n", input_name, line_number, name);
     73    int idx;
     74    pp_entry_t *ppp;
     75
     76    if((ppp = pplookup(name)) == NULL)
     77    {
     78        if(pedantic)
     79            yywarning("%s was not defined", name);
     80        return;
     81    }
     82
     83    if(ppp->iep)
     84    {
     85        if(debuglevel & DEBUGLEVEL_PPMSG)
     86            fprintf(stderr, "del_define: %s:%d: includelogic removed, include_ppp='%s', file=%s\n", input_name, line_number, name, ppp->iep->filename);
     87        if(ppp->iep == includelogiclist)
     88        {
     89            includelogiclist = ppp->iep->next;
     90            if(includelogiclist)
     91                includelogiclist->prev = NULL;
     92        }
     93        else
     94        {
     95            ppp->iep->prev->next = ppp->iep->next;
     96            if(ppp->iep->next)
     97                ppp->iep->next->prev = ppp->iep->prev;
     98        }
     99        free(ppp->iep->filename);
     100        free(ppp->iep);
     101    }
     102
     103    idx = pphash(name);
     104    if(pp_defines[idx] == ppp)
     105    {
     106        pp_defines[idx] = ppp->next;
     107        if(pp_defines[idx])
     108            pp_defines[idx]->prev = NULL;
     109    }
     110    else
     111    {
     112        ppp->prev->next = ppp->next;
     113        if(ppp->next)
     114            ppp->next->prev = ppp->prev;
     115    }
     116
     117    free(ppp);
     118
     119    if(debuglevel & DEBUGLEVEL_PPMSG)
     120        printf("Deleted (%s, %d) <%s>\n", input_name, line_number, name);
    121121}
    122122
    123123pp_entry_t *add_define(char *def, char *text)
    124124{
    125         int len;
    126         char *cptr;
    127         int idx = pphash(def);
    128         pp_entry_t *ppp;
    129 
    130         if((ppp = pplookup(def)) != NULL)
    131         {
    132                 if(pedantic)
    133                         yywarning("Redefinition of %s\n\tPrevious definition: %s:%d", def, ppp->filename, ppp->linenumber);
    134                 del_define(def);
    135         }
    136         ppp = (pp_entry_t *)xmalloc(sizeof(pp_entry_t));
    137         ppp->ident = def;
    138         ppp->type = def_define;
    139         ppp->subst.text = text;
    140         ppp->filename = input_name ? xstrdup(input_name) : "<internal or cmdline>";
    141         ppp->linenumber = input_name ? line_number : 0;
    142         ppp->next = pp_defines[idx];
    143         pp_defines[idx] = ppp;
    144         if(ppp->next)
    145                 ppp->next->prev = ppp;
    146         if(text)
    147         {
    148                 /* Strip trailing white space from subst text */
    149                 len = strlen(text);
    150                 while(len && strchr(" \t\r\n", text[len-1]))
    151                 {
    152                         text[--len] = '\0';
    153                 }
    154                 /* Strip leading white space from subst text */
    155                 for(cptr = text; *cptr && strchr(" \t\r", *cptr); cptr++)
    156                 ;
    157                 if(text != cptr)
    158                         memmove(text, cptr, strlen(cptr)+1);
    159         }
    160         if(debuglevel & DEBUGLEVEL_PPMSG)
    161                 printf("Added define (%s, %d) <%s> to <%s>\n", input_name, line_number, ppp->ident, text ? text : "(null)");
    162 
    163         return ppp;
     125    int len;
     126    char *cptr;
     127    int idx = pphash(def);
     128    pp_entry_t *ppp;
     129
     130    if((ppp = pplookup(def)) != NULL)
     131    {
     132        if(pedantic)
     133            yywarning("Redefinition of %s\n\tPrevious definition: %s:%d", def, ppp->filename, ppp->linenumber);
     134        del_define(def);
     135    }
     136    ppp = (pp_entry_t *)xmalloc(sizeof(pp_entry_t));
     137    ppp->ident = def;
     138    ppp->type = def_define;
     139    ppp->subst.text = text;
     140    ppp->filename = input_name ? xstrdup(input_name) : "<internal or cmdline>";
     141    ppp->linenumber = input_name ? line_number : 0;
     142    ppp->next = pp_defines[idx];
     143    pp_defines[idx] = ppp;
     144    if(ppp->next)
     145        ppp->next->prev = ppp;
     146    if(text)
     147    {
     148        /* Strip trailing white space from subst text */
     149        len = strlen(text);
     150        while(len && strchr(" \t\r\n", text[len-1]))
     151        {
     152            text[--len] = '\0';
     153        }
     154        /* Strip leading white space from subst text */
     155        for(cptr = text; *cptr && strchr(" \t\r", *cptr); cptr++)
     156        ;
     157        if(text != cptr)
     158            memmove(text, cptr, strlen(cptr)+1);
     159    }
     160    if(debuglevel & DEBUGLEVEL_PPMSG)
     161        printf("Added define (%s, %d) <%s> to <%s>\n", input_name, line_number, ppp->ident, text ? text : "(null)");
     162
     163    return ppp;
    164164}
    165165
    166166pp_entry_t *add_cmdline_define(char *set)
    167167{
    168         char *cpy = xstrdup(set);       /* Because gcc passes a R/O string */
    169         char *cptr = strchr(cpy, '=');
    170         if(cptr)
    171                 *cptr = '\0';
    172         return add_define(cpy, xstrdup(cptr ? cptr+1 : ""));
     168    char *cpy = xstrdup(set);   /* Because gcc passes a R/O string */
     169    char *cptr = strchr(cpy, '=');
     170    if(cptr)
     171        *cptr = '\0';
     172    return add_define(cpy, xstrdup(cptr ? cptr+1 : ""));
    173173}
    174174
    175175pp_entry_t *add_special_define(char *id)
    176176{
    177         pp_entry_t *ppp = add_define(xstrdup(id), xstrdup(""));
    178         ppp->type = def_special;
    179         return ppp;
     177    pp_entry_t *ppp = add_define(xstrdup(id), xstrdup(""));
     178    ppp->type = def_special;
     179    return ppp;
    180180}
    181181
    182182pp_entry_t *add_macro(char *id, marg_t *args[], int nargs, mtext_t *exp)
    183183{
    184         int idx = pphash(id);
    185         pp_entry_t *ppp;
    186 
    187         if((ppp = pplookup(id)) != NULL)
    188         {
    189                 if(pedantic)
    190                         yywarning("Redefinition of %s\n\tPrevious definition: %s:%d", id, ppp->filename, ppp->linenumber);
    191                 del_define(id);
    192         }
    193         ppp = (pp_entry_t *)xmalloc(sizeof(pp_entry_t));
    194         ppp->ident      = id;
    195         ppp->type       = def_macro;
    196         ppp->margs      = args;
    197         ppp->nargs      = nargs;
    198         ppp->subst.mtext= exp;
    199         ppp->filename = input_name ? xstrdup(input_name) : "<internal or cmdline>";
    200         ppp->linenumber = input_name ? line_number : 0;
    201         ppp->next       = pp_defines[idx];
    202         pp_defines[idx] = ppp;
    203         if(ppp->next)
    204                 ppp->next->prev = ppp;
    205 
    206         if(debuglevel & DEBUGLEVEL_PPMSG)
    207         {
    208                 fprintf(stderr, "Added macro (%s, %d) <%s(%d)> to <", input_name, line_number, ppp->ident, nargs);
    209                 for(; exp; exp = exp->next)
    210                 {
    211                         switch(exp->type)
    212                         {
    213                         case exp_text:
    214                                 fprintf(stderr, " \"%s\" ", exp->subst.text);
    215                                 break;
    216                         case exp_stringize:
    217                                 fprintf(stderr, " #(%d) ", exp->subst.argidx);
    218                                 break;
    219                         case exp_concat:
    220                                 fprintf(stderr, "##");
    221                                 break;
    222                         case exp_subst:
    223                                 fprintf(stderr, " <%d> ", exp->subst.argidx);
    224                                 break;
    225                         }
    226                 }
    227                 fprintf(stderr, ">\n");
    228         }
    229         return ppp;
     184    int idx = pphash(id);
     185    pp_entry_t *ppp;
     186
     187    if((ppp = pplookup(id)) != NULL)
     188    {
     189        if(pedantic)
     190            yywarning("Redefinition of %s\n\tPrevious definition: %s:%d", id, ppp->filename, ppp->linenumber);
     191        del_define(id);
     192    }
     193    ppp = (pp_entry_t *)xmalloc(sizeof(pp_entry_t));
     194    ppp->ident  = id;
     195    ppp->type   = def_macro;
     196    ppp->margs  = args;
     197    ppp->nargs  = nargs;
     198    ppp->subst.mtext= exp;
     199    ppp->filename = input_name ? xstrdup(input_name) : "<internal or cmdline>";
     200    ppp->linenumber = input_name ? line_number : 0;
     201    ppp->next   = pp_defines[idx];
     202    pp_defines[idx] = ppp;
     203    if(ppp->next)
     204        ppp->next->prev = ppp;
     205
     206    if(debuglevel & DEBUGLEVEL_PPMSG)
     207    {
     208        fprintf(stderr, "Added macro (%s, %d) <%s(%d)> to <", input_name, line_number, ppp->ident, nargs);
     209        for(; exp; exp = exp->next)
     210        {
     211            switch(exp->type)
     212            {
     213            case exp_text:
     214                fprintf(stderr, " \"%s\" ", exp->subst.text);
     215                break;
     216            case exp_stringize:
     217                fprintf(stderr, " #(%d) ", exp->subst.argidx);
     218                break;
     219            case exp_concat:
     220                fprintf(stderr, "##");
     221                break;
     222            case exp_subst:
     223                fprintf(stderr, " <%d> ", exp->subst.argidx);
     224                break;
     225            }
     226        }
     227        fprintf(stderr, ">\n");
     228    }
     229    return ppp;
    230230}
    231231
     
    236236 *-------------------------------------------------------------------------
    237237 */
    238 #if defined(_Windows) || defined(__MSDOS__)
    239 #define INCLUDESEPARATOR        ";"
     238#if defined(_Windows) || defined(__MSDOS__) || defined(__SEMICOLON__)
     239#define INCLUDESEPARATOR    ";"
    240240#else
    241 #define INCLUDESEPARATOR        ":"
     241#define INCLUDESEPARATOR    ":"
    242242#endif
    243243
     
    247247void add_include_path(char *path)
    248248{
    249         char *tok;
    250         char *cpy = xstrdup(path);
    251 
    252         tok = strtok(cpy, INCLUDESEPARATOR);
    253         while(tok)
    254         {
    255                 char *dir;
    256                 char *cptr;
    257                 if(strlen(tok) == 0)
    258                         continue;
    259                 dir = xstrdup(tok);
    260                 for(cptr = dir; *cptr; cptr++)
    261                 {
    262                         /* Convert to forward slash */
    263                         if(*cptr == '\\')
    264                                 *cptr = '/';
    265                 }
    266                 /* Kill eventual trailing '/' */
    267                 if(*(cptr = dir + strlen(dir)-1) == '/')
    268                         *cptr = '\0';
    269 
    270                 /* Add to list */
    271                 nincludepath++;
    272                 includepath = (char **)xrealloc(includepath, nincludepath * sizeof(*includepath));
    273                 includepath[nincludepath-1] = dir;
    274                 tok = strtok(NULL, INCLUDESEPARATOR);
    275         }
    276         free(cpy);
     249    char *tok;
     250    char *cpy = xstrdup(path);
     251
     252    tok = strtok(cpy, INCLUDESEPARATOR);
     253    while(tok)
     254    {
     255        char *dir;
     256        char *cptr;
     257        if(strlen(tok) == 0)
     258            continue;
     259        dir = xstrdup(tok);
     260        for(cptr = dir; *cptr; cptr++)
     261        {
     262            /* Convert to forward slash */
     263            if(*cptr == '\\')
     264                *cptr = '/';
     265        }
     266        /* Kill eventual trailing '/' */
     267        if(*(cptr = dir + strlen(dir)-1) == '/')
     268            *cptr = '\0';
     269
     270        /* Add to list */
     271        nincludepath++;
     272        includepath = (char **)xrealloc(includepath, nincludepath * sizeof(*includepath));
     273        includepath[nincludepath-1] = dir;
     274        tok = strtok(NULL, INCLUDESEPARATOR);
     275    }
     276    free(cpy);
    277277}
    278278
    279279FILE *open_include(const char *name, int search, char **newpath)
    280280{
    281         char *cpy = xstrdup(name);
    282         char *cptr;
    283         FILE *fp;
    284         int i;
    285 
    286         for(cptr = cpy; *cptr; cptr++)
    287         {
    288                 /* kill double backslash */
    289                 if(*cptr == '\\' && *(cptr+1) == '\\')
    290                         memmove(cptr, cptr+1, strlen(cptr));
    291                 /* Convert to forward slash */
    292                 if(*cptr == '\\')
    293                         *cptr = '/';
    294         }
    295 
    296         if(search)
    297         {
    298                 /* Search current dir and then -I path */
     281    char *cpy = xstrdup(name);
     282    char *cptr;
     283    FILE *fp;
     284    int i;
     285
     286    for(cptr = cpy; *cptr; cptr++)
     287    {
     288        /* kill double backslash */
     289        if(*cptr == '\\' && *(cptr+1) == '\\')
     290            memmove(cptr, cptr+1, strlen(cptr));
     291        /* Convert to forward slash */
     292        if(*cptr == '\\')
     293            *cptr = '/';
     294    }
     295
     296    if(search)
     297    {
     298        /* Search current dir and then -I path */
    299299#ifdef __EMX__
    300                 fp = fopen(cpy, "rb");
     300        fp = fopen(cpy, "rb");
    301301#else
    302                 fp = fopen(cpy, "rt");
     302        fp = fopen(cpy, "rt");
    303303#endif
    304                 if(fp)
    305                 {
    306                         if(debuglevel & DEBUGLEVEL_PPMSG)
    307                                 printf("Going to include <%s>\n", name);
    308                         if(newpath)
    309                                 *newpath = cpy;
    310                         else
    311                                 free(cpy);
    312                         return fp;
    313                 }
    314         }
    315         /* Search -I path */
    316         for(i = 0; i < nincludepath; i++)
    317         {
    318                 char *path;
    319                 path = (char *)xmalloc(strlen(includepath[i]) + strlen(cpy) + 2);
    320                 strcpy(path, includepath[i]);
    321                 strcat(path, "/");
    322                 strcat(path, cpy);
     304        if(fp)
     305        {
     306            if(debuglevel & DEBUGLEVEL_PPMSG)
     307                printf("Going to include <%s>\n", name);
     308            if(newpath)
     309                *newpath = cpy;
     310            else
     311                free(cpy);
     312            return fp;
     313        }
     314    }
     315    /* Search -I path */
     316    for(i = 0; i < nincludepath; i++)
     317    {
     318        char *path;
     319        path = (char *)xmalloc(strlen(includepath[i]) + strlen(cpy) + 2);
     320        strcpy(path, includepath[i]);
     321        strcat(path, "/");
     322        strcat(path, cpy);
    323323#ifdef __EMX__
    324                 fp = fopen(path, "rb");
     324        fp = fopen(path, "rb");
    325325#else
    326                 fp = fopen(path, "rt");
     326        fp = fopen(path, "rt");
    327327#endif
    328                 if(fp && (debuglevel & DEBUGLEVEL_PPMSG))
    329                         printf("Going to include <%s>\n", path);
    330                 if(fp)
    331                 {
    332                         if(newpath)
    333                                 *newpath = path;
    334                         else
    335                                 free(path);
    336                         free(cpy);
    337                         return fp;
    338                 }
    339                 free(path);
    340         }
    341         free(cpy);
    342         if(newpath)
    343                 *newpath = NULL;
    344         return NULL;
     328        if(fp && (debuglevel & DEBUGLEVEL_PPMSG))
     329            printf("Going to include <%s>\n", path);
     330        if(fp)
     331        {
     332            if(newpath)
     333                *newpath = path;
     334            else
     335                free(path);
     336            free(cpy);
     337            return fp;
     338        }
     339        free(path);
     340    }
     341    free(cpy);
     342    if(newpath)
     343        *newpath = NULL;
     344    return NULL;
    345345}
    346346
     
    359359 *
    360360 * The states mean:
    361  * if_true      Process input to output
    362  * if_false     Process input but no output
    363  * if_ignore    Process input but no output
    364  * if_elif      Process input but no output
    365  * if_elsefalse Process input but no output
    366  * if_elsettrue Process input to output
     361 * if_true  Process input to output
     362 * if_false Process input but no output
     363 * if_ignore    Process input but no output
     364 * if_elif  Process input but no output
     365 * if_elsefalse Process input but no output
     366 * if_elsettrue Process input to output
    367367 *
    368368 * The possible state-sequences are [state(stack depth)] (rest can be deduced):
    369  *      TOS             #if 1           #else                   #endif
    370  *      if_true(n)      if_true(n+1)    if_elsefalse(n+1)
    371  *      if_false(n)     if_ignore(n+1)  if_ignore(n+1)
    372  *      if_elsetrue(n)  if_true(n+1)    if_elsefalse(n+1)
    373  *      if_elsefalse(n) if_ignore(n+1)  if_ignore(n+1)
    374  *      if_elif(n)      if_ignore(n+1)  if_ignore(n+1)
    375  *      if_ignore(n)    if_ignore(n+1)  if_ignore(n+1)
    376  *
    377  *      TOS             #if 1           #elif 0         #else           #endif
    378  *      if_true(n)      if_true(n+1)    if_elif(n+1)    if_elif(n+1)
    379  *      if_false(n)     if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
    380  *      if_elsetrue(n)  if_true(n+1)    if_elif(n+1)    if_elif(n+1)
    381  *      if_elsefalse(n) if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
    382  *      if_elif(n)      if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
    383  *      if_ignore(n)    if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
    384  *
    385  *      TOS             #if 0           #elif 1         #else           #endif
    386  *      if_true(n)      if_false(n+1)   if_true(n+1)    if_elsefalse(n+1)
    387  *      if_false(n)     if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
    388  *      if_elsetrue(n)  if_false(n+1)   if_true(n+1)    if_elsefalse(n+1)
    389  *      if_elsefalse(n) if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
    390  *      if_elif(n)      if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
    391  *      if_ignore(n)    if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
     369 *  TOS     #if 1       #else           #endif
     370 *  if_true(n)  if_true(n+1)    if_elsefalse(n+1)
     371 *  if_false(n) if_ignore(n+1)  if_ignore(n+1)
     372 *  if_elsetrue(n)  if_true(n+1)    if_elsefalse(n+1)
     373 *  if_elsefalse(n) if_ignore(n+1)  if_ignore(n+1)
     374 *  if_elif(n)  if_ignore(n+1)  if_ignore(n+1)
     375 *  if_ignore(n)    if_ignore(n+1)  if_ignore(n+1)
     376 *
     377 *  TOS     #if 1       #elif 0     #else       #endif
     378 *  if_true(n)  if_true(n+1)    if_elif(n+1)    if_elif(n+1)
     379 *  if_false(n) if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
     380 *  if_elsetrue(n)  if_true(n+1)    if_elif(n+1)    if_elif(n+1)
     381 *  if_elsefalse(n) if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
     382 *  if_elif(n)  if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
     383 *  if_ignore(n)    if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
     384 *
     385 *  TOS     #if 0       #elif 1     #else       #endif
     386 *  if_true(n)  if_false(n+1)   if_true(n+1)    if_elsefalse(n+1)
     387 *  if_false(n) if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
     388 *  if_elsetrue(n)  if_false(n+1)   if_true(n+1)    if_elsefalse(n+1)
     389 *  if_elsefalse(n) if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
     390 *  if_elif(n)  if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
     391 *  if_ignore(n)    if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
    392392 *
    393393 *-------------------------------------------------------------------------
    394394 */
    395395static char *if_state_str[] = {
    396         "if_false",
    397         "if_true",
    398         "if_elif",
    399         "if_elsefalse",
    400         "if_elsetrue",
    401         "if_ignore"
     396    "if_false",
     397    "if_true",
     398    "if_elif",
     399    "if_elsefalse",
     400    "if_elsetrue",
     401    "if_ignore"
    402402};
    403403
    404404void push_if(if_state_t s)
    405405{
    406         if(if_stack_idx >= MAXIFSTACK)
    407                 internal_error(__FILE__, __LINE__, "#if-stack overflow; #{if,ifdef,ifndef} nested too deeply (> %d)", MAXIFSTACK);
    408 
    409         if(debuglevel & DEBUGLEVEL_PPLEX)
    410                 fprintf(stderr, "Push if %s:%d: %s(%d) -> %s(%d)\n", input_name, line_number, if_state_str[if_state()], if_stack_idx, if_state_str[s], if_stack_idx+1);
    411 
    412         if_stack[if_stack_idx++] = s;
    413 
    414         switch(s)
    415         {
    416         case if_true:
    417         case if_elsetrue:
    418                 break;
    419         case if_false:
    420         case if_elsefalse:
    421         case if_elif:
    422         case if_ignore:
    423                 push_ignore_state();
    424                 break;
    425         }
     406    if(if_stack_idx >= MAXIFSTACK)
     407        internal_error(__FILE__, __LINE__, "#if-stack overflow; #{if,ifdef,ifndef} nested too deeply (> %d)", MAXIFSTACK);
     408
     409    if(debuglevel & DEBUGLEVEL_PPLEX)
     410        fprintf(stderr, "Push if %s:%d: %s(%d) -> %s(%d)\n", input_name, line_number, if_state_str[if_state()], if_stack_idx, if_state_str[s], if_stack_idx+1);
     411
     412    if_stack[if_stack_idx++] = s;
     413
     414    switch(s)
     415    {
     416    case if_true:
     417    case if_elsetrue:
     418        break;
     419    case if_false:
     420    case if_elsefalse:
     421    case if_elif:
     422    case if_ignore:
     423        push_ignore_state();
     424        break;
     425    }
    426426}
    427427
    428428if_state_t pop_if(void)
    429429{
    430         if(if_stack_idx <= 0)
    431                 yyerror("#{endif,else,elif} without #{if,ifdef,ifndef} (#if-stack underflow)");
    432 
    433         switch(if_state())
    434         {
    435         case if_true:
    436         case if_elsetrue:
    437                 break;
    438         case if_false:
    439         case if_elsefalse:
    440         case if_elif:
    441         case if_ignore:
    442                 pop_ignore_state();
    443                 break;
    444         }
    445 
    446         if(debuglevel & DEBUGLEVEL_PPLEX)
    447                 fprintf(stderr, "Pop if %s:%d: %s(%d) -> %s(%d)\n",
    448                                 input_name,
    449                                 line_number,
    450                                 if_state_str[if_state()],
    451                                 if_stack_idx,
    452                                 if_state_str[if_stack[if_stack_idx <= 1 ? if_true : if_stack_idx-2]],
    453                                 if_stack_idx-1);
    454 
    455         return if_stack[--if_stack_idx];
     430    if(if_stack_idx <= 0)
     431        yyerror("#{endif,else,elif} without #{if,ifdef,ifndef} (#if-stack underflow)");
     432
     433    switch(if_state())
     434    {
     435    case if_true:
     436    case if_elsetrue:
     437        break;
     438    case if_false:
     439    case if_elsefalse:
     440    case if_elif:
     441    case if_ignore:
     442        pop_ignore_state();
     443        break;
     444    }
     445
     446    if(debuglevel & DEBUGLEVEL_PPLEX)
     447        fprintf(stderr, "Pop if %s:%d: %s(%d) -> %s(%d)\n",
     448                input_name,
     449                line_number,
     450                if_state_str[if_state()],
     451                if_stack_idx,
     452                if_state_str[if_stack[if_stack_idx <= 1 ? if_true : if_stack_idx-2]],
     453                if_stack_idx-1);
     454
     455    return if_stack[--if_stack_idx];
    456456}
    457457
    458458if_state_t if_state(void)
    459459{
    460         if(!if_stack_idx)
    461                 return if_true;
    462         else
    463                 return if_stack[if_stack_idx-1];
     460    if(!if_stack_idx)
     461        return if_true;
     462    else
     463        return if_stack[if_stack_idx-1];
    464464}
    465465
     
    467467void next_if_state(int i)
    468468{
    469         switch(if_state())
    470         {
    471         case if_true:
    472         case if_elsetrue:
    473                 push_if(i ? if_true : if_false);
    474                 break;
    475         case if_false:
    476         case if_elsefalse:
    477         case if_elif:
    478         case if_ignore:
    479                 push_if(if_ignore);
    480                 break;
    481         default:
    482                 internal_error(__FILE__, __LINE__, "Invalid if_state (%d) in #{if,ifdef,ifndef} directive", (int)if_state());
    483         }
     469    switch(if_state())
     470    {
     471    case if_true:
     472    case if_elsetrue:
     473        push_if(i ? if_true : if_false);
     474        break;
     475    case if_false:
     476    case if_elsefalse:
     477    case if_elif:
     478    case if_ignore:
     479        push_if(if_ignore);
     480        break;
     481    default:
     482        internal_error(__FILE__, __LINE__, "Invalid if_state (%d) in #{if,ifdef,ifndef} directive", (int)if_state());
     483    }
    484484}
    485485
    486486int get_if_depth(void)
    487487{
    488         return if_stack_idx;
    489 }
    490 
     488    return if_stack_idx;
     489}
     490
Note: See TracChangeset for help on using the changeset viewer.