Changeset 6112 for trunk/tools/wrc/utils.c
- Timestamp:
- Jun 26, 2001, 2:12:13 AM (24 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/wrc/utils.c
r5523 r6112 22 22 #include "preproc.h" 23 23 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 24 36 /* #define WANT_NEAR_INDICATION */ 25 37 … … 29 41 void make_print(char *str) 30 42 { 31 32 33 34 35 36 43 while(*str) 44 { 45 if(!isprint(*str)) 46 *str = ' '; 47 str++; 48 } 37 49 } 38 50 #endif … … 40 52 static void generic_msg(const char *s, const char *t, const char *n, va_list ap) 41 53 { 42 43 54 fprintf(stderr, "%s:%d:%d: %s: ", input_name ? input_name : "stdin", line_number, char_number, t); 55 vfprintf(stderr, s, ap); 44 56 #ifdef WANT_NEAR_INDICATION 45 46 47 48 49 50 51 52 53 54 55 #endif 56 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"); 57 69 } 58 70 … … 60 72 int yyerror(const char *s, ...) 61 73 { 62 63 64 65 66 67 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; 68 80 } 69 81 70 82 int yywarning(const char *s, ...) 71 83 { 72 73 74 75 76 84 va_list ap; 85 va_start(ap, s); 86 generic_msg(s, "Warning", yytext, ap); 87 va_end(ap); 88 return 0; 77 89 } 78 90 79 91 int pperror(const char *s, ...) 80 92 { 81 82 83 84 85 86 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; 87 99 } 88 100 89 101 int ppwarning(const char *s, ...) 90 102 { 91 92 93 94 95 103 va_list ap; 104 va_start(ap, s); 105 generic_msg(s, "Warning", pptext, ap); 106 va_end(ap); 107 return 0; 96 108 } 97 109 … … 99 111 void internal_error(const char *file, int line, const char *s, ...) 100 112 { 101 102 103 104 105 106 107 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); 108 120 } 109 121 110 122 void error(const char *s, ...) 111 123 { 112 113 114 115 116 117 118 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); 119 131 } 120 132 121 133 void warning(const char *s, ...) 122 134 { 123 124 125 126 127 128 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); 129 141 } 130 142 131 143 void chat(const char *s, ...) 132 144 { 133 134 135 136 137 138 139 140 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 } 142 154 } 143 155 144 156 char *dup_basename(const char *name, const char *ext) 145 157 { 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__) 183 void *_xmalloc(size_t size, char *pszFile, int iLine) 184 #else 170 185 void *xmalloc(size_t size) 186 #endif 171 187 { 172 188 void *res; … … 176 192 if(res == NULL) 177 193 { 178 194 error("Virtual memory exhausted.\n"); 179 195 } 180 196 /* … … 188 204 189 205 206 207 #if defined(__DEBUG_ALLOC__) && defined(__IBMC__) 208 void *_xrealloc(void *p, size_t size, char *pszFile, int iLine) 209 #else 190 210 void *xrealloc(void *p, size_t size) 211 #endif 191 212 { 192 213 void *res; … … 196 217 if(res == NULL) 197 218 { 198 219 error("Virtual memory exhausted.\n"); 199 220 } 200 221 return res; 201 222 } 202 223 224 #if defined(__DEBUG_ALLOC__) && defined(__IBMC__) 225 char *_xstrdup(const char *str, char *pszFile, int iLine) 226 #else 203 227 char *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); 210 239 } 211 240 212 241 short *dupcstr2wstr(const char *str) 213 242 { 214 215 216 217 218 219 220 221 222 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; 223 252 } 224 253 225 254 char *dupwstr2cstr(const short *str) 226 255 { 227 228 229 230 231 232 233 234 235 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; 236 265 } 237 266 238 267 /* 239 268 ***************************************************************************** 240 * Function 241 * Syntax 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 : 246 275 ***************************************************************************** 247 276 */ 248 277 int compare_name_id(name_id_t *n1, name_id_t *n2) 249 278 { 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 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 */ 280 309 } 281 310 … … 285 314 286 315 if((str->type == str_char) && (type == str_unicode)) 287 288 289 290 291 292 293 294 295 296 297 298 299 { 300 301 302 303 304 } 305 306 { 307 308 309 310 311 } 312 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; 313 342 } 314 343 … … 402 431 } 403 432 404 #if def __EMX__433 #if defined(__WIN32OS2__) 405 434 WCHAR WINAPI toupperW( WCHAR ch ) 406 435 { … … 417 446 int strcasecmp( char *p1, char *p2 ) 418 447 { 419 448 return stricmp( p1, p2 ); 420 449 } 421 450 422 451 int lstrcmpiA( char *p1, char *p2 ) 423 452 { 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.