1 /* 2 * Utility routines 3 * 4 * Copyright 1998 Bertho A. Stultiens 5 * Copyright 2002 Ove Kaaven 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 20 */ 21 22 #include "config.h" 23 #include "wine/port.h" 24 25 #include <assert.h> 26 #include <stdio.h> 27 #include <stdlib.h> 28 #include <stdarg.h> 29 #include <string.h> 30 #include <ctype.h> 31 32 #include "widl.h" 33 #include "utils.h" 34 #include "parser.h" 35 36 #define CURRENT_LOCATION { input_name ? input_name : "stdin", line_number, parser_text } 37 38 static const int want_near_indication = 0; 39 40 static void make_print(char *str) 41 { 42 while(*str) 43 { 44 if(!isprint(*str)) 45 *str = ' '; 46 str++; 47 } 48 } 49 50 static void generic_msg(const loc_info_t *loc_info, const char *s, const char *t, va_list ap) 51 { 52 fprintf(stderr, "%s:%d: %s: ", loc_info->input_name, loc_info->line_number, t); 53 vfprintf(stderr, s, ap); 54 55 if (want_near_indication) 56 { 57 char *cpy; 58 if(loc_info->near_text) 59 { 60 cpy = xstrdup(loc_info->near_text); 61 make_print(cpy); 62 fprintf(stderr, " near '%s'", cpy); 63 free(cpy); 64 } 65 } 66 } 67 68 69 void error_loc(const char *s, ...) 70 { 71 loc_info_t cur_loc = CURRENT_LOCATION; 72 va_list ap; 73 va_start(ap, s); 74 generic_msg(&cur_loc, s, "error", ap); 75 va_end(ap); 76 exit(1); 77 } 78 79 /* yyerror: yacc assumes this is not newline terminated. */ 80 void parser_error(const char *s) 81 { 82 error_loc("%s\n", s); 83 } 84 85 void error_loc_info(const loc_info_t *loc_info, const char *s, ...) 86 { 87 va_list ap; 88 va_start(ap, s); 89 generic_msg(loc_info, s, "error", ap); 90 va_end(ap); 91 exit(1); 92 } 93 94 int parser_warning(const char *s, ...) 95 { 96 loc_info_t cur_loc = CURRENT_LOCATION; 97 va_list ap; 98 va_start(ap, s); 99 generic_msg(&cur_loc, s, "warning", ap); 100 va_end(ap); 101 return 0; 102 } 103 104 void error(const char *s, ...) 105 { 106 va_list ap; 107 va_start(ap, s); 108 fprintf(stderr, "error: "); 109 vfprintf(stderr, s, ap); 110 va_end(ap); 111 exit(2); 112 } 113 114 void warning(const char *s, ...) 115 { 116 va_list ap; 117 va_start(ap, s); 118 fprintf(stderr, "warning: "); 119 vfprintf(stderr, s, ap); 120 va_end(ap); 121 } 122 123 void warning_loc_info(const loc_info_t *loc_info, const char *s, ...) 124 { 125 va_list ap; 126 va_start(ap, s); 127 generic_msg(loc_info, s, "warning", ap); 128 va_end(ap); 129 } 130 131 void chat(const char *s, ...) 132 { 133 if(debuglevel & DEBUGLEVEL_CHAT) 134 { 135 va_list ap; 136 va_start(ap, s); 137 fprintf(stderr, "chat: "); 138 vfprintf(stderr, s, ap); 139 va_end(ap); 140 } 141 } 142 143 char *dup_basename(const char *name, const char *ext) 144 { 145 int namelen; 146 int extlen = strlen(ext); 147 char *base; 148 char *slash; 149 150 if(!name) 151 name = "widl.tab"; 152 153 slash = strrchr(name, '/'); 154 if (!slash) 155 slash = strrchr(name, '\\'); 156 157 if (slash) 158 name = slash + 1; 159 160 namelen = strlen(name); 161 162 /* +6 for later extension (strlen("_r.rgs")) and +1 for '\0' */ 163 base = xmalloc(namelen +6 +1); 164 strcpy(base, name); 165 if(!strcasecmp(name + namelen-extlen, ext)) 166 { 167 base[namelen - extlen] = '\0'; 168 } 169 return base; 170 } 171 172 size_t widl_getline(char **linep, size_t *lenp, FILE *fp) 173 { 174 char *line = *linep; 175 size_t len = *lenp; 176 size_t n = 0; 177 178 if (!line) 179 { 180 len = 64; 181 line = xmalloc(len); 182 } 183 184 while (fgets(&line[n], len - n, fp)) 185 { 186 n += strlen(&line[n]); 187 if (line[n - 1] == '\n') 188 break; 189 else if (n == len - 1) 190 { 191 len *= 2; 192 line = xrealloc(line, len); 193 } 194 } 195 196 *linep = line; 197 *lenp = len; 198 return n; 199 } 200 201 void *xmalloc(size_t size) 202 { 203 void *res; 204 205 assert(size > 0); 206 res = malloc(size); 207 if(res == NULL) 208 { 209 error("Virtual memory exhausted.\n"); 210 } 211 memset(res, 0x55, size); 212 return res; 213 } 214 215 216 void *xrealloc(void *p, size_t size) 217 { 218 void *res; 219 220 assert(size > 0); 221 res = realloc(p, size); 222 if(res == NULL) 223 { 224 error("Virtual memory exhausted.\n"); 225 } 226 return res; 227 } 228 229 char *xstrdup(const char *str) 230 { 231 char *s; 232 233 assert(str != NULL); 234 s = xmalloc(strlen(str)+1); 235 return strcpy(s, str); 236 } 237 238 int strendswith(const char* str, const char* end) 239 { 240 int l = strlen(str); 241 int m = strlen(end); 242 return l >= m && strcmp(str + l - m, end) == 0; 243 } 244 245 /******************************************************************* 246 * buffer management 247 * 248 * Function for writing to a memory buffer. 249 */ 250 251 int byte_swapped = 0; 252 unsigned char *output_buffer; 253 size_t output_buffer_pos; 254 size_t output_buffer_size; 255 256 static struct resource 257 { 258 unsigned char *data; 259 size_t size; 260 } resources[16]; 261 static unsigned int nb_resources; 262 263 static void check_output_buffer_space( size_t size ) 264 { 265 if (output_buffer_pos + size >= output_buffer_size) 266 { 267 output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size ); 268 output_buffer = xrealloc( output_buffer, output_buffer_size ); 269 } 270 } 271 272 void init_output_buffer(void) 273 { 274 output_buffer_size = 1024; 275 output_buffer_pos = 0; 276 output_buffer = xmalloc( output_buffer_size ); 277 } 278 279 void flush_output_buffer( const char *name ) 280 { 281 int fd = open( name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666 ); 282 if (fd == -1) error( "Error creating %s\n", name ); 283 if (write( fd, output_buffer, output_buffer_pos ) != output_buffer_pos) 284 error( "Error writing to %s\n", name ); 285 close( fd ); 286 free( output_buffer ); 287 } 288 289 static inline void put_resource_id( const char *str ) 290 { 291 if (str[0] != '#') 292 { 293 while (*str) 294 { 295 unsigned char ch = *str++; 296 put_word( toupper(ch) ); 297 } 298 put_word( 0 ); 299 } 300 else 301 { 302 put_word( 0xffff ); 303 put_word( atoi( str + 1 )); 304 } 305 } 306 307 void add_output_to_resources( const char *type, const char *name ) 308 { 309 size_t data_size = output_buffer_pos; 310 size_t header_size = 5 * sizeof(unsigned int) + 2 * sizeof(unsigned short); 311 312 assert( nb_resources < sizeof(resources)/sizeof(resources[0]) ); 313 314 if (type[0] != '#') header_size += (strlen( type ) + 1) * sizeof(unsigned short); 315 else header_size += 2 * sizeof(unsigned short); 316 if (name[0] != '#') header_size += (strlen( name ) + 1) * sizeof(unsigned short); 317 else header_size += 2 * sizeof(unsigned short); 318 319 header_size = (header_size + 3) & ~3; 320 align_output( 4 ); 321 check_output_buffer_space( header_size ); 322 resources[nb_resources].size = header_size + output_buffer_pos; 323 memmove( output_buffer + header_size, output_buffer, output_buffer_pos ); 324 325 output_buffer_pos = 0; 326 put_dword( data_size ); /* ResSize */ 327 put_dword( header_size ); /* HeaderSize */ 328 put_resource_id( type ); /* ResType */ 329 put_resource_id( name ); /* ResName */ 330 align_output( 4 ); 331 put_dword( 0 ); /* DataVersion */ 332 put_word( 0 ); /* Memory options */ 333 put_word( 0 ); /* Language */ 334 put_dword( 0 ); /* Version */ 335 put_dword( 0 ); /* Characteristics */ 336 337 resources[nb_resources++].data = output_buffer; 338 init_output_buffer(); 339 } 340 341 void flush_output_resources( const char *name ) 342 { 343 int fd; 344 unsigned int i; 345 346 /* all output must have been saved with add_output_to_resources() first */ 347 assert( !output_buffer_pos ); 348 349 put_dword( 0 ); /* ResSize */ 350 put_dword( 32 ); /* HeaderSize */ 351 put_word( 0xffff ); /* ResType */ 352 put_word( 0x0000 ); 353 put_word( 0xffff ); /* ResName */ 354 put_word( 0x0000 ); 355 put_dword( 0 ); /* DataVersion */ 356 put_word( 0 ); /* Memory options */ 357 put_word( 0 ); /* Language */ 358 put_dword( 0 ); /* Version */ 359 put_dword( 0 ); /* Characteristics */ 360 361 fd = open( name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666 ); 362 if (fd == -1) error( "Error creating %s\n", name ); 363 if (write( fd, output_buffer, output_buffer_pos ) != output_buffer_pos) 364 error( "Error writing to %s\n", name ); 365 for (i = 0; i < nb_resources; i++) 366 { 367 if (write( fd, resources[i].data, resources[i].size ) != resources[i].size) 368 error( "Error writing to %s\n", name ); 369 free( resources[i].data ); 370 } 371 close( fd ); 372 nb_resources = 0; 373 free( output_buffer ); 374 } 375 376 void put_data( const void *data, size_t size ) 377 { 378 check_output_buffer_space( size ); 379 memcpy( output_buffer + output_buffer_pos, data, size ); 380 output_buffer_pos += size; 381 } 382 383 void put_byte( unsigned char val ) 384 { 385 check_output_buffer_space( 1 ); 386 output_buffer[output_buffer_pos++] = val; 387 } 388 389 void put_word( unsigned short val ) 390 { 391 if (byte_swapped) val = (val << 8) | (val >> 8); 392 put_data( &val, sizeof(val) ); 393 } 394 395 void put_dword( unsigned int val ) 396 { 397 if (byte_swapped) 398 val = ((val << 24) | ((val << 8) & 0x00ff0000) | ((val >> 8) & 0x0000ff00) | (val >> 24)); 399 put_data( &val, sizeof(val) ); 400 } 401 402 void put_qword( unsigned int val ) 403 { 404 if (byte_swapped) 405 { 406 put_dword( 0 ); 407 put_dword( val ); 408 } 409 else 410 { 411 put_dword( val ); 412 put_dword( 0 ); 413 } 414 } 415 416 /* pointer-sized word */ 417 void put_pword( unsigned int val ) 418 { 419 if (pointer_size == 8) put_qword( val ); 420 else put_dword( val ); 421 } 422 423 void put_str( int indent, const char *format, ... ) 424 { 425 int n; 426 va_list args; 427 428 check_output_buffer_space( 4 * indent ); 429 memset( output_buffer + output_buffer_pos, ' ', 4 * indent ); 430 output_buffer_pos += 4 * indent; 431 432 for (;;) 433 { 434 size_t size = output_buffer_size - output_buffer_pos; 435 va_start( args, format ); 436 n = vsnprintf( (char *)output_buffer + output_buffer_pos, size, format, args ); 437 va_end( args ); 438 if (n == -1) size *= 2; 439 else if ((size_t)n >= size) size = n + 1; 440 else 441 { 442 output_buffer_pos += n; 443 return; 444 } 445 check_output_buffer_space( size ); 446 } 447 } 448 449 void align_output( unsigned int align ) 450 { 451 size_t size = align - (output_buffer_pos % align); 452 453 if (size == align) return; 454 check_output_buffer_space( size ); 455 memset( output_buffer + output_buffer_pos, 0, size ); 456 output_buffer_pos += size; 457 } 458