4 * Copyright 1998,2000 Bertho A. Stultiens
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
35 #define SUPPRESS_YACC_ERROR_MESSAGE
37 static void generic_msg(const char *s
, const char *t
, va_list ap
)
39 fprintf(stderr
, "%s:%d:%d: %s: ", input_name
? input_name
: "stdin", line_number
, char_number
, t
);
40 vfprintf(stderr
, s
, ap
);
44 * The yyerror routine should not exit because we use the error-token
45 * to determine the syntactic error in the source. However, YACC
46 * uses the same routine to print an error just before the error
48 * The extra routine 'xyyerror' is used to exit after giving a real
51 int mcy_error(const char *s
, ...)
53 #ifndef SUPPRESS_YACC_ERROR_MESSAGE
56 generic_msg(s
, "Yacc error", ap
);
62 int xyyerror(const char *s
, ...)
66 generic_msg(s
, "Error", ap
);
72 int mcy_warning(const char *s
, ...)
76 generic_msg(s
, "Warning", ap
);
81 void internal_error(const char *file
, int line
, const char *s
, ...)
85 fprintf(stderr
, "Internal error (please report) %s %d: ", file
, line
);
86 vfprintf(stderr
, s
, ap
);
91 void fatal_perror( const char *msg
, ... )
94 va_start( valist
, msg
);
95 fprintf(stderr
, "Error: ");
96 vfprintf( stderr
, msg
, valist
);
102 void error(const char *s
, ...)
106 fprintf(stderr
, "Error: ");
107 vfprintf(stderr
, s
, ap
);
112 void warning(const char *s
, ...)
116 fprintf(stderr
, "Warning: ");
117 vfprintf(stderr
, s
, ap
);
121 char *dup_basename(const char *name
, const char *ext
)
124 int extlen
= strlen(ext
);
131 slash
= strrchr(name
, '/');
135 namelen
= strlen(name
);
137 /* +4 for later extension and +1 for '\0' */
138 base
= xmalloc(namelen
+4 +1);
140 if(!strcasecmp(name
+ namelen
-extlen
, ext
))
142 base
[namelen
- extlen
] = '\0';
147 void *xmalloc(size_t size
)
155 error("Virtual memory exhausted.\n");
157 memset(res
, 0x55, size
);
162 void *xrealloc(void *p
, size_t size
)
167 res
= realloc(p
, size
);
170 error("Virtual memory exhausted.\n");
175 char *xstrdup(const char *str
)
180 s
= xmalloc(strlen(str
)+1);
181 return strcpy(s
, str
);
184 char *strmake( const char* fmt
, ... )
192 char *p
= xmalloc( size
);
194 n
= vsnprintf( p
, size
, fmt
, ap
);
196 if (n
== -1) size
*= 2;
197 else if ((size_t)n
>= size
) size
= n
+ 1;
203 int unistrlen(const WCHAR
*s
)
206 for(n
= 0; *s
; n
++, s
++)
211 WCHAR
*unistrcpy(WCHAR
*dst
, const WCHAR
*src
)
220 WCHAR
*xunistrdup(const WCHAR
* str
)
225 s
= xmalloc((unistrlen(str
)+1) * sizeof(WCHAR
));
226 return unistrcpy(s
, str
);
229 int unistricmp(const WCHAR
*s1
, const WCHAR
*s2
)
233 static const char warn
[] = "Don't know the uppercase equivalent of non ascii characters;"
234 "comparison might yield wrong results";
237 if((*s1
& 0xffff) > 0x7f || (*s2
& 0xffff) > 0x7f)
247 i
= toupper(*s1
++) - toupper(*s2
++);
252 if((*s1
& 0xffff) > 0x7f || (*s2
& 0xffff) > 0x7f)
259 return toupper(*s1
) - toupper(*s2
);
262 int unistrcmp(const WCHAR
*s1
, const WCHAR
*s2
)
275 /*******************************************************************
278 * Function for writing to a memory buffer.
281 int byte_swapped
= 0;
282 unsigned char *output_buffer
;
283 size_t output_buffer_pos
;
284 size_t output_buffer_size
;
286 static void check_output_buffer_space( size_t size
)
288 if (output_buffer_pos
+ size
>= output_buffer_size
)
290 output_buffer_size
= max( output_buffer_size
* 2, output_buffer_pos
+ size
);
291 output_buffer
= xrealloc( output_buffer
, output_buffer_size
);
295 void init_output_buffer(void)
297 output_buffer_size
= 1024;
298 output_buffer_pos
= 0;
299 output_buffer
= xmalloc( output_buffer_size
);
302 void flush_output_buffer( const char *name
)
304 int fd
= open( name
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666 );
305 if (fd
== -1) error( "Error creating %s\n", name
);
306 if (write( fd
, output_buffer
, output_buffer_pos
) != output_buffer_pos
)
307 error( "Error writing to %s\n", name
);
309 free( output_buffer
);
312 void put_data( const void *data
, size_t size
)
314 check_output_buffer_space( size
);
315 memcpy( output_buffer
+ output_buffer_pos
, data
, size
);
316 output_buffer_pos
+= size
;
319 void put_byte( unsigned char val
)
321 check_output_buffer_space( 1 );
322 output_buffer
[output_buffer_pos
++] = val
;
325 void put_word( unsigned short val
)
327 if (byte_swapped
) val
= (val
<< 8) | (val
>> 8);
328 put_data( &val
, sizeof(val
) );
331 void put_dword( unsigned int val
)
334 val
= ((val
<< 24) | ((val
<< 8) & 0x00ff0000) | ((val
>> 8) & 0x0000ff00) | (val
>> 24));
335 put_data( &val
, sizeof(val
) );
338 void align_output( unsigned int align
)
340 size_t size
= align
- (output_buffer_pos
% align
);
342 if (size
== align
) return;
343 check_output_buffer_space( size
);
344 memset( output_buffer
+ output_buffer_pos
, 0, size
);
345 output_buffer_pos
+= size
;