Changeset 6112
- Timestamp:
- Jun 26, 2001, 2:12:13 AM (24 years ago)
- Location:
- trunk/tools/wrc
- Files:
-
- 2 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 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 } 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 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); 44 56 #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"); 57 69 } 58 70 … … 60 72 int yyerror(const char *s, ...) 61 73 { 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; 68 80 } 69 81 70 82 int yywarning(const char *s, ...) 71 83 { 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; 77 89 } 78 90 79 91 int pperror(const char *s, ...) 80 92 { 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; 87 99 } 88 100 89 101 int ppwarning(const char *s, ...) 90 102 { 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; 96 108 } 97 109 … … 99 111 void internal_error(const char *file, int line, const char *s, ...) 100 112 { 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); 108 120 } 109 121 110 122 void error(const char *s, ...) 111 123 { 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); 119 131 } 120 132 121 133 void warning(const char *s, ...) 122 134 { 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); 129 141 } 130 142 131 143 void chat(const char *s, ...) 132 144 { 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 } 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 error("Virtual memory exhausted.\n");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 error("Virtual memory exhausted.\n");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 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; 223 252 } 224 253 225 254 char *dupwstr2cstr(const short *str) 226 255 { 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; 236 265 } 237 266 238 267 /* 239 268 ***************************************************************************** 240 * Function : compare_name_id241 * 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 : 246 275 ***************************************************************************** 247 276 */ 248 277 int compare_name_id(name_id_t *n1, name_id_t *n2) 249 278 { 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_char257 && 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_unicode262 && 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 else267 {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 else276 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 */ 280 309 } 281 310 … … 285 314 286 315 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; 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 return stricmp( p1, p2 );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 -
trunk/tools/wrc/utils.h
r5523 r6112 13 13 #endif 14 14 15 #include <stddef.h> /* size_t */15 #include <stddef.h> /* size_t */ 16 16 17 #if defined(__DEBUG_ALLOC__) && defined(__IBMC__) 18 void *_xmalloc(size_t, char*, int); 19 void *_xrealloc(void *, size_t, char*, int); 20 char *_xstrdup(const char *, char*, int); 21 #define xmalloc(a) _xmalloc(a, __FILE__, __LINE__) 22 #define xrealloc(a, b) _xrealloc(a, b, __FILE__, __LINE__) 23 #define xstrdup(a) _xstrdup(a, __FILE__, __LINE__) 24 #else 17 25 void *xmalloc(size_t); 18 26 void *xrealloc(void *, size_t); 19 27 char *xstrdup(const char *str); 28 #endif 20 29 21 30 int pperror(const char *s, ...) __attribute__((format (printf, 1, 2))); … … 35 44 void set_language(unsigned short lang, unsigned short sublang); 36 45 46 #if defined(__IBMC__) || defined(__IBMCPP__) 47 int strcasecmp( char *p1, char *p2 ); 48 49 /* Borrowed from Apache NT Port and PHP */ 50 51 extern char *ap_php_optarg; 52 extern int ap_php_optind; 53 extern int ap_php_opterr; 54 extern int ap_php_optopt; 55 int ap_php_getopt(int argc, char* const *argv, const char *optstr); 56 57 #define getopt ap_php_getopt 58 #define optarg ap_php_optarg 59 #define optind ap_php_optind 60 #define opterr ap_php_opterr 61 #define optopt ap_php_optopt 62 37 63 #endif 64 65 #endif
Note:
See TracChangeset
for help on using the changeset viewer.
