Ignore:
Timestamp:
Jun 26, 2001, 2:12:13 AM (24 years ago)
Author:
bird
Message:

Added memory debugging stuff. EMX -> WIN32OS2. Note -Tm+ makes it *very* slow!! Delcared strcasecmp and getopt stuff for VAC.

File:
1 edited

Legend:

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

    r5523 r6112  
    2222#include "preproc.h"
    2323
     24
     25/*
     26 * Ease debugging on OS/2.
     27 */
     28#if defined(__DEBUG_ALLOC__) && defined(__IBMC__)
     29#undef malloc
     30#define malloc(a)       _debug_malloc(a, pszFile, iLine)
     31#undef realloc
     32#define realloc(a,b)    _debug_realloc(a, b, pszFile, iLine)
     33#endif
     34
     35
    2436/* #define WANT_NEAR_INDICATION */
    2537
     
    2941void make_print(char *str)
    3042{
    31         while(*str)
    32         {
    33                 if(!isprint(*str))
    34                         *str = ' ';
    35                 str++;
    36         }
     43    while(*str)
     44    {
     45        if(!isprint(*str))
     46            *str = ' ';
     47        str++;
     48    }
    3749}
    3850#endif
     
    4052static void generic_msg(const char *s, const char *t, const char *n, va_list ap)
    4153{
    42         fprintf(stderr, "%s:%d:%d: %s: ", input_name ? input_name : "stdin", line_number, char_number, t);
    43         vfprintf(stderr, s, ap);
     54    fprintf(stderr, "%s:%d:%d: %s: ", input_name ? input_name : "stdin", line_number, char_number, t);
     55    vfprintf(stderr, s, ap);
    4456#ifdef WANT_NEAR_INDICATION
    45         {
    46                 char *cpy;
    47                 if(n)
    48                 {
    49                         cpy = xstrdup(n);
    50                         make_print(cpy);
    51                         fprintf(stderr, " near '%s'", cpy);
    52                         free(cpy);
    53                 }
    54         }
    55 #endif
    56         fprintf(stderr, "\n");
     57    {
     58        char *cpy;
     59        if(n)
     60        {
     61            cpy = xstrdup(n);
     62            make_print(cpy);
     63            fprintf(stderr, " near '%s'", cpy);
     64            free(cpy);
     65        }
     66    }
     67#endif
     68    fprintf(stderr, "\n");
    5769}
    5870
     
    6072int yyerror(const char *s, ...)
    6173{
    62         va_list ap;
    63         va_start(ap, s);
    64         generic_msg(s, "Error", yytext, ap);
    65         va_end(ap);
    66         exit(1);
    67         return 1;
     74    va_list ap;
     75    va_start(ap, s);
     76    generic_msg(s, "Error", yytext, ap);
     77    va_end(ap);
     78    exit(1);
     79    return 1;
    6880}
    6981
    7082int yywarning(const char *s, ...)
    7183{
    72         va_list ap;
    73         va_start(ap, s);
    74         generic_msg(s, "Warning", yytext, ap);
    75         va_end(ap);
    76         return 0;
     84    va_list ap;
     85    va_start(ap, s);
     86    generic_msg(s, "Warning", yytext, ap);
     87    va_end(ap);
     88    return 0;
    7789}
    7890
    7991int pperror(const char *s, ...)
    8092{
    81         va_list ap;
    82         va_start(ap, s);
    83         generic_msg(s, "Error", pptext, ap);
    84         va_end(ap);
    85         exit(1);
    86         return 1;
     93    va_list ap;
     94    va_start(ap, s);
     95    generic_msg(s, "Error", pptext, ap);
     96    va_end(ap);
     97    exit(1);
     98    return 1;
    8799}
    88100
    89101int ppwarning(const char *s, ...)
    90102{
    91         va_list ap;
    92         va_start(ap, s);
    93         generic_msg(s, "Warning", pptext, ap);
    94         va_end(ap);
    95         return 0;
     103    va_list ap;
     104    va_start(ap, s);
     105    generic_msg(s, "Warning", pptext, ap);
     106    va_end(ap);
     107    return 0;
    96108}
    97109
     
    99111void internal_error(const char *file, int line, const char *s, ...)
    100112{
    101         va_list ap;
    102         va_start(ap, s);
    103         fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
    104         vfprintf(stderr, s, ap);
    105         fprintf(stderr, "\n");
    106         va_end(ap);
    107         exit(3);
     113    va_list ap;
     114    va_start(ap, s);
     115    fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
     116    vfprintf(stderr, s, ap);
     117    fprintf(stderr, "\n");
     118    va_end(ap);
     119    exit(3);
    108120}
    109121
    110122void error(const char *s, ...)
    111123{
    112         va_list ap;
    113         va_start(ap, s);
    114         fprintf(stderr, "Error: ");
    115         vfprintf(stderr, s, ap);
    116         fprintf(stderr, "\n");
    117         va_end(ap);
    118         exit(2);
     124    va_list ap;
     125    va_start(ap, s);
     126    fprintf(stderr, "Error: ");
     127    vfprintf(stderr, s, ap);
     128    fprintf(stderr, "\n");
     129    va_end(ap);
     130    exit(2);
    119131}
    120132
    121133void warning(const char *s, ...)
    122134{
    123         va_list ap;
    124         va_start(ap, s);
    125         fprintf(stderr, "Warning: ");
    126         vfprintf(stderr, s, ap);
    127         fprintf(stderr, "\n");
    128         va_end(ap);
     135    va_list ap;
     136    va_start(ap, s);
     137    fprintf(stderr, "Warning: ");
     138    vfprintf(stderr, s, ap);
     139    fprintf(stderr, "\n");
     140    va_end(ap);
    129141}
    130142
    131143void chat(const char *s, ...)
    132144{
    133         if(debuglevel & DEBUGLEVEL_CHAT)
    134         {
    135                 va_list ap;
    136                 va_start(ap, s);
    137                 fprintf(stderr, "FYI: ");
    138                 vfprintf(stderr, s, ap);
    139                 fprintf(stderr, "\n");
    140                 va_end(ap);
    141         }
     145    if(debuglevel & DEBUGLEVEL_CHAT)
     146    {
     147        va_list ap;
     148        va_start(ap, s);
     149        fprintf(stderr, "FYI: ");
     150        vfprintf(stderr, s, ap);
     151        fprintf(stderr, "\n");
     152        va_end(ap);
     153    }
    142154}
    143155
    144156char *dup_basename(const char *name, const char *ext)
    145157{
    146         int namelen;
    147         int extlen = strlen(ext);
    148         char *base;
    149         char *slash;
    150 
    151         if(!name)
    152                 name = "wrc.tab";
    153 
    154         slash = strrchr(name, '/');
    155         if (slash)
    156                 name = slash + 1;
    157 
    158         namelen = strlen(name);
    159 
    160         /* +4 for later extension and +1 for '\0' */
    161         base = (char *)xmalloc(namelen +4 +1);
    162         strcpy(base, name);
    163         if(!strcasecmp(name + namelen-extlen, ext))
    164         {
    165                 base[namelen - extlen] = '\0';
    166         }
    167         return base;
    168 }
    169 
     158    int namelen;
     159    int extlen = strlen(ext);
     160    char *base;
     161    char *slash;
     162
     163    if(!name)
     164        name = "wrc.tab";
     165
     166    slash = strrchr(name, '/');
     167    if (slash)
     168        name = slash + 1;
     169
     170    namelen = strlen(name);
     171
     172    /* +4 for later extension and +1 for '\0' */
     173    base = (char *)xmalloc(namelen +4 +1);
     174    strcpy(base, name);
     175    if(!strcasecmp(name + namelen-extlen, ext))
     176    {
     177        base[namelen - extlen] = '\0';
     178    }
     179    return base;
     180}
     181
     182#if defined(__DEBUG_ALLOC__) && defined(__IBMC__)
     183void *_xmalloc(size_t size, char *pszFile, int iLine)
     184#else
    170185void *xmalloc(size_t size)
     186#endif
    171187{
    172188    void *res;
     
    176192    if(res == NULL)
    177193    {
    178         error("Virtual memory exhausted.\n");
     194    error("Virtual memory exhausted.\n");
    179195    }
    180196    /*
     
    188204
    189205
     206
     207#if defined(__DEBUG_ALLOC__) && defined(__IBMC__)
     208void *_xrealloc(void *p, size_t size, char *pszFile, int iLine)
     209#else
    190210void *xrealloc(void *p, size_t size)
     211#endif
    191212{
    192213    void *res;
     
    196217    if(res == NULL)
    197218    {
    198         error("Virtual memory exhausted.\n");
     219    error("Virtual memory exhausted.\n");
    199220    }
    200221    return res;
    201222}
    202223
     224#if defined(__DEBUG_ALLOC__) && defined(__IBMC__)
     225char *_xstrdup(const char *str, char *pszFile, int iLine)
     226#else
    203227char *xstrdup(const char *str)
    204 {
    205         char *s;
    206 
    207         assert(str != NULL);
    208         s = (char *)xmalloc(strlen(str)+1);
    209         return strcpy(s, str);
     228#endif
     229{
     230    char *s;
     231
     232    assert(str != NULL);
     233    #if defined(__DEBUG_ALLOC__) && defined(__IBMC__)
     234        s = (char *)_xmalloc(strlen(str)+1, pszFile, iLine);
     235    #else
     236        s = (char *)xmalloc(strlen(str)+1);
     237    #endif
     238    return strcpy(s, str);
    210239}
    211240
    212241short *dupcstr2wstr(const char *str)
    213242{
    214         int len;
    215         WCHAR *ws;
    216 
    217         if (!current_codepage) set_language( LANG_NEUTRAL, SUBLANG_NEUTRAL );
    218         len = cp_mbstowcs( current_codepage, 0, str, strlen(str), NULL, 0 );
    219         ws = xmalloc( sizeof(WCHAR) * (len + 1) );
    220         len = cp_mbstowcs( current_codepage, 0, str, strlen(str), ws, len );
    221         ws[len] = 0;
    222         return ws;
     243    int len;
     244    WCHAR *ws;
     245
     246    if (!current_codepage) set_language( LANG_NEUTRAL, SUBLANG_NEUTRAL );
     247    len = cp_mbstowcs( current_codepage, 0, str, strlen(str), NULL, 0 );
     248    ws = xmalloc( sizeof(WCHAR) * (len + 1) );
     249    len = cp_mbstowcs( current_codepage, 0, str, strlen(str), ws, len );
     250    ws[len] = 0;
     251    return ws;
    223252}
    224253
    225254char *dupwstr2cstr(const short *str)
    226255{
    227         int len;
    228         char *cs;
    229 
    230         if (!current_codepage) set_language( LANG_NEUTRAL, SUBLANG_NEUTRAL );
    231         len = cp_wcstombs( current_codepage, 0, str, strlenW(str), NULL, 0, NULL, NULL );
    232         cs = xmalloc( len + 1 );
    233         len = cp_wcstombs( current_codepage, 0, str, strlenW(str),  cs, len, NULL, NULL );
    234         cs[len] = 0;
    235         return cs;
     256    int len;
     257    char *cs;
     258
     259    if (!current_codepage) set_language( LANG_NEUTRAL, SUBLANG_NEUTRAL );
     260    len = cp_wcstombs( current_codepage, 0, str, strlenW(str), NULL, 0, NULL, NULL );
     261    cs = xmalloc( len + 1 );
     262    len = cp_wcstombs( current_codepage, 0, str, strlenW(str),  cs, len, NULL, NULL );
     263    cs[len] = 0;
     264    return cs;
    236265}
    237266
    238267/*
    239268 *****************************************************************************
    240  * Function     : compare_name_id
    241  * Syntax       : int compare_name_id(name_id_t *n1, name_id_t *n2)
    242  * Input        :
    243  * Output       :
    244  * Description  :
    245  * Remarks      :
     269 * Function : compare_name_id
     270 * Syntax   : int compare_name_id(name_id_t *n1, name_id_t *n2)
     271 * Input    :
     272 * Output   :
     273 * Description  :
     274 * Remarks  :
    246275 *****************************************************************************
    247276*/
    248277int compare_name_id(name_id_t *n1, name_id_t *n2)
    249278{
    250         if(n1->type == name_ord && n2->type == name_ord)
    251         {
    252                 return n1->name.i_name - n2->name.i_name;
    253         }
    254         else if(n1->type == name_str && n2->type == name_str)
    255         {
    256                 if(n1->name.s_name->type == str_char
    257                 && n2->name.s_name->type == str_char)
    258                 {
    259                         return strcasecmp(n1->name.s_name->str.cstr, n2->name.s_name->str.cstr);
    260                 }
    261                 else if(n1->name.s_name->type == str_unicode
    262                 && n2->name.s_name->type == str_unicode)
    263                 {
    264                         return strcmpiW(n1->name.s_name->str.wstr, n2->name.s_name->str.wstr);
    265                 }
    266                 else
    267                 {
    268                         internal_error(__FILE__, __LINE__, "Can't yet compare strings of mixed type");
    269                 }
    270         }
    271         else if(n1->type == name_ord && n2->type == name_str)
    272                 return 1;
    273         else if(n1->type == name_str && n2->type == name_ord)
    274                 return -1;
    275         else
    276                 internal_error(__FILE__, __LINE__, "Comparing name-ids with unknown types (%d, %d)",
    277                                 n1->type, n2->type);
    278 
    279         return 0; /* Keep the compiler happy */
     279    if(n1->type == name_ord && n2->type == name_ord)
     280    {
     281        return n1->name.i_name - n2->name.i_name;
     282    }
     283    else if(n1->type == name_str && n2->type == name_str)
     284    {
     285        if(n1->name.s_name->type == str_char
     286        && n2->name.s_name->type == str_char)
     287        {
     288            return strcasecmp(n1->name.s_name->str.cstr, n2->name.s_name->str.cstr);
     289        }
     290        else if(n1->name.s_name->type == str_unicode
     291        && n2->name.s_name->type == str_unicode)
     292        {
     293            return strcmpiW(n1->name.s_name->str.wstr, n2->name.s_name->str.wstr);
     294        }
     295        else
     296        {
     297            internal_error(__FILE__, __LINE__, "Can't yet compare strings of mixed type");
     298        }
     299    }
     300    else if(n1->type == name_ord && n2->type == name_str)
     301        return 1;
     302    else if(n1->type == name_str && n2->type == name_ord)
     303        return -1;
     304    else
     305        internal_error(__FILE__, __LINE__, "Comparing name-ids with unknown types (%d, %d)",
     306                n1->type, n2->type);
     307
     308    return 0; /* Keep the compiler happy */
    280309}
    281310
     
    285314
    286315        if((str->type == str_char) && (type == str_unicode))
    287         {
    288                 ret->str.wstr = dupcstr2wstr(str->str.cstr);
    289                 ret->type     = str_unicode;
    290                 ret->size     = strlenW(ret->str.wstr);
    291         }
    292         else if((str->type == str_unicode) && (type == str_char))
    293         {
    294                 ret->str.cstr = dupwstr2cstr(str->str.wstr);
    295                 ret->type     = str_char;
    296                 ret->size     = strlen(ret->str.cstr);
    297         }
    298         else if(str->type == str_unicode)
    299         {
    300                 ret->type     = str_unicode;
    301                 ret->size     = strlenW(str->str.wstr);
    302                 ret->str.wstr = xmalloc(sizeof(WCHAR)*(ret->size+1));
    303                 strcpyW(ret->str.wstr, str->str.wstr);
    304         } 
    305         else /* str->type == str_char */
    306         {
    307                 ret->type     = str_char;
    308                 ret->size     = strlen(str->str.cstr);
    309                 ret->str.cstr = xmalloc( ret->size + 1 );
    310                 strcpy(ret->str.cstr, str->str.cstr);
    311         }
    312         return ret;
     316    {
     317        ret->str.wstr = dupcstr2wstr(str->str.cstr);
     318        ret->type     = str_unicode;
     319        ret->size     = strlenW(ret->str.wstr);
     320    }
     321    else if((str->type == str_unicode) && (type == str_char))
     322    {
     323            ret->str.cstr = dupwstr2cstr(str->str.wstr);
     324            ret->type     = str_char;
     325            ret->size     = strlen(ret->str.cstr);
     326    }
     327    else if(str->type == str_unicode)
     328        {
     329            ret->type     = str_unicode;
     330        ret->size     = strlenW(str->str.wstr);
     331        ret->str.wstr = xmalloc(sizeof(WCHAR)*(ret->size+1));
     332        strcpyW(ret->str.wstr, str->str.wstr);
     333    }
     334    else /* str->type == str_char */
     335        {
     336            ret->type     = str_char;
     337        ret->size     = strlen(str->str.cstr);
     338        ret->str.cstr = xmalloc( ret->size + 1 );
     339        strcpy(ret->str.cstr, str->str.cstr);
     340        }
     341    return ret;
    313342}
    314343
     
    402431}
    403432
    404 #ifdef __EMX__
     433#if defined(__WIN32OS2__)
    405434WCHAR WINAPI toupperW( WCHAR ch )
    406435{
     
    417446int strcasecmp( char *p1, char *p2 )
    418447{
    419         return stricmp( p1, p2 );
     448    return stricmp( p1, p2 );
    420449}
    421450
    422451int lstrcmpiA( char *p1, char *p2 )
    423452{
    424         return stricmp( p1, p2 );
    425 }
    426 #endif
     453    return stricmp( p1, p2 );
     454}
     455
     456#endif
Note: See TracChangeset for help on using the changeset viewer.